@vltpkg/dep-id 1.0.0-rc.22 → 1.0.0-rc.24
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 +106 -0
- package/dist/browser.js +510 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +141 -0
- package/package.json +7 -7
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Spec } from '@vltpkg/spec/browser';
|
|
2
|
+
import type { SpecOptions } from '@vltpkg/spec/browser';
|
|
3
|
+
import type { Manifest } from '@vltpkg/types';
|
|
4
|
+
export declare const delimiter: Delimiter;
|
|
5
|
+
export type Delimiter = '~';
|
|
6
|
+
/**
|
|
7
|
+
* Dependency IDs are a URI-encoded set of strings, separated
|
|
8
|
+
* by the {@link Delimiter} character (`'~'`).
|
|
9
|
+
*
|
|
10
|
+
* The first entry is always the specifier type. The rest depend on the
|
|
11
|
+
* type. `git`, `registry`, and `workspace` entries have 3 fields, the rest
|
|
12
|
+
* have 2.
|
|
13
|
+
*
|
|
14
|
+
* - `registry`: `'registry~<registry>~name@specifier'`
|
|
15
|
+
* The `<registry>` portion can be a known named registry name, or a
|
|
16
|
+
* url to a registry. If empty, it is the default registry. Examples:
|
|
17
|
+
* - `~~some-package@2.0.1`
|
|
18
|
+
* - `~npm~whatever@1.2.3`
|
|
19
|
+
* - `~http%3A%2F%2Fvlt.sh%2F~x@1.2.3`
|
|
20
|
+
* - `git`: `'git~<git remote>~<git selector>'`. For example:
|
|
21
|
+
* - `git~github:user/project~branchname`
|
|
22
|
+
* - `git~git%2Bssh%3A%2F%2Fuser%40host%3Aproject.git~semver:1.x`
|
|
23
|
+
* - `workspace`: `'workspace~<path>'`. For example:
|
|
24
|
+
* - `workspace~src/mything`
|
|
25
|
+
* - `remote`: `'remote~<url>'`
|
|
26
|
+
* - `file`: `'file~<path>'`
|
|
27
|
+
*
|
|
28
|
+
* Lastly, the final portion can contain arbitrary string data, and is
|
|
29
|
+
* used to store peer dep resolutions to maintain the peerDep contract.
|
|
30
|
+
*/
|
|
31
|
+
export type DepID = `${'' | 'git'}${Delimiter}${string}${Delimiter}${string}${Delimiter}${string}` | `${'' | 'git'}${Delimiter}${string}${Delimiter}${string}` | `${'file' | 'remote' | 'workspace'}${Delimiter}${string}${Delimiter}${string}` | `${'file' | 'remote' | 'workspace'}${Delimiter}${string}`;
|
|
32
|
+
/**
|
|
33
|
+
* A {@link DepID}, split apart and URI-decoded
|
|
34
|
+
*/
|
|
35
|
+
export type DepIDTuple = [
|
|
36
|
+
type: 'git',
|
|
37
|
+
gitRemote: string,
|
|
38
|
+
gitSelector: string,
|
|
39
|
+
extra?: string
|
|
40
|
+
] | [
|
|
41
|
+
type: 'registry',
|
|
42
|
+
registry: string,
|
|
43
|
+
registrySpec: string,
|
|
44
|
+
extra?: string
|
|
45
|
+
] | [type: 'file', path: string, extra?: string] | [type: 'remote', url: string, extra?: string] | [type: 'workspace', workspace: string, extra?: string];
|
|
46
|
+
export declare const isDepID: (str: unknown) => str is DepID;
|
|
47
|
+
export declare const asDepID: (str: string) => DepID;
|
|
48
|
+
/**
|
|
49
|
+
* turn a {@link DepIDTuple} into a {@link DepID}
|
|
50
|
+
*/
|
|
51
|
+
export declare const joinDepIDTuple: (list: DepIDTuple) => DepID;
|
|
52
|
+
/**
|
|
53
|
+
* turn a {@link DepID} into a {@link DepIDTuple}
|
|
54
|
+
*/
|
|
55
|
+
export declare const splitDepID: (id: string) => DepIDTuple;
|
|
56
|
+
/**
|
|
57
|
+
* Retrieves the base {@link DepID} for a given depID,
|
|
58
|
+
* ignoring any extra information that may be present.
|
|
59
|
+
*/
|
|
60
|
+
export declare const baseDepID: (id: string) => DepID;
|
|
61
|
+
/**
|
|
62
|
+
* Turn a {@link DepID} into a {@link Spec} object
|
|
63
|
+
*/
|
|
64
|
+
export declare const hydrate: (id: DepID, name?: string, options?: SpecOptions) => Spec;
|
|
65
|
+
/**
|
|
66
|
+
* Turn a {@link DepIDTuple} into a {@link Spec} object
|
|
67
|
+
*/
|
|
68
|
+
export declare const hydrateTuple: (tuple: DepIDTuple, name?: string, options?: SpecOptions) => Spec;
|
|
69
|
+
/**
|
|
70
|
+
* Get the {@link DepIDTuple} for a given {@link Spec} and {@link Manifest}.
|
|
71
|
+
* The Manifest is used to get the name and version, if possible. If not found
|
|
72
|
+
* in the manifest, registry ID types will use the name or bareSpec from the
|
|
73
|
+
* specifier, so at least there's something to use later.
|
|
74
|
+
*/
|
|
75
|
+
export declare const getTuple: (spec: Spec, mani: Pick<Manifest, "name" | "version">, extra?: string) => DepIDTuple;
|
|
76
|
+
/**
|
|
77
|
+
* Checks for a potentially manifest-confused package name.
|
|
78
|
+
* Returns `true` if the package name is confused, `false` otherwise.
|
|
79
|
+
*/
|
|
80
|
+
export declare const isPackageNameConfused: (spec?: Spec, name?: string) => boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Get the {@link DepID} for a given {@link Spec} and {@link Manifest}. The
|
|
83
|
+
* Manifest is used to get the name and version, if possible. If not found in
|
|
84
|
+
* the manifest, registry ID types will use the name or bareSpec from the
|
|
85
|
+
* specifier, so at least there's something to use later.
|
|
86
|
+
*/
|
|
87
|
+
export declare const getId: (spec: Spec, mani: Pick<Manifest, "name" | "version">, extra?: string) => DepID;
|
|
88
|
+
/**
|
|
89
|
+
* Join a modifier and a peer set hash into a single "extra" string.
|
|
90
|
+
*/
|
|
91
|
+
export declare function joinExtra({ modifier, peerSetHash, }: {
|
|
92
|
+
modifier?: string;
|
|
93
|
+
peerSetHash?: string;
|
|
94
|
+
}): string | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* Split an "extra" string into a modifier and a peerSetHash.
|
|
97
|
+
*/
|
|
98
|
+
export declare function splitExtra(extra: string): {
|
|
99
|
+
modifier?: string;
|
|
100
|
+
peerSetHash?: string;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Reset internal caches. This should be used when options change since
|
|
104
|
+
* they're not take into account in the memoization keys.
|
|
105
|
+
*/
|
|
106
|
+
export declare const resetCaches: () => void;
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
import { error } from '@vltpkg/error-cause';
|
|
2
|
+
import { currentDefaultRegistryName, defaultRegistryName, Spec, } from '@vltpkg/spec/browser';
|
|
3
|
+
export const delimiter = '~';
|
|
4
|
+
const EXTRA_PEER_SET_DELIMITER = 'peer.';
|
|
5
|
+
const depIDRegExp = new RegExp(`^((git)?${delimiter}[^${delimiter}]*${delimiter}[^${delimiter}]*(${delimiter}[^${delimiter}]*)?$` +
|
|
6
|
+
`|` +
|
|
7
|
+
`^(file|remote|workspace)${delimiter}[^${delimiter}]*)(${delimiter}[^${delimiter}]*)?$`);
|
|
8
|
+
export const isDepID = (str) => typeof str === 'string' && depIDRegExp.test(str);
|
|
9
|
+
export const asDepID = (str) => {
|
|
10
|
+
if (!isDepID(str)) {
|
|
11
|
+
throw error('Expected dep id', {
|
|
12
|
+
found: str,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
return str;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* turn a {@link DepIDTuple} into a {@link DepID}
|
|
19
|
+
*/
|
|
20
|
+
export const joinDepIDTuple = (list) => {
|
|
21
|
+
const [type, first, second, extra] = list;
|
|
22
|
+
const f = encode(first);
|
|
23
|
+
switch (type) {
|
|
24
|
+
case 'registry':
|
|
25
|
+
return `${delimiter}${f || defaultRegistryName}${delimiter}${encode(second)}${extra ? `${delimiter}${encode(extra)}` : ''}`;
|
|
26
|
+
case 'git':
|
|
27
|
+
return `${type}${delimiter}${f}${delimiter}${encode(second)}${extra ? `${delimiter}${encode(extra)}` : ''}`;
|
|
28
|
+
default:
|
|
29
|
+
return `${type}${delimiter}${f}${second ? `${delimiter}${encode(second)}` : ''}`;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
// Escape chars unsafe for filesystems or reserved for DepID syntax
|
|
33
|
+
const ESCAPE = {
|
|
34
|
+
_: '__',
|
|
35
|
+
'+': '_p',
|
|
36
|
+
'\\': '_b',
|
|
37
|
+
':': '_c',
|
|
38
|
+
'~': '_t',
|
|
39
|
+
'<': '_l',
|
|
40
|
+
'>': '_g',
|
|
41
|
+
'"': '_q',
|
|
42
|
+
'|': '_i',
|
|
43
|
+
'?': '_m',
|
|
44
|
+
'*': '_a',
|
|
45
|
+
' ': '_s',
|
|
46
|
+
};
|
|
47
|
+
// Unescape map (includes trailing dot/space escapes)
|
|
48
|
+
const UNESCAPE = {
|
|
49
|
+
__: '_',
|
|
50
|
+
_p: '+',
|
|
51
|
+
_b: '\\',
|
|
52
|
+
_c: ':',
|
|
53
|
+
_t: '~',
|
|
54
|
+
_l: '<',
|
|
55
|
+
_g: '>',
|
|
56
|
+
_q: '"',
|
|
57
|
+
_i: '|',
|
|
58
|
+
_m: '?',
|
|
59
|
+
_a: '*',
|
|
60
|
+
_d: '.',
|
|
61
|
+
_s: ' ',
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Checks if a character code represents a control character (0x00-0x1F).
|
|
65
|
+
* Control characters are non-printable ASCII characters that need special encoding.
|
|
66
|
+
*/
|
|
67
|
+
const isControl = (code) => code >= 0x00 && code <= 0x1f;
|
|
68
|
+
/**
|
|
69
|
+
* Converts a character code to a 2-digit uppercase hexadecimal string.
|
|
70
|
+
* Used to encode control characters as _XX format (e.g., 0x0A becomes "0A").
|
|
71
|
+
*/
|
|
72
|
+
const toHex2Upper = (code) => code.toString(16).toUpperCase().padStart(2, '0');
|
|
73
|
+
/**
|
|
74
|
+
* Checks if a character is a valid hexadecimal digit (0-9, A-F, a-f).
|
|
75
|
+
* Used during decoding to identify hex-encoded control characters (e.g., _0A, _1F).
|
|
76
|
+
*/
|
|
77
|
+
const isHexDigit = (c) => (c >= '0' && c <= '9') ||
|
|
78
|
+
(c >= 'A' && c <= 'F') ||
|
|
79
|
+
(c >= 'a' && c <= 'f');
|
|
80
|
+
/**
|
|
81
|
+
* Encodes a string into a DepID format.
|
|
82
|
+
* Handles special characters, control characters, and trailing dots.
|
|
83
|
+
*/
|
|
84
|
+
const encode = (s) => {
|
|
85
|
+
if (!s)
|
|
86
|
+
return s;
|
|
87
|
+
let out = '';
|
|
88
|
+
// Loop through each character in the string
|
|
89
|
+
for (const ch of s) {
|
|
90
|
+
/* c8 ignore next */
|
|
91
|
+
if (!ch)
|
|
92
|
+
continue;
|
|
93
|
+
// '/' becomes '+', and that '+' is NOT escaped
|
|
94
|
+
if (ch === '/') {
|
|
95
|
+
out += '+';
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
// Escape special characters with predefined mappings
|
|
99
|
+
const mapped = ESCAPE[ch];
|
|
100
|
+
if (mapped) {
|
|
101
|
+
// Found a special character that needs escaping, append the escaped version
|
|
102
|
+
out += mapped;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
// Check if the character is a control character and encode it as _XX
|
|
106
|
+
const code = ch.charCodeAt(0);
|
|
107
|
+
if (isControl(code)) {
|
|
108
|
+
// Found a control character, encode it as _XX
|
|
109
|
+
out += '_' + toHex2Upper(code); // _00, _0A, _1F, etc.
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
// If no special characters or control characters were found, append the character as-is
|
|
113
|
+
out += ch;
|
|
114
|
+
}
|
|
115
|
+
// Escape trailing dot (Windows strips these)
|
|
116
|
+
if (out.endsWith('.'))
|
|
117
|
+
out = out.slice(0, -1) + '_d';
|
|
118
|
+
return out;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Decodes a DepID string back into its original form.
|
|
122
|
+
* Handles special characters, control characters, and trailing dots.
|
|
123
|
+
*/
|
|
124
|
+
const decode = (s) => {
|
|
125
|
+
if (!s)
|
|
126
|
+
return s;
|
|
127
|
+
let out = '';
|
|
128
|
+
// Loop through each character in the string
|
|
129
|
+
for (let i = 0; i < s.length; i++) {
|
|
130
|
+
const ch = s[i];
|
|
131
|
+
/* c8 ignore next */
|
|
132
|
+
if (!ch)
|
|
133
|
+
continue;
|
|
134
|
+
if (ch === '+') {
|
|
135
|
+
out += '/';
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (ch !== '_') {
|
|
139
|
+
out += ch;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
// Try to parse an escape sequence starting at '_'
|
|
143
|
+
const next = s[i + 1];
|
|
144
|
+
if (next === undefined) {
|
|
145
|
+
out += '_';
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
// "__" -> "_"
|
|
149
|
+
if (next === '_') {
|
|
150
|
+
out += '_';
|
|
151
|
+
i += 1;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
// "_X" single-letter escapes
|
|
155
|
+
const two = s.slice(i, i + 2); // "_p", "_d", etc.
|
|
156
|
+
const mapped = UNESCAPE[two];
|
|
157
|
+
if (mapped) {
|
|
158
|
+
out += mapped;
|
|
159
|
+
i += 1;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
// "_XX" hex escape for control chars, where first hex digit is 0 or 1
|
|
163
|
+
const h1 = s[i + 1];
|
|
164
|
+
const h2 = s[i + 2];
|
|
165
|
+
if ((h1 === '0' || h1 === '1') &&
|
|
166
|
+
h2 !== undefined &&
|
|
167
|
+
isHexDigit(h2)) {
|
|
168
|
+
const hex = s.slice(i + 1, i + 3); // "0A", "1F", etc.
|
|
169
|
+
out += String.fromCharCode(parseInt(hex, 16));
|
|
170
|
+
i += 2;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
// Not a recognized escape; leave '_' as-is
|
|
174
|
+
out += '_';
|
|
175
|
+
}
|
|
176
|
+
return out;
|
|
177
|
+
};
|
|
178
|
+
const seenSplitDepIDs = new Map();
|
|
179
|
+
/**
|
|
180
|
+
* turn a {@link DepID} into a {@link DepIDTuple}
|
|
181
|
+
*/
|
|
182
|
+
export const splitDepID = (id) => {
|
|
183
|
+
// memoized entries return early
|
|
184
|
+
const seen = seenSplitDepIDs.get(id);
|
|
185
|
+
if (seen)
|
|
186
|
+
return seen;
|
|
187
|
+
let res;
|
|
188
|
+
const [type, first = '', second, extra] = id.split(delimiter, 4);
|
|
189
|
+
const f = decode(first) || ''; // decode handles + -> / internally
|
|
190
|
+
switch (type) {
|
|
191
|
+
case 'git':
|
|
192
|
+
case '': {
|
|
193
|
+
if (second === undefined) {
|
|
194
|
+
throw error(`invalid ${type} id`, { found: id });
|
|
195
|
+
}
|
|
196
|
+
const decodedSecond = decode(second) || '';
|
|
197
|
+
res = [
|
|
198
|
+
type || 'registry',
|
|
199
|
+
f || defaultRegistryName,
|
|
200
|
+
decodedSecond,
|
|
201
|
+
decode(extra),
|
|
202
|
+
];
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
case 'file':
|
|
206
|
+
case 'remote':
|
|
207
|
+
case 'workspace': {
|
|
208
|
+
res = [type, f, decode(second)];
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
default: {
|
|
212
|
+
throw error('invalid DepID type', {
|
|
213
|
+
found: type,
|
|
214
|
+
validOptions: ['git', 'file', 'workspace', 'remote', ''],
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
seenSplitDepIDs.set(id, res);
|
|
219
|
+
return res;
|
|
220
|
+
};
|
|
221
|
+
/**
|
|
222
|
+
* Retrieves the base {@link DepID} for a given depID,
|
|
223
|
+
* ignoring any extra information that may be present.
|
|
224
|
+
*/
|
|
225
|
+
export const baseDepID = (id) => {
|
|
226
|
+
const [type, first, second] = splitDepID(id);
|
|
227
|
+
switch (type) {
|
|
228
|
+
case 'git':
|
|
229
|
+
case 'registry':
|
|
230
|
+
return joinDepIDTuple([type, first, second]);
|
|
231
|
+
default:
|
|
232
|
+
return joinDepIDTuple([type, first]);
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
const seenHydrated = new Map();
|
|
236
|
+
/**
|
|
237
|
+
* Turn a {@link DepID} into a {@link Spec} object
|
|
238
|
+
*/
|
|
239
|
+
export const hydrate = (id, name, options = {}) => {
|
|
240
|
+
// memozied entries return early
|
|
241
|
+
const cacheKey = (name ?? '') + id;
|
|
242
|
+
const seen = seenHydrated.get(cacheKey);
|
|
243
|
+
if (seen)
|
|
244
|
+
return seen;
|
|
245
|
+
const res = hydrateTuple(splitDepID(id), name, options);
|
|
246
|
+
seenHydrated.set(cacheKey, res);
|
|
247
|
+
return res;
|
|
248
|
+
};
|
|
249
|
+
const seenHydratedTuples = new Map();
|
|
250
|
+
/**
|
|
251
|
+
* Turn a {@link DepIDTuple} into a {@link Spec} object
|
|
252
|
+
*/
|
|
253
|
+
export const hydrateTuple = (tuple, name, options = {}) => {
|
|
254
|
+
const [type, first, second] = tuple;
|
|
255
|
+
// memoized entries return early
|
|
256
|
+
const cacheKey = (name ?? '') + ',' + type + ',' + first + ',' + (second ?? '');
|
|
257
|
+
const seen = seenHydratedTuples.get(cacheKey);
|
|
258
|
+
if (seen)
|
|
259
|
+
return seen;
|
|
260
|
+
let res;
|
|
261
|
+
switch (type) {
|
|
262
|
+
case 'remote': {
|
|
263
|
+
if (!first)
|
|
264
|
+
throw error('no remoteURL found on remote id', {
|
|
265
|
+
found: tuple,
|
|
266
|
+
});
|
|
267
|
+
res = Spec.parse(name ?? '(unknown)', first, options);
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
case 'file': {
|
|
271
|
+
if (!first) {
|
|
272
|
+
throw error('no file path found on remote id', {
|
|
273
|
+
found: tuple,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
res = Spec.parse(name ?? '(unknown)', `file:${first}`, options);
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
case 'registry': {
|
|
280
|
+
if (typeof first !== 'string') {
|
|
281
|
+
throw error('no registry url or name in registry ID', {
|
|
282
|
+
found: tuple,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
if (!second) {
|
|
286
|
+
throw error('no name/specifier in registry ID', {
|
|
287
|
+
found: tuple,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
const defaultName = options.registry &&
|
|
291
|
+
currentDefaultRegistryName(options.registry, options);
|
|
292
|
+
const defaultRegistryURL = options.registry ?
|
|
293
|
+
options.registry.endsWith('/') ?
|
|
294
|
+
options.registry
|
|
295
|
+
: options.registry + '/'
|
|
296
|
+
: undefined;
|
|
297
|
+
const firstURL = first.endsWith('/') ? first : first + '/';
|
|
298
|
+
const hasScope = second.startsWith('@');
|
|
299
|
+
const hasAtVersion = second.includes('@', hasScope ? 1 : 0);
|
|
300
|
+
const name_ = (hasAtVersion && hasScope ?
|
|
301
|
+
`@${second.split('@')[1]}`
|
|
302
|
+
: second.split('@')[0]) /* c8 ignore next */ || '(unknown)';
|
|
303
|
+
const usesDefaultRegistry = !first ||
|
|
304
|
+
first === defaultName ||
|
|
305
|
+
firstURL === defaultRegistryURL;
|
|
306
|
+
const noAliasedNameUsed = !name || name === name_;
|
|
307
|
+
if (usesDefaultRegistry && noAliasedNameUsed) {
|
|
308
|
+
const version = (hasAtVersion &&
|
|
309
|
+
second.split('@')[hasScope ? 2 : 1]) /* c8 ignore next */ ||
|
|
310
|
+
second;
|
|
311
|
+
res = Spec.parse(name || name_, version, options);
|
|
312
|
+
break;
|
|
313
|
+
}
|
|
314
|
+
if (!/^https?:\/\//.test(first)) {
|
|
315
|
+
const reg = options.registries?.[first];
|
|
316
|
+
const jsrReg = options['jsr-registries']?.[first];
|
|
317
|
+
if (first !== defaultRegistryName && !reg && !jsrReg) {
|
|
318
|
+
throw error('named registry not found in options', {
|
|
319
|
+
name: first,
|
|
320
|
+
found: tuple,
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
res = Spec.parse(name || name_, `${first}:${second}`, options);
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
res = Spec.parse(name || name_, `registry:${first}#${second}`, options);
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
case 'git': {
|
|
330
|
+
if (!first) {
|
|
331
|
+
throw error('no git remote in git ID', {
|
|
332
|
+
found: tuple,
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
res = Spec.parse(name ?? '(unknown)', first + '#' + second, options);
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
case 'workspace': {
|
|
339
|
+
if (!first) {
|
|
340
|
+
throw error('no name/path on workspace id', { found: tuple });
|
|
341
|
+
}
|
|
342
|
+
res =
|
|
343
|
+
name && name !== first ?
|
|
344
|
+
Spec.parse(name, `workspace:${first}@*`, options)
|
|
345
|
+
: Spec.parse(first, `workspace:*`, options);
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
seenHydratedTuples.set(cacheKey, res);
|
|
350
|
+
return res;
|
|
351
|
+
};
|
|
352
|
+
const matchRegistryURL = (registry, registries) => {
|
|
353
|
+
// iterate on known registries to check if any known value
|
|
354
|
+
// matches the current default registry of this Spec value
|
|
355
|
+
for (const [alias, url] of Object.entries(registries)) {
|
|
356
|
+
// normalize trailing slash for comparison
|
|
357
|
+
const specRegURL = registry.endsWith('/') ? registry : registry + '/';
|
|
358
|
+
const knownRegURL = url.endsWith('/') ? url : url + '/';
|
|
359
|
+
if (specRegURL === knownRegURL) {
|
|
360
|
+
return alias;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
/**
|
|
365
|
+
* Convert a Spec's registry URL to a known short registry name, if possible.
|
|
366
|
+
*/
|
|
367
|
+
const convertToKnownShortRegistryName = (s, registry) => {
|
|
368
|
+
if (!s.registry)
|
|
369
|
+
return;
|
|
370
|
+
const reg = registry ?? s.registry;
|
|
371
|
+
const namedRegistry = matchRegistryURL(reg, s.options.registries);
|
|
372
|
+
if (namedRegistry)
|
|
373
|
+
return namedRegistry;
|
|
374
|
+
const namedJSRRegistry = matchRegistryURL(reg, s.options['jsr-registries']);
|
|
375
|
+
if (namedJSRRegistry)
|
|
376
|
+
return namedJSRRegistry;
|
|
377
|
+
};
|
|
378
|
+
const seenTuples = new Map();
|
|
379
|
+
/**
|
|
380
|
+
* Get the {@link DepIDTuple} for a given {@link Spec} and {@link Manifest}.
|
|
381
|
+
* The Manifest is used to get the name and version, if possible. If not found
|
|
382
|
+
* in the manifest, registry ID types will use the name or bareSpec from the
|
|
383
|
+
* specifier, so at least there's something to use later.
|
|
384
|
+
*/
|
|
385
|
+
export const getTuple = (spec, mani, extra) => {
|
|
386
|
+
// memoized entries return early
|
|
387
|
+
const cacheKey = String(spec) +
|
|
388
|
+
(spec.registry ?? '') +
|
|
389
|
+
(mani.name ?? '') +
|
|
390
|
+
(mani.version ?? '') +
|
|
391
|
+
(extra ?? '');
|
|
392
|
+
const seen = seenTuples.get(cacheKey);
|
|
393
|
+
if (seen)
|
|
394
|
+
return seen;
|
|
395
|
+
let res;
|
|
396
|
+
const f = spec.final;
|
|
397
|
+
switch (f.type) {
|
|
398
|
+
case 'registry': {
|
|
399
|
+
const version = mani.version ?
|
|
400
|
+
mani.version.startsWith('v') ?
|
|
401
|
+
mani.version.slice(1)
|
|
402
|
+
: mani.version
|
|
403
|
+
: f.bareSpec;
|
|
404
|
+
res = [
|
|
405
|
+
f.type,
|
|
406
|
+
f.namedRegistry ||
|
|
407
|
+
convertToKnownShortRegistryName(f, f.scopeRegistry) ||
|
|
408
|
+
f.scopeRegistry ||
|
|
409
|
+
f.registry ||
|
|
410
|
+
defaultRegistryName,
|
|
411
|
+
`${isPackageNameConfused(spec, mani.name) ? spec.name : (mani.name ?? f.name)}@${version}`,
|
|
412
|
+
extra,
|
|
413
|
+
];
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
case 'git': {
|
|
417
|
+
const { namedGitHost, namedGitHostPath, gitRemote, gitSelector = '', } = f;
|
|
418
|
+
if (!gitRemote)
|
|
419
|
+
throw error('no host on git specifier', { spec });
|
|
420
|
+
if (namedGitHost) {
|
|
421
|
+
if (!namedGitHostPath) {
|
|
422
|
+
throw error('named git host without path portion', {
|
|
423
|
+
spec,
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
res = [
|
|
427
|
+
f.type,
|
|
428
|
+
`${namedGitHost}:${namedGitHostPath}`,
|
|
429
|
+
gitSelector,
|
|
430
|
+
extra,
|
|
431
|
+
];
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
res = [f.type, gitRemote, gitSelector, extra];
|
|
436
|
+
}
|
|
437
|
+
break;
|
|
438
|
+
}
|
|
439
|
+
case 'remote': {
|
|
440
|
+
const { remoteURL } = f;
|
|
441
|
+
if (!remoteURL)
|
|
442
|
+
throw error('no URL on remote specifier', { spec });
|
|
443
|
+
res = [f.type, remoteURL, extra];
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
case 'file':
|
|
447
|
+
case 'workspace':
|
|
448
|
+
throw error('Path-based dep ids are not supported', { spec });
|
|
449
|
+
}
|
|
450
|
+
seenTuples.set(cacheKey, res);
|
|
451
|
+
return res;
|
|
452
|
+
};
|
|
453
|
+
/**
|
|
454
|
+
* Checks for a potentially manifest-confused package name.
|
|
455
|
+
* Returns `true` if the package name is confused, `false` otherwise.
|
|
456
|
+
*/
|
|
457
|
+
export const isPackageNameConfused = (spec, name) => !!spec?.name && // a nameless spec can't be checked
|
|
458
|
+
!spec.subspec && // it's not an aliased package or using a custom protocol
|
|
459
|
+
spec.type === 'registry' && // the defined spec is of type registry
|
|
460
|
+
spec.name !== name; // its name is not the same as the defined spec name
|
|
461
|
+
/**
|
|
462
|
+
* Get the {@link DepID} for a given {@link Spec} and {@link Manifest}. The
|
|
463
|
+
* Manifest is used to get the name and version, if possible. If not found in
|
|
464
|
+
* the manifest, registry ID types will use the name or bareSpec from the
|
|
465
|
+
* specifier, so at least there's something to use later.
|
|
466
|
+
*/
|
|
467
|
+
export const getId = (spec, mani, extra) => joinDepIDTuple(getTuple(spec, mani, extra));
|
|
468
|
+
/**
|
|
469
|
+
* Join a modifier and a peer set hash into a single "extra" string.
|
|
470
|
+
*/
|
|
471
|
+
export function joinExtra({ modifier, peerSetHash, }) {
|
|
472
|
+
if (!modifier && !peerSetHash)
|
|
473
|
+
return;
|
|
474
|
+
if (modifier && peerSetHash)
|
|
475
|
+
return `${modifier}${peerSetHash}`;
|
|
476
|
+
if (modifier)
|
|
477
|
+
return modifier;
|
|
478
|
+
return peerSetHash;
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Split an "extra" string into a modifier and a peerSetHash.
|
|
482
|
+
*/
|
|
483
|
+
export function splitExtra(extra) {
|
|
484
|
+
if (!extra)
|
|
485
|
+
return {};
|
|
486
|
+
// only peerSetHash is present, return it
|
|
487
|
+
if (extra.startsWith(EXTRA_PEER_SET_DELIMITER)) {
|
|
488
|
+
return { peerSetHash: extra };
|
|
489
|
+
}
|
|
490
|
+
// only modifier is present, return its value
|
|
491
|
+
const idx = extra.indexOf(EXTRA_PEER_SET_DELIMITER);
|
|
492
|
+
if (idx === -1) {
|
|
493
|
+
return { modifier: extra };
|
|
494
|
+
}
|
|
495
|
+
// Both modifier and peerSetHash are present
|
|
496
|
+
return {
|
|
497
|
+
modifier: extra.slice(0, idx),
|
|
498
|
+
peerSetHash: extra.slice(idx),
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Reset internal caches. This should be used when options change since
|
|
503
|
+
* they're not take into account in the memoization keys.
|
|
504
|
+
*/
|
|
505
|
+
export const resetCaches = () => {
|
|
506
|
+
seenHydrated.clear();
|
|
507
|
+
seenHydratedTuples.clear();
|
|
508
|
+
seenSplitDepIDs.clear();
|
|
509
|
+
seenTuples.clear();
|
|
510
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Spec } from '@vltpkg/spec';
|
|
2
|
+
import type { SpecOptions } from '@vltpkg/spec';
|
|
3
|
+
import type { DepID, DepIDTuple } from './browser.ts';
|
|
4
|
+
export * from './browser.ts';
|
|
5
|
+
/**
|
|
6
|
+
* Turn a {@link DepID} into a {@link Spec} object using
|
|
7
|
+
* the Node.js-specific `Spec.parse` method.
|
|
8
|
+
*/
|
|
9
|
+
export declare const hydrate: (id: DepID, name?: string, options?: SpecOptions) => Spec;
|
|
10
|
+
/**
|
|
11
|
+
* Turn a {@link DepIDTuple} into a {@link Spec} object using
|
|
12
|
+
* the Node.js-specific `Spec.parse` method.
|
|
13
|
+
*/
|
|
14
|
+
export declare const hydrateTuple: (tuple: DepIDTuple, name?: string, options?: SpecOptions) => Spec;
|
|
15
|
+
/**
|
|
16
|
+
* Reset internal caches. This should be used when options change since
|
|
17
|
+
* they're not take into account in the memoization keys.
|
|
18
|
+
*/
|
|
19
|
+
export declare const resetCaches: () => void;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { error } from '@vltpkg/error-cause';
|
|
2
|
+
import { currentDefaultRegistryName, defaultRegistryName, Spec, } from '@vltpkg/spec';
|
|
3
|
+
import { resetCaches as browserResetCaches, splitDepID, } from "./browser.js";
|
|
4
|
+
// Here we re-export the browser-specific parts of the implementation
|
|
5
|
+
// we disable the eslint rules for import/export since we are intentionally
|
|
6
|
+
// re-exporting the `hydrate` and `hydrateTuple` functions to make sure they
|
|
7
|
+
// use the Node.js-specific `Spec.parse` method that relies on internal node
|
|
8
|
+
// modules like `util` and `path` that are not isomorphic.
|
|
9
|
+
// eslint-disable-next-line import/export
|
|
10
|
+
export * from "./browser.js";
|
|
11
|
+
const seenHydrated = new Map();
|
|
12
|
+
/**
|
|
13
|
+
* Turn a {@link DepID} into a {@link Spec} object using
|
|
14
|
+
* the Node.js-specific `Spec.parse` method.
|
|
15
|
+
*/
|
|
16
|
+
// eslint-disable-next-line import/export
|
|
17
|
+
export const hydrate = (id, name, options = {}) => {
|
|
18
|
+
// memoized entries return early
|
|
19
|
+
const cacheKey = (name ?? '') + id;
|
|
20
|
+
const seen = seenHydrated.get(cacheKey);
|
|
21
|
+
if (seen)
|
|
22
|
+
return seen;
|
|
23
|
+
const res = hydrateTuple(splitDepID(id), name, options);
|
|
24
|
+
seenHydrated.set(cacheKey, res);
|
|
25
|
+
return res;
|
|
26
|
+
};
|
|
27
|
+
const seenHydratedTuples = new Map();
|
|
28
|
+
/**
|
|
29
|
+
* Turn a {@link DepIDTuple} into a {@link Spec} object using
|
|
30
|
+
* the Node.js-specific `Spec.parse` method.
|
|
31
|
+
*/
|
|
32
|
+
// eslint-disable-next-line import/export
|
|
33
|
+
export const hydrateTuple = (tuple, name, options = {}) => {
|
|
34
|
+
const [type, first, second] = tuple;
|
|
35
|
+
// memoized entries return early
|
|
36
|
+
const cacheKey = (name ?? '') + ',' + type + ',' + first + ',' + (second ?? '');
|
|
37
|
+
const seen = seenHydratedTuples.get(cacheKey);
|
|
38
|
+
if (seen)
|
|
39
|
+
return seen;
|
|
40
|
+
let res;
|
|
41
|
+
switch (type) {
|
|
42
|
+
case 'remote': {
|
|
43
|
+
if (!first)
|
|
44
|
+
throw error('no remoteURL found on remote id', {
|
|
45
|
+
found: tuple,
|
|
46
|
+
});
|
|
47
|
+
res = Spec.parse(name ?? '(unknown)', first, options);
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case 'file': {
|
|
51
|
+
if (!first) {
|
|
52
|
+
throw error('no file path found on remote id', {
|
|
53
|
+
found: tuple,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
res = Spec.parse(name ?? '(unknown)', `file:${first}`, options);
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
case 'registry': {
|
|
60
|
+
if (typeof first !== 'string') {
|
|
61
|
+
throw error('no registry url or name in registry ID', {
|
|
62
|
+
found: tuple,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
if (!second) {
|
|
66
|
+
throw error('no name/specifier in registry ID', {
|
|
67
|
+
found: tuple,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
const defaultName = options.registry &&
|
|
71
|
+
currentDefaultRegistryName(options.registry, options);
|
|
72
|
+
const defaultRegistryURL = options.registry ?
|
|
73
|
+
options.registry.endsWith('/') ?
|
|
74
|
+
options.registry
|
|
75
|
+
: options.registry + '/'
|
|
76
|
+
: undefined;
|
|
77
|
+
const firstURL = first.endsWith('/') ? first : first + '/';
|
|
78
|
+
const hasScope = second.startsWith('@');
|
|
79
|
+
const hasAtVersion = second.includes('@', hasScope ? 1 : 0);
|
|
80
|
+
const name_ = (hasAtVersion && hasScope ?
|
|
81
|
+
`@${second.split('@')[1]}`
|
|
82
|
+
: second.split('@')[0]) /* c8 ignore next */ || '(unknown)';
|
|
83
|
+
const usesDefaultRegistry = !first ||
|
|
84
|
+
first === defaultName ||
|
|
85
|
+
firstURL === defaultRegistryURL;
|
|
86
|
+
const noAliasedNameUsed = !name || name === name_;
|
|
87
|
+
if (usesDefaultRegistry && noAliasedNameUsed) {
|
|
88
|
+
const version = (hasAtVersion &&
|
|
89
|
+
second.split('@')[hasScope ? 2 : 1]) /* c8 ignore next */ ||
|
|
90
|
+
second;
|
|
91
|
+
res = Spec.parse(name || name_, version, options);
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
if (!/^https?:\/\//.test(first)) {
|
|
95
|
+
const reg = options.registries?.[first];
|
|
96
|
+
const jsrReg = options['jsr-registries']?.[first];
|
|
97
|
+
if (first !== defaultRegistryName && !reg && !jsrReg) {
|
|
98
|
+
throw error('named registry not found in options', {
|
|
99
|
+
name: first,
|
|
100
|
+
found: tuple,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
res = Spec.parse(name || name_, `${first}:${second}`, options);
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
res = Spec.parse(name || name_, `registry:${first}#${second}`, options);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
case 'git': {
|
|
110
|
+
if (!first) {
|
|
111
|
+
throw error('no git remote in git ID', {
|
|
112
|
+
found: tuple,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
res = Spec.parse(name ?? '(unknown)', first + '#' + second, options);
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
case 'workspace': {
|
|
119
|
+
if (!first) {
|
|
120
|
+
throw error('no name/path on workspace id', { found: tuple });
|
|
121
|
+
}
|
|
122
|
+
res =
|
|
123
|
+
name && name !== first ?
|
|
124
|
+
Spec.parse(name, `workspace:${first}@*`, options)
|
|
125
|
+
: Spec.parse(first, `workspace:*`, options);
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
seenHydratedTuples.set(cacheKey, res);
|
|
130
|
+
return res;
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* Reset internal caches. This should be used when options change since
|
|
134
|
+
* they're not take into account in the memoization keys.
|
|
135
|
+
*/
|
|
136
|
+
// eslint-disable-next-line import/export
|
|
137
|
+
export const resetCaches = () => {
|
|
138
|
+
seenHydrated.clear();
|
|
139
|
+
seenHydratedTuples.clear();
|
|
140
|
+
browserResetCaches();
|
|
141
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vltpkg/dep-id",
|
|
3
3
|
"description": "create and hydrate dependency IDs",
|
|
4
|
-
"version": "1.0.0-rc.
|
|
4
|
+
"version": "1.0.0-rc.24",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/vltpkg/vltpkg.git",
|
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
"email": "support@vlt.sh"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@vltpkg/error-cause": "1.0.0-rc.
|
|
16
|
-
"@vltpkg/spec": "1.0.0-rc.
|
|
17
|
-
"@vltpkg/types": "1.0.0-rc.
|
|
15
|
+
"@vltpkg/error-cause": "1.0.0-rc.24",
|
|
16
|
+
"@vltpkg/spec": "1.0.0-rc.24",
|
|
17
|
+
"@vltpkg/types": "1.0.0-rc.24"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@eslint/js": "^9.39.1",
|
|
@@ -45,18 +45,18 @@
|
|
|
45
45
|
"extends": "../../tap-config.yaml"
|
|
46
46
|
},
|
|
47
47
|
"prettier": "../../.prettierrc.js",
|
|
48
|
-
"module": "./
|
|
48
|
+
"module": "./dist/index.js",
|
|
49
49
|
"type": "module",
|
|
50
50
|
"exports": {
|
|
51
51
|
"./package.json": "./package.json",
|
|
52
52
|
".": {
|
|
53
53
|
"import": {
|
|
54
|
-
"default": "./
|
|
54
|
+
"default": "./dist/index.js"
|
|
55
55
|
}
|
|
56
56
|
},
|
|
57
57
|
"./browser": {
|
|
58
58
|
"import": {
|
|
59
|
-
"default": "./
|
|
59
|
+
"default": "./dist/browser.js"
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
},
|