Skip to content

Commit 7b584d3

Browse files
committed
1 parent 941d0d7 commit 7b584d3

12 files changed

+153
-105
lines changed

node_modules/foreground-child/dist/cjs/index.js node_modules/foreground-child/dist/commonjs/index.js

+4-37
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ exports.foregroundChild = exports.normalizeFgArgs = void 0;
77
const child_process_1 = require("child_process");
88
const cross_spawn_1 = __importDefault(require("cross-spawn"));
99
const signal_exit_1 = require("signal-exit");
10-
const all_signals_js_1 = require("./all-signals.js");
10+
const proxy_signals_js_1 = require("./proxy-signals.js");
1111
const watchdog_js_1 = require("./watchdog.js");
1212
/* c8 ignore start */
1313
const spawn = process?.platform === 'win32' ? cross_spawn_1.default : child_process_1.spawn;
@@ -50,7 +50,6 @@ function foregroundChild(...fgArgs) {
5050
spawnOpts.stdio.push('ipc');
5151
}
5252
const child = spawn(program, args, spawnOpts);
53-
const unproxySignals = proxySignals(child);
5453
const childHangup = () => {
5554
try {
5655
child.kill('SIGHUP');
@@ -63,20 +62,18 @@ function foregroundChild(...fgArgs) {
6362
/* c8 ignore stop */
6463
};
6564
const removeOnExit = (0, signal_exit_1.onExit)(childHangup);
66-
const dog = (0, watchdog_js_1.watchdog)(child);
65+
(0, proxy_signals_js_1.proxySignals)(child);
66+
(0, watchdog_js_1.watchdog)(child);
6767
let done = false;
6868
child.on('close', async (code, signal) => {
69-
dog.kill('SIGKILL');
7069
/* c8 ignore start */
71-
if (done) {
70+
if (done)
7271
return;
73-
}
7472
/* c8 ignore stop */
7573
done = true;
7674
const result = cleanup(code, signal);
7775
const res = isPromise(result) ? await result : result;
7876
removeOnExit();
79-
unproxySignals();
8077
if (res === false)
8178
return;
8279
else if (typeof res === 'string') {
@@ -120,35 +117,5 @@ function foregroundChild(...fgArgs) {
120117
return child;
121118
}
122119
exports.foregroundChild = foregroundChild;
123-
/**
124-
* Starts forwarding signals to `child` through `parent`.
125-
*/
126-
const proxySignals = (child) => {
127-
const listeners = new Map();
128-
for (const sig of all_signals_js_1.allSignals) {
129-
const listener = () => {
130-
// some signals can only be received, not sent
131-
try {
132-
child.kill(sig);
133-
/* c8 ignore start */
134-
}
135-
catch (_) { }
136-
/* c8 ignore stop */
137-
};
138-
try {
139-
// if it's a signal this system doesn't recognize, skip it
140-
process.on(sig, listener);
141-
listeners.set(sig, listener);
142-
/* c8 ignore start */
143-
}
144-
catch (_) { }
145-
/* c8 ignore stop */
146-
}
147-
return () => {
148-
for (const [sig, listener] of listeners) {
149-
process.removeListener(sig, listener);
150-
}
151-
};
152-
};
153120
const isPromise = (o) => !!o && typeof o === 'object' && typeof o.then === 'function';
154121
//# sourceMappingURL=index.js.map
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.proxySignals = void 0;
4+
const all_signals_js_1 = require("./all-signals.js");
5+
/**
6+
* Starts forwarding signals to `child` through `parent`.
7+
*/
8+
const proxySignals = (child) => {
9+
const listeners = new Map();
10+
for (const sig of all_signals_js_1.allSignals) {
11+
const listener = () => {
12+
// some signals can only be received, not sent
13+
try {
14+
child.kill(sig);
15+
/* c8 ignore start */
16+
}
17+
catch (_) { }
18+
/* c8 ignore stop */
19+
};
20+
try {
21+
// if it's a signal this system doesn't recognize, skip it
22+
process.on(sig, listener);
23+
listeners.set(sig, listener);
24+
/* c8 ignore start */
25+
}
26+
catch (_) { }
27+
/* c8 ignore stop */
28+
}
29+
const unproxy = () => {
30+
for (const [sig, listener] of listeners) {
31+
process.removeListener(sig, listener);
32+
}
33+
};
34+
child.on('exit', unproxy);
35+
return unproxy;
36+
};
37+
exports.proxySignals = proxySignals;
38+
//# sourceMappingURL=proxy-signals.js.map

node_modules/foreground-child/dist/cjs/watchdog.js node_modules/foreground-child/dist/commonjs/watchdog.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ if (!isNaN(pid)) {
2727
process.on('SIGHUP', bark)
2828
}
2929
`;
30+
/**
31+
* Pass in a ChildProcess, and this will spawn a watchdog process that
32+
* will make sure it exits if the parent does, thus preventing any
33+
* dangling detached zombie processes.
34+
*
35+
* If the child ends before the parent, then the watchdog will terminate.
36+
*/
3037
const watchdog = (child) => {
3138
let dogExited = false;
3239
const dog = (0, child_process_1.spawn)(process.execPath, ['-e', watchdogCode, String(child.pid)], {
@@ -35,7 +42,7 @@ const watchdog = (child) => {
3542
dog.on('exit', () => (dogExited = true));
3643
child.on('exit', () => {
3744
if (!dogExited)
38-
dog.kill('SIGTERM');
45+
dog.kill('SIGKILL');
3946
});
4047
return dog;
4148
};

node_modules/foreground-child/dist/mjs/index.js node_modules/foreground-child/dist/esm/index.js

+4-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { spawn as nodeSpawn, } from 'child_process';
22
import crossSpawn from 'cross-spawn';
33
import { onExit } from 'signal-exit';
4-
import { allSignals } from './all-signals.js';
4+
import { proxySignals } from './proxy-signals.js';
55
import { watchdog } from './watchdog.js';
66
/* c8 ignore start */
77
const spawn = process?.platform === 'win32' ? crossSpawn : nodeSpawn;
@@ -43,7 +43,6 @@ export function foregroundChild(...fgArgs) {
4343
spawnOpts.stdio.push('ipc');
4444
}
4545
const child = spawn(program, args, spawnOpts);
46-
const unproxySignals = proxySignals(child);
4746
const childHangup = () => {
4847
try {
4948
child.kill('SIGHUP');
@@ -56,20 +55,18 @@ export function foregroundChild(...fgArgs) {
5655
/* c8 ignore stop */
5756
};
5857
const removeOnExit = onExit(childHangup);
59-
const dog = watchdog(child);
58+
proxySignals(child);
59+
watchdog(child);
6060
let done = false;
6161
child.on('close', async (code, signal) => {
62-
dog.kill('SIGKILL');
6362
/* c8 ignore start */
64-
if (done) {
63+
if (done)
6564
return;
66-
}
6765
/* c8 ignore stop */
6866
done = true;
6967
const result = cleanup(code, signal);
7068
const res = isPromise(result) ? await result : result;
7169
removeOnExit();
72-
unproxySignals();
7370
if (res === false)
7471
return;
7572
else if (typeof res === 'string') {
@@ -112,35 +109,5 @@ export function foregroundChild(...fgArgs) {
112109
}
113110
return child;
114111
}
115-
/**
116-
* Starts forwarding signals to `child` through `parent`.
117-
*/
118-
const proxySignals = (child) => {
119-
const listeners = new Map();
120-
for (const sig of allSignals) {
121-
const listener = () => {
122-
// some signals can only be received, not sent
123-
try {
124-
child.kill(sig);
125-
/* c8 ignore start */
126-
}
127-
catch (_) { }
128-
/* c8 ignore stop */
129-
};
130-
try {
131-
// if it's a signal this system doesn't recognize, skip it
132-
process.on(sig, listener);
133-
listeners.set(sig, listener);
134-
/* c8 ignore start */
135-
}
136-
catch (_) { }
137-
/* c8 ignore stop */
138-
}
139-
return () => {
140-
for (const [sig, listener] of listeners) {
141-
process.removeListener(sig, listener);
142-
}
143-
};
144-
};
145112
const isPromise = (o) => !!o && typeof o === 'object' && typeof o.then === 'function';
146113
//# sourceMappingURL=index.js.map
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { allSignals } from './all-signals.js';
2+
/**
3+
* Starts forwarding signals to `child` through `parent`.
4+
*/
5+
export const proxySignals = (child) => {
6+
const listeners = new Map();
7+
for (const sig of allSignals) {
8+
const listener = () => {
9+
// some signals can only be received, not sent
10+
try {
11+
child.kill(sig);
12+
/* c8 ignore start */
13+
}
14+
catch (_) { }
15+
/* c8 ignore stop */
16+
};
17+
try {
18+
// if it's a signal this system doesn't recognize, skip it
19+
process.on(sig, listener);
20+
listeners.set(sig, listener);
21+
/* c8 ignore start */
22+
}
23+
catch (_) { }
24+
/* c8 ignore stop */
25+
}
26+
const unproxy = () => {
27+
for (const [sig, listener] of listeners) {
28+
process.removeListener(sig, listener);
29+
}
30+
};
31+
child.on('exit', unproxy);
32+
return unproxy;
33+
};
34+
//# sourceMappingURL=proxy-signals.js.map

node_modules/foreground-child/dist/mjs/watchdog.js node_modules/foreground-child/dist/esm/watchdog.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ if (!isNaN(pid)) {
2424
process.on('SIGHUP', bark)
2525
}
2626
`;
27+
/**
28+
* Pass in a ChildProcess, and this will spawn a watchdog process that
29+
* will make sure it exits if the parent does, thus preventing any
30+
* dangling detached zombie processes.
31+
*
32+
* If the child ends before the parent, then the watchdog will terminate.
33+
*/
2734
export const watchdog = (child) => {
2835
let dogExited = false;
2936
const dog = spawn(process.execPath, ['-e', watchdogCode, String(child.pid)], {
@@ -32,7 +39,7 @@ export const watchdog = (child) => {
3239
dog.on('exit', () => (dogExited = true));
3340
child.on('exit', () => {
3441
if (!dogExited)
35-
dog.kill('SIGTERM');
42+
dog.kill('SIGKILL');
3643
});
3744
return dog;
3845
};
+54-26
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
11
{
22
"name": "foreground-child",
3-
"version": "3.1.1",
3+
"version": "3.2.1",
44
"description": "Run a child as if it's the foreground process. Give it stdio. Exit when it exits.",
5-
"main": "./dist/cjs/index.js",
6-
"module": "./dist/mjs/index.js",
7-
"types": "./dist/mjs/index.d.ts",
5+
"main": "./dist/commonjs/index.js",
6+
"types": "./dist/commonjs/index.d.ts",
87
"exports": {
8+
"./watchdog": {
9+
"import": {
10+
"source": "./src/watchdog.ts",
11+
"types": "./dist/esm/watchdog.d.ts",
12+
"default": "./dist/esm/watchdog.js"
13+
},
14+
"require": {
15+
"source": "./src/watchdog.ts",
16+
"types": "./dist/commonjs/watchdog.d.ts",
17+
"default": "./dist/commonjs/watchdog.js"
18+
}
19+
},
20+
"./proxy-signals": {
21+
"import": {
22+
"source": "./src/proxy-signals.ts",
23+
"types": "./dist/esm/proxy-signals.d.ts",
24+
"default": "./dist/esm/proxy-signals.js"
25+
},
26+
"require": {
27+
"source": "./src/proxy-signals.ts",
28+
"types": "./dist/commonjs/proxy-signals.d.ts",
29+
"default": "./dist/commonjs/proxy-signals.js"
30+
}
31+
},
32+
"./package.json": "./package.json",
933
".": {
1034
"import": {
11-
"types": "./dist/mjs/index.d.ts",
12-
"default": "./dist/mjs/index.js"
35+
"source": "./src/index.ts",
36+
"types": "./dist/esm/index.d.ts",
37+
"default": "./dist/esm/index.js"
1338
},
1439
"require": {
15-
"types": "./dist/cjs/index.d.ts",
16-
"default": "./dist/cjs/index.js"
40+
"source": "./src/index.ts",
41+
"types": "./dist/commonjs/index.d.ts",
42+
"default": "./dist/commonjs/index.js"
1743
}
1844
}
1945
},
@@ -31,15 +57,16 @@
3157
"preversion": "npm test",
3258
"postversion": "npm publish",
3359
"prepublishOnly": "git push origin --follow-tags",
34-
"prepare": "tsc -p tsconfig.json && tsc -p tsconfig-esm.json && bash ./scripts/fixup.sh",
60+
"prepare": "tshy",
3561
"pretest": "npm run prepare",
3662
"presnap": "npm run prepare",
37-
"test": "c8 tap",
38-
"snap": "c8 tap",
39-
"format": "prettier --write . --loglevel warn",
40-
"typedoc": "typedoc --tsconfig tsconfig-esm.json ./src/*.ts"
63+
"test": "tap",
64+
"snap": "tap",
65+
"format": "prettier --write . --log-level warn",
66+
"typedoc": "typedoc --tsconfig .tshy/esm.json ./src/*.ts"
4167
},
4268
"prettier": {
69+
"experimentalTernaries": true,
4370
"semi": false,
4471
"printWidth": 75,
4572
"tabWidth": 2,
@@ -51,14 +78,7 @@
5178
"endOfLine": "lf"
5279
},
5380
"tap": {
54-
"coverage": false,
55-
"jobs": 1,
56-
"node-arg": [
57-
"--no-warnings",
58-
"--loader",
59-
"ts-node/esm"
60-
],
61-
"ts": false
81+
"typecheck": true
6282
},
6383
"repository": {
6484
"type": "git",
@@ -70,14 +90,22 @@
7090
"@types/cross-spawn": "^6.0.2",
7191
"@types/node": "^18.15.11",
7292
"@types/tap": "^15.0.8",
73-
"c8": "^7.13.0",
74-
"prettier": "^2.8.6",
75-
"tap": "^16.3.4",
76-
"ts-node": "^10.9.1",
93+
"prettier": "^3.3.2",
94+
"tap": "^19.2.5",
95+
"tshy": "^1.15.1",
7796
"typedoc": "^0.24.2",
7897
"typescript": "^5.0.2"
7998
},
8099
"funding": {
81100
"url": "https://github.com/sponsors/isaacs"
82-
}
101+
},
102+
"tshy": {
103+
"exports": {
104+
"./watchdog": "./src/watchdog.ts",
105+
"./proxy-signals": "./src/proxy-signals.ts",
106+
"./package.json": "./package.json",
107+
".": "./src/index.ts"
108+
}
109+
},
110+
"type": "module"
83111
}

0 commit comments

Comments
 (0)