etcher/lib/image-stream/archive.js
Juan Cruz Viotti 6c8bc117ab chore: revise ESLint built-in configuration (#1149)
There are a lot of new rules since the last time I revised the ESLint
rules documentation.

I've updated the main `.eslintrc.yml` to include some newer additions,
plus I added another ESLint configuration file inside `tests`, so we can
add some stricted rules to the production code while relaxing them for
the test suite (due to the fact that Mocha is not very ES6 friendly and
Angular tests require a bit of dark magic to setup).

This is a summary of the most important changes:

- Disallow "magic numbers"

These should now be extracted to constants, which forces us to think of
a good name for them, and thus make the code more self-documenting (I
had to Google up the meaning of some existing magic numbers, so I guess
this will be great for readability purposes).

- Require consistent `return` statements

Some functions relied on JavaScript relaxed casting mechanism to work,
which now have explicit return values. This flag also helped me detect
some promises that were not being returned, and therefore risked not
being caught by the exception handlers in case of errors.

- Disallow redefining function arguments

Immutability makes functions easier to reason about.

- Enforce JavaScript string templates instead of string concatenation

We were heavily mixing boths across the codebase.

There are some extra rules that I tweaked, however most of codebase
changes in this commit are related to the rules mentioned above.

Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
2017-03-07 23:46:44 -04:00

209 lines
5.9 KiB
JavaScript

/*
* Copyright 2016 resin.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const path = require('path');
const Bluebird = require('bluebird');
const rindle = require('rindle');
const _ = require('lodash');
const PassThroughStream = require('stream').PassThrough;
const supportedFileTypes = require('./supported');
/**
* @summary Archive metadata base path
* @constant
* @private
* @type {String}
*/
const ARCHIVE_METADATA_BASE_PATH = '.meta';
/**
* @summary Image extensions
* @constant
* @private
* @type {String[]}
*/
const IMAGE_EXTENSIONS = _.reduce(supportedFileTypes, (accumulator, file) => {
if (file.type === 'image') {
accumulator.push(file.extension);
}
return accumulator;
}, []);
/**
* @summary Extract entry by path
* @function
* @private
*
* @param {String} archive - archive
* @param {String} filePath - entry file path
* @param {Object} options - options
* @param {Object} options.hooks - archive hooks
* @param {Object[]} options.entries - archive entries
* @param {*} [options.default] - entry default value
* @fulfil {*} contents
* @returns {Promise}
*
* extractEntryByPath('my/archive.zip', '_info/logo.svg', {
* hooks: { ... },
* entries: [ ... ],
* default: ''
* }).then((contents) => {
* console.log(contents);
* });
*/
const extractEntryByPath = (archive, filePath, options) => {
const fileEntry = _.find(options.entries, (entry) => {
return _.chain(entry.name)
.split('/')
.tail()
.join('/')
.value() === filePath;
});
if (!fileEntry) {
return Bluebird.resolve(options.default);
}
return options.hooks.extractFile(archive, options.entries, fileEntry.name)
.then(rindle.extract);
};
/**
* @summary Extract archive metadata
* @function
* @private
*
* @param {String} archive - archive
* @param {String} basePath - metadata base path
* @param {Object} options - options
* @param {Object[]} options.entries - archive entries
* @param {Object} options.hooks - archive hooks
* @fulfil {Object} - metadata
* @returns {Promise}
*
* extractArchiveMetadata('my/archive.zip', '.meta', {
* hooks: { ... },
* entries: [ ... ]
* }).then((metadata) => {
* console.log(metadata);
* });
*/
const extractArchiveMetadata = (archive, basePath, options) => {
return Bluebird.props({
logo: extractEntryByPath(archive, `${basePath}/logo.svg`, options),
instructions: extractEntryByPath(archive, `${basePath}/instructions.markdown`, options),
bmap: extractEntryByPath(archive, `${basePath}/image.bmap`, options),
manifest: _.attempt(() => {
return extractEntryByPath(archive, `${basePath}/manifest.json`, {
hooks: options.hooks,
entries: options.entries,
default: '{}'
}).then((manifest) => {
try {
return JSON.parse(manifest);
} catch (parseError) {
const error = new Error('Invalid archive manifest.json');
error.description = 'The archive manifest.json file is not valid JSON.';
throw error;
}
});
})
}).then((results) => {
return {
name: results.manifest.name,
version: results.manifest.version,
url: results.manifest.url,
supportUrl: results.manifest.supportUrl,
releaseNotesUrl: results.manifest.releaseNotesUrl,
checksumType: results.manifest.checksumType,
checksum: results.manifest.checksum,
bytesToZeroOutFromTheBeginning: results.manifest.bytesToZeroOutFromTheBeginning,
recommendedDriveSize: results.manifest.recommendedDriveSize,
logo: results.logo,
bmap: results.bmap,
instructions: results.instructions
};
});
};
/**
* @summary Extract image from archive
* @function
* @public
*
* @param {String} archive - archive path
* @param {Object} hooks - archive hooks
* @param {Function} hooks.getEntries - get entries hook
* @param {Function} hooks.extractFile - extract file hook
* @fulfil {Object} image metadata
* @returns {Promise}
*
* @example
* archive.extractImage('path/to/my/archive.zip', {
* getEntries: (archive) => {
* return [ ..., ..., ... ];
* },
* extractFile: (archive, entries, file) => {
* ...
* }
* }).then((image) => {
* image.stream.pipe(image.transform).pipe(...);
* });
*/
exports.extractImage = (archive, hooks) => {
return hooks.getEntries(archive).then((entries) => {
const imageEntries = _.filter(entries, (entry) => {
const extension = _.replace(path.extname(entry.name), '.', '').toLowerCase();
return _.includes(IMAGE_EXTENSIONS, extension);
});
const VALID_NUMBER_OF_IMAGE_ENTRIES = 1;
if (imageEntries.length !== VALID_NUMBER_OF_IMAGE_ENTRIES) {
const error = new Error('Invalid archive image');
error.description = 'The archive image should contain one and only one top image file.';
error.report = false;
throw error;
}
const imageEntry = _.first(imageEntries);
return Bluebird.props({
imageStream: hooks.extractFile(archive, entries, imageEntry.name),
metadata: extractArchiveMetadata(archive, ARCHIVE_METADATA_BASE_PATH, {
entries,
hooks
})
}).then((results) => {
results.metadata.stream = results.imageStream;
results.metadata.transform = new PassThroughStream();
results.metadata.size = {
original: imageEntry.size,
final: {
estimation: false,
value: imageEntry.size
}
};
return results.metadata;
});
});
};