diff --git a/src/formatitko/katex-server/index.mjs b/src/formatitko/katex-server/index.mjs index 6beddf2..ecb4675 100644 --- a/src/formatitko/katex-server/index.mjs +++ b/src/formatitko/katex-server/index.mjs @@ -41,6 +41,7 @@ if (!unixSocketPath) { // This server listens on a Unix socket at /var/run/mysocket var unixServer = net.createServer(handleClient); unixServer.listen(unixSocketPath); +console.log("OK") function handleExit(signal) { // unixServer.emit('close') diff --git a/src/formatitko/katex.py b/src/formatitko/katex.py index 4470e7f..fc9ed4f 100644 --- a/src/formatitko/katex.py +++ b/src/formatitko/katex.py @@ -28,22 +28,16 @@ class KatexClient: raise Exception("npm not found. Node.js is required to use KaTeX.") else: raise e - self._server_process = subprocess.Popen(["node", srcdir + "/katex-server/index.mjs", self._socket_file]) - self._client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self._server_process = subprocess.Popen(["node", srcdir + "/katex-server/index.mjs", self._socket_file], stdout=subprocess.PIPE) - # Wait for the node program to create the socket file - while not os.path.exists(self._socket_file): - time.sleep(0.01) + self._client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + + ok = self._server_process.stdout.readline() + if ok != b"OK\n": + raise Exception("Failed to connect to katex-server") - # Wait for the node program to start accepting connections - while True: - try: - self._client.connect(self._socket_file) - time.sleep(0.01) - except ConnectionRefusedError: - continue - break + self._client.connect(self._socket_file) def render(self, tex: str, options: Dict={}): # Send formulas to translate