@preventive/archive 1.0.0-beta.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.
@@ -0,0 +1,213 @@
1
+ // A zip archive in, its entries out, in the order of the central directory,
2
+ // which is what a zip is read from. Strict throughout: the end record has to
3
+ // sit at the very end, the central directory right before it, and the
4
+ // entries have to tile everything before that from byte 0 — no bytes belong
5
+ // to nothing, no two entries share any. Each local header has to agree with
6
+ // the central directory, the data has to inflate to exactly the declared
7
+ // size and match its CRC-32, and what this package does not model — zip64,
8
+ // encryption, any method but stored and deflate, several disks — is refused.
9
+ // Given a limit, the sizes the entries declare may not add up to more than
10
+ // it; since each has to come out at exactly its own, that bounds everything
11
+ // unzip() puts out, and it is known from the central directory alone,
12
+ // before a single byte is inflated.
13
+
14
+ import { crc32 } from '@exodus/bytes/crc.js'
15
+ import { EMPTY, sameBytes } from '../bytes.js'
16
+ import { DEFAULT_MODE } from '../entry.js'
17
+ import { ArchiveError, located } from '../error.js'
18
+ import { CENTRAL, END, LOCAL, TYPE_BITS, TYPE_MASK, fromDos, inflate, view } from './format.js'
19
+ import { Names, cleanNames } from '../names.js'
20
+ import { decodeUtf8, quote } from '../text.js'
21
+
22
+ const DESCRIPTOR = 0x08074b50
23
+ const ZIP64_LOCATOR = 0x07064b50
24
+ const ZIP64_EXTRA = 0x0001
25
+ const TIMESTAMP_EXTRA = 0x5455
26
+ // Info-ZIP's Unicode Path field: a second, UTF-8 name that unzip takes over
27
+ // the one in the header when its CRC-32 matches that header name.
28
+ const UNICODE_PATH_EXTRA = 0x7075
29
+
30
+ const ENCRYPTED = 1
31
+ const DESCRIBED = 8 // sizes and CRC follow the data, and may be 0 in the local header
32
+ const DOS_DIRECTORY = 0x10
33
+
34
+ // Bounds-checked little-endian reads over the archive.
35
+ function reader(bytes) {
36
+ const data = view(bytes)
37
+ const check = (at, width) => {
38
+ if (at < 0 || at + width > bytes.length) throw new ArchiveError('the archive is truncated', at)
39
+ }
40
+ 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)),
45
+ }
46
+ }
47
+
48
+ // 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) {
52
+ if (at >= 20 && r.u32(at - 20) === ZIP64_LOCATOR) throw new ArchiveError('zip64 is not supported', at - 20)
53
+ return at
54
+ }
55
+ }
56
+ throw new ArchiveError('no end of central directory record')
57
+ }
58
+
59
+ // Extra fields, each an id, a length and that many bytes, tiling the room.
60
+ function extras(raw, at) {
61
+ const fields = new Map()
62
+ const r = reader(raw)
63
+ for (let pos = 0; pos < raw.length;) {
64
+ const id = r.u16(pos)
65
+ const size = r.u16(pos + 2)
66
+ if (pos + 4 + size > raw.length) throw new ArchiveError('an extra field runs past its room', at)
67
+ if (id === ZIP64_EXTRA) throw new ArchiveError('zip64 is not supported', at)
68
+ // Every name here is UTF-8 already, so this field can only disagree with
69
+ // the name checked above — and unzip would extract the name it carries.
70
+ if (id === UNICODE_PATH_EXTRA) throw new ArchiveError('an entry carries a second name in a Unicode path extra field', at)
71
+ if (!fields.has(id)) fields.set(id, raw.subarray(pos + 4, pos + 4 + size))
72
+ pos += 4 + size
73
+ }
74
+ return fields
75
+ }
76
+
77
+ function readCentral(r, at) {
78
+ if (r.u32(at) !== CENTRAL) throw new ArchiveError('no central directory entry where one is counted', at)
79
+ const nameLength = r.u16(at + 28)
80
+ const extraLength = r.u16(at + 30)
81
+ const entry = {
82
+ at,
83
+ madeBy: r.u16(at + 4),
84
+ version: r.u16(at + 6),
85
+ flags: r.u16(at + 8),
86
+ method: r.u16(at + 10),
87
+ time: r.u16(at + 12),
88
+ date: r.u16(at + 14),
89
+ crc: r.u32(at + 16),
90
+ csize: r.u32(at + 20),
91
+ usize: r.u32(at + 24),
92
+ attributes: r.u32(at + 38),
93
+ offset: r.u32(at + 42),
94
+ name: r.slice(at + 46, nameLength),
95
+ next: at + 46 + nameLength + extraLength + r.u16(at + 32),
96
+ }
97
+ if (r.u16(at + 34) !== 0) throw new ArchiveError('the archive spans several disks', at)
98
+ if (entry.flags & ENCRYPTED) throw new ArchiveError('an entry is encrypted', at)
99
+ if (entry.method !== 0 && entry.method !== 8) throw new ArchiveError(`compression method ${entry.method} is not stored or deflate`, at)
100
+ if (entry.csize === 0xffffffff || entry.usize === 0xffffffff || entry.offset === 0xffffffff) throw new ArchiveError('zip64 is not supported', at)
101
+ // The DOS fields have to be a time even where the extended timestamp,
102
+ // an exact one, is the time kept.
103
+ entry.mtime = fromDos(entry.date, entry.time, at)
104
+ const stamp = extras(r.slice(at + 46 + nameLength, extraLength), at).get(TIMESTAMP_EXTRA)
105
+ if (stamp !== undefined && stamp[0] & 1) {
106
+ if (stamp.length < 5) throw new ArchiveError('an extended timestamp is cut short', at)
107
+ entry.mtime = view(stamp).getInt32(1, true)
108
+ }
109
+ return entry
110
+ }
111
+
112
+ // The local header checked against the central entry; returns where the
113
+ // 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) {
119
+ const at = entry.offset
120
+ if (r.u32(at) !== LOCAL) throw new ArchiveError('no local header where the central directory points', at)
121
+ const nameLength = r.u16(at + 26)
122
+ const extraLength = r.u16(at + 28)
123
+ if (!sameBytes(r.slice(at + 30, nameLength), entry.name)) throw new ArchiveError('the local header names a different entry', at)
124
+ 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
+ for (const [field, key, what] of shared) {
126
+ if (r.u16(at + field) !== entry[key]) throw new ArchiveError(`the local header has ${what}`, at)
127
+ }
128
+ extras(r.slice(at + 30 + nameLength, extraLength), at)
129
+ const described = entry.flags & DESCRIBED
130
+ const agrees = (field, value) => r.u32(at + field) === value || (described && r.u32(at + field) === 0)
131
+ if (!agrees(14, entry.crc) || !agrees(18, entry.csize) || !agrees(22, entry.usize)) throw new ArchiveError('the local header disagrees with the central directory', at)
132
+ entry.dataAt = at + 30 + nameLength + extraLength
133
+ let end = entry.dataAt + entry.csize
134
+ if (described) {
135
+ // The descriptor may start with its signature or not, and a CRC can be
136
+ // that very value, so both layouts are tried against the record; where
137
+ // 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)
140
+ if (matches(end) && !(signed && end + 16 === boundary)) end += 12
141
+ else if (signed) end += 16
142
+ else throw new ArchiveError('the data descriptor disagrees with the central directory', end)
143
+ }
144
+ return end
145
+ }
146
+
147
+ // A directory marker anywhere — the Unix type bits, the DOS bit — needs
148
+ // the slash; the slash needs no marker, since Java writes no attributes at
149
+ // all and every JAR has directories.
150
+ function typeOf(entry, rawName, mode) {
151
+ const format = mode & TYPE_MASK
152
+ if (rawName.endsWith('/')) {
153
+ if (format !== 0 && format !== TYPE_BITS.directory) throw new ArchiveError(`${quote(rawName)} is a directory by its name but not by its mode`, entry.at)
154
+ return 'directory'
155
+ }
156
+ if (format === TYPE_BITS.directory) throw new ArchiveError(`${quote(rawName)} is a directory by its mode but not by its name`, entry.at)
157
+ if (entry.attributes & DOS_DIRECTORY) throw new ArchiveError(`${quote(rawName)} is a directory by its attributes but not by its name`, entry.at)
158
+ if (format === TYPE_BITS.symlink) return 'symlink'
159
+ if (format === 0 || format === TYPE_BITS.file) return 'file'
160
+ throw new ArchiveError(`the mode of ${quote(rawName)} (${mode.toString(8)}) is not one this package reads`, entry.at)
161
+ }
162
+
163
+ async function entryOf(r, entry, names) {
164
+ const { at, method, csize, usize } = entry
165
+ const rawName = decodeUtf8(entry.name, 'entry name', at)
166
+ // Only a Unix maker's attributes carry a mode.
167
+ const mode = entry.madeBy >> 8 === 3 ? entry.attributes >>> 16 : 0
168
+ const type = typeOf(entry, rawName, mode)
169
+ if (type === 'directory' && usize !== 0) throw new ArchiveError(`directory ${quote(rawName)} has data`, at)
170
+ 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)
172
+ if (crc32(body) !== entry.crc) throw new ArchiveError('the data does not match its CRC-32', at)
173
+ const target = type === 'symlink' ? decodeUtf8(body, 'symlink target', at) : ''
174
+ 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 }
176
+ located(() => names.add(out), at)
177
+ return out
178
+ }
179
+
180
+ export async function unzip(bytes, { limit = Infinity } = {}) {
181
+ if (!(bytes instanceof Uint8Array)) throw new ArchiveError('the archive is not a Uint8Array')
182
+ 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)
185
+ const count = r.u16(end + 8)
186
+ 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
+ const size = r.u32(end + 12)
188
+ const start = r.u32(end + 16)
189
+ if (count === 0xffff || size === 0xffffffff || start === 0xffffffff) throw new ArchiveError('zip64 is not supported', end)
190
+ if (start + size !== end) throw new ArchiveError('the central directory does not end at the end record', end)
191
+ const entries = []
192
+ let pos = start
193
+ let total = 0
194
+ for (let i = 0; i < count; i++) {
195
+ const entry = readCentral(r, pos)
196
+ total += entry.usize
197
+ if (total > limit) throw new ArchiveError(`the entries come to more than ${limit} bytes`, pos)
198
+ entries.push(entry)
199
+ pos = entry.next
200
+ }
201
+ if (pos !== end) throw new ArchiveError('the central directory does not hold what the end record counts', pos)
202
+ let expected = 0
203
+ const sorted = entries.toSorted((a, b) => a.offset - b.offset)
204
+ for (const [i, entry] of sorted.entries()) {
205
+ 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)
207
+ }
208
+ 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))
212
+ return out
213
+ }
package/src/zip/zip.js ADDED
@@ -0,0 +1,83 @@
1
+ // Entries in, a zip archive out, laid out the way Info-ZIP lays one out: a
2
+ // Unix maker, the mode in the external attributes, a symlink as a stored
3
+ // entry whose data is its target, an extended-timestamp extra field for an
4
+ // exact UTC mtime, and each file deflated only where that made it smaller.
5
+
6
+ import { crc32 } from '@exodus/bytes/crc.js'
7
+ import { concat, isAscii } from '../bytes.js'
8
+ import { DEFAULT_MODE, checkEntry, wireName } from '../entry.js'
9
+ import { ArchiveError } from '../error.js'
10
+ import { CENTRAL, END, LOCAL, TYPE_BITS, deflate, record, toDos } from './format.js'
11
+ import { Names, cleanNames } from '../names.js'
12
+ import { encodeUtf8, quote } from '../text.js'
13
+
14
+ const METHODS = new Set(['deflate', 'store'])
15
+
16
+ const MADE_BY = (3 << 8) | 20 // Unix, spec 2.0
17
+ const UTF8_NAME = 0x800
18
+ const DOS_EPOCH = 315532800 // 1980-01-01T00:00:00Z, the earliest DOS time
19
+ const LAST_STAMP = 0x7fffffff // the extended timestamp is a signed 32-bit time
20
+ // A count of 0xffff or a size or offset of 0xffffffff sends a reader to
21
+ // zip64, so the last value short of each is the most this writes.
22
+ const MAX_ENTRIES = 0xfffe
23
+ const MAX_SIZE = 0xfffffffe
24
+
25
+ function integer(value, what, min, max) {
26
+ if (!Number.isSafeInteger(value) || value < min || value > max) throw new ArchiveError(`${what} ${String(value)} is not an integer from ${min} to ${max}`)
27
+ return value
28
+ }
29
+
30
+ function normalize(entry) {
31
+ const checked = checkEntry(entry, TYPE_BITS)
32
+ return {
33
+ ...checked,
34
+ mode: integer(entry.mode ?? DEFAULT_MODE[checked.type], 'mode', 0, 0o7777),
35
+ mtime: integer(entry.mtime ?? DOS_EPOCH, 'mtime', DOS_EPOCH, LAST_STAMP),
36
+ }
37
+ }
38
+
39
+ // 0x5455: flag bit 0 and the mtime, as Info-ZIP writes in the central copy.
40
+ const timestamp = (mtime) => record([[2, 0x5455], [2, 5], [1, 1], [4, mtime]])
41
+
42
+ export async function zip(entries, { method = 'deflate' } = {}) {
43
+ if (!METHODS.has(method)) throw new ArchiveError(`method ${quote(String(method))} is not deflate or store`)
44
+ const names = new Names(true)
45
+ const locals = []
46
+ const centrals = []
47
+ let offset = 0
48
+ let count = 0
49
+ for (const given of entries) {
50
+ const entry = normalize(given)
51
+ const e = { ...entry, ...cleanNames(entry.name, entry.type, entry.linkname) }
52
+ names.add(e)
53
+ if (++count > MAX_ENTRIES) throw new ArchiveError(`more than ${MAX_ENTRIES} entries would need zip64`)
54
+ const name = encodeUtf8(wireName(e), 'entry name')
55
+ const body = e.type === 'symlink' ? encodeUtf8(e.linkname, 'symlink target') : e.data
56
+ // Before deflating, which is all the work an entry takes, and is only
57
+ // ever kept where it made the entry smaller.
58
+ if (body.length > MAX_SIZE || offset > MAX_SIZE) throw new ArchiveError(`${quote(e.name)} would need zip64`)
59
+ let stored = body
60
+ let compression = 0
61
+ if (method === 'deflate' && e.type === 'file' && body.length) {
62
+ const packed = await deflate(body)
63
+ if (packed.length < body.length) {
64
+ stored = packed
65
+ compression = 8
66
+ }
67
+ }
68
+ const { time, date } = toDos(e.mtime)
69
+ const extra = timestamp(e.mtime)
70
+ const common = [
71
+ [2, compression === 8 ? 20 : 10], [2, isAscii(name) ? 0 : UTF8_NAME], [2, compression], [2, time], [2, date],
72
+ [4, crc32(body)], [4, stored.length], [4, body.length], [2, name.length], [2, extra.length],
73
+ ]
74
+ locals.push(record([[4, LOCAL], ...common]), name, extra, stored)
75
+ const attributes = (TYPE_BITS[e.type] | e.mode) * 0x10000 + (e.type === 'directory' ? 0x10 : 0)
76
+ centrals.push(record([[4, CENTRAL], [2, MADE_BY], ...common, [2, 0], [2, 0], [2, 0], [4, attributes], [4, offset]]), name, extra)
77
+ offset += 30 + name.length + extra.length + stored.length
78
+ }
79
+ const directory = concat(centrals)
80
+ if (offset + directory.length > MAX_SIZE) throw new ArchiveError('the archive would need zip64')
81
+ const end = record([[4, END], [2, 0], [2, 0], [2, count], [2, count], [4, directory.length], [4, offset], [2, 0]])
82
+ return concat([...locals, directory, end])
83
+ }
package/tar.d.ts ADDED
@@ -0,0 +1,90 @@
1
+ // The typed contract for archive/tar.js, hand-written: keep it name for name
2
+ // with tar.js, and change it in the same commit as the signature.
3
+
4
+ // tar's entry types under tar-stream's names. A 'link' is a hard link to
5
+ // an earlier entry; a 'contiguous-file' is a file for every purpose here.
6
+ export type EntryType =
7
+ | 'file'
8
+ | 'directory'
9
+ | 'symlink'
10
+ | 'link'
11
+ | 'fifo'
12
+ | 'character-device'
13
+ | 'block-device'
14
+ | 'contiguous-file'
15
+
16
+ // An entry read out of an archive. `name` is a relative path with `.`
17
+ // segments and a directory's trailing slash dropped, or `.` for the
18
+ // archive root itself, which only a directory names; `mtime` is whole
19
+ // seconds since the epoch; `linkname` is '' and the device numbers 0
20
+ // where they do not apply; `data` is empty for anything but a file, and
21
+ // a view over the archive bytes where they arrived in one piece.
22
+ export interface Entry {
23
+ name: string
24
+ type: EntryType
25
+ mode: number
26
+ uid: number
27
+ gid: number
28
+ mtime: number
29
+ uname: string
30
+ gname: string
31
+ linkname: string
32
+ devmajor: number
33
+ devminor: number
34
+ data: Uint8Array
35
+ }
36
+
37
+ // An entry to write. `name` is cleaned as above, so `./a`, `a/./b` and
38
+ // `dir/` are taken; only a directory may end in a slash or name the root.
39
+ // `type` defaults to 'file'; `mode` to 0o644, 0o755 for a directory, 0o777
40
+ // for a symlink; owners to 0 with empty names; `mtime` to 0. `linkname` is
41
+ // the target of a symlink or hard link.
42
+ export interface EntryInput {
43
+ name: string
44
+ type?: EntryType
45
+ data?: Uint8Array
46
+ mode?: number
47
+ uid?: number
48
+ gid?: number
49
+ mtime?: number
50
+ uname?: string
51
+ gname?: string
52
+ linkname?: string
53
+ devmajor?: number
54
+ devminor?: number
55
+ }
56
+
57
+ // GNU tar's names: 'gnu' is what plain `tar` writes and the default here,
58
+ // 'ustar' refuses what it cannot hold, 'pax' holds everything.
59
+ export type Format = 'gnu' | 'ustar' | 'pax'
60
+
61
+ // `blocking` is tar's -b: zero padding to a multiple of that many 512-byte
62
+ // blocks, 20 by default; 1 pads nothing past the two zero end blocks.
63
+ export interface PackOptions {
64
+ format?: Format
65
+ blocking?: number
66
+ }
67
+
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.
75
+ export function pack(entries: Iterable<EntryInput>, options?: PackOptions): Uint8Array
76
+ export function unpack(bytes: Uint8Array): Entry[]
77
+
78
+ // The same a piece at a time, as async generators over a plain or an
79
+ // async iterable. A stream keeps no entry's data, so a name that repeats
80
+ // with the same fields is refused there, and such an archive is for the
81
+ // in-memory call. packStream yields each entry's `data` as the very array
82
+ // it was given.
83
+ export function packStream(entries: Iterable<EntryInput> | AsyncIterable<EntryInput>, options?: PackOptions): AsyncGenerator<Uint8Array, void, undefined>
84
+ export function unpackStream(chunks: Iterable<Uint8Array> | AsyncIterable<Uint8Array>): AsyncGenerator<Entry, void, undefined>
85
+
86
+ // `offset` is where in the archive the reader gave up; unset from the writer.
87
+ export class ArchiveError extends Error {
88
+ constructor(detail: string, offset?: number)
89
+ offset: number | undefined
90
+ }
package/tar.js ADDED
@@ -0,0 +1,19 @@
1
+ // The tar half of @preventive/archive, reached as
2
+ // `@preventive/archive/tar.js`; the zip half is beside it, and the two
3
+ // share only the modules at the top of src/: the name rules, the entry
4
+ // shape, and the byte and text helpers. Inside, archive/ runs on browser
5
+ // APIs alone, plus @exodus/bytes for hardened UTF-8 and CRC-32
6
+ // (self-contained.test.js enforces both).
7
+
8
+ // Entries in, one Uint8Array out: byte for byte what GNU tar writes.
9
+ export { pack } from './src/tar/pack.js'
10
+
11
+ // An archive in, its entries out, under the same rules pack() writes by.
12
+ export { unpack } from './src/tar/unpack.js'
13
+
14
+ // The same a chunk or an entry at a time, as async generators over a
15
+ // plain or an async iterable.
16
+ export { packStream } from './src/tar/pack.js'
17
+ export { unpackStream } from './src/tar/unpack.js'
18
+
19
+ export { ArchiveError } from './src/error.js'
package/zip.d.ts ADDED
@@ -0,0 +1,71 @@
1
+ // The typed contract for archive/zip.js, hand-written: keep it name for name
2
+ // with zip.js, and change it in the same commit as the signature.
3
+
4
+ // A 'symlink' is stored the way Info-ZIP stores one: its target as the
5
+ // entry's data, its type in the Unix mode.
6
+ export type EntryType = 'file' | 'directory' | 'symlink'
7
+
8
+ // An entry read out of an archive. `name` is a relative path with `.`
9
+ // segments and a directory's trailing slash dropped, or `.` for the
10
+ // archive root itself, which only a directory names; `mode` is the Unix
11
+ // permission bits, or the usual default where the maker recorded none;
12
+ // `mtime` is whole seconds since the epoch, exact where the archive
13
+ // carries an extended timestamp and DOS time read as UTC otherwise;
14
+ // `linkname` is '' for anything but a symlink; `data` is empty for
15
+ // anything but a file, and a view over the archive bytes where the entry
16
+ // was stored.
17
+ export interface Entry {
18
+ name: string
19
+ type: EntryType
20
+ mode: number
21
+ mtime: number
22
+ linkname: string
23
+ data: Uint8Array
24
+ }
25
+
26
+ // An entry to write. `name` is cleaned as above, so `./a`, `a/./b` and
27
+ // `dir/` are taken; only a directory may end in a slash or name the root.
28
+ // `type` defaults to 'file'; `mode` to 0o644, 0o755 for a directory, 0o777
29
+ // for a symlink; `mtime` to 1980-01-01T00:00:00Z, the earliest DOS time,
30
+ // and must lie between that and 2038-01-19, which the exact timestamp
31
+ // holds. `linkname` is the target of a symlink.
32
+ export interface EntryInput {
33
+ name: string
34
+ type?: EntryType
35
+ data?: Uint8Array
36
+ mode?: number
37
+ mtime?: number
38
+ linkname?: string
39
+ }
40
+
41
+ // 'deflate' (the default) deflates each file and keeps the result only
42
+ // where it is smaller, as Info-ZIP does; 'store' compresses nothing.
43
+ export interface ZipOptions {
44
+ method?: 'deflate' | 'store'
45
+ }
46
+
47
+ // `limit` bounds the entries' sizes, all of them together, in bytes —
48
+ // stored and deflated alike, a symlink's target included. Each entry has to
49
+ // come out at exactly the size it declares, so the sum is checked against
50
+ // the central directory before anything is inflated, and past the limit
51
+ // the call rejects. No limit by default.
52
+ export interface UnzipOptions {
53
+ limit?: number
54
+ }
55
+
56
+ // 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.
64
+ export function zip(entries: Iterable<EntryInput>, options?: ZipOptions): Promise<Uint8Array>
65
+ export function unzip(bytes: Uint8Array, options?: UnzipOptions): Promise<Entry[]>
66
+
67
+ // `offset` is where in the archive the reader gave up; unset from the writer.
68
+ export class ArchiveError extends Error {
69
+ constructor(detail: string, offset?: number)
70
+ offset: number | undefined
71
+ }
package/zip.js ADDED
@@ -0,0 +1,16 @@
1
+ // The zip half of @preventive/archive, reached as
2
+ // `@preventive/archive/zip.js`. Deflate is the platform's
3
+ // CompressionStream and DecompressionStream, raw; the rest is shared with
4
+ // the tar half or browser APIs alone.
5
+
6
+ // Entries in, one Uint8Array out. Strict: a duplicate name, a name that
7
+ // could reach outside the archive, or a value the format cannot hold is
8
+ // refused rather than written down wrong.
9
+ export { zip } from './src/zip/zip.js'
10
+
11
+ // An archive in, its entries out, under the same rules — and with every
12
+ // header, size and checksum in it checked against every other, and the
13
+ // sizes all together held to a limit where one is given.
14
+ export { unzip } from './src/zip/unzip.js'
15
+
16
+ export { ArchiveError } from './src/error.js'