@preventive/archive 1.0.0-beta.1 → 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.1",
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
@@ -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
@@ -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
@@ -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 — 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