@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.
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) {
@@ -3038,7 +3042,7 @@
3038
3042
  commentLength: getUint16(directoryView, offset + 32),
3039
3043
  directory: msDosCompatible && ((getUint8(directoryView, offset + 38) & FILE_ATTR_MSDOS_DIR_MASK) == FILE_ATTR_MSDOS_DIR_MASK),
3040
3044
  offset: getUint32(directoryView, offset + 42) + prependedDataLength,
3041
- internalFileAttribute: getUint32(directoryView, offset + 34),
3045
+ internalFileAttribute: getUint16(directoryView, offset + 36),
3042
3046
  externalFileAttribute: getUint32(directoryView, offset + 38),
3043
3047
  rawFilename: directoryArray.subarray(filenameOffset, extraFieldOffset),
3044
3048
  filenameUTF8: languageEncodingFlag,
@@ -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 });
@@ -3864,6 +3862,7 @@
3864
3862
  rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
3865
3863
  }
3866
3864
  const fileEntry = {
3865
+ lock: pendingFileEntry.lock,
3867
3866
  version: version || VERSION_DEFLATE,
3868
3867
  versionMadeBy,
3869
3868
  zip64,
@@ -3940,7 +3939,7 @@
3940
3939
  let compressedSize = 0;
3941
3940
  if (reader) {
3942
3941
  reader.chunkSize = getChunkSize(config);
3943
- await writeUint8Array(writable, localHeaderArray);
3942
+ await writeData(writable, localHeaderArray);
3944
3943
  fileEntry.dataWritten = pendingFileEntry.dataWritten = true;
3945
3944
  const size = () => reader.size;
3946
3945
  const readable = reader.readable;
@@ -3967,7 +3966,7 @@
3967
3966
  uncompressedSize = fileEntry.uncompressedSize = reader.size = readable.size();
3968
3967
  compressedSize = result.size;
3969
3968
  } else {
3970
- await writeUint8Array(writable, localHeaderArray);
3969
+ await writeData(writable, localHeaderArray);
3971
3970
  fileEntry.dataWritten = pendingFileEntry.dataWritten = true;
3972
3971
  }
3973
3972
  let dataDescriptorArray = new Uint8Array();
@@ -4011,7 +4010,7 @@
4011
4010
  }
4012
4011
  }
4013
4012
  if (dataDescriptor) {
4014
- await writeUint8Array(writable, dataDescriptorArray);
4013
+ await writeData(writable, dataDescriptorArray);
4015
4014
  }
4016
4015
  const length = localHeaderArray.length + compressedSize + dataDescriptorArray.length;
4017
4016
  Object.assign(fileEntry, {
@@ -4086,7 +4085,7 @@
4086
4085
  arraySet(directoryArray, headerArray, offset + 6);
4087
4086
  setUint16(directoryView, offset + 30, extraFieldLength);
4088
4087
  setUint16(directoryView, offset + 32, rawComment.length);
4089
- setUint32(directoryView, offset + 34, internalFileAttribute);
4088
+ setUint16(directoryView, offset + 36, internalFileAttribute);
4090
4089
  if (externalFileAttribute) {
4091
4090
  setUint32(directoryView, offset + 38, externalFileAttribute);
4092
4091
  } else if (directory && msDosCompatible) {
@@ -4143,9 +4142,9 @@
4143
4142
  }
4144
4143
  }
4145
4144
  const { writable } = zipWriter.writer;
4146
- await writeUint8Array(writable, directoryArray);
4145
+ await writeData(writable, directoryArray);
4147
4146
  if (comment && comment.length) {
4148
- await writeUint8Array(writable, comment);
4147
+ await writeData(writable, comment);
4149
4148
  }
4150
4149
  }
4151
4150
 
@@ -4157,7 +4156,7 @@
4157
4156
  }
4158
4157
  }
4159
4158
 
4160
- async function writeUint8Array(writable, array) {
4159
+ async function writeData(writable, array) {
4161
4160
  const streamWriter = writable.getWriter();
4162
4161
  await streamWriter.ready;
4163
4162
  await streamWriter.write(array);
@@ -4230,8 +4229,6 @@
4230
4229
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4231
4230
  */
4232
4231
 
4233
- const CHUNK_SIZE = 512 * 1024;
4234
-
4235
4232
  class ZipEntry {
4236
4233
 
4237
4234
  constructor(fs, name, params, parent) {
@@ -4304,13 +4301,13 @@
4304
4301
  if (!writer || (writer.constructor == zipEntry.Writer && zipEntry.data)) {
4305
4302
  return zipEntry.data;
4306
4303
  } 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);
4304
+ const reader = zipEntry.reader = new zipEntry.Reader(zipEntry.data, options);
4305
+ await Promise.all([initStream(reader), initStream(writer)]);
4306
+ zipEntry.uncompressedSize = reader.size;
4307
+ const readable = reader.readable;
4308
+ readable.size = () => reader.size;
4309
+ await readable.pipeTo(writer.writable);
4310
+ return writer.getData ? writer.getData() : writer.writable;
4314
4311
  }
4315
4312
  }
4316
4313
 
@@ -4330,6 +4327,10 @@
4330
4327
  return this.getData(new Uint8ArrayWriter(), options);
4331
4328
  }
4332
4329
 
4330
+ getWritable(writable = new WritableStream(), options) {
4331
+ return this.getData({ writable }, options);
4332
+ }
4333
+
4333
4334
  replaceBlob(blob) {
4334
4335
  Object.assign(this, {
4335
4336
  data: blob,
@@ -4365,6 +4366,15 @@
4365
4366
  reader: null
4366
4367
  });
4367
4368
  }
4369
+
4370
+ replaceReadable(readable) {
4371
+ Object.assign(this, {
4372
+ data: null,
4373
+ Reader: function () { return { readable }; },
4374
+ Writer: null,
4375
+ reader: null
4376
+ });
4377
+ }
4368
4378
  }
4369
4379
 
4370
4380
  class ZipDirectoryEntry extends ZipEntry {
@@ -4421,6 +4431,12 @@
4421
4431
  });
4422
4432
  }
4423
4433
 
4434
+ addReadable(name, readable) {
4435
+ return addChild(this, name, {
4436
+ Reader: function () { return { readable }; }
4437
+ });
4438
+ }
4439
+
4424
4440
  addFileSystemEntry(fileSystemEntry) {
4425
4441
  return addFileSystemEntry(this, fileSystemEntry);
4426
4442
  }
@@ -4445,6 +4461,10 @@
4445
4461
  await this.importZip(new HttpReader(url, options), options);
4446
4462
  }
4447
4463
 
4464
+ async importReadable(readable, options = {}) {
4465
+ await this.importZip({ readable }, options);
4466
+ }
4467
+
4448
4468
  exportBlob(options = {}) {
4449
4469
  return this.exportZip(new BlobWriter("application/zip"), options);
4450
4470
  }
@@ -4457,10 +4477,13 @@
4457
4477
  return this.exportZip(new Uint8ArrayWriter(), options);
4458
4478
  }
4459
4479
 
4480
+ async exportWritable(writable = new WritableStream(), options = {}) {
4481
+ await this.exportZip({ writable }, options);
4482
+ return writable;
4483
+ }
4484
+
4460
4485
  async importZip(reader, options) {
4461
- if (!reader.initialized) {
4462
- await reader.init();
4463
- }
4486
+ await initStream(reader);
4464
4487
  const zipReader = new ZipReader(reader, options);
4465
4488
  const entries = await zipReader.getEntries();
4466
4489
  entries.forEach((entry) => {
@@ -4479,12 +4502,11 @@
4479
4502
 
4480
4503
  async exportZip(writer, options) {
4481
4504
  const zipEntry = this;
4482
- await initReaders(zipEntry);
4483
- await writer.init();
4505
+ await Promise.all([initReaders(zipEntry), initStream(writer)]);
4484
4506
  const zipWriter = new ZipWriter(writer, options);
4485
4507
  await exportZip(zipWriter, zipEntry, getTotalSize([zipEntry], "uncompressedSize"), options);
4486
4508
  await zipWriter.close();
4487
- return writer.getData();
4509
+ return writer.getData ? writer.getData() : writer.writable;
4488
4510
  }
4489
4511
 
4490
4512
  getChildByName(name) {
@@ -4574,6 +4596,10 @@
4574
4596
  return this.root.addHttpContent(name, url, options);
4575
4597
  }
4576
4598
 
4599
+ addReadable(name, readable) {
4600
+ return this.root.addReadable(name, readable);
4601
+ }
4602
+
4577
4603
  addFileSystemEntry(fileSystemEntry) {
4578
4604
  return this.root.addFileSystemEntry(fileSystemEntry);
4579
4605
  }
@@ -4597,6 +4623,11 @@
4597
4623
  await this.root.importHttpContent(url, options);
4598
4624
  }
4599
4625
 
4626
+ async importReadable(readable, options) {
4627
+ resetFS(this);
4628
+ await this.root.importReadable(readable, options);
4629
+ }
4630
+
4600
4631
  exportBlob(options) {
4601
4632
  return this.root.exportBlob(options);
4602
4633
  }
@@ -4604,6 +4635,14 @@
4604
4635
  exportData64URI(options) {
4605
4636
  return this.root.exportData64URI(options);
4606
4637
  }
4638
+
4639
+ exportUint8Array(options) {
4640
+ return this.root.exportUint8Array(options);
4641
+ }
4642
+
4643
+ exportWritable(writable, options) {
4644
+ return this.root.exportWritable(writable, options);
4645
+ }
4607
4646
  }
4608
4647
 
4609
4648
  const fs = { FS, ZipDirectoryEntry, ZipFileEntry };
@@ -4631,6 +4670,7 @@
4631
4670
  }
4632
4671
 
4633
4672
  async init() {
4673
+ super.init();
4634
4674
  const zipBlobReader = this;
4635
4675
  zipBlobReader.size = zipBlobReader.entry.uncompressedSize;
4636
4676
  const data = await zipBlobReader.entry.getData(new BlobWriter(), Object.assign({}, zipBlobReader.options, options));
@@ -4650,9 +4690,9 @@
4650
4690
  if (child.directory) {
4651
4691
  await initReaders(child);
4652
4692
  } else {
4653
- child.reader = new child.Reader(child.data);
4654
- await child.reader.init();
4655
- child.uncompressedSize = child.reader.size;
4693
+ const reader = child.reader = new child.Reader(child.data);
4694
+ await initStream(reader);
4695
+ child.uncompressedSize = reader.size;
4656
4696
  }
4657
4697
  }
4658
4698
  }
@@ -4761,21 +4801,6 @@
4761
4801
  fs.root = new ZipDirectoryEntry(fs);
4762
4802
  }
4763
4803
 
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
4804
  function addChild(parent, name, params, directory) {
4780
4805
  if (parent.directory) {
4781
4806
  return directory ? new ZipDirectoryEntry(parent.fs, name, params, parent) : new ZipFileEntry(parent.fs, name, params, parent);
@@ -4861,6 +4886,7 @@
4861
4886
  exports.fs = fs;
4862
4887
  exports.getMimeType = getMimeType;
4863
4888
  exports.initShimAsyncCodec = streamCodecShim;
4889
+ exports.initStream = initStream;
4864
4890
  exports.terminateWorkers = terminateWorkers;
4865
4891
 
4866
4892
  Object.defineProperty(exports, '__esModule', { value: true });