@vltpkg/package-info 1.3.4 → 1.3.6

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
@@ -138,8 +138,13 @@ export declare class PackageInfoClient {
138
138
  *
139
139
  * A no-op unless the registry says it serves the endpoint. Anything it
140
140
  * does not deliver is left to `manifest()`.
141
+ *
142
+ * Returns the function that releases the request. A build calls it once
143
+ * it has placed its last node; when the last build reading a request has
144
+ * released it, the stream is ended, so an install never waits on a
145
+ * stream nothing will read. The records it delivered stay answerable.
141
146
  */
142
- prefetchResolve(registry: string, request: ResolveRequest): void;
147
+ prefetchResolve(registry: string, request: ResolveRequest): () => void;
143
148
  /** How many manifests resolve batches have delivered. For tests. */
144
149
  get resolvedManifestCount(): number;
145
150
  manifest(spec: Spec | string, options?: PackageInfoClientRequestOptions): Promise<Manifest | import("@vltpkg/types").Override<Manifest, import("@vltpkg/types").NormalizedFields>>;
package/dist/index.js CHANGED
@@ -52,20 +52,20 @@ export const VLT_PACKUMENT_V2_MIME = 'application/vnd.vlt.packument-v2+json';
52
52
  */
53
53
  export const PACKUMENT_ACCEPT = `${VLT_PACKUMENT_V2_MIME}; q=1.0, ${VLT_PACKUMENT_MIME}; q=0.9, application/json; q=0.8, */*; q=0.1`;
54
54
  /**
55
- * How long `manifest()` waits on an in-flight resolve that is not delivering
56
- * anything before fetching the packument itself. A stream that keeps
57
- * delivering records is waited on for as long as it does; one that goes
58
- * quiet for this long is left to finish on its own.
55
+ * How long a resolve stream may go without delivering a record before a
56
+ * lookup fetches the packument as well. A stream that is delivering gets
57
+ * this long to carry the key; one that has already been quiet for this long
58
+ * gets no more. The stream is still read after that: the lookup takes
59
+ * whichever answers first.
59
60
  */
60
61
  const resolveWaitMs = () => {
61
62
  const configured = Number(process.env.VLT_BATCH_RESOLVE_WAIT_MS);
62
63
  return Number.isFinite(configured) && configured >= 0 ?
63
64
  configured
64
- : 1000;
65
+ : 250;
65
66
  };
66
- // what a resolve wait's timer resolves to, so it cannot be mistaken for a
67
- // manifest
68
- const stalled = Symbol('stalled');
67
+ // what the grace timer resolves to, so it cannot be mistaken for a manifest
68
+ const graceOver = Symbol('graceOver');
69
69
  // request options that change which manifest is picked, or how; a batch
70
70
  // entry never went through pickManifest under them, so they bypass it
71
71
  const SELECTION_OPTIONS = [
@@ -186,10 +186,19 @@ export class PackageInfoClient {
186
186
  // `${registry}${name}@${range}` for every spec that chose a version and
187
187
  // `${registry}${name}@${version}` for the version itself.
188
188
  #resolvedManifests = new Map();
189
- // The resolves still streaming, per registry, each settling when its
190
- // stream ends. The requests overlap whatever the graph build does before
191
- // it asks for its first manifest.
189
+ /**
190
+ * The resolve requests in flight against each registry, with when the
191
+ * registry last delivered a record: the grace a lookup gives the stream
192
+ * is measured from there, and it is set when the request is sent, so
193
+ * a registry with a request in flight always has a time.
194
+ */
192
195
  #resolveInflight = new Map();
196
+ /**
197
+ * each request in flight, by its `#resolveSent` key: its controller and
198
+ * how many builds are still reading it. a request that settled on its
199
+ * own stays in `#resolveSent`, since its records are still here
200
+ */
201
+ #resolveOwners = new Map();
193
202
  // Requests already sent, so the same ask is not repeated. Keyed on the
194
203
  // request rather than the registry: a later step asking for a different
195
204
  // root set is a different question.
@@ -198,10 +207,6 @@ export class PackageInfoClient {
198
207
  // it. Waiting per key rather than on the whole response is what lets a
199
208
  // build place level N while the server still walks level N+1.
200
209
  #resolveWaiters = new Map();
201
- // When each registry's resolve last delivered a record, which is what a
202
- // waiter checks to tell a stream that is still coming from one that has
203
- // stalled.
204
- #resolveLastRecordAt = new Map();
205
210
  #registryClientPromise;
206
211
  #tarPoolPromise;
207
212
  async getRegistryClient() {
@@ -872,36 +877,71 @@ export class PackageInfoClient {
872
877
  *
873
878
  * A no-op unless the registry says it serves the endpoint. Anything it
874
879
  * does not deliver is left to `manifest()`.
880
+ *
881
+ * Returns the function that releases the request. A build calls it once
882
+ * it has placed its last node; when the last build reading a request has
883
+ * released it, the stream is ended, so an install never waits on a
884
+ * stream nothing will read. The records it delivered stay answerable.
875
885
  */
876
886
  prefetchResolve(registry, request) {
877
887
  if (!request.roots.length)
878
- return;
888
+ return () => { };
879
889
  // keyed the way a spec names its registry, with the trailing slash
880
890
  if (!registry.endsWith('/'))
881
891
  registry += '/';
882
892
  const sent = `${registry}${JSON.stringify(request)}`;
893
+ // the same question already in flight is read, not asked again
894
+ const owned = this.#resolveOwners.get(sent);
895
+ if (owned) {
896
+ owned.readers++;
897
+ return () => this.#releaseResolve(sent, owned);
898
+ }
883
899
  if (this.#resolveSent.has(sent))
884
- return;
900
+ return () => { };
885
901
  this.#resolveSent.add(sent);
886
- const promise = this.#runResolve(registry, request);
902
+ const owner = { controller: new AbortController(), readers: 1 };
903
+ this.#resolveOwners.set(sent, owner);
887
904
  let inflight = this.#resolveInflight.get(registry);
888
- if (!inflight)
889
- this.#resolveInflight.set(registry, (inflight = new Set()));
890
- inflight.add(promise);
905
+ if (!inflight) {
906
+ inflight = { requests: new Set(), lastRecordAt: 0 };
907
+ this.#resolveInflight.set(registry, inflight);
908
+ }
909
+ // the stream's grace runs from the moment it is asked for
910
+ inflight.lastRecordAt = Date.now();
911
+ const promise = this.#runResolve(registry, request, owner.controller.signal, inflight);
912
+ inflight.requests.add(promise);
891
913
  // dropped once settled, so manifest() stops waiting on a request that
892
914
  // has already delivered everything it is going to
893
- const clear = () => inflight.delete(promise);
915
+ const clear = () => {
916
+ inflight.requests.delete(promise);
917
+ // a request released and asked again is a new owner by now
918
+ if (this.#resolveOwners.get(sent) === owner)
919
+ this.#resolveOwners.delete(sent);
920
+ };
894
921
  promise.then(clear, clear);
922
+ return () => this.#releaseResolve(sent, owner);
923
+ }
924
+ /**
925
+ * One build is done reading the request; the last one out ends it, and
926
+ * the same question can then be asked again.
927
+ */
928
+ #releaseResolve(sent, owner) {
929
+ if (--owner.readers > 0)
930
+ return;
931
+ if (this.#resolveOwners.get(sent) === owner) {
932
+ this.#resolveOwners.delete(sent);
933
+ this.#resolveSent.delete(sent);
934
+ }
935
+ owner.controller.abort();
895
936
  }
896
937
  /** Never rejects: a resolve that fails leaves every spec to manifest(). */
897
- async #runResolve(registry, request) {
938
+ async #runResolve(registry, request, signal, inflight) {
898
939
  try {
899
940
  if ((await this.capabilities(registry)).resolve === undefined)
900
941
  return;
901
942
  const client = await this.getRegistryClient();
902
- this.#resolveLastRecordAt.set(registry, Date.now());
903
943
  await fetchResolve(client, registry, request, record => {
904
- this.#resolveLastRecordAt.set(registry, Date.now());
944
+ inflight.lastRecordAt = Date.now();
905
945
  // A resolve record is a vlt manifest: `dist.tarball` is the
906
946
  // tarball's basename, resolved against the package's `/-/`
907
947
  // directory like a v2 packument's, and a tarball without a hash
@@ -915,7 +955,7 @@ export class PackageInfoClient {
915
955
  this.#deliverResolved(`${registry}${record.name}@${spec}`, record.manifest);
916
956
  }
917
957
  this.#deliverResolved(`${registry}${record.name}@${record.version}`, record.manifest);
918
- });
958
+ }, signal);
919
959
  }
920
960
  catch {
921
961
  // The per-name path still has every spec.
@@ -933,17 +973,20 @@ export class PackageInfoClient {
933
973
  }
934
974
  }
935
975
  /**
936
- * The manifest an in-flight resolve is expected to carry for `key`, or
937
- * undefined once waiting stopped being worth it: every request settled
938
- * without the key, or no record of any kind arrived for a whole wait.
976
+ * The manifest for `key` from an in-flight resolve, or from `fetch` when
977
+ * the resolve is not carrying it. Undefined when nothing is in flight.
939
978
  *
940
- * A stream that is still delivering is worth waiting on, since its records
941
- * come in the order the build needs them; one that has stalled is not, and
942
- * the packument fetch was the alternative anyway.
979
+ * A stream that is delivering gets a short grace to carry the key, since
980
+ * its records come in the order the build asks for them; one that has
981
+ * gone quiet gets none, so a server that is slow to start never charges
982
+ * that grace to every level of the build. After the grace `fetch` runs as
983
+ * well and the first answer wins, so a slow server can never hold a
984
+ * lookup longer than fetching the packument would have. A fetch that
985
+ * fails while the stream is still open waits for the stream's verdict.
943
986
  */
944
- async #awaitResolved(registry, key) {
987
+ async #awaitResolved(registry, key, fetch) {
945
988
  const inflight = this.#resolveInflight.get(registry);
946
- if (!inflight?.size)
989
+ if (!inflight?.requests.size)
947
990
  return undefined;
948
991
  let wake;
949
992
  const arrival = new Promise(res => {
@@ -953,22 +996,36 @@ export class PackageInfoClient {
953
996
  if (!waiters)
954
997
  this.#resolveWaiters.set(key, (waiters = new Set()));
955
998
  waiters.add(wake);
956
- const settled = Promise.all([...inflight]).then(() => this.#resolvedManifests.get(key));
957
- const waitMs = resolveWaitMs();
999
+ const settled = Promise.all([...inflight.requests]).then(() => this.#resolvedManifests.get(key));
1000
+ const quietFor = Date.now() - inflight.lastRecordAt;
1001
+ const grace = Math.max(0, resolveWaitMs() - quietFor);
958
1002
  try {
959
- for (;;) {
960
- const outcome = await Promise.race([
961
- arrival,
962
- settled,
963
- // unref'd: a pending wait must never be what keeps the process up
964
- setTimeout(waitMs, stalled, { ref: false }),
965
- ]);
966
- if (outcome !== stalled)
967
- return outcome;
968
- const lastRecordAt = this.#resolveLastRecordAt.get(registry) ?? 0;
969
- if (Date.now() - lastRecordAt >= waitMs)
970
- return undefined;
971
- }
1003
+ const early = await Promise.race([
1004
+ arrival,
1005
+ settled,
1006
+ // unref'd: a pending wait must never be what keeps the process up
1007
+ setTimeout(grace, graceOver, { ref: false }),
1008
+ ]);
1009
+ if (early !== graceOver)
1010
+ return early ?? (await fetch());
1011
+ debug('resolve hedge %s', key);
1012
+ const hedge = fetch();
1013
+ const outcome = await Promise.race([
1014
+ arrival,
1015
+ settled,
1016
+ hedge.catch(() => undefined),
1017
+ ]);
1018
+ if (outcome)
1019
+ return outcome;
1020
+ // the hedge failed first, or the stream settled without the key:
1021
+ // the stream gets one more grace to contradict the failure, and
1022
+ // failing that, the failure is the answer
1023
+ const found = await Promise.race([
1024
+ arrival,
1025
+ settled,
1026
+ setTimeout(resolveWaitMs(), graceOver, { ref: false }),
1027
+ ]);
1028
+ return found && found !== graceOver ? found : await hedge;
972
1029
  }
973
1030
  finally {
974
1031
  waiters.delete(wake);
@@ -989,20 +1046,12 @@ export class PackageInfoClient {
989
1046
  case 'registry': {
990
1047
  // a resolve entry never went through pickManifest under any
991
1048
  // selection option, so those take the packument path
992
- if (!SELECTION_OPTIONS.some(k => options[k] !== undefined)) {
993
- const key = `${f.registry ?? ''}${f.name}@${f.bareSpec}`;
994
- const resolved = this.#resolvedManifests.get(key);
1049
+ const selecting = SELECTION_OPTIONS.some(k => options[k] !== undefined);
1050
+ const resolveKey = `${f.registry ?? ''}${f.name}@${f.bareSpec}`;
1051
+ if (!selecting) {
1052
+ const resolved = this.#resolvedManifests.get(resolveKey);
995
1053
  if (resolved)
996
1054
  return resolved;
997
- // Not here yet: give the in-flight resolve a bounded moment to
998
- // deliver this one key rather than fetching a packument for a
999
- // manifest that is probably already on its way.
1000
- if (f.registry) {
1001
- const arrived = await this.#awaitResolved(f.registry, key);
1002
- if (arrived)
1003
- return arrived;
1004
- debug('resolve miss %s', key);
1005
- }
1006
1055
  }
1007
1056
  // Check if manifest is cached, if so just return it earlier
1008
1057
  const cachePath = this._manifestCachePath(spec, options);
@@ -1038,18 +1087,28 @@ export class PackageInfoClient {
1038
1087
  // Cache miss, fetch from packument
1039
1088
  }
1040
1089
  }
1041
- const mani = pickManifest(await this.#packument(f, options, this.#stable(f)), spec, options);
1042
- if (!mani)
1043
- throw this.#resolveError(spec, options);
1044
- // Cache the manifest data. Skip paths already written this
1045
- // run — first writer wins, avoiding duplicate serialization
1046
- // and racing writers for the same path.
1047
- if (cachePath && !this.#manifestWritePaths.has(cachePath)) {
1048
- this.#manifestWritePaths.add(cachePath);
1049
- const vlt = this.#vltPackuments.has(`${f.registry}${f.name}`);
1050
- void this.#writeManifestCache(cachePath, JSON.stringify(vlt ? { ...mani, __VLT_PACKUMENT: true } : mani));
1090
+ const fromPackument = async () => {
1091
+ const mani = pickManifest(await this.#packument(f, options, this.#stable(f)), spec, options);
1092
+ if (!mani)
1093
+ throw this.#resolveError(spec, options);
1094
+ // Cache the manifest data. Skip paths already written this
1095
+ // run — first writer wins, avoiding duplicate serialization
1096
+ // and racing writers for the same path.
1097
+ if (cachePath && !this.#manifestWritePaths.has(cachePath)) {
1098
+ this.#manifestWritePaths.add(cachePath);
1099
+ const vlt = this.#vltPackuments.has(`${f.registry}${f.name}`);
1100
+ void this.#writeManifestCache(cachePath, JSON.stringify(vlt ? { ...mani, __VLT_PACKUMENT: true } : mani));
1101
+ }
1102
+ return mani;
1103
+ };
1104
+ // Nothing local: the in-flight resolve is probably carrying this
1105
+ // key, so it gets a moment before the packument is fetched too
1106
+ if (!selecting && f.registry) {
1107
+ const resolved = await this.#awaitResolved(f.registry, resolveKey, fromPackument);
1108
+ if (resolved)
1109
+ return resolved;
1051
1110
  }
1052
- return mani;
1111
+ return fromPackument();
1053
1112
  }
1054
1113
  case 'git': {
1055
1114
  const { gitRemote, gitCommittish, remoteURL, gitSelectorParsed, } = f;
@@ -46,6 +46,7 @@ export type ResolveRecord = {
46
46
  * Resolve `request` against `registry`, calling `onRecord` for each version
47
47
  * the server resolves, as its line arrives. Settles when the stream ends,
48
48
  * however it ends: a caller waiting on a record that never came learns that
49
- * from the request settling, not from an error.
49
+ * from the request settling, not from an error. Aborting `signal` ends it
50
+ * early, keeping the records that had already arrived.
50
51
  */
51
- export declare const fetchResolve: (client: RegistryClient, registry: string, request: ResolveRequest, onRecord: (record: ResolveRecord) => void) => Promise<void>;
52
+ export declare const fetchResolve: (client: RegistryClient, registry: string, request: ResolveRequest, onRecord: (record: ResolveRecord) => void, signal?: AbortSignal) => Promise<void>;
@@ -3,9 +3,10 @@ import { createInterface } from 'node:readline';
3
3
  * Resolve `request` against `registry`, calling `onRecord` for each version
4
4
  * the server resolves, as its line arrives. Settles when the stream ends,
5
5
  * however it ends: a caller waiting on a record that never came learns that
6
- * from the request settling, not from an error.
6
+ * from the request settling, not from an error. Aborting `signal` ends it
7
+ * early, keeping the records that had already arrived.
7
8
  */
8
- export const fetchResolve = async (client, registry, request, onRecord) => {
9
+ export const fetchResolve = async (client, registry, request, onRecord, signal) => {
9
10
  if (!request.roots.length)
10
11
  return;
11
12
  let body;
@@ -14,6 +15,7 @@ export const fetchResolve = async (client, registry, request, onRecord) => {
14
15
  // POST, not QUERY: CloudFront's allowed-method sets are fixed and
15
16
  // none includes QUERY. The registry answers both.
16
17
  method: 'POST',
18
+ signal,
17
19
  headers: {
18
20
  'content-type': 'application/json',
19
21
  accept: 'application/x-ndjson',
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.3.4",
4
+ "version": "1.3.6",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -13,17 +13,17 @@
13
13
  "url": "http://vlt.sh"
14
14
  },
15
15
  "dependencies": {
16
- "@vltpkg/error-cause": "1.3.4",
17
- "@vltpkg/git": "1.3.4",
18
- "@vltpkg/output": "1.3.4",
19
- "@vltpkg/package-json": "1.3.4",
20
- "@vltpkg/pick-manifest": "1.3.4",
21
- "@vltpkg/registry-client": "1.3.4",
22
- "@vltpkg/spec": "1.3.4",
23
- "@vltpkg/tar": "1.3.4",
24
- "@vltpkg/types": "1.3.4",
25
- "@vltpkg/workspaces": "1.3.4",
26
- "@vltpkg/xdg": "1.3.4",
16
+ "@vltpkg/error-cause": "1.3.6",
17
+ "@vltpkg/git": "1.3.6",
18
+ "@vltpkg/output": "1.3.6",
19
+ "@vltpkg/package-json": "1.3.6",
20
+ "@vltpkg/pick-manifest": "1.3.6",
21
+ "@vltpkg/registry-client": "1.3.6",
22
+ "@vltpkg/spec": "1.3.6",
23
+ "@vltpkg/tar": "1.3.6",
24
+ "@vltpkg/types": "1.3.6",
25
+ "@vltpkg/workspaces": "1.3.6",
26
+ "@vltpkg/xdg": "1.3.6",
27
27
  "tar": "^7.5.2"
28
28
  },
29
29
  "devDependencies": {
@@ -31,8 +31,8 @@
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.3.4",
35
- "@vltpkg/vlt-json": "1.3.4",
34
+ "@vltpkg/cache-unzip": "1.3.6",
35
+ "@vltpkg/vlt-json": "1.3.6",
36
36
  "eslint": "^9.39.1",
37
37
  "pacote": "^21.0.4",
38
38
  "prettier": "^3.7.4",