@vltpkg/spec 1.7.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
@@ -6,6 +6,7 @@ export declare const kCustomInspect: unique symbol;
6
6
  export declare const defaultRegistryName = "npm";
7
7
  export declare const defaultRegistries: {
8
8
  gh: string;
9
+ npm: string;
9
10
  };
10
11
  export declare const defaultJsrRegistries: {
11
12
  jsr: string;
@@ -39,6 +40,13 @@ export declare const gitHostWebsites: {
39
40
  export declare const defaultScopeRegistries: {
40
41
  '@jsr': string;
41
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
+ */
42
50
  export declare const getOptions: (options?: SpecOptions) => SpecOptionsFilled;
43
51
  /** Spec prefixes handled natively, not usable as alias names. */
44
52
  export declare const builtinProtocols: ReadonlySet<string>;
package/dist/browser.js CHANGED
@@ -7,6 +7,7 @@ export const kCustomInspect = Symbol.for('nodejs.util.inspect.custom');
7
7
  export const defaultRegistryName = 'npm';
8
8
  export const defaultRegistries = {
9
9
  gh: 'https://npm.pkg.github.com/',
10
+ npm: 'https://registry.vlt.io/upstream/npm/',
10
11
  };
11
12
  export const defaultJsrRegistries = { jsr: 'https://npm.jsr.io/' };
12
13
  export const defaultGitHosts = {
@@ -38,7 +39,7 @@ export const gitHostWebsites = {
38
39
  export const defaultScopeRegistries = {
39
40
  '@jsr': 'https://npm.jsr.io/',
40
41
  };
41
- export const getOptions = (options) => ({
42
+ const fillOptions = (options) => ({
42
43
  catalogs: {},
43
44
  ...options,
44
45
  // after spread: explicit `catalog: undefined` gets default too
@@ -50,7 +51,13 @@ export const getOptions = (options) => ({
50
51
  },
51
52
  registry: options?.registry,
52
53
  'default-registry-alias': options?.['default-registry-alias'] ?? defaultRegistryName,
53
- 'scoped-registries': options?.['scoped-registries'] ?? {},
54
+ // JSR packages name their JSR deps `@jsr/*`: send that scope to the
55
+ // jsr alias unless the user maps it
56
+ 'scoped-registries': {
57
+ '@jsr': options?.['jsr-registries']?.jsr ??
58
+ defaultScopeRegistries['@jsr'],
59
+ ...options?.['scoped-registries'],
60
+ },
54
61
  'git-hosts': options?.['git-hosts'] ?
55
62
  {
56
63
  ...defaultGitHosts,
@@ -68,6 +75,60 @@ export const getOptions = (options) => ({
68
75
  }
69
76
  : defaultGitHostArchives,
70
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
+ };
71
132
  // RFC3986 scheme chars minus `.`, min 2 chars. Excludes windows drive
72
133
  // letters (`C:/x`) and hostnames (`notgithub.com:user/repo`).
73
134
  const protocolRE = /^([a-zA-Z][a-zA-Z0-9+-]+):/;
@@ -449,14 +510,18 @@ export class Spec {
449
510
  if (!url.endsWith('/'))
450
511
  url += '/';
451
512
  const regSpec = reg.substring(h + 1);
513
+ // filled options are shared: normalize into a copy
514
+ let registries;
452
515
  for (let [name, u] of regs) {
453
516
  if (!u.endsWith('/')) {
454
517
  u += '/';
455
- this.options.registries[name] = u;
518
+ (registries ??= { ...this.options.registries })[name] = u;
456
519
  }
457
520
  if (u === url)
458
521
  this.namedRegistry = name;
459
522
  }
523
+ if (registries)
524
+ this.options = { ...this.options, registries };
460
525
  this.#parseRegistrySpec(regSpec, url);
461
526
  this.#guessRegistryTarball();
462
527
  return;
@@ -639,10 +704,7 @@ export class Spec {
639
704
  #parseRegistrySpec(s, url) {
640
705
  // note: this takes priority over the scoped registry, if set
641
706
  this.registry = url;
642
- this.subspec = this.constructor.parse(s, {
643
- ...this.options,
644
- registry: url,
645
- });
707
+ this.subspec = this.constructor.parse(s, withRegistry(this.options, url));
646
708
  return this.subspec;
647
709
  }
648
710
  #parseJsrRegistrySpec(s, url) {
package/dist/types.d.ts CHANGED
@@ -7,10 +7,12 @@ export type SpecOptionsFilled = {
7
7
  /**
8
8
  * The registry where a spec should be resolved against.
9
9
  *
10
- * There is **no default**. If no registry is configured, registry-type
11
- * specs parsed without one will have `spec.registry === undefined`, and
12
- * anything that needs to build a registry URL will fail. Named registry
13
- * aliases (`gh:`, `jsr:`) and `scoped-registries` are unaffected.
10
+ * There is **no default**: when unset, bare specs resolve through the
11
+ * `default-registry-alias` (the built-in `npm` alias, vlt's public npm
12
+ * mirror, unless configured). Only when that alias has no URL do
13
+ * registry-type specs get `spec.registry === undefined`, and anything
14
+ * that needs to build a registry URL fails. Named registry aliases
15
+ * (`gh:`, `jsr:`) and `scoped-registries` are unaffected.
14
16
  *
15
17
  * For bare specs, this takes precedence over the
16
18
  * `default-registry-alias` lookup but not over a matching
@@ -23,9 +25,10 @@ export type SpecOptionsFilled = {
23
25
  * protocol resolve through, when no `registry` or `scoped-registries`
24
26
  * entry applies.
25
27
  *
26
- * Defaults to `'npm'`. The `npm` alias itself has **no** built-in URL --
27
- * it must be configured (e.g. via `vlt setup`), otherwise bare specs
28
- * resolve with `spec.registry === undefined`.
28
+ * Defaults to `'npm'`, whose built-in URL is vlt's public, read-only
29
+ * npm mirror (`https://registry.vlt.io/upstream/npm/`). `vlt setup` or
30
+ * `registries.npm` point it elsewhere. An alias with no URL makes bare
31
+ * specs resolve with `spec.registry === undefined`.
29
32
  */
30
33
  'default-registry-alias': string;
31
34
  /** shorthand prefix names for known registries */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/spec",
3
3
  "description": "Package specifier library",
4
- "version": "1.7.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.7.0",
17
- "@vltpkg/semver": "1.7.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",