@zip.js/zip.js 2.6.34 → 2.6.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.d.ts CHANGED
@@ -272,7 +272,7 @@ export function getMimeType(fileExtension: string): string
272
272
  * zip.js can handle multiple types of data thanks to a generic API. This feature is based on 2 abstract constructors: `Reader` and `Writer`.
273
273
  * The classes inheriting from `Reader` help to read data from a source of data. The classes inheriting from `Writer` help to write data into a destination.
274
274
  */
275
- interface DataProcessor {
275
+ interface Initializable {
276
276
  /**
277
277
  * Initializes the instance asynchronously
278
278
  */
@@ -317,7 +317,7 @@ interface ReadableReader {
317
317
  * }
318
318
  * ```
319
319
  */
320
- export class Reader<Type> implements ReadableReader, DataProcessor {
320
+ export class Reader<Type> implements Initializable, ReadableReader {
321
321
  /**
322
322
  * Creates the `Reader` instance
323
323
  *
@@ -479,7 +479,7 @@ interface WritableWriter {
479
479
  * }
480
480
  * ```
481
481
  */
482
- export class Writer<Type> implements DataProcessor, WritableWriter {
482
+ export class Writer<Type> implements Initializable, WritableWriter {
483
483
  /**
484
484
  * The `WritableStream` instance.
485
485
  */
@@ -521,13 +521,27 @@ export class TextWriter extends Writer<string> {
521
521
  /**
522
522
  * Represents a `Writer` instance used to retrieve the written data as a `Blob` instance.
523
523
  */
524
- export class BlobWriter extends Writer<Blob> {
524
+ export class BlobWriter implements Initializable, WritableWriter {
525
+ /**
526
+ * The `WritableStream` instance.
527
+ */
528
+ writable: WritableStream
529
+ /**
530
+ * Initializes the instance asynchronously
531
+ */
532
+ init(): Promise<void>
525
533
  /**
526
534
  * Creates the `BlobWriter` instance
527
535
  *
528
536
  * @param mimeString The MIME type of the content.
529
537
  */
530
538
  constructor(mimeString?: string)
539
+ /**
540
+ * Retrieves all the written data
541
+ *
542
+ * @returns A promise resolving to the written data.
543
+ */
544
+ getData(): Promise<Blob>
531
545
  }
532
546
 
533
547
  /**
@@ -545,7 +559,7 @@ export class Data64URIWriter extends Writer<string> {
545
559
  /**
546
560
  * Represents a `Writer` instance used to retrieve the written data from a generator of `Writer` instances (i.e. split zip files).
547
561
  */
548
- export class SplitZipWriter implements DataProcessor, WritableWriter {
562
+ export class SplitZipWriter implements Initializable, WritableWriter {
549
563
  /**
550
564
  * The `WritableStream` instance.
551
565
  */
@@ -558,9 +572,9 @@ export class SplitZipWriter implements DataProcessor, WritableWriter {
558
572
  * Creates the `SplitZipWriter` instance
559
573
  *
560
574
  * @param writerGenerator The MIME type of the content.
561
- * @param maxSize The maximum size of the data written into `Writer` instances.
575
+ * @param maxSize The maximum size of the data written into `Writer` instances (default: 4GB).
562
576
  */
563
- constructor(writerGenerator: AsyncGenerator<Writer<any> | WritableWriter | WritableStream, boolean>, maxSize: number)
577
+ constructor(writerGenerator: AsyncGenerator<Writer<any> | WritableWriter | WritableStream, boolean>, maxSize?: number)
564
578
  }
565
579
 
566
580
  /**
package/lib/core/io.js CHANGED
@@ -571,14 +571,14 @@ class SplitZipWriter extends Stream {
571
571
 
572
572
  constructor(writerGenerator, maxSize = 4294967296) {
573
573
  super();
574
- const writer = this;
575
- Object.assign(writer, {
574
+ const zipWriter = this;
575
+ Object.assign(zipWriter, {
576
576
  diskNumber: 0,
577
577
  diskOffset: 0,
578
578
  size: 0,
579
579
  availableSize: maxSize
580
580
  });
581
- let currentWriter, diskWritable, diskWriter;
581
+ let diskSourceWriter, diskWritable, diskWriter;
582
582
  const writable = new WritableStream({
583
583
  async write(chunk) {
584
584
  if (!diskWriter) {
@@ -586,43 +586,47 @@ class SplitZipWriter extends Stream {
586
586
  if (done && !value) {
587
587
  throw new Error(ERR_ITERATOR_COMPLETED_TOO_SOON);
588
588
  } else {
589
- currentWriter = value;
590
- currentWriter.size = 0;
589
+ diskSourceWriter = value;
590
+ diskSourceWriter.size = 0;
591
591
  diskWritable = value.writable;
592
592
  diskWriter = diskWritable.getWriter();
593
593
  }
594
594
  await this.write(chunk);
595
- } else if (diskWriter && currentWriter.size + chunk.length > maxSize) {
596
- await diskWriter.ready;
597
- const chunkLength = maxSize - currentWriter.size;
598
- await diskWriter.write(chunk.slice(0, chunkLength));
599
- chunk = chunk.slice(chunkLength);
600
- currentWriter.size += chunkLength;
601
- writer.size += chunkLength;
602
- writer.diskOffset += diskWritable.size = currentWriter.size;
603
- writer.diskNumber++;
604
- await diskWriter.close();
595
+ } else if (diskSourceWriter.size + chunk.length >= maxSize) {
596
+ const chunkLength = maxSize - diskSourceWriter.size;
597
+ await writeChunk(chunk.slice(0, chunkLength));
598
+ await closeDisk();
599
+ zipWriter.diskOffset += diskSourceWriter.size;
600
+ zipWriter.diskNumber++;
605
601
  diskWriter = null;
606
- await this.write(chunk);
607
- } else {
608
- await diskWriter.ready;
609
- await diskWriter.write(chunk);
610
- currentWriter.size += chunk.length;
611
- writer.size += chunk.length;
602
+ await this.write(chunk.slice(chunkLength));
603
+ } else if (chunk.length) {
604
+ await writeChunk(chunk);
612
605
  }
613
- writer.availableSize = maxSize - currentWriter.size;
606
+ zipWriter.availableSize = maxSize - diskSourceWriter.size;
614
607
  },
615
608
  async close() {
616
609
  await diskWriter.ready;
617
- diskWritable.size = currentWriter.size;
618
- await diskWriter.close();
610
+ await closeDisk();
619
611
  }
620
612
  });
621
- Object.defineProperty(writer, PROPERTY_NAME_WRITABLE, {
613
+ Object.defineProperty(zipWriter, PROPERTY_NAME_WRITABLE, {
622
614
  get() {
623
615
  return writable;
624
616
  }
625
617
  });
618
+
619
+ async function writeChunk(chunk) {
620
+ await diskWriter.ready;
621
+ await diskWriter.write(chunk);
622
+ diskSourceWriter.size += chunk.length;
623
+ zipWriter.size += chunk.length;
624
+ }
625
+
626
+ async function closeDisk() {
627
+ diskWritable.size = diskSourceWriter.size;
628
+ await diskWriter.close();
629
+ }
626
630
  }
627
631
  }
628
632
 
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "A JavaScript library to zip and unzip files in the browser, Deno and Node.js",
4
4
  "author": "Gildas Lormeau",
5
5
  "license": "BSD-3-Clause",
6
- "version": "2.6.34",
6
+ "version": "2.6.35",
7
7
  "type": "module",
8
8
  "keywords": [
9
9
  "zip",