|
| 1 | +'use strict'; |
| 2 | +const { isURL, URL } = require('internal/url'); |
| 3 | +const { |
| 4 | + ObjectEntries, |
| 5 | + ObjectKeys, |
| 6 | + SafeMap, |
| 7 | + ArrayIsArray, |
| 8 | + StringPrototypeStartsWith, |
| 9 | + StringPrototypeEndsWith, |
| 10 | + StringPrototypeSlice, |
| 11 | + ArrayPrototypeReverse, |
| 12 | + ArrayPrototypeSort, |
| 13 | +} = primordials; |
| 14 | +const { codes: { ERR_INVALID_IMPORT_MAP } } = require('internal/errors'); |
| 15 | +const { shouldBeTreatedAsRelativeOrAbsolutePath } = require('internal/modules/helpers'); |
| 16 | + |
| 17 | +class ImportMap { |
| 18 | + #baseURL; |
| 19 | + #imports = new SafeMap(); |
| 20 | + #scopes = new SafeMap(); |
| 21 | + #specifiers = new SafeMap() |
| 22 | + |
| 23 | + constructor(raw, baseURL) { |
| 24 | + this.#baseURL = baseURL; |
| 25 | + this.process(raw); |
| 26 | + } |
| 27 | + |
| 28 | + // These are convinenince methods mostly for tests |
| 29 | + get baseURL() { |
| 30 | + return this.#baseURL; |
| 31 | + } |
| 32 | + |
| 33 | + get imports() { |
| 34 | + return this.#imports; |
| 35 | + } |
| 36 | + |
| 37 | + get scopes() { |
| 38 | + return this.#scopes; |
| 39 | + } |
| 40 | + |
| 41 | + #getMappedSpecifier(_mappedSpecifier) { |
| 42 | + let mappedSpecifier = this.#specifiers.get(_mappedSpecifier); |
| 43 | + |
| 44 | + // Specifiers are processed and cached in this.#specifiers |
| 45 | + if (!mappedSpecifier) { |
| 46 | + // Try processing as a url, fall back for bare specifiers |
| 47 | + try { |
| 48 | + if (shouldBeTreatedAsRelativeOrAbsolutePath(_mappedSpecifier)) { |
| 49 | + mappedSpecifier = new URL(_mappedSpecifier, this.#baseURL); |
| 50 | + } else { |
| 51 | + mappedSpecifier = new URL(_mappedSpecifier); |
| 52 | + } |
| 53 | + } catch { |
| 54 | + // Ignore exception |
| 55 | + mappedSpecifier = _mappedSpecifier; |
| 56 | + } |
| 57 | + this.#specifiers.set(_mappedSpecifier, mappedSpecifier); |
| 58 | + } |
| 59 | + return mappedSpecifier; |
| 60 | + } |
| 61 | + |
| 62 | + resolve(specifier, parentURL = this.#baseURL) { |
| 63 | + // When using the customized loader the parent |
| 64 | + // will be a string (for transferring to the worker) |
| 65 | + // so just handle that here |
| 66 | + if (!isURL(parentURL)) { |
| 67 | + parentURL = new URL(parentURL); |
| 68 | + } |
| 69 | + |
| 70 | + // Process scopes |
| 71 | + for (const { 0: prefix, 1: mapping } of this.#scopes) { |
| 72 | + const _mappedSpecifier = mapping.get(specifier); |
| 73 | + if (StringPrototypeStartsWith(parentURL.pathname, prefix.pathname) && _mappedSpecifier) { |
| 74 | + const mappedSpecifier = this.#getMappedSpecifier(_mappedSpecifier); |
| 75 | + if (mappedSpecifier !== _mappedSpecifier) { |
| 76 | + mapping.set(specifier, mappedSpecifier); |
| 77 | + } |
| 78 | + specifier = mappedSpecifier; |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Handle bare specifiers with sub paths |
| 84 | + let spec = specifier; |
| 85 | + let hasSlash = (typeof specifier === 'string' && specifier.indexOf('/')) || -1; |
| 86 | + let subSpec; |
| 87 | + let bareSpec; |
| 88 | + if (isURL(spec)) { |
| 89 | + spec = spec.href; |
| 90 | + } else if (hasSlash !== -1) { |
| 91 | + hasSlash += 1; |
| 92 | + subSpec = StringPrototypeSlice(spec, hasSlash); |
| 93 | + bareSpec = StringPrototypeSlice(spec, 0, hasSlash); |
| 94 | + } |
| 95 | + |
| 96 | + let _mappedSpecifier = this.#imports.get(bareSpec) || this.#imports.get(spec); |
| 97 | + if (_mappedSpecifier) { |
| 98 | + // Re-assemble sub spec |
| 99 | + if (_mappedSpecifier === spec && subSpec) { |
| 100 | + _mappedSpecifier += subSpec; |
| 101 | + } |
| 102 | + const mappedSpecifier = this.#getMappedSpecifier(_mappedSpecifier); |
| 103 | + |
| 104 | + if (mappedSpecifier !== _mappedSpecifier) { |
| 105 | + this.imports.set(specifier, mappedSpecifier); |
| 106 | + } |
| 107 | + specifier = mappedSpecifier; |
| 108 | + } |
| 109 | + |
| 110 | + return specifier; |
| 111 | + } |
| 112 | + |
| 113 | + process(raw) { |
| 114 | + if (!raw) { |
| 115 | + throw new ERR_INVALID_IMPORT_MAP('top level must be a plain object'); |
| 116 | + } |
| 117 | + |
| 118 | + // Validation and normalization |
| 119 | + if (raw.imports === null || typeof raw.imports !== 'object' || ArrayIsArray(raw.imports)) { |
| 120 | + throw new ERR_INVALID_IMPORT_MAP('top level key "imports" is required and must be a plain object'); |
| 121 | + } |
| 122 | + if (raw.scopes === null || typeof raw.scopes !== 'object' || ArrayIsArray(raw.scopes)) { |
| 123 | + throw new ERR_INVALID_IMPORT_MAP('top level key "scopes" is required and must be a plain object'); |
| 124 | + } |
| 125 | + |
| 126 | + // Normalize imports |
| 127 | + const importsEntries = ObjectEntries(raw.imports); |
| 128 | + for (let i = 0; i < importsEntries.length; i++) { |
| 129 | + const { 0: specifier, 1: mapping } = importsEntries[i]; |
| 130 | + if (!specifier || typeof specifier !== 'string') { |
| 131 | + throw new ERR_INVALID_IMPORT_MAP('module specifier keys must be non-empty strings'); |
| 132 | + } |
| 133 | + if (!mapping || typeof mapping !== 'string') { |
| 134 | + throw new ERR_INVALID_IMPORT_MAP('module specifier values must be non-empty strings'); |
| 135 | + } |
| 136 | + if (StringPrototypeEndsWith(specifier, '/') && !StringPrototypeEndsWith(mapping, '/')) { |
| 137 | + throw new ERR_INVALID_IMPORT_MAP('module specifier keys ending with "/" must have values that end with "/"'); |
| 138 | + } |
| 139 | + |
| 140 | + this.imports.set(specifier, mapping); |
| 141 | + } |
| 142 | + |
| 143 | + // Normalize scopes |
| 144 | + // Sort the keys according to spec and add to the map in order |
| 145 | + // which preserves the sorted map requirement |
| 146 | + const sortedScopes = ArrayPrototypeReverse(ArrayPrototypeSort(ObjectKeys(raw.scopes))); |
| 147 | + for (let i = 0; i < sortedScopes.length; i++) { |
| 148 | + let scope = sortedScopes[i]; |
| 149 | + const _scopeMap = raw.scopes[scope]; |
| 150 | + if (!scope || typeof scope !== 'string') { |
| 151 | + throw new ERR_INVALID_IMPORT_MAP('import map scopes keys must be non-empty strings'); |
| 152 | + } |
| 153 | + if (!_scopeMap || typeof _scopeMap !== 'object') { |
| 154 | + throw new ERR_INVALID_IMPORT_MAP(`scope values must be plain objects (${scope} is ${typeof _scopeMap})`); |
| 155 | + } |
| 156 | + |
| 157 | + // Normalize scope |
| 158 | + scope = new URL(scope, this.#baseURL); |
| 159 | + |
| 160 | + const scopeMap = new SafeMap(); |
| 161 | + const scopeEntries = ObjectEntries(_scopeMap); |
| 162 | + for (let i = 0; i < scopeEntries.length; i++) { |
| 163 | + const { 0: specifier, 1: mapping } = scopeEntries[i]; |
| 164 | + if (StringPrototypeEndsWith(specifier, '/') && !StringPrototypeEndsWith(mapping, '/')) { |
| 165 | + throw new ERR_INVALID_IMPORT_MAP('module specifier keys ending with "/" must have values that end with "/"'); |
| 166 | + } |
| 167 | + scopeMap.set(specifier, mapping); |
| 168 | + } |
| 169 | + |
| 170 | + this.scopes.set(scope, scopeMap); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +module.exports = { |
| 176 | + ImportMap, |
| 177 | +}; |
0 commit comments