@zip.js/zip.js 2.8.26 → 2.8.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/deno.json +1 -1
- package/dist/zip-core.js +1050 -569
- package/dist/zip-core.min.js +1 -1
- package/dist/zip-fs-core.js +1146 -617
- package/dist/zip-fs-core.min.js +1 -1
- package/dist/zip-fs-native.js +1207 -627
- package/dist/zip-fs-native.min.js +1 -1
- package/dist/zip-fs.js +1279 -681
- package/dist/zip-fs.min.js +1 -1
- package/dist/zip-legacy.js +1051 -570
- package/dist/zip-legacy.min.js +1 -1
- package/dist/zip-module.wasm +0 -0
- package/dist/zip-native.js +1053 -572
- package/dist/zip-native.min.js +1 -1
- package/dist/zip-web-worker-native.js +1 -1
- package/dist/zip-web-worker.js +1 -1
- package/dist/zip.js +1125 -626
- package/dist/zip.min.js +1 -1
- package/eslint.config.mjs +6 -1
- package/index-native.cjs +1207 -627
- package/index-native.min.js +1 -1
- package/index.cjs +1279 -681
- package/index.d.ts +2451 -2355
- package/index.min.js +1 -1
- package/lib/core/codec-pool.js +39 -1
- package/lib/core/codec-worker.js +120 -34
- package/lib/core/configuration.js +3 -0
- package/lib/core/constants.js +6 -0
- package/lib/core/io.js +55 -25
- package/lib/core/options.js +2 -0
- package/lib/core/streams/aes-crypto-stream.js +12 -12
- package/lib/core/streams/codec-stream.js +11 -5
- package/lib/core/streams/codecs/sjcl.js +1 -33
- package/lib/core/streams/common-crypto.js +4 -4
- package/lib/core/streams/zip-crypto-stream.js +25 -15
- package/lib/core/streams/zip-entry-stream.js +35 -1
- package/lib/core/streams/zlib-js/zlib-streams.min.js +1 -1
- package/lib/core/streams/zlib-wasm/zlib-streams.js +73 -55
- package/lib/core/streams/zlib-wasm/zlib-streams.wasm +0 -0
- package/lib/core/util/decode-cp437.js +1 -1
- package/lib/core/util/decode-text.js +2 -1
- package/lib/core/web-worker-base.js +12 -4
- package/lib/core/web-worker-inline-native.js +1 -1
- package/lib/core/web-worker-inline-wasm.js +1 -1
- package/lib/core/zip-fs.js +154 -55
- package/lib/core/zip-reader.js +185 -67
- package/lib/core/zip-writer.js +613 -376
- package/lib/core/zlib-streams-inline.js +1 -1
- package/lib/zip-core-reader.js +3 -1
- package/lib/zip-core-writer.js +1 -0
- 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
|
-
|
|
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,
|
|
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
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1650
|
-
|
|
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 [
|
|
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();
|
|
@@ -1810,6 +1800,37 @@
|
|
|
1810
1800
|
return readable.pipeThrough(transformStream);
|
|
1811
1801
|
}
|
|
1812
1802
|
|
|
1803
|
+
// DecompressionStream implementations can fail with a message-less TypeError on malformed
|
|
1804
|
+
// input; give these errors an identifiable message (errors raised by the other stages of
|
|
1805
|
+
// the pipeline, e.g. ERR_INVALID_PASSWORD, always carry a message and pass through as-is)
|
|
1806
|
+
function mapInflateStreamError(readable) {
|
|
1807
|
+
const reader = readable.getReader();
|
|
1808
|
+
return new ReadableStream({
|
|
1809
|
+
async pull(controller) {
|
|
1810
|
+
let result;
|
|
1811
|
+
try {
|
|
1812
|
+
result = await reader.read();
|
|
1813
|
+
} catch (error) {
|
|
1814
|
+
if (error && error.message) {
|
|
1815
|
+
throw error;
|
|
1816
|
+
}
|
|
1817
|
+
const mappedError = new Error(ERR_INVALID_COMPRESSED_DATA);
|
|
1818
|
+
mappedError.cause = error;
|
|
1819
|
+
throw mappedError;
|
|
1820
|
+
}
|
|
1821
|
+
const { value, done } = result;
|
|
1822
|
+
if (done) {
|
|
1823
|
+
controller.close();
|
|
1824
|
+
} else {
|
|
1825
|
+
controller.enqueue(value);
|
|
1826
|
+
}
|
|
1827
|
+
},
|
|
1828
|
+
cancel(reason) {
|
|
1829
|
+
return reader.cancel(reason);
|
|
1830
|
+
}
|
|
1831
|
+
});
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1813
1834
|
/*
|
|
1814
1835
|
Copyright (c) 2022 Gildas Lormeau. All rights reserved.
|
|
1815
1836
|
|
|
@@ -1839,6 +1860,7 @@
|
|
|
1839
1860
|
*/
|
|
1840
1861
|
|
|
1841
1862
|
|
|
1863
|
+
const DEFAULT_CHUNK_SIZE$1 = 64 * 1024;
|
|
1842
1864
|
const MESSAGE_EVENT_TYPE = "message";
|
|
1843
1865
|
const MESSAGE_START = "start";
|
|
1844
1866
|
const MESSAGE_PULL = "pull";
|
|
@@ -1907,6 +1929,9 @@
|
|
|
1907
1929
|
|
|
1908
1930
|
constructor(chunkSize) {
|
|
1909
1931
|
let pendingChunk;
|
|
1932
|
+
if (!(chunkSize >= 1)) {
|
|
1933
|
+
chunkSize = DEFAULT_CHUNK_SIZE$1;
|
|
1934
|
+
}
|
|
1910
1935
|
super({
|
|
1911
1936
|
transform,
|
|
1912
1937
|
flush(controller) {
|
|
@@ -1924,12 +1949,12 @@
|
|
|
1924
1949
|
chunk = newChunk;
|
|
1925
1950
|
pendingChunk = null;
|
|
1926
1951
|
}
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
pendingChunk = chunk;
|
|
1952
|
+
let offset = 0;
|
|
1953
|
+
while (chunk.length - offset > chunkSize) {
|
|
1954
|
+
controller.enqueue(chunk.slice(offset, offset + chunkSize));
|
|
1955
|
+
offset += chunkSize;
|
|
1932
1956
|
}
|
|
1957
|
+
pendingChunk = offset ? chunk.slice(offset) : chunk;
|
|
1933
1958
|
}
|
|
1934
1959
|
}
|
|
1935
1960
|
}
|
|
@@ -1964,8 +1989,10 @@
|
|
|
1964
1989
|
|
|
1965
1990
|
|
|
1966
1991
|
const MODULE_WORKER_OPTIONS = { type: "module" };
|
|
1992
|
+
const ERROR_EVENT_TYPE = "error";
|
|
1993
|
+
const MESSAGE_ERROR_EVENT_TYPE = "messageerror";
|
|
1967
1994
|
|
|
1968
|
-
let webWorkerSupported, webWorkerURI, webWorkerOptions;
|
|
1995
|
+
let webWorkerSupported, webWorkerSource, webWorkerURI, webWorkerOptions;
|
|
1969
1996
|
let transferStreamsSupported = true;
|
|
1970
1997
|
try {
|
|
1971
1998
|
transferStreamsSupported = typeof structuredClone == FUNCTION_TYPE && structuredClone(new DOMException("", "AbortError")).code !== UNDEFINED_VALUE;
|
|
@@ -1980,8 +2007,9 @@
|
|
|
1980
2007
|
const { signal } = streamOptions;
|
|
1981
2008
|
Object.assign(workerData, {
|
|
1982
2009
|
busy: true,
|
|
2010
|
+
generation: (workerData.generation || 0) + 1,
|
|
1983
2011
|
readable: readable
|
|
1984
|
-
.pipeThrough(new ChunkStream(config
|
|
2012
|
+
.pipeThrough(new ChunkStream(getChunkSize(config)))
|
|
1985
2013
|
.pipeThrough(new ProgressWatcherStream(streamOptions), { signal }),
|
|
1986
2014
|
writable,
|
|
1987
2015
|
options: Object.assign({}, options),
|
|
@@ -2004,15 +2032,17 @@
|
|
|
2004
2032
|
});
|
|
2005
2033
|
},
|
|
2006
2034
|
onTaskFinished() {
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2035
|
+
if (workerData.busy) {
|
|
2036
|
+
const { resolveTerminated } = workerData;
|
|
2037
|
+
if (resolveTerminated) {
|
|
2038
|
+
workerData.resolveTerminated = null;
|
|
2039
|
+
workerData.terminated = true;
|
|
2040
|
+
workerData.worker.terminate();
|
|
2041
|
+
resolveTerminated();
|
|
2042
|
+
}
|
|
2043
|
+
workerData.busy = false;
|
|
2044
|
+
onTaskFinished(workerData);
|
|
2013
2045
|
}
|
|
2014
|
-
workerData.busy = false;
|
|
2015
|
-
onTaskFinished(workerData);
|
|
2016
2046
|
}
|
|
2017
2047
|
});
|
|
2018
2048
|
if (webWorkerSupported === UNDEFINED_VALUE) {
|
|
@@ -2081,6 +2111,7 @@
|
|
|
2081
2111
|
}
|
|
2082
2112
|
Object.assign(workerData, {
|
|
2083
2113
|
worker,
|
|
2114
|
+
terminated: false,
|
|
2084
2115
|
interface: {
|
|
2085
2116
|
run: () => runWebWorker(workerData, { chunkSize, wasmURI, baseURI })
|
|
2086
2117
|
}
|
|
@@ -2135,37 +2166,85 @@
|
|
|
2135
2166
|
result
|
|
2136
2167
|
});
|
|
2137
2168
|
const { readable, options } = workerData;
|
|
2138
|
-
const { writable, closed } = watchClosedStream(workerData.writable);
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2169
|
+
const { writable, closed, abortPipe } = watchClosedStream(workerData.writable);
|
|
2170
|
+
let streamsTransferred;
|
|
2171
|
+
try {
|
|
2172
|
+
streamsTransferred = sendMessage({
|
|
2173
|
+
type: MESSAGE_START,
|
|
2174
|
+
options,
|
|
2175
|
+
config,
|
|
2176
|
+
readable,
|
|
2177
|
+
writable
|
|
2178
|
+
}, workerData);
|
|
2179
|
+
} catch (error) {
|
|
2180
|
+
abortPipe();
|
|
2181
|
+
try {
|
|
2182
|
+
await closed;
|
|
2183
|
+
} catch {
|
|
2184
|
+
// ignored
|
|
2185
|
+
}
|
|
2186
|
+
workerData.onTaskFinished();
|
|
2187
|
+
throw error;
|
|
2188
|
+
}
|
|
2146
2189
|
if (!streamsTransferred) {
|
|
2147
2190
|
Object.assign(workerData, {
|
|
2148
2191
|
reader: readable.getReader(),
|
|
2149
2192
|
writer: writable.getWriter()
|
|
2150
2193
|
});
|
|
2151
2194
|
}
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
await
|
|
2195
|
+
try {
|
|
2196
|
+
const resultValue = await result;
|
|
2197
|
+
await closeWritable();
|
|
2198
|
+
await closed;
|
|
2199
|
+
return resultValue;
|
|
2200
|
+
} catch (error) {
|
|
2201
|
+
await closeWritable();
|
|
2202
|
+
// unblock the pipe watcher if the worker died before closing its end of the stream
|
|
2203
|
+
abortPipe();
|
|
2204
|
+
try {
|
|
2205
|
+
await closed;
|
|
2206
|
+
} catch {
|
|
2207
|
+
// ignored
|
|
2208
|
+
}
|
|
2209
|
+
throw error;
|
|
2210
|
+
}
|
|
2211
|
+
|
|
2212
|
+
async function closeWritable() {
|
|
2213
|
+
if (!streamsTransferred && !writable.locked) {
|
|
2214
|
+
try {
|
|
2215
|
+
await writable.getWriter().close();
|
|
2216
|
+
} catch {
|
|
2217
|
+
// ignored
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2155
2220
|
}
|
|
2156
|
-
await closed;
|
|
2157
|
-
return resultValue;
|
|
2158
2221
|
}
|
|
2159
2222
|
|
|
2160
2223
|
function watchClosedStream(writableSource) {
|
|
2224
|
+
const abortController = new AbortController();
|
|
2161
2225
|
const { writable, readable } = new TransformStream();
|
|
2162
|
-
const closed = readable.pipeTo(writableSource, { preventClose: true });
|
|
2163
|
-
|
|
2226
|
+
const closed = readable.pipeTo(writableSource, { preventClose: true, preventAbort: true, signal: abortController.signal });
|
|
2227
|
+
// the pipe can fail before `closed` is awaited (e.g. the destination write fails);
|
|
2228
|
+
// mark the rejection as handled so it cannot kill the process in the meantime
|
|
2229
|
+
closed.catch(() => { });
|
|
2230
|
+
return { writable, closed, abortPipe: () => abortController.abort() };
|
|
2231
|
+
}
|
|
2232
|
+
|
|
2233
|
+
function terminateWorker$1(workerData) {
|
|
2234
|
+
const { worker } = workerData;
|
|
2235
|
+
if (worker) {
|
|
2236
|
+
try {
|
|
2237
|
+
worker.terminate();
|
|
2238
|
+
} catch {
|
|
2239
|
+
// ignored
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
workerData.interface = null;
|
|
2164
2243
|
}
|
|
2165
2244
|
|
|
2166
2245
|
function getWebWorker(url, baseURI, workerData, isModuleType, useBlobURI = true) {
|
|
2167
2246
|
let worker, resolvedURI, resolvedOptions;
|
|
2168
|
-
if (webWorkerURI === UNDEFINED_VALUE) {
|
|
2247
|
+
if (webWorkerURI === UNDEFINED_VALUE || webWorkerSource !== url) {
|
|
2169
2248
|
// deno-lint-ignore valid-typeof
|
|
2170
2249
|
const isFunctionURI = typeof url == FUNCTION_TYPE;
|
|
2171
2250
|
if (isFunctionURI) {
|
|
@@ -2222,16 +2301,36 @@
|
|
|
2222
2301
|
}
|
|
2223
2302
|
}
|
|
2224
2303
|
}
|
|
2304
|
+
webWorkerSource = url;
|
|
2225
2305
|
webWorkerURI = resolvedURI;
|
|
2226
2306
|
webWorkerOptions = resolvedOptions;
|
|
2227
2307
|
} else {
|
|
2228
2308
|
worker = new Worker(webWorkerURI, webWorkerOptions);
|
|
2229
2309
|
}
|
|
2230
2310
|
worker.addEventListener(MESSAGE_EVENT_TYPE, event => onMessage(event, workerData));
|
|
2311
|
+
worker.addEventListener(ERROR_EVENT_TYPE, event => onWorkerError(event, workerData));
|
|
2312
|
+
worker.addEventListener(MESSAGE_ERROR_EVENT_TYPE, event => onWorkerError(event, workerData));
|
|
2231
2313
|
return worker;
|
|
2232
2314
|
}
|
|
2233
2315
|
|
|
2234
|
-
function
|
|
2316
|
+
function onWorkerError(event, workerData) {
|
|
2317
|
+
if (event.preventDefault) {
|
|
2318
|
+
event.preventDefault();
|
|
2319
|
+
}
|
|
2320
|
+
const { rejectResult, writer, onTaskFinished } = workerData;
|
|
2321
|
+
// the worker is unusable (e.g. its script failed to load); terminate it and
|
|
2322
|
+
// fail the current task instead of letting it hang forever
|
|
2323
|
+
terminateWorker$1(workerData);
|
|
2324
|
+
if (rejectResult) {
|
|
2325
|
+
rejectResult(event.error || new Error(event.message || ERROR_EVENT_TYPE));
|
|
2326
|
+
if (writer) {
|
|
2327
|
+
writer.releaseLock();
|
|
2328
|
+
}
|
|
2329
|
+
onTaskFinished();
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
|
|
2333
|
+
function sendMessage(message, { worker, writer, transferStreams }) {
|
|
2235
2334
|
try {
|
|
2236
2335
|
const { value, readable, writable } = message;
|
|
2237
2336
|
const transferables = [];
|
|
@@ -2265,14 +2364,16 @@
|
|
|
2265
2364
|
if (writer) {
|
|
2266
2365
|
writer.releaseLock();
|
|
2267
2366
|
}
|
|
2268
|
-
onTaskFinished();
|
|
2269
2367
|
throw error;
|
|
2270
2368
|
}
|
|
2271
2369
|
}
|
|
2272
2370
|
|
|
2273
2371
|
async function onMessage({ data }, workerData) {
|
|
2274
2372
|
const { type, value, messageId, result, error } = data;
|
|
2275
|
-
const { reader, writer, resolveResult, rejectResult, onTaskFinished } = workerData;
|
|
2373
|
+
const { reader, writer, resolveResult, rejectResult, onTaskFinished, generation } = workerData;
|
|
2374
|
+
// continuations below can settle after the worker slot has been reassigned to a new
|
|
2375
|
+
// task (e.g. a pending pull read failing late); they must not touch the new task
|
|
2376
|
+
const stale = () => workerData.generation != generation;
|
|
2276
2377
|
try {
|
|
2277
2378
|
if (error) {
|
|
2278
2379
|
const { message, stack, code, name, outputSize } = error;
|
|
@@ -2282,23 +2383,32 @@
|
|
|
2282
2383
|
} else {
|
|
2283
2384
|
if (type == MESSAGE_PULL) {
|
|
2284
2385
|
const { value, done } = await reader.read();
|
|
2285
|
-
|
|
2386
|
+
if (!stale()) {
|
|
2387
|
+
sendMessage({ type: MESSAGE_DATA, value, done, messageId }, workerData);
|
|
2388
|
+
}
|
|
2286
2389
|
}
|
|
2287
2390
|
if (type == MESSAGE_DATA) {
|
|
2288
2391
|
await writer.ready;
|
|
2289
2392
|
await writer.write(new Uint8Array(value));
|
|
2290
|
-
|
|
2393
|
+
if (!stale()) {
|
|
2394
|
+
sendMessage({ type: MESSAGE_ACK_DATA, messageId }, workerData);
|
|
2395
|
+
}
|
|
2291
2396
|
}
|
|
2292
2397
|
if (type == MESSAGE_CLOSE) {
|
|
2293
2398
|
close(null, result);
|
|
2294
2399
|
}
|
|
2295
2400
|
}
|
|
2296
2401
|
} catch (error) {
|
|
2297
|
-
|
|
2298
|
-
|
|
2402
|
+
if (!stale()) {
|
|
2403
|
+
terminateWorker$1(workerData);
|
|
2404
|
+
close(error);
|
|
2405
|
+
}
|
|
2299
2406
|
}
|
|
2300
2407
|
|
|
2301
2408
|
function close(error, result) {
|
|
2409
|
+
if (stale()) {
|
|
2410
|
+
return;
|
|
2411
|
+
}
|
|
2302
2412
|
if (error) {
|
|
2303
2413
|
rejectResult(error);
|
|
2304
2414
|
} else {
|
|
@@ -2342,6 +2452,8 @@
|
|
|
2342
2452
|
|
|
2343
2453
|
let pool = [];
|
|
2344
2454
|
const pendingRequests = [];
|
|
2455
|
+
let starvationTimeout;
|
|
2456
|
+
let starvationDelay;
|
|
2345
2457
|
|
|
2346
2458
|
let indexWorker = 0;
|
|
2347
2459
|
|
|
@@ -2368,14 +2480,20 @@
|
|
|
2368
2480
|
pool.push(workerData);
|
|
2369
2481
|
return new CodecWorker(workerData, stream, workerOptions, onTaskFinished);
|
|
2370
2482
|
} else {
|
|
2371
|
-
return new Promise(resolve =>
|
|
2483
|
+
return new Promise(resolve => {
|
|
2484
|
+
pendingRequests.push({ resolve, stream, workerOptions });
|
|
2485
|
+
starvationDelay = config.workerStarvationTimeout;
|
|
2486
|
+
armStarvationTimeout();
|
|
2487
|
+
});
|
|
2372
2488
|
}
|
|
2373
2489
|
}
|
|
2374
2490
|
|
|
2375
2491
|
function onTaskFinished(workerData) {
|
|
2492
|
+
clearStarvationTimeout();
|
|
2376
2493
|
if (pendingRequests.length) {
|
|
2377
2494
|
const [{ resolve, stream, workerOptions }] = pendingRequests.splice(0, 1);
|
|
2378
2495
|
resolve(new CodecWorker(workerData, stream, workerOptions, onTaskFinished));
|
|
2496
|
+
armStarvationTimeout();
|
|
2379
2497
|
} else if (workerData.worker) {
|
|
2380
2498
|
clearTerminateTimeout(workerData);
|
|
2381
2499
|
terminateWorker(workerData, workerOptions);
|
|
@@ -2385,6 +2503,34 @@
|
|
|
2385
2503
|
}
|
|
2386
2504
|
}
|
|
2387
2505
|
|
|
2506
|
+
function armStarvationTimeout() {
|
|
2507
|
+
if (!starvationTimeout && pendingRequests.length && Number.isFinite(starvationDelay) && starvationDelay >= 0) {
|
|
2508
|
+
starvationTimeout = setTimeout(onWorkerStarvation, starvationDelay);
|
|
2509
|
+
}
|
|
2510
|
+
}
|
|
2511
|
+
|
|
2512
|
+
function clearStarvationTimeout() {
|
|
2513
|
+
if (starvationTimeout) {
|
|
2514
|
+
clearTimeout(starvationTimeout);
|
|
2515
|
+
starvationTimeout = null;
|
|
2516
|
+
}
|
|
2517
|
+
}
|
|
2518
|
+
|
|
2519
|
+
function onWorkerStarvation() {
|
|
2520
|
+
starvationTimeout = null;
|
|
2521
|
+
if (pendingRequests.length) {
|
|
2522
|
+
const [{ resolve, stream, workerOptions }] = pendingRequests.splice(0, 1);
|
|
2523
|
+
const inlineWorkerOptions = Object.assign({}, workerOptions, { useWebWorkers: false, workerURI: UNDEFINED_VALUE });
|
|
2524
|
+
resolve(new CodecWorker({}, stream, inlineWorkerOptions, onInlineTaskFinished));
|
|
2525
|
+
armStarvationTimeout();
|
|
2526
|
+
}
|
|
2527
|
+
}
|
|
2528
|
+
|
|
2529
|
+
function onInlineTaskFinished() {
|
|
2530
|
+
clearStarvationTimeout();
|
|
2531
|
+
armStarvationTimeout();
|
|
2532
|
+
}
|
|
2533
|
+
|
|
2388
2534
|
function terminateWorker(workerData, workerOptions) {
|
|
2389
2535
|
const { config } = workerOptions;
|
|
2390
2536
|
const { terminateWorkerTimeout } = config;
|
|
@@ -2455,6 +2601,7 @@
|
|
|
2455
2601
|
|
|
2456
2602
|
const CONTENT_TYPE_TEXT_PLAIN = "text/plain";
|
|
2457
2603
|
const HTTP_HEADER_CONTENT_LENGTH = "Content-Length";
|
|
2604
|
+
const HTTP_HEADER_CONTENT_ENCODING = "Content-Encoding";
|
|
2458
2605
|
const HTTP_HEADER_CONTENT_RANGE = "Content-Range";
|
|
2459
2606
|
const HTTP_HEADER_ACCEPT_RANGES = "Accept-Ranges";
|
|
2460
2607
|
const HTTP_HEADER_RANGE = "Range";
|
|
@@ -2579,7 +2726,7 @@
|
|
|
2579
2726
|
|
|
2580
2727
|
writeUint8Array(array) {
|
|
2581
2728
|
const writer = this;
|
|
2582
|
-
let indexArray
|
|
2729
|
+
let indexArray;
|
|
2583
2730
|
let dataString = writer.pending;
|
|
2584
2731
|
const delta = writer.pending.length;
|
|
2585
2732
|
writer.pending = "";
|
|
@@ -2589,12 +2736,10 @@
|
|
|
2589
2736
|
for (; indexArray < array.length; indexArray++) {
|
|
2590
2737
|
writer.pending += String.fromCharCode(array[indexArray]);
|
|
2591
2738
|
}
|
|
2592
|
-
if (dataString.length) {
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
writer.pending += dataString;
|
|
2597
|
-
}
|
|
2739
|
+
if (dataString.length > 2) {
|
|
2740
|
+
writer.data += btoa(dataString);
|
|
2741
|
+
} else {
|
|
2742
|
+
writer.pending = dataString + writer.pending;
|
|
2598
2743
|
}
|
|
2599
2744
|
}
|
|
2600
2745
|
|
|
@@ -2641,6 +2786,9 @@
|
|
|
2641
2786
|
}
|
|
2642
2787
|
});
|
|
2643
2788
|
writer.blob = new Response(transformStream.readable, { headers }).blob();
|
|
2789
|
+
// mark early rejections (e.g. aborted pipe) as handled; awaiting getData() still
|
|
2790
|
+
// surfaces them
|
|
2791
|
+
writer.blob.catch(() => { });
|
|
2644
2792
|
}
|
|
2645
2793
|
|
|
2646
2794
|
getData() {
|
|
@@ -2725,7 +2873,8 @@
|
|
|
2725
2873
|
preventHeadRequest,
|
|
2726
2874
|
useRangeHeader,
|
|
2727
2875
|
forceRangeRequests,
|
|
2728
|
-
combineSizeEocd
|
|
2876
|
+
combineSizeEocd,
|
|
2877
|
+
fetch
|
|
2729
2878
|
} = options;
|
|
2730
2879
|
options = Object.assign({}, options);
|
|
2731
2880
|
delete options.preventHeadRequest;
|
|
@@ -2733,13 +2882,15 @@
|
|
|
2733
2882
|
delete options.forceRangeRequests;
|
|
2734
2883
|
delete options.combineSizeEocd;
|
|
2735
2884
|
delete options.useXHR;
|
|
2885
|
+
delete options.fetch;
|
|
2736
2886
|
Object.assign(httpReader, {
|
|
2737
2887
|
url,
|
|
2738
2888
|
options,
|
|
2739
2889
|
preventHeadRequest,
|
|
2740
2890
|
useRangeHeader,
|
|
2741
2891
|
forceRangeRequests,
|
|
2742
|
-
combineSizeEocd
|
|
2892
|
+
combineSizeEocd,
|
|
2893
|
+
fetch
|
|
2743
2894
|
});
|
|
2744
2895
|
}
|
|
2745
2896
|
|
|
@@ -2758,7 +2909,10 @@
|
|
|
2758
2909
|
throw new Error(ERR_HTTP_RANGE);
|
|
2759
2910
|
} else {
|
|
2760
2911
|
if (combineSizeEocd) {
|
|
2761
|
-
|
|
2912
|
+
const eocdCache = new Uint8Array(await response.arrayBuffer());
|
|
2913
|
+
if (response.status == 206 && eocdCache.length == END_OF_CENTRAL_DIR_LENGTH) {
|
|
2914
|
+
httpReader.eocdCache = eocdCache;
|
|
2915
|
+
}
|
|
2762
2916
|
}
|
|
2763
2917
|
let contentSize;
|
|
2764
2918
|
const contentRangeHeader = response.headers.get(HTTP_HEADER_CONTENT_RANGE);
|
|
@@ -2804,7 +2958,18 @@
|
|
|
2804
2958
|
if (response.status != 206) {
|
|
2805
2959
|
throw new Error(ERR_HTTP_RANGE);
|
|
2806
2960
|
}
|
|
2807
|
-
|
|
2961
|
+
const contentRangeHeader = response.headers.get(HTTP_HEADER_CONTENT_RANGE);
|
|
2962
|
+
if (contentRangeHeader) {
|
|
2963
|
+
const rangeStart = Number(contentRangeHeader.trim().split(/[\s-]+/)[1]);
|
|
2964
|
+
if (!Number.isNaN(rangeStart) && rangeStart != index) {
|
|
2965
|
+
throw new Error(ERR_HTTP_RANGE);
|
|
2966
|
+
}
|
|
2967
|
+
}
|
|
2968
|
+
const data = new Uint8Array(await response.arrayBuffer());
|
|
2969
|
+
if (data.length != length) {
|
|
2970
|
+
throw new Error(ERR_HTTP_RANGE);
|
|
2971
|
+
}
|
|
2972
|
+
return data;
|
|
2808
2973
|
}
|
|
2809
2974
|
} else {
|
|
2810
2975
|
const { data } = httpReader;
|
|
@@ -2841,9 +3006,9 @@
|
|
|
2841
3006
|
async function getRequestData(httpReader, sendRequest) {
|
|
2842
3007
|
const response = await sendRequest(HTTP_METHOD_GET, httpReader, getHeaders(httpReader));
|
|
2843
3008
|
httpReader.data = new Uint8Array(await response.arrayBuffer());
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
3009
|
+
// the downloaded content is authoritative, e.g. the Content-Length response header
|
|
3010
|
+
// of the HEAD request can be the encoded size when the content is compressed
|
|
3011
|
+
httpReader.size = httpReader.data.length;
|
|
2847
3012
|
}
|
|
2848
3013
|
|
|
2849
3014
|
async function getContentLength(httpReader, sendRequest, getRequestData) {
|
|
@@ -2852,7 +3017,7 @@
|
|
|
2852
3017
|
} else {
|
|
2853
3018
|
const response = await sendRequest(HTTP_METHOD_HEAD, httpReader, getHeaders(httpReader));
|
|
2854
3019
|
const contentLength = response.headers.get(HTTP_HEADER_CONTENT_LENGTH);
|
|
2855
|
-
if (contentLength) {
|
|
3020
|
+
if (contentLength && !response.headers.get(HTTP_HEADER_CONTENT_ENCODING)) {
|
|
2856
3021
|
httpReader.size = Number(contentLength);
|
|
2857
3022
|
} else {
|
|
2858
3023
|
await getRequestData(httpReader, httpReader.options);
|
|
@@ -2860,8 +3025,8 @@
|
|
|
2860
3025
|
}
|
|
2861
3026
|
}
|
|
2862
3027
|
|
|
2863
|
-
async function sendFetchRequest(method, { options, url }, headers) {
|
|
2864
|
-
const response = await
|
|
3028
|
+
async function sendFetchRequest(method, { fetch: fetchFunction = fetch, options, url }, headers) {
|
|
3029
|
+
const response = await fetchFunction(url, Object.assign({}, options, { method, headers }));
|
|
2865
3030
|
if (response.status < 400) {
|
|
2866
3031
|
return response;
|
|
2867
3032
|
} else {
|
|
@@ -2907,7 +3072,8 @@
|
|
|
2907
3072
|
super();
|
|
2908
3073
|
Object.assign(this, {
|
|
2909
3074
|
url,
|
|
2910
|
-
|
|
3075
|
+
// an explicit fetch implementation takes precedence over useXHR
|
|
3076
|
+
reader: options.useXHR && !options.fetch ? new XHRReader(url, options) : new FetchReader(url, options)
|
|
2911
3077
|
});
|
|
2912
3078
|
}
|
|
2913
3079
|
|
|
@@ -2932,8 +3098,7 @@
|
|
|
2932
3098
|
class HttpRangeReader extends HttpReader {
|
|
2933
3099
|
|
|
2934
3100
|
constructor(url, options = {}) {
|
|
2935
|
-
options
|
|
2936
|
-
super(url, options);
|
|
3101
|
+
super(url, Object.assign({}, options, { useRangeHeader: true }));
|
|
2937
3102
|
}
|
|
2938
3103
|
}
|
|
2939
3104
|
|
|
@@ -3007,7 +3172,7 @@
|
|
|
3007
3172
|
reader.lastDiskNumber = 0;
|
|
3008
3173
|
reader.lastDiskOffset = 0;
|
|
3009
3174
|
await Promise.all(readers.map(async (diskReader, indexDiskReader) => {
|
|
3010
|
-
await diskReader
|
|
3175
|
+
await initStream(diskReader);
|
|
3011
3176
|
if (indexDiskReader != readers.length - 1) {
|
|
3012
3177
|
reader.lastDiskOffset += diskReader.size;
|
|
3013
3178
|
}
|
|
@@ -3091,14 +3256,26 @@
|
|
|
3091
3256
|
writer.diskOffset += diskSourceWriter.size;
|
|
3092
3257
|
writer.diskNumber++;
|
|
3093
3258
|
diskWriter = null;
|
|
3094
|
-
|
|
3259
|
+
// the next disk is not opened yet but its space is available; leaving a
|
|
3260
|
+
// stale value here would force a new disk for every subsequent write
|
|
3261
|
+
writer.availableSize = writer.maxSize;
|
|
3262
|
+
if (chunk.length > availableSize) {
|
|
3263
|
+
await this.write(chunk.subarray(availableSize));
|
|
3264
|
+
}
|
|
3095
3265
|
} else {
|
|
3096
3266
|
await writeChunk(chunk);
|
|
3097
3267
|
}
|
|
3098
3268
|
},
|
|
3099
3269
|
async close() {
|
|
3100
|
-
|
|
3101
|
-
|
|
3270
|
+
if (diskWriter) {
|
|
3271
|
+
await diskWriter.ready;
|
|
3272
|
+
await closeDisk();
|
|
3273
|
+
}
|
|
3274
|
+
},
|
|
3275
|
+
async abort(reason) {
|
|
3276
|
+
if (diskWriter) {
|
|
3277
|
+
await diskWriter.abort(reason);
|
|
3278
|
+
}
|
|
3102
3279
|
}
|
|
3103
3280
|
});
|
|
3104
3281
|
Object.defineProperty(writer, PROPERTY_NAME_WRITABLE, {
|
|
@@ -3113,7 +3290,6 @@
|
|
|
3113
3290
|
await diskWriter.ready;
|
|
3114
3291
|
await diskWriter.write(chunk);
|
|
3115
3292
|
diskSourceWriter.size += chunkLength;
|
|
3116
|
-
writer.size += chunkLength;
|
|
3117
3293
|
writer.availableSize -= chunkLength;
|
|
3118
3294
|
}
|
|
3119
3295
|
}
|
|
@@ -3213,7 +3389,7 @@
|
|
|
3213
3389
|
|
|
3214
3390
|
/* global TextDecoder */
|
|
3215
3391
|
|
|
3216
|
-
const CP437 = "\0☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■
|
|
3392
|
+
const CP437 = "\0☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ".split("");
|
|
3217
3393
|
const VALID_CP437 = CP437.length == 256;
|
|
3218
3394
|
|
|
3219
3395
|
function decodeCP437(stringValue) {
|
|
@@ -3261,7 +3437,8 @@
|
|
|
3261
3437
|
if (encoding && encoding.trim().toLowerCase() == "cp437") {
|
|
3262
3438
|
return decodeCP437(value);
|
|
3263
3439
|
} else {
|
|
3264
|
-
|
|
3440
|
+
// ignoreBOM keeps a leading U+FEFF in filenames/comments instead of dropping it
|
|
3441
|
+
return new TextDecoder(encoding, { ignoreBOM: true }).decode(value);
|
|
3265
3442
|
}
|
|
3266
3443
|
}
|
|
3267
3444
|
|
|
@@ -3438,6 +3615,7 @@
|
|
|
3438
3615
|
const OPTION_CHECK_PASSWORD_ONLY = "checkPasswordOnly";
|
|
3439
3616
|
const OPTION_CHECK_OVERLAPPING_ENTRY_ONLY = "checkOverlappingEntryOnly";
|
|
3440
3617
|
const OPTION_CHECK_OVERLAPPING_ENTRY = "checkOverlappingEntry";
|
|
3618
|
+
const OPTION_CHECK_AMBIGUITY = "checkAmbiguity";
|
|
3441
3619
|
const OPTION_CHECK_SIGNATURE = "checkSignature";
|
|
3442
3620
|
const OPTION_USE_WEB_WORKERS = "useWebWorkers";
|
|
3443
3621
|
const OPTION_USE_COMPRESSION_STREAM = "useCompressionStream";
|
|
@@ -3495,12 +3673,16 @@
|
|
|
3495
3673
|
const ERR_EXTRAFIELD_ZIP64_NOT_FOUND = "Zip64 extra field not found";
|
|
3496
3674
|
const ERR_ENCRYPTED = "File contains encrypted entry";
|
|
3497
3675
|
const ERR_UNSUPPORTED_ENCRYPTION = "Encryption method not supported";
|
|
3498
|
-
const ERR_UNSUPPORTED_COMPRESSION = "Compression method not supported";
|
|
3676
|
+
const ERR_UNSUPPORTED_COMPRESSION$1 = "Compression method not supported";
|
|
3499
3677
|
const ERR_SPLIT_ZIP_FILE = "Split zip file";
|
|
3500
3678
|
const ERR_OVERLAPPING_ENTRY = "Overlapping entry found";
|
|
3679
|
+
const ERR_AMBIGUOUS_ARCHIVE = "Ambiguous archive";
|
|
3501
3680
|
const CHARSET_UTF8 = "utf-8";
|
|
3502
3681
|
const PROPERTY_NAME_UTF8_SUFFIX = "UTF8";
|
|
3503
3682
|
const CHARSET_CP437 = "cp437";
|
|
3683
|
+
// bits of the general purpose flag that change how an entry is interpreted; the level bits (1-2) are excluded
|
|
3684
|
+
// because they are informative only
|
|
3685
|
+
const BITFLAG_AMBIGUITY_MASK = BITFLAG_ENCRYPTED | BITFLAG_DATA_DESCRIPTOR | BITFLAG_LANG_ENCODING_FLAG;
|
|
3504
3686
|
const ZIP64_PROPERTIES = [
|
|
3505
3687
|
[PROPERTY_NAME_UNCOMPRESSED_SIZE, MAX_32_BITS],
|
|
3506
3688
|
[PROPERTY_NAME_COMPRESSED_SIZE, MAX_32_BITS],
|
|
@@ -3525,7 +3707,7 @@
|
|
|
3525
3707
|
reader: new GenericReader(reader),
|
|
3526
3708
|
options,
|
|
3527
3709
|
config: getConfiguration(),
|
|
3528
|
-
readRanges:
|
|
3710
|
+
readRanges: new Map()
|
|
3529
3711
|
});
|
|
3530
3712
|
}
|
|
3531
3713
|
|
|
@@ -3558,21 +3740,30 @@
|
|
|
3558
3740
|
const commentOffset = endOfDirectoryInfo.offset;
|
|
3559
3741
|
const commentLength = getUint16(endOfDirectoryView, 20);
|
|
3560
3742
|
const appendedDataOffset = commentOffset + END_OF_CENTRAL_DIR_LENGTH + commentLength;
|
|
3743
|
+
const checkAmbiguity = getOptionValue$1(zipReader, options, OPTION_CHECK_AMBIGUITY);
|
|
3744
|
+
if (checkAmbiguity && appendedDataOffset != reader.size) {
|
|
3745
|
+
throwAmbiguousArchive("appended data");
|
|
3746
|
+
}
|
|
3561
3747
|
let lastDiskNumber = getUint16(endOfDirectoryView, 4);
|
|
3562
3748
|
const expectedLastDiskNumber = reader.lastDiskNumber || 0;
|
|
3563
3749
|
let diskNumber = getUint16(endOfDirectoryView, 6);
|
|
3564
|
-
let filesLength = getUint16(endOfDirectoryView,
|
|
3750
|
+
let filesLength = getUint16(endOfDirectoryView, 10);
|
|
3565
3751
|
let prependedDataLength = 0;
|
|
3566
|
-
let startOffset
|
|
3752
|
+
let startOffset;
|
|
3753
|
+
let zip64EndOfDirectory;
|
|
3567
3754
|
if (directoryDataOffset == MAX_32_BITS || directoryDataLength == MAX_32_BITS || filesLength == MAX_16_BITS || diskNumber == MAX_16_BITS) {
|
|
3568
|
-
const endOfDirectoryLocatorArray =
|
|
3755
|
+
const endOfDirectoryLocatorArray = endOfDirectoryInfo.offset >= ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH ?
|
|
3756
|
+
await readUint8Array(reader, endOfDirectoryInfo.offset - ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH, ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH) :
|
|
3757
|
+
new Uint8Array();
|
|
3569
3758
|
const endOfDirectoryLocatorView = getDataView$1(endOfDirectoryLocatorArray);
|
|
3570
|
-
if (
|
|
3759
|
+
if (endOfDirectoryLocatorArray.length == ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH &&
|
|
3760
|
+
getUint32(endOfDirectoryLocatorView, 0) == ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE) {
|
|
3571
3761
|
directoryDataOffset = getBigUint64(endOfDirectoryLocatorView, 8);
|
|
3572
3762
|
let endOfDirectoryArray = await readUint8Array(reader, directoryDataOffset, ZIP64_END_OF_CENTRAL_DIR_LENGTH, -1);
|
|
3573
3763
|
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 &&
|
|
3764
|
+
const expectedDirectoryDataOffset = endOfDirectoryInfo.offset - ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH - ZIP64_END_OF_CENTRAL_DIR_LENGTH - (reader.lastDiskOffset || 0);
|
|
3765
|
+
if ((endOfDirectoryArray.length < ZIP64_END_OF_CENTRAL_DIR_LENGTH || getUint32(endOfDirectoryView, 0) != ZIP64_END_OF_CENTRAL_DIR_SIGNATURE) &&
|
|
3766
|
+
directoryDataOffset != expectedDirectoryDataOffset && expectedDirectoryDataOffset >= 0) {
|
|
3576
3767
|
const originalDirectoryDataOffset = directoryDataOffset;
|
|
3577
3768
|
directoryDataOffset = expectedDirectoryDataOffset;
|
|
3578
3769
|
if (directoryDataOffset > originalDirectoryDataOffset) {
|
|
@@ -3581,24 +3772,36 @@
|
|
|
3581
3772
|
endOfDirectoryArray = await readUint8Array(reader, directoryDataOffset, ZIP64_END_OF_CENTRAL_DIR_LENGTH, -1);
|
|
3582
3773
|
endOfDirectoryView = getDataView$1(endOfDirectoryArray);
|
|
3583
3774
|
}
|
|
3584
|
-
if (getUint32(endOfDirectoryView, 0) != ZIP64_END_OF_CENTRAL_DIR_SIGNATURE) {
|
|
3775
|
+
if (endOfDirectoryArray.length < ZIP64_END_OF_CENTRAL_DIR_LENGTH || getUint32(endOfDirectoryView, 0) != ZIP64_END_OF_CENTRAL_DIR_SIGNATURE) {
|
|
3585
3776
|
throw new Error(ERR_EOCDR_LOCATOR_ZIP64_NOT_FOUND);
|
|
3586
3777
|
}
|
|
3778
|
+
zip64EndOfDirectory = true;
|
|
3587
3779
|
if (lastDiskNumber == MAX_16_BITS) {
|
|
3588
3780
|
lastDiskNumber = getUint32(endOfDirectoryView, 16);
|
|
3781
|
+
} else if (checkAmbiguity && lastDiskNumber != getUint32(endOfDirectoryView, 16)) {
|
|
3782
|
+
throwAmbiguousArchive("mismatched zip64 end of central directory record");
|
|
3589
3783
|
}
|
|
3590
3784
|
if (diskNumber == MAX_16_BITS) {
|
|
3591
3785
|
diskNumber = getUint32(endOfDirectoryView, 20);
|
|
3786
|
+
} else if (checkAmbiguity && diskNumber != getUint32(endOfDirectoryView, 20)) {
|
|
3787
|
+
throwAmbiguousArchive("mismatched zip64 end of central directory record");
|
|
3592
3788
|
}
|
|
3593
3789
|
if (filesLength == MAX_16_BITS) {
|
|
3594
3790
|
filesLength = getBigUint64(endOfDirectoryView, 32);
|
|
3791
|
+
} else if (checkAmbiguity && filesLength != getBigUint64(endOfDirectoryView, 32)) {
|
|
3792
|
+
throwAmbiguousArchive("mismatched zip64 end of central directory record");
|
|
3595
3793
|
}
|
|
3596
3794
|
if (directoryDataLength == MAX_32_BITS) {
|
|
3597
3795
|
directoryDataLength = getBigUint64(endOfDirectoryView, 40);
|
|
3796
|
+
} else if (checkAmbiguity && directoryDataLength != getBigUint64(endOfDirectoryView, 40)) {
|
|
3797
|
+
throwAmbiguousArchive("mismatched zip64 end of central directory record");
|
|
3598
3798
|
}
|
|
3599
|
-
directoryDataOffset
|
|
3799
|
+
directoryDataOffset = getBigUint64(endOfDirectoryView, 48) + prependedDataLength;
|
|
3600
3800
|
}
|
|
3601
3801
|
}
|
|
3802
|
+
const declaredDirectoryDataLength = directoryDataLength;
|
|
3803
|
+
const centralDirectoryEndOffset = endOfDirectoryInfo.offset -
|
|
3804
|
+
(zip64EndOfDirectory ? ZIP64_END_OF_CENTRAL_DIR_LENGTH + ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH : 0);
|
|
3602
3805
|
if (directoryDataOffset >= reader.size) {
|
|
3603
3806
|
prependedDataLength = reader.size - directoryDataOffset - directoryDataLength - END_OF_CENTRAL_DIR_LENGTH;
|
|
3604
3807
|
directoryDataOffset = reader.size - directoryDataLength - END_OF_CENTRAL_DIR_LENGTH;
|
|
@@ -3613,8 +3816,11 @@
|
|
|
3613
3816
|
let directoryArray = await readUint8Array(reader, directoryDataOffset, directoryDataLength, diskNumber);
|
|
3614
3817
|
let directoryView = getDataView$1(directoryArray);
|
|
3615
3818
|
if (directoryDataLength) {
|
|
3616
|
-
|
|
3617
|
-
|
|
3819
|
+
if (directoryArray.length < 4) {
|
|
3820
|
+
throw new Error(ERR_BAD_FORMAT);
|
|
3821
|
+
}
|
|
3822
|
+
const expectedDirectoryDataOffset = centralDirectoryEndOffset - directoryDataLength - (reader.lastDiskOffset || 0);
|
|
3823
|
+
if (getUint32(directoryView, offset) != CENTRAL_FILE_HEADER_SIGNATURE && directoryDataOffset != expectedDirectoryDataOffset && diskNumber == lastDiskNumber) {
|
|
3618
3824
|
const originalDirectoryDataOffset = directoryDataOffset;
|
|
3619
3825
|
directoryDataOffset = expectedDirectoryDataOffset;
|
|
3620
3826
|
if (directoryDataOffset > originalDirectoryDataOffset) {
|
|
@@ -3624,8 +3830,8 @@
|
|
|
3624
3830
|
directoryView = getDataView$1(directoryArray);
|
|
3625
3831
|
}
|
|
3626
3832
|
}
|
|
3627
|
-
const expectedDirectoryDataLength =
|
|
3628
|
-
if (directoryDataLength != expectedDirectoryDataLength && expectedDirectoryDataLength >= 0) {
|
|
3833
|
+
const expectedDirectoryDataLength = centralDirectoryEndOffset - directoryDataOffset - (reader.lastDiskOffset || 0);
|
|
3834
|
+
if (directoryDataLength != expectedDirectoryDataLength && expectedDirectoryDataLength >= 0 && diskNumber == lastDiskNumber) {
|
|
3629
3835
|
directoryDataLength = expectedDirectoryDataLength;
|
|
3630
3836
|
directoryArray = await readUint8Array(reader, directoryDataOffset, directoryDataLength, diskNumber);
|
|
3631
3837
|
directoryView = getDataView$1(directoryArray);
|
|
@@ -3633,16 +3839,19 @@
|
|
|
3633
3839
|
if (directoryDataOffset < 0 || directoryDataOffset >= reader.size) {
|
|
3634
3840
|
throw new Error(ERR_BAD_FORMAT);
|
|
3635
3841
|
}
|
|
3842
|
+
startOffset = directoryDataOffset;
|
|
3636
3843
|
const filenameEncoding = getOptionValue$1(zipReader, options, OPTION_FILENAME_ENCODING);
|
|
3637
3844
|
const commentEncoding = getOptionValue$1(zipReader, options, OPTION_COMMENT_ENCODING);
|
|
3845
|
+
const filenames = checkAmbiguity ? new Set() : UNDEFINED_VALUE;
|
|
3846
|
+
let duplicateFilename;
|
|
3638
3847
|
for (let indexFile = 0; indexFile < filesLength; indexFile++) {
|
|
3639
3848
|
const fileEntry = new ZipEntry(reader, config, zipReader.options);
|
|
3640
|
-
if (getUint32(directoryView, offset) != CENTRAL_FILE_HEADER_SIGNATURE) {
|
|
3849
|
+
if (offset + CENTRAL_FILE_HEADER_LENGTH > directoryArray.length || getUint32(directoryView, offset) != CENTRAL_FILE_HEADER_SIGNATURE) {
|
|
3641
3850
|
throw new Error(ERR_CENTRAL_DIRECTORY_NOT_FOUND);
|
|
3642
3851
|
}
|
|
3643
3852
|
readCommonHeader(fileEntry, directoryView, offset + 6);
|
|
3644
3853
|
const languageEncodingFlag = Boolean(fileEntry.bitFlag.languageEncodingFlag);
|
|
3645
|
-
const filenameOffset = offset +
|
|
3854
|
+
const filenameOffset = offset + CENTRAL_FILE_HEADER_LENGTH;
|
|
3646
3855
|
const extraFieldOffset = filenameOffset + fileEntry.filenameLength;
|
|
3647
3856
|
const commentOffset = extraFieldOffset + fileEntry.extraFieldLength;
|
|
3648
3857
|
const versionMadeBy = getUint16(directoryView, offset + 4);
|
|
@@ -3663,7 +3872,7 @@
|
|
|
3663
3872
|
directory: Boolean(msdosAttributesRaw & FILE_ATTR_MSDOS_DIR_MASK),
|
|
3664
3873
|
archive: Boolean(msdosAttributesRaw & FILE_ATTR_MSDOS_ARCHIVE_MASK)
|
|
3665
3874
|
};
|
|
3666
|
-
const offsetFileEntry = getUint32(directoryView, offset + 42)
|
|
3875
|
+
const offsetFileEntry = getUint32(directoryView, offset + 42);
|
|
3667
3876
|
const decode = getOptionValue$1(zipReader, options, OPTION_DECODE_TEXT) || decodeText;
|
|
3668
3877
|
const rawFilenameEncoding = filenameUTF8 ? CHARSET_UTF8 : filenameEncoding || CHARSET_CP437;
|
|
3669
3878
|
const rawCommentEncoding = commentUTF8 ? CHARSET_UTF8 : commentEncoding || CHARSET_CP437;
|
|
@@ -3676,6 +3885,7 @@
|
|
|
3676
3885
|
comment = decodeText(rawComment, rawCommentEncoding);
|
|
3677
3886
|
}
|
|
3678
3887
|
Object.assign(fileEntry, {
|
|
3888
|
+
index: indexFile,
|
|
3679
3889
|
versionMadeBy,
|
|
3680
3890
|
msDosCompatible,
|
|
3681
3891
|
compressedSize: 0,
|
|
@@ -3695,8 +3905,15 @@
|
|
|
3695
3905
|
filename,
|
|
3696
3906
|
comment
|
|
3697
3907
|
});
|
|
3698
|
-
startOffset = Math.max(offsetFileEntry, startOffset);
|
|
3699
3908
|
readCommonFooter(fileEntry, fileEntry, directoryView, offset + 6);
|
|
3909
|
+
fileEntry.offset += prependedDataLength;
|
|
3910
|
+
startOffset = Math.min(fileEntry.offset, startOffset);
|
|
3911
|
+
if (checkAmbiguity) {
|
|
3912
|
+
if (filenames.has(fileEntry.filename)) {
|
|
3913
|
+
duplicateFilename = true;
|
|
3914
|
+
}
|
|
3915
|
+
filenames.add(fileEntry.filename);
|
|
3916
|
+
}
|
|
3700
3917
|
const unixExternalUpper = (fileEntry.externalFileAttributes >> 16) & MAX_16_BITS;
|
|
3701
3918
|
if (fileEntry.unixMode === UNDEFINED_VALUE && (unixExternalUpper & (FILE_ATTR_UNIX_DEFAULT_MASK | FILE_ATTR_UNIX_EXECUTABLE_MASK | FILE_ATTR_UNIX_TYPE_DIR)) != 0) {
|
|
3702
3919
|
fileEntry.unixMode = unixExternalUpper;
|
|
@@ -3717,7 +3934,7 @@
|
|
|
3717
3934
|
internalFileAttribute: fileEntry.internalFileAttributes,
|
|
3718
3935
|
externalFileAttribute: fileEntry.externalFileAttributes,
|
|
3719
3936
|
executable,
|
|
3720
|
-
directory: modeIsDir || upperIsDir || (msDosCompatible && msdosAttributes.directory) || (filename.endsWith(DIRECTORY_SIGNATURE) && !fileEntry.uncompressedSize),
|
|
3937
|
+
directory: modeIsDir || upperIsDir || (msDosCompatible && msdosAttributes.directory) || (fileEntry.filename.endsWith(DIRECTORY_SIGNATURE) && !fileEntry.uncompressedSize),
|
|
3721
3938
|
zipCrypto: fileEntry.encrypted && !fileEntry.extraFieldAES
|
|
3722
3939
|
});
|
|
3723
3940
|
const entry = new Entry(fileEntry);
|
|
@@ -3740,6 +3957,15 @@
|
|
|
3740
3957
|
}
|
|
3741
3958
|
yield entry;
|
|
3742
3959
|
}
|
|
3960
|
+
if (checkAmbiguity && offset != declaredDirectoryDataLength) {
|
|
3961
|
+
throwAmbiguousArchive("trailing central directory data");
|
|
3962
|
+
}
|
|
3963
|
+
if (duplicateFilename) {
|
|
3964
|
+
throwAmbiguousArchive("duplicate filename");
|
|
3965
|
+
}
|
|
3966
|
+
if (checkAmbiguity && (prependedDataLength || (filesLength && startOffset > 0))) {
|
|
3967
|
+
throwAmbiguousArchive("prepended data");
|
|
3968
|
+
}
|
|
3743
3969
|
const extractPrependedData = getOptionValue$1(zipReader, options, OPTION_EXTRACT_PREPENDED_DATA);
|
|
3744
3970
|
const extractAppendedData = getOptionValue$1(zipReader, options, OPTION_EXTRACT_APPENDED_DATA);
|
|
3745
3971
|
if (extractPrependedData) {
|
|
@@ -3779,9 +4005,21 @@
|
|
|
3779
4005
|
readable: (function () {
|
|
3780
4006
|
const { readable, writable } = new TransformStream();
|
|
3781
4007
|
if (value.getData) {
|
|
3782
|
-
|
|
4008
|
+
getData();
|
|
3783
4009
|
return readable;
|
|
3784
4010
|
}
|
|
4011
|
+
|
|
4012
|
+
async function getData() {
|
|
4013
|
+
try {
|
|
4014
|
+
await value.getData(writable);
|
|
4015
|
+
} catch (error) {
|
|
4016
|
+
try {
|
|
4017
|
+
await writable.abort(error);
|
|
4018
|
+
} catch {
|
|
4019
|
+
// ignored
|
|
4020
|
+
}
|
|
4021
|
+
}
|
|
4022
|
+
}
|
|
3785
4023
|
})()
|
|
3786
4024
|
};
|
|
3787
4025
|
delete chunk.getData;
|
|
@@ -3806,6 +4044,7 @@
|
|
|
3806
4044
|
const zipEntry = this;
|
|
3807
4045
|
const {
|
|
3808
4046
|
reader,
|
|
4047
|
+
index,
|
|
3809
4048
|
offset,
|
|
3810
4049
|
diskNumberStart,
|
|
3811
4050
|
extraFieldAES,
|
|
@@ -3831,27 +4070,42 @@
|
|
|
3831
4070
|
rawPassword = rawPassword && rawPassword.length && rawPassword;
|
|
3832
4071
|
if (extraFieldAES) {
|
|
3833
4072
|
if (extraFieldAES.originalCompressionMethod != COMPRESSION_METHOD_AES) {
|
|
3834
|
-
throw new Error(ERR_UNSUPPORTED_COMPRESSION);
|
|
4073
|
+
throw new Error(ERR_UNSUPPORTED_COMPRESSION$1);
|
|
3835
4074
|
}
|
|
3836
4075
|
}
|
|
3837
4076
|
if ((compressionMethod != COMPRESSION_METHOD_STORE && compressionMethod != COMPRESSION_METHOD_DEFLATE && compressionMethod != COMPRESSION_METHOD_DEFLATE_64) && !passThrough) {
|
|
3838
|
-
throw new Error(ERR_UNSUPPORTED_COMPRESSION);
|
|
4077
|
+
throw new Error(ERR_UNSUPPORTED_COMPRESSION$1);
|
|
3839
4078
|
}
|
|
3840
|
-
if (getUint32(dataView, 0) != LOCAL_FILE_HEADER_SIGNATURE) {
|
|
4079
|
+
if (dataArray.length < HEADER_SIZE || getUint32(dataView, 0) != LOCAL_FILE_HEADER_SIGNATURE) {
|
|
3841
4080
|
throw new Error(ERR_LOCAL_FILE_HEADER_NOT_FOUND);
|
|
3842
4081
|
}
|
|
3843
4082
|
readCommonHeader(localDirectory, dataView, 4);
|
|
3844
4083
|
const {
|
|
3845
4084
|
extraFieldLength,
|
|
3846
|
-
filenameLength
|
|
3847
|
-
lastAccessDate,
|
|
3848
|
-
creationDate
|
|
4085
|
+
filenameLength
|
|
3849
4086
|
} = localDirectory;
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
4087
|
+
const checkAmbiguity = getOptionValue$1(zipEntry, options, OPTION_CHECK_AMBIGUITY);
|
|
4088
|
+
let rawLocalFilename = new Uint8Array();
|
|
4089
|
+
if (checkAmbiguity && (filenameLength || extraFieldLength)) {
|
|
4090
|
+
const trailingDataArray = await readUint8Array(reader, offset + HEADER_SIZE, filenameLength + extraFieldLength, diskNumberStart);
|
|
4091
|
+
rawLocalFilename = trailingDataArray.subarray(0, filenameLength);
|
|
4092
|
+
localDirectory.rawExtraField = trailingDataArray.subarray(filenameLength);
|
|
4093
|
+
} else {
|
|
4094
|
+
localDirectory.rawExtraField = extraFieldLength ?
|
|
4095
|
+
await readUint8Array(reader, offset + HEADER_SIZE + filenameLength, extraFieldLength, diskNumberStart) :
|
|
4096
|
+
new Uint8Array();
|
|
4097
|
+
}
|
|
3853
4098
|
readCommonFooter(zipEntry, localDirectory, dataView, 4, true);
|
|
3854
|
-
|
|
4099
|
+
if (checkAmbiguity) {
|
|
4100
|
+
checkLocalDirectory(zipEntry, localDirectory, rawLocalFilename);
|
|
4101
|
+
}
|
|
4102
|
+
const { lastAccessDate, creationDate } = localDirectory;
|
|
4103
|
+
if (lastAccessDate) {
|
|
4104
|
+
fileEntry.lastAccessDate = lastAccessDate;
|
|
4105
|
+
}
|
|
4106
|
+
if (creationDate) {
|
|
4107
|
+
fileEntry.creationDate = creationDate;
|
|
4108
|
+
}
|
|
3855
4109
|
const encrypted = zipEntry.encrypted && localDirectory.encrypted && !passThrough;
|
|
3856
4110
|
const zipCrypto = encrypted && !extraFieldAES;
|
|
3857
4111
|
if (!passThrough) {
|
|
@@ -3897,7 +4151,7 @@
|
|
|
3897
4151
|
outputSize: passThrough ? compressedSize : uncompressedSize,
|
|
3898
4152
|
signature,
|
|
3899
4153
|
compressed: compressionMethod != 0 && !passThrough,
|
|
3900
|
-
encrypted
|
|
4154
|
+
encrypted,
|
|
3901
4155
|
useWebWorkers: getOptionValue$1(zipEntry, options, OPTION_USE_WEB_WORKERS),
|
|
3902
4156
|
useCompressionStream,
|
|
3903
4157
|
transferStreams: getOptionValue$1(zipEntry, options, OPTION_TRANSFER_STREAMS),
|
|
@@ -3911,6 +4165,7 @@
|
|
|
3911
4165
|
await detectOverlappingEntry({
|
|
3912
4166
|
reader,
|
|
3913
4167
|
fileEntry,
|
|
4168
|
+
index,
|
|
3914
4169
|
offset,
|
|
3915
4170
|
diskNumberStart,
|
|
3916
4171
|
signature,
|
|
@@ -4013,7 +4268,7 @@
|
|
|
4013
4268
|
directory.extraFieldUnicodeComment = extraFieldUnicodeComment;
|
|
4014
4269
|
}
|
|
4015
4270
|
const extraFieldAES = extraField.get(EXTRAFIELD_TYPE_AES);
|
|
4016
|
-
if (extraFieldAES) {
|
|
4271
|
+
if (extraFieldAES && extraFieldAES.data.length >= 7) {
|
|
4017
4272
|
readExtraFieldAES(extraFieldAES, directory, compressionMethod);
|
|
4018
4273
|
directory.extraFieldAES = extraFieldAES;
|
|
4019
4274
|
} else {
|
|
@@ -4050,19 +4305,23 @@
|
|
|
4050
4305
|
directory.zip64 = true;
|
|
4051
4306
|
const extraFieldView = getDataView$1(extraFieldZip64.data);
|
|
4052
4307
|
const missingProperties = ZIP64_PROPERTIES.filter(([propertyName, max]) => directory[propertyName] == max);
|
|
4308
|
+
const requiredLength = missingProperties.reduce((length, [, max]) => length + ZIP64_EXTRACTION[max].bytes, 0);
|
|
4309
|
+
if (extraFieldZip64.data.length < requiredLength) {
|
|
4310
|
+
throw new Error(ERR_EXTRAFIELD_ZIP64_NOT_FOUND);
|
|
4311
|
+
}
|
|
4053
4312
|
for (let indexMissingProperty = 0, offset = 0; indexMissingProperty < missingProperties.length; indexMissingProperty++) {
|
|
4054
4313
|
const [propertyName, max] = missingProperties[indexMissingProperty];
|
|
4055
|
-
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
offset += extraction.bytes;
|
|
4059
|
-
} else if (extraFieldZip64[propertyName]) {
|
|
4060
|
-
throw new Error(ERR_EXTRAFIELD_ZIP64_NOT_FOUND);
|
|
4061
|
-
}
|
|
4314
|
+
const extraction = ZIP64_EXTRACTION[max];
|
|
4315
|
+
directory[propertyName] = extraFieldZip64[propertyName] = extraction.getValue(extraFieldView, offset);
|
|
4316
|
+
offset += extraction.bytes;
|
|
4062
4317
|
}
|
|
4063
4318
|
}
|
|
4064
4319
|
|
|
4065
4320
|
function readExtraFieldUnicode(extraFieldUnicode, propertyName, rawPropertyName, directory, fileEntry) {
|
|
4321
|
+
if (extraFieldUnicode.data.length < 5) {
|
|
4322
|
+
extraFieldUnicode.valid = false;
|
|
4323
|
+
return;
|
|
4324
|
+
}
|
|
4066
4325
|
const extraFieldView = getDataView$1(extraFieldUnicode.data);
|
|
4067
4326
|
const crc32 = new Crc32();
|
|
4068
4327
|
crc32.append(fileEntry[rawPropertyName]);
|
|
@@ -4135,32 +4394,30 @@
|
|
|
4135
4394
|
function readExtraFieldUnix(extraField, directory, isInfoZip) {
|
|
4136
4395
|
try {
|
|
4137
4396
|
const view = getDataView$1(new Uint8Array(extraField.data));
|
|
4138
|
-
let
|
|
4139
|
-
|
|
4140
|
-
|
|
4141
|
-
|
|
4142
|
-
|
|
4143
|
-
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
if (
|
|
4150
|
-
|
|
4151
|
-
|
|
4152
|
-
|
|
4153
|
-
|
|
4154
|
-
|
|
4397
|
+
let uid, gid;
|
|
4398
|
+
if (isInfoZip) {
|
|
4399
|
+
// Info-ZIP New Unix "ux" field (0x7875): version + variable-length uid/gid
|
|
4400
|
+
let offset = 0;
|
|
4401
|
+
const version = getUint8(view, offset++);
|
|
4402
|
+
const uidSize = getUint8(view, offset++);
|
|
4403
|
+
uid = unpackUnixId(extraField.data.subarray(offset, offset + uidSize));
|
|
4404
|
+
offset += uidSize;
|
|
4405
|
+
const gidSize = getUint8(view, offset++);
|
|
4406
|
+
gid = unpackUnixId(extraField.data.subarray(offset, offset + gidSize));
|
|
4407
|
+
Object.assign(extraField, { version, uid, gid });
|
|
4408
|
+
} else if (extraField.data.length >= 4) {
|
|
4409
|
+
// Info-ZIP Unix "Ux" field (0x7855): fixed 2-byte uid + gid (the file mode, if any, is
|
|
4410
|
+
// carried in the external file attributes, not here)
|
|
4411
|
+
uid = getUint16(view, 0);
|
|
4412
|
+
gid = getUint16(view, 2);
|
|
4413
|
+
Object.assign(extraField, { uid, gid });
|
|
4414
|
+
}
|
|
4155
4415
|
if (uid !== UNDEFINED_VALUE) {
|
|
4156
4416
|
directory.uid = uid;
|
|
4157
4417
|
}
|
|
4158
4418
|
if (gid !== UNDEFINED_VALUE) {
|
|
4159
4419
|
directory.gid = gid;
|
|
4160
4420
|
}
|
|
4161
|
-
if (unixMode !== UNDEFINED_VALUE) {
|
|
4162
|
-
directory.unixMode = unixMode;
|
|
4163
|
-
}
|
|
4164
4421
|
} catch {
|
|
4165
4422
|
// ignored
|
|
4166
4423
|
}
|
|
@@ -4174,6 +4431,9 @@
|
|
|
4174
4431
|
}
|
|
4175
4432
|
|
|
4176
4433
|
function readExtraFieldExtendedTimestamp(extraFieldExtendedTimestamp, directory, localDirectory) {
|
|
4434
|
+
if (!extraFieldExtendedTimestamp.data.length) {
|
|
4435
|
+
return;
|
|
4436
|
+
}
|
|
4177
4437
|
const extraFieldView = getDataView$1(extraFieldExtendedTimestamp.data);
|
|
4178
4438
|
const flags = getUint8(extraFieldView, 0);
|
|
4179
4439
|
const timeProperties = [];
|
|
@@ -4199,7 +4459,8 @@
|
|
|
4199
4459
|
timeProperties.forEach((propertyName, indexProperty) => {
|
|
4200
4460
|
if (extraFieldExtendedTimestamp.data.length >= offset + 4) {
|
|
4201
4461
|
const time = getUint32(extraFieldView, offset);
|
|
4202
|
-
|
|
4462
|
+
// the extended timestamp is a signed 32-bit time_t value
|
|
4463
|
+
directory[propertyName] = extraFieldExtendedTimestamp[propertyName] = new Date((time | 0) * 1000);
|
|
4203
4464
|
const rawPropertyName = timeRawProperties[indexProperty];
|
|
4204
4465
|
extraFieldExtendedTimestamp[rawPropertyName] = time;
|
|
4205
4466
|
}
|
|
@@ -4210,6 +4471,7 @@
|
|
|
4210
4471
|
async function detectOverlappingEntry({
|
|
4211
4472
|
reader,
|
|
4212
4473
|
fileEntry,
|
|
4474
|
+
index,
|
|
4213
4475
|
offset,
|
|
4214
4476
|
diskNumberStart,
|
|
4215
4477
|
signature,
|
|
@@ -4221,10 +4483,9 @@
|
|
|
4221
4483
|
readRanges
|
|
4222
4484
|
}) {
|
|
4223
4485
|
let diskOffset = 0;
|
|
4224
|
-
if (diskNumberStart) {
|
|
4225
|
-
for (let indexReader = 0; indexReader < diskNumberStart; indexReader++) {
|
|
4226
|
-
|
|
4227
|
-
diskOffset += diskReader.size;
|
|
4486
|
+
if (diskNumberStart && reader.readers) {
|
|
4487
|
+
for (let indexReader = 0; indexReader < Math.min(diskNumberStart, reader.readers.length); indexReader++) {
|
|
4488
|
+
diskOffset += reader.readers[indexReader].size;
|
|
4228
4489
|
}
|
|
4229
4490
|
}
|
|
4230
4491
|
let dataDescriptorLength = 0;
|
|
@@ -4237,7 +4498,8 @@
|
|
|
4237
4498
|
}
|
|
4238
4499
|
if (dataDescriptorLength) {
|
|
4239
4500
|
const dataDescriptorArray = await readUint8Array(reader, dataOffset + compressedSize, dataDescriptorLength + DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH, diskNumberStart);
|
|
4240
|
-
const dataDescriptorSignature =
|
|
4501
|
+
const dataDescriptorSignature = dataDescriptorArray.length == dataDescriptorLength + DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH &&
|
|
4502
|
+
getUint32(getDataView$1(dataDescriptorArray), 0) == DATA_DESCRIPTOR_RECORD_SIGNATURE;
|
|
4241
4503
|
if (dataDescriptorSignature) {
|
|
4242
4504
|
const readSignature = getUint32(getDataView$1(dataDescriptorArray), 4);
|
|
4243
4505
|
let readCompressedSize;
|
|
@@ -4262,14 +4524,14 @@
|
|
|
4262
4524
|
end: diskOffset + dataOffset + compressedSize + dataDescriptorLength,
|
|
4263
4525
|
fileEntry
|
|
4264
4526
|
};
|
|
4265
|
-
for (const otherRange of readRanges) {
|
|
4266
|
-
if (
|
|
4527
|
+
for (const [otherIndex, otherRange] of readRanges) {
|
|
4528
|
+
if (otherIndex != index && range.start < otherRange.end && otherRange.start < range.end) {
|
|
4267
4529
|
const error = new Error(ERR_OVERLAPPING_ENTRY);
|
|
4268
4530
|
error.overlappingEntry = otherRange.fileEntry;
|
|
4269
4531
|
throw error;
|
|
4270
4532
|
}
|
|
4271
4533
|
}
|
|
4272
|
-
readRanges.
|
|
4534
|
+
readRanges.set(index, range);
|
|
4273
4535
|
}
|
|
4274
4536
|
|
|
4275
4537
|
async function seekSignature(reader, signature, startOffset, minimumBytes, maximumLength) {
|
|
@@ -4294,6 +4556,35 @@
|
|
|
4294
4556
|
}
|
|
4295
4557
|
}
|
|
4296
4558
|
|
|
4559
|
+
function checkLocalDirectory(zipEntry, localDirectory, rawLocalFilename) {
|
|
4560
|
+
const { rawFilename } = zipEntry;
|
|
4561
|
+
if (rawLocalFilename.length != rawFilename.length ||
|
|
4562
|
+
rawLocalFilename.some((byteValue, indexByte) => byteValue != rawFilename[indexByte])) {
|
|
4563
|
+
throwAmbiguousArchive("mismatched local file header (filename)");
|
|
4564
|
+
}
|
|
4565
|
+
if ((localDirectory.rawBitFlag & BITFLAG_AMBIGUITY_MASK) != (zipEntry.rawBitFlag & BITFLAG_AMBIGUITY_MASK)) {
|
|
4566
|
+
throwAmbiguousArchive("mismatched local file header (general purpose bit flag)");
|
|
4567
|
+
}
|
|
4568
|
+
if (localDirectory.compressionMethod != zipEntry.compressionMethod) {
|
|
4569
|
+
throwAmbiguousArchive("mismatched local file header (compression method)");
|
|
4570
|
+
}
|
|
4571
|
+
// when the data descriptor flag is set, the local signature and sizes are zeroed out; all-zero values are
|
|
4572
|
+
// also tolerated when the flag is clear because some tools produce them (streaming writers predating bit 3)
|
|
4573
|
+
if (!localDirectory.bitFlag.dataDescriptor &&
|
|
4574
|
+
(localDirectory.signature || localDirectory.compressedSize || localDirectory.uncompressedSize) &&
|
|
4575
|
+
(localDirectory.signature != zipEntry.signature ||
|
|
4576
|
+
localDirectory.compressedSize != zipEntry.compressedSize ||
|
|
4577
|
+
localDirectory.uncompressedSize != zipEntry.uncompressedSize)) {
|
|
4578
|
+
throwAmbiguousArchive("mismatched local file header (signature or sizes)");
|
|
4579
|
+
}
|
|
4580
|
+
}
|
|
4581
|
+
|
|
4582
|
+
function throwAmbiguousArchive(reason) {
|
|
4583
|
+
const error = new Error(ERR_AMBIGUOUS_ARCHIVE);
|
|
4584
|
+
error.reason = reason;
|
|
4585
|
+
throw error;
|
|
4586
|
+
}
|
|
4587
|
+
|
|
4297
4588
|
function getOptionValue$1(zipReader, options, name) {
|
|
4298
4589
|
return options[name] === UNDEFINED_VALUE ? zipReader.options[name] : options[name];
|
|
4299
4590
|
}
|
|
@@ -4332,7 +4623,7 @@
|
|
|
4332
4623
|
}
|
|
4333
4624
|
|
|
4334
4625
|
function getDataView$1(array) {
|
|
4335
|
-
return new DataView(array.buffer);
|
|
4626
|
+
return new DataView(array.buffer, array.byteOffset, array.byteLength);
|
|
4336
4627
|
}
|
|
4337
4628
|
|
|
4338
4629
|
/*
|
|
@@ -4372,13 +4663,19 @@
|
|
|
4372
4663
|
const ERR_INVALID_ENCRYPTION_STRENGTH = "The strength must equal 1, 2, or 3";
|
|
4373
4664
|
const ERR_INVALID_EXTRAFIELD_TYPE = "Extra field type exceeds 65535";
|
|
4374
4665
|
const ERR_INVALID_EXTRAFIELD_DATA = "Extra field data exceeds 64KB";
|
|
4666
|
+
// same message as the constant exported by zip-reader.js
|
|
4667
|
+
const ERR_UNSUPPORTED_COMPRESSION = "Compression method not supported";
|
|
4668
|
+
const MIN_UNIX_TIME = -2147483648;
|
|
4669
|
+
const MAX_UNIX_TIME = 2147483647;
|
|
4375
4670
|
const ERR_UNSUPPORTED_FORMAT = "Zip64 is not supported (set the 'zip64' option to 'true')";
|
|
4376
4671
|
const ERR_UNDEFINED_UNCOMPRESSED_SIZE = "Undefined uncompressed size";
|
|
4672
|
+
const ERR_UNDEFINED_READER = "Undefined reader";
|
|
4377
4673
|
const ERR_ZIP_NOT_EMPTY = "Zip file not empty";
|
|
4378
4674
|
const ERR_INVALID_UID = "Invalid uid (must be integer 0..2^32-1)";
|
|
4379
4675
|
const ERR_INVALID_GID = "Invalid gid (must be integer 0..2^32-1)";
|
|
4380
4676
|
const ERR_INVALID_UNIX_MODE = "Invalid UNIX mode (must be integer 0..65535)";
|
|
4381
4677
|
const ERR_INVALID_UNIX_EXTRA_FIELD_TYPE = "Invalid unixExtraFieldType (must be 'infozip' or 'unix')";
|
|
4678
|
+
const ERR_INVALID_UNIX_ID_SIZE = "uid/gid must be 0..65535 for unixExtraFieldType 'unix' (use 'infozip' for larger ids)";
|
|
4382
4679
|
const ERR_INVALID_MSDOS_ATTRIBUTES = "Invalid msdosAttributesRaw (must be integer 0..255)";
|
|
4383
4680
|
const ERR_INVALID_MSDOS_DATA = "Invalid msdosAttributes (must be an object with boolean flags)";
|
|
4384
4681
|
|
|
@@ -4404,9 +4701,10 @@
|
|
|
4404
4701
|
files: new Map(),
|
|
4405
4702
|
filenames: new Set(),
|
|
4406
4703
|
offset: options[OPTION_OFFSET] === UNDEFINED_VALUE ? writer.size || writer.writable.size || 0 : options[OPTION_OFFSET],
|
|
4407
|
-
|
|
4704
|
+
initialOffset: options[OPTION_OFFSET] === UNDEFINED_VALUE ? 0 : options[OPTION_OFFSET] - (writer.size || writer.writable.size || 0),
|
|
4408
4705
|
pendingAddFileCalls: new Set(),
|
|
4409
|
-
bufferedWrites: 0
|
|
4706
|
+
bufferedWrites: 0,
|
|
4707
|
+
lastFileEntry: UNDEFINED_VALUE
|
|
4410
4708
|
});
|
|
4411
4709
|
}
|
|
4412
4710
|
|
|
@@ -4415,17 +4713,18 @@
|
|
|
4415
4713
|
throw new Error(ERR_ZIP_NOT_EMPTY);
|
|
4416
4714
|
}
|
|
4417
4715
|
reader = new GenericReader(reader);
|
|
4716
|
+
await initStream(reader);
|
|
4418
4717
|
const zipReader = new ZipReader(reader.readable);
|
|
4419
4718
|
const entries = await zipReader.getEntries();
|
|
4420
4719
|
await zipReader.close();
|
|
4720
|
+
await initStream(this.writer);
|
|
4421
4721
|
await reader.readable.pipeTo(this.writer.writable, { preventClose: true, preventAbort: true });
|
|
4422
4722
|
this.writer.size = this.offset = reader.size;
|
|
4423
4723
|
this.filenames = new Set(entries.map(entry => entry.filename));
|
|
4424
4724
|
this.files = new Map(entries.map(entry => {
|
|
4425
4725
|
const {
|
|
4426
4726
|
version,
|
|
4427
|
-
|
|
4428
|
-
lastModDate,
|
|
4727
|
+
rawLastModDate,
|
|
4429
4728
|
lastAccessDate,
|
|
4430
4729
|
creationDate,
|
|
4431
4730
|
rawFilename,
|
|
@@ -4438,6 +4737,7 @@
|
|
|
4438
4737
|
zip64
|
|
4439
4738
|
} = entry;
|
|
4440
4739
|
let {
|
|
4740
|
+
compressionMethod,
|
|
4441
4741
|
rawExtraFieldZip64,
|
|
4442
4742
|
rawExtraFieldAES,
|
|
4443
4743
|
rawExtraFieldExtendedTimestamp,
|
|
@@ -4450,31 +4750,39 @@
|
|
|
4450
4750
|
rawExtraFieldAES = rawExtraFieldAES || new Uint8Array();
|
|
4451
4751
|
rawExtraFieldExtendedTimestamp = rawExtraFieldExtendedTimestamp || new Uint8Array();
|
|
4452
4752
|
rawExtraFieldNTFS = rawExtraFieldNTFS || new Uint8Array();
|
|
4453
|
-
rawExtraFieldUnix =
|
|
4753
|
+
rawExtraFieldUnix = rawExtraFieldUnix || new Uint8Array();
|
|
4454
4754
|
rawExtraField = rawExtraField || new Uint8Array();
|
|
4755
|
+
if (entry.extraFieldAES) {
|
|
4756
|
+
compressionMethod = COMPRESSION_METHOD_AES;
|
|
4757
|
+
}
|
|
4455
4758
|
const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraFieldUnix, rawExtraField);
|
|
4456
|
-
const zip64UncompressedSize = zip64 && uncompressedSize
|
|
4457
|
-
const zip64CompressedSize = zip64 && compressedSize
|
|
4759
|
+
const zip64UncompressedSize = zip64 && uncompressedSize >= MAX_32_BITS;
|
|
4760
|
+
const zip64CompressedSize = zip64 && compressedSize >= MAX_32_BITS;
|
|
4761
|
+
const bitFlagValue = (getBitFlag(level, languageEncodingFlag, dataDescriptor, encrypted, compressionMethod) & ~BITFLAG_LEVEL) | (level << 1);
|
|
4458
4762
|
const {
|
|
4459
4763
|
headerArray,
|
|
4460
4764
|
headerView
|
|
4461
4765
|
} = getHeaderArrayData({
|
|
4462
4766
|
version,
|
|
4463
|
-
bitFlag:
|
|
4767
|
+
bitFlag: bitFlagValue,
|
|
4464
4768
|
compressionMethod,
|
|
4465
4769
|
uncompressedSize,
|
|
4466
4770
|
compressedSize,
|
|
4467
|
-
|
|
4771
|
+
rawLastModDate,
|
|
4468
4772
|
rawFilename,
|
|
4469
4773
|
zip64CompressedSize,
|
|
4470
4774
|
zip64UncompressedSize,
|
|
4471
4775
|
extraFieldLength
|
|
4472
4776
|
});
|
|
4777
|
+
const { signature } = entry;
|
|
4778
|
+
if (signature !== UNDEFINED_VALUE) {
|
|
4779
|
+
setUint32(headerView, HEADER_OFFSET_SIGNATURE, signature);
|
|
4780
|
+
}
|
|
4473
4781
|
Object.assign(entry, {
|
|
4474
4782
|
zip64UncompressedSize,
|
|
4475
4783
|
zip64CompressedSize,
|
|
4476
|
-
zip64Offset: zip64 && this.offset - diskOffset
|
|
4477
|
-
zip64DiskNumberStart: zip64 && diskNumber
|
|
4784
|
+
zip64Offset: zip64 && this.offset - diskOffset >= MAX_32_BITS,
|
|
4785
|
+
zip64DiskNumberStart: zip64 && diskNumber >= MAX_16_BITS,
|
|
4478
4786
|
rawExtraFieldZip64,
|
|
4479
4787
|
rawExtraFieldAES,
|
|
4480
4788
|
rawExtraFieldExtendedTimestamp,
|
|
@@ -4492,6 +4800,7 @@
|
|
|
4492
4800
|
|
|
4493
4801
|
async add(name = "", reader, options = {}) {
|
|
4494
4802
|
const zipWriter = this;
|
|
4803
|
+
options = Object.assign({}, options);
|
|
4495
4804
|
const {
|
|
4496
4805
|
pendingAddFileCalls,
|
|
4497
4806
|
config
|
|
@@ -4502,17 +4811,24 @@
|
|
|
4502
4811
|
await new Promise(resolve => pendingEntries.push(resolve));
|
|
4503
4812
|
}
|
|
4504
4813
|
let promiseAddFile;
|
|
4814
|
+
let nameAdded;
|
|
4505
4815
|
try {
|
|
4506
4816
|
name = name.trim();
|
|
4817
|
+
if (getOptionValue(zipWriter, options, PROPERTY_NAME_DIRECTORY) && !name.endsWith(DIRECTORY_SIGNATURE)) {
|
|
4818
|
+
name += DIRECTORY_SIGNATURE;
|
|
4819
|
+
}
|
|
4507
4820
|
if (zipWriter.filenames.has(name)) {
|
|
4508
4821
|
throw new Error(ERR_DUPLICATED_NAME);
|
|
4509
4822
|
}
|
|
4510
4823
|
zipWriter.filenames.add(name);
|
|
4824
|
+
nameAdded = true;
|
|
4511
4825
|
promiseAddFile = addFile(zipWriter, name, reader, options);
|
|
4512
4826
|
pendingAddFileCalls.add(promiseAddFile);
|
|
4513
4827
|
return await promiseAddFile;
|
|
4514
4828
|
} catch (error) {
|
|
4515
|
-
|
|
4829
|
+
if (nameAdded) {
|
|
4830
|
+
zipWriter.filenames.delete(name);
|
|
4831
|
+
}
|
|
4516
4832
|
throw error;
|
|
4517
4833
|
} finally {
|
|
4518
4834
|
pendingAddFileCalls.delete(promiseAddFile);
|
|
@@ -4545,6 +4861,9 @@
|
|
|
4545
4861
|
const zipWriter = this;
|
|
4546
4862
|
const { pendingAddFileCalls, writer } = this;
|
|
4547
4863
|
const { writable } = writer;
|
|
4864
|
+
if (getLength(comment) > MAX_16_BITS) {
|
|
4865
|
+
throw new Error(ERR_INVALID_COMMENT);
|
|
4866
|
+
}
|
|
4548
4867
|
while (pendingAddFileCalls.size) {
|
|
4549
4868
|
await Promise.allSettled(Array.from(pendingAddFileCalls));
|
|
4550
4869
|
}
|
|
@@ -4563,28 +4882,94 @@
|
|
|
4563
4882
|
const { readable, writable } = new TransformStream();
|
|
4564
4883
|
this.readable = readable;
|
|
4565
4884
|
this.zipWriter = new ZipWriter(writable, options);
|
|
4885
|
+
this.pendingAddFileCalls = new Set();
|
|
4566
4886
|
}
|
|
4567
4887
|
|
|
4568
4888
|
transform(path) {
|
|
4889
|
+
const zipWriter = this.zipWriter;
|
|
4890
|
+
let streamController;
|
|
4569
4891
|
const { readable, writable } = new TransformStream({
|
|
4570
|
-
|
|
4892
|
+
start(controller) {
|
|
4893
|
+
streamController = controller;
|
|
4894
|
+
},
|
|
4895
|
+
flush: () => void closeArchive()
|
|
4571
4896
|
});
|
|
4572
|
-
this.zipWriter.add(path, readable);
|
|
4897
|
+
watchAddFileCall(this, this.zipWriter.add(path, readable), error => streamController.error(error));
|
|
4573
4898
|
return { readable: this.readable, writable };
|
|
4899
|
+
|
|
4900
|
+
async function closeArchive() {
|
|
4901
|
+
try {
|
|
4902
|
+
await zipWriter.close();
|
|
4903
|
+
} catch (error) {
|
|
4904
|
+
try {
|
|
4905
|
+
await zipWriter.writer.writable.abort(error);
|
|
4906
|
+
} catch {
|
|
4907
|
+
// ignored
|
|
4908
|
+
}
|
|
4909
|
+
}
|
|
4910
|
+
}
|
|
4574
4911
|
}
|
|
4575
4912
|
|
|
4576
4913
|
writable(path) {
|
|
4577
|
-
|
|
4578
|
-
|
|
4914
|
+
let streamController;
|
|
4915
|
+
const { readable, writable } = new TransformStream({
|
|
4916
|
+
start(controller) {
|
|
4917
|
+
streamController = controller;
|
|
4918
|
+
}
|
|
4919
|
+
});
|
|
4920
|
+
watchAddFileCall(this, this.zipWriter.add(path, readable), error => streamController.error(error));
|
|
4579
4921
|
return writable;
|
|
4580
4922
|
}
|
|
4581
4923
|
|
|
4582
|
-
close(comment = UNDEFINED_VALUE, options = {}) {
|
|
4924
|
+
async close(comment = UNDEFINED_VALUE, options = {}) {
|
|
4925
|
+
await Promise.all(Array.from(this.pendingAddFileCalls));
|
|
4583
4926
|
return this.zipWriter.close(comment, options);
|
|
4584
4927
|
}
|
|
4585
4928
|
}
|
|
4586
4929
|
|
|
4930
|
+
function watchAddFileCall(zipWriterStream, promiseAddFile, onerror) {
|
|
4931
|
+
zipWriterStream.pendingAddFileCalls.add(promiseAddFile);
|
|
4932
|
+
promiseAddFile.catch(error => {
|
|
4933
|
+
try {
|
|
4934
|
+
onerror(error);
|
|
4935
|
+
} catch {
|
|
4936
|
+
// ignored
|
|
4937
|
+
}
|
|
4938
|
+
});
|
|
4939
|
+
}
|
|
4940
|
+
|
|
4587
4941
|
async function addFile(zipWriter, name, reader, options) {
|
|
4942
|
+
const attributesInfo = resolveAttributes(zipWriter, name, options);
|
|
4943
|
+
({ name } = attributesInfo);
|
|
4944
|
+
const metadataInfo = resolveMetadata(zipWriter, name, options);
|
|
4945
|
+
const { comment } = metadataInfo;
|
|
4946
|
+
const extraField = options[PROPERTY_NAME_EXTRA_FIELD];
|
|
4947
|
+
zipWriter.files.set(name, UNDEFINED_VALUE);
|
|
4948
|
+
let fileEntry;
|
|
4949
|
+
try {
|
|
4950
|
+
const sizesInfo = await resolveSizes(zipWriter, reader, metadataInfo, options);
|
|
4951
|
+
({ reader } = sizesInfo);
|
|
4952
|
+
const { diskOffset, diskNumber } = zipWriter.writer;
|
|
4953
|
+
options = Object.assign({}, options, attributesInfo.resolvedOptions, metadataInfo.resolvedOptions, sizesInfo.resolvedOptions, {
|
|
4954
|
+
internalFileAttribute: metadataInfo.resolvedOptions.internalFileAttributes,
|
|
4955
|
+
externalFileAttribute: attributesInfo.resolvedOptions.externalFileAttributes,
|
|
4956
|
+
signature: options[PROPERTY_NAME_SIGNATURE],
|
|
4957
|
+
offset: zipWriter.offset - diskOffset,
|
|
4958
|
+
diskNumberStart: diskNumber
|
|
4959
|
+
});
|
|
4960
|
+
const headerInfo = getHeaderInfo(options);
|
|
4961
|
+
const dataDescriptorInfo = getDataDescriptorInfo(options);
|
|
4962
|
+
const metadataSize = getLength(headerInfo.localHeaderArray, dataDescriptorInfo.dataDescriptorArray);
|
|
4963
|
+
fileEntry = await getFileEntry(zipWriter, name, reader, { headerInfo, dataDescriptorInfo, metadataSize }, options);
|
|
4964
|
+
} catch (error) {
|
|
4965
|
+
zipWriter.files.delete(name);
|
|
4966
|
+
throw error;
|
|
4967
|
+
}
|
|
4968
|
+
Object.assign(fileEntry, { name, comment, extraField });
|
|
4969
|
+
return new Entry(fileEntry);
|
|
4970
|
+
}
|
|
4971
|
+
|
|
4972
|
+
function resolveAttributes(zipWriter, name, options) {
|
|
4588
4973
|
name = name.trim();
|
|
4589
4974
|
let msDosCompatible = getOptionValue(zipWriter, options, PROPERTY_NAME_MS_DOS_COMPATIBLE);
|
|
4590
4975
|
let versionMadeBy = getOptionValue(zipWriter, options, PROPERTY_NAME_VERSION_MADE_BY, msDosCompatible ? 20 : 768);
|
|
@@ -4592,7 +4977,7 @@
|
|
|
4592
4977
|
const uid = getOptionValue(zipWriter, options, PROPERTY_NAME_UID);
|
|
4593
4978
|
const gid = getOptionValue(zipWriter, options, PROPERTY_NAME_GID);
|
|
4594
4979
|
let unixMode = getOptionValue(zipWriter, options, PROPERTY_NAME_UNIX_MODE);
|
|
4595
|
-
|
|
4980
|
+
let unixExtraFieldType = getOptionValue(zipWriter, options, OPTION_UNIX_EXTRA_FIELD_TYPE);
|
|
4596
4981
|
let setuid = getOptionValue(zipWriter, options, PROPERTY_NAME_SETUID);
|
|
4597
4982
|
let setgid = getOptionValue(zipWriter, options, PROPERTY_NAME_SETGID);
|
|
4598
4983
|
let sticky = getOptionValue(zipWriter, options, PROPERTY_NAME_STICKY);
|
|
@@ -4608,6 +4993,13 @@
|
|
|
4608
4993
|
if (unixExtraFieldType !== UNDEFINED_VALUE && unixExtraFieldType !== INFOZIP_EXTRA_FIELD_TYPE && unixExtraFieldType !== UNIX_EXTRA_FIELD_TYPE) {
|
|
4609
4994
|
throw new Error(ERR_INVALID_UNIX_EXTRA_FIELD_TYPE);
|
|
4610
4995
|
}
|
|
4996
|
+
if (unixExtraFieldType === UNIX_EXTRA_FIELD_TYPE &&
|
|
4997
|
+
((uid !== UNDEFINED_VALUE && uid > MAX_16_BITS) || (gid !== UNDEFINED_VALUE && gid > MAX_16_BITS))) {
|
|
4998
|
+
throw new Error(ERR_INVALID_UNIX_ID_SIZE);
|
|
4999
|
+
}
|
|
5000
|
+
if (unixExtraFieldType === UNDEFINED_VALUE && (uid !== UNDEFINED_VALUE || gid !== UNDEFINED_VALUE)) {
|
|
5001
|
+
unixExtraFieldType = INFOZIP_EXTRA_FIELD_TYPE;
|
|
5002
|
+
}
|
|
4611
5003
|
let msdosAttributesRaw = getOptionValue(zipWriter, options, PROPERTY_NAME_MSDOS_ATTRIBUTES_RAW);
|
|
4612
5004
|
let msdosAttributes = getOptionValue(zipWriter, options, PROPERTY_NAME_MSDOS_ATTRIBUTES);
|
|
4613
5005
|
const hasUnixMetadata = uid !== UNDEFINED_VALUE || gid !== UNDEFINED_VALUE || unixMode !== UNDEFINED_VALUE || unixExtraFieldType;
|
|
@@ -4678,6 +5070,27 @@
|
|
|
4678
5070
|
if (hasMsDosProvided) {
|
|
4679
5071
|
externalFileAttributes = (externalFileAttributes & MAX_32_BITS) | (msdosAttributesRaw & MAX_8_BITS);
|
|
4680
5072
|
}
|
|
5073
|
+
return {
|
|
5074
|
+
name,
|
|
5075
|
+
resolvedOptions: {
|
|
5076
|
+
versionMadeBy,
|
|
5077
|
+
msDosCompatible,
|
|
5078
|
+
externalFileAttributes,
|
|
5079
|
+
unixExternalUpper,
|
|
5080
|
+
uid,
|
|
5081
|
+
gid,
|
|
5082
|
+
unixMode,
|
|
5083
|
+
unixExtraFieldType,
|
|
5084
|
+
setuid,
|
|
5085
|
+
setgid,
|
|
5086
|
+
sticky,
|
|
5087
|
+
msdosAttributesRaw,
|
|
5088
|
+
msdosAttributes
|
|
5089
|
+
}
|
|
5090
|
+
};
|
|
5091
|
+
}
|
|
5092
|
+
|
|
5093
|
+
function resolveMetadata(zipWriter, name, options) {
|
|
4681
5094
|
const encode = getOptionValue(zipWriter, options, OPTION_ENCODE_TEXT, encodeText);
|
|
4682
5095
|
let rawFilename = encode(name);
|
|
4683
5096
|
if (rawFilename === UNDEFINED_VALUE) {
|
|
@@ -4716,10 +5129,14 @@
|
|
|
4716
5129
|
const transferStreams = getOptionValue(zipWriter, options, OPTION_TRANSFER_STREAMS, true);
|
|
4717
5130
|
const bufferedWrite = getOptionValue(zipWriter, options, OPTION_BUFFERED_WRITE);
|
|
4718
5131
|
const createTempStream = getOptionValue(zipWriter, options, OPTION_CREATE_TEMP_STREAM);
|
|
4719
|
-
const dataDescriptorSignature = getOptionValue(zipWriter, options, OPTION_DATA_DESCRIPTOR_SIGNATURE,
|
|
5132
|
+
const dataDescriptorSignature = getOptionValue(zipWriter, options, OPTION_DATA_DESCRIPTOR_SIGNATURE, true);
|
|
4720
5133
|
const signal = getOptionValue(zipWriter, options, OPTION_SIGNAL);
|
|
4721
5134
|
const useUnicodeFileNames = getOptionValue(zipWriter, options, OPTION_USE_UNICODE_FILE_NAMES, true);
|
|
4722
5135
|
const compressionMethod = getOptionValue(zipWriter, options, PROPERTY_NAME_COMPRESSION_METHOD);
|
|
5136
|
+
if (!passThrough && compressionMethod !== UNDEFINED_VALUE &&
|
|
5137
|
+
compressionMethod !== COMPRESSION_METHOD_STORE && compressionMethod !== COMPRESSION_METHOD_DEFLATE) {
|
|
5138
|
+
throw new Error(ERR_UNSUPPORTED_COMPRESSION);
|
|
5139
|
+
}
|
|
4723
5140
|
let level = getOptionValue(zipWriter, options, OPTION_LEVEL);
|
|
4724
5141
|
let useCompressionStream = getOptionValue(zipWriter, options, OPTION_USE_COMPRESSION_STREAM);
|
|
4725
5142
|
let dataDescriptor = getOptionValue(zipWriter, options, OPTION_DATA_DESCRIPTOR);
|
|
@@ -4735,7 +5152,7 @@
|
|
|
4735
5152
|
if (!useCompressionStream && (zipWriter.config.CompressionStream === UNDEFINED_VALUE && zipWriter.config.CompressionStreamZlib === UNDEFINED_VALUE)) {
|
|
4736
5153
|
level = 0;
|
|
4737
5154
|
}
|
|
4738
|
-
|
|
5155
|
+
const zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
|
|
4739
5156
|
if (!zipCrypto && (password !== UNDEFINED_VALUE || rawPassword !== UNDEFINED_VALUE) && !(encryptionStrength >= 1 && encryptionStrength <= 3)) {
|
|
4740
5157
|
throw new Error(ERR_INVALID_ENCRYPTION_STRENGTH);
|
|
4741
5158
|
}
|
|
@@ -4746,6 +5163,7 @@
|
|
|
4746
5163
|
let offset = 0;
|
|
4747
5164
|
extraField.forEach(data => extraFieldSize += 4 + getLength(data));
|
|
4748
5165
|
rawExtraField = new Uint8Array(extraFieldSize);
|
|
5166
|
+
const rawExtraFieldView = getDataView(rawExtraField);
|
|
4749
5167
|
extraField.forEach((data, type) => {
|
|
4750
5168
|
if (type > MAX_16_BITS) {
|
|
4751
5169
|
throw new Error(ERR_INVALID_EXTRAFIELD_TYPE);
|
|
@@ -4753,22 +5171,72 @@
|
|
|
4753
5171
|
if (getLength(data) > MAX_16_BITS) {
|
|
4754
5172
|
throw new Error(ERR_INVALID_EXTRAFIELD_DATA);
|
|
4755
5173
|
}
|
|
4756
|
-
|
|
4757
|
-
|
|
5174
|
+
setUint16(rawExtraFieldView, offset, type);
|
|
5175
|
+
setUint16(rawExtraFieldView, offset + 2, getLength(data));
|
|
4758
5176
|
arraySet(rawExtraField, data, offset + 4);
|
|
4759
5177
|
offset += 4 + getLength(data);
|
|
4760
5178
|
});
|
|
4761
5179
|
}
|
|
5180
|
+
return {
|
|
5181
|
+
comment,
|
|
5182
|
+
resolvedOptions: {
|
|
5183
|
+
rawFilename,
|
|
5184
|
+
rawComment,
|
|
5185
|
+
version,
|
|
5186
|
+
lastModDate,
|
|
5187
|
+
lastAccessDate,
|
|
5188
|
+
creationDate,
|
|
5189
|
+
internalFileAttributes,
|
|
5190
|
+
passThrough,
|
|
5191
|
+
password,
|
|
5192
|
+
rawPassword,
|
|
5193
|
+
encryptionStrength,
|
|
5194
|
+
zipCrypto,
|
|
5195
|
+
extendedTimestamp,
|
|
5196
|
+
keepOrder,
|
|
5197
|
+
useWebWorkers,
|
|
5198
|
+
transferStreams,
|
|
5199
|
+
bufferedWrite,
|
|
5200
|
+
createTempStream,
|
|
5201
|
+
dataDescriptorSignature,
|
|
5202
|
+
signal,
|
|
5203
|
+
useUnicodeFileNames,
|
|
5204
|
+
compressionMethod,
|
|
5205
|
+
level,
|
|
5206
|
+
useCompressionStream,
|
|
5207
|
+
dataDescriptor,
|
|
5208
|
+
zip64,
|
|
5209
|
+
rawExtraField
|
|
5210
|
+
}
|
|
5211
|
+
};
|
|
5212
|
+
}
|
|
5213
|
+
|
|
5214
|
+
async function resolveSizes(zipWriter, reader, { resolvedOptions: metadata }, options) {
|
|
5215
|
+
const { passThrough, zipCrypto, password, rawPassword, encryptionStrength } = metadata;
|
|
5216
|
+
let { dataDescriptor, zip64, level, compressionMethod } = metadata;
|
|
4762
5217
|
let maximumCompressedSize = 0;
|
|
4763
|
-
let maximumEntrySize = 0;
|
|
4764
5218
|
let uncompressedSize = 0;
|
|
4765
5219
|
if (passThrough) {
|
|
5220
|
+
// the headers of a passThrough entry describe the payload verbatim: without a
|
|
5221
|
+
// reader they would declare content that is not there
|
|
5222
|
+
if (!reader) {
|
|
5223
|
+
throw new Error(ERR_UNDEFINED_READER);
|
|
5224
|
+
}
|
|
4766
5225
|
uncompressedSize = options[PROPERTY_NAME_UNCOMPRESSED_SIZE];
|
|
4767
5226
|
if (uncompressedSize === UNDEFINED_VALUE) {
|
|
4768
5227
|
throw new Error(ERR_UNDEFINED_UNCOMPRESSED_SIZE);
|
|
4769
5228
|
}
|
|
4770
5229
|
}
|
|
4771
5230
|
const zip64Enabled = zip64 === true;
|
|
5231
|
+
const encrypted = getOptionValue(zipWriter, options, PROPERTY_NAME_ENCRYPTED);
|
|
5232
|
+
// entries without a reader have no payload: write them as empty stored files instead of
|
|
5233
|
+
// declaring a compressed or encrypted content that does not exist
|
|
5234
|
+
const encryptedEntry = Boolean(reader) && (Boolean((password && getLength(password)) || (rawPassword && getLength(rawPassword))) || (passThrough && encrypted));
|
|
5235
|
+
if (!reader) {
|
|
5236
|
+
level = 0;
|
|
5237
|
+
compressionMethod = COMPRESSION_METHOD_STORE;
|
|
5238
|
+
}
|
|
5239
|
+
const encryptionOverhead = encryptedEntry ? (zipCrypto ? 12 : 16 + encryptionStrength * 4) : 0;
|
|
4772
5240
|
if (reader) {
|
|
4773
5241
|
reader = new GenericReader(reader);
|
|
4774
5242
|
await initStream(reader);
|
|
@@ -4781,16 +5249,15 @@
|
|
|
4781
5249
|
}
|
|
4782
5250
|
} else {
|
|
4783
5251
|
options.uncompressedSize = uncompressedSize = reader.size;
|
|
4784
|
-
maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
|
|
5252
|
+
maximumCompressedSize = getMaximumCompressedSize(uncompressedSize) + encryptionOverhead;
|
|
4785
5253
|
}
|
|
4786
5254
|
} else {
|
|
4787
5255
|
options.uncompressedSize = uncompressedSize;
|
|
4788
|
-
maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
|
|
5256
|
+
maximumCompressedSize = getMaximumCompressedSize(uncompressedSize) + encryptionOverhead;
|
|
4789
5257
|
}
|
|
4790
5258
|
}
|
|
4791
|
-
const
|
|
4792
|
-
const
|
|
4793
|
-
const zip64CompressedSize = zip64Enabled || maximumCompressedSize > MAX_32_BITS;
|
|
5259
|
+
const zip64UncompressedSize = zip64Enabled || uncompressedSize >= MAX_32_BITS;
|
|
5260
|
+
const zip64CompressedSize = zip64Enabled || maximumCompressedSize >= MAX_32_BITS;
|
|
4794
5261
|
if (zip64UncompressedSize || zip64CompressedSize) {
|
|
4795
5262
|
if (zip64 === false) {
|
|
4796
5263
|
throw new Error(ERR_UNSUPPORTED_FORMAT);
|
|
@@ -4799,73 +5266,19 @@
|
|
|
4799
5266
|
}
|
|
4800
5267
|
}
|
|
4801
5268
|
zip64 = zip64 || false;
|
|
4802
|
-
|
|
4803
|
-
|
|
4804
|
-
|
|
4805
|
-
|
|
4806
|
-
|
|
4807
|
-
|
|
4808
|
-
|
|
4809
|
-
|
|
4810
|
-
|
|
4811
|
-
|
|
4812
|
-
|
|
4813
|
-
|
|
4814
|
-
|
|
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);
|
|
5269
|
+
return {
|
|
5270
|
+
reader,
|
|
5271
|
+
resolvedOptions: {
|
|
5272
|
+
dataDescriptor,
|
|
5273
|
+
zip64,
|
|
5274
|
+
zip64UncompressedSize,
|
|
5275
|
+
zip64CompressedSize,
|
|
5276
|
+
uncompressedSize,
|
|
5277
|
+
level,
|
|
5278
|
+
compressionMethod,
|
|
5279
|
+
encrypted: encryptedEntry
|
|
5280
|
+
}
|
|
5281
|
+
};
|
|
4869
5282
|
}
|
|
4870
5283
|
|
|
4871
5284
|
async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
|
|
@@ -4882,37 +5295,40 @@
|
|
|
4882
5295
|
headerInfo
|
|
4883
5296
|
} = entryInfo;
|
|
4884
5297
|
const usdz = zipWriter.options[OPTION_USDZ];
|
|
4885
|
-
const previousFileEntry =
|
|
5298
|
+
const previousFileEntry = zipWriter.lastFileEntry;
|
|
4886
5299
|
let fileEntry = {};
|
|
4887
5300
|
let bufferedWrite;
|
|
4888
5301
|
let releaseLockWriter;
|
|
4889
5302
|
let releaseLockCurrentFileEntry;
|
|
4890
5303
|
let writingBufferedEntryData;
|
|
4891
5304
|
let writingEntryData;
|
|
5305
|
+
let writerSizeBeforeEntry;
|
|
5306
|
+
let flushedBufferedSize = 0;
|
|
4892
5307
|
let fileWriter;
|
|
4893
5308
|
files.set(name, fileEntry);
|
|
5309
|
+
zipWriter.lastFileEntry = fileEntry;
|
|
4894
5310
|
try {
|
|
4895
5311
|
let lockPreviousFileEntry;
|
|
4896
5312
|
if (keepOrder) {
|
|
4897
5313
|
lockPreviousFileEntry = previousFileEntry && previousFileEntry.lock;
|
|
4898
5314
|
requestLockCurrentFileEntry();
|
|
4899
5315
|
}
|
|
4900
|
-
if (
|
|
5316
|
+
if (options.bufferedWrite || !keepOrder || zipWriter.writerLocked || zipWriter.bufferedWrites || !dataDescriptor) {
|
|
5317
|
+
bufferedWrite = true;
|
|
5318
|
+
zipWriter.bufferedWrites++;
|
|
4901
5319
|
if (options.createTempStream) {
|
|
4902
5320
|
fileWriter = await options.createTempStream();
|
|
4903
5321
|
} else {
|
|
4904
5322
|
fileWriter = new TransformStream(UNDEFINED_VALUE, UNDEFINED_VALUE, { highWaterMark: INFINITY_VALUE });
|
|
4905
5323
|
}
|
|
4906
5324
|
fileWriter.size = 0;
|
|
4907
|
-
bufferedWrite = true;
|
|
4908
|
-
zipWriter.bufferedWrites++;
|
|
4909
5325
|
await initStream(writer);
|
|
4910
5326
|
} else {
|
|
4911
5327
|
fileWriter = writer;
|
|
4912
5328
|
await requestLockWriter();
|
|
4913
5329
|
}
|
|
4914
5330
|
await initStream(fileWriter);
|
|
4915
|
-
const {
|
|
5331
|
+
const { diskOffset } = writer;
|
|
4916
5332
|
if (zipWriter.addSplitZipSignature) {
|
|
4917
5333
|
delete zipWriter.addSplitZipSignature;
|
|
4918
5334
|
const signatureArray = new Uint8Array(4);
|
|
@@ -4921,21 +5337,20 @@
|
|
|
4921
5337
|
await writeData(writer, signatureArray);
|
|
4922
5338
|
zipWriter.offset += 4;
|
|
4923
5339
|
}
|
|
4924
|
-
if (usdz) {
|
|
5340
|
+
if (usdz && !bufferedWrite) {
|
|
4925
5341
|
appendExtraFieldUSDZ(entryInfo, zipWriter.offset - diskOffset);
|
|
4926
5342
|
}
|
|
4927
|
-
const {
|
|
4928
|
-
localHeaderView,
|
|
4929
|
-
localHeaderArray
|
|
4930
|
-
} = headerInfo;
|
|
5343
|
+
const { localHeaderArray } = headerInfo;
|
|
4931
5344
|
if (!bufferedWrite) {
|
|
4932
5345
|
await lockPreviousFileEntry;
|
|
4933
|
-
await skipDiskIfNeeded(
|
|
5346
|
+
await skipDiskIfNeeded();
|
|
4934
5347
|
}
|
|
4935
|
-
const
|
|
4936
|
-
|
|
5348
|
+
const diskNumberStart = writer.diskNumber;
|
|
5349
|
+
const entryOffset = getSegmentOffset(zipWriter, writer);
|
|
5350
|
+
fileEntry.diskNumberStart = diskNumberStart;
|
|
4937
5351
|
if (!bufferedWrite) {
|
|
4938
5352
|
writingEntryData = true;
|
|
5353
|
+
writerSizeBeforeEntry = writer.size;
|
|
4939
5354
|
await writeData(fileWriter, localHeaderArray);
|
|
4940
5355
|
}
|
|
4941
5356
|
fileEntry = await createFileEntry(reader, fileWriter, fileEntry, entryInfo, zipWriter.config, options);
|
|
@@ -4948,16 +5363,24 @@
|
|
|
4948
5363
|
await Promise.all([fileWriter.writable.getWriter().close(), lockPreviousFileEntry]);
|
|
4949
5364
|
await requestLockWriter();
|
|
4950
5365
|
writingBufferedEntryData = true;
|
|
5366
|
+
writerSizeBeforeEntry = writer.size;
|
|
5367
|
+
await skipDiskIfNeeded();
|
|
4951
5368
|
fileEntry.diskNumberStart = writer.diskNumber;
|
|
4952
|
-
fileEntry.offset = zipWriter
|
|
4953
|
-
|
|
4954
|
-
|
|
4955
|
-
|
|
4956
|
-
|
|
5369
|
+
fileEntry.offset = getSegmentOffset(zipWriter, writer);
|
|
5370
|
+
if (usdz) {
|
|
5371
|
+
// the padding depends on the entry offset, only known now that the writer is locked
|
|
5372
|
+
const previousMetadataSize = entryInfo.metadataSize;
|
|
5373
|
+
appendExtraFieldUSDZ(entryInfo, zipWriter.offset - writer.diskOffset);
|
|
5374
|
+
fileEntry.size += entryInfo.metadataSize - previousMetadataSize;
|
|
5375
|
+
}
|
|
5376
|
+
updateLocalHeader(fileEntry, headerInfo.localHeaderView, options);
|
|
5377
|
+
await writeData(writer, headerInfo.localHeaderArray);
|
|
5378
|
+
await flushBufferedData(fileWriter.readable, writer, signal, chunkLength => flushedBufferedSize += chunkLength);
|
|
4957
5379
|
writer.size += fileWriter.size;
|
|
4958
5380
|
writingBufferedEntryData = false;
|
|
4959
5381
|
} else {
|
|
4960
|
-
fileEntry.
|
|
5382
|
+
fileEntry.diskNumberStart = diskNumberStart;
|
|
5383
|
+
fileEntry.offset = entryOffset;
|
|
4961
5384
|
}
|
|
4962
5385
|
zipWriter.offset += fileEntry.size;
|
|
4963
5386
|
return fileEntry;
|
|
@@ -4971,10 +5394,9 @@
|
|
|
4971
5394
|
// ignored
|
|
4972
5395
|
}
|
|
4973
5396
|
}
|
|
5397
|
+
zipWriter.offset += writer.size - writerSizeBeforeEntry;
|
|
4974
5398
|
if (bufferedWrite) {
|
|
4975
|
-
zipWriter.offset +=
|
|
4976
|
-
} else {
|
|
4977
|
-
zipWriter.offset = fileWriter.size;
|
|
5399
|
+
zipWriter.offset += flushedBufferedSize;
|
|
4978
5400
|
}
|
|
4979
5401
|
}
|
|
4980
5402
|
files.delete(name);
|
|
@@ -5005,10 +5427,10 @@
|
|
|
5005
5427
|
await lockWriter;
|
|
5006
5428
|
}
|
|
5007
5429
|
|
|
5008
|
-
async function skipDiskIfNeeded(
|
|
5430
|
+
async function skipDiskIfNeeded() {
|
|
5009
5431
|
if (getLength(headerInfo.localHeaderArray) > writer.availableSize) {
|
|
5010
5432
|
writer.availableSize = 0;
|
|
5011
|
-
await writeData(
|
|
5433
|
+
await writeData(writer, new Uint8Array());
|
|
5012
5434
|
}
|
|
5013
5435
|
}
|
|
5014
5436
|
}
|
|
@@ -5149,6 +5571,10 @@
|
|
|
5149
5571
|
uncompressedSize = result.inputSize;
|
|
5150
5572
|
signature = result.signature;
|
|
5151
5573
|
}
|
|
5574
|
+
if ((!zip64CompressedSize && compressedSize >= MAX_32_BITS) ||
|
|
5575
|
+
(!zip64UncompressedSize && uncompressedSize >= MAX_32_BITS)) {
|
|
5576
|
+
throw new Error(ERR_UNSUPPORTED_FORMAT);
|
|
5577
|
+
}
|
|
5152
5578
|
} catch (error) {
|
|
5153
5579
|
if (error.outputSize !== UNDEFINED_VALUE) {
|
|
5154
5580
|
writer.size += error.outputSize;
|
|
@@ -5211,35 +5637,28 @@
|
|
|
5211
5637
|
uncompressedSize
|
|
5212
5638
|
} = options;
|
|
5213
5639
|
let { version, compressionMethod } = options;
|
|
5214
|
-
const compressed = !directory && (
|
|
5640
|
+
const compressed = !directory && (compressionMethod === UNDEFINED_VALUE
|
|
5641
|
+
? (level === UNDEFINED_VALUE || level > 0)
|
|
5642
|
+
: compressionMethod !== COMPRESSION_METHOD_STORE);
|
|
5215
5643
|
let rawLocalExtraFieldZip64;
|
|
5216
5644
|
const uncompressedFile = passThrough || !compressed;
|
|
5217
|
-
|
|
5218
|
-
|
|
5219
|
-
|
|
5220
|
-
|
|
5221
|
-
|
|
5222
|
-
|
|
5223
|
-
|
|
5224
|
-
|
|
5225
|
-
|
|
5226
|
-
|
|
5227
|
-
|
|
5228
|
-
|
|
5229
|
-
setUint16(rawLocalExtraFieldZip64View, 2, getLength(rawLocalExtraFieldZip64) - 4);
|
|
5645
|
+
// entries without a data descriptor are always buffered (cf. getFileEntry), so the
|
|
5646
|
+
// zip64 extra field values can be patched into the local header before flushing it
|
|
5647
|
+
const zip64ExtraFieldComplete = zip64 && (options.bufferedWrite || !dataDescriptor || ((!zip64UncompressedSize && !zip64CompressedSize) || uncompressedFile));
|
|
5648
|
+
const writeLocalExtraFieldZip64 = zip64ExtraFieldComplete || (zip64 && dataDescriptor && (zip64UncompressedSize || zip64CompressedSize));
|
|
5649
|
+
if (zip64 && (zip64UncompressedSize || zip64CompressedSize)) {
|
|
5650
|
+
// APPNOTE 4.5.3: the zip64 extra field of a local header must include both the
|
|
5651
|
+
// uncompressed and compressed size fields
|
|
5652
|
+
const length = 4 + 16;
|
|
5653
|
+
const extraFieldZip64 = createRecordWriter(length);
|
|
5654
|
+
extraFieldZip64.uint16(EXTRAFIELD_TYPE_ZIP64);
|
|
5655
|
+
extraFieldZip64.uint16(length - 4);
|
|
5656
|
+
rawLocalExtraFieldZip64 = extraFieldZip64.array;
|
|
5230
5657
|
if (zip64ExtraFieldComplete) {
|
|
5231
|
-
|
|
5232
|
-
|
|
5233
|
-
|
|
5234
|
-
|
|
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();
|
|
5658
|
+
extraFieldZip64.uint64(uncompressedSize);
|
|
5659
|
+
if (uncompressedFile) {
|
|
5660
|
+
const encryptionOverhead = encrypted ? (zipCrypto ? 12 : 16 + encryptionStrength * 4) : 0;
|
|
5661
|
+
extraFieldZip64.uint64(passThrough ? 0 : uncompressedSize + encryptionOverhead);
|
|
5243
5662
|
}
|
|
5244
5663
|
}
|
|
5245
5664
|
} else {
|
|
@@ -5247,11 +5666,11 @@
|
|
|
5247
5666
|
}
|
|
5248
5667
|
let rawExtraFieldAES;
|
|
5249
5668
|
if (encrypted && !zipCrypto) {
|
|
5250
|
-
|
|
5251
|
-
|
|
5252
|
-
|
|
5253
|
-
|
|
5254
|
-
|
|
5669
|
+
const extraFieldAES = createRecordWriter(getLength(EXTRAFIELD_DATA_AES) + 2);
|
|
5670
|
+
extraFieldAES.uint16(EXTRAFIELD_TYPE_AES);
|
|
5671
|
+
extraFieldAES.bytes(EXTRAFIELD_DATA_AES);
|
|
5672
|
+
rawExtraFieldAES = extraFieldAES.array;
|
|
5673
|
+
rawExtraFieldAES[8] = encryptionStrength;
|
|
5255
5674
|
} else {
|
|
5256
5675
|
rawExtraFieldAES = new Uint8Array();
|
|
5257
5676
|
}
|
|
@@ -5259,33 +5678,38 @@
|
|
|
5259
5678
|
let rawExtraFieldExtendedTimestamp;
|
|
5260
5679
|
let extraFieldExtendedTimestampFlag;
|
|
5261
5680
|
if (extendedTimestamp) {
|
|
5262
|
-
|
|
5263
|
-
|
|
5264
|
-
|
|
5265
|
-
|
|
5266
|
-
|
|
5267
|
-
|
|
5268
|
-
|
|
5269
|
-
|
|
5270
|
-
|
|
5271
|
-
|
|
5272
|
-
|
|
5273
|
-
|
|
5274
|
-
|
|
5275
|
-
|
|
5276
|
-
|
|
5681
|
+
const lastModTimeUnix = getTimeUnix(lastModDate);
|
|
5682
|
+
if (inUnixTimeRange(lastModTimeUnix)) {
|
|
5683
|
+
const extraFieldTimestampLength = 9 + (lastAccessDate ? 4 : 0) + (creationDate ? 4 : 0);
|
|
5684
|
+
const extraFieldTimestamp = createRecordWriter(extraFieldTimestampLength);
|
|
5685
|
+
extraFieldExtendedTimestampFlag = 0x1 + (lastAccessDate ? 0x2 : 0) + (creationDate ? 0x4 : 0);
|
|
5686
|
+
extraFieldTimestamp.uint16(EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP);
|
|
5687
|
+
extraFieldTimestamp.uint16(extraFieldTimestampLength - 4);
|
|
5688
|
+
extraFieldTimestamp.uint8(extraFieldExtendedTimestampFlag);
|
|
5689
|
+
extraFieldTimestamp.uint32(lastModTimeUnix);
|
|
5690
|
+
if (lastAccessDate) {
|
|
5691
|
+
extraFieldTimestamp.uint32(clampUnixTime(getTimeUnix(lastAccessDate)));
|
|
5692
|
+
}
|
|
5693
|
+
if (creationDate) {
|
|
5694
|
+
extraFieldTimestamp.uint32(clampUnixTime(getTimeUnix(creationDate)));
|
|
5695
|
+
}
|
|
5696
|
+
rawExtraFieldExtendedTimestamp = extraFieldTimestamp.array;
|
|
5697
|
+
} else {
|
|
5698
|
+
// the date cannot be stored as a 32-bit time_t; the NTFS field replaces it
|
|
5699
|
+
rawExtraFieldExtendedTimestamp = new Uint8Array();
|
|
5277
5700
|
}
|
|
5278
5701
|
try {
|
|
5279
|
-
rawExtraFieldNTFS = new Uint8Array(36);
|
|
5280
|
-
const extraFieldNTFSView = getDataView(rawExtraFieldNTFS);
|
|
5281
5702
|
const lastModTimeNTFS = getTimeNTFS(lastModDate);
|
|
5282
|
-
|
|
5283
|
-
|
|
5284
|
-
|
|
5285
|
-
|
|
5286
|
-
|
|
5287
|
-
|
|
5288
|
-
|
|
5703
|
+
const extraFieldNTFS = createRecordWriter(36);
|
|
5704
|
+
extraFieldNTFS.uint16(EXTRAFIELD_TYPE_NTFS);
|
|
5705
|
+
extraFieldNTFS.uint16(32);
|
|
5706
|
+
extraFieldNTFS.skip(4);
|
|
5707
|
+
extraFieldNTFS.uint16(EXTRAFIELD_TYPE_NTFS_TAG1);
|
|
5708
|
+
extraFieldNTFS.uint16(24);
|
|
5709
|
+
extraFieldNTFS.uint64(lastModTimeNTFS);
|
|
5710
|
+
extraFieldNTFS.uint64(getTimeNTFS(lastAccessDate) || lastModTimeNTFS);
|
|
5711
|
+
extraFieldNTFS.uint64(getTimeNTFS(creationDate) || lastModTimeNTFS);
|
|
5712
|
+
rawExtraFieldNTFS = extraFieldNTFS.array;
|
|
5289
5713
|
} catch {
|
|
5290
5714
|
rawExtraFieldNTFS = new Uint8Array();
|
|
5291
5715
|
}
|
|
@@ -5294,41 +5718,27 @@
|
|
|
5294
5718
|
}
|
|
5295
5719
|
let rawExtraFieldUnix;
|
|
5296
5720
|
try {
|
|
5297
|
-
const { uid, gid,
|
|
5298
|
-
if (unixExtraFieldType && (uid !== UNDEFINED_VALUE || gid !== UNDEFINED_VALUE
|
|
5721
|
+
const { uid, gid, unixExtraFieldType } = options;
|
|
5722
|
+
if (unixExtraFieldType == INFOZIP_EXTRA_FIELD_TYPE && (uid !== UNDEFINED_VALUE || gid !== UNDEFINED_VALUE)) {
|
|
5299
5723
|
const uidBytes = packUnixId(uid);
|
|
5300
5724
|
const gidBytes = packUnixId(gid);
|
|
5301
|
-
|
|
5302
|
-
|
|
5303
|
-
|
|
5304
|
-
|
|
5305
|
-
|
|
5306
|
-
|
|
5307
|
-
|
|
5308
|
-
|
|
5309
|
-
|
|
5310
|
-
|
|
5311
|
-
|
|
5312
|
-
|
|
5313
|
-
|
|
5314
|
-
|
|
5315
|
-
|
|
5316
|
-
|
|
5317
|
-
|
|
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);
|
|
5725
|
+
const payloadLength = 3 + uidBytes.length + gidBytes.length;
|
|
5726
|
+
const extraFieldUnix = createRecordWriter(4 + payloadLength);
|
|
5727
|
+
extraFieldUnix.uint16(EXTRAFIELD_TYPE_INFOZIP);
|
|
5728
|
+
extraFieldUnix.uint16(payloadLength);
|
|
5729
|
+
extraFieldUnix.uint8(1);
|
|
5730
|
+
extraFieldUnix.uint8(uidBytes.length);
|
|
5731
|
+
extraFieldUnix.bytes(uidBytes);
|
|
5732
|
+
extraFieldUnix.uint8(gidBytes.length);
|
|
5733
|
+
extraFieldUnix.bytes(gidBytes);
|
|
5734
|
+
rawExtraFieldUnix = extraFieldUnix.array;
|
|
5735
|
+
} else if (unixExtraFieldType == UNIX_EXTRA_FIELD_TYPE && (uid !== UNDEFINED_VALUE || gid !== UNDEFINED_VALUE)) {
|
|
5736
|
+
const extraFieldUnix = createRecordWriter(8);
|
|
5737
|
+
extraFieldUnix.uint16(EXTRAFIELD_TYPE_UNIX);
|
|
5738
|
+
extraFieldUnix.uint16(4);
|
|
5739
|
+
extraFieldUnix.uint16((uid === UNDEFINED_VALUE ? 0 : uid) & MAX_16_BITS);
|
|
5740
|
+
extraFieldUnix.uint16((gid === UNDEFINED_VALUE ? 0 : gid) & MAX_16_BITS);
|
|
5741
|
+
rawExtraFieldUnix = extraFieldUnix.array;
|
|
5332
5742
|
} else {
|
|
5333
5743
|
rawExtraFieldUnix = new Uint8Array();
|
|
5334
5744
|
}
|
|
@@ -5346,8 +5756,11 @@
|
|
|
5346
5756
|
rawExtraFieldAES[9] = compressionMethod;
|
|
5347
5757
|
compressionMethod = COMPRESSION_METHOD_AES;
|
|
5348
5758
|
}
|
|
5349
|
-
const localExtraFieldZip64Length =
|
|
5759
|
+
const localExtraFieldZip64Length = writeLocalExtraFieldZip64 ? getLength(rawLocalExtraFieldZip64) : 0;
|
|
5350
5760
|
const extraFieldLength = localExtraFieldZip64Length + getLength(rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraFieldUnix, rawExtraField);
|
|
5761
|
+
if (extraFieldLength > MAX_16_BITS) {
|
|
5762
|
+
throw new Error(ERR_INVALID_EXTRAFIELD_DATA);
|
|
5763
|
+
}
|
|
5351
5764
|
const {
|
|
5352
5765
|
headerArray,
|
|
5353
5766
|
headerView,
|
|
@@ -5363,29 +5776,27 @@
|
|
|
5363
5776
|
zip64UncompressedSize,
|
|
5364
5777
|
extraFieldLength
|
|
5365
5778
|
});
|
|
5366
|
-
|
|
5367
|
-
const localHeaderArray =
|
|
5779
|
+
const localHeader = createRecordWriter(HEADER_SIZE + getLength(rawFilename) + extraFieldLength);
|
|
5780
|
+
const localHeaderArray = localHeader.array;
|
|
5368
5781
|
const localHeaderView = getDataView(localHeaderArray);
|
|
5369
|
-
|
|
5370
|
-
|
|
5371
|
-
|
|
5372
|
-
|
|
5373
|
-
|
|
5374
|
-
|
|
5375
|
-
|
|
5376
|
-
|
|
5377
|
-
|
|
5378
|
-
|
|
5379
|
-
|
|
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);
|
|
5782
|
+
localHeader.uint32(LOCAL_FILE_HEADER_SIGNATURE);
|
|
5783
|
+
localHeader.bytes(headerArray);
|
|
5784
|
+
localHeader.bytes(rawFilename);
|
|
5785
|
+
if (writeLocalExtraFieldZip64) {
|
|
5786
|
+
localHeader.bytes(rawLocalExtraFieldZip64);
|
|
5787
|
+
}
|
|
5788
|
+
localHeader.bytes(rawExtraFieldAES);
|
|
5789
|
+
localHeader.bytes(rawExtraFieldExtendedTimestamp);
|
|
5790
|
+
localHeader.bytes(rawExtraFieldNTFS);
|
|
5791
|
+
localHeader.bytes(rawExtraFieldUnix);
|
|
5792
|
+
localHeader.bytes(rawExtraField);
|
|
5386
5793
|
if (dataDescriptor) {
|
|
5387
|
-
|
|
5388
|
-
|
|
5794
|
+
if (!zip64CompressedSize) {
|
|
5795
|
+
setUint32(localHeaderView, HEADER_OFFSET_COMPRESSED_SIZE + LOCAL_HEADER_COMMON_OFFSET, 0);
|
|
5796
|
+
}
|
|
5797
|
+
if (!zip64UncompressedSize) {
|
|
5798
|
+
setUint32(localHeaderView, HEADER_OFFSET_UNCOMPRESSED_SIZE + LOCAL_HEADER_COMMON_OFFSET, 0);
|
|
5799
|
+
}
|
|
5389
5800
|
}
|
|
5390
5801
|
return {
|
|
5391
5802
|
localHeaderArray,
|
|
@@ -5412,7 +5823,7 @@
|
|
|
5412
5823
|
function appendExtraFieldUSDZ(entryInfo, zipWriterOffset) {
|
|
5413
5824
|
const { headerInfo } = entryInfo;
|
|
5414
5825
|
let { localHeaderArray, extraFieldLength } = headerInfo;
|
|
5415
|
-
let localHeaderArrayView
|
|
5826
|
+
let localHeaderArrayView;
|
|
5416
5827
|
let extraBytesLength = 64 - ((zipWriterOffset + getLength(localHeaderArray)) % 64);
|
|
5417
5828
|
if (extraBytesLength < 4) {
|
|
5418
5829
|
extraBytesLength += 64;
|
|
@@ -5420,13 +5831,14 @@
|
|
|
5420
5831
|
const rawExtraFieldUSDZ = new Uint8Array(extraBytesLength);
|
|
5421
5832
|
const extraFieldUSDZView = getDataView(rawExtraFieldUSDZ);
|
|
5422
5833
|
setUint16(extraFieldUSDZView, 0, EXTRAFIELD_TYPE_USDZ);
|
|
5423
|
-
setUint16(extraFieldUSDZView, 2, extraBytesLength -
|
|
5834
|
+
setUint16(extraFieldUSDZView, 2, extraBytesLength - 4);
|
|
5424
5835
|
const previousLocalHeaderArray = localHeaderArray;
|
|
5425
5836
|
headerInfo.localHeaderArray = localHeaderArray = new Uint8Array(getLength(previousLocalHeaderArray) + extraBytesLength);
|
|
5426
5837
|
arraySet(localHeaderArray, previousLocalHeaderArray);
|
|
5427
5838
|
arraySet(localHeaderArray, rawExtraFieldUSDZ, getLength(previousLocalHeaderArray));
|
|
5428
5839
|
localHeaderArrayView = getDataView(localHeaderArray);
|
|
5429
5840
|
setUint16(localHeaderArrayView, 28, extraFieldLength + extraBytesLength);
|
|
5841
|
+
headerInfo.localHeaderView = localHeaderArrayView;
|
|
5430
5842
|
entryInfo.metadataSize += extraBytesLength;
|
|
5431
5843
|
}
|
|
5432
5844
|
|
|
@@ -5549,37 +5961,32 @@
|
|
|
5549
5961
|
}, localHeaderView, { dataDescriptor }) {
|
|
5550
5962
|
if (!dataDescriptor) {
|
|
5551
5963
|
if (!encrypted) {
|
|
5552
|
-
setUint32(localHeaderView, HEADER_OFFSET_SIGNATURE +
|
|
5964
|
+
setUint32(localHeaderView, HEADER_OFFSET_SIGNATURE + LOCAL_HEADER_COMMON_OFFSET, signature);
|
|
5553
5965
|
}
|
|
5554
5966
|
if (!zip64CompressedSize) {
|
|
5555
|
-
setUint32(localHeaderView, HEADER_OFFSET_COMPRESSED_SIZE +
|
|
5967
|
+
setUint32(localHeaderView, HEADER_OFFSET_COMPRESSED_SIZE + LOCAL_HEADER_COMMON_OFFSET, compressedSize);
|
|
5556
5968
|
}
|
|
5557
5969
|
if (!zip64UncompressedSize) {
|
|
5558
|
-
setUint32(localHeaderView, HEADER_OFFSET_UNCOMPRESSED_SIZE +
|
|
5970
|
+
setUint32(localHeaderView, HEADER_OFFSET_UNCOMPRESSED_SIZE + LOCAL_HEADER_COMMON_OFFSET, uncompressedSize);
|
|
5559
5971
|
}
|
|
5560
5972
|
}
|
|
5561
5973
|
if (zip64 && localExtraFieldZip64Length) {
|
|
5562
|
-
|
|
5563
|
-
|
|
5564
|
-
|
|
5565
|
-
|
|
5566
|
-
}
|
|
5567
|
-
if (zip64CompressedSize) {
|
|
5568
|
-
setBigUint64(localHeaderView, localHeaderOffset, BigInt(compressedSize));
|
|
5569
|
-
localHeaderOffset += 8;
|
|
5570
|
-
}
|
|
5974
|
+
// the local zip64 extra field always contains both size slots
|
|
5975
|
+
const localHeaderOffset = HEADER_SIZE + getLength(rawFilename) + 4;
|
|
5976
|
+
setBigUint64(localHeaderView, localHeaderOffset, BigInt(uncompressedSize));
|
|
5977
|
+
setBigUint64(localHeaderView, localHeaderOffset + 8, BigInt(compressedSize));
|
|
5571
5978
|
}
|
|
5572
5979
|
}
|
|
5573
5980
|
|
|
5574
5981
|
|
|
5575
5982
|
async function closeFile(zipWriter, comment, options) {
|
|
5576
|
-
const
|
|
5577
|
-
const {
|
|
5578
|
-
|
|
5579
|
-
|
|
5983
|
+
const directoryDataLength = createDirectoryRecords(zipWriter.files);
|
|
5984
|
+
const { cdStartDiskNumber, cdStartDiskOffset } = await writeDirectoryRecords(zipWriter, directoryDataLength, options);
|
|
5985
|
+
await writeEndOfDirectoryRecord(zipWriter, comment, options, { cdStartDiskNumber, cdStartDiskOffset, directoryDataLength });
|
|
5986
|
+
}
|
|
5987
|
+
|
|
5988
|
+
function createDirectoryRecords(files) {
|
|
5580
5989
|
let directoryDataLength = 0;
|
|
5581
|
-
let directoryOffset = zipWriter.offset - diskOffset;
|
|
5582
|
-
let filesLength = files.size;
|
|
5583
5990
|
for (const [, fileEntry] of files) {
|
|
5584
5991
|
const {
|
|
5585
5992
|
rawFilename,
|
|
@@ -5596,24 +6003,27 @@
|
|
|
5596
6003
|
uncompressedSize,
|
|
5597
6004
|
compressedSize
|
|
5598
6005
|
} = fileEntry;
|
|
5599
|
-
const zip64Offset = fileEntry.offset
|
|
5600
|
-
const zip64DiskNumberStart = fileEntry.diskNumberStart
|
|
6006
|
+
const zip64Offset = fileEntry.offset >= MAX_32_BITS;
|
|
6007
|
+
const zip64DiskNumberStart = fileEntry.diskNumberStart >= MAX_16_BITS;
|
|
5601
6008
|
let rawExtraFieldZip64;
|
|
5602
6009
|
if (zip64Offset || zip64DiskNumberStart || zip64UncompressedSize || zip64CompressedSize) {
|
|
5603
|
-
|
|
5604
|
-
|
|
5605
|
-
|
|
5606
|
-
|
|
5607
|
-
if (
|
|
5608
|
-
|
|
5609
|
-
|
|
5610
|
-
|
|
5611
|
-
|
|
5612
|
-
|
|
5613
|
-
if (
|
|
5614
|
-
|
|
5615
|
-
|
|
5616
|
-
if (zip64DiskNumberStart) {
|
|
6010
|
+
const length = 4 + (zip64UncompressedSize ? 8 : 0) + (zip64CompressedSize ? 8 : 0) + (zip64Offset ? 8 : 0) + (zip64DiskNumberStart ? 4 : 0);
|
|
6011
|
+
const extraFieldZip64 = createRecordWriter(length);
|
|
6012
|
+
extraFieldZip64.uint16(EXTRAFIELD_TYPE_ZIP64);
|
|
6013
|
+
extraFieldZip64.uint16(length - 4);
|
|
6014
|
+
if (zip64UncompressedSize) {
|
|
6015
|
+
extraFieldZip64.uint64(uncompressedSize);
|
|
6016
|
+
}
|
|
6017
|
+
if (zip64CompressedSize) {
|
|
6018
|
+
extraFieldZip64.uint64(compressedSize);
|
|
6019
|
+
}
|
|
6020
|
+
if (zip64Offset) {
|
|
6021
|
+
extraFieldZip64.uint64(fileEntry.offset);
|
|
6022
|
+
}
|
|
6023
|
+
if (zip64DiskNumberStart) {
|
|
6024
|
+
extraFieldZip64.uint32(fileEntry.diskNumberStart);
|
|
6025
|
+
}
|
|
6026
|
+
rawExtraFieldZip64 = extraFieldZip64.array;
|
|
5617
6027
|
} else {
|
|
5618
6028
|
rawExtraFieldZip64 = new Uint8Array();
|
|
5619
6029
|
}
|
|
@@ -5621,32 +6031,41 @@
|
|
|
5621
6031
|
fileEntry.zip64Offset = zip64Offset;
|
|
5622
6032
|
fileEntry.zip64DiskNumberStart = zip64DiskNumberStart;
|
|
5623
6033
|
let rawExtraFieldTimestamp;
|
|
5624
|
-
|
|
5625
|
-
|
|
5626
|
-
const
|
|
5627
|
-
|
|
5628
|
-
|
|
5629
|
-
|
|
5630
|
-
|
|
6034
|
+
const lastModTimeUnix = getTimeUnix(lastModDate);
|
|
6035
|
+
if (extendedTimestamp && inUnixTimeRange(lastModTimeUnix)) {
|
|
6036
|
+
const extraFieldTimestamp = createRecordWriter(9);
|
|
6037
|
+
extraFieldTimestamp.uint16(EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP);
|
|
6038
|
+
extraFieldTimestamp.uint16(5);
|
|
6039
|
+
extraFieldTimestamp.uint8(extraFieldExtendedTimestampFlag);
|
|
6040
|
+
extraFieldTimestamp.uint32(lastModTimeUnix);
|
|
6041
|
+
rawExtraFieldTimestamp = extraFieldTimestamp.array;
|
|
5631
6042
|
} else {
|
|
5632
6043
|
rawExtraFieldTimestamp = new Uint8Array();
|
|
5633
6044
|
}
|
|
5634
6045
|
fileEntry.rawExtraFieldExtendedTimestamp = rawExtraFieldTimestamp;
|
|
5635
|
-
|
|
5636
|
-
|
|
5637
|
-
|
|
5638
|
-
|
|
5639
|
-
|
|
5640
|
-
|
|
5641
|
-
|
|
5642
|
-
|
|
5643
|
-
|
|
5644
|
-
|
|
6046
|
+
const extraFieldLength = getLength(
|
|
6047
|
+
rawExtraFieldZip64,
|
|
6048
|
+
rawExtraFieldAES,
|
|
6049
|
+
rawExtraFieldNTFS,
|
|
6050
|
+
rawExtraFieldUnix,
|
|
6051
|
+
rawExtraFieldTimestamp,
|
|
6052
|
+
rawExtraField);
|
|
6053
|
+
if (extraFieldLength > MAX_16_BITS) {
|
|
6054
|
+
throw new Error(ERR_INVALID_EXTRAFIELD_DATA);
|
|
6055
|
+
}
|
|
6056
|
+
directoryDataLength += CENTRAL_FILE_HEADER_LENGTH + getLength(rawFilename, rawComment) + extraFieldLength;
|
|
5645
6057
|
}
|
|
6058
|
+
return directoryDataLength;
|
|
6059
|
+
}
|
|
6060
|
+
|
|
6061
|
+
async function writeDirectoryRecords(zipWriter, directoryDataLength, options) {
|
|
6062
|
+
const { files, writer } = zipWriter;
|
|
5646
6063
|
const directoryArray = new Uint8Array(directoryDataLength);
|
|
5647
|
-
const directoryView = getDataView(directoryArray);
|
|
5648
6064
|
await initStream(writer);
|
|
6065
|
+
let offset = 0;
|
|
5649
6066
|
let directoryDiskOffset = 0;
|
|
6067
|
+
let cdStartDiskNumber = writer.diskNumber;
|
|
6068
|
+
let cdStartDiskOffset = writer.diskOffset;
|
|
5650
6069
|
for (const [indexFileEntry, fileEntry] of Array.from(files.values()).entries()) {
|
|
5651
6070
|
const {
|
|
5652
6071
|
offset: fileEntryOffset,
|
|
@@ -5672,52 +6091,46 @@
|
|
|
5672
6091
|
compressedSize
|
|
5673
6092
|
} = fileEntry;
|
|
5674
6093
|
const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraFieldUnix, rawExtraField);
|
|
5675
|
-
|
|
5676
|
-
|
|
6094
|
+
const directoryRecordLength = CENTRAL_FILE_HEADER_LENGTH + getLength(rawFilename, rawComment) + extraFieldLength;
|
|
6095
|
+
if (offset + directoryRecordLength - directoryDiskOffset > writer.availableSize) {
|
|
6096
|
+
await writeData(writer, directoryArray.slice(directoryDiskOffset, offset));
|
|
6097
|
+
directoryDiskOffset = offset;
|
|
6098
|
+
writer.availableSize = 0;
|
|
6099
|
+
await writeData(writer, new Uint8Array());
|
|
6100
|
+
}
|
|
6101
|
+
if (indexFileEntry == 0) {
|
|
6102
|
+
cdStartDiskNumber = writer.diskNumber;
|
|
6103
|
+
cdStartDiskOffset = writer.diskOffset;
|
|
6104
|
+
}
|
|
5677
6105
|
if (!zip64UncompressedSize) {
|
|
5678
6106
|
setUint32(headerView, HEADER_OFFSET_UNCOMPRESSED_SIZE, uncompressedSize);
|
|
5679
6107
|
}
|
|
5680
6108
|
if (!zip64CompressedSize) {
|
|
5681
6109
|
setUint32(headerView, HEADER_OFFSET_COMPRESSED_SIZE, compressedSize);
|
|
5682
6110
|
}
|
|
5683
|
-
|
|
5684
|
-
|
|
5685
|
-
|
|
5686
|
-
|
|
5687
|
-
|
|
5688
|
-
|
|
5689
|
-
|
|
5690
|
-
|
|
5691
|
-
|
|
5692
|
-
|
|
5693
|
-
|
|
5694
|
-
|
|
5695
|
-
|
|
5696
|
-
|
|
5697
|
-
|
|
5698
|
-
|
|
5699
|
-
|
|
5700
|
-
|
|
5701
|
-
|
|
5702
|
-
|
|
5703
|
-
|
|
5704
|
-
|
|
5705
|
-
|
|
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;
|
|
6111
|
+
if ((zip64Offset || zip64DiskNumberStart) && fileEntry.version < VERSION_ZIP64) {
|
|
6112
|
+
setUint16(headerView, HEADER_OFFSET_VERSION, VERSION_ZIP64);
|
|
6113
|
+
}
|
|
6114
|
+
const directoryRecord = createRecordWriter(directoryRecordLength);
|
|
6115
|
+
directoryRecord.uint32(CENTRAL_FILE_HEADER_SIGNATURE);
|
|
6116
|
+
directoryRecord.uint16(versionMadeBy);
|
|
6117
|
+
directoryRecord.bytes(headerArray.subarray(0, HEADER_SIZE - 4 - 2));
|
|
6118
|
+
directoryRecord.uint16(extraFieldLength);
|
|
6119
|
+
directoryRecord.uint16(getLength(rawComment));
|
|
6120
|
+
directoryRecord.uint16(zip64DiskNumberStart ? MAX_16_BITS : diskNumberStart);
|
|
6121
|
+
directoryRecord.uint16(internalFileAttributes);
|
|
6122
|
+
directoryRecord.uint32(externalFileAttributes);
|
|
6123
|
+
directoryRecord.uint32(zip64Offset ? MAX_32_BITS : fileEntryOffset);
|
|
6124
|
+
directoryRecord.bytes(rawFilename);
|
|
6125
|
+
directoryRecord.bytes(rawExtraFieldZip64);
|
|
6126
|
+
directoryRecord.bytes(rawExtraFieldAES);
|
|
6127
|
+
directoryRecord.bytes(rawExtraFieldExtendedTimestamp);
|
|
6128
|
+
directoryRecord.bytes(rawExtraFieldNTFS);
|
|
6129
|
+
directoryRecord.bytes(rawExtraFieldUnix);
|
|
6130
|
+
directoryRecord.bytes(rawExtraField);
|
|
6131
|
+
directoryRecord.bytes(rawComment);
|
|
6132
|
+
arraySet(directoryArray, directoryRecord.array, offset);
|
|
6133
|
+
offset += directoryRecordLength;
|
|
5721
6134
|
if (options.onprogress) {
|
|
5722
6135
|
try {
|
|
5723
6136
|
await options.onprogress(indexFileEntry + 1, files.size, new Entry(fileEntry));
|
|
@@ -5727,36 +6140,53 @@
|
|
|
5727
6140
|
}
|
|
5728
6141
|
}
|
|
5729
6142
|
await writeData(writer, directoryDiskOffset ? directoryArray.slice(directoryDiskOffset) : directoryArray);
|
|
6143
|
+
return { cdStartDiskNumber, cdStartDiskOffset };
|
|
6144
|
+
}
|
|
6145
|
+
|
|
6146
|
+
async function writeEndOfDirectoryRecord(zipWriter, comment, options, cdInfo) {
|
|
6147
|
+
const { writer } = zipWriter;
|
|
6148
|
+
const { cdStartDiskNumber, cdStartDiskOffset } = cdInfo;
|
|
6149
|
+
let { directoryDataLength } = cdInfo;
|
|
6150
|
+
let filesLength = zipWriter.files.size;
|
|
6151
|
+
let diskNumber = cdStartDiskNumber;
|
|
6152
|
+
let directoryOffset = zipWriter.offset - cdStartDiskOffset - (cdStartDiskNumber ? zipWriter.initialOffset : 0);
|
|
5730
6153
|
let lastDiskNumber = writer.diskNumber;
|
|
5731
|
-
|
|
5732
|
-
if (availableSize < END_OF_CENTRAL_DIR_LENGTH) {
|
|
6154
|
+
if (writer.availableSize < END_OF_CENTRAL_DIR_LENGTH) {
|
|
5733
6155
|
lastDiskNumber++;
|
|
5734
6156
|
}
|
|
5735
6157
|
let zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
|
|
5736
|
-
if (directoryOffset
|
|
6158
|
+
if (directoryOffset >= MAX_32_BITS || directoryDataLength >= MAX_32_BITS || filesLength >= MAX_16_BITS || lastDiskNumber >= MAX_16_BITS) {
|
|
5737
6159
|
if (zip64 === false) {
|
|
5738
6160
|
throw new Error(ERR_UNSUPPORTED_FORMAT);
|
|
5739
6161
|
} else {
|
|
5740
6162
|
zip64 = true;
|
|
5741
6163
|
}
|
|
5742
6164
|
}
|
|
5743
|
-
const
|
|
5744
|
-
|
|
5745
|
-
|
|
6165
|
+
const commentLength = getLength(comment);
|
|
6166
|
+
if (commentLength > MAX_16_BITS) {
|
|
6167
|
+
throw new Error(ERR_INVALID_COMMENT);
|
|
6168
|
+
}
|
|
6169
|
+
const endOfdirectoryRecord = createRecordWriter(zip64 ? ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH : END_OF_CENTRAL_DIR_LENGTH);
|
|
6170
|
+
if (getLength(endOfdirectoryRecord.array) + commentLength > writer.availableSize) {
|
|
6171
|
+
writer.availableSize = 0;
|
|
6172
|
+
await writeData(writer, new Uint8Array());
|
|
6173
|
+
}
|
|
6174
|
+
lastDiskNumber = writer.diskNumber;
|
|
5746
6175
|
if (zip64) {
|
|
5747
|
-
|
|
5748
|
-
|
|
5749
|
-
|
|
5750
|
-
|
|
5751
|
-
|
|
5752
|
-
|
|
5753
|
-
|
|
5754
|
-
|
|
5755
|
-
|
|
5756
|
-
|
|
5757
|
-
|
|
5758
|
-
|
|
5759
|
-
|
|
6176
|
+
endOfdirectoryRecord.uint32(ZIP64_END_OF_CENTRAL_DIR_SIGNATURE);
|
|
6177
|
+
endOfdirectoryRecord.uint64(44);
|
|
6178
|
+
endOfdirectoryRecord.uint16(45);
|
|
6179
|
+
endOfdirectoryRecord.uint16(45);
|
|
6180
|
+
endOfdirectoryRecord.uint32(lastDiskNumber);
|
|
6181
|
+
endOfdirectoryRecord.uint32(diskNumber);
|
|
6182
|
+
endOfdirectoryRecord.uint64(filesLength);
|
|
6183
|
+
endOfdirectoryRecord.uint64(filesLength);
|
|
6184
|
+
endOfdirectoryRecord.uint64(directoryDataLength);
|
|
6185
|
+
endOfdirectoryRecord.uint64(directoryOffset);
|
|
6186
|
+
endOfdirectoryRecord.uint32(ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE);
|
|
6187
|
+
endOfdirectoryRecord.uint32(lastDiskNumber);
|
|
6188
|
+
endOfdirectoryRecord.uint64(BigInt(zipWriter.offset) + BigInt(directoryDataLength) - BigInt(writer.diskOffset) - BigInt(writer.diskNumber ? zipWriter.initialOffset : 0));
|
|
6189
|
+
endOfdirectoryRecord.uint32(lastDiskNumber + 1);
|
|
5760
6190
|
const supportZip64SplitFile = getOptionValue(zipWriter, options, OPTION_SUPPORT_ZIP64_SPLIT_FILE, true);
|
|
5761
6191
|
if (supportZip64SplitFile) {
|
|
5762
6192
|
lastDiskNumber = MAX_16_BITS;
|
|
@@ -5765,29 +6195,40 @@
|
|
|
5765
6195
|
filesLength = MAX_16_BITS;
|
|
5766
6196
|
directoryOffset = MAX_32_BITS;
|
|
5767
6197
|
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
6198
|
}
|
|
5785
|
-
|
|
6199
|
+
endOfdirectoryRecord.uint32(END_OF_CENTRAL_DIR_SIGNATURE);
|
|
6200
|
+
endOfdirectoryRecord.uint16(lastDiskNumber);
|
|
6201
|
+
endOfdirectoryRecord.uint16(diskNumber);
|
|
6202
|
+
endOfdirectoryRecord.uint16(filesLength);
|
|
6203
|
+
endOfdirectoryRecord.uint16(filesLength);
|
|
6204
|
+
endOfdirectoryRecord.uint32(directoryDataLength);
|
|
6205
|
+
endOfdirectoryRecord.uint32(directoryOffset);
|
|
6206
|
+
endOfdirectoryRecord.uint16(commentLength);
|
|
6207
|
+
await writeData(writer, endOfdirectoryRecord.array);
|
|
5786
6208
|
if (commentLength) {
|
|
5787
6209
|
await writeData(writer, comment);
|
|
5788
6210
|
}
|
|
5789
6211
|
}
|
|
5790
6212
|
|
|
6213
|
+
function createRecordWriter(length) {
|
|
6214
|
+
const array = new Uint8Array(length);
|
|
6215
|
+
const view = getDataView(array);
|
|
6216
|
+
let offset = 0;
|
|
6217
|
+
return {
|
|
6218
|
+
array,
|
|
6219
|
+
uint8: value => { setUint8(view, offset, value); offset += 1; },
|
|
6220
|
+
uint16: value => { setUint16(view, offset, value); offset += 2; },
|
|
6221
|
+
uint32: value => { setUint32(view, offset, value); offset += 4; },
|
|
6222
|
+
uint64: value => { setBigUint64(view, offset, BigInt(value)); offset += 8; },
|
|
6223
|
+
bytes: value => { arraySet(array, value, offset); offset += getLength(value); },
|
|
6224
|
+
skip: count => offset += count
|
|
6225
|
+
};
|
|
6226
|
+
}
|
|
6227
|
+
|
|
6228
|
+
function getSegmentOffset(zipWriter, writer) {
|
|
6229
|
+
return zipWriter.offset - writer.diskOffset - (writer.diskNumber ? zipWriter.initialOffset : 0);
|
|
6230
|
+
}
|
|
6231
|
+
|
|
5791
6232
|
async function writeData(writer, array) {
|
|
5792
6233
|
const { writable } = writer;
|
|
5793
6234
|
const streamWriter = writable.getWriter();
|
|
@@ -5800,12 +6241,40 @@
|
|
|
5800
6241
|
}
|
|
5801
6242
|
}
|
|
5802
6243
|
|
|
6244
|
+
async function flushBufferedData(readable, writer, signal, onChunkWritten) {
|
|
6245
|
+
const streamWriter = writer.writable.getWriter();
|
|
6246
|
+
try {
|
|
6247
|
+
await readable.pipeTo(new WritableStream({
|
|
6248
|
+
async write(chunk) {
|
|
6249
|
+
await streamWriter.ready;
|
|
6250
|
+
await streamWriter.write(chunk);
|
|
6251
|
+
onChunkWritten(getLength(chunk));
|
|
6252
|
+
}
|
|
6253
|
+
}), { preventClose: true, preventAbort: true, signal });
|
|
6254
|
+
} finally {
|
|
6255
|
+
streamWriter.releaseLock();
|
|
6256
|
+
}
|
|
6257
|
+
}
|
|
6258
|
+
|
|
5803
6259
|
function getTimeNTFS(date) {
|
|
5804
6260
|
if (date) {
|
|
5805
6261
|
return ((BigInt(date.getTime()) + BigInt(11644473600000)) * BigInt(10000));
|
|
5806
6262
|
}
|
|
5807
6263
|
}
|
|
5808
6264
|
|
|
6265
|
+
function getTimeUnix(date) {
|
|
6266
|
+
return Math.floor(date.getTime() / 1000);
|
|
6267
|
+
}
|
|
6268
|
+
|
|
6269
|
+
// the extended timestamp extra field stores dates as signed 32-bit time_t values
|
|
6270
|
+
function inUnixTimeRange(timeUnix) {
|
|
6271
|
+
return timeUnix >= MIN_UNIX_TIME && timeUnix <= MAX_UNIX_TIME;
|
|
6272
|
+
}
|
|
6273
|
+
|
|
6274
|
+
function clampUnixTime(timeUnix) {
|
|
6275
|
+
return Math.min(MAX_UNIX_TIME, Math.max(MIN_UNIX_TIME, timeUnix));
|
|
6276
|
+
}
|
|
6277
|
+
|
|
5809
6278
|
function getOptionValue(zipWriter, options, name, defaultValue) {
|
|
5810
6279
|
const result = options[name] === UNDEFINED_VALUE ? zipWriter.options[name] : options[name];
|
|
5811
6280
|
return result === UNDEFINED_VALUE ? defaultValue : result;
|
|
@@ -5836,7 +6305,7 @@
|
|
|
5836
6305
|
}
|
|
5837
6306
|
|
|
5838
6307
|
function getDataView(array) {
|
|
5839
|
-
return new DataView(array.buffer);
|
|
6308
|
+
return new DataView(array.buffer, array.byteOffset, array.byteLength);
|
|
5840
6309
|
}
|
|
5841
6310
|
|
|
5842
6311
|
function getLength(...arrayLikes) {
|
|
@@ -5852,30 +6321,39 @@
|
|
|
5852
6321
|
uncompressedSize,
|
|
5853
6322
|
compressedSize,
|
|
5854
6323
|
lastModDate,
|
|
6324
|
+
rawLastModDate,
|
|
5855
6325
|
rawFilename,
|
|
5856
6326
|
zip64CompressedSize,
|
|
5857
6327
|
zip64UncompressedSize,
|
|
5858
6328
|
extraFieldLength
|
|
5859
6329
|
}) {
|
|
5860
|
-
const
|
|
6330
|
+
const headerRecord = createRecordWriter(HEADER_SIZE - 4);
|
|
6331
|
+
const headerArray = headerRecord.array;
|
|
5861
6332
|
const headerView = getDataView(headerArray);
|
|
5862
|
-
|
|
5863
|
-
|
|
5864
|
-
|
|
5865
|
-
|
|
5866
|
-
|
|
5867
|
-
|
|
5868
|
-
|
|
5869
|
-
|
|
5870
|
-
|
|
6333
|
+
headerRecord.uint16(version);
|
|
6334
|
+
headerRecord.uint16(bitFlag);
|
|
6335
|
+
headerRecord.uint16(compressionMethod);
|
|
6336
|
+
if (rawLastModDate === UNDEFINED_VALUE) {
|
|
6337
|
+
const dateArray = new Uint32Array(1);
|
|
6338
|
+
const dateView = getDataView(dateArray);
|
|
6339
|
+
setUint16(dateView, 0, (((lastModDate.getHours() << 6) | lastModDate.getMinutes()) << 5) | lastModDate.getSeconds() / 2);
|
|
6340
|
+
setUint16(dateView, 2, ((((lastModDate.getFullYear() - 1980) << 4) | (lastModDate.getMonth() + 1)) << 5) | lastModDate.getDate());
|
|
6341
|
+
rawLastModDate = dateArray[0];
|
|
6342
|
+
}
|
|
6343
|
+
headerRecord.uint32(rawLastModDate);
|
|
6344
|
+
headerRecord.skip(4);
|
|
5871
6345
|
if (zip64CompressedSize || compressedSize !== UNDEFINED_VALUE) {
|
|
5872
|
-
|
|
6346
|
+
headerRecord.uint32(zip64CompressedSize ? MAX_32_BITS : compressedSize);
|
|
6347
|
+
} else {
|
|
6348
|
+
headerRecord.skip(4);
|
|
5873
6349
|
}
|
|
5874
6350
|
if (zip64UncompressedSize || uncompressedSize !== UNDEFINED_VALUE) {
|
|
5875
|
-
|
|
6351
|
+
headerRecord.uint32(zip64UncompressedSize ? MAX_32_BITS : uncompressedSize);
|
|
6352
|
+
} else {
|
|
6353
|
+
headerRecord.skip(4);
|
|
5876
6354
|
}
|
|
5877
|
-
|
|
5878
|
-
|
|
6355
|
+
headerRecord.uint16(getLength(rawFilename));
|
|
6356
|
+
headerRecord.uint16(extraFieldLength);
|
|
5879
6357
|
return {
|
|
5880
6358
|
headerArray,
|
|
5881
6359
|
headerView,
|
|
@@ -5980,6 +6458,7 @@
|
|
|
5980
6458
|
exports.BlobWriter = BlobWriter;
|
|
5981
6459
|
exports.Data64URIReader = Data64URIReader;
|
|
5982
6460
|
exports.Data64URIWriter = Data64URIWriter;
|
|
6461
|
+
exports.ERR_AMBIGUOUS_ARCHIVE = ERR_AMBIGUOUS_ARCHIVE;
|
|
5983
6462
|
exports.ERR_BAD_FORMAT = ERR_BAD_FORMAT;
|
|
5984
6463
|
exports.ERR_CENTRAL_DIRECTORY_NOT_FOUND = ERR_CENTRAL_DIRECTORY_NOT_FOUND;
|
|
5985
6464
|
exports.ERR_DUPLICATED_NAME = ERR_DUPLICATED_NAME;
|
|
@@ -5989,6 +6468,7 @@
|
|
|
5989
6468
|
exports.ERR_EXTRAFIELD_ZIP64_NOT_FOUND = ERR_EXTRAFIELD_ZIP64_NOT_FOUND;
|
|
5990
6469
|
exports.ERR_HTTP_RANGE = ERR_HTTP_RANGE;
|
|
5991
6470
|
exports.ERR_INVALID_COMMENT = ERR_INVALID_COMMENT;
|
|
6471
|
+
exports.ERR_INVALID_COMPRESSED_DATA = ERR_INVALID_COMPRESSED_DATA;
|
|
5992
6472
|
exports.ERR_INVALID_ENCRYPTION_STRENGTH = ERR_INVALID_ENCRYPTION_STRENGTH;
|
|
5993
6473
|
exports.ERR_INVALID_ENTRY_COMMENT = ERR_INVALID_ENTRY_COMMENT;
|
|
5994
6474
|
exports.ERR_INVALID_ENTRY_NAME = ERR_INVALID_ENTRY_NAME;
|
|
@@ -6001,8 +6481,9 @@
|
|
|
6001
6481
|
exports.ERR_LOCAL_FILE_HEADER_NOT_FOUND = ERR_LOCAL_FILE_HEADER_NOT_FOUND;
|
|
6002
6482
|
exports.ERR_OVERLAPPING_ENTRY = ERR_OVERLAPPING_ENTRY;
|
|
6003
6483
|
exports.ERR_SPLIT_ZIP_FILE = ERR_SPLIT_ZIP_FILE;
|
|
6484
|
+
exports.ERR_UNDEFINED_READER = ERR_UNDEFINED_READER;
|
|
6004
6485
|
exports.ERR_UNDEFINED_UNCOMPRESSED_SIZE = ERR_UNDEFINED_UNCOMPRESSED_SIZE;
|
|
6005
|
-
exports.ERR_UNSUPPORTED_COMPRESSION = ERR_UNSUPPORTED_COMPRESSION;
|
|
6486
|
+
exports.ERR_UNSUPPORTED_COMPRESSION = ERR_UNSUPPORTED_COMPRESSION$1;
|
|
6006
6487
|
exports.ERR_UNSUPPORTED_ENCRYPTION = ERR_UNSUPPORTED_ENCRYPTION;
|
|
6007
6488
|
exports.ERR_UNSUPPORTED_FORMAT = ERR_UNSUPPORTED_FORMAT;
|
|
6008
6489
|
exports.ERR_ZIP_NOT_EMPTY = ERR_ZIP_NOT_EMPTY;
|