const runTestCode = (code, filePath) => { // Run Test Code const vm = new NodeVM({ require: { external: true, context: { console: !!process.env.ENABLE_CONSOLE ? console : makeConsoleMock() } } }); vm.run(code, filePath); }
describe("VM2 Babel Compiler", () => { test("compiles a file with babel", () => { const vm = new NodeVM({ compiler }); expect(() => vm.run(` export function multiply(a, b) { return a * b }`) ).not.toThrow(); }); });
fs.readFile(TARGET_PATH, 'utf8', (err, data) => { if (err) throw err; console.log(data); // let functionInSandbox = vm.run(data, __filename); let functionInSandbox = vm.run(data, TARGET_PATH); console.log('====result====', functionInSandbox()); });
fs.readFile(path.resolve('./data/user_info/handle_modules/error_not_login/index.js'), 'utf8', (err, data) => { if (err) throw err; console.log(data); let functionInSandbox = vm.run(data); console.log('====result====', functionInSandbox()); });
initialize(executionPath) { this._executionPath = executionPath; const cliConfigFile = path.join(this._executionPath, CLI_CONFIG_JS_FILE); if (!FileUtils.exists(cliConfigFile)) { return; } try { const nodeVm = new NodeVM({ console: 'inherit', sandbox: {}, require: { external: true, builtin: ['*'], root: this._executionPath, }, }); const cliConfigFileContent = FileUtils.readAsString(cliConfigFile); this._cliConfig = nodeVm.run(cliConfigFileContent, cliConfigFile); } catch (error) { throw NodeTranslationService.getMessage(ERRORS.CLI_CONFIG_ERROR_LOADING_CONFIGURATION_MODULE, cliConfigFile, lineBreak, error); } }
vm.run( `(async function() { try {
fs.readFile(TARGET_PATH, 'utf8', (err, data) => { if (err) throw err; console.log(data); let functionInSandbox = vm.run(data, TARGET_PATH); console.log('====result====', functionInSandbox.default()); });
fs.readFile(TARGET_PATH, 'utf8', (err, data) => { if (err) throw err; console.log(data); // let functionInSandbox = vm.run(data, __filename); let functionInSandbox = vm.run(data, TARGET_PATH); console.log('====result====', functionInSandbox()); });
initialize(executionPath) { this._executionPath = executionPath; const cliConfigFile = path.join(this._executionPath, CLI_CONFIG_JS_FILE); if (!FileUtils.exists(cliConfigFile)) { return; } try { const nodeVm = new NodeVM({ console: 'inherit', sandbox: {}, require: { external: true, builtin: ['*'], root: this._executionPath, }, }); const cliConfigFileContent = FileUtils.readAsString(cliConfigFile); this._cliConfig = nodeVm.run(cliConfigFileContent, cliConfigFile); } catch (error) { throw NodeTranslationService.getMessage(ERRORS.CLI_CONFIG_ERROR_LOADING_CONFIGURATION_MODULE, cliConfigFile, lineBreak, error); } }
vm.run( `(async function() { try {