@vltpkg/tar 1.6.4 → 1.8.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 +22 -3
- package/dist/git-archive-commit.d.ts +13 -0
- package/dist/git-archive-commit.js +51 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/placement-pool.d.ts +5 -4
- package/dist/placement-pool.js +17 -8
- package/dist/placement-worker.d.ts +4 -0
- package/dist/placement-worker.js +2 -2
- package/dist/pool.d.ts +6 -4
- package/dist/pool.js +12 -10
- package/dist/unpack.d.ts +10 -6
- package/dist/unpack.js +34 -17
- package/package.json +12 -7
package/README.md
CHANGED
|
@@ -49,6 +49,21 @@ Read the tarball from a file on disk, skipping `offset` leading bytes,
|
|
|
49
49
|
and unpack it synchronously. The offset lets a tarball be extracted
|
|
50
50
|
straight out of a cache entry whose file starts with a header block.
|
|
51
51
|
|
|
52
|
+
### gitArchiveCommit(tarData)
|
|
53
|
+
|
|
54
|
+
The commit a `git archive` tarball was made from: the full sha in the
|
|
55
|
+
`comment` of its leading pax global header (40 hex, 64 for a sha256
|
|
56
|
+
repo), or `undefined` (eg an npm tarball, or not a tarball). Takes a
|
|
57
|
+
gzipped or raw tarball Buffer and inflates only its head, so it is
|
|
58
|
+
cheap on any size.
|
|
59
|
+
|
|
60
|
+
### gitArchiveSubmodules(tarData)
|
|
61
|
+
|
|
62
|
+
`true` if the tarball's root dir has a `.gitmodules` file: a
|
|
63
|
+
`git archive` of a repo with submodules, which it leaves out. Takes a
|
|
64
|
+
gzipped or raw tarball Buffer and reads all of it; `false` if it is
|
|
65
|
+
not a tarball.
|
|
66
|
+
|
|
52
67
|
### `class Pool`
|
|
53
68
|
|
|
54
69
|
The interface the vlt CLI extracts through. On a large enough install,
|
|
@@ -56,16 +71,20 @@ The interface the vlt CLI extracts through. On a large enough install,
|
|
|
56
71
|
the bytes held in memory. Otherwise, and for every other method, the
|
|
57
72
|
work runs on the main thread. See [Unpack workers](#unpack-workers).
|
|
58
73
|
|
|
59
|
-
#### `pool.unpack(tarData: Buffer, target: string, format?,
|
|
74
|
+
#### `pool.unpack(tarData: Buffer, target: string, format?, path?) => Promise<void>`
|
|
60
75
|
|
|
61
76
|
Unpack the supplied Buffer of data into the target folder. A worker
|
|
62
|
-
gets a copy, so the caller keeps its buffer.
|
|
77
|
+
gets a copy, so the caller keeps its buffer. With `path`, only that
|
|
78
|
+
directory of the tarball's root dir, as the root (eg a package in a
|
|
79
|
+
`git archive` of its monorepo); throws `path not found in tarball` if
|
|
80
|
+
it has no entries there.
|
|
63
81
|
|
|
64
|
-
#### `pool.unpackCacheEntry(file: string, target: string, format?) => Promise<void>`
|
|
82
|
+
#### `pool.unpackCacheEntry(file: string, target: string, format?, path?) => Promise<void>`
|
|
65
83
|
|
|
66
84
|
Unpack the tarball inside a vlt cache entry file, whose first 4 bytes
|
|
67
85
|
are the big-endian length of the head before the body. On a worker the
|
|
68
86
|
file is read there, so the main thread never reads or copies the body.
|
|
87
|
+
`path` as in `pool.unpack`.
|
|
69
88
|
|
|
70
89
|
#### `pool.warm(jobs: number) => Promise<void>`
|
|
71
90
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The commit a `git archive` tarball was made from: the `comment` of
|
|
3
|
+
* its leading pax global header. Takes a gzipped or raw tarball and
|
|
4
|
+
* inflates only its head. `undefined` when there is none (eg an npm
|
|
5
|
+
* tarball) or the data is not a tarball.
|
|
6
|
+
*/
|
|
7
|
+
export declare const gitArchiveCommit: (tarData: Buffer) => string | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Whether a tarball's root dir has a `.gitmodules` file: a `git
|
|
10
|
+
* archive` of a repo with submodules, which it leaves out. Takes a
|
|
11
|
+
* gzipped or raw tarball; `false` if it is not one.
|
|
12
|
+
*/
|
|
13
|
+
export declare const gitArchiveSubmodules: (tarData: Buffer) => boolean;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { constants, gunzipSync } from 'node:zlib';
|
|
2
|
+
import { Header } from 'tar/header';
|
|
3
|
+
import { Parser } from 'tar/parse';
|
|
4
|
+
import { Pax } from 'tar/pax';
|
|
5
|
+
// compressed bytes to inflate: git archive needs ~200 for its first 1024
|
|
6
|
+
const gzipHead = 4096;
|
|
7
|
+
const sha = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
|
|
8
|
+
/**
|
|
9
|
+
* The commit a `git archive` tarball was made from: the `comment` of
|
|
10
|
+
* its leading pax global header. Takes a gzipped or raw tarball and
|
|
11
|
+
* inflates only its head. `undefined` when there is none (eg an npm
|
|
12
|
+
* tarball) or the data is not a tarball.
|
|
13
|
+
*/
|
|
14
|
+
export const gitArchiveCommit = (tarData) => {
|
|
15
|
+
try {
|
|
16
|
+
const buf = tarData[0] === 0x1f && tarData[1] === 0x8b ?
|
|
17
|
+
gunzipSync(tarData.subarray(0, gzipHead), {
|
|
18
|
+
finishFlush: constants.Z_SYNC_FLUSH,
|
|
19
|
+
})
|
|
20
|
+
: tarData;
|
|
21
|
+
const { cksumValid, type, size } = new Header(buf);
|
|
22
|
+
if (!cksumValid || type !== 'GlobalExtendedHeader' || !size) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const { comment } = Pax.parse(buf.subarray(512, 512 + size).toString(), undefined, true);
|
|
26
|
+
return comment && sha.test(comment) ? comment : undefined;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
// a file in the tarball's root dir
|
|
33
|
+
const rootGitmodules = /^[^/]+\/\.gitmodules$/;
|
|
34
|
+
/**
|
|
35
|
+
* Whether a tarball's root dir has a `.gitmodules` file: a `git
|
|
36
|
+
* archive` of a repo with submodules, which it leaves out. Takes a
|
|
37
|
+
* gzipped or raw tarball; `false` if it is not one.
|
|
38
|
+
*/
|
|
39
|
+
export const gitArchiveSubmodules = (tarData) => {
|
|
40
|
+
let found = false;
|
|
41
|
+
try {
|
|
42
|
+
new Parser({
|
|
43
|
+
onReadEntry: e => {
|
|
44
|
+
found ||= e.type === 'File' && rootGitmodules.test(e.path);
|
|
45
|
+
e.resume();
|
|
46
|
+
},
|
|
47
|
+
}).end(tarData);
|
|
48
|
+
}
|
|
49
|
+
catch { }
|
|
50
|
+
return found;
|
|
51
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { unpack, unpackToStoreSync } from './unpack.ts';
|
|
2
2
|
export type { TarballFormat } from './unpack.ts';
|
|
3
|
+
export { gitArchiveCommit } from './git-archive-commit.ts';
|
|
3
4
|
export { cloneAvailable } from './clonefile.ts';
|
|
4
5
|
export * from './link-tree.ts';
|
|
5
6
|
export * from './placement-pool.ts';
|
package/dist/index.js
CHANGED
package/dist/placement-pool.d.ts
CHANGED
|
@@ -77,21 +77,22 @@ export declare class PlacementPool {
|
|
|
77
77
|
* Unpack `tarData` into `target`: on a worker once the pool is
|
|
78
78
|
* threaded, inline otherwise (inflating on libuv's threadpool and
|
|
79
79
|
* writing on the main thread, as before this pool existed). Resolves
|
|
80
|
-
* when the files are in place.
|
|
80
|
+
* when the files are in place. `path`: only that dir of the
|
|
81
|
+
* tarball's root dir, see `unpack()` in `unpack.ts`.
|
|
81
82
|
*
|
|
82
83
|
* The bytes are copied before they go to a worker: the caller may
|
|
83
84
|
* still hold them (the registry client caches its response bodies),
|
|
84
85
|
* and a transfer would detach them for everyone. For a tarball that
|
|
85
86
|
* is already on disk, {@link unpackCacheEntry} avoids the copy.
|
|
86
87
|
*/
|
|
87
|
-
unpack(tarData: Buffer, target: string, format?: TarballFormat): Promise<void>;
|
|
88
|
+
unpack(tarData: Buffer, target: string, format?: TarballFormat, path?: string): Promise<void>;
|
|
88
89
|
/**
|
|
89
90
|
* Unpack the tarball in the vlt cache entry `file` into `target`. A
|
|
90
91
|
* worker reads the file itself, so the main thread neither reads nor
|
|
91
92
|
* copies the body, and nothing is held against the byte cap. Inline
|
|
92
|
-
* when the pool is not threaded, like {@link unpack}
|
|
93
|
+
* when the pool is not threaded, like {@link unpack}, as is `path`.
|
|
93
94
|
*/
|
|
94
|
-
unpackCacheEntry(file: string, target: string, format?: TarballFormat): Promise<void>;
|
|
95
|
+
unpackCacheEntry(file: string, target: string, format?: TarballFormat, path?: string): Promise<void>;
|
|
95
96
|
/**
|
|
96
97
|
* Terminate every worker. Safe to call more than once. Any placement
|
|
97
98
|
* still assigned or queued is rejected rather than left hanging, since
|
package/dist/placement-pool.js
CHANGED
|
@@ -173,24 +173,25 @@ export class PlacementPool {
|
|
|
173
173
|
* Unpack `tarData` into `target`: on a worker once the pool is
|
|
174
174
|
* threaded, inline otherwise (inflating on libuv's threadpool and
|
|
175
175
|
* writing on the main thread, as before this pool existed). Resolves
|
|
176
|
-
* when the files are in place.
|
|
176
|
+
* when the files are in place. `path`: only that dir of the
|
|
177
|
+
* tarball's root dir, see `unpack()` in `unpack.ts`.
|
|
177
178
|
*
|
|
178
179
|
* The bytes are copied before they go to a worker: the caller may
|
|
179
180
|
* still hold them (the registry client caches its response bodies),
|
|
180
181
|
* and a transfer would detach them for everyone. For a tarball that
|
|
181
182
|
* is already on disk, {@link unpackCacheEntry} avoids the copy.
|
|
182
183
|
*/
|
|
183
|
-
async unpack(tarData, target, format) {
|
|
184
|
+
async unpack(tarData, target, format, path) {
|
|
184
185
|
this.#maybeAutoWarm();
|
|
185
186
|
if (this.#inlineNow())
|
|
186
|
-
return unpackParallel(tarData, target, format);
|
|
187
|
+
return unpackParallel(tarData, target, format, path);
|
|
187
188
|
const bytes = tarData.byteLength;
|
|
188
189
|
await this.#reserve(bytes);
|
|
189
190
|
// the pool may have closed, or lost its last worker, while this
|
|
190
191
|
// waited for room: it is inline now, so place inline
|
|
191
192
|
if (this.#inlineNow()) {
|
|
192
193
|
this.#release(bytes);
|
|
193
|
-
return unpackParallel(tarData, target, format);
|
|
194
|
+
return unpackParallel(tarData, target, format, path);
|
|
194
195
|
}
|
|
195
196
|
const copy = new Uint8Array(bytes);
|
|
196
197
|
copy.set(tarData);
|
|
@@ -202,20 +203,28 @@ export class PlacementPool {
|
|
|
202
203
|
byteLength: bytes,
|
|
203
204
|
target,
|
|
204
205
|
format,
|
|
206
|
+
path,
|
|
205
207
|
}, bytes);
|
|
206
208
|
}
|
|
207
209
|
/**
|
|
208
210
|
* Unpack the tarball in the vlt cache entry `file` into `target`. A
|
|
209
211
|
* worker reads the file itself, so the main thread neither reads nor
|
|
210
212
|
* copies the body, and nothing is held against the byte cap. Inline
|
|
211
|
-
* when the pool is not threaded, like {@link unpack}
|
|
213
|
+
* when the pool is not threaded, like {@link unpack}, as is `path`.
|
|
212
214
|
*/
|
|
213
|
-
async unpackCacheEntry(file, target, format) {
|
|
215
|
+
async unpackCacheEntry(file, target, format, path) {
|
|
214
216
|
this.#maybeAutoWarm();
|
|
215
217
|
if (this.#inlineNow()) {
|
|
216
|
-
return unpackCacheEntryParallel(file, target, format);
|
|
218
|
+
return unpackCacheEntryParallel(file, target, format, path);
|
|
217
219
|
}
|
|
218
|
-
await this.#submit({
|
|
220
|
+
await this.#submit({
|
|
221
|
+
kind: 'cacheEntry',
|
|
222
|
+
id: this.#seq++,
|
|
223
|
+
file,
|
|
224
|
+
target,
|
|
225
|
+
format,
|
|
226
|
+
path,
|
|
227
|
+
}, 0);
|
|
219
228
|
}
|
|
220
229
|
// Wait until `bytes` more may be held. Nothing waits while nothing is
|
|
221
230
|
// held, so one oversized tarball never deadlocks.
|
|
@@ -32,6 +32,8 @@ export type UnpackJob = {
|
|
|
32
32
|
byteLength: number;
|
|
33
33
|
target: string;
|
|
34
34
|
format?: TarballFormat;
|
|
35
|
+
/** only that dir of the tarball's root dir, see `unpack.ts` */
|
|
36
|
+
path?: string;
|
|
35
37
|
};
|
|
36
38
|
/**
|
|
37
39
|
* Unpack the tarball in a vlt cache entry file into `target`. The
|
|
@@ -44,6 +46,8 @@ export type CacheEntryJob = {
|
|
|
44
46
|
file: string;
|
|
45
47
|
target: string;
|
|
46
48
|
format?: TarballFormat;
|
|
49
|
+
/** as in {@link UnpackJob} */
|
|
50
|
+
path?: string;
|
|
47
51
|
};
|
|
48
52
|
export type PlacementJob = UnpackJob | CacheEntryJob;
|
|
49
53
|
/**
|
package/dist/placement-worker.js
CHANGED
|
@@ -25,10 +25,10 @@ export const __CODE_SPLIT_SCRIPT_NAME = import.meta.filename;
|
|
|
25
25
|
export const handleJob = (port, job) => {
|
|
26
26
|
try {
|
|
27
27
|
if (job.kind === 'cacheEntry') {
|
|
28
|
-
unpackCacheEntrySync(job.file, job.target, job.format);
|
|
28
|
+
unpackCacheEntrySync(job.file, job.target, job.format, job.path);
|
|
29
29
|
}
|
|
30
30
|
else {
|
|
31
|
-
unpackSync(Buffer.from(job.buffer, job.byteOffset, job.byteLength), job.target, job.format);
|
|
31
|
+
unpackSync(Buffer.from(job.buffer, job.byteOffset, job.byteLength), job.target, job.format, job.path);
|
|
32
32
|
}
|
|
33
33
|
port.postMessage({ id: job.id, result: true });
|
|
34
34
|
}
|
package/dist/pool.d.ts
CHANGED
|
@@ -26,17 +26,19 @@ export declare class Pool {
|
|
|
26
26
|
#private;
|
|
27
27
|
/**
|
|
28
28
|
* Provide the tardata to be unpacked, and the location where it's to be
|
|
29
|
-
* placed. Resolves when the tarball has been extracted.
|
|
29
|
+
* placed. Resolves when the tarball has been extracted. `path`: only
|
|
30
|
+
* that dir of the tarball's root dir, see {@link unpack}. See
|
|
30
31
|
* {@link PlacementPool.unpack}.
|
|
31
32
|
*/
|
|
32
|
-
unpack(tarData: Buffer, target: string, format?: TarballFormat): Promise<void>;
|
|
33
|
+
unpack(tarData: Buffer, target: string, format?: TarballFormat, path?: string): Promise<void>;
|
|
33
34
|
/**
|
|
34
35
|
* Unpack the tarball in the vlt cache entry `file`, whose first 4
|
|
35
36
|
* bytes are the big-endian length of the head before the body. On a
|
|
36
37
|
* worker the file is read there, so the main thread never holds the
|
|
37
|
-
* body.
|
|
38
|
+
* body. `path` as in {@link unpack}. See
|
|
39
|
+
* {@link PlacementPool.unpackCacheEntry}.
|
|
38
40
|
*/
|
|
39
|
-
unpackCacheEntry(file: string, target: string, format?: TarballFormat): Promise<void>;
|
|
41
|
+
unpackCacheEntry(file: string, target: string, format?: TarballFormat, path?: string): Promise<void>;
|
|
40
42
|
/**
|
|
41
43
|
* Same, reading the tarball from a file and skipping `offset` leading
|
|
42
44
|
* bytes, so a cache entry is unpacked without loading it into the
|
package/dist/pool.js
CHANGED
|
@@ -45,29 +45,31 @@ export class Pool {
|
|
|
45
45
|
}
|
|
46
46
|
/**
|
|
47
47
|
* Provide the tardata to be unpacked, and the location where it's to be
|
|
48
|
-
* placed. Resolves when the tarball has been extracted.
|
|
48
|
+
* placed. Resolves when the tarball has been extracted. `path`: only
|
|
49
|
+
* that dir of the tarball's root dir, see {@link unpack}. See
|
|
49
50
|
* {@link PlacementPool.unpack}.
|
|
50
51
|
*/
|
|
51
|
-
async unpack(tarData, target, format) {
|
|
52
|
+
async unpack(tarData, target, format, path) {
|
|
52
53
|
if (mode === '0')
|
|
53
|
-
return unpack(tarData, target, format);
|
|
54
|
+
return unpack(tarData, target, format, path);
|
|
54
55
|
if (mode === '1')
|
|
55
|
-
return unpackSync(tarData, target, format);
|
|
56
|
-
return this.#pool.unpack(tarData, target, format);
|
|
56
|
+
return unpackSync(tarData, target, format, path);
|
|
57
|
+
return this.#pool.unpack(tarData, target, format, path);
|
|
57
58
|
}
|
|
58
59
|
/**
|
|
59
60
|
* Unpack the tarball in the vlt cache entry `file`, whose first 4
|
|
60
61
|
* bytes are the big-endian length of the head before the body. On a
|
|
61
62
|
* worker the file is read there, so the main thread never holds the
|
|
62
|
-
* body.
|
|
63
|
+
* body. `path` as in {@link unpack}. See
|
|
64
|
+
* {@link PlacementPool.unpackCacheEntry}.
|
|
63
65
|
*/
|
|
64
|
-
async unpackCacheEntry(file, target, format) {
|
|
66
|
+
async unpackCacheEntry(file, target, format, path) {
|
|
65
67
|
if (mode === '0') {
|
|
66
|
-
return unpack(cacheEntryBody(await readFile(file)), target, format);
|
|
68
|
+
return unpack(cacheEntryBody(await readFile(file)), target, format, path);
|
|
67
69
|
}
|
|
68
70
|
if (mode === '1')
|
|
69
|
-
return unpackCacheEntrySync(file, target, format);
|
|
70
|
-
return this.#pool.unpackCacheEntry(file, target, format);
|
|
71
|
+
return unpackCacheEntrySync(file, target, format, path);
|
|
72
|
+
return this.#pool.unpackCacheEntry(file, target, format, path);
|
|
71
73
|
}
|
|
72
74
|
/**
|
|
73
75
|
* Same, reading the tarball from a file and skipping `offset` leading
|
package/dist/unpack.d.ts
CHANGED
|
@@ -14,12 +14,16 @@ export declare const checkFs: (h: {
|
|
|
14
14
|
}, tarDir: string | undefined, target: string) => h is {
|
|
15
15
|
path: string;
|
|
16
16
|
};
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* `path`: only what is under that dir of the tarball's root dir, as
|
|
19
|
+
* the root (eg a package in a git archive of its monorepo).
|
|
20
|
+
*/
|
|
21
|
+
export declare const unpack: (tarData: Buffer, target: string, format?: TarballFormat, path?: string) => Promise<void>;
|
|
18
22
|
/**
|
|
19
23
|
* Same as {@link unpack}, but blocking. Faster: the async writers pay a
|
|
20
24
|
* libuv round trip per file, which costs more than the IO itself.
|
|
21
25
|
*/
|
|
22
|
-
export declare const unpackSync: (tarData: Buffer, target: string, format?: TarballFormat) => void;
|
|
26
|
+
export declare const unpackSync: (tarData: Buffer, target: string, format?: TarballFormat, path?: string) => void;
|
|
23
27
|
/**
|
|
24
28
|
* Inflate off the main thread, then write the files on it.
|
|
25
29
|
*
|
|
@@ -33,7 +37,7 @@ export declare const unpackSync: (tarData: Buffer, target: string, format?: Tarb
|
|
|
33
37
|
* giving up the fast writer. It matters most for brotli, whose inflate
|
|
34
38
|
* costs more CPU than gzip's, but it speeds up `.tgz` installs too.
|
|
35
39
|
*/
|
|
36
|
-
export declare const unpackParallel: (tarData: Buffer, target: string, format?: TarballFormat) => Promise<void>;
|
|
40
|
+
export declare const unpackParallel: (tarData: Buffer, target: string, format?: TarballFormat, path?: string) => Promise<void>;
|
|
37
41
|
/**
|
|
38
42
|
* {@link unpackParallel} reading the tarball from a file, so the read
|
|
39
43
|
* comes off the main thread as well. See {@link unpackFileSync} for
|
|
@@ -55,13 +59,13 @@ export declare const unpackFileSync: (file: string, target: string, offset?: num
|
|
|
55
59
|
export declare const cacheEntryBody: (entry: Buffer) => Buffer;
|
|
56
60
|
/**
|
|
57
61
|
* Unpack the tarball in the vlt cache entry `file`. See
|
|
58
|
-
* {@link cacheEntryBody}
|
|
62
|
+
* {@link cacheEntryBody}, and {@link unpack} for `path`.
|
|
59
63
|
*/
|
|
60
|
-
export declare const unpackCacheEntrySync: (file: string, target: string, format?: TarballFormat) => void;
|
|
64
|
+
export declare const unpackCacheEntrySync: (file: string, target: string, format?: TarballFormat, path?: string) => void;
|
|
61
65
|
/**
|
|
62
66
|
* Same, inflating on libuv's threadpool. See {@link unpackParallel}.
|
|
63
67
|
*/
|
|
64
|
-
export declare const unpackCacheEntryParallel: (file: string, target: string, format?: TarballFormat) => Promise<void>;
|
|
68
|
+
export declare const unpackCacheEntryParallel: (file: string, target: string, format?: TarballFormat, path?: string) => Promise<void>;
|
|
65
69
|
/** Sibling temp path used to build `target` before renaming it in. */
|
|
66
70
|
export declare const tmpName: (target: string) => string;
|
|
67
71
|
/**
|
package/dist/unpack.js
CHANGED
|
@@ -2,7 +2,7 @@ import { error } from '@vltpkg/error-cause';
|
|
|
2
2
|
import { randomBytes } from 'node:crypto';
|
|
3
3
|
import { chmodSync, lstatSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync, } from 'node:fs';
|
|
4
4
|
import { lstat, mkdir, readFile, rename, writeFile, } from 'node:fs/promises';
|
|
5
|
-
import { basename, dirname, isAbsolute, join, relative, resolve, sep, } from 'node:path';
|
|
5
|
+
import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep, } from 'node:path';
|
|
6
6
|
import { rimraf, rimrafSync } from 'rimraf';
|
|
7
7
|
import { Header } from 'tar/header';
|
|
8
8
|
import { Pax } from 'tar/pax';
|
|
@@ -198,14 +198,18 @@ const write = async (path, body, executable = false) => {
|
|
|
198
198
|
mode: executable ? 0o777 : 0o666,
|
|
199
199
|
});
|
|
200
200
|
};
|
|
201
|
-
|
|
202
|
-
|
|
201
|
+
/**
|
|
202
|
+
* `path`: only what is under that dir of the tarball's root dir, as
|
|
203
|
+
* the root (eg a package in a git archive of its monorepo).
|
|
204
|
+
*/
|
|
205
|
+
export const unpack = async (tarData, target, format, path) => {
|
|
206
|
+
await unpackUnzipped(await decompress(tarData, format), target, path);
|
|
203
207
|
};
|
|
204
208
|
/**
|
|
205
209
|
* Same as {@link unpack}, but blocking. Faster: the async writers pay a
|
|
206
210
|
* libuv round trip per file, which costs more than the IO itself.
|
|
207
211
|
*/
|
|
208
|
-
export const unpackSync = (tarData, target, format) => {
|
|
212
|
+
export const unpackSync = (tarData, target, format, path) => {
|
|
209
213
|
const inflated = span('inflate', {
|
|
210
214
|
bytes: tarData.length,
|
|
211
215
|
sync: true,
|
|
@@ -213,7 +217,7 @@ export const unpackSync = (tarData, target, format) => {
|
|
|
213
217
|
const buffer = decompressSync(tarData, format);
|
|
214
218
|
inflated({ out: buffer.length });
|
|
215
219
|
const written = span('write', { bytes: buffer.length });
|
|
216
|
-
unpackUnzippedSync(buffer, target);
|
|
220
|
+
unpackUnzippedSync(buffer, target, path);
|
|
217
221
|
written();
|
|
218
222
|
};
|
|
219
223
|
/**
|
|
@@ -229,12 +233,12 @@ export const unpackSync = (tarData, target, format) => {
|
|
|
229
233
|
* giving up the fast writer. It matters most for brotli, whose inflate
|
|
230
234
|
* costs more CPU than gzip's, but it speeds up `.tgz` installs too.
|
|
231
235
|
*/
|
|
232
|
-
export const unpackParallel = async (tarData, target, format) => {
|
|
236
|
+
export const unpackParallel = async (tarData, target, format, path) => {
|
|
233
237
|
const inflated = span('inflate', { bytes: tarData.length });
|
|
234
238
|
const buffer = await decompress(tarData, format);
|
|
235
239
|
inflated({ out: buffer.length });
|
|
236
240
|
const written = span('write', { bytes: buffer.length });
|
|
237
|
-
unpackUnzippedSync(buffer, target);
|
|
241
|
+
unpackUnzippedSync(buffer, target, path);
|
|
238
242
|
written();
|
|
239
243
|
};
|
|
240
244
|
/**
|
|
@@ -264,20 +268,25 @@ export const cacheEntryBody = (entry) => {
|
|
|
264
268
|
};
|
|
265
269
|
/**
|
|
266
270
|
* Unpack the tarball in the vlt cache entry `file`. See
|
|
267
|
-
* {@link cacheEntryBody}
|
|
271
|
+
* {@link cacheEntryBody}, and {@link unpack} for `path`.
|
|
268
272
|
*/
|
|
269
|
-
export const unpackCacheEntrySync = (file, target, format) => unpackSync(cacheEntryBody(readFileSync(file)), target, format);
|
|
273
|
+
export const unpackCacheEntrySync = (file, target, format, path) => unpackSync(cacheEntryBody(readFileSync(file)), target, format, path);
|
|
270
274
|
/**
|
|
271
275
|
* Same, inflating on libuv's threadpool. See {@link unpackParallel}.
|
|
272
276
|
*/
|
|
273
|
-
export const unpackCacheEntryParallel = async (file, target, format) => unpackParallel(cacheEntryBody(await readFile(file)), target, format);
|
|
277
|
+
export const unpackCacheEntryParallel = async (file, target, format, path) => unpackParallel(cacheEntryBody(await readFile(file)), target, format, path);
|
|
274
278
|
/** Sibling temp path used to build `target` before renaming it in. */
|
|
275
279
|
export const tmpName = (target) => dirname(target) + sep + '.' + basename(target) + '.' + tmpSuffix();
|
|
280
|
+
// `path` as a tarDir suffix: `a/b/`, '' for the root
|
|
281
|
+
const subdir = (path) => {
|
|
282
|
+
const p = path && posix.normalize(`${path.replace(/\\/g, '/')}/`);
|
|
283
|
+
return !p || p === './' ? '' : p;
|
|
284
|
+
};
|
|
276
285
|
/**
|
|
277
286
|
* Walk the tar headers and collect what has to be written. No IO, so
|
|
278
287
|
* all writers share it and cannot drift on path sanitization.
|
|
279
288
|
*/
|
|
280
|
-
const parseTarball = (buffer, tmp) => {
|
|
289
|
+
const parseTarball = (buffer, tmp, path) => {
|
|
281
290
|
/* c8 ignore start */
|
|
282
291
|
if (isGzip(buffer)) {
|
|
283
292
|
throw error('still gzipped after unzipping', {
|
|
@@ -304,6 +313,11 @@ const parseTarball = (buffer, tmp) => {
|
|
|
304
313
|
}
|
|
305
314
|
}
|
|
306
315
|
const entries = new Map();
|
|
316
|
+
const sub = subdir(path);
|
|
317
|
+
const root = (p) => {
|
|
318
|
+
const d = findTarDir(p);
|
|
319
|
+
return d && d + sub;
|
|
320
|
+
};
|
|
307
321
|
let tarDir = undefined;
|
|
308
322
|
let offset = 0;
|
|
309
323
|
let h;
|
|
@@ -325,7 +339,7 @@ const parseTarball = (buffer, tmp) => {
|
|
|
325
339
|
switch (h.type) {
|
|
326
340
|
case 'File':
|
|
327
341
|
if (!tarDir)
|
|
328
|
-
tarDir =
|
|
342
|
+
tarDir = root(h.path);
|
|
329
343
|
/* c8 ignore next */
|
|
330
344
|
if (!tarDir)
|
|
331
345
|
continue;
|
|
@@ -352,7 +366,7 @@ const parseTarball = (buffer, tmp) => {
|
|
|
352
366
|
case 'Directory':
|
|
353
367
|
/* c8 ignore next 2 */
|
|
354
368
|
if (!tarDir)
|
|
355
|
-
tarDir =
|
|
369
|
+
tarDir = root(h.path);
|
|
356
370
|
if (!tarDir)
|
|
357
371
|
continue;
|
|
358
372
|
if (!checkFs(h, tarDir, tmp))
|
|
@@ -385,6 +399,9 @@ const parseTarball = (buffer, tmp) => {
|
|
|
385
399
|
break;
|
|
386
400
|
}
|
|
387
401
|
}
|
|
402
|
+
if (sub && !entries.size) {
|
|
403
|
+
throw error('path not found in tarball', { path });
|
|
404
|
+
}
|
|
388
405
|
// Per-unpack memo: paths are tmp-scoped and never reused across
|
|
389
406
|
// unpacks. The unique dir set is the memo; making/made globals
|
|
390
407
|
// previously leaked ~18k strings per install.
|
|
@@ -400,12 +417,12 @@ const parseTarball = (buffer, tmp) => {
|
|
|
400
417
|
}
|
|
401
418
|
return { dirs, files };
|
|
402
419
|
};
|
|
403
|
-
const unpackUnzipped = async (buffer, target) => {
|
|
420
|
+
const unpackUnzipped = async (buffer, target, path) => {
|
|
404
421
|
const tmp = tmpName(target);
|
|
405
422
|
const og = tmp + '.ORIGINAL';
|
|
406
423
|
let succeeded = false;
|
|
407
424
|
try {
|
|
408
|
-
const { dirs, files } = parseTarball(buffer, tmp);
|
|
425
|
+
const { dirs, files } = parseTarball(buffer, tmp, path);
|
|
409
426
|
rethrowFirst(await Promise.allSettled([...dirs].map(d => mkdir(d, { recursive: true, mode: 0o777 }))));
|
|
410
427
|
rethrowFirst(await Promise.allSettled(files.map(f => withWriteLane(() => write(f.path, f.body, f.executable)))));
|
|
411
428
|
const targetExists = await exists(target);
|
|
@@ -430,12 +447,12 @@ const unpackUnzipped = async (buffer, target) => {
|
|
|
430
447
|
}
|
|
431
448
|
}
|
|
432
449
|
};
|
|
433
|
-
const unpackUnzippedSync = (buffer, target) => {
|
|
450
|
+
const unpackUnzippedSync = (buffer, target, path) => {
|
|
434
451
|
const tmp = tmpName(target);
|
|
435
452
|
const og = tmp + '.ORIGINAL';
|
|
436
453
|
let succeeded = false;
|
|
437
454
|
try {
|
|
438
|
-
const { dirs, files } = parseTarball(buffer, tmp);
|
|
455
|
+
const { dirs, files } = parseTarball(buffer, tmp, path);
|
|
439
456
|
for (const d of dirs) {
|
|
440
457
|
mkdirSync(d, { recursive: true, mode: 0o777 });
|
|
441
458
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vltpkg/tar",
|
|
3
3
|
"description": "An extremely limited and very fast tar extractor",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.8.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/vltpkg/vltpkg.git",
|
|
@@ -13,24 +13,24 @@
|
|
|
13
13
|
"url": "http://vlt.sh"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@vltpkg/error-cause": "1.
|
|
17
|
-
"@vltpkg/output": "1.
|
|
18
|
-
"@vltpkg/types": "1.
|
|
16
|
+
"@vltpkg/error-cause": "1.8.0",
|
|
17
|
+
"@vltpkg/output": "1.8.0",
|
|
18
|
+
"@vltpkg/types": "1.8.0",
|
|
19
19
|
"rimraf": "^6.1.2",
|
|
20
20
|
"tar": "^7.5.2"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@eslint/js": "^
|
|
23
|
+
"@eslint/js": "^10.0.1",
|
|
24
24
|
"@types/node": "^22.19.2",
|
|
25
25
|
"@types/pacote": "^11.1.8",
|
|
26
26
|
"@vltpkg/benchmark": "0.0.0",
|
|
27
|
-
"eslint": "^
|
|
27
|
+
"eslint": "^10.14.0",
|
|
28
28
|
"pacote": "^21.0.4",
|
|
29
29
|
"prettier": "^3.7.4",
|
|
30
30
|
"tap": "^21.8.0",
|
|
31
31
|
"typedoc": "~0.27.9",
|
|
32
32
|
"typescript": "5.7.3",
|
|
33
|
-
"typescript-eslint": "^8.
|
|
33
|
+
"typescript-eslint": "^8.56.0"
|
|
34
34
|
},
|
|
35
35
|
"license": "BSD-2-Clause-Patent",
|
|
36
36
|
"engines": {
|
|
@@ -66,6 +66,11 @@
|
|
|
66
66
|
"default": "./dist/clonefile.js"
|
|
67
67
|
}
|
|
68
68
|
},
|
|
69
|
+
"./git-archive-commit": {
|
|
70
|
+
"import": {
|
|
71
|
+
"default": "./dist/git-archive-commit.js"
|
|
72
|
+
}
|
|
73
|
+
},
|
|
69
74
|
"./link-tree": {
|
|
70
75
|
"import": {
|
|
71
76
|
"default": "./dist/link-tree.js"
|