1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const assert_1 = __importDefault(require("assert"));
|
7 | const url_1 = require("url");
|
8 | const utils_1 = require("./utils");
|
9 | function resolve(specifier, parsedImportMap, scriptURL) {
|
10 | const asURL = utils_1.tryURLLikeSpecifierParse(specifier, scriptURL);
|
11 | const normalizedSpecifier = asURL ? asURL.href : specifier;
|
12 | const scriptURLString = scriptURL.href;
|
13 | for (const [scopePrefix, scopeImports] of Object.entries(parsedImportMap.scopes)) {
|
14 | if (scopePrefix === scriptURLString ||
|
15 | (scopePrefix.endsWith("/") && scriptURLString.startsWith(scopePrefix))) {
|
16 | const scopeImportsMatch = resolveImportsMatch(normalizedSpecifier, scopeImports);
|
17 | if (scopeImportsMatch !== null) {
|
18 | return scopeImportsMatch;
|
19 | }
|
20 | }
|
21 | }
|
22 | const topLevelImportsMatch = resolveImportsMatch(normalizedSpecifier, parsedImportMap.imports);
|
23 | if (topLevelImportsMatch !== null) {
|
24 | return topLevelImportsMatch;
|
25 | }
|
26 |
|
27 | if (asURL) {
|
28 | return asURL;
|
29 | }
|
30 | throw new TypeError(`Unmapped bare specifier "${specifier}"`);
|
31 | }
|
32 | exports.resolve = resolve;
|
33 | function resolveImportsMatch(normalizedSpecifier, specifierMap) {
|
34 | for (const [specifierKey, resolutionResult] of Object.entries(specifierMap)) {
|
35 |
|
36 | if (specifierKey === normalizedSpecifier) {
|
37 | if (resolutionResult === null) {
|
38 | throw new TypeError(`Blocked by a null entry for "${specifierKey}"`);
|
39 | }
|
40 | assert_1.default(resolutionResult instanceof url_1.URL);
|
41 | return resolutionResult;
|
42 | }
|
43 |
|
44 | if (specifierKey.endsWith("/") && normalizedSpecifier.startsWith(specifierKey)) {
|
45 | if (resolutionResult === null) {
|
46 | throw new TypeError(`Blocked by a null entry for "${specifierKey}"`);
|
47 | }
|
48 | assert_1.default(resolutionResult instanceof url_1.URL);
|
49 | const afterPrefix = normalizedSpecifier.substring(specifierKey.length);
|
50 |
|
51 | assert_1.default(resolutionResult.href.endsWith("/"));
|
52 | const url = utils_1.tryURLParse(afterPrefix, resolutionResult);
|
53 | if (url === null) {
|
54 | throw new TypeError(`Failed to resolve prefix-match relative URL for "${specifierKey}"`);
|
55 | }
|
56 | assert_1.default(url instanceof url_1.URL);
|
57 | return url;
|
58 | }
|
59 | }
|
60 | return null;
|
61 | }
|