@ac-kit/format-zip 0.2.0 → 0.3.1
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 +76 -0
- package/README.md +77 -19
- 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 +33 -0
- package/dist/_zip-format.d.ts.map +1 -1
- package/dist/_zip-format.js +33 -0
- package/dist/_zip-format.js.map +1 -1
- 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 +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/zip-archive.d.ts +41 -1
- package/dist/zip-archive.d.ts.map +1 -1
- package/dist/zip-archive.js +14 -197
- package/dist/zip-archive.js.map +1 -1
- package/dist/zip-error.d.ts +3 -2
- package/dist/zip-error.d.ts.map +1 -1
- package/dist/zip-error.js +3 -2
- package/dist/zip-error.js.map +1 -1
- 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 +52 -21
- package/dist/zip-writer.d.ts.map +1 -1
- package/dist/zip-writer.js +18 -206
- package/dist/zip-writer.js.map +1 -1
- package/package.json +6 -4
package/dist/zip-writer.js
CHANGED
|
@@ -1,31 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { CENTRAL_HEADER_, END_OF_CENTRAL_DIRECTORY_, LOCAL_HEADER_, UTF8_FLAG_, } from "./_zip-format.js";
|
|
4
|
-
import { ZipLimitError } from "./zip-error.js";
|
|
5
|
-
import { ZipMethod } from "./zip-method.js";
|
|
6
|
-
const VERSION_STORED_ = 10;
|
|
7
|
-
const VERSION_DEFLATE_OR_DIRECTORY_ = 20;
|
|
8
|
-
const MS_DOS_DIRECTORY_ATTRIBUTE_ = 0x10;
|
|
9
|
-
const MAX_UINT16_ = 0xffff;
|
|
10
|
-
// 0xffff entries in the end record is the ZIP64 sentinel (APPNOTE §4.4.21-22).
|
|
11
|
-
const MAX_ENTRIES_ = 0xfffe;
|
|
12
|
-
// A 32-bit size or offset holding 0xffffffff points to a ZIP64 field instead.
|
|
13
|
-
const MAX_UINT32_ = 0xfffffffe;
|
|
14
|
-
const FIRST_DOS_YEAR_ = 1980;
|
|
15
|
-
const LAST_DOS_YEAR_ = 2107;
|
|
16
|
-
// 1980-01-01, the earliest MS-DOS date: day 1, month 1, year 0.
|
|
17
|
-
const FIRST_DOS_DATE_ = 0x21;
|
|
18
|
-
const LAST_DOS_DATE_ = ((LAST_DOS_YEAR_ - FIRST_DOS_YEAR_) << 9) | (12 << 5) | 31;
|
|
19
|
-
// 23:59:58, the last MS-DOS time.
|
|
20
|
-
const LAST_DOS_TIME_ = (23 << 11) | (59 << 5) | 29;
|
|
1
|
+
import { readStreamBytes } from "@ac-kit/core";
|
|
2
|
+
import { createZipStream } from "./zip-stream-writer.js";
|
|
21
3
|
/**
|
|
22
4
|
* Writes a ZIP archive (APPNOTE 6.3) of `entries`, in their order, each stored
|
|
23
|
-
* or deflated with `CompressionStream`,
|
|
24
|
-
*
|
|
25
|
-
*
|
|
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.
|
|
26
10
|
*
|
|
27
|
-
* Reach for it to build an archive
|
|
28
|
-
*
|
|
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.
|
|
29
13
|
*
|
|
30
14
|
* @example
|
|
31
15
|
* const bytes = await createZip([
|
|
@@ -36,188 +20,16 @@ const LAST_DOS_TIME_ = (23 << 11) | (59 << 5) | 29;
|
|
|
36
20
|
* // ["docs/", "docs/readme.txt"]
|
|
37
21
|
*
|
|
38
22
|
* @throws {TypeError} When a name is empty, absolute, holds a `\`, appears
|
|
39
|
-
* twice, or names a directory with content, when a date is invalid,
|
|
40
|
-
* the archive's comment is not
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* @throws When `options.signal` aborts, its reason
|
|
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.
|
|
46
31
|
*/
|
|
47
32
|
export async function createZip(entries, options) {
|
|
48
|
-
|
|
49
|
-
const archiveComment = encodeArchiveComment_(options?.comment ?? "");
|
|
50
|
-
const names = new Set();
|
|
51
|
-
const chunks = [];
|
|
52
|
-
const centralHeaders = [];
|
|
53
|
-
let offset = 0;
|
|
54
|
-
for (const entry of entries) {
|
|
55
|
-
signal?.throwIfAborted();
|
|
56
|
-
checkName_(entry, names);
|
|
57
|
-
if (names.size > MAX_ENTRIES_) {
|
|
58
|
-
throw new ZipLimitError(`The archive holds more than the ${MAX_ENTRIES_} entries of a ZIP without ZIP64`);
|
|
59
|
-
}
|
|
60
|
-
const content = entry.content ?? new Uint8Array(0);
|
|
61
|
-
const comment = entry.comment ?? "";
|
|
62
|
-
const nameBytes = encodeTextUtf8(entry.name);
|
|
63
|
-
const commentBytes = encodeTextUtf8(comment);
|
|
64
|
-
checkLength_(nameBytes, MAX_UINT16_, `The name of "${entry.name}"`);
|
|
65
|
-
checkLength_(commentBytes, MAX_UINT16_, `The comment of "${entry.name}"`);
|
|
66
|
-
checkLength_(content, MAX_UINT32_, `ZIP entry "${entry.name}"`);
|
|
67
|
-
const dosTime = dosDateTime_(entry);
|
|
68
|
-
const { method, data } = await compress_(entry, content, signal);
|
|
69
|
-
const isDirectory = entry.name.endsWith("/");
|
|
70
|
-
// UTF-8 is as long as UTF-16 only when every character is ASCII.
|
|
71
|
-
const isAscii = nameBytes.length === entry.name.length &&
|
|
72
|
-
commentBytes.length === comment.length;
|
|
73
|
-
const shared = {
|
|
74
|
-
version: method === ZipMethod.STORED && !isDirectory
|
|
75
|
-
? VERSION_STORED_
|
|
76
|
-
: VERSION_DEFLATE_OR_DIRECTORY_,
|
|
77
|
-
flags: isAscii ? 0 : UTF8_FLAG_,
|
|
78
|
-
method,
|
|
79
|
-
...dosTime,
|
|
80
|
-
crc32: crc_32(content),
|
|
81
|
-
compressedSize: data.length,
|
|
82
|
-
size: content.length,
|
|
83
|
-
nameLength: nameBytes.length,
|
|
84
|
-
};
|
|
85
|
-
const local = writeLocalHeader_(shared);
|
|
86
|
-
centralHeaders.push(writeCentralHeader_({
|
|
87
|
-
...shared,
|
|
88
|
-
commentLength: commentBytes.length,
|
|
89
|
-
externalAttributes: isDirectory ? MS_DOS_DIRECTORY_ATTRIBUTE_ : 0,
|
|
90
|
-
offset,
|
|
91
|
-
}), nameBytes, commentBytes);
|
|
92
|
-
chunks.push(local, nameBytes, data);
|
|
93
|
-
offset += local.length + nameBytes.length + data.length;
|
|
94
|
-
if (offset > MAX_UINT32_) {
|
|
95
|
-
throw new ZipLimitError("The archive needs ZIP64, past 4 GiB");
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
const directory = concatBytes(centralHeaders);
|
|
99
|
-
if (offset + directory.length > MAX_UINT32_) {
|
|
100
|
-
throw new ZipLimitError("The archive needs ZIP64, past 4 GiB");
|
|
101
|
-
}
|
|
102
|
-
const end = new Uint8Array(22);
|
|
103
|
-
const view = new DataView(end.buffer);
|
|
104
|
-
view.setUint32(0, END_OF_CENTRAL_DIRECTORY_, true);
|
|
105
|
-
view.setUint16(8, names.size, true);
|
|
106
|
-
view.setUint16(10, names.size, true);
|
|
107
|
-
view.setUint32(12, directory.length, true);
|
|
108
|
-
view.setUint32(16, offset, true);
|
|
109
|
-
view.setUint16(20, archiveComment.length, true);
|
|
110
|
-
chunks.push(directory, end, archiveComment);
|
|
111
|
-
return concatBytes(chunks);
|
|
112
|
-
}
|
|
113
|
-
// APPNOTE 6.3 §4.3.7.
|
|
114
|
-
function writeLocalHeader_(fields) {
|
|
115
|
-
const header = new Uint8Array(30);
|
|
116
|
-
const view = new DataView(header.buffer);
|
|
117
|
-
view.setUint32(0, LOCAL_HEADER_, true);
|
|
118
|
-
view.setUint16(4, fields.version, true);
|
|
119
|
-
writeSharedFields_(view, 6, fields);
|
|
120
|
-
return header;
|
|
121
|
-
}
|
|
122
|
-
// APPNOTE 6.3 §4.3.12.
|
|
123
|
-
function writeCentralHeader_(fields) {
|
|
124
|
-
const header = new Uint8Array(46);
|
|
125
|
-
const view = new DataView(header.buffer);
|
|
126
|
-
view.setUint32(0, CENTRAL_HEADER_, true);
|
|
127
|
-
// Version made by: MS-DOS attributes, APPNOTE 2.0.
|
|
128
|
-
view.setUint16(4, VERSION_DEFLATE_OR_DIRECTORY_, true);
|
|
129
|
-
view.setUint16(6, fields.version, true);
|
|
130
|
-
writeSharedFields_(view, 8, fields);
|
|
131
|
-
view.setUint16(32, fields.commentLength, true);
|
|
132
|
-
view.setUint32(38, fields.externalAttributes, true);
|
|
133
|
-
view.setUint32(42, fields.offset, true);
|
|
134
|
-
return header;
|
|
135
|
-
}
|
|
136
|
-
// The run of fields both headers carry, from the flags to the extra length.
|
|
137
|
-
function writeSharedFields_(view, start, fields) {
|
|
138
|
-
view.setUint16(start, fields.flags, true);
|
|
139
|
-
view.setUint16(start + 2, fields.method, true);
|
|
140
|
-
view.setUint16(start + 4, fields.time, true);
|
|
141
|
-
view.setUint16(start + 6, fields.date, true);
|
|
142
|
-
view.setUint32(start + 8, fields.crc32, true);
|
|
143
|
-
view.setUint32(start + 12, fields.compressedSize, true);
|
|
144
|
-
view.setUint32(start + 16, fields.size, true);
|
|
145
|
-
view.setUint16(start + 20, fields.nameLength, true);
|
|
146
|
-
}
|
|
147
|
-
function checkName_(entry, names) {
|
|
148
|
-
const { name } = entry;
|
|
149
|
-
if (name === "" || name.startsWith("/") || name.includes("\\")) {
|
|
150
|
-
throw new TypeError(`Invalid ZIP entry name "${name}"`);
|
|
151
|
-
}
|
|
152
|
-
if (names.has(name)) {
|
|
153
|
-
throw new TypeError(`ZIP entry "${name}" given twice`);
|
|
154
|
-
}
|
|
155
|
-
if (name.endsWith("/") && (entry.content?.length ?? 0) > 0) {
|
|
156
|
-
throw new TypeError(`ZIP directory "${name}" with content`);
|
|
157
|
-
}
|
|
158
|
-
names.add(name);
|
|
159
|
-
}
|
|
160
|
-
function checkLength_(bytes, max, what) {
|
|
161
|
-
if (bytes.length > max) {
|
|
162
|
-
throw new ZipLimitError(`${what} is ${bytes.length} bytes, past ${max}`);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
function encodeArchiveComment_(comment) {
|
|
166
|
-
const bytes = encodeTextUtf8(comment);
|
|
167
|
-
if (bytes.length !== comment.length) {
|
|
168
|
-
throw new TypeError("A ZIP archive's comment must be ASCII");
|
|
169
|
-
}
|
|
170
|
-
checkLength_(bytes, MAX_UINT16_, "The archive's comment");
|
|
171
|
-
return bytes;
|
|
172
|
-
}
|
|
173
|
-
async function compress_(entry, content, signal) {
|
|
174
|
-
if (entry.method === ZipMethod.STORED || entry.name.endsWith("/")) {
|
|
175
|
-
return { method: ZipMethod.STORED, data: content };
|
|
176
|
-
}
|
|
177
|
-
const input = new ReadableStream({
|
|
178
|
-
start(controller) {
|
|
179
|
-
controller.enqueue(content);
|
|
180
|
-
controller.close();
|
|
181
|
-
},
|
|
182
|
-
});
|
|
183
|
-
let data;
|
|
184
|
-
try {
|
|
185
|
-
data = await readStreamBytes(input.pipeThrough(new CompressionStream("deflate-raw")), MAX_UINT32_, signal);
|
|
186
|
-
}
|
|
187
|
-
catch (error) {
|
|
188
|
-
if (error instanceof StreamLengthError) {
|
|
189
|
-
throw new ZipLimitError(`ZIP entry "${entry.name}" deflates past 4 GiB`);
|
|
190
|
-
}
|
|
191
|
-
throw error;
|
|
192
|
-
}
|
|
193
|
-
if (entry.method === undefined && data.length >= content.length) {
|
|
194
|
-
return { method: ZipMethod.STORED, data: content };
|
|
195
|
-
}
|
|
196
|
-
return { method: ZipMethod.DEFLATE, data };
|
|
197
|
-
}
|
|
198
|
-
// APPNOTE 6.3 §4.4.6: the MS-DOS date and time, in local time.
|
|
199
|
-
function dosDateTime_(entry) {
|
|
200
|
-
const { lastModified } = entry;
|
|
201
|
-
if (lastModified === undefined) {
|
|
202
|
-
return { time: 0, date: FIRST_DOS_DATE_ };
|
|
203
|
-
}
|
|
204
|
-
const year = lastModified.getFullYear();
|
|
205
|
-
if (Number.isNaN(year)) {
|
|
206
|
-
throw new TypeError(`ZIP entry "${entry.name}" with an invalid date`);
|
|
207
|
-
}
|
|
208
|
-
if (year < FIRST_DOS_YEAR_) {
|
|
209
|
-
return { time: 0, date: FIRST_DOS_DATE_ };
|
|
210
|
-
}
|
|
211
|
-
if (year > LAST_DOS_YEAR_) {
|
|
212
|
-
return { time: LAST_DOS_TIME_, date: LAST_DOS_DATE_ };
|
|
213
|
-
}
|
|
214
|
-
return {
|
|
215
|
-
time: (lastModified.getHours() << 11) |
|
|
216
|
-
(lastModified.getMinutes() << 5) |
|
|
217
|
-
Math.floor(lastModified.getSeconds() / 2),
|
|
218
|
-
date: ((year - FIRST_DOS_YEAR_) << 9) |
|
|
219
|
-
((lastModified.getMonth() + 1) << 5) |
|
|
220
|
-
lastModified.getDate(),
|
|
221
|
-
};
|
|
33
|
+
return readStreamBytes(createZipStream(entries, options), Number.MAX_SAFE_INTEGER, options?.signal);
|
|
222
34
|
}
|
|
223
35
|
//# sourceMappingURL=zip-writer.js.map
|
package/dist/zip-writer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zip-writer.js","sourceRoot":"","sources":["../src/zip-writer.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
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.
|
|
4
|
-
"description": "ZIP archives:
|
|
3
|
+
"version": "0.3.1",
|
|
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.
|
|
48
|
-
"@ac-kit/noncrypto-hash": "^0.1
|
|
48
|
+
"@ac-kit/core": "^0.11.0",
|
|
49
|
+
"@ac-kit/noncrypto-hash": "^0.2.1"
|
|
49
50
|
},
|
|
50
51
|
"devDependencies": {
|
|
52
|
+
"@ac-kit/math-random": "0.3.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",
|