@vltpkg/tar 1.1.1 → 1.3.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 +84 -2
- package/dist/index.d.ts +6 -1
- package/dist/index.js +4 -1
- package/dist/link-tree.d.ts +32 -0
- package/dist/link-tree.js +163 -0
- package/dist/pool.d.ts +25 -6
- package/dist/pool.js +51 -15
- package/dist/store-entry.d.ts +44 -0
- package/dist/store-entry.js +146 -0
- package/dist/store-index.d.ts +43 -0
- package/dist/store-index.js +86 -0
- package/dist/unpack.d.ts +59 -3
- package/dist/unpack.js +148 -16
- package/package.json +19 -3
package/README.md
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
A library for unpacking JavaScript package tarballs (gzip-compressed
|
|
6
6
|
or raw) into a specified folder.
|
|
7
7
|
|
|
8
|
-
**[Usage](#usage)** · **[
|
|
8
|
+
**[Usage](#usage)** · **[Global store](#global-store)** ·
|
|
9
|
+
**[Caveats](#caveats)**
|
|
9
10
|
|
|
10
11
|
## Overview
|
|
11
12
|
|
|
@@ -50,7 +51,7 @@ straight out of a cache entry whose file starts with a header block.
|
|
|
50
51
|
|
|
51
52
|
### `class Pool`
|
|
52
53
|
|
|
53
|
-
The interface the vlt CLI extracts through.
|
|
54
|
+
The interface the vlt CLI extracts through. All methods are `async`
|
|
54
55
|
for the caller's convenience; the work itself is synchronous and runs
|
|
55
56
|
on the main thread.
|
|
56
57
|
|
|
@@ -65,6 +66,87 @@ Unpack the supplied Buffer of data into the target folder.
|
|
|
65
66
|
|
|
66
67
|
Unpack a tarball read from `file`, skipping `offset` leading bytes.
|
|
67
68
|
|
|
69
|
+
#### `pool.linkFromStore(storeEntry, target, opts?) => Promise<{ how, index } | false>`
|
|
70
|
+
|
|
71
|
+
#### `pool.unpackToStore(tarData, dir) => Promise<{ index }>`
|
|
72
|
+
|
|
73
|
+
See [Global store](#global-store).
|
|
74
|
+
|
|
75
|
+
## Global store
|
|
76
|
+
|
|
77
|
+
A global store entry is a tarball exploded once into a directory, with
|
|
78
|
+
a sidecar index at `<entry>.json` (`StoreIndex`). Packages are then
|
|
79
|
+
materialized by hardlinking files instead of unpacking the tarball
|
|
80
|
+
again. Publishing entries atomically (sidecar, then rename the dir
|
|
81
|
+
into place) is up to the caller.
|
|
82
|
+
|
|
83
|
+
The vlt CLI keeps its global store under `<cache>/store/v1`, filled by
|
|
84
|
+
a background process, and installs from it with the `store-linker`
|
|
85
|
+
config (`auto` by default, `hardlink`, `copy`; `unpack` skips it).
|
|
86
|
+
`auto` only links on Linux and means `unpack` everywhere else.
|
|
87
|
+
|
|
88
|
+
### unpackToStoreSync(tarData, dir)
|
|
89
|
+
|
|
90
|
+
Explode a gzipped or raw tarball into `dir` (must not exist) and
|
|
91
|
+
return `{ index }`. Same parsing, path safety and modes as
|
|
92
|
+
`unpackSync`; package.json `bin` targets also get every exec bit, so
|
|
93
|
+
nothing has to chmod them later. Throws, writing nothing, without a
|
|
94
|
+
valid package.json.
|
|
95
|
+
|
|
96
|
+
The index has files (`[path, size, exec]`, sorted), every directory
|
|
97
|
+
(shortest first), `scripts` (install scripts or a root `binding.gyp`),
|
|
98
|
+
normalized `bins`, `name`, `version` and `manifest` (package.json as
|
|
99
|
+
compact JSON text, if valid; lets reify skip reading it back).
|
|
100
|
+
|
|
101
|
+
### linkFromStore(storeEntry, target, { copy })
|
|
102
|
+
|
|
103
|
+
Hardlink every file of a store entry into a sibling temp dir
|
|
104
|
+
(package.json last), then rename it to `target`. Returns
|
|
105
|
+
`{ how, index }`: `how` is `'link'`, or `'copy'` when files were
|
|
106
|
+
copied instead (see below). Returns `false`, creating nothing, if the
|
|
107
|
+
sidecar is missing or invalid, the entry is not a directory, or the
|
|
108
|
+
target's parent is a symlink. An entry with a missing file is removed
|
|
109
|
+
and `false` returned. Two index paths that collide on a
|
|
110
|
+
case-insensitive target (`EEXIST`) also return `false`.
|
|
111
|
+
|
|
112
|
+
`EXDEV`, `EPERM`, `EACCES` and `ENOTSUP` switch the process to copying
|
|
113
|
+
(read + write into a fresh file); `ENOENT` while both the source and
|
|
114
|
+
the target's parent exist (overlayfs cross-layer links) does the same.
|
|
115
|
+
`EMLINK` copies that file only. Other link errors throw. Every file is
|
|
116
|
+
copied with `copy: true` or when the index has `scripts`, so install
|
|
117
|
+
scripts never write into the store. A copy creates the entry's copied
|
|
118
|
+
marker (`<entry>.copied`), once.
|
|
119
|
+
|
|
120
|
+
`VLT_STORE_VERIFY=1` checks the linked package.json size against the
|
|
121
|
+
index and removes the entry on a mismatch (debugging aid).
|
|
122
|
+
|
|
123
|
+
### readStoreIndex(storeEntry)
|
|
124
|
+
|
|
125
|
+
The sidecar index, or `undefined` if missing or invalid.
|
|
126
|
+
|
|
127
|
+
### storeIndexPath(storeEntry)
|
|
128
|
+
|
|
129
|
+
Sidecar path: `<storeEntry>.json`.
|
|
130
|
+
|
|
131
|
+
### Maintenance
|
|
132
|
+
|
|
133
|
+
- `storeEntryNames(root)`: integrity hex names of the entries under a
|
|
134
|
+
store root (entry dir, sidecar or copied marker).
|
|
135
|
+
- `verifyStoreEntry(storeEntry, tarData)`: compare an entry with its
|
|
136
|
+
tarball (sidecar, file list, bytes). Returns why it differs, or
|
|
137
|
+
`undefined`.
|
|
138
|
+
- `storeEntryLinked(storeEntry, index?)`: true if any file has another
|
|
139
|
+
hardlink, i.e. some `node_modules` uses it.
|
|
140
|
+
- `storeEntryCopied(storeEntry, index?)`: true if the entry has a
|
|
141
|
+
copied marker or install scripts (always copied), i.e. nlink cannot
|
|
142
|
+
show use.
|
|
143
|
+
- `storeCopiedPath(storeEntry)`, `markStoreEntryCopied(storeEntry)`:
|
|
144
|
+
the copied marker, and its exclusive create.
|
|
145
|
+
- `storeEntryTime(storeEntry)`: sidecar mtime in ms (dir mtime without
|
|
146
|
+
one, else 0).
|
|
147
|
+
- `removeStoreEntry(storeEntry)`: remove the dir, then the sidecar and
|
|
148
|
+
copied marker.
|
|
149
|
+
|
|
68
150
|
## Caveats
|
|
69
151
|
|
|
70
152
|
As stated above, **this is not a general purpose tar implementation**.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
export { unpack } from './unpack.ts';
|
|
1
|
+
export { unpack, unpackToStoreSync } from './unpack.ts';
|
|
2
|
+
export type { TarballFormat } from './unpack.ts';
|
|
3
|
+
export * from './link-tree.ts';
|
|
2
4
|
export * from './pool.ts';
|
|
5
|
+
export * from './store-entry.ts';
|
|
6
|
+
export { readStoreIndex, storeIndexPath } from './store-index.ts';
|
|
7
|
+
export type { StoreIndex, StoreIndexFile } from './store-index.ts';
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { StoreIndex } from './store-index.ts';
|
|
2
|
+
/**
|
|
3
|
+
* How reify places packages: `auto` and `hardlink` link from the global
|
|
4
|
+
* store, `copy` copies from it, `unpack` skips it.
|
|
5
|
+
*/
|
|
6
|
+
export type StoreLinker = 'auto' | 'hardlink' | 'copy' | 'unpack';
|
|
7
|
+
/**
|
|
8
|
+
* How a store entry was placed (`copy`: every file copied, for install
|
|
9
|
+
* scripts, `copy` or a downgrade) and its index, or false on a miss.
|
|
10
|
+
*/
|
|
11
|
+
export type StoreLinkResult = {
|
|
12
|
+
how: 'link' | 'copy';
|
|
13
|
+
index: StoreIndex;
|
|
14
|
+
} | false;
|
|
15
|
+
export type LinkFromStoreOptions = {
|
|
16
|
+
/**
|
|
17
|
+
* copy every file instead of hardlinking. Implied when the index says
|
|
18
|
+
* the package runs install scripts, which must not write into the store.
|
|
19
|
+
*/
|
|
20
|
+
copy?: boolean;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Materialize a global store entry at `target` from its sidecar index:
|
|
24
|
+
* hardlink each file into a sibling temp dir (package.json last), then
|
|
25
|
+
* rename it into place. Files that cannot be linked are copied, as is
|
|
26
|
+
* every file of a package with install scripts. Returns how, with the
|
|
27
|
+
* index, or false, leaving `target` untouched, on a store miss (no
|
|
28
|
+
* valid index, entry not a directory, symlinked target parent), a name
|
|
29
|
+
* clash on a case-insensitive target, or a damaged entry, which is
|
|
30
|
+
* removed.
|
|
31
|
+
*/
|
|
32
|
+
export declare const linkFromStore: (storeEntry: string, target: string, { copy }?: LinkFromStoreOptions) => StoreLinkResult;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { chmodSync, linkSync, lstatSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync, } from 'node:fs';
|
|
2
|
+
import { dirname, sep } from 'node:path';
|
|
3
|
+
import { debuglog } from 'node:util';
|
|
4
|
+
import { rimrafSync } from 'rimraf';
|
|
5
|
+
import { markStoreEntryCopied, removeStoreEntry, } from "./store-entry.js";
|
|
6
|
+
import { readStoreIndex } from "./store-index.js";
|
|
7
|
+
import { tmpName } from "./unpack.js";
|
|
8
|
+
const debug = debuglog('vlt');
|
|
9
|
+
const noThrow = { throwIfNoEntry: false };
|
|
10
|
+
// debug spot-check: linked package.json size must match the index
|
|
11
|
+
const verify = process.env.VLT_STORE_VERIFY === '1';
|
|
12
|
+
// Process-wide: once links fail for a reason that will not go away
|
|
13
|
+
// (other device, no hardlink support, permissions), stop trying.
|
|
14
|
+
let copyAll = false;
|
|
15
|
+
const downgrade = (code) => {
|
|
16
|
+
copyAll = true;
|
|
17
|
+
debug('global store: linking failed, copying from now on', code);
|
|
18
|
+
};
|
|
19
|
+
// false: the source is gone, i.e. the store entry is damaged
|
|
20
|
+
const place = (src, dst, exec, copy) => {
|
|
21
|
+
if (!copy && !copyAll) {
|
|
22
|
+
try {
|
|
23
|
+
linkSync(src, dst);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
catch (er) {
|
|
27
|
+
const { code = '' } = er;
|
|
28
|
+
if (code === 'ENOENT') {
|
|
29
|
+
if (!lstatSync(src, noThrow))
|
|
30
|
+
return false;
|
|
31
|
+
// target side vanished (node_modules removed mid-install)
|
|
32
|
+
if (!lstatSync(dirname(dst), noThrow))
|
|
33
|
+
throw er;
|
|
34
|
+
// overlayfs can fail cross-layer links with ENOENT
|
|
35
|
+
downgrade(code);
|
|
36
|
+
}
|
|
37
|
+
else if (code === 'EXDEV' ||
|
|
38
|
+
code === 'EPERM' ||
|
|
39
|
+
code === 'EACCES' ||
|
|
40
|
+
code === 'ENOTSUP') {
|
|
41
|
+
downgrade(code);
|
|
42
|
+
}
|
|
43
|
+
else if (code !== 'EMLINK') {
|
|
44
|
+
throw er;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
let body;
|
|
49
|
+
try {
|
|
50
|
+
body = readFileSync(src);
|
|
51
|
+
}
|
|
52
|
+
catch (er) {
|
|
53
|
+
if (er.code === 'ENOENT')
|
|
54
|
+
return false;
|
|
55
|
+
throw er;
|
|
56
|
+
}
|
|
57
|
+
// `wx`: only ever a fresh file, never truncate what may be a link
|
|
58
|
+
writeFileSync(dst, body, { mode: exec ? 0o777 : 0o666, flag: 'wx' });
|
|
59
|
+
// store bins have every exec bit, whatever the umask
|
|
60
|
+
if (exec)
|
|
61
|
+
chmodSync(dst, statSync(src).mode & 0o777);
|
|
62
|
+
return true;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Fill `tmp` from the entry. 'damaged': a source file is gone (or,
|
|
66
|
+
* with VLT_STORE_VERIFY=1, package.json changed size). 'clash': two
|
|
67
|
+
* index paths map to one name on a case-insensitive target.
|
|
68
|
+
*/
|
|
69
|
+
const fill = (storeEntry, tmp, index, copy) => {
|
|
70
|
+
// Index paths are validated relative '/'-paths: concatenation is
|
|
71
|
+
// safe and much cheaper than join() on this per-file hot path.
|
|
72
|
+
const native = sep === '/' ?
|
|
73
|
+
(p) => p
|
|
74
|
+
: (p) => p.replaceAll('/', sep);
|
|
75
|
+
const put = ([p, , exec]) => place(storeEntry + sep + native(p), tmp + sep + native(p), exec, copy);
|
|
76
|
+
try {
|
|
77
|
+
for (const d of index.dirs)
|
|
78
|
+
mkdirSync(tmp + sep + native(d));
|
|
79
|
+
let pj;
|
|
80
|
+
for (const f of index.files) {
|
|
81
|
+
if (f[0] === 'package.json')
|
|
82
|
+
pj = f;
|
|
83
|
+
else if (!put(f))
|
|
84
|
+
return 'damaged';
|
|
85
|
+
}
|
|
86
|
+
if (!pj)
|
|
87
|
+
return;
|
|
88
|
+
// last, so an interrupted tmp dir never looks complete
|
|
89
|
+
if (!put(pj))
|
|
90
|
+
return 'damaged';
|
|
91
|
+
if (verify && statSync(tmp + sep + pj[0]).size !== pj[1]) {
|
|
92
|
+
return 'damaged';
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
catch (er) {
|
|
96
|
+
if (er.code === 'EEXIST')
|
|
97
|
+
return 'clash';
|
|
98
|
+
throw er;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Materialize a global store entry at `target` from its sidecar index:
|
|
103
|
+
* hardlink each file into a sibling temp dir (package.json last), then
|
|
104
|
+
* rename it into place. Files that cannot be linked are copied, as is
|
|
105
|
+
* every file of a package with install scripts. Returns how, with the
|
|
106
|
+
* index, or false, leaving `target` untouched, on a store miss (no
|
|
107
|
+
* valid index, entry not a directory, symlinked target parent), a name
|
|
108
|
+
* clash on a case-insensitive target, or a damaged entry, which is
|
|
109
|
+
* removed.
|
|
110
|
+
*/
|
|
111
|
+
export const linkFromStore = (storeEntry, target, { copy = false } = {}) => {
|
|
112
|
+
const index = readStoreIndex(storeEntry);
|
|
113
|
+
if (!index || !lstatSync(storeEntry, noThrow)?.isDirectory()) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
const parent = lstatSync(dirname(target), noThrow);
|
|
117
|
+
if (parent?.isSymbolicLink())
|
|
118
|
+
return false;
|
|
119
|
+
if (!parent)
|
|
120
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
121
|
+
const tmp = tmpName(target);
|
|
122
|
+
const og = tmp + '.ORIGINAL';
|
|
123
|
+
let succeeded = false;
|
|
124
|
+
try {
|
|
125
|
+
mkdirSync(tmp);
|
|
126
|
+
const copied = copy || index.scripts;
|
|
127
|
+
const miss = fill(storeEntry, tmp, index, copied);
|
|
128
|
+
if (miss === 'clash') {
|
|
129
|
+
debug('global store: name clash in target', storeEntry);
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
if (miss) {
|
|
133
|
+
debug('global store: removing damaged entry', storeEntry);
|
|
134
|
+
try {
|
|
135
|
+
removeStoreEntry(storeEntry);
|
|
136
|
+
}
|
|
137
|
+
catch { }
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
const targetExists = !!lstatSync(target, noThrow);
|
|
141
|
+
if (targetExists)
|
|
142
|
+
renameSync(target, og);
|
|
143
|
+
renameSync(tmp, target);
|
|
144
|
+
if (targetExists)
|
|
145
|
+
rimrafSync(og);
|
|
146
|
+
// nlink stays 1: tell prune-store it is used
|
|
147
|
+
if (copied || copyAll)
|
|
148
|
+
markStoreEntryCopied(storeEntry);
|
|
149
|
+
succeeded = true;
|
|
150
|
+
return { how: copied || copyAll ? 'copy' : 'link', index };
|
|
151
|
+
}
|
|
152
|
+
finally {
|
|
153
|
+
if (!succeeded) {
|
|
154
|
+
/* c8 ignore start */
|
|
155
|
+
if (lstatSync(og, noThrow)) {
|
|
156
|
+
rimrafSync(target);
|
|
157
|
+
renameSync(og, target);
|
|
158
|
+
}
|
|
159
|
+
/* c8 ignore stop */
|
|
160
|
+
rimrafSync(tmp);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
package/dist/pool.d.ts
CHANGED
|
@@ -1,25 +1,44 @@
|
|
|
1
|
+
import type { LinkFromStoreOptions, StoreLinkResult } from './link-tree.ts';
|
|
2
|
+
import type { StoreIndex } from './store-index.ts';
|
|
3
|
+
import type { TarballFormat } from '@vltpkg/types';
|
|
1
4
|
/**
|
|
2
5
|
* Unpacks tarballs into place.
|
|
3
6
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
+
* Decompression runs on libuv's threadpool, so packages inflate in
|
|
8
|
+
* parallel; the files are then written synchronously on the main
|
|
9
|
+
* thread, which is measurably faster than the async writers (the libuv
|
|
10
|
+
* round trip per file costs more than the IO). `VLT_TAR_SYNC` switches
|
|
11
|
+
* that pairing; see the constant above.
|
|
7
12
|
*
|
|
8
13
|
* There is no queue here. The only limiter is the caller's: reify caps
|
|
9
14
|
* extraction at `Math.max(availableParallelism() - 1, 1) * 8` in flight
|
|
10
15
|
* (`@vltpkg/graph`, `src/reify/index.ts`). A consumer that is not reify
|
|
11
|
-
* is unbounded -- add your own cap.
|
|
16
|
+
* is unbounded -- add your own cap. How many inflate at once is capped
|
|
17
|
+
* lower and elsewhere, by `UV_THREADPOOL_SIZE` (4 by default).
|
|
12
18
|
*/
|
|
13
19
|
export declare class Pool {
|
|
14
20
|
/**
|
|
15
21
|
* Provide the tardata to be unpacked, and the location where it's to be
|
|
16
22
|
* placed. Resolves when the tarball has been extracted.
|
|
17
23
|
*/
|
|
18
|
-
unpack(tarData: Buffer, target: string): Promise<void>;
|
|
24
|
+
unpack(tarData: Buffer, target: string, format?: TarballFormat): Promise<void>;
|
|
19
25
|
/**
|
|
20
26
|
* Same, reading the tarball from a file and skipping `offset` leading
|
|
21
27
|
* bytes, so a cache entry is unpacked without loading it into the
|
|
22
28
|
* registry client's in-memory cache.
|
|
23
29
|
*/
|
|
24
|
-
unpackFile(file: string, target: string, offset?: number): Promise<void>;
|
|
30
|
+
unpackFile(file: string, target: string, offset?: number, format?: TarballFormat): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Hardlink (or copy) a global store entry into `target`. Resolves
|
|
33
|
+
* how, with the index, or false on a store miss. See
|
|
34
|
+
* {@link linkFromStore}.
|
|
35
|
+
*/
|
|
36
|
+
linkFromStore(storeEntry: string, target: string, opts?: LinkFromStoreOptions): Promise<StoreLinkResult>;
|
|
37
|
+
/**
|
|
38
|
+
* Explode a tarball into `dir` for the global store. See
|
|
39
|
+
* {@link unpackToStoreSync}.
|
|
40
|
+
*/
|
|
41
|
+
unpackToStore(tarData: Buffer, dir: string, format?: TarballFormat): Promise<{
|
|
42
|
+
index: StoreIndex;
|
|
43
|
+
}>;
|
|
25
44
|
}
|
package/dist/pool.js
CHANGED
|
@@ -1,38 +1,74 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { linkFromStore } from "./link-tree.js";
|
|
3
|
+
import { unpack, unpackFileParallel, unpackFileSync, unpackParallel, unpackSync, unpackToStoreSync, } from "./unpack.js";
|
|
4
|
+
/**
|
|
5
|
+
* Which decompress/write pair {@link Pool} uses, from `VLT_TAR_SYNC`.
|
|
6
|
+
* Both explicit values are field kill switches: either restores an
|
|
7
|
+
* older behaviour without a release.
|
|
8
|
+
*
|
|
9
|
+
* - unset (default): inflate on libuv's threadpool, write on the main
|
|
10
|
+
* thread. Each half is the faster of its two options, and nothing
|
|
11
|
+
* ties them together.
|
|
12
|
+
* - `1`: inflate and write on the main thread -- the pre-parallel
|
|
13
|
+
* default, for a host where the threadpool hop costs more than the
|
|
14
|
+
* overlap saves.
|
|
15
|
+
* - `0`: the fully async writers, off the main thread throughout.
|
|
16
|
+
*/
|
|
17
|
+
const mode = process.env.VLT_TAR_SYNC;
|
|
5
18
|
/**
|
|
6
19
|
* Unpacks tarballs into place.
|
|
7
20
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
21
|
+
* Decompression runs on libuv's threadpool, so packages inflate in
|
|
22
|
+
* parallel; the files are then written synchronously on the main
|
|
23
|
+
* thread, which is measurably faster than the async writers (the libuv
|
|
24
|
+
* round trip per file costs more than the IO). `VLT_TAR_SYNC` switches
|
|
25
|
+
* that pairing; see the constant above.
|
|
11
26
|
*
|
|
12
27
|
* There is no queue here. The only limiter is the caller's: reify caps
|
|
13
28
|
* extraction at `Math.max(availableParallelism() - 1, 1) * 8` in flight
|
|
14
29
|
* (`@vltpkg/graph`, `src/reify/index.ts`). A consumer that is not reify
|
|
15
|
-
* is unbounded -- add your own cap.
|
|
30
|
+
* is unbounded -- add your own cap. How many inflate at once is capped
|
|
31
|
+
* lower and elsewhere, by `UV_THREADPOOL_SIZE` (4 by default).
|
|
16
32
|
*/
|
|
17
33
|
export class Pool {
|
|
18
34
|
/**
|
|
19
35
|
* Provide the tardata to be unpacked, and the location where it's to be
|
|
20
36
|
* placed. Resolves when the tarball has been extracted.
|
|
21
37
|
*/
|
|
22
|
-
async unpack(tarData, target) {
|
|
23
|
-
if (
|
|
24
|
-
return unpack(tarData, target);
|
|
25
|
-
|
|
38
|
+
async unpack(tarData, target, format) {
|
|
39
|
+
if (mode === '0')
|
|
40
|
+
return unpack(tarData, target, format);
|
|
41
|
+
if (mode === '1')
|
|
42
|
+
return unpackSync(tarData, target, format);
|
|
43
|
+
return unpackParallel(tarData, target, format);
|
|
26
44
|
}
|
|
27
45
|
/**
|
|
28
46
|
* Same, reading the tarball from a file and skipping `offset` leading
|
|
29
47
|
* bytes, so a cache entry is unpacked without loading it into the
|
|
30
48
|
* registry client's in-memory cache.
|
|
31
49
|
*/
|
|
32
|
-
async unpackFile(file, target, offset = 0) {
|
|
33
|
-
if (
|
|
34
|
-
return unpack((await readFile(file)).subarray(offset), target);
|
|
50
|
+
async unpackFile(file, target, offset = 0, format) {
|
|
51
|
+
if (mode === '0') {
|
|
52
|
+
return unpack((await readFile(file)).subarray(offset), target, format);
|
|
35
53
|
}
|
|
36
|
-
|
|
54
|
+
if (mode === '1') {
|
|
55
|
+
return unpackFileSync(file, target, offset, format);
|
|
56
|
+
}
|
|
57
|
+
return unpackFileParallel(file, target, offset, format);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Hardlink (or copy) a global store entry into `target`. Resolves
|
|
61
|
+
* how, with the index, or false on a store miss. See
|
|
62
|
+
* {@link linkFromStore}.
|
|
63
|
+
*/
|
|
64
|
+
async linkFromStore(storeEntry, target, opts) {
|
|
65
|
+
return linkFromStore(storeEntry, target, opts);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Explode a tarball into `dir` for the global store. See
|
|
69
|
+
* {@link unpackToStoreSync}.
|
|
70
|
+
*/
|
|
71
|
+
async unpackToStore(tarData, dir, format) {
|
|
72
|
+
return unpackToStoreSync(tarData, dir, format);
|
|
37
73
|
}
|
|
38
74
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integrity hex names of the global store entries under `root`, each
|
|
3
|
+
* with an entry dir, a sidecar index or a copied marker. Sorted.
|
|
4
|
+
*/
|
|
5
|
+
export declare const storeEntryNames: (root: string) => string[];
|
|
6
|
+
/**
|
|
7
|
+
* Marker next to a global store entry: some install copied from it, so
|
|
8
|
+
* its files' nlink cannot show use. See {@link markStoreEntryCopied}.
|
|
9
|
+
*/
|
|
10
|
+
export declare const storeCopiedPath: (storeEntry: string) => string;
|
|
11
|
+
/**
|
|
12
|
+
* Mark a global store entry as copied from. Called on copies only:
|
|
13
|
+
* one exclusive create per entry, a failed open after that.
|
|
14
|
+
*/
|
|
15
|
+
export declare const markStoreEntryCopied: (storeEntry: string) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Remove a global store entry. Dir first: a sidecar without its dir is
|
|
18
|
+
* a plain miss. Projects linked from it keep their files.
|
|
19
|
+
*/
|
|
20
|
+
export declare const removeStoreEntry: (storeEntry: string) => void;
|
|
21
|
+
/**
|
|
22
|
+
* When a global store entry was written, in ms: its sidecar's mtime,
|
|
23
|
+
* else its dir's, else 0.
|
|
24
|
+
*/
|
|
25
|
+
export declare const storeEntryTime: (storeEntry: string) => number;
|
|
26
|
+
/**
|
|
27
|
+
* True if the global store entry has a valid sidecar and dir, and was
|
|
28
|
+
* copied from (marker) or has install scripts (always copied). `index`:
|
|
29
|
+
* the sidecar, if already read.
|
|
30
|
+
*/
|
|
31
|
+
export declare const storeEntryCopied: (storeEntry: string, index?: import("./store-index.ts").StoreIndex | undefined) => boolean;
|
|
32
|
+
/**
|
|
33
|
+
* True if a file of the global store entry has another hardlink
|
|
34
|
+
* (nlink > 1), i.e. some `node_modules` uses it. Without a valid
|
|
35
|
+
* sidecar nothing can link from it, so false. `index`: the sidecar, if
|
|
36
|
+
* already read.
|
|
37
|
+
*/
|
|
38
|
+
export declare const storeEntryLinked: (storeEntry: string, index?: import("./store-index.ts").StoreIndex | undefined) => boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Check a global store entry against the tarball it was exploded from:
|
|
41
|
+
* sidecar index, file list, every file's bytes and exec bit (not on
|
|
42
|
+
* Windows). Returns why it does not match, or undefined if it does.
|
|
43
|
+
*/
|
|
44
|
+
export declare const verifyStoreEntry: (storeEntry: string, tarData: Buffer) => string | undefined;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { closeSync, lstatSync, openSync, readdirSync, readFileSync, statSync, } from 'node:fs';
|
|
2
|
+
import { join, relative } from 'node:path';
|
|
3
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
4
|
+
import { rimrafSync } from 'rimraf';
|
|
5
|
+
import { readStoreIndex, storeIndexPath } from "./store-index.js";
|
|
6
|
+
import { storeLayout } from "./unpack.js";
|
|
7
|
+
const noThrow = { throwIfNoEntry: false };
|
|
8
|
+
const entryName = /^([0-9a-f]{128})(?:\.json|\.copied)?$/;
|
|
9
|
+
const relPath = (from, to) => relative(from, to).replace(/\\/g, '/');
|
|
10
|
+
/**
|
|
11
|
+
* Integrity hex names of the global store entries under `root`, each
|
|
12
|
+
* with an entry dir, a sidecar index or a copied marker. Sorted.
|
|
13
|
+
*/
|
|
14
|
+
export const storeEntryNames = (root) => {
|
|
15
|
+
let names;
|
|
16
|
+
try {
|
|
17
|
+
names = readdirSync(root);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
const hexes = new Set();
|
|
23
|
+
for (const name of names) {
|
|
24
|
+
const hex = entryName.exec(name)?.[1];
|
|
25
|
+
if (hex)
|
|
26
|
+
hexes.add(hex);
|
|
27
|
+
}
|
|
28
|
+
return [...hexes].sort();
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Marker next to a global store entry: some install copied from it, so
|
|
32
|
+
* its files' nlink cannot show use. See {@link markStoreEntryCopied}.
|
|
33
|
+
*/
|
|
34
|
+
export const storeCopiedPath = (storeEntry) => storeEntry + '.copied';
|
|
35
|
+
/**
|
|
36
|
+
* Mark a global store entry as copied from. Called on copies only:
|
|
37
|
+
* one exclusive create per entry, a failed open after that.
|
|
38
|
+
*/
|
|
39
|
+
export const markStoreEntryCopied = (storeEntry) => {
|
|
40
|
+
try {
|
|
41
|
+
closeSync(openSync(storeCopiedPath(storeEntry), 'wx'));
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// EEXIST: marked. Else prune-store may drop it: only a re-explode.
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Remove a global store entry. Dir first: a sidecar without its dir is
|
|
49
|
+
* a plain miss. Projects linked from it keep their files.
|
|
50
|
+
*/
|
|
51
|
+
export const removeStoreEntry = (storeEntry) => {
|
|
52
|
+
rimrafSync(storeEntry);
|
|
53
|
+
rimrafSync(storeIndexPath(storeEntry));
|
|
54
|
+
rimrafSync(storeCopiedPath(storeEntry));
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* When a global store entry was written, in ms: its sidecar's mtime,
|
|
58
|
+
* else its dir's, else 0.
|
|
59
|
+
*/
|
|
60
|
+
export const storeEntryTime = (storeEntry) => (statSync(storeIndexPath(storeEntry), noThrow) ??
|
|
61
|
+
statSync(storeEntry, noThrow))?.mtimeMs ?? 0;
|
|
62
|
+
/**
|
|
63
|
+
* True if the global store entry has a valid sidecar and dir, and was
|
|
64
|
+
* copied from (marker) or has install scripts (always copied). `index`:
|
|
65
|
+
* the sidecar, if already read.
|
|
66
|
+
*/
|
|
67
|
+
export const storeEntryCopied = (storeEntry, index = readStoreIndex(storeEntry)) => !!index &&
|
|
68
|
+
(index.scripts ||
|
|
69
|
+
!!lstatSync(storeCopiedPath(storeEntry), noThrow)) &&
|
|
70
|
+
!!lstatSync(storeEntry, noThrow)?.isDirectory();
|
|
71
|
+
/**
|
|
72
|
+
* True if a file of the global store entry has another hardlink
|
|
73
|
+
* (nlink > 1), i.e. some `node_modules` uses it. Without a valid
|
|
74
|
+
* sidecar nothing can link from it, so false. `index`: the sidecar, if
|
|
75
|
+
* already read.
|
|
76
|
+
*/
|
|
77
|
+
export const storeEntryLinked = (storeEntry, index = readStoreIndex(storeEntry)) => !!index?.files.some(([p]) => (lstatSync(join(storeEntry, p), noThrow)?.nlink ?? 1) > 1);
|
|
78
|
+
// v1 fields; `manifest` only if present (older sidecars lack it);
|
|
79
|
+
// optional fields added later stay unchecked
|
|
80
|
+
const indexKeys = [
|
|
81
|
+
'v',
|
|
82
|
+
'files',
|
|
83
|
+
'dirs',
|
|
84
|
+
'scripts',
|
|
85
|
+
'bins',
|
|
86
|
+
'name',
|
|
87
|
+
'version',
|
|
88
|
+
];
|
|
89
|
+
/**
|
|
90
|
+
* Check a global store entry against the tarball it was exploded from:
|
|
91
|
+
* sidecar index, file list, every file's bytes and exec bit (not on
|
|
92
|
+
* Windows). Returns why it does not match, or undefined if it does.
|
|
93
|
+
*/
|
|
94
|
+
export const verifyStoreEntry = (storeEntry, tarData) => {
|
|
95
|
+
let layout;
|
|
96
|
+
try {
|
|
97
|
+
layout = storeLayout(tarData, storeEntry);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return 'bad tarball';
|
|
101
|
+
}
|
|
102
|
+
const index = readStoreIndex(storeEntry);
|
|
103
|
+
if (!index)
|
|
104
|
+
return 'no index';
|
|
105
|
+
if (indexKeys.some(k => !isDeepStrictEqual(index[k], layout.index[k])) ||
|
|
106
|
+
(index.manifest !== undefined &&
|
|
107
|
+
index.manifest !== layout.index.manifest)) {
|
|
108
|
+
return 'index differs';
|
|
109
|
+
}
|
|
110
|
+
const found = new Set();
|
|
111
|
+
try {
|
|
112
|
+
for (const d of readdirSync(storeEntry, {
|
|
113
|
+
recursive: true,
|
|
114
|
+
withFileTypes: true,
|
|
115
|
+
})) {
|
|
116
|
+
if (!d.isDirectory()) {
|
|
117
|
+
found.add(relPath(storeEntry, join(d.parentPath, d.name)));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
return 'missing';
|
|
123
|
+
}
|
|
124
|
+
const modes = process.platform !== 'win32';
|
|
125
|
+
for (const f of layout.files) {
|
|
126
|
+
const p = relPath(storeEntry, f.path);
|
|
127
|
+
if (!found.delete(p))
|
|
128
|
+
return `missing ${p}`;
|
|
129
|
+
let body;
|
|
130
|
+
let mode;
|
|
131
|
+
try {
|
|
132
|
+
mode = statSync(f.path).mode;
|
|
133
|
+
body = readFileSync(f.path);
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
return `unreadable ${p}`;
|
|
137
|
+
}
|
|
138
|
+
if (!body.equals(f.body))
|
|
139
|
+
return `modified ${p}`;
|
|
140
|
+
if (modes && ((mode & 0o111) !== 0) !== f.executable) {
|
|
141
|
+
return `mode ${p}`;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const [extra] = found;
|
|
145
|
+
return extra === undefined ? undefined : `extra ${extra}`;
|
|
146
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** `[path, size, exec]` of one file in a global store entry. */
|
|
2
|
+
export type StoreIndexFile = [path: string, size: number, exec: 0 | 1];
|
|
3
|
+
/**
|
|
4
|
+
* Sidecar index of a global store entry, stored at
|
|
5
|
+
* {@link storeIndexPath}. Lets a reader materialize the entry without
|
|
6
|
+
* walking it, and know before touching it whether the package runs
|
|
7
|
+
* install scripts.
|
|
8
|
+
*/
|
|
9
|
+
export type StoreIndex = {
|
|
10
|
+
v: 1;
|
|
11
|
+
/** relative, `/`-separated, sorted by path */
|
|
12
|
+
files: StoreIndexFile[];
|
|
13
|
+
/** every directory, shortest first */
|
|
14
|
+
dirs: string[];
|
|
15
|
+
/** has an install/preinstall/postinstall script or a root binding.gyp */
|
|
16
|
+
scripts: boolean;
|
|
17
|
+
/** normalized package.json `bin` */
|
|
18
|
+
bins?: Record<string, string>;
|
|
19
|
+
name?: string;
|
|
20
|
+
version?: string;
|
|
21
|
+
/**
|
|
22
|
+
* package.json as compact JSON text, if a valid manifest, so reify
|
|
23
|
+
* need not read it back from disk. Text, so a reader that does not
|
|
24
|
+
* need it skips the parse. Absent in entries written before it.
|
|
25
|
+
*/
|
|
26
|
+
manifest?: string;
|
|
27
|
+
};
|
|
28
|
+
/** Index fields read from the tarball's own package.json. */
|
|
29
|
+
export type StoreIndexManifest = Pick<StoreIndex, 'scripts' | 'bins' | 'name' | 'version' | 'manifest'>;
|
|
30
|
+
/** The sidecar is a sibling, so it is never linked with the tree. */
|
|
31
|
+
export declare const storeIndexPath: (storeEntry: string) => string;
|
|
32
|
+
/**
|
|
33
|
+
* Read the sidecar index of a global store entry. Missing, unparseable
|
|
34
|
+
* or malformed is a store miss (undefined), never an error.
|
|
35
|
+
*/
|
|
36
|
+
export declare const readStoreIndex: (storeEntry: string) => StoreIndex | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Index fields from a tarball's package.json. `scripts` uses the same
|
|
39
|
+
* predicate as reify's build check: an install lifecycle script, or a
|
|
40
|
+
* root binding.gyp (implicit `node-gyp rebuild`). Throws on anything
|
|
41
|
+
* but a JSON object, so such tarballs never become store entries.
|
|
42
|
+
*/
|
|
43
|
+
export declare const storeIndexManifest: (packageJson: Buffer, bindingGyp: boolean) => StoreIndexManifest;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { error } from '@vltpkg/error-cause';
|
|
2
|
+
import { isManifest, normalizeBinPaths } from '@vltpkg/types';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import { debuglog } from 'node:util';
|
|
5
|
+
const debug = debuglog('vlt');
|
|
6
|
+
/** The sidecar is a sibling, so it is never linked with the tree. */
|
|
7
|
+
export const storeIndexPath = (storeEntry) => storeEntry + '.json';
|
|
8
|
+
const isRecord = (x) => !!x && typeof x === 'object' && !Array.isArray(x);
|
|
9
|
+
const isOptString = (x) => x === undefined || typeof x === 'string';
|
|
10
|
+
// empty, `.` or `..` segment, or a backslash: could escape when joined
|
|
11
|
+
const unsafePath = /(?:^|\/)\.{0,2}(?:\/|$)|\\/;
|
|
12
|
+
const isRelPath = (p) => typeof p === 'string' && !unsafePath.test(p);
|
|
13
|
+
const isIndexFile = (f) => Array.isArray(f) &&
|
|
14
|
+
f.length === 3 &&
|
|
15
|
+
isRelPath(f[0]) &&
|
|
16
|
+
typeof f[1] === 'number' &&
|
|
17
|
+
(f[2] === 0 || f[2] === 1);
|
|
18
|
+
const isStoreIndex = (x) => isRecord(x) &&
|
|
19
|
+
x.v === 1 &&
|
|
20
|
+
typeof x.scripts === 'boolean' &&
|
|
21
|
+
isOptString(x.name) &&
|
|
22
|
+
isOptString(x.version) &&
|
|
23
|
+
(x.bins === undefined ||
|
|
24
|
+
(isRecord(x.bins) &&
|
|
25
|
+
Object.values(x.bins).every(b => typeof b === 'string'))) &&
|
|
26
|
+
Array.isArray(x.dirs) &&
|
|
27
|
+
x.dirs.every(isRelPath) &&
|
|
28
|
+
Array.isArray(x.files) &&
|
|
29
|
+
x.files.every(isIndexFile) &&
|
|
30
|
+
isOptString(x.manifest);
|
|
31
|
+
/**
|
|
32
|
+
* Read the sidecar index of a global store entry. Missing, unparseable
|
|
33
|
+
* or malformed is a store miss (undefined), never an error.
|
|
34
|
+
*/
|
|
35
|
+
export const readStoreIndex = (storeEntry) => {
|
|
36
|
+
let index;
|
|
37
|
+
try {
|
|
38
|
+
index = JSON.parse(readFileSync(storeIndexPath(storeEntry), 'utf8'));
|
|
39
|
+
}
|
|
40
|
+
catch (er) {
|
|
41
|
+
if (er.code !== 'ENOENT') {
|
|
42
|
+
debug('global store: invalid index', storeEntry);
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
if (isStoreIndex(index))
|
|
47
|
+
return index;
|
|
48
|
+
debug('global store: invalid index', storeEntry);
|
|
49
|
+
return undefined;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Index fields from a tarball's package.json. `scripts` uses the same
|
|
53
|
+
* predicate as reify's build check: an install lifecycle script, or a
|
|
54
|
+
* root binding.gyp (implicit `node-gyp rebuild`). Throws on anything
|
|
55
|
+
* but a JSON object, so such tarballs never become store entries.
|
|
56
|
+
*/
|
|
57
|
+
export const storeIndexManifest = (packageJson, bindingGyp) => {
|
|
58
|
+
let pkg;
|
|
59
|
+
try {
|
|
60
|
+
pkg = JSON.parse(packageJson.toString('utf8').replace(/^\uFEFF/, ''));
|
|
61
|
+
}
|
|
62
|
+
catch (cause) {
|
|
63
|
+
throw error('invalid package.json in tarball', { cause });
|
|
64
|
+
}
|
|
65
|
+
if (!isRecord(pkg)) {
|
|
66
|
+
throw error('invalid package.json in tarball', { found: pkg });
|
|
67
|
+
}
|
|
68
|
+
const { name, version, scripts, bin } = pkg;
|
|
69
|
+
const s = isRecord(scripts) ? scripts : {};
|
|
70
|
+
const result = {
|
|
71
|
+
scripts: !!(s.install || s.preinstall || s.postinstall) || bindingGyp,
|
|
72
|
+
};
|
|
73
|
+
const bins = normalizeBinPaths({
|
|
74
|
+
name: typeof name === 'string' ? name : undefined,
|
|
75
|
+
bin: bin,
|
|
76
|
+
});
|
|
77
|
+
if (bins && Object.keys(bins).length)
|
|
78
|
+
result.bins = bins;
|
|
79
|
+
if (typeof name === 'string')
|
|
80
|
+
result.name = name;
|
|
81
|
+
if (typeof version === 'string')
|
|
82
|
+
result.version = version;
|
|
83
|
+
if (isManifest(pkg))
|
|
84
|
+
result.manifest = JSON.stringify(pkg);
|
|
85
|
+
return result;
|
|
86
|
+
};
|
package/dist/unpack.d.ts
CHANGED
|
@@ -1,17 +1,73 @@
|
|
|
1
|
+
import type { TarballFormat } from '@vltpkg/types';
|
|
2
|
+
import type { StoreIndex } from './store-index.ts';
|
|
3
|
+
export type { TarballFormat };
|
|
4
|
+
/** A file parsed out of a tarball, at its destination path. */
|
|
5
|
+
export type FileEntry = {
|
|
6
|
+
path: string;
|
|
7
|
+
body: Buffer;
|
|
8
|
+
/** written with the exec bit: world-executable in the tar header */
|
|
9
|
+
executable: boolean;
|
|
10
|
+
dir: false;
|
|
11
|
+
};
|
|
1
12
|
export declare const checkFs: (h: {
|
|
2
13
|
path?: string;
|
|
3
14
|
}, tarDir: string | undefined, target: string) => h is {
|
|
4
15
|
path: string;
|
|
5
16
|
};
|
|
6
|
-
export declare const unpack: (tarData: Buffer, target: string) => Promise<void>;
|
|
17
|
+
export declare const unpack: (tarData: Buffer, target: string, format?: TarballFormat) => Promise<void>;
|
|
7
18
|
/**
|
|
8
19
|
* Same as {@link unpack}, but blocking. Faster: the async writers pay a
|
|
9
20
|
* libuv round trip per file, which costs more than the IO itself.
|
|
10
21
|
*/
|
|
11
|
-
export declare const unpackSync: (tarData: Buffer, target: string) => void;
|
|
22
|
+
export declare const unpackSync: (tarData: Buffer, target: string, format?: TarballFormat) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Inflate off the main thread, then write the files on it.
|
|
25
|
+
*
|
|
26
|
+
* zlib's async API hands the decompress to libuv's threadpool, so
|
|
27
|
+
* concurrent extractions inflate in parallel -- on `UV_THREADPOOL_SIZE`
|
|
28
|
+
* threads, 4 by default -- while the main thread stays free. The writes
|
|
29
|
+
* then take the sync path, which is faster than the async writers (a
|
|
30
|
+
* libuv round trip per file costs more than the IO itself).
|
|
31
|
+
*
|
|
32
|
+
* Splitting the two is the whole point: parallel decompression without
|
|
33
|
+
* giving up the fast writer. It matters most for brotli, whose inflate
|
|
34
|
+
* costs more CPU than gzip's, but it speeds up `.tgz` installs too.
|
|
35
|
+
*/
|
|
36
|
+
export declare const unpackParallel: (tarData: Buffer, target: string, format?: TarballFormat) => Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* {@link unpackParallel} reading the tarball from a file, so the read
|
|
39
|
+
* comes off the main thread as well. See {@link unpackFileSync} for
|
|
40
|
+
* what `offset` is for.
|
|
41
|
+
*/
|
|
42
|
+
export declare const unpackFileParallel: (file: string, target: string, offset?: number, format?: TarballFormat) => Promise<void>;
|
|
12
43
|
/**
|
|
13
44
|
* Unpack a tarball straight from a file on disk, skipping `offset`
|
|
14
45
|
* leading bytes. Used to extract from a cache entry in place, so the
|
|
15
46
|
* tarball never lands in the registry client's in-memory cache.
|
|
16
47
|
*/
|
|
17
|
-
export declare const unpackFileSync: (file: string, target: string, offset?: number) => void;
|
|
48
|
+
export declare const unpackFileSync: (file: string, target: string, offset?: number, format?: TarballFormat) => void;
|
|
49
|
+
/** Sibling temp path used to build `target` before renaming it in. */
|
|
50
|
+
export declare const tmpName: (target: string) => string;
|
|
51
|
+
/**
|
|
52
|
+
* What a tarball explodes to in the global store at `dir`: its sidecar
|
|
53
|
+
* index, files and package.json `bin` target paths. Same parsing and
|
|
54
|
+
* path safety as {@link unpackSync}, and the same `format` rule -- only
|
|
55
|
+
* brotli has to be declared. No IO. Throws if the tarball has no valid
|
|
56
|
+
* package.json.
|
|
57
|
+
*/
|
|
58
|
+
export declare const storeLayout: (tarData: Buffer, dir: string, format?: TarballFormat) => {
|
|
59
|
+
index: StoreIndex;
|
|
60
|
+
files: FileEntry[];
|
|
61
|
+
binFiles: string[];
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Explode a tarball into `dir` for the global store and
|
|
65
|
+
* return its sidecar index (see {@link storeLayout}), written straight
|
|
66
|
+
* into `dir` (which must not exist; the caller renames it into place).
|
|
67
|
+
* Same modes as {@link unpackSync} followed by reify's bin chmod.
|
|
68
|
+
* Throws, writing nothing, without a valid package.json. Removes `dir`
|
|
69
|
+
* on failure.
|
|
70
|
+
*/
|
|
71
|
+
export declare const unpackToStoreSync: (tarData: Buffer, dir: string, format?: TarballFormat) => {
|
|
72
|
+
index: StoreIndex;
|
|
73
|
+
};
|
package/dist/unpack.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { error } from '@vltpkg/error-cause';
|
|
2
2
|
import { randomBytes } from 'node:crypto';
|
|
3
|
-
import { lstatSync, mkdirSync, readFileSync, renameSync, writeFileSync, } from 'node:fs';
|
|
4
|
-
import { lstat, mkdir, rename, writeFile } from 'node:fs/promises';
|
|
5
|
-
import { basename, dirname, isAbsolute, relative, resolve, sep, } from 'node:path';
|
|
3
|
+
import { chmodSync, lstatSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync, } from 'node:fs';
|
|
4
|
+
import { lstat, mkdir, readFile, rename, writeFile, } from 'node:fs/promises';
|
|
5
|
+
import { basename, dirname, isAbsolute, join, 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';
|
|
9
|
-
import { unzip as unzipCB, unzipSync as unzipSyncCB } from 'node:zlib';
|
|
9
|
+
import { brotliDecompress as brotliCB, brotliDecompressSync as brotliSyncCB, unzip as unzipCB, unzipSync as unzipSyncCB, } from 'node:zlib';
|
|
10
10
|
import { findTarDir } from "./find-tar-dir.js";
|
|
11
|
+
import { storeIndexManifest } from "./store-index.js";
|
|
11
12
|
// Matches node-tar's MAX_DECOMPRESSION_RATIO, which npm uses via pacote.
|
|
12
13
|
const MAX_DECOMPRESSION_RATIO = 1000;
|
|
13
14
|
// node-tar streams, so its ratio alone bounds peak memory. We inflate into
|
|
@@ -42,6 +43,32 @@ const unzipSync = (input) => {
|
|
|
42
43
|
throw unzipError(er, input.length, max);
|
|
43
44
|
}
|
|
44
45
|
};
|
|
46
|
+
const isGzip = (b) => b[0] === 0x1f && b[1] === 0x8b;
|
|
47
|
+
const brotli = async (input) => {
|
|
48
|
+
const max = unzipMax(input.length);
|
|
49
|
+
return new Promise((res, rej) => brotliCB(input, { maxOutputLength: max }, (er, result) => er ? rej(unzipError(er, input.length, max)) : res(result)));
|
|
50
|
+
};
|
|
51
|
+
const brotliSync = (input) => {
|
|
52
|
+
const max = unzipMax(input.length);
|
|
53
|
+
try {
|
|
54
|
+
return brotliSyncCB(input, { maxOutputLength: max });
|
|
55
|
+
}
|
|
56
|
+
catch (er) {
|
|
57
|
+
throw unzipError(er, input.length, max);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Inflate a tarball body. `gzip` is sniffed from the magic bytes and
|
|
62
|
+
* anything unrecognized is taken for a raw tar, so only `brotli` has to
|
|
63
|
+
* be declared -- its bytes carry no signature of their own. Both paths
|
|
64
|
+
* share the decompression-ratio cap.
|
|
65
|
+
*/
|
|
66
|
+
const decompress = async (input, format) => format === 'brotli' ? brotli(input)
|
|
67
|
+
: isGzip(input) ? unzip(input)
|
|
68
|
+
: input;
|
|
69
|
+
const decompressSync = (input, format) => format === 'brotli' ? brotliSync(input)
|
|
70
|
+
: isGzip(input) ? unzipSync(input)
|
|
71
|
+
: input;
|
|
45
72
|
const exists = async (path) => {
|
|
46
73
|
try {
|
|
47
74
|
await lstat(path);
|
|
@@ -170,35 +197,55 @@ const write = async (path, body, executable = false) => {
|
|
|
170
197
|
mode: executable ? 0o777 : 0o666,
|
|
171
198
|
});
|
|
172
199
|
};
|
|
173
|
-
export const unpack = async (tarData, target) => {
|
|
174
|
-
|
|
175
|
-
await unpackUnzipped(isGzip ? await unzip(tarData) : tarData, target);
|
|
200
|
+
export const unpack = async (tarData, target, format) => {
|
|
201
|
+
await unpackUnzipped(await decompress(tarData, format), target);
|
|
176
202
|
};
|
|
177
203
|
/**
|
|
178
204
|
* Same as {@link unpack}, but blocking. Faster: the async writers pay a
|
|
179
205
|
* libuv round trip per file, which costs more than the IO itself.
|
|
180
206
|
*/
|
|
181
|
-
export const unpackSync = (tarData, target) => {
|
|
182
|
-
|
|
183
|
-
unpackUnzippedSync(isGzip ? unzipSync(tarData) : tarData, target);
|
|
207
|
+
export const unpackSync = (tarData, target, format) => {
|
|
208
|
+
unpackUnzippedSync(decompressSync(tarData, format), target);
|
|
184
209
|
};
|
|
210
|
+
/**
|
|
211
|
+
* Inflate off the main thread, then write the files on it.
|
|
212
|
+
*
|
|
213
|
+
* zlib's async API hands the decompress to libuv's threadpool, so
|
|
214
|
+
* concurrent extractions inflate in parallel -- on `UV_THREADPOOL_SIZE`
|
|
215
|
+
* threads, 4 by default -- while the main thread stays free. The writes
|
|
216
|
+
* then take the sync path, which is faster than the async writers (a
|
|
217
|
+
* libuv round trip per file costs more than the IO itself).
|
|
218
|
+
*
|
|
219
|
+
* Splitting the two is the whole point: parallel decompression without
|
|
220
|
+
* giving up the fast writer. It matters most for brotli, whose inflate
|
|
221
|
+
* costs more CPU than gzip's, but it speeds up `.tgz` installs too.
|
|
222
|
+
*/
|
|
223
|
+
export const unpackParallel = async (tarData, target, format) => {
|
|
224
|
+
unpackUnzippedSync(await decompress(tarData, format), target);
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* {@link unpackParallel} reading the tarball from a file, so the read
|
|
228
|
+
* comes off the main thread as well. See {@link unpackFileSync} for
|
|
229
|
+
* what `offset` is for.
|
|
230
|
+
*/
|
|
231
|
+
export const unpackFileParallel = async (file, target, offset = 0, format) => unpackParallel((await readFile(file)).subarray(offset), target, format);
|
|
185
232
|
/**
|
|
186
233
|
* Unpack a tarball straight from a file on disk, skipping `offset`
|
|
187
234
|
* leading bytes. Used to extract from a cache entry in place, so the
|
|
188
235
|
* tarball never lands in the registry client's in-memory cache.
|
|
189
236
|
*/
|
|
190
|
-
export const unpackFileSync = (file, target, offset = 0) => unpackSync(readFileSync(file).subarray(offset), target);
|
|
191
|
-
|
|
237
|
+
export const unpackFileSync = (file, target, offset = 0, format) => unpackSync(readFileSync(file).subarray(offset), target, format);
|
|
238
|
+
/** Sibling temp path used to build `target` before renaming it in. */
|
|
239
|
+
export const tmpName = (target) => dirname(target) + sep + '.' + basename(target) + '.' + tmpSuffix();
|
|
192
240
|
/**
|
|
193
241
|
* Walk the tar headers and collect what has to be written. No IO, so
|
|
194
|
-
*
|
|
242
|
+
* all writers share it and cannot drift on path sanitization.
|
|
195
243
|
*/
|
|
196
244
|
const parseTarball = (buffer, tmp) => {
|
|
197
245
|
/* c8 ignore start */
|
|
198
|
-
|
|
199
|
-
if (isGzip) {
|
|
246
|
+
if (isGzip(buffer)) {
|
|
200
247
|
throw error('still gzipped after unzipping', {
|
|
201
|
-
found:
|
|
248
|
+
found: true,
|
|
202
249
|
wanted: false,
|
|
203
250
|
});
|
|
204
251
|
}
|
|
@@ -386,3 +433,88 @@ const unpackUnzippedSync = (buffer, target) => {
|
|
|
386
433
|
}
|
|
387
434
|
}
|
|
388
435
|
};
|
|
436
|
+
/**
|
|
437
|
+
* What a tarball explodes to in the global store at `dir`: its sidecar
|
|
438
|
+
* index, files and package.json `bin` target paths. Same parsing and
|
|
439
|
+
* path safety as {@link unpackSync}, and the same `format` rule -- only
|
|
440
|
+
* brotli has to be declared. No IO. Throws if the tarball has no valid
|
|
441
|
+
* package.json.
|
|
442
|
+
*/
|
|
443
|
+
export const storeLayout = (tarData, dir, format) => {
|
|
444
|
+
const { dirs, files } = parseTarball(decompressSync(tarData, format), dir);
|
|
445
|
+
const rel = (p) => relative(dir, p).replace(/\\/g, '/');
|
|
446
|
+
const list = files.map(f => [rel(f.path), f]);
|
|
447
|
+
const pj = list.find(([p]) => p === 'package.json')?.[1];
|
|
448
|
+
if (!pj)
|
|
449
|
+
throw error('no package.json in tarball', { path: dir });
|
|
450
|
+
const manifest = storeIndexManifest(pj.body, list.some(([p]) => p === 'binding.gyp'));
|
|
451
|
+
const bins = new Set(Object.values(manifest.bins ?? {}).map(entryKey));
|
|
452
|
+
const indexFiles = [];
|
|
453
|
+
const binFiles = [];
|
|
454
|
+
for (const [p, f] of list) {
|
|
455
|
+
if (bins.has(entryKey(p))) {
|
|
456
|
+
f.executable = true;
|
|
457
|
+
binFiles.push(f.path);
|
|
458
|
+
}
|
|
459
|
+
indexFiles.push([p, f.body.length, f.executable ? 1 : 0]);
|
|
460
|
+
}
|
|
461
|
+
const allDirs = new Set();
|
|
462
|
+
for (const d of dirs) {
|
|
463
|
+
for (let p = rel(d); p && !allDirs.has(p);) {
|
|
464
|
+
allDirs.add(p);
|
|
465
|
+
p = p.slice(0, Math.max(0, p.lastIndexOf('/')));
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
// stable sort: lexical within each length
|
|
469
|
+
const indexDirs = [...allDirs]
|
|
470
|
+
.sort()
|
|
471
|
+
.sort((a, b) => a.length - b.length);
|
|
472
|
+
return {
|
|
473
|
+
index: {
|
|
474
|
+
v: 1,
|
|
475
|
+
files: indexFiles.sort((a, b) => (a[0] < b[0] ? -1 : 1)),
|
|
476
|
+
dirs: indexDirs,
|
|
477
|
+
...manifest,
|
|
478
|
+
},
|
|
479
|
+
files,
|
|
480
|
+
binFiles,
|
|
481
|
+
};
|
|
482
|
+
};
|
|
483
|
+
/**
|
|
484
|
+
* Explode a tarball into `dir` for the global store and
|
|
485
|
+
* return its sidecar index (see {@link storeLayout}), written straight
|
|
486
|
+
* into `dir` (which must not exist; the caller renames it into place).
|
|
487
|
+
* Same modes as {@link unpackSync} followed by reify's bin chmod.
|
|
488
|
+
* Throws, writing nothing, without a valid package.json. Removes `dir`
|
|
489
|
+
* on failure.
|
|
490
|
+
*/
|
|
491
|
+
export const unpackToStoreSync = (tarData, dir, format) => {
|
|
492
|
+
const { index, files, binFiles } = storeLayout(tarData, dir, format);
|
|
493
|
+
mkdirSync(dirname(dir), { recursive: true });
|
|
494
|
+
mkdirSync(dir, { mode: 0o777 });
|
|
495
|
+
let succeeded = false;
|
|
496
|
+
try {
|
|
497
|
+
// recursive: tolerates dirs that differ only by case, like unpackSync
|
|
498
|
+
for (const d of index.dirs) {
|
|
499
|
+
mkdirSync(join(dir, d), { recursive: true, mode: 0o777 });
|
|
500
|
+
}
|
|
501
|
+
for (const f of files) {
|
|
502
|
+
writeFileSync(f.path, f.body, {
|
|
503
|
+
mode: f.executable ? 0o777 : 0o666,
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
// reify's bin chmod result (umask'd mode plus all exec bits), so it
|
|
507
|
+
// never has to chmod a store inode through a link
|
|
508
|
+
let binMode = 0;
|
|
509
|
+
for (const p of binFiles) {
|
|
510
|
+
binMode ||= (statSync(p).mode & 0o777) | 0o111;
|
|
511
|
+
chmodSync(p, binMode);
|
|
512
|
+
}
|
|
513
|
+
succeeded = true;
|
|
514
|
+
}
|
|
515
|
+
finally {
|
|
516
|
+
if (!succeeded)
|
|
517
|
+
rimrafSync(dir);
|
|
518
|
+
}
|
|
519
|
+
return { index };
|
|
520
|
+
};
|
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.3.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/vltpkg/vltpkg.git",
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
"url": "http://vlt.sh"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@vltpkg/error-cause": "1.
|
|
16
|
+
"@vltpkg/error-cause": "1.3.0",
|
|
17
|
+
"@vltpkg/types": "1.3.0",
|
|
17
18
|
"rimraf": "^6.1.2",
|
|
18
19
|
"tar": "^7.5.2"
|
|
19
20
|
},
|
|
@@ -25,7 +26,7 @@
|
|
|
25
26
|
"eslint": "^9.39.1",
|
|
26
27
|
"pacote": "^21.0.4",
|
|
27
28
|
"prettier": "^3.7.4",
|
|
28
|
-
"tap": "^21.
|
|
29
|
+
"tap": "^21.8.0",
|
|
29
30
|
"typedoc": "~0.27.9",
|
|
30
31
|
"typescript": "5.7.3",
|
|
31
32
|
"typescript-eslint": "^8.49.0"
|
|
@@ -59,11 +60,26 @@
|
|
|
59
60
|
"default": "./dist/index.js"
|
|
60
61
|
}
|
|
61
62
|
},
|
|
63
|
+
"./link-tree": {
|
|
64
|
+
"import": {
|
|
65
|
+
"default": "./dist/link-tree.js"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
62
68
|
"./pool": {
|
|
63
69
|
"import": {
|
|
64
70
|
"default": "./dist/pool.js"
|
|
65
71
|
}
|
|
66
72
|
},
|
|
73
|
+
"./store-entry": {
|
|
74
|
+
"import": {
|
|
75
|
+
"default": "./dist/store-entry.js"
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
"./store-index": {
|
|
79
|
+
"import": {
|
|
80
|
+
"default": "./dist/store-index.js"
|
|
81
|
+
}
|
|
82
|
+
},
|
|
67
83
|
"./unpack": {
|
|
68
84
|
"import": {
|
|
69
85
|
"default": "./dist/unpack.js"
|