From da7c3d12c1ec0f4516fc49656df9778b05f9a384 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Fri, 15 Mar 2019 12:23:40 -0400 Subject: [PATCH] refactor(unzip): remove support for gzip files remove gzip dependency and support for gzip files as that was an unused code path BREAKING CHANGE: Gzipped files are no longer supported as input to the parser. --- lib/unpack.js | 10 ++- lib/unzip.js | 20 ++---- package.json | 1 - test/fixtures/data.js | 7 +- test/fixtures/data/_empty.json.gz | Bin 687 -> 0 bytes test/fixtures/data/_example.json.gz | Bin 2295 -> 0 bytes test/integration/empty.js | 13 ---- test/integration/example.js | 13 ---- test/unit/unzip.js | 103 ++++------------------------ 9 files changed, 22 insertions(+), 145 deletions(-) delete mode 100755 test/fixtures/data/_empty.json.gz delete mode 100755 test/fixtures/data/_example.json.gz diff --git a/lib/unpack.js b/lib/unpack.js index 0918c57..9e4d421 100644 --- a/lib/unpack.js +++ b/lib/unpack.js @@ -2,7 +2,7 @@ var unzip = require('./unzip'); /** * If input a buffer, transforms buffer into a UTF-8 string. - * If input is encoded in zip or gzip format, the input will be extracted and decoded. + * 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 @@ -34,19 +34,17 @@ module.exports = function (input, isSprite, callback) { var signature = input.slice(0, 3).join(' '); var isLegacy = false; var isZip = false; - var isGZip = false; if (signature.indexOf('83 99 114') === 0) isLegacy = true; if (signature.indexOf('80 75') === 0) isZip = true; - if (signature.indexOf('31 139') === 0) isGZip = true; - // If not legacy, zip, or gzip, convert buffer to UTF-8 string and return - if (!isZip && !isLegacy && !isGZip) { + // 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, isGZip, isSprite, callback); + unzip(input, isSprite, callback); }; diff --git a/lib/unzip.js b/lib/unzip.js index 0c930df..83f4faa 100644 --- a/lib/unzip.js +++ b/lib/unzip.js @@ -1,26 +1,16 @@ var JSZip = require('jszip'); -var GZip = require('gzip-js'); /** - * Unpacks a zip or gzip file. - * @param {string} input Zip file provided as a string - * @param {boolean} isGZip Whether the input is a GZip file, otherwise treat as zip + * Unpacks a zip file. + * @param {string} input Zip file provided as a string * @param {boolean} isSprite Whether the input should be treated as * a sprite (true) or whole project (false) - * @param {array} callback Array including both the project and zip archive + * @param {array} callback Array including both the project and zip archive * @return {void} */ -module.exports = function (input, isGZip, isSprite, callback) { +module.exports = function (input, isSprite, callback) { var msg = 'Failed to unzip and extract project.json, with error: '; - if (isGZip) { - var unpackedProject = null; - try { - unpackedProject = GZip.unzip(input); - } catch (e) { - return callback(msg + e); - } - return callback(null, [(new Buffer(unpackedProject)).toString('utf-8'), null]); - } + return JSZip.loadAsync(input) .then(function (zip) { // look for json in the list of files, or in a subdirectory diff --git a/package.json b/package.json index f8a3e8f..7d751b8 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,6 @@ }, "dependencies": { "ajv": "6.3.0", - "gzip-js": "0.3.2", "jszip": "3.1.5", "pify": "4.0.1" }, diff --git a/test/fixtures/data.js b/test/fixtures/data.js index 7aebec8..d6447f0 100644 --- a/test/fixtures/data.js +++ b/test/fixtures/data.js @@ -11,14 +11,12 @@ var sprite2 = glob.sync(path.resolve(__dirname, './data/*.sprite2')); // so that they don't get caught here but can still be used by the // validate unit tests var json = glob.sync(path.resolve(__dirname, './data/*.json')); -var gzipJson = glob.sync(path.resolve(__dirname, './data/*.json.gz')); // Read files and convert to buffers for (var a in sb) sb[a] = fs.readFileSync(sb[a]); for (var b in sb2) sb2[b] = fs.readFileSync(sb2[b]); for (var c in sb3) sb3[c] = fs.readFileSync(sb3[c]); for (var d in json) json[d] = fs.readFileSync(json[d]); -for (var e in gzipJson) gzipJson[e] = fs.readFileSync(gzipJson[e]); for (var f in sprite2) sprite2[f] = fs.readFileSync(sprite2[f]); // Return listings @@ -26,14 +24,12 @@ module.exports = { empty: { sb: fs.readFileSync(path.resolve(__dirname, './data/_empty.sb')), sb2: fs.readFileSync(path.resolve(__dirname, './data/_empty.sb2')), - json: fs.readFileSync(path.resolve(__dirname, './data/_empty.json')), - gzipJson: fs.readFileSync(path.resolve(__dirname, './data/_empty.json.gz')) + json: fs.readFileSync(path.resolve(__dirname, './data/_empty.json')) }, example: { sb: fs.readFileSync(path.resolve(__dirname, './data/_example.sb')), sb2: fs.readFileSync(path.resolve(__dirname, './data/_example.sb2')), json: fs.readFileSync(path.resolve(__dirname, './data/_example.json')), - gzipJson: fs.readFileSync(path.resolve(__dirname, './data/_example.json.gz')), invalidEmpty: fs.readFileSync(path.resolve(__dirname, './data/invalid/_invalidEmpty.sb2')) }, sprites: { @@ -52,7 +48,6 @@ module.exports = { sb2: sb2, sb3: sb3, json: json, - gzipJson: gzipJson, sprite2: sprite2, layerOrderSB3Json: fs.readFileSync(path.resolve(__dirname, './data/_layer_ordering.json')), invalidStageLayerSB3Json: fs.readFileSync(path.resolve(__dirname, './data/invalid/_invalid_stage_layer.json')), diff --git a/test/fixtures/data/_empty.json.gz b/test/fixtures/data/_empty.json.gz deleted file mode 100755 index ec37bf14d472fa74e387599a1f809626dc49ab88..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 687 zcmV;g0#N-QiwFquUYJ?{17BrraCCVtYIARH0JT)hZrd;rJUd@u=o}a#DUwoesndgj zq$wJpND%a}B$u{Yy+FxMgCPH2Q4hE0dRatq`2(PYNkn})qxM#!+5*bSqX(cREQZT9_V=O8wBPQhH z8Fpg{$g2#OZK0;kq&)6n2aWGVNJx2)*wW?TIYH2#-+p?Tx~!XRjt4>qWR$8v|2D3y z$k0tejW_Uut`p5ZfVbD3ddU*Skphhvw*m=jnj}mnL`9U5NGz&iJ=RN`9IEeFm)W*S z%VOGuD;x005{m}igP$!5PlXk zv_W!cgy|YSIwr^Rxo5sJO*le(+h`_C915co%BWNc$}mcR3$BH-cCyWpaY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ueUVP)UocDasa`nSn;d6cX^KUr+~9A-TQMc z;Nm%O0B&W|lu3oD#aOe%N-4PELP{QE{0wkx7dG~!fIAIzRn+wiI57u~C*T(FM8-1JB(*N@f`r=yC)DlznesVv3mbfHEKWl{(JFvx1!uCFp)p8EoY+}M9q9vg*O_V_i7tVo+cHTq;B?2+^ z9LMck;l!g?xbhpB37$<&9QPyTHlflY!cYWAI@kFFD?v5a+B^eXvWrWehV68h<7QJ6 z%@Z9fm-Dg+DIqDR)_IZw1HrVT&+nFYiOsb6ksG;$-`mOc0r$V$$~|~m{>ZIdBdaTD zcMs$3gU7!#UgTq2w}0{uiL`it)gIGVj4StI_xyxo~I z|Br{v|K?HeH;f*Rd8dcG&$yII*D=;d-FU&_zYA?9>+4^=JuL@>AC8Wl&VBz!c-#VX zTmKs{Z@xHteXGf)J%m9x49a*h)w5x!vO<48D?xV$FX6Qx3V+fPyj(6+QDh+d`>Ubi zF}GPAV{U1y+YkL)=GuKIDhOWYn_53gpZ^E_Ecb8!a1Vyo8dY08YTB^ypX<8GAyo&jJ4Wgb{r%ka=Q6L3w#u)!%Ew#flXYiHC(p(+mzT4!XZzYBy&u4y3d6HqGnL>BT|6@?0Dom1UOdb`1;8R%KaK&BXzj)nW-I zm5XKCEY7->k-RZ!Q&jHT!mW$JnR|zegW9iu`r_b|>!4pk|2@uhCoA0~AH!ksscz4< zx-~kiZ|x+SZYU*v4D}ZsUF@~Y)LnG_oKHC*Tsd4vR~<8iy_Er3px|d~vowdGv)Fa{ R^~Kj0{{d##QN5}k008j%fPMe~ diff --git a/test/integration/empty.js b/test/integration/empty.js index cc9494f..fc9b437 100644 --- a/test/integration/empty.js +++ b/test/integration/empty.js @@ -49,16 +49,3 @@ test('json string', function (t) { t.end(); }); }); - -test('gzipped json', function (t) { - parser(data.empty.gzipJson, false, function (err, result) { - t.equal(err, null); - t.equal(Array.isArray(result), true); - var res = result[0]; - var possibleZip = result[1]; - t.type(res, 'object'); - t.type(res.info, 'object'); - t.equal(possibleZip, null); - t.end(); - }); -}); diff --git a/test/integration/example.js b/test/integration/example.js index 98e63c3..e516005 100644 --- a/test/integration/example.js +++ b/test/integration/example.js @@ -50,19 +50,6 @@ test('json string', function (t) { }); }); -test('gzipped json', function (t) { - parser(data.example.gzipJson, false, function (err, result) { - t.equal(err, null); - t.equal(Array.isArray(result), true); - var res = result[0]; - var possibleZip = result[1]; - t.type(res, 'object'); - t.type(res.info, 'object'); - t.equal(possibleZip, null); - t.end(); - }); -}); - test('invalid empty project archive', function (t) { var msg = 'Failed to unzip and extract project.json, with error: '; parser(data.example.invalidEmpty, false, function (err, result) { diff --git a/test/unit/unzip.js b/test/unit/unzip.js index bc9a2cb..01ba6ab 100644 --- a/test/unit/unzip.js +++ b/test/unit/unzip.js @@ -7,7 +7,6 @@ var unzip = require('../../lib/unzip'); var fixtures = { sb: path.resolve(__dirname, '../fixtures/data/_example.sb'), sb2: path.resolve(__dirname, '../fixtures/data/_example.sb2'), - gzipJSON: path.resolve(__dirname, '../fixtures/data/_example.json.gz'), zipFakeProjectJSON: path.resolve(__dirname, '../fixtures/data/_zipFakeProjectJson.zip'), zipNoProjectJSON: @@ -28,7 +27,7 @@ test('spec', function (t) { test('sb', function (t) { var buffer = new Buffer(fixtures.sb); - unzip(buffer, false, false, function (err, res) { + unzip(buffer, false, function (err, res) { t.type(err, 'string'); t.equal(err.startsWith(errorMessage), true); t.type(res, 'undefined'); @@ -38,7 +37,7 @@ test('sb', function (t) { test('sb2', function (t) { var buffer = new Buffer(fixtures.sb2); - unzip(buffer, false, false, function (err, res) { + unzip(buffer, false, function (err, res) { t.equal(err, null); t.equal(Array.isArray(res), true); t.type(res[0], 'string'); @@ -50,23 +49,9 @@ test('sb2', function (t) { }); }); -test('gzipped JSON', function (t) { - var buffer = new Buffer(fixtures.gzipJSON); - unzip(buffer, true, false, function (err, res) { - t.equal(err, null); - t.equal(Array.isArray(res), true); - t.type(res[0], 'string'); - t.doesNotThrow(function () { - JSON.parse(res[0]); - }); - t.equal(res[1], null); - t.end(); - }); -}); - test('sb2 with nested folder', function (t) { var buffer = new Buffer(fixtures.sb2Nested); - unzip(buffer, false, false, function (err, res) { + unzip(buffer, false, function (err, res) { t.equal(err, null); t.equal(Array.isArray(res), true); t.type(res[0], 'string'); @@ -80,7 +65,7 @@ test('sb2 with nested folder', function (t) { test('zip without project json', function (t) { var buffer = new Buffer(fixtures.zipNoProjectJSON); - unzip(buffer, false, false, function (err, res) { + unzip(buffer, false, function (err, res) { t.type(err, 'string'); t.equal(err.startsWith(errorMessage), true); t.type(res, 'undefined'); @@ -90,7 +75,7 @@ test('zip without project json', function (t) { test('zip with fake project json', function (t) { var buffer = new Buffer(fixtures.zipFakeProjectJSON); - unzip(buffer, false, false, function (err, res) { + unzip(buffer, false, function (err, res) { t.equal(err, null); t.equal(Array.isArray(res), true); t.type(res[0], 'string'); @@ -106,7 +91,7 @@ test('zip with fake project json', function (t) { var randomString = 'this is not a zip'; test('random string instead of zip, whole project', function (t) { - unzip(randomString, false, false, function (err, res) { + unzip(randomString, false, function (err, res) { t.type(err, 'string'); t.equal(err.startsWith(errorMessage), true); t.type(res, 'undefined'); @@ -115,25 +100,7 @@ test('random string instead of zip, whole project', function (t) { }); test('random string instead of zip, sprite', function (t) { - unzip(randomString, false, true, function (err, res) { - t.type(err, 'string'); - t.equal(err.startsWith(errorMessage), true); - t.type(res, 'undefined'); - t.end(); - }); -}); - -test('random string instead of gzip, whole project ', function (t) { - unzip(randomString, true, false, function (err, res) { - t.type(err, 'string'); - t.equal(err.startsWith(errorMessage), true); - t.type(res, 'undefined'); - t.end(); - }); -}); - -test('random string instead of gzip, sprite', function (t) { - unzip(randomString, true, true, function (err, res) { + unzip(randomString, true, function (err, res) { t.type(err, 'string'); t.equal(err.startsWith(errorMessage), true); t.type(res, 'undefined'); @@ -143,17 +110,7 @@ test('random string instead of gzip, sprite', function (t) { test('undefined', function (t) { var foo; - unzip(foo, false, false, function (err, obj) { - t.type(err, 'string'); - t.equal(err.startsWith(errorMessage), true); - t.type(obj, 'undefined'); - t.end(); - }); -}); - -test('undefined isGZip', function (t) { - var foo; - unzip(foo, true, false, function (err, obj) { + unzip(foo, false, function (err, obj) { t.type(err, 'string'); t.equal(err.startsWith(errorMessage), true); t.type(obj, 'undefined'); @@ -162,7 +119,7 @@ test('undefined isGZip', function (t) { }); test('null instead of zip, whole project', function (t) { - unzip(null, false, false, function (err, obj) { + unzip(null, false, function (err, obj) { t.type(err, 'string'); t.equal(err.startsWith(errorMessage), true); t.type(obj, 'undefined'); @@ -171,25 +128,7 @@ test('null instead of zip, whole project', function (t) { }); test('null instead of zip, sprite', function (t) { - unzip(null, false, true, function (err, obj) { - t.type(err, 'string'); - t.equal(err.startsWith(errorMessage), true); - t.type(obj, 'undefined'); - t.end(); - }); -}); - -test('null instead of gzip, whole project', function (t) { - unzip(null, true, false, function (err, obj) { - t.type(err, 'string'); - t.equal(err.startsWith(errorMessage), true); - t.type(obj, 'undefined'); - t.end(); - }); -}); - -test('null instead of gzip, sprite', function (t) { - unzip(null, true, true, function (err, obj) { + unzip(null, true, function (err, obj) { t.type(err, 'string'); t.equal(err.startsWith(errorMessage), true); t.type(obj, 'undefined'); @@ -198,7 +137,7 @@ test('null instead of gzip, sprite', function (t) { }); test('object instead of zip, whole project', function (t) { - unzip({}, false, false, function (err, obj) { + unzip({}, false, function (err, obj) { t.type(err, 'string'); t.equal(err.startsWith(errorMessage), true); t.type(obj, 'undefined'); @@ -207,25 +146,7 @@ test('object instead of zip, whole project', function (t) { }); test('object instead of zip, sprite', function (t) { - unzip({}, false, true, function (err, obj) { - t.type(err, 'string'); - t.equal(err.startsWith(errorMessage), true); - t.type(obj, 'undefined'); - t.end(); - }); -}); - -test('object instead of gzip, whole project', function (t) { - unzip({}, true, false, function (err, obj) { - t.type(err, 'string'); - t.equal(err.startsWith(errorMessage), true); - t.type(obj, 'undefined'); - t.end(); - }); -}); - -test('object instead of gzip, sprite', function (t) { - unzip({}, true, false, function (err, obj) { + unzip({}, true, function (err, obj) { t.type(err, 'string'); t.equal(err.startsWith(errorMessage), true); t.type(obj, 'undefined');