@folijs/docx 0.0.0-stage → 0.4.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 +56 -0
- package/LICENSE +21 -0
- package/README.md +151 -2
- package/dist/export/content.d.ts +17 -0
- package/dist/export/content.d.ts.map +1 -0
- package/dist/export/content.js +165 -0
- package/dist/export/content.js.map +1 -0
- package/dist/export/index.d.ts +21 -0
- package/dist/export/index.d.ts.map +1 -0
- package/dist/export/index.js +360 -0
- package/dist/export/index.js.map +1 -0
- package/dist/export/parts.d.ts +24 -0
- package/dist/export/parts.d.ts.map +1 -0
- package/dist/export/parts.js +413 -0
- package/dist/export/parts.js.map +1 -0
- package/dist/export/properties.d.ts +39 -0
- package/dist/export/properties.d.ts.map +1 -0
- package/dist/export/properties.js +506 -0
- package/dist/export/properties.js.map +1 -0
- package/dist/export/xmlout.d.ts +63 -0
- package/dist/export/xmlout.d.ts.map +1 -0
- package/dist/export/xmlout.js +194 -0
- package/dist/export/xmlout.js.map +1 -0
- package/dist/import/content.d.ts +35 -0
- package/dist/import/content.d.ts.map +1 -0
- package/dist/import/content.js +370 -0
- package/dist/import/content.js.map +1 -0
- package/dist/import/index.d.ts +31 -0
- package/dist/import/index.d.ts.map +1 -0
- package/dist/import/index.js +106 -0
- package/dist/import/index.js.map +1 -0
- package/dist/import/ooxml.d.ts +39 -0
- package/dist/import/ooxml.d.ts.map +1 -0
- package/dist/import/ooxml.js +71 -0
- package/dist/import/ooxml.js.map +1 -0
- package/dist/import/parts.d.ts +35 -0
- package/dist/import/parts.d.ts.map +1 -0
- package/dist/import/parts.js +365 -0
- package/dist/import/parts.js.map +1 -0
- package/dist/import/properties.d.ts +50 -0
- package/dist/import/properties.d.ts.map +1 -0
- package/dist/import/properties.js +404 -0
- package/dist/import/properties.js.map +1 -0
- package/dist/import/report.d.ts +28 -0
- package/dist/import/report.d.ts.map +1 -0
- package/dist/import/report.js +42 -0
- package/dist/import/report.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/package.d.ts +70 -0
- package/dist/package.d.ts.map +1 -0
- package/dist/package.js +259 -0
- package/dist/package.js.map +1 -0
- package/dist/settings.d.ts +8 -0
- package/dist/settings.d.ts.map +1 -0
- package/dist/settings.js +72 -0
- package/dist/settings.js.map +1 -0
- package/dist/xml.d.ts +79 -0
- package/dist/xml.d.ts.map +1 -0
- package/dist/xml.js +384 -0
- package/dist/xml.js.map +1 -0
- package/dist/zip.d.ts +66 -0
- package/dist/zip.d.ts.map +1 -0
- package/dist/zip.js +356 -0
- package/dist/zip.js.map +1 -0
- package/package.json +36 -3
- package/src/export/content.ts +229 -0
- package/src/export/index.ts +473 -0
- package/src/export/parts.ts +545 -0
- package/src/export/properties.ts +608 -0
- package/src/export/xmlout.ts +225 -0
- package/src/import/content.ts +413 -0
- package/src/import/index.ts +139 -0
- package/src/import/ooxml.ts +78 -0
- package/src/import/parts.ts +423 -0
- package/src/import/properties.ts +507 -0
- package/src/import/report.ts +64 -0
- package/src/index.ts +24 -0
- package/src/package.ts +328 -0
- package/src/settings.ts +75 -0
- package/src/xml.ts +443 -0
- package/src/zip.ts +401 -0
package/src/zip.ts
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The zip container of a `.docx`, on the platform's `DecompressionStream`/`CompressionStream`
|
|
3
|
+
* ("deflate-raw": browsers, workers, Node 20.12+); no dependency.
|
|
4
|
+
*
|
|
5
|
+
* Reading keeps every entry's compressed bytes as they are in the file, so writing copies an
|
|
6
|
+
* untouched entry verbatim: its data is the original byte for byte, never recompressed. Only
|
|
7
|
+
* replaced entries are compressed again.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** A file in the zip. `raw` is its data as stored (compressed when `method` is 8). */
|
|
11
|
+
export interface ZipEntry {
|
|
12
|
+
readonly name: string;
|
|
13
|
+
/** The name's bytes as stored, written back unchanged. */
|
|
14
|
+
readonly nameBytes: Uint8Array;
|
|
15
|
+
/** 0 stored, 8 deflated. */
|
|
16
|
+
readonly method: 0 | 8;
|
|
17
|
+
/** General purpose flags; only the UTF-8 name bit (11) is written back. */
|
|
18
|
+
readonly flags: number;
|
|
19
|
+
/** DOS time and date. */
|
|
20
|
+
readonly time: number;
|
|
21
|
+
readonly date: number;
|
|
22
|
+
readonly crc32: number;
|
|
23
|
+
/** Uncompressed size. */
|
|
24
|
+
readonly size: number;
|
|
25
|
+
readonly raw: Uint8Array;
|
|
26
|
+
/**
|
|
27
|
+
* As read: the local header (with name and extra field), the data descriptor if any, and the
|
|
28
|
+
* central directory record, written back unchanged while `raw` is the data read with them.
|
|
29
|
+
*/
|
|
30
|
+
readonly stored?: {
|
|
31
|
+
readonly local: Uint8Array;
|
|
32
|
+
readonly descriptor: Uint8Array;
|
|
33
|
+
readonly central: Uint8Array;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ZipLimits {
|
|
38
|
+
/** Most entries accepted. Default 10 000. */
|
|
39
|
+
readonly maxEntries?: number;
|
|
40
|
+
/** Most bytes accepted once uncompressed, all entries together. Default 512 MiB. */
|
|
41
|
+
readonly maxSize?: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A file that is not a zip Foli can read, or is damaged. */
|
|
45
|
+
export class ZipError extends Error {
|
|
46
|
+
override name = "ZipError";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const LOCAL = 0x04034b50;
|
|
50
|
+
const CENTRAL = 0x02014b50;
|
|
51
|
+
const END = 0x06054b50;
|
|
52
|
+
const DESCRIPTOR = 0x08074b50;
|
|
53
|
+
const UTF8_NAME = 0x0800;
|
|
54
|
+
const textDecoder = new TextDecoder();
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Lists the entries of a zip from its central directory, in the directory's order. Nothing is
|
|
58
|
+
* decompressed: see {@link inflateEntry}. Every entry is checked against its local header, and
|
|
59
|
+
* entries may not overlap: Foli reads what any other zip reader would.
|
|
60
|
+
*/
|
|
61
|
+
export function readZip(bytes: Uint8Array, limits: ZipLimits = {}): ZipEntry[] {
|
|
62
|
+
const maxEntries = limits.maxEntries ?? 10_000;
|
|
63
|
+
const maxSize = limits.maxSize ?? 512 * 1024 * 1024;
|
|
64
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
65
|
+
const end = findEnd(view);
|
|
66
|
+
if (view.getUint16(end + 4, true) !== 0 || view.getUint16(end + 6, true) !== 0) {
|
|
67
|
+
throw new ZipError("Multi-disk zip archives are not supported");
|
|
68
|
+
}
|
|
69
|
+
const count = view.getUint16(end + 10, true);
|
|
70
|
+
const dirSize = view.getUint32(end + 12, true);
|
|
71
|
+
const dirOffset = view.getUint32(end + 16, true);
|
|
72
|
+
// shortcut: no ZIP64 (Word writes it only past 4 GiB or 65 535 entries); add if a real file needs it.
|
|
73
|
+
if (count === 0xffff || dirSize === 0xffffffff || dirOffset === 0xffffffff) {
|
|
74
|
+
throw new ZipError("ZIP64 archives are not supported");
|
|
75
|
+
}
|
|
76
|
+
if (count > maxEntries) throw new ZipError(`Too many entries: ${count} (limit ${maxEntries})`);
|
|
77
|
+
|
|
78
|
+
const entries: ZipEntry[] = [];
|
|
79
|
+
const spans: { start: number; end: number; name: string }[] = [];
|
|
80
|
+
const seen = new Set<string>();
|
|
81
|
+
let total = 0;
|
|
82
|
+
let at = dirOffset;
|
|
83
|
+
for (let i = 0; i < count; i++) {
|
|
84
|
+
if (at + 46 > end || view.getUint32(at, true) !== CENTRAL) {
|
|
85
|
+
throw new ZipError("Damaged zip: bad central directory record");
|
|
86
|
+
}
|
|
87
|
+
const flags = view.getUint16(at + 8, true);
|
|
88
|
+
const method = view.getUint16(at + 10, true);
|
|
89
|
+
const time = view.getUint16(at + 12, true);
|
|
90
|
+
const date = view.getUint16(at + 14, true);
|
|
91
|
+
const crc32 = view.getUint32(at + 16, true);
|
|
92
|
+
const compressedSize = view.getUint32(at + 20, true);
|
|
93
|
+
const size = view.getUint32(at + 24, true);
|
|
94
|
+
const nameLength = view.getUint16(at + 28, true);
|
|
95
|
+
const recordLength =
|
|
96
|
+
46 + nameLength + view.getUint16(at + 30, true) + view.getUint16(at + 32, true);
|
|
97
|
+
const local = view.getUint32(at + 42, true);
|
|
98
|
+
if (at + recordLength > end) throw new ZipError("Damaged zip: bad central directory record");
|
|
99
|
+
const central = bytes.subarray(at, at + recordLength);
|
|
100
|
+
const nameBytes = bytes.subarray(at + 46, at + 46 + nameLength);
|
|
101
|
+
// shortcut: names without the UTF-8 flag (CP437) are read as UTF-8, right for ASCII, which
|
|
102
|
+
// part names are; the bytes are written back unchanged either way.
|
|
103
|
+
const name = textDecoder.decode(nameBytes);
|
|
104
|
+
at += recordLength;
|
|
105
|
+
|
|
106
|
+
if (flags & 0x41) throw new ZipError(`Encrypted entry: ${name}`);
|
|
107
|
+
if (compressedSize === 0xffffffff || size === 0xffffffff || local === 0xffffffff) {
|
|
108
|
+
throw new ZipError("ZIP64 archives are not supported");
|
|
109
|
+
}
|
|
110
|
+
if (method !== 0 && method !== 8) {
|
|
111
|
+
throw new ZipError(`Unsupported compression method ${method}: ${name}`);
|
|
112
|
+
}
|
|
113
|
+
// Deflate expands at most 1032 times: a larger declared size is a lie meant to make the
|
|
114
|
+
// reader allocate it.
|
|
115
|
+
if (method === 0 ? size !== compressedSize : size > compressedSize * 1032 + 1024) {
|
|
116
|
+
throw new ZipError(`Damaged zip: impossible size for ${name}`);
|
|
117
|
+
}
|
|
118
|
+
const key = name.toLowerCase();
|
|
119
|
+
if (seen.has(key)) throw new ZipError(`Duplicate entry: ${name}`);
|
|
120
|
+
seen.add(key);
|
|
121
|
+
total += size;
|
|
122
|
+
if (total > maxSize) throw new ZipError(`Too large once uncompressed (limit ${maxSize} bytes)`);
|
|
123
|
+
|
|
124
|
+
if (local + 30 > dirOffset || view.getUint32(local, true) !== LOCAL) {
|
|
125
|
+
throw new ZipError(`Damaged zip: no local header for ${name}`);
|
|
126
|
+
}
|
|
127
|
+
const localNameLength = view.getUint16(local + 26, true);
|
|
128
|
+
// The data follows the local header, whose extra field may differ from the central record's.
|
|
129
|
+
const data = local + 30 + localNameLength + view.getUint16(local + 28, true);
|
|
130
|
+
if (data + compressedSize > dirOffset) {
|
|
131
|
+
throw new ZipError(`Damaged zip: data out of range for ${name}`);
|
|
132
|
+
}
|
|
133
|
+
const mismatch = () =>
|
|
134
|
+
new ZipError(`Damaged zip: local header does not match the directory for ${name}`);
|
|
135
|
+
if (
|
|
136
|
+
view.getUint16(local + 8, true) !== method ||
|
|
137
|
+
!equalBytes(bytes.subarray(local + 30, local + 30 + localNameLength), nameBytes)
|
|
138
|
+
) {
|
|
139
|
+
throw mismatch();
|
|
140
|
+
}
|
|
141
|
+
// A data descriptor (flag 3) follows the data with the CRC and sizes, with or without a
|
|
142
|
+
// signature; the local header then may hold zeros. Either way they must agree.
|
|
143
|
+
let descriptorLength = 0;
|
|
144
|
+
let check = local + 14;
|
|
145
|
+
if (flags & 0x08) {
|
|
146
|
+
const after = data + compressedSize;
|
|
147
|
+
const signed = after + 4 <= dirOffset && view.getUint32(after, true) === DESCRIPTOR;
|
|
148
|
+
descriptorLength = signed ? 16 : 12;
|
|
149
|
+
if (after + descriptorLength > dirOffset) {
|
|
150
|
+
throw new ZipError(`Damaged zip: no data descriptor for ${name}`);
|
|
151
|
+
}
|
|
152
|
+
check = signed ? after + 4 : after;
|
|
153
|
+
}
|
|
154
|
+
if (
|
|
155
|
+
view.getUint32(check, true) !== crc32 ||
|
|
156
|
+
view.getUint32(check + 4, true) !== compressedSize ||
|
|
157
|
+
view.getUint32(check + 8, true) !== size
|
|
158
|
+
) {
|
|
159
|
+
throw mismatch();
|
|
160
|
+
}
|
|
161
|
+
const stop = data + compressedSize + descriptorLength;
|
|
162
|
+
spans.push({ start: local, end: stop, name });
|
|
163
|
+
entries.push({
|
|
164
|
+
name,
|
|
165
|
+
nameBytes,
|
|
166
|
+
method,
|
|
167
|
+
flags,
|
|
168
|
+
time,
|
|
169
|
+
date,
|
|
170
|
+
crc32,
|
|
171
|
+
size,
|
|
172
|
+
raw: bytes.subarray(data, data + compressedSize),
|
|
173
|
+
stored: {
|
|
174
|
+
local: bytes.subarray(local, data),
|
|
175
|
+
descriptor: bytes.subarray(data + compressedSize, stop),
|
|
176
|
+
central,
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
if (at !== dirOffset + dirSize) {
|
|
181
|
+
throw new ZipError("Damaged zip: central directory size mismatch");
|
|
182
|
+
}
|
|
183
|
+
// Overlapping entries share data: another reader could see other content, and a small file
|
|
184
|
+
// could expand without limit.
|
|
185
|
+
spans.sort((a, b) => a.start - b.start);
|
|
186
|
+
for (let i = 1; i < spans.length; i++) {
|
|
187
|
+
const span = spans[i] as (typeof spans)[number];
|
|
188
|
+
if (span.start < (spans[i - 1] as (typeof spans)[number]).end) {
|
|
189
|
+
throw new ZipError(`Damaged zip: ${span.name} overlaps another entry`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return entries;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function equalBytes(a: Uint8Array, b: Uint8Array): boolean {
|
|
196
|
+
if (a.length !== b.length) return false;
|
|
197
|
+
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** The end record: the last one whose central directory is where it says (not one in a comment). */
|
|
202
|
+
function findEnd(view: DataView): number {
|
|
203
|
+
// The end record is 22 bytes plus a comment of at most 65 535.
|
|
204
|
+
const last = view.byteLength - 22;
|
|
205
|
+
for (let at = last; at >= 0 && at >= last - 0xffff; at--) {
|
|
206
|
+
if (view.getUint32(at, true) !== END) continue;
|
|
207
|
+
if (at + 22 + view.getUint16(at + 20, true) > view.byteLength) continue;
|
|
208
|
+
const count = view.getUint16(at + 10, true);
|
|
209
|
+
const dirSize = view.getUint32(at + 12, true);
|
|
210
|
+
const dirOffset = view.getUint32(at + 16, true);
|
|
211
|
+
// ZIP64 and multi-disk archives: accepted here to be refused by name.
|
|
212
|
+
const special =
|
|
213
|
+
count === 0xffff ||
|
|
214
|
+
dirOffset === 0xffffffff ||
|
|
215
|
+
view.getUint16(at + 4, true) !== 0 ||
|
|
216
|
+
view.getUint16(at + 6, true) !== 0;
|
|
217
|
+
// The directory ends where the end record starts (no ZIP64 records in between).
|
|
218
|
+
const directory =
|
|
219
|
+
dirOffset + dirSize === at && (count === 0 || view.getUint32(dirOffset, true) === CENTRAL);
|
|
220
|
+
if (special || directory) return at;
|
|
221
|
+
}
|
|
222
|
+
const head = view.byteLength >= 4 ? view.getUint32(0, false) : 0;
|
|
223
|
+
// Compound File Binary: an encrypted .docx, or a .doc.
|
|
224
|
+
if (head === 0xd0cf11e0) {
|
|
225
|
+
throw new ZipError("Not a zip: an encrypted document or a legacy .doc (Compound File)");
|
|
226
|
+
}
|
|
227
|
+
throw new ZipError("Not a zip archive");
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** The entry's data, decompressed, checked against its size and CRC-32. */
|
|
231
|
+
export async function inflateEntry(entry: ZipEntry): Promise<Uint8Array> {
|
|
232
|
+
const data =
|
|
233
|
+
entry.method === 0
|
|
234
|
+
? new Uint8Array(entry.raw)
|
|
235
|
+
: await inflateRaw(entry.raw, entry.size, entry.name);
|
|
236
|
+
if (data.length !== entry.size) throw new ZipError(`Damaged zip: wrong size for ${entry.name}`);
|
|
237
|
+
if (crc32(data) !== entry.crc32)
|
|
238
|
+
throw new ZipError(`Damaged zip: CRC mismatch for ${entry.name}`);
|
|
239
|
+
return data;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async function inflateRaw(raw: Uint8Array, size: number, name: string): Promise<Uint8Array> {
|
|
243
|
+
// Collected as it comes, not allocated from the declared size.
|
|
244
|
+
const chunks: Uint8Array[] = [];
|
|
245
|
+
let length = 0;
|
|
246
|
+
const reader = streamOf(raw).pipeThrough(new DecompressionStream("deflate-raw")).getReader();
|
|
247
|
+
try {
|
|
248
|
+
for (;;) {
|
|
249
|
+
const { done, value } = await reader.read();
|
|
250
|
+
if (done) break;
|
|
251
|
+
// Never past the declared size: a zip bomb stops here.
|
|
252
|
+
if (length + value.length > size) throw new ZipError(`Damaged zip: wrong size for ${name}`);
|
|
253
|
+
chunks.push(value);
|
|
254
|
+
length += value.length;
|
|
255
|
+
}
|
|
256
|
+
} catch (error) {
|
|
257
|
+
await reader.cancel().catch(() => {});
|
|
258
|
+
if (error instanceof ZipError) throw error;
|
|
259
|
+
throw new ZipError(`Damaged zip: cannot decompress ${name}`);
|
|
260
|
+
}
|
|
261
|
+
if (length !== size) throw new ZipError(`Damaged zip: wrong size for ${name}`);
|
|
262
|
+
return concat(chunks);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* A new deflated entry holding `data`. With `like` (the entry it replaces) it keeps that entry's
|
|
267
|
+
* name bytes, name encoding flag, time and date.
|
|
268
|
+
*/
|
|
269
|
+
export async function deflateEntry(
|
|
270
|
+
name: string,
|
|
271
|
+
data: Uint8Array,
|
|
272
|
+
like?: Pick<ZipEntry, "time" | "date" | "nameBytes" | "flags">,
|
|
273
|
+
): Promise<ZipEntry> {
|
|
274
|
+
const chunks: Uint8Array[] = [];
|
|
275
|
+
const reader = streamOf(data).pipeThrough(new CompressionStream("deflate-raw")).getReader();
|
|
276
|
+
for (;;) {
|
|
277
|
+
const { done, value } = await reader.read();
|
|
278
|
+
if (done) break;
|
|
279
|
+
chunks.push(value);
|
|
280
|
+
}
|
|
281
|
+
return {
|
|
282
|
+
name,
|
|
283
|
+
nameBytes: like?.nameBytes ?? new TextEncoder().encode(name),
|
|
284
|
+
method: 8,
|
|
285
|
+
flags: like ? like.flags & UTF8_NAME : UTF8_NAME,
|
|
286
|
+
time: like?.time ?? 0,
|
|
287
|
+
// 1980-01-01, the earliest DOS date, when there is no entry to take it from.
|
|
288
|
+
date: like?.date ?? 0x21,
|
|
289
|
+
crc32: crc32(data),
|
|
290
|
+
size: data.length,
|
|
291
|
+
raw: concat(chunks),
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* The zip of `entries`, in order; each entry's data written as it is in `raw`. An entry read
|
|
297
|
+
* from a zip keeps its headers byte for byte (only its offset in the directory changes), so a
|
|
298
|
+
* zip written back with its entries unchanged is the same file, short of a trailing comment.
|
|
299
|
+
*/
|
|
300
|
+
export function writeZip(entries: readonly ZipEntry[]): Uint8Array {
|
|
301
|
+
const parts: Uint8Array[] = [];
|
|
302
|
+
const directory: Uint8Array[] = [];
|
|
303
|
+
let offset = 0;
|
|
304
|
+
for (const entry of entries) {
|
|
305
|
+
if (entry.stored) {
|
|
306
|
+
const { local, descriptor } = entry.stored;
|
|
307
|
+
// A copy (a Node Buffer's `slice` is a view: the offset would be written into the input).
|
|
308
|
+
const central = new Uint8Array(entry.stored.central);
|
|
309
|
+
new DataView(central.buffer).setUint32(42, offset, true);
|
|
310
|
+
parts.push(local, entry.raw, descriptor);
|
|
311
|
+
directory.push(central);
|
|
312
|
+
offset += local.length + entry.raw.length + descriptor.length;
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
const flags = entry.flags & UTF8_NAME;
|
|
316
|
+
const local = new Uint8Array(30 + entry.nameBytes.length);
|
|
317
|
+
const l = new DataView(local.buffer);
|
|
318
|
+
l.setUint32(0, LOCAL, true);
|
|
319
|
+
l.setUint16(4, 20, true);
|
|
320
|
+
l.setUint16(6, flags, true);
|
|
321
|
+
l.setUint16(8, entry.method, true);
|
|
322
|
+
l.setUint16(10, entry.time, true);
|
|
323
|
+
l.setUint16(12, entry.date, true);
|
|
324
|
+
l.setUint32(14, entry.crc32, true);
|
|
325
|
+
l.setUint32(18, entry.raw.length, true);
|
|
326
|
+
l.setUint32(22, entry.size, true);
|
|
327
|
+
l.setUint16(26, entry.nameBytes.length, true);
|
|
328
|
+
local.set(entry.nameBytes, 30);
|
|
329
|
+
|
|
330
|
+
const central = new Uint8Array(46 + entry.nameBytes.length);
|
|
331
|
+
const c = new DataView(central.buffer);
|
|
332
|
+
c.setUint32(0, CENTRAL, true);
|
|
333
|
+
c.setUint16(4, 20, true);
|
|
334
|
+
c.setUint16(6, 20, true);
|
|
335
|
+
c.setUint16(8, flags, true);
|
|
336
|
+
c.setUint16(10, entry.method, true);
|
|
337
|
+
c.setUint16(12, entry.time, true);
|
|
338
|
+
c.setUint16(14, entry.date, true);
|
|
339
|
+
c.setUint32(16, entry.crc32, true);
|
|
340
|
+
c.setUint32(20, entry.raw.length, true);
|
|
341
|
+
c.setUint32(24, entry.size, true);
|
|
342
|
+
c.setUint16(28, entry.nameBytes.length, true);
|
|
343
|
+
c.setUint32(42, offset, true);
|
|
344
|
+
central.set(entry.nameBytes, 46);
|
|
345
|
+
|
|
346
|
+
parts.push(local, entry.raw);
|
|
347
|
+
directory.push(central);
|
|
348
|
+
offset += local.length + entry.raw.length;
|
|
349
|
+
}
|
|
350
|
+
if (entries.length > 0xfffe || offset > 0xfffffffe) {
|
|
351
|
+
throw new ZipError("Too large for a zip without ZIP64");
|
|
352
|
+
}
|
|
353
|
+
const dirSize = directory.reduce((sum, record) => sum + record.length, 0);
|
|
354
|
+
const end = new Uint8Array(22);
|
|
355
|
+
const e = new DataView(end.buffer);
|
|
356
|
+
e.setUint32(0, END, true);
|
|
357
|
+
e.setUint16(8, entries.length, true);
|
|
358
|
+
e.setUint16(10, entries.length, true);
|
|
359
|
+
e.setUint32(12, dirSize, true);
|
|
360
|
+
e.setUint32(16, offset, true);
|
|
361
|
+
return concat([...parts, ...directory, end]);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function streamOf(data: Uint8Array): ReadableStream<BufferSource> {
|
|
365
|
+
return new ReadableStream<BufferSource>({
|
|
366
|
+
start(controller) {
|
|
367
|
+
// Never a SharedArrayBuffer here: the bytes come from slices and encoders.
|
|
368
|
+
controller.enqueue(data as Uint8Array<ArrayBuffer>);
|
|
369
|
+
controller.close();
|
|
370
|
+
},
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function concat(chunks: readonly Uint8Array[]): Uint8Array {
|
|
375
|
+
const out = new Uint8Array(chunks.reduce((sum, chunk) => sum + chunk.length, 0));
|
|
376
|
+
let at = 0;
|
|
377
|
+
for (const chunk of chunks) {
|
|
378
|
+
out.set(chunk, at);
|
|
379
|
+
at += chunk.length;
|
|
380
|
+
}
|
|
381
|
+
return out;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
let table: Uint32Array | undefined;
|
|
385
|
+
|
|
386
|
+
/** CRC-32 (IEEE), as zip stores it. */
|
|
387
|
+
export function crc32(data: Uint8Array): number {
|
|
388
|
+
if (!table) {
|
|
389
|
+
table = new Uint32Array(256);
|
|
390
|
+
for (let n = 0; n < 256; n++) {
|
|
391
|
+
let c = n;
|
|
392
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
393
|
+
table[n] = c >>> 0;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
let crc = 0xffffffff;
|
|
397
|
+
for (let i = 0; i < data.length; i++) {
|
|
398
|
+
crc = (table[(crc ^ (data[i] as number)) & 0xff] as number) ^ (crc >>> 8);
|
|
399
|
+
}
|
|
400
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
401
|
+
}
|