tar 7.5.24 → 8.0.1
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/README.md +1712 -1147
- package/dist/commonjs/bytes.d.ts +21 -0
- package/dist/commonjs/bytes.d.ts.map +1 -0
- package/dist/commonjs/bytes.js +83 -0
- package/dist/commonjs/bytes.js.map +1 -0
- package/dist/commonjs/codec.d.ts +53 -0
- package/dist/commonjs/codec.d.ts.map +1 -0
- package/dist/commonjs/codec.js +35 -0
- package/dist/commonjs/codec.js.map +1 -0
- package/dist/commonjs/concat.d.ts +3 -0
- package/dist/commonjs/concat.d.ts.map +1 -0
- package/dist/commonjs/concat.js +20 -0
- package/dist/commonjs/concat.js.map +1 -0
- package/dist/commonjs/create.d.ts +39 -2
- package/dist/commonjs/create.d.ts.map +1 -1
- package/dist/commonjs/create.js +150 -64
- package/dist/commonjs/create.js.map +1 -1
- package/dist/commonjs/dealias.d.ts +79 -0
- package/dist/commonjs/dealias.d.ts.map +1 -0
- package/dist/commonjs/dealias.js +50 -0
- package/dist/commonjs/dealias.js.map +1 -0
- package/dist/commonjs/delete.d.ts +19 -0
- package/dist/commonjs/delete.d.ts.map +1 -0
- package/dist/commonjs/delete.js +121 -0
- package/dist/commonjs/delete.js.map +1 -0
- package/dist/commonjs/entries.d.ts +82 -0
- package/dist/commonjs/entries.d.ts.map +1 -0
- package/dist/commonjs/entries.js +398 -0
- package/dist/commonjs/entries.js.map +1 -0
- package/dist/commonjs/extract.d.ts +14 -2
- package/dist/commonjs/extract.d.ts.map +1 -1
- package/dist/commonjs/extract.js +40 -81
- package/dist/commonjs/extract.js.map +1 -1
- package/dist/commonjs/fs.d.ts +201 -0
- package/dist/commonjs/fs.d.ts.map +1 -0
- package/dist/commonjs/fs.js +5 -0
- package/dist/commonjs/fs.js.map +1 -0
- package/dist/commonjs/header.d.ts +4 -4
- package/dist/commonjs/header.d.ts.map +1 -1
- package/dist/commonjs/header.js +38 -42
- package/dist/commonjs/header.js.map +1 -1
- package/dist/commonjs/index.d.ts +25 -18
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +46 -18
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/index.min.js +2 -3
- package/dist/commonjs/index.min.js.map +4 -4
- package/dist/commonjs/large-numbers.d.ts +2 -2
- package/dist/commonjs/large-numbers.d.ts.map +1 -1
- package/dist/commonjs/large-numbers.js +4 -4
- package/dist/commonjs/large-numbers.js.map +1 -1
- package/dist/commonjs/list.d.ts +23 -6
- package/dist/commonjs/list.d.ts.map +1 -1
- package/dist/commonjs/list.js +99 -138
- package/dist/commonjs/list.js.map +1 -1
- package/dist/commonjs/mkdir.d.ts +16 -11
- package/dist/commonjs/mkdir.d.ts.map +1 -1
- package/dist/commonjs/mkdir.js +121 -159
- package/dist/commonjs/mkdir.js.map +1 -1
- package/dist/commonjs/options.d.ts +284 -513
- package/dist/commonjs/options.d.ts.map +1 -1
- package/dist/commonjs/options.js +20 -61
- package/dist/commonjs/options.js.map +1 -1
- package/dist/commonjs/pack.d.ts +150 -96
- package/dist/commonjs/pack.d.ts.map +1 -1
- package/dist/commonjs/pack.js +752 -445
- package/dist/commonjs/pack.js.map +1 -1
- package/dist/commonjs/parser.d.ts +94 -0
- package/dist/commonjs/parser.d.ts.map +1 -0
- package/dist/commonjs/parser.js +381 -0
- package/dist/commonjs/parser.js.map +1 -0
- package/dist/commonjs/path-reservations.d.ts +17 -7
- package/dist/commonjs/path-reservations.d.ts.map +1 -1
- package/dist/commonjs/path-reservations.js +96 -119
- package/dist/commonjs/path-reservations.js.map +1 -1
- package/dist/commonjs/path.d.ts +19 -0
- package/dist/commonjs/path.d.ts.map +1 -0
- package/dist/commonjs/path.js +90 -0
- package/dist/commonjs/path.js.map +1 -0
- package/dist/commonjs/pax.d.ts +3 -1
- package/dist/commonjs/pax.d.ts.map +1 -1
- package/dist/commonjs/pax.js +53 -43
- package/dist/commonjs/pax.js.map +1 -1
- package/dist/commonjs/read-entry.d.ts +97 -11
- package/dist/commonjs/read-entry.d.ts.map +1 -1
- package/dist/commonjs/read-entry.js +248 -97
- package/dist/commonjs/read-entry.js.map +1 -1
- package/dist/commonjs/read-file.d.ts +22 -0
- package/dist/commonjs/read-file.d.ts.map +1 -0
- package/dist/commonjs/read-file.js +104 -0
- package/dist/commonjs/read-file.js.map +1 -0
- package/dist/commonjs/replace.d.ts +45 -1
- package/dist/commonjs/replace.d.ts.map +1 -1
- package/dist/commonjs/replace.js +165 -211
- package/dist/commonjs/replace.js.map +1 -1
- package/dist/commonjs/server/codecs.d.ts +38 -0
- package/dist/commonjs/server/codecs.d.ts.map +1 -0
- package/dist/commonjs/server/codecs.js +252 -0
- package/dist/commonjs/server/codecs.js.map +1 -0
- package/dist/commonjs/server/index.d.ts +115 -0
- package/dist/commonjs/server/index.d.ts.map +1 -0
- package/dist/commonjs/server/index.js +166 -0
- package/dist/commonjs/server/index.js.map +1 -0
- package/dist/commonjs/server/index.min.js +3 -0
- package/dist/commonjs/server/index.min.js.map +7 -0
- package/dist/commonjs/strip-absolute-path.d.ts +8 -0
- package/dist/commonjs/strip-absolute-path.d.ts.map +1 -1
- package/dist/commonjs/strip-absolute-path.js +17 -20
- package/dist/commonjs/strip-absolute-path.js.map +1 -1
- package/dist/commonjs/to-stream.d.ts +7 -0
- package/dist/commonjs/to-stream.d.ts.map +1 -0
- package/dist/commonjs/to-stream.js +22 -0
- package/dist/commonjs/to-stream.js.map +1 -0
- package/dist/commonjs/types.d.ts +4 -3
- package/dist/commonjs/types.d.ts.map +1 -1
- package/dist/commonjs/types.js +4 -3
- package/dist/commonjs/types.js.map +1 -1
- package/dist/commonjs/unpack.d.ts +143 -89
- package/dist/commonjs/unpack.d.ts.map +1 -1
- package/dist/commonjs/unpack.js +782 -725
- package/dist/commonjs/unpack.js.map +1 -1
- package/dist/commonjs/update.d.ts +11 -1
- package/dist/commonjs/update.d.ts.map +1 -1
- package/dist/commonjs/update.js +37 -28
- package/dist/commonjs/update.js.map +1 -1
- package/dist/commonjs/warn.d.ts +27 -0
- package/dist/commonjs/warn.d.ts.map +1 -0
- package/dist/commonjs/warn.js +36 -0
- package/dist/commonjs/warn.js.map +1 -0
- package/dist/commonjs/win32-path.d.ts +16 -0
- package/dist/commonjs/win32-path.d.ts.map +1 -0
- package/dist/commonjs/win32-path.js +67 -0
- package/dist/commonjs/win32-path.js.map +1 -0
- package/dist/commonjs/write-entry.d.ts +64 -111
- package/dist/commonjs/write-entry.d.ts.map +1 -1
- package/dist/commonjs/write-entry.js +108 -672
- package/dist/commonjs/write-entry.js.map +1 -1
- package/dist/commonjs/write-flag.d.ts +18 -0
- package/dist/commonjs/write-flag.d.ts.map +1 -0
- package/dist/commonjs/write-flag.js +29 -0
- package/dist/commonjs/write-flag.js.map +1 -0
- package/dist/esm/bytes.d.ts +21 -0
- package/dist/esm/bytes.d.ts.map +1 -0
- package/dist/esm/bytes.js +72 -0
- package/dist/esm/bytes.js.map +1 -0
- package/dist/esm/codec.d.ts +53 -0
- package/dist/esm/codec.d.ts.map +1 -0
- package/dist/esm/codec.js +32 -0
- package/dist/esm/codec.js.map +1 -0
- package/dist/esm/concat.d.ts +3 -0
- package/dist/esm/concat.d.ts.map +1 -0
- package/dist/esm/concat.js +16 -0
- package/dist/esm/concat.js.map +1 -0
- package/dist/esm/create.d.ts +39 -2
- package/dist/esm/create.d.ts.map +1 -1
- package/dist/esm/create.js +144 -61
- package/dist/esm/create.js.map +1 -1
- package/dist/esm/dealias.d.ts +79 -0
- package/dist/esm/dealias.d.ts.map +1 -0
- package/dist/esm/dealias.js +46 -0
- package/dist/esm/dealias.js.map +1 -0
- package/dist/esm/delete.d.ts +19 -0
- package/dist/esm/delete.d.ts.map +1 -0
- package/dist/esm/delete.js +117 -0
- package/dist/esm/delete.js.map +1 -0
- package/dist/esm/entries.d.ts +82 -0
- package/dist/esm/entries.d.ts.map +1 -0
- package/dist/esm/entries.js +388 -0
- package/dist/esm/entries.js.map +1 -0
- package/dist/esm/extract.d.ts +14 -2
- package/dist/esm/extract.d.ts.map +1 -1
- package/dist/esm/extract.js +37 -44
- package/dist/esm/extract.js.map +1 -1
- package/dist/esm/fs.d.ts +201 -0
- package/dist/esm/fs.d.ts.map +1 -0
- package/dist/esm/fs.js +4 -0
- package/dist/esm/fs.js.map +1 -0
- package/dist/esm/header.d.ts +4 -4
- package/dist/esm/header.d.ts.map +1 -1
- package/dist/esm/header.js +38 -42
- package/dist/esm/header.js.map +1 -1
- package/dist/esm/index.d.ts +25 -18
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +23 -18
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.min.js +2 -3
- package/dist/esm/index.min.js.map +4 -4
- package/dist/esm/large-numbers.d.ts +2 -2
- package/dist/esm/large-numbers.d.ts.map +1 -1
- package/dist/esm/large-numbers.js +4 -4
- package/dist/esm/large-numbers.js.map +1 -1
- package/dist/esm/list.d.ts +23 -6
- package/dist/esm/list.d.ts.map +1 -1
- package/dist/esm/list.js +96 -101
- package/dist/esm/list.js.map +1 -1
- package/dist/esm/mkdir.d.ts +16 -11
- package/dist/esm/mkdir.d.ts.map +1 -1
- package/dist/esm/mkdir.js +119 -154
- package/dist/esm/mkdir.js.map +1 -1
- package/dist/esm/options.d.ts +284 -513
- package/dist/esm/options.d.ts.map +1 -1
- package/dist/esm/options.js +17 -51
- package/dist/esm/options.js.map +1 -1
- package/dist/esm/pack.d.ts +150 -96
- package/dist/esm/pack.d.ts.map +1 -1
- package/dist/esm/pack.js +750 -441
- package/dist/esm/pack.js.map +1 -1
- package/dist/esm/parser.d.ts +94 -0
- package/dist/esm/parser.d.ts.map +1 -0
- package/dist/esm/parser.js +377 -0
- package/dist/esm/parser.js.map +1 -0
- package/dist/esm/path-reservations.d.ts +17 -7
- package/dist/esm/path-reservations.d.ts.map +1 -1
- package/dist/esm/path-reservations.js +96 -119
- package/dist/esm/path-reservations.js.map +1 -1
- package/dist/esm/path.d.ts +19 -0
- package/dist/esm/path.d.ts.map +1 -0
- package/dist/esm/path.js +80 -0
- package/dist/esm/path.js.map +1 -0
- package/dist/esm/pax.d.ts +3 -1
- package/dist/esm/pax.d.ts.map +1 -1
- package/dist/esm/pax.js +52 -42
- package/dist/esm/pax.js.map +1 -1
- package/dist/esm/read-entry.d.ts +97 -11
- package/dist/esm/read-entry.d.ts.map +1 -1
- package/dist/esm/read-entry.js +245 -96
- package/dist/esm/read-entry.js.map +1 -1
- package/dist/esm/read-file.d.ts +22 -0
- package/dist/esm/read-file.d.ts.map +1 -0
- package/dist/esm/read-file.js +99 -0
- package/dist/esm/read-file.js.map +1 -0
- package/dist/esm/replace.d.ts +45 -1
- package/dist/esm/replace.d.ts.map +1 -1
- package/dist/esm/replace.js +161 -207
- package/dist/esm/replace.js.map +1 -1
- package/dist/esm/server/codecs.d.ts +38 -0
- package/dist/esm/server/codecs.d.ts.map +1 -0
- package/dist/esm/server/codecs.js +214 -0
- package/dist/esm/server/codecs.js.map +1 -0
- package/dist/esm/server/index.d.ts +115 -0
- package/dist/esm/server/index.d.ts.map +1 -0
- package/dist/esm/server/index.js +102 -0
- package/dist/esm/server/index.js.map +1 -0
- package/dist/esm/server/index.min.js +3 -0
- package/dist/esm/server/index.min.js.map +7 -0
- package/dist/esm/strip-absolute-path.d.ts +8 -0
- package/dist/esm/strip-absolute-path.d.ts.map +1 -1
- package/dist/esm/strip-absolute-path.js +17 -20
- package/dist/esm/strip-absolute-path.js.map +1 -1
- package/dist/esm/to-stream.d.ts +7 -0
- package/dist/esm/to-stream.d.ts.map +1 -0
- package/dist/esm/to-stream.js +18 -0
- package/dist/esm/to-stream.js.map +1 -0
- package/dist/esm/types.d.ts +4 -3
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/types.js +4 -3
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/unpack.d.ts +143 -89
- package/dist/esm/unpack.d.ts.map +1 -1
- package/dist/esm/unpack.js +778 -719
- package/dist/esm/unpack.js.map +1 -1
- package/dist/esm/update.d.ts +11 -1
- package/dist/esm/update.d.ts.map +1 -1
- package/dist/esm/update.js +35 -28
- package/dist/esm/update.js.map +1 -1
- package/dist/esm/warn.d.ts +27 -0
- package/dist/esm/warn.d.ts.map +1 -0
- package/dist/esm/warn.js +31 -0
- package/dist/esm/warn.js.map +1 -0
- package/dist/esm/win32-path.d.ts +16 -0
- package/dist/esm/win32-path.d.ts.map +1 -0
- package/dist/esm/win32-path.js +60 -0
- package/dist/esm/win32-path.js.map +1 -0
- package/dist/esm/write-entry.d.ts +64 -111
- package/dist/esm/write-entry.d.ts.map +1 -1
- package/dist/esm/write-entry.js +107 -633
- package/dist/esm/write-entry.js.map +1 -1
- package/dist/esm/write-flag.d.ts +18 -0
- package/dist/esm/write-flag.d.ts.map +1 -0
- package/dist/esm/write-flag.js +25 -0
- package/dist/esm/write-flag.js.map +1 -0
- package/package.json +68 -224
- package/dist/commonjs/get-write-flag.d.ts +0 -2
- package/dist/commonjs/get-write-flag.d.ts.map +0 -1
- package/dist/commonjs/get-write-flag.js +0 -32
- package/dist/commonjs/get-write-flag.js.map +0 -1
- package/dist/commonjs/make-command.d.ts +0 -49
- package/dist/commonjs/make-command.d.ts.map +0 -1
- package/dist/commonjs/make-command.js +0 -52
- package/dist/commonjs/make-command.js.map +0 -1
- package/dist/commonjs/normalize-windows-path.d.ts +0 -2
- package/dist/commonjs/normalize-windows-path.d.ts.map +0 -1
- package/dist/commonjs/normalize-windows-path.js +0 -12
- package/dist/commonjs/normalize-windows-path.js.map +0 -1
- package/dist/commonjs/parse.d.ts +0 -92
- package/dist/commonjs/parse.d.ts.map +0 -1
- package/dist/commonjs/parse.js +0 -655
- package/dist/commonjs/parse.js.map +0 -1
- package/dist/commonjs/process-umask.d.ts +0 -2
- package/dist/commonjs/process-umask.d.ts.map +0 -1
- package/dist/commonjs/process-umask.js +0 -7
- package/dist/commonjs/process-umask.js.map +0 -1
- package/dist/commonjs/warn-method.d.ts +0 -24
- package/dist/commonjs/warn-method.d.ts.map +0 -1
- package/dist/commonjs/warn-method.js +0 -31
- package/dist/commonjs/warn-method.js.map +0 -1
- package/dist/esm/get-write-flag.d.ts +0 -2
- package/dist/esm/get-write-flag.d.ts.map +0 -1
- package/dist/esm/get-write-flag.js +0 -26
- package/dist/esm/get-write-flag.js.map +0 -1
- package/dist/esm/make-command.d.ts +0 -49
- package/dist/esm/make-command.d.ts.map +0 -1
- package/dist/esm/make-command.js +0 -48
- package/dist/esm/make-command.js.map +0 -1
- package/dist/esm/normalize-windows-path.d.ts +0 -2
- package/dist/esm/normalize-windows-path.d.ts.map +0 -1
- package/dist/esm/normalize-windows-path.js +0 -9
- package/dist/esm/normalize-windows-path.js.map +0 -1
- package/dist/esm/parse.d.ts +0 -92
- package/dist/esm/parse.d.ts.map +0 -1
- package/dist/esm/parse.js +0 -651
- package/dist/esm/parse.js.map +0 -1
- package/dist/esm/process-umask.d.ts +0 -2
- package/dist/esm/process-umask.d.ts.map +0 -1
- package/dist/esm/process-umask.js +0 -3
- package/dist/esm/process-umask.js.map +0 -1
- package/dist/esm/warn-method.d.ts +0 -24
- package/dist/esm/warn-method.d.ts.map +0 -1
- package/dist/esm/warn-method.js +0 -27
- package/dist/esm/warn-method.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,1233 +1,1798 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
It is frankly unlikely that any tar implementation in JavaScript
|
|
44
|
-
is going to be as secure as this one, unless a similar amount of
|
|
45
|
-
work is put into it, putting it to the test over many years of
|
|
46
|
-
intensive use and scrutiny. You can vibe-code a tar extractor in
|
|
47
|
-
an afternoon, but you'll regret it.
|
|
48
|
-
|
|
49
|
-
> [!WARNING]
|
|
50
|
-
>
|
|
51
|
-
> **However**, all that being said, _care must still be taken_
|
|
52
|
-
> when dealing with data from unknown sources, especially when
|
|
53
|
-
> extracting files, with this or any library, no matter how
|
|
54
|
-
> hardened it may be. It is _your_ responsibility to use this
|
|
55
|
-
> library safely.
|
|
56
|
-
|
|
57
|
-
1. **NEVER** extract tarball data into a folder that could be
|
|
58
|
-
potentially controlled by an unknown actor. A clever attacker
|
|
59
|
-
can swap out the target of an extracted file with a symbolic
|
|
60
|
-
link to some location of their choosing, resulting in writing
|
|
61
|
-
files outside the target folder. There is no reasonable way to
|
|
62
|
-
harden against this category of attack, and security reports
|
|
63
|
-
about it will be closed.
|
|
64
|
-
[TOCTOU](https://cwe.mitre.org/data/definitions/367.html)
|
|
65
|
-
exposure is unavoidable when creating files based on entries
|
|
66
|
-
in an archive file.
|
|
67
|
-
2. If you are unpacking tarballs that may come from an unknown
|
|
68
|
-
source, it is **highly recommended** that you use a filter
|
|
69
|
-
function that rejects all hardlinks and symbolic links. Link
|
|
70
|
-
files are historically the root of nearly every file
|
|
71
|
-
extraction vulnerability. (npm filters links out of package
|
|
72
|
-
artifacts for this reason.)
|
|
73
|
-
3. If you are extracting tarballs that are compressed (eg, with
|
|
74
|
-
gzip, brotli, or zstd), then it is a very good idea to also
|
|
75
|
-
filter out any files that are excessively large. Even if you
|
|
76
|
-
are restricting the size of the archive file itself, an
|
|
77
|
-
excessively large file of repetitive data can compress down
|
|
78
|
-
very small, and extract to take up a lot of disk space.
|
|
79
|
-
4. **Stay up to date.** Old versions of tar are not maintained or
|
|
80
|
-
tested for newly discovered security advisories, and should be
|
|
81
|
-
assumed to contain every known security vulnerability, and
|
|
82
|
-
many that are unknown.
|
|
83
|
-
|
|
84
|
-
If you find a security vulnerability in node-tar, where it is not
|
|
85
|
-
properly enforcing the intended security protections, then please
|
|
86
|
-
report it using the GitHub Security Advisories system, where it
|
|
87
|
-
will be triaged and corrected if possible.
|
|
88
|
-
|
|
89
|
-
## Background
|
|
90
|
-
|
|
91
|
-
A "tar file" or "tarball" is an archive of file system entries
|
|
92
|
-
(directories, files, links, etc.) The name comes from "tape archive".
|
|
93
|
-
If you run `man tar` on almost any Unix command line, you'll learn
|
|
94
|
-
quite a bit about what it can do, and its history.
|
|
95
|
-
|
|
96
|
-
Tar has 5 main top-level commands:
|
|
97
|
-
|
|
98
|
-
- `c` Create an archive
|
|
99
|
-
- `r` Replace entries within an archive
|
|
100
|
-
- `u` Update entries within an archive (ie, replace if they're newer)
|
|
101
|
-
- `t` List out the contents of an archive
|
|
102
|
-
- `x` Extract an archive to disk
|
|
103
|
-
|
|
104
|
-
The other flags and options modify how this top level function works.
|
|
105
|
-
|
|
106
|
-
## High-Level API
|
|
107
|
-
|
|
108
|
-
These 5 functions are the high-level API. All of them have a
|
|
109
|
-
single-character name (for unix nerds familiar with `tar(1)`) as well
|
|
110
|
-
as a long name (for everyone else).
|
|
111
|
-
|
|
112
|
-
All the high-level functions take the following arguments, all three
|
|
113
|
-
of which are optional and may be omitted.
|
|
114
|
-
|
|
115
|
-
1. `options` - An optional object specifying various options
|
|
116
|
-
2. `paths` - An array of paths to add or extract
|
|
117
|
-
3. `callback` - Called when the command is completed, if async. (If
|
|
118
|
-
sync or no file specified, providing a callback throws a
|
|
119
|
-
`TypeError`.)
|
|
120
|
-
|
|
121
|
-
If the command is sync (ie, if `options.sync=true`), then the
|
|
122
|
-
callback is not allowed, since the action will be completed immediately.
|
|
123
|
-
|
|
124
|
-
If a `file` argument is specified, and the command is async, then a
|
|
125
|
-
`Promise` is returned. In this case, if async, a callback may be
|
|
126
|
-
provided which is called when the command is completed.
|
|
127
|
-
|
|
128
|
-
If a `file` option is not specified, then a stream is returned. For
|
|
129
|
-
`create`, this is a readable stream of the generated archive. For
|
|
130
|
-
`list` and `extract` this is a writable stream that an archive should
|
|
131
|
-
be written into. If a file is not specified, then a callback is not
|
|
132
|
-
allowed, because you're already getting a stream to work with.
|
|
133
|
-
|
|
134
|
-
`replace` and `update` only work on existing archives, and so require
|
|
135
|
-
a `file` argument.
|
|
136
|
-
|
|
137
|
-
Sync commands without a file argument return a stream that acts on its
|
|
138
|
-
input immediately in the same tick. For readable streams, this means
|
|
139
|
-
that all of the data is immediately available by calling
|
|
140
|
-
`stream.read()`. For writable streams, it will be acted upon as soon
|
|
141
|
-
as it is provided, but this can be at any time.
|
|
142
|
-
|
|
143
|
-
### Warnings and Errors
|
|
144
|
-
|
|
145
|
-
Tar emits warnings and errors for recoverable and unrecoverable situations,
|
|
146
|
-
respectively. In many cases, a warning only affects a single entry in an
|
|
147
|
-
archive, or is simply informing you that it's modifying an entry to comply
|
|
148
|
-
with the settings provided.
|
|
149
|
-
|
|
150
|
-
Unrecoverable warnings will always raise an error (ie, emit `'error'` on
|
|
151
|
-
streaming actions, throw for non-streaming sync actions, reject the
|
|
152
|
-
returned Promise for non-streaming async operations, or call a provided
|
|
153
|
-
callback with an `Error` as the first argument). Recoverable errors will
|
|
154
|
-
raise an error only if `strict: true` is set in the options.
|
|
155
|
-
|
|
156
|
-
Respond to (recoverable) warnings by listening to the `warn` event.
|
|
157
|
-
Handlers receive 3 arguments:
|
|
158
|
-
|
|
159
|
-
- `code` String. One of the error codes below. This may not match
|
|
160
|
-
`data.code`, which preserves the original error code from fs and zlib.
|
|
161
|
-
- `message` String. More details about the error.
|
|
162
|
-
- `data` Metadata about the error. An `Error` object for errors raised by
|
|
163
|
-
fs and zlib. All fields are attached to errors raisd by tar. Typically
|
|
164
|
-
contains the following fields, as relevant:
|
|
165
|
-
- `tarCode` The tar error code.
|
|
166
|
-
- `code` Either the tar error code, or the error code set by the
|
|
167
|
-
underlying system.
|
|
168
|
-
- `file` The archive file being read or written.
|
|
169
|
-
- `cwd` Working directory for creation and extraction operations.
|
|
170
|
-
- `entry` The entry object (if it could be created) for `TAR_ENTRY_INFO`,
|
|
171
|
-
`TAR_ENTRY_INVALID`, and `TAR_ENTRY_ERROR` warnings.
|
|
172
|
-
- `header` The header object (if it could be created, and the entry could
|
|
173
|
-
not be created) for `TAR_ENTRY_INFO` and `TAR_ENTRY_INVALID` warnings.
|
|
174
|
-
- `recoverable` Boolean. If `false`, then the warning will emit an
|
|
175
|
-
`error`, even in non-strict mode.
|
|
176
|
-
|
|
177
|
-
#### Error Codes
|
|
178
|
-
|
|
179
|
-
- `TAR_ENTRY_INFO` An informative error indicating that an entry is being
|
|
180
|
-
modified, but otherwise processed normally. For example, removing `/` or
|
|
181
|
-
`C:\` from absolute paths if `preservePaths` is not set.
|
|
182
|
-
|
|
183
|
-
- `TAR_ENTRY_INVALID` An indication that a given entry is not a valid tar
|
|
184
|
-
archive entry, and will be skipped. This occurs when:
|
|
185
|
-
- a checksum fails,
|
|
186
|
-
- a `linkpath` is missing for a link type, or
|
|
187
|
-
- a `linkpath` is provided for a non-link type.
|
|
188
|
-
|
|
189
|
-
If every entry in a parsed archive raises an `TAR_ENTRY_INVALID` error,
|
|
190
|
-
then the archive is presumed to be unrecoverably broken, and
|
|
191
|
-
`TAR_BAD_ARCHIVE` will be raised.
|
|
192
|
-
|
|
193
|
-
- `TAR_ENTRY_ERROR` The entry appears to be a valid tar archive entry, but
|
|
194
|
-
encountered an error which prevented it from being unpacked. This occurs
|
|
195
|
-
when:
|
|
196
|
-
- an unrecoverable fs error happens during unpacking,
|
|
197
|
-
- an entry is trying to extract into an excessively deep
|
|
198
|
-
location (by default, limited to 1024 subfolders),
|
|
199
|
-
- an entry has `..` in the path and `preservePaths` is not set, or
|
|
200
|
-
- an entry is extracting through a symbolic link, when `preservePaths` is
|
|
201
|
-
not set.
|
|
202
|
-
|
|
203
|
-
- `TAR_ENTRY_UNSUPPORTED` An indication that a given entry is
|
|
204
|
-
a valid archive entry, but of a type that is unsupported, and so will be
|
|
205
|
-
skipped in archive creation or extracting.
|
|
206
|
-
|
|
207
|
-
- `TAR_ABORT` When parsing gzipped-encoded archives, the parser will
|
|
208
|
-
abort the parse process raise a warning for any zlib errors encountered.
|
|
209
|
-
Aborts are considered unrecoverable for both parsing and unpacking.
|
|
210
|
-
|
|
211
|
-
- `TAR_BAD_ARCHIVE` The archive file is totally hosed. This can happen for
|
|
212
|
-
a number of reasons, and always occurs at the end of a parse or extract:
|
|
213
|
-
- An entry body was truncated before seeing the full number of bytes.
|
|
214
|
-
- The archive contained only invalid entries, indicating that it is
|
|
215
|
-
likely not an archive, or at least, not an archive this library can
|
|
216
|
-
parse.
|
|
217
|
-
|
|
218
|
-
`TAR_BAD_ARCHIVE` is considered informative for parse operations, but
|
|
219
|
-
unrecoverable for extraction. Note that, if encountered at the end of an
|
|
220
|
-
extraction, tar WILL still have extracted as much it could from the
|
|
221
|
-
archive, so there may be some garbage files to clean up.
|
|
222
|
-
|
|
223
|
-
Errors that occur deeper in the system (ie, either the filesystem or zlib)
|
|
224
|
-
will have their error codes left intact, and a `tarCode` matching one of
|
|
225
|
-
the above will be added to the warning metadata or the raised error object.
|
|
226
|
-
|
|
227
|
-
Errors generated by tar will have one of the above codes set as the
|
|
228
|
-
`error.code` field as well, but since errors originating in zlib or fs will
|
|
229
|
-
have their original codes, it's better to read `error.tarCode` if you wish
|
|
230
|
-
to see how tar is handling the issue.
|
|
231
|
-
|
|
232
|
-
### Examples
|
|
233
|
-
|
|
234
|
-
The API mimics the `tar(1)` command line functionality, with aliases
|
|
235
|
-
for more human-readable option and function names. The goal is that
|
|
236
|
-
if you know how to use `tar(1)` in Unix, then you know how to use
|
|
237
|
-
`import('tar')` in JavaScript.
|
|
238
|
-
|
|
239
|
-
To replicate `tar czf my-tarball.tgz files and folders`, you'd do:
|
|
1
|
+
# tar
|
|
2
|
+
|
|
3
|
+
Tar for JavaScript.
|
|
4
|
+
|
|
5
|
+
This package reads and writes tar archives. It makes archives from
|
|
6
|
+
files or from data in memory. It extracts archives to a file system,
|
|
7
|
+
or gives you the entries one at a time. It reads and writes gzip, and
|
|
8
|
+
in a server-side runtime also brotli and zstd.
|
|
9
|
+
|
|
10
|
+
The package has two entry points:
|
|
11
|
+
|
|
12
|
+
- `tar` is for server-side runtimes: Node.js, and each other runtime
|
|
13
|
+
that has the `node:` modules, such as Bun, Deno, and Cloudflare. It
|
|
14
|
+
uses `node:fs` and `node:zlib`, and it has sync commands. Most users
|
|
15
|
+
want this entry point.
|
|
16
|
+
- `tar/core` is for browsers and each other runtime. It uses no
|
|
17
|
+
`node:` module. It runs in a browser, in a worker, and in each
|
|
18
|
+
runtime that has web streams. It needs the `fs` option for work on
|
|
19
|
+
files.
|
|
20
|
+
|
|
21
|
+
The package has no runtime dependencies. The API uses promises, web
|
|
22
|
+
streams, and `Uint8Array`.
|
|
23
|
+
|
|
24
|
+
## Install
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
npm install tar
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`tar` needs Node.js 22 or later, or another server-side runtime that
|
|
31
|
+
has the `node:` modules. `tar/core` also runs in a browser.
|
|
32
|
+
|
|
33
|
+
The package has ES module and CommonJS builds. You can use `import` or
|
|
34
|
+
`require()`.
|
|
35
|
+
|
|
36
|
+
## Quick start
|
|
37
|
+
|
|
38
|
+
These examples use `tar`, the server-side entry point. A command has two arguments: an options
|
|
39
|
+
object, and for most commands a list of files.
|
|
40
|
+
|
|
41
|
+
### Create an archive
|
|
240
42
|
|
|
241
43
|
```js
|
|
242
44
|
import { create } from 'tar'
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
file: 'my-tarball.tgz'
|
|
247
|
-
},
|
|
248
|
-
['some', 'files', 'and', 'folders']
|
|
249
|
-
).then(_ => { .. tarball has been created .. })
|
|
45
|
+
|
|
46
|
+
// A gzip archive of a directory and a file.
|
|
47
|
+
await create({ file: 'demo.tgz', gzip: true }, ['src', 'package.json'])
|
|
250
48
|
```
|
|
251
49
|
|
|
252
|
-
|
|
50
|
+
With no `file` option, `create()` returns a `ReadableStream` of the
|
|
51
|
+
bytes of the archive.
|
|
253
52
|
|
|
254
53
|
```js
|
|
255
|
-
|
|
256
|
-
import
|
|
257
|
-
tar
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
},
|
|
262
|
-
['some', 'files', 'and', 'folders']
|
|
263
|
-
).pipe(fs.createWriteStream('my-tarball.tgz'))
|
|
54
|
+
import { createWriteStream } from 'node:fs'
|
|
55
|
+
import { Writable } from 'node:stream'
|
|
56
|
+
import { create } from 'tar'
|
|
57
|
+
|
|
58
|
+
const stream = create({ gzip: true }, ['src', 'package.json'])
|
|
59
|
+
await stream.pipeTo(Writable.toWeb(createWriteStream('demo.tgz')))
|
|
264
60
|
```
|
|
265
61
|
|
|
266
|
-
|
|
62
|
+
### Extract an archive
|
|
63
|
+
|
|
64
|
+
The directory to extract into must exist. The default is the current
|
|
65
|
+
working directory.
|
|
267
66
|
|
|
268
67
|
```js
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
68
|
+
import { mkdir } from 'node:fs/promises'
|
|
69
|
+
import { extract } from 'tar'
|
|
70
|
+
|
|
71
|
+
await mkdir('out', { recursive: true })
|
|
72
|
+
await extract({ file: 'archive.tgz', cwd: 'out' })
|
|
73
|
+
|
|
74
|
+
// Only some entries, with the first part of each path removed.
|
|
75
|
+
await extract({ file: 'archive.tgz', cwd: 'out', strip: 1 }, ['src/lib'])
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The `input` option gives the archive as data. It can be a
|
|
79
|
+
`Uint8Array`, an `ArrayBuffer`, a `Blob`, a `ReadableStream`, or an
|
|
80
|
+
iterable of `Uint8Array` chunks.
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
import { mkdir } from 'node:fs/promises'
|
|
84
|
+
import { extract } from 'tar'
|
|
85
|
+
|
|
86
|
+
const res = await fetch('https://example.com/archive.tgz')
|
|
87
|
+
await mkdir('out', { recursive: true })
|
|
88
|
+
await extract({ input: res.body, cwd: 'out' })
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### List the entries
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
import { list } from 'tar'
|
|
95
|
+
|
|
96
|
+
await list({
|
|
97
|
+
file: 'archive.tgz',
|
|
98
|
+
onEntry: entry => console.log(entry.type, entry.path, entry.size),
|
|
99
|
+
})
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
To read the content of the entries, use `entries()`. It is an async
|
|
103
|
+
iterator.
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
import { readFile } from 'node:fs/promises'
|
|
107
|
+
import { entries } from 'tar'
|
|
108
|
+
|
|
109
|
+
for await (const entry of entries(await readFile('archive.tgz'))) {
|
|
110
|
+
if (entry.path === 'package.json') {
|
|
111
|
+
console.log(JSON.parse(await entry.text()))
|
|
273
112
|
}
|
|
274
|
-
|
|
113
|
+
}
|
|
275
114
|
```
|
|
276
115
|
|
|
277
|
-
|
|
116
|
+
### Add files to an archive
|
|
117
|
+
|
|
118
|
+
`replace()` adds entries to the end of an archive that is not
|
|
119
|
+
compressed. `update()` does the same, but it adds a file only if it
|
|
120
|
+
is newer than the entry of the same path in the archive.
|
|
278
121
|
|
|
279
122
|
```js
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
}),
|
|
285
|
-
)
|
|
123
|
+
import { replace, update } from 'tar'
|
|
124
|
+
|
|
125
|
+
await replace({ file: 'archive.tar' }, ['notes.txt'])
|
|
126
|
+
await update({ file: 'archive.tar' }, ['src', 'package.json'])
|
|
286
127
|
```
|
|
287
128
|
|
|
288
|
-
|
|
129
|
+
### Sync commands
|
|
130
|
+
|
|
131
|
+
Each command of `tar` has a sync form. A sync command returns when
|
|
132
|
+
the work is done. `tar/core` has no sync commands.
|
|
289
133
|
|
|
290
134
|
```js
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
135
|
+
import { mkdirSync } from 'node:fs'
|
|
136
|
+
import {
|
|
137
|
+
createSync,
|
|
138
|
+
extractSync,
|
|
139
|
+
listSync,
|
|
140
|
+
deleteSync,
|
|
141
|
+
replaceSync,
|
|
142
|
+
updateSync,
|
|
143
|
+
} from 'tar'
|
|
144
|
+
|
|
145
|
+
createSync({ file: 'demo.tar' }, ['src'])
|
|
146
|
+
replaceSync({ file: 'demo.tar' }, ['package.json'])
|
|
147
|
+
updateSync({ file: 'demo.tar' }, ['README.md'])
|
|
148
|
+
deleteSync({ file: 'demo.tar' }, ['package.json'])
|
|
149
|
+
listSync({
|
|
150
|
+
file: 'demo.tar',
|
|
151
|
+
onEntry: entry => console.log(entry.path),
|
|
294
152
|
})
|
|
153
|
+
mkdirSync('out', { recursive: true })
|
|
154
|
+
extractSync({ file: 'demo.tar', cwd: 'out' })
|
|
295
155
|
```
|
|
296
156
|
|
|
297
|
-
|
|
157
|
+
With no `file` option, `createSync()` returns an iterator of
|
|
158
|
+
`Uint8Array` chunks.
|
|
159
|
+
|
|
160
|
+
## Entry points
|
|
161
|
+
|
|
162
|
+
| Import | Use |
|
|
163
|
+
| ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
164
|
+
| `tar` | Server-side runtimes: Node.js, and each other runtime that has the `node:` modules (Bun, Deno, Cloudflare). All of the core, the defaults below, the sync commands, and the `node:zlib` codecs. |
|
|
165
|
+
| `tar/core` | Browsers, and each other runtime. The core: no `node:` module, no `process`, no `Buffer`. The async commands and the classes. It needs the `fs` option for work on files. |
|
|
166
|
+
| `tar/raw`, `tar/core/raw` | The same two entry points as separate modules that are not minified. |
|
|
167
|
+
|
|
168
|
+
`tar` and `tar/core` are each one minified file.
|
|
169
|
+
|
|
170
|
+
### The defaults of `tar`
|
|
171
|
+
|
|
172
|
+
Each command of `tar` calls the command of the core with these
|
|
173
|
+
defaults:
|
|
174
|
+
|
|
175
|
+
| Option | Default in `tar` |
|
|
176
|
+
| -------------- | ---------------------------------------------------------------- |
|
|
177
|
+
| `fs` | `node:fs/promises`. `node:fs` for a sync command. |
|
|
178
|
+
| `cwd` | `process.cwd()`. A relative `cwd` is resolved from it. |
|
|
179
|
+
| `platform` | `'win32'` if `process.platform` is `'win32'`. If not, `'posix'`. |
|
|
180
|
+
| `processUid` | `process.getuid()` |
|
|
181
|
+
| `processGid` | `process.getgid()` |
|
|
182
|
+
| `processUname` | `process.env.USER` |
|
|
183
|
+
| `processUmask` | `process.umask()`, only when `chmod` is set |
|
|
184
|
+
| `codecs` | Your codecs, then gzip, brotli, and zstd from `node:zlib` |
|
|
185
|
+
|
|
186
|
+
`list()` and `entries()` write no file. They get only the `fs`,
|
|
187
|
+
`platform`, and `codecs` defaults.
|
|
188
|
+
|
|
189
|
+
### The core in a browser or another runtime
|
|
190
|
+
|
|
191
|
+
`tar/core` is the core. It does not know the file system, the working
|
|
192
|
+
directory, the platform, or the user of the process. You give them as
|
|
193
|
+
options.
|
|
194
|
+
|
|
195
|
+
The core needs these globals: `ReadableStream`, `TransformStream`,
|
|
196
|
+
`Blob`, `TextEncoder`, `TextDecoder`, and `crypto.getRandomValues`. It
|
|
197
|
+
needs `CompressionStream` and `DecompressionStream` for gzip.
|
|
198
|
+
|
|
199
|
+
A command of the core works with no `fs` option, if it does not touch
|
|
200
|
+
a file:
|
|
201
|
+
|
|
202
|
+
- `entries(input)` and `list({ input })` read archive data.
|
|
203
|
+
- `create()` with no `file` option, and with entries from memory,
|
|
204
|
+
returns a stream.
|
|
205
|
+
|
|
206
|
+
This example reads an archive from a `fetch()` response. It stops the
|
|
207
|
+
download when it has the entry.
|
|
298
208
|
|
|
299
209
|
```js
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
210
|
+
import { entries } from 'tar/core'
|
|
211
|
+
|
|
212
|
+
const res = await fetch('https://example.com/archive.tgz')
|
|
213
|
+
for await (const entry of entries(res.body)) {
|
|
214
|
+
if (entry.path === 'package.json') {
|
|
215
|
+
console.log(JSON.parse(await entry.text()))
|
|
216
|
+
break
|
|
217
|
+
}
|
|
307
218
|
}
|
|
308
219
|
```
|
|
309
220
|
|
|
310
|
-
|
|
221
|
+
This example makes a gzip archive in memory, and reads it from a
|
|
222
|
+
`Blob`. A `File` from a file input is also a `Blob`.
|
|
311
223
|
|
|
312
224
|
```js
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
225
|
+
import { create, entries, WriteEntry } from 'tar/core'
|
|
226
|
+
|
|
227
|
+
const stream = create({ gzip: true }, [
|
|
228
|
+
new WriteEntry({ path: 'hello.txt' }, 'hello\n'),
|
|
229
|
+
new WriteEntry({ path: 'data.bin' }, new Uint8Array([1, 2, 3])),
|
|
230
|
+
])
|
|
231
|
+
const blob = await new Response(stream).blob()
|
|
232
|
+
|
|
233
|
+
for await (const entry of entries(blob)) {
|
|
234
|
+
console.log(entry.path, entry.size)
|
|
235
|
+
}
|
|
316
236
|
```
|
|
317
237
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
file
|
|
322
|
-
|
|
238
|
+
#### The `input` option
|
|
239
|
+
|
|
240
|
+
`list()` and `extract()` read the archive from `input`, or from the
|
|
241
|
+
path `file` through `fs`. `entries()` takes the input as its first
|
|
242
|
+
argument. The input can be:
|
|
243
|
+
|
|
244
|
+
- a `Uint8Array` or an `ArrayBuffer`
|
|
245
|
+
- a `Blob`
|
|
246
|
+
- a `ReadableStream<Uint8Array>`
|
|
247
|
+
- an async iterable or an iterable of `Uint8Array` chunks
|
|
248
|
+
|
|
249
|
+
A sync command takes a `Uint8Array`, an `ArrayBuffer`, or an iterable
|
|
250
|
+
of `Uint8Array` chunks.
|
|
251
|
+
|
|
252
|
+
The readers do not copy the input. Do not change a chunk after you
|
|
253
|
+
give it.
|
|
254
|
+
|
|
255
|
+
When `input` is set, `file` is only the name of the archive. Warnings
|
|
256
|
+
have it, and the readers use it to find a format by the file name.
|
|
257
|
+
|
|
258
|
+
#### The `fs` option
|
|
259
|
+
|
|
260
|
+
The `fs` option is an object with file system functions. Each function
|
|
261
|
+
has the signature of the function of the same name in
|
|
262
|
+
`node:fs/promises`. For a sync command, it is the function of
|
|
263
|
+
`node:fs`. A command calls only the functions in its type.
|
|
264
|
+
|
|
265
|
+
| Type | Commands | Functions |
|
|
266
|
+
| ----------- | --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
267
|
+
| `TarFS` | `list()` and `extract()` with `file`, and an `@archive` operand | `open(path, 'r')`. The handle has `read`, `stat`, and `close`. |
|
|
268
|
+
| `ExtractFS` | `extract()` | `open`, `stat`, `lstat`, `mkdir`, `chmod`, `chown`, `lchown`, `utimes`, `lutimes`, `rmdir`, `unlink`, `rename`, `link`, `symlink`. The handle has `write`, `utimes`, `chown`, and `close`. `constants` is optional. |
|
|
269
|
+
| `CreateFS` | `create()` | `open`, `lstat`, `stat`, `readdir`, `readlink`. The handle has `read`, `write`, `stat`, and `close`. `lstat` and `stat` can get the option `{ bigint: true }`. |
|
|
270
|
+
| `ReplaceFS` | `replace()`, `update()` | The same as `CreateFS`. `open` gets the flags `'r+'`, `'w+'`, and `'r'`. |
|
|
271
|
+
| `DeleteFS` | `delete()` | `TarFS` and `CreateFS`, and `rename` and `unlink`. |
|
|
272
|
+
|
|
273
|
+
The sync types are `TarFSSync`, `ExtractFSSync`, `CreateFSSync`,
|
|
274
|
+
`ReplaceFSSync`, and `DeleteFSSync`. They have the functions of `node:fs` that work on a
|
|
275
|
+
file descriptor: `openSync`, `fstatSync`, `readSync`, `writeSync`,
|
|
276
|
+
`futimesSync`, `fchownSync`, `closeSync`, and the sync forms of the
|
|
277
|
+
path functions above.
|
|
278
|
+
|
|
279
|
+
Notes for an `ExtractFS`:
|
|
280
|
+
|
|
281
|
+
- `mkdir` must fail if the path exists. `lstat` and `stat` must fail
|
|
282
|
+
if the path does not exist.
|
|
283
|
+
- If `fs.constants` has `O_CREAT`, `O_TRUNC`, `O_WRONLY`, and
|
|
284
|
+
`O_NOFOLLOW`, then `open` gets a number as its flag. With
|
|
285
|
+
`O_NOFOLLOW`, `open` does not follow a symbolic link at the path.
|
|
286
|
+
Without `constants`, the flag is `'w'`.
|
|
287
|
+
- `chown` and `lchown` are called only when the options set an owner.
|
|
288
|
+
`chmod` is called only with the `chmod` option. `rename` is called
|
|
289
|
+
only with `platform: 'win32'`.
|
|
290
|
+
|
|
291
|
+
Notes for a `CreateFS`:
|
|
292
|
+
|
|
293
|
+
- `create()` knows a file by the `dev` and `ino` numbers of its
|
|
294
|
+
stat. Node.js gives the two values as numbers. On Windows, most
|
|
295
|
+
`ino` values are too large for a number, and then two files can
|
|
296
|
+
have the same number.
|
|
297
|
+
- A wrong match loses data in two cases: a file with more than one
|
|
298
|
+
link is written as a hard link to a different file, and with
|
|
299
|
+
`follow`, a directory is skipped as a directory in itself. Only in
|
|
300
|
+
these cases, and only when `dev` or `ino` is not a safe integer,
|
|
301
|
+
`create()` calls `lstat` (`stat` with `follow`) one more time with
|
|
302
|
+
the option `{ bigint: true }`, and reads the `dev` and `ino` of the
|
|
303
|
+
result. `node:fs` gives exact `bigint` values with this option.
|
|
304
|
+
- A file with one link, and a directory without `follow`, never get
|
|
305
|
+
that call. The check that a path is the same object after `open`
|
|
306
|
+
and after `readdir` compares the numbers.
|
|
307
|
+
- Your function can ignore the option and return the stat with
|
|
308
|
+
numbers. `create()` then uses the numbers. Two files with the same
|
|
309
|
+
`dev` and `ino` numbers, and more than one link, are then one file
|
|
310
|
+
and a hard link to it.
|
|
311
|
+
|
|
312
|
+
This example extracts an archive into a `Map`.
|
|
323
313
|
|
|
324
314
|
```js
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
315
|
+
import { create, extract, WriteEntry } from 'tar/core'
|
|
316
|
+
|
|
317
|
+
// path -> { type, mode, mtime, chunks, target }
|
|
318
|
+
const tree = new Map([
|
|
319
|
+
['/out', { type: 'dir', mode: 0o755, mtime: new Date() }],
|
|
320
|
+
])
|
|
321
|
+
const fail = (code, path) =>
|
|
322
|
+
Object.assign(new Error(`${code}: ${path}`), { code })
|
|
323
|
+
const get = path => {
|
|
324
|
+
const node = tree.get(path)
|
|
325
|
+
if (!node) throw fail('ENOENT', path)
|
|
326
|
+
return node
|
|
327
|
+
}
|
|
328
|
+
const stats = node => ({
|
|
329
|
+
isDirectory: () => node.type === 'dir',
|
|
330
|
+
isFile: () => node.type === 'file',
|
|
331
|
+
isSymbolicLink: () => node.type === 'symlink',
|
|
332
|
+
nlink: 1,
|
|
333
|
+
mode: node.mode,
|
|
334
|
+
mtime: node.mtime,
|
|
335
|
+
})
|
|
336
|
+
const setTime = async (path, atime, mtime) => {
|
|
337
|
+
get(path).mtime = mtime
|
|
333
338
|
}
|
|
339
|
+
|
|
340
|
+
const memfs = {
|
|
341
|
+
stat: async path => stats(get(path)),
|
|
342
|
+
lstat: async path => stats(get(path)),
|
|
343
|
+
mkdir: async (path, { mode }) => {
|
|
344
|
+
if (tree.has(path)) throw fail('EEXIST', path)
|
|
345
|
+
tree.set(path, { type: 'dir', mode, mtime: new Date() })
|
|
346
|
+
return undefined
|
|
347
|
+
},
|
|
348
|
+
open: async (path, flags, mode) => {
|
|
349
|
+
const file = { type: 'file', mode, mtime: new Date(), chunks: [] }
|
|
350
|
+
tree.set(path, file)
|
|
351
|
+
return {
|
|
352
|
+
// `data` is a view into the archive data. Keep a copy.
|
|
353
|
+
write: async data => file.chunks.push(data.slice()),
|
|
354
|
+
utimes: async (atime, mtime) => {
|
|
355
|
+
file.mtime = mtime
|
|
356
|
+
},
|
|
357
|
+
chown: async () => {},
|
|
358
|
+
close: async () => {},
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
symlink: async (target, path) => {
|
|
362
|
+
tree.set(path, { type: 'symlink', mode: 0o777, target })
|
|
363
|
+
},
|
|
364
|
+
link: async (target, path) => {
|
|
365
|
+
tree.set(path, get(target))
|
|
366
|
+
},
|
|
367
|
+
unlink: async path => {
|
|
368
|
+
tree.delete(path)
|
|
369
|
+
},
|
|
370
|
+
rmdir: async path => {
|
|
371
|
+
tree.delete(path)
|
|
372
|
+
},
|
|
373
|
+
rename: async (from, to) => {
|
|
374
|
+
tree.set(to, get(from))
|
|
375
|
+
tree.delete(from)
|
|
376
|
+
},
|
|
377
|
+
chmod: async (path, mode) => {
|
|
378
|
+
get(path).mode = mode
|
|
379
|
+
},
|
|
380
|
+
chown: async () => {},
|
|
381
|
+
lchown: async () => {},
|
|
382
|
+
utimes: setTime,
|
|
383
|
+
lutimes: setTime,
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const archive = create({}, [
|
|
387
|
+
new WriteEntry({ path: 'docs/', type: 'Directory', mode: 0o755 }),
|
|
388
|
+
new WriteEntry({ path: 'docs/hello.txt', mode: 0o644 }, 'hello\n'),
|
|
389
|
+
new WriteEntry({ path: 'docs/deep/more.txt' }, 'more\n'),
|
|
390
|
+
])
|
|
391
|
+
|
|
392
|
+
await extract({ input: archive, fs: memfs, cwd: '/out' })
|
|
393
|
+
|
|
394
|
+
for (const [path, node] of tree) console.log(node.type, path)
|
|
395
|
+
console.log(await new Blob(tree.get('/out/docs/hello.txt').chunks).text())
|
|
334
396
|
```
|
|
335
397
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
398
|
+
#### Electron and `asar` files
|
|
399
|
+
|
|
400
|
+
In Electron, the `fs` module is changed: a file with a name that ends
|
|
401
|
+
in `.asar` is a directory. With that module, `extract()` does not
|
|
402
|
+
write an `app.asar` entry as a file, and `create()` reads the paths
|
|
403
|
+
in the `asar` file and not the file. Electron has the module
|
|
404
|
+
`original-fs`, which is `node:fs` with no change. Give it as the `fs`
|
|
405
|
+
option.
|
|
406
|
+
|
|
407
|
+
An async command needs the functions that return promises:
|
|
408
|
+
`originalFs.promises`. A sync command needs `originalFs`.
|
|
409
|
+
|
|
410
|
+
This example is for the main process of Electron. It is not tested,
|
|
411
|
+
because the tests of this package do not run in Electron.
|
|
341
412
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
413
|
+
```javascript
|
|
414
|
+
import originalFs from 'original-fs'
|
|
415
|
+
import { create, extract, extractSync } from 'tar'
|
|
345
416
|
|
|
346
|
-
|
|
417
|
+
await create({ file: 'app.tgz', gzip: true, fs: originalFs.promises }, [
|
|
418
|
+
'MyApp.app',
|
|
419
|
+
])
|
|
420
|
+
await extract({ file: 'app.tgz', cwd: 'out', fs: originalFs.promises })
|
|
421
|
+
extractSync({ file: 'app.tgz', cwd: 'out', fs: originalFs })
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
#### The process options
|
|
425
|
+
|
|
426
|
+
- `cwd` The directory that `extract()` writes into, and the directory
|
|
427
|
+
that the paths of `create()` are relative to. In `tar/core` it must
|
|
428
|
+
be an absolute path, by the rules of `platform`. A path that is not
|
|
429
|
+
absolute is a `TypeError`. `extract()` needs it. `create()` needs it
|
|
430
|
+
only to add a path.
|
|
431
|
+
- `platform` `'posix'` (default) or `'win32'`. With `'win32'`: each
|
|
432
|
+
`\` in a path is a separator, and the path rules of Windows apply.
|
|
433
|
+
`extract()` changes the characters `<|>?:` in a name to private-use
|
|
434
|
+
characters, renames a file before it removes it, and extracts one
|
|
435
|
+
entry at a time. `create()` changes those private-use characters
|
|
436
|
+
back.
|
|
437
|
+
- `processUid`, `processGid` The user ID and the group ID of the
|
|
438
|
+
process. `extract()` uses them to know if it must set an owner. If
|
|
439
|
+
`processUid` is 0, the default of `preserveOwner` is true.
|
|
440
|
+
- `processUname` `create()` writes it as the `uname` of each file that
|
|
441
|
+
`processUid` owns.
|
|
442
|
+
- `processUmask` The umask of the process. `extract()` uses it only
|
|
443
|
+
with `chmod`. Default `0o22`.
|
|
444
|
+
|
|
445
|
+
## Entries
|
|
446
|
+
|
|
447
|
+
### `ReadEntry`
|
|
448
|
+
|
|
449
|
+
`entries()`, `onEntry`, `filter`, and `transform` give you a
|
|
450
|
+
`ReadEntry`. It has the shape of a `Response`: the metadata is in the
|
|
451
|
+
fields, and the content is the body.
|
|
452
|
+
|
|
453
|
+
| Field | Value |
|
|
454
|
+
| ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
455
|
+
| `path` | The path in the archive. |
|
|
456
|
+
| `type` | `'File'`, `'Directory'`, `'SymbolicLink'`, `'Link'` (a hard link), `'CharacterDevice'`, `'BlockDevice'`, `'FIFO'`, `'OldFile'`, `'ContiguousFile'`, or `'GNUDumpDir'`. |
|
|
457
|
+
| `size` | The number of bytes in the body. |
|
|
458
|
+
| `mode` | The permission bits. |
|
|
459
|
+
| `mtime`, `atime`, `ctime` | `Date` objects. |
|
|
460
|
+
| `uid`, `gid`, `uname`, `gname` | The owner. |
|
|
461
|
+
| `linkpath` | The target of a link. |
|
|
462
|
+
| `devmaj`, `devmin` | The device numbers. |
|
|
463
|
+
| `dev`, `ino`, `nlink` | Set only if a PAX header has them. |
|
|
464
|
+
| `header` | The `Header` object of the entry. |
|
|
465
|
+
| `extended`, `globalExtended` | The PAX headers that apply to the entry. |
|
|
466
|
+
| `absolute` | The absolute path. `extract()` sets it. |
|
|
467
|
+
| `bodyUsed` | True when the body started to be read. |
|
|
468
|
+
|
|
469
|
+
A field is `undefined` if the archive does not have it. The values of
|
|
470
|
+
PAX headers and GNU long name headers are applied.
|
|
471
|
+
|
|
472
|
+
Read the body with one of these:
|
|
473
|
+
|
|
474
|
+
- `entry.body` A `ReadableStream<Uint8Array>`.
|
|
475
|
+
- `await entry.bytes()` A new `Uint8Array`.
|
|
476
|
+
- `await entry.arrayBuffer()` A new `ArrayBuffer`.
|
|
477
|
+
- `await entry.text()` A string, decoded as UTF-8. As with `Response.text()`, a byte order mark at the start is not in the string.
|
|
478
|
+
- `await entry.blob()` A new `Blob`.
|
|
479
|
+
|
|
480
|
+
Rules for the body:
|
|
481
|
+
|
|
482
|
+
- You can read the body one time, in one way. A second read throws a
|
|
483
|
+
`TypeError`.
|
|
484
|
+
- Read the body before you ask for the next entry. When the reader
|
|
485
|
+
moves to the next entry, it skips the rest of the body. After that,
|
|
486
|
+
a read throws a `TypeError`.
|
|
487
|
+
- You do not have to read the body.
|
|
488
|
+
- A chunk of `entry.body` is a view into the input. It is not a copy.
|
|
489
|
+
|
|
490
|
+
The readers get input only when you ask for an entry or for more of a
|
|
491
|
+
body.
|
|
347
492
|
|
|
493
|
+
```js
|
|
494
|
+
import { readFile } from 'node:fs/promises'
|
|
495
|
+
import { entries } from 'tar'
|
|
496
|
+
|
|
497
|
+
for await (const entry of entries(await readFile('archive.tgz'))) {
|
|
498
|
+
if (entry.type !== 'File') continue
|
|
499
|
+
if (entry.path.endsWith('.json')) {
|
|
500
|
+
console.log(entry.path, JSON.parse(await entry.text()))
|
|
501
|
+
} else {
|
|
502
|
+
let bytes = 0
|
|
503
|
+
for await (const chunk of entry.body) bytes += chunk.length
|
|
504
|
+
console.log(entry.path, bytes)
|
|
505
|
+
}
|
|
506
|
+
}
|
|
348
507
|
```
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
508
|
+
|
|
509
|
+
### `onEntry` is awaited
|
|
510
|
+
|
|
511
|
+
`list()` and `extract()` call `onEntry` with each entry. If it returns
|
|
512
|
+
a promise, the command waits for it before it reads the next entry.
|
|
513
|
+
|
|
514
|
+
In `list()`, you can read the body in `onEntry`.
|
|
515
|
+
|
|
516
|
+
```js
|
|
517
|
+
import { list } from 'tar'
|
|
518
|
+
|
|
519
|
+
await list({
|
|
520
|
+
file: 'archive.tgz',
|
|
521
|
+
onEntry: async entry => {
|
|
522
|
+
if (entry.path === 'README.md') console.log(await entry.text())
|
|
523
|
+
},
|
|
524
|
+
})
|
|
354
525
|
```
|
|
355
526
|
|
|
356
|
-
|
|
357
|
-
the
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
- `
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
527
|
+
In `extract()`, `onEntry` gets each entry before the checks and before
|
|
528
|
+
the entry is written. If `onEntry` reads the body, the entry is not
|
|
529
|
+
extracted, because its content is used.
|
|
530
|
+
|
|
531
|
+
### `ReadEntrySync`
|
|
532
|
+
|
|
533
|
+
The sync commands give you a `ReadEntrySync`. It has the same fields.
|
|
534
|
+
`bytes()`, `arrayBuffer()`, and `text()` return the value, not a
|
|
535
|
+
promise. `body` is an iterator of `Uint8Array` chunks. There is no
|
|
536
|
+
`blob()`.
|
|
537
|
+
|
|
538
|
+
```js
|
|
539
|
+
import { readFileSync } from 'node:fs'
|
|
540
|
+
import { entriesSync } from 'tar'
|
|
541
|
+
|
|
542
|
+
for (const entry of entriesSync(readFileSync('archive.tgz'))) {
|
|
543
|
+
if (entry.path === 'package.json') console.log(entry.text())
|
|
544
|
+
}
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
### `WriteEntry`
|
|
548
|
+
|
|
549
|
+
A `WriteEntry` is an entry to write. It has the shape of a `Request`:
|
|
550
|
+
the metadata is in the fields, and the content is `body`.
|
|
551
|
+
|
|
552
|
+
```js
|
|
553
|
+
import { WriteEntry } from 'tar'
|
|
554
|
+
|
|
555
|
+
const entry = new WriteEntry(
|
|
556
|
+
{ path: 'hello.txt', mode: 0o644, mtime: new Date() },
|
|
557
|
+
'hello\n',
|
|
558
|
+
)
|
|
559
|
+
console.log(entry.type, entry.size)
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
`new WriteEntry(data, body)`:
|
|
563
|
+
|
|
564
|
+
- `data.path` is required.
|
|
565
|
+
- `data.type` Default `'File'`.
|
|
566
|
+
- `data.mode`, `uid`, `gid`, `uname`, `gname`, `mtime`, `atime`,
|
|
567
|
+
`ctime`, `linkpath`, `dev`, `ino`, `nlink` are optional.
|
|
568
|
+
- `data.size` The number of bytes in the body. The default is the
|
|
569
|
+
length of a body that is a string, a `Uint8Array`, or a `Blob`.
|
|
570
|
+
- `body` A string, a `Uint8Array`, a `Blob`, a
|
|
571
|
+
`ReadableStream<Uint8Array>`, or an iterable or async iterable of
|
|
572
|
+
`Uint8Array` chunks. A string is written as UTF-8.
|
|
573
|
+
|
|
574
|
+
Set `data.size` for a body that is a stream or an iterable. Without
|
|
575
|
+
it, the constructor throws a `TypeError`, because the header has the
|
|
576
|
+
size and comes before the content. The body must have `size` bytes.
|
|
577
|
+
If it does not, the pack stops with an error.
|
|
578
|
+
|
|
579
|
+
A sync command takes only a string, a `Uint8Array`, or a sync iterable
|
|
580
|
+
as the body.
|
|
581
|
+
|
|
582
|
+
### Add entries from memory to an archive
|
|
583
|
+
|
|
584
|
+
The list of files of `create()`, `replace()`, and `update()` can have:
|
|
585
|
+
|
|
586
|
+
- a path
|
|
587
|
+
- a path that starts with `@`, such as `'@other.tar'`: the rest is the
|
|
588
|
+
path of an archive, and its entries are added
|
|
589
|
+
- a `WriteEntry`
|
|
590
|
+
- a `ReadEntry` of an archive that you read
|
|
591
|
+
- an iterable or async iterable of entries, such as the result of
|
|
592
|
+
`entries()`
|
|
593
|
+
|
|
594
|
+
```js
|
|
595
|
+
import { create, WriteEntry } from 'tar'
|
|
596
|
+
|
|
597
|
+
await create({ file: 'bundle.tgz', gzip: true }, [
|
|
598
|
+
'src',
|
|
599
|
+
new WriteEntry(
|
|
600
|
+
{ path: 'build-info.json', mode: 0o644, mtime: new Date() },
|
|
601
|
+
JSON.stringify({ built: new Date().toISOString() }),
|
|
602
|
+
),
|
|
603
|
+
new WriteEntry({ path: 'empty/', type: 'Directory', mode: 0o755 }),
|
|
604
|
+
])
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
### Create an archive from a stream
|
|
608
|
+
|
|
609
|
+
A file of an archive can come from a `ReadableStream`, such as the
|
|
610
|
+
body of a `fetch()` response. Make a `WriteEntry` with the stream as
|
|
611
|
+
its body, and give it to `create()`.
|
|
612
|
+
|
|
613
|
+
You must set `size`, the number of bytes that the stream gives. A tar
|
|
614
|
+
header has the size of the file, and the header comes before the
|
|
615
|
+
content. `create()` cannot write the entry without it. If you do not
|
|
616
|
+
know the size, read the stream into memory first, for example with
|
|
617
|
+
`await new Response(stream).bytes()`, and give the bytes as the body.
|
|
618
|
+
|
|
619
|
+
```js
|
|
620
|
+
import { create, WriteEntry } from 'tar'
|
|
621
|
+
|
|
622
|
+
const text = new TextEncoder().encode('the content of the stream\n')
|
|
623
|
+
// A stream of two chunks. A stream from a file or from the network
|
|
624
|
+
// works in the same way.
|
|
625
|
+
const stream = new ReadableStream({
|
|
626
|
+
start: controller => {
|
|
627
|
+
controller.enqueue(text.subarray(0, 10))
|
|
628
|
+
controller.enqueue(text.subarray(10))
|
|
629
|
+
controller.close()
|
|
630
|
+
},
|
|
631
|
+
})
|
|
632
|
+
|
|
633
|
+
await create({ file: 'from-stream.tgz', gzip: true }, [
|
|
634
|
+
new WriteEntry({ path: 'stream.txt', size: text.length }, stream),
|
|
635
|
+
])
|
|
636
|
+
```
|
|
637
|
+
|
|
638
|
+
A Node.js `Readable` is an async iterable of chunks, so it can also be
|
|
639
|
+
the body. `Readable.toWeb(readable)` makes a `ReadableStream` of it.
|
|
640
|
+
|
|
641
|
+
This example copies an archive, and removes some entries.
|
|
642
|
+
|
|
643
|
+
```js
|
|
644
|
+
import { readFile } from 'node:fs/promises'
|
|
645
|
+
import { create, entries } from 'tar'
|
|
646
|
+
|
|
647
|
+
await create(
|
|
648
|
+
{
|
|
649
|
+
file: 'no-docs.tgz',
|
|
650
|
+
gzip: true,
|
|
651
|
+
filter: path => !path.endsWith('.md'),
|
|
652
|
+
},
|
|
653
|
+
[entries(await readFile('archive.tgz'))],
|
|
654
|
+
)
|
|
655
|
+
```
|
|
656
|
+
|
|
657
|
+
### Remove an entry from an archive
|
|
658
|
+
|
|
659
|
+
`delete()` removes entries from an archive file. `delete` is a
|
|
660
|
+
reserved word, so import it with a different name, or call it as a
|
|
661
|
+
property.
|
|
441
662
|
|
|
442
663
|
```js
|
|
443
664
|
import * as tar from 'tar'
|
|
444
|
-
|
|
445
|
-
|
|
665
|
+
import { delete as del, deleteSync } from 'tar'
|
|
666
|
+
|
|
667
|
+
await tar.delete({ file: 'archive.tar' }, ['README.md'])
|
|
668
|
+
// A directory, and all that is in it.
|
|
669
|
+
await del({ file: 'archive.tar' }, ['src/lib'])
|
|
670
|
+
// Each entry for which the filter returns true.
|
|
671
|
+
deleteSync({
|
|
672
|
+
file: 'archive.tar',
|
|
673
|
+
filter: path => path.endsWith('.map'),
|
|
674
|
+
})
|
|
675
|
+
```
|
|
676
|
+
|
|
677
|
+
## Commands
|
|
678
|
+
|
|
679
|
+
Each command is `command(options, files)`. The aliases `c`, `x`, `t`,
|
|
680
|
+
`r`, and `u` are also exported. `delete()` has no alias.
|
|
681
|
+
|
|
682
|
+
These options are common to each command:
|
|
683
|
+
|
|
684
|
+
- `strict` Make each warning an error. See
|
|
685
|
+
[Warnings and errors](#warnings-and-errors). For example, a path of
|
|
686
|
+
`create()` that does not exist, and a path in `files` of `extract()`
|
|
687
|
+
or `list()` that is not in the archive, are warnings without it.
|
|
688
|
+
- `onwarn` A function `(code, message, data)`. It gets each warning
|
|
689
|
+
when `strict` is not set.
|
|
690
|
+
- `file` The path of the archive file.
|
|
691
|
+
- `fs`, `platform` See [Entry points](#entry-points).
|
|
692
|
+
|
|
693
|
+
### Option aliases
|
|
694
|
+
|
|
695
|
+
Each command takes the option aliases of version 7. An option that is
|
|
696
|
+
set wins over its aliases. `entries()` and the classes take only the
|
|
697
|
+
names of the options.
|
|
698
|
+
|
|
699
|
+
| Option | Aliases |
|
|
700
|
+
| --------------- | --------------------------------------------------------------- |
|
|
701
|
+
| `cwd` | `C` |
|
|
702
|
+
| `file` | `f` |
|
|
703
|
+
| `gzip` | `z` |
|
|
704
|
+
| `preservePaths` | `P` |
|
|
705
|
+
| `unlink` | `U` |
|
|
706
|
+
| `strip` | `strip-components`, `stripComponents` |
|
|
707
|
+
| `newer` | `keep-newer`, `keepNewer`, `keep-newer-files`, `keepNewerFiles` |
|
|
708
|
+
| `keep` | `k`, `keep-existing`, `keepExisting` |
|
|
709
|
+
| `noMtime` | `m`, `no-mtime` |
|
|
710
|
+
| `preserveOwner` | `p` |
|
|
711
|
+
| `follow` | `L`, `h` |
|
|
712
|
+
| `onEntry` | `onentry`, `onReadEntry` |
|
|
713
|
+
| `chmod` | `noChmod: false` is `chmod: true`, if `chmod` is not set |
|
|
714
|
+
|
|
715
|
+
```js
|
|
716
|
+
import { c, t } from 'tar'
|
|
717
|
+
|
|
718
|
+
await c({ f: 'alias.tgz', z: true, C: '.' }, ['src'])
|
|
719
|
+
await t({ f: 'alias.tgz', onentry: entry => console.log(entry.path) })
|
|
720
|
+
```
|
|
721
|
+
|
|
722
|
+
### `create(options, files)`
|
|
723
|
+
|
|
724
|
+
Make an archive of `files`. A directory is added with all that is in
|
|
725
|
+
it.
|
|
726
|
+
|
|
727
|
+
With `file`, the archive is written to that path, and the result is a
|
|
728
|
+
`Promise<void>`. With no `file`, the result is a
|
|
729
|
+
`ReadableStream<Uint8Array>`. `createSync()` returns `undefined` or an
|
|
730
|
+
iterator of chunks. The files are read only when the stream or the
|
|
731
|
+
iterator is read.
|
|
732
|
+
|
|
733
|
+
`create()` throws a `TypeError` when `files` is empty.
|
|
734
|
+
|
|
735
|
+
A path that does not exist is a `TAR_ENTRY_ERROR` warning, as it is
|
|
736
|
+
for bsdtar. The warning has the `ENOENT` error of the file system, and
|
|
737
|
+
the other paths are added. This is also true for a path in a directory
|
|
738
|
+
that is removed after the directory is read. With `strict`, it is an
|
|
739
|
+
error.
|
|
740
|
+
|
|
741
|
+
Each other error from the file system stops the command. With `file`,
|
|
742
|
+
the archive file that is not complete stays on disk.
|
|
743
|
+
|
|
744
|
+
`create()` writes the size that `lstat` gives for a file, and then
|
|
745
|
+
reads that number of bytes. If the file has fewer bytes or more
|
|
746
|
+
bytes, the command stops with an error that has the `code` `'EOF'`,
|
|
747
|
+
the `path`, and a message that starts with
|
|
748
|
+
`encountered unexpected EOF` or `did not encounter expected EOF`. The
|
|
749
|
+
cause is a file that changed while it was read, or a file system that
|
|
750
|
+
gives a wrong size, such as the `fs` module of Electron for a path in
|
|
751
|
+
an `asar` file. See
|
|
752
|
+
[Electron and `asar` files](#electron-and-asar-files).
|
|
753
|
+
|
|
754
|
+
Options:
|
|
755
|
+
|
|
756
|
+
- `file` The path of the archive to write. It is not relative to
|
|
757
|
+
`cwd`.
|
|
758
|
+
- `mode` The mode of the archive file. Default `0o666`.
|
|
759
|
+
- `cwd` The directory that the paths in `files` are relative to.
|
|
760
|
+
- `prefix` A directory to put before the path of each entry.
|
|
761
|
+
- `gzip` Compress with gzip. In `tar`, an object is the options of
|
|
762
|
+
`zlib.createGzip()`.
|
|
763
|
+
- `brotli` Compress with brotli. Not in `tar/core`. An object is the
|
|
764
|
+
options of `zlib.createBrotliCompress()`.
|
|
765
|
+
- `zstd` Compress with zstd. Not in `tar/core`. An object is the
|
|
766
|
+
options of `zlib.createZstdCompress()`.
|
|
767
|
+
- `compress` Compress with a `Codec` of your own: `codec`, or
|
|
768
|
+
`[codec, options]`. See [Compression](#compression).
|
|
769
|
+
- `codecs` More compression formats. See [Compression](#compression).
|
|
770
|
+
- `filter` A function `(path, stat)`. If it returns false, the path is
|
|
771
|
+
not added. For a directory, the paths in it are also not added. For
|
|
772
|
+
an entry of another archive, the second argument is the `ReadEntry`.
|
|
773
|
+
It is not called for a `WriteEntry`.
|
|
774
|
+
- `onWriteEntry` A function that gets the `WriteEntry` of each entry
|
|
775
|
+
before its header is written. It can change the fields. The async
|
|
776
|
+
commands wait for a promise.
|
|
777
|
+
- `portable` Leave out the data that is specific to one system: `uid`,
|
|
778
|
+
`gid`, `uname`, `gname`, `atime`, `ctime`, `dev`, `ino`, `nlink`,
|
|
779
|
+
and the `mtime` of a directory. Remove the write permission of the
|
|
780
|
+
group and of others. With `gzip`, the header of the gzip data does
|
|
781
|
+
not name the system.
|
|
782
|
+
- `follow` Add the target of a symbolic link, not the link.
|
|
783
|
+
- `noDirRecurse` Add a directory without the entries in it.
|
|
784
|
+
- `preservePaths` Keep a root at the start of a path, such as `/`. By
|
|
785
|
+
default the root is removed, with a `TAR_ENTRY_INFO` warning.
|
|
786
|
+
- `noPax` Write no PAX header. A PAX header holds the fields that do
|
|
787
|
+
not fit the basic header, such as a long path. Other tar programs
|
|
788
|
+
can need it.
|
|
789
|
+
- `noMtime` Write no `mtime`.
|
|
790
|
+
- `mtime` A `Date`. The `mtime` of each entry.
|
|
791
|
+
- `maxReadSize` The most bytes to read from a file at one time.
|
|
792
|
+
Default 16 MiB.
|
|
793
|
+
- `jobs` The number of paths that are read ahead at one time.
|
|
794
|
+
Default 4. Async only.
|
|
795
|
+
- `maxReadSize` and `jobs` take a positive integer, or `Infinity`.
|
|
796
|
+
0 gives the default. Each other value is a `TypeError`.
|
|
797
|
+
- `processUid`, `processUname` See
|
|
798
|
+
[The process options](#the-process-options).
|
|
799
|
+
- `strict`, `onwarn`, `fs`, `platform` The common options.
|
|
800
|
+
|
|
801
|
+
The archive has these entry types: files, directories, symbolic links,
|
|
802
|
+
and hard links. A file with more than one link is a hard link entry
|
|
803
|
+
the second time that the pack finds it. Each other object, such as a
|
|
804
|
+
socket, a FIFO, or a device, is skipped with a `TAR_ENTRY_UNSUPPORTED`
|
|
805
|
+
warning. The object is not opened. A path that the `filter` refuses
|
|
806
|
+
gives no warning.
|
|
807
|
+
|
|
808
|
+
```js
|
|
809
|
+
import { create } from 'tar'
|
|
810
|
+
|
|
811
|
+
await create(
|
|
446
812
|
{
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
entry.
|
|
454
|
-
// make the entry executable
|
|
455
|
-
entry.stat.mode = 0o755
|
|
456
|
-
// in the archive, it's lowercase and 0o755
|
|
457
|
-
filesAdded.push([entry.path, entry.stat.mode.toString(8)])
|
|
813
|
+
file: 'release.tgz',
|
|
814
|
+
gzip: { level: 9 },
|
|
815
|
+
prefix: 'package',
|
|
816
|
+
portable: true,
|
|
817
|
+
filter: path => !path.endsWith('.test.js'),
|
|
818
|
+
onWriteEntry: entry => {
|
|
819
|
+
entry.mtime = new Date('2020-01-01')
|
|
458
820
|
},
|
|
459
821
|
},
|
|
460
|
-
['
|
|
822
|
+
['src', 'package.json'],
|
|
461
823
|
)
|
|
462
|
-
console.log('added', filesAdded)
|
|
463
824
|
```
|
|
464
825
|
|
|
465
|
-
|
|
466
|
-
|
|
826
|
+
### Reproducible archives
|
|
827
|
+
|
|
828
|
+
`create()` gives the same tar bytes for the same content, on each
|
|
829
|
+
system and at each time, if you do these things:
|
|
830
|
+
|
|
831
|
+
- Set `portable: true`. It leaves out the data of one system: `uid`,
|
|
832
|
+
`gid`, `uname`, `gname`, `atime`, `ctime`, `dev`, `ino`, `nlink`,
|
|
833
|
+
and the `mtime` of a directory. It removes the write permission of
|
|
834
|
+
the group and of others from each mode.
|
|
835
|
+
- Set `mtime` to a fixed `Date`. Each file and each link then has that
|
|
836
|
+
time, and not the time of the file on disk. Without `mtime`, the
|
|
837
|
+
bytes change when a file is written again with the same content.
|
|
838
|
+
Some programs do not accept a date before 1980.
|
|
839
|
+
- Give the same paths in the same order, with the same `cwd` layout
|
|
840
|
+
and `prefix`. The names in a directory need no work: `create()`
|
|
841
|
+
writes them in sorted order on each runtime and file system.
|
|
842
|
+
- Use the same version of this package.
|
|
843
|
+
|
|
844
|
+
Two things come from the file system, and `portable` does not change
|
|
845
|
+
them:
|
|
846
|
+
|
|
847
|
+
- The mode. Windows does not keep the permission bits, so a file has
|
|
848
|
+
another mode there. To get the same bytes on each system, set
|
|
849
|
+
`entry.mode` in `onWriteEntry`.
|
|
850
|
+
- Hard links. A file with more than one link is a hard link entry the
|
|
851
|
+
second time that the pack finds it. A copy of the tree without the
|
|
852
|
+
links gives other bytes.
|
|
853
|
+
|
|
854
|
+
Compression has its own rules. With `portable`, the header of gzip
|
|
855
|
+
data does not name the system: byte 9 is 255, "unknown". The
|
|
856
|
+
compressed bytes can still differ, because they come from the zlib
|
|
857
|
+
library of the runtime. A different runtime, a different version of
|
|
858
|
+
zlib, or a different `level` can give other bytes for the same input.
|
|
859
|
+
The tar bytes in the compressed data do not differ. To compare two
|
|
860
|
+
archives, or to make a digest that must not change, use the tar bytes.
|
|
861
|
+
|
|
862
|
+
```js
|
|
863
|
+
import { createHash } from 'node:crypto'
|
|
864
|
+
import { create } from 'tar'
|
|
467
865
|
|
|
866
|
+
const options = {
|
|
867
|
+
portable: true,
|
|
868
|
+
mtime: new Date('1985-10-26T08:15:00.000Z'),
|
|
869
|
+
onWriteEntry: entry => {
|
|
870
|
+
entry.mode = entry.type === 'Directory' ? 0o755 : 0o644
|
|
871
|
+
},
|
|
872
|
+
}
|
|
873
|
+
const digest = async () => {
|
|
874
|
+
const hash = createHash('sha256')
|
|
875
|
+
for await (const chunk of create(options, ['src', 'package.json'])) {
|
|
876
|
+
hash.update(chunk)
|
|
877
|
+
}
|
|
878
|
+
return hash.digest('hex')
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
console.log((await digest()) === (await digest()))
|
|
468
882
|
```
|
|
469
|
-
$ node create-lowercase-executable.js
|
|
470
|
-
adding ./bin/SOME-BIN 644
|
|
471
|
-
added [[ './bin/some-bin', '755' ]]
|
|
472
883
|
|
|
473
|
-
|
|
474
|
-
|
|
884
|
+
### `extract(options, files)`
|
|
885
|
+
|
|
886
|
+
Extract the entries of an archive into the directory `cwd`. If `files`
|
|
887
|
+
is set, extract only the entries that it lists, and the entries in the
|
|
888
|
+
directories that it lists. The result is a `Promise<void>`. It
|
|
889
|
+
resolves when all the file system work is done.
|
|
890
|
+
|
|
891
|
+
A problem with one entry is a warning, and the extraction continues.
|
|
892
|
+
A path in `files` that no entry of the archive matches is a
|
|
893
|
+
`TAR_ENTRY_ERROR` warning when the archive ends, as it is for
|
|
894
|
+
`tar xf archive.tar not-found`. With `strict`, it is an error, and the
|
|
895
|
+
entries that match are extracted first.
|
|
896
|
+
See [Warnings and errors](#warnings-and-errors). See
|
|
897
|
+
[Security](#security) before you extract an archive that you do not
|
|
898
|
+
trust.
|
|
899
|
+
|
|
900
|
+
Options:
|
|
901
|
+
|
|
902
|
+
- `file` The path of the archive to read.
|
|
903
|
+
- `input` The archive data. Set `input` or `file`.
|
|
904
|
+
- `cwd` The directory to extract into. It must exist and be a
|
|
905
|
+
directory. If not, the command fails with a `CwdError`.
|
|
906
|
+
- `strip` Remove this number of leading parts from each path. An entry
|
|
907
|
+
with no part left is skipped, with no warning. Thus `strip: 1` does
|
|
908
|
+
not apply the mode, the owner, or the times of the top directory of
|
|
909
|
+
the archive to the extraction directory. `files` and `filter` see
|
|
910
|
+
the path before this change.
|
|
911
|
+
- `filter` A function `(path, entry)`. If it returns false, the entry
|
|
912
|
+
is not extracted.
|
|
913
|
+
- `onEntry` A function that gets each `ReadEntry` before it is
|
|
914
|
+
extracted. A promise is awaited. An entry whose body `onEntry`
|
|
915
|
+
reads is not extracted.
|
|
916
|
+
- `transform` A function that gets each file entry. It returns a
|
|
917
|
+
`TransformStream<Uint8Array, Uint8Array>` that changes the content,
|
|
918
|
+
or `undefined`. In `extractSync()`, it returns a function
|
|
919
|
+
`(chunk) => chunk`, or `undefined`. With `transform`, one entry is
|
|
920
|
+
extracted at a time. The result can be each object that has a
|
|
921
|
+
`readable` and a `writable` web stream. For a Node.js `Transform`
|
|
922
|
+
stream, return `codecStream(stream)`. See
|
|
923
|
+
[Migration from version 7](#migration-from-version-7).
|
|
924
|
+
- `keep` Do not write over an existing file.
|
|
925
|
+
- `newer` Do not write over a file that is newer than the entry.
|
|
926
|
+
- `unlink` Remove a file before the entry is written. By default the
|
|
927
|
+
content of an existing file is replaced in place, and the file keeps
|
|
928
|
+
its mode. Also remove an object in the path to an entry that is not
|
|
929
|
+
a directory, and make a directory there.
|
|
930
|
+
- `noMtime` Do not set the times of the entries.
|
|
931
|
+
- `preserveOwner` Set the owner of each entry to the `uid` and `gid`
|
|
932
|
+
in the archive. Default: true if `processUid` is 0 and `uid` is not
|
|
933
|
+
set.
|
|
934
|
+
- `uid`, `gid` Set the owner of each entry to this user and group. You
|
|
935
|
+
must set the two. You cannot set them with `preserveOwner`. Each
|
|
936
|
+
must be an integer from 0 to 4294967295. Any other number is a
|
|
937
|
+
`TypeError`.
|
|
938
|
+
- `forceChown` Set the owner of each entry, also when it is the owner
|
|
939
|
+
of the process.
|
|
940
|
+
- `chmod` Call `fs.chmod()` on each directory, so that the umask of
|
|
941
|
+
the process does not change its mode. It has no effect on files.
|
|
942
|
+
- `processUmask` The umask of the process. Only used with `chmod`.
|
|
943
|
+
- `umask` The mask for the default modes `dmode` and `fmode`. Default:
|
|
944
|
+
`processUmask` with `chmod`, 0 without it. It does not change the
|
|
945
|
+
mode of an entry that has one.
|
|
946
|
+
- `dmode` The mode of a directory that has no mode in the archive, and
|
|
947
|
+
of a parent directory that is not in the archive. Default
|
|
948
|
+
`0o777 & ~umask`.
|
|
949
|
+
- `fmode` The mode of a file that has no mode in the archive. Default
|
|
950
|
+
`0o666 & ~umask`.
|
|
951
|
+
- `preservePaths` Permit absolute paths, `..` in paths, and extraction
|
|
952
|
+
through symbolic links. This is not safe with an archive that you do
|
|
953
|
+
not trust.
|
|
954
|
+
- `maxDepth` The most parts that a path or a link target can have.
|
|
955
|
+
Default 1024. `Infinity` removes the limit.
|
|
956
|
+
- `jobs` The most entries to extract at one time. Default 16. Async
|
|
957
|
+
only.
|
|
958
|
+
- `maxReadSize` The most bytes to read from the archive file at one
|
|
959
|
+
time. Default 16 MiB.
|
|
960
|
+
- `jobs` and `maxReadSize` take a positive integer, or `Infinity`.
|
|
961
|
+
0 gives the default. Each other value is a `TypeError`.
|
|
962
|
+
- `maxMetaEntrySize`, `maxDecompressionRatio`, `codecs` The options of
|
|
963
|
+
[`entries()`](#entriesinput-options).
|
|
964
|
+
- `processUid`, `processGid` See
|
|
965
|
+
[The process options](#the-process-options).
|
|
966
|
+
- `strict`, `onwarn`, `fs`, `platform` The common options.
|
|
967
|
+
|
|
968
|
+
Files, directories, symbolic links, and hard links are extracted.
|
|
969
|
+
Devices and FIFOs are skipped with a `TAR_ENTRY_UNSUPPORTED` warning.
|
|
970
|
+
|
|
971
|
+
```js
|
|
972
|
+
import { mkdir } from 'node:fs/promises'
|
|
973
|
+
import { extract } from 'tar'
|
|
974
|
+
|
|
975
|
+
await mkdir('out', { recursive: true })
|
|
976
|
+
await extract({
|
|
977
|
+
file: 'archive.tgz',
|
|
978
|
+
cwd: 'out',
|
|
979
|
+
// No links.
|
|
980
|
+
filter: (path, entry) =>
|
|
981
|
+
entry.type !== 'SymbolicLink' && entry.type !== 'Link',
|
|
982
|
+
// Make the text of each .md file upper case.
|
|
983
|
+
transform: entry => {
|
|
984
|
+
if (!entry.path.endsWith('.md')) return undefined
|
|
985
|
+
const decoder = new TextDecoder()
|
|
986
|
+
const encoder = new TextEncoder()
|
|
987
|
+
return new TransformStream({
|
|
988
|
+
transform: (chunk, controller) => {
|
|
989
|
+
const text = decoder.decode(chunk, { stream: true })
|
|
990
|
+
controller.enqueue(encoder.encode(text.toUpperCase()))
|
|
991
|
+
},
|
|
992
|
+
})
|
|
993
|
+
},
|
|
994
|
+
onwarn: (code, message) => console.error(code, message),
|
|
995
|
+
})
|
|
475
996
|
```
|
|
476
997
|
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
### tar.x(options, fileList, callback) [alias: tar.extract]
|
|
480
|
-
|
|
481
|
-
Extract a tarball archive.
|
|
482
|
-
|
|
483
|
-
The `fileList` is an array of paths to extract from the tarball. If
|
|
484
|
-
no paths are provided, then all the entries are extracted.
|
|
485
|
-
|
|
486
|
-
If the archive is gzipped, then tar will detect this and unzip it.
|
|
487
|
-
|
|
488
|
-
Note that all directories that are created will be forced to be
|
|
489
|
-
writable, readable, and listable by their owner, to avoid cases where
|
|
490
|
-
a directory prevents extraction of child entries by virtue of its
|
|
491
|
-
mode.
|
|
492
|
-
|
|
493
|
-
Most extraction errors will cause a `warn` event to be emitted. If
|
|
494
|
-
the `cwd` is missing, or not a directory, then the extraction will
|
|
495
|
-
fail completely.
|
|
496
|
-
|
|
497
|
-
The following options are supported:
|
|
498
|
-
|
|
499
|
-
- `cwd` Extract files relative to the specified directory. Defaults
|
|
500
|
-
to `process.cwd()`. If provided, this must exist and must be a
|
|
501
|
-
directory. [Alias: `C`]
|
|
502
|
-
- `file` The archive file to extract. If not specified, then a
|
|
503
|
-
Writable stream is returned where the archive data should be
|
|
504
|
-
written. [Alias: `f`]
|
|
505
|
-
- `sync` Create files and directories synchronously.
|
|
506
|
-
- `strict` Treat warnings as crash-worthy errors. Default false.
|
|
507
|
-
- `filter` A function that gets called with `(path, entry)` for each
|
|
508
|
-
entry being unpacked. Return `true` to unpack the entry from the
|
|
509
|
-
archive, or `false` to skip it.
|
|
510
|
-
- `newer` Set to true to keep the existing file on disk if it's newer
|
|
511
|
-
than the file in the archive. [Alias: `keep-newer`,
|
|
512
|
-
`keep-newer-files`]
|
|
513
|
-
- `keep` Do not overwrite existing files. In particular, if a file
|
|
514
|
-
appears more than once in an archive, later copies will not
|
|
515
|
-
overwrite earlier copies. [Alias: `k`, `keep-existing`]
|
|
516
|
-
- `preservePaths` Allow absolute paths, paths containing `..`, and
|
|
517
|
-
extracting through symbolic links. By default, `/` is stripped from
|
|
518
|
-
absolute paths, `..` paths are not extracted, and any file whose
|
|
519
|
-
location would be modified by a symbolic link is not extracted.
|
|
520
|
-
[Alias: `P`]
|
|
521
|
-
- `unlink` Unlink files before creating them. Without this option,
|
|
522
|
-
tar overwrites existing files, which preserves existing hardlinks.
|
|
523
|
-
With this option, existing hardlinks will be broken, as will any
|
|
524
|
-
symlink that would affect the location of an extracted file. [Alias:
|
|
525
|
-
`U`]
|
|
526
|
-
- `strip` Remove the specified number of leading path elements.
|
|
527
|
-
Pathnames with fewer elements will be silently skipped. Note that
|
|
528
|
-
the pathname is edited after applying the filter, but before
|
|
529
|
-
security checks. [Alias: `strip-components`, `stripComponents`]
|
|
530
|
-
- `preserveOwner` If true, tar will set the `uid` and `gid` of
|
|
531
|
-
extracted entries to the `uid` and `gid` fields in the archive.
|
|
532
|
-
This defaults to true when run as root, and false otherwise. If
|
|
533
|
-
false, then files and directories will be set with the owner and
|
|
534
|
-
group of the user running the process. This is similar to `-p` in
|
|
535
|
-
`tar(1)`, but ACLs and other system-specific data is never unpacked
|
|
536
|
-
in this implementation, and modes are set by default already.
|
|
537
|
-
[Alias: `p`]
|
|
538
|
-
- `uid` Set to a number to force ownership of all extracted files and
|
|
539
|
-
folders, and all implicitly created directories, to be owned by the
|
|
540
|
-
specified user id, regardless of the `uid` field in the archive.
|
|
541
|
-
Cannot be used along with `preserveOwner`. Requires also setting a
|
|
542
|
-
`gid` option.
|
|
543
|
-
- `gid` Set to a number to force ownership of all extracted files and
|
|
544
|
-
folders, and all implicitly created directories, to be owned by the
|
|
545
|
-
specified group id, regardless of the `gid` field in the archive.
|
|
546
|
-
Cannot be used along with `preserveOwner`. Requires also setting a
|
|
547
|
-
`uid` option.
|
|
548
|
-
- `noMtime` Set to true to omit writing `mtime` value for extracted
|
|
549
|
-
entries. [Alias: `m`, `no-mtime`]
|
|
550
|
-
- `transform` Provide a function that takes an `entry` object, and
|
|
551
|
-
returns a stream, or any falsey value. If a stream is provided,
|
|
552
|
-
then that stream's data will be written instead of the contents of
|
|
553
|
-
the archive entry. If a falsey value is provided, then the entry is
|
|
554
|
-
written to disk as normal. (To exclude items from extraction, use
|
|
555
|
-
the `filter` option described above.)
|
|
556
|
-
- `onReadEntry` A function that gets called with `(entry)` for each entry
|
|
557
|
-
that passes the filter.
|
|
558
|
-
- `onwarn` A function that will get called with `(code, message, data)` for
|
|
559
|
-
any warnings encountered. (See "Warnings and Errors")
|
|
560
|
-
- `chmod` Set to true to call `fs.chmod()` to ensure that the
|
|
561
|
-
extracted file matches the entry mode. This may necessitate a
|
|
562
|
-
call to the deprecated and thread-unsafe `process.umask()`
|
|
563
|
-
method to determine the default umask value, unless a
|
|
564
|
-
`processUmask` options is also provided. Otherwise tar will
|
|
565
|
-
extract with whatever mode is provided, and let the process
|
|
566
|
-
`umask` apply normally.
|
|
567
|
-
- `processUmask` Set to an explicit numeric value to avoid
|
|
568
|
-
calling `process.umask()` when `chmod: true` is set.
|
|
569
|
-
- `maxDepth` The maximum depth of subfolders to extract into. This
|
|
570
|
-
defaults to 1024. Anything deeper than the limit will raise a
|
|
571
|
-
warning and skip the entry. Set to `Infinity` to remove the
|
|
572
|
-
limitation.
|
|
573
|
-
- `maxDecompressionRatio` Defaults to 1000. The maximum ratio of
|
|
574
|
-
decommpressed bytes to compressed bytes, in a compressed
|
|
575
|
-
archive. Set to `Infinity` to allow explosive decompression.
|
|
576
|
-
|
|
577
|
-
The following options are mostly internal, but can be modified in some
|
|
578
|
-
advanced use cases, such as re-using caches between runs.
|
|
579
|
-
|
|
580
|
-
- `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|
581
|
-
Defaults to 16 MB.
|
|
582
|
-
- `umask` Filter the modes of entries like `process.umask()`.
|
|
583
|
-
- `dmode` Default mode for directories
|
|
584
|
-
- `fmode` Default mode for files
|
|
585
|
-
- `maxMetaEntrySize` The maximum size of meta entries that is
|
|
586
|
-
supported. Defaults to 1 MB.
|
|
587
|
-
|
|
588
|
-
Note that using an asynchronous stream type with the `transform`
|
|
589
|
-
option will cause undefined behavior in sync extractions.
|
|
590
|
-
[MiniPass](http://npm.im/minipass)-based streams are designed for this
|
|
591
|
-
use case.
|
|
592
|
-
|
|
593
|
-
### tar.t(options, fileList, callback) [alias: tar.list]
|
|
594
|
-
|
|
595
|
-
List the contents of a tarball archive.
|
|
596
|
-
|
|
597
|
-
The `fileList` is an array of paths to list from the tarball. If
|
|
598
|
-
no paths are provided, then all the entries are listed.
|
|
599
|
-
|
|
600
|
-
If the archive is gzipped, then tar will detect this and unzip it.
|
|
601
|
-
|
|
602
|
-
If the `file` option is _not_ provided, then returns an event emitter that
|
|
603
|
-
emits `entry` events with `tar.ReadEntry` objects. However, they don't
|
|
604
|
-
emit `'data'` or `'end'` events. (If you want to get actual readable
|
|
605
|
-
entries, use the `tar.Parser` class instead.)
|
|
606
|
-
|
|
607
|
-
If a `file` option _is_ provided, then the return value will be a promise
|
|
608
|
-
that resolves when the file has been fully traversed in async mode, or
|
|
609
|
-
`undefined` if `sync: true` is set. Thus, you _must_ specify an `onReadEntry`
|
|
610
|
-
method in order to do anything useful with the data it parses.
|
|
611
|
-
|
|
612
|
-
The following options are supported:
|
|
613
|
-
|
|
614
|
-
- `file` The archive file to list. If not specified, then a
|
|
615
|
-
Writable stream is returned where the archive data should be
|
|
616
|
-
written. [Alias: `f`]
|
|
617
|
-
- `sync` Read the specified file synchronously. (This has no effect
|
|
618
|
-
when a file option isn't specified, because entries are emitted as
|
|
619
|
-
fast as they are parsed from the stream anyway.)
|
|
620
|
-
- `strict` Treat warnings as crash-worthy errors. Default false.
|
|
621
|
-
- `filter` A function that gets called with `(path, entry)` for each
|
|
622
|
-
entry being listed. Return `true` to emit the entry from the
|
|
623
|
-
archive, or `false` to skip it.
|
|
624
|
-
- `onReadEntry` A function that gets called with `(entry)` for each entry
|
|
625
|
-
that passes the filter. This is important for when `file` is set,
|
|
626
|
-
because there is no other way to do anything useful with this method.
|
|
627
|
-
- `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|
628
|
-
Defaults to 16 MB.
|
|
629
|
-
- `noResume` By default, `entry` streams are resumed immediately after
|
|
630
|
-
the call to `onReadEntry`. Set `noResume: true` to suppress this
|
|
631
|
-
behavior. Note that by opting into this, the stream will never
|
|
632
|
-
complete until the entry data is consumed.
|
|
633
|
-
- `onwarn` A function that will get called with `(code, message, data)` for
|
|
634
|
-
any warnings encountered. (See "Warnings and Errors")
|
|
635
|
-
- `maxDecompressionRatio` Defaults to 1000. The maximum ratio of
|
|
636
|
-
decommpressed bytes to compressed bytes, in a compressed
|
|
637
|
-
archive. Set to `Infinity` to allow explosive decompression.
|
|
638
|
-
|
|
639
|
-
### tar.u(options, fileList, callback) [alias: tar.update]
|
|
640
|
-
|
|
641
|
-
Add files to an archive if they are newer than the entry already in
|
|
642
|
-
the tarball archive.
|
|
643
|
-
|
|
644
|
-
The `fileList` is an array of paths to add to the tarball. Adding a
|
|
645
|
-
directory also adds its children recursively.
|
|
646
|
-
|
|
647
|
-
An entry in `fileList` that starts with an `@` symbol is a tar archive
|
|
648
|
-
whose entries will be added. To add a file that starts with `@`,
|
|
649
|
-
prepend it with `./`.
|
|
650
|
-
|
|
651
|
-
The following options are supported:
|
|
652
|
-
|
|
653
|
-
- `file` Required. Write the tarball archive to the specified
|
|
654
|
-
filename. [Alias: `f`]
|
|
655
|
-
- `sync` Act synchronously. If this is set, then any provided file
|
|
656
|
-
will be fully written after the call to `tar.c`.
|
|
657
|
-
- `onwarn` A function that will get called with `(code, message, data)` for
|
|
658
|
-
any warnings encountered. (See "Warnings and Errors")
|
|
659
|
-
- `strict` Treat warnings as crash-worthy errors. Default false.
|
|
660
|
-
- `cwd` The current working directory for adding entries to the
|
|
661
|
-
archive. Defaults to `process.cwd()`. [Alias: `C`]
|
|
662
|
-
- `prefix` A path portion to prefix onto the entries in the archive.
|
|
663
|
-
- `gzip` Set to any truthy value to create a gzipped archive, or an
|
|
664
|
-
object with settings for `zlib.Gzip()` [Alias: `z`]
|
|
665
|
-
- `filter` A function that gets called with `(path, stat)` for each
|
|
666
|
-
entry being added. Return `true` to add the entry to the archive,
|
|
667
|
-
or `false` to omit it.
|
|
668
|
-
- `portable` Omit metadata that is system-specific: `ctime`, `atime`,
|
|
669
|
-
`uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and `nlink`. Note
|
|
670
|
-
that `mtime` is still included, because this is necessary for other
|
|
671
|
-
time-based operations. Additionally, `mode` is set to a "reasonable
|
|
672
|
-
default" for most unix systems, based on a `umask` value of `0o22`.
|
|
673
|
-
- `preservePaths` Allow absolute paths. By default, `/` is stripped
|
|
674
|
-
from absolute paths. [Alias: `P`]
|
|
675
|
-
- `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|
676
|
-
Defaults to 16 MB.
|
|
677
|
-
- `noDirRecurse` Do not recursively archive the contents of
|
|
678
|
-
directories. [Alias: `n`]
|
|
679
|
-
- `follow` Set to true to pack the targets of symbolic links. Without
|
|
680
|
-
this option, symbolic links are archived as such. [Alias: `L`, `h`]
|
|
681
|
-
- `noPax` Suppress pax extended headers. Note that this means that
|
|
682
|
-
long paths and linkpaths will be truncated, and large or negative
|
|
683
|
-
numeric values may be interpreted incorrectly.
|
|
684
|
-
- `noMtime` Set to true to omit writing `mtime` values for entries.
|
|
685
|
-
Note that this prevents using other mtime-based features like
|
|
686
|
-
`tar.update` or the `keepNewer` option with the resulting tar archive.
|
|
687
|
-
[Alias: `m`, `no-mtime`]
|
|
688
|
-
- `mtime` Set to a `Date` object to force a specific `mtime` for
|
|
689
|
-
everything added to the archive. Overridden by `noMtime`.
|
|
690
|
-
- `onWriteEntry` Called with each `WriteEntry` or
|
|
691
|
-
`WriteEntrySync` that is created in the course of writing the
|
|
692
|
-
archive.
|
|
693
|
-
|
|
694
|
-
### tar.r(options, fileList, callback) [alias: tar.replace]
|
|
695
|
-
|
|
696
|
-
Add files to an existing archive. Because later entries override
|
|
697
|
-
earlier entries, this effectively replaces any existing entries.
|
|
698
|
-
|
|
699
|
-
The `fileList` is an array of paths to add to the tarball. Adding a
|
|
700
|
-
directory also adds its children recursively.
|
|
701
|
-
|
|
702
|
-
An entry in `fileList` that starts with an `@` symbol is a tar archive
|
|
703
|
-
whose entries will be added. To add a file that starts with `@`,
|
|
704
|
-
prepend it with `./`.
|
|
705
|
-
|
|
706
|
-
The following options are supported:
|
|
707
|
-
|
|
708
|
-
- `file` Required. Write the tarball archive to the specified
|
|
709
|
-
filename. [Alias: `f`]
|
|
710
|
-
- `sync` Act synchronously. If this is set, then any provided file
|
|
711
|
-
will be fully written after the call to `tar.c`.
|
|
712
|
-
- `onwarn` A function that will get called with `(code, message, data)` for
|
|
713
|
-
any warnings encountered. (See "Warnings and Errors")
|
|
714
|
-
- `strict` Treat warnings as crash-worthy errors. Default false.
|
|
715
|
-
- `cwd` The current working directory for adding entries to the
|
|
716
|
-
archive. Defaults to `process.cwd()`. [Alias: `C`]
|
|
717
|
-
- `prefix` A path portion to prefix onto the entries in the archive.
|
|
718
|
-
- `gzip` Set to any truthy value to create a gzipped archive, or an
|
|
719
|
-
object with settings for `zlib.Gzip()` [Alias: `z`]
|
|
720
|
-
- `filter` A function that gets called with `(path, stat)` for each
|
|
721
|
-
entry being added. Return `true` to add the entry to the archive,
|
|
722
|
-
or `false` to omit it.
|
|
723
|
-
- `portable` Omit metadata that is system-specific: `ctime`, `atime`,
|
|
724
|
-
`uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and `nlink`. Note
|
|
725
|
-
that `mtime` is still included, because this is necessary for other
|
|
726
|
-
time-based operations. Additionally, `mode` is set to a "reasonable
|
|
727
|
-
default" for most unix systems, based on a `umask` value of `0o22`.
|
|
728
|
-
- `preservePaths` Allow absolute paths. By default, `/` is stripped
|
|
729
|
-
from absolute paths. [Alias: `P`]
|
|
730
|
-
- `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|
731
|
-
Defaults to 16 MB.
|
|
732
|
-
- `noDirRecurse` Do not recursively archive the contents of
|
|
733
|
-
directories. [Alias: `n`]
|
|
734
|
-
- `follow` Set to true to pack the targets of symbolic links. Without
|
|
735
|
-
this option, symbolic links are archived as such. [Alias: `L`, `h`]
|
|
736
|
-
- `noPax` Suppress pax extended headers. Note that this means that
|
|
737
|
-
long paths and linkpaths will be truncated, and large or negative
|
|
738
|
-
numeric values may be interpreted incorrectly.
|
|
739
|
-
- `noMtime` Set to true to omit writing `mtime` values for entries.
|
|
740
|
-
Note that this prevents using other mtime-based features like
|
|
741
|
-
`tar.update` or the `keepNewer` option with the resulting tar archive.
|
|
742
|
-
[Alias: `m`, `no-mtime`]
|
|
743
|
-
- `mtime` Set to a `Date` object to force a specific `mtime` for
|
|
744
|
-
everything added to the archive. Overridden by `noMtime`.
|
|
745
|
-
- `onWriteEntry` Called with each `WriteEntry` or
|
|
746
|
-
`WriteEntrySync` that is created in the course of writing the
|
|
747
|
-
archive.
|
|
748
|
-
|
|
749
|
-
## Low-Level API
|
|
750
|
-
|
|
751
|
-
### class Pack
|
|
752
|
-
|
|
753
|
-
A readable tar stream.
|
|
754
|
-
|
|
755
|
-
Has all the standard readable stream interface stuff. `'data'` and
|
|
756
|
-
`'end'` events, `read()` method, `pause()` and `resume()`, etc.
|
|
757
|
-
|
|
758
|
-
#### constructor(options)
|
|
759
|
-
|
|
760
|
-
The following options are supported:
|
|
761
|
-
|
|
762
|
-
- `onwarn` A function that will get called with `(code, message, data)` for
|
|
763
|
-
any warnings encountered. (See "Warnings and Errors")
|
|
764
|
-
- `strict` Treat warnings as crash-worthy errors. Default false.
|
|
765
|
-
- `cwd` The current working directory for creating the archive.
|
|
766
|
-
Defaults to `process.cwd()`.
|
|
767
|
-
- `prefix` A path portion to prefix onto the entries in the archive.
|
|
768
|
-
- `gzip` Set to any truthy value to create a gzipped archive, or an
|
|
769
|
-
object with settings for `zlib.Gzip()`
|
|
770
|
-
- `filter` A function that gets called with `(path, stat)` for each
|
|
771
|
-
entry being added. Return `true` to add the entry to the archive,
|
|
772
|
-
or `false` to omit it.
|
|
773
|
-
- `portable` Omit metadata that is system-specific: `ctime`, `atime`,
|
|
774
|
-
`uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and `nlink`. Note
|
|
775
|
-
that `mtime` is still included, because this is necessary for other
|
|
776
|
-
time-based operations. Additionally, `mode` is set to a "reasonable
|
|
777
|
-
default" for most unix systems, based on a `umask` value of `0o22`.
|
|
778
|
-
- `preservePaths` Allow absolute paths. By default, `/` is stripped
|
|
779
|
-
from absolute paths.
|
|
780
|
-
- `linkCache` A Map object containing the device and inode value for
|
|
781
|
-
any file whose nlink is > 1, to identify hard links.
|
|
782
|
-
- `statCache` A Map object that caches calls `lstat`.
|
|
783
|
-
- `readdirCache` A Map object that caches calls to `readdir`.
|
|
784
|
-
- `jobs` A number specifying how many concurrent jobs to run.
|
|
785
|
-
Defaults to 4.
|
|
786
|
-
- `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|
787
|
-
Defaults to 16 MB.
|
|
788
|
-
- `noDirRecurse` Do not recursively archive the contents of
|
|
789
|
-
directories.
|
|
790
|
-
- `follow` Set to true to pack the targets of symbolic links. Without
|
|
791
|
-
this option, symbolic links are archived as such.
|
|
792
|
-
- `noPax` Suppress pax extended headers. Note that this means that
|
|
793
|
-
long paths and linkpaths will be truncated, and large or negative
|
|
794
|
-
numeric values may be interpreted incorrectly.
|
|
795
|
-
- `noMtime` Set to true to omit writing `mtime` values for entries.
|
|
796
|
-
Note that this prevents using other mtime-based features like
|
|
797
|
-
`tar.update` or the `keepNewer` option with the resulting tar archive.
|
|
798
|
-
- `mtime` Set to a `Date` object to force a specific `mtime` for
|
|
799
|
-
everything added to the archive. Overridden by `noMtime`.
|
|
800
|
-
- `onWriteEntry` Called with each `WriteEntry` or
|
|
801
|
-
`WriteEntrySync` that is created in the course of writing the
|
|
802
|
-
archive.
|
|
803
|
-
|
|
804
|
-
#### add(path)
|
|
805
|
-
|
|
806
|
-
Adds an entry to the archive. Returns the Pack stream.
|
|
807
|
-
|
|
808
|
-
#### write(path)
|
|
809
|
-
|
|
810
|
-
Adds an entry to the archive. Returns true if flushed.
|
|
811
|
-
|
|
812
|
-
#### end()
|
|
813
|
-
|
|
814
|
-
Finishes the archive.
|
|
815
|
-
|
|
816
|
-
### class PackSync
|
|
817
|
-
|
|
818
|
-
Synchronous version of `Pack`.
|
|
819
|
-
|
|
820
|
-
### class Unpack
|
|
821
|
-
|
|
822
|
-
A writable stream that unpacks a tar archive onto the file system.
|
|
823
|
-
|
|
824
|
-
All the normal writable stream stuff is supported. `write()` and
|
|
825
|
-
`end()` methods, `'drain'` events, etc.
|
|
826
|
-
|
|
827
|
-
Note that all directories that are created will be forced to be
|
|
828
|
-
writable, readable, and listable by their owner, to avoid cases where
|
|
829
|
-
a directory prevents extraction of child entries by virtue of its
|
|
830
|
-
mode.
|
|
831
|
-
|
|
832
|
-
`'close'` is emitted when it's done writing stuff to the file system.
|
|
833
|
-
|
|
834
|
-
Most unpack errors will cause a `warn` event to be emitted. If the
|
|
835
|
-
`cwd` is missing, or not a directory, then an error will be emitted.
|
|
836
|
-
|
|
837
|
-
#### constructor(options)
|
|
838
|
-
|
|
839
|
-
- `cwd` Extract files relative to the specified directory. Defaults
|
|
840
|
-
to `process.cwd()`. If provided, this must exist and must be a
|
|
841
|
-
directory.
|
|
842
|
-
- `filter` A function that gets called with `(path, entry)` for each
|
|
843
|
-
entry being unpacked. Return `true` to unpack the entry from the
|
|
844
|
-
archive, or `false` to skip it.
|
|
845
|
-
- `newer` Set to true to keep the existing file on disk if it's newer
|
|
846
|
-
than the file in the archive.
|
|
847
|
-
- `keep` Do not overwrite existing files. In particular, if a file
|
|
848
|
-
appears more than once in an archive, later copies will not
|
|
849
|
-
overwrite earlier copies.
|
|
850
|
-
- `preservePaths` Allow absolute paths, paths containing `..`, and
|
|
851
|
-
extracting through symbolic links. By default, `/` is stripped from
|
|
852
|
-
absolute paths, `..` paths are not extracted, and any file whose
|
|
853
|
-
location would be modified by a symbolic link is not extracted.
|
|
854
|
-
- `unlink` Unlink files before creating them. Without this option,
|
|
855
|
-
tar overwrites existing files, which preserves existing hardlinks.
|
|
856
|
-
With this option, existing hardlinks will be broken, as will any
|
|
857
|
-
symlink that would affect the location of an extracted file.
|
|
858
|
-
- `strip` Remove the specified number of leading path elements.
|
|
859
|
-
Pathnames with fewer elements will be silently skipped. Note that
|
|
860
|
-
the pathname is edited after applying the filter, but before
|
|
861
|
-
security checks.
|
|
862
|
-
- `umask` Filter the modes of entries like `process.umask()`.
|
|
863
|
-
- `dmode` Default mode for directories
|
|
864
|
-
- `fmode` Default mode for files
|
|
865
|
-
- `maxMetaEntrySize` The maximum size of meta entries that is
|
|
866
|
-
supported. Defaults to 1 MB.
|
|
867
|
-
- `preserveOwner` If true, tar will set the `uid` and `gid` of
|
|
868
|
-
extracted entries to the `uid` and `gid` fields in the archive.
|
|
869
|
-
This defaults to true when run as root, and false otherwise. If
|
|
870
|
-
false, then files and directories will be set with the owner and
|
|
871
|
-
group of the user running the process. This is similar to `-p` in
|
|
872
|
-
`tar(1)`, but ACLs and other system-specific data is never unpacked
|
|
873
|
-
in this implementation, and modes are set by default already.
|
|
874
|
-
- `win32` True if on a windows platform. Causes behavior where
|
|
875
|
-
filenames containing `<|>?` chars are converted to
|
|
876
|
-
windows-compatible values while being unpacked.
|
|
877
|
-
- `uid` Set to a number to force ownership of all extracted files and
|
|
878
|
-
folders, and all implicitly created directories, to be owned by the
|
|
879
|
-
specified user id, regardless of the `uid` field in the archive.
|
|
880
|
-
Cannot be used along with `preserveOwner`. Requires also setting a
|
|
881
|
-
`gid` option.
|
|
882
|
-
- `gid` Set to a number to force ownership of all extracted files and
|
|
883
|
-
folders, and all implicitly created directories, to be owned by the
|
|
884
|
-
specified group id, regardless of the `gid` field in the archive.
|
|
885
|
-
Cannot be used along with `preserveOwner`. Requires also setting a
|
|
886
|
-
`uid` option.
|
|
887
|
-
- `noMtime` Set to true to omit writing `mtime` value for extracted
|
|
888
|
-
entries.
|
|
889
|
-
- `transform` Provide a function that takes an `entry` object, and
|
|
890
|
-
returns a stream, or any falsey value. If a stream is provided,
|
|
891
|
-
then that stream's data will be written instead of the contents of
|
|
892
|
-
the archive entry. If a falsey value is provided, then the entry is
|
|
893
|
-
written to disk as normal. (To exclude items from extraction, use
|
|
894
|
-
the `filter` option described above.)
|
|
895
|
-
- `strict` Treat warnings as crash-worthy errors. Default false.
|
|
896
|
-
- `onReadEntry` A function that gets called with `(entry)` for each entry
|
|
897
|
-
that passes the filter.
|
|
898
|
-
- `onwarn` A function that will get called with `(code, message, data)` for
|
|
899
|
-
any warnings encountered. (See "Warnings and Errors")
|
|
900
|
-
- `chmod` Set to true to call `fs.chmod()` to ensure that the
|
|
901
|
-
extracted file matches the entry mode. This may necessitate a
|
|
902
|
-
call to the deprecated and thread-unsafe `process.umask()`
|
|
903
|
-
method to determine the default umask value, unless a
|
|
904
|
-
`processUmask` options is also provided. Otherwise tar will
|
|
905
|
-
extract with whatever mode is provided, and let the process
|
|
906
|
-
`umask` apply normally.
|
|
907
|
-
- `processUmask` Set to an explicit numeric value to avoid
|
|
908
|
-
calling `process.umask()` when `chmod: true` is set.
|
|
909
|
-
- `maxDepth` The maximum depth of subfolders to extract into. This
|
|
910
|
-
defaults to 1024. Anything deeper than the limit will raise a
|
|
911
|
-
warning and skip the entry. Set to `Infinity` to remove the
|
|
912
|
-
limitation.
|
|
913
|
-
- `maxDecompressionRatio` Defaults to 1000. The maximum ratio of
|
|
914
|
-
decommpressed bytes to compressed bytes, in a compressed
|
|
915
|
-
archive. Set to `Infinity` to allow explosive decompression.
|
|
916
|
-
|
|
917
|
-
### class UnpackSync
|
|
918
|
-
|
|
919
|
-
Synchronous version of `Unpack`.
|
|
920
|
-
|
|
921
|
-
Note that using an asynchronous stream type with the `transform`
|
|
922
|
-
option will cause undefined behavior in sync unpack streams.
|
|
923
|
-
[MiniPass](http://npm.im/minipass)-based streams are designed for this
|
|
924
|
-
use case.
|
|
925
|
-
|
|
926
|
-
### class tar.Parser
|
|
927
|
-
|
|
928
|
-
A writable stream that parses a tar archive stream. All the standard
|
|
929
|
-
writable stream stuff is supported.
|
|
930
|
-
|
|
931
|
-
If the archive is gzipped, then tar will detect this and unzip it.
|
|
932
|
-
|
|
933
|
-
Emits `'entry'` events with `tar.ReadEntry` objects, which are
|
|
934
|
-
themselves readable streams that you can pipe wherever.
|
|
935
|
-
|
|
936
|
-
Each `entry` will not emit until the one before it is flushed through,
|
|
937
|
-
so make sure to either consume the data (with `on('data', ...)` or
|
|
938
|
-
`.pipe(...)`) or throw it away with `.resume()` to keep the stream
|
|
939
|
-
flowing.
|
|
940
|
-
|
|
941
|
-
#### constructor(options)
|
|
942
|
-
|
|
943
|
-
Returns an event emitter that emits `entry` events with
|
|
944
|
-
`tar.ReadEntry` objects.
|
|
945
|
-
|
|
946
|
-
The following options are supported:
|
|
947
|
-
|
|
948
|
-
- `strict` Treat warnings as crash-worthy errors. Default false.
|
|
949
|
-
- `filter` A function that gets called with `(path, entry)` for each
|
|
950
|
-
entry being listed. Return `true` to emit the entry from the
|
|
951
|
-
archive, or `false` to skip it.
|
|
952
|
-
- `onReadEntry` A function that gets called with `(entry)` for each entry
|
|
953
|
-
that passes the filter.
|
|
954
|
-
- `onwarn` A function that will get called with `(code, message, data)` for
|
|
955
|
-
any warnings encountered. (See "Warnings and Errors")
|
|
956
|
-
|
|
957
|
-
#### abort(error)
|
|
958
|
-
|
|
959
|
-
Stop all parsing activities. This is called when there are zlib
|
|
960
|
-
errors. It also emits an unrecoverable warning with the error provided.
|
|
961
|
-
|
|
962
|
-
### class tar.ReadEntry extends [MiniPass](http://npm.im/minipass)
|
|
963
|
-
|
|
964
|
-
A representation of an entry that is being read out of a tar archive.
|
|
965
|
-
|
|
966
|
-
It has the following fields:
|
|
967
|
-
|
|
968
|
-
- `extended` The extended metadata object provided to the constructor.
|
|
969
|
-
- `globalExtended` The global extended metadata object provided to the
|
|
970
|
-
constructor.
|
|
971
|
-
- `remain` The number of bytes remaining to be written into the
|
|
972
|
-
stream.
|
|
973
|
-
- `blockRemain` The number of 512-byte blocks remaining to be written
|
|
974
|
-
into the stream.
|
|
975
|
-
- `ignore` Whether this entry should be ignored.
|
|
976
|
-
- `meta` True if this represents metadata about the next entry, false
|
|
977
|
-
if it represents a filesystem object.
|
|
978
|
-
- All the fields from the header, extended header, and global extended
|
|
979
|
-
header are added to the ReadEntry object. So it has `path`, `type`,
|
|
980
|
-
`size`, `mode`, and so on.
|
|
981
|
-
|
|
982
|
-
#### constructor(header, extended, globalExtended)
|
|
983
|
-
|
|
984
|
-
Create a new ReadEntry object with the specified header, extended
|
|
985
|
-
header, and global extended header values.
|
|
986
|
-
|
|
987
|
-
### class tar.WriteEntry extends [MiniPass](http://npm.im/minipass)
|
|
988
|
-
|
|
989
|
-
A representation of an entry that is being written from the file
|
|
990
|
-
system into a tar archive.
|
|
991
|
-
|
|
992
|
-
Emits data for the Header, and for the Pax Extended Header if one is
|
|
993
|
-
required, as well as any body data.
|
|
994
|
-
|
|
995
|
-
Creating a WriteEntry for a directory does not also create
|
|
996
|
-
WriteEntry objects for all of the directory contents.
|
|
997
|
-
|
|
998
|
-
It has the following fields:
|
|
999
|
-
|
|
1000
|
-
- `path` The path field that will be written to the archive. By
|
|
1001
|
-
default, this is also the path from the cwd to the file system
|
|
1002
|
-
object.
|
|
1003
|
-
- `portable` Omit metadata that is system-specific: `ctime`, `atime`,
|
|
1004
|
-
`uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and `nlink`. Note
|
|
1005
|
-
that `mtime` is still included, because this is necessary for other
|
|
1006
|
-
time-based operations. Additionally, `mode` is set to a "reasonable
|
|
1007
|
-
default" for most unix systems, based on a `umask` value of `0o22`.
|
|
1008
|
-
- `myuid` If supported, the uid of the user running the current
|
|
1009
|
-
process.
|
|
1010
|
-
- `myuser` The `env.USER` string if set, or `''`. Set as the entry
|
|
1011
|
-
`uname` field if the file's `uid` matches `this.myuid`.
|
|
1012
|
-
- `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|
1013
|
-
Defaults to 1 MB.
|
|
1014
|
-
- `linkCache` A Map object containing the device and inode value for
|
|
1015
|
-
any file whose nlink is > 1, to identify hard links.
|
|
1016
|
-
- `statCache` A Map object that caches calls `lstat`.
|
|
1017
|
-
- `preservePaths` Allow absolute paths. By default, `/` is stripped
|
|
1018
|
-
from absolute paths.
|
|
1019
|
-
- `cwd` The current working directory for creating the archive.
|
|
1020
|
-
Defaults to `process.cwd()`.
|
|
1021
|
-
- `absolute` The absolute path to the entry on the filesystem. By
|
|
1022
|
-
default, this is `path.resolve(this.cwd, this.path)`, but it can be
|
|
1023
|
-
overridden explicitly.
|
|
1024
|
-
- `strict` Treat warnings as crash-worthy errors. Default false.
|
|
1025
|
-
- `win32` True if on a windows platform. Causes behavior where paths
|
|
1026
|
-
replace `\` with `/` and filenames containing the windows-compatible
|
|
1027
|
-
forms of `<|>?:` characters are converted to actual `<|>?:` characters
|
|
1028
|
-
in the archive.
|
|
1029
|
-
- `noPax` Suppress pax extended headers. Note that this means that
|
|
1030
|
-
long paths and linkpaths will be truncated, and large or negative
|
|
1031
|
-
numeric values may be interpreted incorrectly.
|
|
1032
|
-
- `noMtime` Set to true to omit writing `mtime` values for entries.
|
|
1033
|
-
Note that this prevents using other mtime-based features like
|
|
1034
|
-
`tar.update` or the `keepNewer` option with the resulting tar archive.
|
|
1035
|
-
|
|
1036
|
-
#### constructor(path, options)
|
|
1037
|
-
|
|
1038
|
-
`path` is the path of the entry as it is written in the archive.
|
|
1039
|
-
|
|
1040
|
-
The following options are supported:
|
|
1041
|
-
|
|
1042
|
-
- `portable` Omit metadata that is system-specific: `ctime`, `atime`,
|
|
1043
|
-
`uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and `nlink`. Note
|
|
1044
|
-
that `mtime` is still included, because this is necessary for other
|
|
1045
|
-
time-based operations. Additionally, `mode` is set to a "reasonable
|
|
1046
|
-
default" for most unix systems, based on a `umask` value of `0o22`.
|
|
1047
|
-
- `maxReadSize` The maximum buffer size for `fs.read()` operations.
|
|
1048
|
-
Defaults to 1 MB.
|
|
1049
|
-
- `linkCache` A Map object containing the device and inode value for
|
|
1050
|
-
any file whose nlink is > 1, to identify hard links.
|
|
1051
|
-
- `statCache` A Map object that caches calls `lstat`.
|
|
1052
|
-
- `preservePaths` Allow absolute paths. By default, `/` is stripped
|
|
1053
|
-
from absolute paths.
|
|
1054
|
-
- `cwd` The current working directory for creating the archive.
|
|
1055
|
-
Defaults to `process.cwd()`.
|
|
1056
|
-
- `absolute` The absolute path to the entry on the filesystem. By
|
|
1057
|
-
default, this is `path.resolve(this.cwd, this.path)`, but it can be
|
|
1058
|
-
overridden explicitly.
|
|
1059
|
-
- `strict` Treat warnings as crash-worthy errors. Default false.
|
|
1060
|
-
- `win32` True if on a windows platform. Causes behavior where paths
|
|
1061
|
-
replace `\` with `/`.
|
|
1062
|
-
- `onwarn` A function that will get called with `(code, message, data)` for
|
|
1063
|
-
any warnings encountered. (See "Warnings and Errors")
|
|
1064
|
-
- `noMtime` Set to true to omit writing `mtime` values for entries.
|
|
1065
|
-
Note that this prevents using other mtime-based features like
|
|
1066
|
-
`tar.update` or the `keepNewer` option with the resulting tar archive.
|
|
1067
|
-
- `umask` Set to restrict the modes on the entries in the archive,
|
|
1068
|
-
somewhat like how umask works on file creation. Defaults to
|
|
1069
|
-
`process.umask()` on unix systems, or `0o22` on Windows.
|
|
1070
|
-
|
|
1071
|
-
#### warn(message, data)
|
|
1072
|
-
|
|
1073
|
-
If strict, emit an error with the provided message.
|
|
1074
|
-
|
|
1075
|
-
Otherwise, emit a `'warn'` event with the provided message and data.
|
|
1076
|
-
|
|
1077
|
-
### class tar.WriteEntry.Sync
|
|
1078
|
-
|
|
1079
|
-
Synchronous version of tar.WriteEntry
|
|
1080
|
-
|
|
1081
|
-
### class tar.WriteEntry.Tar
|
|
1082
|
-
|
|
1083
|
-
A version of tar.WriteEntry that gets its data from a tar.ReadEntry
|
|
1084
|
-
instead of from the filesystem.
|
|
1085
|
-
|
|
1086
|
-
#### constructor(readEntry, options)
|
|
1087
|
-
|
|
1088
|
-
`readEntry` is the entry being read out of another archive.
|
|
1089
|
-
|
|
1090
|
-
The following options are supported:
|
|
1091
|
-
|
|
1092
|
-
- `portable` Omit metadata that is system-specific: `ctime`, `atime`,
|
|
1093
|
-
`uid`, `gid`, `uname`, `gname`, `dev`, `ino`, and `nlink`. Note
|
|
1094
|
-
that `mtime` is still included, because this is necessary for other
|
|
1095
|
-
time-based operations. Additionally, `mode` is set to a "reasonable
|
|
1096
|
-
default" for most unix systems, based on a `umask` value of `0o22`.
|
|
1097
|
-
- `preservePaths` Allow absolute paths. By default, `/` is stripped
|
|
1098
|
-
from absolute paths.
|
|
1099
|
-
- `strict` Treat warnings as crash-worthy errors. Default false.
|
|
1100
|
-
- `onwarn` A function that will get called with `(code, message, data)` for
|
|
1101
|
-
any warnings encountered. (See "Warnings and Errors")
|
|
1102
|
-
- `noMtime` Set to true to omit writing `mtime` values for entries.
|
|
1103
|
-
Note that this prevents using other mtime-based features like
|
|
1104
|
-
`tar.update` or the `keepNewer` option with the resulting tar archive.
|
|
1105
|
-
|
|
1106
|
-
### class tar.Header
|
|
1107
|
-
|
|
1108
|
-
A class for reading and writing header blocks.
|
|
1109
|
-
|
|
1110
|
-
It has the following fields:
|
|
1111
|
-
|
|
1112
|
-
- `nullBlock` True if decoding a block which is entirely composed of
|
|
1113
|
-
`0x00` null bytes. (Useful because tar files are terminated by
|
|
1114
|
-
at least 2 null blocks.)
|
|
1115
|
-
- `cksumValid` True if the checksum in the header is valid, false
|
|
1116
|
-
otherwise.
|
|
1117
|
-
- `needPax` True if the values, as encoded, will require a Pax
|
|
1118
|
-
extended header.
|
|
1119
|
-
- `path` The path of the entry.
|
|
1120
|
-
- `mode` The 4 lowest-order octal digits of the file mode. That is,
|
|
1121
|
-
read/write/execute permissions for world, group, and owner, and the
|
|
1122
|
-
setuid, setgid, and sticky bits.
|
|
1123
|
-
- `uid` Numeric user id of the file owner
|
|
1124
|
-
- `gid` Numeric group id of the file owner
|
|
1125
|
-
- `size` Size of the file in bytes
|
|
1126
|
-
- `mtime` Modified time of the file
|
|
1127
|
-
- `cksum` The checksum of the header. This is generated by adding all
|
|
1128
|
-
the bytes of the header block, treating the checksum field itself as
|
|
1129
|
-
all ascii space characters (that is, `0x20`).
|
|
1130
|
-
- `type` The human-readable name of the type of entry this represents,
|
|
1131
|
-
or the alphanumeric key if unknown.
|
|
1132
|
-
- `typeKey` The alphanumeric key for the type of entry this header
|
|
1133
|
-
represents.
|
|
1134
|
-
- `linkpath` The target of Link and SymbolicLink entries.
|
|
1135
|
-
- `uname` Human-readable user name of the file owner
|
|
1136
|
-
- `gname` Human-readable group name of the file owner
|
|
1137
|
-
- `devmaj` The major portion of the device number. Always `0` for
|
|
1138
|
-
files, directories, and links.
|
|
1139
|
-
- `devmin` The minor portion of the device number. Always `0` for
|
|
1140
|
-
files, directories, and links.
|
|
1141
|
-
- `atime` File access time.
|
|
1142
|
-
- `ctime` File change time.
|
|
1143
|
-
|
|
1144
|
-
#### constructor(data, [offset=0])
|
|
1145
|
-
|
|
1146
|
-
`data` is optional. It is either a Buffer that should be interpreted
|
|
1147
|
-
as a tar Header starting at the specified offset and continuing for
|
|
1148
|
-
512 bytes, or a data object of keys and values to set on the header
|
|
1149
|
-
object, and eventually encode as a tar Header.
|
|
998
|
+
### `list(options, files)`
|
|
1150
999
|
|
|
1151
|
-
|
|
1000
|
+
Call `onEntry` with each entry of an archive. If `files` is set, only
|
|
1001
|
+
with the entries that it lists, and the entries in the directories
|
|
1002
|
+
that it lists. The result is a `Promise<void>`.
|
|
1152
1003
|
|
|
1153
|
-
|
|
1004
|
+
Options:
|
|
1154
1005
|
|
|
1155
|
-
|
|
1006
|
+
- `file` The path of the archive to read.
|
|
1007
|
+
- `input` The archive data. Set `input` or `file`.
|
|
1008
|
+
- `onEntry` A function that gets each `ReadEntry`. A promise is
|
|
1009
|
+
awaited.
|
|
1010
|
+
- `filter` A function `(path, entry)`. If it returns false, `onEntry`
|
|
1011
|
+
does not get the entry.
|
|
1012
|
+
- `maxReadSize` The most bytes to read from the archive file at one
|
|
1013
|
+
time. Default 16 MiB. A positive integer, or `Infinity`. 0 gives the
|
|
1014
|
+
default. Each other value is a `TypeError`.
|
|
1015
|
+
- `maxMetaEntrySize`, `maxDecompressionRatio`, `codecs` The options of
|
|
1016
|
+
[`entries()`](#entriesinput-options).
|
|
1017
|
+
- `strict`, `onwarn`, `fs`, `platform` The common options.
|
|
1156
1018
|
|
|
1157
|
-
|
|
1019
|
+
```js
|
|
1020
|
+
import { listSync } from 'tar'
|
|
1158
1021
|
|
|
1159
|
-
|
|
1022
|
+
const paths = []
|
|
1023
|
+
listSync(
|
|
1024
|
+
{ file: 'archive.tgz', onEntry: entry => paths.push(entry.path) },
|
|
1025
|
+
['src'],
|
|
1026
|
+
)
|
|
1027
|
+
console.log(paths)
|
|
1028
|
+
```
|
|
1160
1029
|
|
|
1161
|
-
|
|
1030
|
+
A path in `files` matches an entry if it is the path of the entry, or
|
|
1031
|
+
the path of one of its parent directories. Trailing slashes are
|
|
1032
|
+
ignored.
|
|
1162
1033
|
|
|
1163
|
-
|
|
1034
|
+
A path in `files` that no entry matches is a `TAR_ENTRY_ERROR` warning
|
|
1035
|
+
when the archive ends. The message is `<path>: not found in archive`,
|
|
1036
|
+
and `data.path` is the path. An entry that `filter` refuses still
|
|
1037
|
+
matches its path. With `strict`, the warning is an error, after
|
|
1038
|
+
`onEntry` got the entries that match.
|
|
1164
1039
|
|
|
1165
|
-
|
|
1166
|
-
|
|
1040
|
+
### `replace(options, files)`
|
|
1041
|
+
|
|
1042
|
+
Add `files` to the end of an archive file. The archive must not be
|
|
1043
|
+
compressed. If the file does not exist, it is made. The result is a
|
|
1044
|
+
`Promise<void>`.
|
|
1167
1045
|
|
|
1168
|
-
|
|
1046
|
+
The new entries go after the last complete entry of the archive. If a
|
|
1047
|
+
path is in the archive two times, a reader gets the old entry and then
|
|
1048
|
+
the new entry. An extraction writes the new entry last.
|
|
1169
1049
|
|
|
1170
|
-
|
|
1171
|
-
header entry.
|
|
1050
|
+
Options:
|
|
1172
1051
|
|
|
1173
|
-
|
|
1174
|
-
|
|
1052
|
+
- `file` The path of the archive. Required.
|
|
1053
|
+
- `mtimeCache` A `Map`. The command sets the path and the `mtime` of
|
|
1054
|
+
each entry that the archive has.
|
|
1055
|
+
- All the options of `create()`, but not `gzip`, `brotli`, `zstd`, or
|
|
1056
|
+
`compress`.
|
|
1175
1057
|
|
|
1176
|
-
|
|
1177
|
-
if it is for a single entry.
|
|
1178
|
-
- `atime`
|
|
1179
|
-
- `charset`
|
|
1180
|
-
- `comment`
|
|
1181
|
-
- `ctime`
|
|
1182
|
-
- `gid`
|
|
1183
|
-
- `gname`
|
|
1184
|
-
- `linkpath`
|
|
1185
|
-
- `mtime`
|
|
1186
|
-
- `path`
|
|
1187
|
-
- `size`
|
|
1188
|
-
- `uid`
|
|
1189
|
-
- `uname`
|
|
1190
|
-
- `dev`
|
|
1191
|
-
- `ino`
|
|
1192
|
-
- `nlink`
|
|
1058
|
+
### `delete(options, files)`
|
|
1193
1059
|
|
|
1194
|
-
|
|
1060
|
+
Remove entries from an archive file. An entry is removed if `files`
|
|
1061
|
+
has its path, or the path of one of its parent directories. Each
|
|
1062
|
+
entry with that path is removed. The result is a `Promise<void>`.
|
|
1195
1063
|
|
|
1196
|
-
|
|
1197
|
-
to
|
|
1064
|
+
The command writes the entries that stay to a new file next to the
|
|
1065
|
+
archive, and then renames the new file to the path of the archive.
|
|
1066
|
+
If the command fails, the archive does not change.
|
|
1198
1067
|
|
|
1199
|
-
|
|
1068
|
+
Options:
|
|
1200
1069
|
|
|
1201
|
-
|
|
1202
|
-
|
|
1070
|
+
- `file` The path of the archive. Required.
|
|
1071
|
+
- `filter` A function `(path, entry)`. Return true to remove the
|
|
1072
|
+
entry. With `files`, an entry is removed only if `files` matches it
|
|
1073
|
+
and the filter returns true. With no `files`, the filter is
|
|
1074
|
+
required.
|
|
1075
|
+
- `codecs`, `maxDecompressionRatio`, `maxReadSize`, and the other
|
|
1076
|
+
options of `entries()`.
|
|
1203
1077
|
|
|
1204
|
-
|
|
1078
|
+
Notes:
|
|
1205
1079
|
|
|
1206
|
-
|
|
1207
|
-
|
|
1080
|
+
- A path in `files` that no entry matches is a `TAR_ENTRY_ERROR`
|
|
1081
|
+
warning, `<path>: not found in archive`. With `strict`, it is an
|
|
1082
|
+
error, and the archive does not change.
|
|
1083
|
+
- The entries that stay have the same content, the same order, and
|
|
1084
|
+
the same fields. The headers are written again, so their bytes can
|
|
1085
|
+
differ: for example, a GNU long name header becomes a PAX header
|
|
1086
|
+
with the same path.
|
|
1087
|
+
- The new file has the compression of the archive, with the default
|
|
1088
|
+
options of the codec, and the permission bits of the archive.
|
|
1089
|
+
- The disk must have space for the two files until the rename. A
|
|
1090
|
+
hard link to the old archive file keeps the old content.
|
|
1208
1091
|
|
|
1209
|
-
|
|
1092
|
+
### `update(options, files)`
|
|
1210
1093
|
|
|
1211
|
-
|
|
1212
|
-
|
|
1094
|
+
The same as `replace()`, with the same options. A path is added only
|
|
1095
|
+
if its `mtime` is later than the `mtime` of the last entry of the
|
|
1096
|
+
archive that has that path.
|
|
1213
1097
|
|
|
1214
|
-
|
|
1098
|
+
A directory is always added again, and each path in it is checked.
|
|
1099
|
+
So a changed file in a directory is added, also when the `mtime` of
|
|
1100
|
+
the directory did not change.
|
|
1215
1101
|
|
|
1216
|
-
|
|
1217
|
-
|
|
1102
|
+
A basic tar header holds the `mtime` in whole seconds. For such an
|
|
1103
|
+
entry, the file is newer only if its `mtime` is in a later second. A
|
|
1104
|
+
change in the same second as the entry is not seen.
|
|
1218
1105
|
|
|
1219
|
-
|
|
1220
|
-
object. (This is necessary because multiple metadata entries can
|
|
1221
|
-
occur in sequence.)
|
|
1106
|
+
### `entries(input, options)`
|
|
1222
1107
|
|
|
1223
|
-
|
|
1108
|
+
Read the entries of an archive. The result is an async iterator of
|
|
1109
|
+
`ReadEntry` objects. `entriesSync()` returns an iterator of
|
|
1110
|
+
`ReadEntrySync` objects. `tar/core` does not have `entriesSync()`.
|
|
1111
|
+
|
|
1112
|
+
Entries that are not an object of a file system, such as PAX headers,
|
|
1113
|
+
are not given.
|
|
1114
|
+
|
|
1115
|
+
Options:
|
|
1116
|
+
|
|
1117
|
+
- `filter` A function `(path, entry)`. If it returns false, the entry
|
|
1118
|
+
is skipped.
|
|
1119
|
+
- `maxMetaEntrySize` The largest PAX header or GNU long name entry to
|
|
1120
|
+
read, in bytes. A larger one is skipped. Default 1 MiB.
|
|
1121
|
+
- `maxDecompressionRatio` Stop with an error when the decompressed
|
|
1122
|
+
size is more than this many times the compressed size. Default 1000.
|
|
1123
|
+
`Infinity` removes the limit.
|
|
1124
|
+
- `codecs` More compression formats. See [Compression](#compression).
|
|
1125
|
+
- `file` The name of the archive. Warnings have it, and the reader
|
|
1126
|
+
uses it to find a format by the file name.
|
|
1127
|
+
- `strict`, `onwarn`, `platform` The common options.
|
|
1128
|
+
|
|
1129
|
+
## Compression
|
|
1130
|
+
|
|
1131
|
+
The readers find the format of the input. You set no option to read a
|
|
1132
|
+
compressed archive.
|
|
1133
|
+
|
|
1134
|
+
To write a compressed archive, set `gzip`, `brotli`, or `zstd` in the
|
|
1135
|
+
options of `create()`. To write with a codec of your own, set
|
|
1136
|
+
`compress`. You can set only one of the four.
|
|
1137
|
+
|
|
1138
|
+
| Format | `tar` | `tar/core` |
|
|
1139
|
+
| ------ | ------------------------------------------------------ | -------------------------------------------------------------------------------- |
|
|
1140
|
+
| gzip | Yes, with `node:zlib`. Options of `zlib.createGzip()`. | Yes, with `CompressionStream` and `DecompressionStream`. Async only. No options. |
|
|
1141
|
+
| brotli | Yes, if `node:zlib` of the runtime has it | No |
|
|
1142
|
+
| zstd | Yes, if `node:zlib` of the runtime has it | No |
|
|
1143
|
+
|
|
1144
|
+
```js
|
|
1145
|
+
import { create, list } from 'tar'
|
|
1146
|
+
|
|
1147
|
+
await create({ file: 'src.tar.zst', zstd: true }, ['src'])
|
|
1148
|
+
await create({ file: 'src.tar.br', brotli: true }, ['src'])
|
|
1149
|
+
await create({ file: 'src.tgz', gzip: { level: 9 } }, ['src'])
|
|
1150
|
+
|
|
1151
|
+
for (const file of ['src.tar.zst', 'src.tar.br', 'src.tgz']) {
|
|
1152
|
+
await list({
|
|
1153
|
+
file,
|
|
1154
|
+
onEntry: entry => console.log(file, entry.path),
|
|
1155
|
+
})
|
|
1156
|
+
}
|
|
1157
|
+
```
|
|
1158
|
+
|
|
1159
|
+
### How a format is detected
|
|
1160
|
+
|
|
1161
|
+
1. The reader reads the first 512 bytes of the input, or more if a
|
|
1162
|
+
codec needs more.
|
|
1163
|
+
2. It uses the first codec whose `detect()` function returns true.
|
|
1164
|
+
Gzip data starts with the bytes `1f 8b`. Zstd data starts with the
|
|
1165
|
+
bytes `28 b5 2f fd`.
|
|
1166
|
+
3. If no codec detects the data, and the data does not start with a
|
|
1167
|
+
tar header, the reader uses the first codec that has an ending of
|
|
1168
|
+
the `file` option in its `extensions`.
|
|
1169
|
+
4. If there is no such codec, the input is read as a tar archive.
|
|
1170
|
+
|
|
1171
|
+
Brotli data has no fixed first bytes. The reader uses brotli only if
|
|
1172
|
+
the name of the archive ends in `.tar.br` or `.tbr`. To read brotli
|
|
1173
|
+
from `input`, also set `file`.
|
|
1174
|
+
|
|
1175
|
+
```js
|
|
1176
|
+
import { readFile } from 'node:fs/promises'
|
|
1177
|
+
import { create, entries } from 'tar'
|
|
1178
|
+
|
|
1179
|
+
await create({ file: 'src.tar.br', brotli: true }, ['src'])
|
|
1180
|
+
const data = await readFile('src.tar.br')
|
|
1181
|
+
|
|
1182
|
+
for await (const entry of entries(data, { file: 'src.tar.br' })) {
|
|
1183
|
+
console.log(entry.path)
|
|
1184
|
+
}
|
|
1185
|
+
```
|
|
1186
|
+
|
|
1187
|
+
`replace()` and `update()` refuse an archive that a codec detects, and
|
|
1188
|
+
a `file` name that ends in `.br` or `.tbr`.
|
|
1189
|
+
|
|
1190
|
+
### `maxDecompressionRatio`
|
|
1191
|
+
|
|
1192
|
+
A small compressed file can hold a very large quantity of data. The
|
|
1193
|
+
readers count the compressed bytes and the decompressed bytes. When
|
|
1194
|
+
the decompressed count is more than `maxDecompressionRatio` times the
|
|
1195
|
+
compressed count, the reader stops with a `TAR_ABORT` error. The
|
|
1196
|
+
default is 1000.
|
|
1197
|
+
|
|
1198
|
+
The async codecs of `tar` give at most 64 KiB of output at a time, and
|
|
1199
|
+
only when the reader asks. Thus the reader stops soon after the limit.
|
|
1200
|
+
The sync codecs of `tar` give the limit to `node:zlib` as
|
|
1201
|
+
`maxOutputLength`.
|
|
1202
|
+
|
|
1203
|
+
### The `Codec` type
|
|
1204
|
+
|
|
1205
|
+
A codec is one compression format. Give codecs in the `codecs` option,
|
|
1206
|
+
to add a format or to replace the built-in gzip. Your codecs are tried
|
|
1207
|
+
before the built-in codecs.
|
|
1208
|
+
|
|
1209
|
+
```ts
|
|
1210
|
+
type Codec = {
|
|
1211
|
+
name: string
|
|
1212
|
+
/** The number of bytes that `detect` needs. */
|
|
1213
|
+
headLength: number
|
|
1214
|
+
/** True if the input is in this format. */
|
|
1215
|
+
detect(head: Uint8Array): boolean
|
|
1216
|
+
/** File name endings, for a format that `detect` cannot identify. */
|
|
1217
|
+
extensions?: string[]
|
|
1218
|
+
/** Make a stream that decompresses. */
|
|
1219
|
+
decompress(): CodecStream
|
|
1220
|
+
/** Make a stream that compresses. */
|
|
1221
|
+
compress?(options?: Record<string, unknown>): CodecStream
|
|
1222
|
+
/** Decompress all of `data`. The sync readers need it. */
|
|
1223
|
+
decompressSync?(
|
|
1224
|
+
data: Uint8Array,
|
|
1225
|
+
limit: { maxOutputLength: number },
|
|
1226
|
+
): Uint8Array
|
|
1227
|
+
/** Compress all of `data`. The sync commands need it. */
|
|
1228
|
+
compressSync?(
|
|
1229
|
+
data: Uint8Array,
|
|
1230
|
+
options?: Record<string, unknown>,
|
|
1231
|
+
): Uint8Array
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
type CodecStream = {
|
|
1235
|
+
readable: ReadableStream<Uint8Array>
|
|
1236
|
+
writable: WritableStream<Uint8Array>
|
|
1237
|
+
}
|
|
1238
|
+
```
|
|
1239
|
+
|
|
1240
|
+
A `TransformStream` is a `CodecStream`.
|
|
1241
|
+
|
|
1242
|
+
To compress with a codec, give it in the `compress` option of
|
|
1243
|
+
`create()`. The codec does not need to be in `codecs`. To give options
|
|
1244
|
+
to the `compress()` function of the codec, set `compress` to
|
|
1245
|
+
`[codec, options]`. With `portable`, a codec that has the name `gzip`
|
|
1246
|
+
also gets `portable: true` in its options.
|
|
1247
|
+
|
|
1248
|
+
```js
|
|
1249
|
+
import { create, gzip } from 'tar'
|
|
1250
|
+
|
|
1251
|
+
// The built-in gzip of the core, not the gzip of `node:zlib`.
|
|
1252
|
+
await create({ file: 'core.tgz', compress: gzip }, ['src'])
|
|
1253
|
+
```
|
|
1224
1254
|
|
|
1225
|
-
|
|
1255
|
+
The options `gzip`, `brotli`, and `zstd` select the first codec of
|
|
1256
|
+
`codecs` that has that name, then the built-in codec. An option with
|
|
1257
|
+
the name of another codec selects nothing.
|
|
1226
1258
|
|
|
1227
|
-
|
|
1259
|
+
`decompressSync()` must throw before it makes more than
|
|
1260
|
+
`limit.maxOutputLength` bytes. That number is `Infinity` when the
|
|
1261
|
+
caller sets `maxDecompressionRatio: Infinity`.
|
|
1228
1262
|
|
|
1229
|
-
|
|
1263
|
+
A sync command with compression holds the complete archive in memory.
|
|
1264
|
+
`createSync()` compresses the archive in one call. The sync readers
|
|
1265
|
+
decompress all of the input in one call.
|
|
1230
1266
|
|
|
1231
|
-
|
|
1267
|
+
The exports for codecs are:
|
|
1268
|
+
|
|
1269
|
+
- `gzip` The built-in codec of the core.
|
|
1270
|
+
- `zlibCodecs(zlib)` Makes the gzip, brotli, and zstd codecs of a
|
|
1271
|
+
`node:zlib` module. Not in `tar/core`.
|
|
1272
|
+
- `codecStream(stream)` Makes a `CodecStream` from a `node:zlib`
|
|
1273
|
+
stream, or from another Node.js `Transform` stream. Not in
|
|
1274
|
+
`tar/core`.
|
|
1275
|
+
|
|
1276
|
+
### Recipe: bzip2
|
|
1277
|
+
|
|
1278
|
+
This recipe adds bzip2 with the package
|
|
1279
|
+
[`node-bzip2`](https://www.npmjs.com/package/node-bzip2). That package
|
|
1280
|
+
is a native addon. It is not a dependency of `tar`.
|
|
1281
|
+
|
|
1282
|
+
`node-bzip2` works on complete buffers. It has no stream interface and
|
|
1283
|
+
no output limit. Thus this codec has two limits:
|
|
1284
|
+
|
|
1285
|
+
- The async form collects all of the input in memory before it
|
|
1286
|
+
compresses or decompresses.
|
|
1287
|
+
- All of the data is decompressed before the ratio limit is checked.
|
|
1288
|
+
`maxDecompressionRatio` cannot stop a large output before it is in
|
|
1289
|
+
memory. Do not use this codec for input that you do not trust,
|
|
1290
|
+
unless you limit the size of the input and you accept the memory
|
|
1291
|
+
use.
|
|
1292
|
+
|
|
1293
|
+
```js
|
|
1294
|
+
import bzip2 from 'node-bzip2'
|
|
1295
|
+
import { mkdir } from 'node:fs/promises'
|
|
1296
|
+
import { create, extract, list } from 'tar'
|
|
1297
|
+
|
|
1298
|
+
// A stream that collects all of its input. At the end it gives the
|
|
1299
|
+
// result of `fn` as one chunk.
|
|
1300
|
+
const whole = fn => {
|
|
1301
|
+
const parts = []
|
|
1302
|
+
return new TransformStream({
|
|
1303
|
+
transform: chunk => {
|
|
1304
|
+
parts.push(chunk)
|
|
1305
|
+
},
|
|
1306
|
+
flush: async controller => {
|
|
1307
|
+
controller.enqueue(await fn(Buffer.concat(parts)))
|
|
1308
|
+
},
|
|
1309
|
+
})
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
const bz2 = {
|
|
1313
|
+
name: 'bzip2',
|
|
1314
|
+
// Bzip2 data starts with the characters "BZh".
|
|
1315
|
+
headLength: 3,
|
|
1316
|
+
detect: head => head[0] === 0x42 && head[1] === 0x5a && head[2] === 0x68,
|
|
1317
|
+
decompress: () => whole(data => bzip2.decompressAsync(data)),
|
|
1318
|
+
compress: options => whole(data => bzip2.compressAsync(data, options)),
|
|
1319
|
+
decompressSync: (data, { maxOutputLength }) => {
|
|
1320
|
+
// The limit is checked after the data is decompressed.
|
|
1321
|
+
const out = bzip2.decompress(data)
|
|
1322
|
+
if (out.length > maxOutputLength) {
|
|
1323
|
+
throw new Error('bzip2: the output is larger than the limit')
|
|
1324
|
+
}
|
|
1325
|
+
return out
|
|
1326
|
+
},
|
|
1327
|
+
compressSync: (data, options) => bzip2.compress(data, options),
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
// The second item is the options of the `compress` functions of
|
|
1331
|
+
// node-bzip2.
|
|
1332
|
+
await create({ file: 'src.tar.bz2', compress: [bz2, { level: 9 }] }, [
|
|
1333
|
+
'src',
|
|
1334
|
+
])
|
|
1335
|
+
|
|
1336
|
+
await list({
|
|
1337
|
+
file: 'src.tar.bz2',
|
|
1338
|
+
codecs: [bz2],
|
|
1339
|
+
onEntry: entry => console.log(entry.path),
|
|
1340
|
+
})
|
|
1341
|
+
|
|
1342
|
+
await mkdir('out', { recursive: true })
|
|
1343
|
+
await extract({ file: 'src.tar.bz2', codecs: [bz2], cwd: 'out' })
|
|
1344
|
+
```
|
|
1345
|
+
|
|
1346
|
+
## Lower-level classes
|
|
1347
|
+
|
|
1348
|
+
The commands use these classes. They are exported from `tar` and from
|
|
1349
|
+
`tar/core`. The sync classes are exported only from `tar`.
|
|
1350
|
+
|
|
1351
|
+
| Export | What it is |
|
|
1352
|
+
| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
1353
|
+
| `Parser` | A tar parser that you pull. It does not decompress, and it reads no input by itself. |
|
|
1354
|
+
| `TarReader`, `TarReaderSync` | The iterators that `entries()` and `entriesSync()` return. They add decompression and the entry bodies to a `Parser`. |
|
|
1355
|
+
| `EntryCursor` | Gets entries and the parts of their bodies from a `Parser`. |
|
|
1356
|
+
| `EntryData` | The metadata of an entry. The base class of `ReadEntry` and `ReadEntrySync`. |
|
|
1357
|
+
| `Pack`, `PackSync` | Make an archive. `add()` paths and entries, then `end()`. A `Pack` is the async iterator of the bytes of the archive. A `PackSync` is the iterator. |
|
|
1358
|
+
| `PackBase`, `PackJob` | The parts that `Pack` and `PackSync` share, and a path in the queue of a pack. |
|
|
1359
|
+
| `Unpack`, `UnpackSync` | Extract an archive. `new Unpack(options).extract(input)` returns a promise. |
|
|
1360
|
+
| `UnpackBase` | The checks of each entry, with no use of the file system. |
|
|
1361
|
+
| `Header` | Encodes and decodes one 512 byte header block. |
|
|
1362
|
+
| `Pax` | Encodes and parses a PAX extended header. |
|
|
1363
|
+
| `types` | `types.name` is a `Map` from a type code to a type name. `types.code` is the opposite `Map`. |
|
|
1364
|
+
| `AppendScan` | Finds the place in an archive file where `replace()` adds entries. |
|
|
1365
|
+
| `PathReservations` | Keeps the entries that use the same paths in order during an async extraction. |
|
|
1366
|
+
| `CwdError`, `SymlinkError` | The error classes of an extraction directory that is not usable, and of a path that goes through a symbolic link. |
|
|
1367
|
+
|
|
1368
|
+
`Pack`, `Unpack`, and `Parser` are not streams. They do not have
|
|
1369
|
+
`write()`, `pipe()`, or events.
|
|
1370
|
+
|
|
1371
|
+
### The `Parser` state machine
|
|
1372
|
+
|
|
1373
|
+
Give the parser input with `push()`. Then call `next()` for each event
|
|
1374
|
+
until it returns `undefined`. Then give it more input, or call `end()`
|
|
1375
|
+
and call `next()` again.
|
|
1376
|
+
|
|
1377
|
+
The events are:
|
|
1378
|
+
|
|
1379
|
+
- `{ type: 'entry', entry }` An entry. If `entry.ignore` is true, no
|
|
1380
|
+
other event follows for this entry.
|
|
1381
|
+
- `{ type: 'body', data }` A part of the body of the entry. `data` is
|
|
1382
|
+
a view into the input.
|
|
1383
|
+
- `{ type: 'entryEnd' }` The end of the body of the entry.
|
|
1384
|
+
- `{ type: 'end' }` The end of the archive.
|
|
1385
|
+
|
|
1386
|
+
```js
|
|
1387
|
+
import { Pack, Parser, WriteEntry } from 'tar'
|
|
1388
|
+
|
|
1389
|
+
// Some archive data, in chunks.
|
|
1390
|
+
const pack = new Pack()
|
|
1391
|
+
.add(new WriteEntry({ path: 'a.txt' }, 'the content of a'))
|
|
1392
|
+
.add(new WriteEntry({ path: 'b.txt' }, 'b'))
|
|
1393
|
+
.end()
|
|
1394
|
+
|
|
1395
|
+
const parser = new Parser()
|
|
1396
|
+
const read = () => {
|
|
1397
|
+
for (let ev; (ev = parser.next()); ) {
|
|
1398
|
+
if (ev.type === 'entry') {
|
|
1399
|
+
console.log('entry', ev.entry.path, ev.entry.size)
|
|
1400
|
+
} else if (ev.type === 'body') {
|
|
1401
|
+
console.log(' body', ev.data.length)
|
|
1402
|
+
} else if (ev.type === 'entryEnd') {
|
|
1403
|
+
console.log(' end of entry')
|
|
1404
|
+
} else {
|
|
1405
|
+
console.log('end of archive')
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
for await (const chunk of pack) {
|
|
1411
|
+
parser.push(chunk)
|
|
1412
|
+
read()
|
|
1413
|
+
}
|
|
1414
|
+
parser.end()
|
|
1415
|
+
read()
|
|
1416
|
+
```
|
|
1417
|
+
|
|
1418
|
+
The options of a `Parser` are `filter`, `maxMetaEntrySize`,
|
|
1419
|
+
`platform`, `strict`, `onwarn`, and `file`. `parser.position` is the
|
|
1420
|
+
number of input bytes up to the end of the last complete entry.
|
|
1421
|
+
|
|
1422
|
+
## Warnings and errors
|
|
1423
|
+
|
|
1424
|
+
A warning is a problem that does not stop the command. In most cases
|
|
1425
|
+
it is about one entry. An error stops the command: an async command
|
|
1426
|
+
rejects its promise, a sync command throws, and a stream or an
|
|
1427
|
+
iterator fails at the next read.
|
|
1428
|
+
|
|
1429
|
+
`onwarn(code, message, data)` gets each warning:
|
|
1430
|
+
|
|
1431
|
+
- `code` One of the codes below.
|
|
1432
|
+
- `message` The text of the warning.
|
|
1433
|
+
- `data` An object with more data. If the cause is an `Error`, `data`
|
|
1434
|
+
is that error. It can have these fields:
|
|
1435
|
+
- `tarCode` The code below.
|
|
1436
|
+
- `code` The code of the file system or of the codec, if the cause
|
|
1437
|
+
has one. If not, the same as `tarCode`.
|
|
1438
|
+
- `file` The `file` option.
|
|
1439
|
+
- `cwd` The extraction directory.
|
|
1440
|
+
- `entry` The entry.
|
|
1441
|
+
- `header` The header, if there is no entry.
|
|
1442
|
+
- `recoverable` False if the problem is always an error.
|
|
1443
|
+
|
|
1444
|
+
An error that comes from a warning has the same fields.
|
|
1445
|
+
|
|
1446
|
+
```js
|
|
1447
|
+
import { list } from 'tar'
|
|
1448
|
+
|
|
1449
|
+
await list({
|
|
1450
|
+
// This is not an archive.
|
|
1451
|
+
input: new TextEncoder().encode('x'.repeat(1024)),
|
|
1452
|
+
onwarn: (code, message, data) => {
|
|
1453
|
+
console.log(code, message, data.tarCode)
|
|
1454
|
+
},
|
|
1455
|
+
})
|
|
1456
|
+
```
|
|
1457
|
+
|
|
1458
|
+
### Codes
|
|
1459
|
+
|
|
1460
|
+
- `TAR_ENTRY_INFO` The entry is changed, and then used. For example,
|
|
1461
|
+
the root is removed from an absolute path.
|
|
1462
|
+
- `TAR_ENTRY_INVALID` The header is not valid, and the entry is
|
|
1463
|
+
skipped: the checksum is wrong, the path is missing, a link has no
|
|
1464
|
+
`linkpath`, or an entry that is not a link has a `linkpath`.
|
|
1465
|
+
- `TAR_ENTRY_ERROR` The entry is valid, but it cannot be extracted or
|
|
1466
|
+
added. In an extraction: an error of the file system, a path with
|
|
1467
|
+
`..`, a path or a link target that goes out of the extraction
|
|
1468
|
+
directory, a path that goes through a symbolic link, or a path that
|
|
1469
|
+
is too deep or too long. In `create()`: a path that does not exist,
|
|
1470
|
+
a file or a directory that changed while it was read, or with
|
|
1471
|
+
`follow`, a directory that is in itself. In `extract()` and
|
|
1472
|
+
`list()`: a path in `files` that no entry of the archive matches.
|
|
1473
|
+
- `TAR_ENTRY_UNSUPPORTED` The type of the entry is not supported, and
|
|
1474
|
+
the entry is skipped. `extract()` reports devices and FIFOs.
|
|
1475
|
+
`create()` reports each path that is not a file, a directory, or a
|
|
1476
|
+
symbolic link, such as a socket. `data.path` is the path.
|
|
1477
|
+
- `TAR_BAD_ARCHIVE` The archive is damaged: the body of the last entry
|
|
1478
|
+
is cut off, no header is valid, or an entry comes after one block of
|
|
1479
|
+
zeros. An archive with no entries also gets this warning. This is a
|
|
1480
|
+
warning for `list()`, `entries()`, and `Parser`. It is always an
|
|
1481
|
+
error for `extract()`, because the extraction cannot be complete.
|
|
1482
|
+
- `TAR_ABORT` The data cannot be decompressed, or the decompression
|
|
1483
|
+
ratio is more than the limit. This is always an error.
|
|
1484
|
+
|
|
1485
|
+
If the input ends in the middle of a header block, there is no
|
|
1486
|
+
warning.
|
|
1487
|
+
|
|
1488
|
+
An error of the file system or of a codec keeps its `code`, such as
|
|
1489
|
+
`ENOENT`. Read `tarCode` to get the code of this package.
|
|
1490
|
+
|
|
1491
|
+
### `strict`
|
|
1492
|
+
|
|
1493
|
+
With `strict: true`, each warning is thrown as an error. This includes
|
|
1494
|
+
`TAR_ENTRY_INFO`.
|
|
1495
|
+
|
|
1496
|
+
In strict mode, `extract()` stops at the first error. The entries
|
|
1497
|
+
before it are extracted. The entries after it are not.
|
|
1498
|
+
|
|
1499
|
+
```js
|
|
1500
|
+
import { mkdir, readdir } from 'node:fs/promises'
|
|
1501
|
+
import { create, extract, WriteEntry } from 'tar'
|
|
1502
|
+
|
|
1503
|
+
// An archive with a path that goes out of the extraction directory.
|
|
1504
|
+
const archive = create({ preservePaths: true }, [
|
|
1505
|
+
new WriteEntry({ path: 'first.txt' }, '1'),
|
|
1506
|
+
new WriteEntry({ path: '../outside.txt' }, '2'),
|
|
1507
|
+
new WriteEntry({ path: 'last.txt' }, '3'),
|
|
1508
|
+
])
|
|
1509
|
+
const data = await new Response(archive).arrayBuffer()
|
|
1510
|
+
|
|
1511
|
+
// By default, the bad entry is a warning.
|
|
1512
|
+
await mkdir('loose')
|
|
1513
|
+
await extract({
|
|
1514
|
+
input: data,
|
|
1515
|
+
cwd: 'loose',
|
|
1516
|
+
onwarn: (code, message) => console.log(code, message),
|
|
1517
|
+
})
|
|
1518
|
+
console.log(await readdir('loose')) // first.txt, last.txt
|
|
1519
|
+
|
|
1520
|
+
// In strict mode, it is an error.
|
|
1521
|
+
await mkdir('strict')
|
|
1522
|
+
await extract({ input: data, cwd: 'strict', strict: true }).catch(er =>
|
|
1523
|
+
console.log(er.tarCode, er.message),
|
|
1524
|
+
)
|
|
1525
|
+
console.log(await readdir('strict')) // first.txt
|
|
1526
|
+
```
|
|
1527
|
+
|
|
1528
|
+
Some problems are an error also when `strict` is not set:
|
|
1529
|
+
|
|
1530
|
+
- The extraction directory does not exist or is not a directory
|
|
1531
|
+
(`CwdError`).
|
|
1532
|
+
- The archive cannot be read, or cannot be decompressed.
|
|
1533
|
+
- `TAR_BAD_ARCHIVE` in `extract()`.
|
|
1534
|
+
- `onEntry` or `filter` throws.
|
|
1535
|
+
- In `create()`, a path cannot be read, for a cause other than that
|
|
1536
|
+
it does not exist.
|
|
1537
|
+
|
|
1538
|
+
## Security
|
|
1539
|
+
|
|
1540
|
+
This package is used to extract archives that unknown persons publish,
|
|
1541
|
+
such as the packages of the npm registry. It has many protections
|
|
1542
|
+
against archives that try to write outside of the extraction
|
|
1543
|
+
directory, or to use too many resources.
|
|
1544
|
+
|
|
1545
|
+
`preservePaths: true` turns off the path protections. Do not set it
|
|
1546
|
+
for an archive that you do not trust.
|
|
1547
|
+
|
|
1548
|
+
### What `extract()` does
|
|
1549
|
+
|
|
1550
|
+
Paths:
|
|
1551
|
+
|
|
1552
|
+
- An entry with `..` in its path is refused, with a warning.
|
|
1553
|
+
- The root of an absolute path is removed. The entry is extracted
|
|
1554
|
+
below the extraction directory, with a `TAR_ENTRY_INFO` warning.
|
|
1555
|
+
- An entry cannot replace the extraction directory with a file or a
|
|
1556
|
+
link. A directory entry with the path `.` sets the times of the
|
|
1557
|
+
extraction directory, and its mode with `chmod`, and its owner with
|
|
1558
|
+
the owner options. An entry whose path is empty after `strip` does
|
|
1559
|
+
not.
|
|
1560
|
+
- No entry is extracted through a symbolic link. If a part of the path
|
|
1561
|
+
to an entry is a symbolic link, the entry is refused. With `unlink`,
|
|
1562
|
+
the link is removed and a directory is made.
|
|
1563
|
+
- Where the file system has `O_NOFOLLOW`, a file is opened with it.
|
|
1564
|
+
Then the open fails if the path is a symbolic link.
|
|
1565
|
+
- A path or a link target with more than `maxDepth` parts
|
|
1566
|
+
(default 1024) is refused. A path or a link target of more than
|
|
1567
|
+
32768 characters is refused.
|
|
1568
|
+
|
|
1569
|
+
Links:
|
|
1570
|
+
|
|
1571
|
+
- The target of a hard link is a path in the extraction directory. A
|
|
1572
|
+
target with `..` is refused. The root of an absolute target is
|
|
1573
|
+
removed.
|
|
1574
|
+
- The target of a symbolic link is relative to the directory of the
|
|
1575
|
+
link. A `..` is permitted only at the start of the target, as in
|
|
1576
|
+
`../sibling/file`. A target such as `a/../b` is refused. A target
|
|
1577
|
+
that resolves outside of the extraction directory is refused. The
|
|
1578
|
+
root of an absolute target is removed, with a `TAR_ENTRY_INFO`
|
|
1579
|
+
warning.
|
|
1580
|
+
- If a part of the path to the target of a link is a symbolic link on
|
|
1581
|
+
disk, the link is refused. Thus an archive cannot make a link to a
|
|
1582
|
+
symbolic link.
|
|
1583
|
+
|
|
1584
|
+
Types, owners, and modes:
|
|
1585
|
+
|
|
1586
|
+
- Character devices, block devices, and FIFOs are never made.
|
|
1587
|
+
- The setuid, setgid, and sticky bits of a mode in the archive are
|
|
1588
|
+
always removed. This is also true with `chmod`.
|
|
1589
|
+
- A file or a directory is made with the mode in the archive, and the
|
|
1590
|
+
umask of the process applies. A directory always gets the read,
|
|
1591
|
+
write, and search permissions of the owner.
|
|
1592
|
+
- `chmod: true` sets the mode of each directory with `fs.chmod()`, so
|
|
1593
|
+
that the umask does not change it. It does not change files.
|
|
1594
|
+
- `umask`, `dmode`, and `fmode` set only the default modes. They do
|
|
1595
|
+
not change an entry that has a mode.
|
|
1596
|
+
- If a file exists, its content is replaced and it keeps its mode. Set
|
|
1597
|
+
`unlink` to make a new file with the mode of the entry.
|
|
1598
|
+
- A file that has more than one link is not written in place. It is
|
|
1599
|
+
removed first, so that the other links do not change.
|
|
1600
|
+
- The owner of an entry is not changed, unless `preserveOwner`, `uid`
|
|
1601
|
+
and `gid`, or `forceChown` is set. The default of `preserveOwner` is
|
|
1602
|
+
true only when `processUid` is 0.
|
|
1603
|
+
|
|
1604
|
+
Order and resources:
|
|
1605
|
+
|
|
1606
|
+
- The async extraction writes some entries at the same time. Entries
|
|
1607
|
+
that use the same path, or a path and a directory above it, are
|
|
1608
|
+
written in the order of the archive. A link also waits for the entry
|
|
1609
|
+
of its target. Paths that differ only in case or in Unicode form are
|
|
1610
|
+
the same path for this rule.
|
|
1611
|
+
- The decompression ratio is limited. See
|
|
1612
|
+
[`maxDecompressionRatio`](#maxdecompressionratio).
|
|
1613
|
+
- A PAX header or a GNU long name entry of more than
|
|
1614
|
+
`maxMetaEntrySize` bytes (default 1 MiB) is skipped.
|
|
1615
|
+
- The first null block is the end of the archive, as it is for
|
|
1616
|
+
bsdtar. Data after it is dropped, with no warning. An entry after
|
|
1617
|
+
a single null block is not read.
|
|
1618
|
+
- The option objects are copied to an object with no prototype. A
|
|
1619
|
+
changed `Object.prototype` cannot set an option.
|
|
1620
|
+
|
|
1621
|
+
The parser reads PAX headers and long names in the same way as other
|
|
1622
|
+
tar programs. Thus an archive cannot show different entries to this
|
|
1623
|
+
package than to GNU tar or bsdtar. One difference remains: other
|
|
1624
|
+
programs stop at one block of zeros. `list()` and `entries()` read the
|
|
1625
|
+
entries after it, with a `TAR_BAD_ARCHIVE` warning. `extract()` stops
|
|
1626
|
+
there with an error, and extracts no entry after that block.
|
|
1627
|
+
|
|
1628
|
+
### What you must do
|
|
1629
|
+
|
|
1630
|
+
1. Do not extract an archive into a directory that an unknown person
|
|
1631
|
+
or process can write to. The extraction checks a path and then uses
|
|
1632
|
+
it. Between the check and the use, another process can replace a
|
|
1633
|
+
directory or a file with a symbolic link. Then a file is written
|
|
1634
|
+
outside of the extraction directory. This package cannot prevent
|
|
1635
|
+
that. A security report about extraction into a directory that an
|
|
1636
|
+
attacker controls will be closed.
|
|
1637
|
+
2. If the archive comes from a source that you do not trust, use a
|
|
1638
|
+
`filter` that refuses hard links and symbolic links. Most
|
|
1639
|
+
extraction vulnerabilities of tar programs use links.
|
|
1640
|
+
3. If the archive is compressed, also limit the size of the entries
|
|
1641
|
+
with a `filter`. A small archive can hold large files.
|
|
1642
|
+
`maxDecompressionRatio` limits the ratio, not the total size.
|
|
1643
|
+
4. Use the latest version. Old versions get no fixes.
|
|
1644
|
+
|
|
1645
|
+
```js
|
|
1646
|
+
import { mkdir } from 'node:fs/promises'
|
|
1647
|
+
import { extract } from 'tar'
|
|
1648
|
+
|
|
1649
|
+
const maxSize = 10 * 1024 * 1024
|
|
1650
|
+
|
|
1651
|
+
await mkdir('out', { recursive: true })
|
|
1652
|
+
await extract({
|
|
1653
|
+
file: 'archive.tgz',
|
|
1654
|
+
cwd: 'out',
|
|
1655
|
+
filter: (path, entry) =>
|
|
1656
|
+
entry.type !== 'Link' &&
|
|
1657
|
+
entry.type !== 'SymbolicLink' &&
|
|
1658
|
+
entry.size <= maxSize,
|
|
1659
|
+
})
|
|
1660
|
+
```
|
|
1661
|
+
|
|
1662
|
+
### Races that are out of scope
|
|
1663
|
+
|
|
1664
|
+
These need a local process that changes the file system during the
|
|
1665
|
+
command. They are not vulnerabilities of this package.
|
|
1666
|
+
|
|
1667
|
+
In `extract()`:
|
|
1668
|
+
|
|
1669
|
+
- A parent directory of an entry is replaced with a symbolic link
|
|
1670
|
+
after it is checked and before the entry is made. The extraction
|
|
1671
|
+
checks the parent directories again after it makes the entry. If
|
|
1672
|
+
one of them is a symbolic link then, the entry is a
|
|
1673
|
+
`TAR_ENTRY_ERROR` warning with a `SymlinkError`, and no content is
|
|
1674
|
+
written to a file. But the empty file, the directory, or the link
|
|
1675
|
+
can be made at the place that the symbolic link gives, and an
|
|
1676
|
+
object can be removed there. A process that puts the directory
|
|
1677
|
+
back before the second check is not found.
|
|
1678
|
+
- The path of a link target is changed after it is checked and before
|
|
1679
|
+
the link is made.
|
|
1680
|
+
- Where the file system has no `O_NOFOLLOW` (Windows, or an `fs`
|
|
1681
|
+
option with no `constants`), the path of a file is replaced with a
|
|
1682
|
+
symbolic link after it is checked and before it is opened.
|
|
1683
|
+
|
|
1684
|
+
The extraction sets the times and the owner of a file through the open
|
|
1685
|
+
file, not through its path. It sets the times and the owner of a
|
|
1686
|
+
directory with `lutimes` and `lchown`, which do not follow a symbolic
|
|
1687
|
+
link.
|
|
1688
|
+
|
|
1689
|
+
In `create()`:
|
|
1690
|
+
|
|
1691
|
+
- The pack calls `lstat` on a path, and then opens the file or reads
|
|
1692
|
+
the directory. If the path is a different object then (its device
|
|
1693
|
+
number or inode number is different), the pack skips it with a
|
|
1694
|
+
`TAR_ENTRY_ERROR` warning. Thus a file that is replaced with a
|
|
1695
|
+
symbolic link is not read through the link. This check is not done
|
|
1696
|
+
with `follow`.
|
|
1697
|
+
- If the pack finds that a file got shorter or longer after the
|
|
1698
|
+
`lstat`, it stops with an error. It does not find each change of
|
|
1699
|
+
size.
|
|
1700
|
+
- One race remains. If a parent directory of a path is replaced with a
|
|
1701
|
+
symbolic link before the `lstat` of the path, the pack reads the
|
|
1702
|
+
file that the link gives. Do not make an archive of a directory that
|
|
1703
|
+
an unknown person or process can write to, if the result goes to a
|
|
1704
|
+
person who must not get other files.
|
|
1705
|
+
|
|
1706
|
+
### Report a vulnerability
|
|
1707
|
+
|
|
1708
|
+
If this package does not do what this section says, report it with the
|
|
1709
|
+
GitHub Security Advisories system of the repository.
|
|
1710
|
+
|
|
1711
|
+
## Migration from version 7
|
|
1712
|
+
|
|
1713
|
+
Version 8 is a new API. The commands have the same names and most of
|
|
1714
|
+
the same options.
|
|
1715
|
+
|
|
1716
|
+
The import does not change. `tar` is the entry point for Node.js, as
|
|
1717
|
+
in version 7, and it has the same file system defaults. `tar/core` is
|
|
1718
|
+
new. It is for browsers and for each runtime that does not have the
|
|
1719
|
+
`node:` modules.
|
|
1720
|
+
|
|
1721
|
+
| Version 7 | Version 8 |
|
|
1722
|
+
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
1723
|
+
| Node.js 18 or later | Node.js 22 or later. `tar/core` also runs in a browser. |
|
|
1724
|
+
| `import { x } from 'tar'` | `import { x } from 'tar'`. The same entry point, with the same file system defaults. `tar/core` is a new entry point with no `node:` module. |
|
|
1725
|
+
| `tar.x(options, files, callback)` | No callbacks. `await extract(options, files)`. |
|
|
1726
|
+
| `sync: true` | `createSync`, `extractSync`, `listSync`, `replaceSync`, `updateSync`. |
|
|
1727
|
+
| `tar.c({}, files)` returns a Node.js stream | `create({}, files)` returns a web `ReadableStream`. `createSync()` returns an iterator of chunks. |
|
|
1728
|
+
| `stream.pipe(tar.x({ cwd }))`, `stream.pipe(tar.t())` | `extract({ input: stream, cwd })`, `list({ input: stream })`. The input is a web stream, an iterable of chunks, a `Blob`, or bytes. A Node.js `Readable` is an async iterable of chunks. |
|
|
1729
|
+
| `onReadEntry`, `onentry` | `onEntry`. The old names are aliases of it. A promise is awaited. |
|
|
1730
|
+
| `entry.on('data', ...)`, `entry.resume()` | `entry.body`, `bytes()`, `arrayBuffer()`, `text()`, `blob()`. You do not have to read or drain a body. |
|
|
1731
|
+
| `new Parser()` and `'entry'` events | `for await (const entry of entries(input))` |
|
|
1732
|
+
| `Buffer` | `Uint8Array`. `Header#block` and the result of `Pax#encode()` are a `Uint8Array`. |
|
|
1733
|
+
| Option aliases: `C`, `f`, `z`, `P`, `U`, `k`, `m`, `p`, `L`, `h`, `strip-components`, `stripComponents`, `keep-newer`, `keepNewer`, `keep-newer-files`, `keepNewerFiles`, `keep-existing`, `keepExisting`, `no-mtime` | The same aliases, in the commands. See [Option aliases](#option-aliases). |
|
|
1734
|
+
| `noChmod` | `chmod`. `noChmod: false` is `chmod: true`, if `chmod` is not set. |
|
|
1735
|
+
| `noResume`, `ondone`, `dirCache`, `linkCache`, `readdirCache`, `statCache` | Removed. |
|
|
1736
|
+
| `transform` returns a Node.js stream | `transform` returns a `TransformStream`, or an object with a `readable` and a `writable` web stream. For a Node.js `Transform` stream, return `codecStream(stream)`. In `extractSync()`, it returns a function that gets a chunk and returns a chunk. |
|
|
1737
|
+
| In strict mode, an `Unpack` stream emitted `'error'` and then extracted the entries after it | In strict mode, the extraction stops at the first error. |
|
|
1738
|
+
| `Pack`, `Unpack`, and `Parser` are Node.js streams | `Pack` is an async iterator of chunks. `Unpack` has an `extract(input)` method. `Parser` has `push()` and `next()`. |
|
|
1739
|
+
| `ReadEntry` and `WriteEntry` are Minipass streams | `ReadEntry` has the shape of a `Response`. `WriteEntry` is metadata and a body. |
|
|
1740
|
+
| `WriteEntrySync`, `WriteEntryTar` | Removed. `WriteEntry` is the one class. |
|
|
1741
|
+
| `PackSync`, `UnpackSync` | `PackSync`, `UnpackSync`, `TarReaderSync`, and `ReadEntrySync`. `tar/core` does not have the sync classes. |
|
|
1742
|
+
| Subpath exports: `tar/create`, `tar/extract`, `tar/list`, `tar/replace`, `tar/update`, `tar/pack`, `tar/unpack`, `tar/parse`, `tar/read-entry`, `tar/write-entry`, `tar/header`, `tar/pax`, `tar/types`, `tar/c`, `tar/x`, `tar/t`, `tar/r`, `tar/u` | Only `tar`, `tar/core`, `tar/raw`, and `tar/core/raw`. |
|
|
1743
|
+
| `brotli: true` or `zstd: true` to read an archive | Not needed. The readers detect the format. For brotli, they use the file name. `Codec` objects in the `codecs` option add formats. |
|
|
1744
|
+
| Dependencies: `minipass`, `minizlib`, `@isaacs/fs-minipass`, `chownr`, `yallist` | No dependencies. |
|
|
1745
|
+
|
|
1746
|
+
Version 7 code, and the same work in version 8:
|
|
1747
|
+
|
|
1748
|
+
```js
|
|
1749
|
+
import { createReadStream, mkdirSync } from 'node:fs'
|
|
1750
|
+
import { create, extract, listSync } from 'tar'
|
|
1751
|
+
|
|
1752
|
+
// tar.c({ f: 'demo.tgz', z: true, C: '.' }, ['src'], callback)
|
|
1753
|
+
await create({ file: 'demo.tgz', gzip: true, cwd: '.' }, ['src'])
|
|
1754
|
+
|
|
1755
|
+
// tar.t({ f: 'demo.tgz', sync: true, onReadEntry: e => ... })
|
|
1756
|
+
listSync({ file: 'demo.tgz', onEntry: e => console.log(e.path) })
|
|
1757
|
+
|
|
1758
|
+
// fs.createReadStream('demo.tgz').pipe(tar.x({ strip: 1, C: 'out' }))
|
|
1759
|
+
mkdirSync('out', { recursive: true })
|
|
1760
|
+
await extract({
|
|
1761
|
+
input: createReadStream('demo.tgz'),
|
|
1762
|
+
strip: 1,
|
|
1763
|
+
cwd: 'out',
|
|
1764
|
+
})
|
|
1765
|
+
```
|
|
1766
|
+
|
|
1767
|
+
A `transform` of version 7 returned a Node.js stream, such as a
|
|
1768
|
+
`Transform` of `node:stream` or a Minipass stream. In version 8 the
|
|
1769
|
+
content goes through web streams. To keep a Node.js `Transform`
|
|
1770
|
+
stream, give it to `codecStream()`. The result has the `readable` and
|
|
1771
|
+
`writable` streams that `transform` needs. A stream that is not a
|
|
1772
|
+
`Transform` of `node:stream`, such as a Minipass stream, is not
|
|
1773
|
+
tested with `codecStream()`. Write it again as a `TransformStream`.
|
|
1774
|
+
|
|
1775
|
+
```js
|
|
1776
|
+
import { mkdir } from 'node:fs/promises'
|
|
1777
|
+
import { Transform } from 'node:stream'
|
|
1778
|
+
import { extract, codecStream } from 'tar'
|
|
1779
|
+
|
|
1780
|
+
// The transform stream of version 7.
|
|
1781
|
+
const upper = () =>
|
|
1782
|
+
new Transform({
|
|
1783
|
+
transform: (chunk, encoding, done) =>
|
|
1784
|
+
done(null, String(chunk).toUpperCase()),
|
|
1785
|
+
})
|
|
1786
|
+
|
|
1787
|
+
await mkdir('upper', { recursive: true })
|
|
1788
|
+
await extract({
|
|
1789
|
+
file: 'archive.tgz',
|
|
1790
|
+
cwd: 'upper',
|
|
1791
|
+
transform: entry =>
|
|
1792
|
+
entry.path.endsWith('.md') ? codecStream(upper()) : undefined,
|
|
1793
|
+
})
|
|
1794
|
+
```
|
|
1232
1795
|
|
|
1233
|
-
|
|
1796
|
+
`Duplex.toWeb(stream)` of `node:stream` also gives such an object. In
|
|
1797
|
+
TypeScript it can need a type assertion, because the stream types of
|
|
1798
|
+
`node:stream/web` are not the types of the DOM library.
|