in lib/virtual_context/SandboxRequire.ts [28:68]
function SandboxRequire(file: string): any {
let fileName = resolve(file);
if (EXPORTS_CACHE[fileName] && options.caching) {
return EXPORTS_CACHE[fileName];
}
let fileSource = fs.readFileSync(fileName, 'utf8');
let currentModule = new NodeModule(fileName, _parentModule);
currentModule.filename = fileName;
let childSandboxRequire = createSandboxRequire(fileName, globals, options, EXPORTS_CACHE);
let locals = getLocals(currentModule, childSandboxRequire);
fileSource =
`(function(global, ${ Object.keys(locals).join(', ') }) {
${options.strictMode ? '"use strict";' : ''}
${fileSource}
});`;
// Adds the default Node global variables to the custom globals, specified by the developer.
updateGlobals();
// Runs the loaded file source inside of a new context with the given globals.
let runFn = vm.runInNewContext(fileSource, globals, fileName);
let _localValues = [];
// Iterate through all locals and push the retrieved values to the value array.
Utils.forEach(locals, (value, key) => _localValues.push(value));
// Run our Function inside of the new context.
runFn.apply(currentModule.exports, [globals].concat(_localValues));
if (options.caching) {
EXPORTS_CACHE[fileName] = currentModule.exports;
}
return currentModule.exports;
}