-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
61 lines (47 loc) · 1.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import * as cp from "child_process"
const childEnvName = "_IS_CHILD"
const isChild = process.env[childEnvName]
function log(...args: any[]) {
const name = isChild ? "child" : "parent"
console.log(`[${name}:${process.pid}]`, ...args)
}
process.on("exit", () => log("exiting"))
function setupChild() {
setInterval(() => log("waiting"), 1000)
setTimeout(() => {
log("never got message")
process.exit(1)
}, 10000)
process.on("message", (m) => {
log("received message", m)
process.exit(0)
})
process.send({ fromChild: true })
}
function setupParent() {
const child = cp.spawn(process.argv[0], process.argv.slice(1), {
env: {
[childEnvName]: "true",
...process.env,
},
stdio: ["ipc"],
})
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)
child.on("message", (m) => {
log("received message", m)
child.send({ fromParent: true })
})
// Exit with the same code.
child.on("exit", process.exit)
// Same problem if we immediately send a message; waiting to hear from the
// child process is to demonstrate the messages work one way and to make sure
// there isn't some kind of timing issue with sending too quickly.
// child.send({ fromParent: true })
}
log("setting up")
if (isChild) {
setupChild()
} else {
setupParent()
}