@preventive/archive 1.0.0-beta.4 → 1.0.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/compression.d.ts CHANGED
@@ -13,7 +13,10 @@ export interface Options {
13
13
  limit?: number
14
14
  }
15
15
 
16
- // The whole input through one stream, the whole output back.
16
+ // The whole input through one stream, the whole output back. gzip is
17
+ // decompressed past zero bytes after its last member, as gzip(1) and zlib
18
+ // take them for padding, where the platform's stream refuses them; nothing
19
+ // else after the end is passed over.
17
20
  export function compress(bytes: Uint8Array, format: string, options?: Options): Promise<Uint8Array>
18
21
  export function decompress(bytes: Uint8Array, format: string, options?: Options): Promise<Uint8Array>
19
22
 
package/compression.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // The compression front door of @preventive/archive, reached as
2
2
  // `@preventive/archive/compression.js`: bytes through the platform's
3
3
  // CompressionStream and DecompressionStream, whole, with an output bound,
4
- // and with what came out before a failure kept on the error. The zip half
5
- // deflates through the same code.
4
+ // gzip's zero padding passed over, and with what came out before a failure
5
+ // kept on the error. The zip half deflates through the same code.
6
6
  export { CompressionError, compress, decompress, supports } from './src/compression.js'
7
7
  export { ArchiveError } from './src/error.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preventive/archive",
3
- "version": "1.0.0-beta.4",
3
+ "version": "1.0.0",
4
4
  "description": "Strict in-memory tar and zip on browser APIs",
5
5
  "type": "module",
6
6
  "exports": {
@@ -79,7 +79,37 @@ async function through(bytes, Stream, format, limit, verb, into) {
79
79
  }
80
80
 
81
81
  export const compress = (bytes, format, { limit = Infinity } = {}) => through(bytes, CompressionStream, format, limit, 'compress')
82
- export const decompress = (bytes, format, { limit = Infinity } = {}) => through(bytes, DecompressionStream, format, limit, 'decompress')
82
+
83
+ const inflate = (bytes, format, limit) => through(bytes, DecompressionStream, format, limit, 'decompress')
84
+
85
+ // gzip(1) and zlib take zero bytes after the last member as padding, as old
86
+ // tarballs run on in zeros to a whole 10240-byte record, the way npm 1.0
87
+ // packed them through the system's tar; the platform's stream refuses
88
+ // anything there. So gzip that ends in a zero and does not decompress is
89
+ // tried again ending sooner, at the one place it decompresses whole: its
90
+ // last member's last four bytes are the length it inflates to, mod 2^32,
91
+ // so where that is not 0 it ends within three zeros past the last byte that
92
+ // is not; where the member is empty, as zlib writes one, `03 00` and eight
93
+ // zeros, within nine. Only zeros are passed over, where zlib passes over
94
+ // whatever follows one; and each place tried inflates it all again, which
95
+ // only padded data pays for.
96
+ export async function decompress(bytes, format, { limit = Infinity } = {}) {
97
+ try {
98
+ return await inflate(bytes, format, limit)
99
+ } catch (error) {
100
+ if (format !== 'gzip' || !(error instanceof CompressionError) || error.limited || bytes.at(-1) !== 0) throw error
101
+ const last = bytes.findLastIndex((byte) => byte !== 0)
102
+ for (let end = last + 1; end <= Math.min(last + 10, bytes.length - 1); end++) {
103
+ try {
104
+ return await inflate(bytes.subarray(0, end), format, limit)
105
+ } catch (retry) {
106
+ // Output past the bound before the end is past it wherever the end is.
107
+ if (!(retry instanceof CompressionError) || retry.limited) throw retry
108
+ }
109
+ }
110
+ throw error
111
+ }
112
+ }
83
113
 
84
114
  // Into `into`, which bounds the output, and handed back as much of it as
85
115
  // was filled. Not in the front door: the zip half's, whose entries each
package/src/error.js CHANGED
@@ -9,7 +9,7 @@ export class ArchiveError extends Error {
9
9
  }
10
10
 
11
11
  // The same error, placed: for a check that does not know where in the
12
- // archive it is running. Anything but such a refusal — a bug above all —
12
+ // archive it is running. Anything but such a refusal -- a bug above all --
13
13
  // goes on as it was, rather than dressed up as bad input.
14
14
  export function located(fn, at) {
15
15
  try {
package/src/names.js CHANGED
@@ -1,8 +1,8 @@
1
1
  // Name safety, applied before writing an entry and before handing one out
2
2
  // of an archive. A name is a relative path: `.` segments and a directory's
3
3
  // trailing slash are dropped, and what is left has no empty or `..`
4
- // segment, no control character and no backslash — a separator on
5
- // Windows, where `..\` would get past the check on `..` — and does not
4
+ // segment, no control character and no backslash -- a separator on
5
+ // Windows, where `..\` would get past the check on `..` -- and does not
6
6
  // start with a drive letter and colon, which Windows resolves from that
7
7
  // drive rather than from the archive. A symlink target may use `..`, but
8
8
  // is followed from where the link sits and refused if it climbs above the
@@ -17,7 +17,7 @@
17
17
  // A name may repeat only as the same entry again, field for field and byte
18
18
  // for byte (some npm packagers write `d/f` and `d/./f` both): two different
19
19
  // entries under one name would leave the winner to extraction order.
20
- // Collisions a filesystem might add — case, Unicode normalisation — are
20
+ // Collisions a filesystem might add -- case, Unicode normalisation -- are
21
21
  // the filesystem's, not the archive's, and are not looked for.
22
22
 
23
23
  import { sameBytes } from './bytes.js'
@@ -121,7 +121,7 @@ const FIELDS = ['type', 'mode', 'uid', 'gid', 'mtime', 'uname', 'gname', 'linkna
121
121
  // Each name seen is an entry, a directory entry, or a directory implied by
122
122
  // an entry inside it or by a symlink target walking through it; an implied
123
123
  // directory may still be named as one. An entry inside a non-directory is
124
- // refused — that is the shape of a path through a symlink — and so is a
124
+ // refused -- that is the shape of a path through a symlink -- and so is a
125
125
  // symlink target that walks through one, in either order, so no chain of
126
126
  // links leads out of the archive. Whether entries are kept with their data
127
127
  // decides what a repeat can be compared with: the in-memory calls keep
@@ -184,7 +184,7 @@ export class Names {
184
184
  }
185
185
 
186
186
  // A target followed from `at`: it has to stay inside the archive, and every
187
- // path it walks through has to be a directory — each one it stands on short
187
+ // path it walks through has to be a directory -- each one it stands on short
188
188
  // of the last step, the archive root aside. `what` names it in a refusal.
189
189
  #symlink(at, target, what) {
190
190
  const steps = checkSymlinkTarget(at, target)
package/src/tar/header.js CHANGED
@@ -42,9 +42,9 @@ const DEVMAJOR = 329
42
42
  const DEVMINOR = 337
43
43
  const PREFIX = 345
44
44
  // Under the gnu magic: the old GNU sparse map, its isextended flag and the
45
- // file's real size. libarchive reads them whatever the type flag says —
45
+ // file's real size. libarchive reads them whatever the type flag says --
46
46
  // taking the real size as the file's, and the map and any extension blocks
47
- // after the header as its layout — where GNU tar and the rest read them for
47
+ // after the header as its layout -- where GNU tar and the rest read them for
48
48
  // a sparse entry alone.
49
49
  const OLD_SPARSE = 386
50
50
  const OLD_SPARSE_END = 495
@@ -80,7 +80,7 @@ export function writeNumber(block, offset, size, value, gnu) {
80
80
  // Older tars wrote leading spaces, and either spaces or NULs after; a field
81
81
  // left blank (npm's packer wrote uid and gid so for years) is 0, as GNU tar,
82
82
  // libarchive and the rest read it. A NUL ends the number wherever it sits, so
83
- // a field that opens with one is 0 however it goes on — GNU stops there, and
83
+ // a field that opens with one is 0 however it goes on -- GNU stops there, and
84
84
  // reading the digits past it instead would give a size, and so an archive,
85
85
  // that only this package sees. Only a time may be negative.
86
86
  export function readNumber(block, offset, size, what, at, signed = false) {
package/src/tar/pack.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Writes what GNU tar 1.35 writes for the same entries under `--owner=0
2
2
  // --group=0 --numeric-owner` and, for pax, `--pax-option=delete=atime,
3
- // delete=ctime`. Where GNU would cut or substitute — a name too long for
4
- // ustar, a number out of range — this refuses instead.
3
+ // delete=ctime`. Where GNU would cut or substitute -- a name too long for
4
+ // ustar, a number out of range -- this refuses instead.
5
5
 
6
6
  import { EMPTY, concat, isAscii } from '../bytes.js'
7
7
  import { DEFAULT_MODE, checkEntry, wireName } from '../entry.js'
package/src/tar/unpack.js CHANGED
@@ -12,7 +12,7 @@ import { Names, cleanNames } from '../names.js'
12
12
  import { Records, decodePax } from './pax.js'
13
13
  import { decodeUtf8, hasUnsafe, quote } from '../text.js'
14
14
 
15
- // Type flag → name. NUL is the pre-POSIX regular file.
15
+ // Type flag -> name. NUL is the pre-POSIX regular file.
16
16
  const byFlag = (flags) => Object.entries(flags).map(([name, flag]) => [flag, name])
17
17
  const TYPES = new Map([...byFlag(TYPEFLAGS), [0, 'file']])
18
18
  const EXTENDED = new Map(byFlag(EXTENDED_TYPEFLAGS))
package/src/text.js CHANGED
@@ -5,13 +5,13 @@ import { ArchiveError } from './error.js'
5
5
  // short of a surrogate pair, so a long one cannot flood it.
6
6
  export function quote(text) {
7
7
  if (text.length <= 200) return JSON.stringify(text)
8
- return JSON.stringify(`${text.slice(0, text.codePointAt(199) > 0xffff ? 199 : 200)}…`)
8
+ return JSON.stringify(`${text.slice(0, text.codePointAt(199) > 0xffff ? 199 : 200)}\u2026`)
9
9
  }
10
10
 
11
11
  // A C0, DEL or C1 control (Cc is those three and nothing else); a line or
12
12
  // paragraph separator (Zl, Zp: one each); and every bidirectional control
13
- // Unicode names — embeddings, overrides and isolates, and the three marks
14
- // that shift neutral characters about them unseen — since each breaks or
13
+ // Unicode names -- embeddings, overrides and isolates, and the three marks
14
+ // that shift neutral characters about them unseen -- since each breaks or
15
15
  // reorders a name as shown. A backslash too, if asked.
16
16
  const UNSAFE = /[\p{Cc}\p{Zl}\p{Zp}\p{Bidi_Control}]/u
17
17
  const UNSAFE_OR_BACKSLASH = /[\p{Cc}\p{Zl}\p{Zp}\p{Bidi_Control}\\]/u
package/src/zip/format.js CHANGED
@@ -53,7 +53,7 @@ export async function inflate(bytes, size, at) {
53
53
  return out
54
54
  }
55
55
 
56
- // DOS time is two seconds and a year from 1980, in the maker's local time —
56
+ // DOS time is two seconds and a year from 1980, in the maker's local time --
57
57
  // read and written here as UTC.
58
58
  function dos(seconds) {
59
59
  const date = new Date(seconds * 1000)
package/src/zip/unzip.js CHANGED
@@ -1,11 +1,11 @@
1
1
  // A zip archive in, its entries out, in the order of the central directory,
2
2
  // which is what a zip is read from. Strict throughout: the end record has to
3
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
4
+ // entries have to tile everything before that from byte 0 -- no bytes belong
5
5
  // to nothing, no two entries share any. Each local header has to agree with
6
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.
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
9
  // Given a limit, the sizes the entries declare may not add up to more than
10
10
  // it; since each has to come out at exactly its own, that bounds everything
11
11
  // unzip() puts out, and it is known from the central directory alone,
@@ -13,7 +13,7 @@
13
13
  //
14
14
  // One reader serves both calls. It asks for the archive a range at a time:
15
15
  // views of it when it is in memory, and reads of just those bytes when it
16
- // is a Blob — a File, or a file opened with fs.openAsBlob — so a stream
16
+ // is a Blob -- a File, or a file opened with fs.openAsBlob -- so a stream
17
17
  // over one holds no more of it than the entries' names and the entry it is
18
18
  // on. Everything about the layout is checked before the first entry is
19
19
  // handed over; what an entry's own data holds is checked as it is reached.
@@ -71,7 +71,7 @@ function sourceOf(archive) {
71
71
 
72
72
  // A walk forward through records too small to be worth a read each: a
73
73
  // window over [at, at + width), cut short only where the archive ends,
74
- // and over what else the read it came from holds — up to CHUNK bytes past
74
+ // and over what else the read it came from holds -- up to CHUNK bytes past
75
75
  // `at`, and short of `stop`, so that the records after it come in the same
76
76
  // read and nothing past them is read to no purpose.
77
77
  const CHUNK = 1 << 16
@@ -116,7 +116,7 @@ function extras(raw, at) {
116
116
  if (pos + 4 + size > raw.length) throw new ArchiveError('an extra field runs past its room', at)
117
117
  if (id === ZIP64_EXTRA) throw new ArchiveError('zip64 is not supported', at)
118
118
  // Every name here is UTF-8 already, so this field can only disagree with
119
- // the name checked above — and unzip would extract the name it carries.
119
+ // the name checked above -- and unzip would extract the name it carries.
120
120
  if (id === UNICODE_PATH_EXTRA) throw new ArchiveError('an entry carries a second name in a Unicode path extra field', at)
121
121
  if (!fields.has(id)) fields.set(id, raw.subarray(pos + 4, pos + 4 + size))
122
122
  pos += 4 + size
@@ -165,8 +165,8 @@ async function readCentral(read, at) {
165
165
  // The local header checked against the central entry; returns where the
166
166
  // entry's bytes end, which has to be `boundary`, the next record's start,
167
167
  // and notes where its data starts. The two records have to agree on
168
- // everything both carry — flags included, since a descriptor bit set in
169
- // one and not the other is read two ways by two readers — and the local
168
+ // everything both carry -- flags included, since a descriptor bit set in
169
+ // one and not the other is read two ways by two readers -- and the local
170
170
  // sizes and CRC may be 0 only where a descriptor carries them. `stop` is
171
171
  // as far as the header is read ahead of itself.
172
172
  async function readLocal(read, entry, boundary, stop) {
@@ -201,7 +201,7 @@ async function readLocal(read, entry, boundary, stop) {
201
201
  return end
202
202
  }
203
203
 
204
- // A directory marker anywhere — the Unix type bits, the DOS bit — needs
204
+ // A directory marker anywhere -- the Unix type bits, the DOS bit -- needs
205
205
  // the slash; the slash needs no marker, since Java writes no attributes at
206
206
  // all and every JAR has directories.
207
207
  function typeOf(entry, rawName, mode) {
package/tar.d.ts CHANGED
@@ -23,14 +23,14 @@ export type EntryType =
23
23
  //
24
24
  // Beside those, what the archive itself says. `storedName` and
25
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
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
28
  // hard link's target is ever cleaned, so a symlink's is its `linkname`.
29
29
  // `pax` is the records of the entry's own pax header and `globalPax` those
30
30
  // of the global header it is under, each in the order stored, those read
31
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
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
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
36
  export interface Entry {
@@ -84,8 +84,8 @@ export interface PackOptions {
84
84
  blocking?: number
85
85
  }
86
86
 
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
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
89
  // entry inside something that is not a directory, a symlink whose target
90
90
  // climbs out of the archive or passes through anything but a directory, and
91
91
  // a hard link to no earlier entry. A hard link to a symlink, directly or
package/zip.d.ts CHANGED
@@ -14,8 +14,8 @@ 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
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
19
  // check the cleaned one did. A symlink's target is never rewritten, only
20
20
  // checked, so `linkname` is as stored.
21
21
  export interface Entry {
@@ -50,7 +50,7 @@ export interface ZipOptions {
50
50
  method?: 'deflate' | 'store'
51
51
  }
52
52
 
53
- // `limit` bounds the entries' sizes, all of them together, in bytes —
53
+ // `limit` bounds the entries' sizes, all of them together, in bytes --
54
54
  // stored and deflated alike, a symlink's target included. Each entry has to
55
55
  // come out at exactly the size it declares, so the sum is checked against
56
56
  // the central directory before anything is inflated, and past the limit
@@ -59,8 +59,8 @@ export interface UnzipOptions {
59
59
  limit?: number
60
60
  }
61
61
 
62
- // Both refuse a name that repeats as a different entry — anything but the
63
- // same fields and the same bytes again, however either was stored — an
62
+ // Both refuse a name that repeats as a different entry -- anything but the
63
+ // same fields and the same bytes again, however either was stored -- an
64
64
  // entry inside something that is not a directory, and a symlink whose
65
65
  // target climbs out of the archive or passes through anything but a
66
66
  // directory. unzip() also refuses an entry carrying a Unicode path extra
@@ -71,7 +71,7 @@ export function zip(entries: Iterable<EntryInput>, options?: ZipOptions): Promis
71
71
  export function unzip(bytes: Uint8Array, options?: UnzipOptions): Promise<Entry[]>
72
72
 
73
73
  // The same one entry at a time, as an async generator: over the archive in
74
- // memory, or over a Blob — a File, or a file opened with fs.openAsBlob —
74
+ // memory, or over a Blob -- a File, or a file opened with fs.openAsBlob --
75
75
  // read a range at a time, so that no more of it is held than the entries'
76
76
  // names and the entry it is on, whose data is then its own rather than a
77
77
  // view of the archive.
package/zip.js CHANGED
@@ -8,13 +8,13 @@
8
8
  // refused rather than written down wrong.
9
9
  export { zip } from './src/zip/zip.js'
10
10
 
11
- // An archive in, its entries out, under the same rules — and with every
11
+ // An archive in, its entries out, under the same rules -- and with every
12
12
  // header, size and checksum in it checked against every other, and the
13
13
  // sizes all together held to a limit where one is given.
14
14
  export { unzip } from './src/zip/unzip.js'
15
15
 
16
16
  // The same an entry at a time, as an async generator over the archive in
17
- // memory or over a Blob — a File, or a file opened with fs.openAsBlob —
17
+ // memory or over a Blob -- a File, or a file opened with fs.openAsBlob --
18
18
  // read a range at a time, so no more of it is held than the entry it is on.
19
19
  export { unzipStream } from './src/zip/unzip.js'
20
20