workaround Node.js bug (#4579)

This commit is contained in:
Alex Lam S.L
2021-01-22 03:34:30 +00:00
committed by GitHub
parent 63b5b6d2b3
commit 0358637725

View File

@@ -1,5 +1,5 @@
var execSync = require("child_process").execSync;
var semver = require("semver"); var semver = require("semver");
var spawnSync = require("child_process").spawnSync;
var vm = require("vm"); var vm = require("vm");
setup_log(); setup_log();
@@ -104,7 +104,10 @@ function setup(global, setup_log, builtins) {
var value = ex; var value = ex;
if (value instanceof Error) { if (value instanceof Error) {
value = {}; value = {};
for (var name in ex) value[name] = ex[name]; for (var name in ex) {
value[name] = ex[name];
delete ex[name];
}
} }
process.stderr.write(inspect(value) + "\n\n-----===== UNCAUGHT EXCEPTION =====-----\n\n"); process.stderr.write(inspect(value) + "\n\n-----===== UNCAUGHT EXCEPTION =====-----\n\n");
throw ex; throw ex;
@@ -225,21 +228,22 @@ function run_code_exec(code, toplevel, timeout) {
return directive + setup_code; return directive + setup_code;
}); });
} }
try { var result = spawnSync(process.argv[0], [ '--max-old-space-size=2048' ], {
return execSync('"' + process.argv[0] + '" --max-old-space-size=2048', {
encoding: "utf8", encoding: "utf8",
input: code, input: code,
stdio: "pipe", stdio: "pipe",
timeout: timeout || 5000, timeout: timeout || 5000,
}); });
} catch (ex) { if (result.status === 0) return result.stdout;
var msg = ex.message.replace(/\r\n/g, "\n"); if (result.error && result.error.code == "ETIMEDOUT" || /FATAL ERROR:/.test(msg)) {
if (/ETIMEDOUT|FATAL ERROR:/.test(msg)) return new Error("Script execution timed out."); return new Error("Script execution timed out.");
}
if (result.error) return result.error;
var msg = result.stderr.replace(/\r\n/g, "\n");
var end = msg.indexOf("\n\n-----===== UNCAUGHT EXCEPTION =====-----\n\n"); var end = msg.indexOf("\n\n-----===== UNCAUGHT EXCEPTION =====-----\n\n");
var details; var details;
if (end >= 0) { if (end >= 0) {
var start = msg.indexOf("\n") + 1; details = msg.slice(0, end).replace(/<([1-9][0-9]*) empty items?>/g, function(match, count) {
details = msg.slice(start, end).replace(/<([1-9][0-9]*) empty items?>/g, function(match, count) {
return new Array(+count).join(); return new Array(+count).join();
}); });
try { try {
@@ -248,7 +252,7 @@ function run_code_exec(code, toplevel, timeout) {
} }
var match = /\n([^:\s]*Error)(?:: ([\s\S]+?))?\n( at [\s\S]+)\n$/.exec(msg); var match = /\n([^:\s]*Error)(?:: ([\s\S]+?))?\n( at [\s\S]+)\n$/.exec(msg);
if (!match) return details; if (!match) return details;
ex = new global[match[1]](match[2]); var ex = new global[match[1]](match[2]);
ex.stack = ex.stack.slice(0, ex.stack.indexOf(" at ")) + match[3]; ex.stack = ex.stack.slice(0, ex.stack.indexOf(" at ")) + match[3];
if (typeof details == "object") { if (typeof details == "object") {
for (var name in details) ex[name] = details[name]; for (var name in details) ex[name] = details[name];
@@ -257,4 +261,3 @@ function run_code_exec(code, toplevel, timeout) {
} }
return ex; return ex;
} }
}