Spaces:
Sleeping
Sleeping
File size: 1,871 Bytes
7aec436 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
var unzip = require('./unzip');
/**
* If input a buffer, transforms buffer into a UTF-8 string.
* If input is encoded in zip format, the input will be extracted and decoded.
* If input is a string, passes that string along to the given callback.
* @param {Buffer | string} input Project data
* @param {boolean} isSprite Whether the input should be treated as
* a sprite (true) or a whole project (false)
* @param {Function} callback Error or stringified project data
* @return {void}
*/
module.exports = function (input, isSprite, callback) {
if (typeof input === 'string') {
// Pass string to callback
return callback(null, [input, null]);
}
// Validate input type
var typeError = 'Input must be a Buffer or a string.';
if (!Buffer.isBuffer(input)) {
try {
input = new Buffer(input);
} catch (e) {
return callback(typeError);
}
}
// Determine format
// We don't use the file suffix as this is unreliable and mine-type
// information is unavailable from Scratch's project CDN. Instead, we look
// at the first few bytes from the provided buffer (byte signature).
// https://en.wikipedia.org/wiki/List_of_file_signatures
var signature = input.slice(0, 3).join(' ');
var isLegacy = false;
var isZip = false;
if (signature.indexOf('83 99 114') === 0) isLegacy = true;
if (signature.indexOf('80 75') === 0) isZip = true;
// If not legacy or zip, convert buffer to UTF-8 string and return
if (!isZip && !isLegacy) {
return callback(null, [input.toString('utf-8'), null]);
}
// Return error if legacy encoding detected
if (isLegacy) return callback('Parser only supports Scratch 2.X and above');
unzip(input, isSprite, callback);
};
|