@rahul_dadhich15/archive-kit 0.0.0-stage → 0.1.0
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/CHANGELOG.md +48 -0
- package/LICENSE +21 -0
- package/README.md +385 -2
- package/dist/gzip/index.cjs +439 -0
- package/dist/gzip/index.cjs.map +1 -0
- package/dist/gzip/index.d.cts +174 -0
- package/dist/gzip/index.d.ts +174 -0
- package/dist/gzip/index.js +424 -0
- package/dist/gzip/index.js.map +1 -0
- package/dist/index.cjs +1320 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +120 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.js +1260 -0
- package/dist/index.js.map +1 -0
- package/dist/types-CY4dTVyZ.d.cts +34 -0
- package/dist/types-CY4dTVyZ.d.ts +34 -0
- package/dist/zip-create/index.cjs +357 -0
- package/dist/zip-create/index.cjs.map +1 -0
- package/dist/zip-create/index.d.cts +120 -0
- package/dist/zip-create/index.d.ts +120 -0
- package/dist/zip-create/index.js +350 -0
- package/dist/zip-create/index.js.map +1 -0
- package/dist/zip-extract/index.cjs +701 -0
- package/dist/zip-extract/index.cjs.map +1 -0
- package/dist/zip-extract/index.d.cts +298 -0
- package/dist/zip-extract/index.d.ts +298 -0
- package/dist/zip-extract/index.js +685 -0
- package/dist/zip-extract/index.js.map +1 -0
- package/package.json +91 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,1260 @@
|
|
|
1
|
+
import { gzip, gzipSync, gunzip, gunzipSync, zip, zipSync, inflate } from 'fflate';
|
|
2
|
+
|
|
3
|
+
// src/shared/types.ts
|
|
4
|
+
var ZIP_COMPRESSION_METHOD = {
|
|
5
|
+
STORE: 0,
|
|
6
|
+
SHRUNK: 1,
|
|
7
|
+
REDUCED_1: 2,
|
|
8
|
+
REDUCED_2: 3,
|
|
9
|
+
REDUCED_3: 4,
|
|
10
|
+
REDUCED_4: 5,
|
|
11
|
+
IMPLODED: 6,
|
|
12
|
+
DEFLATE: 8,
|
|
13
|
+
DEFLATE64: 9,
|
|
14
|
+
BZIP2: 12,
|
|
15
|
+
LZMA: 14,
|
|
16
|
+
ZSTD: 93,
|
|
17
|
+
MP3: 94,
|
|
18
|
+
XZ: 95,
|
|
19
|
+
JPEG: 96,
|
|
20
|
+
WAVPACK: 97,
|
|
21
|
+
PPMD: 98,
|
|
22
|
+
AES_ENCRYPTED: 99
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// src/shared/errors.ts
|
|
26
|
+
var ArchiveKitError = class _ArchiveKitError extends Error {
|
|
27
|
+
code;
|
|
28
|
+
details;
|
|
29
|
+
constructor(code, message, details) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.name = "ArchiveKitError";
|
|
32
|
+
this.code = code;
|
|
33
|
+
this.details = details;
|
|
34
|
+
Object.setPrototypeOf(this, _ArchiveKitError.prototype);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// src/shared/binary.ts
|
|
39
|
+
async function toUint8Array(input) {
|
|
40
|
+
if (input instanceof Uint8Array) {
|
|
41
|
+
return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
42
|
+
}
|
|
43
|
+
if (input instanceof ArrayBuffer) {
|
|
44
|
+
return new Uint8Array(input);
|
|
45
|
+
}
|
|
46
|
+
if (typeof Blob !== "undefined" && input instanceof Blob) {
|
|
47
|
+
const arrayBuffer = await input.arrayBuffer();
|
|
48
|
+
return new Uint8Array(arrayBuffer);
|
|
49
|
+
}
|
|
50
|
+
throw new ArchiveKitError(
|
|
51
|
+
"INVALID_INPUT",
|
|
52
|
+
"Expected Uint8Array, ArrayBuffer, Blob, or File input"
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
function toUint8ArraySync(input) {
|
|
56
|
+
if (input instanceof Uint8Array) {
|
|
57
|
+
return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
58
|
+
}
|
|
59
|
+
if (input instanceof ArrayBuffer) {
|
|
60
|
+
return new Uint8Array(input);
|
|
61
|
+
}
|
|
62
|
+
throw new ArchiveKitError(
|
|
63
|
+
"INVALID_INPUT",
|
|
64
|
+
"Synchronous API requires Uint8Array or ArrayBuffer"
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
function toArrayBuffer(input) {
|
|
68
|
+
if (input instanceof ArrayBuffer) {
|
|
69
|
+
return input;
|
|
70
|
+
}
|
|
71
|
+
if (input instanceof Uint8Array) {
|
|
72
|
+
if (input.byteOffset === 0 && input.byteLength === input.buffer.byteLength) {
|
|
73
|
+
return input.buffer;
|
|
74
|
+
}
|
|
75
|
+
return input.buffer.slice(
|
|
76
|
+
input.byteOffset,
|
|
77
|
+
input.byteOffset + input.byteLength
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
throw new ArchiveKitError(
|
|
81
|
+
"INVALID_INPUT",
|
|
82
|
+
"Expected Uint8Array or ArrayBuffer"
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
var textEncoder = new TextEncoder();
|
|
86
|
+
var textDecoder = new TextDecoder("utf-8", { fatal: true });
|
|
87
|
+
function encodeUtf8(text) {
|
|
88
|
+
if (typeof text !== "string") {
|
|
89
|
+
throw new ArchiveKitError("INVALID_INPUT", "Expected string input");
|
|
90
|
+
}
|
|
91
|
+
return textEncoder.encode(text);
|
|
92
|
+
}
|
|
93
|
+
function decodeUtf8(bytes) {
|
|
94
|
+
try {
|
|
95
|
+
return textDecoder.decode(bytes);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
throw new ArchiveKitError(
|
|
98
|
+
"INVALID_INPUT",
|
|
99
|
+
"Data is not valid UTF-8 text",
|
|
100
|
+
err
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function concatUint8Arrays(arrays) {
|
|
105
|
+
let totalLength = 0;
|
|
106
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
107
|
+
const arr = arrays[i];
|
|
108
|
+
if (arr) {
|
|
109
|
+
totalLength += arr.length;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const result = new Uint8Array(totalLength);
|
|
113
|
+
let offset = 0;
|
|
114
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
115
|
+
const arr = arrays[i];
|
|
116
|
+
if (arr) {
|
|
117
|
+
result.set(arr, offset);
|
|
118
|
+
offset += arr.length;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
function compareUint8Arrays(a, b) {
|
|
124
|
+
if (a.length !== b.length) return false;
|
|
125
|
+
for (let i = 0; i < a.length; i++) {
|
|
126
|
+
if (a[i] !== b[i]) return false;
|
|
127
|
+
}
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
var CRC_TABLE = new Uint32Array(256);
|
|
131
|
+
for (let i = 0; i < 256; i++) {
|
|
132
|
+
let c = i;
|
|
133
|
+
for (let k = 0; k < 8; k++) {
|
|
134
|
+
c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
135
|
+
}
|
|
136
|
+
CRC_TABLE[i] = c >>> 0;
|
|
137
|
+
}
|
|
138
|
+
function computeCrc32(data) {
|
|
139
|
+
let crc = 4294967295;
|
|
140
|
+
for (let i = 0; i < data.length; i++) {
|
|
141
|
+
const byte = data[i] ?? 0;
|
|
142
|
+
crc = (CRC_TABLE[(crc ^ byte) & 255] ?? 0) ^ crc >>> 8;
|
|
143
|
+
}
|
|
144
|
+
return (crc ^ 4294967295) >>> 0;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// src/shared/blob.ts
|
|
148
|
+
function createBlob(data, mimeType = "application/octet-stream") {
|
|
149
|
+
if (typeof Blob === "undefined") {
|
|
150
|
+
throw new ArchiveKitError(
|
|
151
|
+
"INVALID_INPUT",
|
|
152
|
+
"Blob API is not available in the current environment"
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
const parts = Array.isArray(data) ? data : [data];
|
|
156
|
+
return new Blob(parts, { type: mimeType });
|
|
157
|
+
}
|
|
158
|
+
async function readBlobAsUint8Array(blob) {
|
|
159
|
+
const buffer = await blob.arrayBuffer();
|
|
160
|
+
return new Uint8Array(buffer);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// src/shared/path.ts
|
|
164
|
+
function normalizeZipPath(rawPath) {
|
|
165
|
+
if (!rawPath || typeof rawPath !== "string") {
|
|
166
|
+
throw new ArchiveKitError(
|
|
167
|
+
"INVALID_ARCHIVE_PATH",
|
|
168
|
+
"Archive path must be a non-empty string"
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
if (rawPath.includes("\0")) {
|
|
172
|
+
throw new ArchiveKitError(
|
|
173
|
+
"INVALID_ARCHIVE_PATH",
|
|
174
|
+
`Archive path contains NUL byte: "${rawPath}"`
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
const path = rawPath.replace(/\\/g, "/");
|
|
178
|
+
if (/^[a-zA-Z]:/i.test(path)) {
|
|
179
|
+
throw new ArchiveKitError(
|
|
180
|
+
"INVALID_ARCHIVE_PATH",
|
|
181
|
+
`Absolute Windows drive paths are prohibited: "${rawPath}"`
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
if (path.startsWith("//")) {
|
|
185
|
+
throw new ArchiveKitError(
|
|
186
|
+
"INVALID_ARCHIVE_PATH",
|
|
187
|
+
`UNC network paths are prohibited: "${rawPath}"`
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
if (path.startsWith("/")) {
|
|
191
|
+
throw new ArchiveKitError(
|
|
192
|
+
"INVALID_ARCHIVE_PATH",
|
|
193
|
+
`Absolute root paths are prohibited: "${rawPath}"`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
const isDirectory = path.endsWith("/");
|
|
197
|
+
const segments = path.split("/");
|
|
198
|
+
const cleaned = [];
|
|
199
|
+
for (const seg of segments) {
|
|
200
|
+
if (seg === "" || seg === ".") {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (seg === "..") {
|
|
204
|
+
throw new ArchiveKitError(
|
|
205
|
+
"INVALID_ARCHIVE_PATH",
|
|
206
|
+
`Path traversal ("..") is strictly prohibited (Zip Slip): "${rawPath}"`
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
cleaned.push(seg);
|
|
210
|
+
}
|
|
211
|
+
if (cleaned.length === 0) {
|
|
212
|
+
throw new ArchiveKitError(
|
|
213
|
+
"INVALID_ARCHIVE_PATH",
|
|
214
|
+
`Path resolved to an empty path: "${rawPath}"`
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
const result = cleaned.join("/");
|
|
218
|
+
return isDirectory ? `${result}/` : result;
|
|
219
|
+
}
|
|
220
|
+
function isDirPath(path) {
|
|
221
|
+
return path.endsWith("/");
|
|
222
|
+
}
|
|
223
|
+
function getBaseName(path) {
|
|
224
|
+
const normalized = path.replace(/\\/g, "/").replace(/\/+$/, "");
|
|
225
|
+
const lastSlash = normalized.lastIndexOf("/");
|
|
226
|
+
return lastSlash === -1 ? normalized : normalized.slice(lastSlash + 1);
|
|
227
|
+
}
|
|
228
|
+
function getDirName(path) {
|
|
229
|
+
const normalized = path.replace(/\\/g, "/").replace(/\/+$/, "");
|
|
230
|
+
const lastSlash = normalized.lastIndexOf("/");
|
|
231
|
+
return lastSlash === -1 ? "" : normalized.slice(0, lastSlash);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// src/shared/validation.ts
|
|
235
|
+
var DEFAULT_SECURITY_LIMITS = {
|
|
236
|
+
maxEntries: 1e4,
|
|
237
|
+
maxEntrySize: 250 * 1024 * 1024,
|
|
238
|
+
// 250 MB
|
|
239
|
+
maxTotalSize: 1024 * 1024 * 1024,
|
|
240
|
+
// 1 GB
|
|
241
|
+
maxCompressionRatio: 100,
|
|
242
|
+
// 100:1 ratio protection against ZIP bombs
|
|
243
|
+
maxInputSize: 1024 * 1024 * 1024
|
|
244
|
+
// 1 GB
|
|
245
|
+
};
|
|
246
|
+
function validateCompressionLevel(level) {
|
|
247
|
+
if (level === void 0) return 6;
|
|
248
|
+
if (!Number.isInteger(level) || level < 0 || level > 9) {
|
|
249
|
+
throw new ArchiveKitError(
|
|
250
|
+
"INVALID_INPUT",
|
|
251
|
+
`Compression level must be an integer between 0 and 9, received: ${level}`
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
return level;
|
|
255
|
+
}
|
|
256
|
+
function validateEntryCount(count, limit = DEFAULT_SECURITY_LIMITS.maxEntries) {
|
|
257
|
+
if (count > limit) {
|
|
258
|
+
throw new ArchiveKitError(
|
|
259
|
+
"ENTRY_LIMIT_EXCEEDED",
|
|
260
|
+
`Archive entry count (${count}) exceeds the maximum limit of ${limit}`
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function validateDecompressedSize(size, limit = DEFAULT_SECURITY_LIMITS.maxEntrySize, path) {
|
|
265
|
+
if (size > limit) {
|
|
266
|
+
throw new ArchiveKitError(
|
|
267
|
+
"FILE_SIZE_LIMIT_EXCEEDED",
|
|
268
|
+
`Entry ${path ? `"${path}" ` : ""}decompressed size (${size} bytes) exceeds limit of ${limit} bytes`
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
function validateTotalDecompressedSize(size, limit = DEFAULT_SECURITY_LIMITS.maxTotalSize) {
|
|
273
|
+
if (size > limit) {
|
|
274
|
+
throw new ArchiveKitError(
|
|
275
|
+
"TOTAL_SIZE_LIMIT_EXCEEDED",
|
|
276
|
+
`Total uncompressed size (${size} bytes) exceeds maximum limit of ${limit} bytes`
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
function validateCompressionRatio(uncompressed, compressed, maxRatio = DEFAULT_SECURITY_LIMITS.maxCompressionRatio, path) {
|
|
281
|
+
if (uncompressed > 1024 * 1024) {
|
|
282
|
+
const ratio = uncompressed / Math.max(1, compressed);
|
|
283
|
+
if (ratio > maxRatio) {
|
|
284
|
+
throw new ArchiveKitError(
|
|
285
|
+
"COMPRESSION_RATIO_LIMIT_EXCEEDED",
|
|
286
|
+
`Compression ratio (${ratio.toFixed(1)}:1) for ${path ? `"${path}" ` : ""}exceeds safe threshold of ${maxRatio}:1`
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function calculateRatio(compressed, original) {
|
|
292
|
+
if (original <= 0) return 1;
|
|
293
|
+
return Number((compressed / original).toFixed(4));
|
|
294
|
+
}
|
|
295
|
+
function calculatePercentageSaved(compressed, original) {
|
|
296
|
+
if (original <= 0) return 0;
|
|
297
|
+
const saved = (1 - compressed / original) * 100;
|
|
298
|
+
return Math.max(0, Number(saved.toFixed(2)));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// src/gzip/metadata.ts
|
|
302
|
+
var GZIP_MAGIC_0 = 31;
|
|
303
|
+
var GZIP_MAGIC_1 = 139;
|
|
304
|
+
var GZIP_METHOD_DEFLATE = 8;
|
|
305
|
+
var FLG_FHCRC = 2;
|
|
306
|
+
var FLG_FEXTRA = 4;
|
|
307
|
+
var FLG_FNAME = 8;
|
|
308
|
+
var FLG_FCOMMENT = 16;
|
|
309
|
+
function isGzipDataSync(data) {
|
|
310
|
+
try {
|
|
311
|
+
const bytes = toUint8ArraySync(data);
|
|
312
|
+
return bytes.length >= 10 && bytes[0] === GZIP_MAGIC_0 && bytes[1] === GZIP_MAGIC_1 && bytes[2] === GZIP_METHOD_DEFLATE;
|
|
313
|
+
} catch {
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
async function isGzipData(data) {
|
|
318
|
+
try {
|
|
319
|
+
const bytes = await toUint8Array(data);
|
|
320
|
+
return isGzipDataSync(bytes);
|
|
321
|
+
} catch {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
function getGzipMetadataSync(data) {
|
|
326
|
+
const bytes = toUint8ArraySync(data);
|
|
327
|
+
if (bytes.length < 10) {
|
|
328
|
+
throw new ArchiveKitError(
|
|
329
|
+
"INVALID_GZIP_DATA",
|
|
330
|
+
"Input data is too small to be a valid GZIP stream (minimum 10 bytes required)"
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
if (bytes[0] !== GZIP_MAGIC_0 || bytes[1] !== GZIP_MAGIC_1) {
|
|
334
|
+
throw new ArchiveKitError(
|
|
335
|
+
"INVALID_GZIP_DATA",
|
|
336
|
+
`Invalid GZIP magic bytes: expected 0x1F8B, found 0x${(bytes[0] ?? 0).toString(16).padStart(2, "0")}${(bytes[1] ?? 0).toString(16).padStart(2, "0")}`
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
const method = bytes[2];
|
|
340
|
+
if (method !== GZIP_METHOD_DEFLATE) {
|
|
341
|
+
throw new ArchiveKitError(
|
|
342
|
+
"UNSUPPORTED_COMPRESSION",
|
|
343
|
+
`Unsupported GZIP compression method: ${method} (only DEFLATE 8 is standard)`
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
const flg = bytes[3] ?? 0;
|
|
347
|
+
const mtimeSeconds = (bytes[4] ?? 0) | (bytes[5] ?? 0) << 8 | (bytes[6] ?? 0) << 16 | (bytes[7] ?? 0) * 16777216;
|
|
348
|
+
const mtime = mtimeSeconds > 0 ? new Date(mtimeSeconds * 1e3) : void 0;
|
|
349
|
+
const os = bytes[9];
|
|
350
|
+
let offset = 10;
|
|
351
|
+
if ((flg & FLG_FEXTRA) !== 0) {
|
|
352
|
+
if (offset + 2 > bytes.length) {
|
|
353
|
+
throw new ArchiveKitError("INVALID_GZIP_DATA", "Truncated GZIP extra field");
|
|
354
|
+
}
|
|
355
|
+
const xlen = (bytes[offset] ?? 0) | (bytes[offset + 1] ?? 0) << 8;
|
|
356
|
+
offset += 2 + xlen;
|
|
357
|
+
if (offset > bytes.length) {
|
|
358
|
+
throw new ArchiveKitError("INVALID_GZIP_DATA", "Truncated GZIP extra field payload");
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
let filename;
|
|
362
|
+
if ((flg & FLG_FNAME) !== 0) {
|
|
363
|
+
const fnStart = offset;
|
|
364
|
+
while (offset < bytes.length && bytes[offset] !== 0) {
|
|
365
|
+
offset++;
|
|
366
|
+
}
|
|
367
|
+
if (offset >= bytes.length) {
|
|
368
|
+
throw new ArchiveKitError("INVALID_GZIP_DATA", "Truncated GZIP filename");
|
|
369
|
+
}
|
|
370
|
+
const fnBytes = bytes.subarray(fnStart, offset);
|
|
371
|
+
try {
|
|
372
|
+
filename = new TextDecoder("utf-8", { fatal: false }).decode(fnBytes);
|
|
373
|
+
} catch {
|
|
374
|
+
filename = String.fromCharCode(...fnBytes);
|
|
375
|
+
}
|
|
376
|
+
offset++;
|
|
377
|
+
}
|
|
378
|
+
let comment;
|
|
379
|
+
if ((flg & FLG_FCOMMENT) !== 0) {
|
|
380
|
+
const cmStart = offset;
|
|
381
|
+
while (offset < bytes.length && bytes[offset] !== 0) {
|
|
382
|
+
offset++;
|
|
383
|
+
}
|
|
384
|
+
if (offset >= bytes.length) {
|
|
385
|
+
throw new ArchiveKitError("INVALID_GZIP_DATA", "Truncated GZIP comment");
|
|
386
|
+
}
|
|
387
|
+
const cmBytes = bytes.subarray(cmStart, offset);
|
|
388
|
+
try {
|
|
389
|
+
comment = new TextDecoder("utf-8", { fatal: false }).decode(cmBytes);
|
|
390
|
+
} catch {
|
|
391
|
+
comment = String.fromCharCode(...cmBytes);
|
|
392
|
+
}
|
|
393
|
+
offset++;
|
|
394
|
+
}
|
|
395
|
+
if ((flg & FLG_FHCRC) !== 0) {
|
|
396
|
+
offset += 2;
|
|
397
|
+
if (offset > bytes.length) {
|
|
398
|
+
throw new ArchiveKitError("INVALID_GZIP_DATA", "Truncated GZIP header CRC");
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
let originalSize;
|
|
402
|
+
if (bytes.length >= 18) {
|
|
403
|
+
const trailerOffset = bytes.length - 4;
|
|
404
|
+
originalSize = ((bytes[trailerOffset] ?? 0) | (bytes[trailerOffset + 1] ?? 0) << 8 | (bytes[trailerOffset + 2] ?? 0) << 16 | (bytes[trailerOffset + 3] ?? 0) * 16777216) >>> 0;
|
|
405
|
+
}
|
|
406
|
+
const compressedSize = bytes.length;
|
|
407
|
+
const ratio = originalSize !== void 0 ? calculateRatio(compressedSize, originalSize) : 1;
|
|
408
|
+
const saved = originalSize !== void 0 ? calculatePercentageSaved(compressedSize, originalSize) : 0;
|
|
409
|
+
let outputFilename = filename;
|
|
410
|
+
if (!outputFilename) {
|
|
411
|
+
outputFilename = "extracted";
|
|
412
|
+
}
|
|
413
|
+
return {
|
|
414
|
+
originalSize,
|
|
415
|
+
compressedSize,
|
|
416
|
+
compressionRatio: ratio,
|
|
417
|
+
compressionPercentageSaved: saved,
|
|
418
|
+
filename,
|
|
419
|
+
comment,
|
|
420
|
+
mtime,
|
|
421
|
+
os,
|
|
422
|
+
mimeType: "application/gzip",
|
|
423
|
+
outputFilename
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
async function getGzipMetadata(data) {
|
|
427
|
+
const bytes = await toUint8Array(data);
|
|
428
|
+
return getGzipMetadataSync(bytes);
|
|
429
|
+
}
|
|
430
|
+
async function gzipCompress(data, options) {
|
|
431
|
+
if (options?.signal?.aborted) {
|
|
432
|
+
throw new ArchiveKitError("OPERATION_CANCELLED", "Compression was aborted");
|
|
433
|
+
}
|
|
434
|
+
const bytes = await toUint8Array(data);
|
|
435
|
+
const level = validateCompressionLevel(options?.level);
|
|
436
|
+
return new Promise((resolve, reject) => {
|
|
437
|
+
let completed = false;
|
|
438
|
+
if (options?.signal) {
|
|
439
|
+
options.signal.addEventListener("abort", () => {
|
|
440
|
+
if (!completed) {
|
|
441
|
+
completed = true;
|
|
442
|
+
reject(new ArchiveKitError("OPERATION_CANCELLED", "Compression was aborted"));
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
gzip(
|
|
447
|
+
bytes,
|
|
448
|
+
{
|
|
449
|
+
level,
|
|
450
|
+
filename: options?.filename,
|
|
451
|
+
mtime: options?.mtime
|
|
452
|
+
},
|
|
453
|
+
(err, result) => {
|
|
454
|
+
if (completed) return;
|
|
455
|
+
completed = true;
|
|
456
|
+
if (err) {
|
|
457
|
+
reject(
|
|
458
|
+
new ArchiveKitError("INVALID_INPUT", `GZIP compression failed: ${err.message}`, err)
|
|
459
|
+
);
|
|
460
|
+
} else {
|
|
461
|
+
resolve(result);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
);
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
function gzipCompressSync(data, options) {
|
|
468
|
+
const bytes = typeof data === "string" ? encodeUtf8(data) : toUint8ArraySync(data);
|
|
469
|
+
const level = validateCompressionLevel(options?.level);
|
|
470
|
+
try {
|
|
471
|
+
return gzipSync(bytes, {
|
|
472
|
+
level,
|
|
473
|
+
filename: options?.filename,
|
|
474
|
+
mtime: options?.mtime
|
|
475
|
+
});
|
|
476
|
+
} catch (err) {
|
|
477
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
478
|
+
throw new ArchiveKitError("INVALID_INPUT", `GZIP compression failed: ${message}`, err);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
async function gzipCompressText(text, options) {
|
|
482
|
+
const bytes = encodeUtf8(text);
|
|
483
|
+
return gzipCompress(bytes, options);
|
|
484
|
+
}
|
|
485
|
+
function gzipCompressTextSync(text, options) {
|
|
486
|
+
const bytes = encodeUtf8(text);
|
|
487
|
+
return gzipCompressSync(bytes, options);
|
|
488
|
+
}
|
|
489
|
+
async function gzipCompressBlob(data, options) {
|
|
490
|
+
const compressed = await gzipCompress(data, options);
|
|
491
|
+
return createBlob(compressed, options?.mimeType ?? "application/gzip");
|
|
492
|
+
}
|
|
493
|
+
var GZIP_MAGIC_02 = 31;
|
|
494
|
+
var GZIP_MAGIC_12 = 139;
|
|
495
|
+
var GZIP_METHOD_DEFLATE2 = 8;
|
|
496
|
+
function validateGzipHeader(bytes) {
|
|
497
|
+
if (bytes.length === 0) {
|
|
498
|
+
throw new ArchiveKitError(
|
|
499
|
+
"INVALID_GZIP_DATA",
|
|
500
|
+
"Input data is empty or too small to be valid GZIP"
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
if (bytes.length < 2 || bytes[0] !== GZIP_MAGIC_02 || bytes[1] !== GZIP_MAGIC_12) {
|
|
504
|
+
throw new ArchiveKitError(
|
|
505
|
+
"INVALID_GZIP_DATA",
|
|
506
|
+
`Invalid GZIP magic bytes: expected 0x1F8B, found 0x${(bytes[0] ?? 0).toString(16).padStart(2, "0")}${(bytes[1] ?? 0).toString(16).padStart(2, "0")}`
|
|
507
|
+
);
|
|
508
|
+
}
|
|
509
|
+
if (bytes.length < 18) {
|
|
510
|
+
throw new ArchiveKitError(
|
|
511
|
+
"INVALID_GZIP_DATA",
|
|
512
|
+
"Input data is too small to be valid GZIP (minimum 18 bytes required for header and trailer)"
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
if (bytes[2] !== GZIP_METHOD_DEFLATE2) {
|
|
516
|
+
throw new ArchiveKitError(
|
|
517
|
+
"UNSUPPORTED_COMPRESSION",
|
|
518
|
+
`Unsupported GZIP compression method: ${bytes[2]}`
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
function verifyGzipTrailer(compressedBytes, decompressed) {
|
|
523
|
+
const trailerOffset = compressedBytes.length - 8;
|
|
524
|
+
const expectedCrc = ((compressedBytes[trailerOffset] ?? 0) | (compressedBytes[trailerOffset + 1] ?? 0) << 8 | (compressedBytes[trailerOffset + 2] ?? 0) << 16 | (compressedBytes[trailerOffset + 3] ?? 0) * 16777216) >>> 0;
|
|
525
|
+
const expectedIsize = ((compressedBytes[trailerOffset + 4] ?? 0) | (compressedBytes[trailerOffset + 5] ?? 0) << 8 | (compressedBytes[trailerOffset + 6] ?? 0) << 16 | (compressedBytes[trailerOffset + 7] ?? 0) * 16777216) >>> 0;
|
|
526
|
+
const actualCrc = computeCrc32(decompressed);
|
|
527
|
+
if (actualCrc !== expectedCrc) {
|
|
528
|
+
throw new ArchiveKitError(
|
|
529
|
+
"DECOMPRESSION_FAILED",
|
|
530
|
+
`GZIP CRC-32 checksum mismatch: expected 0x${expectedCrc.toString(16)}, got 0x${actualCrc.toString(16)} (archive corrupted)`
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
const actualIsize = decompressed.length >>> 0;
|
|
534
|
+
if (actualIsize !== expectedIsize) {
|
|
535
|
+
throw new ArchiveKitError(
|
|
536
|
+
"DECOMPRESSION_FAILED",
|
|
537
|
+
`GZIP ISIZE mismatch: expected ${expectedIsize} bytes, got ${actualIsize} bytes (archive truncated or corrupted)`
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
async function gzipDecompress(data, options) {
|
|
542
|
+
if (options?.signal?.aborted) {
|
|
543
|
+
throw new ArchiveKitError("OPERATION_CANCELLED", "Decompression was aborted");
|
|
544
|
+
}
|
|
545
|
+
const bytes = await toUint8Array(data);
|
|
546
|
+
validateGzipHeader(bytes);
|
|
547
|
+
return new Promise((resolve, reject) => {
|
|
548
|
+
let completed = false;
|
|
549
|
+
if (options?.signal) {
|
|
550
|
+
options.signal.addEventListener("abort", () => {
|
|
551
|
+
if (!completed) {
|
|
552
|
+
completed = true;
|
|
553
|
+
reject(new ArchiveKitError("OPERATION_CANCELLED", "Decompression was aborted"));
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
gunzip(bytes, (err, result) => {
|
|
558
|
+
if (completed) return;
|
|
559
|
+
completed = true;
|
|
560
|
+
if (err) {
|
|
561
|
+
reject(
|
|
562
|
+
new ArchiveKitError(
|
|
563
|
+
"DECOMPRESSION_FAILED",
|
|
564
|
+
`GZIP decompression failed (corrupted or truncated archive): ${err.message}`,
|
|
565
|
+
err
|
|
566
|
+
)
|
|
567
|
+
);
|
|
568
|
+
} else {
|
|
569
|
+
try {
|
|
570
|
+
verifyGzipTrailer(bytes, result);
|
|
571
|
+
resolve(result);
|
|
572
|
+
} catch (verifyErr) {
|
|
573
|
+
reject(verifyErr);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
});
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
function gzipDecompressSync(data, _options) {
|
|
580
|
+
const bytes = toUint8ArraySync(data);
|
|
581
|
+
validateGzipHeader(bytes);
|
|
582
|
+
try {
|
|
583
|
+
const result = gunzipSync(bytes);
|
|
584
|
+
verifyGzipTrailer(bytes, result);
|
|
585
|
+
return result;
|
|
586
|
+
} catch (err) {
|
|
587
|
+
if (err instanceof ArchiveKitError) {
|
|
588
|
+
throw err;
|
|
589
|
+
}
|
|
590
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
591
|
+
throw new ArchiveKitError(
|
|
592
|
+
"DECOMPRESSION_FAILED",
|
|
593
|
+
`GZIP decompression failed (corrupted or truncated archive): ${message}`,
|
|
594
|
+
err
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
async function gzipDecompressText(data, options) {
|
|
599
|
+
const decompressed = await gzipDecompress(data, options);
|
|
600
|
+
return decodeUtf8(decompressed);
|
|
601
|
+
}
|
|
602
|
+
function gzipDecompressTextSync(data, options) {
|
|
603
|
+
const decompressed = gzipDecompressSync(data);
|
|
604
|
+
return decodeUtf8(decompressed);
|
|
605
|
+
}
|
|
606
|
+
async function gzipDecompressBlob(data, options) {
|
|
607
|
+
const decompressed = await gzipDecompress(data, options);
|
|
608
|
+
return createBlob(decompressed, options?.mimeType ?? "application/octet-stream");
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// src/zip-create/validate-entries.ts
|
|
612
|
+
function getZipCreateOptions(options) {
|
|
613
|
+
const compression = options?.compression ?? "deflate";
|
|
614
|
+
if (compression !== "deflate" && compression !== "store") {
|
|
615
|
+
throw new ArchiveKitError(
|
|
616
|
+
"INVALID_INPUT",
|
|
617
|
+
`Invalid compression mode "${compression}". Must be "deflate" or "store".`
|
|
618
|
+
);
|
|
619
|
+
}
|
|
620
|
+
const defaultLevel = compression === "store" ? 0 : 6;
|
|
621
|
+
const level = options?.compressionLevel !== void 0 ? validateCompressionLevel(options.compressionLevel) : defaultLevel;
|
|
622
|
+
return {
|
|
623
|
+
compression,
|
|
624
|
+
compressionLevel: level,
|
|
625
|
+
maxEntries: options?.maxEntries ?? DEFAULT_SECURITY_LIMITS.maxEntries,
|
|
626
|
+
maxTotalInputSize: options?.maxTotalInputSize ?? DEFAULT_SECURITY_LIMITS.maxTotalSize,
|
|
627
|
+
signal: options?.signal,
|
|
628
|
+
comment: options?.comment
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
function validateZipEntries(entries, options) {
|
|
632
|
+
if (!Array.isArray(entries)) {
|
|
633
|
+
throw new ArchiveKitError("INVALID_INPUT", "Entries must be an array");
|
|
634
|
+
}
|
|
635
|
+
const maxEntries = options?.maxEntries ?? DEFAULT_SECURITY_LIMITS.maxEntries;
|
|
636
|
+
validateEntryCount(entries.length, maxEntries);
|
|
637
|
+
const seenPaths = /* @__PURE__ */ new Set();
|
|
638
|
+
for (let i = 0; i < entries.length; i++) {
|
|
639
|
+
const entry = entries[i];
|
|
640
|
+
if (!entry || typeof entry !== "object") {
|
|
641
|
+
throw new ArchiveKitError("INVALID_INPUT", `Entry at index ${i} is not a valid object`);
|
|
642
|
+
}
|
|
643
|
+
if (!entry.path || typeof entry.path !== "string") {
|
|
644
|
+
throw new ArchiveKitError(
|
|
645
|
+
"INVALID_ARCHIVE_PATH",
|
|
646
|
+
`Entry at index ${i} must have a non-empty string path`
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
const normalized = normalizeZipPath(entry.path);
|
|
650
|
+
if (seenPaths.has(normalized)) {
|
|
651
|
+
throw new ArchiveKitError(
|
|
652
|
+
"DUPLICATE_ENTRY",
|
|
653
|
+
`Duplicate archive path detected: "${normalized}"`
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
seenPaths.add(normalized);
|
|
657
|
+
if (entry.compressionLevel !== void 0) {
|
|
658
|
+
validateCompressionLevel(entry.compressionLevel);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
async function resolveEntryBytes(entry, signal) {
|
|
663
|
+
if (signal?.aborted) {
|
|
664
|
+
throw new ArchiveKitError("OPERATION_CANCELLED", "ZIP creation was aborted");
|
|
665
|
+
}
|
|
666
|
+
const isDirectory = entry.type === "directory" || entry.path.endsWith("/");
|
|
667
|
+
if (isDirectory || entry.data === void 0 || entry.data === null) {
|
|
668
|
+
return new Uint8Array(0);
|
|
669
|
+
}
|
|
670
|
+
if (typeof entry.data === "string") {
|
|
671
|
+
return encodeUtf8(entry.data);
|
|
672
|
+
}
|
|
673
|
+
return toUint8Array(entry.data);
|
|
674
|
+
}
|
|
675
|
+
function resolveEntryBytesSync(entry) {
|
|
676
|
+
const isDirectory = entry.type === "directory" || entry.path.endsWith("/");
|
|
677
|
+
if (isDirectory || entry.data === void 0 || entry.data === null) {
|
|
678
|
+
return new Uint8Array(0);
|
|
679
|
+
}
|
|
680
|
+
if (typeof entry.data === "string") {
|
|
681
|
+
return encodeUtf8(entry.data);
|
|
682
|
+
}
|
|
683
|
+
return toUint8ArraySync(entry.data);
|
|
684
|
+
}
|
|
685
|
+
function appendZipComment(zipBytes, comment) {
|
|
686
|
+
if (!comment) return zipBytes;
|
|
687
|
+
const commentBytes = encodeUtf8(comment);
|
|
688
|
+
if (commentBytes.length === 0 || commentBytes.length > 65535) return zipBytes;
|
|
689
|
+
const out = new Uint8Array(zipBytes.length + commentBytes.length);
|
|
690
|
+
out.set(zipBytes, 0);
|
|
691
|
+
out[zipBytes.length - 2] = commentBytes.length & 255;
|
|
692
|
+
out[zipBytes.length - 1] = commentBytes.length >> 8 & 255;
|
|
693
|
+
out.set(commentBytes, zipBytes.length);
|
|
694
|
+
return out;
|
|
695
|
+
}
|
|
696
|
+
async function createZip(entries, options) {
|
|
697
|
+
const opts = getZipCreateOptions(options);
|
|
698
|
+
if (opts.signal?.aborted) {
|
|
699
|
+
throw new ArchiveKitError("OPERATION_CANCELLED", "ZIP creation was aborted");
|
|
700
|
+
}
|
|
701
|
+
validateZipEntries(entries, opts);
|
|
702
|
+
const zippable = {};
|
|
703
|
+
let totalInputSize = 0;
|
|
704
|
+
for (const entry of entries) {
|
|
705
|
+
if (opts.signal?.aborted) {
|
|
706
|
+
throw new ArchiveKitError("OPERATION_CANCELLED", "ZIP creation was aborted");
|
|
707
|
+
}
|
|
708
|
+
let normalizedPath = normalizeZipPath(entry.path);
|
|
709
|
+
const isDir = entry.type === "directory" || normalizedPath.endsWith("/");
|
|
710
|
+
if (isDir && !normalizedPath.endsWith("/")) {
|
|
711
|
+
normalizedPath = `${normalizedPath}/`;
|
|
712
|
+
}
|
|
713
|
+
const data = await resolveEntryBytes(entry, opts.signal);
|
|
714
|
+
totalInputSize += data.byteLength;
|
|
715
|
+
validateTotalDecompressedSize(totalInputSize, opts.maxTotalInputSize);
|
|
716
|
+
const level = opts.compression === "store" ? 0 : entry.compressionLevel ?? opts.compressionLevel ?? 6;
|
|
717
|
+
const fileOpts = {
|
|
718
|
+
level,
|
|
719
|
+
mtime: entry.lastModified
|
|
720
|
+
};
|
|
721
|
+
zippable[normalizedPath] = [data, fileOpts];
|
|
722
|
+
}
|
|
723
|
+
return new Promise((resolve, reject) => {
|
|
724
|
+
let completed = false;
|
|
725
|
+
if (opts.signal) {
|
|
726
|
+
opts.signal.addEventListener("abort", () => {
|
|
727
|
+
if (!completed) {
|
|
728
|
+
completed = true;
|
|
729
|
+
reject(new ArchiveKitError("OPERATION_CANCELLED", "ZIP creation was aborted"));
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
zip(zippable, (err, result) => {
|
|
734
|
+
if (completed) return;
|
|
735
|
+
completed = true;
|
|
736
|
+
if (err) {
|
|
737
|
+
reject(
|
|
738
|
+
new ArchiveKitError("INVALID_INPUT", `ZIP creation failed: ${err.message}`, err)
|
|
739
|
+
);
|
|
740
|
+
} else {
|
|
741
|
+
resolve(appendZipComment(result, opts.comment));
|
|
742
|
+
}
|
|
743
|
+
});
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
function createZipSync(entries, options) {
|
|
747
|
+
const opts = getZipCreateOptions(options);
|
|
748
|
+
validateZipEntries(entries, opts);
|
|
749
|
+
const zippable = {};
|
|
750
|
+
let totalInputSize = 0;
|
|
751
|
+
for (const entry of entries) {
|
|
752
|
+
let normalizedPath = normalizeZipPath(entry.path);
|
|
753
|
+
const isDir = entry.type === "directory" || normalizedPath.endsWith("/");
|
|
754
|
+
if (isDir && !normalizedPath.endsWith("/")) {
|
|
755
|
+
normalizedPath = `${normalizedPath}/`;
|
|
756
|
+
}
|
|
757
|
+
const data = resolveEntryBytesSync(entry);
|
|
758
|
+
totalInputSize += data.byteLength;
|
|
759
|
+
validateTotalDecompressedSize(totalInputSize, opts.maxTotalInputSize);
|
|
760
|
+
const level = opts.compression === "store" ? 0 : entry.compressionLevel ?? opts.compressionLevel ?? 6;
|
|
761
|
+
const fileOpts = {
|
|
762
|
+
level,
|
|
763
|
+
mtime: entry.lastModified
|
|
764
|
+
};
|
|
765
|
+
zippable[normalizedPath] = [data, fileOpts];
|
|
766
|
+
}
|
|
767
|
+
try {
|
|
768
|
+
const result = zipSync(zippable);
|
|
769
|
+
return appendZipComment(result, opts.comment);
|
|
770
|
+
} catch (err) {
|
|
771
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
772
|
+
throw new ArchiveKitError("INVALID_INPUT", `ZIP creation failed: ${message}`, err);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
async function createZipBlob(entries, options) {
|
|
776
|
+
const bytes = await createZip(entries, options);
|
|
777
|
+
return createBlob(bytes, "application/zip");
|
|
778
|
+
}
|
|
779
|
+
async function createZipFromFiles(files, options) {
|
|
780
|
+
if (!Array.isArray(files)) {
|
|
781
|
+
throw new ArchiveKitError("INVALID_INPUT", "Expected an array of files");
|
|
782
|
+
}
|
|
783
|
+
const entries = files.map((item) => {
|
|
784
|
+
const file = item instanceof File ? item : item.file;
|
|
785
|
+
const path = item.path || file.webkitRelativePath || file.name;
|
|
786
|
+
return {
|
|
787
|
+
path,
|
|
788
|
+
data: file,
|
|
789
|
+
lastModified: new Date(file.lastModified)
|
|
790
|
+
};
|
|
791
|
+
});
|
|
792
|
+
return createZipBlob(entries, options);
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
// src/zip-extract/parse.ts
|
|
796
|
+
var SIG_LOC = 67324752;
|
|
797
|
+
var SIG_CEN = 33639248;
|
|
798
|
+
var SIG_EOCD = 101010256;
|
|
799
|
+
function readU16LE(bytes, offset) {
|
|
800
|
+
return (bytes[offset] ?? 0) | (bytes[offset + 1] ?? 0) << 8;
|
|
801
|
+
}
|
|
802
|
+
function readU32LE(bytes, offset) {
|
|
803
|
+
return ((bytes[offset] ?? 0) | (bytes[offset + 1] ?? 0) << 8 | (bytes[offset + 2] ?? 0) << 16 | (bytes[offset + 3] ?? 0) * 16777216) >>> 0;
|
|
804
|
+
}
|
|
805
|
+
function parseDosDateTime(dosTime, dosDate) {
|
|
806
|
+
const year = (dosDate >> 9 & 127) + 1980;
|
|
807
|
+
const month = (dosDate >> 5 & 15) - 1;
|
|
808
|
+
const day = Math.max(1, dosDate & 31);
|
|
809
|
+
const hour = dosTime >> 11 & 31;
|
|
810
|
+
const minute = dosTime >> 5 & 63;
|
|
811
|
+
const second = (dosTime & 31) * 2;
|
|
812
|
+
return new Date(year, month, day, hour, minute, second);
|
|
813
|
+
}
|
|
814
|
+
function findEocd(data) {
|
|
815
|
+
if (data.length < 22) return -1;
|
|
816
|
+
const minOffset = Math.max(0, data.length - 65558);
|
|
817
|
+
for (let i = data.length - 22; i >= minOffset; i--) {
|
|
818
|
+
if (readU32LE(data, i) === SIG_EOCD) {
|
|
819
|
+
return i;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
return -1;
|
|
823
|
+
}
|
|
824
|
+
function parseCentralDirectory(data, limits) {
|
|
825
|
+
const eocd = findEocd(data);
|
|
826
|
+
if (eocd === -1) {
|
|
827
|
+
throw new ArchiveKitError(
|
|
828
|
+
"INVALID_ZIP_ARCHIVE",
|
|
829
|
+
"End of Central Directory (EOCD) record not found; data is not a valid ZIP archive"
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
const totalEntries = readU16LE(data, eocd + 10);
|
|
833
|
+
const cdOffset = readU32LE(data, eocd + 16);
|
|
834
|
+
const commentLen = readU16LE(data, eocd + 20);
|
|
835
|
+
let archiveComment;
|
|
836
|
+
if (commentLen > 0 && eocd + 22 + commentLen <= data.length) {
|
|
837
|
+
const commentBytes = data.subarray(eocd + 22, eocd + 22 + commentLen);
|
|
838
|
+
try {
|
|
839
|
+
archiveComment = new TextDecoder("utf-8", { fatal: false }).decode(commentBytes);
|
|
840
|
+
} catch {
|
|
841
|
+
archiveComment = String.fromCharCode(...commentBytes);
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
const maxEntries = limits?.maxEntries ?? DEFAULT_SECURITY_LIMITS.maxEntries;
|
|
845
|
+
validateEntryCount(totalEntries, maxEntries);
|
|
846
|
+
if (cdOffset > data.length) {
|
|
847
|
+
throw new ArchiveKitError(
|
|
848
|
+
"INVALID_ZIP_ARCHIVE",
|
|
849
|
+
`Corrupted ZIP archive: Central directory offset (${cdOffset}) points beyond archive bounds (${data.length})`
|
|
850
|
+
);
|
|
851
|
+
}
|
|
852
|
+
const entries = [];
|
|
853
|
+
const seenPaths = /* @__PURE__ */ new Set();
|
|
854
|
+
let p = cdOffset;
|
|
855
|
+
let totalComp = 0;
|
|
856
|
+
let totalUncomp = 0;
|
|
857
|
+
for (let i = 0; i < totalEntries; i++) {
|
|
858
|
+
if (p + 46 > data.length) {
|
|
859
|
+
throw new ArchiveKitError(
|
|
860
|
+
"INVALID_ZIP_ARCHIVE",
|
|
861
|
+
`Truncated Central Directory header at entry ${i}`
|
|
862
|
+
);
|
|
863
|
+
}
|
|
864
|
+
const sig = readU32LE(data, p);
|
|
865
|
+
if (sig !== SIG_CEN) {
|
|
866
|
+
throw new ArchiveKitError(
|
|
867
|
+
"INVALID_ZIP_ARCHIVE",
|
|
868
|
+
`Invalid Central Directory signature 0x${sig.toString(16)} at offset ${p}`
|
|
869
|
+
);
|
|
870
|
+
}
|
|
871
|
+
const flags = readU16LE(data, p + 8);
|
|
872
|
+
const encrypted = (flags & 1) !== 0;
|
|
873
|
+
const compressionMethod = readU16LE(data, p + 10);
|
|
874
|
+
const dosTime = readU16LE(data, p + 12);
|
|
875
|
+
const dosDate = readU16LE(data, p + 14);
|
|
876
|
+
const lastModified = parseDosDateTime(dosTime, dosDate);
|
|
877
|
+
const crc32 = readU32LE(data, p + 16);
|
|
878
|
+
const compressedSize = readU32LE(data, p + 20);
|
|
879
|
+
const uncompressedSize = readU32LE(data, p + 24);
|
|
880
|
+
const fnLen = readU16LE(data, p + 28);
|
|
881
|
+
const exLen = readU16LE(data, p + 30);
|
|
882
|
+
const cmLen = readU16LE(data, p + 32);
|
|
883
|
+
const localHeaderOffset = readU32LE(data, p + 42);
|
|
884
|
+
const fnStart = p + 46;
|
|
885
|
+
const fnEnd = fnStart + fnLen;
|
|
886
|
+
if (fnEnd > data.length) {
|
|
887
|
+
throw new ArchiveKitError(
|
|
888
|
+
"INVALID_ZIP_ARCHIVE",
|
|
889
|
+
`Truncated filename in Central Directory entry ${i}`
|
|
890
|
+
);
|
|
891
|
+
}
|
|
892
|
+
const rawFnBytes = data.subarray(fnStart, fnEnd);
|
|
893
|
+
let rawPath;
|
|
894
|
+
try {
|
|
895
|
+
rawPath = new TextDecoder("utf-8", { fatal: false }).decode(rawFnBytes);
|
|
896
|
+
} catch {
|
|
897
|
+
rawPath = String.fromCharCode(...rawFnBytes);
|
|
898
|
+
}
|
|
899
|
+
const normalizedPath = normalizeZipPath(rawPath);
|
|
900
|
+
const isDirectory = rawPath.endsWith("/") || normalizedPath.endsWith("/");
|
|
901
|
+
if (seenPaths.has(normalizedPath)) {
|
|
902
|
+
throw new ArchiveKitError(
|
|
903
|
+
"DUPLICATE_ENTRY",
|
|
904
|
+
`Duplicate archive path detected: "${normalizedPath}"`
|
|
905
|
+
);
|
|
906
|
+
}
|
|
907
|
+
seenPaths.add(normalizedPath);
|
|
908
|
+
let entryComment;
|
|
909
|
+
const cmStart = fnEnd + exLen;
|
|
910
|
+
const cmEnd = cmStart + cmLen;
|
|
911
|
+
if (cmLen > 0 && cmEnd <= data.length) {
|
|
912
|
+
const cmBytes = data.subarray(cmStart, cmEnd);
|
|
913
|
+
try {
|
|
914
|
+
entryComment = new TextDecoder("utf-8", { fatal: false }).decode(cmBytes);
|
|
915
|
+
} catch {
|
|
916
|
+
entryComment = String.fromCharCode(...cmBytes);
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
validateDecompressedSize(uncompressedSize, limits?.maxEntrySize, normalizedPath);
|
|
920
|
+
validateCompressionRatio(uncompressedSize, compressedSize, limits?.maxCompressionRatio, normalizedPath);
|
|
921
|
+
totalComp += compressedSize;
|
|
922
|
+
totalUncomp += uncompressedSize;
|
|
923
|
+
validateTotalDecompressedSize(totalUncomp, limits?.maxTotalSize);
|
|
924
|
+
entries.push({
|
|
925
|
+
path: normalizedPath,
|
|
926
|
+
name: getBaseName(normalizedPath),
|
|
927
|
+
isDirectory,
|
|
928
|
+
compressedSize,
|
|
929
|
+
uncompressedSize,
|
|
930
|
+
compressionMethod,
|
|
931
|
+
lastModified,
|
|
932
|
+
crc32,
|
|
933
|
+
comment: entryComment,
|
|
934
|
+
encrypted,
|
|
935
|
+
localHeaderOffset
|
|
936
|
+
});
|
|
937
|
+
p = cmEnd;
|
|
938
|
+
}
|
|
939
|
+
return {
|
|
940
|
+
entries,
|
|
941
|
+
comment: archiveComment,
|
|
942
|
+
eocdOffset: eocd,
|
|
943
|
+
totalCompressedSize: totalComp,
|
|
944
|
+
totalUncompressedSize: totalUncomp
|
|
945
|
+
};
|
|
946
|
+
}
|
|
947
|
+
function readLocalHeader(data, localHeaderOffset) {
|
|
948
|
+
if (localHeaderOffset + 30 > data.length) {
|
|
949
|
+
throw new ArchiveKitError(
|
|
950
|
+
"INVALID_ZIP_ARCHIVE",
|
|
951
|
+
`Truncated local file header at offset ${localHeaderOffset}`
|
|
952
|
+
);
|
|
953
|
+
}
|
|
954
|
+
const sig = readU32LE(data, localHeaderOffset);
|
|
955
|
+
if (sig !== SIG_LOC) {
|
|
956
|
+
throw new ArchiveKitError(
|
|
957
|
+
"INVALID_ZIP_ARCHIVE",
|
|
958
|
+
`Expected local file header signature 0x04034b50 at offset ${localHeaderOffset}, found 0x${sig.toString(16)}`
|
|
959
|
+
);
|
|
960
|
+
}
|
|
961
|
+
const compressionMethod = readU16LE(data, localHeaderOffset + 8);
|
|
962
|
+
const crc32 = readU32LE(data, localHeaderOffset + 14);
|
|
963
|
+
const compressedSize = readU32LE(data, localHeaderOffset + 18);
|
|
964
|
+
const uncompressedSize = readU32LE(data, localHeaderOffset + 22);
|
|
965
|
+
const fnLen = readU16LE(data, localHeaderOffset + 26);
|
|
966
|
+
const exLen = readU16LE(data, localHeaderOffset + 28);
|
|
967
|
+
const dataOffset = localHeaderOffset + 30 + fnLen + exLen;
|
|
968
|
+
if (dataOffset > data.length) {
|
|
969
|
+
throw new ArchiveKitError(
|
|
970
|
+
"INVALID_ZIP_ARCHIVE",
|
|
971
|
+
`Data offset ${dataOffset} extends beyond archive boundary (${data.length})`
|
|
972
|
+
);
|
|
973
|
+
}
|
|
974
|
+
return {
|
|
975
|
+
dataOffset,
|
|
976
|
+
compressedSize,
|
|
977
|
+
uncompressedSize,
|
|
978
|
+
compressionMethod,
|
|
979
|
+
crc32
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
// src/zip-extract/list-entries.ts
|
|
984
|
+
function isZipArchiveSync(data) {
|
|
985
|
+
try {
|
|
986
|
+
const bytes = toUint8ArraySync(data);
|
|
987
|
+
if (bytes.length < 22) return false;
|
|
988
|
+
const firstSig = readU32LE(bytes, 0);
|
|
989
|
+
if (firstSig !== SIG_LOC && firstSig !== SIG_EOCD) {
|
|
990
|
+
return false;
|
|
991
|
+
}
|
|
992
|
+
return findEocd(bytes) !== -1;
|
|
993
|
+
} catch {
|
|
994
|
+
return false;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
async function isZipArchive(data) {
|
|
998
|
+
try {
|
|
999
|
+
const bytes = await toUint8Array(data);
|
|
1000
|
+
return isZipArchiveSync(bytes);
|
|
1001
|
+
} catch {
|
|
1002
|
+
return false;
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
async function listZipEntries(data, options) {
|
|
1006
|
+
const bytes = await toUint8Array(data);
|
|
1007
|
+
const parsed = parseCentralDirectory(bytes, options);
|
|
1008
|
+
return parsed.entries.map((entry) => ({
|
|
1009
|
+
path: entry.path,
|
|
1010
|
+
name: entry.name,
|
|
1011
|
+
isDirectory: entry.isDirectory,
|
|
1012
|
+
compressedSize: entry.compressedSize,
|
|
1013
|
+
uncompressedSize: entry.uncompressedSize,
|
|
1014
|
+
compressionMethod: entry.compressionMethod,
|
|
1015
|
+
lastModified: entry.lastModified,
|
|
1016
|
+
crc32: entry.crc32,
|
|
1017
|
+
comment: entry.comment,
|
|
1018
|
+
encrypted: entry.encrypted
|
|
1019
|
+
}));
|
|
1020
|
+
}
|
|
1021
|
+
async function getZipMetadata(data) {
|
|
1022
|
+
const bytes = await toUint8Array(data);
|
|
1023
|
+
const parsed = parseCentralDirectory(bytes);
|
|
1024
|
+
const hasEncryptedEntries = parsed.entries.some((e) => e.encrypted);
|
|
1025
|
+
const ratio = calculateRatio(parsed.totalCompressedSize, parsed.totalUncompressedSize);
|
|
1026
|
+
return {
|
|
1027
|
+
isValid: true,
|
|
1028
|
+
entryCount: parsed.entries.length,
|
|
1029
|
+
totalCompressedSize: parsed.totalCompressedSize,
|
|
1030
|
+
totalUncompressedSize: parsed.totalUncompressedSize,
|
|
1031
|
+
compressionRatio: ratio,
|
|
1032
|
+
hasEncryptedEntries,
|
|
1033
|
+
comment: parsed.comment,
|
|
1034
|
+
entries: parsed.entries.map((e) => ({
|
|
1035
|
+
path: e.path,
|
|
1036
|
+
name: e.name,
|
|
1037
|
+
isDirectory: e.isDirectory,
|
|
1038
|
+
compressedSize: e.compressedSize,
|
|
1039
|
+
uncompressedSize: e.uncompressedSize,
|
|
1040
|
+
compressionMethod: e.compressionMethod,
|
|
1041
|
+
lastModified: e.lastModified,
|
|
1042
|
+
crc32: e.crc32,
|
|
1043
|
+
comment: e.comment,
|
|
1044
|
+
encrypted: e.encrypted
|
|
1045
|
+
}))
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
function createExtractedEntry(entry, data) {
|
|
1049
|
+
return {
|
|
1050
|
+
path: entry.path,
|
|
1051
|
+
name: entry.name,
|
|
1052
|
+
isDirectory: entry.isDirectory,
|
|
1053
|
+
compressedSize: entry.compressedSize,
|
|
1054
|
+
uncompressedSize: entry.uncompressedSize,
|
|
1055
|
+
compressionMethod: entry.compressionMethod,
|
|
1056
|
+
lastModified: entry.lastModified,
|
|
1057
|
+
crc32: entry.crc32,
|
|
1058
|
+
comment: entry.comment,
|
|
1059
|
+
encrypted: entry.encrypted,
|
|
1060
|
+
data,
|
|
1061
|
+
text() {
|
|
1062
|
+
return decodeUtf8(this.data);
|
|
1063
|
+
},
|
|
1064
|
+
blob(mimeType = "application/octet-stream") {
|
|
1065
|
+
return createBlob(this.data, mimeType);
|
|
1066
|
+
}
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
async function decompressEntryPayload(archiveBytes, entry, signal, maxEntrySize) {
|
|
1070
|
+
if (signal?.aborted) {
|
|
1071
|
+
throw new ArchiveKitError("OPERATION_CANCELLED", "Decompression was aborted");
|
|
1072
|
+
}
|
|
1073
|
+
if (entry.encrypted) {
|
|
1074
|
+
throw new ArchiveKitError(
|
|
1075
|
+
"UNSUPPORTED_ENCRYPTION",
|
|
1076
|
+
`Cannot extract encrypted entry "${entry.path}". Password/AES encryption is not supported.`
|
|
1077
|
+
);
|
|
1078
|
+
}
|
|
1079
|
+
if (entry.isDirectory) {
|
|
1080
|
+
return new Uint8Array(0);
|
|
1081
|
+
}
|
|
1082
|
+
const loc = readLocalHeader(archiveBytes, entry.localHeaderOffset);
|
|
1083
|
+
const compressedSize = entry.compressedSize || loc.compressedSize;
|
|
1084
|
+
const uncompressedSize = entry.uncompressedSize || loc.uncompressedSize;
|
|
1085
|
+
const method = entry.compressionMethod;
|
|
1086
|
+
const dataEnd = loc.dataOffset + compressedSize;
|
|
1087
|
+
if (dataEnd > archiveBytes.length) {
|
|
1088
|
+
throw new ArchiveKitError(
|
|
1089
|
+
"INVALID_ZIP_ARCHIVE",
|
|
1090
|
+
`Truncated compressed entry data for "${entry.path}"`
|
|
1091
|
+
);
|
|
1092
|
+
}
|
|
1093
|
+
const compressedData = archiveBytes.subarray(loc.dataOffset, dataEnd);
|
|
1094
|
+
if (method === ZIP_COMPRESSION_METHOD.STORE) {
|
|
1095
|
+
validateDecompressedSize(compressedData.byteLength, maxEntrySize, entry.path);
|
|
1096
|
+
if (entry.crc32 !== void 0 && entry.crc32 !== 0) {
|
|
1097
|
+
const actualCrc = computeCrc32(compressedData);
|
|
1098
|
+
if (actualCrc !== entry.crc32) {
|
|
1099
|
+
throw new ArchiveKitError(
|
|
1100
|
+
"DECOMPRESSION_FAILED",
|
|
1101
|
+
`CRC-32 checksum mismatch for STORE entry "${entry.path}": expected 0x${entry.crc32.toString(16)}, got 0x${actualCrc.toString(16)}`
|
|
1102
|
+
);
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
return new Uint8Array(compressedData);
|
|
1106
|
+
}
|
|
1107
|
+
if (method === ZIP_COMPRESSION_METHOD.DEFLATE) {
|
|
1108
|
+
return new Promise((resolve, reject) => {
|
|
1109
|
+
let completed = false;
|
|
1110
|
+
if (signal) {
|
|
1111
|
+
signal.addEventListener("abort", () => {
|
|
1112
|
+
if (!completed) {
|
|
1113
|
+
completed = true;
|
|
1114
|
+
reject(new ArchiveKitError("OPERATION_CANCELLED", "Decompression was aborted"));
|
|
1115
|
+
}
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
1118
|
+
inflate(compressedData, { size: uncompressedSize }, (err, result) => {
|
|
1119
|
+
if (completed) return;
|
|
1120
|
+
completed = true;
|
|
1121
|
+
if (err) {
|
|
1122
|
+
reject(
|
|
1123
|
+
new ArchiveKitError(
|
|
1124
|
+
"DECOMPRESSION_FAILED",
|
|
1125
|
+
`Failed to decompress DEFLATE entry "${entry.path}": ${err.message}`,
|
|
1126
|
+
err
|
|
1127
|
+
)
|
|
1128
|
+
);
|
|
1129
|
+
return;
|
|
1130
|
+
}
|
|
1131
|
+
try {
|
|
1132
|
+
validateDecompressedSize(result.byteLength, maxEntrySize, entry.path);
|
|
1133
|
+
} catch (e) {
|
|
1134
|
+
reject(e);
|
|
1135
|
+
return;
|
|
1136
|
+
}
|
|
1137
|
+
if (entry.crc32 !== void 0 && entry.crc32 !== 0) {
|
|
1138
|
+
const actualCrc = computeCrc32(result);
|
|
1139
|
+
if (actualCrc !== entry.crc32) {
|
|
1140
|
+
reject(
|
|
1141
|
+
new ArchiveKitError(
|
|
1142
|
+
"DECOMPRESSION_FAILED",
|
|
1143
|
+
`CRC-32 checksum mismatch for DEFLATE entry "${entry.path}": expected 0x${entry.crc32.toString(16)}, got 0x${actualCrc.toString(16)}`
|
|
1144
|
+
)
|
|
1145
|
+
);
|
|
1146
|
+
return;
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
resolve(result);
|
|
1150
|
+
});
|
|
1151
|
+
});
|
|
1152
|
+
}
|
|
1153
|
+
throw new ArchiveKitError(
|
|
1154
|
+
"UNSUPPORTED_COMPRESSION",
|
|
1155
|
+
`Unsupported compression method (${method}) for entry "${entry.path}". Supported methods: STORE (0), DEFLATE (8).`
|
|
1156
|
+
);
|
|
1157
|
+
}
|
|
1158
|
+
async function extractZip(data, options) {
|
|
1159
|
+
if (options?.signal?.aborted) {
|
|
1160
|
+
throw new ArchiveKitError("OPERATION_CANCELLED", "Extraction was aborted");
|
|
1161
|
+
}
|
|
1162
|
+
const archiveBytes = await toUint8Array(data);
|
|
1163
|
+
const parsed = parseCentralDirectory(archiveBytes, options);
|
|
1164
|
+
const filter = options?.filter;
|
|
1165
|
+
const entriesToExtract = filter ? parsed.entries.filter((e) => filter(e)) : parsed.entries;
|
|
1166
|
+
const extracted = [];
|
|
1167
|
+
let totalExtractedBytes = 0;
|
|
1168
|
+
let fileCount = 0;
|
|
1169
|
+
let dirCount = 0;
|
|
1170
|
+
for (const entry of entriesToExtract) {
|
|
1171
|
+
if (options?.signal?.aborted) {
|
|
1172
|
+
throw new ArchiveKitError("OPERATION_CANCELLED", "Extraction was aborted");
|
|
1173
|
+
}
|
|
1174
|
+
const payload = await decompressEntryPayload(
|
|
1175
|
+
archiveBytes,
|
|
1176
|
+
entry,
|
|
1177
|
+
options?.signal,
|
|
1178
|
+
options?.maxEntrySize
|
|
1179
|
+
);
|
|
1180
|
+
totalExtractedBytes += payload.byteLength;
|
|
1181
|
+
validateTotalDecompressedSize(totalExtractedBytes, options?.maxTotalSize);
|
|
1182
|
+
if (entry.isDirectory) {
|
|
1183
|
+
dirCount++;
|
|
1184
|
+
} else {
|
|
1185
|
+
fileCount++;
|
|
1186
|
+
}
|
|
1187
|
+
extracted.push(createExtractedEntry(entry, payload));
|
|
1188
|
+
}
|
|
1189
|
+
return {
|
|
1190
|
+
entries: extracted,
|
|
1191
|
+
totalFiles: fileCount,
|
|
1192
|
+
totalDirectories: dirCount,
|
|
1193
|
+
totalCompressedSize: parsed.totalCompressedSize,
|
|
1194
|
+
totalUncompressedSize: totalExtractedBytes,
|
|
1195
|
+
comment: parsed.comment
|
|
1196
|
+
};
|
|
1197
|
+
}
|
|
1198
|
+
async function extractZipEntry(data, path, options) {
|
|
1199
|
+
if (options?.signal?.aborted) {
|
|
1200
|
+
throw new ArchiveKitError("OPERATION_CANCELLED", "Extraction was aborted");
|
|
1201
|
+
}
|
|
1202
|
+
const normalizedTarget = normalizeZipPath(path);
|
|
1203
|
+
const archiveBytes = await toUint8Array(data);
|
|
1204
|
+
const parsed = parseCentralDirectory(archiveBytes, options);
|
|
1205
|
+
const entry = parsed.entries.find((e) => e.path === normalizedTarget);
|
|
1206
|
+
if (!entry) {
|
|
1207
|
+
throw new ArchiveKitError(
|
|
1208
|
+
"INVALID_INPUT",
|
|
1209
|
+
`Entry "${normalizedTarget}" was not found in the ZIP archive`
|
|
1210
|
+
);
|
|
1211
|
+
}
|
|
1212
|
+
const payload = await decompressEntryPayload(
|
|
1213
|
+
archiveBytes,
|
|
1214
|
+
entry,
|
|
1215
|
+
options?.signal,
|
|
1216
|
+
options?.maxEntrySize
|
|
1217
|
+
);
|
|
1218
|
+
return createExtractedEntry(entry, payload);
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
// src/zip-extract/validate-archive.ts
|
|
1222
|
+
async function validateZipArchive(data, options) {
|
|
1223
|
+
const errors = [];
|
|
1224
|
+
const warnings = [];
|
|
1225
|
+
let entryCount = 0;
|
|
1226
|
+
let totalComp = 0;
|
|
1227
|
+
let totalUncomp = 0;
|
|
1228
|
+
try {
|
|
1229
|
+
const bytes = await toUint8Array(data);
|
|
1230
|
+
const parsed = parseCentralDirectory(bytes, options);
|
|
1231
|
+
entryCount = parsed.entries.length;
|
|
1232
|
+
totalComp = parsed.totalCompressedSize;
|
|
1233
|
+
totalUncomp = parsed.totalUncompressedSize;
|
|
1234
|
+
for (const entry of parsed.entries) {
|
|
1235
|
+
if (entry.encrypted) {
|
|
1236
|
+
errors.push(`Entry "${entry.path}" is encrypted (unsupported format).`);
|
|
1237
|
+
}
|
|
1238
|
+
if (entry.compressionMethod !== ZIP_COMPRESSION_METHOD.STORE && entry.compressionMethod !== ZIP_COMPRESSION_METHOD.DEFLATE) {
|
|
1239
|
+
errors.push(
|
|
1240
|
+
`Entry "${entry.path}" uses unsupported compression method ${entry.compressionMethod}.`
|
|
1241
|
+
);
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
} catch (err) {
|
|
1245
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
1246
|
+
errors.push(msg);
|
|
1247
|
+
}
|
|
1248
|
+
return {
|
|
1249
|
+
valid: errors.length === 0,
|
|
1250
|
+
errors,
|
|
1251
|
+
warnings,
|
|
1252
|
+
entryCount,
|
|
1253
|
+
totalCompressedSize: totalComp,
|
|
1254
|
+
totalUncompressedSize: totalUncomp
|
|
1255
|
+
};
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
export { ArchiveKitError, DEFAULT_SECURITY_LIMITS, SIG_CEN, SIG_EOCD, SIG_LOC, ZIP_COMPRESSION_METHOD, calculatePercentageSaved, calculateRatio, compareUint8Arrays, computeCrc32, concatUint8Arrays, createBlob, createZip, createZipBlob, createZipFromFiles, createZipSync, decodeUtf8, encodeUtf8, extractZip, extractZipEntry, findEocd, getBaseName, getDirName, getGzipMetadata, getGzipMetadataSync, getZipCreateOptions, getZipMetadata, gzipCompress, gzipCompressBlob, gzipCompressSync, gzipCompressText, gzipCompressTextSync, gzipDecompress, gzipDecompressBlob, gzipDecompressSync, gzipDecompressText, gzipDecompressTextSync, isDirPath, isGzipData, isGzipDataSync, isZipArchive, isZipArchiveSync, listZipEntries, normalizeZipPath, parseCentralDirectory, readBlobAsUint8Array, readLocalHeader, readU16LE, readU32LE, toArrayBuffer, toUint8Array, toUint8ArraySync, validateCompressionLevel, validateCompressionRatio, validateDecompressedSize, validateEntryCount, validateTotalDecompressedSize, validateZipArchive, validateZipEntries };
|
|
1259
|
+
//# sourceMappingURL=index.js.map
|
|
1260
|
+
//# sourceMappingURL=index.js.map
|