* Spawns a process and exposes it as a raw WebSocket stream. * * @param {string[]} cmd - Command and arguments to execute (e.g. `["sh", "-c", "echo hi"]`). * @param {(ws: WebSocket) => void} callback - Called with the connected WebSocket once the * process is ready. Use `ws.send()` to
(cmd, callback, onError)
| 21 | * process is ready. Use `ws.send()` to write to stdin and `ws.onmessage` to read stdout. |
| 22 | */ |
| 23 | spawnStream(cmd, callback, onError) { |
| 24 | exec((port) => { |
| 25 | const ws = new WebSocket(`ws://127.0.0.1:${port}`); |
| 26 | ws.binaryType = "arraybuffer"; |
| 27 | |
| 28 | ws.onopen = () => { |
| 29 | callback(ws); |
| 30 | }; |
| 31 | |
| 32 | ws.onerror = (e) => { |
| 33 | if (onError) onError(e); |
| 34 | }; |
| 35 | |
| 36 | }, (err) => { if (onError) onError(err); }, "Executor", "spawn", [cmd]); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /** |