-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
generate.js
366 lines (331 loc) · 15 KB
/
generate.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* global QUnit,JSZip,JSZipTestUtils */
'use strict';
QUnit.module("generate");
function testGenerateFor(testCases, fn) {
while(testCases.length) {
var testCase = testCases.shift();
fn(testCase.name, testCase.file, testCase.streamFiles);
}
}
function testGenerate(assert, options) {
var done = assert.async();
var triggeredCallback = false;
new JSZip.external.Promise(function(resolve, reject) {
resolve(options.prepare());
})
.then(function (zip) {
JSZipTestUtils.checkBasicStreamBehavior(assert, zip.generateInternalStream(options.options));
return zip;
})
.then(function(zip) {
var promise = zip.generateAsync(options.options);
zip.file("Hello.txt", "updating the zip file after the call won't change the result");
return promise;
})
.then(function(result) {
triggeredCallback = true;
options.assertions(null, result);
if (!options.skipReloadTest) {
JSZipTestUtils.checkGenerateStability(assert, result, options.options);
}
done();
}, function (err) {
triggeredCallback = true;
options.assertions(err, null);
done();
});
assert.ok(!triggeredCallback, "the async callback is async");
}
testGenerateFor([{
name : "no stream",
file : "ref/all.zip",
streamFiles : false
}, {
name : "with stream",
// zip -fd -0 -X -r all-stream.zip Hello.txt images/
file : "ref/all-stream.zip",
streamFiles : true
}], function(testName, file, streamFiles) {
JSZipTestUtils.testZipFile("generate : type:string. " + testName, file, function(assert, expected) {
testGenerate(assert, {
prepare : JSZipTestUtils.createZipAll,
options : {type:"binarystring",streamFiles:streamFiles},
assertions : function (err, result) {
assert.equal(err, null, "no error");
assert.ok(JSZipTestUtils.similar(result, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP");
}
});
});
JSZipTestUtils.testZipFile("generate : type:base64. " + testName, file, function(assert, expected) {
testGenerate(assert, {
prepare : function () {
// fix date to get a predictable output
var zip = new JSZip();
zip.file("Hello.txt", "Hello World\n", {date: new Date(1234567891011)});
zip.file("images", null, {dir:true, date: new Date(1234876591011)});
zip.file("images/smile.gif", "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=", {base64: true, date: new Date(1234123491011)});
return zip;
},
skipReloadTest : true,
options : {type:"base64",streamFiles:streamFiles},
assertions : function (err, result) {
assert.equal(err, null, "no error");
assert.equal(result, JSZipTestUtils.base64encode("all.zip.base64,stream=" + streamFiles), "generated ZIP matches reference ZIP");
}
});
});
JSZipTestUtils.testZipFile("generate : type:uint8array. " + testName, file, function(assert, expected) {
testGenerate(assert, {
prepare : JSZipTestUtils.createZipAll,
options : {type:"uint8array",streamFiles:streamFiles},
assertions : function (err, result) {
if (JSZip.support.uint8array) {
assert.equal(err, null, "no error");
assert.ok(result instanceof Uint8Array, "the result is a instance of Uint8Array");
// var actual = JSZipTestUtils.toString(result);
assert.ok(JSZipTestUtils.similar(result, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP");
} else {
assert.equal(result, null, "no data");
assert.ok(err.message.match("not supported by this platform"), "the error message is useful");
}
}
});
});
JSZipTestUtils.testZipFile("generate : type:arraybuffer. " + testName, file, function(assert, expected) {
testGenerate(assert, {
prepare : JSZipTestUtils.createZipAll,
options : {type:"arraybuffer",streamFiles:streamFiles},
assertions : function (err, result) {
if (JSZip.support.arraybuffer) {
assert.equal(err, null, "no error");
assert.ok(result instanceof ArrayBuffer, "the result is a instance of ArrayBuffer");
assert.ok(JSZipTestUtils.similar(result, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP");
} else {
assert.equal(result, null, "no data");
assert.ok(err.message.match("not supported by this platform"), "the error message is useful");
}
}
});
});
JSZipTestUtils.testZipFile("generate : type:nodebuffer. " + testName, file, function(assert, expected) {
testGenerate(assert, {
prepare : JSZipTestUtils.createZipAll,
options : {type:"nodebuffer",streamFiles:streamFiles},
assertions : function (err, result) {
if (JSZip.support.nodebuffer) {
assert.equal(err, null, "no error");
assert.ok(result instanceof Buffer, "the result is a instance of ArrayBuffer");
var actual = JSZipTestUtils.toString(result);
assert.ok(JSZipTestUtils.similar(actual, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP");
} else {
assert.equal(result, null, "no data");
assert.ok(err.message.match("not supported by this platform"), "the error message is useful");
}
}
});
});
JSZipTestUtils.testZipFile("generate : type:blob. " + testName, file, function(assert, expected) {
testGenerate(assert, {
prepare : JSZipTestUtils.createZipAll,
options : {type:"blob",streamFiles:streamFiles},
skipReloadTest : true,
assertions : function (err, result) {
if (JSZip.support.blob) {
assert.equal(err, null, "no error");
assert.ok(result instanceof Blob, "the result is a instance of Blob");
assert.equal(result.type, "application/zip", "the result has the right mime type");
assert.equal(result.size, expected.length, "the result has the right length");
} else {
assert.equal(result, null, "no data");
assert.ok(err.message.match("not supported by this platform"), "the error message is useful");
}
}
});
});
JSZipTestUtils.testZipFile("generate : type:blob mimeType:application/ods. " + testName, file, function(assert, expected) {
testGenerate(assert, {
prepare : JSZipTestUtils.createZipAll,
options : {type:"blob",mimeType: "application/ods",streamFiles:streamFiles},
skipReloadTest : true,
assertions : function (err, result) {
if (JSZip.support.blob) {
assert.equal(err, null, "no error");
assert.ok(result instanceof Blob, "the result is a instance of Blob");
assert.equal(result.type, "application/ods", "the result has the right mime type");
assert.equal(result.size, expected.length, "the result has the right length");
} else {
assert.equal(result, null, "no data");
assert.ok(err.message.match("not supported by this platform"), "the error message is useful");
}
}
});
});
});
testGenerateFor([{
name : "no stream",
// zip -0 -X store.zip Hello.txt
file : "ref/store.zip",
streamFiles : false
}, {
name : "with stream",
// zip -0 -X -fd store-stream.zip Hello.txt
file : "ref/store-stream.zip",
streamFiles : true
}], function(testName, file, streamFiles) {
JSZipTestUtils.testZipFile("STORE doesn't compress, " + testName, file, function(assert, expected) {
testGenerate(assert, {
prepare : function () {
var zip = new JSZip();
zip.file("Hello.txt", "This a looong file : we need to see the difference between the different compression methods.\n");
return zip;
},
options : {type:"binarystring", compression:"STORE",streamFiles:streamFiles},
assertions : function (err, result) {
assert.equal(err, null, "no error");
assert.ok(JSZipTestUtils.similar(result, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP");
}
});
});
});
testGenerateFor([{
name : "no stream",
// zip -6 -X deflate.zip Hello.txt
file : "ref/deflate.zip",
streamFiles : false
}, {
name : "with stream",
// zip -6 -X -fd deflate-stream.zip Hello.txt
file : "ref/deflate-stream.zip",
streamFiles : true
}], function(testName, file, streamFiles) {
JSZipTestUtils.testZipFile("DEFLATE compress, " + testName, file, function(assert, expected) {
testGenerate(assert, {
prepare : function () {
var zip = new JSZip();
zip.file("Hello.txt", "This a looong file : we need to see the difference between the different compression methods.\n");
return zip;
},
options : {type:"binarystring", compression:"DEFLATE",streamFiles:streamFiles},
assertions : function (err, result) {
assert.equal(err, null, "no error");
assert.ok(JSZipTestUtils.similar(result, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP");
}
});
});
});
JSZipTestUtils.testZipFile("STORE is the default method", "ref/text.zip", function(assert, expected) {
var zip = new JSZip();
zip.file("Hello.txt", "Hello World\n");
var done = assert.async();
zip.generateAsync({type:"binarystring", compression:'STORE'}).then(function(content) {
// no difference with the "Zip text file" test.
assert.ok(JSZipTestUtils.similar(content, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP");
done();
})['catch'](JSZipTestUtils.assertNoError);
});
function testLazyDecompression(assert, from, to) {
var done = assert.async();
JSZipTestUtils.createZipAll().generateAsync({type:"binarystring", compression:from}).then(function(actual) {
done();
testGenerate(assert, {
prepare : function () {
// the zip object will contain compressed objects
return JSZip.loadAsync(actual);
},
skipReloadTest : true,
options : {type:"binarystring", compression:to},
assertions : function (err, result) {
assert.equal(err, null, from + " -> " + to + " : no error");
}
});
})['catch'](JSZipTestUtils.assertNoError);
}
QUnit.test("Lazy decompression works", function(assert) {
testLazyDecompression(assert, "STORE", "STORE");
testLazyDecompression(assert, "DEFLATE", "STORE");
testLazyDecompression(assert, "STORE", "DEFLATE");
testLazyDecompression(assert, "DEFLATE", "DEFLATE");
});
// zip -0 -X empty.zip plop && zip -d empty.zip plop
JSZipTestUtils.testZipFile("empty zip", "ref/empty.zip", function(assert, expected) {
testGenerate(assert, {
prepare : function () {
var zip = new JSZip();
return zip;
},
options : {type:"binarystring"},
assertions : function (err, result) {
assert.equal(err, null, "no error");
assert.ok(JSZipTestUtils.similar(result, expected, 0 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP");
}
});
});
QUnit.test("unknown compression throws an exception", function (assert) {
testGenerate(assert, {
prepare : JSZipTestUtils.createZipAll,
options : {type:"string",compression:'MAYBE'},
assertions : function (err, result) {
assert.equal(result, null, "no data");
assert.ok(err.message.match("not a valid compression"), "the error message is useful");
}
});
});
QUnit.test("missing type throws an exception", function (assert) {
testGenerate(assert, {
prepare : JSZipTestUtils.createZipAll,
options : {},
assertions : function (err, result) {
assert.equal(result, null, "no data");
assert.ok(err.message.match("No output type specified."), "the error message is useful");
}
});
});
QUnit.test("generateAsync uses the current folder level", function (assert) {
var done = assert.async();
var zip = new JSZip();
zip.file("file1", "a");
zip.folder("root1");
zip.folder("root2").file("leaf1", "a");
zip.folder("root2")
.generateAsync({type:"string"})
.then(JSZip.loadAsync)
.then(function(zip) {
assert.ok(!zip.file("file1"), "root files are not present");
assert.ok(!zip.file("root1"), "root folders are not present");
assert.ok(!zip.file("root2"), "root folders are not present");
assert.ok(zip.file("leaf1"), "leaves are present");
done();
})['catch'](JSZipTestUtils.assertNoError);
});
QUnit.test("generateAsync keep the explicit / folder", function (assert) {
var done = assert.async();
var zip = new JSZip();
zip.file("/file1", "a");
zip.file("/root1/file2", "b");
zip.generateAsync({type:"string"})
.then(JSZip.loadAsync)
.then(function(zip) {
assert.ok(zip.file("/file1"), "root files are present");
assert.ok(zip.file("/root1/file2"), "root folders are present");
done();
})['catch'](JSZipTestUtils.assertNoError);
});
JSZipTestUtils.testZipFile("generate with promises as files", "ref/all.zip", function (assert, expected) {
var done = assert.async();
var zip = new JSZip();
zip.file("Hello.txt", new JSZip.external.Promise(function (resolve, reject) {
setTimeout(function () {
resolve("Hello World\n");
}, 50);
}));
zip.folder("images").file("smile.gif", new JSZip.external.Promise(function (resolve, reject) {
setTimeout(function () {
resolve("R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=");
}, 100);
}), {base64: true});
zip.generateAsync({type:"string"})
.then(function (result) {
assert.ok(JSZipTestUtils.similar(result, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP");
done();
})['catch'](JSZipTestUtils.assertNoError);
});