@definitelytyped/utils
Advanced tools
Comparing version 0.0.58 to 0.0.59-next.0
@@ -8,2 +8,3 @@ "use strict"; | ||
const child_process_1 = require("child_process"); | ||
const net_1 = require("net"); | ||
const DEFAULT_CRASH_RECOVERY_MAX_OLD_SPACE_SIZE = 4096; | ||
@@ -32,3 +33,3 @@ /** Run a command and return the error, stdout, and stderr. (Never throws.) */ | ||
function runWithChildProcesses({ inputs, commandLineArgs, workerFile, nProcesses, handleOutput }) { | ||
return new Promise((resolve, reject) => { | ||
return new Promise(async (resolve, reject) => { | ||
const nPerProcess = Math.floor(inputs.length / nProcesses); | ||
@@ -47,3 +48,5 @@ let processesLeft = nProcesses; | ||
} | ||
const child = child_process_1.fork(workerFile, commandLineArgs); | ||
const child = child_process_1.fork(workerFile, commandLineArgs, { | ||
execArgv: await getChildProcessExecArgv(i) | ||
}); | ||
allChildren.push(child); | ||
@@ -85,3 +88,3 @@ child.send(inputs.slice(lo, hi)); | ||
function runWithListeningChildProcesses({ inputs, commandLineArgs, workerFile, nProcesses, cwd, handleOutput, crashRecovery, crashRecoveryMaxOldSpaceSize = DEFAULT_CRASH_RECOVERY_MAX_OLD_SPACE_SIZE, handleStart, handleCrash, softTimeoutMs = Infinity }) { | ||
return new Promise((resolve, reject) => { | ||
return new Promise(async (resolve, reject) => { | ||
let inputIndex = 0; | ||
@@ -125,3 +128,3 @@ let processesLeft = nProcesses; | ||
}; | ||
const onClose = () => { | ||
const onClose = async () => { | ||
if (rejected || !runningChildren.has(child)) { | ||
@@ -157,6 +160,6 @@ return; | ||
case 1 /* Retry */: | ||
restartChild(resumeTask, process.execArgv); | ||
await restartChild(resumeTask, process.execArgv); | ||
break; | ||
case 2 /* RetryWithMoreMemory */: | ||
restartChild(resumeTask, [ | ||
await restartChild(resumeTask, [ | ||
...getExecArgvWithoutMaxOldSpaceSize(), | ||
@@ -172,3 +175,3 @@ `--max_old_space_size=${crashRecoveryMaxOldSpaceSize}` | ||
else { | ||
restartChild(nextTask, process.execArgv); | ||
await restartChild(nextTask, process.execArgv); | ||
} | ||
@@ -189,5 +192,5 @@ break; | ||
}; | ||
const startChild = (taskAction, execArgv) => { | ||
const startChild = async (taskAction, execArgv) => { | ||
try { | ||
child = child_process_1.fork(workerFile, commandLineArgs, { cwd, execArgv }); | ||
child = child_process_1.fork(workerFile, commandLineArgs, { cwd, execArgv: await getChildProcessExecArgv(i, execArgv) }); | ||
runningChildren.add(child); | ||
@@ -245,3 +248,3 @@ } | ||
}; | ||
const restartChild = (taskAction, execArgv) => { | ||
const restartChild = async (taskAction, execArgv) => { | ||
try { | ||
@@ -251,3 +254,3 @@ assert_1.default(runningChildren.has(child), `${processIndex}> Child not running`); | ||
stopChild(/*done*/ false); | ||
startChild(taskAction, execArgv); | ||
await startChild(taskAction, execArgv); | ||
} | ||
@@ -281,3 +284,3 @@ catch (e) { | ||
}; | ||
startChild(nextTask, process.execArgv); | ||
await startChild(nextTask, process.execArgv); | ||
} | ||
@@ -333,2 +336,68 @@ function fail(err) { | ||
} | ||
async function getChildProcessExecArgv(portOffset = 0, execArgv = process.execArgv) { | ||
const debugArg = execArgv.findIndex(arg => arg === "--inspect" || arg === "--inspect-brk" || arg.startsWith("--inspect=")); | ||
if (debugArg < 0) | ||
return execArgv; | ||
const port = parseInt(execArgv[debugArg].split("=")[1], 10) || 9229; | ||
return [ | ||
...execArgv.slice(0, debugArg), | ||
`--inspect=${await findFreePort(port + 1 + portOffset, 100, 1000)}`, | ||
...execArgv.slice(debugArg + 1) | ||
]; | ||
} | ||
// From VS Code: https://github.com/microsoft/vscode/blob/7d57a8f6f546b5e30027e7cfa87bd834eb5c7bbb/src/vs/base/node/ports.ts | ||
function findFreePort(startPort, giveUpAfter, timeout) { | ||
let done = false; | ||
return new Promise(resolve => { | ||
const timeoutHandle = setTimeout(() => { | ||
if (!done) { | ||
done = true; | ||
return resolve(0); | ||
} | ||
}, timeout); | ||
doFindFreePort(startPort, giveUpAfter, port => { | ||
if (!done) { | ||
done = true; | ||
clearTimeout(timeoutHandle); | ||
return resolve(port); | ||
} | ||
}); | ||
}); | ||
} | ||
function doFindFreePort(startPort, giveUpAfter, clb) { | ||
if (giveUpAfter === 0) { | ||
return clb(0); | ||
} | ||
const client = new net_1.Socket(); | ||
// If we can connect to the port it means the port is already taken so we continue searching | ||
client.once("connect", () => { | ||
dispose(client); | ||
return doFindFreePort(startPort + 1, giveUpAfter - 1, clb); | ||
}); | ||
client.once("data", () => { | ||
// this listener is required since node.js 8.x | ||
}); | ||
client.once("error", (err) => { | ||
dispose(client); | ||
// If we receive any non ECONNREFUSED error, it means the port is used but we cannot connect | ||
if (err.code !== "ECONNREFUSED") { | ||
return doFindFreePort(startPort + 1, giveUpAfter - 1, clb); | ||
} | ||
// Otherwise it means the port is free to use! | ||
return clb(startPort); | ||
}); | ||
client.connect(startPort, "127.0.0.1"); | ||
function dispose(socket) { | ||
try { | ||
socket.removeAllListeners("connect"); | ||
socket.removeAllListeners("error"); | ||
socket.end(); | ||
socket.destroy(); | ||
socket.unref(); | ||
} | ||
catch (error) { | ||
console.error(error); // otherwise this error would get lost in the callback chain | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=process.js.map |
{ | ||
"name": "@definitelytyped/utils", | ||
"version": "0.0.58", | ||
"version": "0.0.59-next.0", | ||
"description": "Shared utilities for DefinitelyTyped tools", | ||
@@ -40,3 +40,3 @@ "homepage": "https://github.com/microsoft/DefinitelyTyped-tools/tree/master/packages/utils#readme", | ||
}, | ||
"gitHead": "90caa4095e638a87ff68f8ee9ddf897bbb8789a9" | ||
"gitHead": "fa0c46e5471e5a3efa6c2d0c956876095014a353" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
137569
2153
5