-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod.ts
279 lines (268 loc) · 7.92 KB
/
mod.ts
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import {
ImportMap,
isImportMap,
isImports,
isScopes,
isSpecifierMap,
isURL,
Scopes,
sortObject,
SpecifierMap,
} from "./_util.ts";
export type { ImportMap } from "./_util.ts";
/* https://wicg.github.io/import-maps/#sort-and-normalize-a-specifier-map */
function sortAndNormalizeSpecifierMap(
originalMap: SpecifierMap,
baseURL: URL,
): SpecifierMap {
const normalized: SpecifierMap = {};
for (const [specifierKey, value] of Object.entries(originalMap)) {
const normalizedSpecifierKey = normalizeSpecifierKey(specifierKey, baseURL);
if (normalizedSpecifierKey === null) continue;
if (typeof value !== "string") {
console.warn(`addresses need to be strings.`);
normalized[normalizedSpecifierKey] = null;
continue;
}
const addressURL = parseUrlLikeImportSpecifier(value, baseURL);
if (addressURL === null) {
console.warn(`the address was invalid.`);
normalized[normalizedSpecifierKey] = null;
continue;
}
if (specifierKey.endsWith("/") && !serializeURL(addressURL).endsWith("/")) {
console.warn(
`an invalid address was given for the specifier key specifierKey; since specifierKey ended in a slash, the address needs to as well.`,
);
normalized[normalizedSpecifierKey] = null;
continue;
}
normalized[normalizedSpecifierKey] = serializeURL(addressURL);
}
return sortObject(normalized) as SpecifierMap;
}
/* https://url.spec.whatwg.org/#concept-url-serializer */
function serializeURL(url: URL): string {
return url.href;
}
/* https://wicg.github.io/import-maps/#sort-and-normalize-scopes */
function sortAndNormalizeScopes(
originalMap: Scopes,
baseURL: URL,
): Scopes {
const normalized: Scopes = {};
for (
const [scopePrefix, potentialSpecifierMap] of Object.entries(originalMap)
) {
if (!isSpecifierMap(potentialSpecifierMap)) {
throw new TypeError(
`the value of the scope with prefix scopePrefix needs to be an object.`,
);
}
let scopePrefixURL;
try {
scopePrefixURL = new URL(scopePrefix, baseURL);
} catch {
console.warn(`the scope prefix URL was not parseable.`);
continue;
}
const normalizedScopePrefix = serializeURL(scopePrefixURL);
normalized[normalizedScopePrefix] = sortAndNormalizeSpecifierMap(
potentialSpecifierMap,
baseURL,
);
}
const sorted: Scopes = {};
for (const key of Object.keys(normalized)) {
sorted[key] = sortObject(normalized[key]) as SpecifierMap;
}
return sortObject(sorted) as Scopes;
}
/* https://wicg.github.io/import-maps/#normalize-a-specifier-key */
function normalizeSpecifierKey(
specifierKey: string,
baseURL: URL,
): string | null {
if (!specifierKey.length) {
console.warn("specifier key cannot be an empty string.");
return null;
}
const url = parseUrlLikeImportSpecifier(specifierKey, baseURL);
if (url !== null) {
return serializeURL(url);
}
return specifierKey;
}
/* https://wicg.github.io/import-maps/#parse-a-url-like-import-specifier */
function parseUrlLikeImportSpecifier(
specifier: string,
baseURL: URL,
): URL | null {
if (
baseURL && (specifier.startsWith("/") ||
specifier.startsWith("./") ||
specifier.startsWith("../"))
) {
try {
const url = new URL(specifier, baseURL);
return url;
} catch {
return null;
}
}
try {
const url = new URL(specifier);
return url;
} catch {
return null;
}
}
const specialSchemes = [
"ftp",
"file",
"http",
"https",
"ws",
"wss",
];
/* https://url.spec.whatwg.org/#is-special */
function isSpecial(asURL: URL): boolean {
return specialSchemes.some((scheme) =>
serializeURL(asURL).startsWith(scheme)
);
}
/* https://wicg.github.io/import-maps/#resolve-an-imports-match */
function resolveImportsMatch(
normalizedSpecifier: string,
asURL: URL | null,
specifierMap: SpecifierMap,
): string | null {
for (
const [specifierKey, resolutionResult] of Object.entries(specifierMap)
) {
if (specifierKey === normalizedSpecifier) {
if (resolutionResult === null) {
throw new TypeError(
`resolution of specifierKey was blocked by a null entry.`,
);
}
if (!isURL(resolutionResult)) {
throw new TypeError(`resolutionResult must be an URL.`);
}
return resolutionResult;
} else if (
specifierKey.endsWith("/") &&
normalizedSpecifier.startsWith(specifierKey) &&
(asURL === null || isSpecial(asURL))
) {
if (resolutionResult === null) {
throw new TypeError(
`resolution of specifierKey was blocked by a null entry.`,
);
}
if (!isURL(resolutionResult)) {
throw new TypeError(`resolutionResult must be an URL.`);
}
const afterPrefix = normalizedSpecifier.slice(specifierKey.length);
if (!resolutionResult.endsWith("/")) {
throw new TypeError(`resolutionResult does not end with "/"`);
}
try {
const url = new URL(afterPrefix, resolutionResult);
if (!isURL(url)) {
throw new TypeError(`url must be an URL.`);
}
if (!serializeURL(url).startsWith(resolutionResult)) {
throw new TypeError(
`resolution of normalizedSpecifier was blocked due to it backtracking above its prefix specifierKey.`,
);
}
return serializeURL(url);
} catch {
throw new TypeError(
`resolution of normalizedSpecifier was blocked since the afterPrefix portion could not be URL-parsed relative to the resolutionResult mapped to by the specifierKey prefix.`,
);
}
}
}
return null;
}
/* https://wicg.github.io/import-maps/#parsing */
// do not parse JSON string as done in the specs. That can be done with JSON.parse
export function resolveImportMap(
importMap: ImportMap,
baseURL: URL,
): ImportMap {
let sortedAndNormalizedImports = {};
if (!isImportMap(importMap)) {
throw new TypeError(`the top-level value needs to be a JSON object.`);
}
const { imports, scopes } = importMap;
if (imports !== undefined) {
if (!isImports(imports)) {
throw new TypeError(`"imports" top-level key needs to be an object.`);
}
sortedAndNormalizedImports = sortAndNormalizeSpecifierMap(
imports,
baseURL,
);
}
let sortedAndNormalizedScopes = {};
if (scopes !== undefined) {
if (!isScopes(scopes)) {
throw new TypeError(`"scopes" top-level key needs to be an object.`);
}
sortedAndNormalizedScopes = sortAndNormalizeScopes(
scopes,
baseURL,
);
}
if (
Object.keys(importMap).find((key) => key !== "imports" && key !== "scopes")
) {
console.warn(`an invalid top-level key was present in the import map.`);
}
return {
imports: sortedAndNormalizedImports,
scopes: sortedAndNormalizedScopes,
};
}
/* https://wicg.github.io/import-maps/#new-resolve-algorithm */
export function resolveModuleSpecifier(
specifier: string,
{ imports = {}, scopes = {} }: ImportMap,
baseURL: URL,
): string {
const baseURLString = serializeURL(baseURL);
const asURL = parseUrlLikeImportSpecifier(specifier, baseURL);
const normalizedSpecifier = asURL !== null ? serializeURL(asURL) : specifier;
for (const [scopePrefix, scopeImports] of Object.entries(scopes)) {
if (
scopePrefix === baseURLString ||
(scopePrefix.endsWith("/") && baseURLString.startsWith(scopePrefix))
) {
const scopeImportsMatch = resolveImportsMatch(
normalizedSpecifier,
asURL,
scopeImports,
);
if (scopeImportsMatch !== null) {
return scopeImportsMatch;
}
}
}
const topLevelImportsMatch = resolveImportsMatch(
normalizedSpecifier,
asURL,
imports,
);
if (topLevelImportsMatch !== null) {
return topLevelImportsMatch;
}
if (asURL !== null) {
return serializeURL(asURL);
}
throw new TypeError(
`specifier was a bare specifier, but was not remapped to anything by importMap.`,
);
}