@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.
@@ -8749,10 +8749,13 @@
8749
8749
  }
8750
8750
  }
8751
8751
  const dataOffset = offset + 30 + localDirectory.filenameLength + localDirectory.extraFieldLength;
8752
+ const size = compressedSize;
8752
8753
  const readable = reader.readable;
8753
- readable.diskNumberStart = diskNumberStart;
8754
- readable.offset = dataOffset;
8755
- let size = readable.size = compressedSize;
8754
+ Object.assign(readable, {
8755
+ diskNumberStart,
8756
+ offset: dataOffset,
8757
+ size
8758
+ });
8756
8759
  const signal = getOptionValue$1(zipEntry, options, "signal");
8757
8760
  const checkPasswordOnly = getOptionValue$1(zipEntry, options, "checkPasswordOnly");
8758
8761
  if (checkPasswordOnly) {
@@ -10373,7 +10376,26 @@
10373
10376
  }
10374
10377
 
10375
10378
  addFileSystemEntry(fileSystemEntry, options = {}) {
10376
- return addFileSystemEntry(this, fileSystemEntry, options);
10379
+ return addFileSystemHandle(this, fileSystemEntry, options);
10380
+ }
10381
+
10382
+ addFileSystemHandle(handle, options = {}) {
10383
+ return addFileSystemHandle(this, handle, options);
10384
+ }
10385
+
10386
+ addFile(file, options = {}) {
10387
+ if (!options.lastModDate) {
10388
+ options.lastModDate = new Date(file.lastModified);
10389
+ }
10390
+ return addChild(this, file.name, {
10391
+ data: file,
10392
+ Reader: function () {
10393
+ const readable = file.stream();
10394
+ const size = file.size;
10395
+ return { readable, size };
10396
+ },
10397
+ options
10398
+ });
10377
10399
  }
10378
10400
 
10379
10401
  addData(name, params) {
@@ -10576,6 +10598,14 @@
10576
10598
  return this.root.addFileSystemEntry(fileSystemEntry, options);
10577
10599
  }
10578
10600
 
10601
+ addFileSystemHandle(handle, options) {
10602
+ return this.root.addFileSystemHandle(handle, options);
10603
+ }
10604
+
10605
+ addFile(file, options) {
10606
+ return this.root.addFile(file, options);
10607
+ }
10608
+
10579
10609
  addData(name, params) {
10580
10610
  return this.root.addData(name, params);
10581
10611
  }
@@ -10766,54 +10796,92 @@
10766
10796
  }
10767
10797
  }
10768
10798
 
10769
- async function addFileSystemEntry(zipEntry, fileSystemEntry, options) {
10770
- if (fileSystemEntry.isDirectory) {
10771
- const entry = zipEntry.addDirectory(fileSystemEntry.name, options);
10772
- await addDirectory(entry, fileSystemEntry);
10773
- return entry;
10774
- } else {
10775
- return new Promise((resolve, reject) => fileSystemEntry.file(file => resolve(zipEntry.addBlob(fileSystemEntry.name, file, options)), reject));
10776
- }
10799
+ async function addFileSystemHandle(zipEntry, handle, options) {
10800
+ await addFile(zipEntry, handle, []);
10777
10801
 
10778
- async function addDirectory(zipEntry, fileEntry) {
10779
- const children = await getChildren(fileEntry);
10780
- for (const child of children) {
10781
- if (child.isDirectory) {
10782
- await addDirectory(zipEntry.addDirectory(child.name, options), child);
10783
- } else {
10784
- await new Promise((resolve, reject) => {
10785
- child.file(file => {
10786
- const childZipEntry = zipEntry.addBlob(child.name, file, options);
10787
- childZipEntry.uncompressedSize = file.size;
10788
- resolve(childZipEntry);
10789
- }, reject);
10790
- });
10802
+ async function addFile(parentEntry, handle, addedEntries) {
10803
+ try {
10804
+ if (handle.isFile || handle.isDirectory) {
10805
+ handle = await transformToFileSystemhandle(handle);
10806
+ }
10807
+ if (handle.kind == "file") {
10808
+ const file = await handle.getFile();
10809
+ addedEntries.push(
10810
+ parentEntry.addData(file.name, {
10811
+ Reader: function () {
10812
+ const readable = file.stream();
10813
+ const size = file.size;
10814
+ return { readable, size };
10815
+ },
10816
+ options: {
10817
+ lastModDate: new Date(file.lastModified)
10818
+ }
10819
+ }, options)
10820
+ );
10821
+ } else if (handle.kind == "directory") {
10822
+ const directoryEntry = parentEntry.addDirectory(handle.name);
10823
+ addedEntries.push(directoryEntry);
10824
+ for await (const childHandle of handle.values()) {
10825
+ await addFile(directoryEntry, childHandle, addedEntries);
10826
+ }
10791
10827
  }
10828
+ } catch (error) {
10829
+ const message = error.message + (handle ? " (" + handle.name + ")" : "");
10830
+ throw new Error(message);
10792
10831
  }
10832
+ return addedEntries;
10793
10833
  }
10834
+ }
10794
10835
 
10795
- function getChildren(fileEntry) {
10796
- return new Promise((resolve, reject) => {
10797
- let entries = [];
10798
- if (fileEntry.isDirectory) {
10799
- readEntries(fileEntry.createReader());
10800
- }
10801
- if (fileEntry.isFile) {
10802
- resolve(entries);
10803
- }
10836
+ async function transformToFileSystemhandle(entry) {
10837
+ const handle = {
10838
+ name: entry.name
10839
+ };
10840
+ if (entry.isFile) {
10841
+ handle.kind = "file";
10842
+ handle.getFile = () =>
10843
+ new Promise((resolve, reject) => entry.file(resolve, reject));
10844
+ }
10845
+ if (entry.isDirectory) {
10846
+ handle.kind = "directory";
10847
+ const handles = await transformToFileSystemhandles(entry);
10848
+ handle.values = () => handles;
10849
+ }
10850
+ return handle;
10851
+ }
10804
10852
 
10805
- function readEntries(directoryReader) {
10806
- directoryReader.readEntries(temporaryEntries => {
10807
- if (!temporaryEntries.length) {
10808
- resolve(entries);
10809
- } else {
10810
- entries = entries.concat(temporaryEntries);
10811
- readEntries(directoryReader);
10812
- }
10813
- }, reject);
10853
+ async function transformToFileSystemhandles(entry) {
10854
+ const entries = [];
10855
+ function readEntries(directoryReader, resolve, reject) {
10856
+ directoryReader.readEntries(async (entriesPart) => {
10857
+ if (!entriesPart.length) {
10858
+ resolve(entries);
10859
+ } else {
10860
+ for (const entry of entriesPart) {
10861
+ entries.push(await transformToFileSystemhandle(entry));
10862
+ }
10863
+ readEntries(directoryReader, resolve, reject);
10814
10864
  }
10815
- });
10865
+ }, reject);
10816
10866
  }
10867
+ await new Promise((resolve, reject) =>
10868
+ readEntries(entry.createReader(), resolve, reject)
10869
+ );
10870
+ return {
10871
+ [Symbol.iterator]() {
10872
+ let entryIndex = 0;
10873
+ return {
10874
+ next() {
10875
+ const result = {
10876
+ value: entries[entryIndex],
10877
+ done: entryIndex === entries.length
10878
+ };
10879
+ entryIndex++;
10880
+ return result;
10881
+ }
10882
+ };
10883
+ }
10884
+ };
10817
10885
  }
10818
10886
 
10819
10887
  function resetFS(fs) {