@ac-kit/format-zip 0.1.2 → 0.3.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 +88 -0
- package/README.md +85 -9
- package/dist/_producer-stream.d.ts +14 -0
- package/dist/_producer-stream.d.ts.map +1 -0
- package/dist/_producer-stream.js +80 -0
- package/dist/_producer-stream.js.map +1 -0
- package/dist/_zip-directory.d.ts +97 -0
- package/dist/_zip-directory.d.ts.map +1 -0
- package/dist/_zip-directory.js +541 -0
- package/dist/_zip-directory.js.map +1 -0
- package/dist/_zip-entries.d.ts +31 -0
- package/dist/_zip-entries.d.ts.map +1 -0
- package/dist/_zip-entries.js +192 -0
- package/dist/_zip-entries.js.map +1 -0
- package/dist/_zip-format.d.ts +48 -0
- package/dist/_zip-format.d.ts.map +1 -0
- package/dist/_zip-format.js +49 -0
- package/dist/_zip-format.js.map +1 -0
- package/dist/_zip-headers.d.ts +49 -0
- package/dist/_zip-headers.d.ts.map +1 -0
- package/dist/_zip-headers.js +158 -0
- package/dist/_zip-headers.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/zip-archive.d.ts +45 -2
- package/dist/zip-archive.d.ts.map +1 -1
- package/dist/zip-archive.js +14 -202
- package/dist/zip-archive.js.map +1 -1
- package/dist/zip-error.d.ts +5 -1
- package/dist/zip-error.d.ts.map +1 -1
- package/dist/zip-error.js +5 -1
- package/dist/zip-error.js.map +1 -1
- package/dist/zip-method.d.ts +11 -0
- package/dist/zip-method.d.ts.map +1 -0
- package/dist/zip-method.js +12 -0
- package/dist/zip-method.js.map +1 -0
- package/dist/zip-source.d.ts +59 -0
- package/dist/zip-source.d.ts.map +1 -0
- package/dist/zip-source.js +149 -0
- package/dist/zip-source.js.map +1 -0
- package/dist/zip-stream-writer.d.ts +61 -0
- package/dist/zip-stream-writer.d.ts.map +1 -0
- package/dist/zip-stream-writer.js +419 -0
- package/dist/zip-stream-writer.js.map +1 -0
- package/dist/zip-writer.d.ts +89 -0
- package/dist/zip-writer.d.ts.map +1 -0
- package/dist/zip-writer.js +35 -0
- package/dist/zip-writer.js.map +1 -0
- package/package.json +6 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zip-archive.d.ts","sourceRoot":"","sources":["../src/zip-archive.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"zip-archive.d.ts","sourceRoot":"","sources":["../src/zip-archive.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG;IACvB,mDAAmD;IACnD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,4EAA4E;IAC5E,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B;;;OAGG;IACH,QAAQ,CAAC,mBAAmB,EAAE,MAAM,CAAC;CACrC,CAAC;AAEF,0CAA0C;AAC1C,MAAM,MAAM,QAAQ,GAAG;IACtB;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,6EAA6E;IAC7E,QAAQ,CAAC,YAAY,EAAE,IAAI,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,6EAA6E;IAC7E,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B,iDAAiD;IACjD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,yDAAyD;AACzD,MAAM,MAAM,UAAU,GAAG;IACxB,QAAQ,CAAC,OAAO,EAAE,SAAS,QAAQ,EAAE,CAAC;IACtC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,uCAAuC;IACvC,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,GAAG,SAAS,CAAC;IAC1C;;;;;;;;;;;;OAYG;IACH,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC;IAC9E;;;;;;;;;;;;;;;;;OAiBG;IACH,MAAM,CACL,KAAK,EAAE,QAAQ,EACf,MAAM,CAAC,EAAE,WAAW,GAClB,cAAc,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC;IAC3C;;;;;;;;;;;;;;OAcG;IACH,WAAW,CACV,KAAK,EAAE,QAAQ,EACf,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,WAAW,GAClB,cAAc,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC;CAC3C,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,OAAO,CACtB,KAAK,EAAE,UAAU,CAAC,WAAW,CAAC,EAC9B,MAAM,EAAE,SAAS,GACf,UAAU,CAqBZ"}
|
package/dist/zip-archive.js
CHANGED
|
@@ -1,22 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { ZipFormatError, ZipLimitError } from "./zip-error.js";
|
|
4
|
-
const END_OF_CENTRAL_DIRECTORY_ = 0x06054b50;
|
|
5
|
-
const ZIP64_END_LOCATOR_ = 0x07064b50;
|
|
6
|
-
const ZIP64_END_OF_CENTRAL_DIRECTORY_ = 0x06064b50;
|
|
7
|
-
const CENTRAL_HEADER_ = 0x02014b50;
|
|
8
|
-
const LOCAL_HEADER_ = 0x04034b50;
|
|
9
|
-
const ZIP64_EXTRA_ = 0x0001;
|
|
10
|
-
const UNICODE_PATH_EXTRA_ = 0x7075;
|
|
11
|
-
const ENCRYPTED_FLAG_ = 0x0001;
|
|
12
|
-
const UTF8_FLAG_ = 0x0800;
|
|
13
|
-
const MAX_COMMENT_ = 0xffff;
|
|
14
|
-
const STORED_ = 0;
|
|
15
|
-
const DEFLATE_ = 8;
|
|
1
|
+
import { MAX_TAIL_, readArchiveDirectory_ } from "./_zip-directory.js";
|
|
2
|
+
import { bytesRanges_, createArchive_ } from "./_zip-entries.js";
|
|
16
3
|
/**
|
|
17
4
|
* Reads a ZIP archive's central directory (APPNOTE 6.3), ZIP64 included, and
|
|
18
5
|
* returns its entries; their content is read on demand by
|
|
19
|
-
* {@link ZipArchive.read}, inflated with
|
|
6
|
+
* {@link ZipArchive.read} or {@link ZipArchive.stream}, inflated with
|
|
7
|
+
* `DecompressionStream`. {@link openZipSource} reads an archive by ranges
|
|
8
|
+
* instead, from a `Blob` or a ranged source.
|
|
20
9
|
*
|
|
21
10
|
* Names are decoded as UTF-8 when flagged so, from an Info-ZIP Unicode Path
|
|
22
11
|
* field when present, and as code page 437 otherwise. Archives split across
|
|
@@ -28,191 +17,14 @@ const DEFLATE_ = 8;
|
|
|
28
17
|
* @throws {UnsupportedError} When it spans several disks.
|
|
29
18
|
*/
|
|
30
19
|
export function openZip(bytes, limits) {
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const within = (offset, length, what) => {
|
|
41
|
-
if (offset < 0 || offset + length > bytes.length) {
|
|
42
|
-
throw new ZipFormatError(`Truncated ${what}`, offset);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
let end = -1;
|
|
46
|
-
for (let offset = bytes.length - 22; offset >= Math.max(0, bytes.length - 22 - MAX_COMMENT_); offset--) {
|
|
47
|
-
if (u32(offset) === END_OF_CENTRAL_DIRECTORY_ &&
|
|
48
|
-
offset + 22 + u16(offset + 20) === bytes.length) {
|
|
49
|
-
end = offset;
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
if (end === -1) {
|
|
54
|
-
throw new ZipFormatError("No end of central directory", bytes.length);
|
|
55
|
-
}
|
|
56
|
-
const comment = decodeText(bytes.subarray(end + 22, end + 22 + u16(end + 20)), "ibm437");
|
|
57
|
-
let count = u16(end + 10);
|
|
58
|
-
let directorySize = u32(end + 12);
|
|
59
|
-
let directoryOffset = u32(end + 16);
|
|
60
|
-
if (u16(end + 4) !== 0 || u16(end + 6) !== 0 || u16(end + 8) !== count) {
|
|
61
|
-
throw new UnsupportedError("ZIP archive split across disks");
|
|
62
|
-
}
|
|
63
|
-
if (count === 0xffff ||
|
|
64
|
-
directorySize === 0xffffffff ||
|
|
65
|
-
directoryOffset === 0xffffffff) {
|
|
66
|
-
const locator = end - 20;
|
|
67
|
-
within(locator, 20, "ZIP64 locator");
|
|
68
|
-
if (u32(locator) !== ZIP64_END_LOCATOR_) {
|
|
69
|
-
throw new ZipFormatError("Missing ZIP64 end of central directory locator", locator);
|
|
70
|
-
}
|
|
71
|
-
const record = u64(locator + 8);
|
|
72
|
-
within(record, 56, "ZIP64 end of central directory");
|
|
73
|
-
if (u32(record) !== ZIP64_END_OF_CENTRAL_DIRECTORY_) {
|
|
74
|
-
throw new ZipFormatError("Missing ZIP64 end of central directory", record);
|
|
75
|
-
}
|
|
76
|
-
count = u64(record + 32);
|
|
77
|
-
directorySize = u64(record + 40);
|
|
78
|
-
directoryOffset = u64(record + 48);
|
|
79
|
-
}
|
|
80
|
-
if (count > limits.maxEntries) {
|
|
81
|
-
throw new ZipLimitError(`The archive lists ${count} entries, more than ${limits.maxEntries}`);
|
|
82
|
-
}
|
|
83
|
-
within(directoryOffset, directorySize, "central directory");
|
|
84
|
-
const entries = [];
|
|
85
|
-
let offset = directoryOffset;
|
|
86
|
-
for (let index = 0; index < count; index++) {
|
|
87
|
-
within(offset, 46, "central directory header");
|
|
88
|
-
if (u32(offset) !== CENTRAL_HEADER_) {
|
|
89
|
-
throw new ZipFormatError("Expected a central directory header", offset);
|
|
90
|
-
}
|
|
91
|
-
const flags = u16(offset + 8);
|
|
92
|
-
const nameLength = u16(offset + 28);
|
|
93
|
-
const extraLength = u16(offset + 30);
|
|
94
|
-
const commentLength = u16(offset + 32);
|
|
95
|
-
within(offset + 46, nameLength + extraLength + commentLength, "central directory header");
|
|
96
|
-
const nameBytes = bytes.subarray(offset + 46, offset + 46 + nameLength);
|
|
97
|
-
const extra = bytes.subarray(offset + 46 + nameLength, offset + 46 + nameLength + extraLength);
|
|
98
|
-
let compressedSize = u32(offset + 20);
|
|
99
|
-
let size = u32(offset + 24);
|
|
100
|
-
let localOffset = u32(offset + 42);
|
|
101
|
-
let unicodeName = null;
|
|
102
|
-
for (let field = 0; field + 4 <= extra.length;) {
|
|
103
|
-
const id = getUint32Le(extra, field, 2);
|
|
104
|
-
const length = getUint32Le(extra, field + 2, 2);
|
|
105
|
-
const data = extra.subarray(field + 4, field + 4 + length);
|
|
106
|
-
if (id === ZIP64_EXTRA_) {
|
|
107
|
-
// Only the fields whose header value is saturated are present, in order.
|
|
108
|
-
let cursor = 0;
|
|
109
|
-
const next = () => {
|
|
110
|
-
if (cursor + 8 > data.length) {
|
|
111
|
-
throw new ZipFormatError("Truncated ZIP64 field", offset);
|
|
112
|
-
}
|
|
113
|
-
const value = getBigUint64Le(data, cursor);
|
|
114
|
-
cursor += 8;
|
|
115
|
-
if (!bigIntIsSafeNumber(value)) {
|
|
116
|
-
throw new ZipFormatError("Size past what this reader can address", offset);
|
|
117
|
-
}
|
|
118
|
-
return Number(value);
|
|
119
|
-
};
|
|
120
|
-
size = size === 0xffffffff ? next() : size;
|
|
121
|
-
compressedSize =
|
|
122
|
-
compressedSize === 0xffffffff ? next() : compressedSize;
|
|
123
|
-
localOffset = localOffset === 0xffffffff ? next() : localOffset;
|
|
124
|
-
}
|
|
125
|
-
else if (id === UNICODE_PATH_EXTRA_ &&
|
|
126
|
-
data[0] === 1 &&
|
|
127
|
-
data.length >= 5 &&
|
|
128
|
-
getUint32Le(data, 1) === crc_32(nameBytes)) {
|
|
129
|
-
unicodeName = decodeUtf8_(data.subarray(5), offset);
|
|
130
|
-
}
|
|
131
|
-
field += 4 + length;
|
|
132
|
-
}
|
|
133
|
-
const name = (flags & UTF8_FLAG_) !== 0
|
|
134
|
-
? decodeUtf8_(nameBytes, offset)
|
|
135
|
-
: (unicodeName ?? decodeText(nameBytes, "ibm437"));
|
|
136
|
-
const time = u16(offset + 12);
|
|
137
|
-
const date = u16(offset + 14);
|
|
138
|
-
entries.push({
|
|
139
|
-
name,
|
|
140
|
-
isDirectory: name.endsWith("/"),
|
|
141
|
-
method: u16(offset + 10),
|
|
142
|
-
compressedSize,
|
|
143
|
-
size,
|
|
144
|
-
crc32: u32(offset + 16),
|
|
145
|
-
lastModified: new Date(1980 + (date >> 9), ((date >> 5) & 0x0f) - 1, date & 0x1f, time >> 11, (time >> 5) & 0x3f, (time & 0x1f) * 2),
|
|
146
|
-
comment: decodeText(bytes.subarray(offset + 46 + nameLength + extraLength, offset + 46 + nameLength + extraLength + commentLength), (flags & UTF8_FLAG_) !== 0 ? "utf-8" : "ibm437"),
|
|
147
|
-
isEncrypted: (flags & ENCRYPTED_FLAG_) !== 0,
|
|
148
|
-
offset: localOffset,
|
|
149
|
-
});
|
|
150
|
-
offset += 46 + nameLength + extraLength + commentLength;
|
|
151
|
-
}
|
|
152
|
-
const byName = new Map(entries.map((entry) => [entry.name, entry]));
|
|
153
|
-
return {
|
|
154
|
-
entries,
|
|
155
|
-
comment,
|
|
156
|
-
entry: (name) => byName.get(name),
|
|
157
|
-
read: async (entry, signal) => {
|
|
158
|
-
if (entry.isEncrypted) {
|
|
159
|
-
throw new UnsupportedError(`ZIP entry "${entry.name}" is encrypted`);
|
|
160
|
-
}
|
|
161
|
-
if (entry.method !== STORED_ && entry.method !== DEFLATE_) {
|
|
162
|
-
throw new UnsupportedError(`ZIP compression method ${entry.method} of "${entry.name}"`);
|
|
163
|
-
}
|
|
164
|
-
if (entry.size > limits.maxEntrySize) {
|
|
165
|
-
throw new ZipLimitError(`ZIP entry "${entry.name}" holds ${entry.size} bytes, more than ${limits.maxEntrySize}`);
|
|
166
|
-
}
|
|
167
|
-
if (entry.size >
|
|
168
|
-
Math.max(entry.compressedSize, 1) * limits.maxCompressionRatio) {
|
|
169
|
-
throw new ZipLimitError(`ZIP entry "${entry.name}" is compressed past ${limits.maxCompressionRatio}:1`);
|
|
170
|
-
}
|
|
171
|
-
within(entry.offset, 30, "local header");
|
|
172
|
-
if (u32(entry.offset) !== LOCAL_HEADER_) {
|
|
173
|
-
throw new ZipFormatError("Expected a local header", entry.offset);
|
|
174
|
-
}
|
|
175
|
-
const start = entry.offset + 30 + u16(entry.offset + 26) + u16(entry.offset + 28);
|
|
176
|
-
within(start, entry.compressedSize, "entry data");
|
|
177
|
-
const data = bytes.subarray(start, start + entry.compressedSize);
|
|
178
|
-
let content;
|
|
179
|
-
if (entry.method === STORED_) {
|
|
180
|
-
if (entry.compressedSize !== entry.size) {
|
|
181
|
-
throw new ZipFormatError(`Stored entry "${entry.name}" with two sizes`, entry.offset);
|
|
182
|
-
}
|
|
183
|
-
content = data.slice();
|
|
184
|
-
}
|
|
185
|
-
else {
|
|
186
|
-
const compressed = new ReadableStream({
|
|
187
|
-
start(controller) {
|
|
188
|
-
controller.enqueue(data);
|
|
189
|
-
controller.close();
|
|
190
|
-
},
|
|
191
|
-
});
|
|
192
|
-
try {
|
|
193
|
-
content = await readStreamBytes(compressed.pipeThrough(new DecompressionStream("deflate-raw")), entry.size, signal);
|
|
194
|
-
}
|
|
195
|
-
catch (error) {
|
|
196
|
-
if (error instanceof StreamLengthError ||
|
|
197
|
-
error instanceof TypeError) {
|
|
198
|
-
throw new ZipFormatError(`Corrupt deflate data in "${entry.name}"`, start);
|
|
199
|
-
}
|
|
200
|
-
throw error;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
if (content.length !== entry.size || crc_32(content) !== entry.crc32) {
|
|
204
|
-
throw new ZipFormatError(`Entry "${entry.name}" does not match its size and CRC-32`, start);
|
|
205
|
-
}
|
|
206
|
-
return content;
|
|
207
|
-
},
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
function decodeUtf8_(bytes, offset) {
|
|
211
|
-
try {
|
|
212
|
-
return decodeText(bytes, "utf-8", { fatal: true });
|
|
213
|
-
}
|
|
214
|
-
catch {
|
|
215
|
-
throw new ZipFormatError("A UTF-8 name that is not UTF-8", offset);
|
|
216
|
-
}
|
|
20
|
+
const reading = readArchiveDirectory_(bytes.subarray(Math.max(0, bytes.length - MAX_TAIL_)), bytes.length, limits);
|
|
21
|
+
// Every read the directory reader asks for lies within the archive.
|
|
22
|
+
let step = reading.next();
|
|
23
|
+
while (step.done !== true) {
|
|
24
|
+
const { offset, length } = step.value;
|
|
25
|
+
step = reading.next(bytes.subarray(offset, offset + length));
|
|
26
|
+
}
|
|
27
|
+
const { comment, entries, directoryOffset } = step.value;
|
|
28
|
+
return createArchive_(entries, comment, bytesRanges_(bytes), limits, directoryOffset);
|
|
217
29
|
}
|
|
218
30
|
//# sourceMappingURL=zip-archive.js.map
|
package/dist/zip-archive.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zip-archive.js","sourceRoot":"","sources":["../src/zip-archive.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"zip-archive.js","sourceRoot":"","sources":["../src/zip-archive.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AACvE,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AA4GjE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,OAAO,CACtB,KAA8B,EAC9B,MAAiB;IAEjB,MAAM,OAAO,GAAG,qBAAqB,CACpC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC,EACrD,KAAK,CAAC,MAAM,EACZ,MAAM,CACN,CAAC;IACF,oEAAoE;IACpE,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;IAC1B,OAAO,IAAI,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;QAC3B,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QACtC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC;IAC9D,CAAC;IACD,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;IAEzD,OAAO,cAAc,CACpB,OAAO,EACP,OAAO,EACP,YAAY,CAAC,KAAK,CAAC,EACnB,MAAM,EACN,eAAe,CACf,CAAC;AACH,CAAC"}
|
package/dist/zip-error.d.ts
CHANGED
|
@@ -6,7 +6,11 @@ export declare class ZipFormatError extends SyntaxError {
|
|
|
6
6
|
/** Byte offset in the archive where reading gave up. */
|
|
7
7
|
position: number);
|
|
8
8
|
}
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* An archive or entry past one of the {@link ZipLimits} given, or past what the
|
|
11
|
+
* format holds: a name or comment over 65 535 bytes, or a stream of unknown
|
|
12
|
+
* size past 4 GiB written without `zip64`.
|
|
13
|
+
*/
|
|
10
14
|
export declare class ZipLimitError extends RangeError {
|
|
11
15
|
constructor(message: string);
|
|
12
16
|
}
|
package/dist/zip-error.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zip-error.d.ts","sourceRoot":"","sources":["../src/zip-error.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,qBAAa,cAAe,SAAQ,WAAW;IAG7C,wDAAwD;IACxD,QAAQ,CAAC,QAAQ,EAAE,MAAM;IAH1B,YACC,OAAO,EAAE,MAAM;IACf,wDAAwD;IAC/C,QAAQ,EAAE,MAAM,EAIzB;CACD;AAED
|
|
1
|
+
{"version":3,"file":"zip-error.d.ts","sourceRoot":"","sources":["../src/zip-error.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,qBAAa,cAAe,SAAQ,WAAW;IAG7C,wDAAwD;IACxD,QAAQ,CAAC,QAAQ,EAAE,MAAM;IAH1B,YACC,OAAO,EAAE,MAAM;IACf,wDAAwD;IAC/C,QAAQ,EAAE,MAAM,EAIzB;CACD;AAED;;;;GAIG;AACH,qBAAa,aAAc,SAAQ,UAAU;IAC5C,YAAY,OAAO,EAAE,MAAM,EAG1B;CACD"}
|
package/dist/zip-error.js
CHANGED
|
@@ -9,7 +9,11 @@ export class ZipFormatError extends SyntaxError {
|
|
|
9
9
|
this.name = "ZipFormatError";
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* An archive or entry past one of the {@link ZipLimits} given, or past what the
|
|
14
|
+
* format holds: a name or comment over 65 535 bytes, or a stream of unknown
|
|
15
|
+
* size past 4 GiB written without `zip64`.
|
|
16
|
+
*/
|
|
13
17
|
export class ZipLimitError extends RangeError {
|
|
14
18
|
constructor(message) {
|
|
15
19
|
super(message);
|
package/dist/zip-error.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zip-error.js","sourceRoot":"","sources":["../src/zip-error.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,MAAM,OAAO,cAAe,SAAQ,WAAW;IAIpC,QAAQ;IAHlB,YACC,OAAe;IACf,wDAAwD;IAC/C,QAAgB;QAEzB,KAAK,CAAC,GAAG,OAAO,OAAO,QAAQ,EAAE,CAAC,CAAC;wBAF1B,QAAQ;QAGjB,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC9B,CAAC;CACD;AAED
|
|
1
|
+
{"version":3,"file":"zip-error.js","sourceRoot":"","sources":["../src/zip-error.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,MAAM,OAAO,cAAe,SAAQ,WAAW;IAIpC,QAAQ;IAHlB,YACC,OAAe;IACf,wDAAwD;IAC/C,QAAgB;QAEzB,KAAK,CAAC,GAAG,OAAO,OAAO,QAAQ,EAAE,CAAC,CAAC;wBAF1B,QAAQ;QAGjB,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC9B,CAAC;CACD;AAED;;;;GAIG;AACH,MAAM,OAAO,aAAc,SAAQ,UAAU;IAC5C,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC7B,CAAC;CACD"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The compression methods of APPNOTE 6.3 §4.4.5 this package reads and writes,
|
|
3
|
+
* by their number in an entry's header.
|
|
4
|
+
*/
|
|
5
|
+
export declare enum ZipMethod {
|
|
6
|
+
/** The content as it is, uncompressed. */
|
|
7
|
+
STORED = 0,
|
|
8
|
+
/** Raw deflate (RFC 1951). */
|
|
9
|
+
DEFLATE = 8
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=zip-method.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zip-method.d.ts","sourceRoot":"","sources":["../src/zip-method.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,oBAAY,SAAS;IACpB,0CAA0C;IAC1C,MAAM,IAAI;IACV,8BAA8B;IAC9B,OAAO,IAAI;CACX"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The compression methods of APPNOTE 6.3 §4.4.5 this package reads and writes,
|
|
3
|
+
* by their number in an entry's header.
|
|
4
|
+
*/
|
|
5
|
+
export var ZipMethod;
|
|
6
|
+
(function (ZipMethod) {
|
|
7
|
+
/** The content as it is, uncompressed. */
|
|
8
|
+
ZipMethod[ZipMethod["STORED"] = 0] = "STORED";
|
|
9
|
+
/** Raw deflate (RFC 1951). */
|
|
10
|
+
ZipMethod[ZipMethod["DEFLATE"] = 8] = "DEFLATE";
|
|
11
|
+
})(ZipMethod || (ZipMethod = {}));
|
|
12
|
+
//# sourceMappingURL=zip-method.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zip-method.js","sourceRoot":"","sources":["../src/zip-method.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAN,IAAY,SAKX;AALD,WAAY,SAAS;IACpB,0CAA0C;IAC1C,6CAAU,CAAA;IACV,8BAA8B;IAC9B,+CAAW,CAAA;AACZ,CAAC,EALW,SAAS,KAAT,SAAS,QAKpB"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { ZipArchive, ZipLimits } from "./zip-archive.js";
|
|
2
|
+
/**
|
|
3
|
+
* An archive {@link openZipSource} reads by ranges: a file, an HTTP resource
|
|
4
|
+
* fetched with `Range`, storage with ranged reads.
|
|
5
|
+
*/
|
|
6
|
+
export type ZipSource = {
|
|
7
|
+
/** The archive's length in bytes, a safe non-negative integer. */
|
|
8
|
+
readonly size: number;
|
|
9
|
+
/**
|
|
10
|
+
* Exactly `length` bytes at `offset`; fewer is a truncated archive. Used for
|
|
11
|
+
* the end records, the central directory and local headers, and for entry
|
|
12
|
+
* data, in chunks of 1 MiB, when `stream` is absent.
|
|
13
|
+
*/
|
|
14
|
+
read(offset: number, length: number, signal?: AbortSignal): Promise<Uint8Array>;
|
|
15
|
+
/**
|
|
16
|
+
* Optional: exactly `length` bytes at `offset` as a stream, which entry data
|
|
17
|
+
* is read through when present, in one request however large the entry.
|
|
18
|
+
*/
|
|
19
|
+
stream?(offset: number, length: number, signal?: AbortSignal): ReadableStream<Uint8Array>;
|
|
20
|
+
};
|
|
21
|
+
/** The limits {@link openZipSource} holds an archive to. */
|
|
22
|
+
export type ZipSourceLimits = ZipLimits & {
|
|
23
|
+
/**
|
|
24
|
+
* Largest central directory read, in bytes: the archive declares its size,
|
|
25
|
+
* and the whole directory is read at once.
|
|
26
|
+
*/
|
|
27
|
+
readonly maxDirectorySize: number;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Reads a ZIP archive by ranges, without holding it whole: one read of its end
|
|
31
|
+
* records (the last 65 KiB at most), one of its central directory, then one of
|
|
32
|
+
* each entry's local header the first time it is read, cached after. Entries
|
|
33
|
+
* are then read as {@link openZip}'s are; {@link ZipArchive.stream} and
|
|
34
|
+
* {@link ZipArchive.streamRange} read only the bytes they deliver, as they are
|
|
35
|
+
* pulled.
|
|
36
|
+
*
|
|
37
|
+
* A `Blob` (a `File`, or Node's `fs.openAsBlob`) is read with `slice`, its
|
|
38
|
+
* entries through `Blob.stream`, so memory stays one chunk. Bytes in memory are
|
|
39
|
+
* read as {@link openZip} reads them, within the same limits. Memory is linear
|
|
40
|
+
* in the central directory, bounded by `maxDirectorySize`, plus one offset per
|
|
41
|
+
* entry read.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* const archive = await openZipSource(await fs.openAsBlob(path), limits);
|
|
45
|
+
* const entry = archive.entry("archive/data.warc.gz")!;
|
|
46
|
+
* const record = archive.streamRange(entry, offset, length);
|
|
47
|
+
*
|
|
48
|
+
* @throws {ZipFormatError} When the archive has no end of central directory, or
|
|
49
|
+
* its directory is truncated or inconsistent, or the source returns fewer
|
|
50
|
+
* bytes than asked.
|
|
51
|
+
* @throws {ZipLimitError} When it lists more than `maxEntries`, or its
|
|
52
|
+
* directory is larger than `maxDirectorySize`.
|
|
53
|
+
* @throws {UnsupportedError} When it spans several disks.
|
|
54
|
+
* @throws {TypeError} When the source's size is not a safe non-negative
|
|
55
|
+
* integer.
|
|
56
|
+
* @throws When `signal` aborts, its reason; the source's own read error.
|
|
57
|
+
*/
|
|
58
|
+
export declare function openZipSource(source: ZipSource | Blob | Uint8Array<ArrayBuffer>, limits: ZipSourceLimits, signal?: AbortSignal): Promise<ZipArchive>;
|
|
59
|
+
//# sourceMappingURL=zip-source.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zip-source.d.ts","sourceRoot":"","sources":["../src/zip-source.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAG9D;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG;IACvB,kEAAkE;IAClE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;;OAIG;IACH,IAAI,CACH,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,WAAW,GAClB,OAAO,CAAC,UAAU,CAAC,CAAC;IACvB;;;OAGG;IACH,MAAM,CAAC,CACN,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,WAAW,GAClB,cAAc,CAAC,UAAU,CAAC,CAAC;CAC9B,CAAC;AAEF,4DAA4D;AAC5D,MAAM,MAAM,eAAe,GAAG,SAAS,GAAG;IACzC;;;OAGG;IACH,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;CAClC,CAAC;AAKF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,aAAa,CAClC,MAAM,EAAE,SAAS,GAAG,IAAI,GAAG,UAAU,CAAC,WAAW,CAAC,EAClD,MAAM,EAAE,eAAe,EACvB,MAAM,CAAC,EAAE,WAAW,GAClB,OAAO,CAAC,UAAU,CAAC,CAgCrB"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { readStreamChunks } from "@ac-kit/core";
|
|
2
|
+
import { producerStream_ } from "./_producer-stream.js";
|
|
3
|
+
import { MAX_TAIL_, readArchiveDirectory_ } from "./_zip-directory.js";
|
|
4
|
+
import { bytesRanges_, createArchive_, ownBuffer_, } from "./_zip-entries.js";
|
|
5
|
+
import { ZipFormatError } from "./zip-error.js";
|
|
6
|
+
/** Bytes per read of entry data from a {@link ZipSource} without `stream`. */
|
|
7
|
+
const READ_CHUNK_ = 1024 * 1024;
|
|
8
|
+
/**
|
|
9
|
+
* Reads a ZIP archive by ranges, without holding it whole: one read of its end
|
|
10
|
+
* records (the last 65 KiB at most), one of its central directory, then one of
|
|
11
|
+
* each entry's local header the first time it is read, cached after. Entries
|
|
12
|
+
* are then read as {@link openZip}'s are; {@link ZipArchive.stream} and
|
|
13
|
+
* {@link ZipArchive.streamRange} read only the bytes they deliver, as they are
|
|
14
|
+
* pulled.
|
|
15
|
+
*
|
|
16
|
+
* A `Blob` (a `File`, or Node's `fs.openAsBlob`) is read with `slice`, its
|
|
17
|
+
* entries through `Blob.stream`, so memory stays one chunk. Bytes in memory are
|
|
18
|
+
* read as {@link openZip} reads them, within the same limits. Memory is linear
|
|
19
|
+
* in the central directory, bounded by `maxDirectorySize`, plus one offset per
|
|
20
|
+
* entry read.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const archive = await openZipSource(await fs.openAsBlob(path), limits);
|
|
24
|
+
* const entry = archive.entry("archive/data.warc.gz")!;
|
|
25
|
+
* const record = archive.streamRange(entry, offset, length);
|
|
26
|
+
*
|
|
27
|
+
* @throws {ZipFormatError} When the archive has no end of central directory, or
|
|
28
|
+
* its directory is truncated or inconsistent, or the source returns fewer
|
|
29
|
+
* bytes than asked.
|
|
30
|
+
* @throws {ZipLimitError} When it lists more than `maxEntries`, or its
|
|
31
|
+
* directory is larger than `maxDirectorySize`.
|
|
32
|
+
* @throws {UnsupportedError} When it spans several disks.
|
|
33
|
+
* @throws {TypeError} When the source's size is not a safe non-negative
|
|
34
|
+
* integer.
|
|
35
|
+
* @throws When `signal` aborts, its reason; the source's own read error.
|
|
36
|
+
*/
|
|
37
|
+
export async function openZipSource(source, limits, signal) {
|
|
38
|
+
signal?.throwIfAborted();
|
|
39
|
+
let ranges;
|
|
40
|
+
if (source instanceof Uint8Array) {
|
|
41
|
+
ranges = bytesRanges_(source);
|
|
42
|
+
}
|
|
43
|
+
else if (source instanceof Blob) {
|
|
44
|
+
ranges = blobRanges_(source);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
ranges = sourceRanges_(source);
|
|
48
|
+
}
|
|
49
|
+
const { size } = ranges;
|
|
50
|
+
const tailLength = Math.min(size, MAX_TAIL_);
|
|
51
|
+
const tailOffset = size - tailLength;
|
|
52
|
+
const tail = await ranges.read(tailOffset, tailLength, signal);
|
|
53
|
+
// A source may resolve despite an abort: checked after every read.
|
|
54
|
+
signal?.throwIfAborted();
|
|
55
|
+
const reading = readArchiveDirectory_(tail, size, limits);
|
|
56
|
+
let step = reading.next();
|
|
57
|
+
while (step.done !== true) {
|
|
58
|
+
const { offset, length } = step.value;
|
|
59
|
+
let bytes;
|
|
60
|
+
if (offset >= tailOffset) {
|
|
61
|
+
bytes = tail.subarray(offset - tailOffset, offset - tailOffset + length);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
bytes = await ranges.read(offset, length, signal);
|
|
65
|
+
signal?.throwIfAborted();
|
|
66
|
+
}
|
|
67
|
+
step = reading.next(bytes);
|
|
68
|
+
}
|
|
69
|
+
const { comment, entries, directoryOffset } = step.value;
|
|
70
|
+
return createArchive_(entries, comment, ranges, limits, directoryOffset);
|
|
71
|
+
}
|
|
72
|
+
function blobRanges_(blob) {
|
|
73
|
+
return {
|
|
74
|
+
size: blob.size,
|
|
75
|
+
read: async (offset, length, signal) => {
|
|
76
|
+
signal?.throwIfAborted();
|
|
77
|
+
const bytes = await blob.slice(offset, offset + length).bytes();
|
|
78
|
+
signal?.throwIfAborted();
|
|
79
|
+
return checkLength_(bytes, offset, length);
|
|
80
|
+
},
|
|
81
|
+
stream: (offset, length) => blob.slice(offset, offset + length).stream(),
|
|
82
|
+
// A Blob read fails only when its backing file changed.
|
|
83
|
+
isSourceError: () => false,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function sourceRanges_(source) {
|
|
87
|
+
const { size } = source;
|
|
88
|
+
if (!Number.isSafeInteger(size) || size < 0) {
|
|
89
|
+
throw new TypeError(`A ZIP source of size ${size}`);
|
|
90
|
+
}
|
|
91
|
+
const sourceErrors = new WeakSet();
|
|
92
|
+
const record = (error) => {
|
|
93
|
+
if (typeof error === "object" && error !== null) {
|
|
94
|
+
sourceErrors.add(error);
|
|
95
|
+
}
|
|
96
|
+
return error;
|
|
97
|
+
};
|
|
98
|
+
const read = async (offset, length, signal) => {
|
|
99
|
+
let bytes;
|
|
100
|
+
try {
|
|
101
|
+
bytes = await source.read(offset, length, signal);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
throw record(error);
|
|
105
|
+
}
|
|
106
|
+
return checkLength_(bytes, offset, length);
|
|
107
|
+
};
|
|
108
|
+
return {
|
|
109
|
+
size,
|
|
110
|
+
read,
|
|
111
|
+
stream: (offset, length, signal) => producerStream_(async function* (combined) {
|
|
112
|
+
if (source.stream === undefined) {
|
|
113
|
+
for (let done = 0; done < length; done += READ_CHUNK_) {
|
|
114
|
+
yield ownBuffer_(await read(offset + done, Math.min(READ_CHUNK_, length - done), combined));
|
|
115
|
+
}
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const stream = source.stream(offset, length, combined);
|
|
119
|
+
let received = 0;
|
|
120
|
+
try {
|
|
121
|
+
for await (const chunk of readStreamChunks(stream, combined)) {
|
|
122
|
+
received += chunk.length;
|
|
123
|
+
if (received > length) {
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
yield ownBuffer_(chunk);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
throw record(error);
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
await stream.cancel(combined.reason).catch(() => { });
|
|
134
|
+
}
|
|
135
|
+
combined.throwIfAborted();
|
|
136
|
+
if (received !== length) {
|
|
137
|
+
throw new ZipFormatError(`The source gave ${received} bytes instead of ${length}`, offset);
|
|
138
|
+
}
|
|
139
|
+
}, signal),
|
|
140
|
+
isSourceError: (error) => typeof error === "object" && error !== null && sourceErrors.has(error),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function checkLength_(bytes, offset, length) {
|
|
144
|
+
if (bytes.length !== length) {
|
|
145
|
+
throw new ZipFormatError(`The source gave ${bytes.length} bytes instead of ${length}`, offset);
|
|
146
|
+
}
|
|
147
|
+
return bytes;
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=zip-source.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zip-source.js","sourceRoot":"","sources":["../src/zip-source.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEhD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AACvE,OAAO,EACN,YAAY,EACZ,cAAc,EACd,UAAU,GAEV,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAuChD,8EAA8E;AAC9E,MAAM,WAAW,GAAG,IAAI,GAAG,IAAI,CAAC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAClC,MAAkD,EAClD,MAAuB,EACvB,MAAoB;IAEpB,MAAM,EAAE,cAAc,EAAE,CAAC;IACzB,IAAI,MAAkB,CAAC;IACvB,IAAI,MAAM,YAAY,UAAU,EAAE,CAAC;QAClC,MAAM,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;IAC/B,CAAC;SAAM,IAAI,MAAM,YAAY,IAAI,EAAE,CAAC;QACnC,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC;IAC9B,CAAC;SAAM,CAAC;QACP,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OAAO,KAAK,EAAE,CAAC;gBAChB,MAAM,MAAM,CAAC,KAAK,CAAC,CAAC;YACrB,CAAC;oBAAS,CAAC;gBACV,MAAM,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;YACtD,CAAC;YACD,QAAQ,CAAC,cAAc,EAAE,CAAC;YAC1B,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;gBACzB,MAAM,IAAI,cAAc,CACvB,mBAAmB,QAAQ,qBAAqB,MAAM,EAAE,EACxD,MAAM,CACN,CAAC;YACH,CAAC;QACF,CAAC,EAAE,MAAM,CAAC;QACX,aAAa,EAAE,CAAC,KAAK,EAAE,EAAE,CACxB,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC;KACvE,CAAC;AACH,CAAC;AAED,SAAS,YAAY,CACpB,KAAiB,EACjB,MAAc,EACd,MAAc;IAEd,IAAI,KAAK,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;QAC7B,MAAM,IAAI,cAAc,CACvB,mBAAmB,KAAK,CAAC,MAAM,qBAAqB,MAAM,EAAE,EAC5D,MAAM,CACN,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { CreateZipOptions, ZipEntryInit } from "./zip-writer.js";
|
|
2
|
+
/**
|
|
3
|
+
* Writes a ZIP archive (APPNOTE 6.3) of `entries` as a stream, the bytes
|
|
4
|
+
* {@link createZip} returns whole. Nothing is written before the first read, and
|
|
5
|
+
* each read produces one chunk: a local header, a piece of content, a data
|
|
6
|
+
* descriptor, part of the central directory, the end records.
|
|
7
|
+
*
|
|
8
|
+
* Entries are pulled lazily: the next one is asked for only once the current
|
|
9
|
+
* one's content has been read to its end, so an async generator may wait for an
|
|
10
|
+
* entry to finish before yielding the next. Content in memory is written with
|
|
11
|
+
* its CRC-32 and sizes in its local header, as {@link createZip} always did. A
|
|
12
|
+
* content stream is read only as the archive is, its CRC-32 computed as it
|
|
13
|
+
* flows, deflated through `CompressionStream` unless `method` is stored, and
|
|
14
|
+
* followed by a data descriptor; a central-directory reader (Info-ZIP, Python's
|
|
15
|
+
* `zipfile`, 7-Zip, wabac.js, {@link openZip}) reads it, a reader walking local
|
|
16
|
+
* headers cannot read a stored one.
|
|
17
|
+
*
|
|
18
|
+
* ZIP64 is written where needed and nowhere else: a ZIP64 extra field in a
|
|
19
|
+
* local header for a known size (or worst-case deflated size) of 4 GiB or more,
|
|
20
|
+
* or with `zip64`; one in a central header for each size or offset of 4 GiB or
|
|
21
|
+
* more; ZIP64 end records for 65 535 entries or more, or a directory past 4
|
|
22
|
+
* GiB.
|
|
23
|
+
*
|
|
24
|
+
* Backpressure: a content stream is read at most a few chunks ahead of what is
|
|
25
|
+
* read from the archive, so memory is O(entries) for the central directory,
|
|
26
|
+
* plus those chunks and the deflate window, whatever the content's size.
|
|
27
|
+
* Cancelling the archive, or aborting `options.signal`, cancels the content
|
|
28
|
+
* stream in flight and calls the entries iterator's `return()`; the bytes
|
|
29
|
+
* already read are a truncated archive. A read pending while the iterator
|
|
30
|
+
* computes its next entry fails with the abort reason at once, but an async
|
|
31
|
+
* generator runs its `return()` (its `finally` blocks) only once the step it is
|
|
32
|
+
* awaiting settles: a generator awaiting slow work should watch the same
|
|
33
|
+
* signal. Chunks may share memory with content given in memory.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const archive = createZipStream(
|
|
37
|
+
* (async function* () {
|
|
38
|
+
* yield {
|
|
39
|
+
* name: "data.warc.gz",
|
|
40
|
+
* content: warc,
|
|
41
|
+
* method: ZipMethod.STORED,
|
|
42
|
+
* zip64: true,
|
|
43
|
+
* };
|
|
44
|
+
* yield { name: "index.cdx", content: index };
|
|
45
|
+
* })(),
|
|
46
|
+
* );
|
|
47
|
+
* await archive.pipeTo(destination);
|
|
48
|
+
*
|
|
49
|
+
* @throws {TypeError} At once, when the archive's comment is not ASCII.
|
|
50
|
+
* @throws {ZipLimitError} At once, when the archive's comment is longer than 65
|
|
51
|
+
* 535 bytes.
|
|
52
|
+
* @throws The stream errors with: `TypeError` when a name is empty, absolute,
|
|
53
|
+
* holds a `\`, appears twice, or names a directory with content, when a date
|
|
54
|
+
* is invalid, or when a `size` differs from content in memory; `RangeError`
|
|
55
|
+
* when a stream's length differs from its `size`; {@link ZipLimitError} when
|
|
56
|
+
* a name or comment is longer than 65 535 bytes, or a stream of unknown size
|
|
57
|
+
* without `zip64` passes 4 GiB; a content stream's or the entries iterator's
|
|
58
|
+
* own error; `options.signal`'s reason.
|
|
59
|
+
*/
|
|
60
|
+
export declare function createZipStream(entries: Iterable<ZipEntryInit> | AsyncIterable<ZipEntryInit>, options?: CreateZipOptions): ReadableStream<Uint8Array<ArrayBuffer>>;
|
|
61
|
+
//# sourceMappingURL=zip-stream-writer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zip-stream-writer.d.ts","sourceRoot":"","sources":["../src/zip-stream-writer.ts"],"names":[],"mappings":"AA4BA,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AActE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyDG;AACH,wBAAgB,eAAe,CAC9B,OAAO,EAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,aAAa,CAAC,YAAY,CAAC,EAC7D,OAAO,CAAC,EAAE,gBAAgB,GACxB,cAAc,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAOzC"}
|