-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-http-worker.ts
More file actions
65 lines (57 loc) · 1.82 KB
/
start-http-worker.ts
File metadata and controls
65 lines (57 loc) · 1.82 KB
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
62
63
64
65
import { spawn } from "child_process";
import { createServer } from "http";
import { Writable } from "stream";
const workers = new Map<string, Writable>();
const port = process.env.PORT ? Number(process.env.PORT) : 6969;
const cmd = process.argv[2] || "stockfish";
/**
* Wraps an engine command (or actually *any* command)
* into a simple HTTP server that pipes:
* - STDOUT -> EventSource-compatible stream (SSE)
* - Request body -> STDIN
*
* The request URL determines session ID
* Closing SSE connection will terminate the session
*
* WARN: GET here is NOT idempotent (creates process on-the-fly)
* Because EventSource can't send POST request,
* and doing it via plain fetch is not very ergonomic
*/
const server = createServer(async (req, res) => {
const id = req.url!;
if (req.method === "GET") {
console.log(`Spawn worker ${id}`);
const ps = spawn(cmd);
workers.set(id, ps.stdin);
res.writeHead(200, "OK", {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
});
ps.stdout.on("data", (chunk: Uint8Array) => {
chunk
.toString()
.trim()
.split("\n")
.forEach((line) => {
res.write(`data: ${line}\n\n`);
});
});
ps.on("exit", (code) => {
console.log(`Worker ${id} exited with code ${code}`);
res.end();
});
req.on("close", () => {
console.log(`Terminate worker ${id}`);
ps.kill();
});
} else if (req.method === "POST") {
const stdin = workers.get(id);
if (!stdin) throw new Error(`No such worker ${id}`);
req.on("data", (chunk) => stdin.write(chunk));
req.on("end", () => stdin.write("\nucinewgame\n"));
req.on("end", () => res.end());
}
});
server.listen(port);
console.log(`Server started at port ${port} for CMD: ${cmd}`);