@preventive/archive 1.0.0-beta.1 → 1.0.0-beta.3
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/entry.js +1 -1
- package/src/names.js +18 -8
- package/src/tar/pack.js +1 -1
- package/src/tar/pax.js +20 -2
- package/src/tar/unpack.js +11 -3
- package/src/zip/unzip.js +1 -1
- package/tar.d.ts +31 -11
- package/zip.d.ts +14 -8
package/package.json
CHANGED
package/src/entry.js
CHANGED
|
@@ -29,6 +29,6 @@ export function checkEntry(entry, types) {
|
|
|
29
29
|
if (data.length !== 0 && !isFile(type)) throw new ArchiveError(`a ${type} cannot carry data (${quote(name)})`)
|
|
30
30
|
const linkname = entry.linkname ?? ''
|
|
31
31
|
checkString(linkname, `link target of ${quote(name)}`)
|
|
32
|
-
if (linkname !== '' && type !== '
|
|
32
|
+
if (linkname !== '' && type !== 'hardlink' && type !== 'symlink') throw new ArchiveError(`a ${type} cannot have a link target (${quote(name)})`)
|
|
33
33
|
return { name, type, data, linkname }
|
|
34
34
|
}
|
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
|
|
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
|
-
|
|
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
|
|
61
|
-
|
|
62
|
-
|
|
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
|
|
@@ -81,7 +91,7 @@ export function cleanNames(path, type, target) {
|
|
|
81
91
|
const name = cleanPath(path, 'entry name', type === 'directory')
|
|
82
92
|
let linkname = target
|
|
83
93
|
if (type === 'symlink') checkSymlinkTarget(name, target)
|
|
84
|
-
else if (type === '
|
|
94
|
+
else if (type === 'hardlink') linkname = cleanPath(target, `hard link target of ${quote(name)}`)
|
|
85
95
|
return { name, linkname }
|
|
86
96
|
}
|
|
87
97
|
|
|
@@ -137,7 +147,7 @@ export class Names {
|
|
|
137
147
|
// The target of the symlink this entry is, if it is one: a symlink's
|
|
138
148
|
// own, or, for a hard link, that of the entry it links to.
|
|
139
149
|
let symlink = type === 'symlink' ? entry.linkname : undefined
|
|
140
|
-
if (type === '
|
|
150
|
+
if (type === 'hardlink') {
|
|
141
151
|
const target = this.#find(entry.linkname)
|
|
142
152
|
if (target?.kind !== 'entry') throw new ArchiveError(`hard link ${quote(name)} targets ${quote(entry.linkname)}, which is not an earlier non-directory entry`)
|
|
143
153
|
// A hard link to a symlink is a second name for that symlink, and a
|
package/src/tar/pack.js
CHANGED
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
|
|
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
|
|
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,12 +9,12 @@ 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.
|
|
16
16
|
const TYPES = new Map([
|
|
17
|
-
[0x30, 'file'], [0, 'file'], [0x31, '
|
|
17
|
+
[0x30, 'file'], [0, 'file'], [0x31, 'hardlink'], [0x32, 'symlink'], [0x33, 'character-device'],
|
|
18
18
|
[0x34, 'block-device'], [0x35, 'directory'], [0x36, 'fifo'], [0x37, 'contiguous-file'],
|
|
19
19
|
])
|
|
20
20
|
const EXTENDED = new Map([[0x78, 'pax'], [0x67, 'global'], [0x4c, 'longname'], [0x4b, 'longlink']])
|
|
@@ -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
|
|
@@ -205,7 +209,7 @@ class Unpacker {
|
|
|
205
209
|
}
|
|
206
210
|
const size = number('size')
|
|
207
211
|
if (size !== 0 && !isFile(type)) throw new ArchiveError(`a ${type} entry has a size`, at)
|
|
208
|
-
if (linkname !== '' && type !== '
|
|
212
|
+
if (linkname !== '' && type !== 'hardlink' && type !== 'symlink') throw new ArchiveError(`a ${type} entry has a link target`, at)
|
|
209
213
|
const owner = (key) => {
|
|
210
214
|
const value = record(key) ?? decodeUtf8(header[key], key, at)
|
|
211
215
|
if (hasUnsafe(value, false)) throw new ArchiveError(`${key} ${quote(value)} holds a control or formatting character`, at)
|
|
@@ -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
|
@@ -233,7 +233,7 @@ async function entryOf(source, entry, names) {
|
|
|
233
233
|
if (crc32(body) !== entry.crc) throw new ArchiveError('the data does not match its CRC-32', at)
|
|
234
234
|
const target = type === 'symlink' ? decodeUtf8(body, 'symlink target', at) : ''
|
|
235
235
|
const named = located(() => cleanNames(rawName, type, target), at)
|
|
236
|
-
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 }
|
|
237
237
|
located(() => names.add(out), at)
|
|
238
238
|
return out
|
|
239
239
|
}
|
package/tar.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
// The typed contract for archive/tar.js, hand-written: keep it name for name
|
|
2
2
|
// with tar.js, and change it in the same commit as the signature.
|
|
3
3
|
|
|
4
|
-
// tar's entry types under tar-stream's names
|
|
5
|
-
//
|
|
4
|
+
// tar's entry types under tar-stream's names, but for a 'hardlink', which
|
|
5
|
+
// tar-stream calls a 'link': a hard link to an earlier entry. A
|
|
6
|
+
// 'contiguous-file' is a file for every purpose here.
|
|
6
7
|
export type EntryType =
|
|
7
8
|
| 'file'
|
|
8
9
|
| 'directory'
|
|
9
10
|
| 'symlink'
|
|
10
|
-
| '
|
|
11
|
+
| 'hardlink'
|
|
11
12
|
| 'fifo'
|
|
12
13
|
| 'character-device'
|
|
13
14
|
| 'block-device'
|
|
@@ -19,6 +20,19 @@ export type EntryType =
|
|
|
19
20
|
// seconds since the epoch; `linkname` is '' and the device numbers 0
|
|
20
21
|
// where they do not apply; `data` is empty for anything but a file, and
|
|
21
22
|
// a view over the archive bytes where they arrived in one piece.
|
|
23
|
+
//
|
|
24
|
+
// Beside those, what the archive itself says. `storedName` and
|
|
25
|
+
// `storedLinkname` are the name and link target as stored, before any
|
|
26
|
+
// cleaning — `./a` where `name` is `a`, `dir/` where it is `dir`, a ustar
|
|
27
|
+
// prefix joined on — and passed every check the cleaned ones did; only a
|
|
28
|
+
// hard link's target is ever cleaned, so a symlink's is its `linkname`.
|
|
29
|
+
// `pax` is the records of the entry's own pax header and `globalPax` those
|
|
30
|
+
// of the global header it is under, each in the order stored, those read
|
|
31
|
+
// into the fields above among them; the fields take an entry's own record
|
|
32
|
+
// over the global one. A Map is shared — every entry under one global
|
|
33
|
+
// header has its, every entry without a header the one empty Map — so it
|
|
34
|
+
// cannot be changed. A value this package does not read is as stored,
|
|
35
|
+
// unchecked, and may hold any character, a control one included.
|
|
22
36
|
export interface Entry {
|
|
23
37
|
name: string
|
|
24
38
|
type: EntryType
|
|
@@ -32,13 +46,18 @@ export interface Entry {
|
|
|
32
46
|
devmajor: number
|
|
33
47
|
devminor: number
|
|
34
48
|
data: Uint8Array
|
|
49
|
+
storedName: string
|
|
50
|
+
storedLinkname: string
|
|
51
|
+
pax: ReadonlyMap<string, string>
|
|
52
|
+
globalPax: ReadonlyMap<string, string>
|
|
35
53
|
}
|
|
36
54
|
|
|
37
55
|
// An entry to write. `name` is cleaned as above, so `./a`, `a/./b` and
|
|
38
56
|
// `dir/` are taken; only a directory may end in a slash or name the root.
|
|
39
57
|
// `type` defaults to 'file'; `mode` to 0o644, 0o755 for a directory, 0o777
|
|
40
58
|
// for a symlink; owners to 0 with empty names; `mtime` to 0. `linkname` is
|
|
41
|
-
// the target of a symlink or hard link.
|
|
59
|
+
// the target of a symlink or hard link. An entry read out is one to write
|
|
60
|
+
// as it is; what it stored is not written again, only these fields.
|
|
42
61
|
export interface EntryInput {
|
|
43
62
|
name: string
|
|
44
63
|
type?: EntryType
|
|
@@ -65,13 +84,14 @@ export interface PackOptions {
|
|
|
65
84
|
blocking?: number
|
|
66
85
|
}
|
|
67
86
|
|
|
68
|
-
// Both refuse a name that repeats as a different entry — anything but
|
|
69
|
-
//
|
|
70
|
-
// that is not a directory, a symlink whose target
|
|
71
|
-
// archive or passes through anything but a directory, and
|
|
72
|
-
// no earlier entry. A hard link to a symlink, directly or
|
|
73
|
-
// hard links, is a second name for it, and its target is
|
|
74
|
-
// that name, which a target safe where the symlink sits
|
|
87
|
+
// Both refuse a name that repeats as a different entry — anything but the
|
|
88
|
+
// same fields and the same bytes again, however either was stored — an
|
|
89
|
+
// entry inside something that is not a directory, a symlink whose target
|
|
90
|
+
// climbs out of the archive or passes through anything but a directory, and
|
|
91
|
+
// a hard link to no earlier entry. A hard link to a symlink, directly or
|
|
92
|
+
// down a chain of hard links, is a second name for it, and its target is
|
|
93
|
+
// walked again from that name, which a target safe where the symlink sits
|
|
94
|
+
// need not survive.
|
|
75
95
|
export function pack(entries: Iterable<EntryInput>, options?: PackOptions): Uint8Array
|
|
76
96
|
export function unpack(bytes: Uint8Array): Entry[]
|
|
77
97
|
|
package/zip.d.ts
CHANGED
|
@@ -14,6 +14,10 @@ export type EntryType = 'file' | 'directory' | 'symlink'
|
|
|
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
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,13 +60,13 @@ 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
|
|
58
|
-
// not a directory, and a symlink whose
|
|
59
|
-
// or passes through anything but a
|
|
60
|
-
// entry carrying a Unicode path extra
|
|
61
|
-
// readers take over the one in the header.
|
|
62
|
-
// takes or makes zip64, so an archive holds at most 65534 entries
|
|
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
|
|