@vltpkg/spec 1.8.0 → 1.8.1

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/browser.d.ts CHANGED
@@ -40,6 +40,13 @@ export declare const gitHostWebsites: {
40
40
  export declare const defaultScopeRegistries: {
41
41
  '@jsr': string;
42
42
  };
43
+ /**
44
+ * Fill in the defaults for a set of spec options.
45
+ *
46
+ * Memoized per options object: the same object comes back until one of
47
+ * the fields read here is reassigned. The result is shared, so treat it
48
+ * (and `spec.options`) as read-only.
49
+ */
43
50
  export declare const getOptions: (options?: SpecOptions) => SpecOptionsFilled;
44
51
  /** Spec prefixes handled natively, not usable as alias names. */
45
52
  export declare const builtinProtocols: ReadonlySet<string>;
package/dist/browser.js CHANGED
@@ -39,7 +39,7 @@ export const gitHostWebsites = {
39
39
  export const defaultScopeRegistries = {
40
40
  '@jsr': 'https://npm.jsr.io/',
41
41
  };
42
- export const getOptions = (options) => ({
42
+ const fillOptions = (options) => ({
43
43
  catalogs: {},
44
44
  ...options,
45
45
  // after spread: explicit `catalog: undefined` gets default too
@@ -75,6 +75,60 @@ export const getOptions = (options) => ({
75
75
  }
76
76
  : defaultGitHostArchives,
77
77
  });
78
+ // every field fillOptions reads: a reassigned one refills the memo entry
79
+ const filledFrom = [
80
+ 'registry',
81
+ 'registries',
82
+ 'scoped-registries',
83
+ 'jsr-registries',
84
+ 'git-hosts',
85
+ 'git-host-archives',
86
+ 'default-registry-alias',
87
+ 'catalog',
88
+ 'catalogs',
89
+ ];
90
+ const filled = new WeakMap();
91
+ const sameFields = (options, from) => {
92
+ let i = 0;
93
+ for (const k of filledFrom) {
94
+ if (options[k] !== from[i++])
95
+ return false;
96
+ }
97
+ return true;
98
+ };
99
+ /**
100
+ * Fill in the defaults for a set of spec options.
101
+ *
102
+ * Memoized per options object: the same object comes back until one of
103
+ * the fields read here is reassigned. The result is shared, so treat it
104
+ * (and `spec.options`) as read-only.
105
+ */
106
+ export const getOptions = (options) => {
107
+ if (!options)
108
+ return fillOptions(options);
109
+ const hit = filled.get(options);
110
+ if (hit && sameFields(options, hit.from))
111
+ return hit.options;
112
+ const res = fillOptions(options);
113
+ filled.set(options, {
114
+ from: filledFrom.map(k => options[k]),
115
+ options: res,
116
+ });
117
+ return res;
118
+ };
119
+ const registryOptions = new WeakMap();
120
+ /** `{ ...options, registry }`, one object per options object and url */
121
+ const withRegistry = (options, registry) => {
122
+ let byUrl = registryOptions.get(options);
123
+ if (!byUrl) {
124
+ byUrl = new Map();
125
+ registryOptions.set(options, byUrl);
126
+ }
127
+ let res = byUrl.get(registry);
128
+ if (!res)
129
+ byUrl.set(registry, (res = { ...options, registry }));
130
+ return res;
131
+ };
78
132
  // RFC3986 scheme chars minus `.`, min 2 chars. Excludes windows drive
79
133
  // letters (`C:/x`) and hostnames (`notgithub.com:user/repo`).
80
134
  const protocolRE = /^([a-zA-Z][a-zA-Z0-9+-]+):/;
@@ -456,14 +510,18 @@ export class Spec {
456
510
  if (!url.endsWith('/'))
457
511
  url += '/';
458
512
  const regSpec = reg.substring(h + 1);
513
+ // filled options are shared: normalize into a copy
514
+ let registries;
459
515
  for (let [name, u] of regs) {
460
516
  if (!u.endsWith('/')) {
461
517
  u += '/';
462
- this.options.registries[name] = u;
518
+ (registries ??= { ...this.options.registries })[name] = u;
463
519
  }
464
520
  if (u === url)
465
521
  this.namedRegistry = name;
466
522
  }
523
+ if (registries)
524
+ this.options = { ...this.options, registries };
467
525
  this.#parseRegistrySpec(regSpec, url);
468
526
  this.#guessRegistryTarball();
469
527
  return;
@@ -646,10 +704,7 @@ export class Spec {
646
704
  #parseRegistrySpec(s, url) {
647
705
  // note: this takes priority over the scoped registry, if set
648
706
  this.registry = url;
649
- this.subspec = this.constructor.parse(s, {
650
- ...this.options,
651
- registry: url,
652
- });
707
+ this.subspec = this.constructor.parse(s, withRegistry(this.options, url));
653
708
  return this.subspec;
654
709
  }
655
710
  #parseJsrRegistrySpec(s, url) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/spec",
3
3
  "description": "Package specifier library",
4
- "version": "1.8.0",
4
+ "version": "1.8.1",
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.8.0",
17
- "@vltpkg/semver": "1.8.0"
16
+ "@vltpkg/error-cause": "1.8.1",
17
+ "@vltpkg/semver": "1.8.1"
18
18
  },
19
19
  "devDependencies": {
20
20
  "@eslint/js": "^10.0.1",