2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-07-18 21:52:55 -07:00
|
|
|
const {
|
2019-11-23 10:09:05 +01:00
|
|
|
ArrayIsArray,
|
2020-11-07 11:08:09 +01:00
|
|
|
ArrayPrototypeConcat,
|
|
|
|
ArrayPrototypeFilter,
|
|
|
|
ArrayPrototypeIncludes,
|
|
|
|
ArrayPrototypeIndexOf,
|
2020-07-16 09:27:53 +02:00
|
|
|
ArrayPrototypeJoin,
|
2020-11-07 11:08:09 +01:00
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeSlice,
|
|
|
|
ArrayPrototypeSplice,
|
2020-11-21 19:28:54 +01:00
|
|
|
ArrayPrototypeUnshift,
|
2021-02-05 16:17:57 +01:00
|
|
|
ArrayPrototypeUnshiftApply,
|
2022-07-08 01:12:32 -07:00
|
|
|
ArrayPrototypeFlatMap,
|
2020-11-07 11:08:09 +01:00
|
|
|
Boolean,
|
2020-01-02 19:19:02 +01:00
|
|
|
Error,
|
2019-11-22 18:04:46 +01:00
|
|
|
JSONParse,
|
|
|
|
ObjectCreate,
|
|
|
|
ObjectDefineProperty,
|
|
|
|
ObjectFreeze,
|
|
|
|
ObjectGetOwnPropertyDescriptor,
|
|
|
|
ObjectGetPrototypeOf,
|
|
|
|
ObjectKeys,
|
2020-09-27 17:39:01 +02:00
|
|
|
ObjectPrototype,
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectPrototypeHasOwnProperty,
|
|
|
|
ObjectSetPrototypeOf,
|
2021-04-26 18:03:54 +02:00
|
|
|
Proxy,
|
2020-11-07 11:08:09 +01:00
|
|
|
ReflectApply,
|
2019-11-22 18:04:46 +01:00
|
|
|
ReflectSet,
|
2021-06-27 15:29:02 -07:00
|
|
|
RegExpPrototypeExec,
|
2019-07-18 21:52:55 -07:00
|
|
|
SafeMap,
|
2022-07-08 01:12:32 -07:00
|
|
|
SafeSet,
|
2020-03-11 23:44:31 +01:00
|
|
|
SafeWeakMap,
|
2019-12-17 14:00:21 -05:00
|
|
|
String,
|
2020-11-07 11:08:09 +01:00
|
|
|
StringPrototypeCharAt,
|
|
|
|
StringPrototypeCharCodeAt,
|
2020-08-02 17:27:51 -04:00
|
|
|
StringPrototypeEndsWith,
|
|
|
|
StringPrototypeLastIndexOf,
|
2020-07-17 11:38:46 -05:00
|
|
|
StringPrototypeIndexOf,
|
2021-06-27 15:29:02 -07:00
|
|
|
StringPrototypeRepeat,
|
2019-11-22 18:04:46 +01:00
|
|
|
StringPrototypeSlice,
|
2020-11-07 11:08:09 +01:00
|
|
|
StringPrototypeSplit,
|
2019-11-22 18:04:46 +01:00
|
|
|
StringPrototypeStartsWith,
|
2019-07-18 21:52:55 -07:00
|
|
|
} = primordials;
|
2019-03-31 13:30:12 +02:00
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
// Map used to store CJS parsing data.
|
|
|
|
const cjsParseCache = new SafeWeakMap();
|
|
|
|
|
|
|
|
// Set first due to cycle with ESM loader functions.
|
|
|
|
module.exports = {
|
|
|
|
wrapSafe, Module, toRealPath, readPackageScope, cjsParseCache,
|
|
|
|
get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; }
|
|
|
|
};
|
|
|
|
|
2018-09-22 09:49:52 -05:00
|
|
|
const { NativeModule } = require('internal/bootstrap/loaders');
|
2019-09-29 14:15:39 -07:00
|
|
|
const {
|
|
|
|
maybeCacheSourceMap,
|
|
|
|
} = require('internal/source_map/source_map_cache');
|
2020-08-27 23:36:50 +02:00
|
|
|
const { pathToFileURL, fileURLToPath, isURLInstance } = require('internal/url');
|
2022-06-27 17:16:06 +02:00
|
|
|
const { deprecate, kEmptyObject } = require('internal/util');
|
2016-05-05 17:31:34 +02:00
|
|
|
const vm = require('vm');
|
2019-02-05 21:45:46 -08:00
|
|
|
const assert = require('internal/assert');
|
2015-02-12 14:50:47 +01:00
|
|
|
const fs = require('fs');
|
2018-05-03 14:40:48 -04:00
|
|
|
const internalFS = require('internal/fs/utils');
|
2015-02-12 14:50:47 +01:00
|
|
|
const path = require('path');
|
2020-08-02 17:27:51 -04:00
|
|
|
const { sep } = path;
|
2020-05-04 12:50:42 +03:00
|
|
|
const { internalModuleStat } = internalBinding('fs');
|
|
|
|
const packageJsonReader = require('internal/modules/package_json_reader');
|
2018-12-16 03:13:12 +08:00
|
|
|
const { safeGetenv } = internalBinding('credentials');
|
2018-03-07 02:30:18 +08:00
|
|
|
const {
|
2021-06-27 15:29:02 -07:00
|
|
|
cjsConditions,
|
|
|
|
hasEsmSyntax,
|
|
|
|
loadNativeModule,
|
2018-03-07 02:30:18 +08:00
|
|
|
makeRequireFunction,
|
2018-11-07 22:17:09 +05:30
|
|
|
normalizeReferrerURL,
|
2018-03-07 02:30:18 +08:00
|
|
|
stripBOM,
|
|
|
|
} = require('internal/modules/cjs/helpers');
|
2018-11-05 04:52:50 +08:00
|
|
|
const { getOptionValue } = require('internal/options');
|
|
|
|
const preserveSymlinks = getOptionValue('--preserve-symlinks');
|
|
|
|
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
|
2020-07-17 11:38:46 -05:00
|
|
|
// Do not eagerly grab .manifest, it may be in TDZ
|
|
|
|
const policy = getOptionValue('--experimental-policy') ?
|
|
|
|
require('internal/process/policy') :
|
2018-09-13 14:27:12 -05:00
|
|
|
null;
|
2019-11-10 16:59:16 +08:00
|
|
|
|
|
|
|
// Whether any user-provided CJS modules had been loaded (executed).
|
|
|
|
// Used for internal assertions.
|
|
|
|
let hasLoadedAnyUserCJSModule = false;
|
|
|
|
|
2018-02-27 14:55:32 +01:00
|
|
|
const {
|
2021-06-27 15:29:02 -07:00
|
|
|
codes: {
|
|
|
|
ERR_INVALID_ARG_VALUE,
|
|
|
|
ERR_INVALID_MODULE_SPECIFIER,
|
|
|
|
ERR_REQUIRE_ESM,
|
|
|
|
ERR_UNKNOWN_BUILTIN_MODULE,
|
|
|
|
},
|
|
|
|
setArrowMessage,
|
|
|
|
} = require('internal/errors');
|
2018-12-06 13:50:41 +08:00
|
|
|
const { validateString } = require('internal/validators');
|
2019-03-20 17:00:57 +01:00
|
|
|
const pendingDeprecation = getOptionValue('--pending-deprecation');
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2018-02-14 20:52:00 +03:00
|
|
|
const {
|
|
|
|
CHAR_FORWARD_SLASH,
|
|
|
|
CHAR_BACKWARD_SLASH,
|
2019-03-29 14:46:53 +01:00
|
|
|
CHAR_COLON
|
2018-02-14 20:52:00 +03:00
|
|
|
} = require('internal/constants');
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2020-05-10 13:49:23 +02:00
|
|
|
const {
|
|
|
|
isProxy
|
|
|
|
} = require('internal/util/types');
|
|
|
|
|
2020-06-27 22:09:24 -07:00
|
|
|
const asyncESM = require('internal/process/esm_loader');
|
2020-09-30 04:24:34 -07:00
|
|
|
const { enrichCJSError } = require('internal/modules/esm/translators');
|
2020-08-02 18:03:48 -07:00
|
|
|
const { kEvaluated } = internalBinding('module_wrap');
|
2020-06-27 22:09:24 -07:00
|
|
|
const {
|
|
|
|
encodedSepRegEx,
|
2020-08-09 16:54:01 -07:00
|
|
|
packageExportsResolve,
|
|
|
|
packageImportsResolve
|
2020-06-27 22:09:24 -07:00
|
|
|
} = require('internal/modules/esm/resolve');
|
|
|
|
|
2018-08-08 03:58:06 +01:00
|
|
|
const isWindows = process.platform === 'win32';
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
const relativeResolveCache = ObjectCreate(null);
|
2019-03-28 23:37:07 +01:00
|
|
|
|
2019-02-22 18:52:38 +01:00
|
|
|
let requireDepth = 0;
|
2019-03-26 16:28:53 +01:00
|
|
|
let statCache = null;
|
2020-11-25 07:54:32 -08:00
|
|
|
let isPreloading = false;
|
2019-07-27 20:23:25 +02:00
|
|
|
|
2016-01-07 22:36:30 +01:00
|
|
|
function stat(filename) {
|
2017-08-20 22:44:47 -07:00
|
|
|
filename = path.toNamespacedPath(filename);
|
2019-03-26 16:28:53 +01:00
|
|
|
if (statCache !== null) {
|
|
|
|
const result = statCache.get(filename);
|
|
|
|
if (result !== undefined) return result;
|
|
|
|
}
|
|
|
|
const result = internalModuleStat(filename);
|
2021-01-17 21:18:45 +08:00
|
|
|
if (statCache !== null && result >= 0) {
|
|
|
|
// Only set cache when `internalModuleStat(filename)` succeeds.
|
|
|
|
statCache.set(filename, result);
|
|
|
|
}
|
2016-01-07 22:36:30 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-07-08 13:06:02 +02:00
|
|
|
function updateChildren(parent, child, scan) {
|
2021-02-05 19:44:44 +05:30
|
|
|
const children = parent?.children;
|
2020-11-07 11:08:09 +01:00
|
|
|
if (children && !(scan && ArrayPrototypeIncludes(children, child)))
|
|
|
|
ArrayPrototypePush(children, child);
|
2017-07-08 13:06:02 +02:00
|
|
|
}
|
2016-01-07 22:36:30 +01:00
|
|
|
|
2020-03-11 23:44:31 +01:00
|
|
|
const moduleParentCache = new SafeWeakMap();
|
2019-03-28 23:34:24 +01:00
|
|
|
function Module(id = '', parent) {
|
2011-01-23 23:40:56 +01:00
|
|
|
this.id = id;
|
2019-03-28 23:34:24 +01:00
|
|
|
this.path = path.dirname(id);
|
2011-01-23 23:40:56 +01:00
|
|
|
this.exports = {};
|
2020-03-11 23:44:31 +01:00
|
|
|
moduleParentCache.set(this, parent);
|
2017-07-08 13:06:02 +02:00
|
|
|
updateChildren(parent, this, false);
|
2011-01-23 23:40:56 +01:00
|
|
|
this.filename = null;
|
|
|
|
this.loaded = false;
|
|
|
|
this.children = [];
|
2011-01-24 10:55:30 -08:00
|
|
|
}
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2019-01-05 06:02:33 +08:00
|
|
|
const builtinModules = [];
|
2021-01-06 19:53:27 +01:00
|
|
|
for (const { 0: id, 1: mod } of NativeModule.map) {
|
2022-03-19 23:27:07 -04:00
|
|
|
if (mod.canBeRequiredByUsers &&
|
|
|
|
NativeModule.canBeRequiredWithoutScheme(id)) {
|
2020-11-07 11:08:09 +01:00
|
|
|
ArrayPrototypePush(builtinModules, id);
|
2019-01-05 06:02:33 +08:00
|
|
|
}
|
|
|
|
}
|
2017-11-24 16:29:38 -05:00
|
|
|
|
2022-07-08 01:12:32 -07:00
|
|
|
const allBuiltins = new SafeSet(
|
|
|
|
ArrayPrototypeFlatMap(builtinModules, (bm) => [bm, `node:${bm}`])
|
|
|
|
);
|
|
|
|
NativeModule.getSchemeOnlyModuleNames().forEach((builtin) => allBuiltins.add(`node:${builtin}`));
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectFreeze(builtinModules);
|
2017-11-24 16:29:38 -05:00
|
|
|
Module.builtinModules = builtinModules;
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
Module._cache = ObjectCreate(null);
|
|
|
|
Module._pathCache = ObjectCreate(null);
|
|
|
|
Module._extensions = ObjectCreate(null);
|
2019-11-12 16:03:15 +00:00
|
|
|
let modulePaths = [];
|
2011-07-15 14:05:01 -07:00
|
|
|
Module.globalPaths = [];
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2018-11-07 22:17:09 +05:30
|
|
|
let patched = false;
|
|
|
|
|
|
|
|
// eslint-disable-next-line func-style
|
|
|
|
let wrap = function(script) {
|
2017-10-15 12:10:20 +02:00
|
|
|
return Module.wrapper[0] + script + Module.wrapper[1];
|
|
|
|
};
|
|
|
|
|
2018-11-07 22:17:09 +05:30
|
|
|
const wrapper = [
|
2017-10-15 12:10:20 +02:00
|
|
|
'(function (exports, require, module, __filename, __dirname) { ',
|
2021-02-06 05:36:58 -08:00
|
|
|
'\n});',
|
2017-10-15 12:10:20 +02:00
|
|
|
];
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2018-11-07 22:17:09 +05:30
|
|
|
let wrapperProxy = new Proxy(wrapper, {
|
2022-06-12 13:52:43 +02:00
|
|
|
__proto__: null,
|
|
|
|
|
2018-11-07 22:17:09 +05:30
|
|
|
set(target, property, value, receiver) {
|
|
|
|
patched = true;
|
2019-11-22 18:04:46 +01:00
|
|
|
return ReflectSet(target, property, value, receiver);
|
2018-11-07 22:17:09 +05:30
|
|
|
},
|
|
|
|
|
|
|
|
defineProperty(target, property, descriptor) {
|
|
|
|
patched = true;
|
2019-11-22 18:04:46 +01:00
|
|
|
return ObjectDefineProperty(target, property, descriptor);
|
2018-11-07 22:17:09 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectDefineProperty(Module, 'wrap', {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2018-11-07 22:17:09 +05:30
|
|
|
get() {
|
|
|
|
return wrap;
|
|
|
|
},
|
|
|
|
|
|
|
|
set(value) {
|
|
|
|
patched = true;
|
|
|
|
wrap = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectDefineProperty(Module, 'wrapper', {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2018-11-07 22:17:09 +05:30
|
|
|
get() {
|
|
|
|
return wrapperProxy;
|
|
|
|
},
|
|
|
|
|
|
|
|
set(value) {
|
|
|
|
patched = true;
|
|
|
|
wrapperProxy = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-15 14:53:12 +01:00
|
|
|
const isPreloadingDesc = { get() { return isPreloading; } };
|
|
|
|
ObjectDefineProperty(Module.prototype, 'isPreloading', isPreloadingDesc);
|
|
|
|
ObjectDefineProperty(NativeModule.prototype, 'isPreloading', isPreloadingDesc);
|
2020-11-25 07:54:32 -08:00
|
|
|
|
2020-03-11 23:44:31 +01:00
|
|
|
function getModuleParent() {
|
|
|
|
return moduleParentCache.get(this);
|
|
|
|
}
|
2020-10-06 17:50:20 +02:00
|
|
|
|
|
|
|
function setModuleParent(value) {
|
|
|
|
moduleParentCache.set(this, value);
|
|
|
|
}
|
|
|
|
|
2020-03-11 23:44:31 +01:00
|
|
|
ObjectDefineProperty(Module.prototype, 'parent', {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-03-11 23:44:31 +01:00
|
|
|
get: pendingDeprecation ? deprecate(
|
|
|
|
getModuleParent,
|
|
|
|
'module.parent is deprecated due to accuracy issues. Please use ' +
|
|
|
|
'require.main to find program entry point instead.',
|
2020-07-14 20:20:08 +01:00
|
|
|
'DEP0144'
|
2020-10-06 17:50:20 +02:00
|
|
|
) : getModuleParent,
|
|
|
|
set: pendingDeprecation ? deprecate(
|
|
|
|
setModuleParent,
|
|
|
|
'module.parent is deprecated due to accuracy issues. Please use ' +
|
|
|
|
'require.main to find program entry point instead.',
|
|
|
|
'DEP0144'
|
|
|
|
) : setModuleParent,
|
2020-03-11 23:44:31 +01:00
|
|
|
});
|
|
|
|
|
2020-03-14 07:55:44 -04:00
|
|
|
let debug = require('internal/util/debuglog').debuglog('module', (fn) => {
|
|
|
|
debug = fn;
|
|
|
|
});
|
2019-03-21 16:45:59 +01:00
|
|
|
Module._debug = deprecate(debug, 'Module._debug is deprecated.', 'DEP0077');
|
2011-01-24 10:55:30 -08:00
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
// Given a module name, and a list of paths to test, returns the first
|
2011-01-23 23:40:56 +01:00
|
|
|
// matching file in the following precedence.
|
|
|
|
//
|
|
|
|
// require("a.<ext>")
|
|
|
|
// -> a.<ext>
|
|
|
|
//
|
|
|
|
// require("a")
|
|
|
|
// -> a
|
|
|
|
// -> a.<ext>
|
|
|
|
// -> a/index.<ext>
|
2011-01-27 17:55:33 -08:00
|
|
|
|
2019-09-08 11:20:43 -04:00
|
|
|
const packageJsonCache = new SafeMap();
|
2011-01-27 17:57:45 -08:00
|
|
|
|
2019-09-08 11:20:43 -04:00
|
|
|
function readPackage(requestPath) {
|
2016-04-07 10:04:47 -04:00
|
|
|
const jsonPath = path.resolve(requestPath, 'package.json');
|
2015-05-26 16:50:16 +02:00
|
|
|
|
2019-09-08 11:20:43 -04:00
|
|
|
const existing = packageJsonCache.get(jsonPath);
|
|
|
|
if (existing !== undefined) return existing;
|
|
|
|
|
2020-07-16 16:00:21 -05:00
|
|
|
const result = packageJsonReader.read(jsonPath);
|
2020-05-04 12:50:42 +03:00
|
|
|
const json = result.containsKeys === false ? '{}' : result.string;
|
2015-05-26 16:50:16 +02:00
|
|
|
if (json === undefined) {
|
2019-09-08 11:20:43 -04:00
|
|
|
packageJsonCache.set(jsonPath, false);
|
2011-09-23 09:25:20 -04:00
|
|
|
return false;
|
|
|
|
}
|
2011-01-27 17:57:45 -08:00
|
|
|
|
2011-09-23 09:25:20 -04:00
|
|
|
try {
|
2019-11-22 18:04:46 +01:00
|
|
|
const parsed = JSONParse(json);
|
2019-09-08 11:20:43 -04:00
|
|
|
const filtered = {
|
2019-08-19 20:59:25 -07:00
|
|
|
name: parsed.name,
|
2019-09-08 11:20:43 -04:00
|
|
|
main: parsed.main,
|
|
|
|
exports: parsed.exports,
|
2020-06-27 22:09:24 -07:00
|
|
|
imports: parsed.imports,
|
2019-09-08 11:20:43 -04:00
|
|
|
type: parsed.type
|
|
|
|
};
|
|
|
|
packageJsonCache.set(jsonPath, filtered);
|
|
|
|
return filtered;
|
2011-09-23 09:25:20 -04:00
|
|
|
} catch (e) {
|
|
|
|
e.path = jsonPath;
|
|
|
|
e.message = 'Error parsing ' + jsonPath + ': ' + e.message;
|
|
|
|
throw e;
|
|
|
|
}
|
2011-01-27 17:57:45 -08:00
|
|
|
}
|
|
|
|
|
2019-09-08 11:20:43 -04:00
|
|
|
function readPackageScope(checkPath) {
|
2020-08-02 17:27:51 -04:00
|
|
|
const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep);
|
2019-09-08 11:20:43 -04:00
|
|
|
let separatorIndex;
|
2020-08-02 17:27:51 -04:00
|
|
|
do {
|
|
|
|
separatorIndex = StringPrototypeLastIndexOf(checkPath, sep);
|
|
|
|
checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex);
|
|
|
|
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
|
2019-09-08 11:20:43 -04:00
|
|
|
return false;
|
2020-08-02 17:27:51 -04:00
|
|
|
const pjson = readPackage(checkPath + sep);
|
2019-10-09 17:21:31 -04:00
|
|
|
if (pjson) return {
|
2020-08-02 17:27:51 -04:00
|
|
|
data: pjson,
|
2019-10-09 17:21:31 -04:00
|
|
|
path: checkPath,
|
|
|
|
};
|
2020-08-02 17:27:51 -04:00
|
|
|
} while (separatorIndex > rootSeparatorIndex);
|
2019-09-08 11:20:43 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-07-18 21:52:55 -07:00
|
|
|
|
2019-03-20 17:00:57 +01:00
|
|
|
function tryPackage(requestPath, exts, isMain, originalPath) {
|
2020-08-09 16:54:01 -07:00
|
|
|
const pkg = readPackage(requestPath)?.main;
|
2011-01-27 17:57:45 -08:00
|
|
|
|
2019-03-20 17:00:57 +01:00
|
|
|
if (!pkg) {
|
|
|
|
return tryExtensions(path.resolve(requestPath, 'index'), exts, isMain);
|
|
|
|
}
|
2011-01-27 17:57:45 -08:00
|
|
|
|
2019-03-20 17:00:57 +01:00
|
|
|
const filename = path.resolve(requestPath, pkg);
|
|
|
|
let actual = tryFile(filename, isMain) ||
|
|
|
|
tryExtensions(filename, exts, isMain) ||
|
|
|
|
tryExtensions(path.resolve(filename, 'index'), exts, isMain);
|
|
|
|
if (actual === false) {
|
|
|
|
actual = tryExtensions(path.resolve(requestPath, 'index'), exts, isMain);
|
|
|
|
if (!actual) {
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
const err = new Error(
|
|
|
|
`Cannot find module '${filename}'. ` +
|
|
|
|
'Please verify that the package.json has a valid "main" entry'
|
|
|
|
);
|
|
|
|
err.code = 'MODULE_NOT_FOUND';
|
|
|
|
err.path = path.resolve(requestPath, 'package.json');
|
|
|
|
err.requestPath = originalPath;
|
|
|
|
// TODO(BridgeAR): Add the requireStack as well.
|
|
|
|
throw err;
|
2021-02-03 14:19:24 +01:00
|
|
|
} else {
|
2019-03-20 17:00:57 +01:00
|
|
|
const jsonPath = path.resolve(requestPath, 'package.json');
|
|
|
|
process.emitWarning(
|
|
|
|
`Invalid 'main' field in '${jsonPath}' of '${pkg}'. ` +
|
|
|
|
'Please either fix that or report it to the module author',
|
|
|
|
'DeprecationWarning',
|
|
|
|
'DEP0128'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return actual;
|
2011-01-27 17:57:45 -08:00
|
|
|
}
|
|
|
|
|
2016-08-14 18:41:28 +02:00
|
|
|
// In order to minimize unnecessary lstat() calls,
|
|
|
|
// this cache is a list of known-real paths.
|
|
|
|
// Set to an empty Map to reset.
|
2020-11-07 11:08:09 +01:00
|
|
|
const realpathCache = new SafeMap();
|
2016-08-14 18:41:28 +02:00
|
|
|
|
2019-01-21 01:22:27 +01:00
|
|
|
// Check if the file exists and is not a directory
|
2016-05-02 16:31:20 -07:00
|
|
|
// if using --preserve-symlinks and isMain is false,
|
|
|
|
// keep symlinks intact, otherwise resolve to the
|
|
|
|
// absolute realpath.
|
2016-03-29 23:28:35 -04:00
|
|
|
function tryFile(requestPath, isMain) {
|
2016-01-07 22:36:30 +01:00
|
|
|
const rc = stat(requestPath);
|
2020-03-19 01:49:49 +02:00
|
|
|
if (rc !== 0) return;
|
2016-05-02 16:31:20 -07:00
|
|
|
if (preserveSymlinks && !isMain) {
|
2020-03-19 01:49:49 +02:00
|
|
|
return path.resolve(requestPath);
|
2016-03-29 23:28:35 -04:00
|
|
|
}
|
2020-03-19 01:49:49 +02:00
|
|
|
return toRealPath(requestPath);
|
2016-08-14 18:41:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function toRealPath(requestPath) {
|
|
|
|
return fs.realpathSync(requestPath, {
|
2016-09-30 16:16:28 +02:00
|
|
|
[internalFS.realpathCacheKey]: realpathCache
|
2016-08-14 18:41:28 +02:00
|
|
|
});
|
2011-01-27 17:55:33 -08:00
|
|
|
}
|
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
// Given a path, check if the file exists with any of the set extensions
|
2016-03-29 23:28:35 -04:00
|
|
|
function tryExtensions(p, exts, isMain) {
|
2019-11-12 16:03:15 +00:00
|
|
|
for (let i = 0; i < exts.length; i++) {
|
2016-03-29 23:28:35 -04:00
|
|
|
const filename = tryFile(p + exts[i], isMain);
|
2011-01-27 17:55:33 -08:00
|
|
|
|
|
|
|
if (filename) {
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-10 13:27:32 +01:00
|
|
|
// Find the longest (possibly multi-dot) extension registered in
|
2018-10-01 22:12:47 -07:00
|
|
|
// Module._extensions
|
|
|
|
function findLongestRegisteredExtension(filename) {
|
|
|
|
const name = path.basename(filename);
|
|
|
|
let currentExtension;
|
|
|
|
let index;
|
|
|
|
let startIndex = 0;
|
2020-11-07 11:08:09 +01:00
|
|
|
while ((index = StringPrototypeIndexOf(name, '.', startIndex)) !== -1) {
|
2018-10-01 22:12:47 -07:00
|
|
|
startIndex = index + 1;
|
|
|
|
if (index === 0) continue; // Skip dotfiles like .gitignore
|
2020-11-07 11:08:09 +01:00
|
|
|
currentExtension = StringPrototypeSlice(name, index);
|
2018-10-01 22:12:47 -07:00
|
|
|
if (Module._extensions[currentExtension]) return currentExtension;
|
|
|
|
}
|
|
|
|
return '.js';
|
|
|
|
}
|
|
|
|
|
2020-07-16 09:27:53 +02:00
|
|
|
function trySelfParentPath(parent) {
|
|
|
|
if (!parent) return false;
|
|
|
|
|
|
|
|
if (parent.filename) {
|
|
|
|
return parent.filename;
|
|
|
|
} else if (parent.id === '<repl>' || parent.id === 'internal/preload') {
|
|
|
|
try {
|
|
|
|
return process.cwd() + path.sep;
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 01:49:49 +02:00
|
|
|
function trySelf(parentPath, request) {
|
2020-07-16 09:27:53 +02:00
|
|
|
if (!parentPath) return false;
|
|
|
|
|
2020-08-09 16:54:01 -07:00
|
|
|
const { data: pkg, path: pkgPath } = readPackageScope(parentPath) || {};
|
2020-01-20 20:55:45 +02:00
|
|
|
if (!pkg || pkg.exports === undefined) return false;
|
2019-08-19 20:59:25 -07:00
|
|
|
if (typeof pkg.name !== 'string') return false;
|
|
|
|
|
|
|
|
let expansion;
|
|
|
|
if (request === pkg.name) {
|
2020-08-09 16:54:01 -07:00
|
|
|
expansion = '.';
|
2019-11-22 18:04:46 +01:00
|
|
|
} else if (StringPrototypeStartsWith(request, `${pkg.name}/`)) {
|
2020-08-09 16:54:01 -07:00
|
|
|
expansion = '.' + StringPrototypeSlice(request, pkg.name.length);
|
2019-08-19 20:59:25 -07:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-09 16:54:01 -07:00
|
|
|
try {
|
|
|
|
return finalizeEsmResolution(packageExportsResolve(
|
|
|
|
pathToFileURL(pkgPath + '/package.json'), expansion, pkg,
|
2021-12-22 19:43:19 +01:00
|
|
|
pathToFileURL(parentPath), cjsConditions), parentPath, pkgPath);
|
2020-08-09 16:54:01 -07:00
|
|
|
} catch (e) {
|
|
|
|
if (e.code === 'ERR_MODULE_NOT_FOUND')
|
|
|
|
throw createEsmNotFoundErr(request, pkgPath + '/package.json');
|
|
|
|
throw e;
|
2019-08-19 20:59:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 21:52:55 -07:00
|
|
|
// This only applies to requests of a specific form:
|
|
|
|
// 1. name/.*
|
|
|
|
// 2. @scope/name/.*
|
2019-09-08 12:54:19 -04:00
|
|
|
const EXPORTS_PATTERN = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/;
|
2020-03-19 01:49:49 +02:00
|
|
|
function resolveExports(nmPath, request) {
|
2019-07-18 21:52:55 -07:00
|
|
|
// The implementation's behavior is meant to mirror resolution in ESM.
|
2020-12-06 12:04:41 +01:00
|
|
|
const { 1: name, 2: expansion = '' } =
|
2022-06-27 17:16:06 +02:00
|
|
|
RegExpPrototypeExec(EXPORTS_PATTERN, request) || kEmptyObject;
|
2020-08-09 16:54:01 -07:00
|
|
|
if (!name)
|
|
|
|
return;
|
|
|
|
const pkgPath = path.resolve(nmPath, name);
|
|
|
|
const pkg = readPackage(pkgPath);
|
2021-02-05 19:44:44 +05:30
|
|
|
if (pkg?.exports != null) {
|
2020-08-09 16:54:01 -07:00
|
|
|
try {
|
|
|
|
return finalizeEsmResolution(packageExportsResolve(
|
|
|
|
pathToFileURL(pkgPath + '/package.json'), '.' + expansion, pkg, null,
|
2021-12-22 19:43:19 +01:00
|
|
|
cjsConditions), null, pkgPath);
|
2020-08-09 16:54:01 -07:00
|
|
|
} catch (e) {
|
|
|
|
if (e.code === 'ERR_MODULE_NOT_FOUND')
|
|
|
|
throw createEsmNotFoundErr(request, pkgPath + '/package.json');
|
|
|
|
throw e;
|
2019-10-13 19:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-02 01:30:32 -04:00
|
|
|
}
|
|
|
|
|
2020-03-19 01:49:49 +02:00
|
|
|
const trailingSlashRegex = /(?:^|\/)\.?\.$/;
|
2016-03-29 23:28:35 -04:00
|
|
|
Module._findPath = function(request, paths, isMain) {
|
2019-07-18 21:52:55 -07:00
|
|
|
const absoluteRequest = path.isAbsolute(request);
|
|
|
|
if (absoluteRequest) {
|
2011-01-23 23:40:56 +01:00
|
|
|
paths = [''];
|
2016-04-07 10:04:47 -04:00
|
|
|
} else if (!paths || paths.length === 0) {
|
|
|
|
return false;
|
2011-01-23 23:40:56 +01:00
|
|
|
}
|
|
|
|
|
2020-11-07 11:08:09 +01:00
|
|
|
const cacheKey = request + '\x00' + ArrayPrototypeJoin(paths, '\x00');
|
2019-03-26 05:21:27 +01:00
|
|
|
const entry = Module._pathCache[cacheKey];
|
2017-01-13 05:19:54 -05:00
|
|
|
if (entry)
|
|
|
|
return entry;
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2019-11-12 16:03:15 +00:00
|
|
|
let exts;
|
|
|
|
let trailingSlash = request.length > 0 &&
|
2020-11-07 11:08:09 +01:00
|
|
|
StringPrototypeCharCodeAt(request, request.length - 1) ===
|
|
|
|
CHAR_FORWARD_SLASH;
|
2017-08-24 09:59:52 -05:00
|
|
|
if (!trailingSlash) {
|
2022-06-27 17:16:06 +02:00
|
|
|
trailingSlash = RegExpPrototypeExec(trailingSlashRegex, request) !== null;
|
2017-08-24 09:59:52 -05:00
|
|
|
}
|
2015-10-28 20:09:38 -07:00
|
|
|
|
2011-01-23 23:40:56 +01:00
|
|
|
// For each path
|
2019-11-12 16:03:15 +00:00
|
|
|
for (let i = 0; i < paths.length; i++) {
|
2015-06-08 23:44:18 +02:00
|
|
|
// Don't search further if path doesn't exist
|
2016-04-07 10:04:47 -04:00
|
|
|
const curPath = paths[i];
|
|
|
|
if (curPath && stat(curPath) < 1) continue;
|
2020-03-19 01:49:49 +02:00
|
|
|
|
|
|
|
if (!absoluteRequest) {
|
|
|
|
const exportsResolved = resolveExports(curPath, request);
|
2020-08-09 16:54:01 -07:00
|
|
|
if (exportsResolved)
|
2020-03-19 01:49:49 +02:00
|
|
|
return exportsResolved;
|
|
|
|
}
|
|
|
|
|
|
|
|
const basePath = path.resolve(curPath, request);
|
2019-11-12 16:03:15 +00:00
|
|
|
let filename;
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2019-11-12 16:03:15 +00:00
|
|
|
const rc = stat(basePath);
|
2011-01-27 13:14:47 -08:00
|
|
|
if (!trailingSlash) {
|
2015-05-26 13:23:26 +02:00
|
|
|
if (rc === 0) { // File.
|
2018-04-10 07:40:56 +00:00
|
|
|
if (!isMain) {
|
|
|
|
if (preserveSymlinks) {
|
|
|
|
filename = path.resolve(basePath);
|
|
|
|
} else {
|
|
|
|
filename = toRealPath(basePath);
|
|
|
|
}
|
|
|
|
} else if (preserveSymlinksMain) {
|
|
|
|
// For the main module, we use the preserveSymlinksMain flag instead
|
|
|
|
// mainly for backward compatibility, as the preserveSymlinks flag
|
|
|
|
// historically has not applied to the main module. Most likely this
|
|
|
|
// was intended to keep .bin/ binaries working, as following those
|
|
|
|
// symlinks is usually required for the imports in the corresponding
|
|
|
|
// files to resolve; that said, in some use cases following symlinks
|
|
|
|
// causes bigger problems which is why the preserveSymlinksMain option
|
|
|
|
// is needed.
|
2016-03-29 23:28:35 -04:00
|
|
|
filename = path.resolve(basePath);
|
|
|
|
} else {
|
2016-08-14 18:41:28 +02:00
|
|
|
filename = toRealPath(basePath);
|
2016-03-29 23:28:35 -04:00
|
|
|
}
|
2014-02-10 21:14:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!filename) {
|
2019-03-07 01:03:53 +01:00
|
|
|
// Try it with each of the extensions
|
2016-04-07 10:04:47 -04:00
|
|
|
if (exts === undefined)
|
2019-11-22 18:04:46 +01:00
|
|
|
exts = ObjectKeys(Module._extensions);
|
2016-03-29 23:28:35 -04:00
|
|
|
filename = tryExtensions(basePath, exts, isMain);
|
2011-01-27 13:14:47 -08:00
|
|
|
}
|
2011-01-23 23:40:56 +01:00
|
|
|
}
|
|
|
|
|
2016-10-19 21:43:36 +02:00
|
|
|
if (!filename && rc === 1) { // Directory.
|
2017-08-24 09:59:52 -05:00
|
|
|
// try it with each of the extensions at "index"
|
2016-04-15 13:32:36 +02:00
|
|
|
if (exts === undefined)
|
2019-11-22 18:04:46 +01:00
|
|
|
exts = ObjectKeys(Module._extensions);
|
2019-03-20 17:00:57 +01:00
|
|
|
filename = tryPackage(basePath, exts, isMain, request);
|
2011-01-23 23:40:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (filename) {
|
|
|
|
Module._pathCache[cacheKey] = filename;
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 20:59:25 -07:00
|
|
|
|
2011-01-23 23:40:56 +01:00
|
|
|
return false;
|
2011-01-24 10:55:30 -08:00
|
|
|
};
|
|
|
|
|
2016-04-07 10:04:47 -04:00
|
|
|
// 'node_modules' character codes reversed
|
2019-03-26 05:21:27 +01:00
|
|
|
const nmChars = [ 115, 101, 108, 117, 100, 111, 109, 95, 101, 100, 111, 110 ];
|
|
|
|
const nmLen = nmChars.length;
|
2018-08-08 03:58:06 +01:00
|
|
|
if (isWindows) {
|
2016-04-07 10:04:47 -04:00
|
|
|
// 'from' is the __dirname of the module.
|
|
|
|
Module._nodeModulePaths = function(from) {
|
2019-03-07 01:03:53 +01:00
|
|
|
// Guarantee that 'from' is absolute.
|
2016-04-07 10:04:47 -04:00
|
|
|
from = path.resolve(from);
|
|
|
|
|
|
|
|
// note: this approach *only* works when the path is guaranteed
|
|
|
|
// to be absolute. Doing a fully-edge-case-correct path.split
|
|
|
|
// that works on both Windows and Posix is non-trivial.
|
2016-05-10 16:16:49 +08:00
|
|
|
|
|
|
|
// return root node_modules when path is 'D:\\'.
|
|
|
|
// path.resolve will make sure from.length >=3 in Windows.
|
2020-11-07 11:08:09 +01:00
|
|
|
if (StringPrototypeCharCodeAt(from, from.length - 1) ===
|
|
|
|
CHAR_BACKWARD_SLASH &&
|
|
|
|
StringPrototypeCharCodeAt(from, from.length - 2) === CHAR_COLON)
|
2016-05-10 16:16:49 +08:00
|
|
|
return [from + 'node_modules'];
|
|
|
|
|
2016-04-07 10:04:47 -04:00
|
|
|
const paths = [];
|
2019-11-12 16:03:15 +00:00
|
|
|
for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) {
|
2020-11-07 11:08:09 +01:00
|
|
|
const code = StringPrototypeCharCodeAt(from, i);
|
2016-05-10 16:16:49 +08:00
|
|
|
// The path segment separator check ('\' and '/') was used to get
|
|
|
|
// node_modules path for every path segment.
|
|
|
|
// Use colon as an extra condition since we can get node_modules
|
2017-04-20 09:05:43 +01:00
|
|
|
// path for drive root like 'C:\node_modules' and don't need to
|
|
|
|
// parse drive name.
|
2018-02-14 20:52:00 +03:00
|
|
|
if (code === CHAR_BACKWARD_SLASH ||
|
|
|
|
code === CHAR_FORWARD_SLASH ||
|
|
|
|
code === CHAR_COLON) {
|
2016-04-07 10:04:47 -04:00
|
|
|
if (p !== nmLen)
|
2020-11-07 11:08:09 +01:00
|
|
|
ArrayPrototypePush(
|
|
|
|
paths,
|
|
|
|
StringPrototypeSlice(from, 0, last) + '\\node_modules'
|
|
|
|
);
|
2016-04-07 10:04:47 -04:00
|
|
|
last = i;
|
|
|
|
p = 0;
|
2016-05-10 16:16:49 +08:00
|
|
|
} else if (p !== -1) {
|
2016-04-07 10:04:47 -04:00
|
|
|
if (nmChars[p] === code) {
|
|
|
|
++p;
|
|
|
|
} else {
|
|
|
|
p = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-09 13:28:30 -08:00
|
|
|
|
2016-04-07 10:04:47 -04:00
|
|
|
return paths;
|
|
|
|
};
|
|
|
|
} else { // posix
|
|
|
|
// 'from' is the __dirname of the module.
|
|
|
|
Module._nodeModulePaths = function(from) {
|
2019-03-07 01:03:53 +01:00
|
|
|
// Guarantee that 'from' is absolute.
|
2016-04-07 10:04:47 -04:00
|
|
|
from = path.resolve(from);
|
|
|
|
// Return early not only to avoid unnecessary work, but to *avoid* returning
|
|
|
|
// an array of two items for a root: [ '//node_modules', '/node_modules' ]
|
|
|
|
if (from === '/')
|
|
|
|
return ['/node_modules'];
|
|
|
|
|
|
|
|
// note: this approach *only* works when the path is guaranteed
|
|
|
|
// to be absolute. Doing a fully-edge-case-correct path.split
|
|
|
|
// that works on both Windows and Posix is non-trivial.
|
|
|
|
const paths = [];
|
2019-11-12 16:03:15 +00:00
|
|
|
for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) {
|
2020-11-07 11:08:09 +01:00
|
|
|
const code = StringPrototypeCharCodeAt(from, i);
|
2018-02-14 20:52:00 +03:00
|
|
|
if (code === CHAR_FORWARD_SLASH) {
|
2016-04-07 10:04:47 -04:00
|
|
|
if (p !== nmLen)
|
2020-11-07 11:08:09 +01:00
|
|
|
ArrayPrototypePush(
|
|
|
|
paths,
|
|
|
|
StringPrototypeSlice(from, 0, last) + '/node_modules'
|
|
|
|
);
|
2016-04-07 10:04:47 -04:00
|
|
|
last = i;
|
|
|
|
p = 0;
|
2016-05-10 16:16:49 +08:00
|
|
|
} else if (p !== -1) {
|
2016-04-07 10:04:47 -04:00
|
|
|
if (nmChars[p] === code) {
|
|
|
|
++p;
|
|
|
|
} else {
|
|
|
|
p = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 16:16:49 +08:00
|
|
|
// Append /node_modules to handle root paths.
|
2020-11-07 11:08:09 +01:00
|
|
|
ArrayPrototypePush(paths, '/node_modules');
|
2016-05-10 16:16:49 +08:00
|
|
|
|
2016-04-07 10:04:47 -04:00
|
|
|
return paths;
|
|
|
|
};
|
|
|
|
}
|
2011-02-09 13:28:30 -08:00
|
|
|
|
2019-03-29 14:46:53 +01:00
|
|
|
Module._resolveLookupPaths = function(request, parent) {
|
2022-04-14 09:08:55 -04:00
|
|
|
if (NativeModule.canBeRequiredByUsers(request) &&
|
|
|
|
NativeModule.canBeRequiredWithoutScheme(request)) {
|
2017-01-13 05:22:27 -05:00
|
|
|
debug('looking for %j in []', request);
|
2019-03-29 14:46:53 +01:00
|
|
|
return null;
|
2011-01-23 23:40:56 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 03:11:41 +01:00
|
|
|
// Check for node modules paths.
|
2020-11-07 11:08:09 +01:00
|
|
|
if (StringPrototypeCharAt(request, 0) !== '.' ||
|
2019-03-29 03:11:41 +01:00
|
|
|
(request.length > 1 &&
|
2020-11-07 11:08:09 +01:00
|
|
|
StringPrototypeCharAt(request, 1) !== '.' &&
|
|
|
|
StringPrototypeCharAt(request, 1) !== '/' &&
|
|
|
|
(!isWindows || StringPrototypeCharAt(request, 1) !== '\\'))) {
|
2015-04-07 16:30:28 +02:00
|
|
|
|
2019-03-29 03:11:41 +01:00
|
|
|
let paths = modulePaths;
|
2021-02-05 19:44:44 +05:30
|
|
|
if (parent?.paths?.length) {
|
2020-11-07 11:08:09 +01:00
|
|
|
paths = ArrayPrototypeConcat(parent.paths, paths);
|
2017-10-31 01:51:01 -04:00
|
|
|
}
|
|
|
|
|
2017-01-13 05:22:27 -05:00
|
|
|
debug('looking for %j in %j', request, paths);
|
2019-03-29 14:46:53 +01:00
|
|
|
return paths.length > 0 ? paths : null;
|
2011-01-23 23:40:56 +01:00
|
|
|
}
|
|
|
|
|
2019-12-07 21:19:57 +08:00
|
|
|
// In REPL, parent.filename is null.
|
2011-01-23 23:40:56 +01:00
|
|
|
if (!parent || !parent.id || !parent.filename) {
|
2018-12-03 17:15:45 +01:00
|
|
|
// Make require('./path/to/foo') work - normally the path is taken
|
2019-12-07 21:19:57 +08:00
|
|
|
// from realpath(__filename) but in REPL there is no filename
|
|
|
|
const mainPaths = ['.'];
|
2017-01-13 05:22:27 -05:00
|
|
|
|
|
|
|
debug('looking for %j in %j', request, mainPaths);
|
2019-03-29 14:46:53 +01:00
|
|
|
return mainPaths;
|
2011-01-23 23:40:56 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:46:53 +01:00
|
|
|
debug('RELATIVE: requested: %s from parent.id %s', request, parent.id);
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2019-03-26 05:21:27 +01:00
|
|
|
const parentDir = [path.dirname(parent.filename)];
|
2019-03-29 14:46:53 +01:00
|
|
|
debug('looking for %j', parentDir);
|
|
|
|
return parentDir;
|
2011-01-24 10:55:30 -08:00
|
|
|
};
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2019-10-11 18:38:50 +02:00
|
|
|
function emitCircularRequireWarning(prop) {
|
|
|
|
process.emitWarning(
|
|
|
|
`Accessing non-existent property '${String(prop)}' of module exports ` +
|
2020-02-04 17:01:01 -10:00
|
|
|
'inside circular dependency'
|
|
|
|
);
|
2019-10-11 18:38:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// A Proxy that can be used as the prototype of a module.exports object and
|
2020-05-10 13:49:23 +02:00
|
|
|
// warns when non-existent properties are accessed.
|
2019-10-11 18:38:50 +02:00
|
|
|
const CircularRequirePrototypeWarningProxy = new Proxy({}, {
|
2022-06-12 13:52:43 +02:00
|
|
|
__proto__: null,
|
|
|
|
|
2019-10-11 18:38:50 +02:00
|
|
|
get(target, prop) {
|
2020-04-24 23:08:57 +02:00
|
|
|
// Allow __esModule access in any case because it is used in the output
|
|
|
|
// of transpiled code to determine whether something comes from an
|
|
|
|
// ES module, and is not used as a regular key of `module.exports`.
|
|
|
|
if (prop in target || prop === '__esModule') return target[prop];
|
2019-10-11 18:38:50 +02:00
|
|
|
emitCircularRequireWarning(prop);
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
|
|
|
|
getOwnPropertyDescriptor(target, prop) {
|
2020-04-24 23:08:57 +02:00
|
|
|
if (ObjectPrototypeHasOwnProperty(target, prop) || prop === '__esModule')
|
2019-11-22 18:04:46 +01:00
|
|
|
return ObjectGetOwnPropertyDescriptor(target, prop);
|
2019-10-11 18:38:50 +02:00
|
|
|
emitCircularRequireWarning(prop);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function getExportsForCircularRequire(module) {
|
|
|
|
if (module.exports &&
|
2020-05-10 13:49:23 +02:00
|
|
|
!isProxy(module.exports) &&
|
2020-09-27 17:39:01 +02:00
|
|
|
ObjectGetPrototypeOf(module.exports) === ObjectPrototype &&
|
2019-10-11 18:38:50 +02:00
|
|
|
// Exclude transpiled ES6 modules / TypeScript code because those may
|
2020-05-10 13:49:23 +02:00
|
|
|
// employ unusual patterns for accessing 'module.exports'. That should
|
|
|
|
// be okay because ES6 modules have a different approach to circular
|
2019-10-11 18:38:50 +02:00
|
|
|
// dependencies anyway.
|
|
|
|
!module.exports.__esModule) {
|
|
|
|
// This is later unset once the module is done loading.
|
2020-05-10 13:49:23 +02:00
|
|
|
ObjectSetPrototypeOf(
|
|
|
|
module.exports, CircularRequirePrototypeWarningProxy);
|
2019-10-11 18:38:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return module.exports;
|
|
|
|
}
|
|
|
|
|
2014-06-12 11:12:54 -07:00
|
|
|
// Check the cache for the requested file.
|
|
|
|
// 1. If a module already exists in the cache: return its exports object.
|
2019-03-09 18:12:05 +01:00
|
|
|
// 2. If the module is native: call
|
|
|
|
// `NativeModule.prototype.compileForPublicLoader()` and return the exports.
|
2014-06-12 11:12:54 -07:00
|
|
|
// 3. Otherwise, create a new module for the file and save it to the cache.
|
|
|
|
// Then have it load the file contents before returning its exports
|
|
|
|
// object.
|
2011-02-02 09:56:32 -08:00
|
|
|
Module._load = function(request, parent, isMain) {
|
2019-03-28 23:37:07 +01:00
|
|
|
let relResolveCacheIdentifier;
|
2011-01-24 20:52:38 -05:00
|
|
|
if (parent) {
|
2015-10-28 17:12:35 -07:00
|
|
|
debug('Module._load REQUEST %s parent: %s', request, parent.id);
|
2019-03-28 23:37:07 +01:00
|
|
|
// Fast path for (lazy loaded) modules in the same directory. The indirect
|
|
|
|
// caching is required to allow cache invalidation without changing the old
|
|
|
|
// cache key names.
|
|
|
|
relResolveCacheIdentifier = `${parent.path}\x00${request}`;
|
|
|
|
const filename = relativeResolveCache[relResolveCacheIdentifier];
|
|
|
|
if (filename !== undefined) {
|
|
|
|
const cachedModule = Module._cache[filename];
|
|
|
|
if (cachedModule !== undefined) {
|
|
|
|
updateChildren(parent, cachedModule, true);
|
2019-10-11 18:38:50 +02:00
|
|
|
if (!cachedModule.loaded)
|
|
|
|
return getExportsForCircularRequire(cachedModule);
|
2019-03-28 23:37:07 +01:00
|
|
|
return cachedModule.exports;
|
|
|
|
}
|
|
|
|
delete relativeResolveCache[relResolveCacheIdentifier];
|
|
|
|
}
|
2011-01-24 20:52:38 -05:00
|
|
|
}
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2022-06-17 16:20:48 +08:00
|
|
|
if (StringPrototypeStartsWith(request, 'node:')) {
|
2021-02-06 12:10:00 +01:00
|
|
|
// Slice 'node:' prefix
|
2022-06-17 16:20:48 +08:00
|
|
|
const id = StringPrototypeSlice(request, 5);
|
2021-02-06 12:10:00 +01:00
|
|
|
|
|
|
|
const module = loadNativeModule(id, request);
|
|
|
|
if (!module?.canBeRequiredByUsers) {
|
2022-06-17 16:20:48 +08:00
|
|
|
throw new ERR_UNKNOWN_BUILTIN_MODULE(request);
|
2021-02-06 12:10:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return module.exports;
|
|
|
|
}
|
2017-10-11 15:45:21 +02:00
|
|
|
|
2022-06-17 16:20:48 +08:00
|
|
|
const filename = Module._resolveFilename(request, parent, isMain);
|
2019-03-26 05:21:27 +01:00
|
|
|
const cachedModule = Module._cache[filename];
|
2019-03-28 23:37:07 +01:00
|
|
|
if (cachedModule !== undefined) {
|
2017-07-08 13:06:02 +02:00
|
|
|
updateChildren(parent, cachedModule, true);
|
2020-05-14 22:40:37 -07:00
|
|
|
if (!cachedModule.loaded) {
|
|
|
|
const parseCachedModule = cjsParseCache.get(cachedModule);
|
|
|
|
if (!parseCachedModule || parseCachedModule.loaded)
|
|
|
|
return getExportsForCircularRequire(cachedModule);
|
|
|
|
parseCachedModule.loaded = true;
|
|
|
|
} else {
|
|
|
|
return cachedModule.exports;
|
|
|
|
}
|
2011-01-23 23:40:56 +01:00
|
|
|
}
|
|
|
|
|
2019-10-11 17:57:13 -04:00
|
|
|
const mod = loadNativeModule(filename, request);
|
2022-04-14 09:08:55 -04:00
|
|
|
if (mod?.canBeRequiredByUsers &&
|
|
|
|
NativeModule.canBeRequiredWithoutScheme(filename)) {
|
2022-03-19 23:27:07 -04:00
|
|
|
return mod.exports;
|
|
|
|
}
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2017-07-08 13:06:02 +02:00
|
|
|
// Don't call updateChildren(), Module constructor already does.
|
2020-05-14 22:40:37 -07:00
|
|
|
const module = cachedModule || new Module(filename, parent);
|
2011-02-02 09:56:32 -08:00
|
|
|
|
|
|
|
if (isMain) {
|
|
|
|
process.mainModule = module;
|
|
|
|
module.id = '.';
|
|
|
|
}
|
|
|
|
|
2011-01-23 23:40:56 +01:00
|
|
|
Module._cache[filename] = module;
|
2019-03-28 23:37:07 +01:00
|
|
|
if (parent !== undefined) {
|
|
|
|
relativeResolveCache[relResolveCacheIdentifier] = filename;
|
|
|
|
}
|
2012-05-08 22:02:28 +02:00
|
|
|
|
2019-03-28 22:42:06 +01:00
|
|
|
let threw = true;
|
2011-02-21 15:46:34 +01:00
|
|
|
try {
|
2021-06-13 09:21:40 -07:00
|
|
|
module.load(filename);
|
2016-04-07 10:04:47 -04:00
|
|
|
threw = false;
|
2012-05-08 22:02:28 +02:00
|
|
|
} finally {
|
2016-04-07 10:04:47 -04:00
|
|
|
if (threw) {
|
2012-05-08 22:02:28 +02:00
|
|
|
delete Module._cache[filename];
|
2019-03-28 23:37:07 +01:00
|
|
|
if (parent !== undefined) {
|
|
|
|
delete relativeResolveCache[relResolveCacheIdentifier];
|
2021-02-05 19:44:44 +05:30
|
|
|
const children = parent?.children;
|
2020-04-14 10:58:01 +08:00
|
|
|
if (ArrayIsArray(children)) {
|
2020-11-07 11:08:09 +01:00
|
|
|
const index = ArrayPrototypeIndexOf(children, module);
|
2020-04-14 10:58:01 +08:00
|
|
|
if (index !== -1) {
|
2020-11-07 11:08:09 +01:00
|
|
|
ArrayPrototypeSplice(children, index, 1);
|
2020-04-14 10:58:01 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-28 23:37:07 +01:00
|
|
|
}
|
2019-10-11 18:38:50 +02:00
|
|
|
} else if (module.exports &&
|
2020-05-10 13:49:23 +02:00
|
|
|
!isProxy(module.exports) &&
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectGetPrototypeOf(module.exports) ===
|
2019-10-11 18:38:50 +02:00
|
|
|
CircularRequirePrototypeWarningProxy) {
|
2020-09-27 17:39:01 +02:00
|
|
|
ObjectSetPrototypeOf(module.exports, ObjectPrototype);
|
2012-05-08 22:02:28 +02:00
|
|
|
}
|
2011-02-21 15:46:34 +01:00
|
|
|
}
|
2019-03-28 22:42:06 +01:00
|
|
|
|
|
|
|
return module.exports;
|
|
|
|
};
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2016-04-25 12:19:28 -04:00
|
|
|
Module._resolveFilename = function(request, parent, isMain, options) {
|
2022-06-17 16:20:48 +08:00
|
|
|
if (
|
|
|
|
(
|
|
|
|
StringPrototypeStartsWith(request, 'node:') &&
|
|
|
|
NativeModule.canBeRequiredByUsers(StringPrototypeSlice(request, 5))
|
|
|
|
) || (
|
|
|
|
NativeModule.canBeRequiredByUsers(request) &&
|
|
|
|
NativeModule.canBeRequiredWithoutScheme(request)
|
|
|
|
)
|
|
|
|
) {
|
2011-11-22 23:46:01 +00:00
|
|
|
return request;
|
2011-01-23 23:40:56 +01:00
|
|
|
}
|
|
|
|
|
2019-11-12 16:03:15 +00:00
|
|
|
let paths;
|
2016-04-25 12:19:28 -04:00
|
|
|
|
2019-05-08 13:42:22 -04:00
|
|
|
if (typeof options === 'object' && options !== null) {
|
2019-11-23 10:09:05 +01:00
|
|
|
if (ArrayIsArray(options.paths)) {
|
2020-11-07 11:08:09 +01:00
|
|
|
const isRelative = StringPrototypeStartsWith(request, './') ||
|
|
|
|
StringPrototypeStartsWith(request, '../') ||
|
|
|
|
((isWindows && StringPrototypeStartsWith(request, '.\\')) ||
|
|
|
|
StringPrototypeStartsWith(request, '..\\'));
|
2017-11-18 01:39:02 -05:00
|
|
|
|
2019-05-08 13:42:22 -04:00
|
|
|
if (isRelative) {
|
|
|
|
paths = options.paths;
|
|
|
|
} else {
|
|
|
|
const fakeParent = new Module('', null);
|
2016-04-25 12:19:28 -04:00
|
|
|
|
2019-05-08 13:42:22 -04:00
|
|
|
paths = [];
|
2016-04-25 12:19:28 -04:00
|
|
|
|
2019-11-12 16:03:15 +00:00
|
|
|
for (let i = 0; i < options.paths.length; i++) {
|
2019-05-08 13:42:22 -04:00
|
|
|
const path = options.paths[i];
|
|
|
|
fakeParent.paths = Module._nodeModulePaths(path);
|
|
|
|
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
|
2019-05-07 11:31:29 -04:00
|
|
|
|
2019-11-12 16:03:15 +00:00
|
|
|
for (let j = 0; j < lookupPaths.length; j++) {
|
2020-11-07 11:08:09 +01:00
|
|
|
if (!ArrayPrototypeIncludes(paths, lookupPaths[j]))
|
|
|
|
ArrayPrototypePush(paths, lookupPaths[j]);
|
2019-05-08 13:42:22 -04:00
|
|
|
}
|
2019-05-07 11:31:29 -04:00
|
|
|
}
|
2016-04-25 12:19:28 -04:00
|
|
|
}
|
2019-06-05 15:35:13 -04:00
|
|
|
} else if (options.paths === undefined) {
|
|
|
|
paths = Module._resolveLookupPaths(request, parent);
|
|
|
|
} else {
|
2020-08-08 19:01:59 +03:00
|
|
|
throw new ERR_INVALID_ARG_VALUE('options.paths', options.paths);
|
2016-04-25 12:19:28 -04:00
|
|
|
}
|
|
|
|
} else {
|
2019-03-29 14:46:53 +01:00
|
|
|
paths = Module._resolveLookupPaths(request, parent);
|
2016-04-25 12:19:28 -04:00
|
|
|
}
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2022-06-30 16:31:02 +08:00
|
|
|
if (request[0] === '#' && (parent?.filename || parent?.id === '<repl>')) {
|
|
|
|
const parentPath = parent?.filename ?? process.cwd() + path.sep;
|
|
|
|
const pkg = readPackageScope(parentPath) || {};
|
|
|
|
if (pkg.data?.imports != null) {
|
|
|
|
try {
|
|
|
|
return finalizeEsmResolution(
|
|
|
|
packageImportsResolve(request, pathToFileURL(parentPath),
|
|
|
|
cjsConditions), parentPath,
|
|
|
|
pkg.path);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code === 'ERR_MODULE_NOT_FOUND')
|
|
|
|
throw createEsmNotFoundErr(request);
|
|
|
|
throw e;
|
2020-06-27 22:09:24 -07:00
|
|
|
}
|
|
|
|
}
|
2020-07-16 09:27:53 +02:00
|
|
|
}
|
|
|
|
|
2021-05-31 08:50:41 -04:00
|
|
|
// Try module self resolution first
|
2020-07-16 09:27:53 +02:00
|
|
|
const parentPath = trySelfParentPath(parent);
|
|
|
|
const selfResolved = trySelf(parentPath, request);
|
|
|
|
if (selfResolved) {
|
|
|
|
const cacheKey = request + '\x00' +
|
|
|
|
(paths.length === 1 ? paths[0] : ArrayPrototypeJoin(paths, '\x00'));
|
|
|
|
Module._pathCache[cacheKey] = selfResolved;
|
|
|
|
return selfResolved;
|
2011-01-23 23:40:56 +01:00
|
|
|
}
|
2019-12-17 01:58:19 -05:00
|
|
|
|
|
|
|
// Look up the filename first, since that's the cache key.
|
|
|
|
const filename = Module._findPath(request, paths, isMain, false);
|
|
|
|
if (filename) return filename;
|
|
|
|
const requireStack = [];
|
|
|
|
for (let cursor = parent;
|
|
|
|
cursor;
|
2020-03-11 23:44:31 +01:00
|
|
|
cursor = moduleParentCache.get(cursor)) {
|
2020-11-07 11:08:09 +01:00
|
|
|
ArrayPrototypePush(requireStack, cursor.filename || cursor.id);
|
2019-12-17 01:58:19 -05:00
|
|
|
}
|
|
|
|
let message = `Cannot find module '${request}'`;
|
|
|
|
if (requireStack.length > 0) {
|
2020-11-07 11:08:09 +01:00
|
|
|
message = message + '\nRequire stack:\n- ' +
|
|
|
|
ArrayPrototypeJoin(requireStack, '\n- ');
|
2019-12-17 01:58:19 -05:00
|
|
|
}
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
const err = new Error(message);
|
|
|
|
err.code = 'MODULE_NOT_FOUND';
|
|
|
|
err.requireStack = requireStack;
|
|
|
|
throw err;
|
2011-01-24 10:55:30 -08:00
|
|
|
};
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2021-09-15 02:52:40 -07:00
|
|
|
function finalizeEsmResolution(resolved, parentPath, pkgPath) {
|
2022-06-27 17:16:06 +02:00
|
|
|
if (RegExpPrototypeExec(encodedSepRegEx, resolved) !== null)
|
2020-08-09 16:54:01 -07:00
|
|
|
throw new ERR_INVALID_MODULE_SPECIFIER(
|
|
|
|
resolved, 'must not include encoded "/" or "\\" characters', parentPath);
|
|
|
|
const filename = fileURLToPath(resolved);
|
2021-09-15 02:52:40 -07:00
|
|
|
const actual = tryFile(filename);
|
2020-08-09 16:54:01 -07:00
|
|
|
if (actual)
|
|
|
|
return actual;
|
|
|
|
const err = createEsmNotFoundErr(filename,
|
|
|
|
path.resolve(pkgPath, 'package.json'));
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createEsmNotFoundErr(request, path) {
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
const err = new Error(`Cannot find module '${request}'`);
|
|
|
|
err.code = 'MODULE_NOT_FOUND';
|
|
|
|
if (path)
|
|
|
|
err.path = path;
|
|
|
|
// TODO(BridgeAR): Add the requireStack as well.
|
|
|
|
return err;
|
|
|
|
}
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2014-06-12 11:12:54 -07:00
|
|
|
// Given a file name, pass it to the proper extension handler.
|
2011-01-23 23:40:56 +01:00
|
|
|
Module.prototype.load = function(filename) {
|
2015-10-28 17:12:35 -07:00
|
|
|
debug('load %j for module %j', filename, this.id);
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2011-01-27 16:55:49 -08:00
|
|
|
assert(!this.loaded);
|
2011-01-23 23:40:56 +01:00
|
|
|
this.filename = filename;
|
2011-02-09 13:28:30 -08:00
|
|
|
this.paths = Module._nodeModulePaths(path.dirname(filename));
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2019-03-26 05:21:27 +01:00
|
|
|
const extension = findLongestRegisteredExtension(filename);
|
2019-09-30 14:55:59 -05:00
|
|
|
// allow .mjs to be overridden
|
2020-11-07 11:08:09 +01:00
|
|
|
if (StringPrototypeEndsWith(filename, '.mjs') && !Module._extensions['.mjs'])
|
2021-06-27 15:29:02 -07:00
|
|
|
throw new ERR_REQUIRE_ESM(filename, true);
|
2020-11-07 11:08:09 +01:00
|
|
|
|
2011-01-23 23:40:56 +01:00
|
|
|
Module._extensions[extension](this, filename);
|
|
|
|
this.loaded = true;
|
2020-07-30 10:28:38 -05:00
|
|
|
|
2021-08-25 22:55:14 +02:00
|
|
|
const esmLoader = asyncESM.esmLoader;
|
2020-07-30 10:28:38 -05:00
|
|
|
// Create module entry at load time to snapshot exports correctly
|
|
|
|
const exports = this.exports;
|
2020-08-02 18:03:48 -07:00
|
|
|
// Preemptively cache
|
|
|
|
if ((module?.module === undefined ||
|
|
|
|
module.module.getStatus() < kEvaluated) &&
|
2021-08-25 22:55:14 +02:00
|
|
|
!esmLoader.cjsCache.has(this))
|
|
|
|
esmLoader.cjsCache.set(this, exports);
|
2011-01-23 23:40:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-06-12 11:12:54 -07:00
|
|
|
// Loads a module at the given file path. Returns that module's
|
|
|
|
// `exports` property.
|
2018-01-25 04:26:52 +08:00
|
|
|
Module.prototype.require = function(id) {
|
2018-12-06 13:50:41 +08:00
|
|
|
validateString(id, 'id');
|
2018-01-25 04:26:52 +08:00
|
|
|
if (id === '') {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_VALUE('id', id,
|
|
|
|
'must be a non-empty string');
|
2018-01-25 04:26:52 +08:00
|
|
|
}
|
2019-02-22 18:52:38 +01:00
|
|
|
requireDepth++;
|
|
|
|
try {
|
|
|
|
return Module._load(id, this, /* isMain */ false);
|
|
|
|
} finally {
|
|
|
|
requireDepth--;
|
|
|
|
}
|
2011-07-14 13:55:51 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-24 22:45:34 +06:00
|
|
|
// Resolved path to process.argv[1] will be lazily placed here
|
2017-04-18 22:10:35 +02:00
|
|
|
// (needed for setting breakpoint when called with --inspect-brk)
|
2019-11-12 16:03:15 +00:00
|
|
|
let resolvedArgv;
|
2019-04-17 22:41:29 +08:00
|
|
|
let hasPausedEntry = false;
|
2012-01-24 22:45:34 +06:00
|
|
|
|
2019-09-30 14:55:59 -05:00
|
|
|
function wrapSafe(filename, content, cjsModuleInstance) {
|
2018-11-07 22:17:09 +05:30
|
|
|
if (patched) {
|
|
|
|
const wrapper = Module.wrap(content);
|
2019-05-18 22:48:46 -05:00
|
|
|
return vm.runInThisContext(wrapper, {
|
2018-11-07 22:17:09 +05:30
|
|
|
filename,
|
|
|
|
lineOffset: 0,
|
|
|
|
displayErrors: true,
|
2021-08-28 01:47:49 +02:00
|
|
|
importModuleDynamically: async (specifier, _, importAssertions) => {
|
2021-08-25 22:55:14 +02:00
|
|
|
const loader = asyncESM.esmLoader;
|
2022-05-20 17:52:16 -07:00
|
|
|
return loader.import(specifier, normalizeReferrerURL(filename),
|
2021-08-28 01:47:49 +02:00
|
|
|
importAssertions);
|
2019-10-11 17:57:13 -04:00
|
|
|
},
|
2018-11-07 22:17:09 +05:30
|
|
|
});
|
|
|
|
}
|
2019-07-27 20:23:25 +02:00
|
|
|
try {
|
2020-09-25 15:22:52 -05:00
|
|
|
return vm.compileFunction(content, [
|
|
|
|
'exports',
|
|
|
|
'require',
|
|
|
|
'module',
|
|
|
|
'__filename',
|
|
|
|
'__dirname',
|
|
|
|
], {
|
2019-07-27 20:23:25 +02:00
|
|
|
filename,
|
2021-08-28 01:47:49 +02:00
|
|
|
importModuleDynamically(specifier, _, importAssertions) {
|
2021-08-25 22:55:14 +02:00
|
|
|
const loader = asyncESM.esmLoader;
|
2022-05-20 17:52:16 -07:00
|
|
|
return loader.import(specifier, normalizeReferrerURL(filename),
|
2021-08-28 01:47:49 +02:00
|
|
|
importAssertions);
|
2020-09-25 15:22:52 -05:00
|
|
|
},
|
|
|
|
});
|
2019-07-27 20:23:25 +02:00
|
|
|
} catch (err) {
|
2019-10-11 17:57:13 -04:00
|
|
|
if (process.mainModule === cjsModuleInstance)
|
2021-07-24 17:01:42 +02:00
|
|
|
enrichCJSError(err, content);
|
2019-07-27 20:23:25 +02:00
|
|
|
throw err;
|
|
|
|
}
|
2019-05-18 22:48:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the file contents in the correct scope or sandbox. Expose
|
|
|
|
// the correct helper variables (require, module, exports) to
|
|
|
|
// the file.
|
|
|
|
// Returns exception, if any.
|
|
|
|
Module.prototype._compile = function(content, filename) {
|
2019-07-18 09:01:59 -05:00
|
|
|
let moduleURL;
|
|
|
|
let redirects;
|
2020-07-17 11:38:46 -05:00
|
|
|
if (policy?.manifest) {
|
2019-07-18 09:01:59 -05:00
|
|
|
moduleURL = pathToFileURL(filename);
|
2020-07-17 11:38:46 -05:00
|
|
|
redirects = policy.manifest.getDependencyMapper(moduleURL);
|
|
|
|
policy.manifest.assertIntegrity(moduleURL, content);
|
2019-05-18 22:48:46 -05:00
|
|
|
}
|
|
|
|
|
2019-09-29 14:15:39 -07:00
|
|
|
maybeCacheSourceMap(filename, content, this);
|
2019-09-30 14:55:59 -05:00
|
|
|
const compiledWrapper = wrapSafe(filename, content, this);
|
2019-05-18 22:48:46 -05:00
|
|
|
|
2019-11-12 16:03:15 +00:00
|
|
|
let inspectorWrapper = null;
|
2019-04-17 22:41:29 +08:00
|
|
|
if (getOptionValue('--inspect-brk') && process._eval == null) {
|
2012-01-24 22:45:34 +06:00
|
|
|
if (!resolvedArgv) {
|
2018-12-10 13:27:32 +01:00
|
|
|
// We enter the repl if we're not given a filename argument.
|
2012-08-11 18:58:40 -07:00
|
|
|
if (process.argv[1]) {
|
2019-11-08 18:38:26 +01:00
|
|
|
try {
|
|
|
|
resolvedArgv = Module._resolveFilename(process.argv[1], null, false);
|
|
|
|
} catch {
|
|
|
|
// We only expect this codepath to be reached in the case of a
|
|
|
|
// preloaded module (it will fail earlier with the main entry)
|
2019-12-05 00:11:04 -05:00
|
|
|
assert(ArrayIsArray(getOptionValue('--require')));
|
2019-11-08 18:38:26 +01:00
|
|
|
}
|
2012-08-11 18:58:40 -07:00
|
|
|
} else {
|
2012-09-08 03:18:39 +09:00
|
|
|
resolvedArgv = 'repl';
|
2012-08-11 18:58:40 -07:00
|
|
|
}
|
2012-01-24 22:45:34 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set breakpoint on module start
|
2019-11-08 18:38:26 +01:00
|
|
|
if (resolvedArgv && !hasPausedEntry && filename === resolvedArgv) {
|
2019-04-17 22:41:29 +08:00
|
|
|
hasPausedEntry = true;
|
2018-12-09 22:12:39 -05:00
|
|
|
inspectorWrapper = internalBinding('inspector').callAndPauseOnStart;
|
2012-01-24 22:45:34 +06:00
|
|
|
}
|
2011-01-23 23:40:56 +01:00
|
|
|
}
|
2019-03-26 05:21:27 +01:00
|
|
|
const dirname = path.dirname(filename);
|
2019-07-18 09:01:59 -05:00
|
|
|
const require = makeRequireFunction(this, redirects);
|
2019-11-12 16:03:15 +00:00
|
|
|
let result;
|
2019-03-26 05:21:27 +01:00
|
|
|
const exports = this.exports;
|
|
|
|
const thisValue = exports;
|
|
|
|
const module = this;
|
2020-11-07 11:08:09 +01:00
|
|
|
if (requireDepth === 0) statCache = new SafeMap();
|
2017-03-27 13:11:05 -07:00
|
|
|
if (inspectorWrapper) {
|
2018-09-13 14:27:12 -05:00
|
|
|
result = inspectorWrapper(compiledWrapper, thisValue, exports,
|
|
|
|
require, module, filename, dirname);
|
2017-03-27 13:11:05 -07:00
|
|
|
} else {
|
2020-11-07 11:08:09 +01:00
|
|
|
result = ReflectApply(compiledWrapper, thisValue,
|
|
|
|
[exports, require, module, filename, dirname]);
|
2017-03-27 13:11:05 -07:00
|
|
|
}
|
2019-11-10 16:59:16 +08:00
|
|
|
hasLoadedAnyUserCJSModule = true;
|
2019-02-22 18:52:38 +01:00
|
|
|
if (requireDepth === 0) statCache = null;
|
2016-01-07 22:36:30 +01:00
|
|
|
return result;
|
2011-01-23 23:40:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Native extension for .js
|
|
|
|
Module._extensions['.js'] = function(module, filename) {
|
2020-05-14 22:40:37 -07:00
|
|
|
// If already analyzed the source, then it will be cached.
|
|
|
|
const cached = cjsParseCache.get(module);
|
|
|
|
let content;
|
2021-02-05 19:44:44 +05:30
|
|
|
if (cached?.source) {
|
2020-05-14 22:40:37 -07:00
|
|
|
content = cached.source;
|
|
|
|
cached.source = undefined;
|
|
|
|
} else {
|
|
|
|
content = fs.readFileSync(filename, 'utf8');
|
|
|
|
}
|
2021-06-27 15:29:02 -07:00
|
|
|
if (StringPrototypeEndsWith(filename, '.js')) {
|
|
|
|
const pkg = readPackageScope(filename);
|
|
|
|
// Function require shouldn't be used in ES modules.
|
|
|
|
if (pkg?.data?.type === 'module') {
|
|
|
|
const parent = moduleParentCache.get(module);
|
|
|
|
const parentPath = parent?.filename;
|
|
|
|
const packageJsonPath = path.resolve(pkg.path, 'package.json');
|
|
|
|
const usesEsm = hasEsmSyntax(content);
|
|
|
|
const err = new ERR_REQUIRE_ESM(filename, usesEsm, parentPath,
|
|
|
|
packageJsonPath);
|
|
|
|
// Attempt to reconstruct the parent require frame.
|
|
|
|
if (Module._cache[parentPath]) {
|
|
|
|
let parentSource;
|
|
|
|
try {
|
|
|
|
parentSource = fs.readFileSync(parentPath, 'utf8');
|
2022-02-02 21:57:11 -08:00
|
|
|
} catch {
|
|
|
|
// Continue regardless of error.
|
|
|
|
}
|
2021-06-27 15:29:02 -07:00
|
|
|
if (parentSource) {
|
|
|
|
const errLine = StringPrototypeSplit(
|
|
|
|
StringPrototypeSlice(err.stack, StringPrototypeIndexOf(
|
|
|
|
err.stack, ' at ')), '\n', 1)[0];
|
|
|
|
const { 1: line, 2: col } =
|
|
|
|
RegExpPrototypeExec(/(\d+):(\d+)\)/, errLine) || [];
|
|
|
|
if (line && col) {
|
|
|
|
const srcLine = StringPrototypeSplit(parentSource, '\n')[line - 1];
|
|
|
|
const frame = `${parentPath}:${line}\n${srcLine}\n${
|
|
|
|
StringPrototypeRepeat(' ', col - 1)}^\n`;
|
|
|
|
setArrowMessage(err, frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 22:48:46 -05:00
|
|
|
module._compile(content, filename);
|
2011-01-23 23:40:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-07-20 17:39:23 -07:00
|
|
|
// Native extension for .json
|
2011-09-23 09:25:20 -04:00
|
|
|
Module._extensions['.json'] = function(module, filename) {
|
2018-09-13 14:27:12 -05:00
|
|
|
const content = fs.readFileSync(filename, 'utf8');
|
|
|
|
|
2020-07-17 11:38:46 -05:00
|
|
|
if (policy?.manifest) {
|
2018-09-13 14:27:12 -05:00
|
|
|
const moduleURL = pathToFileURL(filename);
|
2020-07-17 11:38:46 -05:00
|
|
|
policy.manifest.assertIntegrity(moduleURL, content);
|
2018-09-13 14:27:12 -05:00
|
|
|
}
|
|
|
|
|
2012-07-06 15:26:41 -07:00
|
|
|
try {
|
2019-11-22 18:04:46 +01:00
|
|
|
module.exports = JSONParse(stripBOM(content));
|
2012-07-06 15:26:41 -07:00
|
|
|
} catch (err) {
|
|
|
|
err.message = filename + ': ' + err.message;
|
|
|
|
throw err;
|
|
|
|
}
|
2011-08-02 23:18:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-03-25 22:27:38 +08:00
|
|
|
// Native extension for .node
|
2015-09-19 17:03:32 -05:00
|
|
|
Module._extensions['.node'] = function(module, filename) {
|
2020-07-17 11:38:46 -05:00
|
|
|
if (policy?.manifest) {
|
2018-09-13 14:27:12 -05:00
|
|
|
const content = fs.readFileSync(filename);
|
|
|
|
const moduleURL = pathToFileURL(filename);
|
2020-07-17 11:38:46 -05:00
|
|
|
policy.manifest.assertIntegrity(moduleURL, content);
|
2018-09-13 14:27:12 -05:00
|
|
|
}
|
2019-03-07 01:03:53 +01:00
|
|
|
// Be aware this doesn't use `content`
|
2017-08-20 22:44:47 -07:00
|
|
|
return process.dlopen(module, path.toNamespacedPath(filename));
|
2015-09-19 17:03:32 -05:00
|
|
|
};
|
2012-02-01 22:25:55 +01:00
|
|
|
|
2019-04-25 00:29:22 -04:00
|
|
|
function createRequireFromPath(filename) {
|
2018-10-22 20:42:53 +02:00
|
|
|
// Allow a directory to be passed as the filename
|
|
|
|
const trailingSlash =
|
2020-11-07 11:08:09 +01:00
|
|
|
StringPrototypeEndsWith(filename, '/') ||
|
|
|
|
(isWindows && StringPrototypeEndsWith(filename, '\\'));
|
2018-10-22 20:42:53 +02:00
|
|
|
|
|
|
|
const proxyPath = trailingSlash ?
|
|
|
|
path.join(filename, 'noop.js') :
|
|
|
|
filename;
|
|
|
|
|
|
|
|
const m = new Module(proxyPath);
|
|
|
|
m.filename = proxyPath;
|
|
|
|
|
|
|
|
m.paths = Module._nodeModulePaths(m.path);
|
2019-07-18 09:01:59 -05:00
|
|
|
return makeRequireFunction(m, null);
|
2019-04-25 00:29:22 -04:00
|
|
|
}
|
|
|
|
|
2019-05-09 16:21:19 -04:00
|
|
|
const createRequireError = 'must be a file URL object, file URL string, or ' +
|
2019-04-25 00:29:22 -04:00
|
|
|
'absolute path string';
|
|
|
|
|
|
|
|
function createRequire(filename) {
|
|
|
|
let filepath;
|
2019-05-09 16:47:56 -04:00
|
|
|
|
2020-08-27 23:36:50 +02:00
|
|
|
if (isURLInstance(filename) ||
|
2019-05-09 16:47:56 -04:00
|
|
|
(typeof filename === 'string' && !path.isAbsolute(filename))) {
|
2019-04-25 00:29:22 -04:00
|
|
|
try {
|
|
|
|
filepath = fileURLToPath(filename);
|
|
|
|
} catch {
|
|
|
|
throw new ERR_INVALID_ARG_VALUE('filename', filename,
|
|
|
|
createRequireError);
|
|
|
|
}
|
|
|
|
} else if (typeof filename !== 'string') {
|
|
|
|
throw new ERR_INVALID_ARG_VALUE('filename', filename, createRequireError);
|
|
|
|
} else {
|
|
|
|
filepath = filename;
|
|
|
|
}
|
|
|
|
return createRequireFromPath(filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
Module.createRequire = createRequire;
|
2018-03-14 18:36:59 -05:00
|
|
|
|
2012-10-12 11:44:02 -07:00
|
|
|
Module._initPaths = function() {
|
2019-11-12 16:03:15 +00:00
|
|
|
const homeDir = isWindows ? process.env.USERPROFILE : safeGetenv('HOME');
|
|
|
|
const nodePath = isWindows ? process.env.NODE_PATH : safeGetenv('NODE_PATH');
|
2012-10-09 00:47:38 +02:00
|
|
|
|
2016-10-25 23:58:06 +01:00
|
|
|
// process.execPath is $PREFIX/bin/node except on Windows where it is
|
2019-11-12 16:03:15 +00:00
|
|
|
// $PREFIX\node.exe where $PREFIX is the root of the Node.js installation.
|
|
|
|
const prefixDir = isWindows ?
|
|
|
|
path.resolve(process.execPath, '..') :
|
|
|
|
path.resolve(process.execPath, '..', '..');
|
|
|
|
|
2021-02-05 16:17:57 +01:00
|
|
|
const paths = [path.resolve(prefixDir, 'lib', 'node')];
|
2011-01-23 23:40:56 +01:00
|
|
|
|
2012-10-09 00:47:38 +02:00
|
|
|
if (homeDir) {
|
2020-11-21 19:28:54 +01:00
|
|
|
ArrayPrototypeUnshift(paths, path.resolve(homeDir, '.node_libraries'));
|
|
|
|
ArrayPrototypeUnshift(paths, path.resolve(homeDir, '.node_modules'));
|
2011-01-23 23:40:56 +01:00
|
|
|
}
|
|
|
|
|
2014-03-26 04:47:05 +07:00
|
|
|
if (nodePath) {
|
2021-02-05 16:17:57 +01:00
|
|
|
ArrayPrototypeUnshiftApply(paths, ArrayPrototypeFilter(
|
2020-11-07 11:08:09 +01:00
|
|
|
StringPrototypeSplit(nodePath, path.delimiter),
|
|
|
|
Boolean
|
2021-02-05 16:17:57 +01:00
|
|
|
));
|
2011-01-23 23:40:56 +01:00
|
|
|
}
|
|
|
|
|
2011-07-15 14:05:01 -07:00
|
|
|
modulePaths = paths;
|
|
|
|
|
2019-01-21 01:22:27 +01:00
|
|
|
// Clone as a shallow copy, for introspection.
|
2020-11-07 11:08:09 +01:00
|
|
|
Module.globalPaths = ArrayPrototypeSlice(modulePaths);
|
2011-01-23 23:40:56 +01:00
|
|
|
};
|
|
|
|
|
2015-05-27 10:11:30 -07:00
|
|
|
Module._preloadModules = function(requests) {
|
2019-11-23 10:09:05 +01:00
|
|
|
if (!ArrayIsArray(requests))
|
2015-05-27 10:11:30 -07:00
|
|
|
return;
|
|
|
|
|
2020-11-25 07:54:32 -08:00
|
|
|
isPreloading = true;
|
|
|
|
|
2015-05-27 10:11:30 -07:00
|
|
|
// Preloaded modules have a dummy parent module which is deemed to exist
|
|
|
|
// in the current working directory. This seeds the search path for
|
|
|
|
// preloaded modules.
|
2019-03-26 05:21:27 +01:00
|
|
|
const parent = new Module('internal/preload', null);
|
2015-08-11 20:25:49 -04:00
|
|
|
try {
|
|
|
|
parent.paths = Module._nodeModulePaths(process.cwd());
|
2016-07-08 17:17:47 -07:00
|
|
|
} catch (e) {
|
2015-08-11 20:25:49 -04:00
|
|
|
if (e.code !== 'ENOENT') {
|
2020-11-25 07:54:32 -08:00
|
|
|
isPreloading = false;
|
2015-08-11 20:25:49 -04:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 16:03:15 +00:00
|
|
|
for (let n = 0; n < requests.length; n++)
|
2017-02-27 09:33:07 -08:00
|
|
|
parent.require(requests[n]);
|
2020-11-25 07:54:32 -08:00
|
|
|
isPreloading = false;
|
2015-05-27 10:11:30 -07:00
|
|
|
};
|
|
|
|
|
2019-09-27 12:12:18 -05:00
|
|
|
Module.syncBuiltinESMExports = function syncBuiltinESMExports() {
|
|
|
|
for (const mod of NativeModule.map.values()) {
|
2022-04-14 09:08:55 -04:00
|
|
|
if (mod.canBeRequiredByUsers &&
|
|
|
|
NativeModule.canBeRequiredWithoutScheme(mod.id)) {
|
2019-09-27 12:12:18 -05:00
|
|
|
mod.syncExports();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-08 01:12:32 -07:00
|
|
|
Module.isBuiltin = function isBuiltin(moduleName) {
|
|
|
|
return allBuiltins.has(moduleName);
|
|
|
|
};
|
|
|
|
|
2018-05-07 00:03:12 +02:00
|
|
|
// Backwards compatibility
|
2011-01-23 23:40:56 +01:00
|
|
|
Module.Module = Module;
|