Skip to content

Commit da7c3d1

Browse files
committed
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.
1 parent c5cc58e commit da7c3d1

File tree

9 files changed

+22
-145
lines changed

9 files changed

+22
-145
lines changed

lib/unpack.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var unzip = require('./unzip');
22

33
/**
44
* If input a buffer, transforms buffer into a UTF-8 string.
5-
* If input is encoded in zip or gzip format, the input will be extracted and decoded.
5+
* If input is encoded in zip format, the input will be extracted and decoded.
66
* If input is a string, passes that string along to the given callback.
77
* @param {Buffer | string} input Project data
88
* @param {boolean} isSprite Whether the input should be treated as
@@ -34,19 +34,17 @@ module.exports = function (input, isSprite, callback) {
3434
var signature = input.slice(0, 3).join(' ');
3535
var isLegacy = false;
3636
var isZip = false;
37-
var isGZip = false;
3837

3938
if (signature.indexOf('83 99 114') === 0) isLegacy = true;
4039
if (signature.indexOf('80 75') === 0) isZip = true;
41-
if (signature.indexOf('31 139') === 0) isGZip = true;
4240

43-
// If not legacy, zip, or gzip, convert buffer to UTF-8 string and return
44-
if (!isZip && !isLegacy && !isGZip) {
41+
// If not legacy or zip, convert buffer to UTF-8 string and return
42+
if (!isZip && !isLegacy) {
4543
return callback(null, [input.toString('utf-8'), null]);
4644
}
4745

4846
// Return error if legacy encoding detected
4947
if (isLegacy) return callback('Parser only supports Scratch 2.X and above');
5048

51-
unzip(input, isGZip, isSprite, callback);
49+
unzip(input, isSprite, callback);
5250
};

lib/unzip.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
var JSZip = require('jszip');
2-
var GZip = require('gzip-js');
32

43
/**
5-
* Unpacks a zip or gzip file.
6-
* @param {string} input Zip file provided as a string
7-
* @param {boolean} isGZip Whether the input is a GZip file, otherwise treat as zip
4+
* Unpacks a zip file.
5+
* @param {string} input Zip file provided as a string
86
* @param {boolean} isSprite Whether the input should be treated as
97
* a sprite (true) or whole project (false)
10-
* @param {array} callback Array including both the project and zip archive
8+
* @param {array} callback Array including both the project and zip archive
119
* @return {void}
1210
*/
13-
module.exports = function (input, isGZip, isSprite, callback) {
11+
module.exports = function (input, isSprite, callback) {
1412
var msg = 'Failed to unzip and extract project.json, with error: ';
15-
if (isGZip) {
16-
var unpackedProject = null;
17-
try {
18-
unpackedProject = GZip.unzip(input);
19-
} catch (e) {
20-
return callback(msg + e);
21-
}
22-
return callback(null, [(new Buffer(unpackedProject)).toString('utf-8'), null]);
23-
}
13+
2414
return JSZip.loadAsync(input)
2515
.then(function (zip) {
2616
// look for json in the list of files, or in a subdirectory

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
},
2222
"dependencies": {
2323
"ajv": "6.3.0",
24-
"gzip-js": "0.3.2",
2524
"jszip": "3.1.5",
2625
"pify": "4.0.1"
2726
},

test/fixtures/data.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,25 @@ var sprite2 = glob.sync(path.resolve(__dirname, './data/*.sprite2'));
1111
// so that they don't get caught here but can still be used by the
1212
// validate unit tests
1313
var json = glob.sync(path.resolve(__dirname, './data/*.json'));
14-
var gzipJson = glob.sync(path.resolve(__dirname, './data/*.json.gz'));
1514

1615
// Read files and convert to buffers
1716
for (var a in sb) sb[a] = fs.readFileSync(sb[a]);
1817
for (var b in sb2) sb2[b] = fs.readFileSync(sb2[b]);
1918
for (var c in sb3) sb3[c] = fs.readFileSync(sb3[c]);
2019
for (var d in json) json[d] = fs.readFileSync(json[d]);
21-
for (var e in gzipJson) gzipJson[e] = fs.readFileSync(gzipJson[e]);
2220
for (var f in sprite2) sprite2[f] = fs.readFileSync(sprite2[f]);
2321

2422
// Return listings
2523
module.exports = {
2624
empty: {
2725
sb: fs.readFileSync(path.resolve(__dirname, './data/_empty.sb')),
2826
sb2: fs.readFileSync(path.resolve(__dirname, './data/_empty.sb2')),
29-
json: fs.readFileSync(path.resolve(__dirname, './data/_empty.json')),
30-
gzipJson: fs.readFileSync(path.resolve(__dirname, './data/_empty.json.gz'))
27+
json: fs.readFileSync(path.resolve(__dirname, './data/_empty.json'))
3128
},
3229
example: {
3330
sb: fs.readFileSync(path.resolve(__dirname, './data/_example.sb')),
3431
sb2: fs.readFileSync(path.resolve(__dirname, './data/_example.sb2')),
3532
json: fs.readFileSync(path.resolve(__dirname, './data/_example.json')),
36-
gzipJson: fs.readFileSync(path.resolve(__dirname, './data/_example.json.gz')),
3733
invalidEmpty: fs.readFileSync(path.resolve(__dirname, './data/invalid/_invalidEmpty.sb2'))
3834
},
3935
sprites: {
@@ -52,7 +48,6 @@ module.exports = {
5248
sb2: sb2,
5349
sb3: sb3,
5450
json: json,
55-
gzipJson: gzipJson,
5651
sprite2: sprite2,
5752
layerOrderSB3Json: fs.readFileSync(path.resolve(__dirname, './data/_layer_ordering.json')),
5853
invalidStageLayerSB3Json: fs.readFileSync(path.resolve(__dirname, './data/invalid/_invalid_stage_layer.json')),

test/fixtures/data/_empty.json.gz

-687 Bytes
Binary file not shown.

test/fixtures/data/_example.json.gz

-2.24 KB
Binary file not shown.

test/integration/empty.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,3 @@ test('json string', function (t) {
4949
t.end();
5050
});
5151
});
52-
53-
test('gzipped json', function (t) {
54-
parser(data.empty.gzipJson, false, function (err, result) {
55-
t.equal(err, null);
56-
t.equal(Array.isArray(result), true);
57-
var res = result[0];
58-
var possibleZip = result[1];
59-
t.type(res, 'object');
60-
t.type(res.info, 'object');
61-
t.equal(possibleZip, null);
62-
t.end();
63-
});
64-
});

test/integration/example.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,6 @@ test('json string', function (t) {
5050
});
5151
});
5252

53-
test('gzipped json', function (t) {
54-
parser(data.example.gzipJson, false, function (err, result) {
55-
t.equal(err, null);
56-
t.equal(Array.isArray(result), true);
57-
var res = result[0];
58-
var possibleZip = result[1];
59-
t.type(res, 'object');
60-
t.type(res.info, 'object');
61-
t.equal(possibleZip, null);
62-
t.end();
63-
});
64-
});
65-
6653
test('invalid empty project archive', function (t) {
6754
var msg = 'Failed to unzip and extract project.json, with error: ';
6855
parser(data.example.invalidEmpty, false, function (err, result) {

test/unit/unzip.js

Lines changed: 12 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var unzip = require('../../lib/unzip');
77
var fixtures = {
88
sb: path.resolve(__dirname, '../fixtures/data/_example.sb'),
99
sb2: path.resolve(__dirname, '../fixtures/data/_example.sb2'),
10-
gzipJSON: path.resolve(__dirname, '../fixtures/data/_example.json.gz'),
1110
zipFakeProjectJSON:
1211
path.resolve(__dirname, '../fixtures/data/_zipFakeProjectJson.zip'),
1312
zipNoProjectJSON:
@@ -28,7 +27,7 @@ test('spec', function (t) {
2827

2928
test('sb', function (t) {
3029
var buffer = new Buffer(fixtures.sb);
31-
unzip(buffer, false, false, function (err, res) {
30+
unzip(buffer, false, function (err, res) {
3231
t.type(err, 'string');
3332
t.equal(err.startsWith(errorMessage), true);
3433
t.type(res, 'undefined');
@@ -38,7 +37,7 @@ test('sb', function (t) {
3837

3938
test('sb2', function (t) {
4039
var buffer = new Buffer(fixtures.sb2);
41-
unzip(buffer, false, false, function (err, res) {
40+
unzip(buffer, false, function (err, res) {
4241
t.equal(err, null);
4342
t.equal(Array.isArray(res), true);
4443
t.type(res[0], 'string');
@@ -50,23 +49,9 @@ test('sb2', function (t) {
5049
});
5150
});
5251

53-
test('gzipped JSON', function (t) {
54-
var buffer = new Buffer(fixtures.gzipJSON);
55-
unzip(buffer, true, false, function (err, res) {
56-
t.equal(err, null);
57-
t.equal(Array.isArray(res), true);
58-
t.type(res[0], 'string');
59-
t.doesNotThrow(function () {
60-
JSON.parse(res[0]);
61-
});
62-
t.equal(res[1], null);
63-
t.end();
64-
});
65-
});
66-
6752
test('sb2 with nested folder', function (t) {
6853
var buffer = new Buffer(fixtures.sb2Nested);
69-
unzip(buffer, false, false, function (err, res) {
54+
unzip(buffer, false, function (err, res) {
7055
t.equal(err, null);
7156
t.equal(Array.isArray(res), true);
7257
t.type(res[0], 'string');
@@ -80,7 +65,7 @@ test('sb2 with nested folder', function (t) {
8065

8166
test('zip without project json', function (t) {
8267
var buffer = new Buffer(fixtures.zipNoProjectJSON);
83-
unzip(buffer, false, false, function (err, res) {
68+
unzip(buffer, false, function (err, res) {
8469
t.type(err, 'string');
8570
t.equal(err.startsWith(errorMessage), true);
8671
t.type(res, 'undefined');
@@ -90,7 +75,7 @@ test('zip without project json', function (t) {
9075

9176
test('zip with fake project json', function (t) {
9277
var buffer = new Buffer(fixtures.zipFakeProjectJSON);
93-
unzip(buffer, false, false, function (err, res) {
78+
unzip(buffer, false, function (err, res) {
9479
t.equal(err, null);
9580
t.equal(Array.isArray(res), true);
9681
t.type(res[0], 'string');
@@ -106,7 +91,7 @@ test('zip with fake project json', function (t) {
10691
var randomString = 'this is not a zip';
10792

10893
test('random string instead of zip, whole project', function (t) {
109-
unzip(randomString, false, false, function (err, res) {
94+
unzip(randomString, false, function (err, res) {
11095
t.type(err, 'string');
11196
t.equal(err.startsWith(errorMessage), true);
11297
t.type(res, 'undefined');
@@ -115,25 +100,7 @@ test('random string instead of zip, whole project', function (t) {
115100
});
116101

117102
test('random string instead of zip, sprite', function (t) {
118-
unzip(randomString, false, true, function (err, res) {
119-
t.type(err, 'string');
120-
t.equal(err.startsWith(errorMessage), true);
121-
t.type(res, 'undefined');
122-
t.end();
123-
});
124-
});
125-
126-
test('random string instead of gzip, whole project ', function (t) {
127-
unzip(randomString, true, false, function (err, res) {
128-
t.type(err, 'string');
129-
t.equal(err.startsWith(errorMessage), true);
130-
t.type(res, 'undefined');
131-
t.end();
132-
});
133-
});
134-
135-
test('random string instead of gzip, sprite', function (t) {
136-
unzip(randomString, true, true, function (err, res) {
103+
unzip(randomString, true, function (err, res) {
137104
t.type(err, 'string');
138105
t.equal(err.startsWith(errorMessage), true);
139106
t.type(res, 'undefined');
@@ -143,17 +110,7 @@ test('random string instead of gzip, sprite', function (t) {
143110

144111
test('undefined', function (t) {
145112
var foo;
146-
unzip(foo, false, false, function (err, obj) {
147-
t.type(err, 'string');
148-
t.equal(err.startsWith(errorMessage), true);
149-
t.type(obj, 'undefined');
150-
t.end();
151-
});
152-
});
153-
154-
test('undefined isGZip', function (t) {
155-
var foo;
156-
unzip(foo, true, false, function (err, obj) {
113+
unzip(foo, false, function (err, obj) {
157114
t.type(err, 'string');
158115
t.equal(err.startsWith(errorMessage), true);
159116
t.type(obj, 'undefined');
@@ -162,7 +119,7 @@ test('undefined isGZip', function (t) {
162119
});
163120

164121
test('null instead of zip, whole project', function (t) {
165-
unzip(null, false, false, function (err, obj) {
122+
unzip(null, false, function (err, obj) {
166123
t.type(err, 'string');
167124
t.equal(err.startsWith(errorMessage), true);
168125
t.type(obj, 'undefined');
@@ -171,25 +128,7 @@ test('null instead of zip, whole project', function (t) {
171128
});
172129

173130
test('null instead of zip, sprite', function (t) {
174-
unzip(null, false, true, function (err, obj) {
175-
t.type(err, 'string');
176-
t.equal(err.startsWith(errorMessage), true);
177-
t.type(obj, 'undefined');
178-
t.end();
179-
});
180-
});
181-
182-
test('null instead of gzip, whole project', function (t) {
183-
unzip(null, true, false, function (err, obj) {
184-
t.type(err, 'string');
185-
t.equal(err.startsWith(errorMessage), true);
186-
t.type(obj, 'undefined');
187-
t.end();
188-
});
189-
});
190-
191-
test('null instead of gzip, sprite', function (t) {
192-
unzip(null, true, true, function (err, obj) {
131+
unzip(null, true, function (err, obj) {
193132
t.type(err, 'string');
194133
t.equal(err.startsWith(errorMessage), true);
195134
t.type(obj, 'undefined');
@@ -198,7 +137,7 @@ test('null instead of gzip, sprite', function (t) {
198137
});
199138

200139
test('object instead of zip, whole project', function (t) {
201-
unzip({}, false, false, function (err, obj) {
140+
unzip({}, false, function (err, obj) {
202141
t.type(err, 'string');
203142
t.equal(err.startsWith(errorMessage), true);
204143
t.type(obj, 'undefined');
@@ -207,25 +146,7 @@ test('object instead of zip, whole project', function (t) {
207146
});
208147

209148
test('object instead of zip, sprite', function (t) {
210-
unzip({}, false, true, function (err, obj) {
211-
t.type(err, 'string');
212-
t.equal(err.startsWith(errorMessage), true);
213-
t.type(obj, 'undefined');
214-
t.end();
215-
});
216-
});
217-
218-
test('object instead of gzip, whole project', function (t) {
219-
unzip({}, true, false, function (err, obj) {
220-
t.type(err, 'string');
221-
t.equal(err.startsWith(errorMessage), true);
222-
t.type(obj, 'undefined');
223-
t.end();
224-
});
225-
});
226-
227-
test('object instead of gzip, sprite', function (t) {
228-
unzip({}, true, false, function (err, obj) {
149+
unzip({}, true, function (err, obj) {
229150
t.type(err, 'string');
230151
t.equal(err.startsWith(errorMessage), true);
231152
t.type(obj, 'undefined');

0 commit comments

Comments
 (0)