@zip.js/zip.js 2.5.9 → 2.5.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/core/io.js CHANGED
@@ -55,7 +55,7 @@ class Stream {
55
55
 
56
56
  class Reader extends Stream {
57
57
 
58
- getStream({ offset, size, chunkSize } = { offset: 0, chunkSize: DEFAULT_CHUNK_SIZE }) {
58
+ getReadable({ offset, size, chunkSize } = { offset: 0, chunkSize: DEFAULT_CHUNK_SIZE }) {
59
59
  const reader = this;
60
60
  return new ReadableStream({
61
61
  start() {
@@ -76,7 +76,7 @@ class Reader extends Stream {
76
76
 
77
77
  class WriterStream extends Stream {
78
78
 
79
- getStream() {
79
+ get writable() {
80
80
  const writer = this;
81
81
  return new WritableStream({
82
82
  write(chunk) {
@@ -227,9 +227,10 @@ class BlobWriter extends Writer {
227
227
 
228
228
  constructor(contentType) {
229
229
  super();
230
- this.contentType = contentType;
231
- this.arrayBuffersMaxlength = 8;
232
- initArrayBuffers(this);
230
+ const writer = this;
231
+ writer.contentType = contentType;
232
+ writer.arrayBuffersMaxlength = 8;
233
+ initArrayBuffers(writer);
233
234
  }
234
235
 
235
236
  writeUint8Array(array) {
@@ -270,8 +271,10 @@ class ReadableStreamReader {
270
271
  const reader = this;
271
272
  reader.readableStream = readableStream.pipeThrough(new TransformStream({
272
273
  transform(chunk, controller) {
273
- reader.size += chunk.length;
274
- controller.enqueue(chunk);
274
+ if (chunk) {
275
+ reader.size += chunk.length;
276
+ controller.enqueue(chunk);
277
+ }
275
278
  }
276
279
  }));
277
280
  reader.size = 0;
@@ -281,7 +284,7 @@ class ReadableStreamReader {
281
284
  this.initialized = true;
282
285
  }
283
286
 
284
- getStream() {
287
+ getReadable() {
285
288
  return this.readableStream;
286
289
  }
287
290
  }
@@ -290,8 +293,11 @@ class WritableStreamWriter extends Writer {
290
293
 
291
294
  constructor(writable) {
292
295
  super();
293
- const writer = this;
294
- writer.writable = writable;
296
+ Object.defineProperty(this, "writable", {
297
+ get() {
298
+ return writable;
299
+ }
300
+ });
295
301
  }
296
302
 
297
303
  async writeUint8Array(array) {
@@ -301,10 +307,6 @@ class WritableStreamWriter extends Writer {
301
307
  writer.releaseLock();
302
308
  }
303
309
 
304
- getStream() {
305
- return this.writable;
306
- }
307
-
308
310
  async getData() {
309
311
  await this.writable.getWriter().close();
310
312
  return this.writable;
@@ -320,11 +322,11 @@ class FetchReader extends Reader {
320
322
  reader.preventHeadRequest = options.preventHeadRequest;
321
323
  reader.useRangeHeader = options.useRangeHeader;
322
324
  reader.forceRangeRequests = options.forceRangeRequests;
323
- reader.options = Object.assign({}, options);
324
- delete reader.options.preventHeadRequest;
325
- delete reader.options.useRangeHeader;
326
- delete reader.options.forceRangeRequests;
327
- delete reader.options.useXHR;
325
+ reader.options = options = Object.assign({}, options);
326
+ delete options.preventHeadRequest;
327
+ delete options.useRangeHeader;
328
+ delete options.forceRangeRequests;
329
+ delete options.useXHR;
328
330
  }
329
331
 
330
332
  async init() {
@@ -492,12 +494,9 @@ class HttpReader extends Reader {
492
494
 
493
495
  constructor(url, options = {}) {
494
496
  super();
495
- this.url = url;
496
- if (options.useXHR) {
497
- this.reader = new XHRReader(url, options);
498
- } else {
499
- this.reader = new FetchReader(url, options);
500
- }
497
+ const reader = this;
498
+ reader.url = url;
499
+ reader.reader = options.useXHR ? new XHRReader(url, options) : new FetchReader(url, options);
501
500
  }
502
501
 
503
502
  set size(value) {
@@ -588,4 +587,4 @@ export {
588
587
  WritableStreamWriter,
589
588
  ERR_HTTP_RANGE,
590
589
  ERR_NOT_SEEKABLE_READER
591
- };
590
+ };
@@ -1891,7 +1891,7 @@ const ZipDeflate = (() => {
1891
1891
  }
1892
1892
 
1893
1893
  ZStream.prototype = {
1894
- deflateInit: function (level, bits) {
1894
+ deflateInit(level, bits) {
1895
1895
  const that = this;
1896
1896
  that.dstate = new Deflate();
1897
1897
  if (!bits)
@@ -1899,7 +1899,7 @@ const ZipDeflate = (() => {
1899
1899
  return that.dstate.deflateInit(that, level, bits);
1900
1900
  },
1901
1901
 
1902
- deflate: function (flush) {
1902
+ deflate(flush) {
1903
1903
  const that = this;
1904
1904
  if (!that.dstate) {
1905
1905
  return Z_STREAM_ERROR;
@@ -1907,7 +1907,7 @@ const ZipDeflate = (() => {
1907
1907
  return that.dstate.deflate(that, flush);
1908
1908
  },
1909
1909
 
1910
- deflateEnd: function () {
1910
+ deflateEnd() {
1911
1911
  const that = this;
1912
1912
  if (!that.dstate)
1913
1913
  return Z_STREAM_ERROR;
@@ -1916,14 +1916,14 @@ const ZipDeflate = (() => {
1916
1916
  return ret;
1917
1917
  },
1918
1918
 
1919
- deflateParams: function (level, strategy) {
1919
+ deflateParams(level, strategy) {
1920
1920
  const that = this;
1921
1921
  if (!that.dstate)
1922
1922
  return Z_STREAM_ERROR;
1923
1923
  return that.dstate.deflateParams(that, level, strategy);
1924
1924
  },
1925
1925
 
1926
- deflateSetDictionary: function (dictionary, dictLength) {
1926
+ deflateSetDictionary(dictionary, dictLength) {
1927
1927
  const that = this;
1928
1928
  if (!that.dstate)
1929
1929
  return Z_STREAM_ERROR;
@@ -1935,7 +1935,7 @@ const ZipDeflate = (() => {
1935
1935
  // this function so some applications may wish to modify it to avoid
1936
1936
  // allocating a large strm->next_in buffer and copying from it.
1937
1937
  // (See also flush_pending()).
1938
- read_buf: function (buf, start, size) {
1938
+ read_buf(buf, start, size) {
1939
1939
  const that = this;
1940
1940
  let len = that.avail_in;
1941
1941
  if (len > size)
@@ -1953,7 +1953,7 @@ const ZipDeflate = (() => {
1953
1953
  // through this function so some applications may wish to modify it
1954
1954
  // to avoid allocating a large strm->next_out buffer and copying into it.
1955
1955
  // (See also read_buf()).
1956
- flush_pending: function () {
1956
+ flush_pending() {
1957
1957
  const that = this;
1958
1958
  let len = that.dstate.pending;
1959
1959
 
@@ -2055,7 +2055,7 @@ const ZipInflate = (() => {
2055
2055
  }
2056
2056
 
2057
2057
  ZStream.prototype = {
2058
- inflateInit: function (bits) {
2058
+ inflateInit(bits) {
2059
2059
  const that = this;
2060
2060
  that.istate = new Inflate();
2061
2061
  if (!bits)
@@ -2063,14 +2063,14 @@ const ZipInflate = (() => {
2063
2063
  return that.istate.inflateInit(that, bits);
2064
2064
  },
2065
2065
 
2066
- inflate: function (f) {
2066
+ inflate(f) {
2067
2067
  const that = this;
2068
2068
  if (!that.istate)
2069
2069
  return Z_STREAM_ERROR;
2070
2070
  return that.istate.inflate(that, f);
2071
2071
  },
2072
2072
 
2073
- inflateEnd: function () {
2073
+ inflateEnd() {
2074
2074
  const that = this;
2075
2075
  if (!that.istate)
2076
2076
  return Z_STREAM_ERROR;
@@ -2079,23 +2079,23 @@ const ZipInflate = (() => {
2079
2079
  return ret;
2080
2080
  },
2081
2081
 
2082
- inflateSync: function () {
2082
+ inflateSync() {
2083
2083
  const that = this;
2084
2084
  if (!that.istate)
2085
2085
  return Z_STREAM_ERROR;
2086
2086
  return that.istate.inflateSync(that);
2087
2087
  },
2088
- inflateSetDictionary: function (dictionary, dictLength) {
2088
+ inflateSetDictionary(dictionary, dictLength) {
2089
2089
  const that = this;
2090
2090
  if (!that.istate)
2091
2091
  return Z_STREAM_ERROR;
2092
2092
  return that.istate.inflateSetDictionary(that, dictionary, dictLength);
2093
2093
  },
2094
- read_byte: function (start) {
2094
+ read_byte(start) {
2095
2095
  const that = this;
2096
2096
  return that.next_in[start];
2097
2097
  },
2098
- read_buf: function (start, size) {
2098
+ read_buf(start, size) {
2099
2099
  const that = this;
2100
2100
  return that.next_in.subarray(start, start + size);
2101
2101
  }
@@ -232,7 +232,7 @@ hash.sha1.prototype = {
232
232
  * Reset the hash state.
233
233
  * @return this
234
234
  */
235
- reset: function () {
235
+ reset() {
236
236
  const sha1 = this;
237
237
  sha1._h = this._init.slice(0);
238
238
  sha1._buffer = [];
@@ -245,7 +245,7 @@ hash.sha1.prototype = {
245
245
  * @param {bitArray|String} data the data to hash.
246
246
  * @return this
247
247
  */
248
- update: function (data) {
248
+ update(data) {
249
249
  const sha1 = this;
250
250
  if (typeof data === "string") {
251
251
  data = codec.utf8String.toBits(data);
@@ -271,7 +271,7 @@ hash.sha1.prototype = {
271
271
  * Complete hashing and output the hash value.
272
272
  * @return {bitArray} The hash value, an array of 5 big-endian words. TODO
273
273
  */
274
- finalize: function () {
274
+ finalize() {
275
275
  const sha1 = this;
276
276
  let b = sha1._buffer;
277
277
  const h = sha1._h;
@@ -311,7 +311,7 @@ hash.sha1.prototype = {
311
311
  * The SHA-1 logical functions f(0), f(1), ..., f(79).
312
312
  * @private
313
313
  */
314
- _f: function (t, b, c, d) {
314
+ _f(t, b, c, d) {
315
315
  if (t <= 19) {
316
316
  return (b & c) | (~b & d);
317
317
  } else if (t <= 39) {
@@ -327,7 +327,7 @@ hash.sha1.prototype = {
327
327
  * Circular left-shift operator.
328
328
  * @private
329
329
  */
330
- _S: function (n, x) {
330
+ _S(n, x) {
331
331
  return (x << n) | (x >>> 32 - n);
332
332
  },
333
333
 
@@ -336,7 +336,7 @@ hash.sha1.prototype = {
336
336
  * @param {Uint32Array|bitArray} words one block of words.
337
337
  * @private
338
338
  */
339
- _block: function (words) {
339
+ _block(words) {
340
340
  const sha1 = this;
341
341
  const h = sha1._h;
342
342
  // When words is passed to _block, it has 16 elements. SHA1 _block
@@ -66,8 +66,7 @@ class DeflateStream extends TransformStream {
66
66
  readable = pipeCodecStream(codecConstructor, readable, chunkSize);
67
67
  } else {
68
68
  try {
69
- const compressionStream = new CompressionStream(COMPRESSION_FORMAT);
70
- readable = readable.pipeThrough(compressionStream);
69
+ readable = readable.pipeThrough(new CompressionStream(COMPRESSION_FORMAT));
71
70
  } catch (_error) {
72
71
  DEFLATE_RAW_SUPPORTED = false;
73
72
  readable = pipeCodecStream(codecConstructor, readable, chunkSize);
@@ -153,8 +152,8 @@ export {
153
152
  ERR_INVALID_SIGNATURE
154
153
  };
155
154
 
156
- function pipeCodecStream(codecConstructor, stream, chunkSize) {
157
- return stream.pipeThrough(new CodecStream(codecConstructor, { chunkSize }));
155
+ function pipeCodecStream(codecConstructor, readable, chunkSize) {
156
+ return readable.pipeThrough(new CodecStream(codecConstructor, { chunkSize }));
158
157
  }
159
158
 
160
159
  function filterEmptyChunks(readable) {
@@ -110,9 +110,12 @@ async function init(message) {
110
110
 
111
111
  function sendMessage(message) {
112
112
  if (message.data) {
113
- const data = message.data;
113
+ let { data } = message;
114
114
  if (data && data.length) {
115
115
  try {
116
+ if (data.length != data.buffer.byteLength) {
117
+ data = new Uint8Array(data);
118
+ }
116
119
  message.data = data.buffer;
117
120
  postMessage(message, [message.data]);
118
121
  } catch (_error) {
@@ -503,11 +503,11 @@ async function exportZip(zipWriter, entry, totalSize, options) {
503
503
  await zipWriter.add(name, child.reader, Object.assign({
504
504
  directory: child.directory
505
505
  }, Object.assign({}, options, {
506
- onprogress: indexProgress => {
506
+ onprogress: async indexProgress => {
507
507
  if (options.onprogress) {
508
508
  entryOffsets.set(name, indexProgress);
509
509
  try {
510
- options.onprogress(Array.from(entryOffsets.values()).reduce((previousValue, currentValue) => previousValue + currentValue), totalSize);
510
+ await options.onprogress(Array.from(entryOffsets.values()).reduce((previousValue, currentValue) => previousValue + currentValue), totalSize);
511
511
  } catch (_error) {
512
512
  // ignored
513
513
  }
@@ -201,7 +201,7 @@ class ZipReader {
201
201
  offset = endOffset;
202
202
  if (options.onprogress) {
203
203
  try {
204
- options.onprogress(indexFile + 1, filesLength, new Entry(fileEntry));
204
+ await options.onprogress(indexFile + 1, filesLength, new Entry(fileEntry));
205
205
  } catch (_error) {
206
206
  // ignored
207
207
  }
@@ -301,25 +301,31 @@ class ZipEntry {
301
301
  }
302
302
  const dataOffset = offset + 30 + localDirectory.filenameLength + localDirectory.extraFieldLength;
303
303
  const size = () => compressedSize;
304
- const readable = reader.getStream({ offset: dataOffset, size, chunkSize: getChunkSize(config) });
305
- const writable = writer.getStream();
304
+ const readable = reader.getReadable({ offset: dataOffset, size, chunkSize: getChunkSize(config) });
305
+ const { writable } = writer;
306
306
  const signal = getOptionValue(zipEntry, options, "signal");
307
307
  if (!writer.initialized) {
308
308
  await writer.init();
309
309
  }
310
- await runCodec(config.Inflate, readable, writable, {
311
- codecType: CODEC_INFLATE,
312
- password,
313
- zipCrypto,
314
- encryptionStrength: extraFieldAES && extraFieldAES.strength,
315
- signed: getOptionValue(zipEntry, options, "checkSignature"),
316
- passwordVerification: zipCrypto && (bitFlag.dataDescriptor ? ((rawLastModDate >>> 8) & 0xFF) : ((signature >>> 24) & 0xFF)),
317
- signature,
318
- compressed: compressionMethod != 0,
319
- encrypted,
320
- useWebWorkers: getOptionValue(zipEntry, options, "useWebWorkers"),
321
- useCompressionStream: getOptionValue(zipEntry, options, "useCompressionStream")
322
- }, config, { signal, onprogress: options.onprogress, size });
310
+ const { onstart, onprogress, onend } = options;
311
+ const codecOptions = {
312
+ options: {
313
+ codecType: CODEC_INFLATE,
314
+ password,
315
+ zipCrypto,
316
+ encryptionStrength: extraFieldAES && extraFieldAES.strength,
317
+ signed: getOptionValue(zipEntry, options, "checkSignature"),
318
+ passwordVerification: zipCrypto && (bitFlag.dataDescriptor ? ((rawLastModDate >>> 8) & 0xFF) : ((signature >>> 24) & 0xFF)),
319
+ signature,
320
+ compressed: compressionMethod != 0,
321
+ encrypted,
322
+ useWebWorkers: getOptionValue(zipEntry, options, "useWebWorkers"),
323
+ useCompressionStream: getOptionValue(zipEntry, options, "useCompressionStream")
324
+ },
325
+ config,
326
+ streamOptions: { signal, size, onstart, onprogress, onend }
327
+ };
328
+ await runCodec(config.Inflate, { readable, writable }, codecOptions);
323
329
  return writer.getData();
324
330
  }
325
331
  }
@@ -209,6 +209,9 @@ async function addFile(zipWriter, name, reader, options) {
209
209
  }
210
210
  let maximumCompressedSize = 0;
211
211
  let keepOrder = getOptionValue(zipWriter, options, "keepOrder");
212
+ if (keepOrder === undefined) {
213
+ keepOrder = true;
214
+ }
212
215
  let uncompressedSize = 0;
213
216
  let msDosCompatible = getOptionValue(zipWriter, options, "msDosCompatible");
214
217
  if (msDosCompatible === undefined) {
@@ -292,11 +295,10 @@ async function getFileEntry(zipWriter, name, reader, options) {
292
295
  try {
293
296
  let lockPreviousFileEntry;
294
297
  let fileWriter;
295
- let lockCurrentFileEntry;
296
298
  if (options.keepOrder) {
297
299
  lockPreviousFileEntry = previousFileEntry && previousFileEntry.lock;
298
300
  }
299
- fileEntry.lock = lockCurrentFileEntry = new Promise(resolve => resolveLockCurrentFileEntry = resolve);
301
+ fileEntry.lock = new Promise(resolve => resolveLockCurrentFileEntry = resolve);
300
302
  if (options.bufferedWrite || zipWriter.lockWrite || !options.dataDescriptor) {
301
303
  fileWriter = new BlobWriter();
302
304
  fileWriter.init();
@@ -308,8 +310,7 @@ async function getFileEntry(zipWriter, name, reader, options) {
308
310
  }
309
311
  fileWriter = writer;
310
312
  }
311
- fileEntry = await createFileEntry(reader, fileWriter.getStream(), zipWriter.config, options);
312
- fileEntry.lock = lockCurrentFileEntry;
313
+ fileEntry = await createFileEntry(reader, fileWriter.writable, zipWriter.config, options);
313
314
  files.set(name, fileEntry);
314
315
  fileEntry.filename = name;
315
316
  if (bufferedWrite) {
@@ -324,7 +325,7 @@ async function getFileEntry(zipWriter, name, reader, options) {
324
325
  }
325
326
  } while (pendingFileEntry && pendingFileEntry.lock);
326
327
  fileEntry.writingBufferedData = true;
327
- const writable = writer.getStream();
328
+ const { writable } = writer;
328
329
  if (!options.dataDescriptor) {
329
330
  const headerLength = 26;
330
331
  const arrayBuffer = await sliceAsArrayBuffer(blob, 0, headerLength);
@@ -388,7 +389,9 @@ async function createFileEntry(reader, writable, config, options) {
388
389
  rawComment,
389
390
  rawExtraField,
390
391
  useWebWorkers,
392
+ onstart,
391
393
  onprogress,
394
+ onend,
392
395
  signal,
393
396
  encryptionStrength,
394
397
  extendedTimestamp,
@@ -521,20 +524,25 @@ async function createFileEntry(reader, writable, config, options) {
521
524
  await writeUint8Array(writable, localHeaderArray);
522
525
  fileEntry.dataWritten = true;
523
526
  const size = () => reader.size;
524
- const readable = reader.getStream({ offset: 0, size, chunkSize: getChunkSize(config) });
525
- result = await runCodec(config.Deflate, readable, writable, {
526
- codecType: CODEC_DEFLATE,
527
- level,
528
- password,
529
- encryptionStrength,
530
- zipCrypto: encrypted && zipCrypto,
531
- passwordVerification: encrypted && zipCrypto && (rawLastModDate >> 8) & 0xFF,
532
- signed: true,
533
- compressed,
534
- encrypted,
535
- useWebWorkers,
536
- useCompressionStream
537
- }, config, { signal, onprogress, size });
527
+ const readable = reader.getReadable({ offset: 0, size, chunkSize: getChunkSize(config) });
528
+ const codecOptions = {
529
+ options: {
530
+ codecType: CODEC_DEFLATE,
531
+ level,
532
+ password,
533
+ encryptionStrength,
534
+ zipCrypto: encrypted && zipCrypto,
535
+ passwordVerification: encrypted && zipCrypto && (rawLastModDate >> 8) & 0xFF,
536
+ signed: true,
537
+ compressed,
538
+ encrypted,
539
+ useWebWorkers,
540
+ useCompressionStream
541
+ },
542
+ config,
543
+ streamOptions: { signal, size, onstart, onprogress, onend }
544
+ };
545
+ result = await runCodec(config.Deflate, { readable, writable }, codecOptions);
538
546
  uncompressedSize = fileEntry.uncompressedSize = reader.size;
539
547
  compressedSize = result.length;
540
548
  } else {
@@ -677,7 +685,7 @@ async function closeFile(zipWriter, comment, options) {
677
685
  offset += 46 + rawFilename.length + extraFieldLength + rawComment.length;
678
686
  if (options.onprogress) {
679
687
  try {
680
- options.onprogress(indexFileEntry + 1, files.size, new Entry(fileEntry));
688
+ await options.onprogress(indexFileEntry + 1, files.size, new Entry(fileEntry));
681
689
  } catch (_error) {
682
690
  // ignored
683
691
  }
@@ -705,7 +713,7 @@ async function closeFile(zipWriter, comment, options) {
705
713
  setUint16(directoryView, offset + 10, filesLength);
706
714
  setUint32(directoryView, offset + 12, directoryDataLength);
707
715
  setUint32(directoryView, offset + 16, directoryOffset);
708
- const writable = zipWriter.writer.getStream();
716
+ const { writable } = zipWriter.writer;
709
717
  await writeUint8Array(writable, directoryArray);
710
718
  if (comment && comment.length) {
711
719
  await writeUint8Array(writable, comment);