@zip.js/zip.js 2.7.7 → 2.7.9

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
@@ -8745,10 +8745,13 @@ class ZipEntry$1 {
8745
8745
  }
8746
8746
  }
8747
8747
  const dataOffset = offset + 30 + localDirectory.filenameLength + localDirectory.extraFieldLength;
8748
+ const size = compressedSize;
8748
8749
  const readable = reader.readable;
8749
- readable.diskNumberStart = diskNumberStart;
8750
- readable.offset = dataOffset;
8751
- let size = readable.size = compressedSize;
8750
+ Object.assign(readable, {
8751
+ diskNumberStart,
8752
+ offset: dataOffset,
8753
+ size
8754
+ });
8752
8755
  const signal = getOptionValue$1(zipEntry, options, "signal");
8753
8756
  const checkPasswordOnly = getOptionValue$1(zipEntry, options, "checkPasswordOnly");
8754
8757
  if (checkPasswordOnly) {
@@ -10369,7 +10372,26 @@ class ZipDirectoryEntry extends ZipEntry {
10369
10372
  }
10370
10373
 
10371
10374
  addFileSystemEntry(fileSystemEntry, options = {}) {
10372
- return addFileSystemEntry(this, fileSystemEntry, options);
10375
+ return addFileSystemHandle(this, fileSystemEntry, options);
10376
+ }
10377
+
10378
+ addFileSystemHandle(handle, options = {}) {
10379
+ return addFileSystemHandle(this, handle, options);
10380
+ }
10381
+
10382
+ addFile(file, options = {}) {
10383
+ if (!options.lastModDate) {
10384
+ options.lastModDate = new Date(file.lastModified);
10385
+ }
10386
+ return addChild(this, file.name, {
10387
+ data: file,
10388
+ Reader: function () {
10389
+ const readable = file.stream();
10390
+ const size = file.size;
10391
+ return { readable, size };
10392
+ },
10393
+ options
10394
+ });
10373
10395
  }
10374
10396
 
10375
10397
  addData(name, params) {
@@ -10572,6 +10594,14 @@ class FS {
10572
10594
  return this.root.addFileSystemEntry(fileSystemEntry, options);
10573
10595
  }
10574
10596
 
10597
+ addFileSystemHandle(handle, options) {
10598
+ return this.root.addFileSystemHandle(handle, options);
10599
+ }
10600
+
10601
+ addFile(file, options) {
10602
+ return this.root.addFile(file, options);
10603
+ }
10604
+
10575
10605
  addData(name, params) {
10576
10606
  return this.root.addData(name, params);
10577
10607
  }
@@ -10762,54 +10792,92 @@ async function exportZip(zipWriter, entry, totalSize, options) {
10762
10792
  }
10763
10793
  }
10764
10794
 
10765
- async function addFileSystemEntry(zipEntry, fileSystemEntry, options) {
10766
- if (fileSystemEntry.isDirectory) {
10767
- const entry = zipEntry.addDirectory(fileSystemEntry.name, options);
10768
- await addDirectory(entry, fileSystemEntry);
10769
- return entry;
10770
- } else {
10771
- return new Promise((resolve, reject) => fileSystemEntry.file(file => resolve(zipEntry.addBlob(fileSystemEntry.name, file, options)), reject));
10772
- }
10795
+ async function addFileSystemHandle(zipEntry, handle, options) {
10796
+ await addFile(zipEntry, handle, []);
10773
10797
 
10774
- async function addDirectory(zipEntry, fileEntry) {
10775
- const children = await getChildren(fileEntry);
10776
- for (const child of children) {
10777
- if (child.isDirectory) {
10778
- await addDirectory(zipEntry.addDirectory(child.name, options), child);
10779
- } else {
10780
- await new Promise((resolve, reject) => {
10781
- child.file(file => {
10782
- const childZipEntry = zipEntry.addBlob(child.name, file, options);
10783
- childZipEntry.uncompressedSize = file.size;
10784
- resolve(childZipEntry);
10785
- }, reject);
10786
- });
10798
+ async function addFile(parentEntry, handle, addedEntries) {
10799
+ try {
10800
+ if (handle.isFile || handle.isDirectory) {
10801
+ handle = await transformToFileSystemhandle(handle);
10802
+ }
10803
+ if (handle.kind == "file") {
10804
+ const file = await handle.getFile();
10805
+ addedEntries.push(
10806
+ parentEntry.addData(file.name, {
10807
+ Reader: function () {
10808
+ const readable = file.stream();
10809
+ const size = file.size;
10810
+ return { readable, size };
10811
+ },
10812
+ options: {
10813
+ lastModDate: new Date(file.lastModified)
10814
+ }
10815
+ }, options)
10816
+ );
10817
+ } else if (handle.kind == "directory") {
10818
+ const directoryEntry = parentEntry.addDirectory(handle.name);
10819
+ addedEntries.push(directoryEntry);
10820
+ for await (const childHandle of handle.values()) {
10821
+ await addFile(directoryEntry, childHandle, addedEntries);
10822
+ }
10787
10823
  }
10824
+ } catch (error) {
10825
+ const message = error.message + (handle ? " (" + handle.name + ")" : "");
10826
+ throw new Error(message);
10788
10827
  }
10828
+ return addedEntries;
10789
10829
  }
10830
+ }
10790
10831
 
10791
- function getChildren(fileEntry) {
10792
- return new Promise((resolve, reject) => {
10793
- let entries = [];
10794
- if (fileEntry.isDirectory) {
10795
- readEntries(fileEntry.createReader());
10796
- }
10797
- if (fileEntry.isFile) {
10798
- resolve(entries);
10799
- }
10832
+ async function transformToFileSystemhandle(entry) {
10833
+ const handle = {
10834
+ name: entry.name
10835
+ };
10836
+ if (entry.isFile) {
10837
+ handle.kind = "file";
10838
+ handle.getFile = () =>
10839
+ new Promise((resolve, reject) => entry.file(resolve, reject));
10840
+ }
10841
+ if (entry.isDirectory) {
10842
+ handle.kind = "directory";
10843
+ const handles = await transformToFileSystemhandles(entry);
10844
+ handle.values = () => handles;
10845
+ }
10846
+ return handle;
10847
+ }
10800
10848
 
10801
- function readEntries(directoryReader) {
10802
- directoryReader.readEntries(temporaryEntries => {
10803
- if (!temporaryEntries.length) {
10804
- resolve(entries);
10805
- } else {
10806
- entries = entries.concat(temporaryEntries);
10807
- readEntries(directoryReader);
10808
- }
10809
- }, reject);
10849
+ async function transformToFileSystemhandles(entry) {
10850
+ const entries = [];
10851
+ function readEntries(directoryReader, resolve, reject) {
10852
+ directoryReader.readEntries(async (entriesPart) => {
10853
+ if (!entriesPart.length) {
10854
+ resolve(entries);
10855
+ } else {
10856
+ for (const entry of entriesPart) {
10857
+ entries.push(await transformToFileSystemhandle(entry));
10858
+ }
10859
+ readEntries(directoryReader, resolve, reject);
10810
10860
  }
10811
- });
10861
+ }, reject);
10812
10862
  }
10863
+ await new Promise((resolve, reject) =>
10864
+ readEntries(entry.createReader(), resolve, reject)
10865
+ );
10866
+ return {
10867
+ [Symbol.iterator]() {
10868
+ let entryIndex = 0;
10869
+ return {
10870
+ next() {
10871
+ const result = {
10872
+ value: entries[entryIndex],
10873
+ done: entryIndex === entries.length
10874
+ };
10875
+ entryIndex++;
10876
+ return result;
10877
+ }
10878
+ };
10879
+ }
10880
+ };
10813
10881
  }
10814
10882
 
10815
10883
  function resetFS(fs) {
package/index.d.ts CHANGED
@@ -9,6 +9,13 @@ declare global {
9
9
  */
10
10
  // deno-lint-ignore no-empty-interface
11
11
  interface FileSystemEntry {}
12
+ /**
13
+ * Represents the `FileSystemHandle` class.
14
+ *
15
+ * @see {@link https://fs.spec.whatwg.org/#api-filesystemhandle}
16
+ */
17
+ // deno-lint-ignore no-empty-interface
18
+ interface FileSystemHandle {}
12
19
  }
13
20
 
14
21
  /**
@@ -1530,6 +1537,17 @@ export class ZipDirectoryEntry extends ZipEntry {
1530
1537
  readable: ReadableStream,
1531
1538
  options?: ZipWriterAddDataOptions,
1532
1539
  ): ZipFileEntry<ReadableStream, void>;
1540
+ /**
1541
+ * Adds an entry with content provided via a `File` instance
1542
+ *
1543
+ * @param file The `File` instance.
1544
+ * @param options The options.
1545
+ * @returns A promise resolving to a {@link ZipFileEntry} or a {@link ZipDirectoryEntry} instance.
1546
+ */
1547
+ addFile(
1548
+ file: File,
1549
+ options?: ZipWriterAddDataOptions,
1550
+ ): Promise<ZipEntry>;
1533
1551
  /**
1534
1552
  * Adds an entry with content provided via a `FileSystemEntry` instance
1535
1553
  *
@@ -1541,6 +1559,17 @@ export class ZipDirectoryEntry extends ZipEntry {
1541
1559
  fileSystemEntry: FileSystemEntry,
1542
1560
  options?: ZipWriterAddDataOptions,
1543
1561
  ): Promise<ZipEntry>;
1562
+ /**
1563
+ * Adds an entry with content provided via a `FileSystemHandle` instance
1564
+ *
1565
+ * @param fileSystemHandle The `fileSystemHandle` instance.
1566
+ * @param options The options.
1567
+ * @returns A promise resolving to a {@link ZipFileEntry} or a {@link ZipDirectoryEntry} instance.
1568
+ */
1569
+ addFileSystemHandle(
1570
+ fileSystemEntry: FileSystemHandle,
1571
+ options?: ZipWriterAddDataOptions,
1572
+ ): Promise<ZipEntry>;
1544
1573
  /**
1545
1574
  * Extracts a zip file provided as a `Blob` instance into the entry
1546
1575
  *
@@ -318,7 +318,26 @@ class ZipDirectoryEntry extends ZipEntry {
318
318
  }
319
319
 
320
320
  addFileSystemEntry(fileSystemEntry, options = {}) {
321
- return addFileSystemEntry(this, fileSystemEntry, options);
321
+ return addFileSystemHandle(this, fileSystemEntry, options);
322
+ }
323
+
324
+ addFileSystemHandle(handle, options = {}) {
325
+ return addFileSystemHandle(this, handle, options);
326
+ }
327
+
328
+ addFile(file, options = {}) {
329
+ if (!options.lastModDate) {
330
+ options.lastModDate = new Date(file.lastModified);
331
+ }
332
+ return addChild(this, file.name, {
333
+ data: file,
334
+ Reader: function () {
335
+ const readable = file.stream();
336
+ const size = file.size;
337
+ return { readable, size };
338
+ },
339
+ options
340
+ });
322
341
  }
323
342
 
324
343
  addData(name, params) {
@@ -521,6 +540,14 @@ class FS {
521
540
  return this.root.addFileSystemEntry(fileSystemEntry, options);
522
541
  }
523
542
 
543
+ addFileSystemHandle(handle, options) {
544
+ return this.root.addFileSystemHandle(handle, options);
545
+ }
546
+
547
+ addFile(file, options) {
548
+ return this.root.addFile(file, options);
549
+ }
550
+
524
551
  addData(name, params) {
525
552
  return this.root.addData(name, params);
526
553
  }
@@ -712,54 +739,92 @@ async function exportZip(zipWriter, entry, totalSize, options) {
712
739
  }
713
740
  }
714
741
 
715
- async function addFileSystemEntry(zipEntry, fileSystemEntry, options) {
716
- if (fileSystemEntry.isDirectory) {
717
- const entry = zipEntry.addDirectory(fileSystemEntry.name, options);
718
- await addDirectory(entry, fileSystemEntry);
719
- return entry;
720
- } else {
721
- return new Promise((resolve, reject) => fileSystemEntry.file(file => resolve(zipEntry.addBlob(fileSystemEntry.name, file, options)), reject));
722
- }
742
+ async function addFileSystemHandle(zipEntry, handle, options) {
743
+ await addFile(zipEntry, handle, []);
723
744
 
724
- async function addDirectory(zipEntry, fileEntry) {
725
- const children = await getChildren(fileEntry);
726
- for (const child of children) {
727
- if (child.isDirectory) {
728
- await addDirectory(zipEntry.addDirectory(child.name, options), child);
729
- } else {
730
- await new Promise((resolve, reject) => {
731
- child.file(file => {
732
- const childZipEntry = zipEntry.addBlob(child.name, file, options);
733
- childZipEntry.uncompressedSize = file.size;
734
- resolve(childZipEntry);
735
- }, reject);
736
- });
745
+ async function addFile(parentEntry, handle, addedEntries) {
746
+ try {
747
+ if (handle.isFile || handle.isDirectory) {
748
+ handle = await transformToFileSystemhandle(handle);
749
+ }
750
+ if (handle.kind == "file") {
751
+ const file = await handle.getFile();
752
+ addedEntries.push(
753
+ parentEntry.addData(file.name, {
754
+ Reader: function () {
755
+ const readable = file.stream();
756
+ const size = file.size;
757
+ return { readable, size };
758
+ },
759
+ options: {
760
+ lastModDate: new Date(file.lastModified)
761
+ }
762
+ }, options)
763
+ );
764
+ } else if (handle.kind == "directory") {
765
+ const directoryEntry = parentEntry.addDirectory(handle.name);
766
+ addedEntries.push(directoryEntry);
767
+ for await (const childHandle of handle.values()) {
768
+ await addFile(directoryEntry, childHandle, addedEntries);
769
+ }
737
770
  }
771
+ } catch (error) {
772
+ const message = error.message + (handle ? " (" + handle.name + ")" : "");
773
+ throw new Error(message);
738
774
  }
775
+ return addedEntries;
739
776
  }
777
+ }
740
778
 
741
- function getChildren(fileEntry) {
742
- return new Promise((resolve, reject) => {
743
- let entries = [];
744
- if (fileEntry.isDirectory) {
745
- readEntries(fileEntry.createReader());
746
- }
747
- if (fileEntry.isFile) {
748
- resolve(entries);
749
- }
779
+ async function transformToFileSystemhandle(entry) {
780
+ const handle = {
781
+ name: entry.name
782
+ };
783
+ if (entry.isFile) {
784
+ handle.kind = "file";
785
+ handle.getFile = () =>
786
+ new Promise((resolve, reject) => entry.file(resolve, reject));
787
+ }
788
+ if (entry.isDirectory) {
789
+ handle.kind = "directory";
790
+ const handles = await transformToFileSystemhandles(entry);
791
+ handle.values = () => handles;
792
+ }
793
+ return handle;
794
+ }
750
795
 
751
- function readEntries(directoryReader) {
752
- directoryReader.readEntries(temporaryEntries => {
753
- if (!temporaryEntries.length) {
754
- resolve(entries);
755
- } else {
756
- entries = entries.concat(temporaryEntries);
757
- readEntries(directoryReader);
758
- }
759
- }, reject);
796
+ async function transformToFileSystemhandles(entry) {
797
+ const entries = [];
798
+ function readEntries(directoryReader, resolve, reject) {
799
+ directoryReader.readEntries(async (entriesPart) => {
800
+ if (!entriesPart.length) {
801
+ resolve(entries);
802
+ } else {
803
+ for (const entry of entriesPart) {
804
+ entries.push(await transformToFileSystemhandle(entry));
805
+ }
806
+ readEntries(directoryReader, resolve, reject);
760
807
  }
761
- });
762
- }
808
+ }, reject);
809
+ }
810
+ await new Promise((resolve, reject) =>
811
+ readEntries(entry.createReader(), resolve, reject)
812
+ );
813
+ return {
814
+ [Symbol.iterator]() {
815
+ let entryIndex = 0;
816
+ return {
817
+ next() {
818
+ const result = {
819
+ value: entries[entryIndex],
820
+ done: entryIndex === entries.length
821
+ };
822
+ entryIndex++;
823
+ return result;
824
+ }
825
+ };
826
+ }
827
+ };
763
828
  }
764
829
 
765
830
  function resetFS(fs) {
@@ -392,10 +392,13 @@ class ZipEntry {
392
392
  }
393
393
  }
394
394
  const dataOffset = offset + 30 + localDirectory.filenameLength + localDirectory.extraFieldLength;
395
+ const size = compressedSize;
395
396
  const readable = reader.readable;
396
- readable.diskNumberStart = diskNumberStart;
397
- readable.offset = dataOffset;
398
- let size = readable.size = compressedSize;
397
+ Object.assign(readable, {
398
+ diskNumberStart,
399
+ offset: dataOffset,
400
+ size
401
+ });
399
402
  const signal = getOptionValue(zipEntry, options, "signal");
400
403
  const checkPasswordOnly = getOptionValue(zipEntry, options, "checkPasswordOnly");
401
404
  if (checkPasswordOnly) {
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.7.7",
6
+ "version": "2.7.9",
7
7
  "type": "module",
8
8
  "keywords": [
9
9
  "zip",