@preventive/archive 1.0.0-beta.0 → 1.0.0-beta.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/package.json +1 -1
- package/src/zip/unzip.js +118 -34
- package/zip.d.ts +13 -1
- package/zip.js +5 -0
package/package.json
CHANGED
package/src/zip/unzip.js
CHANGED
|
@@ -10,6 +10,13 @@
|
|
|
10
10
|
// it; since each has to come out at exactly its own, that bounds everything
|
|
11
11
|
// unzip() puts out, and it is known from the central directory alone,
|
|
12
12
|
// before a single byte is inflated.
|
|
13
|
+
//
|
|
14
|
+
// One reader serves both calls. It asks for the archive a range at a time:
|
|
15
|
+
// views of it when it is in memory, and reads of just those bytes when it
|
|
16
|
+
// is a Blob — a File, or a file opened with fs.openAsBlob — so a stream
|
|
17
|
+
// over one holds no more of it than the entries' names and the entry it is
|
|
18
|
+
// on. Everything about the layout is checked before the first entry is
|
|
19
|
+
// handed over; what an entry's own data holds is checked as it is reached.
|
|
13
20
|
|
|
14
21
|
import { crc32 } from '@exodus/bytes/crc.js'
|
|
15
22
|
import { EMPTY, sameBytes } from '../bytes.js'
|
|
@@ -31,26 +38,69 @@ const ENCRYPTED = 1
|
|
|
31
38
|
const DESCRIBED = 8 // sizes and CRC follow the data, and may be 0 in the local header
|
|
32
39
|
const DOS_DIRECTORY = 0x10
|
|
33
40
|
|
|
34
|
-
// Bounds-checked little-endian reads over
|
|
35
|
-
|
|
41
|
+
// Bounds-checked little-endian reads over `bytes`, which sit at `base` in
|
|
42
|
+
// the archive: offsets are the archive's own, and one past what was read,
|
|
43
|
+
// which stops short only at the archive's end, is past its end.
|
|
44
|
+
function reader(bytes, base = 0) {
|
|
36
45
|
const data = view(bytes)
|
|
37
46
|
const check = (at, width) => {
|
|
38
|
-
if (at <
|
|
47
|
+
if (at < base || at + width > base + bytes.length) throw new ArchiveError('the archive is truncated', at)
|
|
39
48
|
}
|
|
40
49
|
return {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
50
|
+
u16: (at) => (check(at, 2), data.getUint16(at - base, true)),
|
|
51
|
+
u32: (at) => (check(at, 4), data.getUint32(at - base, true)),
|
|
52
|
+
slice: (at, width) => (check(at, width), bytes.subarray(at - base, at - base + width)),
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Where the archive's bytes come from, a range at a time: `read` for the
|
|
57
|
+
// bytes themselves, `window` for a reader over them, cut short where the
|
|
58
|
+
// archive ends. In memory, a range is a view and every window is the one
|
|
59
|
+
// reader over the whole; a Blob reads each range as it is asked for.
|
|
60
|
+
function sourceOf(archive) {
|
|
61
|
+
if (archive instanceof Uint8Array) {
|
|
62
|
+
const whole = reader(archive)
|
|
63
|
+
return { size: archive.length, read: (from, to) => archive.subarray(from, to), window: () => whole }
|
|
64
|
+
}
|
|
65
|
+
if (archive instanceof Blob) {
|
|
66
|
+
const read = async (from, to) => new Uint8Array(await archive.slice(from, to).arrayBuffer())
|
|
67
|
+
return { size: archive.size, read, window: async (from, to) => reader(await read(from, Math.min(to, archive.size)), from) }
|
|
68
|
+
}
|
|
69
|
+
throw new ArchiveError('the archive is not a Uint8Array or a Blob')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// A walk forward through records too small to be worth a read each: a
|
|
73
|
+
// window over [at, at + width), cut short only where the archive ends,
|
|
74
|
+
// and over what else the read it came from holds — up to CHUNK bytes past
|
|
75
|
+
// `at`, and short of `stop`, so that the records after it come in the same
|
|
76
|
+
// read and nothing past them is read to no purpose.
|
|
77
|
+
const CHUNK = 1 << 16
|
|
78
|
+
|
|
79
|
+
function walk(source) {
|
|
80
|
+
let r
|
|
81
|
+
let from = 0
|
|
82
|
+
let to = 0
|
|
83
|
+
return async (at, width, stop = Infinity) => {
|
|
84
|
+
const need = Math.min(at + width, source.size)
|
|
85
|
+
if (r === undefined || at < from || need > to) {
|
|
86
|
+
from = at
|
|
87
|
+
to = Math.max(need, Math.min(at + CHUNK, stop, source.size))
|
|
88
|
+
r = await source.window(from, to)
|
|
89
|
+
}
|
|
90
|
+
return r
|
|
45
91
|
}
|
|
46
92
|
}
|
|
47
93
|
|
|
48
94
|
// The end record is the last 22 bytes plus its comment, and nothing after.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
95
|
+
// Read with the 20 bytes ahead of the furthest back it can start, where a
|
|
96
|
+
// zip64 locator would sit.
|
|
97
|
+
async function findEnd(source) {
|
|
98
|
+
const { size } = source
|
|
99
|
+
const r = await source.window(Math.max(0, size - 22 - 0xffff - 20), size)
|
|
100
|
+
for (let at = size - 22; at >= Math.max(0, size - 22 - 0xffff); at--) {
|
|
101
|
+
if (r.u32(at) === END && at + 22 + r.u16(at + 20) === size) {
|
|
52
102
|
if (at >= 20 && r.u32(at - 20) === ZIP64_LOCATOR) throw new ArchiveError('zip64 is not supported', at - 20)
|
|
53
|
-
return at
|
|
103
|
+
return { end: at, r }
|
|
54
104
|
}
|
|
55
105
|
}
|
|
56
106
|
throw new ArchiveError('no end of central directory record')
|
|
@@ -74,10 +124,12 @@ function extras(raw, at) {
|
|
|
74
124
|
return fields
|
|
75
125
|
}
|
|
76
126
|
|
|
77
|
-
function readCentral(
|
|
127
|
+
async function readCentral(read, at) {
|
|
128
|
+
let r = await read(at, 46)
|
|
78
129
|
if (r.u32(at) !== CENTRAL) throw new ArchiveError('no central directory entry where one is counted', at)
|
|
79
130
|
const nameLength = r.u16(at + 28)
|
|
80
131
|
const extraLength = r.u16(at + 30)
|
|
132
|
+
r = await read(at, 46 + nameLength + extraLength)
|
|
81
133
|
const entry = {
|
|
82
134
|
at,
|
|
83
135
|
madeBy: r.u16(at + 4),
|
|
@@ -91,7 +143,8 @@ function readCentral(r, at) {
|
|
|
91
143
|
usize: r.u32(at + 24),
|
|
92
144
|
attributes: r.u32(at + 38),
|
|
93
145
|
offset: r.u32(at + 42),
|
|
94
|
-
|
|
146
|
+
// A copy, so that no entry holds on to the chunk it was read from.
|
|
147
|
+
name: r.slice(at + 46, nameLength).slice(),
|
|
95
148
|
next: at + 46 + nameLength + extraLength + r.u16(at + 32),
|
|
96
149
|
}
|
|
97
150
|
if (r.u16(at + 34) !== 0) throw new ArchiveError('the archive spans several disks', at)
|
|
@@ -111,15 +164,18 @@ function readCentral(r, at) {
|
|
|
111
164
|
|
|
112
165
|
// The local header checked against the central entry; returns where the
|
|
113
166
|
// entry's bytes end, which has to be `boundary`, the next record's start,
|
|
114
|
-
// and notes where its data starts. The two records have
|
|
115
|
-
//
|
|
116
|
-
//
|
|
117
|
-
//
|
|
118
|
-
|
|
167
|
+
// and notes where its data starts. The two records have to agree on
|
|
168
|
+
// everything both carry — flags included, since a descriptor bit set in
|
|
169
|
+
// one and not the other is read two ways by two readers — and the local
|
|
170
|
+
// sizes and CRC may be 0 only where a descriptor carries them. `stop` is
|
|
171
|
+
// as far as the header is read ahead of itself.
|
|
172
|
+
async function readLocal(read, entry, boundary, stop) {
|
|
119
173
|
const at = entry.offset
|
|
174
|
+
let r = await read(at, 30, stop)
|
|
120
175
|
if (r.u32(at) !== LOCAL) throw new ArchiveError('no local header where the central directory points', at)
|
|
121
176
|
const nameLength = r.u16(at + 26)
|
|
122
177
|
const extraLength = r.u16(at + 28)
|
|
178
|
+
r = await read(at, 30 + nameLength + extraLength, stop)
|
|
123
179
|
if (!sameBytes(r.slice(at + 30, nameLength), entry.name)) throw new ArchiveError('the local header names a different entry', at)
|
|
124
180
|
const shared = [[4, 'version', 'a different version'], [6, 'flags', 'different flags'], [8, 'method', 'a different compression method'], [10, 'time', 'a different time'], [12, 'date', 'a different date']]
|
|
125
181
|
for (const [field, key, what] of shared) {
|
|
@@ -135,8 +191,9 @@ function readLocal(r, entry, boundary) {
|
|
|
135
191
|
// The descriptor may start with its signature or not, and a CRC can be
|
|
136
192
|
// that very value, so both layouts are tried against the record; where
|
|
137
193
|
// both fit (every word the signature), the one reaching the boundary is it.
|
|
138
|
-
const
|
|
139
|
-
const
|
|
194
|
+
const d = await read(end, 16, boundary)
|
|
195
|
+
const matches = (from) => d.u32(from) === entry.crc && d.u32(from + 4) === entry.csize && d.u32(from + 8) === entry.usize
|
|
196
|
+
const signed = d.u32(end) === DESCRIPTOR && matches(end + 4)
|
|
140
197
|
if (matches(end) && !(signed && end + 16 === boundary)) end += 12
|
|
141
198
|
else if (signed) end += 16
|
|
142
199
|
else throw new ArchiveError('the data descriptor disagrees with the central directory', end)
|
|
@@ -160,7 +217,7 @@ function typeOf(entry, rawName, mode) {
|
|
|
160
217
|
throw new ArchiveError(`the mode of ${quote(rawName)} (${mode.toString(8)}) is not one this package reads`, entry.at)
|
|
161
218
|
}
|
|
162
219
|
|
|
163
|
-
async function entryOf(
|
|
220
|
+
async function entryOf(source, entry, names) {
|
|
164
221
|
const { at, method, csize, usize } = entry
|
|
165
222
|
const rawName = decodeUtf8(entry.name, 'entry name', at)
|
|
166
223
|
// Only a Unix maker's attributes carry a mode.
|
|
@@ -168,7 +225,11 @@ async function entryOf(r, entry, names) {
|
|
|
168
225
|
const type = typeOf(entry, rawName, mode)
|
|
169
226
|
if (type === 'directory' && usize !== 0) throw new ArchiveError(`directory ${quote(rawName)} has data`, at)
|
|
170
227
|
if (method === 0 && csize !== usize) throw new ArchiveError('a stored entry has two sizes', at)
|
|
171
|
-
|
|
228
|
+
// A Blob can come back short if what is behind it changed since the
|
|
229
|
+
// layout was checked; the bytes checked are the bytes read, or nothing.
|
|
230
|
+
const raw = await source.read(entry.dataAt, entry.dataAt + csize)
|
|
231
|
+
if (raw.length !== csize) throw new ArchiveError('the archive is truncated', entry.dataAt)
|
|
232
|
+
const body = method === 8 ? await inflate(raw, usize, at) : raw
|
|
172
233
|
if (crc32(body) !== entry.crc) throw new ArchiveError('the data does not match its CRC-32', at)
|
|
173
234
|
const target = type === 'symlink' ? decodeUtf8(body, 'symlink target', at) : ''
|
|
174
235
|
const named = located(() => cleanNames(rawName, type, target), at)
|
|
@@ -177,37 +238,60 @@ async function entryOf(r, entry, names) {
|
|
|
177
238
|
return out
|
|
178
239
|
}
|
|
179
240
|
|
|
180
|
-
|
|
181
|
-
|
|
241
|
+
// The entries one at a time, the layout all checked before the first.
|
|
242
|
+
// `keep` holds entries for a repeat to be compared with, which only the
|
|
243
|
+
// in-memory call can afford.
|
|
244
|
+
async function* entries(source, { limit = Infinity } = {}, keep = false) {
|
|
182
245
|
if (limit !== Infinity && !(Number.isSafeInteger(limit) && limit >= 0)) throw new ArchiveError(`limit ${String(limit)} is not a whole number of bytes`)
|
|
183
|
-
const r =
|
|
184
|
-
const end = findEnd(r)
|
|
246
|
+
const { end, r } = await findEnd(source)
|
|
185
247
|
const count = r.u16(end + 8)
|
|
186
248
|
if (r.u16(end + 4) !== 0 || r.u16(end + 6) !== 0 || count !== r.u16(end + 10)) throw new ArchiveError('the archive spans several disks', end)
|
|
187
249
|
const size = r.u32(end + 12)
|
|
188
250
|
const start = r.u32(end + 16)
|
|
189
251
|
if (count === 0xffff || size === 0xffffffff || start === 0xffffffff) throw new ArchiveError('zip64 is not supported', end)
|
|
190
252
|
if (start + size !== end) throw new ArchiveError('the central directory does not end at the end record', end)
|
|
191
|
-
|
|
253
|
+
// A chunk at a time, not the whole of a directory that can near 4 GiB,
|
|
254
|
+
// and cut only at the archive's end, so a record counted past the
|
|
255
|
+
// directory meets the end record, as it would in the archive.
|
|
256
|
+
const directory = walk(source)
|
|
257
|
+
const list = []
|
|
192
258
|
let pos = start
|
|
193
259
|
let total = 0
|
|
194
260
|
for (let i = 0; i < count; i++) {
|
|
195
|
-
const entry = readCentral(
|
|
261
|
+
const entry = await readCentral(directory, pos)
|
|
196
262
|
total += entry.usize
|
|
197
263
|
if (total > limit) throw new ArchiveError(`the entries come to more than ${limit} bytes`, pos)
|
|
198
|
-
|
|
264
|
+
list.push(entry)
|
|
199
265
|
pos = entry.next
|
|
200
266
|
}
|
|
201
267
|
if (pos !== end) throw new ArchiveError('the central directory does not hold what the end record counts', pos)
|
|
202
268
|
let expected = 0
|
|
203
|
-
const sorted =
|
|
269
|
+
const sorted = list.toSorted((a, b) => a.offset - b.offset)
|
|
270
|
+
const boundaries = sorted.map((_, i) => sorted[i + 1]?.offset ?? start)
|
|
271
|
+
// The headers are read ahead through, but never the data between them,
|
|
272
|
+
// which is read again when its entry is reached: from each header to its
|
|
273
|
+
// own data, give or take a descriptor, by the directory's sizes, or on
|
|
274
|
+
// through the next header where it has none.
|
|
275
|
+
const stops = []
|
|
276
|
+
for (let i = sorted.length - 1; i >= 0; i--) {
|
|
277
|
+
stops[i] = sorted[i].csize === 0 && i + 1 < sorted.length ? stops[i + 1] : boundaries[i] - sorted[i].csize
|
|
278
|
+
}
|
|
279
|
+
const locals = walk(source)
|
|
204
280
|
for (const [i, entry] of sorted.entries()) {
|
|
205
281
|
if (entry.offset !== expected) throw new ArchiveError(entry.offset > expected ? 'bytes belong to no entry' : 'two entries overlap', expected)
|
|
206
|
-
expected = readLocal(
|
|
282
|
+
expected = await readLocal(locals, entry, boundaries[i], stops[i])
|
|
207
283
|
}
|
|
208
284
|
if (expected !== start) throw new ArchiveError('bytes belong to no entry', expected)
|
|
209
|
-
const names = new Names(
|
|
210
|
-
const
|
|
211
|
-
|
|
285
|
+
const names = new Names(keep)
|
|
286
|
+
for (const entry of list) yield await entryOf(source, entry, names)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export async function unzip(bytes, options) {
|
|
290
|
+
if (!(bytes instanceof Uint8Array)) throw new ArchiveError('the archive is not a Uint8Array')
|
|
291
|
+
const out = await Array.fromAsync(entries(sourceOf(bytes), options, true))
|
|
212
292
|
return out
|
|
213
293
|
}
|
|
294
|
+
|
|
295
|
+
export async function* unzipStream(archive, options) {
|
|
296
|
+
yield* entries(sourceOf(archive), options)
|
|
297
|
+
}
|
package/zip.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export type EntryType = 'file' | 'directory' | 'symlink'
|
|
|
13
13
|
// carries an extended timestamp and DOS time read as UTC otherwise;
|
|
14
14
|
// `linkname` is '' for anything but a symlink; `data` is empty for
|
|
15
15
|
// anything but a file, and a view over the archive bytes where the entry
|
|
16
|
-
// was stored.
|
|
16
|
+
// was stored and the archive is in memory rather than a Blob.
|
|
17
17
|
export interface Entry {
|
|
18
18
|
name: string
|
|
19
19
|
type: EntryType
|
|
@@ -64,6 +64,18 @@ export interface UnzipOptions {
|
|
|
64
64
|
export function zip(entries: Iterable<EntryInput>, options?: ZipOptions): Promise<Uint8Array>
|
|
65
65
|
export function unzip(bytes: Uint8Array, options?: UnzipOptions): Promise<Entry[]>
|
|
66
66
|
|
|
67
|
+
// The same one entry at a time, as an async generator: over the archive in
|
|
68
|
+
// memory, or over a Blob — a File, or a file opened with fs.openAsBlob —
|
|
69
|
+
// read a range at a time, so that no more of it is held than the entries'
|
|
70
|
+
// names and the entry it is on, whose data is then its own rather than a
|
|
71
|
+
// view of the archive.
|
|
72
|
+
// The layout is checked whole before the first entry comes out; what an
|
|
73
|
+
// entry holds is checked as it is reached, so a bad one ends the stream
|
|
74
|
+
// after the ones before it. A stream keeps no entry's data, so a name that
|
|
75
|
+
// repeats with the same fields is refused there, and such an archive is
|
|
76
|
+
// for the in-memory call.
|
|
77
|
+
export function unzipStream(archive: Uint8Array | Blob, options?: UnzipOptions): AsyncGenerator<Entry, void, undefined>
|
|
78
|
+
|
|
67
79
|
// `offset` is where in the archive the reader gave up; unset from the writer.
|
|
68
80
|
export class ArchiveError extends Error {
|
|
69
81
|
constructor(detail: string, offset?: number)
|
package/zip.js
CHANGED
|
@@ -13,4 +13,9 @@ export { zip } from './src/zip/zip.js'
|
|
|
13
13
|
// sizes all together held to a limit where one is given.
|
|
14
14
|
export { unzip } from './src/zip/unzip.js'
|
|
15
15
|
|
|
16
|
+
// The same an entry at a time, as an async generator over the archive in
|
|
17
|
+
// memory or over a Blob — a File, or a file opened with fs.openAsBlob —
|
|
18
|
+
// read a range at a time, so no more of it is held than the entry it is on.
|
|
19
|
+
export { unzipStream } from './src/zip/unzip.js'
|
|
20
|
+
|
|
16
21
|
export { ArchiveError } from './src/error.js'
|