@mininglamp-oss/octo-runtime 1.1.0 → 1.1.1
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.
- package/README.md +4 -2
- package/apps/cli/README.md +3 -1
- package/apps/cli/dist/build-info.json +2 -2
- package/apps/cli/dist/extras.d.ts +4 -2
- package/apps/cli/dist/extras.js +20 -21
- package/apps/cli/dist/help.js +3 -0
- package/apps/cli/dist/logs.d.ts +10 -0
- package/apps/cli/dist/logs.js +186 -0
- package/apps/cli/dist/main.d.ts +1 -0
- package/apps/cli/dist/main.js +94 -19
- package/apps/cli/dist/{default_adapters.json → manifest.json} +5 -0
- package/apps/cli/dist/module-manifest.d.ts +3 -0
- package/apps/cli/dist/module-manifest.js +11 -0
- package/apps/cli/dist/openclaw.js +30 -6
- package/apps/cli/dist/output.js +3 -0
- package/apps/cli/dist/playboard.js +10 -8
- package/apps/cli/dist/status-output.d.ts +10 -0
- package/apps/cli/dist/status-output.js +65 -0
- package/apps/cli/dist/worker.d.ts +1 -0
- package/apps/cli/dist/worker.js +18 -0
- package/dist/public/host.d.ts +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-acp-client/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-acp/package.json +4 -4
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-antigravity/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-claude-code/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-codebuddy/package.json +4 -4
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-hermes/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-kimi-code/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-mcode/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-openclaw/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-opencode/package.json +4 -4
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-qodercli/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-qoderclicn/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/index.d.ts +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/index.js +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/module-manifest.d.ts +33 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/module-manifest.js +118 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/runtime-log-worker.js +7 -5
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/runtime-log.d.ts +3 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/runtime-log.js +45 -10
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/service-status.d.ts +11 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/service-status.js +70 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/package.json +5 -3
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-zcode-gui/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/build-info.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/extras.d.ts +4 -2
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/extras.js +20 -21
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/help.js +3 -0
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/logs.d.ts +10 -0
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/logs.js +186 -0
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/main.d.ts +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/main.js +94 -19
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/{default_adapters.json → manifest.json} +5 -0
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/module-manifest.d.ts +3 -0
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/module-manifest.js +11 -0
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/openclaw.js +30 -6
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/output.js +3 -0
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/playboard.js +10 -8
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/status-output.d.ts +10 -0
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/status-output.js +65 -0
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/worker.d.ts +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/worker.js +18 -0
- package/node_modules/@mininglamp-oss/octo-runtime-cli/package.json +22 -9
- package/node_modules/@mininglamp-oss/octo-runtime-cross-service/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-executor-core/dist/model-input.d.ts +1 -1
- package/node_modules/@mininglamp-oss/octo-runtime-executor-core/dist/model-input.js +5 -5
- package/node_modules/@mininglamp-oss/octo-runtime-executor-core/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/heartbeat.d.ts +16 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/heartbeat.js +59 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/invocation-manifest.d.ts +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/invocation-manifest.js +4 -1
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/manifest-adapters.d.ts +6 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/manifest-adapters.js +88 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/module-specifier.d.ts +1 -2
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/module-specifier.js +2 -9
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/process.d.ts +7 -2
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/process.js +138 -9
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/runner.js +3 -92
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/serve.js +36 -12
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/server.d.ts +4 -1
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/server.js +14 -6
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/service.js +2 -1
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/supervisor.d.ts +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/supervisor.js +57 -34
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/package.json +8 -16
- package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/config-schema.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/dist/application/im-channel.js +16 -17
- package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/package.json +6 -6
- package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-wecom/config-schema.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-wecom/package.json +5 -5
- package/node_modules/@mininglamp-oss/octo-runtime-extra-webui/config-schema.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-extra-webui/dist/index.js +1 -1
- package/node_modules/@mininglamp-oss/octo-runtime-extra-webui/dist/store.d.ts +4 -1
- package/node_modules/@mininglamp-oss/octo-runtime-extra-webui/dist/store.js +54 -4
- package/node_modules/@mininglamp-oss/octo-runtime-extra-webui/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/extra-config.d.ts +8 -1
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/extra-config.js +27 -12
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/extra-service.js +39 -2
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/index.d.ts +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/package.json +4 -4
- package/node_modules/@mininglamp-oss/octo-runtime-protocol/package.json +1 -1
- package/node_modules/import-meta-resolve/index.d.ts +19 -0
- package/node_modules/import-meta-resolve/index.d.ts.map +1 -0
- package/node_modules/import-meta-resolve/index.js +47 -0
- package/node_modules/import-meta-resolve/lib/errors.d.ts +24 -0
- package/node_modules/import-meta-resolve/lib/errors.d.ts.map +1 -0
- package/node_modules/import-meta-resolve/lib/errors.js +504 -0
- package/node_modules/import-meta-resolve/lib/get-format.d.ts +13 -0
- package/node_modules/import-meta-resolve/lib/get-format.d.ts.map +1 -0
- package/node_modules/import-meta-resolve/lib/get-format.js +159 -0
- package/node_modules/import-meta-resolve/lib/package-json-reader.d.ts +31 -0
- package/node_modules/import-meta-resolve/lib/package-json-reader.d.ts.map +1 -0
- package/node_modules/import-meta-resolve/lib/package-json-reader.js +177 -0
- package/node_modules/import-meta-resolve/lib/resolve.d.ts +29 -0
- package/node_modules/import-meta-resolve/lib/resolve.d.ts.map +1 -0
- package/node_modules/import-meta-resolve/lib/resolve.js +1301 -0
- package/node_modules/import-meta-resolve/lib/utils.d.ts +6 -0
- package/node_modules/import-meta-resolve/lib/utils.d.ts.map +1 -0
- package/node_modules/import-meta-resolve/lib/utils.js +47 -0
- package/node_modules/import-meta-resolve/license +74 -0
- package/node_modules/import-meta-resolve/package.json +78 -0
- package/node_modules/import-meta-resolve/readme.md +252 -0
- package/package.json +28 -26
- package/packages/octo-runtime-extra-channel-gateway/config-schema.json +2 -2
- package/packages/octo-runtime-extra-channel-wecom/config-schema.json +2 -2
- package/packages/octo-runtime-extra-webui/config-schema.json +2 -2
|
@@ -0,0 +1,1301 @@
|
|
|
1
|
+
// Manually “tree shaken” from:
|
|
2
|
+
// <https://github.com/nodejs/node/blob/81a9a97/lib/internal/modules/esm/resolve.js>
|
|
3
|
+
// Last checked on: Apr 29, 2024.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @import {Stats} from 'node:fs'
|
|
7
|
+
* @import {ErrnoException} from './errors.js'
|
|
8
|
+
* @import {PackageConfig} from './package-json-reader.js'
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import assert from 'node:assert'
|
|
12
|
+
import {statSync, realpathSync} from 'node:fs'
|
|
13
|
+
import process from 'node:process'
|
|
14
|
+
import {fileURLToPath, pathToFileURL} from 'node:url'
|
|
15
|
+
import path from 'node:path'
|
|
16
|
+
import {builtinModules} from 'node:module'
|
|
17
|
+
import {defaultGetFormatWithoutErrors} from './get-format.js'
|
|
18
|
+
import {codes} from './errors.js'
|
|
19
|
+
import {getPackageScopeConfig, read} from './package-json-reader.js'
|
|
20
|
+
import {getConditionsSet} from './utils.js'
|
|
21
|
+
|
|
22
|
+
const RegExpPrototypeSymbolReplace = RegExp.prototype[Symbol.replace]
|
|
23
|
+
|
|
24
|
+
const {
|
|
25
|
+
ERR_NETWORK_IMPORT_DISALLOWED,
|
|
26
|
+
ERR_INVALID_MODULE_SPECIFIER,
|
|
27
|
+
ERR_INVALID_PACKAGE_CONFIG,
|
|
28
|
+
ERR_INVALID_PACKAGE_TARGET,
|
|
29
|
+
ERR_MODULE_NOT_FOUND,
|
|
30
|
+
ERR_PACKAGE_IMPORT_NOT_DEFINED,
|
|
31
|
+
ERR_PACKAGE_PATH_NOT_EXPORTED,
|
|
32
|
+
ERR_UNSUPPORTED_DIR_IMPORT,
|
|
33
|
+
ERR_UNSUPPORTED_RESOLVE_REQUEST
|
|
34
|
+
} = codes
|
|
35
|
+
|
|
36
|
+
const own = {}.hasOwnProperty
|
|
37
|
+
|
|
38
|
+
const invalidSegmentRegEx =
|
|
39
|
+
/(^|\\|\/)((\.|%2e)(\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))?(\\|\/|$)/i
|
|
40
|
+
const deprecatedInvalidSegmentRegEx =
|
|
41
|
+
/(^|\\|\/)((\.|%2e)(\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))(\\|\/|$)/i
|
|
42
|
+
const invalidPackageNameRegEx = /^\.|%|\\/
|
|
43
|
+
const patternRegEx = /\*/g
|
|
44
|
+
const encodedSeparatorRegEx = /%2f|%5c/i
|
|
45
|
+
/** @type {Set<string>} */
|
|
46
|
+
const emittedPackageWarnings = new Set()
|
|
47
|
+
|
|
48
|
+
const doubleSlashRegEx = /[/\\]{2}/
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
*
|
|
52
|
+
* @param {string} target
|
|
53
|
+
* @param {string} request
|
|
54
|
+
* @param {string} match
|
|
55
|
+
* @param {URL} packageJsonUrl
|
|
56
|
+
* @param {boolean} internal
|
|
57
|
+
* @param {URL} base
|
|
58
|
+
* @param {boolean} isTarget
|
|
59
|
+
*/
|
|
60
|
+
function emitInvalidSegmentDeprecation(
|
|
61
|
+
target,
|
|
62
|
+
request,
|
|
63
|
+
match,
|
|
64
|
+
packageJsonUrl,
|
|
65
|
+
internal,
|
|
66
|
+
base,
|
|
67
|
+
isTarget
|
|
68
|
+
) {
|
|
69
|
+
// @ts-expect-error: apparently it does exist, TS.
|
|
70
|
+
if (process.noDeprecation) {
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const pjsonPath = fileURLToPath(packageJsonUrl)
|
|
75
|
+
const double = doubleSlashRegEx.exec(isTarget ? target : request) !== null
|
|
76
|
+
process.emitWarning(
|
|
77
|
+
`Use of deprecated ${
|
|
78
|
+
double ? 'double slash' : 'leading or trailing slash matching'
|
|
79
|
+
} resolving "${target}" for module ` +
|
|
80
|
+
`request "${request}" ${
|
|
81
|
+
request === match ? '' : `matched to "${match}" `
|
|
82
|
+
}in the "${
|
|
83
|
+
internal ? 'imports' : 'exports'
|
|
84
|
+
}" field module resolution of the package at ${pjsonPath}${
|
|
85
|
+
base ? ` imported from ${fileURLToPath(base)}` : ''
|
|
86
|
+
}.`,
|
|
87
|
+
'DeprecationWarning',
|
|
88
|
+
'DEP0166'
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @param {URL} url
|
|
94
|
+
* @param {URL} packageJsonUrl
|
|
95
|
+
* @param {URL} base
|
|
96
|
+
* @param {string} [main]
|
|
97
|
+
* @returns {void}
|
|
98
|
+
*/
|
|
99
|
+
function emitLegacyIndexDeprecation(url, packageJsonUrl, base, main) {
|
|
100
|
+
// @ts-expect-error: apparently it does exist, TS.
|
|
101
|
+
if (process.noDeprecation) {
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const format = defaultGetFormatWithoutErrors(url, {parentURL: base.href})
|
|
106
|
+
if (format !== 'module') return
|
|
107
|
+
const urlPath = fileURLToPath(url.href)
|
|
108
|
+
const packagePath = fileURLToPath(new URL('.', packageJsonUrl))
|
|
109
|
+
const basePath = fileURLToPath(base)
|
|
110
|
+
if (!main) {
|
|
111
|
+
process.emitWarning(
|
|
112
|
+
`No "main" or "exports" field defined in the package.json for ${packagePath} resolving the main entry point "${urlPath.slice(
|
|
113
|
+
packagePath.length
|
|
114
|
+
)}", imported from ${basePath}.\nDefault "index" lookups for the main are deprecated for ES modules.`,
|
|
115
|
+
'DeprecationWarning',
|
|
116
|
+
'DEP0151'
|
|
117
|
+
)
|
|
118
|
+
} else if (path.resolve(packagePath, main) !== urlPath) {
|
|
119
|
+
process.emitWarning(
|
|
120
|
+
`Package ${packagePath} has a "main" field set to "${main}", ` +
|
|
121
|
+
`excluding the full filename and extension to the resolved file at "${urlPath.slice(
|
|
122
|
+
packagePath.length
|
|
123
|
+
)}", imported from ${basePath}.\n Automatic extension resolution of the "main" field is ` +
|
|
124
|
+
'deprecated for ES modules.',
|
|
125
|
+
'DeprecationWarning',
|
|
126
|
+
'DEP0151'
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @param {string} path
|
|
133
|
+
* @returns {Stats | undefined}
|
|
134
|
+
*/
|
|
135
|
+
function tryStatSync(path) {
|
|
136
|
+
// Note: from Node 15 onwards we can use `throwIfNoEntry: false` instead.
|
|
137
|
+
try {
|
|
138
|
+
return statSync(path)
|
|
139
|
+
} catch {
|
|
140
|
+
// Note: in Node code this returns `new Stats`,
|
|
141
|
+
// but in Node 22 that’s marked as a deprecated internal API.
|
|
142
|
+
// Which, well, we kinda are, but still to prevent that warning,
|
|
143
|
+
// just yield `undefined`.
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Legacy CommonJS main resolution:
|
|
149
|
+
* 1. let M = pkg_url + (json main field)
|
|
150
|
+
* 2. TRY(M, M.js, M.json, M.node)
|
|
151
|
+
* 3. TRY(M/index.js, M/index.json, M/index.node)
|
|
152
|
+
* 4. TRY(pkg_url/index.js, pkg_url/index.json, pkg_url/index.node)
|
|
153
|
+
* 5. NOT_FOUND
|
|
154
|
+
*
|
|
155
|
+
* @param {URL} url
|
|
156
|
+
* @returns {boolean}
|
|
157
|
+
*/
|
|
158
|
+
function fileExists(url) {
|
|
159
|
+
const stats = statSync(url, {throwIfNoEntry: false})
|
|
160
|
+
const isFile = stats ? stats.isFile() : undefined
|
|
161
|
+
return isFile === null || isFile === undefined ? false : isFile
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* @param {URL} packageJsonUrl
|
|
166
|
+
* @param {PackageConfig} packageConfig
|
|
167
|
+
* @param {URL} base
|
|
168
|
+
* @returns {URL}
|
|
169
|
+
*/
|
|
170
|
+
function legacyMainResolve(packageJsonUrl, packageConfig, base) {
|
|
171
|
+
/** @type {URL | undefined} */
|
|
172
|
+
let guess
|
|
173
|
+
if (packageConfig.main !== undefined) {
|
|
174
|
+
guess = new URL(packageConfig.main, packageJsonUrl)
|
|
175
|
+
// Note: fs check redundances will be handled by Descriptor cache here.
|
|
176
|
+
if (fileExists(guess)) return guess
|
|
177
|
+
|
|
178
|
+
const tries = [
|
|
179
|
+
`./${packageConfig.main}.js`,
|
|
180
|
+
`./${packageConfig.main}.json`,
|
|
181
|
+
`./${packageConfig.main}.node`,
|
|
182
|
+
`./${packageConfig.main}/index.js`,
|
|
183
|
+
`./${packageConfig.main}/index.json`,
|
|
184
|
+
`./${packageConfig.main}/index.node`
|
|
185
|
+
]
|
|
186
|
+
let i = -1
|
|
187
|
+
|
|
188
|
+
while (++i < tries.length) {
|
|
189
|
+
guess = new URL(tries[i], packageJsonUrl)
|
|
190
|
+
if (fileExists(guess)) break
|
|
191
|
+
guess = undefined
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (guess) {
|
|
195
|
+
emitLegacyIndexDeprecation(
|
|
196
|
+
guess,
|
|
197
|
+
packageJsonUrl,
|
|
198
|
+
base,
|
|
199
|
+
packageConfig.main
|
|
200
|
+
)
|
|
201
|
+
return guess
|
|
202
|
+
}
|
|
203
|
+
// Fallthrough.
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const tries = ['./index.js', './index.json', './index.node']
|
|
207
|
+
let i = -1
|
|
208
|
+
|
|
209
|
+
while (++i < tries.length) {
|
|
210
|
+
guess = new URL(tries[i], packageJsonUrl)
|
|
211
|
+
if (fileExists(guess)) break
|
|
212
|
+
guess = undefined
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (guess) {
|
|
216
|
+
emitLegacyIndexDeprecation(guess, packageJsonUrl, base, packageConfig.main)
|
|
217
|
+
return guess
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Not found.
|
|
221
|
+
throw new ERR_MODULE_NOT_FOUND(
|
|
222
|
+
fileURLToPath(new URL('.', packageJsonUrl)),
|
|
223
|
+
fileURLToPath(base)
|
|
224
|
+
)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* @param {URL} resolved
|
|
229
|
+
* @param {URL} base
|
|
230
|
+
* @param {boolean} [preserveSymlinks]
|
|
231
|
+
* @returns {URL}
|
|
232
|
+
*/
|
|
233
|
+
function finalizeResolution(resolved, base, preserveSymlinks) {
|
|
234
|
+
if (encodedSeparatorRegEx.exec(resolved.pathname) !== null) {
|
|
235
|
+
throw new ERR_INVALID_MODULE_SPECIFIER(
|
|
236
|
+
resolved.pathname,
|
|
237
|
+
'must not include encoded "/" or "\\" characters',
|
|
238
|
+
fileURLToPath(base)
|
|
239
|
+
)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** @type {string} */
|
|
243
|
+
let filePath
|
|
244
|
+
|
|
245
|
+
try {
|
|
246
|
+
filePath = fileURLToPath(resolved)
|
|
247
|
+
} catch (error) {
|
|
248
|
+
const cause = /** @type {ErrnoException} */ (error)
|
|
249
|
+
Object.defineProperty(cause, 'input', {value: String(resolved)})
|
|
250
|
+
Object.defineProperty(cause, 'module', {value: String(base)})
|
|
251
|
+
throw cause
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const stats = tryStatSync(
|
|
255
|
+
filePath.endsWith('/') ? filePath.slice(-1) : filePath
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
if (stats && stats.isDirectory()) {
|
|
259
|
+
const error = new ERR_UNSUPPORTED_DIR_IMPORT(filePath, fileURLToPath(base))
|
|
260
|
+
// @ts-expect-error Add this for `import.meta.resolve`.
|
|
261
|
+
error.url = String(resolved)
|
|
262
|
+
throw error
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (!stats || !stats.isFile()) {
|
|
266
|
+
const error = new ERR_MODULE_NOT_FOUND(
|
|
267
|
+
filePath || resolved.pathname,
|
|
268
|
+
base && fileURLToPath(base),
|
|
269
|
+
true
|
|
270
|
+
)
|
|
271
|
+
// @ts-expect-error Add this for `import.meta.resolve`.
|
|
272
|
+
error.url = String(resolved)
|
|
273
|
+
throw error
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (!preserveSymlinks) {
|
|
277
|
+
const real = realpathSync(filePath)
|
|
278
|
+
const {search, hash} = resolved
|
|
279
|
+
resolved = pathToFileURL(real + (filePath.endsWith(path.sep) ? '/' : ''))
|
|
280
|
+
resolved.search = search
|
|
281
|
+
resolved.hash = hash
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return resolved
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* @param {string} specifier
|
|
289
|
+
* @param {URL | undefined} packageJsonUrl
|
|
290
|
+
* @param {URL} base
|
|
291
|
+
* @returns {Error}
|
|
292
|
+
*/
|
|
293
|
+
function importNotDefined(specifier, packageJsonUrl, base) {
|
|
294
|
+
return new ERR_PACKAGE_IMPORT_NOT_DEFINED(
|
|
295
|
+
specifier,
|
|
296
|
+
packageJsonUrl && fileURLToPath(new URL('.', packageJsonUrl)),
|
|
297
|
+
fileURLToPath(base)
|
|
298
|
+
)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* @param {string} subpath
|
|
303
|
+
* @param {URL} packageJsonUrl
|
|
304
|
+
* @param {URL} base
|
|
305
|
+
* @returns {Error}
|
|
306
|
+
*/
|
|
307
|
+
function exportsNotFound(subpath, packageJsonUrl, base) {
|
|
308
|
+
return new ERR_PACKAGE_PATH_NOT_EXPORTED(
|
|
309
|
+
fileURLToPath(new URL('.', packageJsonUrl)),
|
|
310
|
+
subpath,
|
|
311
|
+
base && fileURLToPath(base)
|
|
312
|
+
)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* @param {string} request
|
|
317
|
+
* @param {string} match
|
|
318
|
+
* @param {URL} packageJsonUrl
|
|
319
|
+
* @param {boolean} internal
|
|
320
|
+
* @param {URL} [base]
|
|
321
|
+
* @returns {never}
|
|
322
|
+
*/
|
|
323
|
+
function throwInvalidSubpath(request, match, packageJsonUrl, internal, base) {
|
|
324
|
+
const reason = `request is not a valid match in pattern "${match}" for the "${
|
|
325
|
+
internal ? 'imports' : 'exports'
|
|
326
|
+
}" resolution of ${fileURLToPath(packageJsonUrl)}`
|
|
327
|
+
throw new ERR_INVALID_MODULE_SPECIFIER(
|
|
328
|
+
request,
|
|
329
|
+
reason,
|
|
330
|
+
base && fileURLToPath(base)
|
|
331
|
+
)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* @param {string} subpath
|
|
336
|
+
* @param {unknown} target
|
|
337
|
+
* @param {URL} packageJsonUrl
|
|
338
|
+
* @param {boolean} internal
|
|
339
|
+
* @param {URL} [base]
|
|
340
|
+
* @returns {Error}
|
|
341
|
+
*/
|
|
342
|
+
function invalidPackageTarget(subpath, target, packageJsonUrl, internal, base) {
|
|
343
|
+
target =
|
|
344
|
+
typeof target === 'object' && target !== null
|
|
345
|
+
? JSON.stringify(target, null, '')
|
|
346
|
+
: `${target}`
|
|
347
|
+
|
|
348
|
+
return new ERR_INVALID_PACKAGE_TARGET(
|
|
349
|
+
fileURLToPath(new URL('.', packageJsonUrl)),
|
|
350
|
+
subpath,
|
|
351
|
+
target,
|
|
352
|
+
internal,
|
|
353
|
+
base && fileURLToPath(base)
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* @param {string} target
|
|
359
|
+
* @param {string} subpath
|
|
360
|
+
* @param {string} match
|
|
361
|
+
* @param {URL} packageJsonUrl
|
|
362
|
+
* @param {URL} base
|
|
363
|
+
* @param {boolean} pattern
|
|
364
|
+
* @param {boolean} internal
|
|
365
|
+
* @param {boolean} isPathMap
|
|
366
|
+
* @param {Set<string> | undefined} conditions
|
|
367
|
+
* @returns {URL}
|
|
368
|
+
*/
|
|
369
|
+
function resolvePackageTargetString(
|
|
370
|
+
target,
|
|
371
|
+
subpath,
|
|
372
|
+
match,
|
|
373
|
+
packageJsonUrl,
|
|
374
|
+
base,
|
|
375
|
+
pattern,
|
|
376
|
+
internal,
|
|
377
|
+
isPathMap,
|
|
378
|
+
conditions
|
|
379
|
+
) {
|
|
380
|
+
if (subpath !== '' && !pattern && target[target.length - 1] !== '/')
|
|
381
|
+
throw invalidPackageTarget(match, target, packageJsonUrl, internal, base)
|
|
382
|
+
|
|
383
|
+
if (!target.startsWith('./')) {
|
|
384
|
+
if (internal && !target.startsWith('../') && !target.startsWith('/')) {
|
|
385
|
+
let isURL = false
|
|
386
|
+
|
|
387
|
+
try {
|
|
388
|
+
new URL(target)
|
|
389
|
+
isURL = true
|
|
390
|
+
} catch {
|
|
391
|
+
// Continue regardless of error.
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (!isURL) {
|
|
395
|
+
const exportTarget = pattern
|
|
396
|
+
? RegExpPrototypeSymbolReplace.call(
|
|
397
|
+
patternRegEx,
|
|
398
|
+
target,
|
|
399
|
+
() => subpath
|
|
400
|
+
)
|
|
401
|
+
: target + subpath
|
|
402
|
+
|
|
403
|
+
return packageResolve(exportTarget, packageJsonUrl, conditions)
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
throw invalidPackageTarget(match, target, packageJsonUrl, internal, base)
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
if (invalidSegmentRegEx.exec(target.slice(2)) !== null) {
|
|
411
|
+
if (deprecatedInvalidSegmentRegEx.exec(target.slice(2)) === null) {
|
|
412
|
+
if (!isPathMap) {
|
|
413
|
+
const request = pattern
|
|
414
|
+
? match.replace('*', () => subpath)
|
|
415
|
+
: match + subpath
|
|
416
|
+
const resolvedTarget = pattern
|
|
417
|
+
? RegExpPrototypeSymbolReplace.call(
|
|
418
|
+
patternRegEx,
|
|
419
|
+
target,
|
|
420
|
+
() => subpath
|
|
421
|
+
)
|
|
422
|
+
: target
|
|
423
|
+
emitInvalidSegmentDeprecation(
|
|
424
|
+
resolvedTarget,
|
|
425
|
+
request,
|
|
426
|
+
match,
|
|
427
|
+
packageJsonUrl,
|
|
428
|
+
internal,
|
|
429
|
+
base,
|
|
430
|
+
true
|
|
431
|
+
)
|
|
432
|
+
}
|
|
433
|
+
} else {
|
|
434
|
+
throw invalidPackageTarget(match, target, packageJsonUrl, internal, base)
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const resolved = new URL(target, packageJsonUrl)
|
|
439
|
+
const resolvedPath = resolved.pathname
|
|
440
|
+
const packagePath = new URL('.', packageJsonUrl).pathname
|
|
441
|
+
|
|
442
|
+
if (!resolvedPath.startsWith(packagePath))
|
|
443
|
+
throw invalidPackageTarget(match, target, packageJsonUrl, internal, base)
|
|
444
|
+
|
|
445
|
+
if (subpath === '') return resolved
|
|
446
|
+
|
|
447
|
+
if (invalidSegmentRegEx.exec(subpath) !== null) {
|
|
448
|
+
const request = pattern
|
|
449
|
+
? match.replace('*', () => subpath)
|
|
450
|
+
: match + subpath
|
|
451
|
+
if (deprecatedInvalidSegmentRegEx.exec(subpath) === null) {
|
|
452
|
+
if (!isPathMap) {
|
|
453
|
+
const resolvedTarget = pattern
|
|
454
|
+
? RegExpPrototypeSymbolReplace.call(
|
|
455
|
+
patternRegEx,
|
|
456
|
+
target,
|
|
457
|
+
() => subpath
|
|
458
|
+
)
|
|
459
|
+
: target
|
|
460
|
+
emitInvalidSegmentDeprecation(
|
|
461
|
+
resolvedTarget,
|
|
462
|
+
request,
|
|
463
|
+
match,
|
|
464
|
+
packageJsonUrl,
|
|
465
|
+
internal,
|
|
466
|
+
base,
|
|
467
|
+
false
|
|
468
|
+
)
|
|
469
|
+
}
|
|
470
|
+
} else {
|
|
471
|
+
throwInvalidSubpath(request, match, packageJsonUrl, internal, base)
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
if (pattern) {
|
|
476
|
+
return new URL(
|
|
477
|
+
RegExpPrototypeSymbolReplace.call(
|
|
478
|
+
patternRegEx,
|
|
479
|
+
resolved.href,
|
|
480
|
+
() => subpath
|
|
481
|
+
)
|
|
482
|
+
)
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return new URL(subpath, resolved)
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* @param {string} key
|
|
490
|
+
* @returns {boolean}
|
|
491
|
+
*/
|
|
492
|
+
function isArrayIndex(key) {
|
|
493
|
+
const keyNumber = Number(key)
|
|
494
|
+
if (`${keyNumber}` !== key) return false
|
|
495
|
+
return keyNumber >= 0 && keyNumber < 0xff_ff_ff_ff
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* @param {URL} packageJsonUrl
|
|
500
|
+
* @param {unknown} target
|
|
501
|
+
* @param {string} subpath
|
|
502
|
+
* @param {string} packageSubpath
|
|
503
|
+
* @param {URL} base
|
|
504
|
+
* @param {boolean} pattern
|
|
505
|
+
* @param {boolean} internal
|
|
506
|
+
* @param {boolean} isPathMap
|
|
507
|
+
* @param {Set<string> | undefined} conditions
|
|
508
|
+
* @returns {URL | null}
|
|
509
|
+
*/
|
|
510
|
+
function resolvePackageTarget(
|
|
511
|
+
packageJsonUrl,
|
|
512
|
+
target,
|
|
513
|
+
subpath,
|
|
514
|
+
packageSubpath,
|
|
515
|
+
base,
|
|
516
|
+
pattern,
|
|
517
|
+
internal,
|
|
518
|
+
isPathMap,
|
|
519
|
+
conditions
|
|
520
|
+
) {
|
|
521
|
+
if (typeof target === 'string') {
|
|
522
|
+
return resolvePackageTargetString(
|
|
523
|
+
target,
|
|
524
|
+
subpath,
|
|
525
|
+
packageSubpath,
|
|
526
|
+
packageJsonUrl,
|
|
527
|
+
base,
|
|
528
|
+
pattern,
|
|
529
|
+
internal,
|
|
530
|
+
isPathMap,
|
|
531
|
+
conditions
|
|
532
|
+
)
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
if (Array.isArray(target)) {
|
|
536
|
+
/** @type {Array<unknown>} */
|
|
537
|
+
const targetList = target
|
|
538
|
+
if (targetList.length === 0) return null
|
|
539
|
+
|
|
540
|
+
/** @type {ErrnoException | null | undefined} */
|
|
541
|
+
let lastException
|
|
542
|
+
let i = -1
|
|
543
|
+
|
|
544
|
+
while (++i < targetList.length) {
|
|
545
|
+
const targetItem = targetList[i]
|
|
546
|
+
/** @type {URL | null} */
|
|
547
|
+
let resolveResult
|
|
548
|
+
try {
|
|
549
|
+
resolveResult = resolvePackageTarget(
|
|
550
|
+
packageJsonUrl,
|
|
551
|
+
targetItem,
|
|
552
|
+
subpath,
|
|
553
|
+
packageSubpath,
|
|
554
|
+
base,
|
|
555
|
+
pattern,
|
|
556
|
+
internal,
|
|
557
|
+
isPathMap,
|
|
558
|
+
conditions
|
|
559
|
+
)
|
|
560
|
+
} catch (error) {
|
|
561
|
+
const exception = /** @type {ErrnoException} */ (error)
|
|
562
|
+
lastException = exception
|
|
563
|
+
if (exception.code === 'ERR_INVALID_PACKAGE_TARGET') continue
|
|
564
|
+
throw error
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if (resolveResult === undefined) continue
|
|
568
|
+
|
|
569
|
+
if (resolveResult === null) {
|
|
570
|
+
lastException = null
|
|
571
|
+
continue
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
return resolveResult
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
if (lastException === undefined || lastException === null) {
|
|
578
|
+
return null
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
throw lastException
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
if (typeof target === 'object' && target !== null) {
|
|
585
|
+
const keys = Object.getOwnPropertyNames(target)
|
|
586
|
+
let i = -1
|
|
587
|
+
|
|
588
|
+
while (++i < keys.length) {
|
|
589
|
+
const key = keys[i]
|
|
590
|
+
if (isArrayIndex(key)) {
|
|
591
|
+
throw new ERR_INVALID_PACKAGE_CONFIG(
|
|
592
|
+
fileURLToPath(packageJsonUrl),
|
|
593
|
+
base,
|
|
594
|
+
'"exports" cannot contain numeric property keys.'
|
|
595
|
+
)
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
i = -1
|
|
600
|
+
|
|
601
|
+
while (++i < keys.length) {
|
|
602
|
+
const key = keys[i]
|
|
603
|
+
if (key === 'default' || (conditions && conditions.has(key))) {
|
|
604
|
+
// @ts-expect-error: indexable.
|
|
605
|
+
const conditionalTarget = /** @type {unknown} */ (target[key])
|
|
606
|
+
const resolveResult = resolvePackageTarget(
|
|
607
|
+
packageJsonUrl,
|
|
608
|
+
conditionalTarget,
|
|
609
|
+
subpath,
|
|
610
|
+
packageSubpath,
|
|
611
|
+
base,
|
|
612
|
+
pattern,
|
|
613
|
+
internal,
|
|
614
|
+
isPathMap,
|
|
615
|
+
conditions
|
|
616
|
+
)
|
|
617
|
+
if (resolveResult === undefined) continue
|
|
618
|
+
return resolveResult
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
return null
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
if (target === null) {
|
|
626
|
+
return null
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
throw invalidPackageTarget(
|
|
630
|
+
packageSubpath,
|
|
631
|
+
target,
|
|
632
|
+
packageJsonUrl,
|
|
633
|
+
internal,
|
|
634
|
+
base
|
|
635
|
+
)
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* @param {unknown} exports
|
|
640
|
+
* @param {URL} packageJsonUrl
|
|
641
|
+
* @param {URL} base
|
|
642
|
+
* @returns {boolean}
|
|
643
|
+
*/
|
|
644
|
+
function isConditionalExportsMainSugar(exports, packageJsonUrl, base) {
|
|
645
|
+
if (typeof exports === 'string' || Array.isArray(exports)) return true
|
|
646
|
+
if (typeof exports !== 'object' || exports === null) return false
|
|
647
|
+
|
|
648
|
+
const keys = Object.getOwnPropertyNames(exports)
|
|
649
|
+
let isConditionalSugar = false
|
|
650
|
+
let i = 0
|
|
651
|
+
let keyIndex = -1
|
|
652
|
+
while (++keyIndex < keys.length) {
|
|
653
|
+
const key = keys[keyIndex]
|
|
654
|
+
const currentIsConditionalSugar = key === '' || key[0] !== '.'
|
|
655
|
+
if (i++ === 0) {
|
|
656
|
+
isConditionalSugar = currentIsConditionalSugar
|
|
657
|
+
} else if (isConditionalSugar !== currentIsConditionalSugar) {
|
|
658
|
+
throw new ERR_INVALID_PACKAGE_CONFIG(
|
|
659
|
+
fileURLToPath(packageJsonUrl),
|
|
660
|
+
base,
|
|
661
|
+
'"exports" cannot contain some keys starting with \'.\' and some not.' +
|
|
662
|
+
' The exports object must either be an object of package subpath keys' +
|
|
663
|
+
' or an object of main entry condition name keys only.'
|
|
664
|
+
)
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
return isConditionalSugar
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* @param {string} match
|
|
673
|
+
* @param {URL} pjsonUrl
|
|
674
|
+
* @param {URL} base
|
|
675
|
+
*/
|
|
676
|
+
function emitTrailingSlashPatternDeprecation(match, pjsonUrl, base) {
|
|
677
|
+
// @ts-expect-error: apparently it does exist, TS.
|
|
678
|
+
if (process.noDeprecation) {
|
|
679
|
+
return
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const pjsonPath = fileURLToPath(pjsonUrl)
|
|
683
|
+
if (emittedPackageWarnings.has(pjsonPath + '|' + match)) return
|
|
684
|
+
emittedPackageWarnings.add(pjsonPath + '|' + match)
|
|
685
|
+
process.emitWarning(
|
|
686
|
+
`Use of deprecated trailing slash pattern mapping "${match}" in the ` +
|
|
687
|
+
`"exports" field module resolution of the package at ${pjsonPath}${
|
|
688
|
+
base ? ` imported from ${fileURLToPath(base)}` : ''
|
|
689
|
+
}. Mapping specifiers ending in "/" is no longer supported.`,
|
|
690
|
+
'DeprecationWarning',
|
|
691
|
+
'DEP0155'
|
|
692
|
+
)
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* @param {URL} packageJsonUrl
|
|
697
|
+
* @param {string} packageSubpath
|
|
698
|
+
* @param {Record<string, unknown>} packageConfig
|
|
699
|
+
* @param {URL} base
|
|
700
|
+
* @param {Set<string> | undefined} conditions
|
|
701
|
+
* @returns {URL}
|
|
702
|
+
*/
|
|
703
|
+
function packageExportsResolve(
|
|
704
|
+
packageJsonUrl,
|
|
705
|
+
packageSubpath,
|
|
706
|
+
packageConfig,
|
|
707
|
+
base,
|
|
708
|
+
conditions
|
|
709
|
+
) {
|
|
710
|
+
let exports = packageConfig.exports
|
|
711
|
+
|
|
712
|
+
if (isConditionalExportsMainSugar(exports, packageJsonUrl, base)) {
|
|
713
|
+
exports = {'.': exports}
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
if (
|
|
717
|
+
own.call(exports, packageSubpath) &&
|
|
718
|
+
!packageSubpath.includes('*') &&
|
|
719
|
+
!packageSubpath.endsWith('/')
|
|
720
|
+
) {
|
|
721
|
+
// @ts-expect-error: indexable.
|
|
722
|
+
const target = exports[packageSubpath]
|
|
723
|
+
const resolveResult = resolvePackageTarget(
|
|
724
|
+
packageJsonUrl,
|
|
725
|
+
target,
|
|
726
|
+
'',
|
|
727
|
+
packageSubpath,
|
|
728
|
+
base,
|
|
729
|
+
false,
|
|
730
|
+
false,
|
|
731
|
+
false,
|
|
732
|
+
conditions
|
|
733
|
+
)
|
|
734
|
+
if (resolveResult === null || resolveResult === undefined) {
|
|
735
|
+
throw exportsNotFound(packageSubpath, packageJsonUrl, base)
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
return resolveResult
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
let bestMatch = ''
|
|
742
|
+
let bestMatchSubpath = ''
|
|
743
|
+
const keys = Object.getOwnPropertyNames(exports)
|
|
744
|
+
let i = -1
|
|
745
|
+
|
|
746
|
+
while (++i < keys.length) {
|
|
747
|
+
const key = keys[i]
|
|
748
|
+
const patternIndex = key.indexOf('*')
|
|
749
|
+
|
|
750
|
+
if (
|
|
751
|
+
patternIndex !== -1 &&
|
|
752
|
+
packageSubpath.startsWith(key.slice(0, patternIndex))
|
|
753
|
+
) {
|
|
754
|
+
// When this reaches EOL, this can throw at the top of the whole function:
|
|
755
|
+
//
|
|
756
|
+
// if (StringPrototypeEndsWith(packageSubpath, '/'))
|
|
757
|
+
// throwInvalidSubpath(packageSubpath)
|
|
758
|
+
//
|
|
759
|
+
// To match "imports" and the spec.
|
|
760
|
+
if (packageSubpath.endsWith('/')) {
|
|
761
|
+
emitTrailingSlashPatternDeprecation(
|
|
762
|
+
packageSubpath,
|
|
763
|
+
packageJsonUrl,
|
|
764
|
+
base
|
|
765
|
+
)
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
const patternTrailer = key.slice(patternIndex + 1)
|
|
769
|
+
|
|
770
|
+
if (
|
|
771
|
+
packageSubpath.length >= key.length &&
|
|
772
|
+
packageSubpath.endsWith(patternTrailer) &&
|
|
773
|
+
patternKeyCompare(bestMatch, key) === 1 &&
|
|
774
|
+
key.lastIndexOf('*') === patternIndex
|
|
775
|
+
) {
|
|
776
|
+
bestMatch = key
|
|
777
|
+
bestMatchSubpath = packageSubpath.slice(
|
|
778
|
+
patternIndex,
|
|
779
|
+
packageSubpath.length - patternTrailer.length
|
|
780
|
+
)
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
if (bestMatch) {
|
|
786
|
+
// @ts-expect-error: indexable.
|
|
787
|
+
const target = /** @type {unknown} */ (exports[bestMatch])
|
|
788
|
+
const resolveResult = resolvePackageTarget(
|
|
789
|
+
packageJsonUrl,
|
|
790
|
+
target,
|
|
791
|
+
bestMatchSubpath,
|
|
792
|
+
bestMatch,
|
|
793
|
+
base,
|
|
794
|
+
true,
|
|
795
|
+
false,
|
|
796
|
+
packageSubpath.endsWith('/'),
|
|
797
|
+
conditions
|
|
798
|
+
)
|
|
799
|
+
|
|
800
|
+
if (resolveResult === null || resolveResult === undefined) {
|
|
801
|
+
throw exportsNotFound(packageSubpath, packageJsonUrl, base)
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
return resolveResult
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
throw exportsNotFound(packageSubpath, packageJsonUrl, base)
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/**
|
|
811
|
+
* @param {string} a
|
|
812
|
+
* @param {string} b
|
|
813
|
+
*/
|
|
814
|
+
function patternKeyCompare(a, b) {
|
|
815
|
+
const aPatternIndex = a.indexOf('*')
|
|
816
|
+
const bPatternIndex = b.indexOf('*')
|
|
817
|
+
const baseLengthA = aPatternIndex === -1 ? a.length : aPatternIndex + 1
|
|
818
|
+
const baseLengthB = bPatternIndex === -1 ? b.length : bPatternIndex + 1
|
|
819
|
+
if (baseLengthA > baseLengthB) return -1
|
|
820
|
+
if (baseLengthB > baseLengthA) return 1
|
|
821
|
+
if (aPatternIndex === -1) return 1
|
|
822
|
+
if (bPatternIndex === -1) return -1
|
|
823
|
+
if (a.length > b.length) return -1
|
|
824
|
+
if (b.length > a.length) return 1
|
|
825
|
+
return 0
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
/**
|
|
829
|
+
* @param {string} name
|
|
830
|
+
* @param {URL} base
|
|
831
|
+
* @param {Set<string>} [conditions]
|
|
832
|
+
* @returns {URL}
|
|
833
|
+
*/
|
|
834
|
+
function packageImportsResolve(name, base, conditions) {
|
|
835
|
+
if (name === '#' || name.startsWith('#/') || name.endsWith('/')) {
|
|
836
|
+
const reason = 'is not a valid internal imports specifier name'
|
|
837
|
+
throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base))
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
/** @type {URL | undefined} */
|
|
841
|
+
let packageJsonUrl
|
|
842
|
+
|
|
843
|
+
const packageConfig = getPackageScopeConfig(base)
|
|
844
|
+
|
|
845
|
+
if (packageConfig.exists) {
|
|
846
|
+
packageJsonUrl = pathToFileURL(packageConfig.pjsonPath)
|
|
847
|
+
const imports = packageConfig.imports
|
|
848
|
+
if (imports) {
|
|
849
|
+
if (own.call(imports, name) && !name.includes('*')) {
|
|
850
|
+
const resolveResult = resolvePackageTarget(
|
|
851
|
+
packageJsonUrl,
|
|
852
|
+
imports[name],
|
|
853
|
+
'',
|
|
854
|
+
name,
|
|
855
|
+
base,
|
|
856
|
+
false,
|
|
857
|
+
true,
|
|
858
|
+
false,
|
|
859
|
+
conditions
|
|
860
|
+
)
|
|
861
|
+
if (resolveResult !== null && resolveResult !== undefined) {
|
|
862
|
+
return resolveResult
|
|
863
|
+
}
|
|
864
|
+
} else {
|
|
865
|
+
let bestMatch = ''
|
|
866
|
+
let bestMatchSubpath = ''
|
|
867
|
+
const keys = Object.getOwnPropertyNames(imports)
|
|
868
|
+
let i = -1
|
|
869
|
+
|
|
870
|
+
while (++i < keys.length) {
|
|
871
|
+
const key = keys[i]
|
|
872
|
+
const patternIndex = key.indexOf('*')
|
|
873
|
+
|
|
874
|
+
if (patternIndex !== -1 && name.startsWith(key.slice(0, -1))) {
|
|
875
|
+
const patternTrailer = key.slice(patternIndex + 1)
|
|
876
|
+
if (
|
|
877
|
+
name.length >= key.length &&
|
|
878
|
+
name.endsWith(patternTrailer) &&
|
|
879
|
+
patternKeyCompare(bestMatch, key) === 1 &&
|
|
880
|
+
key.lastIndexOf('*') === patternIndex
|
|
881
|
+
) {
|
|
882
|
+
bestMatch = key
|
|
883
|
+
bestMatchSubpath = name.slice(
|
|
884
|
+
patternIndex,
|
|
885
|
+
name.length - patternTrailer.length
|
|
886
|
+
)
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
if (bestMatch) {
|
|
892
|
+
const target = imports[bestMatch]
|
|
893
|
+
const resolveResult = resolvePackageTarget(
|
|
894
|
+
packageJsonUrl,
|
|
895
|
+
target,
|
|
896
|
+
bestMatchSubpath,
|
|
897
|
+
bestMatch,
|
|
898
|
+
base,
|
|
899
|
+
true,
|
|
900
|
+
true,
|
|
901
|
+
false,
|
|
902
|
+
conditions
|
|
903
|
+
)
|
|
904
|
+
|
|
905
|
+
if (resolveResult !== null && resolveResult !== undefined) {
|
|
906
|
+
return resolveResult
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
throw importNotDefined(name, packageJsonUrl, base)
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
/**
|
|
917
|
+
* @param {string} specifier
|
|
918
|
+
* @param {URL} base
|
|
919
|
+
*/
|
|
920
|
+
function parsePackageName(specifier, base) {
|
|
921
|
+
let separatorIndex = specifier.indexOf('/')
|
|
922
|
+
let validPackageName = true
|
|
923
|
+
let isScoped = false
|
|
924
|
+
if (specifier[0] === '@') {
|
|
925
|
+
isScoped = true
|
|
926
|
+
if (separatorIndex === -1 || specifier.length === 0) {
|
|
927
|
+
validPackageName = false
|
|
928
|
+
} else {
|
|
929
|
+
separatorIndex = specifier.indexOf('/', separatorIndex + 1)
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
const packageName =
|
|
934
|
+
separatorIndex === -1 ? specifier : specifier.slice(0, separatorIndex)
|
|
935
|
+
|
|
936
|
+
// Package name cannot have leading . and cannot have percent-encoding or
|
|
937
|
+
// \\ separators.
|
|
938
|
+
if (invalidPackageNameRegEx.exec(packageName) !== null) {
|
|
939
|
+
validPackageName = false
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
if (!validPackageName) {
|
|
943
|
+
throw new ERR_INVALID_MODULE_SPECIFIER(
|
|
944
|
+
specifier,
|
|
945
|
+
'is not a valid package name',
|
|
946
|
+
fileURLToPath(base)
|
|
947
|
+
)
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
const packageSubpath =
|
|
951
|
+
'.' + (separatorIndex === -1 ? '' : specifier.slice(separatorIndex))
|
|
952
|
+
|
|
953
|
+
return {packageName, packageSubpath, isScoped}
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
/**
|
|
957
|
+
* @param {string} specifier
|
|
958
|
+
* @param {URL} base
|
|
959
|
+
* @param {Set<string> | undefined} conditions
|
|
960
|
+
* @returns {URL}
|
|
961
|
+
*/
|
|
962
|
+
function packageResolve(specifier, base, conditions) {
|
|
963
|
+
if (builtinModules.includes(specifier)) {
|
|
964
|
+
return new URL('node:' + specifier)
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
const {packageName, packageSubpath, isScoped} = parsePackageName(
|
|
968
|
+
specifier,
|
|
969
|
+
base
|
|
970
|
+
)
|
|
971
|
+
|
|
972
|
+
// ResolveSelf
|
|
973
|
+
const packageConfig = getPackageScopeConfig(base)
|
|
974
|
+
|
|
975
|
+
// Can’t test.
|
|
976
|
+
/* c8 ignore next 16 */
|
|
977
|
+
if (packageConfig.exists) {
|
|
978
|
+
const packageJsonUrl = pathToFileURL(packageConfig.pjsonPath)
|
|
979
|
+
if (
|
|
980
|
+
packageConfig.name === packageName &&
|
|
981
|
+
packageConfig.exports !== undefined &&
|
|
982
|
+
packageConfig.exports !== null
|
|
983
|
+
) {
|
|
984
|
+
return packageExportsResolve(
|
|
985
|
+
packageJsonUrl,
|
|
986
|
+
packageSubpath,
|
|
987
|
+
packageConfig,
|
|
988
|
+
base,
|
|
989
|
+
conditions
|
|
990
|
+
)
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
let packageJsonUrl = new URL(
|
|
995
|
+
'./node_modules/' + packageName + '/package.json',
|
|
996
|
+
base
|
|
997
|
+
)
|
|
998
|
+
let packageJsonPath = fileURLToPath(packageJsonUrl)
|
|
999
|
+
/** @type {string} */
|
|
1000
|
+
let lastPath
|
|
1001
|
+
do {
|
|
1002
|
+
const stat = tryStatSync(packageJsonPath.slice(0, -13))
|
|
1003
|
+
if (!stat || !stat.isDirectory()) {
|
|
1004
|
+
lastPath = packageJsonPath
|
|
1005
|
+
packageJsonUrl = new URL(
|
|
1006
|
+
(isScoped ? '../../../../node_modules/' : '../../../node_modules/') +
|
|
1007
|
+
packageName +
|
|
1008
|
+
'/package.json',
|
|
1009
|
+
packageJsonUrl
|
|
1010
|
+
)
|
|
1011
|
+
packageJsonPath = fileURLToPath(packageJsonUrl)
|
|
1012
|
+
continue
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
// Package match.
|
|
1016
|
+
const packageConfig = read(packageJsonPath, {base, specifier})
|
|
1017
|
+
if (packageConfig.exports !== undefined && packageConfig.exports !== null) {
|
|
1018
|
+
return packageExportsResolve(
|
|
1019
|
+
packageJsonUrl,
|
|
1020
|
+
packageSubpath,
|
|
1021
|
+
packageConfig,
|
|
1022
|
+
base,
|
|
1023
|
+
conditions
|
|
1024
|
+
)
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
if (packageSubpath === '.') {
|
|
1028
|
+
return legacyMainResolve(packageJsonUrl, packageConfig, base)
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
return new URL(packageSubpath, packageJsonUrl)
|
|
1032
|
+
// Cross-platform root check.
|
|
1033
|
+
} while (packageJsonPath.length !== lastPath.length)
|
|
1034
|
+
|
|
1035
|
+
throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), false)
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
/**
|
|
1039
|
+
* @param {string} specifier
|
|
1040
|
+
* @returns {boolean}
|
|
1041
|
+
*/
|
|
1042
|
+
function isRelativeSpecifier(specifier) {
|
|
1043
|
+
if (specifier[0] === '.') {
|
|
1044
|
+
if (specifier.length === 1 || specifier[1] === '/') return true
|
|
1045
|
+
if (
|
|
1046
|
+
specifier[1] === '.' &&
|
|
1047
|
+
(specifier.length === 2 || specifier[2] === '/')
|
|
1048
|
+
) {
|
|
1049
|
+
return true
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
return false
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
/**
|
|
1057
|
+
* @param {string} specifier
|
|
1058
|
+
* @returns {boolean}
|
|
1059
|
+
*/
|
|
1060
|
+
function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) {
|
|
1061
|
+
if (specifier === '') return false
|
|
1062
|
+
if (specifier[0] === '/') return true
|
|
1063
|
+
return isRelativeSpecifier(specifier)
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
/**
|
|
1067
|
+
* The “Resolver Algorithm Specification” as detailed in the Node docs (which is
|
|
1068
|
+
* sync and slightly lower-level than `resolve`).
|
|
1069
|
+
*
|
|
1070
|
+
* @param {string} specifier
|
|
1071
|
+
* `/example.js`, `./example.js`, `../example.js`, `some-package`, `fs`, etc.
|
|
1072
|
+
* @param {URL} base
|
|
1073
|
+
* Full URL (to a file) that `specifier` is resolved relative from.
|
|
1074
|
+
* @param {Set<string>} [conditions]
|
|
1075
|
+
* Conditions.
|
|
1076
|
+
* @param {boolean} [preserveSymlinks]
|
|
1077
|
+
* Keep symlinks instead of resolving them.
|
|
1078
|
+
* @returns {URL}
|
|
1079
|
+
* A URL object to the found thing.
|
|
1080
|
+
*/
|
|
1081
|
+
export function moduleResolve(specifier, base, conditions, preserveSymlinks) {
|
|
1082
|
+
// Note: The Node code supports `base` as a string (in this internal API) too,
|
|
1083
|
+
// we don’t.
|
|
1084
|
+
if (conditions === undefined) {
|
|
1085
|
+
conditions = getConditionsSet()
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
const protocol = base.protocol
|
|
1089
|
+
const isData = protocol === 'data:'
|
|
1090
|
+
const isRemote = isData || protocol === 'http:' || protocol === 'https:'
|
|
1091
|
+
// Order swapped from spec for minor perf gain.
|
|
1092
|
+
// Ok since relative URLs cannot parse as URLs.
|
|
1093
|
+
/** @type {URL | undefined} */
|
|
1094
|
+
let resolved
|
|
1095
|
+
|
|
1096
|
+
if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
|
|
1097
|
+
try {
|
|
1098
|
+
resolved = new URL(specifier, base)
|
|
1099
|
+
} catch (error_) {
|
|
1100
|
+
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base)
|
|
1101
|
+
error.cause = error_
|
|
1102
|
+
throw error
|
|
1103
|
+
}
|
|
1104
|
+
} else if (protocol === 'file:' && specifier[0] === '#') {
|
|
1105
|
+
resolved = packageImportsResolve(specifier, base, conditions)
|
|
1106
|
+
} else {
|
|
1107
|
+
try {
|
|
1108
|
+
resolved = new URL(specifier)
|
|
1109
|
+
} catch (error_) {
|
|
1110
|
+
// Note: actual code uses `canBeRequiredWithoutScheme`.
|
|
1111
|
+
if (isRemote && !builtinModules.includes(specifier)) {
|
|
1112
|
+
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base)
|
|
1113
|
+
error.cause = error_
|
|
1114
|
+
throw error
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
resolved = packageResolve(specifier, base, conditions)
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
assert.ok(resolved !== undefined, 'expected to be defined')
|
|
1122
|
+
|
|
1123
|
+
if (resolved.protocol !== 'file:') {
|
|
1124
|
+
return resolved
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
return finalizeResolution(resolved, base, preserveSymlinks)
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
/**
|
|
1131
|
+
* @param {string} specifier
|
|
1132
|
+
* @param {URL | undefined} parsed
|
|
1133
|
+
* @param {URL | undefined} parsedParentURL
|
|
1134
|
+
*/
|
|
1135
|
+
function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
|
|
1136
|
+
if (parsedParentURL) {
|
|
1137
|
+
// Avoid accessing the `protocol` property due to the lazy getters.
|
|
1138
|
+
const parentProtocol = parsedParentURL.protocol
|
|
1139
|
+
|
|
1140
|
+
if (parentProtocol === 'http:' || parentProtocol === 'https:') {
|
|
1141
|
+
if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
|
|
1142
|
+
// Avoid accessing the `protocol` property due to the lazy getters.
|
|
1143
|
+
const parsedProtocol = parsed?.protocol
|
|
1144
|
+
|
|
1145
|
+
// `data:` and `blob:` disallowed due to allowing file: access via
|
|
1146
|
+
// indirection
|
|
1147
|
+
if (
|
|
1148
|
+
parsedProtocol &&
|
|
1149
|
+
parsedProtocol !== 'https:' &&
|
|
1150
|
+
parsedProtocol !== 'http:'
|
|
1151
|
+
) {
|
|
1152
|
+
throw new ERR_NETWORK_IMPORT_DISALLOWED(
|
|
1153
|
+
specifier,
|
|
1154
|
+
parsedParentURL,
|
|
1155
|
+
'remote imports cannot import from a local location.'
|
|
1156
|
+
)
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
return {url: parsed?.href || ''}
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
if (builtinModules.includes(specifier)) {
|
|
1163
|
+
throw new ERR_NETWORK_IMPORT_DISALLOWED(
|
|
1164
|
+
specifier,
|
|
1165
|
+
parsedParentURL,
|
|
1166
|
+
'remote imports cannot import from a local location.'
|
|
1167
|
+
)
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
throw new ERR_NETWORK_IMPORT_DISALLOWED(
|
|
1171
|
+
specifier,
|
|
1172
|
+
parsedParentURL,
|
|
1173
|
+
'only relative and absolute specifiers are supported.'
|
|
1174
|
+
)
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
// Note: this is from:
|
|
1180
|
+
// <https://github.com/nodejs/node/blob/3e74590/lib/internal/url.js#L687>
|
|
1181
|
+
/**
|
|
1182
|
+
* Checks if a value has the shape of a WHATWG URL object.
|
|
1183
|
+
*
|
|
1184
|
+
* Using a symbol or instanceof would not be able to recognize URL objects
|
|
1185
|
+
* coming from other implementations (e.g. in Electron), so instead we are
|
|
1186
|
+
* checking some well known properties for a lack of a better test.
|
|
1187
|
+
*
|
|
1188
|
+
* We use `href` and `protocol` as they are the only properties that are
|
|
1189
|
+
* easy to retrieve and calculate due to the lazy nature of the getters.
|
|
1190
|
+
*
|
|
1191
|
+
* @template {unknown} Value
|
|
1192
|
+
* @param {Value} self
|
|
1193
|
+
* @returns {Value is URL}
|
|
1194
|
+
*/
|
|
1195
|
+
function isURL(self) {
|
|
1196
|
+
return Boolean(
|
|
1197
|
+
self &&
|
|
1198
|
+
typeof self === 'object' &&
|
|
1199
|
+
'href' in self &&
|
|
1200
|
+
typeof self.href === 'string' &&
|
|
1201
|
+
'protocol' in self &&
|
|
1202
|
+
typeof self.protocol === 'string' &&
|
|
1203
|
+
self.href &&
|
|
1204
|
+
self.protocol
|
|
1205
|
+
)
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
/**
|
|
1209
|
+
* Validate user-input in `context` supplied by a custom loader.
|
|
1210
|
+
*
|
|
1211
|
+
* @param {unknown} parentURL
|
|
1212
|
+
* @returns {asserts parentURL is URL | string | undefined}
|
|
1213
|
+
*/
|
|
1214
|
+
function throwIfInvalidParentURL(parentURL) {
|
|
1215
|
+
if (parentURL === undefined) {
|
|
1216
|
+
return // Main entry point, so no parent
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
if (typeof parentURL !== 'string' && !isURL(parentURL)) {
|
|
1220
|
+
throw new codes.ERR_INVALID_ARG_TYPE(
|
|
1221
|
+
'parentURL',
|
|
1222
|
+
['string', 'URL'],
|
|
1223
|
+
parentURL
|
|
1224
|
+
)
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
/**
|
|
1229
|
+
* @param {string} specifier
|
|
1230
|
+
* @param {{parentURL?: string, conditions?: Array<string>}} context
|
|
1231
|
+
* @returns {{url: string, format?: string | null}}
|
|
1232
|
+
*/
|
|
1233
|
+
export function defaultResolve(specifier, context = {}) {
|
|
1234
|
+
const {parentURL} = context
|
|
1235
|
+
assert.ok(parentURL !== undefined, 'expected `parentURL` to be defined')
|
|
1236
|
+
throwIfInvalidParentURL(parentURL)
|
|
1237
|
+
|
|
1238
|
+
/** @type {URL | undefined} */
|
|
1239
|
+
let parsedParentURL
|
|
1240
|
+
if (parentURL) {
|
|
1241
|
+
try {
|
|
1242
|
+
parsedParentURL = new URL(parentURL)
|
|
1243
|
+
} catch {
|
|
1244
|
+
// Ignore exception
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
/** @type {URL | undefined} */
|
|
1249
|
+
let parsed
|
|
1250
|
+
/** @type {string | undefined} */
|
|
1251
|
+
let protocol
|
|
1252
|
+
|
|
1253
|
+
try {
|
|
1254
|
+
parsed = shouldBeTreatedAsRelativeOrAbsolutePath(specifier)
|
|
1255
|
+
? new URL(specifier, parsedParentURL)
|
|
1256
|
+
: new URL(specifier)
|
|
1257
|
+
|
|
1258
|
+
// Avoid accessing the `protocol` property due to the lazy getters.
|
|
1259
|
+
protocol = parsed.protocol
|
|
1260
|
+
|
|
1261
|
+
if (protocol === 'data:') {
|
|
1262
|
+
return {url: parsed.href, format: null}
|
|
1263
|
+
}
|
|
1264
|
+
} catch {
|
|
1265
|
+
// Ignore exception
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
// There are multiple deep branches that can either throw or return; instead
|
|
1269
|
+
// of duplicating that deeply nested logic for the possible returns, DRY and
|
|
1270
|
+
// check for a return. This seems the least gnarly.
|
|
1271
|
+
const maybeReturn = checkIfDisallowedImport(
|
|
1272
|
+
specifier,
|
|
1273
|
+
parsed,
|
|
1274
|
+
parsedParentURL
|
|
1275
|
+
)
|
|
1276
|
+
|
|
1277
|
+
if (maybeReturn) return maybeReturn
|
|
1278
|
+
|
|
1279
|
+
// This must come after checkIfDisallowedImport
|
|
1280
|
+
if (protocol === undefined && parsed) {
|
|
1281
|
+
protocol = parsed.protocol
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
if (protocol === 'node:') {
|
|
1285
|
+
return {url: specifier}
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
// This must come after checkIfDisallowedImport
|
|
1289
|
+
if (parsed && parsed.protocol === 'node:') return {url: specifier}
|
|
1290
|
+
|
|
1291
|
+
const conditions = getConditionsSet(context.conditions)
|
|
1292
|
+
|
|
1293
|
+
const url = moduleResolve(specifier, new URL(parentURL), conditions, false)
|
|
1294
|
+
|
|
1295
|
+
return {
|
|
1296
|
+
// Do NOT cast `url` to a string: that will work even when there are real
|
|
1297
|
+
// problems, silencing them
|
|
1298
|
+
url: url.href,
|
|
1299
|
+
format: defaultGetFormatWithoutErrors(url, {parentURL})
|
|
1300
|
+
}
|
|
1301
|
+
}
|