@zip.js/zip.js 2.8.56 → 2.8.57

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
@@ -4727,6 +4727,7 @@ const ERR_UNSAFE_FILENAME = "Unsafe filename";
4727
4727
  const ERR_INVALID_STRICTNESS = "Invalid strictness (must be 'strict', 'balanced' or 'tolerant')";
4728
4728
  const ERR_INVALID_FILENAME_VALIDATION = "Invalid filenameValidation (must be 'strict', 'balanced' or 'tolerant')";
4729
4729
  const ERR_INVALID_MAX_APPENDED_DATA_SIZE = "Invalid maxAppendedDataSize (must be a number greater than or equal to 0)";
4730
+ const ERR_UNSUPPORTED_UINT64 = "64-bit value exceeds Number.MAX_SAFE_INTEGER";
4730
4731
  const DRIVE_LETTER_REGEXP = /^[a-zA-Z]:/;
4731
4732
  const CHARSET_UTF8 = "utf-8";
4732
4733
  const PROPERTY_NAME_UTF8_SUFFIX = "UTF8";
@@ -4749,6 +4750,7 @@ const ZIP64_EXTRACTION = {
4749
4750
  bytes: 8
4750
4751
  }
4751
4752
  };
4753
+ const MAX_SAFE_UINT64 = BigInt(Number.MAX_SAFE_INTEGER);
4752
4754
  const MAX_END_OF_CENTRAL_DIR_PROBES = 64;
4753
4755
  const MAX_DEFLATE_EXPANSION_RATIO = 1032;
4754
4756
  const CENTRAL_DIRECTORY_UNREACHABLE = 0;
@@ -4844,7 +4846,7 @@ class ZipReader {
4844
4846
  zip64EndOfDirectoryVersion2 = getBigUint64(endOfDirectoryView, 4) > ZIP64_END_OF_CENTRAL_DIR_LENGTH - 12;
4845
4847
  if (zip64EndOfDirectoryVersion2) {
4846
4848
  const extensibleDataLength = Math.min(
4847
- Number(getBigUint64(endOfDirectoryView, 4)) - (ZIP64_END_OF_CENTRAL_DIR_LENGTH - 12),
4849
+ getBigUint64(endOfDirectoryView, 4) - (ZIP64_END_OF_CENTRAL_DIR_LENGTH - 12),
4848
4850
  reader.size - directoryDataOffset - ZIP64_END_OF_CENTRAL_DIR_LENGTH);
4849
4851
  if (extensibleDataLength > 0) {
4850
4852
  zip64EndOfDirectoryLength += extensibleDataLength;
@@ -5425,8 +5427,8 @@ function getDirectoryEncryptionInfo(rawExtensibleData) {
5425
5427
  const hashDataLength = getUint16$1(extensibleDataView, 26);
5426
5428
  Object.assign(directoryEncryptionInfo, {
5427
5429
  compressionMethod: getUint16$1(extensibleDataView, 0),
5428
- compressedSize: Number(getBigUint64(extensibleDataView, 2)),
5429
- uncompressedSize: Number(getBigUint64(extensibleDataView, 10)),
5430
+ compressedSize: getBigUint64(extensibleDataView, 2),
5431
+ uncompressedSize: getBigUint64(extensibleDataView, 10),
5430
5432
  encryptionAlgorithm: getUint16$1(extensibleDataView, 18),
5431
5433
  bitLength: getUint16$1(extensibleDataView, 20),
5432
5434
  flags: getUint16$1(extensibleDataView, 22),
@@ -6061,7 +6063,11 @@ function getUint32$1(view, offset) {
6061
6063
  }
6062
6064
 
6063
6065
  function getBigUint64(view, offset) {
6064
- return Number(view.getBigUint64(offset, true));
6066
+ const value = view.getBigUint64(offset, true);
6067
+ if (value > MAX_SAFE_UINT64) {
6068
+ throw new Error(ERR_UNSUPPORTED_UINT64);
6069
+ }
6070
+ return Number(value);
6065
6071
  }
6066
6072
 
6067
6073
  var zipReader = /*#__PURE__*/Object.freeze({
@@ -6090,6 +6096,7 @@ var zipReader = /*#__PURE__*/Object.freeze({
6090
6096
  ERR_UNSAFE_FILENAME: ERR_UNSAFE_FILENAME,
6091
6097
  ERR_UNSUPPORTED_COMPRESSION: ERR_UNSUPPORTED_COMPRESSION$1,
6092
6098
  ERR_UNSUPPORTED_ENCRYPTION: ERR_UNSUPPORTED_ENCRYPTION,
6099
+ ERR_UNSUPPORTED_UINT64: ERR_UNSUPPORTED_UINT64,
6093
6100
  ERR_WORKER_STARTUP_TIMEOUT: ERR_WORKER_STARTUP_TIMEOUT,
6094
6101
  ZipReader: ZipReader,
6095
6102
  ZipReaderStream: ZipReaderStream
@@ -6230,7 +6237,7 @@ class ZipWriter {
6230
6237
  encrypted,
6231
6238
  uncompressedSize,
6232
6239
  compressedSize,
6233
- zip64
6240
+ extraFieldZip64
6234
6241
  } = entry;
6235
6242
  let {
6236
6243
  compressionMethod,
@@ -6247,13 +6254,13 @@ class ZipWriter {
6247
6254
  rawExtraFieldExtendedTimestamp = rawExtraFieldExtendedTimestamp || EMPTY_UINT8_ARRAY;
6248
6255
  rawExtraFieldNTFS = rawExtraFieldNTFS || EMPTY_UINT8_ARRAY;
6249
6256
  rawExtraFieldUnix = rawExtraFieldUnix || EMPTY_UINT8_ARRAY;
6250
- rawExtraField = rawExtraField || EMPTY_UINT8_ARRAY;
6257
+ rawExtraField = removeExtraFieldZip64(rawExtraField || EMPTY_UINT8_ARRAY);
6251
6258
  if (entry.extraFieldAES) {
6252
6259
  compressionMethod = COMPRESSION_METHOD_AES;
6253
6260
  }
6254
6261
  const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraFieldUnix, rawExtraField);
6255
- const zip64UncompressedSize = zip64 && uncompressedSize >= MAX_32_BITS;
6256
- const zip64CompressedSize = zip64 && compressedSize >= MAX_32_BITS;
6262
+ const zip64UncompressedSize = Boolean(extraFieldZip64) && extraFieldZip64.uncompressedSize !== UNDEFINED_VALUE;
6263
+ const zip64CompressedSize = Boolean(extraFieldZip64) && extraFieldZip64.compressedSize !== UNDEFINED_VALUE;
6257
6264
  const bitFlagValue = (getBitFlag(level, languageEncodingFlag, dataDescriptor, encrypted, compressionMethod) & ~BITFLAG_LEVEL) | (level << 1);
6258
6265
  const {
6259
6266
  headerArray,
@@ -6809,6 +6816,10 @@ function resolveEntrySizes(zipWriter, hasContent, contentSize, metadata, options
6809
6816
  maximumCompressedSize = contentSize === UNDEFINED_VALUE ? getMaximumCompressedSize(uncompressedSize) + encryptionOverhead : contentSize;
6810
6817
  }
6811
6818
  }
6819
+ const emptyEntry = !encryptedEntry && (!hasContent || (contentSize === 0 && !passThrough)) && !isCompressed(compressionMethod, level);
6820
+ if (emptyEntry && !zipCrypto && getOptionValue(zipWriter, options, OPTION_DATA_DESCRIPTOR) === UNDEFINED_VALUE) {
6821
+ dataDescriptor = false;
6822
+ }
6812
6823
  const zip64UncompressedSize = zip64Enabled || uncompressedSize >= MAX_32_BITS;
6813
6824
  const zip64CompressedSize = zip64Enabled || maximumCompressedSize >= MAX_32_BITS;
6814
6825
  if (zip64UncompressedSize || zip64CompressedSize) {
@@ -6823,6 +6834,7 @@ function resolveEntrySizes(zipWriter, hasContent, contentSize, metadata, options
6823
6834
  maximumCompressedSize,
6824
6835
  resolvedOptions: {
6825
6836
  dataDescriptor,
6837
+ emptyEntry,
6826
6838
  zip64,
6827
6839
  zip64UncompressedSize,
6828
6840
  zip64CompressedSize,
@@ -6921,6 +6933,7 @@ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
6921
6933
  const {
6922
6934
  keepOrder,
6923
6935
  dataDescriptor,
6936
+ emptyEntry,
6924
6937
  signal
6925
6938
  } = options;
6926
6939
  const {
@@ -6945,7 +6958,7 @@ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
6945
6958
  lockPreviousFileEntry = previousFileEntry && previousFileEntry.lockFileEntry;
6946
6959
  requestLockCurrentFileEntry();
6947
6960
  }
6948
- if (options.bufferedWrite || !keepOrder || zipWriter.writerLocked || zipWriter.bufferedWrites || !dataDescriptor) {
6961
+ if (options.bufferedWrite || !keepOrder || zipWriter.writerLocked || zipWriter.bufferedWrites || (!dataDescriptor && !emptyEntry)) {
6949
6962
  bufferedWrite = true;
6950
6963
  zipWriter.bufferedWrites++;
6951
6964
  if (options.createTempStream) {
@@ -7367,8 +7380,8 @@ function getHeaderInfo(options) {
7367
7380
  try {
7368
7381
  const { uid, gid, unixExtraFieldType } = options;
7369
7382
  if (unixExtraFieldType == INFOZIP_EXTRA_FIELD_TYPE$1 && (uid !== UNDEFINED_VALUE || gid !== UNDEFINED_VALUE)) {
7370
- const uidBytes = packUnixId(uid);
7371
- const gidBytes = packUnixId(gid);
7383
+ const uidBytes = packUnixId(uid === UNDEFINED_VALUE ? 0 : uid);
7384
+ const gidBytes = packUnixId(gid === UNDEFINED_VALUE ? 0 : gid);
7372
7385
  const payloadLength = 3 + uidBytes.length + gidBytes.length;
7373
7386
  const extraFieldUnix = createRecordWriter(4 + payloadLength);
7374
7387
  extraFieldUnix.writeUint16(EXTRAFIELD_TYPE_INFOZIP);
@@ -7499,18 +7512,14 @@ function appendExtraFieldUSDZ(entryInfo, zipWriterOffset) {
7499
7512
  }
7500
7513
 
7501
7514
  function packUnixId(id) {
7502
- if (id === UNDEFINED_VALUE) {
7503
- return EMPTY_UINT8_ARRAY;
7504
- } else {
7505
- const dataArray = new Uint8Array(4);
7506
- const dataView = getDataView(dataArray);
7507
- dataView.setUint32(0, id, true);
7508
- let length = 4;
7509
- while (length > 1 && dataArray[length - 1] === 0) {
7510
- length--;
7511
- }
7512
- return dataArray.subarray(0, length);
7515
+ const dataArray = new Uint8Array(4);
7516
+ const dataView = getDataView(dataArray);
7517
+ dataView.setUint32(0, id, true);
7518
+ let length = 4;
7519
+ while (length > 1 && dataArray[length - 1] === 0) {
7520
+ length--;
7513
7521
  }
7522
+ return dataArray.subarray(0, length);
7514
7523
  }
7515
7524
 
7516
7525
  function normalizeMsdosAttributes(msdosAttributesRaw, msdosAttributes) {
@@ -7929,6 +7938,21 @@ async function startsWithSplitZipSignature(reader) {
7929
7938
  return getUint32(getDataView(signatureArray), 0) == SPLIT_ZIP_FILE_SIGNATURE;
7930
7939
  }
7931
7940
 
7941
+ function removeExtraFieldZip64(rawExtraField) {
7942
+ const rawExtraFieldView = getDataView(rawExtraField);
7943
+ let offsetExtraField = 0;
7944
+ while (offsetExtraField + 4 <= getLength(rawExtraField)) {
7945
+ const size = 4 + getUint16(rawExtraFieldView, offsetExtraField + 2);
7946
+ if (getUint16(rawExtraFieldView, offsetExtraField) == EXTRAFIELD_TYPE_ZIP64) {
7947
+ return removeExtraFieldZip64(concat(
7948
+ rawExtraField.subarray(0, offsetExtraField),
7949
+ rawExtraField.subarray(Math.min(offsetExtraField + size, getLength(rawExtraField)))));
7950
+ }
7951
+ offsetExtraField += size;
7952
+ }
7953
+ return rawExtraField;
7954
+ }
7955
+
7932
7956
  async function copyZipData(zipWriter, reader, entries, directoryOffset) {
7933
7957
  const { writer } = zipWriter;
7934
7958
  const entryPositions = new Map();
@@ -9861,7 +9885,7 @@ function keepsContentType(writer, data) {
9861
9885
  } else if (writer.constructor == BlobWriter) {
9862
9886
  return data.type == contentType;
9863
9887
  } else if (writer.constructor == Data64URIWriter) {
9864
- return data.startsWith(writer.data);
9888
+ return data.startsWith("data:" + (contentType || "") + ";base64,");
9865
9889
  } else {
9866
9890
  return true;
9867
9891
  }
@@ -10534,6 +10558,7 @@ exports.ERR_UNSUPPORTED_ENCRYPTION = ERR_UNSUPPORTED_ENCRYPTION;
10534
10558
  exports.ERR_UNSUPPORTED_ENCRYPTION_PASS_THROUGH = ERR_UNSUPPORTED_ENCRYPTION_PASS_THROUGH;
10535
10559
  exports.ERR_UNSUPPORTED_ENCRYPTION_USDZ = ERR_UNSUPPORTED_ENCRYPTION_USDZ;
10536
10560
  exports.ERR_UNSUPPORTED_FORMAT = ERR_UNSUPPORTED_FORMAT;
10561
+ exports.ERR_UNSUPPORTED_UINT64 = ERR_UNSUPPORTED_UINT64;
10537
10562
  exports.ERR_WORKER_STARTUP_TIMEOUT = ERR_WORKER_STARTUP_TIMEOUT;
10538
10563
  exports.ERR_WRITER_NOT_INITIALIZED = ERR_WRITER_NOT_INITIALIZED;
10539
10564
  exports.ERR_ZIP_NOT_EMPTY = ERR_ZIP_NOT_EMPTY;
package/index.d.cts CHANGED
@@ -2952,7 +2952,9 @@ export interface ZipWriterConstructorOptions extends WorkerConfiguration {
2952
2952
  *
2953
2953
  * When set to `false`, the {@link ZipWriterConstructorOptions#bufferedWrite} option will automatically be
2954
2954
  * set to `true`. It will be automatically set to `false` when it is `undefined` and the
2955
- * {@link ZipWriterConstructorOptions#bufferedWrite} option is set to `true`, or when the
2955
+ * {@link ZipWriterConstructorOptions#bufferedWrite} option is set to `true`, or when the entry is a folder
2956
+ * or an empty entry stored without compression or encryption, since the header can then carry the sizes and
2957
+ * the CRC-32 directly. It will be automatically set to `true` when the
2956
2958
  * {@link ZipWriterConstructorOptions#zipCrypto} option is set to `true`. Otherwise, the default value is `true`.
2957
2959
  */
2958
2960
  dataDescriptor?: boolean;
@@ -3029,6 +3031,9 @@ export interface ZipWriterConstructorOptions extends WorkerConfiguration {
3029
3031
  * - "unix": Info-ZIP Unix extra field type 2 (0x7855), storing fixed 2-byte uid/gid (0..65535); a
3030
3032
  * larger uid or gid is rejected. The Unix mode is not part of this field; it is written to the
3031
3033
  * external file attributes.
3034
+ *
3035
+ * When {@link ZipFS} exports imported entries, their uid/gid are re-emitted as "infozip" regardless
3036
+ * of the field type found in the imported zip file, unless this option is set explicitly.
3032
3037
  */
3033
3038
  unixExtraFieldType?: "infozip" | "unix";
3034
3039
  /**
@@ -4320,6 +4325,13 @@ export const ERR_INVALID_FILENAME_VALIDATION: string;
4320
4325
  * Invalid maxAppendedDataSize error (thrown when the `maxAppendedDataSize` option is not a number greater than or equal to 0)
4321
4326
  */
4322
4327
  export const ERR_INVALID_MAX_APPENDED_DATA_SIZE: string;
4328
+ /**
4329
+ * Unsupported 64-bit value error
4330
+ *
4331
+ * @remarks Thrown when a 64-bit size, offset, or entry count read from a zip file exceeds `Number.MAX_SAFE_INTEGER`,
4332
+ * instead of processing the value with a loss of precision.
4333
+ */
4334
+ export const ERR_UNSUPPORTED_UINT64: string;
4323
4335
  /**
4324
4336
  * Iteration completed too soon error
4325
4337
  */
package/index.d.ts CHANGED
@@ -2952,7 +2952,9 @@ export interface ZipWriterConstructorOptions extends WorkerConfiguration {
2952
2952
  *
2953
2953
  * When set to `false`, the {@link ZipWriterConstructorOptions#bufferedWrite} option will automatically be
2954
2954
  * set to `true`. It will be automatically set to `false` when it is `undefined` and the
2955
- * {@link ZipWriterConstructorOptions#bufferedWrite} option is set to `true`, or when the
2955
+ * {@link ZipWriterConstructorOptions#bufferedWrite} option is set to `true`, or when the entry is a folder
2956
+ * or an empty entry stored without compression or encryption, since the header can then carry the sizes and
2957
+ * the CRC-32 directly. It will be automatically set to `true` when the
2956
2958
  * {@link ZipWriterConstructorOptions#zipCrypto} option is set to `true`. Otherwise, the default value is `true`.
2957
2959
  */
2958
2960
  dataDescriptor?: boolean;
@@ -3029,6 +3031,9 @@ export interface ZipWriterConstructorOptions extends WorkerConfiguration {
3029
3031
  * - "unix": Info-ZIP Unix extra field type 2 (0x7855), storing fixed 2-byte uid/gid (0..65535); a
3030
3032
  * larger uid or gid is rejected. The Unix mode is not part of this field; it is written to the
3031
3033
  * external file attributes.
3034
+ *
3035
+ * When {@link ZipFS} exports imported entries, their uid/gid are re-emitted as "infozip" regardless
3036
+ * of the field type found in the imported zip file, unless this option is set explicitly.
3032
3037
  */
3033
3038
  unixExtraFieldType?: "infozip" | "unix";
3034
3039
  /**
@@ -4320,6 +4325,13 @@ export const ERR_INVALID_FILENAME_VALIDATION: string;
4320
4325
  * Invalid maxAppendedDataSize error (thrown when the `maxAppendedDataSize` option is not a number greater than or equal to 0)
4321
4326
  */
4322
4327
  export const ERR_INVALID_MAX_APPENDED_DATA_SIZE: string;
4328
+ /**
4329
+ * Unsupported 64-bit value error
4330
+ *
4331
+ * @remarks Thrown when a 64-bit size, offset, or entry count read from a zip file exceeds `Number.MAX_SAFE_INTEGER`,
4332
+ * instead of processing the value with a loss of precision.
4333
+ */
4334
+ export const ERR_UNSUPPORTED_UINT64: string;
4323
4335
  /**
4324
4336
  * Iteration completed too soon error
4325
4337
  */