@vltpkg/spec 0.0.0-0.1730239248325

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,654 @@
1
+ import { error, typeError, } from '@vltpkg/error-cause';
2
+ import { parseRange } from '@vltpkg/semver';
3
+ export * from './types.js';
4
+ export const kCustomInspect = Symbol.for('nodejs.util.inspect.custom');
5
+ export const defaultRegistry = 'https://registry.npmjs.org/';
6
+ export const defaultRegistries = {
7
+ npm: 'https://registry.npmjs.org/',
8
+ };
9
+ export const defaultGitHosts = {
10
+ github: 'git+ssh://git@github.com:$1/$2.git',
11
+ bitbucket: 'git+ssh://git@bitbucket.org:$1/$2.git',
12
+ gitlab: 'git+ssh://git@gitlab.com:$1/$2.git',
13
+ gist: 'git+ssh://git@gist.github.com/$1.git',
14
+ };
15
+ export const defaultGitHostArchives = {
16
+ github: 'https://codeload.github.com/$1/$2/tar.gz/$committish',
17
+ bitbucket: 'https://bitbucket.org/$1/$2/get/$committish.tar.gz',
18
+ gist: 'https://codeload.github.com/gist/$1/tar.gz/$committish',
19
+ gitlab: 'https://gitlab.com/$1/$2/repository/archive.tar.gz?ref=$committish',
20
+ };
21
+ /**
22
+ * These are just for legacy support of urls that are supported by npm
23
+ * and observed in the wild.
24
+ *
25
+ * Not configurable, because no more will be added. If you wish to define
26
+ * custom git hosts, use it with the `'git-hosts'` and `'git-host-archives'`
27
+ * options.
28
+ */
29
+ export const gitHostWebsites = {
30
+ github: 'https://github.com/',
31
+ bitbucket: 'https://bitbucket.org/',
32
+ gist: 'https://gist.github.com/',
33
+ gitlab: 'https://gitlab.com/',
34
+ };
35
+ export const defaultScopeRegistries = {};
36
+ export const getOptions = (options) => ({
37
+ ...options,
38
+ registry: options?.registry ?? defaultRegistry,
39
+ 'scope-registries': options?.['scope-registries'] ?? {},
40
+ 'git-hosts': options?.['git-hosts'] ?
41
+ {
42
+ ...defaultGitHosts,
43
+ ...options['git-hosts'],
44
+ }
45
+ : defaultGitHosts,
46
+ registries: options?.registries ?
47
+ {
48
+ ...defaultRegistries,
49
+ ...options.registries,
50
+ }
51
+ : defaultRegistries,
52
+ 'git-host-archives': options?.['git-host-archives'] ?
53
+ {
54
+ ...defaultGitHostArchives,
55
+ ...options['git-host-archives'],
56
+ }
57
+ : defaultGitHostArchives,
58
+ });
59
+ /**
60
+ * Various nameless scenarios that are handled in the
61
+ * standard spec parsing and should return an unknown name.
62
+ *
63
+ * Returns `true` if the name can not be inferred, `false` otherwise.
64
+ */
65
+ const startsWithSpecIdentifier = (spec, options) => spec.startsWith('/') ||
66
+ spec.startsWith('./') ||
67
+ spec.startsWith('../') ||
68
+ spec.startsWith('file:') ||
69
+ spec.startsWith('http:') ||
70
+ spec.startsWith('https:') ||
71
+ spec.startsWith('workspace:') ||
72
+ spec.startsWith('git@') ||
73
+ spec.startsWith('git://') ||
74
+ spec.startsWith('git+ssh://') ||
75
+ spec.startsWith('git+http://') ||
76
+ spec.startsWith('git+https://') ||
77
+ spec.startsWith('git+file://') ||
78
+ spec.startsWith('git@github.com') ||
79
+ spec.startsWith('registry:') ||
80
+ // anything that starts with a known git host key, or a
81
+ // custom registered registry protocol e.g: `github:`, `custom:`
82
+ [
83
+ ...Object.keys(options['git-hosts']),
84
+ ...Object.keys(options.registries),
85
+ ].some(key => spec.startsWith(`${key}:`));
86
+ /**
87
+ * Returns the location in which the first `@` value is found in a given
88
+ * string, also takes into account that a string starting with @ is
89
+ * using a scoped-name.
90
+ */
91
+ const findFirstAt = (spec, hasScope) => spec.indexOf('@', hasScope ? 1 : 0);
92
+ /**
93
+ * Return `true` if a given spec string is likely to be a git spec.
94
+ */
95
+ const findGitIdentifier = (spec) => spec.indexOf('#') > 2;
96
+ /**
97
+ * Return `true` if a given spec string is likely to be a file spec.
98
+ */
99
+ const findFileIdentifier = (spec) => spec.includes('/');
100
+ /**
101
+ * The base, isomorphic Spec implementation.
102
+ */
103
+ export class Spec {
104
+ static parse(spec, bareOrOptions, options) {
105
+ return typeof spec === 'object' ? spec : (new this(spec, bareOrOptions, options));
106
+ }
107
+ static parseArgs(specOrBareSpec, opts) {
108
+ const options = getOptions(opts ?? {});
109
+ if (startsWithSpecIdentifier(specOrBareSpec, options)) {
110
+ const parsed = this.parse('(unknown)', specOrBareSpec, options);
111
+ // try to look into a potential parsed subspec for a name
112
+ if (parsed.subspec) {
113
+ parsed.name = parsed.subspec.name;
114
+ parsed.spec = `${parsed.name}@${parsed.bareSpec}`;
115
+ }
116
+ return parsed;
117
+ }
118
+ else {
119
+ const hasScope = specOrBareSpec.startsWith('@');
120
+ const at = findFirstAt(specOrBareSpec, hasScope);
121
+ if (at > -1) {
122
+ return this.parse(specOrBareSpec.substring(0, at), specOrBareSpec.substring(at + 1), options);
123
+ }
124
+ else if (findGitIdentifier(specOrBareSpec) ||
125
+ (!hasScope && findFileIdentifier(specOrBareSpec))) {
126
+ return this.parse('(unknown)', specOrBareSpec, options);
127
+ }
128
+ else {
129
+ // doesn't have an @, so it's just a name with no version
130
+ return this.parse(`${specOrBareSpec}@`, options);
131
+ }
132
+ }
133
+ }
134
+ static nodejsDependencies;
135
+ type;
136
+ spec;
137
+ options;
138
+ name;
139
+ scope;
140
+ scopeRegistry;
141
+ bareSpec;
142
+ gitRemote;
143
+ gitSelector;
144
+ gitSelectorParsed;
145
+ gitCommittish;
146
+ namedGitHost;
147
+ namedGitHostPath;
148
+ workspaceSpec;
149
+ workspace;
150
+ namedRegistry;
151
+ registry;
152
+ registrySpec;
153
+ conventionalRegistryTarball;
154
+ semver;
155
+ range;
156
+ distTag;
157
+ remoteURL;
158
+ file;
159
+ subspec;
160
+ #final;
161
+ #toString;
162
+ /**
163
+ * Return the final entry in the chain of subspecs
164
+ * When deciding which thing to actually fetch, spec.final is the thing
165
+ * to look at.
166
+ */
167
+ get final() {
168
+ if (this.#final)
169
+ return this.#final;
170
+ return (this.#final = this.subspec ? this.subspec.final : this);
171
+ }
172
+ /**
173
+ * Normally, the string value of a Spec is just the string passed in to
174
+ * be parsed. However, in the case of a chain of subspecs, like
175
+ * `foo@npm:bar@npm:baz@npm:quux@latest`, this simplifies out the middle
176
+ * parts of the chain, returning just `foo@npm:quux@latest`
177
+ */
178
+ toString() {
179
+ if (this.#toString !== undefined)
180
+ return this.#toString;
181
+ let sub = this;
182
+ // we want the SECOND from the last in the chain
183
+ while (sub.subspec?.subspec)
184
+ sub = sub.subspec;
185
+ if (sub.subspec && sub.subspec.type !== 'registry')
186
+ sub = sub.subspec;
187
+ return (this.#toString = this.name + '@' + sub.bareSpec);
188
+ }
189
+ constructor(spec, bareOrOptions, options = {}) {
190
+ if (bareOrOptions && typeof bareOrOptions === 'object') {
191
+ options = bareOrOptions;
192
+ bareOrOptions = undefined;
193
+ }
194
+ this.options = getOptions(options);
195
+ if (typeof bareOrOptions === 'string') {
196
+ this.name = spec;
197
+ this.#parseScope(spec);
198
+ this.bareSpec = bareOrOptions;
199
+ this.spec = `${this.name}@${bareOrOptions}`;
200
+ }
201
+ else {
202
+ this.spec = spec;
203
+ const hasScope = spec.startsWith('@');
204
+ let at = findFirstAt(spec, hasScope);
205
+ if (at === -1) {
206
+ // assume that an unadorned spec is just a name at the default
207
+ // registry
208
+ at = spec.length;
209
+ spec += '@';
210
+ }
211
+ this.name = spec.substring(0, at);
212
+ if (hasScope)
213
+ this.#parseScope(this.name);
214
+ this.bareSpec = spec.substring(at + 1);
215
+ }
216
+ // legacy affordance: allow project urls like
217
+ // 'https://github.com/user/project#commitish' because npm suports it and
218
+ // this pattern is observed in the wild.
219
+ if (this.bareSpec.startsWith('https://')) {
220
+ for (const [name, origin] of Object.entries(gitHostWebsites)) {
221
+ if (this.bareSpec.startsWith(origin)) {
222
+ const parsed = new URL(this.bareSpec);
223
+ const [user, project] = parsed.pathname
224
+ .replace(/\.git$/, '')
225
+ .replace(/\/+/g, ' ')
226
+ .trim()
227
+ .split(' ');
228
+ if (user && project) {
229
+ this.bareSpec = `${name}:${user}/${project}${parsed.hash}`;
230
+ this.spec = `${this.name}@${this.bareSpec}`;
231
+ break;
232
+ }
233
+ }
234
+ }
235
+ }
236
+ if (this.bareSpec.startsWith('workspace:')) {
237
+ this.type = 'workspace';
238
+ const ws = this.bareSpec.substring('workspace:'.length).trim();
239
+ const w = ws.lastIndexOf('@');
240
+ if (w === -1) {
241
+ this.workspace = this.name;
242
+ }
243
+ else {
244
+ const wsName = ws.substring(0, w);
245
+ if (!wsName ||
246
+ wsName === '*' ||
247
+ wsName === '~' ||
248
+ wsName === '^' ||
249
+ (wsName.startsWith('@') ?
250
+ wsName.split('/').length !== 2 ||
251
+ wsName.substring(1).includes('@')
252
+ : wsName.includes('@'))) {
253
+ throw this.#error('workspace: name must be a path or valid package name', { found: wsName });
254
+ }
255
+ this.workspace = wsName;
256
+ }
257
+ // workspace: is the same as workspace:*
258
+ const wss = w === -1 ? ws : ws.substring(w + 1) || '*';
259
+ const range = wss === '*' ? undefined : parseRange(wss);
260
+ if (wss !== '*' && wss !== '~' && wss !== '^' && !range) {
261
+ throw this.#error('workspace: spec must be one of *, ~, or ^, or a valid semver range', {
262
+ found: wss,
263
+ wanted: `'*'|'~'|'^'|SemverRange`,
264
+ });
265
+ }
266
+ this.workspaceSpec = wss;
267
+ if (range) {
268
+ this.semver = wss;
269
+ this.range = range;
270
+ }
271
+ return;
272
+ }
273
+ if (this.bareSpec.startsWith('git://') ||
274
+ this.bareSpec.startsWith('git+ssh://') ||
275
+ this.bareSpec.startsWith('git+http://') ||
276
+ this.bareSpec.startsWith('git+https://') ||
277
+ this.bareSpec.startsWith('git+file://') ||
278
+ // legacy affordance
279
+ this.bareSpec.startsWith('git@github.com')) {
280
+ if (this.bareSpec.startsWith('git@')) {
281
+ this.bareSpec = `git+ssh://${this.bareSpec}`;
282
+ this.spec = `${this.name}@${this.bareSpec}`;
283
+ }
284
+ this.type = 'git';
285
+ // see if it's one of the known named hosts, and if so, prefer
286
+ // the shorter syntax.
287
+ for (const [name, host] of Object.entries(this.options['git-hosts'])) {
288
+ const s = host.indexOf('$');
289
+ if (s > 0 && this.bareSpec.startsWith(host.substring(0, s))) {
290
+ const p = this.bareSpec
291
+ .substring(s)
292
+ .replace(/\.git(#.*)?$/, '$1');
293
+ this.bareSpec = `${name}:${p}`;
294
+ this.spec = `${this.name}@${this.bareSpec}`;
295
+ this.#parseHostedGit(name, host);
296
+ this.type = 'git';
297
+ return;
298
+ }
299
+ }
300
+ this.#parseGitSelector(this.bareSpec);
301
+ return;
302
+ }
303
+ // spooky
304
+ const ghosts = Object.entries(this.options['git-hosts']);
305
+ for (const [name, template] of ghosts) {
306
+ if (this.#parseHostedGit(name, template)) {
307
+ this.type = 'git';
308
+ return;
309
+ }
310
+ }
311
+ if (this.bareSpec.startsWith('registry:')) {
312
+ const reg = this.bareSpec.substring('registry:'.length);
313
+ const h = reg.indexOf('#');
314
+ if (h === -1) {
315
+ throw this.#error('registry: must include name/version');
316
+ }
317
+ this.type = 'registry';
318
+ let url = reg.substring(0, h);
319
+ if (!url.endsWith('/'))
320
+ url += '/';
321
+ const regSpec = reg.substring(h + 1);
322
+ for (let [name, u] of Object.entries(this.options.registries)) {
323
+ if (!u.endsWith('/')) {
324
+ u += '/';
325
+ this.options.registries[name] = u;
326
+ }
327
+ if (u === url)
328
+ this.namedRegistry = name;
329
+ }
330
+ this.#parseRegistrySpec(regSpec, url);
331
+ this.#guessRegistryTarball();
332
+ return;
333
+ }
334
+ const regs = Object.entries(this.options.registries);
335
+ for (const [host, url] of regs) {
336
+ const h = `${host}:`;
337
+ if (this.bareSpec.startsWith(h)) {
338
+ this.type = 'registry';
339
+ this.namedRegistry = host;
340
+ this.#parseRegistrySpec(this.bareSpec.substring(h.length), url).namedRegistry ??= host;
341
+ this.#guessRegistryTarball();
342
+ return;
343
+ }
344
+ }
345
+ if (this.bareSpec.startsWith('https://') ||
346
+ this.bareSpec.startsWith('http://')) {
347
+ this.remoteURL = this.bareSpec;
348
+ this.type = 'remote';
349
+ return;
350
+ }
351
+ // explicit file: url
352
+ if (this.bareSpec.startsWith('file:')) {
353
+ this.type = 'file';
354
+ const [path, uri] = getNormalizeFile(this.constructor.nodejsDependencies)(this.bareSpec, this);
355
+ this.file = path;
356
+ this.bareSpec = uri.replace(/\/+$/, '');
357
+ this.spec = `${this.name}@${this.bareSpec}`;
358
+ return;
359
+ }
360
+ // legacy! once upon a time, `user/project` was a shorthand for pulling
361
+ // packages from github, instead of the more verbose and explicit
362
+ // `github:user/project`.
363
+ if (!this.bareSpec.startsWith('./') &&
364
+ !this.bareSpec.startsWith('../') &&
365
+ this.options['git-hosts'].github) {
366
+ const hash = this.bareSpec.indexOf('#');
367
+ const up = hash === -1 ? this.bareSpec : this.bareSpec.substring(0, hash);
368
+ if (up.split('/').length === 2) {
369
+ this.bareSpec = `github:${this.bareSpec}`;
370
+ this.spec = `${this.name}@${this.bareSpec}`;
371
+ this.#parseHostedGit('github', this.options['git-hosts'].github);
372
+ this.type = 'git';
373
+ return;
374
+ }
375
+ }
376
+ // if it contains a / and isn't picked up in the github shorthand,
377
+ // then convert to file: specifier
378
+ if (this.bareSpec.includes('/') ||
379
+ this.bareSpec === '.' ||
380
+ this.bareSpec === '..') {
381
+ this.type = 'file';
382
+ const [file, uri] = getNormalizeFile(this.constructor.nodejsDependencies)(`file:${this.bareSpec}`, this);
383
+ this.bareSpec = uri;
384
+ this.spec = `${this.name}@${this.bareSpec}`;
385
+ this.file = file;
386
+ return;
387
+ }
388
+ // at this point, must be either semver range or dist-tag
389
+ this.type = 'registry';
390
+ const range = parseRange(this.bareSpec);
391
+ if (range) {
392
+ this.semver = this.bareSpec.trim();
393
+ this.range = range;
394
+ }
395
+ else {
396
+ this.distTag = this.bareSpec;
397
+ }
398
+ this.registrySpec = this.bareSpec;
399
+ const { 'scope-registries': scopeRegs, registry } = this.options;
400
+ const scopeReg = this.scope && scopeRegs[this.scope];
401
+ this.registry = scopeReg ?? registry;
402
+ this.#guessRegistryTarball();
403
+ }
404
+ #parseScope(name) {
405
+ if (!name.startsWith('@'))
406
+ return;
407
+ const s = name.indexOf('/');
408
+ if (s > 1 && s < name.length - 1) {
409
+ const scope = name.substring(0, s);
410
+ this.registry = this.scopeRegistry =
411
+ this.options['scope-registries'][scope];
412
+ this.scope = scope;
413
+ }
414
+ }
415
+ #parseHostedGit(name, template) {
416
+ if (this.bareSpec.startsWith(`${name}:`)) {
417
+ const h = this.bareSpec.indexOf('#');
418
+ const bare = h === -1 ? this.bareSpec : this.bareSpec.substring(0, h);
419
+ const hash = h === -1 ? '' : this.bareSpec.substring(h);
420
+ const hostPath = bare.substring(name.length + 1);
421
+ if (!hostPath) {
422
+ throw error('invalid named git host specifier', {
423
+ spec: this,
424
+ });
425
+ }
426
+ const split = hostPath.split('/');
427
+ let t = template;
428
+ for (let i = 0; i < split.length; i++) {
429
+ t = t.split(`$${i + 1}`).join(split[i]);
430
+ }
431
+ t += hash;
432
+ this.namedGitHost = name;
433
+ this.namedGitHostPath = hostPath;
434
+ this.#parseGitSelector(t);
435
+ if (this.gitCommittish && !this.gitSelectorParsed?.path) {
436
+ const archiveHost = this.options['git-host-archives'][name];
437
+ if (archiveHost) {
438
+ this.type = 'remote';
439
+ let t = archiveHost;
440
+ t = t.split('$committish').join(this.gitCommittish);
441
+ for (let i = 0; i < split.length; i++) {
442
+ t = t.split(`$${i + 1}`).join(split[i]);
443
+ }
444
+ this.remoteURL = t;
445
+ }
446
+ }
447
+ return true;
448
+ }
449
+ return false;
450
+ }
451
+ /* c8 ignore start */
452
+ [kCustomInspect]() {
453
+ return `@vltpkg/spec.Spec ${String(this)}`;
454
+ }
455
+ /* c8 ignore stop */
456
+ #guessRegistryTarball() {
457
+ // only try to guess if it's a single comparator for a single version
458
+ const { name, registry, range } = this.final;
459
+ if (!registry || !range?.isSingle)
460
+ return;
461
+ const stripScope = /^@[^/]+\//;
462
+ this.conventionalRegistryTarball = String(new URL(`/${name}/-/${name.replace(stripScope, '')}-${range}.tgz`, registry));
463
+ }
464
+ #parseRegistrySpec(s, url) {
465
+ // note: this takes priority over the scoped registry, if set
466
+ this.registry = url;
467
+ this.subspec = this.constructor.parse(s, {
468
+ ...this.options,
469
+ registry: url,
470
+ });
471
+ return this.subspec;
472
+ }
473
+ #error(message, extra = {}) {
474
+ return error(message, { spec: this.spec, ...extra }, this.#error);
475
+ }
476
+ #parseGitSelector(s) {
477
+ const h = s.indexOf('#');
478
+ if (h === -1) {
479
+ this.gitRemote = s;
480
+ return;
481
+ }
482
+ this.gitRemote = s.substring(0, h);
483
+ this.gitSelector = s.substring(h + 1);
484
+ const [selectorParsed, committish, range] = this.constructor.parseGitSelector(this.gitSelector, this);
485
+ this.range = range;
486
+ this.gitCommittish = committish;
487
+ this.gitSelectorParsed = selectorParsed;
488
+ }
489
+ /**
490
+ * Should only ever be called with the bit that comes AFTER the #
491
+ * in the git remote url.
492
+ */
493
+ static parseGitSelector(selector, spec) {
494
+ if (!selector)
495
+ return [{}];
496
+ const split = selector.split('::');
497
+ const first = split[0];
498
+ let committish = undefined;
499
+ let range = undefined;
500
+ const parsed = {};
501
+ /* c8 ignore start - for TS's benefit */
502
+ if (typeof first !== 'string') {
503
+ throw typeError('impossible', {
504
+ found: first,
505
+ wanted: String,
506
+ });
507
+ }
508
+ /* c8 ignore stop */
509
+ if (!first.includes(':')) {
510
+ committish = first;
511
+ split.shift();
512
+ }
513
+ for (const kv of split) {
514
+ const c = kv.indexOf(':');
515
+ /* c8 ignore next */
516
+ if (c === -1)
517
+ continue;
518
+ const k = kv.substring(0, c);
519
+ const v = kv.substring(c + 1);
520
+ if (k === 'semver') {
521
+ if (committish) {
522
+ throw error('Cannot specify a semver range and committish value', { spec });
523
+ }
524
+ range = parseRange(v);
525
+ if (!range) {
526
+ throw error(`Invalid git tag semver range: ${v}`, { spec });
527
+ }
528
+ }
529
+ if (k === 'semver' || k === 'path') {
530
+ if (k === 'path') {
531
+ if (
532
+ /* c8 ignore next */ this.nodejsDependencies?.isAbsolute(v) ||
533
+ /(^|\/|\\)\.\.($|\\|\/)/.test(v)) {
534
+ throw error('Invalid path in git selector', { spec });
535
+ }
536
+ // normalize
537
+ /* c8 ignore start */
538
+ parsed.path = (this.nodejsDependencies ?
539
+ this.nodejsDependencies.join('/', v).substring(1)
540
+ : v).replace(/\\/g, '/');
541
+ /* c8 ignore stop */
542
+ }
543
+ else {
544
+ parsed[k] = v;
545
+ }
546
+ }
547
+ }
548
+ return [parsed, committish, range];
549
+ }
550
+ }
551
+ // normalize our kinda-sorta spec compliant `file:` specifiers
552
+ //
553
+ // For historical reasons, we need to support a lot of non-spec-compliant
554
+ // behaviors, but this massages the result into a *slightly* less offensive
555
+ // shape.
556
+ //
557
+ // The result will be either a fully compliant `file://` with an absolute path,
558
+ // or a `file:` with a relative path starting with `~`, `./`, or `../`.
559
+ export const getNormalizeFile = (opts) => (bareSpec, spec) => {
560
+ const slashes = bareSpec.substring('file:'.length, 'file://'.length);
561
+ const pref = `file:${slashes === '//' ? slashes : ''}`;
562
+ const rest = bareSpec.substring(pref.length);
563
+ // default to '/' because eol == '/' for parsing purposes
564
+ const [a = '', b = '/', c = '/', d = '/'] = rest.split('', 4);
565
+ if (!a) {
566
+ // file:// => file:.
567
+ // file: => file:.
568
+ return ['.', 'file:.'];
569
+ }
570
+ if ((a === '/' && b === '~' && c !== '/') ||
571
+ (a === '~' && b !== '/')) {
572
+ throw error(`invalid file: specifier. '~username' not supported`, { spec });
573
+ }
574
+ if (a === '~') {
575
+ // file://~ => file:~
576
+ // file://~/x => file:~/x
577
+ return /* c8 ignore start */ opts ?
578
+ [
579
+ opts.resolve(opts.homedir(), rest.substring(2)),
580
+ `file:${rest}`,
581
+ ]
582
+ : [rest, `file:${rest}`];
583
+ }
584
+ if (a === '/' && b === '~') {
585
+ // file:///~ => file:~
586
+ // file:/~/x => file:~/x
587
+ /* c8 ignore start - tested in test/index.ts */
588
+ return opts ?
589
+ [
590
+ opts.resolve(opts.homedir(), rest.substring(3)),
591
+ `file:${rest.substring(1)}`,
592
+ ]
593
+ : /* c8 ignore stop */
594
+ [rest.substring(1), `file:${rest.substring(1)}`];
595
+ }
596
+ if (a === '/' &&
597
+ b === '.' &&
598
+ (c === '/' || (c === '.' && d === '/'))) {
599
+ // file:/./x => file:./x
600
+ // file:///./x => file:./x
601
+ // file:/../x => file:../x
602
+ // file://../x => file:../x
603
+ return [rest.substring(1), `file:${rest.substring(1)}`];
604
+ }
605
+ if (a === '.' && (b === '/' || (b === '.' && c === '/'))) {
606
+ // file://. => file:.
607
+ // file://./x => file:./x
608
+ // file://../x => file:../x
609
+ return [rest, `file:${rest}`];
610
+ }
611
+ if (slashes === '//') {
612
+ // must be valid URI, since we ruled out relative and homedir above
613
+ // not relative, but note that file://host/share is
614
+ // windows-specific and does not work on darwin, so disallow it.
615
+ try {
616
+ const parsed = new URL(bareSpec);
617
+ if (parsed.host) {
618
+ if (parsed.host !== 'localhost') {
619
+ throw error(`invalid file:// specifier. host must be empty or 'localhost'`, {
620
+ spec,
621
+ found: parsed.host,
622
+ validOptions: ['', 'localhost'],
623
+ });
624
+ }
625
+ }
626
+ // normalize blank authority
627
+ // file://X:/foo => file:///X:/foo
628
+ // file://localhost/x => file:///x
629
+ // interpret a `file:///D:/x` as `D:/x` though
630
+ return [
631
+ parsed.pathname.replace(/^\/([a-zA-Z]:\/)/, '$1'),
632
+ `file://${parsed.pathname}`,
633
+ ];
634
+ }
635
+ catch (er) {
636
+ // invalid URI for other reasons, eg file://x\u0000y/z
637
+ throw error('invalid file:// specifier', {
638
+ spec,
639
+ cause: er,
640
+ });
641
+ }
642
+ }
643
+ // no //, no authority, be ungovernable
644
+ /* c8 ignore start */
645
+ if (opts?.winPath.isAbsolute(rest)) {
646
+ // file:/absolute => file:///absolute
647
+ // file:/D:/foo => file:///D:/foo
648
+ return [rest, `file://${rest}`];
649
+ }
650
+ /* c8 ignore stop */
651
+ // file:x => file:./x
652
+ return [`./${rest}`, `file:./${rest}`];
653
+ };
654
+ //# sourceMappingURL=browser.js.map