mirror of
https://github.com/scratchfoundation/scratch-parser.git
synced 2025-07-30 08:00:38 -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
25
index.js
25
index.js
|
@ -14,12 +14,21 @@ var analyze = require('./lib/analyze');
|
|||
* @return {Object}
|
||||
*/
|
||||
module.exports = function (input, callback) {
|
||||
async.waterfall([
|
||||
function (cb) {
|
||||
unpack(input, cb);
|
||||
},
|
||||
parse,
|
||||
validate,
|
||||
analyze
|
||||
], callback);
|
||||
unpack(input, function(err, unpackedProject) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
async.waterfall([
|
||||
function (cb) {
|
||||
parse(unpackedProject[0], cb);
|
||||
},
|
||||
validate,
|
||||
analyze
|
||||
], 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.
|
||||
|
@ -12,13 +12,17 @@ var JSZip = require('jszip');
|
|||
module.exports = function (input, callback) {
|
||||
if (typeof input === 'string') {
|
||||
// Pass string to callback
|
||||
return callback(null, input);
|
||||
return callback(null, [input, null]);
|
||||
}
|
||||
|
||||
// Validate input type
|
||||
var typeError = 'Input must be a Buffer or a string.';
|
||||
if (!Buffer.isBuffer(input)) {
|
||||
return callback(typeError);
|
||||
try {
|
||||
input = new Buffer(input);
|
||||
} catch (e) {
|
||||
return callback(typeError);
|
||||
}
|
||||
}
|
||||
|
||||
// Determine format
|
||||
|
@ -34,17 +38,12 @@ module.exports = function (input, callback) {
|
|||
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'));
|
||||
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');
|
||||
|
||||
// Handle zip
|
||||
// @todo Handle error
|
||||
JSZip.loadAsync(input).then(function (zip) {
|
||||
zip.file('project.json').async('string')
|
||||
.then(function (project) {
|
||||
callback(null, project);
|
||||
});
|
||||
});
|
||||
zip(input, callback);
|
||||
};
|
||||
|
|
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