mirror of
https://github.com/nodejs/node.git
synced 2025-05-17 18:26:24 +00:00

Modify passing of the `internalBinding` function so that it’s easier for core modules to adopt, and also not even accessible through `--expose-internals`. This also splits the module wrapper into a separate version for internal bindings and for CJS modules, which seems like a good idea given the different semantics. PR-URL: https://github.com/nodejs/node/pull/16218 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
32 lines
959 B
JavaScript
32 lines
959 B
JavaScript
'use strict';
|
|
|
|
const { URL } = require('url');
|
|
const CJSmodule = require('module');
|
|
const errors = require('internal/errors');
|
|
const { resolve } = internalBinding('module_wrap');
|
|
|
|
module.exports = (target, base) => {
|
|
if (base === undefined) {
|
|
// We cannot search without a base.
|
|
throw new errors.Error('ERR_MISSING_MODULE', target);
|
|
}
|
|
try {
|
|
return resolve(target, base);
|
|
} catch (e) {
|
|
e.stack; // cause V8 to generate stack before rethrow
|
|
let error = e;
|
|
try {
|
|
const questionedBase = new URL(base);
|
|
const tmpMod = new CJSmodule(questionedBase.pathname, null);
|
|
tmpMod.paths = CJSmodule._nodeModulePaths(
|
|
new URL('./', questionedBase).pathname);
|
|
const found = CJSmodule._resolveFilename(target, tmpMod);
|
|
error = new errors.Error('ERR_MODULE_RESOLUTION_LEGACY', target,
|
|
base, found);
|
|
} catch (problemChecking) {
|
|
// ignore
|
|
}
|
|
throw error;
|
|
}
|
|
};
|