@zip.js/zip.js 2.8.50 → 2.8.51

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.
@@ -8235,6 +8235,8 @@
8235
8235
  const ERR_ENTRY_EXISTS = "Entry filename already exists";
8236
8236
  const ERR_READABLE_CONSUMED = "Readable stream already consumed";
8237
8237
  const ERR_ABORT_EXPORT = "zipjs-abort-export";
8238
+ const ERR_ABORTED = "The operation was aborted";
8239
+ const ABORT_ERROR_NAME = "AbortError";
8238
8240
  const INFOZIP_EXTRA_FIELD_TYPE = "infozip";
8239
8241
  const INTERPRETED_EXTRA_FIELD_TYPES = new Set([
8240
8242
  EXTRAFIELD_TYPE_ZIP64,
@@ -8687,6 +8689,10 @@
8687
8689
  }
8688
8690
  }
8689
8691
 
8692
+ getChildren(options = {}) {
8693
+ return collectChildren(this, options.recursive);
8694
+ }
8695
+
8690
8696
  isPasswordProtected() {
8691
8697
  const children = this.children;
8692
8698
  for (let childIndex = 0; childIndex < children.length; childIndex++) {
@@ -8778,6 +8784,10 @@
8778
8784
  return this.root.getChildByName(name);
8779
8785
  }
8780
8786
 
8787
+ getChildren(options) {
8788
+ return this.root.getChildren(options);
8789
+ }
8790
+
8781
8791
  addDirectory(name, options) {
8782
8792
  return this.root.addDirectory(name, options);
8783
8793
  }
@@ -9037,103 +9047,101 @@
9037
9047
  async function exportZip(zipWriter, entry, totalSize, options, readers) {
9038
9048
  const selectedEntry = entry;
9039
9049
  const entryOffsets = new Map();
9040
- await process(zipWriter, entry);
9050
+ if (options.bufferedWrite) {
9051
+ await processChildren(entry);
9052
+ } else {
9053
+ for (const child of entry.getChildren({ recursive: true })) {
9054
+ await addChild(child);
9055
+ }
9056
+ }
9041
9057
 
9042
- async function process(zipWriter, entry) {
9043
- await exportChild();
9058
+ async function processChildren(entry) {
9059
+ const results = await Promise.allSettled(entry.children.map(async child => {
9060
+ await addChild(child);
9061
+ await processChildren(child);
9062
+ }));
9063
+ const errorResult = results.find(result => result.status == "rejected");
9064
+ if (errorResult) {
9065
+ throw errorResult.reason;
9066
+ }
9067
+ }
9044
9068
 
9045
- async function exportChild() {
9046
- if (options.bufferedWrite) {
9047
- const results = await Promise.allSettled(entry.children.map(processChild));
9048
- const errorResult = results.find(result => result.status == "rejected");
9049
- if (errorResult) {
9050
- throw errorResult.reason;
9069
+ async function addChild(child) {
9070
+ const name = options.relativePath ? child.getRelativeName(selectedEntry) : child.getFullname();
9071
+ const childOptions = child.options || {};
9072
+ let zipEntryOptions = {};
9073
+ if (child.data instanceof Entry) {
9074
+ const {
9075
+ externalFileAttributes,
9076
+ versionMadeBy,
9077
+ comment,
9078
+ lastModDate,
9079
+ creationDate,
9080
+ lastAccessDate,
9081
+ uncompressedSize,
9082
+ encrypted,
9083
+ zipCrypto,
9084
+ crc32,
9085
+ compressionMethod,
9086
+ extraFieldAES,
9087
+ internalFileAttributes,
9088
+ extraField,
9089
+ uid,
9090
+ gid
9091
+ } = child.data;
9092
+ zipEntryOptions = {
9093
+ externalFileAttributes,
9094
+ versionMadeBy,
9095
+ comment,
9096
+ lastModDate,
9097
+ creationDate,
9098
+ lastAccessDate,
9099
+ internalFileAttributes
9100
+ };
9101
+ const userExtraField = getUserExtraField(extraField);
9102
+ if (userExtraField) {
9103
+ zipEntryOptions.extraField = userExtraField;
9104
+ }
9105
+ if (uid !== UNDEFINED_VALUE || gid !== UNDEFINED_VALUE) {
9106
+ Object.assign(zipEntryOptions, {
9107
+ uid,
9108
+ gid,
9109
+ unixExtraFieldType: options.unixExtraFieldType || INFOZIP_EXTRA_FIELD_TYPE
9110
+ });
9111
+ }
9112
+ if (child.passThrough) {
9113
+ let level, encryptionStrength;
9114
+ if (compressionMethod === 0) {
9115
+ level = 0;
9051
9116
  }
9052
- } else {
9053
- for (const child of entry.children) {
9054
- await processChild(child);
9117
+ if (extraFieldAES) {
9118
+ encryptionStrength = extraFieldAES.strength;
9055
9119
  }
9056
- }
9057
- }
9058
-
9059
- async function processChild(child) {
9060
- const name = options.relativePath ? child.getRelativeName(selectedEntry) : child.getFullname();
9061
- const childOptions = child.options || {};
9062
- let zipEntryOptions = {};
9063
- if (child.data instanceof Entry) {
9064
- const {
9065
- externalFileAttributes,
9066
- versionMadeBy,
9067
- comment,
9068
- lastModDate,
9069
- creationDate,
9070
- lastAccessDate,
9071
- uncompressedSize,
9120
+ zipEntryOptions = Object.assign(zipEntryOptions, {
9121
+ passThrough: true,
9072
9122
  encrypted,
9073
9123
  zipCrypto,
9074
9124
  crc32,
9075
- compressionMethod,
9076
- extraFieldAES,
9077
- internalFileAttributes,
9078
- extraField,
9079
- uid,
9080
- gid
9081
- } = child.data;
9082
- zipEntryOptions = {
9083
- externalFileAttributes,
9084
- versionMadeBy,
9085
- comment,
9086
- lastModDate,
9087
- creationDate,
9088
- lastAccessDate,
9089
- internalFileAttributes
9090
- };
9091
- const userExtraField = getUserExtraField(extraField);
9092
- if (userExtraField) {
9093
- zipEntryOptions.extraField = userExtraField;
9094
- }
9095
- if (uid !== UNDEFINED_VALUE || gid !== UNDEFINED_VALUE) {
9096
- Object.assign(zipEntryOptions, {
9097
- uid,
9098
- gid,
9099
- unixExtraFieldType: options.unixExtraFieldType || INFOZIP_EXTRA_FIELD_TYPE
9100
- });
9101
- }
9102
- if (child.passThrough) {
9103
- let level, encryptionStrength;
9104
- if (compressionMethod === 0) {
9105
- level = 0;
9106
- }
9107
- if (extraFieldAES) {
9108
- encryptionStrength = extraFieldAES.strength;
9109
- }
9110
- zipEntryOptions = Object.assign(zipEntryOptions, {
9111
- passThrough: true,
9112
- encrypted,
9113
- zipCrypto,
9114
- crc32,
9115
- uncompressedSize,
9116
- level,
9117
- encryptionStrength,
9118
- compressionMethod
9119
- });
9120
- }
9125
+ uncompressedSize,
9126
+ level,
9127
+ encryptionStrength,
9128
+ compressionMethod
9129
+ });
9121
9130
  }
9122
- await zipWriter.add(name, readers.get(child), Object.assign({}, options, zipEntryOptions, childOptions, {
9123
- directory: child.directory,
9124
- onprogress: async indexProgress => {
9125
- if (options.onprogress) {
9126
- entryOffsets.set(name, indexProgress);
9127
- try {
9128
- await options.onprogress(Array.from(entryOffsets.values()).reduce((previousValue, currentValue) => previousValue + currentValue), totalSize);
9129
- } catch {
9130
- // ignored
9131
- }
9131
+ }
9132
+ await zipWriter.add(name, readers.get(child), Object.assign({}, options, zipEntryOptions, childOptions, {
9133
+ directory: child.directory,
9134
+ onprogress: async indexProgress => {
9135
+ if (options.onprogress) {
9136
+ entryOffsets.set(name, indexProgress);
9137
+ try {
9138
+ await options.onprogress(Array.from(entryOffsets.values()).reduce((previousValue, currentValue) => previousValue + currentValue), totalSize);
9139
+ } catch {
9140
+ // ignored
9132
9141
  }
9133
9142
  }
9134
- }));
9135
- await process(zipWriter, child);
9136
- }
9143
+ }
9144
+ }));
9137
9145
  }
9138
9146
  }
9139
9147
 
@@ -9189,6 +9197,7 @@
9189
9197
  const { signal } = abortController;
9190
9198
  const releaseSignal = forwardAbort(options.signal, abortController);
9191
9199
  const exportedEntryNames = [];
9200
+ let exportAborted = false;
9192
9201
  try {
9193
9202
  await exportChildren(zipEntry, directoryHandle);
9194
9203
  } catch (error) {
@@ -9221,10 +9230,10 @@
9221
9230
 
9222
9231
  async function exportChild(child, parentHandle) {
9223
9232
  if (signal.aborted) {
9224
- if (isExportAborted(signal.reason)) {
9233
+ if (exportAborted || isExportAborted(signal.reason)) {
9225
9234
  return;
9226
9235
  }
9227
- throw signal.reason;
9236
+ throw signal.reason === UNDEFINED_VALUE ? new DOMException(ERR_ABORTED, ABORT_ERROR_NAME) : signal.reason;
9228
9237
  }
9229
9238
  try {
9230
9239
  if (child.directory) {
@@ -9233,22 +9242,27 @@
9233
9242
  } else {
9234
9243
  const fileHandle = await parentHandle.getFileHandle(child.name, { create: true });
9235
9244
  const writable = await fileHandle.createWritable();
9236
- await child.getData({ writable }, Object.assign({}, options, {
9237
- signal,
9238
- onprogress: async progress => {
9239
- if (options.onprogress) {
9240
- writtenSizes.set(child.id, progress);
9241
- try {
9242
- await options.onprogress(Array.from(writtenSizes.values()).reduce((previousValue, currentValue) => previousValue + currentValue, 0), totalSize);
9243
- } catch {
9244
- // ignored
9245
+ try {
9246
+ await child.getData({ writable }, Object.assign({}, options, {
9247
+ signal,
9248
+ onprogress: async progress => {
9249
+ if (options.onprogress) {
9250
+ writtenSizes.set(child.id, progress);
9251
+ try {
9252
+ await options.onprogress(Array.from(writtenSizes.values()).reduce((previousValue, currentValue) => previousValue + currentValue, 0), totalSize);
9253
+ } catch {
9254
+ // ignored
9255
+ }
9245
9256
  }
9246
9257
  }
9247
- }
9248
- }));
9258
+ }));
9259
+ } catch (error) {
9260
+ throw exportAborted ? new Error(ERR_ABORT_EXPORT) : error;
9261
+ }
9249
9262
  exportedEntryNames.push(child.getRelativeName(zipEntry));
9250
9263
  }
9251
9264
  } catch (error) {
9265
+ exportAborted = true;
9252
9266
  abortController.abort(new Error(ERR_ABORT_EXPORT));
9253
9267
  try {
9254
9268
  if (error.entryName === UNDEFINED_VALUE) {
@@ -9320,6 +9334,21 @@
9320
9334
  fs.root = new ZipDirectoryEntry(fs);
9321
9335
  }
9322
9336
 
9337
+ function collectChildren(directory, recursive) {
9338
+ const children = [];
9339
+ const pendingDirectories = [directory];
9340
+ let directoryIndex = 0;
9341
+ while (directoryIndex < pendingDirectories.length) {
9342
+ for (const child of pendingDirectories[directoryIndex++].children) {
9343
+ children.push(child);
9344
+ if (recursive) {
9345
+ pendingDirectories.push(child);
9346
+ }
9347
+ }
9348
+ }
9349
+ return children;
9350
+ }
9351
+
9323
9352
  function registerEntries(fs, entry) {
9324
9353
  const pendingEntries = [entry];
9325
9354
  while (pendingEntries.length) {
@@ -9438,6 +9467,7 @@
9438
9467
  exports.BlobWriter = BlobWriter;
9439
9468
  exports.Data64URIReader = Data64URIReader;
9440
9469
  exports.Data64URIWriter = Data64URIWriter;
9470
+ exports.ERR_ABORTED = ERR_ABORTED;
9441
9471
  exports.ERR_AMBIGUOUS_ARCHIVE = ERR_AMBIGUOUS_ARCHIVE;
9442
9472
  exports.ERR_BAD_FORMAT = ERR_BAD_FORMAT;
9443
9473
  exports.ERR_CENTRAL_DIRECTORY_NOT_FOUND = ERR_CENTRAL_DIRECTORY_NOT_FOUND;