@preventive/archive 1.2.1 → 1.3.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/compression.js +41 -6
- package/src/tar/unpack.js +8 -3
- package/tar.d.ts +15 -9
package/package.json
CHANGED
package/src/compression.js
CHANGED
|
@@ -45,18 +45,24 @@ const chunksOf = (stream) => (Symbol.asyncIterator in stream ? stream : readerCh
|
|
|
45
45
|
// the stream makes and joined at the end, or, given `into`, copied into it
|
|
46
46
|
// as each piece comes and the piece let go: joining holds the output twice
|
|
47
47
|
// at the end, which a caller who knows how much is coming need not pay.
|
|
48
|
+
// Given `expected` bytes instead, the first that many are copied into one
|
|
49
|
+
// array of that size as they come, and any past it gathered in pieces.
|
|
48
50
|
// Output past `limit` is refused where it is, not after it has all been
|
|
49
51
|
// made: leaving the loop cancels the stream. A bound that is no number
|
|
50
52
|
// (NaN) refuses everything, not nothing.
|
|
51
|
-
async function through(bytes, Stream, format, limit, verb, into) {
|
|
53
|
+
async function through(bytes, Stream, format, limit, verb, into, expected = 0) {
|
|
52
54
|
if (!(bytes instanceof Uint8Array)) throw new ArchiveError('the data is not a Uint8Array')
|
|
53
55
|
const transform = new Stream(format)
|
|
54
56
|
const chunks = []
|
|
55
57
|
let length = 0
|
|
58
|
+
// Where the output is copied as it comes, and how much of it is filled.
|
|
59
|
+
let target = into ?? (expected > 0 ? new Uint8Array(expected) : undefined)
|
|
60
|
+
let filled = 0
|
|
56
61
|
// What has come out so far, as one array: handed back at the end, and
|
|
57
62
|
// with the error if the stream fails first.
|
|
58
63
|
const made = () => {
|
|
59
64
|
if (into !== undefined) return into.subarray(0, length)
|
|
65
|
+
if (target !== undefined) return filled === target.length ? target : target.subarray(0, filled)
|
|
60
66
|
return chunks.length === 1 ? chunks[0] : concat(chunks)
|
|
61
67
|
}
|
|
62
68
|
// The stream's own failure is read from its output; the writes fail with it.
|
|
@@ -66,8 +72,17 @@ async function through(bytes, Stream, format, limit, verb, into) {
|
|
|
66
72
|
try {
|
|
67
73
|
for await (const chunk of chunksOf(transform.readable)) {
|
|
68
74
|
if (!(length + chunk.length <= limit)) throw new RangeError(`output past ${limit} bytes`)
|
|
69
|
-
if (into === undefined)
|
|
70
|
-
|
|
75
|
+
if (target !== undefined && filled + chunk.length > target.length && into === undefined) {
|
|
76
|
+
// More than was expected: what is filled is the first piece.
|
|
77
|
+
chunks.push(target.subarray(0, filled))
|
|
78
|
+
target = undefined
|
|
79
|
+
}
|
|
80
|
+
if (target === undefined) {
|
|
81
|
+
chunks.push(chunk)
|
|
82
|
+
} else {
|
|
83
|
+
target.set(chunk, filled)
|
|
84
|
+
filled += chunk.length
|
|
85
|
+
}
|
|
71
86
|
length += chunk.length
|
|
72
87
|
}
|
|
73
88
|
} catch (cause) {
|
|
@@ -79,7 +94,27 @@ async function through(bytes, Stream, format, limit, verb, into) {
|
|
|
79
94
|
|
|
80
95
|
export const compress = (bytes, format, { limit = Infinity } = {}) => through(bytes, CompressionStream, format, limit, 'compress')
|
|
81
96
|
|
|
82
|
-
|
|
97
|
+
// What gzip says it decompresses to: its last member's trailer ends with the
|
|
98
|
+
// length of that member's data, mod 2^32, which is all of it where the data
|
|
99
|
+
// is one member and no more than 4 GiB. It is the data's own word, so it is
|
|
100
|
+
// taken only where `limit` lets that much through and it is no more than
|
|
101
|
+
// RATIO times the data: tarballs come to 2 to 13 times theirs, so one that
|
|
102
|
+
// says more is gathered in pieces as before, and a trailer that lies costs
|
|
103
|
+
// no more than that. Else, as when the data ends in padding, none is
|
|
104
|
+
// expected. Where it says less than comes, the rest is gathered as any
|
|
105
|
+
// output is; where it says more, the stream fails on it, as zlib holds the
|
|
106
|
+
// trailer to what it made.
|
|
107
|
+
const RATIO = 16
|
|
108
|
+
function expectedOf(bytes, limit) {
|
|
109
|
+
if (bytes.length < 18) return 0
|
|
110
|
+
const said = new DataView(bytes.buffer, bytes.byteOffset + bytes.length - 4, 4).getUint32(0, true)
|
|
111
|
+
return said <= limit && said <= bytes.length * RATIO ? said : 0
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// With `sized`, the output made into one array as big as the trailer says
|
|
115
|
+
// (expectedOf), which only the data as given has: a place that padding is
|
|
116
|
+
// tried ending at has the member's checksum or none where a trailer would be.
|
|
117
|
+
const inflate = (bytes, format, limit, sized) => through(bytes, DecompressionStream, format, limit, 'decompress', undefined, sized && format === 'gzip' ? expectedOf(bytes, limit) : 0)
|
|
83
118
|
|
|
84
119
|
// gzip(1) and zlib take zero bytes after the last member as padding, as old
|
|
85
120
|
// tarballs run on in zeros to a whole 10240-byte record, the way npm 1.0
|
|
@@ -94,13 +129,13 @@ const inflate = (bytes, format, limit) => through(bytes, DecompressionStream, fo
|
|
|
94
129
|
// only padded data pays for.
|
|
95
130
|
export async function decompress(bytes, format, { limit = Infinity } = {}) {
|
|
96
131
|
try {
|
|
97
|
-
return await inflate(bytes, format, limit)
|
|
132
|
+
return await inflate(bytes, format, limit, true)
|
|
98
133
|
} catch (error) {
|
|
99
134
|
if (format !== 'gzip' || !(error instanceof CompressionError) || error.limited || bytes.at(-1) !== 0) throw error
|
|
100
135
|
const last = bytes.findLastIndex((byte) => byte !== 0)
|
|
101
136
|
for (let end = last + 1; end <= Math.min(last + 10, bytes.length - 1); end++) {
|
|
102
137
|
try {
|
|
103
|
-
return await inflate(bytes.subarray(0, end), format, limit)
|
|
138
|
+
return await inflate(bytes.subarray(0, end), format, limit, false)
|
|
104
139
|
} catch (retry) {
|
|
105
140
|
// Output past the bound before the end is past it wherever the end is.
|
|
106
141
|
if (!(retry instanceof CompressionError) || retry.limited) throw retry
|
package/src/tar/unpack.js
CHANGED
|
@@ -78,13 +78,16 @@ class Unpacker {
|
|
|
78
78
|
#done = false
|
|
79
79
|
#count = 0
|
|
80
80
|
#maxEntries
|
|
81
|
+
#trailingGlobal
|
|
81
82
|
|
|
82
83
|
// `keep` holds entries for a repeat to be compared with, which only the
|
|
83
84
|
// in-memory call can afford; the options are unpack's.
|
|
84
|
-
constructor(keep, { symlinksAbove, maxEntries = Infinity } = {}) {
|
|
85
|
+
constructor(keep, { symlinksAbove, maxEntries = Infinity, trailingGlobal = false } = {}) {
|
|
85
86
|
if (maxEntries !== Infinity && !(Number.isSafeInteger(maxEntries) && maxEntries >= 0)) throw new ArchiveError(`maxEntries ${String(maxEntries)} is not a whole number of entries`)
|
|
87
|
+
if (typeof trailingGlobal !== 'boolean') throw new ArchiveError(`trailingGlobal ${String(trailingGlobal)} is not a boolean`)
|
|
86
88
|
this.#names = new Names(keep, levelsAbove(symlinksAbove))
|
|
87
89
|
this.#maxEntries = maxEntries
|
|
90
|
+
this.#trailingGlobal = trailingGlobal
|
|
88
91
|
}
|
|
89
92
|
|
|
90
93
|
// The entries a chunk completes, each as it is read, so that a chunk
|
|
@@ -155,8 +158,10 @@ class Unpacker {
|
|
|
155
158
|
if (block === null) return false
|
|
156
159
|
const at = this.#position - BLOCK
|
|
157
160
|
if (isZeroBlock(block)) {
|
|
158
|
-
// A global header too: one with no entry after it stands for nothing
|
|
159
|
-
|
|
161
|
+
// A global header too: one with no entry after it stands for nothing,
|
|
162
|
+
// unless the caller takes it as GNU tar does (trailingGlobal).
|
|
163
|
+
const unused = this.#trailingGlobal ? this.#extended.filter(({ kind }) => kind !== 'global') : this.#extended
|
|
164
|
+
if (unused.length > 0) throw new ArchiveError('an extended header is not followed by an entry', at)
|
|
160
165
|
if (++this.#zeros === 2) {
|
|
161
166
|
this.#done = true
|
|
162
167
|
const after = this.#position
|
package/tar.d.ts
CHANGED
|
@@ -36,15 +36,16 @@ export type EntryType =
|
|
|
36
36
|
// `extended` is the extended headers read since the entry before it, in
|
|
37
37
|
// the order stored, each its kind -- its own pax header, a global one,
|
|
38
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
|
|
40
|
-
//
|
|
41
|
-
// POSIX's 'ustar', or 'gnu', under which the
|
|
42
|
-
// prefix in are GNU's own and not read as one.
|
|
43
|
-
// are its header's type flag and mode as
|
|
44
|
-
// 'file' as '0' is, and the file type bits
|
|
45
|
-
// `mode` keeps. `octal` is whether every numeric
|
|
46
|
-
// extended one's too -- holds octal digits, as
|
|
47
|
-
// than nothing or GNU's base 256, both of which
|
|
39
|
+
// none was, and frozen, as a Map is. An extended header with no entry after
|
|
40
|
+
// it is refused, a global one too unless `trailingGlobal` takes it. `magic`
|
|
41
|
+
// is the entry's own header's: POSIX's 'ustar', or 'gnu', under which the
|
|
42
|
+
// bytes ustar holds a name's prefix in are GNU's own and not read as one.
|
|
43
|
+
// `typeflag` and `storedMode` are its header's type flag and mode as
|
|
44
|
+
// stored: '\0' where `type` is a 'file' as '0' is, and the file type bits
|
|
45
|
+
// among the permission bits `mode` keeps. `octal` is whether every numeric
|
|
46
|
+
// field of its header -- an extended one's too -- holds octal digits, as
|
|
47
|
+
// POSIX writes them, rather than nothing or GNU's base 256, both of which
|
|
48
|
+
// are read too.
|
|
48
49
|
export interface Entry {
|
|
49
50
|
name: string
|
|
50
51
|
type: EntryType
|
|
@@ -125,8 +126,13 @@ export interface PackOptions extends SymlinkOptions {
|
|
|
125
126
|
// counted and a repeat again, its extended headers not: past it the call
|
|
126
127
|
// throws with `limited` set, at the header that crosses it, before that
|
|
127
128
|
// entry is read. No bound by default.
|
|
129
|
+
// `trailingGlobal` takes a global header with no entry after it, as GNU tar
|
|
130
|
+
// lists one: as nothing, carried in no entry's `extended`. By default it is
|
|
131
|
+
// refused, as any extended header with no entry to describe is. A pax or
|
|
132
|
+
// long name header with nothing after it is refused either way.
|
|
128
133
|
export interface UnpackOptions extends SymlinkOptions {
|
|
129
134
|
maxEntries?: number
|
|
135
|
+
trailingGlobal?: boolean
|
|
130
136
|
}
|
|
131
137
|
|
|
132
138
|
// Both refuse a name that repeats as a different entry -- anything but the
|