Skip to content

Commit

Permalink
deps: foreground-child@3.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jun 27, 2024
1 parent 941d0d7 commit 7b584d3
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exports.foregroundChild = exports.normalizeFgArgs = void 0;
const child_process_1 = require("child_process");
const cross_spawn_1 = __importDefault(require("cross-spawn"));
const signal_exit_1 = require("signal-exit");
const all_signals_js_1 = require("./all-signals.js");
const proxy_signals_js_1 = require("./proxy-signals.js");
const watchdog_js_1 = require("./watchdog.js");
/* c8 ignore start */
const spawn = process?.platform === 'win32' ? cross_spawn_1.default : child_process_1.spawn;
Expand Down Expand Up @@ -50,7 +50,6 @@ function foregroundChild(...fgArgs) {
spawnOpts.stdio.push('ipc');
}
const child = spawn(program, args, spawnOpts);
const unproxySignals = proxySignals(child);
const childHangup = () => {
try {
child.kill('SIGHUP');
Expand All @@ -63,20 +62,18 @@ function foregroundChild(...fgArgs) {
/* c8 ignore stop */
};
const removeOnExit = (0, signal_exit_1.onExit)(childHangup);
const dog = (0, watchdog_js_1.watchdog)(child);
(0, proxy_signals_js_1.proxySignals)(child);
(0, watchdog_js_1.watchdog)(child);
let done = false;
child.on('close', async (code, signal) => {
dog.kill('SIGKILL');
/* c8 ignore start */
if (done) {
if (done)
return;
}
/* c8 ignore stop */
done = true;
const result = cleanup(code, signal);
const res = isPromise(result) ? await result : result;
removeOnExit();
unproxySignals();
if (res === false)
return;
else if (typeof res === 'string') {
Expand Down Expand Up @@ -120,35 +117,5 @@ function foregroundChild(...fgArgs) {
return child;
}
exports.foregroundChild = foregroundChild;
/**
* Starts forwarding signals to `child` through `parent`.
*/
const proxySignals = (child) => {
const listeners = new Map();
for (const sig of all_signals_js_1.allSignals) {
const listener = () => {
// some signals can only be received, not sent
try {
child.kill(sig);
/* c8 ignore start */
}
catch (_) { }
/* c8 ignore stop */
};
try {
// if it's a signal this system doesn't recognize, skip it
process.on(sig, listener);
listeners.set(sig, listener);
/* c8 ignore start */
}
catch (_) { }
/* c8 ignore stop */
}
return () => {
for (const [sig, listener] of listeners) {
process.removeListener(sig, listener);
}
};
};
const isPromise = (o) => !!o && typeof o === 'object' && typeof o.then === 'function';
//# sourceMappingURL=index.js.map
38 changes: 38 additions & 0 deletions node_modules/foreground-child/dist/commonjs/proxy-signals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.proxySignals = void 0;
const all_signals_js_1 = require("./all-signals.js");
/**
* Starts forwarding signals to `child` through `parent`.
*/
const proxySignals = (child) => {
const listeners = new Map();
for (const sig of all_signals_js_1.allSignals) {
const listener = () => {
// some signals can only be received, not sent
try {
child.kill(sig);
/* c8 ignore start */
}
catch (_) { }
/* c8 ignore stop */
};
try {
// if it's a signal this system doesn't recognize, skip it
process.on(sig, listener);
listeners.set(sig, listener);
/* c8 ignore start */
}
catch (_) { }
/* c8 ignore stop */
}
const unproxy = () => {
for (const [sig, listener] of listeners) {
process.removeListener(sig, listener);
}
};
child.on('exit', unproxy);
return unproxy;
};
exports.proxySignals = proxySignals;
//# sourceMappingURL=proxy-signals.js.map
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ if (!isNaN(pid)) {
process.on('SIGHUP', bark)
}
`;
/**
* Pass in a ChildProcess, and this will spawn a watchdog process that
* will make sure it exits if the parent does, thus preventing any
* dangling detached zombie processes.
*
* If the child ends before the parent, then the watchdog will terminate.
*/
const watchdog = (child) => {
let dogExited = false;
const dog = (0, child_process_1.spawn)(process.execPath, ['-e', watchdogCode, String(child.pid)], {
Expand All @@ -35,7 +42,7 @@ const watchdog = (child) => {
dog.on('exit', () => (dogExited = true));
child.on('exit', () => {
if (!dogExited)
dog.kill('SIGTERM');
dog.kill('SIGKILL');
});
return dog;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spawn as nodeSpawn, } from 'child_process';
import crossSpawn from 'cross-spawn';
import { onExit } from 'signal-exit';
import { allSignals } from './all-signals.js';
import { proxySignals } from './proxy-signals.js';
import { watchdog } from './watchdog.js';
/* c8 ignore start */
const spawn = process?.platform === 'win32' ? crossSpawn : nodeSpawn;
Expand Down Expand Up @@ -43,7 +43,6 @@ export function foregroundChild(...fgArgs) {
spawnOpts.stdio.push('ipc');
}
const child = spawn(program, args, spawnOpts);
const unproxySignals = proxySignals(child);
const childHangup = () => {
try {
child.kill('SIGHUP');
Expand All @@ -56,20 +55,18 @@ export function foregroundChild(...fgArgs) {
/* c8 ignore stop */
};
const removeOnExit = onExit(childHangup);
const dog = watchdog(child);
proxySignals(child);
watchdog(child);
let done = false;
child.on('close', async (code, signal) => {
dog.kill('SIGKILL');
/* c8 ignore start */
if (done) {
if (done)
return;
}
/* c8 ignore stop */
done = true;
const result = cleanup(code, signal);
const res = isPromise(result) ? await result : result;
removeOnExit();
unproxySignals();
if (res === false)
return;
else if (typeof res === 'string') {
Expand Down Expand Up @@ -112,35 +109,5 @@ export function foregroundChild(...fgArgs) {
}
return child;
}
/**
* Starts forwarding signals to `child` through `parent`.
*/
const proxySignals = (child) => {
const listeners = new Map();
for (const sig of allSignals) {
const listener = () => {
// some signals can only be received, not sent
try {
child.kill(sig);
/* c8 ignore start */
}
catch (_) { }
/* c8 ignore stop */
};
try {
// if it's a signal this system doesn't recognize, skip it
process.on(sig, listener);
listeners.set(sig, listener);
/* c8 ignore start */
}
catch (_) { }
/* c8 ignore stop */
}
return () => {
for (const [sig, listener] of listeners) {
process.removeListener(sig, listener);
}
};
};
const isPromise = (o) => !!o && typeof o === 'object' && typeof o.then === 'function';
//# sourceMappingURL=index.js.map
34 changes: 34 additions & 0 deletions node_modules/foreground-child/dist/esm/proxy-signals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { allSignals } from './all-signals.js';
/**
* Starts forwarding signals to `child` through `parent`.
*/
export const proxySignals = (child) => {
const listeners = new Map();
for (const sig of allSignals) {
const listener = () => {
// some signals can only be received, not sent
try {
child.kill(sig);
/* c8 ignore start */
}
catch (_) { }
/* c8 ignore stop */
};
try {
// if it's a signal this system doesn't recognize, skip it
process.on(sig, listener);
listeners.set(sig, listener);
/* c8 ignore start */
}
catch (_) { }
/* c8 ignore stop */
}
const unproxy = () => {
for (const [sig, listener] of listeners) {
process.removeListener(sig, listener);
}
};
child.on('exit', unproxy);
return unproxy;
};
//# sourceMappingURL=proxy-signals.js.map
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ if (!isNaN(pid)) {
process.on('SIGHUP', bark)
}
`;
/**
* Pass in a ChildProcess, and this will spawn a watchdog process that
* will make sure it exits if the parent does, thus preventing any
* dangling detached zombie processes.
*
* If the child ends before the parent, then the watchdog will terminate.
*/
export const watchdog = (child) => {
let dogExited = false;
const dog = spawn(process.execPath, ['-e', watchdogCode, String(child.pid)], {
Expand All @@ -32,7 +39,7 @@ export const watchdog = (child) => {
dog.on('exit', () => (dogExited = true));
child.on('exit', () => {
if (!dogExited)
dog.kill('SIGTERM');
dog.kill('SIGKILL');
});
return dog;
};
Expand Down
80 changes: 54 additions & 26 deletions node_modules/foreground-child/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
{
"name": "foreground-child",
"version": "3.1.1",
"version": "3.2.1",
"description": "Run a child as if it's the foreground process. Give it stdio. Exit when it exits.",
"main": "./dist/cjs/index.js",
"module": "./dist/mjs/index.js",
"types": "./dist/mjs/index.d.ts",
"main": "./dist/commonjs/index.js",
"types": "./dist/commonjs/index.d.ts",
"exports": {
"./watchdog": {
"import": {
"source": "./src/watchdog.ts",
"types": "./dist/esm/watchdog.d.ts",
"default": "./dist/esm/watchdog.js"
},
"require": {
"source": "./src/watchdog.ts",
"types": "./dist/commonjs/watchdog.d.ts",
"default": "./dist/commonjs/watchdog.js"
}
},
"./proxy-signals": {
"import": {
"source": "./src/proxy-signals.ts",
"types": "./dist/esm/proxy-signals.d.ts",
"default": "./dist/esm/proxy-signals.js"
},
"require": {
"source": "./src/proxy-signals.ts",
"types": "./dist/commonjs/proxy-signals.d.ts",
"default": "./dist/commonjs/proxy-signals.js"
}
},
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/mjs/index.d.ts",
"default": "./dist/mjs/index.js"
"source": "./src/index.ts",
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.js"
"source": "./src/index.ts",
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
}
},
Expand All @@ -31,15 +57,16 @@
"preversion": "npm test",
"postversion": "npm publish",
"prepublishOnly": "git push origin --follow-tags",
"prepare": "tsc -p tsconfig.json && tsc -p tsconfig-esm.json && bash ./scripts/fixup.sh",
"prepare": "tshy",
"pretest": "npm run prepare",
"presnap": "npm run prepare",
"test": "c8 tap",
"snap": "c8 tap",
"format": "prettier --write . --loglevel warn",
"typedoc": "typedoc --tsconfig tsconfig-esm.json ./src/*.ts"
"test": "tap",
"snap": "tap",
"format": "prettier --write . --log-level warn",
"typedoc": "typedoc --tsconfig .tshy/esm.json ./src/*.ts"
},
"prettier": {
"experimentalTernaries": true,
"semi": false,
"printWidth": 75,
"tabWidth": 2,
Expand All @@ -51,14 +78,7 @@
"endOfLine": "lf"
},
"tap": {
"coverage": false,
"jobs": 1,
"node-arg": [
"--no-warnings",
"--loader",
"ts-node/esm"
],
"ts": false
"typecheck": true
},
"repository": {
"type": "git",
Expand All @@ -70,14 +90,22 @@
"@types/cross-spawn": "^6.0.2",
"@types/node": "^18.15.11",
"@types/tap": "^15.0.8",
"c8": "^7.13.0",
"prettier": "^2.8.6",
"tap": "^16.3.4",
"ts-node": "^10.9.1",
"prettier": "^3.3.2",
"tap": "^19.2.5",
"tshy": "^1.15.1",
"typedoc": "^0.24.2",
"typescript": "^5.0.2"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
}
},
"tshy": {
"exports": {
"./watchdog": "./src/watchdog.ts",
"./proxy-signals": "./src/proxy-signals.ts",
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"type": "module"
}
Loading

0 comments on commit 7b584d3

Please # to comment.