Files
fx_cast/app/src/daemon/index.ts
Matt Hensman 474dbad1aa Add snap support (#60)
* Initial app daemon implementation

* Pass script path to child bridge processes

* Change WebSocket server port

* Fix error sending message whilst WebSocket connection is closing

* Initial ext daemon connection implementation
2019-05-15 11:30:30 +01:00

62 lines
1.5 KiB
TypeScript

"use strict";
import { spawn } from "child_process";
import { Readable } from "stream";
import path from "path";
import WebSocket from "ws";
import { DecodeTransform
, EncodeTransform } from "../transforms";
const wss = new WebSocket.Server({ port: 9556 });
wss.on("connection", socket => {
/**
* Daemon and bridge are the same binary, so spawn a new
* version of self in bridge mode.
*/
const bridge = spawn(process.execPath, [ process.argv[1] ]);
// Stream for incoming WebSocket messages
const messageStream = new Readable({
objectMode: true
});
// tslint:disable-next-line:no-empty
messageStream._read = () => {};
/**
* Incoming JSON messages from the extension over the
* WebSocket connection are parsed and re-encoded to be sent
* to bridge stdin.
*/
socket.on("message", (message: string) => {
messageStream.push(JSON.parse(message));
});
messageStream
.pipe(new EncodeTransform())
.pipe(bridge.stdin);
/**
* Incoming messages from the bridge are decoded and
* stringified and sent to the extension over the WebSocket
* connection.
*/
bridge.stdout
.pipe(new DecodeTransform())
.on("data", data => {
// Socket can be CLOSING here
if (socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify(data));
}
});
// Handle termination
socket.on("close", () => bridge.kill());
bridge.on("exit", () => socket.close());
});