@zip.js/zip.js 2.8.26 → 2.8.29

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 (53) hide show
  1. package/BENCHMARKS.md +168 -0
  2. package/README.md +2 -2
  3. package/deno.json +1 -1
  4. package/dist/zip-core.js +1113 -574
  5. package/dist/zip-core.min.js +1 -1
  6. package/dist/zip-fs-core.js +1209 -622
  7. package/dist/zip-fs-core.min.js +1 -1
  8. package/dist/zip-fs-native.js +1270 -632
  9. package/dist/zip-fs-native.min.js +1 -1
  10. package/dist/zip-fs.js +1347 -686
  11. package/dist/zip-fs.min.js +1 -1
  12. package/dist/zip-legacy.js +1114 -575
  13. package/dist/zip-legacy.min.js +1 -1
  14. package/dist/zip-module.wasm +0 -0
  15. package/dist/zip-native.js +1116 -577
  16. package/dist/zip-native.min.js +1 -1
  17. package/dist/zip-web-worker-native.js +1 -1
  18. package/dist/zip-web-worker.js +1 -1
  19. package/dist/zip.js +1193 -631
  20. package/dist/zip.min.js +1 -1
  21. package/eslint.config.mjs +6 -1
  22. package/index-native.cjs +1270 -632
  23. package/index-native.min.js +1 -1
  24. package/index.cjs +1347 -686
  25. package/index.d.ts +2451 -2355
  26. package/index.min.js +1 -1
  27. package/lib/core/codec-pool.js +39 -1
  28. package/lib/core/codec-worker.js +130 -35
  29. package/lib/core/configuration.js +3 -0
  30. package/lib/core/constants.js +6 -0
  31. package/lib/core/io.js +55 -25
  32. package/lib/core/options.js +2 -0
  33. package/lib/core/streams/aes-crypto-stream.js +12 -12
  34. package/lib/core/streams/codec-stream.js +11 -5
  35. package/lib/core/streams/codecs/sjcl.js +1 -33
  36. package/lib/core/streams/common-crypto.js +4 -4
  37. package/lib/core/streams/zip-crypto-stream.js +25 -15
  38. package/lib/core/streams/zip-entry-stream.js +89 -5
  39. package/lib/core/streams/zlib-js/zlib-streams.min.js +1 -1
  40. package/lib/core/streams/zlib-wasm/zlib-streams.js +79 -56
  41. package/lib/core/streams/zlib-wasm/zlib-streams.wasm +0 -0
  42. package/lib/core/util/decode-cp437.js +1 -1
  43. package/lib/core/util/decode-text.js +2 -1
  44. package/lib/core/web-worker-base.js +12 -4
  45. package/lib/core/web-worker-inline-native.js +1 -1
  46. package/lib/core/web-worker-inline-wasm.js +1 -1
  47. package/lib/core/zip-fs.js +154 -55
  48. package/lib/core/zip-reader.js +185 -67
  49. package/lib/core/zip-writer.js +613 -376
  50. package/lib/core/zlib-streams-inline.js +1 -1
  51. package/lib/zip-core-reader.js +3 -1
  52. package/lib/zip-core-writer.js +1 -0
  53. package/package.json +20 -6
package/dist/zip-core.js CHANGED
@@ -50,6 +50,7 @@
50
50
  const END_OF_CENTRAL_DIR_SIGNATURE = 0x06054b50;
51
51
  const ZIP64_END_OF_CENTRAL_DIR_SIGNATURE = 0x06064b50;
52
52
  const ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE = 0x07064b50;
53
+ const CENTRAL_FILE_HEADER_LENGTH = 46;
53
54
  const END_OF_CENTRAL_DIR_LENGTH = 22;
54
55
  const ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH = 20;
55
56
  const ZIP64_END_OF_CENTRAL_DIR_LENGTH = 56;
@@ -97,9 +98,11 @@
97
98
  const DIRECTORY_SIGNATURE = "/";
98
99
 
99
100
  const HEADER_SIZE = 30;
101
+ const HEADER_OFFSET_VERSION = 0;
100
102
  const HEADER_OFFSET_SIGNATURE = 10;
101
103
  const HEADER_OFFSET_COMPRESSED_SIZE = 14;
102
104
  const HEADER_OFFSET_UNCOMPRESSED_SIZE = 18;
105
+ const LOCAL_HEADER_COMMON_OFFSET = 4;
103
106
 
104
107
  const MAX_DATE = new Date(2107, 11, 31);
105
108
  const MIN_DATE = new Date(1980, 0, 1);
@@ -154,6 +157,7 @@
154
157
  chunkSize: 64 * 1024,
155
158
  maxWorkers,
156
159
  terminateWorkerTimeout: 5000,
160
+ workerStarvationTimeout: 5000,
157
161
  useWebWorkers: true,
158
162
  useCompressionStream: true,
159
163
  CompressionStream: typeof CompressionStream != UNDEFINED_TYPE && CompressionStream,
@@ -176,6 +180,7 @@
176
180
  chunkSize,
177
181
  maxWorkers,
178
182
  terminateWorkerTimeout,
183
+ workerStarvationTimeout,
179
184
  useCompressionStream,
180
185
  useWebWorkers,
181
186
  CompressionStream,
@@ -191,6 +196,7 @@
191
196
  setIfDefined("chunkSize", chunkSize);
192
197
  setIfDefined("maxWorkers", maxWorkers);
193
198
  setIfDefined("terminateWorkerTimeout", terminateWorkerTimeout);
199
+ setIfDefined("workerStarvationTimeout", workerStarvationTimeout);
194
200
  setIfDefined("useCompressionStream", useCompressionStream);
195
201
  setIfDefined("useWebWorkers", useWebWorkers);
196
202
  setIfDefined("CompressionStream", CompressionStream);
@@ -952,37 +958,6 @@
952
958
  }
953
959
  };
954
960
 
955
- /**
956
- * Random values
957
- * @namespace
958
- */
959
- const random = {
960
- /**
961
- * Generate random words with pure js, cryptographically not as strong & safe as native implementation.
962
- * @param {TypedArray} typedArray The array to fill.
963
- * @return {TypedArray} The random values.
964
- */
965
- getRandomValues(typedArray) {
966
- const words = new Uint32Array(typedArray.buffer);
967
- const r = (m_w) => {
968
- let m_z = 0x3ade68b1;
969
- const mask = 0xffffffff;
970
- return function () {
971
- m_z = (0x9069 * (m_z & 0xFFFF) + (m_z >> 0x10)) & mask;
972
- m_w = (0x4650 * (m_w & 0xFFFF) + (m_w >> 0x10)) & mask;
973
- const result = ((((m_z << 0x10) + m_w) & mask) / 0x100000000) + .5;
974
- return result * (Math.random() > .5 ? 1 : -1);
975
- };
976
- };
977
- for (let i = 0, rcache; i < typedArray.length; i += 4) {
978
- const _r = r((rcache || Math.random()) * 0x100000000);
979
- rcache = _r() * 0x3ade67b7;
980
- words[i / 4] = (_r() * 0x100000000) | 0;
981
- }
982
- return typedArray;
983
- }
984
- };
985
-
986
961
  /** @fileOverview CTR mode implementation.
987
962
  *
988
963
  * Special thanks to Roy Nicholson for pointing out a bug in our
@@ -1213,12 +1188,15 @@
1213
1188
  const ERR_INVALID_PASSWORD = "Invalid password";
1214
1189
  const ERR_INVALID_SIGNATURE = "Invalid signature";
1215
1190
  const ERR_ABORT_CHECK_PASSWORD = "zipjs-abort-check-password";
1191
+ const ERR_UNSUPPORTED_CRYPTO_API = "Crypto API not supported";
1216
1192
 
1217
1193
  function getRandomValues(array) {
1218
1194
  if (GET_RANDOM_VALUES_SUPPORTED) {
1219
1195
  return crypto.getRandomValues(array);
1220
1196
  } else {
1221
- return random.getRandomValues(array);
1197
+ // all supported runtimes expose crypto.getRandomValues; failing hard beats
1198
+ // silently generating predictable encryption salts
1199
+ throw new Error(ERR_UNSUPPORTED_CRYPTO_API);
1222
1200
  }
1223
1201
  }
1224
1202
 
@@ -1277,13 +1255,12 @@
1277
1255
 
1278
1256
  class AESDecryptionStream extends TransformStream {
1279
1257
 
1280
- constructor({ password, rawPassword, signed, encryptionStrength, checkPasswordOnly }) {
1258
+ constructor({ password, rawPassword, encryptionStrength, checkPasswordOnly }) {
1281
1259
  super({
1282
1260
  start() {
1283
1261
  Object.assign(this, {
1284
1262
  ready: new Promise(resolve => this.resolveReady = resolve),
1285
1263
  password: encodePassword(password, rawPassword),
1286
- signed,
1287
1264
  strength: encryptionStrength - 1,
1288
1265
  pending: new Uint8Array()
1289
1266
  });
@@ -1312,7 +1289,6 @@
1312
1289
  },
1313
1290
  async flush(controller) {
1314
1291
  const {
1315
- signed,
1316
1292
  ctr,
1317
1293
  hmac,
1318
1294
  pending,
@@ -1329,13 +1305,15 @@
1329
1305
  const decryptedChunk = ctr.update(encryptedChunk);
1330
1306
  decryptedChunkArray = fromBits(codecBytes, decryptedChunk);
1331
1307
  }
1332
- if (signed) {
1333
- const signature = subarray(fromBits(codecBytes, hmac.digest()), 0, SIGNATURE_LENGTH);
1334
- for (let indexSignature = 0; indexSignature < SIGNATURE_LENGTH; indexSignature++) {
1335
- if (signature[indexSignature] != originalSignature[indexSignature]) {
1336
- throw new Error(ERR_INVALID_SIGNATURE);
1337
- }
1338
- }
1308
+ const signature = subarray(fromBits(codecBytes, hmac.digest()), 0, SIGNATURE_LENGTH);
1309
+ // branch-free comparison: accumulate the differences instead of
1310
+ // short-circuiting on the first mismatching byte
1311
+ let invalidSignature = pending.length < SIGNATURE_LENGTH ? 1 : 0;
1312
+ for (let indexSignature = 0; indexSignature < SIGNATURE_LENGTH; indexSignature++) {
1313
+ invalidSignature |= signature[indexSignature] ^ originalSignature[indexSignature];
1314
+ }
1315
+ if (invalidSignature) {
1316
+ throw new Error(ERR_INVALID_SIGNATURE);
1339
1317
  }
1340
1318
  controller.enqueue(decryptedChunkArray);
1341
1319
  }
@@ -1407,11 +1385,11 @@
1407
1385
  hmac,
1408
1386
  pending
1409
1387
  } = aesCrypto;
1410
- const inputLength = input.length - paddingEnd;
1411
1388
  if (pending.length) {
1412
1389
  input = concat(pending, input);
1413
- output = expand(output, inputLength - (inputLength % BLOCK_LENGTH));
1414
1390
  }
1391
+ const inputLength = input.length - paddingEnd;
1392
+ output = expand(output, paddingStart + (inputLength - (inputLength % BLOCK_LENGTH)));
1415
1393
  let offset;
1416
1394
  for (offset = 0; offset <= inputLength - BLOCK_LENGTH; offset += BLOCK_LENGTH) {
1417
1395
  const inputChunk = toBits(codecBytes, subarray(input, offset, offset + BLOCK_LENGTH));
@@ -1559,21 +1537,24 @@
1559
1537
 
1560
1538
  class ZipCryptoDecryptionStream extends TransformStream {
1561
1539
 
1562
- constructor({ password, passwordVerification, checkPasswordOnly }) {
1540
+ constructor({ password, rawPassword, passwordVerification, checkPasswordOnly }) {
1563
1541
  super({
1564
1542
  start() {
1565
1543
  Object.assign(this, {
1566
1544
  password,
1545
+ rawPassword,
1567
1546
  passwordVerification
1568
1547
  });
1569
- createKeys(this, password);
1548
+ createKeys(this, password, rawPassword);
1570
1549
  },
1571
1550
  transform(chunk, controller) {
1572
1551
  const zipCrypto = this;
1573
- if (zipCrypto.password) {
1552
+ if (zipCrypto.password || zipCrypto.rawPassword) {
1574
1553
  const decryptedHeader = decrypt(zipCrypto, chunk.subarray(0, HEADER_LENGTH));
1575
- zipCrypto.password = null;
1576
- if (decryptedHeader.at(-1) != zipCrypto.passwordVerification) {
1554
+ zipCrypto.password = zipCrypto.rawPassword = null;
1555
+ // verify the password with a branch-free comparison of the verification byte, mirroring
1556
+ // the constant-time AES HMAC signature check
1557
+ if ((decryptedHeader.at(-1) ^ zipCrypto.passwordVerification) != 0) {
1577
1558
  throw new Error(ERR_INVALID_PASSWORD);
1578
1559
  }
1579
1560
  chunk = chunk.subarray(HEADER_LENGTH);
@@ -1590,21 +1571,22 @@
1590
1571
 
1591
1572
  class ZipCryptoEncryptionStream extends TransformStream {
1592
1573
 
1593
- constructor({ password, passwordVerification }) {
1574
+ constructor({ password, rawPassword, passwordVerification }) {
1594
1575
  super({
1595
1576
  start() {
1596
1577
  Object.assign(this, {
1597
1578
  password,
1579
+ rawPassword,
1598
1580
  passwordVerification
1599
1581
  });
1600
- createKeys(this, password);
1582
+ createKeys(this, password, rawPassword);
1601
1583
  },
1602
1584
  transform(chunk, controller) {
1603
1585
  const zipCrypto = this;
1604
1586
  let output;
1605
1587
  let offset;
1606
- if (zipCrypto.password) {
1607
- zipCrypto.password = null;
1588
+ if (zipCrypto.password || zipCrypto.rawPassword) {
1589
+ zipCrypto.password = zipCrypto.rawPassword = null;
1608
1590
  const header = getRandomValues(new Uint8Array(HEADER_LENGTH));
1609
1591
  header[HEADER_LENGTH - 1] = zipCrypto.passwordVerification;
1610
1592
  output = new Uint8Array(chunk.length + header.length);
@@ -1639,25 +1621,31 @@
1639
1621
  return output;
1640
1622
  }
1641
1623
 
1642
- function createKeys(target, password) {
1624
+ function createKeys(target, password, rawPassword) {
1643
1625
  const keys = [0x12345678, 0x23456789, 0x34567890];
1644
1626
  Object.assign(target, {
1645
1627
  keys,
1646
1628
  crcKey0: new Crc32(keys[0]),
1647
1629
  crcKey2: new Crc32(keys[2])
1648
1630
  });
1649
- for (let index = 0; index < password.length; index++) {
1650
- updateKeys(target, password.charCodeAt(index));
1631
+ if (rawPassword) {
1632
+ for (let index = 0; index < rawPassword.length; index++) {
1633
+ updateKeys(target, rawPassword[index]);
1634
+ }
1635
+ } else {
1636
+ for (let index = 0; index < password.length; index++) {
1637
+ updateKeys(target, password.charCodeAt(index));
1638
+ }
1651
1639
  }
1652
1640
  }
1653
1641
 
1654
1642
  function updateKeys(target, byte) {
1655
- let [key0, key1, key2] = target.keys;
1643
+ let [, key1] = target.keys;
1656
1644
  target.crcKey0.append([byte]);
1657
- key0 = ~target.crcKey0.get();
1645
+ const key0 = ~target.crcKey0.get();
1658
1646
  key1 = getInt32(Math.imul(getInt32(key1 + getInt8(key0)), 134775813) + 1);
1659
1647
  target.crcKey2.append([key1 >>> 24]);
1660
- key2 = ~target.crcKey2.get();
1648
+ const key2 = ~target.crcKey2.get();
1661
1649
  target.keys = [key0, key1, key2];
1662
1650
  }
1663
1651
 
@@ -1704,6 +1692,7 @@
1704
1692
 
1705
1693
 
1706
1694
  const ERR_INVALID_UNCOMPRESSED_SIZE = "Invalid uncompressed size";
1695
+ const ERR_INVALID_COMPRESSED_DATA = "Invalid compressed data";
1707
1696
  const FORMAT_DEFLATE_RAW = "deflate-raw";
1708
1697
  const FORMAT_DEFLATE64_RAW = "deflate64-raw";
1709
1698
 
@@ -1760,6 +1749,7 @@
1760
1749
  }
1761
1750
  if (compressed) {
1762
1751
  readable = pipeThroughCommpressionStream(readable, useCompressionStream, { chunkSize, deflate64 }, DecompressionStream, DecompressionStreamZlib, DecompressionStream);
1752
+ readable = mapInflateStreamError(readable);
1763
1753
  }
1764
1754
  if ((!encrypted || zipCrypto) && signed) {
1765
1755
  crc32Stream = new Crc32Stream();
@@ -1788,14 +1778,15 @@
1788
1778
  function pipeThroughCommpressionStream(readable, useCompressionStream, options, CompressionStreamNative, CompressionStreamZlib, CompressionStream) {
1789
1779
  const Stream = useCompressionStream && CompressionStreamNative ? CompressionStreamNative : CompressionStreamZlib || CompressionStream;
1790
1780
  const format = options.deflate64 ? FORMAT_DEFLATE64_RAW : FORMAT_DEFLATE_RAW;
1781
+ let codecStream;
1791
1782
  try {
1792
- readable = pipeThrough(readable, new Stream(format, options));
1783
+ codecStream = new Stream(format, options);
1793
1784
  } catch (error) {
1794
1785
  if (useCompressionStream) {
1795
1786
  if (CompressionStreamZlib) {
1796
- readable = pipeThrough(readable, new CompressionStreamZlib(format, options));
1787
+ codecStream = new CompressionStreamZlib(format, options);
1797
1788
  } else if (CompressionStream) {
1798
- readable = pipeThrough(readable, new CompressionStream(format, options));
1789
+ codecStream = new CompressionStream(format, options);
1799
1790
  } else {
1800
1791
  throw error;
1801
1792
  }
@@ -1803,13 +1794,93 @@
1803
1794
  throw error;
1804
1795
  }
1805
1796
  }
1806
- return readable;
1797
+ // The native CompressionStream/DecompressionStream and the pure-JS zlib port do not signal
1798
+ // backpressure on their writable side (their `ready` never pends), so a plain pipeThrough drains
1799
+ // the whole source into them and peak memory grows with the entry size. Awaiting each write()
1800
+ // paces the source to the codec's consumption rate, keeping streaming memory bounded. The WASM
1801
+ // codec already backpressures, so this is a no-op cost for it.
1802
+ return pipeThroughBackpressured(readable, codecStream);
1807
1803
  }
1808
1804
 
1809
1805
  function pipeThrough(readable, transformStream) {
1810
1806
  return readable.pipeThrough(transformStream);
1811
1807
  }
1812
1808
 
1809
+ // Like readable.pipeThrough(transformStream), but drives the writable side manually and awaits each
1810
+ // write() so that a codec which under-reports backpressure cannot pull the whole source into memory.
1811
+ // Errors propagate in both directions: a source error aborts the codec; a downstream cancel/error
1812
+ // (surfaced through the codec's writable rejecting) cancels the source.
1813
+ function pipeThroughBackpressured(readable, transformStream) {
1814
+ const writer = transformStream.writable.getWriter();
1815
+ const reader = readable.getReader();
1816
+ pump();
1817
+ return transformStream.readable;
1818
+
1819
+ async function pump() {
1820
+ try {
1821
+ for (; ;) {
1822
+ await writer.ready;
1823
+ const result = await reader.read();
1824
+ if (result.done) {
1825
+ await writer.close();
1826
+ break;
1827
+ }
1828
+ await writer.write(result.value);
1829
+ }
1830
+ } catch (error) {
1831
+ await abort(writer, error);
1832
+ await cancel(reader, error);
1833
+ }
1834
+ }
1835
+ }
1836
+
1837
+ async function abort(writer, error) {
1838
+ try {
1839
+ await writer.abort(error);
1840
+ } catch {
1841
+ // the writable may already be errored/closed
1842
+ }
1843
+ }
1844
+
1845
+ async function cancel(reader, error) {
1846
+ try {
1847
+ await reader.cancel(error);
1848
+ } catch {
1849
+ // the readable may already be errored/closed
1850
+ }
1851
+ }
1852
+
1853
+ // DecompressionStream implementations can fail with a message-less TypeError on malformed
1854
+ // input; give these errors an identifiable message (errors raised by the other stages of
1855
+ // the pipeline, e.g. ERR_INVALID_PASSWORD, always carry a message and pass through as-is)
1856
+ function mapInflateStreamError(readable) {
1857
+ const reader = readable.getReader();
1858
+ return new ReadableStream({
1859
+ async pull(controller) {
1860
+ let result;
1861
+ try {
1862
+ result = await reader.read();
1863
+ } catch (error) {
1864
+ if (error && error.message) {
1865
+ throw error;
1866
+ }
1867
+ const mappedError = new Error(ERR_INVALID_COMPRESSED_DATA);
1868
+ mappedError.cause = error;
1869
+ throw mappedError;
1870
+ }
1871
+ const { value, done } = result;
1872
+ if (done) {
1873
+ controller.close();
1874
+ } else {
1875
+ controller.enqueue(value);
1876
+ }
1877
+ },
1878
+ cancel(reason) {
1879
+ return reader.cancel(reason);
1880
+ }
1881
+ });
1882
+ }
1883
+
1813
1884
  /*
1814
1885
  Copyright (c) 2022 Gildas Lormeau. All rights reserved.
1815
1886
 
@@ -1839,6 +1910,7 @@
1839
1910
  */
1840
1911
 
1841
1912
 
1913
+ const DEFAULT_CHUNK_SIZE$1 = 64 * 1024;
1842
1914
  const MESSAGE_EVENT_TYPE = "message";
1843
1915
  const MESSAGE_START = "start";
1844
1916
  const MESSAGE_PULL = "pull";
@@ -1907,6 +1979,9 @@
1907
1979
 
1908
1980
  constructor(chunkSize) {
1909
1981
  let pendingChunk;
1982
+ if (!(chunkSize >= 1)) {
1983
+ chunkSize = DEFAULT_CHUNK_SIZE$1;
1984
+ }
1910
1985
  super({
1911
1986
  transform,
1912
1987
  flush(controller) {
@@ -1924,12 +1999,12 @@
1924
1999
  chunk = newChunk;
1925
2000
  pendingChunk = null;
1926
2001
  }
1927
- if (chunk.length > chunkSize) {
1928
- controller.enqueue(chunk.slice(0, chunkSize));
1929
- transform(chunk.slice(chunkSize), controller);
1930
- } else {
1931
- pendingChunk = chunk;
2002
+ let offset = 0;
2003
+ while (chunk.length - offset > chunkSize) {
2004
+ controller.enqueue(chunk.slice(offset, offset + chunkSize));
2005
+ offset += chunkSize;
1932
2006
  }
2007
+ pendingChunk = offset ? chunk.slice(offset) : chunk;
1933
2008
  }
1934
2009
  }
1935
2010
  }
@@ -1964,8 +2039,10 @@
1964
2039
 
1965
2040
 
1966
2041
  const MODULE_WORKER_OPTIONS = { type: "module" };
2042
+ const ERROR_EVENT_TYPE = "error";
2043
+ const MESSAGE_ERROR_EVENT_TYPE = "messageerror";
1967
2044
 
1968
- let webWorkerSupported, webWorkerURI, webWorkerOptions;
2045
+ let webWorkerSupported, webWorkerSource, webWorkerURI, webWorkerOptions;
1969
2046
  let transferStreamsSupported = true;
1970
2047
  try {
1971
2048
  transferStreamsSupported = typeof structuredClone == FUNCTION_TYPE && structuredClone(new DOMException("", "AbortError")).code !== UNDEFINED_VALUE;
@@ -1980,8 +2057,9 @@
1980
2057
  const { signal } = streamOptions;
1981
2058
  Object.assign(workerData, {
1982
2059
  busy: true,
2060
+ generation: (workerData.generation || 0) + 1,
1983
2061
  readable: readable
1984
- .pipeThrough(new ChunkStream(config.chunkSize))
2062
+ .pipeThrough(new ChunkStream(getChunkSize(config)))
1985
2063
  .pipeThrough(new ProgressWatcherStream(streamOptions), { signal }),
1986
2064
  writable,
1987
2065
  options: Object.assign({}, options),
@@ -2004,15 +2082,17 @@
2004
2082
  });
2005
2083
  },
2006
2084
  onTaskFinished() {
2007
- const { resolveTerminated } = workerData;
2008
- if (resolveTerminated) {
2009
- workerData.resolveTerminated = null;
2010
- workerData.terminated = true;
2011
- workerData.worker.terminate();
2012
- resolveTerminated();
2085
+ if (workerData.busy) {
2086
+ const { resolveTerminated } = workerData;
2087
+ if (resolveTerminated) {
2088
+ workerData.resolveTerminated = null;
2089
+ workerData.terminated = true;
2090
+ workerData.worker.terminate();
2091
+ resolveTerminated();
2092
+ }
2093
+ workerData.busy = false;
2094
+ onTaskFinished(workerData);
2013
2095
  }
2014
- workerData.busy = false;
2015
- onTaskFinished(workerData);
2016
2096
  }
2017
2097
  });
2018
2098
  if (webWorkerSupported === UNDEFINED_VALUE) {
@@ -2081,6 +2161,7 @@
2081
2161
  }
2082
2162
  Object.assign(workerData, {
2083
2163
  worker,
2164
+ terminated: false,
2084
2165
  interface: {
2085
2166
  run: () => runWebWorker(workerData, { chunkSize, wasmURI, baseURI })
2086
2167
  }
@@ -2096,7 +2177,15 @@
2096
2177
  try {
2097
2178
  await initModule(config);
2098
2179
  } catch {
2099
- options.useCompressionStream = true;
2180
+ // The WASM module failed to load. Fall back to the native CompressionStream only if the
2181
+ // zlib codec that would otherwise run depends on that module; a self-contained codec
2182
+ // supplied through config (e.g. the pure-JS port) stays usable and is kept as requested.
2183
+ const ZlibStream = options.codecType.startsWith(CODEC_DEFLATE) ?
2184
+ config.CompressionStreamZlib :
2185
+ config.DecompressionStreamZlib;
2186
+ if (!ZlibStream || ZlibStream.requiresModule) {
2187
+ options.useCompressionStream = true;
2188
+ }
2100
2189
  }
2101
2190
  }
2102
2191
  codecStream = new CodecStream(options, config);
@@ -2135,37 +2224,85 @@
2135
2224
  result
2136
2225
  });
2137
2226
  const { readable, options } = workerData;
2138
- const { writable, closed } = watchClosedStream(workerData.writable);
2139
- const streamsTransferred = sendMessage({
2140
- type: MESSAGE_START,
2141
- options,
2142
- config,
2143
- readable,
2144
- writable
2145
- }, workerData);
2227
+ const { writable, closed, abortPipe } = watchClosedStream(workerData.writable);
2228
+ let streamsTransferred;
2229
+ try {
2230
+ streamsTransferred = sendMessage({
2231
+ type: MESSAGE_START,
2232
+ options,
2233
+ config,
2234
+ readable,
2235
+ writable
2236
+ }, workerData);
2237
+ } catch (error) {
2238
+ abortPipe();
2239
+ try {
2240
+ await closed;
2241
+ } catch {
2242
+ // ignored
2243
+ }
2244
+ workerData.onTaskFinished();
2245
+ throw error;
2246
+ }
2146
2247
  if (!streamsTransferred) {
2147
2248
  Object.assign(workerData, {
2148
2249
  reader: readable.getReader(),
2149
2250
  writer: writable.getWriter()
2150
2251
  });
2151
2252
  }
2152
- const resultValue = await result;
2153
- if (!streamsTransferred) {
2154
- await writable.getWriter().close();
2253
+ try {
2254
+ const resultValue = await result;
2255
+ await closeWritable();
2256
+ await closed;
2257
+ return resultValue;
2258
+ } catch (error) {
2259
+ await closeWritable();
2260
+ // unblock the pipe watcher if the worker died before closing its end of the stream
2261
+ abortPipe();
2262
+ try {
2263
+ await closed;
2264
+ } catch {
2265
+ // ignored
2266
+ }
2267
+ throw error;
2268
+ }
2269
+
2270
+ async function closeWritable() {
2271
+ if (!streamsTransferred && !writable.locked) {
2272
+ try {
2273
+ await writable.getWriter().close();
2274
+ } catch {
2275
+ // ignored
2276
+ }
2277
+ }
2155
2278
  }
2156
- await closed;
2157
- return resultValue;
2158
2279
  }
2159
2280
 
2160
2281
  function watchClosedStream(writableSource) {
2282
+ const abortController = new AbortController();
2161
2283
  const { writable, readable } = new TransformStream();
2162
- const closed = readable.pipeTo(writableSource, { preventClose: true });
2163
- return { writable, closed };
2284
+ const closed = readable.pipeTo(writableSource, { preventClose: true, preventAbort: true, signal: abortController.signal });
2285
+ // the pipe can fail before `closed` is awaited (e.g. the destination write fails);
2286
+ // mark the rejection as handled so it cannot kill the process in the meantime
2287
+ closed.catch(() => { });
2288
+ return { writable, closed, abortPipe: () => abortController.abort() };
2289
+ }
2290
+
2291
+ function terminateWorker$1(workerData) {
2292
+ const { worker } = workerData;
2293
+ if (worker) {
2294
+ try {
2295
+ worker.terminate();
2296
+ } catch {
2297
+ // ignored
2298
+ }
2299
+ }
2300
+ workerData.interface = null;
2164
2301
  }
2165
2302
 
2166
2303
  function getWebWorker(url, baseURI, workerData, isModuleType, useBlobURI = true) {
2167
2304
  let worker, resolvedURI, resolvedOptions;
2168
- if (webWorkerURI === UNDEFINED_VALUE) {
2305
+ if (webWorkerURI === UNDEFINED_VALUE || webWorkerSource !== url) {
2169
2306
  // deno-lint-ignore valid-typeof
2170
2307
  const isFunctionURI = typeof url == FUNCTION_TYPE;
2171
2308
  if (isFunctionURI) {
@@ -2222,16 +2359,36 @@
2222
2359
  }
2223
2360
  }
2224
2361
  }
2362
+ webWorkerSource = url;
2225
2363
  webWorkerURI = resolvedURI;
2226
2364
  webWorkerOptions = resolvedOptions;
2227
2365
  } else {
2228
2366
  worker = new Worker(webWorkerURI, webWorkerOptions);
2229
2367
  }
2230
2368
  worker.addEventListener(MESSAGE_EVENT_TYPE, event => onMessage(event, workerData));
2369
+ worker.addEventListener(ERROR_EVENT_TYPE, event => onWorkerError(event, workerData));
2370
+ worker.addEventListener(MESSAGE_ERROR_EVENT_TYPE, event => onWorkerError(event, workerData));
2231
2371
  return worker;
2232
2372
  }
2233
2373
 
2234
- function sendMessage(message, { worker, writer, onTaskFinished, transferStreams }) {
2374
+ function onWorkerError(event, workerData) {
2375
+ if (event.preventDefault) {
2376
+ event.preventDefault();
2377
+ }
2378
+ const { rejectResult, writer, onTaskFinished } = workerData;
2379
+ // the worker is unusable (e.g. its script failed to load); terminate it and
2380
+ // fail the current task instead of letting it hang forever
2381
+ terminateWorker$1(workerData);
2382
+ if (rejectResult) {
2383
+ rejectResult(event.error || new Error(event.message || ERROR_EVENT_TYPE));
2384
+ if (writer) {
2385
+ writer.releaseLock();
2386
+ }
2387
+ onTaskFinished();
2388
+ }
2389
+ }
2390
+
2391
+ function sendMessage(message, { worker, writer, transferStreams }) {
2235
2392
  try {
2236
2393
  const { value, readable, writable } = message;
2237
2394
  const transferables = [];
@@ -2265,14 +2422,16 @@
2265
2422
  if (writer) {
2266
2423
  writer.releaseLock();
2267
2424
  }
2268
- onTaskFinished();
2269
2425
  throw error;
2270
2426
  }
2271
2427
  }
2272
2428
 
2273
2429
  async function onMessage({ data }, workerData) {
2274
2430
  const { type, value, messageId, result, error } = data;
2275
- const { reader, writer, resolveResult, rejectResult, onTaskFinished } = workerData;
2431
+ const { reader, writer, resolveResult, rejectResult, onTaskFinished, generation } = workerData;
2432
+ // continuations below can settle after the worker slot has been reassigned to a new
2433
+ // task (e.g. a pending pull read failing late); they must not touch the new task
2434
+ const stale = () => workerData.generation != generation;
2276
2435
  try {
2277
2436
  if (error) {
2278
2437
  const { message, stack, code, name, outputSize } = error;
@@ -2282,23 +2441,32 @@
2282
2441
  } else {
2283
2442
  if (type == MESSAGE_PULL) {
2284
2443
  const { value, done } = await reader.read();
2285
- sendMessage({ type: MESSAGE_DATA, value, done, messageId }, workerData);
2444
+ if (!stale()) {
2445
+ sendMessage({ type: MESSAGE_DATA, value, done, messageId }, workerData);
2446
+ }
2286
2447
  }
2287
2448
  if (type == MESSAGE_DATA) {
2288
2449
  await writer.ready;
2289
2450
  await writer.write(new Uint8Array(value));
2290
- sendMessage({ type: MESSAGE_ACK_DATA, messageId }, workerData);
2451
+ if (!stale()) {
2452
+ sendMessage({ type: MESSAGE_ACK_DATA, messageId }, workerData);
2453
+ }
2291
2454
  }
2292
2455
  if (type == MESSAGE_CLOSE) {
2293
2456
  close(null, result);
2294
2457
  }
2295
2458
  }
2296
2459
  } catch (error) {
2297
- sendMessage({ type: MESSAGE_CLOSE, messageId }, workerData);
2298
- close(error);
2460
+ if (!stale()) {
2461
+ terminateWorker$1(workerData);
2462
+ close(error);
2463
+ }
2299
2464
  }
2300
2465
 
2301
2466
  function close(error, result) {
2467
+ if (stale()) {
2468
+ return;
2469
+ }
2302
2470
  if (error) {
2303
2471
  rejectResult(error);
2304
2472
  } else {
@@ -2342,6 +2510,8 @@
2342
2510
 
2343
2511
  let pool = [];
2344
2512
  const pendingRequests = [];
2513
+ let starvationTimeout;
2514
+ let starvationDelay;
2345
2515
 
2346
2516
  let indexWorker = 0;
2347
2517
 
@@ -2368,14 +2538,20 @@
2368
2538
  pool.push(workerData);
2369
2539
  return new CodecWorker(workerData, stream, workerOptions, onTaskFinished);
2370
2540
  } else {
2371
- return new Promise(resolve => pendingRequests.push({ resolve, stream, workerOptions }));
2541
+ return new Promise(resolve => {
2542
+ pendingRequests.push({ resolve, stream, workerOptions });
2543
+ starvationDelay = config.workerStarvationTimeout;
2544
+ armStarvationTimeout();
2545
+ });
2372
2546
  }
2373
2547
  }
2374
2548
 
2375
2549
  function onTaskFinished(workerData) {
2550
+ clearStarvationTimeout();
2376
2551
  if (pendingRequests.length) {
2377
2552
  const [{ resolve, stream, workerOptions }] = pendingRequests.splice(0, 1);
2378
2553
  resolve(new CodecWorker(workerData, stream, workerOptions, onTaskFinished));
2554
+ armStarvationTimeout();
2379
2555
  } else if (workerData.worker) {
2380
2556
  clearTerminateTimeout(workerData);
2381
2557
  terminateWorker(workerData, workerOptions);
@@ -2385,6 +2561,34 @@
2385
2561
  }
2386
2562
  }
2387
2563
 
2564
+ function armStarvationTimeout() {
2565
+ if (!starvationTimeout && pendingRequests.length && Number.isFinite(starvationDelay) && starvationDelay >= 0) {
2566
+ starvationTimeout = setTimeout(onWorkerStarvation, starvationDelay);
2567
+ }
2568
+ }
2569
+
2570
+ function clearStarvationTimeout() {
2571
+ if (starvationTimeout) {
2572
+ clearTimeout(starvationTimeout);
2573
+ starvationTimeout = null;
2574
+ }
2575
+ }
2576
+
2577
+ function onWorkerStarvation() {
2578
+ starvationTimeout = null;
2579
+ if (pendingRequests.length) {
2580
+ const [{ resolve, stream, workerOptions }] = pendingRequests.splice(0, 1);
2581
+ const inlineWorkerOptions = Object.assign({}, workerOptions, { useWebWorkers: false, workerURI: UNDEFINED_VALUE });
2582
+ resolve(new CodecWorker({}, stream, inlineWorkerOptions, onInlineTaskFinished));
2583
+ armStarvationTimeout();
2584
+ }
2585
+ }
2586
+
2587
+ function onInlineTaskFinished() {
2588
+ clearStarvationTimeout();
2589
+ armStarvationTimeout();
2590
+ }
2591
+
2388
2592
  function terminateWorker(workerData, workerOptions) {
2389
2593
  const { config } = workerOptions;
2390
2594
  const { terminateWorkerTimeout } = config;
@@ -2455,6 +2659,7 @@
2455
2659
 
2456
2660
  const CONTENT_TYPE_TEXT_PLAIN = "text/plain";
2457
2661
  const HTTP_HEADER_CONTENT_LENGTH = "Content-Length";
2662
+ const HTTP_HEADER_CONTENT_ENCODING = "Content-Encoding";
2458
2663
  const HTTP_HEADER_CONTENT_RANGE = "Content-Range";
2459
2664
  const HTTP_HEADER_ACCEPT_RANGES = "Accept-Ranges";
2460
2665
  const HTTP_HEADER_RANGE = "Range";
@@ -2579,7 +2784,7 @@
2579
2784
 
2580
2785
  writeUint8Array(array) {
2581
2786
  const writer = this;
2582
- let indexArray = 0;
2787
+ let indexArray;
2583
2788
  let dataString = writer.pending;
2584
2789
  const delta = writer.pending.length;
2585
2790
  writer.pending = "";
@@ -2589,12 +2794,10 @@
2589
2794
  for (; indexArray < array.length; indexArray++) {
2590
2795
  writer.pending += String.fromCharCode(array[indexArray]);
2591
2796
  }
2592
- if (dataString.length) {
2593
- if (dataString.length > 2) {
2594
- writer.data += btoa(dataString);
2595
- } else {
2596
- writer.pending += dataString;
2597
- }
2797
+ if (dataString.length > 2) {
2798
+ writer.data += btoa(dataString);
2799
+ } else {
2800
+ writer.pending = dataString + writer.pending;
2598
2801
  }
2599
2802
  }
2600
2803
 
@@ -2641,6 +2844,9 @@
2641
2844
  }
2642
2845
  });
2643
2846
  writer.blob = new Response(transformStream.readable, { headers }).blob();
2847
+ // mark early rejections (e.g. aborted pipe) as handled; awaiting getData() still
2848
+ // surfaces them
2849
+ writer.blob.catch(() => { });
2644
2850
  }
2645
2851
 
2646
2852
  getData() {
@@ -2725,7 +2931,8 @@
2725
2931
  preventHeadRequest,
2726
2932
  useRangeHeader,
2727
2933
  forceRangeRequests,
2728
- combineSizeEocd
2934
+ combineSizeEocd,
2935
+ fetch
2729
2936
  } = options;
2730
2937
  options = Object.assign({}, options);
2731
2938
  delete options.preventHeadRequest;
@@ -2733,13 +2940,15 @@
2733
2940
  delete options.forceRangeRequests;
2734
2941
  delete options.combineSizeEocd;
2735
2942
  delete options.useXHR;
2943
+ delete options.fetch;
2736
2944
  Object.assign(httpReader, {
2737
2945
  url,
2738
2946
  options,
2739
2947
  preventHeadRequest,
2740
2948
  useRangeHeader,
2741
2949
  forceRangeRequests,
2742
- combineSizeEocd
2950
+ combineSizeEocd,
2951
+ fetch
2743
2952
  });
2744
2953
  }
2745
2954
 
@@ -2758,7 +2967,10 @@
2758
2967
  throw new Error(ERR_HTTP_RANGE);
2759
2968
  } else {
2760
2969
  if (combineSizeEocd) {
2761
- httpReader.eocdCache = new Uint8Array(await response.arrayBuffer());
2970
+ const eocdCache = new Uint8Array(await response.arrayBuffer());
2971
+ if (response.status == 206 && eocdCache.length == END_OF_CENTRAL_DIR_LENGTH) {
2972
+ httpReader.eocdCache = eocdCache;
2973
+ }
2762
2974
  }
2763
2975
  let contentSize;
2764
2976
  const contentRangeHeader = response.headers.get(HTTP_HEADER_CONTENT_RANGE);
@@ -2804,7 +3016,18 @@
2804
3016
  if (response.status != 206) {
2805
3017
  throw new Error(ERR_HTTP_RANGE);
2806
3018
  }
2807
- return new Uint8Array(await response.arrayBuffer());
3019
+ const contentRangeHeader = response.headers.get(HTTP_HEADER_CONTENT_RANGE);
3020
+ if (contentRangeHeader) {
3021
+ const rangeStart = Number(contentRangeHeader.trim().split(/[\s-]+/)[1]);
3022
+ if (!Number.isNaN(rangeStart) && rangeStart != index) {
3023
+ throw new Error(ERR_HTTP_RANGE);
3024
+ }
3025
+ }
3026
+ const data = new Uint8Array(await response.arrayBuffer());
3027
+ if (data.length != length) {
3028
+ throw new Error(ERR_HTTP_RANGE);
3029
+ }
3030
+ return data;
2808
3031
  }
2809
3032
  } else {
2810
3033
  const { data } = httpReader;
@@ -2841,9 +3064,9 @@
2841
3064
  async function getRequestData(httpReader, sendRequest) {
2842
3065
  const response = await sendRequest(HTTP_METHOD_GET, httpReader, getHeaders(httpReader));
2843
3066
  httpReader.data = new Uint8Array(await response.arrayBuffer());
2844
- if (!httpReader.size) {
2845
- httpReader.size = httpReader.data.length;
2846
- }
3067
+ // the downloaded content is authoritative, e.g. the Content-Length response header
3068
+ // of the HEAD request can be the encoded size when the content is compressed
3069
+ httpReader.size = httpReader.data.length;
2847
3070
  }
2848
3071
 
2849
3072
  async function getContentLength(httpReader, sendRequest, getRequestData) {
@@ -2852,7 +3075,7 @@
2852
3075
  } else {
2853
3076
  const response = await sendRequest(HTTP_METHOD_HEAD, httpReader, getHeaders(httpReader));
2854
3077
  const contentLength = response.headers.get(HTTP_HEADER_CONTENT_LENGTH);
2855
- if (contentLength) {
3078
+ if (contentLength && !response.headers.get(HTTP_HEADER_CONTENT_ENCODING)) {
2856
3079
  httpReader.size = Number(contentLength);
2857
3080
  } else {
2858
3081
  await getRequestData(httpReader, httpReader.options);
@@ -2860,8 +3083,8 @@
2860
3083
  }
2861
3084
  }
2862
3085
 
2863
- async function sendFetchRequest(method, { options, url }, headers) {
2864
- const response = await fetch(url, Object.assign({}, options, { method, headers }));
3086
+ async function sendFetchRequest(method, { fetch: fetchFunction = fetch, options, url }, headers) {
3087
+ const response = await fetchFunction(url, Object.assign({}, options, { method, headers }));
2865
3088
  if (response.status < 400) {
2866
3089
  return response;
2867
3090
  } else {
@@ -2907,7 +3130,8 @@
2907
3130
  super();
2908
3131
  Object.assign(this, {
2909
3132
  url,
2910
- reader: options.useXHR ? new XHRReader(url, options) : new FetchReader(url, options)
3133
+ // an explicit fetch implementation takes precedence over useXHR
3134
+ reader: options.useXHR && !options.fetch ? new XHRReader(url, options) : new FetchReader(url, options)
2911
3135
  });
2912
3136
  }
2913
3137
 
@@ -2932,8 +3156,7 @@
2932
3156
  class HttpRangeReader extends HttpReader {
2933
3157
 
2934
3158
  constructor(url, options = {}) {
2935
- options.useRangeHeader = true;
2936
- super(url, options);
3159
+ super(url, Object.assign({}, options, { useRangeHeader: true }));
2937
3160
  }
2938
3161
  }
2939
3162
 
@@ -3007,7 +3230,7 @@
3007
3230
  reader.lastDiskNumber = 0;
3008
3231
  reader.lastDiskOffset = 0;
3009
3232
  await Promise.all(readers.map(async (diskReader, indexDiskReader) => {
3010
- await diskReader.init();
3233
+ await initStream(diskReader);
3011
3234
  if (indexDiskReader != readers.length - 1) {
3012
3235
  reader.lastDiskOffset += diskReader.size;
3013
3236
  }
@@ -3091,14 +3314,26 @@
3091
3314
  writer.diskOffset += diskSourceWriter.size;
3092
3315
  writer.diskNumber++;
3093
3316
  diskWriter = null;
3094
- await this.write(chunk.subarray(availableSize));
3317
+ // the next disk is not opened yet but its space is available; leaving a
3318
+ // stale value here would force a new disk for every subsequent write
3319
+ writer.availableSize = writer.maxSize;
3320
+ if (chunk.length > availableSize) {
3321
+ await this.write(chunk.subarray(availableSize));
3322
+ }
3095
3323
  } else {
3096
3324
  await writeChunk(chunk);
3097
3325
  }
3098
3326
  },
3099
3327
  async close() {
3100
- await diskWriter.ready;
3101
- await closeDisk();
3328
+ if (diskWriter) {
3329
+ await diskWriter.ready;
3330
+ await closeDisk();
3331
+ }
3332
+ },
3333
+ async abort(reason) {
3334
+ if (diskWriter) {
3335
+ await diskWriter.abort(reason);
3336
+ }
3102
3337
  }
3103
3338
  });
3104
3339
  Object.defineProperty(writer, PROPERTY_NAME_WRITABLE, {
@@ -3113,7 +3348,6 @@
3113
3348
  await diskWriter.ready;
3114
3349
  await diskWriter.write(chunk);
3115
3350
  diskSourceWriter.size += chunkLength;
3116
- writer.size += chunkLength;
3117
3351
  writer.availableSize -= chunkLength;
3118
3352
  }
3119
3353
  }
@@ -3213,7 +3447,7 @@
3213
3447
 
3214
3448
  /* global TextDecoder */
3215
3449
 
3216
- const CP437 = "\0☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ".split("");
3450
+ const CP437 = "\0☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ".split("");
3217
3451
  const VALID_CP437 = CP437.length == 256;
3218
3452
 
3219
3453
  function decodeCP437(stringValue) {
@@ -3261,7 +3495,8 @@
3261
3495
  if (encoding && encoding.trim().toLowerCase() == "cp437") {
3262
3496
  return decodeCP437(value);
3263
3497
  } else {
3264
- return new TextDecoder(encoding).decode(value);
3498
+ // ignoreBOM keeps a leading U+FEFF in filenames/comments instead of dropping it
3499
+ return new TextDecoder(encoding, { ignoreBOM: true }).decode(value);
3265
3500
  }
3266
3501
  }
3267
3502
 
@@ -3438,6 +3673,7 @@
3438
3673
  const OPTION_CHECK_PASSWORD_ONLY = "checkPasswordOnly";
3439
3674
  const OPTION_CHECK_OVERLAPPING_ENTRY_ONLY = "checkOverlappingEntryOnly";
3440
3675
  const OPTION_CHECK_OVERLAPPING_ENTRY = "checkOverlappingEntry";
3676
+ const OPTION_CHECK_AMBIGUITY = "checkAmbiguity";
3441
3677
  const OPTION_CHECK_SIGNATURE = "checkSignature";
3442
3678
  const OPTION_USE_WEB_WORKERS = "useWebWorkers";
3443
3679
  const OPTION_USE_COMPRESSION_STREAM = "useCompressionStream";
@@ -3495,12 +3731,16 @@
3495
3731
  const ERR_EXTRAFIELD_ZIP64_NOT_FOUND = "Zip64 extra field not found";
3496
3732
  const ERR_ENCRYPTED = "File contains encrypted entry";
3497
3733
  const ERR_UNSUPPORTED_ENCRYPTION = "Encryption method not supported";
3498
- const ERR_UNSUPPORTED_COMPRESSION = "Compression method not supported";
3734
+ const ERR_UNSUPPORTED_COMPRESSION$1 = "Compression method not supported";
3499
3735
  const ERR_SPLIT_ZIP_FILE = "Split zip file";
3500
3736
  const ERR_OVERLAPPING_ENTRY = "Overlapping entry found";
3737
+ const ERR_AMBIGUOUS_ARCHIVE = "Ambiguous archive";
3501
3738
  const CHARSET_UTF8 = "utf-8";
3502
3739
  const PROPERTY_NAME_UTF8_SUFFIX = "UTF8";
3503
3740
  const CHARSET_CP437 = "cp437";
3741
+ // bits of the general purpose flag that change how an entry is interpreted; the level bits (1-2) are excluded
3742
+ // because they are informative only
3743
+ const BITFLAG_AMBIGUITY_MASK = BITFLAG_ENCRYPTED | BITFLAG_DATA_DESCRIPTOR | BITFLAG_LANG_ENCODING_FLAG;
3504
3744
  const ZIP64_PROPERTIES = [
3505
3745
  [PROPERTY_NAME_UNCOMPRESSED_SIZE, MAX_32_BITS],
3506
3746
  [PROPERTY_NAME_COMPRESSED_SIZE, MAX_32_BITS],
@@ -3525,7 +3765,7 @@
3525
3765
  reader: new GenericReader(reader),
3526
3766
  options,
3527
3767
  config: getConfiguration(),
3528
- readRanges: []
3768
+ readRanges: new Map()
3529
3769
  });
3530
3770
  }
3531
3771
 
@@ -3558,21 +3798,30 @@
3558
3798
  const commentOffset = endOfDirectoryInfo.offset;
3559
3799
  const commentLength = getUint16(endOfDirectoryView, 20);
3560
3800
  const appendedDataOffset = commentOffset + END_OF_CENTRAL_DIR_LENGTH + commentLength;
3801
+ const checkAmbiguity = getOptionValue$1(zipReader, options, OPTION_CHECK_AMBIGUITY);
3802
+ if (checkAmbiguity && appendedDataOffset != reader.size) {
3803
+ throwAmbiguousArchive("appended data");
3804
+ }
3561
3805
  let lastDiskNumber = getUint16(endOfDirectoryView, 4);
3562
3806
  const expectedLastDiskNumber = reader.lastDiskNumber || 0;
3563
3807
  let diskNumber = getUint16(endOfDirectoryView, 6);
3564
- let filesLength = getUint16(endOfDirectoryView, 8);
3808
+ let filesLength = getUint16(endOfDirectoryView, 10);
3565
3809
  let prependedDataLength = 0;
3566
- let startOffset = 0;
3810
+ let startOffset;
3811
+ let zip64EndOfDirectory;
3567
3812
  if (directoryDataOffset == MAX_32_BITS || directoryDataLength == MAX_32_BITS || filesLength == MAX_16_BITS || diskNumber == MAX_16_BITS) {
3568
- const endOfDirectoryLocatorArray = await readUint8Array(reader, endOfDirectoryInfo.offset - ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH, ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH);
3813
+ const endOfDirectoryLocatorArray = endOfDirectoryInfo.offset >= ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH ?
3814
+ await readUint8Array(reader, endOfDirectoryInfo.offset - ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH, ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH) :
3815
+ new Uint8Array();
3569
3816
  const endOfDirectoryLocatorView = getDataView$1(endOfDirectoryLocatorArray);
3570
- if (getUint32(endOfDirectoryLocatorView, 0) == ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE) {
3817
+ if (endOfDirectoryLocatorArray.length == ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH &&
3818
+ getUint32(endOfDirectoryLocatorView, 0) == ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE) {
3571
3819
  directoryDataOffset = getBigUint64(endOfDirectoryLocatorView, 8);
3572
3820
  let endOfDirectoryArray = await readUint8Array(reader, directoryDataOffset, ZIP64_END_OF_CENTRAL_DIR_LENGTH, -1);
3573
3821
  let endOfDirectoryView = getDataView$1(endOfDirectoryArray);
3574
- const expectedDirectoryDataOffset = endOfDirectoryInfo.offset - ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH - ZIP64_END_OF_CENTRAL_DIR_LENGTH;
3575
- if (getUint32(endOfDirectoryView, 0) != ZIP64_END_OF_CENTRAL_DIR_SIGNATURE && directoryDataOffset != expectedDirectoryDataOffset) {
3822
+ const expectedDirectoryDataOffset = endOfDirectoryInfo.offset - ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH - ZIP64_END_OF_CENTRAL_DIR_LENGTH - (reader.lastDiskOffset || 0);
3823
+ if ((endOfDirectoryArray.length < ZIP64_END_OF_CENTRAL_DIR_LENGTH || getUint32(endOfDirectoryView, 0) != ZIP64_END_OF_CENTRAL_DIR_SIGNATURE) &&
3824
+ directoryDataOffset != expectedDirectoryDataOffset && expectedDirectoryDataOffset >= 0) {
3576
3825
  const originalDirectoryDataOffset = directoryDataOffset;
3577
3826
  directoryDataOffset = expectedDirectoryDataOffset;
3578
3827
  if (directoryDataOffset > originalDirectoryDataOffset) {
@@ -3581,24 +3830,36 @@
3581
3830
  endOfDirectoryArray = await readUint8Array(reader, directoryDataOffset, ZIP64_END_OF_CENTRAL_DIR_LENGTH, -1);
3582
3831
  endOfDirectoryView = getDataView$1(endOfDirectoryArray);
3583
3832
  }
3584
- if (getUint32(endOfDirectoryView, 0) != ZIP64_END_OF_CENTRAL_DIR_SIGNATURE) {
3833
+ if (endOfDirectoryArray.length < ZIP64_END_OF_CENTRAL_DIR_LENGTH || getUint32(endOfDirectoryView, 0) != ZIP64_END_OF_CENTRAL_DIR_SIGNATURE) {
3585
3834
  throw new Error(ERR_EOCDR_LOCATOR_ZIP64_NOT_FOUND);
3586
3835
  }
3836
+ zip64EndOfDirectory = true;
3587
3837
  if (lastDiskNumber == MAX_16_BITS) {
3588
3838
  lastDiskNumber = getUint32(endOfDirectoryView, 16);
3839
+ } else if (checkAmbiguity && lastDiskNumber != getUint32(endOfDirectoryView, 16)) {
3840
+ throwAmbiguousArchive("mismatched zip64 end of central directory record");
3589
3841
  }
3590
3842
  if (diskNumber == MAX_16_BITS) {
3591
3843
  diskNumber = getUint32(endOfDirectoryView, 20);
3844
+ } else if (checkAmbiguity && diskNumber != getUint32(endOfDirectoryView, 20)) {
3845
+ throwAmbiguousArchive("mismatched zip64 end of central directory record");
3592
3846
  }
3593
3847
  if (filesLength == MAX_16_BITS) {
3594
3848
  filesLength = getBigUint64(endOfDirectoryView, 32);
3849
+ } else if (checkAmbiguity && filesLength != getBigUint64(endOfDirectoryView, 32)) {
3850
+ throwAmbiguousArchive("mismatched zip64 end of central directory record");
3595
3851
  }
3596
3852
  if (directoryDataLength == MAX_32_BITS) {
3597
3853
  directoryDataLength = getBigUint64(endOfDirectoryView, 40);
3854
+ } else if (checkAmbiguity && directoryDataLength != getBigUint64(endOfDirectoryView, 40)) {
3855
+ throwAmbiguousArchive("mismatched zip64 end of central directory record");
3598
3856
  }
3599
- directoryDataOffset -= directoryDataLength;
3857
+ directoryDataOffset = getBigUint64(endOfDirectoryView, 48) + prependedDataLength;
3600
3858
  }
3601
3859
  }
3860
+ const declaredDirectoryDataLength = directoryDataLength;
3861
+ const centralDirectoryEndOffset = endOfDirectoryInfo.offset -
3862
+ (zip64EndOfDirectory ? ZIP64_END_OF_CENTRAL_DIR_LENGTH + ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH : 0);
3602
3863
  if (directoryDataOffset >= reader.size) {
3603
3864
  prependedDataLength = reader.size - directoryDataOffset - directoryDataLength - END_OF_CENTRAL_DIR_LENGTH;
3604
3865
  directoryDataOffset = reader.size - directoryDataLength - END_OF_CENTRAL_DIR_LENGTH;
@@ -3613,8 +3874,11 @@
3613
3874
  let directoryArray = await readUint8Array(reader, directoryDataOffset, directoryDataLength, diskNumber);
3614
3875
  let directoryView = getDataView$1(directoryArray);
3615
3876
  if (directoryDataLength) {
3616
- const expectedDirectoryDataOffset = endOfDirectoryInfo.offset - directoryDataLength;
3617
- if (getUint32(directoryView, offset) != CENTRAL_FILE_HEADER_SIGNATURE && directoryDataOffset != expectedDirectoryDataOffset) {
3877
+ if (directoryArray.length < 4) {
3878
+ throw new Error(ERR_BAD_FORMAT);
3879
+ }
3880
+ const expectedDirectoryDataOffset = centralDirectoryEndOffset - directoryDataLength - (reader.lastDiskOffset || 0);
3881
+ if (getUint32(directoryView, offset) != CENTRAL_FILE_HEADER_SIGNATURE && directoryDataOffset != expectedDirectoryDataOffset && diskNumber == lastDiskNumber) {
3618
3882
  const originalDirectoryDataOffset = directoryDataOffset;
3619
3883
  directoryDataOffset = expectedDirectoryDataOffset;
3620
3884
  if (directoryDataOffset > originalDirectoryDataOffset) {
@@ -3624,8 +3888,8 @@
3624
3888
  directoryView = getDataView$1(directoryArray);
3625
3889
  }
3626
3890
  }
3627
- const expectedDirectoryDataLength = endOfDirectoryInfo.offset - directoryDataOffset - (reader.lastDiskOffset || 0);
3628
- if (directoryDataLength != expectedDirectoryDataLength && expectedDirectoryDataLength >= 0) {
3891
+ const expectedDirectoryDataLength = centralDirectoryEndOffset - directoryDataOffset - (reader.lastDiskOffset || 0);
3892
+ if (directoryDataLength != expectedDirectoryDataLength && expectedDirectoryDataLength >= 0 && diskNumber == lastDiskNumber) {
3629
3893
  directoryDataLength = expectedDirectoryDataLength;
3630
3894
  directoryArray = await readUint8Array(reader, directoryDataOffset, directoryDataLength, diskNumber);
3631
3895
  directoryView = getDataView$1(directoryArray);
@@ -3633,16 +3897,19 @@
3633
3897
  if (directoryDataOffset < 0 || directoryDataOffset >= reader.size) {
3634
3898
  throw new Error(ERR_BAD_FORMAT);
3635
3899
  }
3900
+ startOffset = directoryDataOffset;
3636
3901
  const filenameEncoding = getOptionValue$1(zipReader, options, OPTION_FILENAME_ENCODING);
3637
3902
  const commentEncoding = getOptionValue$1(zipReader, options, OPTION_COMMENT_ENCODING);
3903
+ const filenames = checkAmbiguity ? new Set() : UNDEFINED_VALUE;
3904
+ let duplicateFilename;
3638
3905
  for (let indexFile = 0; indexFile < filesLength; indexFile++) {
3639
3906
  const fileEntry = new ZipEntry(reader, config, zipReader.options);
3640
- if (getUint32(directoryView, offset) != CENTRAL_FILE_HEADER_SIGNATURE) {
3907
+ if (offset + CENTRAL_FILE_HEADER_LENGTH > directoryArray.length || getUint32(directoryView, offset) != CENTRAL_FILE_HEADER_SIGNATURE) {
3641
3908
  throw new Error(ERR_CENTRAL_DIRECTORY_NOT_FOUND);
3642
3909
  }
3643
3910
  readCommonHeader(fileEntry, directoryView, offset + 6);
3644
3911
  const languageEncodingFlag = Boolean(fileEntry.bitFlag.languageEncodingFlag);
3645
- const filenameOffset = offset + 46;
3912
+ const filenameOffset = offset + CENTRAL_FILE_HEADER_LENGTH;
3646
3913
  const extraFieldOffset = filenameOffset + fileEntry.filenameLength;
3647
3914
  const commentOffset = extraFieldOffset + fileEntry.extraFieldLength;
3648
3915
  const versionMadeBy = getUint16(directoryView, offset + 4);
@@ -3663,7 +3930,7 @@
3663
3930
  directory: Boolean(msdosAttributesRaw & FILE_ATTR_MSDOS_DIR_MASK),
3664
3931
  archive: Boolean(msdosAttributesRaw & FILE_ATTR_MSDOS_ARCHIVE_MASK)
3665
3932
  };
3666
- const offsetFileEntry = getUint32(directoryView, offset + 42) + prependedDataLength;
3933
+ const offsetFileEntry = getUint32(directoryView, offset + 42);
3667
3934
  const decode = getOptionValue$1(zipReader, options, OPTION_DECODE_TEXT) || decodeText;
3668
3935
  const rawFilenameEncoding = filenameUTF8 ? CHARSET_UTF8 : filenameEncoding || CHARSET_CP437;
3669
3936
  const rawCommentEncoding = commentUTF8 ? CHARSET_UTF8 : commentEncoding || CHARSET_CP437;
@@ -3676,6 +3943,7 @@
3676
3943
  comment = decodeText(rawComment, rawCommentEncoding);
3677
3944
  }
3678
3945
  Object.assign(fileEntry, {
3946
+ index: indexFile,
3679
3947
  versionMadeBy,
3680
3948
  msDosCompatible,
3681
3949
  compressedSize: 0,
@@ -3695,8 +3963,15 @@
3695
3963
  filename,
3696
3964
  comment
3697
3965
  });
3698
- startOffset = Math.max(offsetFileEntry, startOffset);
3699
3966
  readCommonFooter(fileEntry, fileEntry, directoryView, offset + 6);
3967
+ fileEntry.offset += prependedDataLength;
3968
+ startOffset = Math.min(fileEntry.offset, startOffset);
3969
+ if (checkAmbiguity) {
3970
+ if (filenames.has(fileEntry.filename)) {
3971
+ duplicateFilename = true;
3972
+ }
3973
+ filenames.add(fileEntry.filename);
3974
+ }
3700
3975
  const unixExternalUpper = (fileEntry.externalFileAttributes >> 16) & MAX_16_BITS;
3701
3976
  if (fileEntry.unixMode === UNDEFINED_VALUE && (unixExternalUpper & (FILE_ATTR_UNIX_DEFAULT_MASK | FILE_ATTR_UNIX_EXECUTABLE_MASK | FILE_ATTR_UNIX_TYPE_DIR)) != 0) {
3702
3977
  fileEntry.unixMode = unixExternalUpper;
@@ -3717,7 +3992,7 @@
3717
3992
  internalFileAttribute: fileEntry.internalFileAttributes,
3718
3993
  externalFileAttribute: fileEntry.externalFileAttributes,
3719
3994
  executable,
3720
- directory: modeIsDir || upperIsDir || (msDosCompatible && msdosAttributes.directory) || (filename.endsWith(DIRECTORY_SIGNATURE) && !fileEntry.uncompressedSize),
3995
+ directory: modeIsDir || upperIsDir || (msDosCompatible && msdosAttributes.directory) || (fileEntry.filename.endsWith(DIRECTORY_SIGNATURE) && !fileEntry.uncompressedSize),
3721
3996
  zipCrypto: fileEntry.encrypted && !fileEntry.extraFieldAES
3722
3997
  });
3723
3998
  const entry = new Entry(fileEntry);
@@ -3740,6 +4015,15 @@
3740
4015
  }
3741
4016
  yield entry;
3742
4017
  }
4018
+ if (checkAmbiguity && offset != declaredDirectoryDataLength) {
4019
+ throwAmbiguousArchive("trailing central directory data");
4020
+ }
4021
+ if (duplicateFilename) {
4022
+ throwAmbiguousArchive("duplicate filename");
4023
+ }
4024
+ if (checkAmbiguity && (prependedDataLength || (filesLength && startOffset > 0))) {
4025
+ throwAmbiguousArchive("prepended data");
4026
+ }
3743
4027
  const extractPrependedData = getOptionValue$1(zipReader, options, OPTION_EXTRACT_PREPENDED_DATA);
3744
4028
  const extractAppendedData = getOptionValue$1(zipReader, options, OPTION_EXTRACT_APPENDED_DATA);
3745
4029
  if (extractPrependedData) {
@@ -3779,9 +4063,21 @@
3779
4063
  readable: (function () {
3780
4064
  const { readable, writable } = new TransformStream();
3781
4065
  if (value.getData) {
3782
- value.getData(writable);
4066
+ getData();
3783
4067
  return readable;
3784
4068
  }
4069
+
4070
+ async function getData() {
4071
+ try {
4072
+ await value.getData(writable);
4073
+ } catch (error) {
4074
+ try {
4075
+ await writable.abort(error);
4076
+ } catch {
4077
+ // ignored
4078
+ }
4079
+ }
4080
+ }
3785
4081
  })()
3786
4082
  };
3787
4083
  delete chunk.getData;
@@ -3806,6 +4102,7 @@
3806
4102
  const zipEntry = this;
3807
4103
  const {
3808
4104
  reader,
4105
+ index,
3809
4106
  offset,
3810
4107
  diskNumberStart,
3811
4108
  extraFieldAES,
@@ -3831,27 +4128,42 @@
3831
4128
  rawPassword = rawPassword && rawPassword.length && rawPassword;
3832
4129
  if (extraFieldAES) {
3833
4130
  if (extraFieldAES.originalCompressionMethod != COMPRESSION_METHOD_AES) {
3834
- throw new Error(ERR_UNSUPPORTED_COMPRESSION);
4131
+ throw new Error(ERR_UNSUPPORTED_COMPRESSION$1);
3835
4132
  }
3836
4133
  }
3837
4134
  if ((compressionMethod != COMPRESSION_METHOD_STORE && compressionMethod != COMPRESSION_METHOD_DEFLATE && compressionMethod != COMPRESSION_METHOD_DEFLATE_64) && !passThrough) {
3838
- throw new Error(ERR_UNSUPPORTED_COMPRESSION);
4135
+ throw new Error(ERR_UNSUPPORTED_COMPRESSION$1);
3839
4136
  }
3840
- if (getUint32(dataView, 0) != LOCAL_FILE_HEADER_SIGNATURE) {
4137
+ if (dataArray.length < HEADER_SIZE || getUint32(dataView, 0) != LOCAL_FILE_HEADER_SIGNATURE) {
3841
4138
  throw new Error(ERR_LOCAL_FILE_HEADER_NOT_FOUND);
3842
4139
  }
3843
4140
  readCommonHeader(localDirectory, dataView, 4);
3844
4141
  const {
3845
4142
  extraFieldLength,
3846
- filenameLength,
3847
- lastAccessDate,
3848
- creationDate
4143
+ filenameLength
3849
4144
  } = localDirectory;
3850
- localDirectory.rawExtraField = extraFieldLength ?
3851
- await readUint8Array(reader, offset + HEADER_SIZE + filenameLength, extraFieldLength, diskNumberStart) :
3852
- new Uint8Array();
4145
+ const checkAmbiguity = getOptionValue$1(zipEntry, options, OPTION_CHECK_AMBIGUITY);
4146
+ let rawLocalFilename = new Uint8Array();
4147
+ if (checkAmbiguity && (filenameLength || extraFieldLength)) {
4148
+ const trailingDataArray = await readUint8Array(reader, offset + HEADER_SIZE, filenameLength + extraFieldLength, diskNumberStart);
4149
+ rawLocalFilename = trailingDataArray.subarray(0, filenameLength);
4150
+ localDirectory.rawExtraField = trailingDataArray.subarray(filenameLength);
4151
+ } else {
4152
+ localDirectory.rawExtraField = extraFieldLength ?
4153
+ await readUint8Array(reader, offset + HEADER_SIZE + filenameLength, extraFieldLength, diskNumberStart) :
4154
+ new Uint8Array();
4155
+ }
3853
4156
  readCommonFooter(zipEntry, localDirectory, dataView, 4, true);
3854
- Object.assign(fileEntry, { lastAccessDate, creationDate });
4157
+ if (checkAmbiguity) {
4158
+ checkLocalDirectory(zipEntry, localDirectory, rawLocalFilename);
4159
+ }
4160
+ const { lastAccessDate, creationDate } = localDirectory;
4161
+ if (lastAccessDate) {
4162
+ fileEntry.lastAccessDate = lastAccessDate;
4163
+ }
4164
+ if (creationDate) {
4165
+ fileEntry.creationDate = creationDate;
4166
+ }
3855
4167
  const encrypted = zipEntry.encrypted && localDirectory.encrypted && !passThrough;
3856
4168
  const zipCrypto = encrypted && !extraFieldAES;
3857
4169
  if (!passThrough) {
@@ -3897,7 +4209,7 @@
3897
4209
  outputSize: passThrough ? compressedSize : uncompressedSize,
3898
4210
  signature,
3899
4211
  compressed: compressionMethod != 0 && !passThrough,
3900
- encrypted: zipEntry.encrypted && !passThrough,
4212
+ encrypted,
3901
4213
  useWebWorkers: getOptionValue$1(zipEntry, options, OPTION_USE_WEB_WORKERS),
3902
4214
  useCompressionStream,
3903
4215
  transferStreams: getOptionValue$1(zipEntry, options, OPTION_TRANSFER_STREAMS),
@@ -3911,6 +4223,7 @@
3911
4223
  await detectOverlappingEntry({
3912
4224
  reader,
3913
4225
  fileEntry,
4226
+ index,
3914
4227
  offset,
3915
4228
  diskNumberStart,
3916
4229
  signature,
@@ -4013,7 +4326,7 @@
4013
4326
  directory.extraFieldUnicodeComment = extraFieldUnicodeComment;
4014
4327
  }
4015
4328
  const extraFieldAES = extraField.get(EXTRAFIELD_TYPE_AES);
4016
- if (extraFieldAES) {
4329
+ if (extraFieldAES && extraFieldAES.data.length >= 7) {
4017
4330
  readExtraFieldAES(extraFieldAES, directory, compressionMethod);
4018
4331
  directory.extraFieldAES = extraFieldAES;
4019
4332
  } else {
@@ -4050,19 +4363,23 @@
4050
4363
  directory.zip64 = true;
4051
4364
  const extraFieldView = getDataView$1(extraFieldZip64.data);
4052
4365
  const missingProperties = ZIP64_PROPERTIES.filter(([propertyName, max]) => directory[propertyName] == max);
4366
+ const requiredLength = missingProperties.reduce((length, [, max]) => length + ZIP64_EXTRACTION[max].bytes, 0);
4367
+ if (extraFieldZip64.data.length < requiredLength) {
4368
+ throw new Error(ERR_EXTRAFIELD_ZIP64_NOT_FOUND);
4369
+ }
4053
4370
  for (let indexMissingProperty = 0, offset = 0; indexMissingProperty < missingProperties.length; indexMissingProperty++) {
4054
4371
  const [propertyName, max] = missingProperties[indexMissingProperty];
4055
- if (directory[propertyName] == max) {
4056
- const extraction = ZIP64_EXTRACTION[max];
4057
- directory[propertyName] = extraFieldZip64[propertyName] = extraction.getValue(extraFieldView, offset);
4058
- offset += extraction.bytes;
4059
- } else if (extraFieldZip64[propertyName]) {
4060
- throw new Error(ERR_EXTRAFIELD_ZIP64_NOT_FOUND);
4061
- }
4372
+ const extraction = ZIP64_EXTRACTION[max];
4373
+ directory[propertyName] = extraFieldZip64[propertyName] = extraction.getValue(extraFieldView, offset);
4374
+ offset += extraction.bytes;
4062
4375
  }
4063
4376
  }
4064
4377
 
4065
4378
  function readExtraFieldUnicode(extraFieldUnicode, propertyName, rawPropertyName, directory, fileEntry) {
4379
+ if (extraFieldUnicode.data.length < 5) {
4380
+ extraFieldUnicode.valid = false;
4381
+ return;
4382
+ }
4066
4383
  const extraFieldView = getDataView$1(extraFieldUnicode.data);
4067
4384
  const crc32 = new Crc32();
4068
4385
  crc32.append(fileEntry[rawPropertyName]);
@@ -4135,32 +4452,30 @@
4135
4452
  function readExtraFieldUnix(extraField, directory, isInfoZip) {
4136
4453
  try {
4137
4454
  const view = getDataView$1(new Uint8Array(extraField.data));
4138
- let offset = 0;
4139
- const version = getUint8(view, offset++);
4140
- const uidSize = getUint8(view, offset++);
4141
- const uidBytes = extraField.data.subarray(offset, offset + uidSize);
4142
- offset += uidSize;
4143
- const uid = unpackUnixId(uidBytes);
4144
- const gidSize = getUint8(view, offset++);
4145
- const gidBytes = extraField.data.subarray(offset, offset + gidSize);
4146
- offset += gidSize;
4147
- const gid = unpackUnixId(gidBytes);
4148
- let unixMode = UNDEFINED_VALUE;
4149
- if (!isInfoZip && offset + 2 <= extraField.data.length) {
4150
- const base = extraField.data;
4151
- const modeView = new DataView(base.buffer, base.byteOffset + offset, 2);
4152
- unixMode = modeView.getUint16(0, true);
4153
- }
4154
- Object.assign(extraField, { version, uid, gid, unixMode });
4455
+ let uid, gid;
4456
+ if (isInfoZip) {
4457
+ // Info-ZIP New Unix "ux" field (0x7875): version + variable-length uid/gid
4458
+ let offset = 0;
4459
+ const version = getUint8(view, offset++);
4460
+ const uidSize = getUint8(view, offset++);
4461
+ uid = unpackUnixId(extraField.data.subarray(offset, offset + uidSize));
4462
+ offset += uidSize;
4463
+ const gidSize = getUint8(view, offset++);
4464
+ gid = unpackUnixId(extraField.data.subarray(offset, offset + gidSize));
4465
+ Object.assign(extraField, { version, uid, gid });
4466
+ } else if (extraField.data.length >= 4) {
4467
+ // Info-ZIP Unix "Ux" field (0x7855): fixed 2-byte uid + gid (the file mode, if any, is
4468
+ // carried in the external file attributes, not here)
4469
+ uid = getUint16(view, 0);
4470
+ gid = getUint16(view, 2);
4471
+ Object.assign(extraField, { uid, gid });
4472
+ }
4155
4473
  if (uid !== UNDEFINED_VALUE) {
4156
4474
  directory.uid = uid;
4157
4475
  }
4158
4476
  if (gid !== UNDEFINED_VALUE) {
4159
4477
  directory.gid = gid;
4160
4478
  }
4161
- if (unixMode !== UNDEFINED_VALUE) {
4162
- directory.unixMode = unixMode;
4163
- }
4164
4479
  } catch {
4165
4480
  // ignored
4166
4481
  }
@@ -4174,6 +4489,9 @@
4174
4489
  }
4175
4490
 
4176
4491
  function readExtraFieldExtendedTimestamp(extraFieldExtendedTimestamp, directory, localDirectory) {
4492
+ if (!extraFieldExtendedTimestamp.data.length) {
4493
+ return;
4494
+ }
4177
4495
  const extraFieldView = getDataView$1(extraFieldExtendedTimestamp.data);
4178
4496
  const flags = getUint8(extraFieldView, 0);
4179
4497
  const timeProperties = [];
@@ -4199,7 +4517,8 @@
4199
4517
  timeProperties.forEach((propertyName, indexProperty) => {
4200
4518
  if (extraFieldExtendedTimestamp.data.length >= offset + 4) {
4201
4519
  const time = getUint32(extraFieldView, offset);
4202
- directory[propertyName] = extraFieldExtendedTimestamp[propertyName] = new Date(time * 1000);
4520
+ // the extended timestamp is a signed 32-bit time_t value
4521
+ directory[propertyName] = extraFieldExtendedTimestamp[propertyName] = new Date((time | 0) * 1000);
4203
4522
  const rawPropertyName = timeRawProperties[indexProperty];
4204
4523
  extraFieldExtendedTimestamp[rawPropertyName] = time;
4205
4524
  }
@@ -4210,6 +4529,7 @@
4210
4529
  async function detectOverlappingEntry({
4211
4530
  reader,
4212
4531
  fileEntry,
4532
+ index,
4213
4533
  offset,
4214
4534
  diskNumberStart,
4215
4535
  signature,
@@ -4221,10 +4541,9 @@
4221
4541
  readRanges
4222
4542
  }) {
4223
4543
  let diskOffset = 0;
4224
- if (diskNumberStart) {
4225
- for (let indexReader = 0; indexReader < diskNumberStart; indexReader++) {
4226
- const diskReader = reader.readers[indexReader];
4227
- diskOffset += diskReader.size;
4544
+ if (diskNumberStart && reader.readers) {
4545
+ for (let indexReader = 0; indexReader < Math.min(diskNumberStart, reader.readers.length); indexReader++) {
4546
+ diskOffset += reader.readers[indexReader].size;
4228
4547
  }
4229
4548
  }
4230
4549
  let dataDescriptorLength = 0;
@@ -4237,7 +4556,8 @@
4237
4556
  }
4238
4557
  if (dataDescriptorLength) {
4239
4558
  const dataDescriptorArray = await readUint8Array(reader, dataOffset + compressedSize, dataDescriptorLength + DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH, diskNumberStart);
4240
- const dataDescriptorSignature = getUint32(getDataView$1(dataDescriptorArray), 0) == DATA_DESCRIPTOR_RECORD_SIGNATURE;
4559
+ const dataDescriptorSignature = dataDescriptorArray.length == dataDescriptorLength + DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH &&
4560
+ getUint32(getDataView$1(dataDescriptorArray), 0) == DATA_DESCRIPTOR_RECORD_SIGNATURE;
4241
4561
  if (dataDescriptorSignature) {
4242
4562
  const readSignature = getUint32(getDataView$1(dataDescriptorArray), 4);
4243
4563
  let readCompressedSize;
@@ -4262,14 +4582,14 @@
4262
4582
  end: diskOffset + dataOffset + compressedSize + dataDescriptorLength,
4263
4583
  fileEntry
4264
4584
  };
4265
- for (const otherRange of readRanges) {
4266
- if (otherRange.fileEntry != fileEntry && range.start >= otherRange.start && range.start < otherRange.end) {
4585
+ for (const [otherIndex, otherRange] of readRanges) {
4586
+ if (otherIndex != index && range.start < otherRange.end && otherRange.start < range.end) {
4267
4587
  const error = new Error(ERR_OVERLAPPING_ENTRY);
4268
4588
  error.overlappingEntry = otherRange.fileEntry;
4269
4589
  throw error;
4270
4590
  }
4271
4591
  }
4272
- readRanges.push(range);
4592
+ readRanges.set(index, range);
4273
4593
  }
4274
4594
 
4275
4595
  async function seekSignature(reader, signature, startOffset, minimumBytes, maximumLength) {
@@ -4294,6 +4614,35 @@
4294
4614
  }
4295
4615
  }
4296
4616
 
4617
+ function checkLocalDirectory(zipEntry, localDirectory, rawLocalFilename) {
4618
+ const { rawFilename } = zipEntry;
4619
+ if (rawLocalFilename.length != rawFilename.length ||
4620
+ rawLocalFilename.some((byteValue, indexByte) => byteValue != rawFilename[indexByte])) {
4621
+ throwAmbiguousArchive("mismatched local file header (filename)");
4622
+ }
4623
+ if ((localDirectory.rawBitFlag & BITFLAG_AMBIGUITY_MASK) != (zipEntry.rawBitFlag & BITFLAG_AMBIGUITY_MASK)) {
4624
+ throwAmbiguousArchive("mismatched local file header (general purpose bit flag)");
4625
+ }
4626
+ if (localDirectory.compressionMethod != zipEntry.compressionMethod) {
4627
+ throwAmbiguousArchive("mismatched local file header (compression method)");
4628
+ }
4629
+ // when the data descriptor flag is set, the local signature and sizes are zeroed out; all-zero values are
4630
+ // also tolerated when the flag is clear because some tools produce them (streaming writers predating bit 3)
4631
+ if (!localDirectory.bitFlag.dataDescriptor &&
4632
+ (localDirectory.signature || localDirectory.compressedSize || localDirectory.uncompressedSize) &&
4633
+ (localDirectory.signature != zipEntry.signature ||
4634
+ localDirectory.compressedSize != zipEntry.compressedSize ||
4635
+ localDirectory.uncompressedSize != zipEntry.uncompressedSize)) {
4636
+ throwAmbiguousArchive("mismatched local file header (signature or sizes)");
4637
+ }
4638
+ }
4639
+
4640
+ function throwAmbiguousArchive(reason) {
4641
+ const error = new Error(ERR_AMBIGUOUS_ARCHIVE);
4642
+ error.reason = reason;
4643
+ throw error;
4644
+ }
4645
+
4297
4646
  function getOptionValue$1(zipReader, options, name) {
4298
4647
  return options[name] === UNDEFINED_VALUE ? zipReader.options[name] : options[name];
4299
4648
  }
@@ -4332,7 +4681,7 @@
4332
4681
  }
4333
4682
 
4334
4683
  function getDataView$1(array) {
4335
- return new DataView(array.buffer);
4684
+ return new DataView(array.buffer, array.byteOffset, array.byteLength);
4336
4685
  }
4337
4686
 
4338
4687
  /*
@@ -4372,13 +4721,19 @@
4372
4721
  const ERR_INVALID_ENCRYPTION_STRENGTH = "The strength must equal 1, 2, or 3";
4373
4722
  const ERR_INVALID_EXTRAFIELD_TYPE = "Extra field type exceeds 65535";
4374
4723
  const ERR_INVALID_EXTRAFIELD_DATA = "Extra field data exceeds 64KB";
4724
+ // same message as the constant exported by zip-reader.js
4725
+ const ERR_UNSUPPORTED_COMPRESSION = "Compression method not supported";
4726
+ const MIN_UNIX_TIME = -2147483648;
4727
+ const MAX_UNIX_TIME = 2147483647;
4375
4728
  const ERR_UNSUPPORTED_FORMAT = "Zip64 is not supported (set the 'zip64' option to 'true')";
4376
4729
  const ERR_UNDEFINED_UNCOMPRESSED_SIZE = "Undefined uncompressed size";
4730
+ const ERR_UNDEFINED_READER = "Undefined reader";
4377
4731
  const ERR_ZIP_NOT_EMPTY = "Zip file not empty";
4378
4732
  const ERR_INVALID_UID = "Invalid uid (must be integer 0..2^32-1)";
4379
4733
  const ERR_INVALID_GID = "Invalid gid (must be integer 0..2^32-1)";
4380
4734
  const ERR_INVALID_UNIX_MODE = "Invalid UNIX mode (must be integer 0..65535)";
4381
4735
  const ERR_INVALID_UNIX_EXTRA_FIELD_TYPE = "Invalid unixExtraFieldType (must be 'infozip' or 'unix')";
4736
+ const ERR_INVALID_UNIX_ID_SIZE = "uid/gid must be 0..65535 for unixExtraFieldType 'unix' (use 'infozip' for larger ids)";
4382
4737
  const ERR_INVALID_MSDOS_ATTRIBUTES = "Invalid msdosAttributesRaw (must be integer 0..255)";
4383
4738
  const ERR_INVALID_MSDOS_DATA = "Invalid msdosAttributes (must be an object with boolean flags)";
4384
4739
 
@@ -4404,9 +4759,10 @@
4404
4759
  files: new Map(),
4405
4760
  filenames: new Set(),
4406
4761
  offset: options[OPTION_OFFSET] === UNDEFINED_VALUE ? writer.size || writer.writable.size || 0 : options[OPTION_OFFSET],
4407
- pendingEntriesSize: 0,
4762
+ initialOffset: options[OPTION_OFFSET] === UNDEFINED_VALUE ? 0 : options[OPTION_OFFSET] - (writer.size || writer.writable.size || 0),
4408
4763
  pendingAddFileCalls: new Set(),
4409
- bufferedWrites: 0
4764
+ bufferedWrites: 0,
4765
+ lastFileEntry: UNDEFINED_VALUE
4410
4766
  });
4411
4767
  }
4412
4768
 
@@ -4415,17 +4771,18 @@
4415
4771
  throw new Error(ERR_ZIP_NOT_EMPTY);
4416
4772
  }
4417
4773
  reader = new GenericReader(reader);
4774
+ await initStream(reader);
4418
4775
  const zipReader = new ZipReader(reader.readable);
4419
4776
  const entries = await zipReader.getEntries();
4420
4777
  await zipReader.close();
4778
+ await initStream(this.writer);
4421
4779
  await reader.readable.pipeTo(this.writer.writable, { preventClose: true, preventAbort: true });
4422
4780
  this.writer.size = this.offset = reader.size;
4423
4781
  this.filenames = new Set(entries.map(entry => entry.filename));
4424
4782
  this.files = new Map(entries.map(entry => {
4425
4783
  const {
4426
4784
  version,
4427
- compressionMethod,
4428
- lastModDate,
4785
+ rawLastModDate,
4429
4786
  lastAccessDate,
4430
4787
  creationDate,
4431
4788
  rawFilename,
@@ -4438,6 +4795,7 @@
4438
4795
  zip64
4439
4796
  } = entry;
4440
4797
  let {
4798
+ compressionMethod,
4441
4799
  rawExtraFieldZip64,
4442
4800
  rawExtraFieldAES,
4443
4801
  rawExtraFieldExtendedTimestamp,
@@ -4450,31 +4808,39 @@
4450
4808
  rawExtraFieldAES = rawExtraFieldAES || new Uint8Array();
4451
4809
  rawExtraFieldExtendedTimestamp = rawExtraFieldExtendedTimestamp || new Uint8Array();
4452
4810
  rawExtraFieldNTFS = rawExtraFieldNTFS || new Uint8Array();
4453
- rawExtraFieldUnix = entry.rawExtraFieldUnix || new Uint8Array();
4811
+ rawExtraFieldUnix = rawExtraFieldUnix || new Uint8Array();
4454
4812
  rawExtraField = rawExtraField || new Uint8Array();
4813
+ if (entry.extraFieldAES) {
4814
+ compressionMethod = COMPRESSION_METHOD_AES;
4815
+ }
4455
4816
  const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraFieldUnix, rawExtraField);
4456
- const zip64UncompressedSize = zip64 && uncompressedSize > MAX_32_BITS;
4457
- const zip64CompressedSize = zip64 && compressedSize > MAX_32_BITS;
4817
+ const zip64UncompressedSize = zip64 && uncompressedSize >= MAX_32_BITS;
4818
+ const zip64CompressedSize = zip64 && compressedSize >= MAX_32_BITS;
4819
+ const bitFlagValue = (getBitFlag(level, languageEncodingFlag, dataDescriptor, encrypted, compressionMethod) & ~BITFLAG_LEVEL) | (level << 1);
4458
4820
  const {
4459
4821
  headerArray,
4460
4822
  headerView
4461
4823
  } = getHeaderArrayData({
4462
4824
  version,
4463
- bitFlag: getBitFlag(level, languageEncodingFlag, dataDescriptor, encrypted, compressionMethod),
4825
+ bitFlag: bitFlagValue,
4464
4826
  compressionMethod,
4465
4827
  uncompressedSize,
4466
4828
  compressedSize,
4467
- lastModDate,
4829
+ rawLastModDate,
4468
4830
  rawFilename,
4469
4831
  zip64CompressedSize,
4470
4832
  zip64UncompressedSize,
4471
4833
  extraFieldLength
4472
4834
  });
4835
+ const { signature } = entry;
4836
+ if (signature !== UNDEFINED_VALUE) {
4837
+ setUint32(headerView, HEADER_OFFSET_SIGNATURE, signature);
4838
+ }
4473
4839
  Object.assign(entry, {
4474
4840
  zip64UncompressedSize,
4475
4841
  zip64CompressedSize,
4476
- zip64Offset: zip64 && this.offset - diskOffset > MAX_32_BITS,
4477
- zip64DiskNumberStart: zip64 && diskNumber > MAX_16_BITS,
4842
+ zip64Offset: zip64 && this.offset - diskOffset >= MAX_32_BITS,
4843
+ zip64DiskNumberStart: zip64 && diskNumber >= MAX_16_BITS,
4478
4844
  rawExtraFieldZip64,
4479
4845
  rawExtraFieldAES,
4480
4846
  rawExtraFieldExtendedTimestamp,
@@ -4492,6 +4858,7 @@
4492
4858
 
4493
4859
  async add(name = "", reader, options = {}) {
4494
4860
  const zipWriter = this;
4861
+ options = Object.assign({}, options);
4495
4862
  const {
4496
4863
  pendingAddFileCalls,
4497
4864
  config
@@ -4502,17 +4869,24 @@
4502
4869
  await new Promise(resolve => pendingEntries.push(resolve));
4503
4870
  }
4504
4871
  let promiseAddFile;
4872
+ let nameAdded;
4505
4873
  try {
4506
4874
  name = name.trim();
4875
+ if (getOptionValue(zipWriter, options, PROPERTY_NAME_DIRECTORY) && !name.endsWith(DIRECTORY_SIGNATURE)) {
4876
+ name += DIRECTORY_SIGNATURE;
4877
+ }
4507
4878
  if (zipWriter.filenames.has(name)) {
4508
4879
  throw new Error(ERR_DUPLICATED_NAME);
4509
4880
  }
4510
4881
  zipWriter.filenames.add(name);
4882
+ nameAdded = true;
4511
4883
  promiseAddFile = addFile(zipWriter, name, reader, options);
4512
4884
  pendingAddFileCalls.add(promiseAddFile);
4513
4885
  return await promiseAddFile;
4514
4886
  } catch (error) {
4515
- zipWriter.filenames.delete(name);
4887
+ if (nameAdded) {
4888
+ zipWriter.filenames.delete(name);
4889
+ }
4516
4890
  throw error;
4517
4891
  } finally {
4518
4892
  pendingAddFileCalls.delete(promiseAddFile);
@@ -4545,6 +4919,9 @@
4545
4919
  const zipWriter = this;
4546
4920
  const { pendingAddFileCalls, writer } = this;
4547
4921
  const { writable } = writer;
4922
+ if (getLength(comment) > MAX_16_BITS) {
4923
+ throw new Error(ERR_INVALID_COMMENT);
4924
+ }
4548
4925
  while (pendingAddFileCalls.size) {
4549
4926
  await Promise.allSettled(Array.from(pendingAddFileCalls));
4550
4927
  }
@@ -4563,28 +4940,94 @@
4563
4940
  const { readable, writable } = new TransformStream();
4564
4941
  this.readable = readable;
4565
4942
  this.zipWriter = new ZipWriter(writable, options);
4943
+ this.pendingAddFileCalls = new Set();
4566
4944
  }
4567
4945
 
4568
4946
  transform(path) {
4947
+ const zipWriter = this.zipWriter;
4948
+ let streamController;
4569
4949
  const { readable, writable } = new TransformStream({
4570
- flush: () => { this.zipWriter.close(); }
4950
+ start(controller) {
4951
+ streamController = controller;
4952
+ },
4953
+ flush: () => void closeArchive()
4571
4954
  });
4572
- this.zipWriter.add(path, readable);
4955
+ watchAddFileCall(this, this.zipWriter.add(path, readable), error => streamController.error(error));
4573
4956
  return { readable: this.readable, writable };
4957
+
4958
+ async function closeArchive() {
4959
+ try {
4960
+ await zipWriter.close();
4961
+ } catch (error) {
4962
+ try {
4963
+ await zipWriter.writer.writable.abort(error);
4964
+ } catch {
4965
+ // ignored
4966
+ }
4967
+ }
4968
+ }
4574
4969
  }
4575
4970
 
4576
4971
  writable(path) {
4577
- const { readable, writable } = new TransformStream();
4578
- this.zipWriter.add(path, readable);
4972
+ let streamController;
4973
+ const { readable, writable } = new TransformStream({
4974
+ start(controller) {
4975
+ streamController = controller;
4976
+ }
4977
+ });
4978
+ watchAddFileCall(this, this.zipWriter.add(path, readable), error => streamController.error(error));
4579
4979
  return writable;
4580
4980
  }
4581
4981
 
4582
- close(comment = UNDEFINED_VALUE, options = {}) {
4982
+ async close(comment = UNDEFINED_VALUE, options = {}) {
4983
+ await Promise.all(Array.from(this.pendingAddFileCalls));
4583
4984
  return this.zipWriter.close(comment, options);
4584
4985
  }
4585
4986
  }
4586
4987
 
4988
+ function watchAddFileCall(zipWriterStream, promiseAddFile, onerror) {
4989
+ zipWriterStream.pendingAddFileCalls.add(promiseAddFile);
4990
+ promiseAddFile.catch(error => {
4991
+ try {
4992
+ onerror(error);
4993
+ } catch {
4994
+ // ignored
4995
+ }
4996
+ });
4997
+ }
4998
+
4587
4999
  async function addFile(zipWriter, name, reader, options) {
5000
+ const attributesInfo = resolveAttributes(zipWriter, name, options);
5001
+ ({ name } = attributesInfo);
5002
+ const metadataInfo = resolveMetadata(zipWriter, name, options);
5003
+ const { comment } = metadataInfo;
5004
+ const extraField = options[PROPERTY_NAME_EXTRA_FIELD];
5005
+ zipWriter.files.set(name, UNDEFINED_VALUE);
5006
+ let fileEntry;
5007
+ try {
5008
+ const sizesInfo = await resolveSizes(zipWriter, reader, metadataInfo, options);
5009
+ ({ reader } = sizesInfo);
5010
+ const { diskOffset, diskNumber } = zipWriter.writer;
5011
+ options = Object.assign({}, options, attributesInfo.resolvedOptions, metadataInfo.resolvedOptions, sizesInfo.resolvedOptions, {
5012
+ internalFileAttribute: metadataInfo.resolvedOptions.internalFileAttributes,
5013
+ externalFileAttribute: attributesInfo.resolvedOptions.externalFileAttributes,
5014
+ signature: options[PROPERTY_NAME_SIGNATURE],
5015
+ offset: zipWriter.offset - diskOffset,
5016
+ diskNumberStart: diskNumber
5017
+ });
5018
+ const headerInfo = getHeaderInfo(options);
5019
+ const dataDescriptorInfo = getDataDescriptorInfo(options);
5020
+ const metadataSize = getLength(headerInfo.localHeaderArray, dataDescriptorInfo.dataDescriptorArray);
5021
+ fileEntry = await getFileEntry(zipWriter, name, reader, { headerInfo, dataDescriptorInfo, metadataSize }, options);
5022
+ } catch (error) {
5023
+ zipWriter.files.delete(name);
5024
+ throw error;
5025
+ }
5026
+ Object.assign(fileEntry, { name, comment, extraField });
5027
+ return new Entry(fileEntry);
5028
+ }
5029
+
5030
+ function resolveAttributes(zipWriter, name, options) {
4588
5031
  name = name.trim();
4589
5032
  let msDosCompatible = getOptionValue(zipWriter, options, PROPERTY_NAME_MS_DOS_COMPATIBLE);
4590
5033
  let versionMadeBy = getOptionValue(zipWriter, options, PROPERTY_NAME_VERSION_MADE_BY, msDosCompatible ? 20 : 768);
@@ -4592,7 +5035,7 @@
4592
5035
  const uid = getOptionValue(zipWriter, options, PROPERTY_NAME_UID);
4593
5036
  const gid = getOptionValue(zipWriter, options, PROPERTY_NAME_GID);
4594
5037
  let unixMode = getOptionValue(zipWriter, options, PROPERTY_NAME_UNIX_MODE);
4595
- const unixExtraFieldType = getOptionValue(zipWriter, options, OPTION_UNIX_EXTRA_FIELD_TYPE);
5038
+ let unixExtraFieldType = getOptionValue(zipWriter, options, OPTION_UNIX_EXTRA_FIELD_TYPE);
4596
5039
  let setuid = getOptionValue(zipWriter, options, PROPERTY_NAME_SETUID);
4597
5040
  let setgid = getOptionValue(zipWriter, options, PROPERTY_NAME_SETGID);
4598
5041
  let sticky = getOptionValue(zipWriter, options, PROPERTY_NAME_STICKY);
@@ -4608,6 +5051,13 @@
4608
5051
  if (unixExtraFieldType !== UNDEFINED_VALUE && unixExtraFieldType !== INFOZIP_EXTRA_FIELD_TYPE && unixExtraFieldType !== UNIX_EXTRA_FIELD_TYPE) {
4609
5052
  throw new Error(ERR_INVALID_UNIX_EXTRA_FIELD_TYPE);
4610
5053
  }
5054
+ if (unixExtraFieldType === UNIX_EXTRA_FIELD_TYPE &&
5055
+ ((uid !== UNDEFINED_VALUE && uid > MAX_16_BITS) || (gid !== UNDEFINED_VALUE && gid > MAX_16_BITS))) {
5056
+ throw new Error(ERR_INVALID_UNIX_ID_SIZE);
5057
+ }
5058
+ if (unixExtraFieldType === UNDEFINED_VALUE && (uid !== UNDEFINED_VALUE || gid !== UNDEFINED_VALUE)) {
5059
+ unixExtraFieldType = INFOZIP_EXTRA_FIELD_TYPE;
5060
+ }
4611
5061
  let msdosAttributesRaw = getOptionValue(zipWriter, options, PROPERTY_NAME_MSDOS_ATTRIBUTES_RAW);
4612
5062
  let msdosAttributes = getOptionValue(zipWriter, options, PROPERTY_NAME_MSDOS_ATTRIBUTES);
4613
5063
  const hasUnixMetadata = uid !== UNDEFINED_VALUE || gid !== UNDEFINED_VALUE || unixMode !== UNDEFINED_VALUE || unixExtraFieldType;
@@ -4678,6 +5128,27 @@
4678
5128
  if (hasMsDosProvided) {
4679
5129
  externalFileAttributes = (externalFileAttributes & MAX_32_BITS) | (msdosAttributesRaw & MAX_8_BITS);
4680
5130
  }
5131
+ return {
5132
+ name,
5133
+ resolvedOptions: {
5134
+ versionMadeBy,
5135
+ msDosCompatible,
5136
+ externalFileAttributes,
5137
+ unixExternalUpper,
5138
+ uid,
5139
+ gid,
5140
+ unixMode,
5141
+ unixExtraFieldType,
5142
+ setuid,
5143
+ setgid,
5144
+ sticky,
5145
+ msdosAttributesRaw,
5146
+ msdosAttributes
5147
+ }
5148
+ };
5149
+ }
5150
+
5151
+ function resolveMetadata(zipWriter, name, options) {
4681
5152
  const encode = getOptionValue(zipWriter, options, OPTION_ENCODE_TEXT, encodeText);
4682
5153
  let rawFilename = encode(name);
4683
5154
  if (rawFilename === UNDEFINED_VALUE) {
@@ -4716,10 +5187,14 @@
4716
5187
  const transferStreams = getOptionValue(zipWriter, options, OPTION_TRANSFER_STREAMS, true);
4717
5188
  const bufferedWrite = getOptionValue(zipWriter, options, OPTION_BUFFERED_WRITE);
4718
5189
  const createTempStream = getOptionValue(zipWriter, options, OPTION_CREATE_TEMP_STREAM);
4719
- const dataDescriptorSignature = getOptionValue(zipWriter, options, OPTION_DATA_DESCRIPTOR_SIGNATURE, false);
5190
+ const dataDescriptorSignature = getOptionValue(zipWriter, options, OPTION_DATA_DESCRIPTOR_SIGNATURE, true);
4720
5191
  const signal = getOptionValue(zipWriter, options, OPTION_SIGNAL);
4721
5192
  const useUnicodeFileNames = getOptionValue(zipWriter, options, OPTION_USE_UNICODE_FILE_NAMES, true);
4722
5193
  const compressionMethod = getOptionValue(zipWriter, options, PROPERTY_NAME_COMPRESSION_METHOD);
5194
+ if (!passThrough && compressionMethod !== UNDEFINED_VALUE &&
5195
+ compressionMethod !== COMPRESSION_METHOD_STORE && compressionMethod !== COMPRESSION_METHOD_DEFLATE) {
5196
+ throw new Error(ERR_UNSUPPORTED_COMPRESSION);
5197
+ }
4723
5198
  let level = getOptionValue(zipWriter, options, OPTION_LEVEL);
4724
5199
  let useCompressionStream = getOptionValue(zipWriter, options, OPTION_USE_COMPRESSION_STREAM);
4725
5200
  let dataDescriptor = getOptionValue(zipWriter, options, OPTION_DATA_DESCRIPTOR);
@@ -4735,7 +5210,7 @@
4735
5210
  if (!useCompressionStream && (zipWriter.config.CompressionStream === UNDEFINED_VALUE && zipWriter.config.CompressionStreamZlib === UNDEFINED_VALUE)) {
4736
5211
  level = 0;
4737
5212
  }
4738
- let zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
5213
+ const zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
4739
5214
  if (!zipCrypto && (password !== UNDEFINED_VALUE || rawPassword !== UNDEFINED_VALUE) && !(encryptionStrength >= 1 && encryptionStrength <= 3)) {
4740
5215
  throw new Error(ERR_INVALID_ENCRYPTION_STRENGTH);
4741
5216
  }
@@ -4746,6 +5221,7 @@
4746
5221
  let offset = 0;
4747
5222
  extraField.forEach(data => extraFieldSize += 4 + getLength(data));
4748
5223
  rawExtraField = new Uint8Array(extraFieldSize);
5224
+ const rawExtraFieldView = getDataView(rawExtraField);
4749
5225
  extraField.forEach((data, type) => {
4750
5226
  if (type > MAX_16_BITS) {
4751
5227
  throw new Error(ERR_INVALID_EXTRAFIELD_TYPE);
@@ -4753,22 +5229,72 @@
4753
5229
  if (getLength(data) > MAX_16_BITS) {
4754
5230
  throw new Error(ERR_INVALID_EXTRAFIELD_DATA);
4755
5231
  }
4756
- arraySet(rawExtraField, new Uint16Array([type]), offset);
4757
- arraySet(rawExtraField, new Uint16Array([getLength(data)]), offset + 2);
5232
+ setUint16(rawExtraFieldView, offset, type);
5233
+ setUint16(rawExtraFieldView, offset + 2, getLength(data));
4758
5234
  arraySet(rawExtraField, data, offset + 4);
4759
5235
  offset += 4 + getLength(data);
4760
5236
  });
4761
5237
  }
5238
+ return {
5239
+ comment,
5240
+ resolvedOptions: {
5241
+ rawFilename,
5242
+ rawComment,
5243
+ version,
5244
+ lastModDate,
5245
+ lastAccessDate,
5246
+ creationDate,
5247
+ internalFileAttributes,
5248
+ passThrough,
5249
+ password,
5250
+ rawPassword,
5251
+ encryptionStrength,
5252
+ zipCrypto,
5253
+ extendedTimestamp,
5254
+ keepOrder,
5255
+ useWebWorkers,
5256
+ transferStreams,
5257
+ bufferedWrite,
5258
+ createTempStream,
5259
+ dataDescriptorSignature,
5260
+ signal,
5261
+ useUnicodeFileNames,
5262
+ compressionMethod,
5263
+ level,
5264
+ useCompressionStream,
5265
+ dataDescriptor,
5266
+ zip64,
5267
+ rawExtraField
5268
+ }
5269
+ };
5270
+ }
5271
+
5272
+ async function resolveSizes(zipWriter, reader, { resolvedOptions: metadata }, options) {
5273
+ const { passThrough, zipCrypto, password, rawPassword, encryptionStrength } = metadata;
5274
+ let { dataDescriptor, zip64, level, compressionMethod } = metadata;
4762
5275
  let maximumCompressedSize = 0;
4763
- let maximumEntrySize = 0;
4764
5276
  let uncompressedSize = 0;
4765
5277
  if (passThrough) {
5278
+ // the headers of a passThrough entry describe the payload verbatim: without a
5279
+ // reader they would declare content that is not there
5280
+ if (!reader) {
5281
+ throw new Error(ERR_UNDEFINED_READER);
5282
+ }
4766
5283
  uncompressedSize = options[PROPERTY_NAME_UNCOMPRESSED_SIZE];
4767
5284
  if (uncompressedSize === UNDEFINED_VALUE) {
4768
5285
  throw new Error(ERR_UNDEFINED_UNCOMPRESSED_SIZE);
4769
5286
  }
4770
5287
  }
4771
5288
  const zip64Enabled = zip64 === true;
5289
+ const encrypted = getOptionValue(zipWriter, options, PROPERTY_NAME_ENCRYPTED);
5290
+ // entries without a reader have no payload: write them as empty stored files instead of
5291
+ // declaring a compressed or encrypted content that does not exist
5292
+ const encryptedEntry = Boolean(reader) && (Boolean((password && getLength(password)) || (rawPassword && getLength(rawPassword))) || (passThrough && encrypted));
5293
+ if (!reader) {
5294
+ level = 0;
5295
+ compressionMethod = COMPRESSION_METHOD_STORE;
5296
+ }
5297
+ const encryptionOverhead = encryptedEntry ? (zipCrypto ? 12 : 16 + encryptionStrength * 4) : 0;
4772
5298
  if (reader) {
4773
5299
  reader = new GenericReader(reader);
4774
5300
  await initStream(reader);
@@ -4781,16 +5307,15 @@
4781
5307
  }
4782
5308
  } else {
4783
5309
  options.uncompressedSize = uncompressedSize = reader.size;
4784
- maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
5310
+ maximumCompressedSize = getMaximumCompressedSize(uncompressedSize) + encryptionOverhead;
4785
5311
  }
4786
5312
  } else {
4787
5313
  options.uncompressedSize = uncompressedSize;
4788
- maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
5314
+ maximumCompressedSize = getMaximumCompressedSize(uncompressedSize) + encryptionOverhead;
4789
5315
  }
4790
5316
  }
4791
- const { diskOffset, diskNumber } = zipWriter.writer;
4792
- const zip64UncompressedSize = zip64Enabled || uncompressedSize > MAX_32_BITS;
4793
- const zip64CompressedSize = zip64Enabled || maximumCompressedSize > MAX_32_BITS;
5317
+ const zip64UncompressedSize = zip64Enabled || uncompressedSize >= MAX_32_BITS;
5318
+ const zip64CompressedSize = zip64Enabled || maximumCompressedSize >= MAX_32_BITS;
4794
5319
  if (zip64UncompressedSize || zip64CompressedSize) {
4795
5320
  if (zip64 === false) {
4796
5321
  throw new Error(ERR_UNSUPPORTED_FORMAT);
@@ -4799,73 +5324,19 @@
4799
5324
  }
4800
5325
  }
4801
5326
  zip64 = zip64 || false;
4802
- const encrypted = getOptionValue(zipWriter, options, PROPERTY_NAME_ENCRYPTED);
4803
- options = Object.assign({}, options, {
4804
- rawFilename,
4805
- rawComment,
4806
- version,
4807
- versionMadeBy,
4808
- lastModDate,
4809
- lastAccessDate,
4810
- creationDate,
4811
- rawExtraField,
4812
- zip64,
4813
- zip64UncompressedSize,
4814
- zip64CompressedSize,
4815
- password,
4816
- rawPassword,
4817
- level,
4818
- useWebWorkers,
4819
- transferStreams,
4820
- encryptionStrength,
4821
- extendedTimestamp,
4822
- zipCrypto,
4823
- bufferedWrite,
4824
- createTempStream,
4825
- keepOrder,
4826
- useUnicodeFileNames,
4827
- dataDescriptor,
4828
- dataDescriptorSignature,
4829
- signal,
4830
- msDosCompatible,
4831
- internalFileAttribute: internalFileAttributes,
4832
- internalFileAttributes,
4833
- externalFileAttribute: externalFileAttributes,
4834
- externalFileAttributes,
4835
- useCompressionStream,
4836
- passThrough,
4837
- encrypted: Boolean((password && getLength(password)) || (rawPassword && getLength(rawPassword))) || (passThrough && encrypted),
4838
- signature: options[PROPERTY_NAME_SIGNATURE],
4839
- compressionMethod,
4840
- uncompressedSize,
4841
- offset: zipWriter.offset - diskOffset,
4842
- diskNumberStart: diskNumber,
4843
- uid,
4844
- gid,
4845
- setuid,
4846
- setgid,
4847
- sticky,
4848
- unixMode,
4849
- msdosAttributesRaw,
4850
- msdosAttributes,
4851
- unixExternalUpper
4852
- });
4853
- const headerInfo = getHeaderInfo(options);
4854
- const dataDescriptorInfo = getDataDescriptorInfo(options);
4855
- const metadataSize = getLength(headerInfo.localHeaderArray, dataDescriptorInfo.dataDescriptorArray);
4856
- maximumEntrySize = metadataSize + maximumCompressedSize;
4857
- if (zipWriter.options[OPTION_USDZ]) {
4858
- maximumEntrySize += maximumEntrySize + 64;
4859
- }
4860
- zipWriter.pendingEntriesSize += maximumEntrySize;
4861
- let fileEntry;
4862
- try {
4863
- fileEntry = await getFileEntry(zipWriter, name, reader, { headerInfo, dataDescriptorInfo, metadataSize }, options);
4864
- } finally {
4865
- zipWriter.pendingEntriesSize -= maximumEntrySize;
4866
- }
4867
- Object.assign(fileEntry, { name, comment, extraField });
4868
- return new Entry(fileEntry);
5327
+ return {
5328
+ reader,
5329
+ resolvedOptions: {
5330
+ dataDescriptor,
5331
+ zip64,
5332
+ zip64UncompressedSize,
5333
+ zip64CompressedSize,
5334
+ uncompressedSize,
5335
+ level,
5336
+ compressionMethod,
5337
+ encrypted: encryptedEntry
5338
+ }
5339
+ };
4869
5340
  }
4870
5341
 
4871
5342
  async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
@@ -4882,37 +5353,40 @@
4882
5353
  headerInfo
4883
5354
  } = entryInfo;
4884
5355
  const usdz = zipWriter.options[OPTION_USDZ];
4885
- const previousFileEntry = Array.from(files.values()).pop();
5356
+ const previousFileEntry = zipWriter.lastFileEntry;
4886
5357
  let fileEntry = {};
4887
5358
  let bufferedWrite;
4888
5359
  let releaseLockWriter;
4889
5360
  let releaseLockCurrentFileEntry;
4890
5361
  let writingBufferedEntryData;
4891
5362
  let writingEntryData;
5363
+ let writerSizeBeforeEntry;
5364
+ let flushedBufferedSize = 0;
4892
5365
  let fileWriter;
4893
5366
  files.set(name, fileEntry);
5367
+ zipWriter.lastFileEntry = fileEntry;
4894
5368
  try {
4895
5369
  let lockPreviousFileEntry;
4896
5370
  if (keepOrder) {
4897
5371
  lockPreviousFileEntry = previousFileEntry && previousFileEntry.lock;
4898
5372
  requestLockCurrentFileEntry();
4899
5373
  }
4900
- if ((options.bufferedWrite || !keepOrder || zipWriter.writerLocked || zipWriter.bufferedWrites || !dataDescriptor) && !usdz) {
5374
+ if (options.bufferedWrite || !keepOrder || zipWriter.writerLocked || zipWriter.bufferedWrites || !dataDescriptor) {
5375
+ bufferedWrite = true;
5376
+ zipWriter.bufferedWrites++;
4901
5377
  if (options.createTempStream) {
4902
5378
  fileWriter = await options.createTempStream();
4903
5379
  } else {
4904
5380
  fileWriter = new TransformStream(UNDEFINED_VALUE, UNDEFINED_VALUE, { highWaterMark: INFINITY_VALUE });
4905
5381
  }
4906
5382
  fileWriter.size = 0;
4907
- bufferedWrite = true;
4908
- zipWriter.bufferedWrites++;
4909
5383
  await initStream(writer);
4910
5384
  } else {
4911
5385
  fileWriter = writer;
4912
5386
  await requestLockWriter();
4913
5387
  }
4914
5388
  await initStream(fileWriter);
4915
- const { writable, diskOffset } = writer;
5389
+ const { diskOffset } = writer;
4916
5390
  if (zipWriter.addSplitZipSignature) {
4917
5391
  delete zipWriter.addSplitZipSignature;
4918
5392
  const signatureArray = new Uint8Array(4);
@@ -4921,21 +5395,20 @@
4921
5395
  await writeData(writer, signatureArray);
4922
5396
  zipWriter.offset += 4;
4923
5397
  }
4924
- if (usdz) {
5398
+ if (usdz && !bufferedWrite) {
4925
5399
  appendExtraFieldUSDZ(entryInfo, zipWriter.offset - diskOffset);
4926
5400
  }
4927
- const {
4928
- localHeaderView,
4929
- localHeaderArray
4930
- } = headerInfo;
5401
+ const { localHeaderArray } = headerInfo;
4931
5402
  if (!bufferedWrite) {
4932
5403
  await lockPreviousFileEntry;
4933
- await skipDiskIfNeeded(writable);
5404
+ await skipDiskIfNeeded();
4934
5405
  }
4935
- const { diskNumber } = writer;
4936
- fileEntry.diskNumberStart = diskNumber;
5406
+ const diskNumberStart = writer.diskNumber;
5407
+ const entryOffset = getSegmentOffset(zipWriter, writer);
5408
+ fileEntry.diskNumberStart = diskNumberStart;
4937
5409
  if (!bufferedWrite) {
4938
5410
  writingEntryData = true;
5411
+ writerSizeBeforeEntry = writer.size;
4939
5412
  await writeData(fileWriter, localHeaderArray);
4940
5413
  }
4941
5414
  fileEntry = await createFileEntry(reader, fileWriter, fileEntry, entryInfo, zipWriter.config, options);
@@ -4948,16 +5421,24 @@
4948
5421
  await Promise.all([fileWriter.writable.getWriter().close(), lockPreviousFileEntry]);
4949
5422
  await requestLockWriter();
4950
5423
  writingBufferedEntryData = true;
5424
+ writerSizeBeforeEntry = writer.size;
5425
+ await skipDiskIfNeeded();
4951
5426
  fileEntry.diskNumberStart = writer.diskNumber;
4952
- fileEntry.offset = zipWriter.offset - writer.diskOffset;
4953
- updateLocalHeader(fileEntry, localHeaderView, options);
4954
- await skipDiskIfNeeded(writable);
4955
- await writeData(writer, localHeaderArray);
4956
- await fileWriter.readable.pipeTo(writable, { preventClose: true, preventAbort: true, signal });
5427
+ fileEntry.offset = getSegmentOffset(zipWriter, writer);
5428
+ if (usdz) {
5429
+ // the padding depends on the entry offset, only known now that the writer is locked
5430
+ const previousMetadataSize = entryInfo.metadataSize;
5431
+ appendExtraFieldUSDZ(entryInfo, zipWriter.offset - writer.diskOffset);
5432
+ fileEntry.size += entryInfo.metadataSize - previousMetadataSize;
5433
+ }
5434
+ updateLocalHeader(fileEntry, headerInfo.localHeaderView, options);
5435
+ await writeData(writer, headerInfo.localHeaderArray);
5436
+ await flushBufferedData(fileWriter.readable, writer, signal, chunkLength => flushedBufferedSize += chunkLength);
4957
5437
  writer.size += fileWriter.size;
4958
5438
  writingBufferedEntryData = false;
4959
5439
  } else {
4960
- fileEntry.offset = zipWriter.offset - diskOffset;
5440
+ fileEntry.diskNumberStart = diskNumberStart;
5441
+ fileEntry.offset = entryOffset;
4961
5442
  }
4962
5443
  zipWriter.offset += fileEntry.size;
4963
5444
  return fileEntry;
@@ -4971,10 +5452,9 @@
4971
5452
  // ignored
4972
5453
  }
4973
5454
  }
5455
+ zipWriter.offset += writer.size - writerSizeBeforeEntry;
4974
5456
  if (bufferedWrite) {
4975
- zipWriter.offset += fileWriter.size;
4976
- } else {
4977
- zipWriter.offset = fileWriter.size;
5457
+ zipWriter.offset += flushedBufferedSize;
4978
5458
  }
4979
5459
  }
4980
5460
  files.delete(name);
@@ -5005,10 +5485,10 @@
5005
5485
  await lockWriter;
5006
5486
  }
5007
5487
 
5008
- async function skipDiskIfNeeded(writable) {
5488
+ async function skipDiskIfNeeded() {
5009
5489
  if (getLength(headerInfo.localHeaderArray) > writer.availableSize) {
5010
5490
  writer.availableSize = 0;
5011
- await writeData(writable, new Uint8Array());
5491
+ await writeData(writer, new Uint8Array());
5012
5492
  }
5013
5493
  }
5014
5494
  }
@@ -5149,6 +5629,10 @@
5149
5629
  uncompressedSize = result.inputSize;
5150
5630
  signature = result.signature;
5151
5631
  }
5632
+ if ((!zip64CompressedSize && compressedSize >= MAX_32_BITS) ||
5633
+ (!zip64UncompressedSize && uncompressedSize >= MAX_32_BITS)) {
5634
+ throw new Error(ERR_UNSUPPORTED_FORMAT);
5635
+ }
5152
5636
  } catch (error) {
5153
5637
  if (error.outputSize !== UNDEFINED_VALUE) {
5154
5638
  writer.size += error.outputSize;
@@ -5211,35 +5695,28 @@
5211
5695
  uncompressedSize
5212
5696
  } = options;
5213
5697
  let { version, compressionMethod } = options;
5214
- const compressed = !directory && (level > 0 || (level === UNDEFINED_VALUE && compressionMethod !== 0));
5698
+ const compressed = !directory && (compressionMethod === UNDEFINED_VALUE
5699
+ ? (level === UNDEFINED_VALUE || level > 0)
5700
+ : compressionMethod !== COMPRESSION_METHOD_STORE);
5215
5701
  let rawLocalExtraFieldZip64;
5216
5702
  const uncompressedFile = passThrough || !compressed;
5217
- const zip64ExtraFieldComplete = zip64 && (options.bufferedWrite || ((!zip64UncompressedSize && !zip64CompressedSize) || uncompressedFile));
5218
- if (zip64) {
5219
- let rawLocalExtraFieldZip64Length = 4;
5220
- if (zip64UncompressedSize) {
5221
- rawLocalExtraFieldZip64Length += 8;
5222
- }
5223
- if (zip64CompressedSize) {
5224
- rawLocalExtraFieldZip64Length += 8;
5225
- }
5226
- rawLocalExtraFieldZip64 = new Uint8Array(rawLocalExtraFieldZip64Length);
5227
- const rawLocalExtraFieldZip64View = getDataView(rawLocalExtraFieldZip64);
5228
- setUint16(rawLocalExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
5229
- setUint16(rawLocalExtraFieldZip64View, 2, getLength(rawLocalExtraFieldZip64) - 4);
5703
+ // entries without a data descriptor are always buffered (cf. getFileEntry), so the
5704
+ // zip64 extra field values can be patched into the local header before flushing it
5705
+ const zip64ExtraFieldComplete = zip64 && (options.bufferedWrite || !dataDescriptor || ((!zip64UncompressedSize && !zip64CompressedSize) || uncompressedFile));
5706
+ const writeLocalExtraFieldZip64 = zip64ExtraFieldComplete || (zip64 && dataDescriptor && (zip64UncompressedSize || zip64CompressedSize));
5707
+ if (zip64 && (zip64UncompressedSize || zip64CompressedSize)) {
5708
+ // APPNOTE 4.5.3: the zip64 extra field of a local header must include both the
5709
+ // uncompressed and compressed size fields
5710
+ const length = 4 + 16;
5711
+ const extraFieldZip64 = createRecordWriter(length);
5712
+ extraFieldZip64.uint16(EXTRAFIELD_TYPE_ZIP64);
5713
+ extraFieldZip64.uint16(length - 4);
5714
+ rawLocalExtraFieldZip64 = extraFieldZip64.array;
5230
5715
  if (zip64ExtraFieldComplete) {
5231
- const rawLocalExtraFieldZip64View = getDataView(rawLocalExtraFieldZip64);
5232
- let rawLocalExtraFieldZip64Offset = 4;
5233
- if (zip64UncompressedSize) {
5234
- setBigUint64(rawLocalExtraFieldZip64View, rawLocalExtraFieldZip64Offset, BigInt(uncompressedSize));
5235
- rawLocalExtraFieldZip64Offset += 8;
5236
- }
5237
- if (zip64CompressedSize && uncompressedFile) {
5238
- setBigUint64(rawLocalExtraFieldZip64View, rawLocalExtraFieldZip64Offset, BigInt(uncompressedSize));
5239
- rawLocalExtraFieldZip64Offset += 8;
5240
- }
5241
- if (rawLocalExtraFieldZip64Offset == 4) {
5242
- rawLocalExtraFieldZip64 = new Uint8Array();
5716
+ extraFieldZip64.uint64(uncompressedSize);
5717
+ if (uncompressedFile) {
5718
+ const encryptionOverhead = encrypted ? (zipCrypto ? 12 : 16 + encryptionStrength * 4) : 0;
5719
+ extraFieldZip64.uint64(passThrough ? 0 : uncompressedSize + encryptionOverhead);
5243
5720
  }
5244
5721
  }
5245
5722
  } else {
@@ -5247,11 +5724,11 @@
5247
5724
  }
5248
5725
  let rawExtraFieldAES;
5249
5726
  if (encrypted && !zipCrypto) {
5250
- rawExtraFieldAES = new Uint8Array(getLength(EXTRAFIELD_DATA_AES) + 2);
5251
- const extraFieldAESView = getDataView(rawExtraFieldAES);
5252
- setUint16(extraFieldAESView, 0, EXTRAFIELD_TYPE_AES);
5253
- arraySet(rawExtraFieldAES, EXTRAFIELD_DATA_AES, 2);
5254
- setUint8(extraFieldAESView, 8, encryptionStrength);
5727
+ const extraFieldAES = createRecordWriter(getLength(EXTRAFIELD_DATA_AES) + 2);
5728
+ extraFieldAES.uint16(EXTRAFIELD_TYPE_AES);
5729
+ extraFieldAES.bytes(EXTRAFIELD_DATA_AES);
5730
+ rawExtraFieldAES = extraFieldAES.array;
5731
+ rawExtraFieldAES[8] = encryptionStrength;
5255
5732
  } else {
5256
5733
  rawExtraFieldAES = new Uint8Array();
5257
5734
  }
@@ -5259,33 +5736,38 @@
5259
5736
  let rawExtraFieldExtendedTimestamp;
5260
5737
  let extraFieldExtendedTimestampFlag;
5261
5738
  if (extendedTimestamp) {
5262
- rawExtraFieldExtendedTimestamp = new Uint8Array(9 + (lastAccessDate ? 4 : 0) + (creationDate ? 4 : 0));
5263
- const extraFieldExtendedTimestampView = getDataView(rawExtraFieldExtendedTimestamp);
5264
- setUint16(extraFieldExtendedTimestampView, 0, EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP);
5265
- setUint16(extraFieldExtendedTimestampView, 2, getLength(rawExtraFieldExtendedTimestamp) - 4);
5266
- extraFieldExtendedTimestampFlag = 0x1 + (lastAccessDate ? 0x2 : 0) + (creationDate ? 0x4 : 0);
5267
- setUint8(extraFieldExtendedTimestampView, 4, extraFieldExtendedTimestampFlag);
5268
- let offset = 5;
5269
- setUint32(extraFieldExtendedTimestampView, offset, Math.floor(lastModDate.getTime() / 1000));
5270
- offset += 4;
5271
- if (lastAccessDate) {
5272
- setUint32(extraFieldExtendedTimestampView, offset, Math.floor(lastAccessDate.getTime() / 1000));
5273
- offset += 4;
5274
- }
5275
- if (creationDate) {
5276
- setUint32(extraFieldExtendedTimestampView, offset, Math.floor(creationDate.getTime() / 1000));
5739
+ const lastModTimeUnix = getTimeUnix(lastModDate);
5740
+ if (inUnixTimeRange(lastModTimeUnix)) {
5741
+ const extraFieldTimestampLength = 9 + (lastAccessDate ? 4 : 0) + (creationDate ? 4 : 0);
5742
+ const extraFieldTimestamp = createRecordWriter(extraFieldTimestampLength);
5743
+ extraFieldExtendedTimestampFlag = 0x1 + (lastAccessDate ? 0x2 : 0) + (creationDate ? 0x4 : 0);
5744
+ extraFieldTimestamp.uint16(EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP);
5745
+ extraFieldTimestamp.uint16(extraFieldTimestampLength - 4);
5746
+ extraFieldTimestamp.uint8(extraFieldExtendedTimestampFlag);
5747
+ extraFieldTimestamp.uint32(lastModTimeUnix);
5748
+ if (lastAccessDate) {
5749
+ extraFieldTimestamp.uint32(clampUnixTime(getTimeUnix(lastAccessDate)));
5750
+ }
5751
+ if (creationDate) {
5752
+ extraFieldTimestamp.uint32(clampUnixTime(getTimeUnix(creationDate)));
5753
+ }
5754
+ rawExtraFieldExtendedTimestamp = extraFieldTimestamp.array;
5755
+ } else {
5756
+ // the date cannot be stored as a 32-bit time_t; the NTFS field replaces it
5757
+ rawExtraFieldExtendedTimestamp = new Uint8Array();
5277
5758
  }
5278
5759
  try {
5279
- rawExtraFieldNTFS = new Uint8Array(36);
5280
- const extraFieldNTFSView = getDataView(rawExtraFieldNTFS);
5281
5760
  const lastModTimeNTFS = getTimeNTFS(lastModDate);
5282
- setUint16(extraFieldNTFSView, 0, EXTRAFIELD_TYPE_NTFS);
5283
- setUint16(extraFieldNTFSView, 2, 32);
5284
- setUint16(extraFieldNTFSView, 8, EXTRAFIELD_TYPE_NTFS_TAG1);
5285
- setUint16(extraFieldNTFSView, 10, 24);
5286
- setBigUint64(extraFieldNTFSView, 12, lastModTimeNTFS);
5287
- setBigUint64(extraFieldNTFSView, 20, getTimeNTFS(lastAccessDate) || lastModTimeNTFS);
5288
- setBigUint64(extraFieldNTFSView, 28, getTimeNTFS(creationDate) || lastModTimeNTFS);
5761
+ const extraFieldNTFS = createRecordWriter(36);
5762
+ extraFieldNTFS.uint16(EXTRAFIELD_TYPE_NTFS);
5763
+ extraFieldNTFS.uint16(32);
5764
+ extraFieldNTFS.skip(4);
5765
+ extraFieldNTFS.uint16(EXTRAFIELD_TYPE_NTFS_TAG1);
5766
+ extraFieldNTFS.uint16(24);
5767
+ extraFieldNTFS.uint64(lastModTimeNTFS);
5768
+ extraFieldNTFS.uint64(getTimeNTFS(lastAccessDate) || lastModTimeNTFS);
5769
+ extraFieldNTFS.uint64(getTimeNTFS(creationDate) || lastModTimeNTFS);
5770
+ rawExtraFieldNTFS = extraFieldNTFS.array;
5289
5771
  } catch {
5290
5772
  rawExtraFieldNTFS = new Uint8Array();
5291
5773
  }
@@ -5294,41 +5776,27 @@
5294
5776
  }
5295
5777
  let rawExtraFieldUnix;
5296
5778
  try {
5297
- const { uid, gid, unixMode, setuid, setgid, sticky, unixExtraFieldType } = options;
5298
- if (unixExtraFieldType && (uid !== UNDEFINED_VALUE || gid !== UNDEFINED_VALUE || unixMode !== UNDEFINED_VALUE)) {
5779
+ const { uid, gid, unixExtraFieldType } = options;
5780
+ if (unixExtraFieldType == INFOZIP_EXTRA_FIELD_TYPE && (uid !== UNDEFINED_VALUE || gid !== UNDEFINED_VALUE)) {
5299
5781
  const uidBytes = packUnixId(uid);
5300
5782
  const gidBytes = packUnixId(gid);
5301
- let modeArray = new Uint8Array();
5302
- if (unixExtraFieldType == UNIX_EXTRA_FIELD_TYPE && unixMode !== UNDEFINED_VALUE) {
5303
- let modeToWrite = unixMode & MAX_16_BITS;
5304
- if (setuid) {
5305
- modeToWrite |= FILE_ATTR_UNIX_SETUID_MASK;
5306
- }
5307
- if (setgid) {
5308
- modeToWrite |= FILE_ATTR_UNIX_SETGID_MASK;
5309
- }
5310
- if (sticky) {
5311
- modeToWrite |= FILE_ATTR_UNIX_STICKY_MASK;
5312
- }
5313
- modeArray = new Uint8Array(2);
5314
- const modeDataView = new DataView(modeArray.buffer);
5315
- modeDataView.setUint16(0, modeToWrite, true);
5316
- }
5317
- const payloadLength = 3 + uidBytes.length + gidBytes.length + modeArray.length;
5318
- rawExtraFieldUnix = new Uint8Array(4 + payloadLength);
5319
- const rawExtraFieldUnixView = getDataView(rawExtraFieldUnix);
5320
- setUint16(rawExtraFieldUnixView, 0, unixExtraFieldType == INFOZIP_EXTRA_FIELD_TYPE ? EXTRAFIELD_TYPE_INFOZIP : EXTRAFIELD_TYPE_UNIX);
5321
- setUint16(rawExtraFieldUnixView, 2, payloadLength);
5322
- setUint8(rawExtraFieldUnixView, 4, 1);
5323
- setUint8(rawExtraFieldUnixView, 5, uidBytes.length);
5324
- let offset = 6;
5325
- arraySet(rawExtraFieldUnix, uidBytes, offset);
5326
- offset += uidBytes.length;
5327
- setUint8(rawExtraFieldUnixView, offset, gidBytes.length);
5328
- offset++;
5329
- arraySet(rawExtraFieldUnix, gidBytes, offset);
5330
- offset += gidBytes.length;
5331
- arraySet(rawExtraFieldUnix, modeArray, offset);
5783
+ const payloadLength = 3 + uidBytes.length + gidBytes.length;
5784
+ const extraFieldUnix = createRecordWriter(4 + payloadLength);
5785
+ extraFieldUnix.uint16(EXTRAFIELD_TYPE_INFOZIP);
5786
+ extraFieldUnix.uint16(payloadLength);
5787
+ extraFieldUnix.uint8(1);
5788
+ extraFieldUnix.uint8(uidBytes.length);
5789
+ extraFieldUnix.bytes(uidBytes);
5790
+ extraFieldUnix.uint8(gidBytes.length);
5791
+ extraFieldUnix.bytes(gidBytes);
5792
+ rawExtraFieldUnix = extraFieldUnix.array;
5793
+ } else if (unixExtraFieldType == UNIX_EXTRA_FIELD_TYPE && (uid !== UNDEFINED_VALUE || gid !== UNDEFINED_VALUE)) {
5794
+ const extraFieldUnix = createRecordWriter(8);
5795
+ extraFieldUnix.uint16(EXTRAFIELD_TYPE_UNIX);
5796
+ extraFieldUnix.uint16(4);
5797
+ extraFieldUnix.uint16((uid === UNDEFINED_VALUE ? 0 : uid) & MAX_16_BITS);
5798
+ extraFieldUnix.uint16((gid === UNDEFINED_VALUE ? 0 : gid) & MAX_16_BITS);
5799
+ rawExtraFieldUnix = extraFieldUnix.array;
5332
5800
  } else {
5333
5801
  rawExtraFieldUnix = new Uint8Array();
5334
5802
  }
@@ -5346,8 +5814,11 @@
5346
5814
  rawExtraFieldAES[9] = compressionMethod;
5347
5815
  compressionMethod = COMPRESSION_METHOD_AES;
5348
5816
  }
5349
- const localExtraFieldZip64Length = zip64ExtraFieldComplete ? getLength(rawLocalExtraFieldZip64) : 0;
5817
+ const localExtraFieldZip64Length = writeLocalExtraFieldZip64 ? getLength(rawLocalExtraFieldZip64) : 0;
5350
5818
  const extraFieldLength = localExtraFieldZip64Length + getLength(rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraFieldUnix, rawExtraField);
5819
+ if (extraFieldLength > MAX_16_BITS) {
5820
+ throw new Error(ERR_INVALID_EXTRAFIELD_DATA);
5821
+ }
5351
5822
  const {
5352
5823
  headerArray,
5353
5824
  headerView,
@@ -5363,29 +5834,27 @@
5363
5834
  zip64UncompressedSize,
5364
5835
  extraFieldLength
5365
5836
  });
5366
- let localHeaderOffset = HEADER_SIZE;
5367
- const localHeaderArray = new Uint8Array(localHeaderOffset + getLength(rawFilename) + extraFieldLength);
5837
+ const localHeader = createRecordWriter(HEADER_SIZE + getLength(rawFilename) + extraFieldLength);
5838
+ const localHeaderArray = localHeader.array;
5368
5839
  const localHeaderView = getDataView(localHeaderArray);
5369
- setUint32(localHeaderView, 0, LOCAL_FILE_HEADER_SIGNATURE);
5370
- arraySet(localHeaderArray, headerArray, 4);
5371
- arraySet(localHeaderArray, rawFilename, localHeaderOffset);
5372
- localHeaderOffset += getLength(rawFilename);
5373
- if (zip64ExtraFieldComplete) {
5374
- arraySet(localHeaderArray, rawLocalExtraFieldZip64, localHeaderOffset);
5375
- }
5376
- localHeaderOffset += localExtraFieldZip64Length;
5377
- arraySet(localHeaderArray, rawExtraFieldAES, localHeaderOffset);
5378
- localHeaderOffset += getLength(rawExtraFieldAES);
5379
- arraySet(localHeaderArray, rawExtraFieldExtendedTimestamp, localHeaderOffset);
5380
- localHeaderOffset += getLength(rawExtraFieldExtendedTimestamp);
5381
- arraySet(localHeaderArray, rawExtraFieldNTFS, localHeaderOffset);
5382
- localHeaderOffset += getLength(rawExtraFieldNTFS);
5383
- arraySet(localHeaderArray, rawExtraFieldUnix, localHeaderOffset);
5384
- localHeaderOffset += getLength(rawExtraFieldUnix);
5385
- arraySet(localHeaderArray, rawExtraField, localHeaderOffset);
5840
+ localHeader.uint32(LOCAL_FILE_HEADER_SIGNATURE);
5841
+ localHeader.bytes(headerArray);
5842
+ localHeader.bytes(rawFilename);
5843
+ if (writeLocalExtraFieldZip64) {
5844
+ localHeader.bytes(rawLocalExtraFieldZip64);
5845
+ }
5846
+ localHeader.bytes(rawExtraFieldAES);
5847
+ localHeader.bytes(rawExtraFieldExtendedTimestamp);
5848
+ localHeader.bytes(rawExtraFieldNTFS);
5849
+ localHeader.bytes(rawExtraFieldUnix);
5850
+ localHeader.bytes(rawExtraField);
5386
5851
  if (dataDescriptor) {
5387
- setUint32(localHeaderView, HEADER_OFFSET_COMPRESSED_SIZE + 4, 0);
5388
- setUint32(localHeaderView, HEADER_OFFSET_UNCOMPRESSED_SIZE + 4, 0);
5852
+ if (!zip64CompressedSize) {
5853
+ setUint32(localHeaderView, HEADER_OFFSET_COMPRESSED_SIZE + LOCAL_HEADER_COMMON_OFFSET, 0);
5854
+ }
5855
+ if (!zip64UncompressedSize) {
5856
+ setUint32(localHeaderView, HEADER_OFFSET_UNCOMPRESSED_SIZE + LOCAL_HEADER_COMMON_OFFSET, 0);
5857
+ }
5389
5858
  }
5390
5859
  return {
5391
5860
  localHeaderArray,
@@ -5412,7 +5881,7 @@
5412
5881
  function appendExtraFieldUSDZ(entryInfo, zipWriterOffset) {
5413
5882
  const { headerInfo } = entryInfo;
5414
5883
  let { localHeaderArray, extraFieldLength } = headerInfo;
5415
- let localHeaderArrayView = getDataView(localHeaderArray);
5884
+ let localHeaderArrayView;
5416
5885
  let extraBytesLength = 64 - ((zipWriterOffset + getLength(localHeaderArray)) % 64);
5417
5886
  if (extraBytesLength < 4) {
5418
5887
  extraBytesLength += 64;
@@ -5420,13 +5889,14 @@
5420
5889
  const rawExtraFieldUSDZ = new Uint8Array(extraBytesLength);
5421
5890
  const extraFieldUSDZView = getDataView(rawExtraFieldUSDZ);
5422
5891
  setUint16(extraFieldUSDZView, 0, EXTRAFIELD_TYPE_USDZ);
5423
- setUint16(extraFieldUSDZView, 2, extraBytesLength - 2);
5892
+ setUint16(extraFieldUSDZView, 2, extraBytesLength - 4);
5424
5893
  const previousLocalHeaderArray = localHeaderArray;
5425
5894
  headerInfo.localHeaderArray = localHeaderArray = new Uint8Array(getLength(previousLocalHeaderArray) + extraBytesLength);
5426
5895
  arraySet(localHeaderArray, previousLocalHeaderArray);
5427
5896
  arraySet(localHeaderArray, rawExtraFieldUSDZ, getLength(previousLocalHeaderArray));
5428
5897
  localHeaderArrayView = getDataView(localHeaderArray);
5429
5898
  setUint16(localHeaderArrayView, 28, extraFieldLength + extraBytesLength);
5899
+ headerInfo.localHeaderView = localHeaderArrayView;
5430
5900
  entryInfo.metadataSize += extraBytesLength;
5431
5901
  }
5432
5902
 
@@ -5549,37 +6019,32 @@
5549
6019
  }, localHeaderView, { dataDescriptor }) {
5550
6020
  if (!dataDescriptor) {
5551
6021
  if (!encrypted) {
5552
- setUint32(localHeaderView, HEADER_OFFSET_SIGNATURE + 4, signature);
6022
+ setUint32(localHeaderView, HEADER_OFFSET_SIGNATURE + LOCAL_HEADER_COMMON_OFFSET, signature);
5553
6023
  }
5554
6024
  if (!zip64CompressedSize) {
5555
- setUint32(localHeaderView, HEADER_OFFSET_COMPRESSED_SIZE + 4, compressedSize);
6025
+ setUint32(localHeaderView, HEADER_OFFSET_COMPRESSED_SIZE + LOCAL_HEADER_COMMON_OFFSET, compressedSize);
5556
6026
  }
5557
6027
  if (!zip64UncompressedSize) {
5558
- setUint32(localHeaderView, HEADER_OFFSET_UNCOMPRESSED_SIZE + 4, uncompressedSize);
6028
+ setUint32(localHeaderView, HEADER_OFFSET_UNCOMPRESSED_SIZE + LOCAL_HEADER_COMMON_OFFSET, uncompressedSize);
5559
6029
  }
5560
6030
  }
5561
6031
  if (zip64 && localExtraFieldZip64Length) {
5562
- let localHeaderOffset = HEADER_SIZE + getLength(rawFilename) + 4;
5563
- if (zip64UncompressedSize) {
5564
- setBigUint64(localHeaderView, localHeaderOffset, BigInt(uncompressedSize));
5565
- localHeaderOffset += 8;
5566
- }
5567
- if (zip64CompressedSize) {
5568
- setBigUint64(localHeaderView, localHeaderOffset, BigInt(compressedSize));
5569
- localHeaderOffset += 8;
5570
- }
6032
+ // the local zip64 extra field always contains both size slots
6033
+ const localHeaderOffset = HEADER_SIZE + getLength(rawFilename) + 4;
6034
+ setBigUint64(localHeaderView, localHeaderOffset, BigInt(uncompressedSize));
6035
+ setBigUint64(localHeaderView, localHeaderOffset + 8, BigInt(compressedSize));
5571
6036
  }
5572
6037
  }
5573
6038
 
5574
6039
 
5575
6040
  async function closeFile(zipWriter, comment, options) {
5576
- const { files, writer } = zipWriter;
5577
- const { diskOffset } = writer;
5578
- let { diskNumber } = writer;
5579
- let offset = 0;
6041
+ const directoryDataLength = createDirectoryRecords(zipWriter.files);
6042
+ const { cdStartDiskNumber, cdStartDiskOffset } = await writeDirectoryRecords(zipWriter, directoryDataLength, options);
6043
+ await writeEndOfDirectoryRecord(zipWriter, comment, options, { cdStartDiskNumber, cdStartDiskOffset, directoryDataLength });
6044
+ }
6045
+
6046
+ function createDirectoryRecords(files) {
5580
6047
  let directoryDataLength = 0;
5581
- let directoryOffset = zipWriter.offset - diskOffset;
5582
- let filesLength = files.size;
5583
6048
  for (const [, fileEntry] of files) {
5584
6049
  const {
5585
6050
  rawFilename,
@@ -5596,24 +6061,27 @@
5596
6061
  uncompressedSize,
5597
6062
  compressedSize
5598
6063
  } = fileEntry;
5599
- const zip64Offset = fileEntry.offset > MAX_32_BITS;
5600
- const zip64DiskNumberStart = fileEntry.diskNumberStart > MAX_16_BITS;
6064
+ const zip64Offset = fileEntry.offset >= MAX_32_BITS;
6065
+ const zip64DiskNumberStart = fileEntry.diskNumberStart >= MAX_16_BITS;
5601
6066
  let rawExtraFieldZip64;
5602
6067
  if (zip64Offset || zip64DiskNumberStart || zip64UncompressedSize || zip64CompressedSize) {
5603
- let length = 4;
5604
- if (zip64UncompressedSize) length += 8;
5605
- if (zip64CompressedSize) length += 8;
5606
- if (zip64Offset) length += 8;
5607
- if (zip64DiskNumberStart) length += 4;
5608
- rawExtraFieldZip64 = new Uint8Array(length);
5609
- const zip64View = getDataView(rawExtraFieldZip64);
5610
- setUint16(zip64View, 0, EXTRAFIELD_TYPE_ZIP64);
5611
- setUint16(zip64View, 2, length - 4);
5612
- let zip64FieldOffset = 4;
5613
- if (zip64UncompressedSize) { setBigUint64(zip64View, zip64FieldOffset, BigInt(uncompressedSize)); zip64FieldOffset += 8; }
5614
- if (zip64CompressedSize) { setBigUint64(zip64View, zip64FieldOffset, BigInt(compressedSize)); zip64FieldOffset += 8; }
5615
- if (zip64Offset) { setBigUint64(zip64View, zip64FieldOffset, BigInt(fileEntry.offset)); zip64FieldOffset += 8; }
5616
- if (zip64DiskNumberStart) { setUint32(zip64View, zip64FieldOffset, fileEntry.diskNumberStart); }
6068
+ const length = 4 + (zip64UncompressedSize ? 8 : 0) + (zip64CompressedSize ? 8 : 0) + (zip64Offset ? 8 : 0) + (zip64DiskNumberStart ? 4 : 0);
6069
+ const extraFieldZip64 = createRecordWriter(length);
6070
+ extraFieldZip64.uint16(EXTRAFIELD_TYPE_ZIP64);
6071
+ extraFieldZip64.uint16(length - 4);
6072
+ if (zip64UncompressedSize) {
6073
+ extraFieldZip64.uint64(uncompressedSize);
6074
+ }
6075
+ if (zip64CompressedSize) {
6076
+ extraFieldZip64.uint64(compressedSize);
6077
+ }
6078
+ if (zip64Offset) {
6079
+ extraFieldZip64.uint64(fileEntry.offset);
6080
+ }
6081
+ if (zip64DiskNumberStart) {
6082
+ extraFieldZip64.uint32(fileEntry.diskNumberStart);
6083
+ }
6084
+ rawExtraFieldZip64 = extraFieldZip64.array;
5617
6085
  } else {
5618
6086
  rawExtraFieldZip64 = new Uint8Array();
5619
6087
  }
@@ -5621,32 +6089,41 @@
5621
6089
  fileEntry.zip64Offset = zip64Offset;
5622
6090
  fileEntry.zip64DiskNumberStart = zip64DiskNumberStart;
5623
6091
  let rawExtraFieldTimestamp;
5624
- if (extendedTimestamp) {
5625
- rawExtraFieldTimestamp = new Uint8Array(9);
5626
- const extraFieldExtendedTimestampView = getDataView(rawExtraFieldTimestamp);
5627
- setUint16(extraFieldExtendedTimestampView, 0, EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP);
5628
- setUint16(extraFieldExtendedTimestampView, 2, 5);
5629
- setUint8(extraFieldExtendedTimestampView, 4, extraFieldExtendedTimestampFlag);
5630
- setUint32(extraFieldExtendedTimestampView, 5, Math.floor(lastModDate.getTime() / 1000));
6092
+ const lastModTimeUnix = getTimeUnix(lastModDate);
6093
+ if (extendedTimestamp && inUnixTimeRange(lastModTimeUnix)) {
6094
+ const extraFieldTimestamp = createRecordWriter(9);
6095
+ extraFieldTimestamp.uint16(EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP);
6096
+ extraFieldTimestamp.uint16(5);
6097
+ extraFieldTimestamp.uint8(extraFieldExtendedTimestampFlag);
6098
+ extraFieldTimestamp.uint32(lastModTimeUnix);
6099
+ rawExtraFieldTimestamp = extraFieldTimestamp.array;
5631
6100
  } else {
5632
6101
  rawExtraFieldTimestamp = new Uint8Array();
5633
6102
  }
5634
6103
  fileEntry.rawExtraFieldExtendedTimestamp = rawExtraFieldTimestamp;
5635
- directoryDataLength += 46 +
5636
- getLength(
5637
- rawFilename,
5638
- rawComment,
5639
- rawExtraFieldZip64,
5640
- rawExtraFieldAES,
5641
- rawExtraFieldNTFS,
5642
- rawExtraFieldUnix,
5643
- rawExtraFieldTimestamp,
5644
- rawExtraField);
6104
+ const extraFieldLength = getLength(
6105
+ rawExtraFieldZip64,
6106
+ rawExtraFieldAES,
6107
+ rawExtraFieldNTFS,
6108
+ rawExtraFieldUnix,
6109
+ rawExtraFieldTimestamp,
6110
+ rawExtraField);
6111
+ if (extraFieldLength > MAX_16_BITS) {
6112
+ throw new Error(ERR_INVALID_EXTRAFIELD_DATA);
6113
+ }
6114
+ directoryDataLength += CENTRAL_FILE_HEADER_LENGTH + getLength(rawFilename, rawComment) + extraFieldLength;
5645
6115
  }
6116
+ return directoryDataLength;
6117
+ }
6118
+
6119
+ async function writeDirectoryRecords(zipWriter, directoryDataLength, options) {
6120
+ const { files, writer } = zipWriter;
5646
6121
  const directoryArray = new Uint8Array(directoryDataLength);
5647
- const directoryView = getDataView(directoryArray);
5648
6122
  await initStream(writer);
6123
+ let offset = 0;
5649
6124
  let directoryDiskOffset = 0;
6125
+ let cdStartDiskNumber = writer.diskNumber;
6126
+ let cdStartDiskOffset = writer.diskOffset;
5650
6127
  for (const [indexFileEntry, fileEntry] of Array.from(files.values()).entries()) {
5651
6128
  const {
5652
6129
  offset: fileEntryOffset,
@@ -5672,52 +6149,46 @@
5672
6149
  compressedSize
5673
6150
  } = fileEntry;
5674
6151
  const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraFieldUnix, rawExtraField);
5675
- setUint32(directoryView, offset, CENTRAL_FILE_HEADER_SIGNATURE);
5676
- setUint16(directoryView, offset + 4, versionMadeBy);
6152
+ const directoryRecordLength = CENTRAL_FILE_HEADER_LENGTH + getLength(rawFilename, rawComment) + extraFieldLength;
6153
+ if (offset + directoryRecordLength - directoryDiskOffset > writer.availableSize) {
6154
+ await writeData(writer, directoryArray.slice(directoryDiskOffset, offset));
6155
+ directoryDiskOffset = offset;
6156
+ writer.availableSize = 0;
6157
+ await writeData(writer, new Uint8Array());
6158
+ }
6159
+ if (indexFileEntry == 0) {
6160
+ cdStartDiskNumber = writer.diskNumber;
6161
+ cdStartDiskOffset = writer.diskOffset;
6162
+ }
5677
6163
  if (!zip64UncompressedSize) {
5678
6164
  setUint32(headerView, HEADER_OFFSET_UNCOMPRESSED_SIZE, uncompressedSize);
5679
6165
  }
5680
6166
  if (!zip64CompressedSize) {
5681
6167
  setUint32(headerView, HEADER_OFFSET_COMPRESSED_SIZE, compressedSize);
5682
6168
  }
5683
- arraySet(directoryArray, headerArray, offset + 6);
5684
- let directoryOffset = offset + HEADER_SIZE;
5685
- setUint16(directoryView, directoryOffset, extraFieldLength);
5686
- directoryOffset += 2;
5687
- setUint16(directoryView, directoryOffset, getLength(rawComment));
5688
- directoryOffset += 2;
5689
- setUint16(directoryView, directoryOffset, zip64DiskNumberStart ? MAX_16_BITS : diskNumberStart);
5690
- directoryOffset += 2;
5691
- setUint16(directoryView, directoryOffset, internalFileAttributes);
5692
- directoryOffset += 2;
5693
- if (externalFileAttributes) {
5694
- setUint32(directoryView, directoryOffset, externalFileAttributes);
5695
- }
5696
- directoryOffset += 4;
5697
- setUint32(directoryView, directoryOffset, zip64Offset ? MAX_32_BITS : fileEntryOffset);
5698
- directoryOffset += 4;
5699
- arraySet(directoryArray, rawFilename, directoryOffset);
5700
- directoryOffset += getLength(rawFilename);
5701
- arraySet(directoryArray, rawExtraFieldZip64, directoryOffset);
5702
- directoryOffset += getLength(rawExtraFieldZip64);
5703
- arraySet(directoryArray, rawExtraFieldAES, directoryOffset);
5704
- directoryOffset += getLength(rawExtraFieldAES);
5705
- arraySet(directoryArray, rawExtraFieldExtendedTimestamp, directoryOffset);
5706
- directoryOffset += getLength(rawExtraFieldExtendedTimestamp);
5707
- arraySet(directoryArray, rawExtraFieldNTFS, directoryOffset);
5708
- directoryOffset += getLength(rawExtraFieldNTFS);
5709
- arraySet(directoryArray, rawExtraFieldUnix, directoryOffset);
5710
- directoryOffset += getLength(rawExtraFieldUnix);
5711
- arraySet(directoryArray, rawExtraField, directoryOffset);
5712
- directoryOffset += getLength(rawExtraField);
5713
- arraySet(directoryArray, rawComment, directoryOffset);
5714
- directoryOffset += getLength(rawComment);
5715
- if (offset - directoryDiskOffset > writer.availableSize) {
5716
- writer.availableSize = 0;
5717
- await writeData(writer, directoryArray.slice(directoryDiskOffset, offset));
5718
- directoryDiskOffset = offset;
5719
- }
5720
- offset = directoryOffset;
6169
+ if ((zip64Offset || zip64DiskNumberStart) && fileEntry.version < VERSION_ZIP64) {
6170
+ setUint16(headerView, HEADER_OFFSET_VERSION, VERSION_ZIP64);
6171
+ }
6172
+ const directoryRecord = createRecordWriter(directoryRecordLength);
6173
+ directoryRecord.uint32(CENTRAL_FILE_HEADER_SIGNATURE);
6174
+ directoryRecord.uint16(versionMadeBy);
6175
+ directoryRecord.bytes(headerArray.subarray(0, HEADER_SIZE - 4 - 2));
6176
+ directoryRecord.uint16(extraFieldLength);
6177
+ directoryRecord.uint16(getLength(rawComment));
6178
+ directoryRecord.uint16(zip64DiskNumberStart ? MAX_16_BITS : diskNumberStart);
6179
+ directoryRecord.uint16(internalFileAttributes);
6180
+ directoryRecord.uint32(externalFileAttributes);
6181
+ directoryRecord.uint32(zip64Offset ? MAX_32_BITS : fileEntryOffset);
6182
+ directoryRecord.bytes(rawFilename);
6183
+ directoryRecord.bytes(rawExtraFieldZip64);
6184
+ directoryRecord.bytes(rawExtraFieldAES);
6185
+ directoryRecord.bytes(rawExtraFieldExtendedTimestamp);
6186
+ directoryRecord.bytes(rawExtraFieldNTFS);
6187
+ directoryRecord.bytes(rawExtraFieldUnix);
6188
+ directoryRecord.bytes(rawExtraField);
6189
+ directoryRecord.bytes(rawComment);
6190
+ arraySet(directoryArray, directoryRecord.array, offset);
6191
+ offset += directoryRecordLength;
5721
6192
  if (options.onprogress) {
5722
6193
  try {
5723
6194
  await options.onprogress(indexFileEntry + 1, files.size, new Entry(fileEntry));
@@ -5727,36 +6198,53 @@
5727
6198
  }
5728
6199
  }
5729
6200
  await writeData(writer, directoryDiskOffset ? directoryArray.slice(directoryDiskOffset) : directoryArray);
6201
+ return { cdStartDiskNumber, cdStartDiskOffset };
6202
+ }
6203
+
6204
+ async function writeEndOfDirectoryRecord(zipWriter, comment, options, cdInfo) {
6205
+ const { writer } = zipWriter;
6206
+ const { cdStartDiskNumber, cdStartDiskOffset } = cdInfo;
6207
+ let { directoryDataLength } = cdInfo;
6208
+ let filesLength = zipWriter.files.size;
6209
+ let diskNumber = cdStartDiskNumber;
6210
+ let directoryOffset = zipWriter.offset - cdStartDiskOffset - (cdStartDiskNumber ? zipWriter.initialOffset : 0);
5730
6211
  let lastDiskNumber = writer.diskNumber;
5731
- const { availableSize } = writer;
5732
- if (availableSize < END_OF_CENTRAL_DIR_LENGTH) {
6212
+ if (writer.availableSize < END_OF_CENTRAL_DIR_LENGTH) {
5733
6213
  lastDiskNumber++;
5734
6214
  }
5735
6215
  let zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
5736
- if (directoryOffset > MAX_32_BITS || directoryDataLength > MAX_32_BITS || filesLength > MAX_16_BITS || lastDiskNumber > MAX_16_BITS) {
6216
+ if (directoryOffset >= MAX_32_BITS || directoryDataLength >= MAX_32_BITS || filesLength >= MAX_16_BITS || lastDiskNumber >= MAX_16_BITS) {
5737
6217
  if (zip64 === false) {
5738
6218
  throw new Error(ERR_UNSUPPORTED_FORMAT);
5739
6219
  } else {
5740
6220
  zip64 = true;
5741
6221
  }
5742
6222
  }
5743
- const endOfdirectoryArray = new Uint8Array(zip64 ? ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH : END_OF_CENTRAL_DIR_LENGTH);
5744
- const endOfdirectoryView = getDataView(endOfdirectoryArray);
5745
- offset = 0;
6223
+ const commentLength = getLength(comment);
6224
+ if (commentLength > MAX_16_BITS) {
6225
+ throw new Error(ERR_INVALID_COMMENT);
6226
+ }
6227
+ const endOfdirectoryRecord = createRecordWriter(zip64 ? ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH : END_OF_CENTRAL_DIR_LENGTH);
6228
+ if (getLength(endOfdirectoryRecord.array) + commentLength > writer.availableSize) {
6229
+ writer.availableSize = 0;
6230
+ await writeData(writer, new Uint8Array());
6231
+ }
6232
+ lastDiskNumber = writer.diskNumber;
5746
6233
  if (zip64) {
5747
- setUint32(endOfdirectoryView, 0, ZIP64_END_OF_CENTRAL_DIR_SIGNATURE);
5748
- setBigUint64(endOfdirectoryView, 4, BigInt(44));
5749
- setUint16(endOfdirectoryView, 12, 45);
5750
- setUint16(endOfdirectoryView, 14, 45);
5751
- setUint32(endOfdirectoryView, 16, lastDiskNumber);
5752
- setUint32(endOfdirectoryView, 20, diskNumber);
5753
- setBigUint64(endOfdirectoryView, 24, BigInt(filesLength));
5754
- setBigUint64(endOfdirectoryView, 32, BigInt(filesLength));
5755
- setBigUint64(endOfdirectoryView, 40, BigInt(directoryDataLength));
5756
- setBigUint64(endOfdirectoryView, 48, BigInt(directoryOffset));
5757
- setUint32(endOfdirectoryView, 56, ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE);
5758
- setBigUint64(endOfdirectoryView, 64, BigInt(directoryOffset) + BigInt(directoryDataLength));
5759
- setUint32(endOfdirectoryView, 72, lastDiskNumber + 1);
6234
+ endOfdirectoryRecord.uint32(ZIP64_END_OF_CENTRAL_DIR_SIGNATURE);
6235
+ endOfdirectoryRecord.uint64(44);
6236
+ endOfdirectoryRecord.uint16(45);
6237
+ endOfdirectoryRecord.uint16(45);
6238
+ endOfdirectoryRecord.uint32(lastDiskNumber);
6239
+ endOfdirectoryRecord.uint32(diskNumber);
6240
+ endOfdirectoryRecord.uint64(filesLength);
6241
+ endOfdirectoryRecord.uint64(filesLength);
6242
+ endOfdirectoryRecord.uint64(directoryDataLength);
6243
+ endOfdirectoryRecord.uint64(directoryOffset);
6244
+ endOfdirectoryRecord.uint32(ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE);
6245
+ endOfdirectoryRecord.uint32(lastDiskNumber);
6246
+ endOfdirectoryRecord.uint64(BigInt(zipWriter.offset) + BigInt(directoryDataLength) - BigInt(writer.diskOffset) - BigInt(writer.diskNumber ? zipWriter.initialOffset : 0));
6247
+ endOfdirectoryRecord.uint32(lastDiskNumber + 1);
5760
6248
  const supportZip64SplitFile = getOptionValue(zipWriter, options, OPTION_SUPPORT_ZIP64_SPLIT_FILE, true);
5761
6249
  if (supportZip64SplitFile) {
5762
6250
  lastDiskNumber = MAX_16_BITS;
@@ -5765,29 +6253,40 @@
5765
6253
  filesLength = MAX_16_BITS;
5766
6254
  directoryOffset = MAX_32_BITS;
5767
6255
  directoryDataLength = MAX_32_BITS;
5768
- offset += ZIP64_END_OF_CENTRAL_DIR_LENGTH + ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH;
5769
- }
5770
- setUint32(endOfdirectoryView, offset, END_OF_CENTRAL_DIR_SIGNATURE);
5771
- setUint16(endOfdirectoryView, offset + 4, lastDiskNumber);
5772
- setUint16(endOfdirectoryView, offset + 6, diskNumber);
5773
- setUint16(endOfdirectoryView, offset + 8, filesLength);
5774
- setUint16(endOfdirectoryView, offset + 10, filesLength);
5775
- setUint32(endOfdirectoryView, offset + 12, directoryDataLength);
5776
- setUint32(endOfdirectoryView, offset + 16, directoryOffset);
5777
- const commentLength = getLength(comment);
5778
- if (commentLength) {
5779
- if (commentLength <= MAX_16_BITS) {
5780
- setUint16(endOfdirectoryView, offset + 20, commentLength);
5781
- } else {
5782
- throw new Error(ERR_INVALID_COMMENT);
5783
- }
5784
6256
  }
5785
- await writeData(writer, endOfdirectoryArray);
6257
+ endOfdirectoryRecord.uint32(END_OF_CENTRAL_DIR_SIGNATURE);
6258
+ endOfdirectoryRecord.uint16(lastDiskNumber);
6259
+ endOfdirectoryRecord.uint16(diskNumber);
6260
+ endOfdirectoryRecord.uint16(filesLength);
6261
+ endOfdirectoryRecord.uint16(filesLength);
6262
+ endOfdirectoryRecord.uint32(directoryDataLength);
6263
+ endOfdirectoryRecord.uint32(directoryOffset);
6264
+ endOfdirectoryRecord.uint16(commentLength);
6265
+ await writeData(writer, endOfdirectoryRecord.array);
5786
6266
  if (commentLength) {
5787
6267
  await writeData(writer, comment);
5788
6268
  }
5789
6269
  }
5790
6270
 
6271
+ function createRecordWriter(length) {
6272
+ const array = new Uint8Array(length);
6273
+ const view = getDataView(array);
6274
+ let offset = 0;
6275
+ return {
6276
+ array,
6277
+ uint8: value => { setUint8(view, offset, value); offset += 1; },
6278
+ uint16: value => { setUint16(view, offset, value); offset += 2; },
6279
+ uint32: value => { setUint32(view, offset, value); offset += 4; },
6280
+ uint64: value => { setBigUint64(view, offset, BigInt(value)); offset += 8; },
6281
+ bytes: value => { arraySet(array, value, offset); offset += getLength(value); },
6282
+ skip: count => offset += count
6283
+ };
6284
+ }
6285
+
6286
+ function getSegmentOffset(zipWriter, writer) {
6287
+ return zipWriter.offset - writer.diskOffset - (writer.diskNumber ? zipWriter.initialOffset : 0);
6288
+ }
6289
+
5791
6290
  async function writeData(writer, array) {
5792
6291
  const { writable } = writer;
5793
6292
  const streamWriter = writable.getWriter();
@@ -5800,12 +6299,40 @@
5800
6299
  }
5801
6300
  }
5802
6301
 
6302
+ async function flushBufferedData(readable, writer, signal, onChunkWritten) {
6303
+ const streamWriter = writer.writable.getWriter();
6304
+ try {
6305
+ await readable.pipeTo(new WritableStream({
6306
+ async write(chunk) {
6307
+ await streamWriter.ready;
6308
+ await streamWriter.write(chunk);
6309
+ onChunkWritten(getLength(chunk));
6310
+ }
6311
+ }), { preventClose: true, preventAbort: true, signal });
6312
+ } finally {
6313
+ streamWriter.releaseLock();
6314
+ }
6315
+ }
6316
+
5803
6317
  function getTimeNTFS(date) {
5804
6318
  if (date) {
5805
6319
  return ((BigInt(date.getTime()) + BigInt(11644473600000)) * BigInt(10000));
5806
6320
  }
5807
6321
  }
5808
6322
 
6323
+ function getTimeUnix(date) {
6324
+ return Math.floor(date.getTime() / 1000);
6325
+ }
6326
+
6327
+ // the extended timestamp extra field stores dates as signed 32-bit time_t values
6328
+ function inUnixTimeRange(timeUnix) {
6329
+ return timeUnix >= MIN_UNIX_TIME && timeUnix <= MAX_UNIX_TIME;
6330
+ }
6331
+
6332
+ function clampUnixTime(timeUnix) {
6333
+ return Math.min(MAX_UNIX_TIME, Math.max(MIN_UNIX_TIME, timeUnix));
6334
+ }
6335
+
5809
6336
  function getOptionValue(zipWriter, options, name, defaultValue) {
5810
6337
  const result = options[name] === UNDEFINED_VALUE ? zipWriter.options[name] : options[name];
5811
6338
  return result === UNDEFINED_VALUE ? defaultValue : result;
@@ -5836,7 +6363,7 @@
5836
6363
  }
5837
6364
 
5838
6365
  function getDataView(array) {
5839
- return new DataView(array.buffer);
6366
+ return new DataView(array.buffer, array.byteOffset, array.byteLength);
5840
6367
  }
5841
6368
 
5842
6369
  function getLength(...arrayLikes) {
@@ -5852,30 +6379,39 @@
5852
6379
  uncompressedSize,
5853
6380
  compressedSize,
5854
6381
  lastModDate,
6382
+ rawLastModDate,
5855
6383
  rawFilename,
5856
6384
  zip64CompressedSize,
5857
6385
  zip64UncompressedSize,
5858
6386
  extraFieldLength
5859
6387
  }) {
5860
- const headerArray = new Uint8Array(HEADER_SIZE - 4);
6388
+ const headerRecord = createRecordWriter(HEADER_SIZE - 4);
6389
+ const headerArray = headerRecord.array;
5861
6390
  const headerView = getDataView(headerArray);
5862
- setUint16(headerView, 0, version);
5863
- setUint16(headerView, 2, bitFlag);
5864
- setUint16(headerView, 4, compressionMethod);
5865
- const dateArray = new Uint32Array(1);
5866
- const dateView = getDataView(dateArray);
5867
- setUint16(dateView, 0, (((lastModDate.getHours() << 6) | lastModDate.getMinutes()) << 5) | lastModDate.getSeconds() / 2);
5868
- setUint16(dateView, 2, ((((lastModDate.getFullYear() - 1980) << 4) | (lastModDate.getMonth() + 1)) << 5) | lastModDate.getDate());
5869
- const rawLastModDate = dateArray[0];
5870
- setUint32(headerView, 6, rawLastModDate);
6391
+ headerRecord.uint16(version);
6392
+ headerRecord.uint16(bitFlag);
6393
+ headerRecord.uint16(compressionMethod);
6394
+ if (rawLastModDate === UNDEFINED_VALUE) {
6395
+ const dateArray = new Uint32Array(1);
6396
+ const dateView = getDataView(dateArray);
6397
+ setUint16(dateView, 0, (((lastModDate.getHours() << 6) | lastModDate.getMinutes()) << 5) | lastModDate.getSeconds() / 2);
6398
+ setUint16(dateView, 2, ((((lastModDate.getFullYear() - 1980) << 4) | (lastModDate.getMonth() + 1)) << 5) | lastModDate.getDate());
6399
+ rawLastModDate = dateArray[0];
6400
+ }
6401
+ headerRecord.uint32(rawLastModDate);
6402
+ headerRecord.skip(4);
5871
6403
  if (zip64CompressedSize || compressedSize !== UNDEFINED_VALUE) {
5872
- setUint32(headerView, HEADER_OFFSET_COMPRESSED_SIZE, zip64CompressedSize ? MAX_32_BITS : compressedSize);
6404
+ headerRecord.uint32(zip64CompressedSize ? MAX_32_BITS : compressedSize);
6405
+ } else {
6406
+ headerRecord.skip(4);
5873
6407
  }
5874
6408
  if (zip64UncompressedSize || uncompressedSize !== UNDEFINED_VALUE) {
5875
- setUint32(headerView, HEADER_OFFSET_UNCOMPRESSED_SIZE, zip64UncompressedSize ? MAX_32_BITS : uncompressedSize);
6409
+ headerRecord.uint32(zip64UncompressedSize ? MAX_32_BITS : uncompressedSize);
6410
+ } else {
6411
+ headerRecord.skip(4);
5876
6412
  }
5877
- setUint16(headerView, 22, getLength(rawFilename));
5878
- setUint16(headerView, 24, extraFieldLength);
6413
+ headerRecord.uint16(getLength(rawFilename));
6414
+ headerRecord.uint16(extraFieldLength);
5879
6415
  return {
5880
6416
  headerArray,
5881
6417
  headerView,
@@ -5980,6 +6516,7 @@
5980
6516
  exports.BlobWriter = BlobWriter;
5981
6517
  exports.Data64URIReader = Data64URIReader;
5982
6518
  exports.Data64URIWriter = Data64URIWriter;
6519
+ exports.ERR_AMBIGUOUS_ARCHIVE = ERR_AMBIGUOUS_ARCHIVE;
5983
6520
  exports.ERR_BAD_FORMAT = ERR_BAD_FORMAT;
5984
6521
  exports.ERR_CENTRAL_DIRECTORY_NOT_FOUND = ERR_CENTRAL_DIRECTORY_NOT_FOUND;
5985
6522
  exports.ERR_DUPLICATED_NAME = ERR_DUPLICATED_NAME;
@@ -5989,6 +6526,7 @@
5989
6526
  exports.ERR_EXTRAFIELD_ZIP64_NOT_FOUND = ERR_EXTRAFIELD_ZIP64_NOT_FOUND;
5990
6527
  exports.ERR_HTTP_RANGE = ERR_HTTP_RANGE;
5991
6528
  exports.ERR_INVALID_COMMENT = ERR_INVALID_COMMENT;
6529
+ exports.ERR_INVALID_COMPRESSED_DATA = ERR_INVALID_COMPRESSED_DATA;
5992
6530
  exports.ERR_INVALID_ENCRYPTION_STRENGTH = ERR_INVALID_ENCRYPTION_STRENGTH;
5993
6531
  exports.ERR_INVALID_ENTRY_COMMENT = ERR_INVALID_ENTRY_COMMENT;
5994
6532
  exports.ERR_INVALID_ENTRY_NAME = ERR_INVALID_ENTRY_NAME;
@@ -6001,8 +6539,9 @@
6001
6539
  exports.ERR_LOCAL_FILE_HEADER_NOT_FOUND = ERR_LOCAL_FILE_HEADER_NOT_FOUND;
6002
6540
  exports.ERR_OVERLAPPING_ENTRY = ERR_OVERLAPPING_ENTRY;
6003
6541
  exports.ERR_SPLIT_ZIP_FILE = ERR_SPLIT_ZIP_FILE;
6542
+ exports.ERR_UNDEFINED_READER = ERR_UNDEFINED_READER;
6004
6543
  exports.ERR_UNDEFINED_UNCOMPRESSED_SIZE = ERR_UNDEFINED_UNCOMPRESSED_SIZE;
6005
- exports.ERR_UNSUPPORTED_COMPRESSION = ERR_UNSUPPORTED_COMPRESSION;
6544
+ exports.ERR_UNSUPPORTED_COMPRESSION = ERR_UNSUPPORTED_COMPRESSION$1;
6006
6545
  exports.ERR_UNSUPPORTED_ENCRYPTION = ERR_UNSUPPORTED_ENCRYPTION;
6007
6546
  exports.ERR_UNSUPPORTED_FORMAT = ERR_UNSUPPORTED_FORMAT;
6008
6547
  exports.ERR_ZIP_NOT_EMPTY = ERR_ZIP_NOT_EMPTY;