nitro-nightly 4.0.0-20251010-091516-7cafddba → 4.0.0-20251030-091344-d4418b98
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/dist/{_chunks/build2.mjs → _build/build.mjs} +108 -58
- package/dist/_build/build2.mjs +556 -0
- package/dist/_build/info.mjs +1002 -0
- package/dist/_build/prepare.mjs +1511 -0
- package/dist/_build/snapshot.mjs +185 -0
- package/dist/{_chunks/plugin.mjs → _build/vite.mjs} +295 -296
- package/dist/_build/vite2.mjs +149 -0
- package/dist/_chunks/_deps/@jridgewell/gen-mapping.mjs +189 -0
- package/dist/_chunks/_deps/@jridgewell/remapping.mjs +137 -0
- package/dist/_chunks/_deps/@jridgewell/resolve-uri.mjs +231 -0
- package/dist/_chunks/_deps/@jridgewell/sourcemap-codec.mjs +173 -0
- package/dist/_chunks/_deps/@jridgewell/trace-mapping.mjs +170 -0
- package/dist/_chunks/_deps/@pi0/vite-plugin-fullstack.mjs +575 -0
- package/dist/_chunks/_deps/@rollup/plugin-alias.mjs +89 -0
- package/dist/_chunks/_deps/@rollup/plugin-commonjs.mjs +2376 -0
- package/dist/_chunks/{index2.mjs → _deps/@rollup/plugin-inject.mjs} +5 -90
- package/dist/_chunks/_deps/@rollup/plugin-json.mjs +37 -0
- package/dist/_chunks/_deps/@rollup/plugin-node-resolve.mjs +1386 -0
- package/dist/_chunks/_deps/@rollup/plugin-replace.mjs +133 -0
- package/dist/_chunks/_deps/@rollup/pluginutils.mjs +346 -0
- package/dist/_chunks/_deps/acorn.mjs +6225 -0
- package/dist/_chunks/_deps/c12.mjs +510 -0
- package/dist/_chunks/_deps/chokidar.mjs +1428 -0
- package/dist/_chunks/_deps/citty.mjs +460 -0
- package/dist/_chunks/_deps/commondir.mjs +77 -0
- package/dist/_chunks/_deps/compatx.mjs +76 -0
- package/dist/_chunks/_deps/confbox.mjs +300 -0
- package/dist/_chunks/_deps/debug.mjs +885 -0
- package/dist/_chunks/_deps/deepmerge.mjs +147 -0
- package/dist/_chunks/_deps/depd.mjs +550 -0
- package/dist/_chunks/_deps/dot-prop.mjs +282 -0
- package/dist/_chunks/_deps/dotenv.mjs +555 -0
- package/dist/_chunks/_deps/duplexer.mjs +1 -0
- package/dist/_chunks/_deps/ee-first.mjs +104 -0
- package/dist/_chunks/_deps/encodeurl.mjs +69 -0
- package/dist/_chunks/_deps/escape-html.mjs +87 -0
- package/dist/_chunks/_deps/escape-string-regexp.mjs +13 -0
- package/dist/_chunks/_deps/estree-walker.mjs +433 -0
- package/dist/_chunks/_deps/etag.mjs +147 -0
- package/dist/_chunks/_deps/exsolve.mjs +1416 -0
- package/dist/_chunks/_deps/fdir.mjs +569 -0
- package/dist/_chunks/_deps/fresh.mjs +145 -0
- package/dist/_chunks/_deps/function-bind.mjs +106 -0
- package/dist/_chunks/{index4.mjs → _deps/giget.mjs} +21 -776
- package/dist/_chunks/_deps/gzip-size.mjs +19 -0
- package/dist/_chunks/_deps/hasown.mjs +19 -0
- package/dist/_chunks/_deps/http-errors.mjs +307 -0
- package/dist/_chunks/_deps/httpxy.mjs +580 -0
- package/dist/_chunks/_deps/inherits.mjs +57 -0
- package/dist/_chunks/_deps/is-core-module.mjs +596 -0
- package/dist/_chunks/_deps/is-module.mjs +25 -0
- package/dist/_chunks/_deps/is-reference.mjs +31 -0
- package/dist/_chunks/_deps/js-tokens.mjs +411 -0
- package/dist/_chunks/_deps/klona.mjs +137 -0
- package/dist/_chunks/_deps/knitwork.mjs +172 -0
- package/dist/_chunks/_deps/local-pkg.mjs +163 -0
- package/dist/_chunks/_deps/magic-string.mjs +1296 -0
- package/dist/_chunks/_deps/mime-db.mjs +11685 -0
- package/dist/_chunks/_deps/mime-types.mjs +287 -0
- package/dist/_chunks/_deps/mime.mjs +1172 -0
- package/dist/_chunks/_deps/mlly.mjs +2413 -0
- package/dist/_chunks/_deps/ms.mjs +172 -0
- package/dist/_chunks/_deps/node-fetch-native.mjs +3 -0
- package/dist/_chunks/_deps/nypm.mjs +219 -0
- package/dist/_chunks/_deps/on-finished.mjs +246 -0
- package/dist/_chunks/_deps/parseurl.mjs +168 -0
- package/dist/_chunks/_deps/path-parse.mjs +85 -0
- package/dist/_chunks/{pathe.M-eThtNZ.mjs → _deps/pathe.mjs} +48 -1
- package/dist/_chunks/_deps/perfect-debounce.mjs +88 -0
- package/dist/_chunks/_deps/picomatch.mjs +2144 -0
- package/dist/_chunks/_deps/pkg-types.mjs +247 -0
- package/dist/_chunks/{snapshot.mjs → _deps/pretty-bytes.mjs} +1 -105
- package/dist/_chunks/_deps/quansync.mjs +99 -0
- package/dist/_chunks/_deps/range-parser.mjs +171 -0
- package/dist/_chunks/_deps/rc9.mjs +219 -0
- package/dist/_chunks/_deps/readdirp.mjs +245 -0
- package/dist/_chunks/_deps/resolve.mjs +1260 -0
- package/dist/_chunks/_deps/rou3.mjs +326 -0
- package/dist/_chunks/_deps/send.mjs +1022 -0
- package/dist/_chunks/_deps/serve-static.mjs +228 -0
- package/dist/_chunks/_deps/setprototypeof.mjs +26 -0
- package/dist/_chunks/_deps/statuses.mjs +457 -0
- package/dist/_chunks/_deps/std-env.mjs +3 -0
- package/dist/_chunks/_deps/strip-literal.mjs +67 -0
- package/dist/_chunks/_deps/supports-color.mjs +44 -0
- package/dist/_chunks/_deps/tinyexec.mjs +552 -0
- package/dist/_chunks/_deps/tinyglobby.mjs +293 -0
- package/dist/_chunks/_deps/toidentifier.mjs +41 -0
- package/dist/_chunks/_deps/ultrahtml.mjs +3 -0
- package/dist/_chunks/_deps/unimport.mjs +2267 -0
- package/dist/_chunks/_deps/unplugin-utils.mjs +65 -0
- package/dist/_chunks/_deps/unplugin.mjs +1294 -0
- package/dist/_chunks/_deps/untyped.mjs +375 -0
- package/dist/_chunks/{info.mjs → _deps/unwasm.mjs} +8 -4206
- package/dist/_chunks/_deps/webpack-virtual-modules.mjs +360 -0
- package/dist/_chunks/_presets/_all.mjs +59 -0
- package/dist/_chunks/_presets/_nitro.mjs +74 -0
- package/dist/_chunks/_presets/_resolve.mjs +64 -0
- package/dist/_chunks/_presets/_static.mjs +69 -0
- package/dist/_chunks/_presets/_types.mjs +3 -0
- package/dist/_chunks/_presets/_utils.mjs +31 -0
- package/dist/_chunks/_presets/alwaysdata.mjs +17 -0
- package/dist/_chunks/_presets/aws-amplify.mjs +111 -0
- package/dist/_chunks/_presets/aws-lambda.mjs +23 -0
- package/dist/_chunks/_presets/azure.mjs +162 -0
- package/dist/_chunks/_presets/bun.mjs +19 -0
- package/dist/_chunks/_presets/cleavr.mjs +15 -0
- package/dist/_chunks/_presets/cloudflare.mjs +608 -0
- package/dist/_chunks/_presets/deno.mjs +196 -0
- package/dist/_chunks/_presets/digitalocean.mjs +14 -0
- package/dist/_chunks/_presets/firebase.mjs +47 -0
- package/dist/_chunks/_presets/flightcontrol.mjs +14 -0
- package/dist/_chunks/_presets/genezio.mjs +13 -0
- package/dist/_chunks/_presets/heroku.mjs +14 -0
- package/dist/_chunks/_presets/iis.mjs +194 -0
- package/dist/_chunks/_presets/index.mjs +62 -0
- package/dist/_chunks/_presets/koyeb.mjs +14 -0
- package/dist/_chunks/_presets/netlify.mjs +241 -0
- package/dist/_chunks/_presets/node.mjs +54 -0
- package/dist/_chunks/_presets/platform.mjs +14 -0
- package/dist/_chunks/_presets/render.mjs +14 -0
- package/dist/_chunks/_presets/standard.mjs +23 -0
- package/dist/_chunks/_presets/stormkit.mjs +18 -0
- package/dist/_chunks/_presets/vercel.mjs +375 -0
- package/dist/_chunks/_presets/winterjs.mjs +22 -0
- package/dist/_chunks/_presets/zeabur.mjs +69 -0
- package/dist/_chunks/_presets/zerops.mjs +27 -0
- package/dist/_chunks/app.mjs +9 -19206
- package/dist/_chunks/{index3.mjs → builder.mjs} +560 -970
- package/dist/_chunks/server.mjs +6 -4
- package/dist/{cli → _cli}/build.mjs +3 -8
- package/dist/{cli → _cli}/dev.mjs +38 -12
- package/dist/{cli/index2.mjs → _cli/index.mjs} +1 -2
- package/dist/{cli → _cli}/list.mjs +3 -4
- package/dist/{cli → _cli}/prepare.mjs +3 -4
- package/dist/{cli → _cli}/run.mjs +3 -4
- package/dist/{index.d.mts → builder.d.mts} +11 -11
- package/dist/builder.mjs +117 -0
- package/dist/cli/index.mjs +7 -464
- package/dist/node_modules/@speed-highlight/core/dist/index.js +1 -1
- package/dist/node_modules/@speed-highlight/core/dist/terminal.js +1 -1
- package/dist/node_modules/@speed-highlight/core/package.json +7 -3
- package/dist/node_modules/cookie-es/dist/index.mjs +262 -0
- package/dist/node_modules/cookie-es/package.json +37 -0
- package/dist/node_modules/hookable/dist/index.mjs +243 -266
- package/dist/node_modules/hookable/package.json +29 -26
- package/dist/node_modules/rendu/dist/index.mjs +380 -0
- package/dist/node_modules/rendu/package.json +47 -0
- package/dist/presets/_nitro/runtime/nitro-dev.mjs +4 -7
- package/dist/presets/_nitro/runtime/nitro-prerenderer.mjs +5 -4
- package/dist/presets/azure/runtime/azure-swa.mjs +1 -1
- package/dist/presets/cloudflare/runtime/_module-handler.mjs +7 -6
- package/dist/presets/cloudflare/runtime/cloudflare-durable.mjs +4 -5
- package/dist/presets/cloudflare/runtime/plugin.dev.mjs +7 -10
- package/dist/presets/cloudflare/runtime/shims/workers.dev.d.mts +21 -0
- package/dist/presets/cloudflare/runtime/shims/workers.dev.mjs +27 -0
- package/dist/presets/node/runtime/node-server.mjs +1 -1
- package/dist/runtime/index.d.mts +1 -1
- package/dist/runtime/index.mjs +1 -1
- package/dist/runtime/internal/app.d.mts +3 -1
- package/dist/runtime/internal/app.mjs +90 -64
- package/dist/runtime/internal/error/prod.d.mts +3 -2
- package/dist/runtime/internal/error/prod.mjs +9 -13
- package/dist/runtime/internal/renderer.mjs +4 -4
- package/dist/runtime/internal/routes/dev-tasks.d.mts +30 -2
- package/dist/runtime/internal/shutdown.d.mts +1 -2
- package/dist/runtime/internal/shutdown.mjs +3 -2
- package/dist/runtime/internal/task.mjs +1 -2
- package/dist/types/index.d.mts +1603 -10
- package/dist/vite.d.mts +5 -0
- package/dist/vite.mjs +94 -30
- package/lib/index.mjs +1 -0
- package/lib/indexd.mts +1 -0
- package/lib/runtime/meta.mjs +35 -0
- package/package.json +40 -41
- package/dist/_chunks/build.mjs +0 -84
- package/dist/_chunks/build3.mjs +0 -6452
- package/dist/_chunks/detect-acorn.mjs +0 -503
- package/dist/_chunks/index.mjs +0 -22242
- package/dist/_chunks/json5.mjs +0 -68
- package/dist/_chunks/jsonc.mjs +0 -51
- package/dist/_chunks/toml.mjs +0 -259
- package/dist/_chunks/yaml.mjs +0 -86
- package/dist/index.mjs +0 -55
- package/dist/node_modules/klona/dist/index.mjs +0 -81
- package/dist/node_modules/klona/full/index.mjs +0 -53
- package/dist/node_modules/klona/package.json +0 -74
- package/dist/node_modules/std-env/dist/index.mjs +0 -1
- package/dist/node_modules/std-env/package.json +0 -46
- package/dist/presets.mjs +0 -2460
- package/dist/runtime/internal/debug.d.mts +0 -2
- package/dist/runtime/internal/debug.mjs +0 -5
- package/lib/runtime-meta.mjs +0 -38
- /package/dist/{cli → _cli}/common.mjs +0 -0
- /package/lib/{runtime-meta.d.mts → runtime/meta.d.mts} +0 -0
|
@@ -0,0 +1,2413 @@
|
|
|
1
|
+
import { t as tokenizer } from './acorn.mjs';
|
|
2
|
+
import { builtinModules } from 'node:module';
|
|
3
|
+
import fs, { promises, statSync, realpathSync } from 'node:fs';
|
|
4
|
+
import { joinURL } from 'ufo';
|
|
5
|
+
import { a as readPackageJSON } from './pkg-types.mjs';
|
|
6
|
+
import { fileURLToPath as fileURLToPath$1, pathToFileURL as pathToFileURL$1, URL as URL$1 } from 'node:url';
|
|
7
|
+
import assert from 'node:assert';
|
|
8
|
+
import process$1 from 'node:process';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import v8 from 'node:v8';
|
|
11
|
+
import { format, inspect } from 'node:util';
|
|
12
|
+
import { n as normalize, i as isAbsolute } from './pathe.mjs';
|
|
13
|
+
|
|
14
|
+
const BUILTIN_MODULES = new Set(builtinModules);
|
|
15
|
+
function normalizeSlash(path) {
|
|
16
|
+
return path.replace(/\\/g, "/");
|
|
17
|
+
}
|
|
18
|
+
function matchAll(regex, string, addition) {
|
|
19
|
+
const matches = [];
|
|
20
|
+
for (const match of string.matchAll(regex)) {
|
|
21
|
+
matches.push({
|
|
22
|
+
...addition,
|
|
23
|
+
...match.groups,
|
|
24
|
+
code: match[0],
|
|
25
|
+
start: match.index,
|
|
26
|
+
end: (match.index || 0) + match[0].length
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return matches;
|
|
30
|
+
}
|
|
31
|
+
function clearImports(imports) {
|
|
32
|
+
return (imports || "").replace(/\/\/[^\n]*\n|\/\*.*\*\//g, "").replace(/\s+/g, " ");
|
|
33
|
+
}
|
|
34
|
+
function getImportNames(cleanedImports) {
|
|
35
|
+
const topLevelImports = cleanedImports.replace(/{[^}]*}/, "");
|
|
36
|
+
const namespacedImport = topLevelImports.match(/\* as \s*(\S*)/)?.[1];
|
|
37
|
+
const defaultImport = topLevelImports.split(",").find((index) => !/[*{}]/.test(index))?.trim() || void 0;
|
|
38
|
+
return {
|
|
39
|
+
namespacedImport,
|
|
40
|
+
defaultImport
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @typedef ErrnoExceptionFields
|
|
46
|
+
* @property {number | undefined} [errnode]
|
|
47
|
+
* @property {string | undefined} [code]
|
|
48
|
+
* @property {string | undefined} [path]
|
|
49
|
+
* @property {string | undefined} [syscall]
|
|
50
|
+
* @property {string | undefined} [url]
|
|
51
|
+
*
|
|
52
|
+
* @typedef {Error & ErrnoExceptionFields} ErrnoException
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
const own$1 = {}.hasOwnProperty;
|
|
57
|
+
|
|
58
|
+
const classRegExp = /^([A-Z][a-z\d]*)+$/;
|
|
59
|
+
// Sorted by a rough estimate on most frequently used entries.
|
|
60
|
+
const kTypes = new Set([
|
|
61
|
+
'string',
|
|
62
|
+
'function',
|
|
63
|
+
'number',
|
|
64
|
+
'object',
|
|
65
|
+
// Accept 'Function' and 'Object' as alternative to the lower cased version.
|
|
66
|
+
'Function',
|
|
67
|
+
'Object',
|
|
68
|
+
'boolean',
|
|
69
|
+
'bigint',
|
|
70
|
+
'symbol'
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
const codes = {};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Create a list string in the form like 'A and B' or 'A, B, ..., and Z'.
|
|
77
|
+
* We cannot use Intl.ListFormat because it's not available in
|
|
78
|
+
* --without-intl builds.
|
|
79
|
+
*
|
|
80
|
+
* @param {Array<string>} array
|
|
81
|
+
* An array of strings.
|
|
82
|
+
* @param {string} [type]
|
|
83
|
+
* The list type to be inserted before the last element.
|
|
84
|
+
* @returns {string}
|
|
85
|
+
*/
|
|
86
|
+
function formatList(array, type = 'and') {
|
|
87
|
+
return array.length < 3
|
|
88
|
+
? array.join(` ${type} `)
|
|
89
|
+
: `${array.slice(0, -1).join(', ')}, ${type} ${array[array.length - 1]}`
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** @type {Map<string, MessageFunction | string>} */
|
|
93
|
+
const messages = new Map();
|
|
94
|
+
const nodeInternalPrefix = '__node_internal_';
|
|
95
|
+
/** @type {number} */
|
|
96
|
+
let userStackTraceLimit;
|
|
97
|
+
|
|
98
|
+
codes.ERR_INVALID_ARG_TYPE = createError(
|
|
99
|
+
'ERR_INVALID_ARG_TYPE',
|
|
100
|
+
/**
|
|
101
|
+
* @param {string} name
|
|
102
|
+
* @param {Array<string> | string} expected
|
|
103
|
+
* @param {unknown} actual
|
|
104
|
+
*/
|
|
105
|
+
(name, expected, actual) => {
|
|
106
|
+
assert(typeof name === 'string', "'name' must be a string");
|
|
107
|
+
if (!Array.isArray(expected)) {
|
|
108
|
+
expected = [expected];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let message = 'The ';
|
|
112
|
+
if (name.endsWith(' argument')) {
|
|
113
|
+
// For cases like 'first argument'
|
|
114
|
+
message += `${name} `;
|
|
115
|
+
} else {
|
|
116
|
+
const type = name.includes('.') ? 'property' : 'argument';
|
|
117
|
+
message += `"${name}" ${type} `;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
message += 'must be ';
|
|
121
|
+
|
|
122
|
+
/** @type {Array<string>} */
|
|
123
|
+
const types = [];
|
|
124
|
+
/** @type {Array<string>} */
|
|
125
|
+
const instances = [];
|
|
126
|
+
/** @type {Array<string>} */
|
|
127
|
+
const other = [];
|
|
128
|
+
|
|
129
|
+
for (const value of expected) {
|
|
130
|
+
assert(
|
|
131
|
+
typeof value === 'string',
|
|
132
|
+
'All expected entries have to be of type string'
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
if (kTypes.has(value)) {
|
|
136
|
+
types.push(value.toLowerCase());
|
|
137
|
+
} else if (classRegExp.exec(value) === null) {
|
|
138
|
+
assert(
|
|
139
|
+
value !== 'object',
|
|
140
|
+
'The value "object" should be written as "Object"'
|
|
141
|
+
);
|
|
142
|
+
other.push(value);
|
|
143
|
+
} else {
|
|
144
|
+
instances.push(value);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Special handle `object` in case other instances are allowed to outline
|
|
149
|
+
// the differences between each other.
|
|
150
|
+
if (instances.length > 0) {
|
|
151
|
+
const pos = types.indexOf('object');
|
|
152
|
+
if (pos !== -1) {
|
|
153
|
+
types.slice(pos, 1);
|
|
154
|
+
instances.push('Object');
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (types.length > 0) {
|
|
159
|
+
message += `${types.length > 1 ? 'one of type' : 'of type'} ${formatList(
|
|
160
|
+
types,
|
|
161
|
+
'or'
|
|
162
|
+
)}`;
|
|
163
|
+
if (instances.length > 0 || other.length > 0) message += ' or ';
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (instances.length > 0) {
|
|
167
|
+
message += `an instance of ${formatList(instances, 'or')}`;
|
|
168
|
+
if (other.length > 0) message += ' or ';
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (other.length > 0) {
|
|
172
|
+
if (other.length > 1) {
|
|
173
|
+
message += `one of ${formatList(other, 'or')}`;
|
|
174
|
+
} else {
|
|
175
|
+
if (other[0].toLowerCase() !== other[0]) message += 'an ';
|
|
176
|
+
message += `${other[0]}`;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
message += `. Received ${determineSpecificType(actual)}`;
|
|
181
|
+
|
|
182
|
+
return message
|
|
183
|
+
},
|
|
184
|
+
TypeError
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
codes.ERR_INVALID_MODULE_SPECIFIER = createError(
|
|
188
|
+
'ERR_INVALID_MODULE_SPECIFIER',
|
|
189
|
+
/**
|
|
190
|
+
* @param {string} request
|
|
191
|
+
* @param {string} reason
|
|
192
|
+
* @param {string} [base]
|
|
193
|
+
*/
|
|
194
|
+
(request, reason, base = undefined) => {
|
|
195
|
+
return `Invalid module "${request}" ${reason}${
|
|
196
|
+
base ? ` imported from ${base}` : ''
|
|
197
|
+
}`
|
|
198
|
+
},
|
|
199
|
+
TypeError
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
codes.ERR_INVALID_PACKAGE_CONFIG = createError(
|
|
203
|
+
'ERR_INVALID_PACKAGE_CONFIG',
|
|
204
|
+
/**
|
|
205
|
+
* @param {string} path
|
|
206
|
+
* @param {string} [base]
|
|
207
|
+
* @param {string} [message]
|
|
208
|
+
*/
|
|
209
|
+
(path, base, message) => {
|
|
210
|
+
return `Invalid package config ${path}${
|
|
211
|
+
base ? ` while importing ${base}` : ''
|
|
212
|
+
}${message ? `. ${message}` : ''}`
|
|
213
|
+
},
|
|
214
|
+
Error
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
codes.ERR_INVALID_PACKAGE_TARGET = createError(
|
|
218
|
+
'ERR_INVALID_PACKAGE_TARGET',
|
|
219
|
+
/**
|
|
220
|
+
* @param {string} packagePath
|
|
221
|
+
* @param {string} key
|
|
222
|
+
* @param {unknown} target
|
|
223
|
+
* @param {boolean} [isImport=false]
|
|
224
|
+
* @param {string} [base]
|
|
225
|
+
*/
|
|
226
|
+
(packagePath, key, target, isImport = false, base = undefined) => {
|
|
227
|
+
const relatedError =
|
|
228
|
+
typeof target === 'string' &&
|
|
229
|
+
!isImport &&
|
|
230
|
+
target.length > 0 &&
|
|
231
|
+
!target.startsWith('./');
|
|
232
|
+
if (key === '.') {
|
|
233
|
+
assert(isImport === false);
|
|
234
|
+
return (
|
|
235
|
+
`Invalid "exports" main target ${JSON.stringify(target)} defined ` +
|
|
236
|
+
`in the package config ${packagePath}package.json${
|
|
237
|
+
base ? ` imported from ${base}` : ''
|
|
238
|
+
}${relatedError ? '; targets must start with "./"' : ''}`
|
|
239
|
+
)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return `Invalid "${
|
|
243
|
+
isImport ? 'imports' : 'exports'
|
|
244
|
+
}" target ${JSON.stringify(
|
|
245
|
+
target
|
|
246
|
+
)} defined for '${key}' in the package config ${packagePath}package.json${
|
|
247
|
+
base ? ` imported from ${base}` : ''
|
|
248
|
+
}${relatedError ? '; targets must start with "./"' : ''}`
|
|
249
|
+
},
|
|
250
|
+
Error
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
codes.ERR_MODULE_NOT_FOUND = createError(
|
|
254
|
+
'ERR_MODULE_NOT_FOUND',
|
|
255
|
+
/**
|
|
256
|
+
* @param {string} path
|
|
257
|
+
* @param {string} base
|
|
258
|
+
* @param {boolean} [exactUrl]
|
|
259
|
+
*/
|
|
260
|
+
(path, base, exactUrl = false) => {
|
|
261
|
+
return `Cannot find ${
|
|
262
|
+
exactUrl ? 'module' : 'package'
|
|
263
|
+
} '${path}' imported from ${base}`
|
|
264
|
+
},
|
|
265
|
+
Error
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
codes.ERR_NETWORK_IMPORT_DISALLOWED = createError(
|
|
269
|
+
'ERR_NETWORK_IMPORT_DISALLOWED',
|
|
270
|
+
"import of '%s' by %s is not supported: %s",
|
|
271
|
+
Error
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
codes.ERR_PACKAGE_IMPORT_NOT_DEFINED = createError(
|
|
275
|
+
'ERR_PACKAGE_IMPORT_NOT_DEFINED',
|
|
276
|
+
/**
|
|
277
|
+
* @param {string} specifier
|
|
278
|
+
* @param {string} packagePath
|
|
279
|
+
* @param {string} base
|
|
280
|
+
*/
|
|
281
|
+
(specifier, packagePath, base) => {
|
|
282
|
+
return `Package import specifier "${specifier}" is not defined${
|
|
283
|
+
packagePath ? ` in package ${packagePath}package.json` : ''
|
|
284
|
+
} imported from ${base}`
|
|
285
|
+
},
|
|
286
|
+
TypeError
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
codes.ERR_PACKAGE_PATH_NOT_EXPORTED = createError(
|
|
290
|
+
'ERR_PACKAGE_PATH_NOT_EXPORTED',
|
|
291
|
+
/**
|
|
292
|
+
* @param {string} packagePath
|
|
293
|
+
* @param {string} subpath
|
|
294
|
+
* @param {string} [base]
|
|
295
|
+
*/
|
|
296
|
+
(packagePath, subpath, base = undefined) => {
|
|
297
|
+
if (subpath === '.')
|
|
298
|
+
return `No "exports" main defined in ${packagePath}package.json${
|
|
299
|
+
base ? ` imported from ${base}` : ''
|
|
300
|
+
}`
|
|
301
|
+
return `Package subpath '${subpath}' is not defined by "exports" in ${packagePath}package.json${
|
|
302
|
+
base ? ` imported from ${base}` : ''
|
|
303
|
+
}`
|
|
304
|
+
},
|
|
305
|
+
Error
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
codes.ERR_UNSUPPORTED_DIR_IMPORT = createError(
|
|
309
|
+
'ERR_UNSUPPORTED_DIR_IMPORT',
|
|
310
|
+
"Directory import '%s' is not supported " +
|
|
311
|
+
'resolving ES modules imported from %s',
|
|
312
|
+
Error
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
codes.ERR_UNSUPPORTED_RESOLVE_REQUEST = createError(
|
|
316
|
+
'ERR_UNSUPPORTED_RESOLVE_REQUEST',
|
|
317
|
+
'Failed to resolve module specifier "%s" from "%s": Invalid relative URL or base scheme is not hierarchical.',
|
|
318
|
+
TypeError
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
codes.ERR_UNKNOWN_FILE_EXTENSION = createError(
|
|
322
|
+
'ERR_UNKNOWN_FILE_EXTENSION',
|
|
323
|
+
/**
|
|
324
|
+
* @param {string} extension
|
|
325
|
+
* @param {string} path
|
|
326
|
+
*/
|
|
327
|
+
(extension, path) => {
|
|
328
|
+
return `Unknown file extension "${extension}" for ${path}`
|
|
329
|
+
},
|
|
330
|
+
TypeError
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
codes.ERR_INVALID_ARG_VALUE = createError(
|
|
334
|
+
'ERR_INVALID_ARG_VALUE',
|
|
335
|
+
/**
|
|
336
|
+
* @param {string} name
|
|
337
|
+
* @param {unknown} value
|
|
338
|
+
* @param {string} [reason='is invalid']
|
|
339
|
+
*/
|
|
340
|
+
(name, value, reason = 'is invalid') => {
|
|
341
|
+
let inspected = inspect(value);
|
|
342
|
+
|
|
343
|
+
if (inspected.length > 128) {
|
|
344
|
+
inspected = `${inspected.slice(0, 128)}...`;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const type = name.includes('.') ? 'property' : 'argument';
|
|
348
|
+
|
|
349
|
+
return `The ${type} '${name}' ${reason}. Received ${inspected}`
|
|
350
|
+
},
|
|
351
|
+
TypeError
|
|
352
|
+
// Note: extra classes have been shaken out.
|
|
353
|
+
// , RangeError
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Utility function for registering the error codes. Only used here. Exported
|
|
358
|
+
* *only* to allow for testing.
|
|
359
|
+
* @param {string} sym
|
|
360
|
+
* @param {MessageFunction | string} value
|
|
361
|
+
* @param {ErrorConstructor} constructor
|
|
362
|
+
* @returns {new (...parameters: Array<any>) => Error}
|
|
363
|
+
*/
|
|
364
|
+
function createError(sym, value, constructor) {
|
|
365
|
+
// Special case for SystemError that formats the error message differently
|
|
366
|
+
// The SystemErrors only have SystemError as their base classes.
|
|
367
|
+
messages.set(sym, value);
|
|
368
|
+
|
|
369
|
+
return makeNodeErrorWithCode(constructor, sym)
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* @param {ErrorConstructor} Base
|
|
374
|
+
* @param {string} key
|
|
375
|
+
* @returns {ErrorConstructor}
|
|
376
|
+
*/
|
|
377
|
+
function makeNodeErrorWithCode(Base, key) {
|
|
378
|
+
// @ts-expect-error It’s a Node error.
|
|
379
|
+
return NodeError
|
|
380
|
+
/**
|
|
381
|
+
* @param {Array<unknown>} parameters
|
|
382
|
+
*/
|
|
383
|
+
function NodeError(...parameters) {
|
|
384
|
+
const limit = Error.stackTraceLimit;
|
|
385
|
+
if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;
|
|
386
|
+
const error = new Base();
|
|
387
|
+
// Reset the limit and setting the name property.
|
|
388
|
+
if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = limit;
|
|
389
|
+
const message = getMessage(key, parameters, error);
|
|
390
|
+
Object.defineProperties(error, {
|
|
391
|
+
// Note: no need to implement `kIsNodeError` symbol, would be hard,
|
|
392
|
+
// probably.
|
|
393
|
+
message: {
|
|
394
|
+
value: message,
|
|
395
|
+
enumerable: false,
|
|
396
|
+
writable: true,
|
|
397
|
+
configurable: true
|
|
398
|
+
},
|
|
399
|
+
toString: {
|
|
400
|
+
/** @this {Error} */
|
|
401
|
+
value() {
|
|
402
|
+
return `${this.name} [${key}]: ${this.message}`
|
|
403
|
+
},
|
|
404
|
+
enumerable: false,
|
|
405
|
+
writable: true,
|
|
406
|
+
configurable: true
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
captureLargerStackTrace(error);
|
|
411
|
+
// @ts-expect-error It’s a Node error.
|
|
412
|
+
error.code = key;
|
|
413
|
+
return error
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* @returns {boolean}
|
|
419
|
+
*/
|
|
420
|
+
function isErrorStackTraceLimitWritable() {
|
|
421
|
+
// Do no touch Error.stackTraceLimit as V8 would attempt to install
|
|
422
|
+
// it again during deserialization.
|
|
423
|
+
try {
|
|
424
|
+
if (v8.startupSnapshot.isBuildingSnapshot()) {
|
|
425
|
+
return false
|
|
426
|
+
}
|
|
427
|
+
} catch {}
|
|
428
|
+
|
|
429
|
+
const desc = Object.getOwnPropertyDescriptor(Error, 'stackTraceLimit');
|
|
430
|
+
if (desc === undefined) {
|
|
431
|
+
return Object.isExtensible(Error)
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
return own$1.call(desc, 'writable') && desc.writable !== undefined
|
|
435
|
+
? desc.writable
|
|
436
|
+
: desc.set !== undefined
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* This function removes unnecessary frames from Node.js core errors.
|
|
441
|
+
* @template {(...parameters: unknown[]) => unknown} T
|
|
442
|
+
* @param {T} wrappedFunction
|
|
443
|
+
* @returns {T}
|
|
444
|
+
*/
|
|
445
|
+
function hideStackFrames(wrappedFunction) {
|
|
446
|
+
// We rename the functions that will be hidden to cut off the stacktrace
|
|
447
|
+
// at the outermost one
|
|
448
|
+
const hidden = nodeInternalPrefix + wrappedFunction.name;
|
|
449
|
+
Object.defineProperty(wrappedFunction, 'name', {value: hidden});
|
|
450
|
+
return wrappedFunction
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const captureLargerStackTrace = hideStackFrames(
|
|
454
|
+
/**
|
|
455
|
+
* @param {Error} error
|
|
456
|
+
* @returns {Error}
|
|
457
|
+
*/
|
|
458
|
+
// @ts-expect-error: fine
|
|
459
|
+
function (error) {
|
|
460
|
+
const stackTraceLimitIsWritable = isErrorStackTraceLimitWritable();
|
|
461
|
+
if (stackTraceLimitIsWritable) {
|
|
462
|
+
userStackTraceLimit = Error.stackTraceLimit;
|
|
463
|
+
Error.stackTraceLimit = Number.POSITIVE_INFINITY;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
Error.captureStackTrace(error);
|
|
467
|
+
|
|
468
|
+
// Reset the limit
|
|
469
|
+
if (stackTraceLimitIsWritable) Error.stackTraceLimit = userStackTraceLimit;
|
|
470
|
+
|
|
471
|
+
return error
|
|
472
|
+
}
|
|
473
|
+
);
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* @param {string} key
|
|
477
|
+
* @param {Array<unknown>} parameters
|
|
478
|
+
* @param {Error} self
|
|
479
|
+
* @returns {string}
|
|
480
|
+
*/
|
|
481
|
+
function getMessage(key, parameters, self) {
|
|
482
|
+
const message = messages.get(key);
|
|
483
|
+
assert(message !== undefined, 'expected `message` to be found');
|
|
484
|
+
|
|
485
|
+
if (typeof message === 'function') {
|
|
486
|
+
assert(
|
|
487
|
+
message.length <= parameters.length, // Default options do not count.
|
|
488
|
+
`Code: ${key}; The provided arguments length (${parameters.length}) does not ` +
|
|
489
|
+
`match the required ones (${message.length}).`
|
|
490
|
+
);
|
|
491
|
+
return Reflect.apply(message, self, parameters)
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const regex = /%[dfijoOs]/g;
|
|
495
|
+
let expectedLength = 0;
|
|
496
|
+
while (regex.exec(message) !== null) expectedLength++;
|
|
497
|
+
assert(
|
|
498
|
+
expectedLength === parameters.length,
|
|
499
|
+
`Code: ${key}; The provided arguments length (${parameters.length}) does not ` +
|
|
500
|
+
`match the required ones (${expectedLength}).`
|
|
501
|
+
);
|
|
502
|
+
if (parameters.length === 0) return message
|
|
503
|
+
|
|
504
|
+
parameters.unshift(message);
|
|
505
|
+
return Reflect.apply(format, null, parameters)
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* Determine the specific type of a value for type-mismatch errors.
|
|
510
|
+
* @param {unknown} value
|
|
511
|
+
* @returns {string}
|
|
512
|
+
*/
|
|
513
|
+
function determineSpecificType(value) {
|
|
514
|
+
if (value === null || value === undefined) {
|
|
515
|
+
return String(value)
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
if (typeof value === 'function' && value.name) {
|
|
519
|
+
return `function ${value.name}`
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (typeof value === 'object') {
|
|
523
|
+
if (value.constructor && value.constructor.name) {
|
|
524
|
+
return `an instance of ${value.constructor.name}`
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
return `${inspect(value, {depth: -1})}`
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
let inspected = inspect(value, {colors: false});
|
|
531
|
+
|
|
532
|
+
if (inspected.length > 28) {
|
|
533
|
+
inspected = `${inspected.slice(0, 25)}...`;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
return `type ${typeof value} (${inspected})`
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// Manually “tree shaken” from:
|
|
540
|
+
// <https://github.com/nodejs/node/blob/7c3dce0/lib/internal/modules/package_json_reader.js>
|
|
541
|
+
// Last checked on: Apr 29, 2023.
|
|
542
|
+
// Removed the native dependency.
|
|
543
|
+
// Also: no need to cache, we do that in resolve already.
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
const hasOwnProperty$1 = {}.hasOwnProperty;
|
|
547
|
+
|
|
548
|
+
const {ERR_INVALID_PACKAGE_CONFIG: ERR_INVALID_PACKAGE_CONFIG$1} = codes;
|
|
549
|
+
|
|
550
|
+
/** @type {Map<string, PackageConfig>} */
|
|
551
|
+
const cache = new Map();
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* @param {string} jsonPath
|
|
555
|
+
* @param {{specifier: URL | string, base?: URL}} options
|
|
556
|
+
* @returns {PackageConfig}
|
|
557
|
+
*/
|
|
558
|
+
function read(jsonPath, {base, specifier}) {
|
|
559
|
+
const existing = cache.get(jsonPath);
|
|
560
|
+
|
|
561
|
+
if (existing) {
|
|
562
|
+
return existing
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/** @type {string | undefined} */
|
|
566
|
+
let string;
|
|
567
|
+
|
|
568
|
+
try {
|
|
569
|
+
string = fs.readFileSync(path.toNamespacedPath(jsonPath), 'utf8');
|
|
570
|
+
} catch (error) {
|
|
571
|
+
const exception = /** @type {ErrnoException} */ (error);
|
|
572
|
+
|
|
573
|
+
if (exception.code !== 'ENOENT') {
|
|
574
|
+
throw exception
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/** @type {PackageConfig} */
|
|
579
|
+
const result = {
|
|
580
|
+
exists: false,
|
|
581
|
+
pjsonPath: jsonPath,
|
|
582
|
+
main: undefined,
|
|
583
|
+
name: undefined,
|
|
584
|
+
type: 'none', // Ignore unknown types for forwards compatibility
|
|
585
|
+
exports: undefined,
|
|
586
|
+
imports: undefined
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
if (string !== undefined) {
|
|
590
|
+
/** @type {Record<string, unknown>} */
|
|
591
|
+
let parsed;
|
|
592
|
+
|
|
593
|
+
try {
|
|
594
|
+
parsed = JSON.parse(string);
|
|
595
|
+
} catch (error_) {
|
|
596
|
+
const cause = /** @type {ErrnoException} */ (error_);
|
|
597
|
+
const error = new ERR_INVALID_PACKAGE_CONFIG$1(
|
|
598
|
+
jsonPath,
|
|
599
|
+
(base ? `"${specifier}" from ` : '') + fileURLToPath$1(base || specifier),
|
|
600
|
+
cause.message
|
|
601
|
+
);
|
|
602
|
+
error.cause = cause;
|
|
603
|
+
throw error
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
result.exists = true;
|
|
607
|
+
|
|
608
|
+
if (
|
|
609
|
+
hasOwnProperty$1.call(parsed, 'name') &&
|
|
610
|
+
typeof parsed.name === 'string'
|
|
611
|
+
) {
|
|
612
|
+
result.name = parsed.name;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
if (
|
|
616
|
+
hasOwnProperty$1.call(parsed, 'main') &&
|
|
617
|
+
typeof parsed.main === 'string'
|
|
618
|
+
) {
|
|
619
|
+
result.main = parsed.main;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
if (hasOwnProperty$1.call(parsed, 'exports')) {
|
|
623
|
+
// @ts-expect-error: assume valid.
|
|
624
|
+
result.exports = parsed.exports;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
if (hasOwnProperty$1.call(parsed, 'imports')) {
|
|
628
|
+
// @ts-expect-error: assume valid.
|
|
629
|
+
result.imports = parsed.imports;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// Ignore unknown types for forwards compatibility
|
|
633
|
+
if (
|
|
634
|
+
hasOwnProperty$1.call(parsed, 'type') &&
|
|
635
|
+
(parsed.type === 'commonjs' || parsed.type === 'module')
|
|
636
|
+
) {
|
|
637
|
+
result.type = parsed.type;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
cache.set(jsonPath, result);
|
|
642
|
+
|
|
643
|
+
return result
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* @param {URL | string} resolved
|
|
648
|
+
* @returns {PackageConfig}
|
|
649
|
+
*/
|
|
650
|
+
function getPackageScopeConfig(resolved) {
|
|
651
|
+
// Note: in Node, this is now a native module.
|
|
652
|
+
let packageJSONUrl = new URL('package.json', resolved);
|
|
653
|
+
|
|
654
|
+
while (true) {
|
|
655
|
+
const packageJSONPath = packageJSONUrl.pathname;
|
|
656
|
+
if (packageJSONPath.endsWith('node_modules/package.json')) {
|
|
657
|
+
break
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
const packageConfig = read(fileURLToPath$1(packageJSONUrl), {
|
|
661
|
+
specifier: resolved
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
if (packageConfig.exists) {
|
|
665
|
+
return packageConfig
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
const lastPackageJSONUrl = packageJSONUrl;
|
|
669
|
+
packageJSONUrl = new URL('../package.json', packageJSONUrl);
|
|
670
|
+
|
|
671
|
+
// Terminates at root where ../package.json equals ../../package.json
|
|
672
|
+
// (can't just check "/package.json" for Windows support).
|
|
673
|
+
if (packageJSONUrl.pathname === lastPackageJSONUrl.pathname) {
|
|
674
|
+
break
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
const packageJSONPath = fileURLToPath$1(packageJSONUrl);
|
|
679
|
+
// ^^ Note: in Node, this is now a native module.
|
|
680
|
+
|
|
681
|
+
return {
|
|
682
|
+
pjsonPath: packageJSONPath,
|
|
683
|
+
exists: false,
|
|
684
|
+
type: 'none'
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* Returns the package type for a given URL.
|
|
690
|
+
* @param {URL} url - The URL to get the package type for.
|
|
691
|
+
* @returns {PackageType}
|
|
692
|
+
*/
|
|
693
|
+
function getPackageType(url) {
|
|
694
|
+
// To do @anonrig: Write a C++ function that returns only "type".
|
|
695
|
+
return getPackageScopeConfig(url).type
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
// Manually “tree shaken” from:
|
|
699
|
+
// <https://github.com/nodejs/node/blob/7c3dce0/lib/internal/modules/esm/get_format.js>
|
|
700
|
+
// Last checked on: Apr 29, 2023.
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
const {ERR_UNKNOWN_FILE_EXTENSION} = codes;
|
|
704
|
+
|
|
705
|
+
const hasOwnProperty = {}.hasOwnProperty;
|
|
706
|
+
|
|
707
|
+
/** @type {Record<string, string>} */
|
|
708
|
+
const extensionFormatMap = {
|
|
709
|
+
// @ts-expect-error: hush.
|
|
710
|
+
__proto__: null,
|
|
711
|
+
'.cjs': 'commonjs',
|
|
712
|
+
'.js': 'module',
|
|
713
|
+
'.json': 'json',
|
|
714
|
+
'.mjs': 'module'
|
|
715
|
+
};
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* @param {string | null} mime
|
|
719
|
+
* @returns {string | null}
|
|
720
|
+
*/
|
|
721
|
+
function mimeToFormat(mime) {
|
|
722
|
+
if (
|
|
723
|
+
mime &&
|
|
724
|
+
/\s*(text|application)\/javascript\s*(;\s*charset=utf-?8\s*)?/i.test(mime)
|
|
725
|
+
)
|
|
726
|
+
return 'module'
|
|
727
|
+
if (mime === 'application/json') return 'json'
|
|
728
|
+
return null
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* @callback ProtocolHandler
|
|
733
|
+
* @param {URL} parsed
|
|
734
|
+
* @param {{parentURL: string, source?: Buffer}} context
|
|
735
|
+
* @param {boolean} ignoreErrors
|
|
736
|
+
* @returns {string | null | void}
|
|
737
|
+
*/
|
|
738
|
+
|
|
739
|
+
/**
|
|
740
|
+
* @type {Record<string, ProtocolHandler>}
|
|
741
|
+
*/
|
|
742
|
+
const protocolHandlers = {
|
|
743
|
+
// @ts-expect-error: hush.
|
|
744
|
+
__proto__: null,
|
|
745
|
+
'data:': getDataProtocolModuleFormat,
|
|
746
|
+
'file:': getFileProtocolModuleFormat,
|
|
747
|
+
'http:': getHttpProtocolModuleFormat,
|
|
748
|
+
'https:': getHttpProtocolModuleFormat,
|
|
749
|
+
'node:'() {
|
|
750
|
+
return 'builtin'
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* @param {URL} parsed
|
|
756
|
+
*/
|
|
757
|
+
function getDataProtocolModuleFormat(parsed) {
|
|
758
|
+
const {1: mime} = /^([^/]+\/[^;,]+)[^,]*?(;base64)?,/.exec(
|
|
759
|
+
parsed.pathname
|
|
760
|
+
) || [null, null, null];
|
|
761
|
+
return mimeToFormat(mime)
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* Returns the file extension from a URL.
|
|
766
|
+
*
|
|
767
|
+
* Should give similar result to
|
|
768
|
+
* `require('node:path').extname(require('node:url').fileURLToPath(url))`
|
|
769
|
+
* when used with a `file:` URL.
|
|
770
|
+
*
|
|
771
|
+
* @param {URL} url
|
|
772
|
+
* @returns {string}
|
|
773
|
+
*/
|
|
774
|
+
function extname(url) {
|
|
775
|
+
const pathname = url.pathname;
|
|
776
|
+
let index = pathname.length;
|
|
777
|
+
|
|
778
|
+
while (index--) {
|
|
779
|
+
const code = pathname.codePointAt(index);
|
|
780
|
+
|
|
781
|
+
if (code === 47 /* `/` */) {
|
|
782
|
+
return ''
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
if (code === 46 /* `.` */) {
|
|
786
|
+
return pathname.codePointAt(index - 1) === 47 /* `/` */
|
|
787
|
+
? ''
|
|
788
|
+
: pathname.slice(index)
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
return ''
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* @type {ProtocolHandler}
|
|
797
|
+
*/
|
|
798
|
+
function getFileProtocolModuleFormat(url, _context, ignoreErrors) {
|
|
799
|
+
const value = extname(url);
|
|
800
|
+
|
|
801
|
+
if (value === '.js') {
|
|
802
|
+
const packageType = getPackageType(url);
|
|
803
|
+
|
|
804
|
+
if (packageType !== 'none') {
|
|
805
|
+
return packageType
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
return 'commonjs'
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
if (value === '') {
|
|
812
|
+
const packageType = getPackageType(url);
|
|
813
|
+
|
|
814
|
+
// Legacy behavior
|
|
815
|
+
if (packageType === 'none' || packageType === 'commonjs') {
|
|
816
|
+
return 'commonjs'
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
// Note: we don’t implement WASM, so we don’t need
|
|
820
|
+
// `getFormatOfExtensionlessFile` from `formats`.
|
|
821
|
+
return 'module'
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
const format = extensionFormatMap[value];
|
|
825
|
+
if (format) return format
|
|
826
|
+
|
|
827
|
+
// Explicit undefined return indicates load hook should rerun format check
|
|
828
|
+
if (ignoreErrors) {
|
|
829
|
+
return undefined
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
const filepath = fileURLToPath$1(url);
|
|
833
|
+
throw new ERR_UNKNOWN_FILE_EXTENSION(value, filepath)
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
function getHttpProtocolModuleFormat() {
|
|
837
|
+
// To do: HTTPS imports.
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
/**
|
|
841
|
+
* @param {URL} url
|
|
842
|
+
* @param {{parentURL: string}} context
|
|
843
|
+
* @returns {string | null}
|
|
844
|
+
*/
|
|
845
|
+
function defaultGetFormatWithoutErrors(url, context) {
|
|
846
|
+
const protocol = url.protocol;
|
|
847
|
+
|
|
848
|
+
if (!hasOwnProperty.call(protocolHandlers, protocol)) {
|
|
849
|
+
return null
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
return protocolHandlers[protocol](url, context, true) || null
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
// Manually “tree shaken” from:
|
|
856
|
+
// <https://github.com/nodejs/node/blob/81a9a97/lib/internal/modules/esm/resolve.js>
|
|
857
|
+
// Last checked on: Apr 29, 2023.
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
const RegExpPrototypeSymbolReplace = RegExp.prototype[Symbol.replace];
|
|
861
|
+
|
|
862
|
+
const {
|
|
863
|
+
ERR_INVALID_MODULE_SPECIFIER,
|
|
864
|
+
ERR_INVALID_PACKAGE_CONFIG,
|
|
865
|
+
ERR_INVALID_PACKAGE_TARGET,
|
|
866
|
+
ERR_MODULE_NOT_FOUND,
|
|
867
|
+
ERR_PACKAGE_IMPORT_NOT_DEFINED,
|
|
868
|
+
ERR_PACKAGE_PATH_NOT_EXPORTED,
|
|
869
|
+
ERR_UNSUPPORTED_DIR_IMPORT,
|
|
870
|
+
ERR_UNSUPPORTED_RESOLVE_REQUEST
|
|
871
|
+
} = codes;
|
|
872
|
+
|
|
873
|
+
const own = {}.hasOwnProperty;
|
|
874
|
+
|
|
875
|
+
const invalidSegmentRegEx =
|
|
876
|
+
/(^|\\|\/)((\.|%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;
|
|
877
|
+
const deprecatedInvalidSegmentRegEx =
|
|
878
|
+
/(^|\\|\/)((\.|%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;
|
|
879
|
+
const invalidPackageNameRegEx = /^\.|%|\\/;
|
|
880
|
+
const patternRegEx = /\*/g;
|
|
881
|
+
const encodedSeparatorRegEx = /%2f|%5c/i;
|
|
882
|
+
/** @type {Set<string>} */
|
|
883
|
+
const emittedPackageWarnings = new Set();
|
|
884
|
+
|
|
885
|
+
const doubleSlashRegEx = /[/\\]{2}/;
|
|
886
|
+
|
|
887
|
+
/**
|
|
888
|
+
*
|
|
889
|
+
* @param {string} target
|
|
890
|
+
* @param {string} request
|
|
891
|
+
* @param {string} match
|
|
892
|
+
* @param {URL} packageJsonUrl
|
|
893
|
+
* @param {boolean} internal
|
|
894
|
+
* @param {URL} base
|
|
895
|
+
* @param {boolean} isTarget
|
|
896
|
+
*/
|
|
897
|
+
function emitInvalidSegmentDeprecation(
|
|
898
|
+
target,
|
|
899
|
+
request,
|
|
900
|
+
match,
|
|
901
|
+
packageJsonUrl,
|
|
902
|
+
internal,
|
|
903
|
+
base,
|
|
904
|
+
isTarget
|
|
905
|
+
) {
|
|
906
|
+
// @ts-expect-error: apparently it does exist, TS.
|
|
907
|
+
if (process$1.noDeprecation) {
|
|
908
|
+
return
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
const pjsonPath = fileURLToPath$1(packageJsonUrl);
|
|
912
|
+
const double = doubleSlashRegEx.exec(isTarget ? target : request) !== null;
|
|
913
|
+
process$1.emitWarning(
|
|
914
|
+
`Use of deprecated ${
|
|
915
|
+
double ? 'double slash' : 'leading or trailing slash matching'
|
|
916
|
+
} resolving "${target}" for module ` +
|
|
917
|
+
`request "${request}" ${
|
|
918
|
+
request === match ? '' : `matched to "${match}" `
|
|
919
|
+
}in the "${
|
|
920
|
+
internal ? 'imports' : 'exports'
|
|
921
|
+
}" field module resolution of the package at ${pjsonPath}${
|
|
922
|
+
base ? ` imported from ${fileURLToPath$1(base)}` : ''
|
|
923
|
+
}.`,
|
|
924
|
+
'DeprecationWarning',
|
|
925
|
+
'DEP0166'
|
|
926
|
+
);
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
/**
|
|
930
|
+
* @param {URL} url
|
|
931
|
+
* @param {URL} packageJsonUrl
|
|
932
|
+
* @param {URL} base
|
|
933
|
+
* @param {string} [main]
|
|
934
|
+
* @returns {void}
|
|
935
|
+
*/
|
|
936
|
+
function emitLegacyIndexDeprecation(url, packageJsonUrl, base, main) {
|
|
937
|
+
// @ts-expect-error: apparently it does exist, TS.
|
|
938
|
+
if (process$1.noDeprecation) {
|
|
939
|
+
return
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
const format = defaultGetFormatWithoutErrors(url, {parentURL: base.href});
|
|
943
|
+
if (format !== 'module') return
|
|
944
|
+
const urlPath = fileURLToPath$1(url.href);
|
|
945
|
+
const packagePath = fileURLToPath$1(new URL$1('.', packageJsonUrl));
|
|
946
|
+
const basePath = fileURLToPath$1(base);
|
|
947
|
+
if (!main) {
|
|
948
|
+
process$1.emitWarning(
|
|
949
|
+
`No "main" or "exports" field defined in the package.json for ${packagePath} resolving the main entry point "${urlPath.slice(
|
|
950
|
+
packagePath.length
|
|
951
|
+
)}", imported from ${basePath}.\nDefault "index" lookups for the main are deprecated for ES modules.`,
|
|
952
|
+
'DeprecationWarning',
|
|
953
|
+
'DEP0151'
|
|
954
|
+
);
|
|
955
|
+
} else if (path.resolve(packagePath, main) !== urlPath) {
|
|
956
|
+
process$1.emitWarning(
|
|
957
|
+
`Package ${packagePath} has a "main" field set to "${main}", ` +
|
|
958
|
+
`excluding the full filename and extension to the resolved file at "${urlPath.slice(
|
|
959
|
+
packagePath.length
|
|
960
|
+
)}", imported from ${basePath}.\n Automatic extension resolution of the "main" field is ` +
|
|
961
|
+
'deprecated for ES modules.',
|
|
962
|
+
'DeprecationWarning',
|
|
963
|
+
'DEP0151'
|
|
964
|
+
);
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
/**
|
|
969
|
+
* @param {string} path
|
|
970
|
+
* @returns {Stats | undefined}
|
|
971
|
+
*/
|
|
972
|
+
function tryStatSync(path) {
|
|
973
|
+
// Note: from Node 15 onwards we can use `throwIfNoEntry: false` instead.
|
|
974
|
+
try {
|
|
975
|
+
return statSync(path)
|
|
976
|
+
} catch {
|
|
977
|
+
// Note: in Node code this returns `new Stats`,
|
|
978
|
+
// but in Node 22 that’s marked as a deprecated internal API.
|
|
979
|
+
// Which, well, we kinda are, but still to prevent that warning,
|
|
980
|
+
// just yield `undefined`.
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
/**
|
|
985
|
+
* Legacy CommonJS main resolution:
|
|
986
|
+
* 1. let M = pkg_url + (json main field)
|
|
987
|
+
* 2. TRY(M, M.js, M.json, M.node)
|
|
988
|
+
* 3. TRY(M/index.js, M/index.json, M/index.node)
|
|
989
|
+
* 4. TRY(pkg_url/index.js, pkg_url/index.json, pkg_url/index.node)
|
|
990
|
+
* 5. NOT_FOUND
|
|
991
|
+
*
|
|
992
|
+
* @param {URL} url
|
|
993
|
+
* @returns {boolean}
|
|
994
|
+
*/
|
|
995
|
+
function fileExists(url) {
|
|
996
|
+
const stats = statSync(url, {throwIfNoEntry: false});
|
|
997
|
+
const isFile = stats ? stats.isFile() : undefined;
|
|
998
|
+
return isFile === null || isFile === undefined ? false : isFile
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
/**
|
|
1002
|
+
* @param {URL} packageJsonUrl
|
|
1003
|
+
* @param {PackageConfig} packageConfig
|
|
1004
|
+
* @param {URL} base
|
|
1005
|
+
* @returns {URL}
|
|
1006
|
+
*/
|
|
1007
|
+
function legacyMainResolve(packageJsonUrl, packageConfig, base) {
|
|
1008
|
+
/** @type {URL | undefined} */
|
|
1009
|
+
let guess;
|
|
1010
|
+
if (packageConfig.main !== undefined) {
|
|
1011
|
+
guess = new URL$1(packageConfig.main, packageJsonUrl);
|
|
1012
|
+
// Note: fs check redundances will be handled by Descriptor cache here.
|
|
1013
|
+
if (fileExists(guess)) return guess
|
|
1014
|
+
|
|
1015
|
+
const tries = [
|
|
1016
|
+
`./${packageConfig.main}.js`,
|
|
1017
|
+
`./${packageConfig.main}.json`,
|
|
1018
|
+
`./${packageConfig.main}.node`,
|
|
1019
|
+
`./${packageConfig.main}/index.js`,
|
|
1020
|
+
`./${packageConfig.main}/index.json`,
|
|
1021
|
+
`./${packageConfig.main}/index.node`
|
|
1022
|
+
];
|
|
1023
|
+
let i = -1;
|
|
1024
|
+
|
|
1025
|
+
while (++i < tries.length) {
|
|
1026
|
+
guess = new URL$1(tries[i], packageJsonUrl);
|
|
1027
|
+
if (fileExists(guess)) break
|
|
1028
|
+
guess = undefined;
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
if (guess) {
|
|
1032
|
+
emitLegacyIndexDeprecation(
|
|
1033
|
+
guess,
|
|
1034
|
+
packageJsonUrl,
|
|
1035
|
+
base,
|
|
1036
|
+
packageConfig.main
|
|
1037
|
+
);
|
|
1038
|
+
return guess
|
|
1039
|
+
}
|
|
1040
|
+
// Fallthrough.
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
const tries = ['./index.js', './index.json', './index.node'];
|
|
1044
|
+
let i = -1;
|
|
1045
|
+
|
|
1046
|
+
while (++i < tries.length) {
|
|
1047
|
+
guess = new URL$1(tries[i], packageJsonUrl);
|
|
1048
|
+
if (fileExists(guess)) break
|
|
1049
|
+
guess = undefined;
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
if (guess) {
|
|
1053
|
+
emitLegacyIndexDeprecation(guess, packageJsonUrl, base, packageConfig.main);
|
|
1054
|
+
return guess
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
// Not found.
|
|
1058
|
+
throw new ERR_MODULE_NOT_FOUND(
|
|
1059
|
+
fileURLToPath$1(new URL$1('.', packageJsonUrl)),
|
|
1060
|
+
fileURLToPath$1(base)
|
|
1061
|
+
)
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
/**
|
|
1065
|
+
* @param {URL} resolved
|
|
1066
|
+
* @param {URL} base
|
|
1067
|
+
* @param {boolean} [preserveSymlinks]
|
|
1068
|
+
* @returns {URL}
|
|
1069
|
+
*/
|
|
1070
|
+
function finalizeResolution(resolved, base, preserveSymlinks) {
|
|
1071
|
+
if (encodedSeparatorRegEx.exec(resolved.pathname) !== null) {
|
|
1072
|
+
throw new ERR_INVALID_MODULE_SPECIFIER(
|
|
1073
|
+
resolved.pathname,
|
|
1074
|
+
'must not include encoded "/" or "\\" characters',
|
|
1075
|
+
fileURLToPath$1(base)
|
|
1076
|
+
)
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
/** @type {string} */
|
|
1080
|
+
let filePath;
|
|
1081
|
+
|
|
1082
|
+
try {
|
|
1083
|
+
filePath = fileURLToPath$1(resolved);
|
|
1084
|
+
} catch (error) {
|
|
1085
|
+
const cause = /** @type {ErrnoException} */ (error);
|
|
1086
|
+
Object.defineProperty(cause, 'input', {value: String(resolved)});
|
|
1087
|
+
Object.defineProperty(cause, 'module', {value: String(base)});
|
|
1088
|
+
throw cause
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
const stats = tryStatSync(
|
|
1092
|
+
filePath.endsWith('/') ? filePath.slice(-1) : filePath
|
|
1093
|
+
);
|
|
1094
|
+
|
|
1095
|
+
if (stats && stats.isDirectory()) {
|
|
1096
|
+
const error = new ERR_UNSUPPORTED_DIR_IMPORT(filePath, fileURLToPath$1(base));
|
|
1097
|
+
// @ts-expect-error Add this for `import.meta.resolve`.
|
|
1098
|
+
error.url = String(resolved);
|
|
1099
|
+
throw error
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
if (!stats || !stats.isFile()) {
|
|
1103
|
+
const error = new ERR_MODULE_NOT_FOUND(
|
|
1104
|
+
filePath || resolved.pathname,
|
|
1105
|
+
base && fileURLToPath$1(base),
|
|
1106
|
+
true
|
|
1107
|
+
);
|
|
1108
|
+
// @ts-expect-error Add this for `import.meta.resolve`.
|
|
1109
|
+
error.url = String(resolved);
|
|
1110
|
+
throw error
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
{
|
|
1114
|
+
const real = realpathSync(filePath);
|
|
1115
|
+
const {search, hash} = resolved;
|
|
1116
|
+
resolved = pathToFileURL$1(real + (filePath.endsWith(path.sep) ? '/' : ''));
|
|
1117
|
+
resolved.search = search;
|
|
1118
|
+
resolved.hash = hash;
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
return resolved
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
/**
|
|
1125
|
+
* @param {string} specifier
|
|
1126
|
+
* @param {URL | undefined} packageJsonUrl
|
|
1127
|
+
* @param {URL} base
|
|
1128
|
+
* @returns {Error}
|
|
1129
|
+
*/
|
|
1130
|
+
function importNotDefined(specifier, packageJsonUrl, base) {
|
|
1131
|
+
return new ERR_PACKAGE_IMPORT_NOT_DEFINED(
|
|
1132
|
+
specifier,
|
|
1133
|
+
packageJsonUrl && fileURLToPath$1(new URL$1('.', packageJsonUrl)),
|
|
1134
|
+
fileURLToPath$1(base)
|
|
1135
|
+
)
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
/**
|
|
1139
|
+
* @param {string} subpath
|
|
1140
|
+
* @param {URL} packageJsonUrl
|
|
1141
|
+
* @param {URL} base
|
|
1142
|
+
* @returns {Error}
|
|
1143
|
+
*/
|
|
1144
|
+
function exportsNotFound(subpath, packageJsonUrl, base) {
|
|
1145
|
+
return new ERR_PACKAGE_PATH_NOT_EXPORTED(
|
|
1146
|
+
fileURLToPath$1(new URL$1('.', packageJsonUrl)),
|
|
1147
|
+
subpath,
|
|
1148
|
+
base && fileURLToPath$1(base)
|
|
1149
|
+
)
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
/**
|
|
1153
|
+
* @param {string} request
|
|
1154
|
+
* @param {string} match
|
|
1155
|
+
* @param {URL} packageJsonUrl
|
|
1156
|
+
* @param {boolean} internal
|
|
1157
|
+
* @param {URL} [base]
|
|
1158
|
+
* @returns {never}
|
|
1159
|
+
*/
|
|
1160
|
+
function throwInvalidSubpath(request, match, packageJsonUrl, internal, base) {
|
|
1161
|
+
const reason = `request is not a valid match in pattern "${match}" for the "${
|
|
1162
|
+
internal ? 'imports' : 'exports'
|
|
1163
|
+
}" resolution of ${fileURLToPath$1(packageJsonUrl)}`;
|
|
1164
|
+
throw new ERR_INVALID_MODULE_SPECIFIER(
|
|
1165
|
+
request,
|
|
1166
|
+
reason,
|
|
1167
|
+
base && fileURLToPath$1(base)
|
|
1168
|
+
)
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
/**
|
|
1172
|
+
* @param {string} subpath
|
|
1173
|
+
* @param {unknown} target
|
|
1174
|
+
* @param {URL} packageJsonUrl
|
|
1175
|
+
* @param {boolean} internal
|
|
1176
|
+
* @param {URL} [base]
|
|
1177
|
+
* @returns {Error}
|
|
1178
|
+
*/
|
|
1179
|
+
function invalidPackageTarget(subpath, target, packageJsonUrl, internal, base) {
|
|
1180
|
+
target =
|
|
1181
|
+
typeof target === 'object' && target !== null
|
|
1182
|
+
? JSON.stringify(target, null, '')
|
|
1183
|
+
: `${target}`;
|
|
1184
|
+
|
|
1185
|
+
return new ERR_INVALID_PACKAGE_TARGET(
|
|
1186
|
+
fileURLToPath$1(new URL$1('.', packageJsonUrl)),
|
|
1187
|
+
subpath,
|
|
1188
|
+
target,
|
|
1189
|
+
internal,
|
|
1190
|
+
base && fileURLToPath$1(base)
|
|
1191
|
+
)
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/**
|
|
1195
|
+
* @param {string} target
|
|
1196
|
+
* @param {string} subpath
|
|
1197
|
+
* @param {string} match
|
|
1198
|
+
* @param {URL} packageJsonUrl
|
|
1199
|
+
* @param {URL} base
|
|
1200
|
+
* @param {boolean} pattern
|
|
1201
|
+
* @param {boolean} internal
|
|
1202
|
+
* @param {boolean} isPathMap
|
|
1203
|
+
* @param {Set<string> | undefined} conditions
|
|
1204
|
+
* @returns {URL}
|
|
1205
|
+
*/
|
|
1206
|
+
function resolvePackageTargetString(
|
|
1207
|
+
target,
|
|
1208
|
+
subpath,
|
|
1209
|
+
match,
|
|
1210
|
+
packageJsonUrl,
|
|
1211
|
+
base,
|
|
1212
|
+
pattern,
|
|
1213
|
+
internal,
|
|
1214
|
+
isPathMap,
|
|
1215
|
+
conditions
|
|
1216
|
+
) {
|
|
1217
|
+
if (subpath !== '' && !pattern && target[target.length - 1] !== '/')
|
|
1218
|
+
throw invalidPackageTarget(match, target, packageJsonUrl, internal, base)
|
|
1219
|
+
|
|
1220
|
+
if (!target.startsWith('./')) {
|
|
1221
|
+
if (internal && !target.startsWith('../') && !target.startsWith('/')) {
|
|
1222
|
+
let isURL = false;
|
|
1223
|
+
|
|
1224
|
+
try {
|
|
1225
|
+
new URL$1(target);
|
|
1226
|
+
isURL = true;
|
|
1227
|
+
} catch {
|
|
1228
|
+
// Continue regardless of error.
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
if (!isURL) {
|
|
1232
|
+
const exportTarget = pattern
|
|
1233
|
+
? RegExpPrototypeSymbolReplace.call(
|
|
1234
|
+
patternRegEx,
|
|
1235
|
+
target,
|
|
1236
|
+
() => subpath
|
|
1237
|
+
)
|
|
1238
|
+
: target + subpath;
|
|
1239
|
+
|
|
1240
|
+
return packageResolve(exportTarget, packageJsonUrl, conditions)
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
throw invalidPackageTarget(match, target, packageJsonUrl, internal, base)
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
if (invalidSegmentRegEx.exec(target.slice(2)) !== null) {
|
|
1248
|
+
if (deprecatedInvalidSegmentRegEx.exec(target.slice(2)) === null) {
|
|
1249
|
+
if (!isPathMap) {
|
|
1250
|
+
const request = pattern
|
|
1251
|
+
? match.replace('*', () => subpath)
|
|
1252
|
+
: match + subpath;
|
|
1253
|
+
const resolvedTarget = pattern
|
|
1254
|
+
? RegExpPrototypeSymbolReplace.call(
|
|
1255
|
+
patternRegEx,
|
|
1256
|
+
target,
|
|
1257
|
+
() => subpath
|
|
1258
|
+
)
|
|
1259
|
+
: target;
|
|
1260
|
+
emitInvalidSegmentDeprecation(
|
|
1261
|
+
resolvedTarget,
|
|
1262
|
+
request,
|
|
1263
|
+
match,
|
|
1264
|
+
packageJsonUrl,
|
|
1265
|
+
internal,
|
|
1266
|
+
base,
|
|
1267
|
+
true
|
|
1268
|
+
);
|
|
1269
|
+
}
|
|
1270
|
+
} else {
|
|
1271
|
+
throw invalidPackageTarget(match, target, packageJsonUrl, internal, base)
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
const resolved = new URL$1(target, packageJsonUrl);
|
|
1276
|
+
const resolvedPath = resolved.pathname;
|
|
1277
|
+
const packagePath = new URL$1('.', packageJsonUrl).pathname;
|
|
1278
|
+
|
|
1279
|
+
if (!resolvedPath.startsWith(packagePath))
|
|
1280
|
+
throw invalidPackageTarget(match, target, packageJsonUrl, internal, base)
|
|
1281
|
+
|
|
1282
|
+
if (subpath === '') return resolved
|
|
1283
|
+
|
|
1284
|
+
if (invalidSegmentRegEx.exec(subpath) !== null) {
|
|
1285
|
+
const request = pattern
|
|
1286
|
+
? match.replace('*', () => subpath)
|
|
1287
|
+
: match + subpath;
|
|
1288
|
+
if (deprecatedInvalidSegmentRegEx.exec(subpath) === null) {
|
|
1289
|
+
if (!isPathMap) {
|
|
1290
|
+
const resolvedTarget = pattern
|
|
1291
|
+
? RegExpPrototypeSymbolReplace.call(
|
|
1292
|
+
patternRegEx,
|
|
1293
|
+
target,
|
|
1294
|
+
() => subpath
|
|
1295
|
+
)
|
|
1296
|
+
: target;
|
|
1297
|
+
emitInvalidSegmentDeprecation(
|
|
1298
|
+
resolvedTarget,
|
|
1299
|
+
request,
|
|
1300
|
+
match,
|
|
1301
|
+
packageJsonUrl,
|
|
1302
|
+
internal,
|
|
1303
|
+
base,
|
|
1304
|
+
false
|
|
1305
|
+
);
|
|
1306
|
+
}
|
|
1307
|
+
} else {
|
|
1308
|
+
throwInvalidSubpath(request, match, packageJsonUrl, internal, base);
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
if (pattern) {
|
|
1313
|
+
return new URL$1(
|
|
1314
|
+
RegExpPrototypeSymbolReplace.call(
|
|
1315
|
+
patternRegEx,
|
|
1316
|
+
resolved.href,
|
|
1317
|
+
() => subpath
|
|
1318
|
+
)
|
|
1319
|
+
)
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
return new URL$1(subpath, resolved)
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
/**
|
|
1326
|
+
* @param {string} key
|
|
1327
|
+
* @returns {boolean}
|
|
1328
|
+
*/
|
|
1329
|
+
function isArrayIndex(key) {
|
|
1330
|
+
const keyNumber = Number(key);
|
|
1331
|
+
if (`${keyNumber}` !== key) return false
|
|
1332
|
+
return keyNumber >= 0 && keyNumber < 0xff_ff_ff_ff
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
/**
|
|
1336
|
+
* @param {URL} packageJsonUrl
|
|
1337
|
+
* @param {unknown} target
|
|
1338
|
+
* @param {string} subpath
|
|
1339
|
+
* @param {string} packageSubpath
|
|
1340
|
+
* @param {URL} base
|
|
1341
|
+
* @param {boolean} pattern
|
|
1342
|
+
* @param {boolean} internal
|
|
1343
|
+
* @param {boolean} isPathMap
|
|
1344
|
+
* @param {Set<string> | undefined} conditions
|
|
1345
|
+
* @returns {URL | null}
|
|
1346
|
+
*/
|
|
1347
|
+
function resolvePackageTarget(
|
|
1348
|
+
packageJsonUrl,
|
|
1349
|
+
target,
|
|
1350
|
+
subpath,
|
|
1351
|
+
packageSubpath,
|
|
1352
|
+
base,
|
|
1353
|
+
pattern,
|
|
1354
|
+
internal,
|
|
1355
|
+
isPathMap,
|
|
1356
|
+
conditions
|
|
1357
|
+
) {
|
|
1358
|
+
if (typeof target === 'string') {
|
|
1359
|
+
return resolvePackageTargetString(
|
|
1360
|
+
target,
|
|
1361
|
+
subpath,
|
|
1362
|
+
packageSubpath,
|
|
1363
|
+
packageJsonUrl,
|
|
1364
|
+
base,
|
|
1365
|
+
pattern,
|
|
1366
|
+
internal,
|
|
1367
|
+
isPathMap,
|
|
1368
|
+
conditions
|
|
1369
|
+
)
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
if (Array.isArray(target)) {
|
|
1373
|
+
/** @type {Array<unknown>} */
|
|
1374
|
+
const targetList = target;
|
|
1375
|
+
if (targetList.length === 0) return null
|
|
1376
|
+
|
|
1377
|
+
/** @type {ErrnoException | null | undefined} */
|
|
1378
|
+
let lastException;
|
|
1379
|
+
let i = -1;
|
|
1380
|
+
|
|
1381
|
+
while (++i < targetList.length) {
|
|
1382
|
+
const targetItem = targetList[i];
|
|
1383
|
+
/** @type {URL | null} */
|
|
1384
|
+
let resolveResult;
|
|
1385
|
+
try {
|
|
1386
|
+
resolveResult = resolvePackageTarget(
|
|
1387
|
+
packageJsonUrl,
|
|
1388
|
+
targetItem,
|
|
1389
|
+
subpath,
|
|
1390
|
+
packageSubpath,
|
|
1391
|
+
base,
|
|
1392
|
+
pattern,
|
|
1393
|
+
internal,
|
|
1394
|
+
isPathMap,
|
|
1395
|
+
conditions
|
|
1396
|
+
);
|
|
1397
|
+
} catch (error) {
|
|
1398
|
+
const exception = /** @type {ErrnoException} */ (error);
|
|
1399
|
+
lastException = exception;
|
|
1400
|
+
if (exception.code === 'ERR_INVALID_PACKAGE_TARGET') continue
|
|
1401
|
+
throw error
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
if (resolveResult === undefined) continue
|
|
1405
|
+
|
|
1406
|
+
if (resolveResult === null) {
|
|
1407
|
+
lastException = null;
|
|
1408
|
+
continue
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
return resolveResult
|
|
1412
|
+
}
|
|
1413
|
+
|
|
1414
|
+
if (lastException === undefined || lastException === null) {
|
|
1415
|
+
return null
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
throw lastException
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
if (typeof target === 'object' && target !== null) {
|
|
1422
|
+
const keys = Object.getOwnPropertyNames(target);
|
|
1423
|
+
let i = -1;
|
|
1424
|
+
|
|
1425
|
+
while (++i < keys.length) {
|
|
1426
|
+
const key = keys[i];
|
|
1427
|
+
if (isArrayIndex(key)) {
|
|
1428
|
+
throw new ERR_INVALID_PACKAGE_CONFIG(
|
|
1429
|
+
fileURLToPath$1(packageJsonUrl),
|
|
1430
|
+
base,
|
|
1431
|
+
'"exports" cannot contain numeric property keys.'
|
|
1432
|
+
)
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
i = -1;
|
|
1437
|
+
|
|
1438
|
+
while (++i < keys.length) {
|
|
1439
|
+
const key = keys[i];
|
|
1440
|
+
if (key === 'default' || (conditions && conditions.has(key))) {
|
|
1441
|
+
// @ts-expect-error: indexable.
|
|
1442
|
+
const conditionalTarget = /** @type {unknown} */ (target[key]);
|
|
1443
|
+
const resolveResult = resolvePackageTarget(
|
|
1444
|
+
packageJsonUrl,
|
|
1445
|
+
conditionalTarget,
|
|
1446
|
+
subpath,
|
|
1447
|
+
packageSubpath,
|
|
1448
|
+
base,
|
|
1449
|
+
pattern,
|
|
1450
|
+
internal,
|
|
1451
|
+
isPathMap,
|
|
1452
|
+
conditions
|
|
1453
|
+
);
|
|
1454
|
+
if (resolveResult === undefined) continue
|
|
1455
|
+
return resolveResult
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
|
|
1459
|
+
return null
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
if (target === null) {
|
|
1463
|
+
return null
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
throw invalidPackageTarget(
|
|
1467
|
+
packageSubpath,
|
|
1468
|
+
target,
|
|
1469
|
+
packageJsonUrl,
|
|
1470
|
+
internal,
|
|
1471
|
+
base
|
|
1472
|
+
)
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
/**
|
|
1476
|
+
* @param {unknown} exports
|
|
1477
|
+
* @param {URL} packageJsonUrl
|
|
1478
|
+
* @param {URL} base
|
|
1479
|
+
* @returns {boolean}
|
|
1480
|
+
*/
|
|
1481
|
+
function isConditionalExportsMainSugar(exports, packageJsonUrl, base) {
|
|
1482
|
+
if (typeof exports === 'string' || Array.isArray(exports)) return true
|
|
1483
|
+
if (typeof exports !== 'object' || exports === null) return false
|
|
1484
|
+
|
|
1485
|
+
const keys = Object.getOwnPropertyNames(exports);
|
|
1486
|
+
let isConditionalSugar = false;
|
|
1487
|
+
let i = 0;
|
|
1488
|
+
let keyIndex = -1;
|
|
1489
|
+
while (++keyIndex < keys.length) {
|
|
1490
|
+
const key = keys[keyIndex];
|
|
1491
|
+
const currentIsConditionalSugar = key === '' || key[0] !== '.';
|
|
1492
|
+
if (i++ === 0) {
|
|
1493
|
+
isConditionalSugar = currentIsConditionalSugar;
|
|
1494
|
+
} else if (isConditionalSugar !== currentIsConditionalSugar) {
|
|
1495
|
+
throw new ERR_INVALID_PACKAGE_CONFIG(
|
|
1496
|
+
fileURLToPath$1(packageJsonUrl),
|
|
1497
|
+
base,
|
|
1498
|
+
'"exports" cannot contain some keys starting with \'.\' and some not.' +
|
|
1499
|
+
' The exports object must either be an object of package subpath keys' +
|
|
1500
|
+
' or an object of main entry condition name keys only.'
|
|
1501
|
+
)
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
return isConditionalSugar
|
|
1506
|
+
}
|
|
1507
|
+
|
|
1508
|
+
/**
|
|
1509
|
+
* @param {string} match
|
|
1510
|
+
* @param {URL} pjsonUrl
|
|
1511
|
+
* @param {URL} base
|
|
1512
|
+
*/
|
|
1513
|
+
function emitTrailingSlashPatternDeprecation(match, pjsonUrl, base) {
|
|
1514
|
+
// @ts-expect-error: apparently it does exist, TS.
|
|
1515
|
+
if (process$1.noDeprecation) {
|
|
1516
|
+
return
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
const pjsonPath = fileURLToPath$1(pjsonUrl);
|
|
1520
|
+
if (emittedPackageWarnings.has(pjsonPath + '|' + match)) return
|
|
1521
|
+
emittedPackageWarnings.add(pjsonPath + '|' + match);
|
|
1522
|
+
process$1.emitWarning(
|
|
1523
|
+
`Use of deprecated trailing slash pattern mapping "${match}" in the ` +
|
|
1524
|
+
`"exports" field module resolution of the package at ${pjsonPath}${
|
|
1525
|
+
base ? ` imported from ${fileURLToPath$1(base)}` : ''
|
|
1526
|
+
}. Mapping specifiers ending in "/" is no longer supported.`,
|
|
1527
|
+
'DeprecationWarning',
|
|
1528
|
+
'DEP0155'
|
|
1529
|
+
);
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
/**
|
|
1533
|
+
* @param {URL} packageJsonUrl
|
|
1534
|
+
* @param {string} packageSubpath
|
|
1535
|
+
* @param {Record<string, unknown>} packageConfig
|
|
1536
|
+
* @param {URL} base
|
|
1537
|
+
* @param {Set<string> | undefined} conditions
|
|
1538
|
+
* @returns {URL}
|
|
1539
|
+
*/
|
|
1540
|
+
function packageExportsResolve(
|
|
1541
|
+
packageJsonUrl,
|
|
1542
|
+
packageSubpath,
|
|
1543
|
+
packageConfig,
|
|
1544
|
+
base,
|
|
1545
|
+
conditions
|
|
1546
|
+
) {
|
|
1547
|
+
let exports = packageConfig.exports;
|
|
1548
|
+
|
|
1549
|
+
if (isConditionalExportsMainSugar(exports, packageJsonUrl, base)) {
|
|
1550
|
+
exports = {'.': exports};
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
if (
|
|
1554
|
+
own.call(exports, packageSubpath) &&
|
|
1555
|
+
!packageSubpath.includes('*') &&
|
|
1556
|
+
!packageSubpath.endsWith('/')
|
|
1557
|
+
) {
|
|
1558
|
+
// @ts-expect-error: indexable.
|
|
1559
|
+
const target = exports[packageSubpath];
|
|
1560
|
+
const resolveResult = resolvePackageTarget(
|
|
1561
|
+
packageJsonUrl,
|
|
1562
|
+
target,
|
|
1563
|
+
'',
|
|
1564
|
+
packageSubpath,
|
|
1565
|
+
base,
|
|
1566
|
+
false,
|
|
1567
|
+
false,
|
|
1568
|
+
false,
|
|
1569
|
+
conditions
|
|
1570
|
+
);
|
|
1571
|
+
if (resolveResult === null || resolveResult === undefined) {
|
|
1572
|
+
throw exportsNotFound(packageSubpath, packageJsonUrl, base)
|
|
1573
|
+
}
|
|
1574
|
+
|
|
1575
|
+
return resolveResult
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
let bestMatch = '';
|
|
1579
|
+
let bestMatchSubpath = '';
|
|
1580
|
+
const keys = Object.getOwnPropertyNames(exports);
|
|
1581
|
+
let i = -1;
|
|
1582
|
+
|
|
1583
|
+
while (++i < keys.length) {
|
|
1584
|
+
const key = keys[i];
|
|
1585
|
+
const patternIndex = key.indexOf('*');
|
|
1586
|
+
|
|
1587
|
+
if (
|
|
1588
|
+
patternIndex !== -1 &&
|
|
1589
|
+
packageSubpath.startsWith(key.slice(0, patternIndex))
|
|
1590
|
+
) {
|
|
1591
|
+
// When this reaches EOL, this can throw at the top of the whole function:
|
|
1592
|
+
//
|
|
1593
|
+
// if (StringPrototypeEndsWith(packageSubpath, '/'))
|
|
1594
|
+
// throwInvalidSubpath(packageSubpath)
|
|
1595
|
+
//
|
|
1596
|
+
// To match "imports" and the spec.
|
|
1597
|
+
if (packageSubpath.endsWith('/')) {
|
|
1598
|
+
emitTrailingSlashPatternDeprecation(
|
|
1599
|
+
packageSubpath,
|
|
1600
|
+
packageJsonUrl,
|
|
1601
|
+
base
|
|
1602
|
+
);
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
const patternTrailer = key.slice(patternIndex + 1);
|
|
1606
|
+
|
|
1607
|
+
if (
|
|
1608
|
+
packageSubpath.length >= key.length &&
|
|
1609
|
+
packageSubpath.endsWith(patternTrailer) &&
|
|
1610
|
+
patternKeyCompare(bestMatch, key) === 1 &&
|
|
1611
|
+
key.lastIndexOf('*') === patternIndex
|
|
1612
|
+
) {
|
|
1613
|
+
bestMatch = key;
|
|
1614
|
+
bestMatchSubpath = packageSubpath.slice(
|
|
1615
|
+
patternIndex,
|
|
1616
|
+
packageSubpath.length - patternTrailer.length
|
|
1617
|
+
);
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
if (bestMatch) {
|
|
1623
|
+
// @ts-expect-error: indexable.
|
|
1624
|
+
const target = /** @type {unknown} */ (exports[bestMatch]);
|
|
1625
|
+
const resolveResult = resolvePackageTarget(
|
|
1626
|
+
packageJsonUrl,
|
|
1627
|
+
target,
|
|
1628
|
+
bestMatchSubpath,
|
|
1629
|
+
bestMatch,
|
|
1630
|
+
base,
|
|
1631
|
+
true,
|
|
1632
|
+
false,
|
|
1633
|
+
packageSubpath.endsWith('/'),
|
|
1634
|
+
conditions
|
|
1635
|
+
);
|
|
1636
|
+
|
|
1637
|
+
if (resolveResult === null || resolveResult === undefined) {
|
|
1638
|
+
throw exportsNotFound(packageSubpath, packageJsonUrl, base)
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
return resolveResult
|
|
1642
|
+
}
|
|
1643
|
+
|
|
1644
|
+
throw exportsNotFound(packageSubpath, packageJsonUrl, base)
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
/**
|
|
1648
|
+
* @param {string} a
|
|
1649
|
+
* @param {string} b
|
|
1650
|
+
*/
|
|
1651
|
+
function patternKeyCompare(a, b) {
|
|
1652
|
+
const aPatternIndex = a.indexOf('*');
|
|
1653
|
+
const bPatternIndex = b.indexOf('*');
|
|
1654
|
+
const baseLengthA = aPatternIndex === -1 ? a.length : aPatternIndex + 1;
|
|
1655
|
+
const baseLengthB = bPatternIndex === -1 ? b.length : bPatternIndex + 1;
|
|
1656
|
+
if (baseLengthA > baseLengthB) return -1
|
|
1657
|
+
if (baseLengthB > baseLengthA) return 1
|
|
1658
|
+
if (aPatternIndex === -1) return 1
|
|
1659
|
+
if (bPatternIndex === -1) return -1
|
|
1660
|
+
if (a.length > b.length) return -1
|
|
1661
|
+
if (b.length > a.length) return 1
|
|
1662
|
+
return 0
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
/**
|
|
1666
|
+
* @param {string} name
|
|
1667
|
+
* @param {URL} base
|
|
1668
|
+
* @param {Set<string>} [conditions]
|
|
1669
|
+
* @returns {URL}
|
|
1670
|
+
*/
|
|
1671
|
+
function packageImportsResolve(name, base, conditions) {
|
|
1672
|
+
if (name === '#' || name.startsWith('#/') || name.endsWith('/')) {
|
|
1673
|
+
const reason = 'is not a valid internal imports specifier name';
|
|
1674
|
+
throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath$1(base))
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
/** @type {URL | undefined} */
|
|
1678
|
+
let packageJsonUrl;
|
|
1679
|
+
|
|
1680
|
+
const packageConfig = getPackageScopeConfig(base);
|
|
1681
|
+
|
|
1682
|
+
if (packageConfig.exists) {
|
|
1683
|
+
packageJsonUrl = pathToFileURL$1(packageConfig.pjsonPath);
|
|
1684
|
+
const imports = packageConfig.imports;
|
|
1685
|
+
if (imports) {
|
|
1686
|
+
if (own.call(imports, name) && !name.includes('*')) {
|
|
1687
|
+
const resolveResult = resolvePackageTarget(
|
|
1688
|
+
packageJsonUrl,
|
|
1689
|
+
imports[name],
|
|
1690
|
+
'',
|
|
1691
|
+
name,
|
|
1692
|
+
base,
|
|
1693
|
+
false,
|
|
1694
|
+
true,
|
|
1695
|
+
false,
|
|
1696
|
+
conditions
|
|
1697
|
+
);
|
|
1698
|
+
if (resolveResult !== null && resolveResult !== undefined) {
|
|
1699
|
+
return resolveResult
|
|
1700
|
+
}
|
|
1701
|
+
} else {
|
|
1702
|
+
let bestMatch = '';
|
|
1703
|
+
let bestMatchSubpath = '';
|
|
1704
|
+
const keys = Object.getOwnPropertyNames(imports);
|
|
1705
|
+
let i = -1;
|
|
1706
|
+
|
|
1707
|
+
while (++i < keys.length) {
|
|
1708
|
+
const key = keys[i];
|
|
1709
|
+
const patternIndex = key.indexOf('*');
|
|
1710
|
+
|
|
1711
|
+
if (patternIndex !== -1 && name.startsWith(key.slice(0, -1))) {
|
|
1712
|
+
const patternTrailer = key.slice(patternIndex + 1);
|
|
1713
|
+
if (
|
|
1714
|
+
name.length >= key.length &&
|
|
1715
|
+
name.endsWith(patternTrailer) &&
|
|
1716
|
+
patternKeyCompare(bestMatch, key) === 1 &&
|
|
1717
|
+
key.lastIndexOf('*') === patternIndex
|
|
1718
|
+
) {
|
|
1719
|
+
bestMatch = key;
|
|
1720
|
+
bestMatchSubpath = name.slice(
|
|
1721
|
+
patternIndex,
|
|
1722
|
+
name.length - patternTrailer.length
|
|
1723
|
+
);
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1728
|
+
if (bestMatch) {
|
|
1729
|
+
const target = imports[bestMatch];
|
|
1730
|
+
const resolveResult = resolvePackageTarget(
|
|
1731
|
+
packageJsonUrl,
|
|
1732
|
+
target,
|
|
1733
|
+
bestMatchSubpath,
|
|
1734
|
+
bestMatch,
|
|
1735
|
+
base,
|
|
1736
|
+
true,
|
|
1737
|
+
true,
|
|
1738
|
+
false,
|
|
1739
|
+
conditions
|
|
1740
|
+
);
|
|
1741
|
+
|
|
1742
|
+
if (resolveResult !== null && resolveResult !== undefined) {
|
|
1743
|
+
return resolveResult
|
|
1744
|
+
}
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
throw importNotDefined(name, packageJsonUrl, base)
|
|
1751
|
+
}
|
|
1752
|
+
|
|
1753
|
+
/**
|
|
1754
|
+
* @param {string} specifier
|
|
1755
|
+
* @param {URL} base
|
|
1756
|
+
*/
|
|
1757
|
+
function parsePackageName(specifier, base) {
|
|
1758
|
+
let separatorIndex = specifier.indexOf('/');
|
|
1759
|
+
let validPackageName = true;
|
|
1760
|
+
let isScoped = false;
|
|
1761
|
+
if (specifier[0] === '@') {
|
|
1762
|
+
isScoped = true;
|
|
1763
|
+
if (separatorIndex === -1 || specifier.length === 0) {
|
|
1764
|
+
validPackageName = false;
|
|
1765
|
+
} else {
|
|
1766
|
+
separatorIndex = specifier.indexOf('/', separatorIndex + 1);
|
|
1767
|
+
}
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
const packageName =
|
|
1771
|
+
separatorIndex === -1 ? specifier : specifier.slice(0, separatorIndex);
|
|
1772
|
+
|
|
1773
|
+
// Package name cannot have leading . and cannot have percent-encoding or
|
|
1774
|
+
// \\ separators.
|
|
1775
|
+
if (invalidPackageNameRegEx.exec(packageName) !== null) {
|
|
1776
|
+
validPackageName = false;
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1779
|
+
if (!validPackageName) {
|
|
1780
|
+
throw new ERR_INVALID_MODULE_SPECIFIER(
|
|
1781
|
+
specifier,
|
|
1782
|
+
'is not a valid package name',
|
|
1783
|
+
fileURLToPath$1(base)
|
|
1784
|
+
)
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
const packageSubpath =
|
|
1788
|
+
'.' + (separatorIndex === -1 ? '' : specifier.slice(separatorIndex));
|
|
1789
|
+
|
|
1790
|
+
return {packageName, packageSubpath, isScoped}
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
/**
|
|
1794
|
+
* @param {string} specifier
|
|
1795
|
+
* @param {URL} base
|
|
1796
|
+
* @param {Set<string> | undefined} conditions
|
|
1797
|
+
* @returns {URL}
|
|
1798
|
+
*/
|
|
1799
|
+
function packageResolve(specifier, base, conditions) {
|
|
1800
|
+
if (builtinModules.includes(specifier)) {
|
|
1801
|
+
return new URL$1('node:' + specifier)
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1804
|
+
const {packageName, packageSubpath, isScoped} = parsePackageName(
|
|
1805
|
+
specifier,
|
|
1806
|
+
base
|
|
1807
|
+
);
|
|
1808
|
+
|
|
1809
|
+
// ResolveSelf
|
|
1810
|
+
const packageConfig = getPackageScopeConfig(base);
|
|
1811
|
+
|
|
1812
|
+
// Can’t test.
|
|
1813
|
+
/* c8 ignore next 16 */
|
|
1814
|
+
if (packageConfig.exists) {
|
|
1815
|
+
const packageJsonUrl = pathToFileURL$1(packageConfig.pjsonPath);
|
|
1816
|
+
if (
|
|
1817
|
+
packageConfig.name === packageName &&
|
|
1818
|
+
packageConfig.exports !== undefined &&
|
|
1819
|
+
packageConfig.exports !== null
|
|
1820
|
+
) {
|
|
1821
|
+
return packageExportsResolve(
|
|
1822
|
+
packageJsonUrl,
|
|
1823
|
+
packageSubpath,
|
|
1824
|
+
packageConfig,
|
|
1825
|
+
base,
|
|
1826
|
+
conditions
|
|
1827
|
+
)
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
|
|
1831
|
+
let packageJsonUrl = new URL$1(
|
|
1832
|
+
'./node_modules/' + packageName + '/package.json',
|
|
1833
|
+
base
|
|
1834
|
+
);
|
|
1835
|
+
let packageJsonPath = fileURLToPath$1(packageJsonUrl);
|
|
1836
|
+
/** @type {string} */
|
|
1837
|
+
let lastPath;
|
|
1838
|
+
do {
|
|
1839
|
+
const stat = tryStatSync(packageJsonPath.slice(0, -13));
|
|
1840
|
+
if (!stat || !stat.isDirectory()) {
|
|
1841
|
+
lastPath = packageJsonPath;
|
|
1842
|
+
packageJsonUrl = new URL$1(
|
|
1843
|
+
(isScoped ? '../../../../node_modules/' : '../../../node_modules/') +
|
|
1844
|
+
packageName +
|
|
1845
|
+
'/package.json',
|
|
1846
|
+
packageJsonUrl
|
|
1847
|
+
);
|
|
1848
|
+
packageJsonPath = fileURLToPath$1(packageJsonUrl);
|
|
1849
|
+
continue
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1852
|
+
// Package match.
|
|
1853
|
+
const packageConfig = read(packageJsonPath, {base, specifier});
|
|
1854
|
+
if (packageConfig.exports !== undefined && packageConfig.exports !== null) {
|
|
1855
|
+
return packageExportsResolve(
|
|
1856
|
+
packageJsonUrl,
|
|
1857
|
+
packageSubpath,
|
|
1858
|
+
packageConfig,
|
|
1859
|
+
base,
|
|
1860
|
+
conditions
|
|
1861
|
+
)
|
|
1862
|
+
}
|
|
1863
|
+
|
|
1864
|
+
if (packageSubpath === '.') {
|
|
1865
|
+
return legacyMainResolve(packageJsonUrl, packageConfig, base)
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
return new URL$1(packageSubpath, packageJsonUrl)
|
|
1869
|
+
// Cross-platform root check.
|
|
1870
|
+
} while (packageJsonPath.length !== lastPath.length)
|
|
1871
|
+
|
|
1872
|
+
throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath$1(base), false)
|
|
1873
|
+
}
|
|
1874
|
+
|
|
1875
|
+
/**
|
|
1876
|
+
* @param {string} specifier
|
|
1877
|
+
* @returns {boolean}
|
|
1878
|
+
*/
|
|
1879
|
+
function isRelativeSpecifier(specifier) {
|
|
1880
|
+
if (specifier[0] === '.') {
|
|
1881
|
+
if (specifier.length === 1 || specifier[1] === '/') return true
|
|
1882
|
+
if (
|
|
1883
|
+
specifier[1] === '.' &&
|
|
1884
|
+
(specifier.length === 2 || specifier[2] === '/')
|
|
1885
|
+
) {
|
|
1886
|
+
return true
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
return false
|
|
1891
|
+
}
|
|
1892
|
+
|
|
1893
|
+
/**
|
|
1894
|
+
* @param {string} specifier
|
|
1895
|
+
* @returns {boolean}
|
|
1896
|
+
*/
|
|
1897
|
+
function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) {
|
|
1898
|
+
if (specifier === '') return false
|
|
1899
|
+
if (specifier[0] === '/') return true
|
|
1900
|
+
return isRelativeSpecifier(specifier)
|
|
1901
|
+
}
|
|
1902
|
+
|
|
1903
|
+
/**
|
|
1904
|
+
* The “Resolver Algorithm Specification” as detailed in the Node docs (which is
|
|
1905
|
+
* sync and slightly lower-level than `resolve`).
|
|
1906
|
+
*
|
|
1907
|
+
* @param {string} specifier
|
|
1908
|
+
* `/example.js`, `./example.js`, `../example.js`, `some-package`, `fs`, etc.
|
|
1909
|
+
* @param {URL} base
|
|
1910
|
+
* Full URL (to a file) that `specifier` is resolved relative from.
|
|
1911
|
+
* @param {Set<string>} [conditions]
|
|
1912
|
+
* Conditions.
|
|
1913
|
+
* @param {boolean} [preserveSymlinks]
|
|
1914
|
+
* Keep symlinks instead of resolving them.
|
|
1915
|
+
* @returns {URL}
|
|
1916
|
+
* A URL object to the found thing.
|
|
1917
|
+
*/
|
|
1918
|
+
function moduleResolve(specifier, base, conditions, preserveSymlinks) {
|
|
1919
|
+
// Note: The Node code supports `base` as a string (in this internal API) too,
|
|
1920
|
+
// we don’t.
|
|
1921
|
+
const protocol = base.protocol;
|
|
1922
|
+
const isData = protocol === 'data:';
|
|
1923
|
+
const isRemote = isData || protocol === 'http:' || protocol === 'https:';
|
|
1924
|
+
// Order swapped from spec for minor perf gain.
|
|
1925
|
+
// Ok since relative URLs cannot parse as URLs.
|
|
1926
|
+
/** @type {URL | undefined} */
|
|
1927
|
+
let resolved;
|
|
1928
|
+
|
|
1929
|
+
if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
|
|
1930
|
+
try {
|
|
1931
|
+
resolved = new URL$1(specifier, base);
|
|
1932
|
+
} catch (error_) {
|
|
1933
|
+
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
|
|
1934
|
+
error.cause = error_;
|
|
1935
|
+
throw error
|
|
1936
|
+
}
|
|
1937
|
+
} else if (protocol === 'file:' && specifier[0] === '#') {
|
|
1938
|
+
resolved = packageImportsResolve(specifier, base, conditions);
|
|
1939
|
+
} else {
|
|
1940
|
+
try {
|
|
1941
|
+
resolved = new URL$1(specifier);
|
|
1942
|
+
} catch (error_) {
|
|
1943
|
+
// Note: actual code uses `canBeRequiredWithoutScheme`.
|
|
1944
|
+
if (isRemote && !builtinModules.includes(specifier)) {
|
|
1945
|
+
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
|
|
1946
|
+
error.cause = error_;
|
|
1947
|
+
throw error
|
|
1948
|
+
}
|
|
1949
|
+
|
|
1950
|
+
resolved = packageResolve(specifier, base, conditions);
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1954
|
+
assert(resolved !== undefined, 'expected to be defined');
|
|
1955
|
+
|
|
1956
|
+
if (resolved.protocol !== 'file:') {
|
|
1957
|
+
return resolved
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
return finalizeResolution(resolved, base)
|
|
1961
|
+
}
|
|
1962
|
+
|
|
1963
|
+
function fileURLToPath(id) {
|
|
1964
|
+
if (typeof id === "string" && !id.startsWith("file://")) {
|
|
1965
|
+
return normalizeSlash(id);
|
|
1966
|
+
}
|
|
1967
|
+
return normalizeSlash(fileURLToPath$1(id));
|
|
1968
|
+
}
|
|
1969
|
+
function pathToFileURL(id) {
|
|
1970
|
+
return pathToFileURL$1(fileURLToPath(id)).toString();
|
|
1971
|
+
}
|
|
1972
|
+
const INVALID_CHAR_RE = /[\u0000-\u001F"#$&*+,/:;<=>?@[\]^`{|}\u007F]+/g;
|
|
1973
|
+
function sanitizeURIComponent(name = "", replacement = "_") {
|
|
1974
|
+
return name.replace(INVALID_CHAR_RE, replacement).replace(/%../g, replacement);
|
|
1975
|
+
}
|
|
1976
|
+
function sanitizeFilePath(filePath = "") {
|
|
1977
|
+
return filePath.replace(/\?.*$/, "").split(/[/\\]/g).map((p) => sanitizeURIComponent(p)).join("/").replace(/^([A-Za-z])_\//, "$1:/");
|
|
1978
|
+
}
|
|
1979
|
+
function normalizeid(id) {
|
|
1980
|
+
if (typeof id !== "string") {
|
|
1981
|
+
id = id.toString();
|
|
1982
|
+
}
|
|
1983
|
+
if (/(?:node|data|http|https|file):/.test(id)) {
|
|
1984
|
+
return id;
|
|
1985
|
+
}
|
|
1986
|
+
if (BUILTIN_MODULES.has(id)) {
|
|
1987
|
+
return "node:" + id;
|
|
1988
|
+
}
|
|
1989
|
+
return "file://" + encodeURI(normalizeSlash(id));
|
|
1990
|
+
}
|
|
1991
|
+
async function loadURL(url) {
|
|
1992
|
+
const code = await promises.readFile(fileURLToPath(url), "utf8");
|
|
1993
|
+
return code;
|
|
1994
|
+
}
|
|
1995
|
+
|
|
1996
|
+
const DEFAULT_CONDITIONS_SET = /* @__PURE__ */ new Set(["node", "import"]);
|
|
1997
|
+
const DEFAULT_EXTENSIONS = [".mjs", ".cjs", ".js", ".json"];
|
|
1998
|
+
const NOT_FOUND_ERRORS = /* @__PURE__ */ new Set([
|
|
1999
|
+
"ERR_MODULE_NOT_FOUND",
|
|
2000
|
+
"ERR_UNSUPPORTED_DIR_IMPORT",
|
|
2001
|
+
"MODULE_NOT_FOUND",
|
|
2002
|
+
"ERR_PACKAGE_PATH_NOT_EXPORTED"
|
|
2003
|
+
]);
|
|
2004
|
+
function _tryModuleResolve(id, url, conditions) {
|
|
2005
|
+
try {
|
|
2006
|
+
return moduleResolve(id, url, conditions);
|
|
2007
|
+
} catch (error) {
|
|
2008
|
+
if (!NOT_FOUND_ERRORS.has(error?.code)) {
|
|
2009
|
+
throw error;
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
}
|
|
2013
|
+
function _resolve(id, options = {}) {
|
|
2014
|
+
if (typeof id !== "string") {
|
|
2015
|
+
if (id instanceof URL) {
|
|
2016
|
+
id = fileURLToPath(id);
|
|
2017
|
+
} else {
|
|
2018
|
+
throw new TypeError("input must be a `string` or `URL`");
|
|
2019
|
+
}
|
|
2020
|
+
}
|
|
2021
|
+
if (/(?:node|data|http|https):/.test(id)) {
|
|
2022
|
+
return id;
|
|
2023
|
+
}
|
|
2024
|
+
if (BUILTIN_MODULES.has(id)) {
|
|
2025
|
+
return "node:" + id;
|
|
2026
|
+
}
|
|
2027
|
+
if (id.startsWith("file://")) {
|
|
2028
|
+
id = fileURLToPath(id);
|
|
2029
|
+
}
|
|
2030
|
+
if (isAbsolute(id)) {
|
|
2031
|
+
try {
|
|
2032
|
+
const stat = statSync(id);
|
|
2033
|
+
if (stat.isFile()) {
|
|
2034
|
+
return pathToFileURL(id);
|
|
2035
|
+
}
|
|
2036
|
+
} catch (error) {
|
|
2037
|
+
if (error?.code !== "ENOENT") {
|
|
2038
|
+
throw error;
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
}
|
|
2042
|
+
const conditionsSet = options.conditions ? new Set(options.conditions) : DEFAULT_CONDITIONS_SET;
|
|
2043
|
+
const _urls = (Array.isArray(options.url) ? options.url : [options.url]).filter(Boolean).map((url) => new URL(normalizeid(url.toString())));
|
|
2044
|
+
if (_urls.length === 0) {
|
|
2045
|
+
_urls.push(new URL(pathToFileURL(process.cwd())));
|
|
2046
|
+
}
|
|
2047
|
+
const urls = [..._urls];
|
|
2048
|
+
for (const url of _urls) {
|
|
2049
|
+
if (url.protocol === "file:") {
|
|
2050
|
+
urls.push(
|
|
2051
|
+
new URL("./", url),
|
|
2052
|
+
// If url is directory
|
|
2053
|
+
new URL(joinURL(url.pathname, "_index.js"), url),
|
|
2054
|
+
// TODO: Remove in next major version?
|
|
2055
|
+
new URL("node_modules", url)
|
|
2056
|
+
);
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
let resolved;
|
|
2060
|
+
for (const url of urls) {
|
|
2061
|
+
resolved = _tryModuleResolve(id, url, conditionsSet);
|
|
2062
|
+
if (resolved) {
|
|
2063
|
+
break;
|
|
2064
|
+
}
|
|
2065
|
+
for (const prefix of ["", "/index"]) {
|
|
2066
|
+
for (const extension of options.extensions || DEFAULT_EXTENSIONS) {
|
|
2067
|
+
resolved = _tryModuleResolve(
|
|
2068
|
+
joinURL(id, prefix) + extension,
|
|
2069
|
+
url,
|
|
2070
|
+
conditionsSet
|
|
2071
|
+
);
|
|
2072
|
+
if (resolved) {
|
|
2073
|
+
break;
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
if (resolved) {
|
|
2077
|
+
break;
|
|
2078
|
+
}
|
|
2079
|
+
}
|
|
2080
|
+
if (resolved) {
|
|
2081
|
+
break;
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
if (!resolved) {
|
|
2085
|
+
const error = new Error(
|
|
2086
|
+
`Cannot find module ${id} imported from ${urls.join(", ")}`
|
|
2087
|
+
);
|
|
2088
|
+
error.code = "ERR_MODULE_NOT_FOUND";
|
|
2089
|
+
throw error;
|
|
2090
|
+
}
|
|
2091
|
+
return pathToFileURL(resolved);
|
|
2092
|
+
}
|
|
2093
|
+
function resolveSync(id, options) {
|
|
2094
|
+
return _resolve(id, options);
|
|
2095
|
+
}
|
|
2096
|
+
function resolve(id, options) {
|
|
2097
|
+
try {
|
|
2098
|
+
return Promise.resolve(resolveSync(id, options));
|
|
2099
|
+
} catch (error) {
|
|
2100
|
+
return Promise.reject(error);
|
|
2101
|
+
}
|
|
2102
|
+
}
|
|
2103
|
+
function resolvePathSync(id, options) {
|
|
2104
|
+
return fileURLToPath(resolveSync(id, options));
|
|
2105
|
+
}
|
|
2106
|
+
function resolvePath(id, options) {
|
|
2107
|
+
try {
|
|
2108
|
+
return Promise.resolve(resolvePathSync(id, options));
|
|
2109
|
+
} catch (error) {
|
|
2110
|
+
return Promise.reject(error);
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
const NODE_MODULES_RE = /^(.+\/node_modules\/)([^/@]+|@[^/]+\/[^/]+)(\/?.*?)?$/;
|
|
2114
|
+
function parseNodeModulePath(path) {
|
|
2115
|
+
if (!path) {
|
|
2116
|
+
return {};
|
|
2117
|
+
}
|
|
2118
|
+
path = normalize(fileURLToPath(path));
|
|
2119
|
+
const match = NODE_MODULES_RE.exec(path);
|
|
2120
|
+
if (!match) {
|
|
2121
|
+
return {};
|
|
2122
|
+
}
|
|
2123
|
+
const [, dir, name, subpath] = match;
|
|
2124
|
+
return {
|
|
2125
|
+
dir,
|
|
2126
|
+
name,
|
|
2127
|
+
subpath: subpath ? `.${subpath}` : void 0
|
|
2128
|
+
};
|
|
2129
|
+
}
|
|
2130
|
+
async function lookupNodeModuleSubpath(path) {
|
|
2131
|
+
path = normalize(fileURLToPath(path));
|
|
2132
|
+
const { name, subpath } = parseNodeModulePath(path);
|
|
2133
|
+
if (!name || !subpath) {
|
|
2134
|
+
return subpath;
|
|
2135
|
+
}
|
|
2136
|
+
const { exports } = await readPackageJSON(path).catch(() => {
|
|
2137
|
+
}) || {};
|
|
2138
|
+
if (exports) {
|
|
2139
|
+
const resolvedSubpath = _findSubpath(subpath, exports);
|
|
2140
|
+
if (resolvedSubpath) {
|
|
2141
|
+
return resolvedSubpath;
|
|
2142
|
+
}
|
|
2143
|
+
}
|
|
2144
|
+
return subpath;
|
|
2145
|
+
}
|
|
2146
|
+
function _findSubpath(subpath, exports) {
|
|
2147
|
+
if (typeof exports === "string") {
|
|
2148
|
+
exports = { ".": exports };
|
|
2149
|
+
}
|
|
2150
|
+
if (!subpath.startsWith(".")) {
|
|
2151
|
+
subpath = subpath.startsWith("/") ? `.${subpath}` : `./${subpath}`;
|
|
2152
|
+
}
|
|
2153
|
+
if (subpath in (exports || {})) {
|
|
2154
|
+
return subpath;
|
|
2155
|
+
}
|
|
2156
|
+
return _flattenExports(exports).find((p) => p.fsPath === subpath)?.subpath;
|
|
2157
|
+
}
|
|
2158
|
+
function _flattenExports(exports = {}, parentSubpath = "./") {
|
|
2159
|
+
return Object.entries(exports).flatMap(([key, value]) => {
|
|
2160
|
+
const [subpath, condition] = key.startsWith(".") ? [key.slice(1), void 0] : ["", key];
|
|
2161
|
+
const _subPath = joinURL(parentSubpath, subpath);
|
|
2162
|
+
if (typeof value === "string") {
|
|
2163
|
+
return [{ subpath: _subPath, fsPath: value, condition }];
|
|
2164
|
+
} else {
|
|
2165
|
+
return _flattenExports(value, _subPath);
|
|
2166
|
+
}
|
|
2167
|
+
});
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
const ESM_STATIC_IMPORT_RE = /(?<=\s|^|;|\})import\s*(?:[\s"']*(?<imports>[\p{L}\p{M}\w\t\n\r $*,/{}@.]+)from\s*)?["']\s*(?<specifier>(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gmu;
|
|
2171
|
+
const EXPORT_DECAL_RE = /\bexport\s+(?<declaration>(?:async function\s*\*?|function\s*\*?|let|const enum|const|enum|var|class))\s+\*?(?<name>[\w$]+)(?<extraNames>.*,\s*[\s\w:[\]{}]*[\w$\]}]+)*/g;
|
|
2172
|
+
const EXPORT_DECAL_TYPE_RE = /\bexport\s+(?<declaration>(?:interface|type|declare (?:async function|function|let|const enum|const|enum|var|class)))\s+(?<name>[\w$]+)/g;
|
|
2173
|
+
const EXPORT_NAMED_RE = /\bexport\s*{(?<exports>[^}]+?)[\s,]*}(?:\s*from\s*["']\s*(?<specifier>(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][^\n;]*)?/g;
|
|
2174
|
+
const EXPORT_NAMED_TYPE_RE = /\bexport\s+type\s*{(?<exports>[^}]+?)[\s,]*}(?:\s*from\s*["']\s*(?<specifier>(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][^\n;]*)?/g;
|
|
2175
|
+
const EXPORT_NAMED_DESTRUCT = /\bexport\s+(?:let|var|const)\s+(?:{(?<exports1>[^}]+?)[\s,]*}|\[(?<exports2>[^\]]+?)[\s,]*])\s+=/gm;
|
|
2176
|
+
const EXPORT_STAR_RE = /\bexport\s*\*(?:\s*as\s+(?<name>[\w$]+)\s+)?\s*(?:\s*from\s*["']\s*(?<specifier>(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][^\n;]*)?/g;
|
|
2177
|
+
const EXPORT_DEFAULT_RE = /\bexport\s+default\s+(async function|function|class|true|false|\W|\d)|\bexport\s+default\s+(?<defaultName>.*)/g;
|
|
2178
|
+
const EXPORT_DEFAULT_CLASS_RE = /\bexport\s+default\s+(?<declaration>class)\s+(?<name>[\w$]+)/g;
|
|
2179
|
+
const TYPE_RE = /^\s*?type\s/;
|
|
2180
|
+
function findStaticImports(code) {
|
|
2181
|
+
return _filterStatement(
|
|
2182
|
+
_tryGetLocations(code, "import"),
|
|
2183
|
+
matchAll(ESM_STATIC_IMPORT_RE, code, { type: "static" })
|
|
2184
|
+
);
|
|
2185
|
+
}
|
|
2186
|
+
function parseStaticImport(matched) {
|
|
2187
|
+
const cleanedImports = clearImports(matched.imports);
|
|
2188
|
+
const namedImports = {};
|
|
2189
|
+
const _matches = cleanedImports.match(/{([^}]*)}/)?.[1]?.split(",") || [];
|
|
2190
|
+
for (const namedImport of _matches) {
|
|
2191
|
+
const _match = namedImport.match(/^\s*(\S*) as (\S*)\s*$/);
|
|
2192
|
+
const source = _match?.[1] || namedImport.trim();
|
|
2193
|
+
const importName = _match?.[2] || source;
|
|
2194
|
+
if (source && !TYPE_RE.test(source)) {
|
|
2195
|
+
namedImports[source] = importName;
|
|
2196
|
+
}
|
|
2197
|
+
}
|
|
2198
|
+
const { namespacedImport, defaultImport } = getImportNames(cleanedImports);
|
|
2199
|
+
return {
|
|
2200
|
+
...matched,
|
|
2201
|
+
defaultImport,
|
|
2202
|
+
namespacedImport,
|
|
2203
|
+
namedImports
|
|
2204
|
+
};
|
|
2205
|
+
}
|
|
2206
|
+
function findExports(code) {
|
|
2207
|
+
const declaredExports = matchAll(EXPORT_DECAL_RE, code, {
|
|
2208
|
+
type: "declaration"
|
|
2209
|
+
});
|
|
2210
|
+
for (const declaredExport of declaredExports) {
|
|
2211
|
+
if (/^export\s+(?:async\s+)?function/.test(declaredExport.code)) {
|
|
2212
|
+
continue;
|
|
2213
|
+
}
|
|
2214
|
+
const extraNamesStr = declaredExport.extraNames;
|
|
2215
|
+
if (extraNamesStr) {
|
|
2216
|
+
const extraNames = matchAll(
|
|
2217
|
+
/({.*?})|(\[.*?])|(,\s*(?<name>\w+))/g,
|
|
2218
|
+
extraNamesStr,
|
|
2219
|
+
{}
|
|
2220
|
+
).map((m) => m.name).filter(Boolean);
|
|
2221
|
+
declaredExport.names = [declaredExport.name, ...extraNames];
|
|
2222
|
+
}
|
|
2223
|
+
delete declaredExport.extraNames;
|
|
2224
|
+
}
|
|
2225
|
+
const namedExports = normalizeNamedExports(
|
|
2226
|
+
matchAll(EXPORT_NAMED_RE, code, {
|
|
2227
|
+
type: "named"
|
|
2228
|
+
})
|
|
2229
|
+
);
|
|
2230
|
+
const destructuredExports = matchAll(
|
|
2231
|
+
EXPORT_NAMED_DESTRUCT,
|
|
2232
|
+
code,
|
|
2233
|
+
{ type: "named" }
|
|
2234
|
+
);
|
|
2235
|
+
for (const namedExport of destructuredExports) {
|
|
2236
|
+
namedExport.exports = namedExport.exports1 || namedExport.exports2;
|
|
2237
|
+
namedExport.names = namedExport.exports.replace(/^\r?\n?/, "").split(/\s*,\s*/g).filter((name) => !TYPE_RE.test(name)).map(
|
|
2238
|
+
(name) => name.replace(/^.*?\s*:\s*/, "").replace(/\s*=\s*.*$/, "").trim()
|
|
2239
|
+
);
|
|
2240
|
+
}
|
|
2241
|
+
const defaultExport = matchAll(EXPORT_DEFAULT_RE, code, {
|
|
2242
|
+
type: "default",
|
|
2243
|
+
name: "default"
|
|
2244
|
+
});
|
|
2245
|
+
const defaultClassExports = matchAll(EXPORT_DEFAULT_CLASS_RE, code, {
|
|
2246
|
+
type: "declaration"
|
|
2247
|
+
});
|
|
2248
|
+
const starExports = matchAll(EXPORT_STAR_RE, code, {
|
|
2249
|
+
type: "star"
|
|
2250
|
+
});
|
|
2251
|
+
const exports = normalizeExports([
|
|
2252
|
+
...declaredExports,
|
|
2253
|
+
...namedExports,
|
|
2254
|
+
...destructuredExports,
|
|
2255
|
+
...defaultExport,
|
|
2256
|
+
...defaultClassExports,
|
|
2257
|
+
...starExports
|
|
2258
|
+
]);
|
|
2259
|
+
if (exports.length === 0) {
|
|
2260
|
+
return [];
|
|
2261
|
+
}
|
|
2262
|
+
const exportLocations = _tryGetLocations(code, "export");
|
|
2263
|
+
if (exportLocations && exportLocations.length === 0) {
|
|
2264
|
+
return [];
|
|
2265
|
+
}
|
|
2266
|
+
return (
|
|
2267
|
+
// Filter false positive export matches
|
|
2268
|
+
_filterStatement(exportLocations, exports).filter((exp, index, exports2) => {
|
|
2269
|
+
const nextExport = exports2[index + 1];
|
|
2270
|
+
return !nextExport || exp.type !== nextExport.type || !exp.name || exp.name !== nextExport.name;
|
|
2271
|
+
})
|
|
2272
|
+
);
|
|
2273
|
+
}
|
|
2274
|
+
function findTypeExports(code) {
|
|
2275
|
+
const declaredExports = matchAll(
|
|
2276
|
+
EXPORT_DECAL_TYPE_RE,
|
|
2277
|
+
code,
|
|
2278
|
+
{ type: "declaration" }
|
|
2279
|
+
);
|
|
2280
|
+
const namedExports = normalizeNamedExports(
|
|
2281
|
+
matchAll(EXPORT_NAMED_TYPE_RE, code, {
|
|
2282
|
+
type: "named"
|
|
2283
|
+
})
|
|
2284
|
+
);
|
|
2285
|
+
const exports = normalizeExports([
|
|
2286
|
+
...declaredExports,
|
|
2287
|
+
...namedExports
|
|
2288
|
+
]);
|
|
2289
|
+
if (exports.length === 0) {
|
|
2290
|
+
return [];
|
|
2291
|
+
}
|
|
2292
|
+
const exportLocations = _tryGetLocations(code, "export");
|
|
2293
|
+
if (exportLocations && exportLocations.length === 0) {
|
|
2294
|
+
return [];
|
|
2295
|
+
}
|
|
2296
|
+
return (
|
|
2297
|
+
// Filter false positive export matches
|
|
2298
|
+
_filterStatement(exportLocations, exports).filter((exp, index, exports2) => {
|
|
2299
|
+
const nextExport = exports2[index + 1];
|
|
2300
|
+
return !nextExport || exp.type !== nextExport.type || !exp.name || exp.name !== nextExport.name;
|
|
2301
|
+
})
|
|
2302
|
+
);
|
|
2303
|
+
}
|
|
2304
|
+
function normalizeExports(exports) {
|
|
2305
|
+
for (const exp of exports) {
|
|
2306
|
+
if (!exp.name && exp.names && exp.names.length === 1) {
|
|
2307
|
+
exp.name = exp.names[0];
|
|
2308
|
+
}
|
|
2309
|
+
if (exp.name === "default" && exp.type !== "default") {
|
|
2310
|
+
exp._type = exp.type;
|
|
2311
|
+
exp.type = "default";
|
|
2312
|
+
}
|
|
2313
|
+
if (!exp.names && exp.name) {
|
|
2314
|
+
exp.names = [exp.name];
|
|
2315
|
+
}
|
|
2316
|
+
if (exp.type === "declaration" && exp.declaration) {
|
|
2317
|
+
exp.declarationType = exp.declaration.replace(
|
|
2318
|
+
/^declare\s*/,
|
|
2319
|
+
""
|
|
2320
|
+
);
|
|
2321
|
+
}
|
|
2322
|
+
}
|
|
2323
|
+
return exports;
|
|
2324
|
+
}
|
|
2325
|
+
function normalizeNamedExports(namedExports) {
|
|
2326
|
+
for (const namedExport of namedExports) {
|
|
2327
|
+
namedExport.names = namedExport.exports.replace(/^\r?\n?/, "").split(/\s*,\s*/g).filter((name) => !TYPE_RE.test(name)).map((name) => name.replace(/^.*?\sas\s/, "").trim());
|
|
2328
|
+
}
|
|
2329
|
+
return namedExports;
|
|
2330
|
+
}
|
|
2331
|
+
async function resolveModuleExportNames(id, options) {
|
|
2332
|
+
const url = await resolvePath(id, options);
|
|
2333
|
+
const code = await loadURL(url);
|
|
2334
|
+
const exports = findExports(code);
|
|
2335
|
+
const exportNames = new Set(
|
|
2336
|
+
exports.flatMap((exp) => exp.names).filter(Boolean)
|
|
2337
|
+
);
|
|
2338
|
+
for (const exp of exports) {
|
|
2339
|
+
if (exp.type !== "star" || !exp.specifier) {
|
|
2340
|
+
continue;
|
|
2341
|
+
}
|
|
2342
|
+
const subExports = await resolveModuleExportNames(exp.specifier, {
|
|
2343
|
+
...options,
|
|
2344
|
+
url
|
|
2345
|
+
});
|
|
2346
|
+
for (const subExport of subExports) {
|
|
2347
|
+
exportNames.add(subExport);
|
|
2348
|
+
}
|
|
2349
|
+
}
|
|
2350
|
+
return [...exportNames];
|
|
2351
|
+
}
|
|
2352
|
+
function _filterStatement(locations, statements) {
|
|
2353
|
+
return statements.filter((exp) => {
|
|
2354
|
+
return !locations || locations.some((location) => {
|
|
2355
|
+
return exp.start <= location.start && exp.end >= location.end;
|
|
2356
|
+
});
|
|
2357
|
+
});
|
|
2358
|
+
}
|
|
2359
|
+
function _tryGetLocations(code, label) {
|
|
2360
|
+
try {
|
|
2361
|
+
return _getLocations(code, label);
|
|
2362
|
+
} catch {
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
function _getLocations(code, label) {
|
|
2366
|
+
const tokens = tokenizer(code, {
|
|
2367
|
+
ecmaVersion: "latest",
|
|
2368
|
+
sourceType: "module",
|
|
2369
|
+
allowHashBang: true,
|
|
2370
|
+
allowAwaitOutsideFunction: true,
|
|
2371
|
+
allowImportExportEverywhere: true
|
|
2372
|
+
});
|
|
2373
|
+
const locations = [];
|
|
2374
|
+
for (const token of tokens) {
|
|
2375
|
+
if (token.type.label === label) {
|
|
2376
|
+
locations.push({
|
|
2377
|
+
start: token.start,
|
|
2378
|
+
end: token.end
|
|
2379
|
+
});
|
|
2380
|
+
}
|
|
2381
|
+
}
|
|
2382
|
+
return locations;
|
|
2383
|
+
}
|
|
2384
|
+
|
|
2385
|
+
const ESM_RE = /(?:[\s;]|^)(?:import[\s\w*,{}]*from|import\s*["'*{]|export\b\s*(?:[*{]|default|class|type|function|const|var|let|async function)|import\.meta\b)/m;
|
|
2386
|
+
const CJS_RE = /(?:[\s;]|^)(?:module\.exports\b|exports\.\w|require\s*\(|global\.\w)/m;
|
|
2387
|
+
const COMMENT_RE = /\/\*.+?\*\/|\/\/.*(?=[nr])/g;
|
|
2388
|
+
function hasESMSyntax(code, opts = {}) {
|
|
2389
|
+
if (opts.stripComments) {
|
|
2390
|
+
code = code.replace(COMMENT_RE, "");
|
|
2391
|
+
}
|
|
2392
|
+
return ESM_RE.test(code);
|
|
2393
|
+
}
|
|
2394
|
+
function hasCJSSyntax(code, opts = {}) {
|
|
2395
|
+
if (opts.stripComments) {
|
|
2396
|
+
code = code.replace(COMMENT_RE, "");
|
|
2397
|
+
}
|
|
2398
|
+
return CJS_RE.test(code);
|
|
2399
|
+
}
|
|
2400
|
+
function detectSyntax(code, opts = {}) {
|
|
2401
|
+
if (opts.stripComments) {
|
|
2402
|
+
code = code.replace(COMMENT_RE, "");
|
|
2403
|
+
}
|
|
2404
|
+
const hasESM = hasESMSyntax(code, {});
|
|
2405
|
+
const hasCJS = hasCJSSyntax(code, {});
|
|
2406
|
+
return {
|
|
2407
|
+
hasESM,
|
|
2408
|
+
hasCJS,
|
|
2409
|
+
isMixed: hasESM && hasCJS
|
|
2410
|
+
};
|
|
2411
|
+
}
|
|
2412
|
+
|
|
2413
|
+
export { resolvePath as a, findExports as b, findTypeExports as c, detectSyntax as d, resolve as e, findStaticImports as f, resolveModuleExportNames as g, fileURLToPath as h, parseNodeModulePath as i, lookupNodeModuleSubpath as l, parseStaticImport as p, resolvePathSync as r, sanitizeFilePath as s };
|