@zip.js/zip.js 2.7.34 → 2.7.35

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/index.cjs CHANGED
@@ -8919,8 +8919,8 @@ class SplitDataWriter extends Stream {
8919
8919
 
8920
8920
  constructor(writerGenerator, maxSize = 4294967295) {
8921
8921
  super();
8922
- const zipWriter = this;
8923
- Object.assign(zipWriter, {
8922
+ const writer = this;
8923
+ Object.assign(writer, {
8924
8924
  diskNumber: 0,
8925
8925
  diskOffset: 0,
8926
8926
  size: 0,
@@ -8930,7 +8930,7 @@ class SplitDataWriter extends Stream {
8930
8930
  let diskSourceWriter, diskWritable, diskWriter;
8931
8931
  const writable = new WritableStream({
8932
8932
  async write(chunk) {
8933
- const { availableSize } = zipWriter;
8933
+ const { availableSize } = writer;
8934
8934
  if (!diskWriter) {
8935
8935
  const { value, done } = await writerGenerator.next();
8936
8936
  if (done && !value) {
@@ -8939,9 +8939,9 @@ class SplitDataWriter extends Stream {
8939
8939
  diskSourceWriter = value;
8940
8940
  diskSourceWriter.size = 0;
8941
8941
  if (diskSourceWriter.maxSize) {
8942
- zipWriter.maxSize = diskSourceWriter.maxSize;
8942
+ writer.maxSize = diskSourceWriter.maxSize;
8943
8943
  }
8944
- zipWriter.availableSize = zipWriter.maxSize;
8944
+ writer.availableSize = writer.maxSize;
8945
8945
  await initStream(diskSourceWriter);
8946
8946
  diskWritable = value.writable;
8947
8947
  diskWriter = diskWritable.getWriter();
@@ -8950,8 +8950,8 @@ class SplitDataWriter extends Stream {
8950
8950
  } else if (chunk.length >= availableSize) {
8951
8951
  await writeChunk(chunk.slice(0, availableSize));
8952
8952
  await closeDisk();
8953
- zipWriter.diskOffset += diskSourceWriter.size;
8954
- zipWriter.diskNumber++;
8953
+ writer.diskOffset += diskSourceWriter.size;
8954
+ writer.diskNumber++;
8955
8955
  diskWriter = null;
8956
8956
  await this.write(chunk.slice(availableSize));
8957
8957
  } else {
@@ -8963,7 +8963,7 @@ class SplitDataWriter extends Stream {
8963
8963
  await closeDisk();
8964
8964
  }
8965
8965
  });
8966
- Object.defineProperty(zipWriter, PROPERTY_NAME_WRITABLE, {
8966
+ Object.defineProperty(writer, PROPERTY_NAME_WRITABLE, {
8967
8967
  get() {
8968
8968
  return writable;
8969
8969
  }
@@ -8975,8 +8975,8 @@ class SplitDataWriter extends Stream {
8975
8975
  await diskWriter.ready;
8976
8976
  await diskWriter.write(chunk);
8977
8977
  diskSourceWriter.size += chunkLength;
8978
- zipWriter.size += chunkLength;
8979
- zipWriter.availableSize -= chunkLength;
8978
+ writer.size += chunkLength;
8979
+ writer.availableSize -= chunkLength;
8980
8980
  }
8981
8981
  }
8982
8982
 
@@ -8996,6 +8996,8 @@ function isHttpFamily(url) {
8996
8996
  async function initStream(stream, initSize) {
8997
8997
  if (stream.init && !stream.initialized) {
8998
8998
  await stream.init(initSize);
8999
+ } else {
9000
+ return Promise.resolve();
8999
9001
  }
9000
9002
  }
9001
9003
 
@@ -9197,8 +9199,8 @@ class Entry {
9197
9199
  1. Redistributions of source code must retain the above copyright notice,
9198
9200
  this list of conditions and the following disclaimer.
9199
9201
 
9200
- 2. Redistributions in binary form must reproduce the above copyright
9201
- notice, this list of conditions and the following disclaimer in
9202
+ 2. Redistributions in binary form must reproduce the above copyright
9203
+ notice, this list of conditions and the following disclaimer in
9202
9204
  the documentation and/or other materials provided with the distribution.
9203
9205
 
9204
9206
  3. The names of the authors may not be used to endorse or promote products
@@ -9327,7 +9329,7 @@ class ZipReader {
9327
9329
  }
9328
9330
  if (directoryDataOffset >= reader.size) {
9329
9331
  prependedDataLength = reader.size - directoryDataOffset - directoryDataLength - END_OF_CENTRAL_DIR_LENGTH;
9330
- directoryDataOffset = reader.size - directoryDataLength - END_OF_CENTRAL_DIR_LENGTH;
9332
+ directoryDataOffset = reader.size - directoryDataLength - END_OF_CENTRAL_DIR_LENGTH;
9331
9333
  }
9332
9334
  if (expectedLastDiskNumber != lastDiskNumber) {
9333
9335
  throw new Error(ERR_SPLIT_ZIP_FILE);
@@ -9444,6 +9446,34 @@ class ZipReader {
9444
9446
  }
9445
9447
  }
9446
9448
 
9449
+ class ZipReaderStream {
9450
+
9451
+ constructor(options = {}) {
9452
+ const { readable, writable } = new TransformStream();
9453
+ const gen = new ZipReader(readable, options).getEntriesGenerator();
9454
+ this.readable = new ReadableStream({
9455
+ async pull(controller) {
9456
+ const { done, value } = await gen.next();
9457
+ if (done)
9458
+ return controller.close();
9459
+ const chunk = {
9460
+ ...value,
9461
+ readable: (function () {
9462
+ const { readable, writable } = new TransformStream();
9463
+ if (value.getData) {
9464
+ value.getData(writable);
9465
+ return readable;
9466
+ }
9467
+ })()
9468
+ };
9469
+ delete chunk.getData;
9470
+ controller.enqueue(chunk);
9471
+ }
9472
+ });
9473
+ this.writable = writable;
9474
+ }
9475
+ }
9476
+
9447
9477
  class ZipEntry$1 {
9448
9478
 
9449
9479
  constructor(reader, config, options) {
@@ -9833,8 +9863,8 @@ function getDataView$1(array) {
9833
9863
  1. Redistributions of source code must retain the above copyright notice,
9834
9864
  this list of conditions and the following disclaimer.
9835
9865
 
9836
- 2. Redistributions in binary form must reproduce the above copyright
9837
- notice, this list of conditions and the following disclaimer in
9866
+ 2. Redistributions in binary form must reproduce the above copyright
9867
+ notice, this list of conditions and the following disclaimer in
9838
9868
  the documentation and/or other materials provided with the distribution.
9839
9869
 
9840
9870
  3. The names of the authors may not be used to endorse or promote products
@@ -9871,9 +9901,12 @@ class ZipWriter {
9871
9901
 
9872
9902
  constructor(writer, options = {}) {
9873
9903
  writer = initWriter(writer);
9904
+ const addSplitZipSignature =
9905
+ writer.availableSize !== UNDEFINED_VALUE && writer.availableSize > 0 && writer.availableSize !== Infinity &&
9906
+ writer.maxSize !== UNDEFINED_VALUE && writer.maxSize > 0 && writer.maxSize !== Infinity;
9874
9907
  Object.assign(this, {
9875
9908
  writer,
9876
- addSplitZipSignature: writer instanceof SplitDataWriter,
9909
+ addSplitZipSignature,
9877
9910
  options,
9878
9911
  config: getConfiguration(),
9879
9912
  files: new Map(),
@@ -9936,6 +9969,33 @@ class ZipWriter {
9936
9969
  }
9937
9970
  }
9938
9971
 
9972
+ class ZipWriterStream {
9973
+
9974
+ constructor(options = {}) {
9975
+ const { readable, writable } = new TransformStream();
9976
+ this.readable = readable;
9977
+ this.zipWriter = new ZipWriter(writable, options);
9978
+ }
9979
+
9980
+ transform(path) {
9981
+ const { readable, writable } = new TransformStream({
9982
+ flush: () => { this.zipWriter.close(); }
9983
+ });
9984
+ this.zipWriter.add(path, readable);
9985
+ return { readable: this.readable, writable };
9986
+ }
9987
+
9988
+ writable(path) {
9989
+ const { readable, writable } = new TransformStream();
9990
+ this.zipWriter.add(path, readable);
9991
+ return writable;
9992
+ }
9993
+
9994
+ close(comment = undefined, options = {}) {
9995
+ return this.zipWriter.close(comment, options);
9996
+ }
9997
+ }
9998
+
9939
9999
  async function addFile(zipWriter, name, reader, options) {
9940
10000
  name = name.trim();
9941
10001
  if (options.directory && (!name.endsWith(DIRECTORY_SIGNATURE))) {
@@ -10106,6 +10166,7 @@ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
10106
10166
  let writingBufferedEntryData;
10107
10167
  let writingEntryData;
10108
10168
  let fileWriter;
10169
+ let blobPromise;
10109
10170
  files.set(name, fileEntry);
10110
10171
  try {
10111
10172
  let lockPreviousFileEntry;
@@ -10114,7 +10175,8 @@ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
10114
10175
  requestLockCurrentFileEntry();
10115
10176
  }
10116
10177
  if ((options.bufferedWrite || zipWriter.writerLocked || (zipWriter.bufferedWrites && keepOrder) || !dataDescriptor) && !usdz) {
10117
- fileWriter = new BlobWriter();
10178
+ fileWriter = new TransformStream();
10179
+ blobPromise = new Response(fileWriter.readable).blob();
10118
10180
  fileWriter.writable.size = 0;
10119
10181
  bufferedWrite = true;
10120
10182
  zipWriter.bufferedWrites++;
@@ -10150,7 +10212,7 @@ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
10150
10212
  fileEntry.filename = name;
10151
10213
  if (bufferedWrite) {
10152
10214
  await fileWriter.writable.getWriter().close();
10153
- let blob = await fileWriter.getData();
10215
+ let blob = await blobPromise;
10154
10216
  await lockPreviousFileEntry;
10155
10217
  await requestLockWriter();
10156
10218
  writingBufferedEntryData = true;
@@ -11741,8 +11803,8 @@ function addChild(parent, name, params, directory) {
11741
11803
  1. Redistributions of source code must retain the above copyright notice,
11742
11804
  this list of conditions and the following disclaimer.
11743
11805
 
11744
- 2. Redistributions in binary form must reproduce the above copyright
11745
- notice, this list of conditions and the following disclaimer in
11806
+ 2. Redistributions in binary form must reproduce the above copyright
11807
+ notice, this list of conditions and the following disclaimer in
11746
11808
  the documentation and/or other materials provided with the distribution.
11747
11809
 
11748
11810
  3. The names of the authors may not be used to endorse or promote products
@@ -11814,7 +11876,9 @@ exports.Uint8ArrayReader = Uint8ArrayReader;
11814
11876
  exports.Uint8ArrayWriter = Uint8ArrayWriter;
11815
11877
  exports.Writer = Writer;
11816
11878
  exports.ZipReader = ZipReader;
11879
+ exports.ZipReaderStream = ZipReaderStream;
11817
11880
  exports.ZipWriter = ZipWriter;
11881
+ exports.ZipWriterStream = ZipWriterStream;
11818
11882
  exports.configure = configure;
11819
11883
  exports.fs = fs;
11820
11884
  exports.getMimeType = getMimeType;
package/index.d.ts CHANGED
@@ -242,7 +242,7 @@ declare class SyncCodec {
242
242
  }
243
243
 
244
244
  /**
245
- * Represents an intance used to compress data.
245
+ * Represents an instance used to compress data.
246
246
  */
247
247
  declare class ZipDeflate extends SyncCodec {
248
248
  /**
@@ -599,7 +599,7 @@ export class SplitDataWriter implements Initializable, WritableWriter {
599
599
  /**
600
600
  * Creates the {@link SplitDataWriter} instance
601
601
  *
602
- * @param writerGenerator A generator of Writer isntances.
602
+ * @param writerGenerator A generator of Writer instances.
603
603
  * @param maxSize The maximum size of the data written into {@link Writer} instances (default: 4GB).
604
604
  */
605
605
  constructor(
@@ -616,6 +616,40 @@ export class SplitDataWriter implements Initializable, WritableWriter {
616
616
  */
617
617
  export class Uint8ArrayWriter extends Writer<Uint8Array> {}
618
618
 
619
+ /**
620
+ * Represents an instance used to create an unzipped stream.
621
+ *
622
+ * @example
623
+ * This example will take a zip file, decompress it and then recompress each file in it, saving it to disk.
624
+ * ```
625
+ * for await (const entry of (await fetch(urlToZippedFile)).body.pipeThrough(new ZipWriterStream()))
626
+ * if (entry.readable) {
627
+ * console.log(entry.filename)
628
+ * entry.readable
629
+ * .pipeThrough(ZipReaderStream().transform(entry.filename))
630
+ * .pipeTo((await Deno.create(entry.filename + '.zip')).writable)
631
+ * }
632
+ * ```
633
+ */
634
+ export class ZipReaderStream<T> {
635
+ /**
636
+ * Creates the stream.
637
+ *
638
+ * @param options The options.
639
+ */
640
+ constructor (options?: ZipReaderConstructorOptions);
641
+
642
+ /**
643
+ * The readable stream.
644
+ */
645
+ readable: ReadableStream<Omit<Entry, 'getData'> & { readable?: ReadableStream<Uint8Array>; }>;
646
+
647
+ /**
648
+ * The writable stream.
649
+ */
650
+ writable: WritableStream<T>;
651
+ }
652
+
619
653
  /**
620
654
  * Represents an instance used to read a zip file.
621
655
  *
@@ -688,7 +722,7 @@ export class ZipReader<Type> {
688
722
  * Returns a generator used to iterate on all the entries in the zip file
689
723
  *
690
724
  * @param options The options.
691
- * @returns An asynchrounous generator of {@link Entry} instances.
725
+ * @returns An asynchronous generator of {@link Entry} instances.
692
726
  */
693
727
  getEntriesGenerator(
694
728
  options?: ZipReaderGetEntriesOptions,
@@ -947,6 +981,86 @@ interface EntryGetDataOptions
947
981
  interface EntryGetDataCheckPasswordOptions
948
982
  extends EntryGetDataOptions, ZipReaderCheckPasswordOptions {}
949
983
 
984
+ /**
985
+ * Represents an instance used to create a zipped stream.
986
+ *
987
+ * @example
988
+ * This example creates a zipped file called numbers.txt.zip containing the numbers 0 - 1000 each on their own line.
989
+ * ```
990
+ * const readable = ReadableStream.from((function* () {
991
+ * for (let i = 0; i < 1000; ++i)
992
+ * yield i + '\n'
993
+ * })())
994
+ *
995
+ * readable
996
+ * .pipeThrough(new ZipWriterStream().transform('numbers.txt'))
997
+ * .pipeTo((await Deno.create('numbers.txt.zip')).writable)
998
+ * ```
999
+ *
1000
+ * @example
1001
+ * This example creates a zipped file called Archive.zip containing two files called numbers.txt and letters.txt
1002
+ * ```
1003
+ * const readable1 = ReadableStream.from((function* () {
1004
+ * for (let i = 0; i < 1000; ++i)
1005
+ * yield i + '\n'
1006
+ * })())
1007
+ * const readable2 = ReadableStream.from((function* () {
1008
+ * const letters = 'abcdefghijklmnopqrstuvwxyz'.split('')
1009
+ * while (letters.length)
1010
+ * yield letters.shift() + '\n'
1011
+ * })())
1012
+ *
1013
+ * const zipper = new ZipWriterStream()
1014
+ * zipper.readable.pipeTo((await Deno.create('Archive.zip')).writable)
1015
+ * readable1.pipeTo(zipper.writable('numbers.txt'))
1016
+ * readable2.pipeTo(zipper.writable('letters.txt'))
1017
+ * zipper.close()
1018
+ * ```
1019
+ */
1020
+ export class ZipWriterStream {
1021
+ /**
1022
+ * Creates the stream.
1023
+ *
1024
+ * @param options The options.
1025
+ */
1026
+ constructor (options?: ZipWriterConstructorOptions);
1027
+
1028
+ /**
1029
+ * The readable stream.
1030
+ */
1031
+ readable: ReadableStream<Uint8Array>;
1032
+
1033
+ /**
1034
+ * The ZipWriter property.
1035
+ */
1036
+ zipWriter: ZipWriter<unknown>
1037
+
1038
+ /**
1039
+ * Returns an object containing a readable and writable property for the .pipeThrough method
1040
+ *
1041
+ * @param path The name of the stream when unzipped.
1042
+ * @returns An object containing readable and writable properties
1043
+ */
1044
+ transform<T>(path: string): { readable: ReadableStream<T>, writable: WritableStream<T>; };
1045
+
1046
+ /**
1047
+ * Returns a WritableStream for the .pipeTo method
1048
+ *
1049
+ * @param path The directory path of where the stream should exist in the zipped stream.
1050
+ * @returns A WritableStream.
1051
+ */
1052
+ writable<T>(path: string): WritableStream<T>;
1053
+
1054
+ /**
1055
+ * Writes the entries directory, writes the global comment, and returns the content of the zipped file.
1056
+ *
1057
+ * @param comment The global comment of the zip file.
1058
+ * @param options The options.
1059
+ * @returns The content of the zip file.
1060
+ */
1061
+ close(comment?: Uint8Array, options?: ZipWriterCloseOptions): Promise<unknown>;
1062
+ }
1063
+
950
1064
  /**
951
1065
  * Represents an instance used to create a zip file.
952
1066
  *
@@ -1324,7 +1438,7 @@ declare class ZipEntry {
1324
1438
  /**
1325
1439
  * Set the name of the entry
1326
1440
  *
1327
- * @param name The new name of the netry.
1441
+ * @param name The new name of the entry.
1328
1442
  */
1329
1443
  rename(name: string): void;
1330
1444
  }
@@ -1487,7 +1601,7 @@ export class ZipDirectoryEntry extends ZipEntry {
1487
1601
  options?: ZipWriterAddDataOptions,
1488
1602
  ): ZipFileEntry<string, string>;
1489
1603
  /**
1490
- * Adds aentry entry with content provided as a `Blob` instance
1604
+ * Adds a entry entry with content provided as a `Blob` instance
1491
1605
  *
1492
1606
  * @param name The relative filename of the entry.
1493
1607
  * @param blob The `Blob` instance.
@@ -1500,7 +1614,7 @@ export class ZipDirectoryEntry extends ZipEntry {
1500
1614
  options?: ZipWriterAddDataOptions,
1501
1615
  ): ZipFileEntry<Blob, Blob>;
1502
1616
  /**
1503
- * Adds aentry entry with content provided as a Data URI `string` encoded in Base64
1617
+ * Adds a entry entry with content provided as a Data URI `string` encoded in Base64
1504
1618
  *
1505
1619
  * @param name The relative filename of the entry.
1506
1620
  * @param dataURI The Data URI `string` encoded in Base64.
@@ -1539,7 +1653,7 @@ export class ZipDirectoryEntry extends ZipEntry {
1539
1653
  options?: HttpOptions & ZipWriterAddDataOptions,
1540
1654
  ): ZipFileEntry<string, void>;
1541
1655
  /**
1542
- * Adds aentry entry with content provided via a `ReadableStream` instance
1656
+ * Adds a entry entry with content provided via a `ReadableStream` instance
1543
1657
  *
1544
1658
  * @param name The relative filename of the entry.
1545
1659
  * @param readable The `ReadableStream` instance.
package/index.js CHANGED
@@ -9,8 +9,8 @@
9
9
  1. Redistributions of source code must retain the above copyright notice,
10
10
  this list of conditions and the following disclaimer.
11
11
 
12
- 2. Redistributions in binary form must reproduce the above copyright
13
- notice, this list of conditions and the following disclaimer in
12
+ 2. Redistributions in binary form must reproduce the above copyright
13
+ notice, this list of conditions and the following disclaimer in
14
14
  the documentation and/or other materials provided with the distribution.
15
15
 
16
16
  3. The names of the authors may not be used to endorse or promote products
@@ -42,6 +42,8 @@ export {
42
42
  initShimAsyncCodec,
43
43
  ZipReader,
44
44
  ZipWriter,
45
+ ZipReaderStream,
46
+ ZipWriterStream,
45
47
  Reader,
46
48
  Writer,
47
49
  TextReader,
@@ -83,4 +85,4 @@ export {
83
85
  ERR_SPLIT_ZIP_FILE,
84
86
  ERR_ITERATOR_COMPLETED_TOO_SOON
85
87
  } from "./lib/zip-fs.js";
86
- export { getMimeType, terminateWorkers };
88
+ export { getMimeType, terminateWorkers };