@preventive/archive 1.1.0 → 1.2.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.
- package/package.json +1 -1
- package/src/tar/header.js +6 -0
- package/src/tar/unpack.js +28 -8
- package/tar.d.ts +31 -4
package/package.json
CHANGED
package/src/tar/header.js
CHANGED
|
@@ -135,6 +135,10 @@ export function encodeHeader(f) {
|
|
|
135
135
|
return block
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
+
// Whether a numeric field holds octal digits, as POSIX writes it, rather
|
|
139
|
+
// than nothing or GNU's base 256, both of which readNumber reads.
|
|
140
|
+
const isOctal = (block, offset, size) => /^ *[0-7]+ *$/u.test(ascii(untilNul(block.subarray(offset, offset + size))))
|
|
141
|
+
|
|
138
142
|
export function decodeHeader(block, at) {
|
|
139
143
|
if (checksum(block) !== readNumber(block, CHKSUM, 8, 'checksum', at)) throw new ArchiveError('header checksum does not match', at)
|
|
140
144
|
const magic = ascii(block.subarray(MAGIC, MAGIC + 8))
|
|
@@ -155,5 +159,7 @@ export function decodeHeader(block, at) {
|
|
|
155
159
|
...Object.fromEntries(NUMBERS.map(([key, offset, size]) => [key, readNumber(block, offset, size, key, at, key === 'mtime')])),
|
|
156
160
|
devmajor: device ? readNumber(block, DEVMAJOR, 8, 'devmajor', at) : 0,
|
|
157
161
|
devminor: device ? readNumber(block, DEVMINOR, 8, 'devminor', at) : 0,
|
|
162
|
+
octal: [['checksum', CHKSUM, 8], ...NUMBERS, ...device ? [['devmajor', DEVMAJOR, 8], ['devminor', DEVMINOR, 8]] : []]
|
|
163
|
+
.every(([, offset, size]) => isOctal(block, offset, size)),
|
|
158
164
|
}
|
|
159
165
|
}
|
package/src/tar/unpack.js
CHANGED
|
@@ -57,6 +57,10 @@ const nonePending = () => ({ pax: null, longname: null, longlink: null })
|
|
|
57
57
|
// global one.
|
|
58
58
|
const NO_RECORDS = new Records()
|
|
59
59
|
|
|
60
|
+
// The extended headers of every entry with none ahead of it.
|
|
61
|
+
const NO_EXTENDED = Object.freeze([])
|
|
62
|
+
const magicOf = (header) => (header.gnu ? 'gnu' : 'ustar')
|
|
63
|
+
|
|
60
64
|
class Unpacker {
|
|
61
65
|
#chunks = []
|
|
62
66
|
#offset = 0
|
|
@@ -64,6 +68,9 @@ class Unpacker {
|
|
|
64
68
|
#position = 0
|
|
65
69
|
#names
|
|
66
70
|
#pending = nonePending()
|
|
71
|
+
// The extended headers read since the last entry, in order: each its
|
|
72
|
+
// kind and its magic.
|
|
73
|
+
#extended = []
|
|
67
74
|
#global = null
|
|
68
75
|
// The header whose body is due: { extended, size, at, entry }.
|
|
69
76
|
#awaiting = null
|
|
@@ -80,12 +87,14 @@ class Unpacker {
|
|
|
80
87
|
this.#maxEntries = maxEntries
|
|
81
88
|
}
|
|
82
89
|
|
|
83
|
-
|
|
90
|
+
// The entries a chunk completes, each as it is read, so that a chunk
|
|
91
|
+
// holding many has no more of them held than the one being read.
|
|
92
|
+
*push(chunk) {
|
|
84
93
|
if (!(chunk instanceof Uint8Array)) throw new ArchiveError('a chunk is not a Uint8Array')
|
|
85
94
|
if (this.#done) {
|
|
86
95
|
this.#trailing(chunk, this.#position)
|
|
87
96
|
this.#position += chunk.length
|
|
88
|
-
return
|
|
97
|
+
return
|
|
89
98
|
}
|
|
90
99
|
if (chunk.length) {
|
|
91
100
|
this.#chunks.push(chunk)
|
|
@@ -93,8 +102,10 @@ class Unpacker {
|
|
|
93
102
|
}
|
|
94
103
|
const out = []
|
|
95
104
|
let more = true
|
|
96
|
-
while (more && !this.#done)
|
|
97
|
-
|
|
105
|
+
while (more && !this.#done) {
|
|
106
|
+
more = this.#awaiting === null ? this.#header() : this.#body(out)
|
|
107
|
+
yield* out.splice(0)
|
|
108
|
+
}
|
|
98
109
|
}
|
|
99
110
|
|
|
100
111
|
end() {
|
|
@@ -144,7 +155,8 @@ class Unpacker {
|
|
|
144
155
|
if (block === null) return false
|
|
145
156
|
const at = this.#position - BLOCK
|
|
146
157
|
if (isZeroBlock(block)) {
|
|
147
|
-
|
|
158
|
+
// A global header too: one with no entry after it stands for nothing.
|
|
159
|
+
if (this.#extended.length > 0) throw new ArchiveError('an extended header is not followed by an entry', at)
|
|
148
160
|
if (++this.#zeros === 2) {
|
|
149
161
|
this.#done = true
|
|
150
162
|
const after = this.#position
|
|
@@ -161,13 +173,13 @@ class Unpacker {
|
|
|
161
173
|
this.#awaiting = { extended: null, at, ...this.#resolve(header, at) }
|
|
162
174
|
} else {
|
|
163
175
|
if (header.size > MAX_EXTENDED) throw new ArchiveError(`a ${extended} header of ${header.size} bytes is longer than any tar writes`, at)
|
|
164
|
-
this.#awaiting = { extended, size: header.size, at, entry: null }
|
|
176
|
+
this.#awaiting = { extended, size: header.size, at, entry: null, magic: magicOf(header), octal: header.octal }
|
|
165
177
|
}
|
|
166
178
|
return true
|
|
167
179
|
}
|
|
168
180
|
|
|
169
181
|
#body(out) {
|
|
170
|
-
const { extended, size, at, entry } = this.#awaiting
|
|
182
|
+
const { extended, size, at, entry, magic, octal } = this.#awaiting
|
|
171
183
|
const whole = Math.ceil(size / BLOCK) * BLOCK
|
|
172
184
|
const body = this.#take(whole)
|
|
173
185
|
if (body === null) return false
|
|
@@ -186,9 +198,11 @@ class Unpacker {
|
|
|
186
198
|
// Once, here: a global header stands until another replaces it, and
|
|
187
199
|
// re-reading its keys under every entry is work an archive can ask for.
|
|
188
200
|
sparse(this.#global.keys(), at)
|
|
201
|
+
this.#extended.push(Object.freeze({ kind: extended, magic, octal }))
|
|
189
202
|
} else {
|
|
190
203
|
if (this.#pending[extended] !== null) throw new ArchiveError(`two ${extended} headers ahead of one entry`, at)
|
|
191
204
|
this.#pending[extended] = extended === 'pax' ? decodePax(raw, at) : decodeUtf8(untilNul(raw), `a ${extended} header`, at)
|
|
205
|
+
this.#extended.push(Object.freeze({ kind: extended, magic, octal }))
|
|
192
206
|
}
|
|
193
207
|
return true
|
|
194
208
|
}
|
|
@@ -236,7 +250,13 @@ class Unpacker {
|
|
|
236
250
|
storedLinkname: rawTarget,
|
|
237
251
|
pax: pax ?? NO_RECORDS,
|
|
238
252
|
globalPax: this.#global ?? NO_RECORDS,
|
|
253
|
+
extended: this.#extended.length ? Object.freeze(this.#extended) : NO_EXTENDED,
|
|
254
|
+
magic: magicOf(header),
|
|
255
|
+
typeflag: String.fromCodePoint(header.typeflag),
|
|
256
|
+
storedMode: header.mode,
|
|
257
|
+
octal: header.octal,
|
|
239
258
|
}
|
|
259
|
+
this.#extended = []
|
|
240
260
|
return { entry, size }
|
|
241
261
|
}
|
|
242
262
|
|
|
@@ -248,7 +268,7 @@ class Unpacker {
|
|
|
248
268
|
|
|
249
269
|
export function unpack(bytes, options) {
|
|
250
270
|
const unpacker = new Unpacker(true, options)
|
|
251
|
-
const entries = unpacker.push(bytes)
|
|
271
|
+
const entries = [...unpacker.push(bytes)]
|
|
252
272
|
unpacker.end()
|
|
253
273
|
return entries
|
|
254
274
|
}
|
package/tar.d.ts
CHANGED
|
@@ -33,6 +33,18 @@ export type EntryType =
|
|
|
33
33
|
// header has its, every entry without a header the one empty Map -- so it
|
|
34
34
|
// cannot be changed. A value this package does not read is as stored,
|
|
35
35
|
// unchecked, and may hold any character, a control one included.
|
|
36
|
+
// `extended` is the extended headers read since the entry before it, in
|
|
37
|
+
// the order stored, each its kind -- its own pax header, a global one,
|
|
38
|
+
// GNU's long name and long link -- and its magic; the one empty list where
|
|
39
|
+
// none was, and frozen, as a Map is. An extended header, a global one too,
|
|
40
|
+
// with no entry after it is refused. `magic` is the entry's own header's:
|
|
41
|
+
// POSIX's 'ustar', or 'gnu', under which the bytes ustar holds a name's
|
|
42
|
+
// prefix in are GNU's own and not read as one. `typeflag` and `storedMode`
|
|
43
|
+
// are its header's type flag and mode as stored: '\0' where `type` is a
|
|
44
|
+
// 'file' as '0' is, and the file type bits among the permission bits
|
|
45
|
+
// `mode` keeps. `octal` is whether every numeric field of its header -- an
|
|
46
|
+
// extended one's too -- holds octal digits, as POSIX writes them, rather
|
|
47
|
+
// than nothing or GNU's base 256, both of which are read too.
|
|
36
48
|
export interface Entry {
|
|
37
49
|
name: string
|
|
38
50
|
type: EntryType
|
|
@@ -50,6 +62,18 @@ export interface Entry {
|
|
|
50
62
|
storedLinkname: string
|
|
51
63
|
pax: ReadonlyMap<string, string>
|
|
52
64
|
globalPax: ReadonlyMap<string, string>
|
|
65
|
+
extended: readonly ExtendedHeader[]
|
|
66
|
+
magic: 'ustar' | 'gnu'
|
|
67
|
+
typeflag: string
|
|
68
|
+
storedMode: number
|
|
69
|
+
octal: boolean
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// An extended header, as `extended` lists them.
|
|
73
|
+
export interface ExtendedHeader {
|
|
74
|
+
kind: 'pax' | 'global' | 'longname' | 'longlink'
|
|
75
|
+
magic: 'ustar' | 'gnu'
|
|
76
|
+
octal: boolean
|
|
53
77
|
}
|
|
54
78
|
|
|
55
79
|
// An entry to write. `name` is cleaned as above, so `./a`, `a/./b` and
|
|
@@ -117,10 +141,13 @@ export function pack(entries: Iterable<EntryInput>, options?: PackOptions): Uint
|
|
|
117
141
|
export function unpack(bytes: Uint8Array, options?: UnpackOptions): Entry[]
|
|
118
142
|
|
|
119
143
|
// The same a piece at a time, as async generators over a plain or an
|
|
120
|
-
// async iterable.
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
// it
|
|
144
|
+
// async iterable. unpackStream yields each entry as it is read, however
|
|
145
|
+
// many a chunk holds, its `data` a view of the chunk wherever one holds it
|
|
146
|
+
// whole: an archive whole in memory, given as one chunk, is read an entry
|
|
147
|
+
// at a time and nothing of it copied. A stream keeps no entry's data, so a
|
|
148
|
+
// name that repeats with the same fields is refused there, and such an
|
|
149
|
+
// archive is for the in-memory call. packStream yields each entry's `data`
|
|
150
|
+
// as the very array it was given.
|
|
124
151
|
export function packStream(entries: Iterable<EntryInput> | AsyncIterable<EntryInput>, options?: PackOptions): AsyncGenerator<Uint8Array, void, undefined>
|
|
125
152
|
export function unpackStream(chunks: Iterable<Uint8Array> | AsyncIterable<Uint8Array>, options?: UnpackOptions): AsyncGenerator<Entry, void, undefined>
|
|
126
153
|
|