@vltpkg/package-info 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.
@@ -0,0 +1,18 @@
1
+ import type { RegistryClient } from '@vltpkg/registry-client';
2
+ import type { Spec } from '@vltpkg/spec';
3
+ /** what an archive host is asked with */
4
+ export type Asker = Pick<RegistryClient, 'requestStream'>;
5
+ /**
6
+ * The host a git dep is fetched from: its archive's origin (`::path:`
7
+ * too), or the remote it is cloned from (`#semver:`: its tags).
8
+ */
9
+ export declare const gitHost: ({ final: f }: Spec) => string | undefined;
10
+ /**
11
+ * Throws if the host a git dep is fetched from doesn't answer within
12
+ * `ms`: an archive host with any HTTP answer to a `HEAD` of its root, a
13
+ * remote with its refs (`git ls-remote`, cached for the clone after
14
+ * it). Unasked, a host that drops packets is waited on as long as the
15
+ * OS waits: minutes. One that fails sooner (refused, unknown, denied)
16
+ * is left to the fetch, which reports it or falls back.
17
+ */
18
+ export declare const gitHostAnswers: (spec: Spec, ms: number, rc: () => Promise<Asker>) => Promise<void>;
@@ -0,0 +1,46 @@
1
+ import { error } from '@vltpkg/error-cause';
2
+ import { revs } from '@vltpkg/git';
3
+ import { gitArchiveRef } from "./git-pin.js";
4
+ /**
5
+ * The host a git dep is fetched from: its archive's origin (`::path:`
6
+ * too), or the remote it is cloned from (`#semver:`: its tags).
7
+ */
8
+ export const gitHost = ({ final: f }) => {
9
+ const url = gitArchiveRef(f);
10
+ return url ? new URL(url).origin : f.gitRemote;
11
+ };
12
+ /**
13
+ * Throws if the host a git dep is fetched from doesn't answer within
14
+ * `ms`: an archive host with any HTTP answer to a `HEAD` of its root, a
15
+ * remote with its refs (`git ls-remote`, cached for the clone after
16
+ * it). Unasked, a host that drops packets is waited on as long as the
17
+ * OS waits: minutes. One that fails sooner (refused, unknown, denied)
18
+ * is left to the fetch, which reports it or falls back.
19
+ */
20
+ export const gitHostAnswers = async (spec, ms, rc) => {
21
+ const f = spec.final;
22
+ const url = gitArchiveRef(f);
23
+ const start = Date.now();
24
+ try {
25
+ if (url) {
26
+ const { body } = await (await rc()).requestStream(new URL('/', url), {
27
+ method: 'HEAD',
28
+ connectTimeout: ms,
29
+ headersTimeout: ms,
30
+ });
31
+ // eg an empty gzip body
32
+ body.on('error', () => { });
33
+ body.resume();
34
+ }
35
+ else if (f.gitRemote) {
36
+ // looked up afresh: the cached lookup takes a failure for no
37
+ // refs. still cached for the clone
38
+ await revs(f.gitRemote, { timeout: ms, noGitRevCache: true });
39
+ }
40
+ }
41
+ catch (er) {
42
+ if (Date.now() - start < ms)
43
+ return;
44
+ throw error(`${gitHost(spec)} did not answer within ${ms / 1000}s`, { cause: er });
45
+ }
46
+ };
@@ -0,0 +1,88 @@
1
+ import type { CacheEntry, RegistryClient } from '@vltpkg/registry-client';
2
+ import { Spec } from '@vltpkg/spec';
3
+ import type { Integrity } from '@vltpkg/types';
4
+ /**
5
+ * A hosted git dep locked to a commit: the archive url or a clone pin,
6
+ * the archive url its response was read from (the cache has it), the
7
+ * sha (none: locked to the ref url)
8
+ */
9
+ export type GitPin = {
10
+ resolved: string;
11
+ archive: string;
12
+ response: CacheEntry;
13
+ sha?: string;
14
+ };
15
+ /**
16
+ * What a run keeps of a pin: the archive's integrity in place of its
17
+ * response, whether it has a root `.gitmodules` (unset: not checked yet)
18
+ */
19
+ export type GitPinned = Omit<GitPin, 'response'> & {
20
+ integrity?: Integrity;
21
+ submodules?: boolean;
22
+ };
23
+ /** what an archive's head is read with */
24
+ export type Streamer = Pick<RegistryClient, 'requestStream'>;
25
+ /**
26
+ * The archive url a hosted git dep's commit is read from: its
27
+ * `remoteURL`, or for `::path:` the repo's at the ref.
28
+ */
29
+ export declare const gitArchiveRef: (f: Spec) => string | undefined;
30
+ /**
31
+ * Lock a hosted git ref (archive url `ref`) to the commit it points at:
32
+ * the sha in the head of its archive (pax header; a ref moves, so read
33
+ * off the host, and only that far), else `git ls-remote`. The archive
34
+ * at the sha is what gets locked. No sha, or no url that names one:
35
+ * the ref archive, revalidated, with a warning.
36
+ *
37
+ * `get` fetches an archive into the cache (`revalidate`: asking the
38
+ * host even for a cached one); `rc` streams the head of one.
39
+ */
40
+ export declare const gitPin: (spec: Spec, ref: string, get: (url: string, revalidate?: true) => Promise<CacheEntry>, rc: Streamer) => Promise<GitPin>;
41
+ /**
42
+ * Lock a hosted `#semver:` git dep to the archive at its best matching
43
+ * tag's commit (`git ls-remote`), as `keep` makes of it. Undefined: no
44
+ * archive url can name a commit, or no tag matches.
45
+ */
46
+ export declare const gitTagPin: (spec: Spec, get: (url: string) => Promise<CacheEntry>, keep: (pin: GitPin) => GitPinned) => Promise<GitPinned | undefined>;
47
+ /**
48
+ * Archives omit submodules: a dep cloned before archives were tried
49
+ * (no ref, `::path:`, `#semver:`) of a repo with them (root
50
+ * `.gitmodules`) is cloned at the archive's commit instead. `get`
51
+ * reads the pin's archive back, from the cache.
52
+ */
53
+ export declare const gitSubmodulePin: (spec: Spec, pin: GitPinned, get: (url: string) => Promise<CacheEntry>) => Promise<GitPinned>;
54
+ /**
55
+ * A hosted git dep cloned before archives were tried (no ref,
56
+ * `::path:`, `#semver:`) whose archive the host won't serve (any error
57
+ * answer, eg a private repo) or can't be reached: cloned instead.
58
+ */
59
+ export declare const cloneInstead: (spec: Spec, er: unknown) => boolean;
60
+ /** a cloned git dep locked to a commit: `<remote>#<sha>`, path kept */
61
+ export declare const gitClonePin: ({ gitRemote, gitSelectorParsed }: Spec, sha: string) => string;
62
+ /** the commit a cloned git dep's `resolved` is locked to, if any */
63
+ export declare const gitPinnedCommit: (resolved?: string) => string | undefined;
64
+ /**
65
+ * `dist.tarball` of a hosted git archive pin: the url, `::path:` kept
66
+ * so the graph takes it for that dir only.
67
+ */
68
+ export declare const gitPinTarball: ({ gitSelectorParsed }: Spec, url: string) => string;
69
+ /**
70
+ * What `resolve()` answers for a hosted git archive pin. An archive url
71
+ * can't name a `::path:`, so then the commit, as a clone pin.
72
+ */
73
+ export declare const gitPinResolution: (spec: Spec, { resolved, sha, integrity }: GitPinned) => {
74
+ resolved: string;
75
+ spec: Spec;
76
+ integrity?: undefined;
77
+ } | {
78
+ resolved: string;
79
+ integrity: `sha512-${string}` | undefined;
80
+ spec: Spec;
81
+ };
82
+ /**
83
+ * Why a hosted git archive failed its locked integrity, if it did: the
84
+ * host re-packed the archive at the locked commit, or the ref an older
85
+ * lockfile locked moved. `names`: what dependents call it (aliases
86
+ * included), for `vlt update <name>`. Default: the spec's name.
87
+ */
88
+ export declare const gitArchiveChanged: (spec: Spec, url: string, er: unknown, names?: string[]) => string | undefined;
@@ -0,0 +1,194 @@
1
+ import { resolve as gitResolve } from '@vltpkg/git';
2
+ import { Spec } from '@vltpkg/spec';
3
+ // a full commit sha: the archive at one never changes
4
+ const fullSha = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
5
+ // bytes of a git archive that hold its commit: the pax header leads,
6
+ // and gitArchiveCommit() inflates no more than this
7
+ const archiveHead = 4096;
8
+ // the sha `git ls-remote` gives the ref, if git can tell
9
+ const lsRemote = async ({ final: f }) => f.gitRemote &&
10
+ (await gitResolve(f.gitRemote, f.gitCommittish, { spec: f }))?.sha;
11
+ // cloned before archives were tried: no ref, `::path:`, `#semver:`
12
+ const clonedBefore = (f) => !f.gitCommittish || !f.remoteURL;
13
+ // a hosted git dep's archive url at `committish` (none: `HEAD`)
14
+ const archiveAt = (f, committish) => Spec.parse(f.name, `${f.namedGitHost}:${f.namedGitHostPath}` +
15
+ (committish ? `#${committish}` : ''), f.options).final.remoteURL;
16
+ // the host's archive url can name a commit
17
+ const archivable = (f) => !!f.namedGitHost &&
18
+ !!f.options['git-host-archives'][f.namedGitHost]?.includes('$committish');
19
+ /**
20
+ * The archive url a hosted git dep's commit is read from: its
21
+ * `remoteURL`, or for `::path:` the repo's at the ref.
22
+ */
23
+ export const gitArchiveRef = (f) => f.remoteURL ??
24
+ (!f.range && archivable(f) ?
25
+ archiveAt(f, f.gitCommittish)
26
+ : undefined);
27
+ /**
28
+ * Lock a hosted git ref (archive url `ref`) to the commit it points at:
29
+ * the sha in the head of its archive (pax header; a ref moves, so read
30
+ * off the host, and only that far), else `git ls-remote`. The archive
31
+ * at the sha is what gets locked. No sha, or no url that names one:
32
+ * the ref archive, revalidated, with a warning.
33
+ *
34
+ * `get` fetches an archive into the cache (`revalidate`: asking the
35
+ * host even for a cached one); `rc` streams the head of one.
36
+ */
37
+ export const gitPin = async (spec, ref, get, rc) => {
38
+ const f = spec.final;
39
+ if (fullSha.test(String(f.gitCommittish))) {
40
+ return {
41
+ resolved: ref,
42
+ archive: ref,
43
+ response: await get(ref),
44
+ sha: f.gitCommittish,
45
+ };
46
+ }
47
+ const sha = archivable(f) ? await commitOf(spec, ref, rc) : undefined;
48
+ const resolved = sha && archiveAt(f, sha);
49
+ if (!resolved) {
50
+ const response = await get(ref, true);
51
+ // eslint-disable-next-line no-console
52
+ console.error(`warning: ${spec}: locked to ${ref}, not a commit`);
53
+ return { resolved: ref, archive: ref, response };
54
+ }
55
+ return {
56
+ resolved,
57
+ archive: resolved,
58
+ response: await get(resolved),
59
+ sha,
60
+ };
61
+ };
62
+ // the commit the ref's archive was made from, if it can be found
63
+ const commitOf = async (spec, ref, rc) => {
64
+ const data = await readHead(rc, ref, archiveHead);
65
+ // lazy, like the tar pool
66
+ const { gitArchiveCommit } = await import('@vltpkg/tar/git-archive-commit');
67
+ return (data && gitArchiveCommit(data)) ?? (await lsRemote(spec));
68
+ };
69
+ // the first `bytes` of what a url serves: a 200 read that far, then
70
+ // dropped, connection and all. undefined for any other answer, or
71
+ // none: the full fetch that follows reports it (and takes the
72
+ // redirects and retries request() does)
73
+ const readHead = async (rc, url, bytes) => {
74
+ const res = await rc.requestStream(url).catch(() => undefined);
75
+ if (!res)
76
+ return undefined;
77
+ const { statusCode, body } = res;
78
+ if (statusCode !== 200) {
79
+ body.resume();
80
+ return undefined;
81
+ }
82
+ return await new Promise(resolve => {
83
+ const chunks = [];
84
+ let n = 0;
85
+ body.on('data', (chunk) => {
86
+ chunks.push(chunk);
87
+ n += chunk.length;
88
+ if (n >= bytes)
89
+ body.destroy();
90
+ });
91
+ // a body cut short, by the host or just above: what was read
92
+ body.on('error', () => { });
93
+ body.on('close', () => resolve(Buffer.concat(chunks)));
94
+ });
95
+ };
96
+ /**
97
+ * Lock a hosted `#semver:` git dep to the archive at its best matching
98
+ * tag's commit (`git ls-remote`), as `keep` makes of it. Undefined: no
99
+ * archive url can name a commit, or no tag matches.
100
+ */
101
+ export const gitTagPin = async (spec, get, keep) => {
102
+ const f = spec.final;
103
+ const sha = archivable(f) ? await lsRemote(spec) : undefined;
104
+ const resolved = sha && archiveAt(f, sha);
105
+ return resolved ?
106
+ keep({
107
+ resolved,
108
+ archive: resolved,
109
+ response: await get(resolved),
110
+ sha,
111
+ })
112
+ : undefined;
113
+ };
114
+ /**
115
+ * Archives omit submodules: a dep cloned before archives were tried
116
+ * (no ref, `::path:`, `#semver:`) of a repo with them (root
117
+ * `.gitmodules`) is cloned at the archive's commit instead. `get`
118
+ * reads the pin's archive back, from the cache.
119
+ */
120
+ export const gitSubmodulePin = async (spec, pin, get) => {
121
+ const f = spec.final;
122
+ if (!pin.sha || !clonedBefore(f))
123
+ return pin;
124
+ const { gitArchiveSubmodules } = await import('@vltpkg/tar/git-archive-commit');
125
+ // once per pin
126
+ pin.submodules ??= gitArchiveSubmodules((await get(pin.archive)).buffer());
127
+ return pin.submodules ?
128
+ { ...pin, resolved: gitClonePin(f, pin.sha) }
129
+ : pin;
130
+ };
131
+ /**
132
+ * A hosted git dep cloned before archives were tried (no ref,
133
+ * `::path:`, `#semver:`) whose archive the host won't serve (any error
134
+ * answer, eg a private repo) or can't be reached: cloned instead.
135
+ */
136
+ export const cloneInstead = (spec, er) => {
137
+ const f = spec.final;
138
+ const c = er?.cause;
139
+ return (clonedBefore(f) &&
140
+ (c?.code === 'EREQUEST' || !!c?.response?.statusCode));
141
+ };
142
+ /** a cloned git dep locked to a commit: `<remote>#<sha>`, path kept */
143
+ export const gitClonePin = ({ gitRemote, gitSelectorParsed }, sha) => `${gitRemote}#${sha}` +
144
+ (gitSelectorParsed?.path ? `::path:${gitSelectorParsed.path}` : '');
145
+ /** the commit a cloned git dep's `resolved` is locked to, if any */
146
+ export const gitPinnedCommit = (resolved) => /#([0-9a-f]{40}|[0-9a-f]{64})(?:::|$)/.exec(resolved ?? '')?.[1];
147
+ /**
148
+ * `dist.tarball` of a hosted git archive pin: the url, `::path:` kept
149
+ * so the graph takes it for that dir only.
150
+ */
151
+ export const gitPinTarball = ({ gitSelectorParsed }, url) => gitSelectorParsed?.path ?
152
+ `${url}::path:${gitSelectorParsed.path}`
153
+ : url;
154
+ /**
155
+ * What `resolve()` answers for a hosted git archive pin. An archive url
156
+ * can't name a `::path:`, so then the commit, as a clone pin.
157
+ */
158
+ export const gitPinResolution = (spec, { resolved, sha, integrity }) => spec.final.gitSelectorParsed?.path && sha ?
159
+ { resolved: gitClonePin(spec.final, sha), spec }
160
+ : { resolved, integrity, spec };
161
+ // a full sha anywhere in a url
162
+ const shaIn = /(?<![0-9a-f])(?:[0-9a-f]{40}|[0-9a-f]{64})(?![0-9a-f])/;
163
+ // a name `vlt update` takes: a package name, never a spec or query
164
+ const pkgName = /^(?:@[\w.-]+\/)?[\w.-]+$/;
165
+ /**
166
+ * Why a hosted git archive failed its locked integrity, if it did: the
167
+ * host re-packed the archive at the locked commit, or the ref an older
168
+ * lockfile locked moved. `names`: what dependents call it (aliases
169
+ * included), for `vlt update <name>`. Default: the spec's name.
170
+ */
171
+ export const gitArchiveChanged = (spec, url, er, names = []) => {
172
+ const f = spec.final;
173
+ if (!f.namedGitHost ||
174
+ er?.cause?.code !== 'EINTEGRITY') {
175
+ return;
176
+ }
177
+ const sha = shaIn.exec(url)?.[0];
178
+ if (!sha) {
179
+ return ('git archive at the locked ref no longer matches the lockfile ' +
180
+ 'integrity: the ref may have moved. Run `vlt install` to lock ' +
181
+ 'its current commit.');
182
+ }
183
+ // the url `vlt update` takes the commit from, if other (eg the
184
+ // redirect target of an older lockfile's url): may be cached too
185
+ const now = archiveAt(f, sha);
186
+ const keys = [url, ...(now && now !== url ? [now] : [])];
187
+ // only this dep re-resolves, everything else stays locked
188
+ const deps = new Set((names.length ? names : [spec.name]).filter(n => pkgName.test(n)));
189
+ const update = deps.size ? [...deps].join(' ') : '<name>';
190
+ return ('git archive at the locked commit no longer matches the ' +
191
+ 'lockfile integrity: the host may have re-packed it. To lock ' +
192
+ `the new one, run \`vlt cache delete "${keys.join('" "')}"\`, ` +
193
+ `then \`vlt update ${update}\`.`);
194
+ };
package/dist/index.d.ts CHANGED
@@ -97,6 +97,13 @@ export type PackageInfoClientRequestOptions = PickManifestOptions & RegistryClie
97
97
  * specs are unaffected.
98
98
  */
99
99
  backgroundRevalidate?: boolean;
100
+ /**
101
+ * Milliseconds a git dep's host gets to answer (connect to its
102
+ * archive host, `git ls-remote` its remote) before the manifest is
103
+ * given up on; once per host per run. Unset: as long as the OS
104
+ * waits, minutes for one that drops packets.
105
+ */
106
+ gitHostTimeout?: number;
100
107
  };
101
108
  export type PackageInfoClientExtractOptions = PackageInfoClientRequestOptions & {
102
109
  integrity?: Integrity;
@@ -108,6 +115,12 @@ export type PackageInfoClientExtractOptions = PackageInfoClientRequestOptions &
108
115
  * Other network bodies are always checked.
109
116
  */
110
117
  fromLockfile?: boolean;
118
+ /**
119
+ * What dependents call the package (aliases included), for error
120
+ * advice that names it, eg `vlt update <name>`. Default: the spec's
121
+ * name.
122
+ */
123
+ names?: string[];
111
124
  };
112
125
  export declare class PackageInfoClient {
113
126
  #private;
package/dist/index.js CHANGED
@@ -11,6 +11,8 @@ import { storeRoot } from '@vltpkg/registry-client/store-root';
11
11
  import { Spec } from '@vltpkg/spec';
12
12
  import { storeIndexPath } from '@vltpkg/tar/store-index';
13
13
  import { asPackument, brotliTarballUrl, integrityHex, tarballFormat, } from '@vltpkg/types';
14
+ import { cloneInstead, gitArchiveChanged, gitArchiveRef, gitClonePin, gitPin, gitPinnedCommit, gitPinResolution, gitPinTarball, gitSubmodulePin, gitTagPin, } from "./git-pin.js";
15
+ import { gitHost, gitHostAnswers } from "./git-host.js";
14
16
  import { fetchResolve } from "./resolve-batch.js";
15
17
  import { Monorepo } from '@vltpkg/workspaces';
16
18
  import { XDG } from '@vltpkg/xdg';
@@ -81,8 +83,9 @@ const SELECTION_OPTIONS = [
81
83
  // the maximum duration of a manifest cache file
82
84
  const manifestCacheMaxAge = 5 * 60 * 1000;
83
85
  /**
84
- * There is no default registry. Anything that needs to build a registry
85
- * URL fails with a `ECONFIG` error when none has been configured.
86
+ * Anything that needs to build a registry URL fails with a `ECONFIG`
87
+ * error when the spec has no registry (e.g. `default-registry-alias`
88
+ * names an alias with no URL).
86
89
  */
87
90
  const noRegistryError = (spec) => error('No registry configured to resolve this spec. Set "registry" in ' +
88
91
  'vlt.json, pass --registry, or run `vlt login --registry=<url>`. ' +
@@ -152,12 +155,22 @@ const storedIntegrity = (response) => !response.fromCache ? response.integrityAc
152
155
  : !response.contentType ? undefined
153
156
  : (response.integrity ??
154
157
  (response.isGzip ? response.integrityActual : undefined));
158
+ // what a run keeps of a git pin: its archive's integrity, not its bytes
159
+ const kept = ({ response, ...pin }) => ({
160
+ ...pin,
161
+ integrity: storedIntegrity(response),
162
+ });
155
163
  export class PackageInfoClient {
156
164
  #registryClient;
157
165
  #projectRoot;
158
166
  #tarPool;
159
167
  options;
160
168
  #resolutions = new Map();
169
+ // ref archive url -> the pin it got this run; no archive bytes (the
170
+ // cache has those)
171
+ #gitPins = new Map();
172
+ // git host -> whether it answered in time this run (a rejection)
173
+ #gitHosts = new Map();
161
174
  packageJson;
162
175
  monorepo;
163
176
  #trustedIntegrities = new Map();
@@ -392,14 +405,16 @@ export class PackageInfoClient {
392
405
  async #extract(spec, target, options, extractSpan) {
393
406
  if (typeof spec === 'string')
394
407
  spec = Spec.parse(spec, this.options);
395
- const { from = this.#projectRoot, integrity, resolved, fromLockfile = false, } = options;
408
+ const { from = this.#projectRoot, integrity, resolved, fromLockfile = false, names, } = options;
396
409
  const f = spec.final;
397
410
  // vlt <= 1.6.0 locked hosted git tarballs with the bare git remote
398
411
  // as `resolved`, which can't be fetched
399
412
  const url = (f.type === 'git' &&
400
413
  f.remoteURL &&
414
+ f.gitCommittish &&
401
415
  resolved &&
402
- !/^https?:\/\//.test(resolved)) ?
416
+ !/^https?:\/\//.test(resolved) &&
417
+ !gitPinnedCommit(resolved)) ?
403
418
  f.remoteURL
404
419
  : resolved;
405
420
  // If the caller already provides both integrity and resolved
@@ -409,7 +424,9 @@ export class PackageInfoClient {
409
424
  // is what pins it -- `required`, so a registry that serves the
410
425
  // alternate unlabelled is rejected rather than trusted.
411
426
  const brotli = !!url && tarballFormat(url) === 'brotli';
412
- const r = url && (integrity || brotli) ?
427
+ // a locked git dep is fetched as is, never re-resolved
428
+ const lockedGit = f.type === 'git';
429
+ const r = url && (integrity || brotli || lockedGit) ?
413
430
  {
414
431
  resolved: url,
415
432
  integrity,
@@ -424,26 +441,36 @@ export class PackageInfoClient {
424
441
  extractSpan.end = end;
425
442
  switch (f.type) {
426
443
  case 'git': {
427
- const { gitRemote, gitCommittish, remoteURL, gitSelectorParsed, } = f;
428
- if (!remoteURL) {
444
+ const { gitRemote, gitCommittish, gitSelectorParsed } = f;
445
+ // not hosted, or locked to a clone
446
+ if (!f.namedGitHost || !/^https?:\/\//.test(r.resolved)) {
429
447
  /* c8 ignore start - Impossible, would throw on the resolve */
430
448
  if (!gitRemote)
431
449
  throw this.#resolveError(spec, options, 'no remote on git: specifier');
432
450
  /* c8 ignore stop */
451
+ // the locked commit; a row locked to a ref: the ref
452
+ const sha = gitPinnedCommit(r.resolved);
453
+ const s = spec;
454
+ const cloneTo = (dir) => sha ?
455
+ clone(gitRemote, sha, dir)
456
+ : clone(gitRemote, gitCommittish, dir, { spec: s });
433
457
  const { path } = gitSelectorParsed ?? {};
434
458
  if (path !== undefined) {
435
459
  // use obvious name because it's in node_modules
436
460
  const tmp = pathResolve(dirname(target), `.TEMP.${basename(target)}-${randomBytes(6).toString('hex')}`);
437
- await clone(gitRemote, gitCommittish, tmp, { spec });
461
+ await cloneTo(tmp);
438
462
  const src = pathResolve(tmp, path);
439
463
  await rename(src, target);
440
464
  // intentionally not awaited
441
465
  void rm(tmp, { recursive: true, force: true });
442
466
  }
443
467
  else {
444
- await clone(gitRemote, gitCommittish, target, { spec });
468
+ await cloneTo(target);
445
469
  // intentionally not awaited
446
- void rm(target + '/.git', { recursive: true });
470
+ void rm(target + '/.git', {
471
+ recursive: true,
472
+ force: true,
473
+ });
447
474
  }
448
475
  end({ how: 'git' });
449
476
  return r;
@@ -455,6 +482,8 @@ export class PackageInfoClient {
455
482
  // brotli bytes carry no signature of their own, so every unpack
456
483
  // below has to be told; gzip and raw tar are sniffed.
457
484
  const format = tarballFormat(r.resolved);
485
+ // a git `::path:`: that dir of the archive
486
+ const sub = f.gitSelectorParsed?.path || undefined;
458
487
  // git tarballs keep the unpack path
459
488
  const storeOn = f.type === 'registry' && this.#storeLinker !== 'unpack';
460
489
  const copy = this.#storeLinker === 'copy';
@@ -516,7 +545,7 @@ export class PackageInfoClient {
516
545
  });
517
546
  // only the head was read here: the unpacker reads the body
518
547
  // from the file, on a worker when the pool has them
519
- await pool.unpackCacheEntry(cached.path, target, format);
548
+ await pool.unpackCacheEntry(cached.path, target, format, sub);
520
549
  unpacked();
521
550
  logRequest(r.resolved, 'cache');
522
551
  r.integrity ??= cached.integrity;
@@ -613,7 +642,15 @@ export class PackageInfoClient {
613
642
  'cause' in er &&
614
643
  er.cause
615
644
  ?.code === 'EINTEGRITY') {
616
- buf = await fetchTarball(false);
645
+ buf = await fetchTarball(false).catch((er) => {
646
+ const why = gitArchiveChanged(spec, r.resolved, er, names);
647
+ throw why ?
648
+ this.#resolveError(spec, options, why, {
649
+ url: r.resolved,
650
+ cause: er,
651
+ })
652
+ : er;
653
+ });
617
654
  }
618
655
  else {
619
656
  throw er;
@@ -624,7 +661,7 @@ export class PackageInfoClient {
624
661
  url: r.resolved,
625
662
  bytes: buf.length,
626
663
  });
627
- await pool.unpack(buf, target, format);
664
+ await pool.unpack(buf, target, format, sub);
628
665
  unpacked();
629
666
  }
630
667
  catch (er) {
@@ -944,7 +981,8 @@ export class PackageInfoClient {
944
981
  case 'git': {
945
982
  const { remoteURL, gitRemote, gitCommittish, gitSelectorParsed, } = f;
946
983
  const s = spec;
947
- if (!remoteURL) {
984
+ // no ref: cloned, as before the HEAD archive
985
+ if (!remoteURL || !gitCommittish) {
948
986
  if (!gitRemote) {
949
987
  throw this.#resolveError(spec, options, 'no remote on git: specifier');
950
988
  }
@@ -1204,6 +1242,11 @@ export class PackageInfoClient {
1204
1242
  if (typeof spec === 'string')
1205
1243
  spec = Spec.parse(spec, this.options);
1206
1244
  const f = spec.final;
1245
+ if (f.type === 'git' && options.gitHostTimeout !== undefined) {
1246
+ await this.#gitHostAnswers(spec, options.gitHostTimeout);
1247
+ }
1248
+ // a hosted git dep: the archive at its commit
1249
+ let pin;
1207
1250
  switch (f.type) {
1208
1251
  case 'registry': {
1209
1252
  // a resolve entry never went through pickManifest under any
@@ -1273,31 +1316,44 @@ export class PackageInfoClient {
1273
1316
  return fromPackument();
1274
1317
  }
1275
1318
  case 'git': {
1276
- const { gitRemote, gitCommittish, remoteURL, gitSelectorParsed, } = f;
1277
- if (!remoteURL) {
1319
+ const { gitRemote, gitCommittish, gitSelectorParsed } = f;
1320
+ pin = await this.#gitArchive(spec, options);
1321
+ // a clone pin of a dir: from a clone, it may be in a submodule
1322
+ const at = gitSelectorParsed?.path ?
1323
+ gitPinnedCommit(pin?.resolved)
1324
+ : undefined;
1325
+ if (!pin || at) {
1278
1326
  const s = spec;
1279
1327
  if (!gitRemote)
1280
1328
  throw this.#resolveError(spec, options, 'no git remote');
1281
1329
  return await this.#tmpdir(async (dir) => {
1282
- await clone(gitRemote, gitCommittish, dir, { spec: s });
1330
+ const sha = await (at ?
1331
+ clone(gitRemote, at, dir)
1332
+ : clone(gitRemote, gitCommittish, dir, { spec: s }));
1283
1333
  const { path } = gitSelectorParsed ?? {};
1284
1334
  const pkgDir = path !== undefined ? pathResolve(dir, path) : dir;
1285
- return this.#readExtracted(pkgDir, s, options);
1335
+ const mani = this.#readExtracted(pkgDir, s, options);
1336
+ // the graph locks a git node to dist.tarball
1337
+ mani.dist = { tarball: gitClonePin(s.final, sha) };
1338
+ return mani;
1286
1339
  });
1287
1340
  }
1288
1341
  // fallthrough to remote
1289
1342
  }
1290
1343
  case 'remote': {
1291
- const { remoteURL } = f;
1292
- if (!remoteURL) {
1344
+ const url = pin?.resolved ?? f.remoteURL;
1345
+ if (!url) {
1293
1346
  throw this.#resolveError(spec, options, 'no remoteURL on remote specifier');
1294
1347
  }
1295
1348
  const s = spec;
1349
+ const p = pin;
1350
+ // a git `::path:`: that dir of the archive
1351
+ const sub = f.gitSelectorParsed?.path || undefined;
1352
+ // a pin's archive: from the cache, the pin put it there (a
1353
+ // clone pin's manifest is read from the archive at its commit)
1354
+ const archive = p?.archive ?? url;
1296
1355
  return await this.#tmpdir(async (dir) => {
1297
- const response = await (await this.getRegistryClient()).request(remoteURL);
1298
- if (response.statusCode !== 200) {
1299
- throw this.#resolveError(s, options, `failed to fetch URL: ${registryErrorMessage(response)}`, { response, url: remoteURL });
1300
- }
1356
+ const response = await this.#fetchURL(s, archive, options);
1301
1357
  const buf = response.buffer();
1302
1358
  // the graph hands this back to extract() as the lockfile
1303
1359
  // hash: the hash the entry is stored under, or none for an
@@ -1305,13 +1361,21 @@ export class PackageInfoClient {
1305
1361
  // fetches again and records
1306
1362
  const integrity = storedIntegrity(response);
1307
1363
  try {
1308
- await (await this.getTarPool()).unpack(buf, dir, tarballFormat(remoteURL));
1364
+ await (await this.getTarPool()).unpack(buf, dir, tarballFormat(archive), sub);
1309
1365
  }
1310
1366
  catch (er) {
1311
1367
  throw this.#resolveError(s, options, 'tar unpack failed', { cause: er });
1312
1368
  }
1313
1369
  const mani = this.#readExtracted(dir, s, options);
1314
- if (integrity)
1370
+ // the graph locks a git node to dist.tarball; a clone has
1371
+ // no integrity
1372
+ if (p) {
1373
+ mani.dist =
1374
+ gitPinnedCommit(url) ?
1375
+ { tarball: url }
1376
+ : { tarball: gitPinTarball(f, url), integrity };
1377
+ }
1378
+ else if (integrity)
1315
1379
  mani.dist = { integrity };
1316
1380
  return mani;
1317
1381
  });
@@ -1569,10 +1633,14 @@ export class PackageInfoClient {
1569
1633
  throw this.#resolveError(spec, options);
1570
1634
  }
1571
1635
  case 'git': {
1572
- const { gitRemote, remoteURL, gitSelectorParsed } = f;
1573
- if (remoteURL && gitSelectorParsed?.path === undefined) {
1574
- // known git host with a tarball download endpoint
1575
- const r = { resolved: remoteURL, spec };
1636
+ const { gitRemote } = f;
1637
+ // known git host with a tarball download endpoint
1638
+ const pin = await this.#gitArchive(spec, options);
1639
+ if (pin) {
1640
+ // a clone pin has no integrity
1641
+ const r = gitPinnedCommit(pin.resolved) ?
1642
+ { resolved: pin.resolved, spec }
1643
+ : gitPinResolution(spec, pin);
1576
1644
  this.#resolutions.set(memoKey, r);
1577
1645
  return r;
1578
1646
  }
@@ -1583,16 +1651,7 @@ export class PackageInfoClient {
1583
1651
  spec,
1584
1652
  });
1585
1653
  if (rev) {
1586
- const r = {
1587
- resolved: `${gitRemote}#${rev.sha}`,
1588
- spec,
1589
- };
1590
- if (gitSelectorParsed) {
1591
- r.resolved += Object.entries(gitSelectorParsed)
1592
- .filter(([_, v]) => v)
1593
- .map(([k, v]) => `::${k}:${v}`)
1594
- .join('');
1595
- }
1654
+ const r = { resolved: gitClonePin(f, rev.sha), spec };
1596
1655
  this.#resolutions.set(memoKey, r);
1597
1656
  return r;
1598
1657
  }
@@ -1603,7 +1662,7 @@ export class PackageInfoClient {
1603
1662
  spec: s,
1604
1663
  });
1605
1664
  const r = {
1606
- resolved: `${gitRemote}#${sha}`,
1665
+ resolved: gitClonePin(s.final, sha),
1607
1666
  spec: s,
1608
1667
  };
1609
1668
  this.#resolutions.set(memoKey, r);
@@ -1612,6 +1671,55 @@ export class PackageInfoClient {
1612
1671
  }
1613
1672
  }
1614
1673
  }
1674
+ // `revalidate`: ask the host even for a cached entry still within
1675
+ // max-age; one it can't reach keeps the entry
1676
+ async #fetchURL(spec, url, options, revalidate) {
1677
+ const response = await (await this.getRegistryClient()).request(url, revalidate ? { forceRevalidate: true } : {});
1678
+ if (response.statusCode !== 200) {
1679
+ throw this.#resolveError(spec, options, `failed to fetch URL: ${registryErrorMessage(response)}`, { response, url });
1680
+ }
1681
+ return response;
1682
+ }
1683
+ // whether a git dep's host answers within `ms`, once per host per run
1684
+ #gitHostAnswers(spec, ms) {
1685
+ const host = gitHost(spec);
1686
+ // no remote: the manifest() throws its own error
1687
+ if (!host)
1688
+ return;
1689
+ let answers = this.#gitHosts.get(host);
1690
+ if (!answers) {
1691
+ answers = gitHostAnswers(spec, ms, () => this.getRegistryClient());
1692
+ this.#gitHosts.set(host, answers);
1693
+ }
1694
+ return answers;
1695
+ }
1696
+ // a hosted git ref locked to its commit, once per ref url per run
1697
+ #gitPin(spec, ref, options) {
1698
+ let pin = this.#gitPins.get(ref);
1699
+ if (!pin) {
1700
+ pin = this.getRegistryClient()
1701
+ .then(rc => gitPin(spec, ref, (url, revalidate) => this.#fetchURL(spec, url, options, revalidate), rc))
1702
+ .then(kept);
1703
+ this.#gitPins.set(ref, pin);
1704
+ }
1705
+ return pin;
1706
+ }
1707
+ // a hosted git dep's archive or clone pin; undefined: clone by ref
1708
+ async #gitArchive(spec, options) {
1709
+ const f = spec.final;
1710
+ try {
1711
+ const ref = gitArchiveRef(f);
1712
+ const get = (url) => this.#fetchURL(spec, url, options);
1713
+ const pin = ref ? await this.#gitPin(spec, ref, options)
1714
+ : f.range ? await gitTagPin(spec, get, kept)
1715
+ : undefined;
1716
+ return pin && (await gitSubmodulePin(spec, pin, get));
1717
+ }
1718
+ catch (er) {
1719
+ if (!cloneInstead(spec, er))
1720
+ throw er;
1721
+ }
1722
+ }
1615
1723
  /**
1616
1724
  * Read the manifest out of a directory we cloned or unpacked. Failures are
1617
1725
  * labeled with the spec, because the internal directory says nothing about
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/package-info",
3
3
  "description": "Resolve and fetch package metadata and tarballs",
4
- "version": "1.6.4",
4
+ "version": "1.8.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -13,33 +13,33 @@
13
13
  "url": "http://vlt.sh"
14
14
  },
15
15
  "dependencies": {
16
- "@vltpkg/error-cause": "1.6.4",
17
- "@vltpkg/git": "1.6.4",
18
- "@vltpkg/output": "1.6.4",
19
- "@vltpkg/package-json": "1.6.4",
20
- "@vltpkg/pick-manifest": "1.6.4",
21
- "@vltpkg/registry-client": "1.6.4",
22
- "@vltpkg/spec": "1.6.4",
23
- "@vltpkg/tar": "1.6.4",
24
- "@vltpkg/types": "1.6.4",
25
- "@vltpkg/workspaces": "1.6.4",
26
- "@vltpkg/xdg": "1.6.4",
16
+ "@vltpkg/error-cause": "1.8.0",
17
+ "@vltpkg/git": "1.8.0",
18
+ "@vltpkg/output": "1.8.0",
19
+ "@vltpkg/package-json": "1.8.0",
20
+ "@vltpkg/pick-manifest": "1.8.0",
21
+ "@vltpkg/registry-client": "1.8.0",
22
+ "@vltpkg/spec": "1.8.0",
23
+ "@vltpkg/tar": "1.8.0",
24
+ "@vltpkg/types": "1.8.0",
25
+ "@vltpkg/workspaces": "1.8.0",
26
+ "@vltpkg/xdg": "1.8.0",
27
27
  "tar": "^7.5.2"
28
28
  },
29
29
  "devDependencies": {
30
- "@eslint/js": "^9.39.1",
30
+ "@eslint/js": "^10.0.1",
31
31
  "@types/node": "^22.19.2",
32
32
  "@types/pacote": "^11.1.8",
33
33
  "@vltpkg/benchmark": "0.0.0",
34
- "@vltpkg/cache-unzip": "1.6.4",
35
- "@vltpkg/vlt-json": "1.6.4",
36
- "eslint": "^9.39.1",
34
+ "@vltpkg/cache-unzip": "1.8.0",
35
+ "@vltpkg/vlt-json": "1.8.0",
36
+ "eslint": "^10.14.0",
37
37
  "pacote": "^21.0.4",
38
38
  "prettier": "^3.7.4",
39
39
  "tap": "^21.8.0",
40
40
  "typedoc": "~0.27.9",
41
41
  "typescript": "5.7.3",
42
- "typescript-eslint": "^8.49.0"
42
+ "typescript-eslint": "^8.56.0"
43
43
  },
44
44
  "license": "BSD-2-Clause-Patent",
45
45
  "engines": {