@zip.js/zip.js 2.6.5 → 2.6.6

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.
@@ -7940,6 +7940,12 @@
7940
7940
  return protocol == "http:" || protocol == "https:";
7941
7941
  }
7942
7942
 
7943
+ async function initStream(stream) {
7944
+ if (stream.init && !stream.initialized) {
7945
+ await stream.init();
7946
+ }
7947
+ }
7948
+
7943
7949
  /*
7944
7950
  Copyright (c) 2022 Gildas Lormeau. All rights reserved.
7945
7951
 
@@ -8184,9 +8190,7 @@
8184
8190
  const zipReader = this;
8185
8191
  let { reader } = zipReader;
8186
8192
  const { config } = zipReader;
8187
- if (!reader.initialized && reader.init) {
8188
- await reader.init();
8189
- }
8193
+ await initStream(reader);
8190
8194
  if (reader.size === undefined || !reader.readUint8Array) {
8191
8195
  const blob = await new Response(reader.readable).blob();
8192
8196
  reader = new BlobReader(blob);
@@ -8252,7 +8256,7 @@
8252
8256
  throw new Error(ERR_BAD_FORMAT);
8253
8257
  }
8254
8258
  const filenameEncoding = getOptionValue$1(zipReader, options, "filenameEncoding");
8255
- const commentEncoding = getOptionValue$1(zipReader, options, "commentEncoding");
8259
+ const commentEncoding = getOptionValue$1(zipReader, options, "commentEncoding");
8256
8260
  for (let indexFile = 0; indexFile < filesLength; indexFile++) {
8257
8261
  const fileEntry = new ZipEntry$1(reader, config, zipReader.options);
8258
8262
  if (getUint32(directoryView, offset) != CENTRAL_FILE_HEADER_SIGNATURE) {
@@ -8390,9 +8394,7 @@
8390
8394
  readable.size = size;
8391
8395
  const { writable } = writer;
8392
8396
  const signal = getOptionValue$1(zipEntry, options, "signal");
8393
- if (writer.init && !writer.initialized) {
8394
- await writer.init();
8395
- }
8397
+ await initStream(writer);
8396
8398
  const { onstart, onprogress, onend } = options;
8397
8399
  const workerOptions = {
8398
8400
  options: {
@@ -8852,9 +8854,7 @@
8852
8854
  const internalFileAttribute = getOptionValue(zipWriter, options, "internalFileAttribute") || 0;
8853
8855
  const externalFileAttribute = getOptionValue(zipWriter, options, "externalFileAttribute") || 0;
8854
8856
  if (reader) {
8855
- if (reader.init && !reader.initialized) {
8856
- await reader.init();
8857
- }
8857
+ await initStream(reader);
8858
8858
  if (reader.size === undefined) {
8859
8859
  options.dataDescriptor = true;
8860
8860
  } else {
@@ -8951,9 +8951,7 @@
8951
8951
  bufferedWrite = true;
8952
8952
  } else {
8953
8953
  zipWriter.lockWriter = Promise.resolve();
8954
- if (writer.init && !writer.initialized) {
8955
- await writer.init();
8956
- }
8954
+ await initStream(writer);
8957
8955
  fileWriter = writer;
8958
8956
  }
8959
8957
  fileEntry = await createFileEntry(reader, fileEntry, fileWriter.writable, zipWriter.config, options);
@@ -8979,7 +8977,7 @@
8979
8977
  setUint32(arrayBufferView, 18, fileEntry.compressedSize);
8980
8978
  setUint32(arrayBufferView, 22, fileEntry.uncompressedSize);
8981
8979
  }
8982
- await writeUint8Array(writable, new Uint8Array(arrayBuffer));
8980
+ await writeData(writable, new Uint8Array(arrayBuffer));
8983
8981
  blob = blob.slice(headerLength);
8984
8982
  }
8985
8983
  await blob.stream().pipeTo(writable, { preventClose: true, signal });
@@ -9175,7 +9173,7 @@
9175
9173
  let compressedSize = 0;
9176
9174
  if (reader) {
9177
9175
  reader.chunkSize = getChunkSize(config);
9178
- await writeUint8Array(writable, localHeaderArray);
9176
+ await writeData(writable, localHeaderArray);
9179
9177
  fileEntry.dataWritten = pendingFileEntry.dataWritten = true;
9180
9178
  const size = () => reader.size;
9181
9179
  const readable = reader.readable;
@@ -9202,7 +9200,7 @@
9202
9200
  uncompressedSize = fileEntry.uncompressedSize = reader.size = readable.size();
9203
9201
  compressedSize = result.size;
9204
9202
  } else {
9205
- await writeUint8Array(writable, localHeaderArray);
9203
+ await writeData(writable, localHeaderArray);
9206
9204
  fileEntry.dataWritten = pendingFileEntry.dataWritten = true;
9207
9205
  }
9208
9206
  let dataDescriptorArray = new Uint8Array();
@@ -9246,7 +9244,7 @@
9246
9244
  }
9247
9245
  }
9248
9246
  if (dataDescriptor) {
9249
- await writeUint8Array(writable, dataDescriptorArray);
9247
+ await writeData(writable, dataDescriptorArray);
9250
9248
  }
9251
9249
  const length = localHeaderArray.length + compressedSize + dataDescriptorArray.length;
9252
9250
  Object.assign(fileEntry, {
@@ -9378,9 +9376,9 @@
9378
9376
  }
9379
9377
  }
9380
9378
  const { writable } = zipWriter.writer;
9381
- await writeUint8Array(writable, directoryArray);
9379
+ await writeData(writable, directoryArray);
9382
9380
  if (comment && comment.length) {
9383
- await writeUint8Array(writable, comment);
9381
+ await writeData(writable, comment);
9384
9382
  }
9385
9383
  }
9386
9384
 
@@ -9392,7 +9390,7 @@
9392
9390
  }
9393
9391
  }
9394
9392
 
9395
- async function writeUint8Array(writable, array) {
9393
+ async function writeData(writable, array) {
9396
9394
  const streamWriter = writable.getWriter();
9397
9395
  await streamWriter.ready;
9398
9396
  await streamWriter.write(array);
@@ -9465,8 +9463,6 @@
9465
9463
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9466
9464
  */
9467
9465
 
9468
- const CHUNK_SIZE = 512 * 1024;
9469
-
9470
9466
  class ZipEntry {
9471
9467
 
9472
9468
  constructor(fs, name, params, parent) {
@@ -9539,13 +9535,13 @@
9539
9535
  if (!writer || (writer.constructor == zipEntry.Writer && zipEntry.data)) {
9540
9536
  return zipEntry.data;
9541
9537
  } else {
9542
- zipEntry.reader = new zipEntry.Reader(zipEntry.data, options);
9543
- await zipEntry.reader.init();
9544
- if (writer.init && !writer.initialized) {
9545
- await writer.init();
9546
- }
9547
- zipEntry.uncompressedSize = zipEntry.reader.size;
9548
- return pipe(zipEntry.reader, writer);
9538
+ const reader = zipEntry.reader = new zipEntry.Reader(zipEntry.data, options);
9539
+ await Promise.all([initStream(reader), initStream(writer)]);
9540
+ zipEntry.uncompressedSize = reader.size;
9541
+ const readable = reader.readable;
9542
+ readable.size = () => reader.size;
9543
+ await readable.pipeTo(writer.writable);
9544
+ return writer.getData ? writer.getData() : writer.writable;
9549
9545
  }
9550
9546
  }
9551
9547
 
@@ -9565,6 +9561,10 @@
9565
9561
  return this.getData(new Uint8ArrayWriter(), options);
9566
9562
  }
9567
9563
 
9564
+ getWritable(writable = new WritableStream(), options) {
9565
+ return this.getData({ writable }, options);
9566
+ }
9567
+
9568
9568
  replaceBlob(blob) {
9569
9569
  Object.assign(this, {
9570
9570
  data: blob,
@@ -9600,6 +9600,15 @@
9600
9600
  reader: null
9601
9601
  });
9602
9602
  }
9603
+
9604
+ replaceReadable(readable) {
9605
+ Object.assign(this, {
9606
+ data: null,
9607
+ Reader: function () { return { readable }; },
9608
+ Writer: null,
9609
+ reader: null
9610
+ });
9611
+ }
9603
9612
  }
9604
9613
 
9605
9614
  class ZipDirectoryEntry extends ZipEntry {
@@ -9656,6 +9665,12 @@
9656
9665
  });
9657
9666
  }
9658
9667
 
9668
+ addReadable(name, readable) {
9669
+ return addChild(this, name, {
9670
+ Reader: function () { return { readable }; }
9671
+ });
9672
+ }
9673
+
9659
9674
  addFileSystemEntry(fileSystemEntry) {
9660
9675
  return addFileSystemEntry(this, fileSystemEntry);
9661
9676
  }
@@ -9680,6 +9695,10 @@
9680
9695
  await this.importZip(new HttpReader(url, options), options);
9681
9696
  }
9682
9697
 
9698
+ async importReadable(readable, options = {}) {
9699
+ await this.importZip({ readable }, options);
9700
+ }
9701
+
9683
9702
  exportBlob(options = {}) {
9684
9703
  return this.exportZip(new BlobWriter("application/zip"), options);
9685
9704
  }
@@ -9692,10 +9711,13 @@
9692
9711
  return this.exportZip(new Uint8ArrayWriter(), options);
9693
9712
  }
9694
9713
 
9714
+ async exportWritable(writable = new WritableStream(), options = {}) {
9715
+ await this.exportZip({ writable }, options);
9716
+ return writable;
9717
+ }
9718
+
9695
9719
  async importZip(reader, options) {
9696
- if (!reader.initialized) {
9697
- await reader.init();
9698
- }
9720
+ await initStream(reader);
9699
9721
  const zipReader = new ZipReader(reader, options);
9700
9722
  const entries = await zipReader.getEntries();
9701
9723
  entries.forEach((entry) => {
@@ -9714,12 +9736,11 @@
9714
9736
 
9715
9737
  async exportZip(writer, options) {
9716
9738
  const zipEntry = this;
9717
- await initReaders(zipEntry);
9718
- await writer.init();
9739
+ await Promise.all([initReaders(zipEntry), initStream(writer)]);
9719
9740
  const zipWriter = new ZipWriter(writer, options);
9720
9741
  await exportZip(zipWriter, zipEntry, getTotalSize([zipEntry], "uncompressedSize"), options);
9721
9742
  await zipWriter.close();
9722
- return writer.getData();
9743
+ return writer.getData ? writer.getData() : writer.writable;
9723
9744
  }
9724
9745
 
9725
9746
  getChildByName(name) {
@@ -9809,6 +9830,10 @@
9809
9830
  return this.root.addHttpContent(name, url, options);
9810
9831
  }
9811
9832
 
9833
+ addReadable(name, readable) {
9834
+ return this.root.addReadable(name, readable);
9835
+ }
9836
+
9812
9837
  addFileSystemEntry(fileSystemEntry) {
9813
9838
  return this.root.addFileSystemEntry(fileSystemEntry);
9814
9839
  }
@@ -9832,6 +9857,11 @@
9832
9857
  await this.root.importHttpContent(url, options);
9833
9858
  }
9834
9859
 
9860
+ async importReadable(readable, options) {
9861
+ resetFS(this);
9862
+ await this.root.importReadable(readable, options);
9863
+ }
9864
+
9835
9865
  exportBlob(options) {
9836
9866
  return this.root.exportBlob(options);
9837
9867
  }
@@ -9839,6 +9869,14 @@
9839
9869
  exportData64URI(options) {
9840
9870
  return this.root.exportData64URI(options);
9841
9871
  }
9872
+
9873
+ exportUint8Array(options) {
9874
+ return this.root.exportUint8Array(options);
9875
+ }
9876
+
9877
+ exportWritable(writable, options) {
9878
+ return this.root.exportWritable(writable, options);
9879
+ }
9842
9880
  }
9843
9881
 
9844
9882
  const fs = { FS, ZipDirectoryEntry, ZipFileEntry };
@@ -9866,6 +9904,7 @@
9866
9904
  }
9867
9905
 
9868
9906
  async init() {
9907
+ super.init();
9869
9908
  const zipBlobReader = this;
9870
9909
  zipBlobReader.size = zipBlobReader.entry.uncompressedSize;
9871
9910
  const data = await zipBlobReader.entry.getData(new BlobWriter(), Object.assign({}, zipBlobReader.options, options));
@@ -9885,9 +9924,9 @@
9885
9924
  if (child.directory) {
9886
9925
  await initReaders(child);
9887
9926
  } else {
9888
- child.reader = new child.Reader(child.data);
9889
- await child.reader.init();
9890
- child.uncompressedSize = child.reader.size;
9927
+ const reader = child.reader = new child.Reader(child.data);
9928
+ await initStream(reader);
9929
+ child.uncompressedSize = reader.size;
9891
9930
  }
9892
9931
  }
9893
9932
  }
@@ -9996,21 +10035,6 @@
9996
10035
  fs.root = new ZipDirectoryEntry(fs);
9997
10036
  }
9998
10037
 
9999
- function pipe(reader, writer) {
10000
- return copyChunk();
10001
-
10002
- async function copyChunk(chunkIndex = 0) {
10003
- const index = chunkIndex * CHUNK_SIZE;
10004
- if (index < reader.size) {
10005
- const array = await reader.readUint8Array(index, Math.min(CHUNK_SIZE, reader.size - index));
10006
- await writer.writeUint8Array(array);
10007
- return copyChunk(chunkIndex + 1);
10008
- } else {
10009
- return writer.getData();
10010
- }
10011
- }
10012
- }
10013
-
10014
10038
  function addChild(parent, name, params, directory) {
10015
10039
  if (parent.directory) {
10016
10040
  return directory ? new ZipDirectoryEntry(parent.fs, name, params, parent) : new ZipFileEntry(parent.fs, name, params, parent);