@vltpkg/package-info 1.6.4 → 1.7.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';
@@ -152,12 +154,22 @@ const storedIntegrity = (response) => !response.fromCache ? response.integrityAc
152
154
  : !response.contentType ? undefined
153
155
  : (response.integrity ??
154
156
  (response.isGzip ? response.integrityActual : undefined));
157
+ // what a run keeps of a git pin: its archive's integrity, not its bytes
158
+ const kept = ({ response, ...pin }) => ({
159
+ ...pin,
160
+ integrity: storedIntegrity(response),
161
+ });
155
162
  export class PackageInfoClient {
156
163
  #registryClient;
157
164
  #projectRoot;
158
165
  #tarPool;
159
166
  options;
160
167
  #resolutions = new Map();
168
+ // ref archive url -> the pin it got this run; no archive bytes (the
169
+ // cache has those)
170
+ #gitPins = new Map();
171
+ // git host -> whether it answered in time this run (a rejection)
172
+ #gitHosts = new Map();
161
173
  packageJson;
162
174
  monorepo;
163
175
  #trustedIntegrities = new Map();
@@ -392,14 +404,16 @@ export class PackageInfoClient {
392
404
  async #extract(spec, target, options, extractSpan) {
393
405
  if (typeof spec === 'string')
394
406
  spec = Spec.parse(spec, this.options);
395
- const { from = this.#projectRoot, integrity, resolved, fromLockfile = false, } = options;
407
+ const { from = this.#projectRoot, integrity, resolved, fromLockfile = false, names, } = options;
396
408
  const f = spec.final;
397
409
  // vlt <= 1.6.0 locked hosted git tarballs with the bare git remote
398
410
  // as `resolved`, which can't be fetched
399
411
  const url = (f.type === 'git' &&
400
412
  f.remoteURL &&
413
+ f.gitCommittish &&
401
414
  resolved &&
402
- !/^https?:\/\//.test(resolved)) ?
415
+ !/^https?:\/\//.test(resolved) &&
416
+ !gitPinnedCommit(resolved)) ?
403
417
  f.remoteURL
404
418
  : resolved;
405
419
  // If the caller already provides both integrity and resolved
@@ -409,7 +423,9 @@ export class PackageInfoClient {
409
423
  // is what pins it -- `required`, so a registry that serves the
410
424
  // alternate unlabelled is rejected rather than trusted.
411
425
  const brotli = !!url && tarballFormat(url) === 'brotli';
412
- const r = url && (integrity || brotli) ?
426
+ // a locked git dep is fetched as is, never re-resolved
427
+ const lockedGit = f.type === 'git';
428
+ const r = url && (integrity || brotli || lockedGit) ?
413
429
  {
414
430
  resolved: url,
415
431
  integrity,
@@ -424,26 +440,36 @@ export class PackageInfoClient {
424
440
  extractSpan.end = end;
425
441
  switch (f.type) {
426
442
  case 'git': {
427
- const { gitRemote, gitCommittish, remoteURL, gitSelectorParsed, } = f;
428
- if (!remoteURL) {
443
+ const { gitRemote, gitCommittish, gitSelectorParsed } = f;
444
+ // not hosted, or locked to a clone
445
+ if (!f.namedGitHost || !/^https?:\/\//.test(r.resolved)) {
429
446
  /* c8 ignore start - Impossible, would throw on the resolve */
430
447
  if (!gitRemote)
431
448
  throw this.#resolveError(spec, options, 'no remote on git: specifier');
432
449
  /* c8 ignore stop */
450
+ // the locked commit; a row locked to a ref: the ref
451
+ const sha = gitPinnedCommit(r.resolved);
452
+ const s = spec;
453
+ const cloneTo = (dir) => sha ?
454
+ clone(gitRemote, sha, dir)
455
+ : clone(gitRemote, gitCommittish, dir, { spec: s });
433
456
  const { path } = gitSelectorParsed ?? {};
434
457
  if (path !== undefined) {
435
458
  // use obvious name because it's in node_modules
436
459
  const tmp = pathResolve(dirname(target), `.TEMP.${basename(target)}-${randomBytes(6).toString('hex')}`);
437
- await clone(gitRemote, gitCommittish, tmp, { spec });
460
+ await cloneTo(tmp);
438
461
  const src = pathResolve(tmp, path);
439
462
  await rename(src, target);
440
463
  // intentionally not awaited
441
464
  void rm(tmp, { recursive: true, force: true });
442
465
  }
443
466
  else {
444
- await clone(gitRemote, gitCommittish, target, { spec });
467
+ await cloneTo(target);
445
468
  // intentionally not awaited
446
- void rm(target + '/.git', { recursive: true });
469
+ void rm(target + '/.git', {
470
+ recursive: true,
471
+ force: true,
472
+ });
447
473
  }
448
474
  end({ how: 'git' });
449
475
  return r;
@@ -455,6 +481,8 @@ export class PackageInfoClient {
455
481
  // brotli bytes carry no signature of their own, so every unpack
456
482
  // below has to be told; gzip and raw tar are sniffed.
457
483
  const format = tarballFormat(r.resolved);
484
+ // a git `::path:`: that dir of the archive
485
+ const sub = f.gitSelectorParsed?.path || undefined;
458
486
  // git tarballs keep the unpack path
459
487
  const storeOn = f.type === 'registry' && this.#storeLinker !== 'unpack';
460
488
  const copy = this.#storeLinker === 'copy';
@@ -516,7 +544,7 @@ export class PackageInfoClient {
516
544
  });
517
545
  // only the head was read here: the unpacker reads the body
518
546
  // from the file, on a worker when the pool has them
519
- await pool.unpackCacheEntry(cached.path, target, format);
547
+ await pool.unpackCacheEntry(cached.path, target, format, sub);
520
548
  unpacked();
521
549
  logRequest(r.resolved, 'cache');
522
550
  r.integrity ??= cached.integrity;
@@ -613,7 +641,15 @@ export class PackageInfoClient {
613
641
  'cause' in er &&
614
642
  er.cause
615
643
  ?.code === 'EINTEGRITY') {
616
- buf = await fetchTarball(false);
644
+ buf = await fetchTarball(false).catch((er) => {
645
+ const why = gitArchiveChanged(spec, r.resolved, er, names);
646
+ throw why ?
647
+ this.#resolveError(spec, options, why, {
648
+ url: r.resolved,
649
+ cause: er,
650
+ })
651
+ : er;
652
+ });
617
653
  }
618
654
  else {
619
655
  throw er;
@@ -624,7 +660,7 @@ export class PackageInfoClient {
624
660
  url: r.resolved,
625
661
  bytes: buf.length,
626
662
  });
627
- await pool.unpack(buf, target, format);
663
+ await pool.unpack(buf, target, format, sub);
628
664
  unpacked();
629
665
  }
630
666
  catch (er) {
@@ -944,7 +980,8 @@ export class PackageInfoClient {
944
980
  case 'git': {
945
981
  const { remoteURL, gitRemote, gitCommittish, gitSelectorParsed, } = f;
946
982
  const s = spec;
947
- if (!remoteURL) {
983
+ // no ref: cloned, as before the HEAD archive
984
+ if (!remoteURL || !gitCommittish) {
948
985
  if (!gitRemote) {
949
986
  throw this.#resolveError(spec, options, 'no remote on git: specifier');
950
987
  }
@@ -1204,6 +1241,11 @@ export class PackageInfoClient {
1204
1241
  if (typeof spec === 'string')
1205
1242
  spec = Spec.parse(spec, this.options);
1206
1243
  const f = spec.final;
1244
+ if (f.type === 'git' && options.gitHostTimeout !== undefined) {
1245
+ await this.#gitHostAnswers(spec, options.gitHostTimeout);
1246
+ }
1247
+ // a hosted git dep: the archive at its commit
1248
+ let pin;
1207
1249
  switch (f.type) {
1208
1250
  case 'registry': {
1209
1251
  // a resolve entry never went through pickManifest under any
@@ -1273,31 +1315,44 @@ export class PackageInfoClient {
1273
1315
  return fromPackument();
1274
1316
  }
1275
1317
  case 'git': {
1276
- const { gitRemote, gitCommittish, remoteURL, gitSelectorParsed, } = f;
1277
- if (!remoteURL) {
1318
+ const { gitRemote, gitCommittish, gitSelectorParsed } = f;
1319
+ pin = await this.#gitArchive(spec, options);
1320
+ // a clone pin of a dir: from a clone, it may be in a submodule
1321
+ const at = gitSelectorParsed?.path ?
1322
+ gitPinnedCommit(pin?.resolved)
1323
+ : undefined;
1324
+ if (!pin || at) {
1278
1325
  const s = spec;
1279
1326
  if (!gitRemote)
1280
1327
  throw this.#resolveError(spec, options, 'no git remote');
1281
1328
  return await this.#tmpdir(async (dir) => {
1282
- await clone(gitRemote, gitCommittish, dir, { spec: s });
1329
+ const sha = await (at ?
1330
+ clone(gitRemote, at, dir)
1331
+ : clone(gitRemote, gitCommittish, dir, { spec: s }));
1283
1332
  const { path } = gitSelectorParsed ?? {};
1284
1333
  const pkgDir = path !== undefined ? pathResolve(dir, path) : dir;
1285
- return this.#readExtracted(pkgDir, s, options);
1334
+ const mani = this.#readExtracted(pkgDir, s, options);
1335
+ // the graph locks a git node to dist.tarball
1336
+ mani.dist = { tarball: gitClonePin(s.final, sha) };
1337
+ return mani;
1286
1338
  });
1287
1339
  }
1288
1340
  // fallthrough to remote
1289
1341
  }
1290
1342
  case 'remote': {
1291
- const { remoteURL } = f;
1292
- if (!remoteURL) {
1343
+ const url = pin?.resolved ?? f.remoteURL;
1344
+ if (!url) {
1293
1345
  throw this.#resolveError(spec, options, 'no remoteURL on remote specifier');
1294
1346
  }
1295
1347
  const s = spec;
1348
+ const p = pin;
1349
+ // a git `::path:`: that dir of the archive
1350
+ const sub = f.gitSelectorParsed?.path || undefined;
1351
+ // a pin's archive: from the cache, the pin put it there (a
1352
+ // clone pin's manifest is read from the archive at its commit)
1353
+ const archive = p?.archive ?? url;
1296
1354
  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
- }
1355
+ const response = await this.#fetchURL(s, archive, options);
1301
1356
  const buf = response.buffer();
1302
1357
  // the graph hands this back to extract() as the lockfile
1303
1358
  // hash: the hash the entry is stored under, or none for an
@@ -1305,13 +1360,21 @@ export class PackageInfoClient {
1305
1360
  // fetches again and records
1306
1361
  const integrity = storedIntegrity(response);
1307
1362
  try {
1308
- await (await this.getTarPool()).unpack(buf, dir, tarballFormat(remoteURL));
1363
+ await (await this.getTarPool()).unpack(buf, dir, tarballFormat(archive), sub);
1309
1364
  }
1310
1365
  catch (er) {
1311
1366
  throw this.#resolveError(s, options, 'tar unpack failed', { cause: er });
1312
1367
  }
1313
1368
  const mani = this.#readExtracted(dir, s, options);
1314
- if (integrity)
1369
+ // the graph locks a git node to dist.tarball; a clone has
1370
+ // no integrity
1371
+ if (p) {
1372
+ mani.dist =
1373
+ gitPinnedCommit(url) ?
1374
+ { tarball: url }
1375
+ : { tarball: gitPinTarball(f, url), integrity };
1376
+ }
1377
+ else if (integrity)
1315
1378
  mani.dist = { integrity };
1316
1379
  return mani;
1317
1380
  });
@@ -1569,10 +1632,14 @@ export class PackageInfoClient {
1569
1632
  throw this.#resolveError(spec, options);
1570
1633
  }
1571
1634
  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 };
1635
+ const { gitRemote } = f;
1636
+ // known git host with a tarball download endpoint
1637
+ const pin = await this.#gitArchive(spec, options);
1638
+ if (pin) {
1639
+ // a clone pin has no integrity
1640
+ const r = gitPinnedCommit(pin.resolved) ?
1641
+ { resolved: pin.resolved, spec }
1642
+ : gitPinResolution(spec, pin);
1576
1643
  this.#resolutions.set(memoKey, r);
1577
1644
  return r;
1578
1645
  }
@@ -1583,16 +1650,7 @@ export class PackageInfoClient {
1583
1650
  spec,
1584
1651
  });
1585
1652
  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
- }
1653
+ const r = { resolved: gitClonePin(f, rev.sha), spec };
1596
1654
  this.#resolutions.set(memoKey, r);
1597
1655
  return r;
1598
1656
  }
@@ -1603,7 +1661,7 @@ export class PackageInfoClient {
1603
1661
  spec: s,
1604
1662
  });
1605
1663
  const r = {
1606
- resolved: `${gitRemote}#${sha}`,
1664
+ resolved: gitClonePin(s.final, sha),
1607
1665
  spec: s,
1608
1666
  };
1609
1667
  this.#resolutions.set(memoKey, r);
@@ -1612,6 +1670,55 @@ export class PackageInfoClient {
1612
1670
  }
1613
1671
  }
1614
1672
  }
1673
+ // `revalidate`: ask the host even for a cached entry still within
1674
+ // max-age; one it can't reach keeps the entry
1675
+ async #fetchURL(spec, url, options, revalidate) {
1676
+ const response = await (await this.getRegistryClient()).request(url, revalidate ? { forceRevalidate: true } : {});
1677
+ if (response.statusCode !== 200) {
1678
+ throw this.#resolveError(spec, options, `failed to fetch URL: ${registryErrorMessage(response)}`, { response, url });
1679
+ }
1680
+ return response;
1681
+ }
1682
+ // whether a git dep's host answers within `ms`, once per host per run
1683
+ #gitHostAnswers(spec, ms) {
1684
+ const host = gitHost(spec);
1685
+ // no remote: the manifest() throws its own error
1686
+ if (!host)
1687
+ return;
1688
+ let answers = this.#gitHosts.get(host);
1689
+ if (!answers) {
1690
+ answers = gitHostAnswers(spec, ms, () => this.getRegistryClient());
1691
+ this.#gitHosts.set(host, answers);
1692
+ }
1693
+ return answers;
1694
+ }
1695
+ // a hosted git ref locked to its commit, once per ref url per run
1696
+ #gitPin(spec, ref, options) {
1697
+ let pin = this.#gitPins.get(ref);
1698
+ if (!pin) {
1699
+ pin = this.getRegistryClient()
1700
+ .then(rc => gitPin(spec, ref, (url, revalidate) => this.#fetchURL(spec, url, options, revalidate), rc))
1701
+ .then(kept);
1702
+ this.#gitPins.set(ref, pin);
1703
+ }
1704
+ return pin;
1705
+ }
1706
+ // a hosted git dep's archive or clone pin; undefined: clone by ref
1707
+ async #gitArchive(spec, options) {
1708
+ const f = spec.final;
1709
+ try {
1710
+ const ref = gitArchiveRef(f);
1711
+ const get = (url) => this.#fetchURL(spec, url, options);
1712
+ const pin = ref ? await this.#gitPin(spec, ref, options)
1713
+ : f.range ? await gitTagPin(spec, get, kept)
1714
+ : undefined;
1715
+ return pin && (await gitSubmodulePin(spec, pin, get));
1716
+ }
1717
+ catch (er) {
1718
+ if (!cloneInstead(spec, er))
1719
+ throw er;
1720
+ }
1721
+ }
1615
1722
  /**
1616
1723
  * Read the manifest out of a directory we cloned or unpacked. Failures are
1617
1724
  * 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.7.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.7.0",
17
+ "@vltpkg/git": "1.7.0",
18
+ "@vltpkg/output": "1.7.0",
19
+ "@vltpkg/package-json": "1.7.0",
20
+ "@vltpkg/pick-manifest": "1.7.0",
21
+ "@vltpkg/registry-client": "1.7.0",
22
+ "@vltpkg/spec": "1.7.0",
23
+ "@vltpkg/tar": "1.7.0",
24
+ "@vltpkg/types": "1.7.0",
25
+ "@vltpkg/workspaces": "1.7.0",
26
+ "@vltpkg/xdg": "1.7.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.7.0",
35
+ "@vltpkg/vlt-json": "1.7.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": {