tar 7.5.23 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1705 -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 -37
- 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 +135 -89
- package/dist/commonjs/unpack.d.ts.map +1 -1
- package/dist/commonjs/unpack.js +760 -727
- 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 -36
- 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 +135 -89
- package/dist/esm/unpack.d.ts.map +1 -1
- package/dist/esm/unpack.js +756 -721
- 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/dist/esm/pack.js
CHANGED
|
@@ -1,502 +1,811 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import
|
|
10
|
-
import {
|
|
1
|
+
import { isBlob, isReadableStream, isUint8Array } from './bytes.js';
|
|
2
|
+
import { modeFix } from './mode-fix.js';
|
|
3
|
+
import * as winchars from './winchars.js';
|
|
4
|
+
import { gzip } from './codec.js';
|
|
5
|
+
import { concat } from './concat.js';
|
|
6
|
+
import { countOption, ownOptions, } from './options.js';
|
|
7
|
+
import { isAbsolute, relative, resolve } from './path.js';
|
|
8
|
+
import { stripAbsolutePath } from './strip-absolute-path.js';
|
|
9
|
+
import { toStream } from './to-stream.js';
|
|
10
|
+
import { warn } from './warn.js';
|
|
11
|
+
import { win32IsAbsolute, win32Resolve } from './win32-path.js';
|
|
12
|
+
import { WriteEntry } from './write-entry.js';
|
|
13
|
+
const MAX_READ = 16 * 1024 * 1024;
|
|
14
|
+
const noop = () => { };
|
|
15
|
+
const blockLength = (size) => 512 * Math.ceil(size / 512);
|
|
16
|
+
const slashes = (path) => path.replaceAll('\\', '/');
|
|
17
|
+
// The identity of a file: a key for a `Map`, and a value to compare.
|
|
18
|
+
// The key of two `bigint` values is the key of the same two numbers.
|
|
19
|
+
const idOf = (id) => `${id.dev}:${id.ino}`;
|
|
20
|
+
const BIG = { bigint: true };
|
|
21
|
+
// Node.js gives `dev` and `ino` as numbers. A value that is not a
|
|
22
|
+
// safe integer can be the value of more than one file. On NTFS, most
|
|
23
|
+
// `ino` values are not safe integers.
|
|
24
|
+
const lossy = ({ dev, ino }) => !Number.isSafeInteger(dev) || !Number.isSafeInteger(ino);
|
|
25
|
+
// True if the path is still the object of its stat. The numbers are
|
|
26
|
+
// enough here: one object always gives the same numbers, so numbers
|
|
27
|
+
// that are not exact give no false alarm, and need no second stat.
|
|
28
|
+
const same = (a, b) => a.dev === b.dev && a.ino === b.ino;
|
|
29
|
+
const eof = (message, path) => Object.assign(new Error(message), { path, syscall: 'read', code: 'EOF' });
|
|
30
|
+
// The file does not have the number of bytes that its stat gave.
|
|
31
|
+
const fewer = (path, size) => eof(`encountered unexpected EOF: ${path} has fewer than the ${size} bytes of its stat`, path);
|
|
32
|
+
const more = (path, size) => eof(`did not encounter expected EOF: ${path} has more than the ${size} bytes of its stat`, path);
|
|
33
|
+
const pathOf = (entry) => entry.absolute ?? entry.path;
|
|
34
|
+
const tooLong = (entry) => Object.assign(new Error('writing more data than expected'), {
|
|
35
|
+
path: pathOf(entry),
|
|
36
|
+
});
|
|
37
|
+
// The package has more than one copy of each class: the entry points
|
|
38
|
+
// `tar` and `tar/core` are separate bundles, and each has an ESM and
|
|
39
|
+
// a CommonJS build. `instanceof` is false for an entry of a different
|
|
40
|
+
// copy, so these checks use the shape of the item. A `PackJob` is
|
|
41
|
+
// always of this copy, because only the pack makes it.
|
|
42
|
+
// A source of entries is an iterable. An entry is not.
|
|
43
|
+
const isSource = (item) => Symbol.asyncIterator in item || Symbol.iterator in item;
|
|
44
|
+
// A `WriteEntry` can encode its header. An entry of an archive
|
|
45
|
+
// cannot.
|
|
46
|
+
const isWriteEntry = (item) => typeof item.encode === 'function';
|
|
47
|
+
/** A path in the queue of a pack. */
|
|
11
48
|
export class PackJob {
|
|
12
49
|
path;
|
|
13
50
|
absolute;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
piped = false;
|
|
51
|
+
/** The directories above it, when the pack follows symbolic links. */
|
|
52
|
+
up;
|
|
53
|
+
/** The target, when the pack writes the file as a hard link. */
|
|
54
|
+
linkpath;
|
|
55
|
+
/** Set when `Pack` starts to read the path. */
|
|
56
|
+
ready;
|
|
21
57
|
constructor(path, absolute) {
|
|
22
|
-
this.path = path
|
|
58
|
+
this.path = path;
|
|
23
59
|
this.absolute = absolute;
|
|
24
60
|
}
|
|
25
61
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const ENDED = Symbol('ended');
|
|
33
|
-
const QUEUE = Symbol('queue');
|
|
34
|
-
const PENDINGLINKS = Symbol('pendingLinks');
|
|
35
|
-
const CURRENT = Symbol('current');
|
|
36
|
-
const PROCESS = Symbol('process');
|
|
37
|
-
const PROCESSING = Symbol('processing');
|
|
38
|
-
const PROCESSJOB = Symbol('processJob');
|
|
39
|
-
const JOBS = Symbol('jobs');
|
|
40
|
-
const JOBDONE = Symbol('jobDone');
|
|
41
|
-
const ADDFSENTRY = Symbol('addFSEntry');
|
|
42
|
-
const ADDTARENTRY = Symbol('addTarEntry');
|
|
43
|
-
const STAT = Symbol('stat');
|
|
44
|
-
const READDIR = Symbol('readdir');
|
|
45
|
-
const ONREADDIR = Symbol('onreaddir');
|
|
46
|
-
const PIPE = Symbol('pipe');
|
|
47
|
-
const ENTRY = Symbol('entry');
|
|
48
|
-
const ENTRYOPT = Symbol('entryOpt');
|
|
49
|
-
const WRITEENTRYCLASS = Symbol('writeEntryClass');
|
|
50
|
-
const WRITE = Symbol('write');
|
|
51
|
-
const ONDRAIN = Symbol('ondrain');
|
|
52
|
-
import path from 'path';
|
|
53
|
-
import { normalizeWindowsPath } from './normalize-windows-path.js';
|
|
54
|
-
export class Pack extends Minipass {
|
|
55
|
-
sync = false;
|
|
62
|
+
/**
|
|
63
|
+
* The parts of `Pack` and `PackSync` that are the same: the options,
|
|
64
|
+
* the queue, and the rules that make an entry from a path or from an
|
|
65
|
+
* entry of another archive.
|
|
66
|
+
*/
|
|
67
|
+
export class PackBase {
|
|
56
68
|
opt;
|
|
69
|
+
fs;
|
|
70
|
+
/** The normalized form of `opt.cwd`. */
|
|
57
71
|
cwd;
|
|
72
|
+
win32;
|
|
58
73
|
maxReadSize;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
this.on('warn', opt.onwarn);
|
|
107
|
-
}
|
|
108
|
-
this.portable = !!opt.portable;
|
|
109
|
-
if (opt.gzip || opt.brotli || opt.zstd) {
|
|
110
|
-
if ((opt.gzip ? 1 : 0) + (opt.brotli ? 1 : 0) + (opt.zstd ? 1 : 0) >
|
|
111
|
-
1) {
|
|
112
|
-
throw new TypeError('gzip, brotli, zstd are mutually exclusive');
|
|
113
|
-
}
|
|
114
|
-
if (opt.gzip) {
|
|
115
|
-
if (typeof opt.gzip !== 'object') {
|
|
116
|
-
opt.gzip = {};
|
|
117
|
-
}
|
|
118
|
-
if (this.portable) {
|
|
119
|
-
opt.gzip.portable = true;
|
|
120
|
-
}
|
|
121
|
-
this.zip = new zlib.Gzip(opt.gzip);
|
|
122
|
-
}
|
|
123
|
-
if (opt.brotli) {
|
|
124
|
-
if (typeof opt.brotli !== 'object') {
|
|
125
|
-
opt.brotli = {};
|
|
126
|
-
}
|
|
127
|
-
this.zip = new zlib.BrotliCompress(opt.brotli);
|
|
128
|
-
}
|
|
129
|
-
if (opt.zstd) {
|
|
130
|
-
if (typeof opt.zstd !== 'object') {
|
|
131
|
-
opt.zstd = {};
|
|
132
|
-
}
|
|
133
|
-
this.zip = new zlib.ZstdCompress(opt.zstd);
|
|
74
|
+
/** The options of `WriteEntry.encode`. */
|
|
75
|
+
header;
|
|
76
|
+
/** The compression of the output. */
|
|
77
|
+
codec;
|
|
78
|
+
codecOptions;
|
|
79
|
+
/** True after `end()`. */
|
|
80
|
+
ended = false;
|
|
81
|
+
head;
|
|
82
|
+
tail;
|
|
83
|
+
wake;
|
|
84
|
+
// The first path of each file that has more than one link.
|
|
85
|
+
#links = new Map();
|
|
86
|
+
constructor(opt, compress) {
|
|
87
|
+
opt = this.opt = ownOptions(opt);
|
|
88
|
+
this.fs = opt.fs;
|
|
89
|
+
this.win32 = opt.platform === 'win32';
|
|
90
|
+
this.maxReadSize = countOption('maxReadSize', opt.maxReadSize, MAX_READ);
|
|
91
|
+
const { prefix, noPax } = opt;
|
|
92
|
+
this.header = {
|
|
93
|
+
prefix: prefix && this.win32 ? slashes(prefix) : prefix,
|
|
94
|
+
noPax,
|
|
95
|
+
};
|
|
96
|
+
const cwd = opt.cwd && (this.win32 ? slashes(opt.cwd) : opt.cwd);
|
|
97
|
+
// This code has no working directory to make a path absolute.
|
|
98
|
+
if (cwd && !(this.win32 ? win32IsAbsolute : isAbsolute)(cwd)) {
|
|
99
|
+
throw new TypeError('cwd must be an absolute path');
|
|
100
|
+
}
|
|
101
|
+
this.cwd = cwd && this.#resolve(cwd, '');
|
|
102
|
+
const set = opt;
|
|
103
|
+
const names = ['gzip', 'brotli', 'zstd', 'compress'].filter(name => set[name]);
|
|
104
|
+
if (names.length > 1) {
|
|
105
|
+
throw new TypeError(`${names.join(', ')} are mutually exclusive`);
|
|
106
|
+
}
|
|
107
|
+
const [name] = names;
|
|
108
|
+
if (name) {
|
|
109
|
+
const custom = opt.compress;
|
|
110
|
+
// The named options use a codec of `codecs`, or the gzip of
|
|
111
|
+
// the runtime. The `compress` option has its own codec.
|
|
112
|
+
const [codec, value] = !custom ?
|
|
113
|
+
[
|
|
114
|
+
[...(opt.codecs ?? []), gzip].find(c => c.name === name),
|
|
115
|
+
set[name],
|
|
116
|
+
]
|
|
117
|
+
: Array.isArray(custom) ? custom
|
|
118
|
+
: [custom];
|
|
119
|
+
if (!codec?.[compress]) {
|
|
120
|
+
throw new TypeError(`no codec named ${codec?.name ?? name} has a ${compress} function`);
|
|
134
121
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
this.on('resume', () => zip.resume());
|
|
122
|
+
this.codec = codec;
|
|
123
|
+
this.codecOptions = {
|
|
124
|
+
...(typeof value === 'object' ? value : undefined),
|
|
125
|
+
...(codec.name === 'gzip' && opt.portable ?
|
|
126
|
+
{ portable: true }
|
|
127
|
+
: undefined),
|
|
128
|
+
};
|
|
143
129
|
}
|
|
144
|
-
else {
|
|
145
|
-
this.on('drain', this[ONDRAIN]);
|
|
146
|
-
}
|
|
147
|
-
this.noDirRecurse = !!opt.noDirRecurse;
|
|
148
|
-
this.follow = !!opt.follow;
|
|
149
|
-
this.noMtime = !!opt.noMtime;
|
|
150
|
-
if (opt.mtime)
|
|
151
|
-
this.mtime = opt.mtime;
|
|
152
|
-
this.filter =
|
|
153
|
-
typeof opt.filter === 'function' ? opt.filter : () => true;
|
|
154
|
-
this[QUEUE] = new Yallist();
|
|
155
|
-
this[JOBS] = 0;
|
|
156
|
-
this.jobs = Number(opt.jobs) || 4;
|
|
157
|
-
this[PROCESSING] = false;
|
|
158
|
-
this[ENDED] = false;
|
|
159
|
-
}
|
|
160
|
-
[WRITE](chunk) {
|
|
161
|
-
return super.write(chunk);
|
|
162
|
-
}
|
|
163
|
-
add(path) {
|
|
164
|
-
this.write(path);
|
|
165
|
-
return this;
|
|
166
130
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
if (typeof path === 'function') {
|
|
170
|
-
cb = path;
|
|
171
|
-
path = undefined;
|
|
172
|
-
}
|
|
173
|
-
if (typeof encoding === 'function') {
|
|
174
|
-
cb = encoding;
|
|
175
|
-
encoding = undefined;
|
|
176
|
-
}
|
|
177
|
-
/* c8 ignore stop */
|
|
178
|
-
if (path) {
|
|
179
|
-
this.add(path);
|
|
180
|
-
}
|
|
181
|
-
this[ENDED] = true;
|
|
182
|
-
this[PROCESS]();
|
|
183
|
-
/* c8 ignore next */
|
|
184
|
-
if (cb)
|
|
185
|
-
cb();
|
|
186
|
-
return this;
|
|
131
|
+
#resolve(cwd, path) {
|
|
132
|
+
return this.win32 ? win32Resolve(cwd, path) : resolve(cwd, path);
|
|
187
133
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
if (typeof path === 'string') {
|
|
193
|
-
this[ADDFSENTRY](path);
|
|
134
|
+
/** The absolute form of a path that is relative to `cwd`. */
|
|
135
|
+
resolve(path) {
|
|
136
|
+
if (!this.cwd || !this.fs) {
|
|
137
|
+
throw new TypeError('a path needs the cwd and fs options');
|
|
194
138
|
}
|
|
139
|
+
return this.#resolve(this.cwd, this.win32 ? slashes(path) : path);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Put a path, an entry, or a source of entries at the end of the
|
|
143
|
+
* queue. A directory is added with all that is in it.
|
|
144
|
+
*/
|
|
145
|
+
add(item) {
|
|
146
|
+
if (this.ended)
|
|
147
|
+
throw new Error('write after end');
|
|
148
|
+
if (typeof item !== 'string')
|
|
149
|
+
this.push(item);
|
|
195
150
|
else {
|
|
196
|
-
this
|
|
151
|
+
const path = this.win32 ? slashes(item) : item;
|
|
152
|
+
this.push(new PackJob(path || './', this.resolve(path)));
|
|
197
153
|
}
|
|
198
|
-
return this
|
|
154
|
+
return this;
|
|
199
155
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
const job = new PackJob(p.path, absolute);
|
|
208
|
-
job.entry = new WriteEntryTar(p, this[ENTRYOPT](job));
|
|
209
|
-
job.entry.on('end', () => this[JOBDONE](job));
|
|
210
|
-
this[JOBS] += 1;
|
|
211
|
-
this[QUEUE].push(job);
|
|
212
|
-
}
|
|
213
|
-
this[PROCESS]();
|
|
214
|
-
}
|
|
215
|
-
[ADDFSENTRY](p) {
|
|
216
|
-
const absolute = normalizeWindowsPath(path.resolve(this.cwd, p));
|
|
217
|
-
this[QUEUE].push(new PackJob(p, absolute));
|
|
218
|
-
this[PROCESS]();
|
|
219
|
-
}
|
|
220
|
-
[STAT](job) {
|
|
221
|
-
job.pending = true;
|
|
222
|
-
this[JOBS] += 1;
|
|
223
|
-
const stat = this.follow ? 'stat' : 'lstat';
|
|
224
|
-
fs[stat](job.absolute, (er, stat) => {
|
|
225
|
-
job.pending = false;
|
|
226
|
-
this[JOBS] -= 1;
|
|
227
|
-
if (er) {
|
|
228
|
-
this.emit('error', er);
|
|
229
|
-
}
|
|
230
|
-
else {
|
|
231
|
-
this[ONSTAT](job, stat);
|
|
232
|
-
}
|
|
233
|
-
});
|
|
156
|
+
/** Add no more. The archive ends after the last item of the queue. */
|
|
157
|
+
end() {
|
|
158
|
+
this.ended = true;
|
|
159
|
+
this.wake?.();
|
|
160
|
+
return this;
|
|
234
161
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
162
|
+
push(item) {
|
|
163
|
+
const node = { item };
|
|
164
|
+
if (this.tail)
|
|
165
|
+
this.tail.next = node;
|
|
166
|
+
else
|
|
167
|
+
this.head = node;
|
|
168
|
+
this.tail = node;
|
|
169
|
+
this.wake?.();
|
|
170
|
+
}
|
|
171
|
+
shift() {
|
|
172
|
+
this.head = this.head?.next;
|
|
173
|
+
if (!this.head)
|
|
174
|
+
this.tail = undefined;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* True if the pack must read the exact `dev` and `ino` of the
|
|
178
|
+
* object with the `bigint` option: the numbers are not exact, and a
|
|
179
|
+
* wrong match loses data. That is a file with more than one link,
|
|
180
|
+
* which a wrong match makes a hard link to a different file, and
|
|
181
|
+
* with `follow` a directory, which a wrong match skips as a
|
|
182
|
+
* directory in itself. Each other object gets no second stat.
|
|
183
|
+
*/
|
|
184
|
+
needsExact(stat) {
|
|
185
|
+
return (lossy(stat) &&
|
|
186
|
+
(stat.isDirectory() ?
|
|
187
|
+
!!this.opt.follow
|
|
188
|
+
: stat.isFile() && stat.nlink > 1));
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The type of the entry to write for a path, or `undefined` to
|
|
192
|
+
* write no entry. The pack calls it one time for each path, in the
|
|
193
|
+
* order of the queue. `id` is the stat, or the exact `dev` and `ino`
|
|
194
|
+
* of the object.
|
|
195
|
+
*/
|
|
196
|
+
plan(job, stat, id) {
|
|
197
|
+
const { filter } = this.opt;
|
|
198
|
+
if (filter && !filter(job.path, stat))
|
|
199
|
+
return;
|
|
200
|
+
if (stat.isDirectory()) {
|
|
201
|
+
for (let a = job.up; a; a = a.up) {
|
|
202
|
+
if (a.key !== idOf(id))
|
|
203
|
+
continue;
|
|
204
|
+
warn(this.opt, 'TAR_ENTRY_ERROR', 'directory is its own ancestor', {
|
|
205
|
+
path: job.path,
|
|
206
|
+
absolute: job.absolute,
|
|
207
|
+
});
|
|
208
|
+
return;
|
|
251
209
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
210
|
+
return 'Directory';
|
|
211
|
+
}
|
|
212
|
+
if (stat.isSymbolicLink())
|
|
213
|
+
return 'SymbolicLink';
|
|
214
|
+
if (!stat.isFile()) {
|
|
215
|
+
// A socket, a FIFO, or a device.
|
|
216
|
+
warn(this.opt, 'TAR_ENTRY_UNSUPPORTED', 'unsupported file type', {
|
|
217
|
+
path: job.path,
|
|
218
|
+
absolute: job.absolute,
|
|
219
|
+
});
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (stat.nlink > 1) {
|
|
223
|
+
const key = idOf(id);
|
|
224
|
+
const target = this.#links.get(key);
|
|
225
|
+
const cwd = this.cwd;
|
|
226
|
+
// A link is only made to a path in the cwd. A directory beside
|
|
227
|
+
// the cwd can have a name that starts with the name of the cwd.
|
|
228
|
+
// A path that is added two times is a file each time: a link to
|
|
229
|
+
// itself cannot be extracted.
|
|
230
|
+
if (target !== job.absolute &&
|
|
231
|
+
target?.startsWith(cwd.endsWith('/') ? cwd : cwd + '/')) {
|
|
232
|
+
job.linkpath = relative(cwd, target);
|
|
233
|
+
return 'Link';
|
|
263
234
|
}
|
|
235
|
+
this.#links.set(key, job.absolute);
|
|
264
236
|
}
|
|
265
|
-
|
|
237
|
+
return 'File';
|
|
266
238
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
239
|
+
/** The jobs for the names in the directory of `job`. */
|
|
240
|
+
children(job, names, id) {
|
|
241
|
+
const base = job.path === './' ? '' : job.path.replace(/\/*$/, '/');
|
|
242
|
+
const dir = job.absolute.endsWith('/') ? job.absolute : job.absolute + '/';
|
|
243
|
+
// Only a pack that follows symbolic links can find a directory
|
|
244
|
+
// in itself.
|
|
245
|
+
const up = this.opt.follow ? { key: idOf(id), up: job.up } : undefined;
|
|
246
|
+
// Node.js sorts the names of a directory. Bun does not. Sort
|
|
247
|
+
// them, so that each runtime makes the same archive.
|
|
248
|
+
return [...names].sort().map(name => {
|
|
249
|
+
const child = new PackJob(base + name, dir + name);
|
|
250
|
+
child.up = up;
|
|
251
|
+
return child;
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Report a path that does not exist, and skip it. Each other error
|
|
256
|
+
* of the stat is thrown.
|
|
257
|
+
*/
|
|
258
|
+
missing(job, er) {
|
|
259
|
+
if (er?.code !== 'ENOENT')
|
|
260
|
+
throw er;
|
|
261
|
+
warn(this.opt, 'TAR_ENTRY_ERROR', er, {
|
|
262
|
+
path: job.path,
|
|
263
|
+
absolute: job.absolute,
|
|
277
264
|
});
|
|
278
265
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
266
|
+
/** Report a path that is a different object after its `lstat`. */
|
|
267
|
+
changed(job, type) {
|
|
268
|
+
warn(this.opt, 'TAR_ENTRY_ERROR', type === 'Directory' ?
|
|
269
|
+
'directory changed between lstat and readdir'
|
|
270
|
+
: 'file changed between lstat and open', { path: job.path, absolute: job.absolute });
|
|
271
|
+
}
|
|
272
|
+
/** Make the entry for a path. */
|
|
273
|
+
pathEntry(job, stat, type, linkpath, body) {
|
|
274
|
+
const { processUid, processUname = '' } = this.opt;
|
|
275
|
+
const entry = new WriteEntry({
|
|
276
|
+
path: job.path,
|
|
277
|
+
type,
|
|
278
|
+
mode: stat.mode,
|
|
279
|
+
uid: stat.uid,
|
|
280
|
+
gid: stat.gid,
|
|
281
|
+
uname: stat.uid === processUid ? processUname : '',
|
|
282
|
+
size: type === 'File' ? stat.size : 0,
|
|
283
|
+
mtime: stat.mtime,
|
|
284
|
+
atime: stat.atime,
|
|
285
|
+
ctime: stat.ctime,
|
|
286
|
+
linkpath: linkpath && this.win32 ? slashes(linkpath) : linkpath,
|
|
287
|
+
dev: stat.dev,
|
|
288
|
+
ino: stat.ino,
|
|
289
|
+
nlink: stat.nlink,
|
|
290
|
+
}, body);
|
|
291
|
+
entry.absolute = job.absolute;
|
|
292
|
+
return this.#finish(entry, true);
|
|
283
293
|
}
|
|
284
|
-
|
|
285
|
-
|
|
294
|
+
/**
|
|
295
|
+
* Make the entry to write for an entry that the caller added.
|
|
296
|
+
* `undefined` if the filter refuses an entry of another archive.
|
|
297
|
+
*/
|
|
298
|
+
addedEntry(item) {
|
|
299
|
+
if (isWriteEntry(item))
|
|
300
|
+
return this.#finish(item, false);
|
|
301
|
+
const { filter } = this.opt;
|
|
302
|
+
if (filter && !filter(item.path, item))
|
|
286
303
|
return;
|
|
304
|
+
return this.#finish(new WriteEntry(item, item.body), false);
|
|
305
|
+
}
|
|
306
|
+
// Apply the options to the fields of the entry.
|
|
307
|
+
#finish(entry, fromPath) {
|
|
308
|
+
const { opt, win32 } = this;
|
|
309
|
+
const dir = entry.type === 'Directory';
|
|
310
|
+
if (win32 && !fromPath) {
|
|
311
|
+
entry.path = slashes(entry.path);
|
|
312
|
+
if (entry.linkpath)
|
|
313
|
+
entry.linkpath = slashes(entry.linkpath);
|
|
314
|
+
}
|
|
315
|
+
const [root, stripped] = opt.preservePaths ? ['', entry.path] : stripAbsolutePath(entry.path);
|
|
316
|
+
entry.path = stripped;
|
|
317
|
+
if (fromPath) {
|
|
318
|
+
if (win32)
|
|
319
|
+
entry.path = winchars.decode(entry.path);
|
|
320
|
+
if (entry.path === '')
|
|
321
|
+
entry.path = './';
|
|
322
|
+
}
|
|
323
|
+
if (root) {
|
|
324
|
+
warn(opt, 'TAR_ENTRY_INFO', `stripping ${root} from absolute path`, {
|
|
325
|
+
entry,
|
|
326
|
+
path: root + entry.path,
|
|
327
|
+
});
|
|
287
328
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
329
|
+
if (fromPath && dir && !entry.path.endsWith('/'))
|
|
330
|
+
entry.path += '/';
|
|
331
|
+
if (opt.portable) {
|
|
332
|
+
entry.uid =
|
|
333
|
+
entry.gid =
|
|
334
|
+
entry.dev =
|
|
335
|
+
entry.ino =
|
|
336
|
+
entry.nlink =
|
|
337
|
+
undefined;
|
|
338
|
+
entry.uname = entry.gname = undefined;
|
|
339
|
+
entry.atime = entry.ctime = undefined;
|
|
340
|
+
}
|
|
341
|
+
if (entry.mode !== undefined) {
|
|
342
|
+
entry.mode = modeFix(entry.mode, dir, !!opt.portable);
|
|
343
|
+
}
|
|
344
|
+
entry.mtime =
|
|
345
|
+
opt.noMtime || (opt.portable && dir) ?
|
|
346
|
+
undefined
|
|
347
|
+
: opt.mtime || entry.mtime;
|
|
348
|
+
return entry;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
// Read from the handle into `buf` at `off`, until the buffer is full
|
|
352
|
+
// or the file has no more bytes. `pos` is the place in the file.
|
|
353
|
+
// Returns the new place.
|
|
354
|
+
const fill = async (handle, buf, off, pos, size, path) => {
|
|
355
|
+
while (off < buf.length && pos < size) {
|
|
356
|
+
const { bytesRead } = await handle.read(buf, off, buf.length - off, pos);
|
|
357
|
+
if (bytesRead <= 0)
|
|
358
|
+
throw fewer(path, size);
|
|
359
|
+
if (bytesRead > size - pos)
|
|
360
|
+
throw more(path, size);
|
|
361
|
+
off += bytesRead;
|
|
362
|
+
pos += bytesRead;
|
|
363
|
+
}
|
|
364
|
+
return pos;
|
|
365
|
+
};
|
|
366
|
+
const fillSync = (fs, fd, buf, off, pos, size, path) => {
|
|
367
|
+
while (off < buf.length && pos < size) {
|
|
368
|
+
const bytesRead = fs.readSync(fd, buf, off, buf.length - off, pos);
|
|
369
|
+
if (bytesRead <= 0)
|
|
370
|
+
throw fewer(path, size);
|
|
371
|
+
if (bytesRead > size - pos)
|
|
372
|
+
throw more(path, size);
|
|
373
|
+
off += bytesRead;
|
|
374
|
+
pos += bytesRead;
|
|
375
|
+
}
|
|
376
|
+
return pos;
|
|
377
|
+
};
|
|
378
|
+
// The chunks of a body, from each kind of content.
|
|
379
|
+
async function* chunks(body) {
|
|
380
|
+
if (isUint8Array(body))
|
|
381
|
+
yield body;
|
|
382
|
+
else if (isBlob(body) || isReadableStream(body)) {
|
|
383
|
+
const reader = (isBlob(body) ? body.stream() : body).getReader();
|
|
384
|
+
try {
|
|
385
|
+
for (;;) {
|
|
386
|
+
const res = await reader.read();
|
|
387
|
+
if (res.done)
|
|
388
|
+
break;
|
|
389
|
+
yield res.value;
|
|
295
390
|
}
|
|
296
391
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
392
|
+
finally {
|
|
393
|
+
await reader.cancel().catch(noop);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
else if (body)
|
|
397
|
+
yield* body;
|
|
398
|
+
}
|
|
399
|
+
function* chunksSync(body) {
|
|
400
|
+
if (isUint8Array(body))
|
|
401
|
+
yield body;
|
|
402
|
+
else if (body && Symbol.iterator in body)
|
|
403
|
+
yield* body;
|
|
404
|
+
else if (body) {
|
|
405
|
+
throw new TypeError('PackSync needs a body that is a string, a Uint8Array, or an iterable');
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
// Count the bytes of a body part. Returns the bytes that are left.
|
|
409
|
+
const count = (entry, remain, chunk) => {
|
|
410
|
+
if (chunk.length > remain)
|
|
411
|
+
throw tooLong(entry);
|
|
412
|
+
return remain - chunk.length;
|
|
413
|
+
};
|
|
414
|
+
// The zeros after a body, or an error if the body was too short.
|
|
415
|
+
const padding = (entry, remain) => {
|
|
416
|
+
if (remain)
|
|
417
|
+
throw eof('encountered unexpected EOF', pathOf(entry));
|
|
418
|
+
return new Uint8Array(blockLength(entry.size) - entry.size);
|
|
419
|
+
};
|
|
420
|
+
// The body of the entry, with the zeros that fill its last block.
|
|
421
|
+
async function* bodyBlocks(entry) {
|
|
422
|
+
if (!entry.size)
|
|
423
|
+
return;
|
|
424
|
+
let remain = entry.size;
|
|
425
|
+
for await (const chunk of chunks(entry.body)) {
|
|
426
|
+
remain = count(entry, remain, chunk);
|
|
427
|
+
yield chunk;
|
|
428
|
+
}
|
|
429
|
+
const pad = padding(entry, remain);
|
|
430
|
+
if (pad.length)
|
|
431
|
+
yield pad;
|
|
432
|
+
}
|
|
433
|
+
function* bodyBlocksSync(entry) {
|
|
434
|
+
if (!entry.size)
|
|
435
|
+
return;
|
|
436
|
+
let remain = entry.size;
|
|
437
|
+
for (const chunk of chunksSync(entry.body)) {
|
|
438
|
+
remain = count(entry, remain, chunk);
|
|
439
|
+
yield chunk;
|
|
440
|
+
}
|
|
441
|
+
const pad = padding(entry, remain);
|
|
442
|
+
if (pad.length)
|
|
443
|
+
yield pad;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Makes an archive. `add()` paths and entries, then `end()`. The
|
|
447
|
+
* pack is the async iterator of the bytes of the archive, compressed
|
|
448
|
+
* if an option selects a codec.
|
|
449
|
+
*
|
|
450
|
+
* It reads the file system only when the consumer asks for bytes.
|
|
451
|
+
* Then it reads the first `jobs` paths of the queue at the same
|
|
452
|
+
* time, and writes them in the order of the queue. The entries of a
|
|
453
|
+
* directory go to the end of the queue.
|
|
454
|
+
*
|
|
455
|
+
* `return()` closes each file that is open.
|
|
456
|
+
*/
|
|
457
|
+
export class Pack extends PackBase {
|
|
458
|
+
jobs;
|
|
459
|
+
#out;
|
|
460
|
+
// Settles when the pack called `plan()` for each path that it
|
|
461
|
+
// started to read. It keeps the calls in the order of the queue.
|
|
462
|
+
#planned = Promise.resolve();
|
|
463
|
+
constructor(opt = {}) {
|
|
464
|
+
super(opt, 'compress');
|
|
465
|
+
this.jobs = countOption('jobs', this.opt.jobs, 4);
|
|
466
|
+
}
|
|
467
|
+
async next() {
|
|
468
|
+
const res = await (this.#out ??= this.#output()).next();
|
|
469
|
+
return res.done ? { value: undefined, done: true } : res;
|
|
470
|
+
}
|
|
471
|
+
/** Stop, and close each file that is open. */
|
|
472
|
+
async return() {
|
|
473
|
+
this.end();
|
|
474
|
+
await this.#out?.return();
|
|
475
|
+
return { value: undefined, done: true };
|
|
476
|
+
}
|
|
477
|
+
[Symbol.asyncIterator]() {
|
|
478
|
+
return this;
|
|
479
|
+
}
|
|
480
|
+
#output() {
|
|
481
|
+
const { codec } = this;
|
|
482
|
+
return codec?.compress ?
|
|
483
|
+
this.#compress(codec.compress(this.codecOptions))
|
|
484
|
+
: this.#run();
|
|
485
|
+
}
|
|
486
|
+
async *#compress(stream) {
|
|
487
|
+
const run = this.#run();
|
|
488
|
+
// An error of the pack goes to the consumer, also when the codec
|
|
489
|
+
// does not pass it on: then the cancel ends the read.
|
|
490
|
+
let failure;
|
|
491
|
+
const reader = toStream({
|
|
492
|
+
next: () => run.next().catch(er => {
|
|
493
|
+
failure = { er };
|
|
494
|
+
void reader.cancel().catch(noop);
|
|
495
|
+
throw er;
|
|
496
|
+
}),
|
|
497
|
+
})
|
|
498
|
+
.pipeThrough(stream)
|
|
499
|
+
.getReader();
|
|
500
|
+
try {
|
|
501
|
+
for (;;) {
|
|
502
|
+
const res = await reader.read();
|
|
503
|
+
if (failure)
|
|
504
|
+
throw failure.er;
|
|
505
|
+
if (res.done)
|
|
506
|
+
break;
|
|
507
|
+
yield res.value;
|
|
305
508
|
}
|
|
306
509
|
}
|
|
510
|
+
finally {
|
|
511
|
+
await reader.cancel().catch(noop);
|
|
512
|
+
await run.return();
|
|
513
|
+
}
|
|
307
514
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
const key = `${stat.dev}:${stat.ino}`;
|
|
318
|
-
const pending = this[PENDINGLINKS].get(key);
|
|
319
|
-
if (pending) {
|
|
320
|
-
this[PENDINGLINKS].delete(key);
|
|
321
|
-
for (const job of pending) {
|
|
322
|
-
job.pending = false;
|
|
323
|
-
this[PROCESSJOB](job);
|
|
515
|
+
async *#run() {
|
|
516
|
+
try {
|
|
517
|
+
for (;;) {
|
|
518
|
+
const node = this.head;
|
|
519
|
+
if (!node) {
|
|
520
|
+
if (this.ended)
|
|
521
|
+
break;
|
|
522
|
+
await new Promise(res => (this.wake = res));
|
|
523
|
+
continue;
|
|
324
524
|
}
|
|
525
|
+
this.#start();
|
|
526
|
+
const { item } = node;
|
|
527
|
+
if (item instanceof PackJob)
|
|
528
|
+
yield* this.#path(item);
|
|
529
|
+
else if (!isSource(item))
|
|
530
|
+
yield* this.#entry(item);
|
|
531
|
+
else
|
|
532
|
+
for await (const entry of item)
|
|
533
|
+
yield* this.#entry(entry);
|
|
534
|
+
this.shift();
|
|
535
|
+
}
|
|
536
|
+
yield new Uint8Array(1024);
|
|
537
|
+
}
|
|
538
|
+
finally {
|
|
539
|
+
// Close the files of the paths that the pack did not write.
|
|
540
|
+
for (let node = this.head; node; node = node.next) {
|
|
541
|
+
const { item } = node;
|
|
542
|
+
if (!(item instanceof PackJob) || !item.ready)
|
|
543
|
+
break;
|
|
544
|
+
const load = await item.ready.catch(noop);
|
|
545
|
+
await load?.handle?.close().catch(noop);
|
|
325
546
|
}
|
|
326
547
|
}
|
|
327
|
-
this[PROCESS]();
|
|
328
548
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
job
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
549
|
+
// Start to read the paths at the start of the queue.
|
|
550
|
+
#start() {
|
|
551
|
+
const fs = this.fs;
|
|
552
|
+
let node = this.head;
|
|
553
|
+
for (let n = 0; node && n < this.jobs; n++, node = node.next) {
|
|
554
|
+
const job = node.item;
|
|
555
|
+
// An entry can change what the filter does with a later path.
|
|
556
|
+
if (!(job instanceof PackJob))
|
|
557
|
+
break;
|
|
558
|
+
if (job.ready)
|
|
559
|
+
continue;
|
|
560
|
+
const stat = this.#stat(fs, job.absolute);
|
|
561
|
+
stat.catch(noop);
|
|
562
|
+
const planned = this.#planned
|
|
563
|
+
.then(() => stat)
|
|
564
|
+
.then(([st, id]) => {
|
|
565
|
+
const type = this.plan(job, st, id);
|
|
566
|
+
return type && { load: this.#load(fs, job, st, id, type) };
|
|
567
|
+
}, er => this.missing(job, er));
|
|
568
|
+
this.#planned = planned.catch(noop);
|
|
569
|
+
job.ready = planned.then(p => p?.load);
|
|
570
|
+
job.ready.catch(noop);
|
|
344
571
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
572
|
+
}
|
|
573
|
+
// The stat of a path, and the identity of the object.
|
|
574
|
+
async #stat(fs, path) {
|
|
575
|
+
const { follow } = this.opt;
|
|
576
|
+
const stat = await (follow ? fs.stat(path) : fs.lstat(path));
|
|
577
|
+
const id = !this.needsExact(stat) ?
|
|
578
|
+
stat
|
|
579
|
+
: await (follow ? fs.stat(path, BIG) : fs.lstat(path, BIG));
|
|
580
|
+
return [stat, id];
|
|
581
|
+
}
|
|
582
|
+
async #load(fs, job, stat, id, type) {
|
|
583
|
+
const { absolute } = job;
|
|
584
|
+
const { follow, noDirRecurse } = this.opt;
|
|
585
|
+
const load = { stat, id, type };
|
|
586
|
+
if (type === 'SymbolicLink') {
|
|
587
|
+
load.linkpath = await fs.readlink(absolute);
|
|
588
|
+
}
|
|
589
|
+
else if (type === 'Directory' && !noDirRecurse) {
|
|
590
|
+
load.names = await fs.readdir(absolute);
|
|
591
|
+
load.changed = !follow && !same(stat, await fs.lstat(absolute));
|
|
592
|
+
}
|
|
593
|
+
else if (type === 'File' && stat.size) {
|
|
594
|
+
const handle = await fs.open(absolute, 'r');
|
|
595
|
+
try {
|
|
596
|
+
const { size } = stat;
|
|
597
|
+
const length = blockLength(size);
|
|
598
|
+
if (!follow && !same(stat, await handle.stat())) {
|
|
599
|
+
load.changed = true;
|
|
600
|
+
}
|
|
601
|
+
else if (length > this.maxReadSize)
|
|
602
|
+
load.handle = handle;
|
|
603
|
+
else {
|
|
604
|
+
load.buf = new Uint8Array(512 + length);
|
|
605
|
+
await fill(handle, load.buf, 512, 0, size, absolute);
|
|
606
|
+
}
|
|
349
607
|
}
|
|
350
|
-
|
|
351
|
-
|
|
608
|
+
catch (er) {
|
|
609
|
+
await handle.close().catch(noop);
|
|
610
|
+
throw er;
|
|
352
611
|
}
|
|
612
|
+
if (!load.handle)
|
|
613
|
+
await handle.close();
|
|
353
614
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
if (
|
|
615
|
+
return load;
|
|
616
|
+
}
|
|
617
|
+
async *#path(job) {
|
|
618
|
+
const load = await job.ready;
|
|
619
|
+
if (!load)
|
|
359
620
|
return;
|
|
360
|
-
}
|
|
361
|
-
if (
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
this
|
|
621
|
+
const { stat, id, type, names, buf, handle } = load;
|
|
622
|
+
if (load.changed)
|
|
623
|
+
return this.changed(job, type);
|
|
624
|
+
let ok = false;
|
|
625
|
+
try {
|
|
626
|
+
const body = buf ?
|
|
627
|
+
buf.subarray(512, 512 + stat.size)
|
|
628
|
+
: handle && this.#read(handle, stat.size, job.absolute);
|
|
629
|
+
const entry = this.pathEntry(job, stat, type, load.linkpath ?? job.linkpath, body);
|
|
630
|
+
if (names) {
|
|
631
|
+
for (const child of this.children(job, names, id)) {
|
|
632
|
+
this.push(child);
|
|
633
|
+
}
|
|
368
634
|
}
|
|
369
|
-
|
|
370
|
-
|
|
635
|
+
await this.opt.onWriteEntry?.(entry);
|
|
636
|
+
// The header goes in the space before a body that is in `buf`.
|
|
637
|
+
const whole = buf && entry.body === body && entry.size === stat.size;
|
|
638
|
+
yield* entry.encode(this.header, whole ? buf : undefined);
|
|
639
|
+
if (!whole)
|
|
640
|
+
yield* bodyBlocks(entry);
|
|
641
|
+
ok = true;
|
|
642
|
+
}
|
|
643
|
+
finally {
|
|
644
|
+
if (handle) {
|
|
645
|
+
load.handle = undefined;
|
|
646
|
+
const closed = handle.close();
|
|
647
|
+
await (ok ? closed : closed.catch(noop));
|
|
371
648
|
}
|
|
372
649
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
this
|
|
650
|
+
}
|
|
651
|
+
// The chunks of a file that is larger than `maxReadSize`.
|
|
652
|
+
async *#read(handle, size, path) {
|
|
653
|
+
const length = blockLength(size);
|
|
654
|
+
for (let pos = 0; pos < size;) {
|
|
655
|
+
// Ask for the bytes of the last block also, to find a file that
|
|
656
|
+
// is larger now.
|
|
657
|
+
const buf = new Uint8Array(Math.min(length - pos, this.maxReadSize));
|
|
658
|
+
const end = await fill(handle, buf, 0, pos, size, path);
|
|
659
|
+
yield buf.subarray(0, end - pos);
|
|
660
|
+
pos = end;
|
|
381
661
|
}
|
|
382
662
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
663
|
+
async *#entry(item) {
|
|
664
|
+
const entry = this.addedEntry(item);
|
|
665
|
+
if (!entry)
|
|
666
|
+
return;
|
|
667
|
+
await this.opt.onWriteEntry?.(entry);
|
|
668
|
+
yield* entry.encode(this.header);
|
|
669
|
+
yield* bodyBlocks(entry);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Makes an archive with no promise. `add()` paths and entries. The
|
|
674
|
+
* pack is the iterator of the bytes of the archive. It reads the
|
|
675
|
+
* file system only when the consumer asks for bytes, one path at a
|
|
676
|
+
* time. The entries of a directory come before the next item that
|
|
677
|
+
* the caller added.
|
|
678
|
+
*
|
|
679
|
+
* The pack ends when the iterator comes to the end of the queue.
|
|
680
|
+
* With compression, the iterator gives all the archive in one chunk.
|
|
681
|
+
*/
|
|
682
|
+
export class PackSync extends PackBase {
|
|
683
|
+
#out;
|
|
684
|
+
constructor(opt = {}) {
|
|
685
|
+
super(opt, 'compressSync');
|
|
686
|
+
}
|
|
687
|
+
next() {
|
|
688
|
+
const res = (this.#out ??= this.#output()).next();
|
|
689
|
+
return res.done ? { value: undefined, done: true } : res;
|
|
690
|
+
}
|
|
691
|
+
/** Stop, and close the file that is open. */
|
|
692
|
+
return() {
|
|
693
|
+
this.ended = true;
|
|
694
|
+
this.#out?.return();
|
|
695
|
+
return { value: undefined, done: true };
|
|
400
696
|
}
|
|
401
|
-
[
|
|
402
|
-
this
|
|
697
|
+
[Symbol.iterator]() {
|
|
698
|
+
return this;
|
|
699
|
+
}
|
|
700
|
+
*#output() {
|
|
701
|
+
const { codec } = this;
|
|
702
|
+
if (!codec?.compressSync)
|
|
703
|
+
return yield* this.#run();
|
|
704
|
+
yield codec.compressSync(concat([...this.#run()]), this.codecOptions);
|
|
705
|
+
}
|
|
706
|
+
*#run() {
|
|
707
|
+
for (let node; (node = this.head); this.shift()) {
|
|
708
|
+
const { item } = node;
|
|
709
|
+
if (item instanceof PackJob) {
|
|
710
|
+
const jobs = [item];
|
|
711
|
+
for (const job of jobs)
|
|
712
|
+
yield* this.#path(job, jobs);
|
|
713
|
+
}
|
|
714
|
+
else if (!isSource(item))
|
|
715
|
+
yield* this.#entry(item);
|
|
716
|
+
else
|
|
717
|
+
for (const entry of item)
|
|
718
|
+
yield* this.#entry(entry);
|
|
719
|
+
}
|
|
720
|
+
this.ended = true;
|
|
721
|
+
yield new Uint8Array(1024);
|
|
722
|
+
}
|
|
723
|
+
*#path(job, jobs) {
|
|
724
|
+
const fs = this.fs;
|
|
725
|
+
const { absolute } = job;
|
|
726
|
+
const { follow, noDirRecurse } = this.opt;
|
|
727
|
+
let stat;
|
|
403
728
|
try {
|
|
404
|
-
|
|
405
|
-
return e
|
|
406
|
-
.on('end', () => this[JOBDONE](job))
|
|
407
|
-
.on('error', er => this.emit('error', er));
|
|
729
|
+
stat = follow ? fs.statSync(absolute) : fs.lstatSync(absolute);
|
|
408
730
|
}
|
|
409
731
|
catch (er) {
|
|
410
|
-
this.
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
[ONDRAIN]() {
|
|
414
|
-
if (this[CURRENT] && this[CURRENT].entry) {
|
|
415
|
-
this[CURRENT].entry.resume();
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
// like .pipe() but using super, because our write() is special
|
|
419
|
-
[PIPE](job) {
|
|
420
|
-
job.piped = true;
|
|
421
|
-
if (job.readdir) {
|
|
422
|
-
job.readdir.forEach(entry => {
|
|
423
|
-
const p = job.path;
|
|
424
|
-
const base = p === './' ? '' : p.replace(/\/*$/, '/');
|
|
425
|
-
this[ADDFSENTRY](base + entry);
|
|
426
|
-
});
|
|
732
|
+
return this.missing(job, er);
|
|
427
733
|
}
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
734
|
+
const id = !this.needsExact(stat) ? stat
|
|
735
|
+
: follow ? fs.statSync(absolute, BIG)
|
|
736
|
+
: fs.lstatSync(absolute, BIG);
|
|
737
|
+
const type = this.plan(job, stat, id);
|
|
738
|
+
if (!type)
|
|
739
|
+
return;
|
|
740
|
+
let { linkpath } = job;
|
|
741
|
+
let buf;
|
|
742
|
+
let body;
|
|
743
|
+
let fd;
|
|
744
|
+
let ok = false;
|
|
745
|
+
try {
|
|
746
|
+
if (type === 'SymbolicLink')
|
|
747
|
+
linkpath = fs.readlinkSync(absolute);
|
|
748
|
+
else if (type === 'Directory' && !noDirRecurse) {
|
|
749
|
+
const names = fs.readdirSync(absolute);
|
|
750
|
+
if (!follow && !same(stat, fs.lstatSync(absolute))) {
|
|
751
|
+
return this.changed(job, type);
|
|
438
752
|
}
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
753
|
+
jobs.push(...this.children(job, names, id));
|
|
754
|
+
}
|
|
755
|
+
else if (type === 'File' && stat.size) {
|
|
756
|
+
const { size } = stat;
|
|
757
|
+
const length = blockLength(size);
|
|
758
|
+
fd = fs.openSync(absolute, 'r');
|
|
759
|
+
if (!follow && !same(stat, fs.fstatSync(fd))) {
|
|
760
|
+
return this.changed(job, type);
|
|
445
761
|
}
|
|
446
|
-
|
|
762
|
+
if (length > this.maxReadSize) {
|
|
763
|
+
body = this.#read(fs, fd, size, absolute);
|
|
764
|
+
}
|
|
765
|
+
else {
|
|
766
|
+
buf = new Uint8Array(512 + length);
|
|
767
|
+
fillSync(fs, fd, buf, 512, 0, size, absolute);
|
|
768
|
+
body = buf.subarray(512, 512 + size);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
const entry = this.pathEntry(job, stat, type, linkpath, body);
|
|
772
|
+
this.opt.onWriteEntry?.(entry);
|
|
773
|
+
// The header goes in the space before a body that is in `buf`.
|
|
774
|
+
const whole = buf && entry.body === body && entry.size === stat.size;
|
|
775
|
+
yield* entry.encode(this.header, whole ? buf : undefined);
|
|
776
|
+
if (!whole)
|
|
777
|
+
yield* bodyBlocksSync(entry);
|
|
778
|
+
ok = true;
|
|
779
|
+
}
|
|
780
|
+
finally {
|
|
781
|
+
// An error of close must not hide the first error.
|
|
782
|
+
if (fd !== undefined && !ok) {
|
|
783
|
+
try {
|
|
784
|
+
fs.closeSync(fd);
|
|
785
|
+
}
|
|
786
|
+
catch { }
|
|
787
|
+
}
|
|
447
788
|
}
|
|
789
|
+
if (fd !== undefined)
|
|
790
|
+
fs.closeSync(fd);
|
|
448
791
|
}
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
792
|
+
// The chunks of a file that is larger than `maxReadSize`.
|
|
793
|
+
*#read(fs, fd, size, path) {
|
|
794
|
+
const length = blockLength(size);
|
|
795
|
+
for (let pos = 0; pos < size;) {
|
|
796
|
+
const buf = new Uint8Array(Math.min(length - pos, this.maxReadSize));
|
|
797
|
+
const end = fillSync(fs, fd, buf, 0, pos, size, path);
|
|
798
|
+
yield buf.subarray(0, end - pos);
|
|
799
|
+
pos = end;
|
|
452
800
|
}
|
|
453
|
-
return super.pause();
|
|
454
801
|
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
super(opt);
|
|
463
|
-
this[WRITEENTRYCLASS] = WriteEntrySync;
|
|
464
|
-
}
|
|
465
|
-
// pause/resume are no-ops in sync streams.
|
|
466
|
-
pause() { }
|
|
467
|
-
resume() { }
|
|
468
|
-
[STAT](job) {
|
|
469
|
-
const stat = this.follow ? 'statSync' : 'lstatSync';
|
|
470
|
-
this[ONSTAT](job, fs[stat](job.absolute));
|
|
471
|
-
}
|
|
472
|
-
[READDIR](job) {
|
|
473
|
-
this[ONREADDIR](job, fs.readdirSync(job.absolute));
|
|
474
|
-
}
|
|
475
|
-
// gotta get it all in this tick
|
|
476
|
-
[PIPE](job) {
|
|
477
|
-
const source = job.entry;
|
|
478
|
-
const zip = this.zip;
|
|
479
|
-
if (job.readdir) {
|
|
480
|
-
job.readdir.forEach(entry => {
|
|
481
|
-
const p = job.path;
|
|
482
|
-
const base = p === './' ? '' : p.replace(/\/*$/, '/');
|
|
483
|
-
this[ADDFSENTRY](base + entry);
|
|
484
|
-
});
|
|
485
|
-
}
|
|
486
|
-
/* c8 ignore start */
|
|
487
|
-
if (!source)
|
|
488
|
-
throw new Error('Cannot pipe without source');
|
|
489
|
-
/* c8 ignore stop */
|
|
490
|
-
if (zip) {
|
|
491
|
-
source.on('data', chunk => {
|
|
492
|
-
zip.write(chunk);
|
|
493
|
-
});
|
|
494
|
-
}
|
|
495
|
-
else {
|
|
496
|
-
source.on('data', chunk => {
|
|
497
|
-
super[WRITE](chunk);
|
|
498
|
-
});
|
|
499
|
-
}
|
|
802
|
+
*#entry(item) {
|
|
803
|
+
const entry = this.addedEntry(item);
|
|
804
|
+
if (!entry)
|
|
805
|
+
return;
|
|
806
|
+
this.opt.onWriteEntry?.(entry);
|
|
807
|
+
yield* entry.encode(this.header);
|
|
808
|
+
yield* bodyBlocksSync(entry);
|
|
500
809
|
}
|
|
501
810
|
}
|
|
502
811
|
//# sourceMappingURL=pack.js.map
|