This repository was archived by the owner on Feb 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathutils.js
72 lines (57 loc) · 2.08 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
const { URL } = require('url');
// https://fetch.spec.whatwg.org/#fetch-scheme
const FETCH_SCHEMES = new Set(['http', 'https', 'ftp', 'about', 'blob', 'data', 'file', 'filesystem']);
// Tentative, so better to centralize so we can change in one place as necessary (including tests).
exports.BUILT_IN_MODULE_SCHEME = 'std';
// Useful for comparing to .protocol
exports.BUILT_IN_MODULE_PROTOCOL = `${exports.BUILT_IN_MODULE_SCHEME}:`;
exports.tryURLParse = (string, baseURL) => {
try {
return new URL(string, baseURL);
} catch (e) { // TODO remove useless binding when ESLint and Jest support that
return null;
}
};
exports.tryURLLikeSpecifierParse = (specifier, baseURL) => {
if (specifier === '') {
return { type: 'invalid', message: 'Invalid empty string specifier.' };
}
if (specifier.startsWith('/') || specifier.startsWith('./') || specifier.startsWith('../')) {
if (baseURL.protocol === 'data:') {
return { type: 'invalid', message: `Path-based module specifier ${JSON.stringify(specifier)} ` +
'cannot be used with a base URL that uses the "data:" scheme.' };
}
return { type: 'url', specifier: new URL(specifier, baseURL).href, isBuiltin: false };
}
const url = exports.tryURLParse(specifier);
if (url === null) {
return { type: 'nonURL', specifier };
}
if (exports.hasFetchScheme(url)) {
return { type: 'url', specifier: url.href, isBuiltin: false };
}
if (url.protocol === exports.BUILT_IN_MODULE_PROTOCOL) {
return { type: 'url', specifier: url.href, isBuiltin: true };
}
return { type: 'nonURL', specifier };
};
exports.hasFetchScheme = url => {
return FETCH_SCHEMES.has(url.protocol.slice(0, -1));
};
exports.sortObjectKeysByLongestFirst = obj => {
const sortedEntries = Object.entries(obj).sort((a, b) => longerLengthThenCodeUnitOrder(a[0], b[0]));
return Object.fromEntries(sortedEntries);
};
function longerLengthThenCodeUnitOrder(a, b) {
return compare(b.length, a.length) || compare(a, b);
}
function compare(a, b) {
if (a > b) {
return 1;
}
if (b > a) {
return -1;
}
return 0;
}