@vltpkg/tar 1.2.0 → 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/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { unpack, unpackToStoreSync } from './unpack.ts';
2
+ export type { TarballFormat } from './unpack.ts';
2
3
  export * from './link-tree.ts';
3
4
  export * from './pool.ts';
4
5
  export * from './store-entry.ts';
package/dist/pool.d.ts CHANGED
@@ -1,29 +1,33 @@
1
1
  import type { LinkFromStoreOptions, StoreLinkResult } from './link-tree.ts';
2
2
  import type { StoreIndex } from './store-index.ts';
3
+ import type { TarballFormat } from '@vltpkg/types';
3
4
  /**
4
5
  * Unpacks tarballs into place.
5
6
  *
6
- * Unpacking runs synchronously on the main thread, which is measurably
7
- * faster than the async writers (the libuv round trip per file costs
8
- * more than the IO).
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.
9
12
  *
10
13
  * There is no queue here. The only limiter is the caller's: reify caps
11
14
  * extraction at `Math.max(availableParallelism() - 1, 1) * 8` in flight
12
15
  * (`@vltpkg/graph`, `src/reify/index.ts`). A consumer that is not reify
13
- * 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).
14
18
  */
15
19
  export declare class Pool {
16
20
  /**
17
21
  * Provide the tardata to be unpacked, and the location where it's to be
18
22
  * placed. Resolves when the tarball has been extracted.
19
23
  */
20
- unpack(tarData: Buffer, target: string): Promise<void>;
24
+ unpack(tarData: Buffer, target: string, format?: TarballFormat): Promise<void>;
21
25
  /**
22
26
  * Same, reading the tarball from a file and skipping `offset` leading
23
27
  * bytes, so a cache entry is unpacked without loading it into the
24
28
  * registry client's in-memory cache.
25
29
  */
26
- unpackFile(file: string, target: string, offset?: number): Promise<void>;
30
+ unpackFile(file: string, target: string, offset?: number, format?: TarballFormat): Promise<void>;
27
31
  /**
28
32
  * Hardlink (or copy) a global store entry into `target`. Resolves
29
33
  * how, with the index, or false on a store miss. See
@@ -34,7 +38,7 @@ export declare class Pool {
34
38
  * Explode a tarball into `dir` for the global store. See
35
39
  * {@link unpackToStoreSync}.
36
40
  */
37
- unpackToStore(tarData: Buffer, dir: string): Promise<{
41
+ unpackToStore(tarData: Buffer, dir: string, format?: TarballFormat): Promise<{
38
42
  index: StoreIndex;
39
43
  }>;
40
44
  }
package/dist/pool.js CHANGED
@@ -1,40 +1,60 @@
1
1
  import { readFile } from 'node:fs/promises';
2
2
  import { linkFromStore } from "./link-tree.js";
3
- import { unpack, unpackFileSync, unpackSync, unpackToStoreSync, } from "./unpack.js";
4
- // Field kill switch: restores the async writer without a release.
5
- const syncUnpack = process.env.VLT_TAR_SYNC !== '0';
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;
6
18
  /**
7
19
  * Unpacks tarballs into place.
8
20
  *
9
- * Unpacking runs synchronously on the main thread, which is measurably
10
- * faster than the async writers (the libuv round trip per file costs
11
- * more than the IO).
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.
12
26
  *
13
27
  * There is no queue here. The only limiter is the caller's: reify caps
14
28
  * extraction at `Math.max(availableParallelism() - 1, 1) * 8` in flight
15
29
  * (`@vltpkg/graph`, `src/reify/index.ts`). A consumer that is not reify
16
- * 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).
17
32
  */
18
33
  export class Pool {
19
34
  /**
20
35
  * Provide the tardata to be unpacked, and the location where it's to be
21
36
  * placed. Resolves when the tarball has been extracted.
22
37
  */
23
- async unpack(tarData, target) {
24
- if (!syncUnpack)
25
- return unpack(tarData, target);
26
- unpackSync(tarData, target);
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);
27
44
  }
28
45
  /**
29
46
  * Same, reading the tarball from a file and skipping `offset` leading
30
47
  * bytes, so a cache entry is unpacked without loading it into the
31
48
  * registry client's in-memory cache.
32
49
  */
33
- async unpackFile(file, target, offset = 0) {
34
- if (!syncUnpack) {
35
- 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);
53
+ }
54
+ if (mode === '1') {
55
+ return unpackFileSync(file, target, offset, format);
36
56
  }
37
- unpackFileSync(file, target, offset);
57
+ return unpackFileParallel(file, target, offset, format);
38
58
  }
39
59
  /**
40
60
  * Hardlink (or copy) a global store entry into `target`. Resolves
@@ -48,7 +68,7 @@ export class Pool {
48
68
  * Explode a tarball into `dir` for the global store. See
49
69
  * {@link unpackToStoreSync}.
50
70
  */
51
- async unpackToStore(tarData, dir) {
52
- return unpackToStoreSync(tarData, dir);
71
+ async unpackToStore(tarData, dir, format) {
72
+ return unpackToStoreSync(tarData, dir, format);
53
73
  }
54
74
  }
package/dist/unpack.d.ts CHANGED
@@ -1,4 +1,6 @@
1
+ import type { TarballFormat } from '@vltpkg/types';
1
2
  import type { StoreIndex } from './store-index.ts';
3
+ export type { TarballFormat };
2
4
  /** A file parsed out of a tarball, at its destination path. */
3
5
  export type FileEntry = {
4
6
  path: string;
@@ -12,39 +14,60 @@ export declare const checkFs: (h: {
12
14
  }, tarDir: string | undefined, target: string) => h is {
13
15
  path: string;
14
16
  };
15
- export declare const unpack: (tarData: Buffer, target: string) => Promise<void>;
17
+ export declare const unpack: (tarData: Buffer, target: string, format?: TarballFormat) => Promise<void>;
16
18
  /**
17
19
  * Same as {@link unpack}, but blocking. Faster: the async writers pay a
18
20
  * libuv round trip per file, which costs more than the IO itself.
19
21
  */
20
- 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>;
21
43
  /**
22
44
  * Unpack a tarball straight from a file on disk, skipping `offset`
23
45
  * leading bytes. Used to extract from a cache entry in place, so the
24
46
  * tarball never lands in the registry client's in-memory cache.
25
47
  */
26
- 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;
27
49
  /** Sibling temp path used to build `target` before renaming it in. */
28
50
  export declare const tmpName: (target: string) => string;
29
51
  /**
30
- * What a gzipped or raw tarball explodes to in the global store at
31
- * `dir`: its sidecar index, files and package.json `bin` target paths.
32
- * Same parsing and path safety as {@link unpackSync}. No IO. Throws if
33
- * the tarball has no valid package.json.
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.
34
57
  */
35
- export declare const storeLayout: (tarData: Buffer, dir: string) => {
58
+ export declare const storeLayout: (tarData: Buffer, dir: string, format?: TarballFormat) => {
36
59
  index: StoreIndex;
37
60
  files: FileEntry[];
38
61
  binFiles: string[];
39
62
  };
40
63
  /**
41
- * Explode a gzipped or raw tarball into `dir` for the global store and
64
+ * Explode a tarball into `dir` for the global store and
42
65
  * return its sidecar index (see {@link storeLayout}), written straight
43
66
  * into `dir` (which must not exist; the caller renames it into place).
44
67
  * Same modes as {@link unpackSync} followed by reify's bin chmod.
45
68
  * Throws, writing nothing, without a valid package.json. Removes `dir`
46
69
  * on failure.
47
70
  */
48
- export declare const unpackToStoreSync: (tarData: Buffer, dir: string) => {
71
+ export declare const unpackToStoreSync: (tarData: Buffer, dir: string, format?: TarballFormat) => {
49
72
  index: StoreIndex;
50
73
  };
package/dist/unpack.js CHANGED
@@ -1,12 +1,12 @@
1
1
  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
- import { lstat, mkdir, rename, writeFile } from 'node:fs/promises';
4
+ import { lstat, mkdir, readFile, rename, writeFile, } from 'node:fs/promises';
5
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
11
  import { storeIndexManifest } from "./store-index.js";
12
12
  // Matches node-tar's MAX_DECOMPRESSION_RATIO, which npm uses via pacote.
@@ -43,6 +43,32 @@ const unzipSync = (input) => {
43
43
  throw unzipError(er, input.length, max);
44
44
  }
45
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;
46
72
  const exists = async (path) => {
47
73
  try {
48
74
  await lstat(path);
@@ -171,24 +197,44 @@ const write = async (path, body, executable = false) => {
171
197
  mode: executable ? 0o777 : 0o666,
172
198
  });
173
199
  };
174
- export const unpack = async (tarData, target) => {
175
- const isGzip = tarData[0] === 0x1f && tarData[1] === 0x8b;
176
- await unpackUnzipped(isGzip ? await unzip(tarData) : tarData, target);
200
+ export const unpack = async (tarData, target, format) => {
201
+ await unpackUnzipped(await decompress(tarData, format), target);
177
202
  };
178
203
  /**
179
204
  * Same as {@link unpack}, but blocking. Faster: the async writers pay a
180
205
  * libuv round trip per file, which costs more than the IO itself.
181
206
  */
182
- export const unpackSync = (tarData, target) => {
183
- const isGzip = tarData[0] === 0x1f && tarData[1] === 0x8b;
184
- unpackUnzippedSync(isGzip ? unzipSync(tarData) : tarData, target);
207
+ export const unpackSync = (tarData, target, format) => {
208
+ unpackUnzippedSync(decompressSync(tarData, format), target);
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);
185
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);
186
232
  /**
187
233
  * Unpack a tarball straight from a file on disk, skipping `offset`
188
234
  * leading bytes. Used to extract from a cache entry in place, so the
189
235
  * tarball never lands in the registry client's in-memory cache.
190
236
  */
191
- export const unpackFileSync = (file, target, offset = 0) => unpackSync(readFileSync(file).subarray(offset), target);
237
+ export const unpackFileSync = (file, target, offset = 0, format) => unpackSync(readFileSync(file).subarray(offset), target, format);
192
238
  /** Sibling temp path used to build `target` before renaming it in. */
193
239
  export const tmpName = (target) => dirname(target) + sep + '.' + basename(target) + '.' + tmpSuffix();
194
240
  /**
@@ -197,10 +243,9 @@ export const tmpName = (target) => dirname(target) + sep + '.' + basename(target
197
243
  */
198
244
  const parseTarball = (buffer, tmp) => {
199
245
  /* c8 ignore start */
200
- const isGzip = buffer[0] === 0x1f && buffer[1] === 0x8b;
201
- if (isGzip) {
246
+ if (isGzip(buffer)) {
202
247
  throw error('still gzipped after unzipping', {
203
- found: isGzip,
248
+ found: true,
204
249
  wanted: false,
205
250
  });
206
251
  }
@@ -389,14 +434,14 @@ const unpackUnzippedSync = (buffer, target) => {
389
434
  }
390
435
  };
391
436
  /**
392
- * What a gzipped or raw tarball explodes to in the global store at
393
- * `dir`: its sidecar index, files and package.json `bin` target paths.
394
- * Same parsing and path safety as {@link unpackSync}. No IO. Throws if
395
- * the tarball has no valid package.json.
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.
396
442
  */
397
- export const storeLayout = (tarData, dir) => {
398
- const isGzip = tarData[0] === 0x1f && tarData[1] === 0x8b;
399
- const { dirs, files } = parseTarball(isGzip ? unzipSync(tarData) : tarData, dir);
443
+ export const storeLayout = (tarData, dir, format) => {
444
+ const { dirs, files } = parseTarball(decompressSync(tarData, format), dir);
400
445
  const rel = (p) => relative(dir, p).replace(/\\/g, '/');
401
446
  const list = files.map(f => [rel(f.path), f]);
402
447
  const pj = list.find(([p]) => p === 'package.json')?.[1];
@@ -436,15 +481,15 @@ export const storeLayout = (tarData, dir) => {
436
481
  };
437
482
  };
438
483
  /**
439
- * Explode a gzipped or raw tarball into `dir` for the global store and
484
+ * Explode a tarball into `dir` for the global store and
440
485
  * return its sidecar index (see {@link storeLayout}), written straight
441
486
  * into `dir` (which must not exist; the caller renames it into place).
442
487
  * Same modes as {@link unpackSync} followed by reify's bin chmod.
443
488
  * Throws, writing nothing, without a valid package.json. Removes `dir`
444
489
  * on failure.
445
490
  */
446
- export const unpackToStoreSync = (tarData, dir) => {
447
- const { index, files, binFiles } = storeLayout(tarData, dir);
491
+ export const unpackToStoreSync = (tarData, dir, format) => {
492
+ const { index, files, binFiles } = storeLayout(tarData, dir, format);
448
493
  mkdirSync(dirname(dir), { recursive: true });
449
494
  mkdirSync(dir, { mode: 0o777 });
450
495
  let succeeded = false;
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.2.0",
4
+ "version": "1.3.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -13,8 +13,8 @@
13
13
  "url": "http://vlt.sh"
14
14
  },
15
15
  "dependencies": {
16
- "@vltpkg/error-cause": "1.2.0",
17
- "@vltpkg/types": "1.2.0",
16
+ "@vltpkg/error-cause": "1.3.0",
17
+ "@vltpkg/types": "1.3.0",
18
18
  "rimraf": "^6.1.2",
19
19
  "tar": "^7.5.2"
20
20
  },