|
| 1 | +import { Socket, createConnection } from "node:net"; |
| 2 | + |
| 3 | +export enum ControlCommand { |
| 4 | + Ping = 1, |
| 5 | + Quit = 2, |
| 6 | + TestSynctex = 10, |
| 7 | + TestSearch = 11, |
| 8 | + TestDest = 12, |
| 9 | + TestNamedDest = 13, |
| 10 | + TestChm = 14, |
| 11 | +} |
| 12 | + |
| 13 | +export type ControlArg = number | string | Uint8Array | ControlArg[]; |
| 14 | + |
| 15 | +const enum ArgType { |
| 16 | + End = 0, |
| 17 | + Int32 = 1, |
| 18 | + Bytes = 2, |
| 19 | + String = 3, |
| 20 | + List = 4, |
| 21 | +} |
| 22 | + |
| 23 | +function appendU16(out: number[], v: number): void { |
| 24 | + out.push(v & 0xff, (v >>> 8) & 0xff); |
| 25 | +} |
| 26 | + |
| 27 | +function appendU32(out: number[], v: number): void { |
| 28 | + out.push(v & 0xff, (v >>> 8) & 0xff, (v >>> 16) & 0xff, (v >>> 24) & 0xff); |
| 29 | +} |
| 30 | + |
| 31 | +function appendBytes(out: number[], bytes: Uint8Array): void { |
| 32 | + for (const b of bytes) { |
| 33 | + out.push(b); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function encodeArg(out: number[], arg: ControlArg): void { |
| 38 | + if (typeof arg === "number") { |
| 39 | + appendU16(out, ArgType.Int32); |
| 40 | + appendU32(out, arg | 0); |
| 41 | + return; |
| 42 | + } |
| 43 | + if (typeof arg === "string") { |
| 44 | + const bytes = new TextEncoder().encode(arg); |
| 45 | + appendU16(out, ArgType.String); |
| 46 | + appendU32(out, bytes.length); |
| 47 | + appendBytes(out, bytes); |
| 48 | + out.push(0); |
| 49 | + return; |
| 50 | + } |
| 51 | + if (Array.isArray(arg)) { |
| 52 | + appendU16(out, ArgType.List); |
| 53 | + appendU16(out, arg.length); |
| 54 | + for (const el of arg) { |
| 55 | + encodeArg(out, el); |
| 56 | + } |
| 57 | + return; |
| 58 | + } |
| 59 | + appendU16(out, ArgType.Bytes); |
| 60 | + appendU32(out, arg.byteLength); |
| 61 | + appendBytes(out, arg); |
| 62 | +} |
| 63 | + |
| 64 | +function encodeRequest(cmd: number, id: number, args: ControlArg[]): Buffer { |
| 65 | + const payload: number[] = []; |
| 66 | + appendU16(payload, cmd); |
| 67 | + appendU16(payload, id); |
| 68 | + for (const arg of args) { |
| 69 | + encodeArg(payload, arg); |
| 70 | + } |
| 71 | + appendU16(payload, ArgType.End); |
| 72 | + |
| 73 | + const packet: number[] = []; |
| 74 | + appendU32(packet, payload.length); |
| 75 | + packet.push(...payload); |
| 76 | + return Buffer.from(packet); |
| 77 | +} |
| 78 | + |
| 79 | +class PacketReader { |
| 80 | + pos = 0; |
| 81 | + |
| 82 | + constructor(readonly data: Buffer) {} |
| 83 | + |
| 84 | + u16(): number { |
| 85 | + const v = this.data.readUInt16LE(this.pos); |
| 86 | + this.pos += 2; |
| 87 | + return v; |
| 88 | + } |
| 89 | + |
| 90 | + u32(): number { |
| 91 | + const v = this.data.readUInt32LE(this.pos); |
| 92 | + this.pos += 4; |
| 93 | + return v; |
| 94 | + } |
| 95 | + |
| 96 | + i32(): number { |
| 97 | + const v = this.data.readInt32LE(this.pos); |
| 98 | + this.pos += 4; |
| 99 | + return v; |
| 100 | + } |
| 101 | + |
| 102 | + bytes(len: number): Buffer { |
| 103 | + const v = this.data.subarray(this.pos, this.pos + len); |
| 104 | + this.pos += len; |
| 105 | + return v; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +function decodeArg(r: PacketReader): ControlArg | undefined { |
| 110 | + const type = r.u16(); |
| 111 | + if (type === ArgType.End) { |
| 112 | + return undefined; |
| 113 | + } |
| 114 | + if (type === ArgType.Int32) { |
| 115 | + return r.i32(); |
| 116 | + } |
| 117 | + if (type === ArgType.Bytes) { |
| 118 | + return r.bytes(r.u32()); |
| 119 | + } |
| 120 | + if (type === ArgType.String) { |
| 121 | + const len = r.u32(); |
| 122 | + const bytes = r.bytes(len); |
| 123 | + const zero = r.bytes(1)[0]; |
| 124 | + if (zero !== 0) { |
| 125 | + throw new Error("invalid control string terminator"); |
| 126 | + } |
| 127 | + return new TextDecoder().decode(bytes); |
| 128 | + } |
| 129 | + if (type === ArgType.List) { |
| 130 | + const n = r.u16(); |
| 131 | + const list: ControlArg[] = []; |
| 132 | + for (let i = 0; i < n; i++) { |
| 133 | + const arg = decodeArg(r); |
| 134 | + if (arg === undefined) { |
| 135 | + throw new Error("unexpected end marker in control list"); |
| 136 | + } |
| 137 | + list.push(arg); |
| 138 | + } |
| 139 | + return list; |
| 140 | + } |
| 141 | + throw new Error(`unknown control argument type ${type}`); |
| 142 | +} |
| 143 | + |
| 144 | +function pipePath(pipeName: string): string { |
| 145 | + return pipeName.startsWith("\\\\.\\pipe\\") ? pipeName : `\\\\.\\pipe\\${pipeName}`; |
| 146 | +} |
| 147 | + |
| 148 | +async function readExactly(socket: Socket, len: number): Promise<Buffer> { |
| 149 | + const chunks: Buffer[] = []; |
| 150 | + let total = 0; |
| 151 | + while (total < len) { |
| 152 | + const chunk = socket.read(len - total) as Buffer | null; |
| 153 | + if (chunk) { |
| 154 | + chunks.push(chunk); |
| 155 | + total += chunk.length; |
| 156 | + continue; |
| 157 | + } |
| 158 | + await waitForReadable(socket); |
| 159 | + } |
| 160 | + return Buffer.concat(chunks, total); |
| 161 | +} |
| 162 | + |
| 163 | +function waitForReadable(socket: Socket): Promise<void> { |
| 164 | + return new Promise((resolve, reject) => { |
| 165 | + const cleanup = () => { |
| 166 | + socket.off("readable", onReadable); |
| 167 | + socket.off("end", onEnd); |
| 168 | + socket.off("close", onClose); |
| 169 | + socket.off("error", onError); |
| 170 | + }; |
| 171 | + const onReadable = () => { |
| 172 | + cleanup(); |
| 173 | + resolve(); |
| 174 | + }; |
| 175 | + const onEnd = () => { |
| 176 | + cleanup(); |
| 177 | + reject(new Error("control pipe closed while reading")); |
| 178 | + }; |
| 179 | + const onClose = () => { |
| 180 | + cleanup(); |
| 181 | + reject(new Error("control pipe closed while reading")); |
| 182 | + }; |
| 183 | + const onError = (err: Error) => { |
| 184 | + cleanup(); |
| 185 | + reject(err); |
| 186 | + }; |
| 187 | + socket.once("readable", onReadable); |
| 188 | + socket.once("end", onEnd); |
| 189 | + socket.once("close", onClose); |
| 190 | + socket.once("error", onError); |
| 191 | + }); |
| 192 | +} |
| 193 | + |
| 194 | +function connectSocket(path: string): Promise<Socket> { |
| 195 | + return new Promise((resolve, reject) => { |
| 196 | + const socket = createConnection(path); |
| 197 | + const cleanup = () => { |
| 198 | + socket.off("connect", onConnect); |
| 199 | + socket.off("error", onError); |
| 200 | + }; |
| 201 | + const onConnect = () => { |
| 202 | + cleanup(); |
| 203 | + resolve(socket); |
| 204 | + }; |
| 205 | + const onError = (err: Error) => { |
| 206 | + cleanup(); |
| 207 | + socket.destroy(); |
| 208 | + reject(err); |
| 209 | + }; |
| 210 | + socket.once("connect", onConnect); |
| 211 | + socket.once("error", onError); |
| 212 | + }); |
| 213 | +} |
| 214 | + |
| 215 | +function cleanEnv(env: Record<string, string | undefined> | undefined): Record<string, string> | undefined { |
| 216 | + if (!env) { |
| 217 | + return undefined; |
| 218 | + } |
| 219 | + const res: Record<string, string> = {}; |
| 220 | + for (const [key, value] of Object.entries(env)) { |
| 221 | + if (value !== undefined) { |
| 222 | + res[key] = value; |
| 223 | + } |
| 224 | + } |
| 225 | + return res; |
| 226 | +} |
| 227 | + |
| 228 | +export class ControlClient { |
| 229 | + private nextId = 1; |
| 230 | + |
| 231 | + constructor(readonly socket: Socket) {} |
| 232 | + |
| 233 | + static async connect(pipeName: string, timeoutMs = 10000): Promise<ControlClient> { |
| 234 | + const path = pipePath(pipeName); |
| 235 | + const deadline = Date.now() + timeoutMs; |
| 236 | + let lastErr: unknown; |
| 237 | + while (Date.now() < deadline) { |
| 238 | + try { |
| 239 | + const socket = await connectSocket(path); |
| 240 | + return new ControlClient(socket); |
| 241 | + } catch (e) { |
| 242 | + lastErr = e; |
| 243 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 244 | + } |
| 245 | + } |
| 246 | + throw new Error(`failed to connect to Sumatra control pipe ${path}: ${lastErr}`); |
| 247 | + } |
| 248 | + |
| 249 | + async request(cmd: ControlCommand, args: ControlArg[] = []): Promise<ControlArg[]> { |
| 250 | + const id = this.nextId++ & 0xffff; |
| 251 | + this.socket.write(encodeRequest(cmd, id, args)); |
| 252 | + |
| 253 | + const sizeBuf = await readExactly(this.socket, 4); |
| 254 | + const size = sizeBuf.readUInt32LE(0); |
| 255 | + const payload = await readExactly(this.socket, size); |
| 256 | + const r = new PacketReader(payload); |
| 257 | + const responseId = r.u16(); |
| 258 | + if (responseId !== id) { |
| 259 | + throw new Error(`control response id mismatch: got ${responseId}, expected ${id}`); |
| 260 | + } |
| 261 | + const result: ControlArg[] = []; |
| 262 | + for (;;) { |
| 263 | + const arg = decodeArg(r); |
| 264 | + if (arg === undefined) { |
| 265 | + break; |
| 266 | + } |
| 267 | + result.push(arg); |
| 268 | + } |
| 269 | + return result; |
| 270 | + } |
| 271 | + |
| 272 | + async quit(): Promise<void> { |
| 273 | + await this.request(ControlCommand.Quit); |
| 274 | + } |
| 275 | + |
| 276 | + close(): void { |
| 277 | + this.socket.end(); |
| 278 | + } |
| 279 | +} |
| 280 | + |
| 281 | +export function uniquePipeName(prefix = "sumatra-control"): string { |
| 282 | + return `${prefix}-${process.pid}-${Date.now()}-${Math.floor(Math.random() * 0x100000000).toString(16)}`; |
| 283 | +} |
| 284 | + |
| 285 | +export async function withControlledSumatra<T>( |
| 286 | + exe: string, |
| 287 | + fn: (client: ControlClient) => Promise<T>, |
| 288 | + extraArgs: string[] = [], |
| 289 | + options: { cwd?: string; env?: Record<string, string | undefined>; connectTimeoutMs?: number } = {}, |
| 290 | +): Promise<T> { |
| 291 | + const pipeName = uniquePipeName(); |
| 292 | + const proc = Bun.spawn([exe, "-for-testing", "-dbg-control", pipeName, ...extraArgs], { |
| 293 | + stdout: "ignore", |
| 294 | + stderr: "ignore", |
| 295 | + cwd: options.cwd, |
| 296 | + env: cleanEnv(options.env), |
| 297 | + }); |
| 298 | + let client: ControlClient | undefined; |
| 299 | + try { |
| 300 | + client = await ControlClient.connect(pipeName, options.connectTimeoutMs ?? 10000); |
| 301 | + return await fn(client); |
| 302 | + } finally { |
| 303 | + if (client) { |
| 304 | + try { |
| 305 | + await client.quit(); |
| 306 | + } catch { |
| 307 | + proc.kill(); |
| 308 | + } |
| 309 | + client.close(); |
| 310 | + } else { |
| 311 | + proc.kill(); |
| 312 | + } |
| 313 | + await proc.exited; |
| 314 | + } |
| 315 | +} |
| 316 | + |
| 317 | +export async function runControlCommand( |
| 318 | + exe: string, |
| 319 | + cmd: ControlCommand, |
| 320 | + args: ControlArg[] = [], |
| 321 | + extraArgs: string[] = [], |
| 322 | +): Promise<ControlArg[]> { |
| 323 | + return await withControlledSumatra(exe, (client) => client.request(cmd, args), extraArgs); |
| 324 | +} |
| 325 | + |
| 326 | +if (import.meta.main) { |
| 327 | + const [exe, cmdName, ...args] = process.argv.slice(2); |
| 328 | + if (!exe || !cmdName) { |
| 329 | + console.log("Usage: bun cmd/control.ts <SumatraPDF-dll.exe> <ping|test-search> [args...]"); |
| 330 | + process.exit(1); |
| 331 | + } |
| 332 | + const cmd = cmdName === "ping" ? ControlCommand.Ping : cmdName === "test-search" ? ControlCommand.TestSearch : 0; |
| 333 | + if (!cmd) { |
| 334 | + throw new Error(`unknown command: ${cmdName}`); |
| 335 | + } |
| 336 | + const res = await runControlCommand(exe, cmd, args); |
| 337 | + console.log(JSON.stringify(res)); |
| 338 | +} |
0 commit comments