@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.
Files changed (51) hide show
  1. package/CHANGELOG.md +88 -0
  2. package/README.md +85 -9
  3. package/dist/_producer-stream.d.ts +14 -0
  4. package/dist/_producer-stream.d.ts.map +1 -0
  5. package/dist/_producer-stream.js +80 -0
  6. package/dist/_producer-stream.js.map +1 -0
  7. package/dist/_zip-directory.d.ts +97 -0
  8. package/dist/_zip-directory.d.ts.map +1 -0
  9. package/dist/_zip-directory.js +541 -0
  10. package/dist/_zip-directory.js.map +1 -0
  11. package/dist/_zip-entries.d.ts +31 -0
  12. package/dist/_zip-entries.d.ts.map +1 -0
  13. package/dist/_zip-entries.js +192 -0
  14. package/dist/_zip-entries.js.map +1 -0
  15. package/dist/_zip-format.d.ts +48 -0
  16. package/dist/_zip-format.d.ts.map +1 -0
  17. package/dist/_zip-format.js +49 -0
  18. package/dist/_zip-format.js.map +1 -0
  19. package/dist/_zip-headers.d.ts +49 -0
  20. package/dist/_zip-headers.d.ts.map +1 -0
  21. package/dist/_zip-headers.js +158 -0
  22. package/dist/_zip-headers.js.map +1 -0
  23. package/dist/index.d.ts +4 -0
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +4 -0
  26. package/dist/index.js.map +1 -1
  27. package/dist/zip-archive.d.ts +45 -2
  28. package/dist/zip-archive.d.ts.map +1 -1
  29. package/dist/zip-archive.js +14 -202
  30. package/dist/zip-archive.js.map +1 -1
  31. package/dist/zip-error.d.ts +5 -1
  32. package/dist/zip-error.d.ts.map +1 -1
  33. package/dist/zip-error.js +5 -1
  34. package/dist/zip-error.js.map +1 -1
  35. package/dist/zip-method.d.ts +11 -0
  36. package/dist/zip-method.d.ts.map +1 -0
  37. package/dist/zip-method.js +12 -0
  38. package/dist/zip-method.js.map +1 -0
  39. package/dist/zip-source.d.ts +59 -0
  40. package/dist/zip-source.d.ts.map +1 -0
  41. package/dist/zip-source.js +149 -0
  42. package/dist/zip-source.js.map +1 -0
  43. package/dist/zip-stream-writer.d.ts +61 -0
  44. package/dist/zip-stream-writer.d.ts.map +1 -0
  45. package/dist/zip-stream-writer.js +419 -0
  46. package/dist/zip-stream-writer.js.map +1 -0
  47. package/dist/zip-writer.d.ts +89 -0
  48. package/dist/zip-writer.d.ts.map +1 -0
  49. package/dist/zip-writer.js +35 -0
  50. package/dist/zip-writer.js.map +1 -0
  51. package/package.json +6 -4
@@ -0,0 +1,419 @@
1
+ import { concatBytes, encodeTextUtf8, readStreamBytes, readStreamChunks, } from "@ac-kit/core";
2
+ import { crc_32 } from "@ac-kit/noncrypto-hash";
3
+ import { producerStream_ } from "./_producer-stream.js";
4
+ import { ownBuffer_ } from "./_zip-entries.js";
5
+ import { DATA_DESCRIPTOR_FLAG_, MAX_COMMENT_, UTF8_FLAG_, VERSION_DEFLATE_OR_DIRECTORY_, VERSION_STORED_, VERSION_ZIP64_, } from "./_zip-format.js";
6
+ import { needsZip64_, writeCentralHeader_, writeDataDescriptor_, writeEndRecords_, writeLocalHeader_, } from "./_zip-headers.js";
7
+ import { ZipLimitError } from "./zip-error.js";
8
+ import { ZipMethod } from "./zip-method.js";
9
+ const MS_DOS_DIRECTORY_ATTRIBUTE_ = 0x10;
10
+ const FIRST_DOS_YEAR_ = 1980;
11
+ const LAST_DOS_YEAR_ = 2107;
12
+ // 1980-01-01, the earliest MS-DOS date: day 1, month 1, year 0.
13
+ const FIRST_DOS_DATE_ = 0x21;
14
+ const LAST_DOS_DATE_ = ((LAST_DOS_YEAR_ - FIRST_DOS_YEAR_) << 9) | (12 << 5) | 31;
15
+ // 23:59:58, the last MS-DOS time.
16
+ const LAST_DOS_TIME_ = (23 << 11) | (59 << 5) | 29;
17
+ /** Central headers are emitted in chunks of about this many bytes. */
18
+ const DIRECTORY_CHUNK_ = 64 * 1024;
19
+ /**
20
+ * Writes a ZIP archive (APPNOTE 6.3) of `entries` as a stream, the bytes
21
+ * {@link createZip} returns whole. Nothing is written before the first read, and
22
+ * each read produces one chunk: a local header, a piece of content, a data
23
+ * descriptor, part of the central directory, the end records.
24
+ *
25
+ * Entries are pulled lazily: the next one is asked for only once the current
26
+ * one's content has been read to its end, so an async generator may wait for an
27
+ * entry to finish before yielding the next. Content in memory is written with
28
+ * its CRC-32 and sizes in its local header, as {@link createZip} always did. A
29
+ * content stream is read only as the archive is, its CRC-32 computed as it
30
+ * flows, deflated through `CompressionStream` unless `method` is stored, and
31
+ * followed by a data descriptor; a central-directory reader (Info-ZIP, Python's
32
+ * `zipfile`, 7-Zip, wabac.js, {@link openZip}) reads it, a reader walking local
33
+ * headers cannot read a stored one.
34
+ *
35
+ * ZIP64 is written where needed and nowhere else: a ZIP64 extra field in a
36
+ * local header for a known size (or worst-case deflated size) of 4 GiB or more,
37
+ * or with `zip64`; one in a central header for each size or offset of 4 GiB or
38
+ * more; ZIP64 end records for 65 535 entries or more, or a directory past 4
39
+ * GiB.
40
+ *
41
+ * Backpressure: a content stream is read at most a few chunks ahead of what is
42
+ * read from the archive, so memory is O(entries) for the central directory,
43
+ * plus those chunks and the deflate window, whatever the content's size.
44
+ * Cancelling the archive, or aborting `options.signal`, cancels the content
45
+ * stream in flight and calls the entries iterator's `return()`; the bytes
46
+ * already read are a truncated archive. A read pending while the iterator
47
+ * computes its next entry fails with the abort reason at once, but an async
48
+ * generator runs its `return()` (its `finally` blocks) only once the step it is
49
+ * awaiting settles: a generator awaiting slow work should watch the same
50
+ * signal. Chunks may share memory with content given in memory.
51
+ *
52
+ * @example
53
+ * const archive = createZipStream(
54
+ * (async function* () {
55
+ * yield {
56
+ * name: "data.warc.gz",
57
+ * content: warc,
58
+ * method: ZipMethod.STORED,
59
+ * zip64: true,
60
+ * };
61
+ * yield { name: "index.cdx", content: index };
62
+ * })(),
63
+ * );
64
+ * await archive.pipeTo(destination);
65
+ *
66
+ * @throws {TypeError} At once, when the archive's comment is not ASCII.
67
+ * @throws {ZipLimitError} At once, when the archive's comment is longer than 65
68
+ * 535 bytes.
69
+ * @throws The stream errors with: `TypeError` when a name is empty, absolute,
70
+ * holds a `\`, appears twice, or names a directory with content, when a date
71
+ * is invalid, or when a `size` differs from content in memory; `RangeError`
72
+ * when a stream's length differs from its `size`; {@link ZipLimitError} when
73
+ * a name or comment is longer than 65 535 bytes, or a stream of unknown size
74
+ * without `zip64` passes 4 GiB; a content stream's or the entries iterator's
75
+ * own error; `options.signal`'s reason.
76
+ */
77
+ export function createZipStream(entries, options) {
78
+ const archiveComment = encodeArchiveComment_(options?.comment ?? "");
79
+ return producerStream_((signal) => writeArchive_(entries, archiveComment, signal), options?.signal);
80
+ }
81
+ async function* writeArchive_(entries, archiveComment, signal) {
82
+ signal.throwIfAborted();
83
+ const names = new Set();
84
+ const centralHeaders = [];
85
+ let offset = 0;
86
+ const iterator = Symbol.asyncIterator in entries
87
+ ? entries[Symbol.asyncIterator]()
88
+ : entries[Symbol.iterator]();
89
+ // Told to stop as soon as the signal aborts, even while it computes the next
90
+ // entry: an async generator applies it once that step settles.
91
+ const handleAbort = () => {
92
+ void Promise.resolve()
93
+ .then(() => iterator.return?.())
94
+ .catch(() => { });
95
+ };
96
+ signal.addEventListener("abort", handleAbort, { once: true });
97
+ let isDone = false;
98
+ try {
99
+ for (;;) {
100
+ const step = await iterator.next();
101
+ if (step.done === true) {
102
+ isDone = true;
103
+ break;
104
+ }
105
+ const entry = step.value;
106
+ let shared;
107
+ try {
108
+ signal.throwIfAborted();
109
+ shared = checkEntry_(entry, names);
110
+ }
111
+ catch (error) {
112
+ if (entry.content instanceof ReadableStream) {
113
+ await entry.content.cancel(error).catch(() => { });
114
+ }
115
+ throw error;
116
+ }
117
+ const isDirectory = entry.name.endsWith("/");
118
+ const localOffset = offset;
119
+ let central;
120
+ if (entry.content instanceof ReadableStream) {
121
+ const written = yield* writeStreamedEntry_(entry, entry.content, shared, signal);
122
+ offset += written.length;
123
+ central = written.fields;
124
+ }
125
+ else {
126
+ const content = entry.content ?? new Uint8Array(0);
127
+ if (entry.size !== undefined && entry.size !== content.length) {
128
+ throw new TypeError(`ZIP entry "${entry.name}" of ${content.length} bytes declared as ${entry.size}`);
129
+ }
130
+ const { method, data } = await compress_(entry, content, signal);
131
+ const zip64 = entry.zip64 === true ||
132
+ needsZip64_(content.length) ||
133
+ needsZip64_(data.length);
134
+ central = {
135
+ ...shared,
136
+ zip64Sizes: zip64,
137
+ version: version_(method, isDirectory, zip64),
138
+ method,
139
+ crc32: crc_32(content),
140
+ compressedSize: data.length,
141
+ size: content.length,
142
+ };
143
+ const local = writeLocalHeader_(central, zip64);
144
+ yield local;
145
+ if (data.length > 0) {
146
+ yield data;
147
+ }
148
+ offset += local.length + data.length;
149
+ }
150
+ centralHeaders.push(writeCentralHeader_({ ...central, offset: localOffset }));
151
+ }
152
+ }
153
+ finally {
154
+ signal.removeEventListener("abort", handleAbort);
155
+ if (!isDone && !signal.aborted) {
156
+ await iterator.return?.();
157
+ }
158
+ }
159
+ signal.throwIfAborted();
160
+ const directoryOffset = offset;
161
+ let directorySize = 0;
162
+ let pending = [];
163
+ let pendingSize = 0;
164
+ for (const header of centralHeaders) {
165
+ pending.push(header);
166
+ pendingSize += header.length;
167
+ if (pendingSize >= DIRECTORY_CHUNK_) {
168
+ yield concatBytes(pending);
169
+ directorySize += pendingSize;
170
+ pending = [];
171
+ pendingSize = 0;
172
+ }
173
+ }
174
+ directorySize += pendingSize;
175
+ pending.push(writeEndRecords_(names.size, directorySize, directoryOffset, archiveComment));
176
+ yield concatBytes(pending);
177
+ }
178
+ /**
179
+ * Writes a stream's local header, its content as it is read, and its data
180
+ * descriptor, and returns the central header's fields and the bytes written.
181
+ */
182
+ async function* writeStreamedEntry_(entry, content, shared, signal) {
183
+ const declared = entry.size;
184
+ const method = entry.method === ZipMethod.STORED ? ZipMethod.STORED : ZipMethod.DEFLATE;
185
+ const zip64 = entry.zip64 === true ||
186
+ (declared !== undefined &&
187
+ needsZip64_(method === ZipMethod.STORED ? declared : deflateBound_(declared)));
188
+ const flags = shared.flags | DATA_DESCRIPTOR_FLAG_;
189
+ const version = version_(method, false, zip64);
190
+ const local = writeLocalHeader_({ ...shared, flags, version, method, crc32: 0, compressedSize: 0, size: 0 }, zip64);
191
+ let size = 0;
192
+ let crc32 = 0;
193
+ let compressedSize = 0;
194
+ const count = (chunk) => {
195
+ size += chunk.length;
196
+ if (declared !== undefined && size > declared) {
197
+ throw new RangeError(`ZIP entry "${entry.name}" passes its declared ${declared} bytes`);
198
+ }
199
+ if (!zip64 && needsZip64_(size)) {
200
+ throw new ZipLimitError(`ZIP entry "${entry.name}" passes 4 GiB without zip64`);
201
+ }
202
+ crc32 = crc_32(chunk, { continueFrom: crc32 });
203
+ return ownBuffer_(chunk);
204
+ };
205
+ let started = false;
206
+ try {
207
+ yield local;
208
+ started = true;
209
+ }
210
+ finally {
211
+ if (!started) {
212
+ await content.cancel(signal.reason).catch(() => { });
213
+ }
214
+ }
215
+ if (method === ZipMethod.STORED) {
216
+ let finished = false;
217
+ try {
218
+ for await (const chunk of readStreamChunks(content, signal)) {
219
+ yield count(chunk);
220
+ }
221
+ finished = true;
222
+ }
223
+ finally {
224
+ // The loop released its lock, however it ended.
225
+ if (!finished) {
226
+ await content.cancel(signal.reason).catch(() => { });
227
+ }
228
+ }
229
+ compressedSize = size;
230
+ }
231
+ else {
232
+ compressedSize = yield* deflate_(content, count, signal, (written) => {
233
+ if (!zip64 && needsZip64_(written)) {
234
+ throw new ZipLimitError(`ZIP entry "${entry.name}" deflates past 4 GiB without zip64`);
235
+ }
236
+ });
237
+ }
238
+ signal.throwIfAborted();
239
+ if (declared !== undefined && size !== declared) {
240
+ throw new RangeError(`ZIP entry "${entry.name}" ends at ${size} bytes, not its declared ${declared}`);
241
+ }
242
+ const descriptor = writeDataDescriptor_(crc32, compressedSize, size, zip64);
243
+ yield descriptor;
244
+ return {
245
+ fields: {
246
+ ...shared,
247
+ zip64Sizes: zip64,
248
+ flags,
249
+ version,
250
+ method,
251
+ crc32,
252
+ compressedSize,
253
+ size,
254
+ },
255
+ length: local.length + compressedSize + descriptor.length,
256
+ };
257
+ }
258
+ /**
259
+ * Deflates `content` through `CompressionStream`, passing each chunk to `count`
260
+ * on its way in, and yields the output; returns its length. The content is read
261
+ * by a pump that waits for each write, so it runs only as far ahead as the
262
+ * compressor's own buffering. Ending early (an error, a cancel, an abort)
263
+ * cancels the content directly, not through a pipe whose shutdown may not reach
264
+ * it.
265
+ */
266
+ async function* deflate_(content, count, signal, check) {
267
+ const compressor = new CompressionStream("deflate-raw");
268
+ const writer = compressor.writable.getWriter();
269
+ const stop = new AbortController();
270
+ const pumpSignal = AbortSignal.any([signal, stop.signal]);
271
+ let failure;
272
+ const pumping = (async () => {
273
+ try {
274
+ for await (const chunk of readStreamChunks(content, pumpSignal)) {
275
+ await writer.write(count(chunk));
276
+ }
277
+ pumpSignal.throwIfAborted();
278
+ await writer.close();
279
+ }
280
+ catch (error) {
281
+ failure ??= { error };
282
+ // The loop released its lock; the content is cancelled unless done.
283
+ await Promise.allSettled([content.cancel(error), writer.abort(error)]);
284
+ }
285
+ })();
286
+ let compressedSize = 0;
287
+ try {
288
+ for await (const chunk of readStreamChunks(compressor.readable, signal)) {
289
+ compressedSize += chunk.length;
290
+ check(compressedSize);
291
+ yield ownBuffer_(chunk);
292
+ }
293
+ await pumping;
294
+ }
295
+ catch (error) {
296
+ throw failure === undefined ? error : failure.error;
297
+ }
298
+ finally {
299
+ stop.abort(signal.reason);
300
+ await Promise.allSettled([
301
+ compressor.readable.cancel(signal.reason),
302
+ pumping,
303
+ ]);
304
+ }
305
+ if (failure !== undefined) {
306
+ throw failure.error;
307
+ }
308
+ return compressedSize;
309
+ }
310
+ function version_(method, isDirectory, zip64) {
311
+ if (zip64) {
312
+ return VERSION_ZIP64_;
313
+ }
314
+ return method === ZipMethod.STORED && !isDirectory
315
+ ? VERSION_STORED_
316
+ : VERSION_DEFLATE_OR_DIRECTORY_;
317
+ }
318
+ /**
319
+ * The most bytes deflate may turn `size` bytes into, zlib's `deflateBound`
320
+ * rounded up, without the zlib wrapper.
321
+ */
322
+ function deflateBound_(size) {
323
+ return (size +
324
+ Math.ceil(size / 4096) +
325
+ Math.ceil(size / 16384) +
326
+ Math.ceil(size / 33554432) +
327
+ 13);
328
+ }
329
+ /** Checks what can be checked before writing anything of `entry`. */
330
+ function checkEntry_(entry, names) {
331
+ const { name, content, size } = entry;
332
+ if (name === "" || name.startsWith("/") || name.includes("\\")) {
333
+ throw new TypeError(`Invalid ZIP entry name "${name}"`);
334
+ }
335
+ if (names.has(name)) {
336
+ throw new TypeError(`ZIP entry "${name}" given twice`);
337
+ }
338
+ const isDirectory = name.endsWith("/");
339
+ if (isDirectory &&
340
+ (content instanceof ReadableStream || (content?.length ?? 0) > 0)) {
341
+ throw new TypeError(`ZIP directory "${name}" with content`);
342
+ }
343
+ if (size !== undefined && (!Number.isSafeInteger(size) || size < 0)) {
344
+ throw new TypeError(`ZIP entry "${name}" with size ${size}`);
345
+ }
346
+ names.add(name);
347
+ const nameBytes = encodeTextUtf8(name);
348
+ const comment = entry.comment ?? "";
349
+ const commentBytes = encodeTextUtf8(comment);
350
+ checkLength_(nameBytes, `The name of "${name}"`);
351
+ checkLength_(commentBytes, `The comment of "${name}"`);
352
+ // UTF-8 is as long as UTF-16 only when every character is ASCII.
353
+ const isAscii = nameBytes.length === name.length && commentBytes.length === comment.length;
354
+ return {
355
+ flags: isAscii ? 0 : UTF8_FLAG_,
356
+ ...dosDateTime_(entry),
357
+ nameBytes,
358
+ commentBytes,
359
+ externalAttributes: isDirectory ? MS_DOS_DIRECTORY_ATTRIBUTE_ : 0,
360
+ };
361
+ }
362
+ function checkLength_(bytes, what) {
363
+ if (bytes.length > MAX_COMMENT_) {
364
+ throw new ZipLimitError(`${what} is ${bytes.length} bytes, past ${MAX_COMMENT_}`);
365
+ }
366
+ }
367
+ function encodeArchiveComment_(comment) {
368
+ const bytes = encodeTextUtf8(comment);
369
+ if (bytes.length !== comment.length) {
370
+ throw new TypeError("A ZIP archive's comment must be ASCII");
371
+ }
372
+ checkLength_(bytes, "The archive's comment");
373
+ return bytes;
374
+ }
375
+ async function compress_(entry, content, signal) {
376
+ // Deflate never shrinks nothing, so empty content is stored without trying.
377
+ if (entry.method === ZipMethod.STORED ||
378
+ entry.name.endsWith("/") ||
379
+ (entry.method === undefined && content.length === 0)) {
380
+ return { method: ZipMethod.STORED, data: content };
381
+ }
382
+ const input = new ReadableStream({
383
+ start(controller) {
384
+ controller.enqueue(content);
385
+ controller.close();
386
+ },
387
+ });
388
+ const data = await readStreamBytes(input.pipeThrough(new CompressionStream("deflate-raw")), Number.MAX_SAFE_INTEGER, signal);
389
+ if (entry.method === undefined && data.length >= content.length) {
390
+ return { method: ZipMethod.STORED, data: content };
391
+ }
392
+ return { method: ZipMethod.DEFLATE, data };
393
+ }
394
+ // APPNOTE 6.3 §4.4.6: the MS-DOS date and time, in local time.
395
+ function dosDateTime_(entry) {
396
+ const { lastModified } = entry;
397
+ if (lastModified === undefined) {
398
+ return { time: 0, date: FIRST_DOS_DATE_ };
399
+ }
400
+ const year = lastModified.getFullYear();
401
+ if (Number.isNaN(year)) {
402
+ throw new TypeError(`ZIP entry "${entry.name}" with an invalid date`);
403
+ }
404
+ if (year < FIRST_DOS_YEAR_) {
405
+ return { time: 0, date: FIRST_DOS_DATE_ };
406
+ }
407
+ if (year > LAST_DOS_YEAR_) {
408
+ return { time: LAST_DOS_TIME_, date: LAST_DOS_DATE_ };
409
+ }
410
+ return {
411
+ time: (lastModified.getHours() << 11) |
412
+ (lastModified.getMinutes() << 5) |
413
+ Math.floor(lastModified.getSeconds() / 2),
414
+ date: ((year - FIRST_DOS_YEAR_) << 9) |
415
+ ((lastModified.getMonth() + 1) << 5) |
416
+ lastModified.getDate(),
417
+ };
418
+ }
419
+ //# sourceMappingURL=zip-stream-writer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zip-stream-writer.js","sourceRoot":"","sources":["../src/zip-stream-writ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Ae,EAAE,CAAC;IAC3C,CAAC;IACD,IAAI,IAAI,GAAG,cAAc,EAAE,CAAC;QAC3B,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,IAAI,EAAE,cAAc,EAAE,CAAC;IACvD,CAAC;IAED,OAAO;QACN,IAAI,EACH,CAAC,YAAY,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC;YAC/B,CAAC,YAAY,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;YAChC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;QAC1C,IAAI,EACH,CAAC,CAAC,IAAI,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC;YAC/B,CAAC,CAAC,YAAY,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;YACpC,YAAY,CAAC,OAAO,EAAE;KACvB,CAAC;AACH,CAAC"}
@@ -0,0 +1,89 @@
1
+ import type { ZipMethod } from "./zip-method.js";
2
+ /** One entry of an archive {@link createZip} or {@link createZipStream} writes. */
3
+ export type ZipEntryInit = {
4
+ /**
5
+ * The path inside the archive, `/`-separated and relative: no leading `/`, no
6
+ * `\`. A directory's ends with `/` and has no content.
7
+ */
8
+ readonly name: string;
9
+ /**
10
+ * The uncompressed content; none for a directory. In memory, its CRC-32 and
11
+ * sizes go in the local header. A stream is read once, as the archive is
12
+ * read, and its CRC-32 and sizes go in a data descriptor after it (APPNOTE
13
+ * §4.3.9, general purpose bit 3).
14
+ */
15
+ readonly content?: Uint8Array<ArrayBuffer> | ReadableStream<Uint8Array>;
16
+ /**
17
+ * The uncompressed size, in bytes: checked against content in memory, and
18
+ * against a stream when it ends, which fails with `RangeError` as soon as it
19
+ * passes it. A stream with a known size gets ZIP64 sizes when it may need
20
+ * them.
21
+ */
22
+ readonly size?: number;
23
+ /**
24
+ * How the content is written. Absent, content in memory is deflated, and
25
+ * stored instead when deflating would not make it smaller, as zip tools do; a
26
+ * stream is deflated.
27
+ */
28
+ readonly method?: ZipMethod;
29
+ /**
30
+ * The modification time, written in the local time zone at a two-second
31
+ * resolution, between 1980 and 2107 (clamped to them). Default: 1980-01-01
32
+ * 00:00, so the same entries always make the same bytes.
33
+ */
34
+ readonly lastModified?: Date;
35
+ readonly comment?: string;
36
+ /**
37
+ * Writes the local header's sizes in a ZIP64 extra field, and a stream's data
38
+ * descriptor with 8-byte sizes, so a stream of unknown size may pass 4 GiB.
39
+ * Without it, a stream of unknown size past 4 GiB fails with
40
+ * {@link ZipLimitError}; a size known up front gets ZIP64 when it needs it
41
+ * either way, and the central directory and end records always do.
42
+ *
43
+ * Readers that walk local headers rather than the central directory (Java's
44
+ * `ZipInputStream`) may misread a small entry written so; Info-ZIP, Python's
45
+ * `zipfile`, 7-Zip and wabac.js read it.
46
+ */
47
+ readonly zip64?: boolean;
48
+ };
49
+ /** Optional settings of {@link createZip} and {@link createZipStream}. */
50
+ export type CreateZipOptions = {
51
+ /** The archive's comment, in ASCII: the format has no flag for its encoding. */
52
+ readonly comment?: string;
53
+ /**
54
+ * Aborting it stops the writing, between two entries or within one, and
55
+ * cancels the content stream being read.
56
+ */
57
+ readonly signal?: AbortSignal;
58
+ };
59
+ /**
60
+ * Writes a ZIP archive (APPNOTE 6.3) of `entries`, in their order, each stored
61
+ * or deflated with `CompressionStream`, and returns it whole: the bytes
62
+ * {@link createZipStream} streams. A name or comment holding anything but ASCII
63
+ * is written in UTF-8, flagged so. ZIP64 structures are written where a count,
64
+ * size or offset needs them, and only there, so an archive that needs none is
65
+ * readable by any tool.
66
+ *
67
+ * Reach for it to build an archive in memory; {@link openZip} reads one back.
68
+ * Time and memory are linear in the entries' total size.
69
+ *
70
+ * @example
71
+ * const bytes = await createZip([
72
+ * { name: "docs/" },
73
+ * { name: "docs/readme.txt", content: new TextEncoder().encode("Hello") },
74
+ * ]);
75
+ * openZip(bytes, limits).entries.map((entry) => entry.name);
76
+ * // ["docs/", "docs/readme.txt"]
77
+ *
78
+ * @throws {TypeError} When a name is empty, absolute, holds a `\`, appears
79
+ * twice, or names a directory with content, when a date is invalid, when a
80
+ * `size` differs from content in memory, or when the archive's comment is not
81
+ * ASCII.
82
+ * @throws {RangeError} When a stream's length differs from its `size`.
83
+ * @throws {ZipLimitError} When a name or comment is longer than 65 535 bytes,
84
+ * or a stream of unknown size without `zip64` passes 4 GiB.
85
+ * @throws When `options.signal` aborts, its reason; a content stream's own
86
+ * error.
87
+ */
88
+ export declare function createZip(entries: Iterable<ZipEntryInit> | AsyncIterable<ZipEntryInit>, options?: CreateZipOptions): Promise<Uint8Array<ArrayBuffer>>;
89
+ //# sourceMappingURL=zip-writer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zip-writer.d.ts","sourceRoot":"","sources":["../src/zip-writer.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAGjD,mFAAmF;AACnF,MAAM,MAAM,YAAY,GAAG;IAC1B;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;;;OAKG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,UAAU,CAAC,WAAW,CAAC,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;IACxE;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC;IAC5B;;;;OAIG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,IAAI,CAAC;IAC7B,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,0EAA0E;AAC1E,MAAM,MAAM,gBAAgB,GAAG;IAC9B,gFAAgF;IAChF,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,SAAS,CAC9B,OAAO,EAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,aAAa,CAAC,YAAY,CAAC,EAC7D,OAAO,CAAC,EAAE,gBAAgB,GACxB,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAMlC"}
@@ -0,0 +1,35 @@
1
+ import { readStreamBytes } from "@ac-kit/core";
2
+ import { createZipStream } from "./zip-stream-writer.js";
3
+ /**
4
+ * Writes a ZIP archive (APPNOTE 6.3) of `entries`, in their order, each stored
5
+ * or deflated with `CompressionStream`, and returns it whole: the bytes
6
+ * {@link createZipStream} streams. A name or comment holding anything but ASCII
7
+ * is written in UTF-8, flagged so. ZIP64 structures are written where a count,
8
+ * size or offset needs them, and only there, so an archive that needs none is
9
+ * readable by any tool.
10
+ *
11
+ * Reach for it to build an archive in memory; {@link openZip} reads one back.
12
+ * Time and memory are linear in the entries' total size.
13
+ *
14
+ * @example
15
+ * const bytes = await createZip([
16
+ * { name: "docs/" },
17
+ * { name: "docs/readme.txt", content: new TextEncoder().encode("Hello") },
18
+ * ]);
19
+ * openZip(bytes, limits).entries.map((entry) => entry.name);
20
+ * // ["docs/", "docs/readme.txt"]
21
+ *
22
+ * @throws {TypeError} When a name is empty, absolute, holds a `\`, appears
23
+ * twice, or names a directory with content, when a date is invalid, when a
24
+ * `size` differs from content in memory, or when the archive's comment is not
25
+ * ASCII.
26
+ * @throws {RangeError} When a stream's length differs from its `size`.
27
+ * @throws {ZipLimitError} When a name or comment is longer than 65 535 bytes,
28
+ * or a stream of unknown size without `zip64` passes 4 GiB.
29
+ * @throws When `options.signal` aborts, its reason; a content stream's own
30
+ * error.
31
+ */
32
+ export async function createZip(entries, options) {
33
+ return readStreamBytes(createZipStream(entries, options), Number.MAX_SAFE_INTEGER, options?.signal);
34
+ }
35
+ //# sourceMappingURL=zip-writer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zip-writer.js","sourceRoot":"","sources":["../src/zip-writer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAG/C,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AA6DzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC9B,OAA6D,EAC7D,OAA0B;IAE1B,OAAO,eAAe,CACrB,eAAe,CAAC,OAAO,EAAE,OAAO,CAAC,EACjC,MAAM,CAAC,gBAAgB,EACvB,OAAO,EAAE,MAAM,CACf,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@ac-kit/format-zip",
3
- "version": "0.1.2",
4
- "description": "ZIP archives: the central directory, ZIP64 included, and entries inflated on demand within limits.",
3
+ "version": "0.3.0",
4
+ "description": "ZIP archives: read from memory or by ranges, written whole or streamed, ZIP64 included, entries inflated on demand within limits.",
5
5
  "keywords": [
6
6
  "archive",
7
7
  "deflate",
8
+ "stream",
8
9
  "zip",
9
10
  "zip64"
10
11
  ],
@@ -44,10 +45,11 @@
44
45
  "prepack": "yarn build"
45
46
  },
46
47
  "dependencies": {
47
- "@ac-kit/core": "^0.8.0",
48
- "@ac-kit/noncrypto-hash": "^0.1.7"
48
+ "@ac-kit/core": "^0.10.0",
49
+ "@ac-kit/noncrypto-hash": "^0.2.0"
49
50
  },
50
51
  "devDependencies": {
52
+ "@ac-kit/math-random": "0.2.0",
51
53
  "@ac-kit/tsconfig": "0.1.0",
52
54
  "@ac-kit/typedoc-config": "0.1.0",
53
55
  "@ac-kit/vitest-config": "0.1.1",