@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
|
@@ -0,0 +1,685 @@
|
|
|
1
|
+
import { 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
|
+
new TextEncoder();
|
|
68
|
+
var textDecoder = new TextDecoder("utf-8", { fatal: true });
|
|
69
|
+
function decodeUtf8(bytes) {
|
|
70
|
+
try {
|
|
71
|
+
return textDecoder.decode(bytes);
|
|
72
|
+
} catch (err) {
|
|
73
|
+
throw new ArchiveKitError(
|
|
74
|
+
"INVALID_INPUT",
|
|
75
|
+
"Data is not valid UTF-8 text",
|
|
76
|
+
err
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
var CRC_TABLE = new Uint32Array(256);
|
|
81
|
+
for (let i = 0; i < 256; i++) {
|
|
82
|
+
let c = i;
|
|
83
|
+
for (let k = 0; k < 8; k++) {
|
|
84
|
+
c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
85
|
+
}
|
|
86
|
+
CRC_TABLE[i] = c >>> 0;
|
|
87
|
+
}
|
|
88
|
+
function computeCrc32(data) {
|
|
89
|
+
let crc = 4294967295;
|
|
90
|
+
for (let i = 0; i < data.length; i++) {
|
|
91
|
+
const byte = data[i] ?? 0;
|
|
92
|
+
crc = (CRC_TABLE[(crc ^ byte) & 255] ?? 0) ^ crc >>> 8;
|
|
93
|
+
}
|
|
94
|
+
return (crc ^ 4294967295) >>> 0;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/shared/blob.ts
|
|
98
|
+
function createBlob(data, mimeType = "application/octet-stream") {
|
|
99
|
+
if (typeof Blob === "undefined") {
|
|
100
|
+
throw new ArchiveKitError(
|
|
101
|
+
"INVALID_INPUT",
|
|
102
|
+
"Blob API is not available in the current environment"
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
const parts = Array.isArray(data) ? data : [data];
|
|
106
|
+
return new Blob(parts, { type: mimeType });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// src/shared/path.ts
|
|
110
|
+
function normalizeZipPath(rawPath) {
|
|
111
|
+
if (!rawPath || typeof rawPath !== "string") {
|
|
112
|
+
throw new ArchiveKitError(
|
|
113
|
+
"INVALID_ARCHIVE_PATH",
|
|
114
|
+
"Archive path must be a non-empty string"
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
if (rawPath.includes("\0")) {
|
|
118
|
+
throw new ArchiveKitError(
|
|
119
|
+
"INVALID_ARCHIVE_PATH",
|
|
120
|
+
`Archive path contains NUL byte: "${rawPath}"`
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
const path = rawPath.replace(/\\/g, "/");
|
|
124
|
+
if (/^[a-zA-Z]:/i.test(path)) {
|
|
125
|
+
throw new ArchiveKitError(
|
|
126
|
+
"INVALID_ARCHIVE_PATH",
|
|
127
|
+
`Absolute Windows drive paths are prohibited: "${rawPath}"`
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
if (path.startsWith("//")) {
|
|
131
|
+
throw new ArchiveKitError(
|
|
132
|
+
"INVALID_ARCHIVE_PATH",
|
|
133
|
+
`UNC network paths are prohibited: "${rawPath}"`
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
if (path.startsWith("/")) {
|
|
137
|
+
throw new ArchiveKitError(
|
|
138
|
+
"INVALID_ARCHIVE_PATH",
|
|
139
|
+
`Absolute root paths are prohibited: "${rawPath}"`
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
const isDirectory = path.endsWith("/");
|
|
143
|
+
const segments = path.split("/");
|
|
144
|
+
const cleaned = [];
|
|
145
|
+
for (const seg of segments) {
|
|
146
|
+
if (seg === "" || seg === ".") {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (seg === "..") {
|
|
150
|
+
throw new ArchiveKitError(
|
|
151
|
+
"INVALID_ARCHIVE_PATH",
|
|
152
|
+
`Path traversal ("..") is strictly prohibited (Zip Slip): "${rawPath}"`
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
cleaned.push(seg);
|
|
156
|
+
}
|
|
157
|
+
if (cleaned.length === 0) {
|
|
158
|
+
throw new ArchiveKitError(
|
|
159
|
+
"INVALID_ARCHIVE_PATH",
|
|
160
|
+
`Path resolved to an empty path: "${rawPath}"`
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
const result = cleaned.join("/");
|
|
164
|
+
return isDirectory ? `${result}/` : result;
|
|
165
|
+
}
|
|
166
|
+
function getBaseName(path) {
|
|
167
|
+
const normalized = path.replace(/\\/g, "/").replace(/\/+$/, "");
|
|
168
|
+
const lastSlash = normalized.lastIndexOf("/");
|
|
169
|
+
return lastSlash === -1 ? normalized : normalized.slice(lastSlash + 1);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// src/shared/validation.ts
|
|
173
|
+
var DEFAULT_SECURITY_LIMITS = {
|
|
174
|
+
maxEntries: 1e4,
|
|
175
|
+
maxEntrySize: 250 * 1024 * 1024,
|
|
176
|
+
// 250 MB
|
|
177
|
+
maxTotalSize: 1024 * 1024 * 1024,
|
|
178
|
+
// 1 GB
|
|
179
|
+
maxCompressionRatio: 100};
|
|
180
|
+
function validateEntryCount(count, limit = DEFAULT_SECURITY_LIMITS.maxEntries) {
|
|
181
|
+
if (count > limit) {
|
|
182
|
+
throw new ArchiveKitError(
|
|
183
|
+
"ENTRY_LIMIT_EXCEEDED",
|
|
184
|
+
`Archive entry count (${count}) exceeds the maximum limit of ${limit}`
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function validateDecompressedSize(size, limit = DEFAULT_SECURITY_LIMITS.maxEntrySize, path) {
|
|
189
|
+
if (size > limit) {
|
|
190
|
+
throw new ArchiveKitError(
|
|
191
|
+
"FILE_SIZE_LIMIT_EXCEEDED",
|
|
192
|
+
`Entry ${path ? `"${path}" ` : ""}decompressed size (${size} bytes) exceeds limit of ${limit} bytes`
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function validateTotalDecompressedSize(size, limit = DEFAULT_SECURITY_LIMITS.maxTotalSize) {
|
|
197
|
+
if (size > limit) {
|
|
198
|
+
throw new ArchiveKitError(
|
|
199
|
+
"TOTAL_SIZE_LIMIT_EXCEEDED",
|
|
200
|
+
`Total uncompressed size (${size} bytes) exceeds maximum limit of ${limit} bytes`
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function validateCompressionRatio(uncompressed, compressed, maxRatio = DEFAULT_SECURITY_LIMITS.maxCompressionRatio, path) {
|
|
205
|
+
if (uncompressed > 1024 * 1024) {
|
|
206
|
+
const ratio = uncompressed / Math.max(1, compressed);
|
|
207
|
+
if (ratio > maxRatio) {
|
|
208
|
+
throw new ArchiveKitError(
|
|
209
|
+
"COMPRESSION_RATIO_LIMIT_EXCEEDED",
|
|
210
|
+
`Compression ratio (${ratio.toFixed(1)}:1) for ${path ? `"${path}" ` : ""}exceeds safe threshold of ${maxRatio}:1`
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function calculateRatio(compressed, original) {
|
|
216
|
+
if (original <= 0) return 1;
|
|
217
|
+
return Number((compressed / original).toFixed(4));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// src/zip-extract/parse.ts
|
|
221
|
+
var SIG_LOC = 67324752;
|
|
222
|
+
var SIG_CEN = 33639248;
|
|
223
|
+
var SIG_EOCD = 101010256;
|
|
224
|
+
function readU16LE(bytes, offset) {
|
|
225
|
+
return (bytes[offset] ?? 0) | (bytes[offset + 1] ?? 0) << 8;
|
|
226
|
+
}
|
|
227
|
+
function readU32LE(bytes, offset) {
|
|
228
|
+
return ((bytes[offset] ?? 0) | (bytes[offset + 1] ?? 0) << 8 | (bytes[offset + 2] ?? 0) << 16 | (bytes[offset + 3] ?? 0) * 16777216) >>> 0;
|
|
229
|
+
}
|
|
230
|
+
function parseDosDateTime(dosTime, dosDate) {
|
|
231
|
+
const year = (dosDate >> 9 & 127) + 1980;
|
|
232
|
+
const month = (dosDate >> 5 & 15) - 1;
|
|
233
|
+
const day = Math.max(1, dosDate & 31);
|
|
234
|
+
const hour = dosTime >> 11 & 31;
|
|
235
|
+
const minute = dosTime >> 5 & 63;
|
|
236
|
+
const second = (dosTime & 31) * 2;
|
|
237
|
+
return new Date(year, month, day, hour, minute, second);
|
|
238
|
+
}
|
|
239
|
+
function findEocd(data) {
|
|
240
|
+
if (data.length < 22) return -1;
|
|
241
|
+
const minOffset = Math.max(0, data.length - 65558);
|
|
242
|
+
for (let i = data.length - 22; i >= minOffset; i--) {
|
|
243
|
+
if (readU32LE(data, i) === SIG_EOCD) {
|
|
244
|
+
return i;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return -1;
|
|
248
|
+
}
|
|
249
|
+
function parseCentralDirectory(data, limits) {
|
|
250
|
+
const eocd = findEocd(data);
|
|
251
|
+
if (eocd === -1) {
|
|
252
|
+
throw new ArchiveKitError(
|
|
253
|
+
"INVALID_ZIP_ARCHIVE",
|
|
254
|
+
"End of Central Directory (EOCD) record not found; data is not a valid ZIP archive"
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
const totalEntries = readU16LE(data, eocd + 10);
|
|
258
|
+
const cdOffset = readU32LE(data, eocd + 16);
|
|
259
|
+
const commentLen = readU16LE(data, eocd + 20);
|
|
260
|
+
let archiveComment;
|
|
261
|
+
if (commentLen > 0 && eocd + 22 + commentLen <= data.length) {
|
|
262
|
+
const commentBytes = data.subarray(eocd + 22, eocd + 22 + commentLen);
|
|
263
|
+
try {
|
|
264
|
+
archiveComment = new TextDecoder("utf-8", { fatal: false }).decode(commentBytes);
|
|
265
|
+
} catch {
|
|
266
|
+
archiveComment = String.fromCharCode(...commentBytes);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
const maxEntries = limits?.maxEntries ?? DEFAULT_SECURITY_LIMITS.maxEntries;
|
|
270
|
+
validateEntryCount(totalEntries, maxEntries);
|
|
271
|
+
if (cdOffset > data.length) {
|
|
272
|
+
throw new ArchiveKitError(
|
|
273
|
+
"INVALID_ZIP_ARCHIVE",
|
|
274
|
+
`Corrupted ZIP archive: Central directory offset (${cdOffset}) points beyond archive bounds (${data.length})`
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
const entries = [];
|
|
278
|
+
const seenPaths = /* @__PURE__ */ new Set();
|
|
279
|
+
let p = cdOffset;
|
|
280
|
+
let totalComp = 0;
|
|
281
|
+
let totalUncomp = 0;
|
|
282
|
+
for (let i = 0; i < totalEntries; i++) {
|
|
283
|
+
if (p + 46 > data.length) {
|
|
284
|
+
throw new ArchiveKitError(
|
|
285
|
+
"INVALID_ZIP_ARCHIVE",
|
|
286
|
+
`Truncated Central Directory header at entry ${i}`
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
const sig = readU32LE(data, p);
|
|
290
|
+
if (sig !== SIG_CEN) {
|
|
291
|
+
throw new ArchiveKitError(
|
|
292
|
+
"INVALID_ZIP_ARCHIVE",
|
|
293
|
+
`Invalid Central Directory signature 0x${sig.toString(16)} at offset ${p}`
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
const flags = readU16LE(data, p + 8);
|
|
297
|
+
const encrypted = (flags & 1) !== 0;
|
|
298
|
+
const compressionMethod = readU16LE(data, p + 10);
|
|
299
|
+
const dosTime = readU16LE(data, p + 12);
|
|
300
|
+
const dosDate = readU16LE(data, p + 14);
|
|
301
|
+
const lastModified = parseDosDateTime(dosTime, dosDate);
|
|
302
|
+
const crc32 = readU32LE(data, p + 16);
|
|
303
|
+
const compressedSize = readU32LE(data, p + 20);
|
|
304
|
+
const uncompressedSize = readU32LE(data, p + 24);
|
|
305
|
+
const fnLen = readU16LE(data, p + 28);
|
|
306
|
+
const exLen = readU16LE(data, p + 30);
|
|
307
|
+
const cmLen = readU16LE(data, p + 32);
|
|
308
|
+
const localHeaderOffset = readU32LE(data, p + 42);
|
|
309
|
+
const fnStart = p + 46;
|
|
310
|
+
const fnEnd = fnStart + fnLen;
|
|
311
|
+
if (fnEnd > data.length) {
|
|
312
|
+
throw new ArchiveKitError(
|
|
313
|
+
"INVALID_ZIP_ARCHIVE",
|
|
314
|
+
`Truncated filename in Central Directory entry ${i}`
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
const rawFnBytes = data.subarray(fnStart, fnEnd);
|
|
318
|
+
let rawPath;
|
|
319
|
+
try {
|
|
320
|
+
rawPath = new TextDecoder("utf-8", { fatal: false }).decode(rawFnBytes);
|
|
321
|
+
} catch {
|
|
322
|
+
rawPath = String.fromCharCode(...rawFnBytes);
|
|
323
|
+
}
|
|
324
|
+
const normalizedPath = normalizeZipPath(rawPath);
|
|
325
|
+
const isDirectory = rawPath.endsWith("/") || normalizedPath.endsWith("/");
|
|
326
|
+
if (seenPaths.has(normalizedPath)) {
|
|
327
|
+
throw new ArchiveKitError(
|
|
328
|
+
"DUPLICATE_ENTRY",
|
|
329
|
+
`Duplicate archive path detected: "${normalizedPath}"`
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
seenPaths.add(normalizedPath);
|
|
333
|
+
let entryComment;
|
|
334
|
+
const cmStart = fnEnd + exLen;
|
|
335
|
+
const cmEnd = cmStart + cmLen;
|
|
336
|
+
if (cmLen > 0 && cmEnd <= data.length) {
|
|
337
|
+
const cmBytes = data.subarray(cmStart, cmEnd);
|
|
338
|
+
try {
|
|
339
|
+
entryComment = new TextDecoder("utf-8", { fatal: false }).decode(cmBytes);
|
|
340
|
+
} catch {
|
|
341
|
+
entryComment = String.fromCharCode(...cmBytes);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
validateDecompressedSize(uncompressedSize, limits?.maxEntrySize, normalizedPath);
|
|
345
|
+
validateCompressionRatio(uncompressedSize, compressedSize, limits?.maxCompressionRatio, normalizedPath);
|
|
346
|
+
totalComp += compressedSize;
|
|
347
|
+
totalUncomp += uncompressedSize;
|
|
348
|
+
validateTotalDecompressedSize(totalUncomp, limits?.maxTotalSize);
|
|
349
|
+
entries.push({
|
|
350
|
+
path: normalizedPath,
|
|
351
|
+
name: getBaseName(normalizedPath),
|
|
352
|
+
isDirectory,
|
|
353
|
+
compressedSize,
|
|
354
|
+
uncompressedSize,
|
|
355
|
+
compressionMethod,
|
|
356
|
+
lastModified,
|
|
357
|
+
crc32,
|
|
358
|
+
comment: entryComment,
|
|
359
|
+
encrypted,
|
|
360
|
+
localHeaderOffset
|
|
361
|
+
});
|
|
362
|
+
p = cmEnd;
|
|
363
|
+
}
|
|
364
|
+
return {
|
|
365
|
+
entries,
|
|
366
|
+
comment: archiveComment,
|
|
367
|
+
eocdOffset: eocd,
|
|
368
|
+
totalCompressedSize: totalComp,
|
|
369
|
+
totalUncompressedSize: totalUncomp
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
function readLocalHeader(data, localHeaderOffset) {
|
|
373
|
+
if (localHeaderOffset + 30 > data.length) {
|
|
374
|
+
throw new ArchiveKitError(
|
|
375
|
+
"INVALID_ZIP_ARCHIVE",
|
|
376
|
+
`Truncated local file header at offset ${localHeaderOffset}`
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
const sig = readU32LE(data, localHeaderOffset);
|
|
380
|
+
if (sig !== SIG_LOC) {
|
|
381
|
+
throw new ArchiveKitError(
|
|
382
|
+
"INVALID_ZIP_ARCHIVE",
|
|
383
|
+
`Expected local file header signature 0x04034b50 at offset ${localHeaderOffset}, found 0x${sig.toString(16)}`
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
const compressionMethod = readU16LE(data, localHeaderOffset + 8);
|
|
387
|
+
const crc32 = readU32LE(data, localHeaderOffset + 14);
|
|
388
|
+
const compressedSize = readU32LE(data, localHeaderOffset + 18);
|
|
389
|
+
const uncompressedSize = readU32LE(data, localHeaderOffset + 22);
|
|
390
|
+
const fnLen = readU16LE(data, localHeaderOffset + 26);
|
|
391
|
+
const exLen = readU16LE(data, localHeaderOffset + 28);
|
|
392
|
+
const dataOffset = localHeaderOffset + 30 + fnLen + exLen;
|
|
393
|
+
if (dataOffset > data.length) {
|
|
394
|
+
throw new ArchiveKitError(
|
|
395
|
+
"INVALID_ZIP_ARCHIVE",
|
|
396
|
+
`Data offset ${dataOffset} extends beyond archive boundary (${data.length})`
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
return {
|
|
400
|
+
dataOffset,
|
|
401
|
+
compressedSize,
|
|
402
|
+
uncompressedSize,
|
|
403
|
+
compressionMethod,
|
|
404
|
+
crc32
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// src/zip-extract/list-entries.ts
|
|
409
|
+
function isZipArchiveSync(data) {
|
|
410
|
+
try {
|
|
411
|
+
const bytes = toUint8ArraySync(data);
|
|
412
|
+
if (bytes.length < 22) return false;
|
|
413
|
+
const firstSig = readU32LE(bytes, 0);
|
|
414
|
+
if (firstSig !== SIG_LOC && firstSig !== SIG_EOCD) {
|
|
415
|
+
return false;
|
|
416
|
+
}
|
|
417
|
+
return findEocd(bytes) !== -1;
|
|
418
|
+
} catch {
|
|
419
|
+
return false;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
async function isZipArchive(data) {
|
|
423
|
+
try {
|
|
424
|
+
const bytes = await toUint8Array(data);
|
|
425
|
+
return isZipArchiveSync(bytes);
|
|
426
|
+
} catch {
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
async function listZipEntries(data, options) {
|
|
431
|
+
const bytes = await toUint8Array(data);
|
|
432
|
+
const parsed = parseCentralDirectory(bytes, options);
|
|
433
|
+
return parsed.entries.map((entry) => ({
|
|
434
|
+
path: entry.path,
|
|
435
|
+
name: entry.name,
|
|
436
|
+
isDirectory: entry.isDirectory,
|
|
437
|
+
compressedSize: entry.compressedSize,
|
|
438
|
+
uncompressedSize: entry.uncompressedSize,
|
|
439
|
+
compressionMethod: entry.compressionMethod,
|
|
440
|
+
lastModified: entry.lastModified,
|
|
441
|
+
crc32: entry.crc32,
|
|
442
|
+
comment: entry.comment,
|
|
443
|
+
encrypted: entry.encrypted
|
|
444
|
+
}));
|
|
445
|
+
}
|
|
446
|
+
async function getZipMetadata(data) {
|
|
447
|
+
const bytes = await toUint8Array(data);
|
|
448
|
+
const parsed = parseCentralDirectory(bytes);
|
|
449
|
+
const hasEncryptedEntries = parsed.entries.some((e) => e.encrypted);
|
|
450
|
+
const ratio = calculateRatio(parsed.totalCompressedSize, parsed.totalUncompressedSize);
|
|
451
|
+
return {
|
|
452
|
+
isValid: true,
|
|
453
|
+
entryCount: parsed.entries.length,
|
|
454
|
+
totalCompressedSize: parsed.totalCompressedSize,
|
|
455
|
+
totalUncompressedSize: parsed.totalUncompressedSize,
|
|
456
|
+
compressionRatio: ratio,
|
|
457
|
+
hasEncryptedEntries,
|
|
458
|
+
comment: parsed.comment,
|
|
459
|
+
entries: parsed.entries.map((e) => ({
|
|
460
|
+
path: e.path,
|
|
461
|
+
name: e.name,
|
|
462
|
+
isDirectory: e.isDirectory,
|
|
463
|
+
compressedSize: e.compressedSize,
|
|
464
|
+
uncompressedSize: e.uncompressedSize,
|
|
465
|
+
compressionMethod: e.compressionMethod,
|
|
466
|
+
lastModified: e.lastModified,
|
|
467
|
+
crc32: e.crc32,
|
|
468
|
+
comment: e.comment,
|
|
469
|
+
encrypted: e.encrypted
|
|
470
|
+
}))
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
function createExtractedEntry(entry, data) {
|
|
474
|
+
return {
|
|
475
|
+
path: entry.path,
|
|
476
|
+
name: entry.name,
|
|
477
|
+
isDirectory: entry.isDirectory,
|
|
478
|
+
compressedSize: entry.compressedSize,
|
|
479
|
+
uncompressedSize: entry.uncompressedSize,
|
|
480
|
+
compressionMethod: entry.compressionMethod,
|
|
481
|
+
lastModified: entry.lastModified,
|
|
482
|
+
crc32: entry.crc32,
|
|
483
|
+
comment: entry.comment,
|
|
484
|
+
encrypted: entry.encrypted,
|
|
485
|
+
data,
|
|
486
|
+
text() {
|
|
487
|
+
return decodeUtf8(this.data);
|
|
488
|
+
},
|
|
489
|
+
blob(mimeType = "application/octet-stream") {
|
|
490
|
+
return createBlob(this.data, mimeType);
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
async function decompressEntryPayload(archiveBytes, entry, signal, maxEntrySize) {
|
|
495
|
+
if (signal?.aborted) {
|
|
496
|
+
throw new ArchiveKitError("OPERATION_CANCELLED", "Decompression was aborted");
|
|
497
|
+
}
|
|
498
|
+
if (entry.encrypted) {
|
|
499
|
+
throw new ArchiveKitError(
|
|
500
|
+
"UNSUPPORTED_ENCRYPTION",
|
|
501
|
+
`Cannot extract encrypted entry "${entry.path}". Password/AES encryption is not supported.`
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
if (entry.isDirectory) {
|
|
505
|
+
return new Uint8Array(0);
|
|
506
|
+
}
|
|
507
|
+
const loc = readLocalHeader(archiveBytes, entry.localHeaderOffset);
|
|
508
|
+
const compressedSize = entry.compressedSize || loc.compressedSize;
|
|
509
|
+
const uncompressedSize = entry.uncompressedSize || loc.uncompressedSize;
|
|
510
|
+
const method = entry.compressionMethod;
|
|
511
|
+
const dataEnd = loc.dataOffset + compressedSize;
|
|
512
|
+
if (dataEnd > archiveBytes.length) {
|
|
513
|
+
throw new ArchiveKitError(
|
|
514
|
+
"INVALID_ZIP_ARCHIVE",
|
|
515
|
+
`Truncated compressed entry data for "${entry.path}"`
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
const compressedData = archiveBytes.subarray(loc.dataOffset, dataEnd);
|
|
519
|
+
if (method === ZIP_COMPRESSION_METHOD.STORE) {
|
|
520
|
+
validateDecompressedSize(compressedData.byteLength, maxEntrySize, entry.path);
|
|
521
|
+
if (entry.crc32 !== void 0 && entry.crc32 !== 0) {
|
|
522
|
+
const actualCrc = computeCrc32(compressedData);
|
|
523
|
+
if (actualCrc !== entry.crc32) {
|
|
524
|
+
throw new ArchiveKitError(
|
|
525
|
+
"DECOMPRESSION_FAILED",
|
|
526
|
+
`CRC-32 checksum mismatch for STORE entry "${entry.path}": expected 0x${entry.crc32.toString(16)}, got 0x${actualCrc.toString(16)}`
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
return new Uint8Array(compressedData);
|
|
531
|
+
}
|
|
532
|
+
if (method === ZIP_COMPRESSION_METHOD.DEFLATE) {
|
|
533
|
+
return new Promise((resolve, reject) => {
|
|
534
|
+
let completed = false;
|
|
535
|
+
if (signal) {
|
|
536
|
+
signal.addEventListener("abort", () => {
|
|
537
|
+
if (!completed) {
|
|
538
|
+
completed = true;
|
|
539
|
+
reject(new ArchiveKitError("OPERATION_CANCELLED", "Decompression was aborted"));
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
inflate(compressedData, { size: uncompressedSize }, (err, result) => {
|
|
544
|
+
if (completed) return;
|
|
545
|
+
completed = true;
|
|
546
|
+
if (err) {
|
|
547
|
+
reject(
|
|
548
|
+
new ArchiveKitError(
|
|
549
|
+
"DECOMPRESSION_FAILED",
|
|
550
|
+
`Failed to decompress DEFLATE entry "${entry.path}": ${err.message}`,
|
|
551
|
+
err
|
|
552
|
+
)
|
|
553
|
+
);
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
try {
|
|
557
|
+
validateDecompressedSize(result.byteLength, maxEntrySize, entry.path);
|
|
558
|
+
} catch (e) {
|
|
559
|
+
reject(e);
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
if (entry.crc32 !== void 0 && entry.crc32 !== 0) {
|
|
563
|
+
const actualCrc = computeCrc32(result);
|
|
564
|
+
if (actualCrc !== entry.crc32) {
|
|
565
|
+
reject(
|
|
566
|
+
new ArchiveKitError(
|
|
567
|
+
"DECOMPRESSION_FAILED",
|
|
568
|
+
`CRC-32 checksum mismatch for DEFLATE entry "${entry.path}": expected 0x${entry.crc32.toString(16)}, got 0x${actualCrc.toString(16)}`
|
|
569
|
+
)
|
|
570
|
+
);
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
resolve(result);
|
|
575
|
+
});
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
throw new ArchiveKitError(
|
|
579
|
+
"UNSUPPORTED_COMPRESSION",
|
|
580
|
+
`Unsupported compression method (${method}) for entry "${entry.path}". Supported methods: STORE (0), DEFLATE (8).`
|
|
581
|
+
);
|
|
582
|
+
}
|
|
583
|
+
async function extractZip(data, options) {
|
|
584
|
+
if (options?.signal?.aborted) {
|
|
585
|
+
throw new ArchiveKitError("OPERATION_CANCELLED", "Extraction was aborted");
|
|
586
|
+
}
|
|
587
|
+
const archiveBytes = await toUint8Array(data);
|
|
588
|
+
const parsed = parseCentralDirectory(archiveBytes, options);
|
|
589
|
+
const filter = options?.filter;
|
|
590
|
+
const entriesToExtract = filter ? parsed.entries.filter((e) => filter(e)) : parsed.entries;
|
|
591
|
+
const extracted = [];
|
|
592
|
+
let totalExtractedBytes = 0;
|
|
593
|
+
let fileCount = 0;
|
|
594
|
+
let dirCount = 0;
|
|
595
|
+
for (const entry of entriesToExtract) {
|
|
596
|
+
if (options?.signal?.aborted) {
|
|
597
|
+
throw new ArchiveKitError("OPERATION_CANCELLED", "Extraction was aborted");
|
|
598
|
+
}
|
|
599
|
+
const payload = await decompressEntryPayload(
|
|
600
|
+
archiveBytes,
|
|
601
|
+
entry,
|
|
602
|
+
options?.signal,
|
|
603
|
+
options?.maxEntrySize
|
|
604
|
+
);
|
|
605
|
+
totalExtractedBytes += payload.byteLength;
|
|
606
|
+
validateTotalDecompressedSize(totalExtractedBytes, options?.maxTotalSize);
|
|
607
|
+
if (entry.isDirectory) {
|
|
608
|
+
dirCount++;
|
|
609
|
+
} else {
|
|
610
|
+
fileCount++;
|
|
611
|
+
}
|
|
612
|
+
extracted.push(createExtractedEntry(entry, payload));
|
|
613
|
+
}
|
|
614
|
+
return {
|
|
615
|
+
entries: extracted,
|
|
616
|
+
totalFiles: fileCount,
|
|
617
|
+
totalDirectories: dirCount,
|
|
618
|
+
totalCompressedSize: parsed.totalCompressedSize,
|
|
619
|
+
totalUncompressedSize: totalExtractedBytes,
|
|
620
|
+
comment: parsed.comment
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
async function extractZipEntry(data, path, options) {
|
|
624
|
+
if (options?.signal?.aborted) {
|
|
625
|
+
throw new ArchiveKitError("OPERATION_CANCELLED", "Extraction was aborted");
|
|
626
|
+
}
|
|
627
|
+
const normalizedTarget = normalizeZipPath(path);
|
|
628
|
+
const archiveBytes = await toUint8Array(data);
|
|
629
|
+
const parsed = parseCentralDirectory(archiveBytes, options);
|
|
630
|
+
const entry = parsed.entries.find((e) => e.path === normalizedTarget);
|
|
631
|
+
if (!entry) {
|
|
632
|
+
throw new ArchiveKitError(
|
|
633
|
+
"INVALID_INPUT",
|
|
634
|
+
`Entry "${normalizedTarget}" was not found in the ZIP archive`
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
const payload = await decompressEntryPayload(
|
|
638
|
+
archiveBytes,
|
|
639
|
+
entry,
|
|
640
|
+
options?.signal,
|
|
641
|
+
options?.maxEntrySize
|
|
642
|
+
);
|
|
643
|
+
return createExtractedEntry(entry, payload);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// src/zip-extract/validate-archive.ts
|
|
647
|
+
async function validateZipArchive(data, options) {
|
|
648
|
+
const errors = [];
|
|
649
|
+
const warnings = [];
|
|
650
|
+
let entryCount = 0;
|
|
651
|
+
let totalComp = 0;
|
|
652
|
+
let totalUncomp = 0;
|
|
653
|
+
try {
|
|
654
|
+
const bytes = await toUint8Array(data);
|
|
655
|
+
const parsed = parseCentralDirectory(bytes, options);
|
|
656
|
+
entryCount = parsed.entries.length;
|
|
657
|
+
totalComp = parsed.totalCompressedSize;
|
|
658
|
+
totalUncomp = parsed.totalUncompressedSize;
|
|
659
|
+
for (const entry of parsed.entries) {
|
|
660
|
+
if (entry.encrypted) {
|
|
661
|
+
errors.push(`Entry "${entry.path}" is encrypted (unsupported format).`);
|
|
662
|
+
}
|
|
663
|
+
if (entry.compressionMethod !== ZIP_COMPRESSION_METHOD.STORE && entry.compressionMethod !== ZIP_COMPRESSION_METHOD.DEFLATE) {
|
|
664
|
+
errors.push(
|
|
665
|
+
`Entry "${entry.path}" uses unsupported compression method ${entry.compressionMethod}.`
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
} catch (err) {
|
|
670
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
671
|
+
errors.push(msg);
|
|
672
|
+
}
|
|
673
|
+
return {
|
|
674
|
+
valid: errors.length === 0,
|
|
675
|
+
errors,
|
|
676
|
+
warnings,
|
|
677
|
+
entryCount,
|
|
678
|
+
totalCompressedSize: totalComp,
|
|
679
|
+
totalUncompressedSize: totalUncomp
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
export { SIG_CEN, SIG_EOCD, SIG_LOC, extractZip, extractZipEntry, findEocd, getZipMetadata, isZipArchive, isZipArchiveSync, listZipEntries, parseCentralDirectory, readLocalHeader, readU16LE, readU32LE, validateZipArchive };
|
|
684
|
+
//# sourceMappingURL=index.js.map
|
|
685
|
+
//# sourceMappingURL=index.js.map
|