@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.
package/dist/zip-fs.js CHANGED
@@ -2705,6 +2705,12 @@
2705
2705
  return protocol == "http:" || protocol == "https:";
2706
2706
  }
2707
2707
 
2708
+ async function initStream(stream) {
2709
+ if (stream.init && !stream.initialized) {
2710
+ await stream.init();
2711
+ }
2712
+ }
2713
+
2708
2714
  /*
2709
2715
  Copyright (c) 2022 Gildas Lormeau. All rights reserved.
2710
2716
 
@@ -2949,9 +2955,7 @@
2949
2955
  const zipReader = this;
2950
2956
  let { reader } = zipReader;
2951
2957
  const { config } = zipReader;
2952
- if (!reader.initialized && reader.init) {
2953
- await reader.init();
2954
- }
2958
+ await initStream(reader);
2955
2959
  if (reader.size === undefined || !reader.readUint8Array) {
2956
2960
  const blob = await new Response(reader.readable).blob();
2957
2961
  reader = new BlobReader(blob);
@@ -3017,7 +3021,7 @@
3017
3021
  throw new Error(ERR_BAD_FORMAT);
3018
3022
  }
3019
3023
  const filenameEncoding = getOptionValue$1(zipReader, options, "filenameEncoding");
3020
- const commentEncoding = getOptionValue$1(zipReader, options, "commentEncoding");
3024
+ const commentEncoding = getOptionValue$1(zipReader, options, "commentEncoding");
3021
3025
  for (let indexFile = 0; indexFile < filesLength; indexFile++) {
3022
3026
  const fileEntry = new ZipEntry$1(reader, config, zipReader.options);
3023
3027
  if (getUint32(directoryView, offset) != CENTRAL_FILE_HEADER_SIGNATURE) {
@@ -3155,9 +3159,7 @@
3155
3159
  readable.size = size;
3156
3160
  const { writable } = writer;
3157
3161
  const signal = getOptionValue$1(zipEntry, options, "signal");
3158
- if (writer.init && !writer.initialized) {
3159
- await writer.init();
3160
- }
3162
+ await initStream(writer);
3161
3163
  const { onstart, onprogress, onend } = options;
3162
3164
  const workerOptions = {
3163
3165
  options: {
@@ -3617,9 +3619,7 @@
3617
3619
  const internalFileAttribute = getOptionValue(zipWriter, options, "internalFileAttribute") || 0;
3618
3620
  const externalFileAttribute = getOptionValue(zipWriter, options, "externalFileAttribute") || 0;
3619
3621
  if (reader) {
3620
- if (reader.init && !reader.initialized) {
3621
- await reader.init();
3622
- }
3622
+ await initStream(reader);
3623
3623
  if (reader.size === undefined) {
3624
3624
  options.dataDescriptor = true;
3625
3625
  } else {
@@ -3716,9 +3716,7 @@
3716
3716
  bufferedWrite = true;
3717
3717
  } else {
3718
3718
  zipWriter.lockWriter = Promise.resolve();
3719
- if (writer.init && !writer.initialized) {
3720
- await writer.init();
3721
- }
3719
+ await initStream(writer);
3722
3720
  fileWriter = writer;
3723
3721
  }
3724
3722
  fileEntry = await createFileEntry(reader, fileEntry, fileWriter.writable, zipWriter.config, options);
@@ -3744,7 +3742,7 @@
3744
3742
  setUint32(arrayBufferView, 18, fileEntry.compressedSize);
3745
3743
  setUint32(arrayBufferView, 22, fileEntry.uncompressedSize);
3746
3744
  }
3747
- await writeUint8Array(writable, new Uint8Array(arrayBuffer));
3745
+ await writeData(writable, new Uint8Array(arrayBuffer));
3748
3746
  blob = blob.slice(headerLength);
3749
3747
  }
3750
3748
  await blob.stream().pipeTo(writable, { preventClose: true, signal });
@@ -3940,7 +3938,7 @@
3940
3938
  let compressedSize = 0;
3941
3939
  if (reader) {
3942
3940
  reader.chunkSize = getChunkSize(config);
3943
- await writeUint8Array(writable, localHeaderArray);
3941
+ await writeData(writable, localHeaderArray);
3944
3942
  fileEntry.dataWritten = pendingFileEntry.dataWritten = true;
3945
3943
  const size = () => reader.size;
3946
3944
  const readable = reader.readable;
@@ -3967,7 +3965,7 @@
3967
3965
  uncompressedSize = fileEntry.uncompressedSize = reader.size = readable.size();
3968
3966
  compressedSize = result.size;
3969
3967
  } else {
3970
- await writeUint8Array(writable, localHeaderArray);
3968
+ await writeData(writable, localHeaderArray);
3971
3969
  fileEntry.dataWritten = pendingFileEntry.dataWritten = true;
3972
3970
  }
3973
3971
  let dataDescriptorArray = new Uint8Array();
@@ -4011,7 +4009,7 @@
4011
4009
  }
4012
4010
  }
4013
4011
  if (dataDescriptor) {
4014
- await writeUint8Array(writable, dataDescriptorArray);
4012
+ await writeData(writable, dataDescriptorArray);
4015
4013
  }
4016
4014
  const length = localHeaderArray.length + compressedSize + dataDescriptorArray.length;
4017
4015
  Object.assign(fileEntry, {
@@ -4143,9 +4141,9 @@
4143
4141
  }
4144
4142
  }
4145
4143
  const { writable } = zipWriter.writer;
4146
- await writeUint8Array(writable, directoryArray);
4144
+ await writeData(writable, directoryArray);
4147
4145
  if (comment && comment.length) {
4148
- await writeUint8Array(writable, comment);
4146
+ await writeData(writable, comment);
4149
4147
  }
4150
4148
  }
4151
4149
 
@@ -4157,7 +4155,7 @@
4157
4155
  }
4158
4156
  }
4159
4157
 
4160
- async function writeUint8Array(writable, array) {
4158
+ async function writeData(writable, array) {
4161
4159
  const streamWriter = writable.getWriter();
4162
4160
  await streamWriter.ready;
4163
4161
  await streamWriter.write(array);
@@ -4230,8 +4228,6 @@
4230
4228
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4231
4229
  */
4232
4230
 
4233
- const CHUNK_SIZE = 512 * 1024;
4234
-
4235
4231
  class ZipEntry {
4236
4232
 
4237
4233
  constructor(fs, name, params, parent) {
@@ -4304,13 +4300,13 @@
4304
4300
  if (!writer || (writer.constructor == zipEntry.Writer && zipEntry.data)) {
4305
4301
  return zipEntry.data;
4306
4302
  } else {
4307
- zipEntry.reader = new zipEntry.Reader(zipEntry.data, options);
4308
- await zipEntry.reader.init();
4309
- if (writer.init && !writer.initialized) {
4310
- await writer.init();
4311
- }
4312
- zipEntry.uncompressedSize = zipEntry.reader.size;
4313
- return pipe(zipEntry.reader, writer);
4303
+ const reader = zipEntry.reader = new zipEntry.Reader(zipEntry.data, options);
4304
+ await Promise.all([initStream(reader), initStream(writer)]);
4305
+ zipEntry.uncompressedSize = reader.size;
4306
+ const readable = reader.readable;
4307
+ readable.size = () => reader.size;
4308
+ await readable.pipeTo(writer.writable);
4309
+ return writer.getData ? writer.getData() : writer.writable;
4314
4310
  }
4315
4311
  }
4316
4312
 
@@ -4330,6 +4326,10 @@
4330
4326
  return this.getData(new Uint8ArrayWriter(), options);
4331
4327
  }
4332
4328
 
4329
+ getWritable(writable = new WritableStream(), options) {
4330
+ return this.getData({ writable }, options);
4331
+ }
4332
+
4333
4333
  replaceBlob(blob) {
4334
4334
  Object.assign(this, {
4335
4335
  data: blob,
@@ -4365,6 +4365,15 @@
4365
4365
  reader: null
4366
4366
  });
4367
4367
  }
4368
+
4369
+ replaceReadable(readable) {
4370
+ Object.assign(this, {
4371
+ data: null,
4372
+ Reader: function () { return { readable }; },
4373
+ Writer: null,
4374
+ reader: null
4375
+ });
4376
+ }
4368
4377
  }
4369
4378
 
4370
4379
  class ZipDirectoryEntry extends ZipEntry {
@@ -4421,6 +4430,12 @@
4421
4430
  });
4422
4431
  }
4423
4432
 
4433
+ addReadable(name, readable) {
4434
+ return addChild(this, name, {
4435
+ Reader: function () { return { readable }; }
4436
+ });
4437
+ }
4438
+
4424
4439
  addFileSystemEntry(fileSystemEntry) {
4425
4440
  return addFileSystemEntry(this, fileSystemEntry);
4426
4441
  }
@@ -4445,6 +4460,10 @@
4445
4460
  await this.importZip(new HttpReader(url, options), options);
4446
4461
  }
4447
4462
 
4463
+ async importReadable(readable, options = {}) {
4464
+ await this.importZip({ readable }, options);
4465
+ }
4466
+
4448
4467
  exportBlob(options = {}) {
4449
4468
  return this.exportZip(new BlobWriter("application/zip"), options);
4450
4469
  }
@@ -4457,10 +4476,13 @@
4457
4476
  return this.exportZip(new Uint8ArrayWriter(), options);
4458
4477
  }
4459
4478
 
4479
+ async exportWritable(writable = new WritableStream(), options = {}) {
4480
+ await this.exportZip({ writable }, options);
4481
+ return writable;
4482
+ }
4483
+
4460
4484
  async importZip(reader, options) {
4461
- if (!reader.initialized) {
4462
- await reader.init();
4463
- }
4485
+ await initStream(reader);
4464
4486
  const zipReader = new ZipReader(reader, options);
4465
4487
  const entries = await zipReader.getEntries();
4466
4488
  entries.forEach((entry) => {
@@ -4479,12 +4501,11 @@
4479
4501
 
4480
4502
  async exportZip(writer, options) {
4481
4503
  const zipEntry = this;
4482
- await initReaders(zipEntry);
4483
- await writer.init();
4504
+ await Promise.all([initReaders(zipEntry), initStream(writer)]);
4484
4505
  const zipWriter = new ZipWriter(writer, options);
4485
4506
  await exportZip(zipWriter, zipEntry, getTotalSize([zipEntry], "uncompressedSize"), options);
4486
4507
  await zipWriter.close();
4487
- return writer.getData();
4508
+ return writer.getData ? writer.getData() : writer.writable;
4488
4509
  }
4489
4510
 
4490
4511
  getChildByName(name) {
@@ -4574,6 +4595,10 @@
4574
4595
  return this.root.addHttpContent(name, url, options);
4575
4596
  }
4576
4597
 
4598
+ addReadable(name, readable) {
4599
+ return this.root.addReadable(name, readable);
4600
+ }
4601
+
4577
4602
  addFileSystemEntry(fileSystemEntry) {
4578
4603
  return this.root.addFileSystemEntry(fileSystemEntry);
4579
4604
  }
@@ -4597,6 +4622,11 @@
4597
4622
  await this.root.importHttpContent(url, options);
4598
4623
  }
4599
4624
 
4625
+ async importReadable(readable, options) {
4626
+ resetFS(this);
4627
+ await this.root.importReadable(readable, options);
4628
+ }
4629
+
4600
4630
  exportBlob(options) {
4601
4631
  return this.root.exportBlob(options);
4602
4632
  }
@@ -4604,6 +4634,14 @@
4604
4634
  exportData64URI(options) {
4605
4635
  return this.root.exportData64URI(options);
4606
4636
  }
4637
+
4638
+ exportUint8Array(options) {
4639
+ return this.root.exportUint8Array(options);
4640
+ }
4641
+
4642
+ exportWritable(writable, options) {
4643
+ return this.root.exportWritable(writable, options);
4644
+ }
4607
4645
  }
4608
4646
 
4609
4647
  const fs = { FS, ZipDirectoryEntry, ZipFileEntry };
@@ -4631,6 +4669,7 @@
4631
4669
  }
4632
4670
 
4633
4671
  async init() {
4672
+ super.init();
4634
4673
  const zipBlobReader = this;
4635
4674
  zipBlobReader.size = zipBlobReader.entry.uncompressedSize;
4636
4675
  const data = await zipBlobReader.entry.getData(new BlobWriter(), Object.assign({}, zipBlobReader.options, options));
@@ -4650,9 +4689,9 @@
4650
4689
  if (child.directory) {
4651
4690
  await initReaders(child);
4652
4691
  } else {
4653
- child.reader = new child.Reader(child.data);
4654
- await child.reader.init();
4655
- child.uncompressedSize = child.reader.size;
4692
+ const reader = child.reader = new child.Reader(child.data);
4693
+ await initStream(reader);
4694
+ child.uncompressedSize = reader.size;
4656
4695
  }
4657
4696
  }
4658
4697
  }
@@ -4761,21 +4800,6 @@
4761
4800
  fs.root = new ZipDirectoryEntry(fs);
4762
4801
  }
4763
4802
 
4764
- function pipe(reader, writer) {
4765
- return copyChunk();
4766
-
4767
- async function copyChunk(chunkIndex = 0) {
4768
- const index = chunkIndex * CHUNK_SIZE;
4769
- if (index < reader.size) {
4770
- const array = await reader.readUint8Array(index, Math.min(CHUNK_SIZE, reader.size - index));
4771
- await writer.writeUint8Array(array);
4772
- return copyChunk(chunkIndex + 1);
4773
- } else {
4774
- return writer.getData();
4775
- }
4776
- }
4777
- }
4778
-
4779
4803
  function addChild(parent, name, params, directory) {
4780
4804
  if (parent.directory) {
4781
4805
  return directory ? new ZipDirectoryEntry(parent.fs, name, params, parent) : new ZipFileEntry(parent.fs, name, params, parent);
@@ -4861,6 +4885,7 @@
4861
4885
  exports.fs = fs;
4862
4886
  exports.getMimeType = getMimeType;
4863
4887
  exports.initShimAsyncCodec = streamCodecShim;
4888
+ exports.initStream = initStream;
4864
4889
  exports.terminateWorkers = terminateWorkers;
4865
4890
 
4866
4891
  Object.defineProperty(exports, '__esModule', { value: true });