@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/dist/zip.js CHANGED
@@ -4,7 +4,7 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.zip = {}));
5
5
  })(this, (function (exports) { 'use strict';
6
6
 
7
- const { Array, Object, String, Number, BigInt, Math, Date, Map, Set, Response, URL, Error, Uint8Array, Uint16Array, Uint32Array, DataView, Blob, Promise, TextEncoder, TextDecoder, document, crypto, btoa, TransformStream, ReadableStream, WritableStream, CompressionStream, DecompressionStream, navigator, Worker, Symbol, setTimeout, clearTimeout, structuredClone } = typeof globalThis !== 'undefined' ? globalThis : this || self;
7
+ const { Array, Object, String, Number, BigInt, Math, Date, Map, Set, Response, URL, Error, Uint8Array, Uint16Array, Uint32Array, DataView, Blob, Promise, TextEncoder, TextDecoder, document, crypto, btoa, TransformStream, ReadableStream, WritableStream, CompressionStream, DecompressionStream, navigator, Worker, Symbol, setTimeout, clearTimeout, structuredClone } = typeof globalThis !== 'undefined' ? globalThis : self;
8
8
 
9
9
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
10
10
  /*
@@ -3614,11 +3614,10 @@
3614
3614
  const data = await readUint8Array(reader, offset + chunkOffset, dataSize);
3615
3615
  if (data.length) {
3616
3616
  controller.enqueue(data);
3617
+ chunkOffset += data.length;
3617
3618
  }
3618
- if ((chunkOffset + chunkSize >= size) || (!data.length && dataSize)) {
3619
+ if ((size !== UNDEFINED_VALUE && chunkOffset >= size) || (!data.length && dataSize)) {
3619
3620
  controller.close();
3620
- } else {
3621
- chunkOffset += chunkSize;
3622
3621
  }
3623
3622
  }
3624
3623
  });
@@ -6253,6 +6252,7 @@
6253
6252
  const ERR_INVALID_VERSION = "Version exceeds 65535";
6254
6253
  const ERR_INVALID_ENCRYPTION_STRENGTH = "The strength must equal 1, 2, or 3";
6255
6254
  const ERR_UNSUPPORTED_ENCRYPTION_USDZ = "Encryption is not supported in USDZ files";
6255
+ const ERR_UNSUPPORTED_SPLIT_USDZ = "Split zip files are not supported in USDZ files";
6256
6256
  const ERR_UNSUPPORTED_ENCRYPTION_PASS_THROUGH = "Encryption is not supported when the 'passThrough' option is set";
6257
6257
  const ERR_INVALID_EXTRAFIELD = "Invalid extra field (must be a Map)";
6258
6258
  const ERR_INVALID_EXTRAFIELD_TYPE = "Invalid extra field type (must be integer 0..65535)";
@@ -6267,6 +6267,7 @@
6267
6267
  const ERR_UNDEFINED_COMPRESSION_METHOD = "Undefined compression method";
6268
6268
  const ERR_UNDETERMINED_SIZE = "Undetermined size";
6269
6269
  const ERR_UNDEFINED_READER = "Undefined reader";
6270
+ const ERR_INVALID_READER = "Invalid reader (must be a Reader instance, a ReadableStream instance, or an object with a 'readable' property)";
6270
6271
  const ERR_ZIP_NOT_EMPTY = "Zip file not empty";
6271
6272
  const ERR_INVALID_UID = "Invalid uid (must be integer 0..2^32-1)";
6272
6273
  const ERR_INVALID_GID = "Invalid gid (must be integer 0..2^32-1)";
@@ -6298,6 +6299,9 @@
6298
6299
  const addSplitZipSignature =
6299
6300
  availableSize > 0 && availableSize !== INFINITY_VALUE &&
6300
6301
  maxSize > 0 && maxSize !== INFINITY_VALUE;
6302
+ if (addSplitZipSignature && options[OPTION_USDZ]) {
6303
+ throw new Error(ERR_UNSUPPORTED_SPLIT_USDZ);
6304
+ }
6301
6305
  Object.assign(this, {
6302
6306
  writer,
6303
6307
  addSplitZipSignature,
@@ -6309,6 +6313,7 @@
6309
6313
  pendingAddFileCalls: new Set(),
6310
6314
  pendingErrors: [],
6311
6315
  bufferedWrites: 0,
6316
+ directWrites: 0,
6312
6317
  lastFileEntry: UNDEFINED_VALUE
6313
6318
  });
6314
6319
  }
@@ -6412,6 +6417,7 @@
6412
6417
  }
6413
6418
  const { offset, diskNumberStart } = entryPositions.get(entry);
6414
6419
  Object.assign(entry, {
6420
+ zip64Enabled: true,
6415
6421
  zip64UncompressedSize,
6416
6422
  zip64CompressedSize,
6417
6423
  offset,
@@ -6987,6 +6993,9 @@
6987
6993
  if (reader) {
6988
6994
  reader = new GenericReader(reader);
6989
6995
  await initStream(reader);
6996
+ if (!reader.readable && !reader.readUint8Array) {
6997
+ throw new Error(ERR_INVALID_READER);
6998
+ }
6990
6999
  ({ size: contentSize } = reader);
6991
7000
  }
6992
7001
  return Object.assign({ reader }, resolveEntrySizes(zipWriter, Boolean(reader), contentSize, metadata, options));
@@ -6997,6 +7006,7 @@
6997
7006
  let { dataDescriptor, zip64, level, compressionMethod } = metadata;
6998
7007
  let maximumCompressedSize = 0;
6999
7008
  let uncompressedSize = 0;
7009
+ let unknownSize = false;
7000
7010
  if (passThrough && hasContent) {
7001
7011
  uncompressedSize = options[PROPERTY_NAME_UNCOMPRESSED_SIZE];
7002
7012
  if (uncompressedSize === UNDEFINED_VALUE) {
@@ -7022,8 +7032,8 @@
7022
7032
  if (contentSize === UNDEFINED_VALUE) {
7023
7033
  dataDescriptor = true;
7024
7034
  if (zip64 || zip64 === UNDEFINED_VALUE) {
7025
- zip64 = true;
7026
- uncompressedSize = maximumCompressedSize = MAX_32_BITS + 1;
7035
+ zip64 = unknownSize = true;
7036
+ maximumCompressedSize = MAX_32_BITS + 1;
7027
7037
  }
7028
7038
  } else {
7029
7039
  options.uncompressedSize = uncompressedSize = contentSize;
@@ -7038,7 +7048,7 @@
7038
7048
  if (emptyEntry && !zipCrypto && getOptionValue(zipWriter, options, OPTION_DATA_DESCRIPTOR) === UNDEFINED_VALUE) {
7039
7049
  dataDescriptor = false;
7040
7050
  }
7041
- const zip64UncompressedSize = zip64Enabled || uncompressedSize >= MAX_32_BITS;
7051
+ const zip64UncompressedSize = zip64Enabled || unknownSize || uncompressedSize >= MAX_32_BITS;
7042
7052
  const zip64CompressedSize = zip64Enabled || maximumCompressedSize >= MAX_32_BITS;
7043
7053
  if (zip64UncompressedSize || zip64CompressedSize) {
7044
7054
  if (zip64 === false) {
@@ -7054,6 +7064,8 @@
7054
7064
  dataDescriptor,
7055
7065
  emptyEntry,
7056
7066
  zip64,
7067
+ zip64Enabled,
7068
+ unknownSize,
7057
7069
  zip64UncompressedSize,
7058
7070
  zip64CompressedSize,
7059
7071
  uncompressedSize,
@@ -7084,6 +7096,7 @@
7084
7096
  const usdz = zipWriter.options[OPTION_USDZ];
7085
7097
  let fileEntry = pendingFileEntry;
7086
7098
  let bufferedWrite;
7099
+ let directWrite;
7087
7100
  let releaseLockWriter;
7088
7101
  let writingBufferedEntryData;
7089
7102
  let writingEntryData;
@@ -7093,7 +7106,7 @@
7093
7106
  const lockPreviousFileEntry = keepOrder && previousFileEntry ? previousFileEntry.lockFileEntry : UNDEFINED_VALUE;
7094
7107
  fileEntries.set(name, fileEntry);
7095
7108
  try {
7096
- if (options.bufferedWrite || !keepOrder || zipWriter.writerLocked || zipWriter.bufferedWrites || (!dataDescriptor && !emptyEntry)) {
7109
+ if (options.bufferedWrite || !keepOrder || zipWriter.writerLocked || zipWriter.bufferedWrites || zipWriter.directWrites || (!dataDescriptor && !emptyEntry)) {
7097
7110
  bufferedWrite = true;
7098
7111
  zipWriter.bufferedWrites++;
7099
7112
  if (options.createTempStream) {
@@ -7104,6 +7117,8 @@
7104
7117
  fileWriter.size = 0;
7105
7118
  await initStream(writer);
7106
7119
  } else {
7120
+ directWrite = true;
7121
+ zipWriter.directWrites++;
7107
7122
  fileWriter = writer;
7108
7123
  await lockPreviousFileEntry;
7109
7124
  await requestLockWriter();
@@ -7182,6 +7197,9 @@
7182
7197
  if (bufferedWrite) {
7183
7198
  zipWriter.bufferedWrites--;
7184
7199
  }
7200
+ if (directWrite) {
7201
+ zipWriter.directWrites--;
7202
+ }
7185
7203
  if (releaseLockFileEntry) {
7186
7204
  releaseLockFileEntry(lockPreviousFileEntry);
7187
7205
  }
@@ -7246,6 +7264,7 @@
7246
7264
  rawPassword,
7247
7265
  level,
7248
7266
  zip64,
7267
+ zip64Enabled,
7249
7268
  zip64UncompressedSize,
7250
7269
  zip64CompressedSize,
7251
7270
  zipCrypto,
@@ -7286,6 +7305,7 @@
7286
7305
  lockFileEntry,
7287
7306
  versionMadeBy,
7288
7307
  zip64,
7308
+ zip64Enabled,
7289
7309
  directory: Boolean(directory),
7290
7310
  executable: Boolean(executable),
7291
7311
  filenameUTF8: true,
@@ -7326,8 +7346,8 @@
7326
7346
  }
7327
7347
  const { writable } = writer;
7328
7348
  if (reader) {
7329
- const readable = toCompatibleReadable(createReadable(reader));
7330
7349
  const size = reader.size;
7350
+ const readable = toCompatibleReadable(createReadable(reader, { size }));
7331
7351
  const workerOptions = {
7332
7352
  options: {
7333
7353
  codecType: CODEC_DEFLATE,
@@ -7429,13 +7449,14 @@
7429
7449
  zip64UncompressedSize,
7430
7450
  zip64CompressedSize,
7431
7451
  uncompressedSize,
7452
+ unknownSize,
7432
7453
  crc32
7433
7454
  } = options;
7434
7455
  let { version, compressionMethod } = options;
7435
7456
  const compressed = !directory && isCompressed(compressionMethod, level);
7436
7457
  let rawLocalExtraFieldZip64;
7437
7458
  const uncompressedFile = passThrough || !compressed;
7438
- const zip64ExtraFieldComplete = zip64 && (options.bufferedWrite || !dataDescriptor || ((!zip64UncompressedSize && !zip64CompressedSize) || uncompressedFile));
7459
+ const zip64ExtraFieldComplete = zip64 && (options.bufferedWrite || !dataDescriptor || ((!zip64UncompressedSize && !zip64CompressedSize) || (uncompressedFile && !unknownSize)));
7439
7460
  const writeLocalExtraFieldZip64 = zip64ExtraFieldComplete || (zip64 && dataDescriptor && (zip64UncompressedSize || zip64CompressedSize));
7440
7461
  if (zip64 && (zip64UncompressedSize || zip64CompressedSize)) {
7441
7462
  const length = 4 + 16;
@@ -7569,6 +7590,10 @@
7569
7590
  throw new Error(ERR_INVALID_EXTRAFIELD_DATA);
7570
7591
  }
7571
7592
  const dosLastModDate = new Date(Math.ceil(Math.floor(lastModDate.getTime() / 1000) / 2) * 2000);
7593
+ const clampedLastModDate = dosLastModDate < MIN_DATE ? MIN_DATE : dosLastModDate > MAX_DATE ? MAX_DATE : dosLastModDate;
7594
+ const storedLastModDate = getLength(rawExtraFieldExtendedTimestamp) ?
7595
+ new Date(getTimeUnix(lastModDate) * 1000) :
7596
+ getLength(rawExtraFieldNTFS) ? lastModDate : clampedLastModDate;
7572
7597
  const {
7573
7598
  headerArray,
7574
7599
  headerView,
@@ -7578,7 +7603,7 @@
7578
7603
  bitFlag: getBitFlag(level, useUnicodeFileNames, dataDescriptor, encrypted, compressionMethod),
7579
7604
  compressionMethod,
7580
7605
  uncompressedSize,
7581
- lastModDate: dosLastModDate < MIN_DATE ? MIN_DATE : dosLastModDate > MAX_DATE ? MAX_DATE : dosLastModDate,
7606
+ lastModDate: clampedLastModDate,
7582
7607
  rawLastModDate: rawLastModDateOption,
7583
7608
  rawFilename,
7584
7609
  zip64CompressedSize,
@@ -7613,7 +7638,7 @@
7613
7638
  localHeaderView,
7614
7639
  headerArray,
7615
7640
  headerView,
7616
- lastModDate,
7641
+ lastModDate: storedLastModDate,
7617
7642
  rawLastModDate,
7618
7643
  encrypted,
7619
7644
  compressed,
@@ -7785,14 +7810,15 @@
7785
7810
 
7786
7811
 
7787
7812
  async function closeFile(zipWriter, comment, options) {
7788
- const directoryDataLength = createDirectoryRecords(zipWriter.fileEntries);
7813
+ const { directoryDataLength, zip64Entries } = createDirectoryRecords(zipWriter.fileEntries);
7789
7814
  const { directoryStart, directoryEnd, directoryArray } = await writeDirectoryRecords(zipWriter, directoryDataLength, options);
7790
7815
  const signatureLength = await writeDigitalSignatureRecord(zipWriter, directoryArray, options);
7791
- await writeEndOfDirectoryRecord(zipWriter, comment, options, { directoryStart, directoryEnd, directoryDataLength, signatureLength });
7816
+ await writeEndOfDirectoryRecord(zipWriter, comment, options, { directoryStart, directoryEnd, directoryDataLength, signatureLength, zip64Entries });
7792
7817
  }
7793
7818
 
7794
7819
  function createDirectoryRecords(files) {
7795
7820
  let directoryDataLength = 0;
7821
+ let zip64Entries = false;
7796
7822
  for (const [, fileEntry] of files) {
7797
7823
  const {
7798
7824
  rawFilename,
@@ -7805,11 +7831,20 @@
7805
7831
  extendedTimestamp,
7806
7832
  extraFieldExtendedTimestampFlag,
7807
7833
  lastModDate,
7808
- zip64UncompressedSize,
7809
- zip64CompressedSize,
7834
+ zip64Enabled,
7810
7835
  uncompressedSize,
7811
7836
  compressedSize
7812
7837
  } = fileEntry;
7838
+ let { zip64UncompressedSize, zip64CompressedSize } = fileEntry;
7839
+ if (!zip64Enabled) {
7840
+ if (zip64UncompressedSize && uncompressedSize < MAX_32_BITS) {
7841
+ zip64UncompressedSize = fileEntry.zip64UncompressedSize = false;
7842
+ }
7843
+ if (zip64CompressedSize && compressedSize < MAX_32_BITS) {
7844
+ zip64CompressedSize = fileEntry.zip64CompressedSize = false;
7845
+ }
7846
+ }
7847
+ zip64Entries = zip64Entries || zip64UncompressedSize || zip64CompressedSize;
7813
7848
  const zip64Offset = fileEntry.offset >= MAX_32_BITS;
7814
7849
  const zip64DiskNumberStart = fileEntry.diskNumberStart >= MAX_16_BITS;
7815
7850
  let rawExtraFieldZip64;
@@ -7863,7 +7898,7 @@
7863
7898
  }
7864
7899
  directoryDataLength += CENTRAL_FILE_HEADER_LENGTH + getLength(rawFilename, rawComment) + extraFieldLength;
7865
7900
  }
7866
- return directoryDataLength;
7901
+ return { directoryDataLength, zip64Entries };
7867
7902
  }
7868
7903
 
7869
7904
  async function writeDirectoryRecords(zipWriter, directoryDataLength, options) {
@@ -7983,7 +8018,7 @@
7983
8018
 
7984
8019
  async function writeEndOfDirectoryRecord(zipWriter, comment, options, cdInfo) {
7985
8020
  const { writer } = zipWriter;
7986
- const { directoryStart, directoryEnd, signatureLength } = cdInfo;
8021
+ const { directoryStart, directoryEnd, signatureLength, zip64Entries } = cdInfo;
7987
8022
  let { directoryDataLength } = cdInfo;
7988
8023
  let fileEntriesLength = zipWriter.fileEntries.size;
7989
8024
  let diskNumber = directoryStart.diskNumber;
@@ -8003,6 +8038,8 @@
8003
8038
  } else {
8004
8039
  zip64 = true;
8005
8040
  }
8041
+ } else if (zip64 === UNDEFINED_VALUE && zip64Entries) {
8042
+ zip64 = true;
8006
8043
  }
8007
8044
  const endOfdirectoryRecord = createRecordWriter(zip64 ? ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH : END_OF_CENTRAL_DIR_LENGTH);
8008
8045
  if (exceedsAvailableSize(writer, getLength(endOfdirectoryRecord.array) + commentLength)) {
@@ -8506,7 +8543,7 @@
8506
8543
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8507
8544
  */
8508
8545
 
8509
- const VERSION = "2.11.1";
8546
+ const VERSION = "2.11.3";
8510
8547
 
8511
8548
  /*
8512
8549
  Copyright (c) 2025 Gildas Lormeau. All rights reserved.
@@ -9438,6 +9475,7 @@
9438
9475
  exports.ERR_INVALID_MSDOS_DATA = ERR_INVALID_MSDOS_DATA;
9439
9476
  exports.ERR_INVALID_PASSWORD = ERR_INVALID_PASSWORD;
9440
9477
  exports.ERR_INVALID_PASSWORD_TYPE = ERR_INVALID_PASSWORD_TYPE;
9478
+ exports.ERR_INVALID_READER = ERR_INVALID_READER;
9441
9479
  exports.ERR_INVALID_SIGNAL = ERR_INVALID_SIGNAL;
9442
9480
  exports.ERR_INVALID_SIGNATURE_DATA = ERR_INVALID_SIGNATURE_DATA;
9443
9481
  exports.ERR_INVALID_STRICTNESS = ERR_INVALID_STRICTNESS;
@@ -9464,6 +9502,7 @@
9464
9502
  exports.ERR_UNSUPPORTED_ENCRYPTION_PASS_THROUGH = ERR_UNSUPPORTED_ENCRYPTION_PASS_THROUGH;
9465
9503
  exports.ERR_UNSUPPORTED_ENCRYPTION_USDZ = ERR_UNSUPPORTED_ENCRYPTION_USDZ;
9466
9504
  exports.ERR_UNSUPPORTED_FORMAT = ERR_UNSUPPORTED_FORMAT;
9505
+ exports.ERR_UNSUPPORTED_SPLIT_USDZ = ERR_UNSUPPORTED_SPLIT_USDZ;
9467
9506
  exports.ERR_UNSUPPORTED_UINT64 = ERR_UNSUPPORTED_UINT64;
9468
9507
  exports.ERR_WORKER_STARTUP_TIMEOUT = ERR_WORKER_STARTUP_TIMEOUT;
9469
9508
  exports.ERR_WRITER_NOT_INITIALIZED = ERR_WRITER_NOT_INITIALIZED;