mirror of
https://github.com/nodejs/node.git
synced 2025-05-05 19:08:17 +00:00

PR-URL: https://github.com/nodejs/node/pull/23522 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
167 lines
4.3 KiB
JavaScript
167 lines
4.3 KiB
JavaScript
'use strict';
|
|
|
|
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
|
|
|
|
const {
|
|
CHAR_LINE_FEED,
|
|
CHAR_CARRIAGE_RETURN,
|
|
CHAR_EXCLAMATION_MARK,
|
|
CHAR_HASH,
|
|
} = require('internal/constants');
|
|
|
|
const { getOptions } = internalBinding('options');
|
|
|
|
// Invoke with makeRequireFunction(module) where |module| is the Module object
|
|
// to use as the context for the require() function.
|
|
function makeRequireFunction(mod) {
|
|
const Module = mod.constructor;
|
|
|
|
function require(path) {
|
|
try {
|
|
exports.requireDepth += 1;
|
|
return mod.require(path);
|
|
} finally {
|
|
exports.requireDepth -= 1;
|
|
}
|
|
}
|
|
|
|
function resolve(request, options) {
|
|
if (typeof request !== 'string') {
|
|
throw new ERR_INVALID_ARG_TYPE('request', 'string', request);
|
|
}
|
|
return Module._resolveFilename(request, mod, false, options);
|
|
}
|
|
|
|
require.resolve = resolve;
|
|
|
|
function paths(request) {
|
|
if (typeof request !== 'string') {
|
|
throw new ERR_INVALID_ARG_TYPE('request', 'string', request);
|
|
}
|
|
return Module._resolveLookupPaths(request, mod, true);
|
|
}
|
|
|
|
resolve.paths = paths;
|
|
|
|
require.main = process.mainModule;
|
|
|
|
// Enable support to add extra extension types.
|
|
require.extensions = Module._extensions;
|
|
|
|
require.cache = Module._cache;
|
|
|
|
return require;
|
|
}
|
|
|
|
/**
|
|
* Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
|
|
* because the buffer-to-string conversion in `fs.readFileSync()`
|
|
* translates it to FEFF, the UTF-16 BOM.
|
|
*/
|
|
function stripBOM(content) {
|
|
if (content.charCodeAt(0) === 0xFEFF) {
|
|
content = content.slice(1);
|
|
}
|
|
return content;
|
|
}
|
|
|
|
/**
|
|
* Find end of shebang line and slice it off
|
|
*/
|
|
function stripShebang(content) {
|
|
// Remove shebang
|
|
var contLen = content.length;
|
|
if (contLen >= 2) {
|
|
if (content.charCodeAt(0) === CHAR_HASH &&
|
|
content.charCodeAt(1) === CHAR_EXCLAMATION_MARK) {
|
|
if (contLen === 2) {
|
|
// Exact match
|
|
content = '';
|
|
} else {
|
|
// Find end of shebang line and slice it off
|
|
var i = 2;
|
|
for (; i < contLen; ++i) {
|
|
var code = content.charCodeAt(i);
|
|
if (code === CHAR_LINE_FEED || code === CHAR_CARRIAGE_RETURN)
|
|
break;
|
|
}
|
|
if (i === contLen)
|
|
content = '';
|
|
else {
|
|
// Note that this actually includes the newline character(s) in the
|
|
// new output. This duplicates the behavior of the regular expression
|
|
// that was previously used to replace the shebang line
|
|
content = content.slice(i);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return content;
|
|
}
|
|
|
|
const builtinLibs = [
|
|
'assert', 'async_hooks', 'buffer', 'child_process', 'cluster', 'crypto',
|
|
'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'http2', 'https', 'net',
|
|
'os', 'path', 'perf_hooks', 'punycode', 'querystring', 'readline', 'repl',
|
|
'stream', 'string_decoder', 'tls', 'trace_events', 'tty', 'url', 'util',
|
|
'v8', 'vm', 'zlib'
|
|
];
|
|
|
|
if (getOptions('--experimental-worker')) {
|
|
builtinLibs.push('worker_threads');
|
|
builtinLibs.sort();
|
|
}
|
|
|
|
if (typeof process.binding('inspector').open === 'function') {
|
|
builtinLibs.push('inspector');
|
|
builtinLibs.sort();
|
|
}
|
|
|
|
function addBuiltinLibsToObject(object) {
|
|
// Make built-in modules available directly (loaded lazily).
|
|
builtinLibs.forEach((name) => {
|
|
// Goals of this mechanism are:
|
|
// - Lazy loading of built-in modules
|
|
// - Having all built-in modules available as non-enumerable properties
|
|
// - Allowing the user to re-assign these variables as if there were no
|
|
// pre-existing globals with the same name.
|
|
|
|
const setReal = (val) => {
|
|
// Deleting the property before re-assigning it disables the
|
|
// getter/setter mechanism.
|
|
delete object[name];
|
|
object[name] = val;
|
|
};
|
|
|
|
Object.defineProperty(object, name, {
|
|
get: () => {
|
|
const lib = require(name);
|
|
|
|
// Disable the current getter/setter and set up a new
|
|
// non-enumerable property.
|
|
delete object[name];
|
|
Object.defineProperty(object, name, {
|
|
get: () => lib,
|
|
set: setReal,
|
|
configurable: true,
|
|
enumerable: false
|
|
});
|
|
|
|
return lib;
|
|
},
|
|
set: setReal,
|
|
configurable: true,
|
|
enumerable: false
|
|
});
|
|
});
|
|
}
|
|
|
|
module.exports = exports = {
|
|
addBuiltinLibsToObject,
|
|
builtinLibs,
|
|
makeRequireFunction,
|
|
requireDepth: 0,
|
|
stripBOM,
|
|
stripShebang
|
|
};
|