{"version":3,"file":"clipboard-command.d.ts","sourceRoot":"","sources":["../../src/utils/clipboard-command.ts"],"names":[],"mappings":"AAEA,kFAAkF;AAClF,wBAAgB,mBAAmB,CAClC,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,OAAO,CAAC,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,cAAc,CAAC,EAAE,MAAM,CAAA;CAAE,GACvE,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAoC7B","sourcesContent":["import { spawn } from \"node:child_process\";\n\n/** Undefined means the command failed; an empty buffer is a successful result. */\nexport function runClipboardCommand(\n\tcommand: string,\n\targs: readonly string[],\n\toptions?: { input?: string; timeoutMs?: number; maxBufferBytes?: number },\n): Promise<Buffer | undefined> {\n\treturn new Promise((resolve) => {\n\t\t// Clipboard writers can daemonize. Do not give them output pipes to retain.\n\t\tconst child = spawn(command, args, {\n\t\t\tstdio: [\"pipe\", options?.input === undefined ? \"pipe\" : \"ignore\", \"ignore\"],\n\t\t\twindowsHide: true,\n\t\t});\n\t\tconst chunks: Buffer[] = [];\n\t\tlet length = 0;\n\t\tlet settled = false;\n\t\tconst finish = (result: Buffer | undefined): void => {\n\t\t\tif (settled) return;\n\t\t\tsettled = true;\n\t\t\tclearTimeout(timer);\n\t\t\tresolve(result);\n\t\t};\n\t\tconst abort = (): void => {\n\t\t\tchild.kill(\"SIGKILL\");\n\t\t\tchild.stdout?.destroy();\n\t\t\tchild.stdin?.destroy();\n\t\t\tfinish(undefined);\n\t\t};\n\t\tconst timer = setTimeout(abort, options?.timeoutMs ?? 3000);\n\t\tchild.on(\"error\", () => finish(undefined));\n\t\tchild.on(\"close\", (code) => {\n\t\t\tif (!settled) finish(code === 0 ? Buffer.concat(chunks, length) : undefined);\n\t\t});\n\t\tchild.stdout?.on(\"data\", (chunk: Buffer) => {\n\t\t\tif (settled) return;\n\t\t\tlength += chunk.length;\n\t\t\tif (length > (options?.maxBufferBytes ?? 50 * 1024 * 1024)) abort();\n\t\t\telse chunks.push(chunk);\n\t\t});\n\t\tchild.stdin?.on(\"error\", () => {}); // A writer may exit before consuming all input.\n\t\tchild.stdin?.end(options?.input);\n\t});\n}\n"]}