aboutsummarylogtreecommitdiffstats
path: root/preload.js
blob: ae186cf8a16afe0eac95a9a24df9211e7c59bfb2 (plain)
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
const childProcess = require("child_process");
const { contextBridge } = require("electron");
const readline = require("readline");

function startStockfish() {
	const proc = childProcess.exec("fairy-stockfish");
	const reader = readline.createInterface(proc.stdout);

	console.log("starting stockfish");

	reader.on("line", line => {
		const evt = new CustomEvent("stockfish-outer", {detail: line});
		window.dispatchEvent(evt);
	});

	return {
		send: line => {
			console.log("sending", line);
			proc.stdin.write(line + "\r\n");
		},
		addListener: listener => reader.on("line", listener),
		removeListener: listener => reader.off("line", listener),
		kill: () => proc.kill(),
	};
}

contextBridge.exposeInMainWorld("startStockfish", startStockfish);