@zip.js/zip.js 2.5.11 → 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) {
@@ -283,7 +284,7 @@ class ReadableStreamReader {
283
284
  this.initialized = true;
284
285
  }
285
286
 
286
- getStream() {
287
+ getReadable() {
287
288
  return this.readableStream;
288
289
  }
289
290
  }
@@ -292,8 +293,11 @@ class WritableStreamWriter extends Writer {
292
293
 
293
294
  constructor(writable) {
294
295
  super();
295
- const writer = this;
296
- writer.writable = writable;
296
+ Object.defineProperty(this, "writable", {
297
+ get() {
298
+ return writable;
299
+ }
300
+ });
297
301
  }
298
302
 
299
303
  async writeUint8Array(array) {
@@ -303,10 +307,6 @@ class WritableStreamWriter extends Writer {
303
307
  writer.releaseLock();
304
308
  }
305
309
 
306
- getStream() {
307
- return this.writable;
308
- }
309
-
310
310
  async getData() {
311
311
  await this.writable.getWriter().close();
312
312
  return this.writable;
@@ -322,11 +322,11 @@ class FetchReader extends Reader {
322
322
  reader.preventHeadRequest = options.preventHeadRequest;
323
323
  reader.useRangeHeader = options.useRangeHeader;
324
324
  reader.forceRangeRequests = options.forceRangeRequests;
325
- reader.options = Object.assign({}, options);
326
- delete reader.options.preventHeadRequest;
327
- delete reader.options.useRangeHeader;
328
- delete reader.options.forceRangeRequests;
329
- 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;
330
330
  }
331
331
 
332
332
  async init() {
@@ -494,12 +494,9 @@ class HttpReader extends Reader {
494
494
 
495
495
  constructor(url, options = {}) {
496
496
  super();
497
- this.url = url;
498
- if (options.useXHR) {
499
- this.reader = new XHRReader(url, options);
500
- } else {
501
- this.reader = new FetchReader(url, options);
502
- }
497
+ const reader = this;
498
+ reader.url = url;
499
+ reader.reader = options.useXHR ? new XHRReader(url, options) : new FetchReader(url, options);
503
500
  }
504
501
 
505
502
  set size(value) {
@@ -590,4 +587,4 @@ export {
590
587
  WritableStreamWriter,
591
588
  ERR_HTTP_RANGE,
592
589
  ERR_NOT_SEEKABLE_READER
593
- };
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) {
@@ -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
  }
@@ -295,11 +295,10 @@ async function getFileEntry(zipWriter, name, reader, options) {
295
295
  try {
296
296
  let lockPreviousFileEntry;
297
297
  let fileWriter;
298
- let lockCurrentFileEntry;
299
298
  if (options.keepOrder) {
300
299
  lockPreviousFileEntry = previousFileEntry && previousFileEntry.lock;
301
300
  }
302
- fileEntry.lock = lockCurrentFileEntry = new Promise(resolve => resolveLockCurrentFileEntry = resolve);
301
+ fileEntry.lock = new Promise(resolve => resolveLockCurrentFileEntry = resolve);
303
302
  if (options.bufferedWrite || zipWriter.lockWrite || !options.dataDescriptor) {
304
303
  fileWriter = new BlobWriter();
305
304
  fileWriter.init();
@@ -311,8 +310,7 @@ async function getFileEntry(zipWriter, name, reader, options) {
311
310
  }
312
311
  fileWriter = writer;
313
312
  }
314
- fileEntry = await createFileEntry(reader, fileWriter.getStream(), zipWriter.config, options);
315
- fileEntry.lock = lockCurrentFileEntry;
313
+ fileEntry = await createFileEntry(reader, fileWriter.writable, zipWriter.config, options);
316
314
  files.set(name, fileEntry);
317
315
  fileEntry.filename = name;
318
316
  if (bufferedWrite) {
@@ -327,7 +325,7 @@ async function getFileEntry(zipWriter, name, reader, options) {
327
325
  }
328
326
  } while (pendingFileEntry && pendingFileEntry.lock);
329
327
  fileEntry.writingBufferedData = true;
330
- const writable = writer.getStream();
328
+ const { writable } = writer;
331
329
  if (!options.dataDescriptor) {
332
330
  const headerLength = 26;
333
331
  const arrayBuffer = await sliceAsArrayBuffer(blob, 0, headerLength);
@@ -391,7 +389,9 @@ async function createFileEntry(reader, writable, config, options) {
391
389
  rawComment,
392
390
  rawExtraField,
393
391
  useWebWorkers,
392
+ onstart,
394
393
  onprogress,
394
+ onend,
395
395
  signal,
396
396
  encryptionStrength,
397
397
  extendedTimestamp,
@@ -524,20 +524,25 @@ async function createFileEntry(reader, writable, config, options) {
524
524
  await writeUint8Array(writable, localHeaderArray);
525
525
  fileEntry.dataWritten = true;
526
526
  const size = () => reader.size;
527
- const readable = reader.getStream({ offset: 0, size, chunkSize: getChunkSize(config) });
528
- result = await runCodec(config.Deflate, readable, writable, {
529
- codecType: CODEC_DEFLATE,
530
- level,
531
- password,
532
- encryptionStrength,
533
- zipCrypto: encrypted && zipCrypto,
534
- passwordVerification: encrypted && zipCrypto && (rawLastModDate >> 8) & 0xFF,
535
- signed: true,
536
- compressed,
537
- encrypted,
538
- useWebWorkers,
539
- useCompressionStream
540
- }, 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);
541
546
  uncompressedSize = fileEntry.uncompressedSize = reader.size;
542
547
  compressedSize = result.length;
543
548
  } else {
@@ -680,7 +685,7 @@ async function closeFile(zipWriter, comment, options) {
680
685
  offset += 46 + rawFilename.length + extraFieldLength + rawComment.length;
681
686
  if (options.onprogress) {
682
687
  try {
683
- options.onprogress(indexFileEntry + 1, files.size, new Entry(fileEntry));
688
+ await options.onprogress(indexFileEntry + 1, files.size, new Entry(fileEntry));
684
689
  } catch (_error) {
685
690
  // ignored
686
691
  }
@@ -708,7 +713,7 @@ async function closeFile(zipWriter, comment, options) {
708
713
  setUint16(directoryView, offset + 10, filesLength);
709
714
  setUint32(directoryView, offset + 12, directoryDataLength);
710
715
  setUint32(directoryView, offset + 16, directoryOffset);
711
- const writable = zipWriter.writer.getStream();
716
+ const { writable } = zipWriter.writer;
712
717
  await writeUint8Array(writable, directoryArray);
713
718
  if (comment && comment.length) {
714
719
  await writeUint8Array(writable, comment);