@zip.js/zip.js 2.6.5 → 2.6.8

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) {
@@ -8273,7 +8277,7 @@
8273
8277
  commentLength: getUint16(directoryView, offset + 32),
8274
8278
  directory: msDosCompatible && ((getUint8(directoryView, offset + 38) & FILE_ATTR_MSDOS_DIR_MASK) == FILE_ATTR_MSDOS_DIR_MASK),
8275
8279
  offset: getUint32(directoryView, offset + 42) + prependedDataLength,
8276
- internalFileAttribute: getUint32(directoryView, offset + 34),
8280
+ internalFileAttribute: getUint16(directoryView, offset + 36),
8277
8281
  externalFileAttribute: getUint32(directoryView, offset + 38),
8278
8282
  rawFilename: directoryArray.subarray(filenameOffset, extraFieldOffset),
8279
8283
  filenameUTF8: languageEncodingFlag,
@@ -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 });
@@ -9099,6 +9097,7 @@
9099
9097
  rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
9100
9098
  }
9101
9099
  const fileEntry = {
9100
+ lock: pendingFileEntry.lock,
9102
9101
  version: version || VERSION_DEFLATE,
9103
9102
  versionMadeBy,
9104
9103
  zip64,
@@ -9175,7 +9174,7 @@
9175
9174
  let compressedSize = 0;
9176
9175
  if (reader) {
9177
9176
  reader.chunkSize = getChunkSize(config);
9178
- await writeUint8Array(writable, localHeaderArray);
9177
+ await writeData(writable, localHeaderArray);
9179
9178
  fileEntry.dataWritten = pendingFileEntry.dataWritten = true;
9180
9179
  const size = () => reader.size;
9181
9180
  const readable = reader.readable;
@@ -9202,7 +9201,7 @@
9202
9201
  uncompressedSize = fileEntry.uncompressedSize = reader.size = readable.size();
9203
9202
  compressedSize = result.size;
9204
9203
  } else {
9205
- await writeUint8Array(writable, localHeaderArray);
9204
+ await writeData(writable, localHeaderArray);
9206
9205
  fileEntry.dataWritten = pendingFileEntry.dataWritten = true;
9207
9206
  }
9208
9207
  let dataDescriptorArray = new Uint8Array();
@@ -9246,7 +9245,7 @@
9246
9245
  }
9247
9246
  }
9248
9247
  if (dataDescriptor) {
9249
- await writeUint8Array(writable, dataDescriptorArray);
9248
+ await writeData(writable, dataDescriptorArray);
9250
9249
  }
9251
9250
  const length = localHeaderArray.length + compressedSize + dataDescriptorArray.length;
9252
9251
  Object.assign(fileEntry, {
@@ -9321,7 +9320,7 @@
9321
9320
  arraySet(directoryArray, headerArray, offset + 6);
9322
9321
  setUint16(directoryView, offset + 30, extraFieldLength);
9323
9322
  setUint16(directoryView, offset + 32, rawComment.length);
9324
- setUint32(directoryView, offset + 34, internalFileAttribute);
9323
+ setUint16(directoryView, offset + 36, internalFileAttribute);
9325
9324
  if (externalFileAttribute) {
9326
9325
  setUint32(directoryView, offset + 38, externalFileAttribute);
9327
9326
  } else if (directory && msDosCompatible) {
@@ -9378,9 +9377,9 @@
9378
9377
  }
9379
9378
  }
9380
9379
  const { writable } = zipWriter.writer;
9381
- await writeUint8Array(writable, directoryArray);
9380
+ await writeData(writable, directoryArray);
9382
9381
  if (comment && comment.length) {
9383
- await writeUint8Array(writable, comment);
9382
+ await writeData(writable, comment);
9384
9383
  }
9385
9384
  }
9386
9385
 
@@ -9392,7 +9391,7 @@
9392
9391
  }
9393
9392
  }
9394
9393
 
9395
- async function writeUint8Array(writable, array) {
9394
+ async function writeData(writable, array) {
9396
9395
  const streamWriter = writable.getWriter();
9397
9396
  await streamWriter.ready;
9398
9397
  await streamWriter.write(array);
@@ -9465,8 +9464,6 @@
9465
9464
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9466
9465
  */
9467
9466
 
9468
- const CHUNK_SIZE = 512 * 1024;
9469
-
9470
9467
  class ZipEntry {
9471
9468
 
9472
9469
  constructor(fs, name, params, parent) {
@@ -9539,13 +9536,13 @@
9539
9536
  if (!writer || (writer.constructor == zipEntry.Writer && zipEntry.data)) {
9540
9537
  return zipEntry.data;
9541
9538
  } 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);
9539
+ const reader = zipEntry.reader = new zipEntry.Reader(zipEntry.data, options);
9540
+ await Promise.all([initStream(reader), initStream(writer)]);
9541
+ zipEntry.uncompressedSize = reader.size;
9542
+ const readable = reader.readable;
9543
+ readable.size = () => reader.size;
9544
+ await readable.pipeTo(writer.writable);
9545
+ return writer.getData ? writer.getData() : writer.writable;
9549
9546
  }
9550
9547
  }
9551
9548
 
@@ -9565,6 +9562,10 @@
9565
9562
  return this.getData(new Uint8ArrayWriter(), options);
9566
9563
  }
9567
9564
 
9565
+ getWritable(writable = new WritableStream(), options) {
9566
+ return this.getData({ writable }, options);
9567
+ }
9568
+
9568
9569
  replaceBlob(blob) {
9569
9570
  Object.assign(this, {
9570
9571
  data: blob,
@@ -9600,6 +9601,15 @@
9600
9601
  reader: null
9601
9602
  });
9602
9603
  }
9604
+
9605
+ replaceReadable(readable) {
9606
+ Object.assign(this, {
9607
+ data: null,
9608
+ Reader: function () { return { readable }; },
9609
+ Writer: null,
9610
+ reader: null
9611
+ });
9612
+ }
9603
9613
  }
9604
9614
 
9605
9615
  class ZipDirectoryEntry extends ZipEntry {
@@ -9656,6 +9666,12 @@
9656
9666
  });
9657
9667
  }
9658
9668
 
9669
+ addReadable(name, readable) {
9670
+ return addChild(this, name, {
9671
+ Reader: function () { return { readable }; }
9672
+ });
9673
+ }
9674
+
9659
9675
  addFileSystemEntry(fileSystemEntry) {
9660
9676
  return addFileSystemEntry(this, fileSystemEntry);
9661
9677
  }
@@ -9680,6 +9696,10 @@
9680
9696
  await this.importZip(new HttpReader(url, options), options);
9681
9697
  }
9682
9698
 
9699
+ async importReadable(readable, options = {}) {
9700
+ await this.importZip({ readable }, options);
9701
+ }
9702
+
9683
9703
  exportBlob(options = {}) {
9684
9704
  return this.exportZip(new BlobWriter("application/zip"), options);
9685
9705
  }
@@ -9692,10 +9712,13 @@
9692
9712
  return this.exportZip(new Uint8ArrayWriter(), options);
9693
9713
  }
9694
9714
 
9715
+ async exportWritable(writable = new WritableStream(), options = {}) {
9716
+ await this.exportZip({ writable }, options);
9717
+ return writable;
9718
+ }
9719
+
9695
9720
  async importZip(reader, options) {
9696
- if (!reader.initialized) {
9697
- await reader.init();
9698
- }
9721
+ await initStream(reader);
9699
9722
  const zipReader = new ZipReader(reader, options);
9700
9723
  const entries = await zipReader.getEntries();
9701
9724
  entries.forEach((entry) => {
@@ -9714,12 +9737,11 @@
9714
9737
 
9715
9738
  async exportZip(writer, options) {
9716
9739
  const zipEntry = this;
9717
- await initReaders(zipEntry);
9718
- await writer.init();
9740
+ await Promise.all([initReaders(zipEntry), initStream(writer)]);
9719
9741
  const zipWriter = new ZipWriter(writer, options);
9720
9742
  await exportZip(zipWriter, zipEntry, getTotalSize([zipEntry], "uncompressedSize"), options);
9721
9743
  await zipWriter.close();
9722
- return writer.getData();
9744
+ return writer.getData ? writer.getData() : writer.writable;
9723
9745
  }
9724
9746
 
9725
9747
  getChildByName(name) {
@@ -9809,6 +9831,10 @@
9809
9831
  return this.root.addHttpContent(name, url, options);
9810
9832
  }
9811
9833
 
9834
+ addReadable(name, readable) {
9835
+ return this.root.addReadable(name, readable);
9836
+ }
9837
+
9812
9838
  addFileSystemEntry(fileSystemEntry) {
9813
9839
  return this.root.addFileSystemEntry(fileSystemEntry);
9814
9840
  }
@@ -9832,6 +9858,11 @@
9832
9858
  await this.root.importHttpContent(url, options);
9833
9859
  }
9834
9860
 
9861
+ async importReadable(readable, options) {
9862
+ resetFS(this);
9863
+ await this.root.importReadable(readable, options);
9864
+ }
9865
+
9835
9866
  exportBlob(options) {
9836
9867
  return this.root.exportBlob(options);
9837
9868
  }
@@ -9839,6 +9870,14 @@
9839
9870
  exportData64URI(options) {
9840
9871
  return this.root.exportData64URI(options);
9841
9872
  }
9873
+
9874
+ exportUint8Array(options) {
9875
+ return this.root.exportUint8Array(options);
9876
+ }
9877
+
9878
+ exportWritable(writable, options) {
9879
+ return this.root.exportWritable(writable, options);
9880
+ }
9842
9881
  }
9843
9882
 
9844
9883
  const fs = { FS, ZipDirectoryEntry, ZipFileEntry };
@@ -9866,6 +9905,7 @@
9866
9905
  }
9867
9906
 
9868
9907
  async init() {
9908
+ super.init();
9869
9909
  const zipBlobReader = this;
9870
9910
  zipBlobReader.size = zipBlobReader.entry.uncompressedSize;
9871
9911
  const data = await zipBlobReader.entry.getData(new BlobWriter(), Object.assign({}, zipBlobReader.options, options));
@@ -9885,9 +9925,9 @@
9885
9925
  if (child.directory) {
9886
9926
  await initReaders(child);
9887
9927
  } else {
9888
- child.reader = new child.Reader(child.data);
9889
- await child.reader.init();
9890
- child.uncompressedSize = child.reader.size;
9928
+ const reader = child.reader = new child.Reader(child.data);
9929
+ await initStream(reader);
9930
+ child.uncompressedSize = reader.size;
9891
9931
  }
9892
9932
  }
9893
9933
  }
@@ -9996,21 +10036,6 @@
9996
10036
  fs.root = new ZipDirectoryEntry(fs);
9997
10037
  }
9998
10038
 
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
10039
  function addChild(parent, name, params, directory) {
10015
10040
  if (parent.directory) {
10016
10041
  return directory ? new ZipDirectoryEntry(parent.fs, name, params, parent) : new ZipFileEntry(parent.fs, name, params, parent);