2017-11-26 17:12:09 -06:00
|
|
|
'use strict';
|
|
|
|
|
2019-04-05 11:11:26 +02:00
|
|
|
const {
|
2020-12-29 20:07:57 +01:00
|
|
|
ArrayPrototypeForEach,
|
2020-11-07 10:19:54 +01:00
|
|
|
ArrayPrototypeMap,
|
2020-05-14 22:40:37 -07:00
|
|
|
Boolean,
|
2019-11-22 18:04:46 +01:00
|
|
|
JSONParse,
|
2020-10-17 12:34:16 +02:00
|
|
|
ObjectGetPrototypeOf,
|
2020-05-14 22:40:37 -07:00
|
|
|
ObjectPrototypeHasOwnProperty,
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectKeys,
|
2020-12-29 20:07:57 +01:00
|
|
|
SafeArrayIterator,
|
2019-04-05 11:11:26 +02:00
|
|
|
SafeMap,
|
2020-05-14 22:40:37 -07:00
|
|
|
SafeSet,
|
2019-11-22 18:04:46 +01:00
|
|
|
StringPrototypeReplace,
|
2020-11-07 10:19:54 +01:00
|
|
|
StringPrototypeSlice,
|
2020-09-30 04:24:34 -07:00
|
|
|
StringPrototypeStartsWith,
|
2020-10-17 12:34:16 +02:00
|
|
|
SyntaxErrorPrototype,
|
2021-04-26 18:03:54 +02:00
|
|
|
globalThis: { WebAssembly },
|
2019-04-05 11:11:26 +02:00
|
|
|
} = primordials;
|
|
|
|
|
2020-03-11 10:38:00 -05:00
|
|
|
let _TYPES = null;
|
|
|
|
function lazyTypes() {
|
|
|
|
if (_TYPES !== null) return _TYPES;
|
|
|
|
return _TYPES = require('internal/util/types');
|
|
|
|
}
|
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
const { readFileSync } = require('fs');
|
2020-10-04 07:34:15 -07:00
|
|
|
const { extname, isAbsolute } = require('path');
|
2018-03-07 02:30:18 +08:00
|
|
|
const {
|
2021-07-24 17:01:42 +02:00
|
|
|
hasEsmSyntax,
|
|
|
|
loadNativeModule,
|
2019-07-18 09:01:59 -05:00
|
|
|
stripBOM,
|
2018-03-07 02:30:18 +08:00
|
|
|
} = require('internal/modules/cjs/helpers');
|
2020-05-14 22:40:37 -07:00
|
|
|
const {
|
|
|
|
Module: CJSModule,
|
|
|
|
cjsParseCache
|
|
|
|
} = require('internal/modules/cjs/loader');
|
2017-11-26 17:12:09 -06:00
|
|
|
const internalURLModule = require('internal/url');
|
2018-03-23 15:32:23 +01:00
|
|
|
const createDynamicModule = require(
|
|
|
|
'internal/modules/esm/create_dynamic_module');
|
2018-08-28 17:28:46 +02:00
|
|
|
const { fileURLToPath, URL } = require('url');
|
2020-03-14 07:55:44 -04:00
|
|
|
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
|
|
|
|
debug = fn;
|
|
|
|
});
|
2019-12-14 22:27:48 -05:00
|
|
|
const { emitExperimentalWarning } = require('internal/util');
|
2020-03-11 10:38:00 -05:00
|
|
|
const {
|
|
|
|
ERR_UNKNOWN_BUILTIN_MODULE,
|
|
|
|
ERR_INVALID_RETURN_PROPERTY_VALUE
|
|
|
|
} = require('internal/errors').codes;
|
2019-09-29 14:15:39 -07:00
|
|
|
const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache');
|
2019-10-04 13:37:27 -04:00
|
|
|
const moduleWrap = internalBinding('module_wrap');
|
|
|
|
const { ModuleWrap } = moduleWrap;
|
2020-05-14 22:40:37 -07:00
|
|
|
const asyncESM = require('internal/process/esm_loader');
|
2020-09-30 04:24:34 -07:00
|
|
|
const { emitWarningSync } = require('internal/process/warning');
|
2021-04-26 18:03:54 +02:00
|
|
|
const { TextDecoder } = require('internal/encoding');
|
2018-07-07 23:32:23 -05:00
|
|
|
|
2020-10-10 04:08:58 -07:00
|
|
|
let cjsParse;
|
|
|
|
async function initCJSParse() {
|
2020-10-13 12:31:23 -07:00
|
|
|
if (typeof WebAssembly === 'undefined') {
|
2020-11-02 08:09:59 -08:00
|
|
|
cjsParse = require('internal/deps/cjs-module-lexer/lexer').parse;
|
2020-10-13 12:31:23 -07:00
|
|
|
} else {
|
2020-10-10 04:08:58 -07:00
|
|
|
const { parse, init } =
|
|
|
|
require('internal/deps/cjs-module-lexer/dist/lexer');
|
|
|
|
await init();
|
2020-10-13 12:31:23 -07:00
|
|
|
cjsParse = parse;
|
2020-10-10 04:08:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 17:12:09 -06:00
|
|
|
const translators = new SafeMap();
|
2018-08-28 17:28:46 +02:00
|
|
|
exports.translators = translators;
|
2020-09-30 04:24:34 -07:00
|
|
|
exports.enrichCJSError = enrichCJSError;
|
2017-11-26 17:12:09 -06:00
|
|
|
|
2020-03-11 10:38:00 -05:00
|
|
|
let DECODER = null;
|
|
|
|
function assertBufferSource(body, allowString, hookName) {
|
|
|
|
if (allowString && typeof body === 'string') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { isArrayBufferView, isAnyArrayBuffer } = lazyTypes();
|
|
|
|
if (isArrayBufferView(body) || isAnyArrayBuffer(body)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
|
|
|
|
`${allowString ? 'string, ' : ''}array buffer, or typed array`,
|
|
|
|
hookName,
|
|
|
|
'source',
|
|
|
|
body
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function stringify(body) {
|
|
|
|
if (typeof body === 'string') return body;
|
|
|
|
assertBufferSource(body, false, 'transformSource');
|
|
|
|
DECODER = DECODER === null ? new TextDecoder() : DECODER;
|
|
|
|
return DECODER.decode(body);
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:03:07 -05:00
|
|
|
function errPath(url) {
|
|
|
|
const parsed = new URL(url);
|
|
|
|
if (parsed.protocol === 'file:') {
|
|
|
|
return fileURLToPath(parsed);
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2021-08-28 01:47:49 +02:00
|
|
|
async function importModuleDynamically(specifier, { url }, assertions) {
|
|
|
|
return asyncESM.esmLoader.import(specifier, url, assertions);
|
2019-12-19 16:25:52 -05:00
|
|
|
}
|
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
// Strategy for loading a standard JavaScript module.
|
2021-08-25 22:55:14 +02:00
|
|
|
translators.set('module', async function moduleStrategy(url, source, isMain) {
|
|
|
|
assertBufferSource(source, true, 'load');
|
2020-03-11 10:38:00 -05:00
|
|
|
source = stringify(source);
|
2019-09-20 11:43:02 -07:00
|
|
|
maybeCacheSourceMap(url, source);
|
2017-11-26 17:12:09 -06:00
|
|
|
debug(`Translating StandardModule ${url}`);
|
2019-10-04 20:08:00 -07:00
|
|
|
const module = new ModuleWrap(url, undefined, source, 0, 0);
|
2019-10-04 13:37:27 -04:00
|
|
|
moduleWrap.callbackMap.set(module, {
|
2021-07-02 15:57:01 -05:00
|
|
|
initializeImportMeta: (meta, wrap) => this.importMetaInitialize(meta, {
|
|
|
|
url: wrap.url
|
|
|
|
}),
|
2018-08-17 17:26:34 -05:00
|
|
|
importModuleDynamically,
|
|
|
|
});
|
2019-10-04 13:37:27 -04:00
|
|
|
return module;
|
2017-11-26 17:12:09 -06:00
|
|
|
});
|
|
|
|
|
2021-07-24 17:01:42 +02:00
|
|
|
/**
|
|
|
|
* @param {Error | any} err
|
|
|
|
* @param {string} [content] Content of the file, if known.
|
|
|
|
* @param {string} [filename] Useful only if `content` is unknown.
|
|
|
|
*/
|
|
|
|
function enrichCJSError(err, content, filename) {
|
|
|
|
if (err != null && ObjectGetPrototypeOf(err) === SyntaxErrorPrototype &&
|
|
|
|
hasEsmSyntax(content || readFileSync(filename, 'utf-8'))) {
|
2020-09-30 04:24:34 -07:00
|
|
|
// Emit the warning synchronously because we are in the middle of handling
|
|
|
|
// a SyntaxError that will throw and likely terminate the process before an
|
|
|
|
// asynchronous warning would be emitted.
|
|
|
|
emitWarningSync(
|
|
|
|
'To load an ES module, set "type": "module" in the package.json or use ' +
|
|
|
|
'the .mjs extension.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 17:12:09 -06:00
|
|
|
// Strategy for loading a node-style CommonJS module
|
|
|
|
const isWindows = process.platform === 'win32';
|
|
|
|
const winSepRegEx = /\//g;
|
2021-08-25 22:55:14 +02:00
|
|
|
translators.set('commonjs', async function commonjsStrategy(url, source,
|
|
|
|
isMain) {
|
2017-11-26 17:12:09 -06:00
|
|
|
debug(`Translating CJSModule ${url}`);
|
2020-05-14 22:40:37 -07:00
|
|
|
|
|
|
|
let filename = internalURLModule.fileURLToPath(new URL(url));
|
|
|
|
if (isWindows)
|
|
|
|
filename = StringPrototypeReplace(filename, winSepRegEx, '\\');
|
|
|
|
|
2020-10-10 04:08:58 -07:00
|
|
|
if (!cjsParse) await initCJSParse();
|
2020-05-14 22:40:37 -07:00
|
|
|
const { module, exportNames } = cjsPreparseModuleExports(filename);
|
|
|
|
const namesWithDefault = exportNames.has('default') ?
|
|
|
|
[...exportNames] : ['default', ...exportNames];
|
|
|
|
|
|
|
|
return new ModuleWrap(url, undefined, namesWithDefault, function() {
|
2017-11-26 17:12:09 -06:00
|
|
|
debug(`Loading CJSModule ${url}`);
|
2020-05-14 22:40:37 -07:00
|
|
|
|
2020-08-02 18:03:48 -07:00
|
|
|
let exports;
|
2021-08-25 22:55:14 +02:00
|
|
|
if (asyncESM.esmLoader.cjsCache.has(module)) {
|
|
|
|
exports = asyncESM.esmLoader.cjsCache.get(module);
|
|
|
|
asyncESM.esmLoader.cjsCache.delete(module);
|
2020-08-02 18:03:48 -07:00
|
|
|
} else {
|
2020-09-30 04:24:34 -07:00
|
|
|
try {
|
|
|
|
exports = CJSModule._load(filename, undefined, isMain);
|
|
|
|
} catch (err) {
|
2021-07-24 17:01:42 +02:00
|
|
|
enrichCJSError(err, undefined, filename);
|
2020-09-30 04:24:34 -07:00
|
|
|
throw err;
|
|
|
|
}
|
2020-05-14 22:40:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const exportName of exportNames) {
|
|
|
|
if (!ObjectPrototypeHasOwnProperty(exports, exportName) ||
|
|
|
|
exportName === 'default')
|
|
|
|
continue;
|
|
|
|
// We might trigger a getter -> dont fail.
|
|
|
|
let value;
|
|
|
|
try {
|
|
|
|
value = exports[exportName];
|
|
|
|
} catch {}
|
|
|
|
this.setExport(exportName, value);
|
2020-08-02 18:03:48 -07:00
|
|
|
}
|
|
|
|
this.setExport('default', exports);
|
2019-10-04 20:08:00 -07:00
|
|
|
});
|
2017-11-26 17:12:09 -06:00
|
|
|
});
|
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
function cjsPreparseModuleExports(filename) {
|
|
|
|
let module = CJSModule._cache[filename];
|
|
|
|
if (module) {
|
|
|
|
const cached = cjsParseCache.get(module);
|
|
|
|
if (cached)
|
|
|
|
return { module, exportNames: cached.exportNames };
|
|
|
|
}
|
|
|
|
const loaded = Boolean(module);
|
|
|
|
if (!loaded) {
|
|
|
|
module = new CJSModule(filename);
|
|
|
|
module.filename = filename;
|
|
|
|
module.paths = CJSModule._nodeModulePaths(module.path);
|
|
|
|
CJSModule._cache[filename] = module;
|
|
|
|
}
|
|
|
|
|
|
|
|
let source;
|
|
|
|
try {
|
|
|
|
source = readFileSync(filename, 'utf8');
|
|
|
|
} catch {}
|
|
|
|
|
2020-09-30 04:24:34 -07:00
|
|
|
let exports, reexports;
|
|
|
|
try {
|
|
|
|
({ exports, reexports } = cjsParse(source || ''));
|
|
|
|
} catch {
|
|
|
|
exports = [];
|
|
|
|
reexports = [];
|
|
|
|
}
|
2020-05-14 22:40:37 -07:00
|
|
|
|
2020-12-29 20:07:57 +01:00
|
|
|
const exportNames = new SafeSet(new SafeArrayIterator(exports));
|
2020-05-14 22:40:37 -07:00
|
|
|
|
|
|
|
// Set first for cycles.
|
|
|
|
cjsParseCache.set(module, { source, exportNames, loaded });
|
|
|
|
|
|
|
|
if (reexports.length) {
|
|
|
|
module.filename = filename;
|
|
|
|
module.paths = CJSModule._nodeModulePaths(module.path);
|
|
|
|
}
|
2020-12-29 20:07:57 +01:00
|
|
|
ArrayPrototypeForEach(reexports, (reexport) => {
|
2020-05-14 22:40:37 -07:00
|
|
|
let resolved;
|
|
|
|
try {
|
|
|
|
resolved = CJSModule._resolveFilename(reexport, module);
|
|
|
|
} catch {
|
2020-12-29 20:07:57 +01:00
|
|
|
return;
|
2020-05-14 22:40:37 -07:00
|
|
|
}
|
|
|
|
const ext = extname(resolved);
|
2020-10-04 07:34:15 -07:00
|
|
|
if ((ext === '.js' || ext === '.cjs' || !CJSModule._extensions[ext]) &&
|
|
|
|
isAbsolute(resolved)) {
|
2020-05-14 22:40:37 -07:00
|
|
|
const { exportNames: reexportNames } = cjsPreparseModuleExports(resolved);
|
|
|
|
for (const name of reexportNames)
|
|
|
|
exportNames.add(name);
|
|
|
|
}
|
2020-12-29 20:07:57 +01:00
|
|
|
});
|
2020-05-14 22:40:37 -07:00
|
|
|
|
|
|
|
return { module, exportNames };
|
|
|
|
}
|
|
|
|
|
2017-11-26 17:12:09 -06:00
|
|
|
// Strategy for loading a node builtin CommonJS module that isn't
|
|
|
|
// through normal resolution
|
2018-08-28 17:28:46 +02:00
|
|
|
translators.set('builtin', async function builtinStrategy(url) {
|
2017-11-26 17:12:09 -06:00
|
|
|
debug(`Translating BuiltinModule ${url}`);
|
2020-09-27 21:24:44 -07:00
|
|
|
// Slice 'node:' scheme
|
2020-11-07 10:19:54 +01:00
|
|
|
const id = StringPrototypeSlice(url, 5);
|
2021-02-06 12:10:00 +01:00
|
|
|
const module = loadNativeModule(id, url);
|
2020-11-07 10:19:54 +01:00
|
|
|
if (!StringPrototypeStartsWith(url, 'node:') || !module) {
|
2020-09-27 21:24:44 -07:00
|
|
|
throw new ERR_UNKNOWN_BUILTIN_MODULE(url);
|
2019-03-09 18:12:05 +01:00
|
|
|
}
|
2019-09-27 12:12:18 -05:00
|
|
|
debug(`Loading BuiltinModule ${url}`);
|
|
|
|
return module.getESMFacade();
|
2017-11-26 17:12:09 -06:00
|
|
|
});
|
|
|
|
|
2018-02-12 13:02:42 +02:00
|
|
|
// Strategy for loading a JSON file
|
2021-08-25 22:55:14 +02:00
|
|
|
translators.set('json', async function jsonStrategy(url, source) {
|
2019-11-24 12:40:40 +08:00
|
|
|
emitExperimentalWarning('Importing JSON modules');
|
2021-08-25 22:55:14 +02:00
|
|
|
assertBufferSource(source, true, 'load');
|
2018-08-28 17:28:46 +02:00
|
|
|
debug(`Loading JSONModule ${url}`);
|
2020-11-07 10:19:54 +01:00
|
|
|
const pathname = StringPrototypeStartsWith(url, 'file:') ?
|
|
|
|
fileURLToPath(url) : null;
|
2019-07-09 16:03:07 -05:00
|
|
|
let modulePath;
|
|
|
|
let module;
|
|
|
|
if (pathname) {
|
|
|
|
modulePath = isWindows ?
|
2019-11-22 18:04:46 +01:00
|
|
|
StringPrototypeReplace(pathname, winSepRegEx, '\\') : pathname;
|
2019-07-09 16:03:07 -05:00
|
|
|
module = CJSModule._cache[modulePath];
|
|
|
|
if (module && module.loaded) {
|
|
|
|
const exports = module.exports;
|
2019-10-04 20:08:00 -07:00
|
|
|
return new ModuleWrap(url, undefined, ['default'], function() {
|
2019-10-04 13:37:27 -04:00
|
|
|
this.setExport('default', exports);
|
2019-10-04 20:08:00 -07:00
|
|
|
});
|
2019-07-09 16:03:07 -05:00
|
|
|
}
|
2018-08-28 17:28:46 +02:00
|
|
|
}
|
2020-03-11 10:38:00 -05:00
|
|
|
source = stringify(source);
|
2019-07-09 16:03:07 -05:00
|
|
|
if (pathname) {
|
|
|
|
// A require call could have been called on the same file during loading and
|
|
|
|
// that resolves synchronously. To make sure we always return the identical
|
|
|
|
// export, we have to check again if the module already exists or not.
|
|
|
|
module = CJSModule._cache[modulePath];
|
|
|
|
if (module && module.loaded) {
|
|
|
|
const exports = module.exports;
|
2019-10-04 20:08:00 -07:00
|
|
|
return new ModuleWrap(url, undefined, ['default'], function() {
|
2019-10-04 13:37:27 -04:00
|
|
|
this.setExport('default', exports);
|
2019-10-04 20:08:00 -07:00
|
|
|
});
|
2019-07-09 16:03:07 -05:00
|
|
|
}
|
2019-05-13 14:19:28 +02:00
|
|
|
}
|
2018-08-28 17:28:46 +02:00
|
|
|
try {
|
2019-12-14 22:27:48 -05:00
|
|
|
const exports = JSONParse(stripBOM(source));
|
2018-08-28 17:28:46 +02:00
|
|
|
module = {
|
|
|
|
exports,
|
|
|
|
loaded: true
|
|
|
|
};
|
|
|
|
} catch (err) {
|
|
|
|
// TODO (BridgeAR): We could add a NodeCore error that wraps the JSON
|
|
|
|
// parse error instead of just manipulating the original error message.
|
|
|
|
// That would allow to add further properties and maybe additional
|
|
|
|
// debugging information.
|
2019-07-09 16:03:07 -05:00
|
|
|
err.message = errPath(url) + ': ' + err.message;
|
2018-08-28 17:28:46 +02:00
|
|
|
throw err;
|
|
|
|
}
|
2019-07-09 16:03:07 -05:00
|
|
|
if (pathname) {
|
|
|
|
CJSModule._cache[modulePath] = module;
|
|
|
|
}
|
2019-10-04 20:08:00 -07:00
|
|
|
return new ModuleWrap(url, undefined, ['default'], function() {
|
2018-08-28 17:28:46 +02:00
|
|
|
debug(`Parsing JSONModule ${url}`);
|
2019-10-04 13:37:27 -04:00
|
|
|
this.setExport('default', module.exports);
|
2019-10-04 20:08:00 -07:00
|
|
|
});
|
2017-11-26 17:12:09 -06:00
|
|
|
});
|
2019-04-30 00:27:20 +08:00
|
|
|
|
|
|
|
// Strategy for loading a wasm module
|
2021-08-25 22:55:14 +02:00
|
|
|
translators.set('wasm', async function(url, source) {
|
2021-11-09 20:39:29 -08:00
|
|
|
emitExperimentalWarning('Importing WebAssembly modules');
|
2021-08-25 22:55:14 +02:00
|
|
|
|
|
|
|
assertBufferSource(source, false, 'load');
|
|
|
|
|
2019-04-30 00:27:20 +08:00
|
|
|
debug(`Translating WASMModule ${url}`);
|
2021-08-25 22:55:14 +02:00
|
|
|
|
2019-04-30 00:27:20 +08:00
|
|
|
let compiled;
|
|
|
|
try {
|
2019-12-14 22:27:48 -05:00
|
|
|
compiled = await WebAssembly.compile(source);
|
2019-04-30 00:27:20 +08:00
|
|
|
} catch (err) {
|
2019-07-09 16:03:07 -05:00
|
|
|
err.message = errPath(url) + ': ' + err.message;
|
2019-04-30 00:27:20 +08:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
const imports =
|
2020-11-07 10:19:54 +01:00
|
|
|
ArrayPrototypeMap(WebAssembly.Module.imports(compiled),
|
|
|
|
({ module }) => module);
|
|
|
|
const exports =
|
|
|
|
ArrayPrototypeMap(WebAssembly.Module.exports(compiled),
|
|
|
|
({ name }) => name);
|
2019-04-30 00:27:20 +08:00
|
|
|
|
|
|
|
return createDynamicModule(imports, exports, url, (reflect) => {
|
|
|
|
const { exports } = new WebAssembly.Instance(compiled, reflect.imports);
|
2019-11-22 18:04:46 +01:00
|
|
|
for (const expt of ObjectKeys(exports))
|
2019-04-30 00:27:20 +08:00
|
|
|
reflect.exports[expt].set(exports[expt]);
|
2019-10-04 13:37:27 -04:00
|
|
|
}).module;
|
2019-04-30 00:27:20 +08:00
|
|
|
});
|