mirror of
https://github.com/scratchfoundation/scratch-parser.git
synced 2025-08-28 22:18:45 -04:00
refactor($lib): Add callback wrapper around JSZip promise-based interface. Refactor m
Refactor unpack and main exported function to return optional zip, if originally provided, in addition to the validated project. BREAKING CHANGE: Change to main api to return originally provided zip (or null if string was provided) along with validated project, in a 2-element array.
This commit is contained in:
parent
a74a97a906
commit
7f8672dfbc
3 changed files with 48 additions and 20 deletions
15
index.js
15
index.js
|
@ -14,12 +14,21 @@ var analyze = require('./lib/analyze');
|
||||||
* @return {Object}
|
* @return {Object}
|
||||||
*/
|
*/
|
||||||
module.exports = function (input, callback) {
|
module.exports = function (input, callback) {
|
||||||
|
unpack(input, function(err, unpackedProject) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function (cb) {
|
function (cb) {
|
||||||
unpack(input, cb);
|
parse(unpackedProject[0], cb);
|
||||||
},
|
},
|
||||||
parse,
|
|
||||||
validate,
|
validate,
|
||||||
analyze
|
analyze
|
||||||
], callback);
|
], function(err2, validatedInput) {
|
||||||
|
if (err2) {
|
||||||
|
return callback(err2);
|
||||||
|
}
|
||||||
|
callback(null, [validatedInput, unpackedProject[1]]);
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
var JSZip = require('jszip');
|
var zip = require('./zip');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If input a buffer, transforms buffer into a UTF-8 string.
|
* If input a buffer, transforms buffer into a UTF-8 string.
|
||||||
|
@ -12,14 +12,18 @@ var JSZip = require('jszip');
|
||||||
module.exports = function (input, callback) {
|
module.exports = function (input, callback) {
|
||||||
if (typeof input === 'string') {
|
if (typeof input === 'string') {
|
||||||
// Pass string to callback
|
// Pass string to callback
|
||||||
return callback(null, input);
|
return callback(null, [input, null]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate input type
|
// Validate input type
|
||||||
var typeError = 'Input must be a Buffer or a string.';
|
var typeError = 'Input must be a Buffer or a string.';
|
||||||
if (!Buffer.isBuffer(input)) {
|
if (!Buffer.isBuffer(input)) {
|
||||||
|
try {
|
||||||
|
input = new Buffer(input);
|
||||||
|
} catch (e) {
|
||||||
return callback(typeError);
|
return callback(typeError);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Determine format
|
// Determine format
|
||||||
// We don't use the file suffix as this is unreliable and mine-type
|
// We don't use the file suffix as this is unreliable and mine-type
|
||||||
|
@ -34,17 +38,12 @@ module.exports = function (input, callback) {
|
||||||
if (signature.indexOf('80 75') === 0) isZip = true;
|
if (signature.indexOf('80 75') === 0) isZip = true;
|
||||||
|
|
||||||
// If not legacy or zip, convert buffer to UTF-8 string and return
|
// If not legacy or zip, convert buffer to UTF-8 string and return
|
||||||
if (!isZip && !isLegacy) return callback(null, input.toString('utf-8'));
|
if (!isZip && !isLegacy) {
|
||||||
|
return callback(null, [input.toString('utf-8'), null]);
|
||||||
|
}
|
||||||
|
|
||||||
// Return error if legacy encoding detected
|
// Return error if legacy encoding detected
|
||||||
if (isLegacy) return callback('Parser only supports Scratch 2.X');
|
if (isLegacy) return callback('Parser only supports Scratch 2.X');
|
||||||
|
|
||||||
// Handle zip
|
zip(input, callback);
|
||||||
// @todo Handle error
|
|
||||||
JSZip.loadAsync(input).then(function (zip) {
|
|
||||||
zip.file('project.json').async('string')
|
|
||||||
.then(function (project) {
|
|
||||||
callback(null, project);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
20
lib/zip.js
Normal file
20
lib/zip.js
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
var JSZip = require('jszip');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unpacks a zip
|
||||||
|
*
|
||||||
|
* @param {String} Input
|
||||||
|
*
|
||||||
|
* @return {Object}
|
||||||
|
*/
|
||||||
|
module.exports = function (input, callback) {
|
||||||
|
JSZip.loadAsync(input).then(function (zip) {
|
||||||
|
zip.file('project.json').async('string')
|
||||||
|
.then(function (project) {
|
||||||
|
callback(null, [project, zip]);
|
||||||
|
})
|
||||||
|
.catch(function (err) {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue