|
| 1 | +import { spawn } from "node:child_process" |
| 2 | +import { fileURLToPath } from "node:url" |
| 3 | + |
| 4 | +export type PtyExit = { |
| 5 | + readonly exitCode: number | null |
| 6 | + readonly signal: number | null |
| 7 | +} |
| 8 | + |
| 9 | +export type PtyBridge = { |
| 10 | + readonly kill: () => void |
| 11 | + readonly onData: (handler: (data: string) => void) => void |
| 12 | + readonly onExit: (handler: (exit: PtyExit) => void) => void |
| 13 | + readonly resize: (cols: number, rows: number) => void |
| 14 | + readonly write: (data: string) => void |
| 15 | +} |
| 16 | + |
| 17 | +type PtyBridgeSpec = { |
| 18 | + readonly args: ReadonlyArray<string> |
| 19 | + readonly cols: number |
| 20 | + readonly command: string |
| 21 | + readonly cwd: string |
| 22 | + readonly rows: number |
| 23 | +} |
| 24 | + |
| 25 | +type HelperOutputMessage = |
| 26 | + | { readonly type: "data"; readonly data: string } |
| 27 | + | { readonly type: "error"; readonly message: string } |
| 28 | + | { readonly type: "exit"; readonly exitCode: number | null; readonly signal: number | null } |
| 29 | + |
| 30 | +const helperPath = fileURLToPath(new URL("./pty-helper.js", import.meta.url)) |
| 31 | +const nodeCommand = process.env["DOCKER_GIT_NODE_BINARY"]?.trim() || "node" |
| 32 | + |
| 33 | +const encodeStartSpec = (spec: PtyBridgeSpec): string => |
| 34 | + Buffer.from(JSON.stringify(spec), "utf8").toString("base64url") |
| 35 | + |
| 36 | +const encodeInput = (data: string): string => Buffer.from(data, "utf8").toString("base64") |
| 37 | + |
| 38 | +const encodeCommand = (message: object): string => `${JSON.stringify(message)}\n` |
| 39 | + |
| 40 | +const decodeOutputMessage = (line: string): HelperOutputMessage | null => { |
| 41 | + try { |
| 42 | + return JSON.parse(line) as HelperOutputMessage |
| 43 | + } catch { |
| 44 | + return null |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +const decodeOutputData = (data: string): string => Buffer.from(data, "base64").toString("utf8") |
| 49 | + |
| 50 | +export const spawnPtyBridge = (spec: PtyBridgeSpec): PtyBridge => { |
| 51 | + const child = spawn(nodeCommand, [helperPath, encodeStartSpec(spec)], { |
| 52 | + cwd: spec.cwd, |
| 53 | + env: { |
| 54 | + ...process.env, |
| 55 | + TERM: "xterm-256color" |
| 56 | + }, |
| 57 | + stdio: ["pipe", "pipe", "pipe"] |
| 58 | + }) |
| 59 | + const dataHandlers = new Set<(data: string) => void>() |
| 60 | + const exitHandlers = new Set<(exit: PtyExit) => void>() |
| 61 | + let exitSeen = false |
| 62 | + let pending = "" |
| 63 | + |
| 64 | + const emitData = (data: string): void => { |
| 65 | + for (const handler of dataHandlers) { |
| 66 | + handler(data) |
| 67 | + } |
| 68 | + } |
| 69 | + const emitExit = (exit: PtyExit): void => { |
| 70 | + if (exitSeen) { |
| 71 | + return |
| 72 | + } |
| 73 | + exitSeen = true |
| 74 | + for (const handler of exitHandlers) { |
| 75 | + handler(exit) |
| 76 | + } |
| 77 | + } |
| 78 | + const sendCommand = (message: object): void => { |
| 79 | + if (!child.stdin.destroyed) { |
| 80 | + child.stdin.write(encodeCommand(message)) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + child.stdout.setEncoding("utf8") |
| 85 | + child.stdout.on("data", (chunk) => { |
| 86 | + pending += chunk |
| 87 | + const lines = pending.split("\n") |
| 88 | + pending = lines.pop() ?? "" |
| 89 | + for (const line of lines) { |
| 90 | + const message = decodeOutputMessage(line) |
| 91 | + if (message === null) { |
| 92 | + continue |
| 93 | + } |
| 94 | + if (message.type === "data") { |
| 95 | + emitData(decodeOutputData(message.data)) |
| 96 | + } else if (message.type === "error") { |
| 97 | + emitData(`\r\n[pty helper error] ${message.message}\r\n`) |
| 98 | + } else { |
| 99 | + emitExit({ exitCode: message.exitCode, signal: message.signal }) |
| 100 | + } |
| 101 | + } |
| 102 | + }) |
| 103 | + child.stderr.on("data", (chunk) => { |
| 104 | + emitData(`\r\n[pty helper stderr] ${String(chunk)}\r\n`) |
| 105 | + }) |
| 106 | + child.on("exit", (exitCode) => { |
| 107 | + emitExit({ exitCode, signal: null }) |
| 108 | + }) |
| 109 | + |
| 110 | + return { |
| 111 | + kill: () => { |
| 112 | + sendCommand({ type: "kill" }) |
| 113 | + child.kill() |
| 114 | + }, |
| 115 | + onData: (handler) => { |
| 116 | + dataHandlers.add(handler) |
| 117 | + }, |
| 118 | + onExit: (handler) => { |
| 119 | + exitHandlers.add(handler) |
| 120 | + }, |
| 121 | + resize: (cols, rows) => { |
| 122 | + sendCommand({ type: "resize", cols, rows }) |
| 123 | + }, |
| 124 | + write: (data) => { |
| 125 | + sendCommand({ type: "input", data: encodeInput(data) }) |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments