forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.js
More file actions
284 lines (240 loc) · 8.23 KB
/
Copy pathloader.js
File metadata and controls
284 lines (240 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
'use strict';
// This is needed to avoid cycles in esm/resolve <-> cjs/loader
require('internal/modules/cjs/loader');
const {
FunctionPrototypeBind,
ObjectSetPrototypeOf,
} = primordials;
const {
ERR_INVALID_ARG_VALUE,
ERR_INVALID_RETURN_PROPERTY,
ERR_INVALID_RETURN_PROPERTY_VALUE,
ERR_INVALID_RETURN_VALUE,
ERR_UNKNOWN_MODULE_FORMAT,
} = require('internal/errors').codes;
const { URL, pathToFileURL } = require('internal/url');
const { validateString } = require('internal/validators');
const ModuleMap = require('internal/modules/esm/module_map');
const ModuleJob = require('internal/modules/esm/module_job');
const {
defaultResolve,
DEFAULT_CONDITIONS,
} = require('internal/modules/esm/resolve');
const { defaultGetFormat } = require('internal/modules/esm/get_format');
const { defaultGetSource } = require(
'internal/modules/esm/get_source');
const { translators } = require(
'internal/modules/esm/translators');
const { getOptionValue } = require('internal/options');
const {
isArrayBufferView,
isAnyArrayBuffer,
} = require('internal/util/types');
let cwd; // Initialized in importLoader
function validateSource(source, hookName, allowString) {
if (allowString && typeof source === 'string') {
return;
}
if (isArrayBufferView(source) || isAnyArrayBuffer(source)) {
return;
}
throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
`${allowString ? 'string, ' : ''}array buffer, or typed array`,
hookName,
'source',
source,
);
}
/* A Loader instance is used as the main entry point for loading ES modules.
* Currently, this is a singleton -- there is only one used for loading
* the main module and everything in its dependency graph. */
class Loader {
constructor() {
// Methods which translate input code or other information
// into es modules
this.translators = translators;
// Registry of loaded modules, akin to `require.cache`
this.moduleMap = new ModuleMap();
// Preload code is provided by loaders to be run after hook initialization.
this.globalPreloadCode = [];
// Loader resolve hook.
this._resolve = defaultResolve;
// Loader getFormat hook.
this._getFormat = defaultGetFormat;
// Loader getSource hook.
this._getSource = defaultGetSource;
// Transform source hooks.
this.transformSourceHooks = [];
// The index for assigning unique URLs to anonymous module evaluation
this.evalIndex = 0;
}
async resolve(specifier, parentURL) {
const isMain = parentURL === undefined;
if (!isMain)
validateString(parentURL, 'parentURL');
const resolveResponse = await this._resolve(
specifier, { parentURL, conditions: DEFAULT_CONDITIONS });
if (typeof resolveResponse !== 'object') {
throw new ERR_INVALID_RETURN_VALUE(
'object', 'loader resolve', resolveResponse);
}
const { url } = resolveResponse;
if (typeof url !== 'string') {
throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
'string', 'loader resolve', 'url', url);
}
return url;
}
async getFormat(url) {
const getFormatResponse = await this._getFormat(url, {});
if (typeof getFormatResponse !== 'object') {
throw new ERR_INVALID_RETURN_VALUE(
'object', 'loader getFormat', getFormatResponse);
}
const { format } = getFormatResponse;
if (typeof format !== 'string') {
throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
'string', 'loader getFormat', 'format', format);
}
if (format === 'builtin') {
return format;
}
if (this._resolve !== defaultResolve) {
try {
new URL(url);
} catch {
throw new ERR_INVALID_RETURN_PROPERTY(
'url', 'loader resolve', 'url', url
);
}
}
if (this._resolve === defaultResolve &&
!url.startsWith('file:') &&
!url.startsWith('data:')
) {
throw new ERR_INVALID_RETURN_PROPERTY(
'file: or data: url', 'loader resolve', 'url', url
);
}
return format;
}
async getSource(url, format) {
const { source: originalSource } = await this._getSource(url, { format });
const allowString = format !== 'wasm';
validateSource(originalSource, 'getSource', allowString);
let source = originalSource;
for (let i = 0; i < this.transformSourceHooks.length; i += 1) {
const hook = this.transformSourceHooks[i];
({ source } = await hook(source, { url, format, originalSource }));
validateSource(source, 'transformSource', allowString);
}
return source;
}
async eval(
source,
url = pathToFileURL(`${process.cwd()}/[eval${++this.evalIndex}]`).href
) {
const evalInstance = (url) => {
const { ModuleWrap, callbackMap } = internalBinding('module_wrap');
const module = new ModuleWrap(url, undefined, source, 0, 0);
callbackMap.set(module, {
importModuleDynamically: (specifier, { url }) => {
return this.import(specifier, url);
}
});
return module;
};
const job = new ModuleJob(this, url, evalInstance, false, false);
this.moduleMap.set(url, job);
const { module } = await job.run();
return {
namespace: module.getNamespace(),
};
}
async import(specifier, parent) {
const job = await this.getModuleJob(specifier, parent);
const { module } = await job.run();
return module.getNamespace();
}
async importLoader(specifier) {
if (cwd === undefined) {
try {
// `process.cwd()` can fail.
cwd = process.cwd() + '/';
} catch {
cwd = 'file:///';
}
cwd = pathToFileURL(cwd).href;
}
const { url } = await defaultResolve(specifier, cwd,
{ conditions: DEFAULT_CONDITIONS });
const { format } = await defaultGetFormat(url, {});
// !!! CRITICAL SECTION !!!
// NO AWAIT OPS BETWEEN HERE AND SETTING JOB IN MODULE MAP!
// YIELDING CONTROL COULD RESULT IN MAP BEING OVERRIDDEN!
let job = this.moduleMap.get(url);
if (job === undefined) {
if (!translators.has(format))
throw new ERR_UNKNOWN_MODULE_FORMAT(format);
const loaderInstance = translators.get(format);
job = new ModuleJob(this, url, loaderInstance, false, false);
this.moduleMap.set(url, job);
// !!! END CRITICAL SECTION !!!
}
const { module } = await job.run();
return module.getNamespace();
}
hook(hooks) {
const {
resolve,
getFormat,
getSource,
} = hooks;
// Use .bind() to avoid giving access to the Loader instance when called.
if (resolve !== undefined)
this._resolve = FunctionPrototypeBind(resolve, null);
if (getFormat !== undefined) {
this._getFormat = FunctionPrototypeBind(getFormat, null);
}
if (getSource !== undefined) {
this._getSource = FunctionPrototypeBind(getSource, null);
}
}
runGlobalPreloadCode() {
for (let i = 0; i < this.globalPreloadCode.length; i += 1) {
const preloadCode = this.globalPreloadCode[i];
const { compileFunction } = require('vm');
const preloadInit = compileFunction(preloadCode, ['getBuiltin'], {
filename: '<preload>',
});
const { NativeModule } = require('internal/bootstrap/loaders');
preloadInit.call(globalThis, (builtinName) => {
if (NativeModule.canBeRequiredByUsers(builtinName)) {
return require(builtinName);
}
throw new ERR_INVALID_ARG_VALUE('builtinName', builtinName);
});
}
}
async getModuleJob(specifier, parentURL) {
const url = await this.resolve(specifier, parentURL);
const format = await this.getFormat(url);
// !!! CRITICAL SECTION !!!
// NO AWAIT OPS BETWEEN HERE AND SETTING JOB IN MODULE MAP
let job = this.moduleMap.get(url);
if (job !== undefined)
return job;
if (!translators.has(format))
throw new ERR_UNKNOWN_MODULE_FORMAT(format);
const loaderInstance = translators.get(format);
const inspectBrk = parentURL === undefined &&
format === 'module' && getOptionValue('--inspect-brk');
job = new ModuleJob(this, url, loaderInstance, parentURL === undefined,
inspectBrk);
this.moduleMap.set(url, job);
// !!! END CRITICAL SECTION !!!
return job;
}
}
ObjectSetPrototypeOf(Loader.prototype, null);
exports.Loader = Loader;