@zip.js/zip.js 2.8.46 → 2.8.47

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.
Files changed (46) hide show
  1. package/deno.json +1 -1
  2. package/dist/zip-core-external.js +85 -9
  3. package/dist/zip-core-external.min.js +1 -1
  4. package/dist/zip-core.js +95 -8
  5. package/dist/zip-core.min.js +1 -1
  6. package/dist/zip-fs-core-external.js +85 -9
  7. package/dist/zip-fs-core-external.min.js +1 -1
  8. package/dist/zip-fs-core.js +86 -8
  9. package/dist/zip-fs-core.min.js +1 -1
  10. package/dist/zip-fs-external.js +85 -9
  11. package/dist/zip-fs-external.min.js +1 -1
  12. package/dist/zip-fs-native.js +99 -10
  13. package/dist/zip-fs-native.min.js +1 -1
  14. package/dist/zip-fs.js +98 -9
  15. package/dist/zip-fs.min.js +1 -1
  16. package/dist/zip-legacy.js +97 -10
  17. package/dist/zip-legacy.min.js +1 -1
  18. package/dist/zip-native.js +97 -10
  19. package/dist/zip-native.min.js +1 -1
  20. package/dist/zip-web-worker-native.js +1 -1
  21. package/dist/zip-web-worker.js +1 -1
  22. package/dist/zip.js +96 -9
  23. package/dist/zip.min.js +1 -1
  24. package/index-native.cjs +99 -10
  25. package/index-native.min.js +1 -1
  26. package/index.cjs +98 -9
  27. package/index.d.cts +141 -0
  28. package/index.d.ts +141 -0
  29. package/index.min.js +1 -1
  30. package/lib/core/codec-pool.js +6 -3
  31. package/lib/core/codec-worker.js +2 -1
  32. package/lib/core/constants.js +2 -0
  33. package/lib/core/options.js +4 -0
  34. package/lib/core/streams/codec-stream.js +2 -0
  35. package/lib/core/streams/common-crypto.js +2 -1
  36. package/lib/core/streams/zip-entry-stream.js +4 -2
  37. package/lib/core/util/sync-access-handle-temp-stream.js +2 -1
  38. package/lib/core/web-worker-inline-native.js +1 -1
  39. package/lib/core/web-worker-inline-wasm.js +1 -1
  40. package/lib/core/zip-fs.js +1 -1
  41. package/lib/core/zip-reader.js +58 -5
  42. package/lib/core/zip-writer.js +43 -8
  43. package/lib/zip-core-base.js +2 -1
  44. package/lib/zip-core-reader.js +2 -1
  45. package/lib/zip-core-writer.js +10 -1
  46. package/package.json +1 -1
package/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zip-js/zip-js",
3
- "version": "2.8.46",
3
+ "version": "2.8.47",
4
4
  "exports": {
5
5
  ".": "./index.js",
6
6
  "./external": "./lib/zip-fs-external.js",
@@ -38,6 +38,7 @@ const LOCAL_FILE_HEADER_SIGNATURE = 0x04034b50;
38
38
  const SPLIT_ZIP_FILE_SIGNATURE = 0x08074b50;
39
39
  const DATA_DESCRIPTOR_RECORD_SIGNATURE = SPLIT_ZIP_FILE_SIGNATURE;
40
40
  const ARCHIVE_EXTRA_DATA_SIGNATURE = 0x08064b50;
41
+ const DIGITAL_SIGNATURE_RECORD_SIGNATURE = 0x05054b50;
41
42
  const CENTRAL_FILE_HEADER_SIGNATURE = 0x02014b50;
42
43
  const END_OF_CENTRAL_DIR_SIGNATURE = 0x06054b50;
43
44
  const ZIP64_END_OF_CENTRAL_DIR_SIGNATURE = 0x06064b50;
@@ -4485,6 +4486,8 @@ const OPTION_UNIX_EXTRA_FIELD_TYPE = "unixExtraFieldType";
4485
4486
  const OPTION_LOCAL_EXTRA_FIELD = "localExtraField";
4486
4487
  const OPTION_STRICTNESS = "strictness";
4487
4488
  const OPTION_MAX_APPENDED_DATA_SIZE = "maxAppendedDataSize";
4489
+ const OPTION_DECRYPT_CENTRAL_DIRECTORY = "decryptCentralDirectory";
4490
+ const OPTION_SIGN_CENTRAL_DIRECTORY = "signCentralDirectory";
4488
4491
  const STRICTNESS_STRICT = "strict";
4489
4492
  const STRICTNESS_BALANCED = "balanced";
4490
4493
  const STRICTNESS_TOLERANT = "tolerant";
@@ -4610,6 +4613,7 @@ class ZipReader {
4610
4613
  let startOffset;
4611
4614
  let zip64EndOfDirectory;
4612
4615
  let zip64EndOfDirectoryVersion2;
4616
+ let directoryEncryptionInfo;
4613
4617
  const requiresZip64 = directoryDataOffset == MAX_32_BITS || directoryDataLength == MAX_32_BITS || filesLength == MAX_16_BITS || diskNumber == MAX_16_BITS;
4614
4618
  if (directoryDataOffset != MAX_32_BITS && diskNumber != MAX_16_BITS) {
4615
4619
  directoryDataOffset += getDiskOffset$1(reader, diskNumber);
@@ -4640,6 +4644,15 @@ class ZipReader {
4640
4644
  }
4641
4645
  zip64EndOfDirectory = true;
4642
4646
  zip64EndOfDirectoryVersion2 = getBigUint64(endOfDirectoryView, 4) > ZIP64_END_OF_CENTRAL_DIR_LENGTH - 12;
4647
+ if (zip64EndOfDirectoryVersion2) {
4648
+ const extensibleDataLength = Math.min(
4649
+ Number(getBigUint64(endOfDirectoryView, 4)) - (ZIP64_END_OF_CENTRAL_DIR_LENGTH - 12),
4650
+ reader.size - directoryDataOffset - ZIP64_END_OF_CENTRAL_DIR_LENGTH);
4651
+ if (extensibleDataLength > 0) {
4652
+ const rawExtensibleData = await readUint8Array(reader, directoryDataOffset + ZIP64_END_OF_CENTRAL_DIR_LENGTH, extensibleDataLength);
4653
+ directoryEncryptionInfo = getDirectoryEncryptionInfo(rawExtensibleData);
4654
+ }
4655
+ }
4643
4656
  if (lastDiskNumber == MAX_16_BITS) {
4644
4657
  lastDiskNumber = getUint32(endOfDirectoryView, 16);
4645
4658
  } else if (checkAmbiguity && lastDiskNumber != getUint32(endOfDirectoryView, 16)) {
@@ -4663,7 +4676,7 @@ class ZipReader {
4663
4676
  directoryDataOffset = getDiskOffset$1(reader, diskNumber) + getBigUint64(endOfDirectoryView, 48) + prependedDataLength;
4664
4677
  }
4665
4678
  }
4666
- const declaredDirectoryDataLength = directoryDataLength;
4679
+ let declaredDirectoryDataLength = directoryDataLength;
4667
4680
  const centralDirectoryEndOffset = endOfDirectoryInfo.offset -
4668
4681
  (zip64EndOfDirectory ? ZIP64_END_OF_CENTRAL_DIR_LENGTH + ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH : 0);
4669
4682
  if (directoryDataOffset >= reader.size) {
@@ -4711,6 +4724,18 @@ class ZipReader {
4711
4724
  if (directoryDataOffset < 0 || directoryDataOffset >= reader.size) {
4712
4725
  throw new Error(ERR_BAD_FORMAT);
4713
4726
  }
4727
+ zipReader.directoryOffset = directoryDataOffset;
4728
+ zipReader.directoryLength = declaredDirectoryDataLength;
4729
+ const decryptCentralDirectory = getOptionValue$1(zipReader, options, OPTION_DECRYPT_CENTRAL_DIRECTORY);
4730
+ let decryptedDirectory;
4731
+ if (decryptCentralDirectory && filesLength && directoryArray.length >= 4 &&
4732
+ getUint32(directoryView, 0) != CENTRAL_FILE_HEADER_SIGNATURE &&
4733
+ (zip64EndOfDirectoryVersion2 || detectEncryptedCentralDirectory(directoryView))) {
4734
+ directoryArray = await decryptCentralDirectory(directoryArray, directoryEncryptionInfo);
4735
+ directoryView = getDataView(directoryArray);
4736
+ declaredDirectoryDataLength = directoryArray.length;
4737
+ decryptedDirectory = true;
4738
+ }
4714
4739
  startOffset = directoryDataOffset;
4715
4740
  const filenameEncoding = getOptionValue$1(zipReader, options, OPTION_FILENAME_ENCODING);
4716
4741
  const commentEncoding = getOptionValue$1(zipReader, options, OPTION_COMMENT_ENCODING);
@@ -4719,7 +4744,7 @@ class ZipReader {
4719
4744
  for (let indexFile = 0; indexFile < filesLength; indexFile++) {
4720
4745
  const fileEntry = new ZipEntry(reader, config, zipReader.options);
4721
4746
  if (offset + CENTRAL_FILE_HEADER_LENGTH > directoryArray.length || getUint32(directoryView, offset) != CENTRAL_FILE_HEADER_SIGNATURE) {
4722
- if (indexFile == 0 && (zip64EndOfDirectoryVersion2 || detectEncryptedCentralDirectory(directoryView))) {
4747
+ if (indexFile == 0 && !decryptedDirectory && (zip64EndOfDirectoryVersion2 || detectEncryptedCentralDirectory(directoryView))) {
4723
4748
  throw new Error(ERR_ENCRYPTED_CENTRAL_DIRECTORY);
4724
4749
  }
4725
4750
  throw new Error(ERR_CENTRAL_DIRECTORY_NOT_FOUND);
@@ -4832,7 +4857,15 @@ class ZipReader {
4832
4857
  }
4833
4858
  yield entry;
4834
4859
  }
4835
- if (checkAmbiguity && offset != declaredDirectoryDataLength) {
4860
+ let offsetAfterSignature = offset;
4861
+ if (offset + 6 <= directoryArray.length && getUint32(directoryView, offset) == DIGITAL_SIGNATURE_RECORD_SIGNATURE) {
4862
+ const signatureDataLength = getUint16(directoryView, offset + 4);
4863
+ if (offset + 6 + signatureDataLength <= directoryArray.length) {
4864
+ zipReader.digitalSignature = directoryArray.subarray(offset + 6, offset + 6 + signatureDataLength);
4865
+ offsetAfterSignature = offset + 6 + signatureDataLength;
4866
+ }
4867
+ }
4868
+ if (checkAmbiguity && offset != declaredDirectoryDataLength && offsetAfterSignature != declaredDirectoryDataLength) {
4836
4869
  throwAmbiguousArchive("trailing central directory data");
4837
4870
  }
4838
4871
  if (duplicateFilename) {
@@ -5104,6 +5137,25 @@ function detectEncryptedCentralDirectory(directoryView) {
5104
5137
  return false;
5105
5138
  }
5106
5139
 
5140
+ function getDirectoryEncryptionInfo(rawExtensibleData) {
5141
+ const directoryEncryptionInfo = { rawExtensibleData };
5142
+ if (rawExtensibleData.length >= 28) {
5143
+ const extensibleDataView = getDataView(rawExtensibleData);
5144
+ const hashDataLength = getUint16(extensibleDataView, 26);
5145
+ Object.assign(directoryEncryptionInfo, {
5146
+ compressionMethod: getUint16(extensibleDataView, 0),
5147
+ compressedSize: Number(getBigUint64(extensibleDataView, 2)),
5148
+ uncompressedSize: Number(getBigUint64(extensibleDataView, 10)),
5149
+ encryptionAlgorithm: getUint16(extensibleDataView, 18),
5150
+ bitLength: getUint16(extensibleDataView, 20),
5151
+ flags: getUint16(extensibleDataView, 22),
5152
+ hashAlgorithm: getUint16(extensibleDataView, 24),
5153
+ hashData: rawExtensibleData.subarray(28, 28 + hashDataLength)
5154
+ });
5155
+ }
5156
+ return directoryEncryptionInfo;
5157
+ }
5158
+
5107
5159
  function readCommonHeader(directory, dataView, offset) {
5108
5160
  const rawBitFlag = directory.rawBitFlag = getUint16(dataView, offset + 2);
5109
5161
  const encrypted = (rawBitFlag & BITFLAG_ENCRYPTED) == BITFLAG_ENCRYPTED;
@@ -5655,6 +5707,7 @@ var zipReader = /*#__PURE__*/Object.freeze({
5655
5707
  ERR_SPLIT_ZIP_FILE: ERR_SPLIT_ZIP_FILE,
5656
5708
  ERR_UNSUPPORTED_COMPRESSION: ERR_UNSUPPORTED_COMPRESSION$1,
5657
5709
  ERR_UNSUPPORTED_ENCRYPTION: ERR_UNSUPPORTED_ENCRYPTION,
5710
+ ERR_WORKER_STARTUP_TIMEOUT: ERR_WORKER_STARTUP_TIMEOUT,
5658
5711
  ZipReader: ZipReader,
5659
5712
  ZipReaderStream: ZipReaderStream
5660
5713
  });
@@ -5713,6 +5766,7 @@ const ERR_INVALID_UNIX_EXTRA_FIELD_TYPE = "Invalid unixExtraFieldType (must be '
5713
5766
  const ERR_INVALID_UNIX_ID_SIZE = "uid/gid must be 0..65535 for unixExtraFieldType 'unix' (use 'infozip' for larger ids)";
5714
5767
  const ERR_INVALID_MSDOS_ATTRIBUTES = "Invalid msdosAttributesRaw (must be integer 0..255)";
5715
5768
  const ERR_INVALID_MSDOS_DATA = "Invalid msdosAttributes (must be an object with boolean flags)";
5769
+ const ERR_INVALID_SIGNATURE_DATA = "Signature data exceeds 64KB";
5716
5770
 
5717
5771
  const EXTRAFIELD_DATA_AES = new Uint8Array([0x07, 0x00, 0x02, 0x00, 0x41, 0x45, 0x03, 0x00, 0x00]);
5718
5772
  const EXTRAFIELD_OFFSET_AES_VENDOR_VERSION = 4;
@@ -7072,8 +7126,9 @@ function updateLocalHeader({
7072
7126
 
7073
7127
  async function closeFile(zipWriter, comment, options) {
7074
7128
  const directoryDataLength = createDirectoryRecords(zipWriter.files);
7075
- const directoryStart = await writeDirectoryRecords(zipWriter, directoryDataLength, options);
7076
- await writeEndOfDirectoryRecord(zipWriter, comment, options, { directoryStart, directoryDataLength });
7129
+ const { directoryStart, directoryArray } = await writeDirectoryRecords(zipWriter, directoryDataLength, options);
7130
+ const signatureLength = await writeDigitalSignatureRecord(zipWriter, directoryArray, options);
7131
+ await writeEndOfDirectoryRecord(zipWriter, comment, options, { directoryStart, directoryDataLength, signatureLength });
7077
7132
  }
7078
7133
 
7079
7134
  function createDirectoryRecords(files) {
@@ -7230,12 +7285,33 @@ async function writeDirectoryRecords(zipWriter, directoryDataLength, options) {
7230
7285
  }
7231
7286
  }
7232
7287
  await writeData(writer, directoryDiskOffset ? directoryArray.slice(directoryDiskOffset) : directoryArray);
7233
- return { diskNumber: directoryStartDiskNumber, diskOffset: directoryStartDiskOffset };
7288
+ return {
7289
+ directoryStart: { diskNumber: directoryStartDiskNumber, diskOffset: directoryStartDiskOffset },
7290
+ directoryArray
7291
+ };
7292
+ }
7293
+
7294
+ async function writeDigitalSignatureRecord(zipWriter, directoryArray, options) {
7295
+ const signCentralDirectory = getOptionValue(zipWriter, options, OPTION_SIGN_CENTRAL_DIRECTORY);
7296
+ if (signCentralDirectory) {
7297
+ const signatureData = await signCentralDirectory(directoryArray);
7298
+ const signatureDataLength = getLength(signatureData);
7299
+ if (signatureDataLength > MAX_16_BITS) {
7300
+ throw new Error(ERR_INVALID_SIGNATURE_DATA);
7301
+ }
7302
+ const signatureRecord = createRecordWriter(6 + signatureDataLength);
7303
+ signatureRecord.uint32(DIGITAL_SIGNATURE_RECORD_SIGNATURE);
7304
+ signatureRecord.uint16(signatureDataLength);
7305
+ signatureRecord.bytes(signatureData);
7306
+ await writeData(zipWriter.writer, signatureRecord.array);
7307
+ return 6 + signatureDataLength;
7308
+ }
7309
+ return 0;
7234
7310
  }
7235
7311
 
7236
7312
  async function writeEndOfDirectoryRecord(zipWriter, comment, options, cdInfo) {
7237
7313
  const { writer } = zipWriter;
7238
- const { directoryStart } = cdInfo;
7314
+ const { directoryStart, signatureLength } = cdInfo;
7239
7315
  let { directoryDataLength } = cdInfo;
7240
7316
  let filesLength = zipWriter.files.size;
7241
7317
  let diskNumber = directoryStart.diskNumber;
@@ -7274,7 +7350,7 @@ async function writeEndOfDirectoryRecord(zipWriter, comment, options, cdInfo) {
7274
7350
  endOfdirectoryRecord.uint64(directoryOffset);
7275
7351
  endOfdirectoryRecord.uint32(ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE);
7276
7352
  endOfdirectoryRecord.uint32(lastDiskNumber);
7277
- endOfdirectoryRecord.uint64(BigInt(getSegmentOffset(zipWriter, writer)) + BigInt(directoryDataLength));
7353
+ endOfdirectoryRecord.uint64(BigInt(getSegmentOffset(zipWriter, writer)) + BigInt(directoryDataLength) + BigInt(signatureLength));
7278
7354
  endOfdirectoryRecord.uint32(lastDiskNumber + 1);
7279
7355
  const supportZip64SplitFile = getOptionValue(zipWriter, options, OPTION_SUPPORT_ZIP64_SPLIT_FILE, true);
7280
7356
  if (supportZip64SplitFile) {
@@ -8328,4 +8404,4 @@ function terminateWorkersAndModule() {
8328
8404
 
8329
8405
  configureExternalAssets();
8330
8406
 
8331
- export { BlobReader, BlobWriter, Data64URIReader, Data64URIWriter, ERR_AMBIGUOUS_ARCHIVE, ERR_BAD_FORMAT, ERR_CENTRAL_DIRECTORY_NOT_FOUND, ERR_DUPLICATED_NAME, ERR_ENCRYPTED, ERR_ENCRYPTED_CENTRAL_DIRECTORY, ERR_EOCDR_LOCATOR_ZIP64_NOT_FOUND, ERR_EOCDR_NOT_FOUND, ERR_EXTRAFIELD_ZIP64_NOT_FOUND, ERR_HTTP_RANGE, ERR_HTTP_RESOURCE_CHANGED, ERR_INVALID_AUTHENTICATION_CODE, ERR_INVALID_CODEC_DEFINITION, ERR_INVALID_CODEC_MODULE, ERR_INVALID_COMMENT, ERR_INVALID_COMPRESSED_DATA, ERR_INVALID_CRC32, ERR_INVALID_ENCRYPTION_STRENGTH, ERR_INVALID_ENTRY_COMMENT, ERR_INVALID_ENTRY_NAME, ERR_INVALID_EXTRAFIELD_DATA, ERR_INVALID_EXTRAFIELD_TYPE, ERR_INVALID_PASSWORD, ERR_INVALID_SIGNATURE, ERR_INVALID_UNCOMPRESSED_SIZE, ERR_INVALID_VERSION, ERR_ITERATOR_COMPLETED_TOO_SOON, ERR_LOCAL_FILE_HEADER_NOT_FOUND, ERR_OVERLAPPING_ENTRY, ERR_RESERVED_COMPRESSION_METHOD, ERR_SPLIT_ZIP_FILE, ERR_UNDEFINED_READER, ERR_UNDEFINED_UNCOMPRESSED_SIZE, ERR_UNSUPPORTED_COMPRESSION$1 as ERR_UNSUPPORTED_COMPRESSION, ERR_UNSUPPORTED_ENCRYPTION, ERR_UNSUPPORTED_ENCRYPTION_USDZ, ERR_UNSUPPORTED_FORMAT, ERR_WRITER_NOT_INITIALIZED, ERR_ZIP_NOT_EMPTY, HttpRangeReader, HttpReader, Reader, SplitDataReader, SplitDataWriter, TextReader, TextWriter, Uint8ArrayReader, Uint8ArrayWriter, Writer, ZipReader, ZipReaderStream, ZipWriter, ZipWriterStream, configure, createBlobTempStream, createOPFSTempStream, createSyncAccessHandleTempStream, getMimeType, registerCodec, resetConfiguration, terminateWorkersAndModule as terminateWorkers, unregisterCodec };
8407
+ export { BlobReader, BlobWriter, Data64URIReader, Data64URIWriter, ERR_AMBIGUOUS_ARCHIVE, ERR_BAD_FORMAT, ERR_CENTRAL_DIRECTORY_NOT_FOUND, ERR_DUPLICATED_NAME, ERR_ENCRYPTED, ERR_ENCRYPTED_CENTRAL_DIRECTORY, ERR_EOCDR_LOCATOR_ZIP64_NOT_FOUND, ERR_EOCDR_NOT_FOUND, ERR_EXTRAFIELD_ZIP64_NOT_FOUND, ERR_HTTP_RANGE, ERR_HTTP_RESOURCE_CHANGED, ERR_INVALID_AUTHENTICATION_CODE, ERR_INVALID_CODEC_DEFINITION, ERR_INVALID_CODEC_MODULE, ERR_INVALID_COMMENT, ERR_INVALID_COMPRESSED_DATA, ERR_INVALID_CRC32, ERR_INVALID_ENCRYPTION_STRENGTH, ERR_INVALID_ENTRY_COMMENT, ERR_INVALID_ENTRY_NAME, ERR_INVALID_EXTRAFIELD_DATA, ERR_INVALID_EXTRAFIELD_TYPE, ERR_INVALID_GID, ERR_INVALID_MSDOS_ATTRIBUTES, ERR_INVALID_MSDOS_DATA, ERR_INVALID_PASSWORD, ERR_INVALID_SIGNATURE, ERR_INVALID_SIGNATURE_DATA, ERR_INVALID_UID, ERR_INVALID_UNCOMPRESSED_SIZE, ERR_INVALID_UNIX_EXTRA_FIELD_TYPE, ERR_INVALID_UNIX_ID_SIZE, ERR_INVALID_UNIX_MODE, ERR_INVALID_VERSION, ERR_ITERATOR_COMPLETED_TOO_SOON, ERR_LOCAL_FILE_HEADER_NOT_FOUND, ERR_OVERLAPPING_ENTRY, ERR_RESERVED_COMPRESSION_METHOD, ERR_SPLIT_ZIP_FILE, ERR_UNDEFINED_READER, ERR_UNDEFINED_UNCOMPRESSED_SIZE, ERR_UNSUPPORTED_COMPRESSION$1 as ERR_UNSUPPORTED_COMPRESSION, ERR_UNSUPPORTED_CONTEXT, ERR_UNSUPPORTED_CRYPTO_API, ERR_UNSUPPORTED_ENCRYPTION, ERR_UNSUPPORTED_ENCRYPTION_USDZ, ERR_UNSUPPORTED_FORMAT, ERR_WORKER_STARTUP_TIMEOUT, ERR_WRITER_NOT_INITIALIZED, ERR_ZIP_NOT_EMPTY, HttpRangeReader, HttpReader, Reader, SplitDataReader, SplitDataWriter, TextReader, TextWriter, Uint8ArrayReader, Uint8ArrayWriter, Writer, ZipReader, ZipReaderStream, ZipWriter, ZipWriterStream, configure, createBlobTempStream, createOPFSTempStream, createSyncAccessHandleTempStream, getMimeType, registerCodec, resetConfiguration, terminateWorkersAndModule as terminateWorkers, unregisterCodec };