@preventive/archive 1.0.0-beta.0 → 1.0.0-beta.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preventive/archive",
3
- "version": "1.0.0-beta.0",
3
+ "version": "1.0.0-beta.2",
4
4
  "description": "Strict in-memory tar and zip on browser APIs",
5
5
  "type": "module",
6
6
  "exports": {
package/src/names.js CHANGED
@@ -11,7 +11,8 @@
11
11
  // chain of hard links, is a second name for it, and is held to the same walk
12
12
  // from where that name sits, since a target safe under `a/b/` need not be
13
13
  // safe at the root. Lengths are bounded by what a filesystem takes at all:
14
- // PATH_MAX for the whole, NAME_MAX for a segment, in bytes.
14
+ // PATH_MAX for the whole as the archive stores it, a directory's slash
15
+ // counted, and NAME_MAX for a segment, in bytes.
15
16
  //
16
17
  // A name may repeat only as the same entry again, field for field and byte
17
18
  // for byte (some npm packagers write `d/f` and `d/./f` both): two different
@@ -45,10 +46,13 @@ function checkText(path, what) {
45
46
  }
46
47
 
47
48
  // What is left of `.` or `./` is the archive root, which only a directory
48
- // may name; it comes back as `.`.
49
+ // may name; it comes back as `.`. A path with nothing to drop comes back as
50
+ // the very string it came in as, so the name an archive stores and the one
51
+ // cleaned out of it are one string where they are one path.
49
52
  export function cleanPath(path, what, directory = false) {
50
53
  const segments = checkText(path, what)
51
- if (segments.at(-1) === '') {
54
+ const slash = segments.at(-1) === ''
55
+ if (slash) {
52
56
  if (!directory) throw new ArchiveError(`${what} ${quote(path)} ends in a slash but is not a directory`)
53
57
  segments.pop()
54
58
  }
@@ -57,9 +61,15 @@ export function cleanPath(path, what, directory = false) {
57
61
  if (segment === '') throw new ArchiveError(`${what} ${quote(path)} has an empty segment`)
58
62
  if (segment === '..') throw new ArchiveError(`${what} ${quote(path)} has a .. segment`)
59
63
  }
60
- if (kept.length) return kept.join('/')
61
- if (!directory) throw new ArchiveError(`${what} ${quote(path)} names the archive root but is not a directory`)
62
- return '.'
64
+ if (kept.length === 0) {
65
+ if (!directory) throw new ArchiveError(`${what} ${quote(path)} names the archive root but is not a directory`)
66
+ return '.'
67
+ }
68
+ const name = slash || kept.length < segments.length ? kept.join('/') : path
69
+ // As the archive stores it, a directory's name carries its slash: what
70
+ // pack takes, unpack then reads.
71
+ if (utf8Length(name) + (directory ? 1 : 0) > PATH_MAX) throw new ArchiveError(`${what} ${quote(path)} is longer than ${PATH_MAX} bytes`)
72
+ return name
63
73
  }
64
74
 
65
75
  // Checks the target, walked from the link's directory, never climbs above
package/src/tar/pax.js CHANGED
@@ -16,8 +16,26 @@ function record(keyword, value) {
16
16
 
17
17
  export const encodePax = (records) => concat(records.map(([keyword, value]) => record(keyword, value)))
18
18
 
19
+ // Records as an entry hands them out: a Map that nothing can change, since
20
+ // every entry under the same global header shares that header's.
21
+ const READ_ONLY = 'pax records cannot be changed'
22
+ export class Records extends Map {
23
+ set() {
24
+ throw new TypeError(READ_ONLY)
25
+ }
26
+
27
+ delete() {
28
+ throw new TypeError(READ_ONLY)
29
+ }
30
+
31
+ clear() {
32
+ throw new TypeError(READ_ONLY)
33
+ }
34
+ }
35
+ const put = (records, keyword, value) => Map.prototype.set.call(records, keyword, value)
36
+
19
37
  export function decodePax(bytes, at) {
20
- const records = new Map()
38
+ const records = new Records()
21
39
  for (let pos = 0; pos < bytes.length;) {
22
40
  let i = pos
23
41
  let length = 0
@@ -30,7 +48,7 @@ export function decodePax(bytes, at) {
30
48
  const keyword = decodeUtf8(bytes.subarray(i + 1, equals), 'a pax record', at)
31
49
  if (keyword === '' || hasUnsafe(keyword, false) || keyword.includes(' ')) throw new ArchiveError(`pax keyword ${quote(keyword)} is malformed`, at)
32
50
  if (records.has(keyword)) throw new ArchiveError(`pax keyword ${keyword} repeats`, at)
33
- records.set(keyword, decodeUtf8(bytes.subarray(equals + 1, end - 1), 'a pax record', at))
51
+ put(records, keyword, decodeUtf8(bytes.subarray(equals + 1, end - 1), 'a pax record', at))
34
52
  pos = end
35
53
  }
36
54
  return records
package/src/tar/unpack.js CHANGED
@@ -9,7 +9,7 @@ import { isFile } from '../entry.js'
9
9
  import { ArchiveError, located } from '../error.js'
10
10
  import { BLOCK, decodeHeader, isDevice, isZeroBlock, untilNul } from './header.js'
11
11
  import { Names, cleanNames } from '../names.js'
12
- import { decodePax } from './pax.js'
12
+ import { Records, decodePax } from './pax.js'
13
13
  import { decodeUtf8, hasUnsafe, quote } from '../text.js'
14
14
 
15
15
  // NUL is the pre-POSIX regular file.
@@ -55,6 +55,10 @@ function paxTime(value, what, at) {
55
55
  // with and once each entry has taken them.
56
56
  const nonePending = () => ({ pax: null, longname: null, longlink: null })
57
57
 
58
+ // The records of every entry with no pax header of its own, or under no
59
+ // global one.
60
+ const NO_RECORDS = new Records()
61
+
58
62
  class Unpacker {
59
63
  #chunks = []
60
64
  #offset = 0
@@ -224,6 +228,10 @@ class Unpacker {
224
228
  devmajor: isDevice(type) ? number('devmajor') : 0,
225
229
  devminor: isDevice(type) ? number('devminor') : 0,
226
230
  data: EMPTY,
231
+ storedName: rawName,
232
+ storedLinkname: rawTarget,
233
+ pax: pax ?? NO_RECORDS,
234
+ globalPax: this.#global ?? NO_RECORDS,
227
235
  }
228
236
  return { entry, size }
229
237
  }
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 the archive.
35
- function reader(bytes) {
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 < 0 || at + width > bytes.length) throw new ArchiveError('the archive is truncated', at)
47
+ if (at < base || at + width > base + bytes.length) throw new ArchiveError('the archive is truncated', at)
39
48
  }
40
49
  return {
41
- length: bytes.length,
42
- u16: (at) => (check(at, 2), data.getUint16(at, true)),
43
- u32: (at) => (check(at, 4), data.getUint32(at, true)),
44
- slice: (at, width) => (check(at, width), bytes.subarray(at, at + width)),
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
- function findEnd(r) {
50
- for (let at = r.length - 22; at >= Math.max(0, r.length - 22 - 0xffff); at--) {
51
- if (r.u32(at) === END && at + 22 + r.u16(at + 20) === r.length) {
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(r, at) {
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
- name: r.slice(at + 46, nameLength),
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
- // to agree on everything both carry — flags included, since a descriptor
116
- // bit set in one and not the other is read two ways by two readers — and
117
- // the local sizes and CRC may be 0 only where a descriptor carries them.
118
- function readLocal(r, entry, boundary) {
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 matches = (from) => r.u32(from) === entry.crc && r.u32(from + 4) === entry.csize && r.u32(from + 8) === entry.usize
139
- const signed = r.u32(end) === DESCRIPTOR && matches(end + 4)
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(r, entry, names) {
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,46 +225,73 @@ 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
- const body = method === 8 ? await inflate(r.slice(entry.dataAt, csize), usize, at) : r.slice(entry.dataAt, csize)
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)
175
- const out = { ...named, type, mode: mode === 0 ? DEFAULT_MODE[type] : mode & 0o7777, mtime: entry.mtime, data: type === 'file' ? body : EMPTY }
236
+ const out = { ...named, type, mode: mode === 0 ? DEFAULT_MODE[type] : mode & 0o7777, mtime: entry.mtime, data: type === 'file' ? body : EMPTY, storedName: rawName }
176
237
  located(() => names.add(out), at)
177
238
  return out
178
239
  }
179
240
 
180
- export async function unzip(bytes, { limit = Infinity } = {}) {
181
- if (!(bytes instanceof Uint8Array)) throw new ArchiveError('the archive is not a Uint8Array')
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 = reader(bytes)
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
- const entries = []
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(r, pos)
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
- entries.push(entry)
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 = entries.toSorted((a, b) => a.offset - b.offset)
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(r, entry, sorted[i + 1]?.offset ?? start)
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(true)
210
- const out = []
211
- for (const entry of entries) out.push(await entryOf(r, entry, names))
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/tar.d.ts CHANGED
@@ -19,6 +19,19 @@ export type EntryType =
19
19
  // seconds since the epoch; `linkname` is '' and the device numbers 0
20
20
  // where they do not apply; `data` is empty for anything but a file, and
21
21
  // a view over the archive bytes where they arrived in one piece.
22
+ //
23
+ // Beside those, what the archive itself says. `storedName` and
24
+ // `storedLinkname` are the name and link target as stored, before any
25
+ // cleaning — `./a` where `name` is `a`, `dir/` where it is `dir`, a ustar
26
+ // prefix joined on — and passed every check the cleaned ones did; only a
27
+ // hard link's target is ever cleaned, so a symlink's is its `linkname`.
28
+ // `pax` is the records of the entry's own pax header and `globalPax` those
29
+ // of the global header it is under, each in the order stored, those read
30
+ // into the fields above among them; the fields take an entry's own record
31
+ // over the global one. A Map is shared — every entry under one global
32
+ // header has its, every entry without a header the one empty Map — so it
33
+ // cannot be changed. A value this package does not read is as stored,
34
+ // unchecked, and may hold any character, a control one included.
22
35
  export interface Entry {
23
36
  name: string
24
37
  type: EntryType
@@ -32,13 +45,18 @@ export interface Entry {
32
45
  devmajor: number
33
46
  devminor: number
34
47
  data: Uint8Array
48
+ storedName: string
49
+ storedLinkname: string
50
+ pax: ReadonlyMap<string, string>
51
+ globalPax: ReadonlyMap<string, string>
35
52
  }
36
53
 
37
54
  // An entry to write. `name` is cleaned as above, so `./a`, `a/./b` and
38
55
  // `dir/` are taken; only a directory may end in a slash or name the root.
39
56
  // `type` defaults to 'file'; `mode` to 0o644, 0o755 for a directory, 0o777
40
57
  // for a symlink; owners to 0 with empty names; `mtime` to 0. `linkname` is
41
- // the target of a symlink or hard link.
58
+ // the target of a symlink or hard link. An entry read out is one to write
59
+ // as it is; what it stored is not written again, only these fields.
42
60
  export interface EntryInput {
43
61
  name: string
44
62
  type?: EntryType
@@ -65,13 +83,14 @@ export interface PackOptions {
65
83
  blocking?: number
66
84
  }
67
85
 
68
- // Both refuse a name that repeats as a different entry — anything but
69
- // the same fields and the same bytes again — an entry inside something
70
- // that is not a directory, a symlink whose target climbs out of the
71
- // archive or passes through anything but a directory, and a hard link to
72
- // no earlier entry. A hard link to a symlink, directly or down a chain of
73
- // hard links, is a second name for it, and its target is walked again from
74
- // that name, which a target safe where the symlink sits need not survive.
86
+ // Both refuse a name that repeats as a different entry — anything but the
87
+ // same fields and the same bytes again, however either was stored — an
88
+ // entry inside something that is not a directory, a symlink whose target
89
+ // climbs out of the archive or passes through anything but a directory, and
90
+ // a hard link to no earlier entry. A hard link to a symlink, directly or
91
+ // down a chain of hard links, is a second name for it, and its target is
92
+ // walked again from that name, which a target safe where the symlink sits
93
+ // need not survive.
75
94
  export function pack(entries: Iterable<EntryInput>, options?: PackOptions): Uint8Array
76
95
  export function unpack(bytes: Uint8Array): Entry[]
77
96
 
package/zip.d.ts CHANGED
@@ -13,7 +13,11 @@ 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
+ // `storedName` is the name as the archive stores it, before any cleaning —
18
+ // `./a` where `name` is `a`, `dir/` where it is `dir` — and passed every
19
+ // check the cleaned one did. A symlink's target is never rewritten, only
20
+ // checked, so `linkname` is as stored.
17
21
  export interface Entry {
18
22
  name: string
19
23
  type: EntryType
@@ -21,6 +25,7 @@ export interface Entry {
21
25
  mtime: number
22
26
  linkname: string
23
27
  data: Uint8Array
28
+ storedName: string
24
29
  }
25
30
 
26
31
  // An entry to write. `name` is cleaned as above, so `./a`, `a/./b` and
@@ -28,7 +33,8 @@ export interface Entry {
28
33
  // `type` defaults to 'file'; `mode` to 0o644, 0o755 for a directory, 0o777
29
34
  // for a symlink; `mtime` to 1980-01-01T00:00:00Z, the earliest DOS time,
30
35
  // and must lie between that and 2038-01-19, which the exact timestamp
31
- // holds. `linkname` is the target of a symlink.
36
+ // holds. `linkname` is the target of a symlink. An entry read out is one
37
+ // to write as it is; its stored name is not written again, its name is.
32
38
  export interface EntryInput {
33
39
  name: string
34
40
  type?: EntryType
@@ -54,16 +60,28 @@ export interface UnzipOptions {
54
60
  }
55
61
 
56
62
  // Both refuse a name that repeats as a different entry — anything but the
57
- // same fields and the same bytes again — an entry inside something that is
58
- // not a directory, and a symlink whose target climbs out of the archive
59
- // or passes through anything but a directory. unzip() also refuses an
60
- // entry carrying a Unicode path extra field, a second name that other
61
- // readers take over the one in the header. Neither
62
- // takes or makes zip64, so an archive holds at most 65534 entries and
63
- // stays under 4 GiB.
63
+ // same fields and the same bytes again, however either was stored — an
64
+ // entry inside something that is not a directory, and a symlink whose
65
+ // target climbs out of the archive or passes through anything but a
66
+ // directory. unzip() also refuses an entry carrying a Unicode path extra
67
+ // field, a second name that other readers take over the one in the header.
68
+ // Neither takes or makes zip64, so an archive holds at most 65534 entries
69
+ // and stays under 4 GiB.
64
70
  export function zip(entries: Iterable<EntryInput>, options?: ZipOptions): Promise<Uint8Array>
65
71
  export function unzip(bytes: Uint8Array, options?: UnzipOptions): Promise<Entry[]>
66
72
 
73
+ // The same one entry at a time, as an async generator: over the archive in
74
+ // memory, or over a Blob — a File, or a file opened with fs.openAsBlob —
75
+ // read a range at a time, so that no more of it is held than the entries'
76
+ // names and the entry it is on, whose data is then its own rather than a
77
+ // view of the archive.
78
+ // The layout is checked whole before the first entry comes out; what an
79
+ // entry holds is checked as it is reached, so a bad one ends the stream
80
+ // after the ones before it. A stream keeps no entry's data, so a name that
81
+ // repeats with the same fields is refused there, and such an archive is
82
+ // for the in-memory call.
83
+ export function unzipStream(archive: Uint8Array | Blob, options?: UnzipOptions): AsyncGenerator<Entry, void, undefined>
84
+
67
85
  // `offset` is where in the archive the reader gave up; unset from the writer.
68
86
  export class ArchiveError extends Error {
69
87
  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'