-
Notifications
You must be signed in to change notification settings - Fork 1
/
fork.mjs
60 lines (57 loc) · 1.54 KB
/
fork.mjs
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
import z from "zod";
import {fork} from "node:child_process";
import {internalEventsDTOSchema, externalEventsSchema} from "./crates/bsnext_client/generated/schema.js";
const m = fork('./bin.js', ['-f', 'json'], {stdio: 'pipe'});
const either = z.union([internalEventsDTOSchema, externalEventsSchema]);
const lines = []
/**
* @param {import("zod").infer<either>} input
*/
function handle(input) {
switch (input.kind) {
case "ServersChanged":
process.send(input);
}
}
m.stdout.on('data', (chunk) => {
for (let line of chunk.toString().split('\n')) {
if (line.trim() === "") continue;
lines.push(line);
console.log(line);
const json = JSON.parse(line);
const parsed = either.safeParse(json);
if (parsed.error) {
// console.log(parsed.error)
} else {
handle(parsed.data)
}
}
})
m.stderr.on('data', (e) => {
console.log("error", e.toString())
})
m.on('spawn', (...args) => {
console.log('did spawn', ...args)
setTimeout(() => {
console.log('will kill')
m.kill();
}, 2000)
})
m.on('disconnect', (...args) => {
console.log('did disconnect', ...args)
})
m.on('close', (err, signal) => {
if (err) {
if (err !== 0) {
console.log('did close with error code', err)
process.exit(err)
}
}
console.log('did close', {err, signal})
})
m.on('exit', (err, signal) => {
console.log('did exit', {err, signal})
})
m.on('error', (err) => {
console.error('did error', err)
})