@zip.js/zip.js 2.11.1 → 2.11.3

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-native.cjs CHANGED
@@ -3604,11 +3604,10 @@ class Reader extends Stream {
3604
3604
  const data = await readUint8Array(reader, offset + chunkOffset, dataSize);
3605
3605
  if (data.length) {
3606
3606
  controller.enqueue(data);
3607
+ chunkOffset += data.length;
3607
3608
  }
3608
- if ((chunkOffset + chunkSize >= size) || (!data.length && dataSize)) {
3609
+ if ((size !== UNDEFINED_VALUE && chunkOffset >= size) || (!data.length && dataSize)) {
3609
3610
  controller.close();
3610
- } else {
3611
- chunkOffset += chunkSize;
3612
3611
  }
3613
3612
  }
3614
3613
  });
@@ -6263,6 +6262,7 @@ const ERR_INVALID_ENTRY_NAME = "File entry name exceeds 64KB";
6263
6262
  const ERR_INVALID_VERSION = "Version exceeds 65535";
6264
6263
  const ERR_INVALID_ENCRYPTION_STRENGTH = "The strength must equal 1, 2, or 3";
6265
6264
  const ERR_UNSUPPORTED_ENCRYPTION_USDZ = "Encryption is not supported in USDZ files";
6265
+ const ERR_UNSUPPORTED_SPLIT_USDZ = "Split zip files are not supported in USDZ files";
6266
6266
  const ERR_UNSUPPORTED_ENCRYPTION_PASS_THROUGH = "Encryption is not supported when the 'passThrough' option is set";
6267
6267
  const ERR_INVALID_EXTRAFIELD = "Invalid extra field (must be a Map)";
6268
6268
  const ERR_INVALID_EXTRAFIELD_TYPE = "Invalid extra field type (must be integer 0..65535)";
@@ -6277,6 +6277,7 @@ const ERR_UNDEFINED_UNCOMPRESSED_SIZE = "Undefined uncompressed size";
6277
6277
  const ERR_UNDEFINED_COMPRESSION_METHOD = "Undefined compression method";
6278
6278
  const ERR_UNDETERMINED_SIZE = "Undetermined size";
6279
6279
  const ERR_UNDEFINED_READER = "Undefined reader";
6280
+ const ERR_INVALID_READER = "Invalid reader (must be a Reader instance, a ReadableStream instance, or an object with a 'readable' property)";
6280
6281
  const ERR_ZIP_NOT_EMPTY = "Zip file not empty";
6281
6282
  const ERR_INVALID_UID = "Invalid uid (must be integer 0..2^32-1)";
6282
6283
  const ERR_INVALID_GID = "Invalid gid (must be integer 0..2^32-1)";
@@ -6308,6 +6309,9 @@ class ZipWriter {
6308
6309
  const addSplitZipSignature =
6309
6310
  availableSize > 0 && availableSize !== INFINITY_VALUE &&
6310
6311
  maxSize > 0 && maxSize !== INFINITY_VALUE;
6312
+ if (addSplitZipSignature && options[OPTION_USDZ]) {
6313
+ throw new Error(ERR_UNSUPPORTED_SPLIT_USDZ);
6314
+ }
6311
6315
  Object.assign(this, {
6312
6316
  writer,
6313
6317
  addSplitZipSignature,
@@ -6319,6 +6323,7 @@ class ZipWriter {
6319
6323
  pendingAddFileCalls: new Set(),
6320
6324
  pendingErrors: [],
6321
6325
  bufferedWrites: 0,
6326
+ directWrites: 0,
6322
6327
  lastFileEntry: UNDEFINED_VALUE
6323
6328
  });
6324
6329
  }
@@ -6422,6 +6427,7 @@ class ZipWriter {
6422
6427
  }
6423
6428
  const { offset, diskNumberStart } = entryPositions.get(entry);
6424
6429
  Object.assign(entry, {
6430
+ zip64Enabled: true,
6425
6431
  zip64UncompressedSize,
6426
6432
  zip64CompressedSize,
6427
6433
  offset,
@@ -6997,6 +7003,9 @@ async function resolveSizes(zipWriter, reader, { resolvedOptions: metadata }, op
6997
7003
  if (reader) {
6998
7004
  reader = new GenericReader(reader);
6999
7005
  await initStream(reader);
7006
+ if (!reader.readable && !reader.readUint8Array) {
7007
+ throw new Error(ERR_INVALID_READER);
7008
+ }
7000
7009
  ({ size: contentSize } = reader);
7001
7010
  }
7002
7011
  return Object.assign({ reader }, resolveEntrySizes(zipWriter, Boolean(reader), contentSize, metadata, options));
@@ -7007,6 +7016,7 @@ function resolveEntrySizes(zipWriter, hasContent, contentSize, metadata, options
7007
7016
  let { dataDescriptor, zip64, level, compressionMethod } = metadata;
7008
7017
  let maximumCompressedSize = 0;
7009
7018
  let uncompressedSize = 0;
7019
+ let unknownSize = false;
7010
7020
  if (passThrough && hasContent) {
7011
7021
  uncompressedSize = options[PROPERTY_NAME_UNCOMPRESSED_SIZE];
7012
7022
  if (uncompressedSize === UNDEFINED_VALUE) {
@@ -7032,8 +7042,8 @@ function resolveEntrySizes(zipWriter, hasContent, contentSize, metadata, options
7032
7042
  if (contentSize === UNDEFINED_VALUE) {
7033
7043
  dataDescriptor = true;
7034
7044
  if (zip64 || zip64 === UNDEFINED_VALUE) {
7035
- zip64 = true;
7036
- uncompressedSize = maximumCompressedSize = MAX_32_BITS + 1;
7045
+ zip64 = unknownSize = true;
7046
+ maximumCompressedSize = MAX_32_BITS + 1;
7037
7047
  }
7038
7048
  } else {
7039
7049
  options.uncompressedSize = uncompressedSize = contentSize;
@@ -7048,7 +7058,7 @@ function resolveEntrySizes(zipWriter, hasContent, contentSize, metadata, options
7048
7058
  if (emptyEntry && !zipCrypto && getOptionValue(zipWriter, options, OPTION_DATA_DESCRIPTOR) === UNDEFINED_VALUE) {
7049
7059
  dataDescriptor = false;
7050
7060
  }
7051
- const zip64UncompressedSize = zip64Enabled || uncompressedSize >= MAX_32_BITS;
7061
+ const zip64UncompressedSize = zip64Enabled || unknownSize || uncompressedSize >= MAX_32_BITS;
7052
7062
  const zip64CompressedSize = zip64Enabled || maximumCompressedSize >= MAX_32_BITS;
7053
7063
  if (zip64UncompressedSize || zip64CompressedSize) {
7054
7064
  if (zip64 === false) {
@@ -7064,6 +7074,8 @@ function resolveEntrySizes(zipWriter, hasContent, contentSize, metadata, options
7064
7074
  dataDescriptor,
7065
7075
  emptyEntry,
7066
7076
  zip64,
7077
+ zip64Enabled,
7078
+ unknownSize,
7067
7079
  zip64UncompressedSize,
7068
7080
  zip64CompressedSize,
7069
7081
  uncompressedSize,
@@ -7137,7 +7149,7 @@ async function getEntriesSize(writerOptions, entries, writeOrderGuaranteed, comm
7137
7149
  if (layoutDependsOnWriteOrder && !writeOrderGuaranteed) {
7138
7150
  throw new Error(ERR_UNDETERMINED_SIZE);
7139
7151
  }
7140
- const directoryDataLength = createDirectoryRecords(files);
7152
+ const { directoryDataLength, zip64Entries } = createDirectoryRecords(files);
7141
7153
  let zip64 = getOptionValue(zipWriter, writerOptions, PROPERTY_NAME_ZIP64);
7142
7154
  if (offset >= MAX_32_BITS || directoryDataLength >= MAX_32_BITS || files.size >= MAX_16_BITS) {
7143
7155
  if (zip64 === false) {
@@ -7145,6 +7157,8 @@ async function getEntriesSize(writerOptions, entries, writeOrderGuaranteed, comm
7145
7157
  } else {
7146
7158
  zip64 = true;
7147
7159
  }
7160
+ } else if (zip64 === UNDEFINED_VALUE && zip64Entries) {
7161
+ zip64 = true;
7148
7162
  }
7149
7163
  return offset - initialOffset + directoryDataLength + commentLength + (zip64 ? ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH : END_OF_CENTRAL_DIR_LENGTH);
7150
7164
  }
@@ -7169,6 +7183,7 @@ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
7169
7183
  const usdz = zipWriter.options[OPTION_USDZ];
7170
7184
  let fileEntry = pendingFileEntry;
7171
7185
  let bufferedWrite;
7186
+ let directWrite;
7172
7187
  let releaseLockWriter;
7173
7188
  let writingBufferedEntryData;
7174
7189
  let writingEntryData;
@@ -7178,7 +7193,7 @@ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
7178
7193
  const lockPreviousFileEntry = keepOrder && previousFileEntry ? previousFileEntry.lockFileEntry : UNDEFINED_VALUE;
7179
7194
  fileEntries.set(name, fileEntry);
7180
7195
  try {
7181
- if (options.bufferedWrite || !keepOrder || zipWriter.writerLocked || zipWriter.bufferedWrites || (!dataDescriptor && !emptyEntry)) {
7196
+ if (options.bufferedWrite || !keepOrder || zipWriter.writerLocked || zipWriter.bufferedWrites || zipWriter.directWrites || (!dataDescriptor && !emptyEntry)) {
7182
7197
  bufferedWrite = true;
7183
7198
  zipWriter.bufferedWrites++;
7184
7199
  if (options.createTempStream) {
@@ -7189,6 +7204,8 @@ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
7189
7204
  fileWriter.size = 0;
7190
7205
  await initStream(writer);
7191
7206
  } else {
7207
+ directWrite = true;
7208
+ zipWriter.directWrites++;
7192
7209
  fileWriter = writer;
7193
7210
  await lockPreviousFileEntry;
7194
7211
  await requestLockWriter();
@@ -7267,6 +7284,9 @@ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
7267
7284
  if (bufferedWrite) {
7268
7285
  zipWriter.bufferedWrites--;
7269
7286
  }
7287
+ if (directWrite) {
7288
+ zipWriter.directWrites--;
7289
+ }
7270
7290
  if (releaseLockFileEntry) {
7271
7291
  releaseLockFileEntry(lockPreviousFileEntry);
7272
7292
  }
@@ -7331,6 +7351,7 @@ async function createFileEntry(reader, writer, { diskNumberStart, lockFileEntry
7331
7351
  rawPassword,
7332
7352
  level,
7333
7353
  zip64,
7354
+ zip64Enabled,
7334
7355
  zip64UncompressedSize,
7335
7356
  zip64CompressedSize,
7336
7357
  zipCrypto,
@@ -7371,6 +7392,7 @@ async function createFileEntry(reader, writer, { diskNumberStart, lockFileEntry
7371
7392
  lockFileEntry,
7372
7393
  versionMadeBy,
7373
7394
  zip64,
7395
+ zip64Enabled,
7374
7396
  directory: Boolean(directory),
7375
7397
  executable: Boolean(executable),
7376
7398
  filenameUTF8: true,
@@ -7411,8 +7433,8 @@ async function createFileEntry(reader, writer, { diskNumberStart, lockFileEntry
7411
7433
  }
7412
7434
  const { writable } = writer;
7413
7435
  if (reader) {
7414
- const readable = toCompatibleReadable(createReadable(reader));
7415
7436
  const size = reader.size;
7437
+ const readable = toCompatibleReadable(createReadable(reader, { size }));
7416
7438
  const workerOptions = {
7417
7439
  options: {
7418
7440
  codecType: CODEC_DEFLATE,
@@ -7514,13 +7536,14 @@ function getHeaderInfo(options) {
7514
7536
  zip64UncompressedSize,
7515
7537
  zip64CompressedSize,
7516
7538
  uncompressedSize,
7539
+ unknownSize,
7517
7540
  crc32
7518
7541
  } = options;
7519
7542
  let { version, compressionMethod } = options;
7520
7543
  const compressed = !directory && isCompressed(compressionMethod, level);
7521
7544
  let rawLocalExtraFieldZip64;
7522
7545
  const uncompressedFile = passThrough || !compressed;
7523
- const zip64ExtraFieldComplete = zip64 && (options.bufferedWrite || !dataDescriptor || ((!zip64UncompressedSize && !zip64CompressedSize) || uncompressedFile));
7546
+ const zip64ExtraFieldComplete = zip64 && (options.bufferedWrite || !dataDescriptor || ((!zip64UncompressedSize && !zip64CompressedSize) || (uncompressedFile && !unknownSize)));
7524
7547
  const writeLocalExtraFieldZip64 = zip64ExtraFieldComplete || (zip64 && dataDescriptor && (zip64UncompressedSize || zip64CompressedSize));
7525
7548
  if (zip64 && (zip64UncompressedSize || zip64CompressedSize)) {
7526
7549
  const length = 4 + 16;
@@ -7654,6 +7677,10 @@ function getHeaderInfo(options) {
7654
7677
  throw new Error(ERR_INVALID_EXTRAFIELD_DATA);
7655
7678
  }
7656
7679
  const dosLastModDate = new Date(Math.ceil(Math.floor(lastModDate.getTime() / 1000) / 2) * 2000);
7680
+ const clampedLastModDate = dosLastModDate < MIN_DATE ? MIN_DATE : dosLastModDate > MAX_DATE ? MAX_DATE : dosLastModDate;
7681
+ const storedLastModDate = getLength(rawExtraFieldExtendedTimestamp) ?
7682
+ new Date(getTimeUnix(lastModDate) * 1000) :
7683
+ getLength(rawExtraFieldNTFS) ? lastModDate : clampedLastModDate;
7657
7684
  const {
7658
7685
  headerArray,
7659
7686
  headerView,
@@ -7663,7 +7690,7 @@ function getHeaderInfo(options) {
7663
7690
  bitFlag: getBitFlag(level, useUnicodeFileNames, dataDescriptor, encrypted, compressionMethod),
7664
7691
  compressionMethod,
7665
7692
  uncompressedSize,
7666
- lastModDate: dosLastModDate < MIN_DATE ? MIN_DATE : dosLastModDate > MAX_DATE ? MAX_DATE : dosLastModDate,
7693
+ lastModDate: clampedLastModDate,
7667
7694
  rawLastModDate: rawLastModDateOption,
7668
7695
  rawFilename,
7669
7696
  zip64CompressedSize,
@@ -7698,7 +7725,7 @@ function getHeaderInfo(options) {
7698
7725
  localHeaderView,
7699
7726
  headerArray,
7700
7727
  headerView,
7701
- lastModDate,
7728
+ lastModDate: storedLastModDate,
7702
7729
  rawLastModDate,
7703
7730
  encrypted,
7704
7731
  compressed,
@@ -7870,14 +7897,15 @@ function updateLocalHeader({
7870
7897
 
7871
7898
 
7872
7899
  async function closeFile(zipWriter, comment, options) {
7873
- const directoryDataLength = createDirectoryRecords(zipWriter.fileEntries);
7900
+ const { directoryDataLength, zip64Entries } = createDirectoryRecords(zipWriter.fileEntries);
7874
7901
  const { directoryStart, directoryEnd, directoryArray } = await writeDirectoryRecords(zipWriter, directoryDataLength, options);
7875
7902
  const signatureLength = await writeDigitalSignatureRecord(zipWriter, directoryArray, options);
7876
- await writeEndOfDirectoryRecord(zipWriter, comment, options, { directoryStart, directoryEnd, directoryDataLength, signatureLength });
7903
+ await writeEndOfDirectoryRecord(zipWriter, comment, options, { directoryStart, directoryEnd, directoryDataLength, signatureLength, zip64Entries });
7877
7904
  }
7878
7905
 
7879
7906
  function createDirectoryRecords(files) {
7880
7907
  let directoryDataLength = 0;
7908
+ let zip64Entries = false;
7881
7909
  for (const [, fileEntry] of files) {
7882
7910
  const {
7883
7911
  rawFilename,
@@ -7890,11 +7918,20 @@ function createDirectoryRecords(files) {
7890
7918
  extendedTimestamp,
7891
7919
  extraFieldExtendedTimestampFlag,
7892
7920
  lastModDate,
7893
- zip64UncompressedSize,
7894
- zip64CompressedSize,
7921
+ zip64Enabled,
7895
7922
  uncompressedSize,
7896
7923
  compressedSize
7897
7924
  } = fileEntry;
7925
+ let { zip64UncompressedSize, zip64CompressedSize } = fileEntry;
7926
+ if (!zip64Enabled) {
7927
+ if (zip64UncompressedSize && uncompressedSize < MAX_32_BITS) {
7928
+ zip64UncompressedSize = fileEntry.zip64UncompressedSize = false;
7929
+ }
7930
+ if (zip64CompressedSize && compressedSize < MAX_32_BITS) {
7931
+ zip64CompressedSize = fileEntry.zip64CompressedSize = false;
7932
+ }
7933
+ }
7934
+ zip64Entries = zip64Entries || zip64UncompressedSize || zip64CompressedSize;
7898
7935
  const zip64Offset = fileEntry.offset >= MAX_32_BITS;
7899
7936
  const zip64DiskNumberStart = fileEntry.diskNumberStart >= MAX_16_BITS;
7900
7937
  let rawExtraFieldZip64;
@@ -7948,7 +7985,7 @@ function createDirectoryRecords(files) {
7948
7985
  }
7949
7986
  directoryDataLength += CENTRAL_FILE_HEADER_LENGTH + getLength(rawFilename, rawComment) + extraFieldLength;
7950
7987
  }
7951
- return directoryDataLength;
7988
+ return { directoryDataLength, zip64Entries };
7952
7989
  }
7953
7990
 
7954
7991
  async function writeDirectoryRecords(zipWriter, directoryDataLength, options) {
@@ -8068,7 +8105,7 @@ async function writeDigitalSignatureRecord(zipWriter, directoryArray, options) {
8068
8105
 
8069
8106
  async function writeEndOfDirectoryRecord(zipWriter, comment, options, cdInfo) {
8070
8107
  const { writer } = zipWriter;
8071
- const { directoryStart, directoryEnd, signatureLength } = cdInfo;
8108
+ const { directoryStart, directoryEnd, signatureLength, zip64Entries } = cdInfo;
8072
8109
  let { directoryDataLength } = cdInfo;
8073
8110
  let fileEntriesLength = zipWriter.fileEntries.size;
8074
8111
  let diskNumber = directoryStart.diskNumber;
@@ -8088,6 +8125,8 @@ async function writeEndOfDirectoryRecord(zipWriter, comment, options, cdInfo) {
8088
8125
  } else {
8089
8126
  zip64 = true;
8090
8127
  }
8128
+ } else if (zip64 === UNDEFINED_VALUE && zip64Entries) {
8129
+ zip64 = true;
8091
8130
  }
8092
8131
  const endOfdirectoryRecord = createRecordWriter(zip64 ? ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH : END_OF_CENTRAL_DIR_LENGTH);
8093
8132
  if (exceedsAvailableSize(writer, getLength(endOfdirectoryRecord.array) + commentLength)) {
@@ -8591,7 +8630,7 @@ function formatSupported(StreamClass, format) {
8591
8630
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8592
8631
  */
8593
8632
 
8594
- const VERSION = "2.11.1";
8633
+ const VERSION = "2.11.3";
8595
8634
 
8596
8635
  /*
8597
8636
  Copyright (c) 2025 Gildas Lormeau. All rights reserved.
@@ -9615,12 +9654,14 @@ class ZipDirectoryEntry extends ZipEntry {
9615
9654
  if (options.bufferedWrite === UNDEFINED_VALUE) {
9616
9655
  options.bufferedWrite = true;
9617
9656
  }
9618
- const entries = zipEntry.getChildren({ recursive: true }).filter(child => !isImplicitDirectory(child)).map(child => {
9657
+ const children = zipEntry.getChildren({ recursive: true });
9658
+ const entries = children.filter(child => !isImplicitDirectory(child)).map(child => {
9619
9659
  const { name, entryOptions } = getChildEntryOptions(child, zipEntry, options);
9620
9660
  return { name, size: child.directory ? 0 : getDeterminedSize(child, isPassThrough(child, options)), options: entryOptions };
9621
9661
  });
9622
9662
  const writeOrderGuaranteed = !options.bufferedWrite ||
9623
- (entries.every(entry => entry.options.keepOrder !== false) && zipEntry.children.every(child => !child.children.length));
9663
+ (entries.every(entry => entry.options.keepOrder !== false) &&
9664
+ children.every(child => isImplicitDirectory(child) || !child.children.length));
9624
9665
  return getEntriesSize(options, entries, writeOrderGuaranteed, options.globalComment);
9625
9666
  }
9626
9667
 
@@ -10649,6 +10690,7 @@ exports.ERR_INVALID_MSDOS_DATA = ERR_INVALID_MSDOS_DATA;
10649
10690
  exports.ERR_INVALID_PASSWORD = ERR_INVALID_PASSWORD;
10650
10691
  exports.ERR_INVALID_PASSWORD_TYPE = ERR_INVALID_PASSWORD_TYPE;
10651
10692
  exports.ERR_INVALID_PASS_THROUGH = ERR_INVALID_PASS_THROUGH;
10693
+ exports.ERR_INVALID_READER = ERR_INVALID_READER;
10652
10694
  exports.ERR_INVALID_READER_OPTIONS = ERR_INVALID_READER_OPTIONS;
10653
10695
  exports.ERR_INVALID_SIGNAL = ERR_INVALID_SIGNAL;
10654
10696
  exports.ERR_INVALID_SIGNATURE_DATA = ERR_INVALID_SIGNATURE_DATA;
@@ -10677,6 +10719,7 @@ exports.ERR_UNSUPPORTED_ENCRYPTION = ERR_UNSUPPORTED_ENCRYPTION;
10677
10719
  exports.ERR_UNSUPPORTED_ENCRYPTION_PASS_THROUGH = ERR_UNSUPPORTED_ENCRYPTION_PASS_THROUGH;
10678
10720
  exports.ERR_UNSUPPORTED_ENCRYPTION_USDZ = ERR_UNSUPPORTED_ENCRYPTION_USDZ;
10679
10721
  exports.ERR_UNSUPPORTED_FORMAT = ERR_UNSUPPORTED_FORMAT;
10722
+ exports.ERR_UNSUPPORTED_SPLIT_USDZ = ERR_UNSUPPORTED_SPLIT_USDZ;
10680
10723
  exports.ERR_UNSUPPORTED_UINT64 = ERR_UNSUPPORTED_UINT64;
10681
10724
  exports.ERR_WORKER_STARTUP_TIMEOUT = ERR_WORKER_STARTUP_TIMEOUT;
10682
10725
  exports.ERR_WRITER_NOT_INITIALIZED = ERR_WRITER_NOT_INITIALIZED;