2017-11-26 17:12:09 -06:00
|
|
|
'use strict';
|
|
|
|
|
2019-04-30 00:27:20 +08:00
|
|
|
/* global WebAssembly */
|
|
|
|
|
2019-04-05 11:11:26 +02:00
|
|
|
const {
|
2020-05-14 22:40:37 -07:00
|
|
|
Boolean,
|
2019-11-22 18:04:46 +01:00
|
|
|
JSONParse,
|
2020-05-14 22:40:37 -07:00
|
|
|
ObjectPrototypeHasOwnProperty,
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectKeys,
|
2020-05-03 12:04:59 +02:00
|
|
|
PromisePrototypeCatch,
|
|
|
|
PromiseReject,
|
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,
|
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');
|
|
|
|
const { extname } = require('path');
|
2018-03-07 02:30:18 +08:00
|
|
|
const {
|
2019-07-18 09:01:59 -05:00
|
|
|
stripBOM,
|
|
|
|
loadNativeModule
|
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');
|
2019-12-14 22:27:48 -05:00
|
|
|
const { defaultGetSource } = require(
|
|
|
|
'internal/modules/esm/get_source');
|
|
|
|
const { defaultTransformSource } = require(
|
|
|
|
'internal/modules/esm/transform_source');
|
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;
|
2019-12-19 16:25:52 -05:00
|
|
|
const { getOptionValue } = require('internal/options');
|
|
|
|
const experimentalImportMetaResolve =
|
|
|
|
getOptionValue('--experimental-import-meta-resolve');
|
2020-05-14 22:40:37 -07:00
|
|
|
const asyncESM = require('internal/process/esm_loader');
|
|
|
|
const cjsParse = require('internal/deps/cjs-module-lexer/lexer');
|
2018-07-07 23:32:23 -05:00
|
|
|
|
2017-11-26 17:12:09 -06:00
|
|
|
const translators = new SafeMap();
|
2018-08-28 17:28:46 +02:00
|
|
|
exports.translators = translators;
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-17 17:26:34 -05:00
|
|
|
async function importModuleDynamically(specifier, { url }) {
|
2020-08-02 18:03:48 -07:00
|
|
|
return asyncESM.ESMLoader.import(specifier, url);
|
2019-12-19 16:25:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function createImportMetaResolve(defaultParentUrl) {
|
|
|
|
return async function resolve(specifier, parentUrl = defaultParentUrl) {
|
2020-05-03 12:04:59 +02:00
|
|
|
return PromisePrototypeCatch(
|
2020-08-02 18:03:48 -07:00
|
|
|
asyncESM.ESMLoader.resolve(specifier, parentUrl),
|
2020-05-03 12:04:59 +02:00
|
|
|
(error) => (
|
|
|
|
error.code === 'ERR_UNSUPPORTED_DIR_IMPORT' ?
|
|
|
|
error.url : PromiseReject(error))
|
|
|
|
);
|
2019-12-19 16:25:52 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function initializeImportMeta(meta, { url }) {
|
|
|
|
// Alphabetical
|
|
|
|
if (experimentalImportMetaResolve)
|
|
|
|
meta.resolve = createImportMetaResolve(url);
|
|
|
|
meta.url = url;
|
2018-08-17 17:26:34 -05:00
|
|
|
}
|
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
// Strategy for loading a standard JavaScript module.
|
2018-08-28 17:28:46 +02:00
|
|
|
translators.set('module', async function moduleStrategy(url) {
|
2019-12-14 22:27:48 -05:00
|
|
|
let { source } = await this._getSource(
|
|
|
|
url, { format: 'module' }, defaultGetSource);
|
2020-03-11 10:38:00 -05:00
|
|
|
assertBufferSource(source, true, 'getSource');
|
2019-12-14 22:27:48 -05:00
|
|
|
({ source } = await this._transformSource(
|
|
|
|
source, { url, format: 'module' }, defaultTransformSource));
|
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, {
|
2018-08-17 17:26:34 -05:00
|
|
|
initializeImportMeta,
|
|
|
|
importModuleDynamically,
|
|
|
|
});
|
2019-10-04 13:37:27 -04:00
|
|
|
return module;
|
2017-11-26 17:12:09 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// Strategy for loading a node-style CommonJS module
|
|
|
|
const isWindows = process.platform === 'win32';
|
|
|
|
const winSepRegEx = /\//g;
|
2020-05-14 22:40:37 -07:00
|
|
|
translators.set('commonjs', async function commonjsStrategy(url, 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, '\\');
|
|
|
|
|
|
|
|
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;
|
2020-05-14 22:40:37 -07: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-05-14 22:40:37 -07:00
|
|
|
exports = CJSModule._load(filename, undefined, isMain);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {}
|
|
|
|
|
|
|
|
const { exports, reexports } = cjsParse(source || '');
|
|
|
|
|
|
|
|
const exportNames = new SafeSet(exports);
|
|
|
|
|
|
|
|
// Set first for cycles.
|
|
|
|
cjsParseCache.set(module, { source, exportNames, loaded });
|
|
|
|
|
|
|
|
if (reexports.length) {
|
|
|
|
module.filename = filename;
|
|
|
|
module.paths = CJSModule._nodeModulePaths(module.path);
|
|
|
|
}
|
|
|
|
for (const reexport of reexports) {
|
|
|
|
let resolved;
|
|
|
|
try {
|
|
|
|
resolved = CJSModule._resolveFilename(reexport, module);
|
|
|
|
} catch {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const ext = extname(resolved);
|
|
|
|
if (ext === '.js' || ext === '.cjs' || !CJSModule._extensions[ext]) {
|
|
|
|
const { exportNames: reexportNames } = cjsPreparseModuleExports(resolved);
|
|
|
|
for (const name of reexportNames)
|
|
|
|
exportNames.add(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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}`);
|
2019-12-19 16:25:52 -05:00
|
|
|
// Slice 'nodejs:' scheme
|
|
|
|
const id = url.slice(7);
|
2019-07-18 09:01:59 -05:00
|
|
|
const module = loadNativeModule(id, url, true);
|
2019-12-19 16:25:52 -05:00
|
|
|
if (!url.startsWith('nodejs:') || !module) {
|
2019-03-09 18:12:05 +01:00
|
|
|
throw new ERR_UNKNOWN_BUILTIN_MODULE(id);
|
|
|
|
}
|
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
|
2018-08-28 17:28:46 +02:00
|
|
|
translators.set('json', async function jsonStrategy(url) {
|
2019-11-24 12:40:40 +08:00
|
|
|
emitExperimentalWarning('Importing JSON modules');
|
2017-11-26 17:12:09 -06:00
|
|
|
debug(`Translating JSONModule ${url}`);
|
2018-08-28 17:28:46 +02:00
|
|
|
debug(`Loading JSONModule ${url}`);
|
2019-07-09 16:03:07 -05:00
|
|
|
const pathname = url.startsWith('file:') ? fileURLToPath(url) : null;
|
|
|
|
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
|
|
|
}
|
2019-12-14 22:27:48 -05:00
|
|
|
let { source } = await this._getSource(
|
|
|
|
url, { format: 'json' }, defaultGetSource);
|
2020-03-11 10:38:00 -05:00
|
|
|
assertBufferSource(source, true, 'getSource');
|
2019-12-14 22:27:48 -05:00
|
|
|
({ source } = await this._transformSource(
|
|
|
|
source, { url, format: 'json' }, defaultTransformSource));
|
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
|
|
|
|
translators.set('wasm', async function(url) {
|
2019-11-24 12:40:40 +08:00
|
|
|
emitExperimentalWarning('Importing Web Assembly modules');
|
2019-12-14 22:27:48 -05:00
|
|
|
let { source } = await this._getSource(
|
|
|
|
url, { format: 'wasm' }, defaultGetSource);
|
2020-03-11 10:38:00 -05:00
|
|
|
assertBufferSource(source, false, 'getSource');
|
2019-12-14 22:27:48 -05:00
|
|
|
({ source } = await this._transformSource(
|
|
|
|
source, { url, format: 'wasm' }, defaultTransformSource));
|
2020-03-11 10:38:00 -05:00
|
|
|
assertBufferSource(source, false, 'transformSource');
|
2019-04-30 00:27:20 +08:00
|
|
|
debug(`Translating WASMModule ${url}`);
|
|
|
|
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 =
|
|
|
|
WebAssembly.Module.imports(compiled).map(({ module }) => module);
|
|
|
|
const exports = WebAssembly.Module.exports(compiled).map(({ name }) => name);
|
|
|
|
|
|
|
|
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
|
|
|
});
|