| 1 | import { spawn } from 'node:child_process'; |
| 2 | import { appendFileSync } from 'node:fs'; |
| 3 | |
| 4 | const [logPath, agentScript] = process.argv.slice(2); |
| 5 | const child = spawn(process.execPath, [agentScript], { stdio: ['pipe', 'pipe', 'inherit'], env: process.env }); |
| 6 | |
| 7 | function tap(tag) { |
| 8 | let buffer = ''; |
| 9 | return (chunk) => { |
| 10 | buffer += chunk; |
| 11 | let index; |
| 12 | while ((index = buffer.indexOf('\n')) >= 0) { |
| 13 | const line = buffer.slice(0, index); |
| 14 | buffer = buffer.slice(index + 1); |
| 15 | if (line.trim()) appendFileSync(logPath, `${tag} ${line}\n`); |
| 16 | } |
| 17 | }; |
| 18 | } |
| 19 | |
| 20 | const fromClient = tap('C'); |
| 21 | const fromAgent = tap('A'); |
| 22 | process.stdin.setEncoding('utf8'); |
| 23 | child.stdout.setEncoding('utf8'); |
| 24 | process.stdin.on('data', (chunk) => { |
| 25 | fromClient(chunk); |
| 26 | child.stdin.write(chunk); |
| 27 | }); |
| 28 | process.stdin.on('end', () => child.stdin.end()); |
| 29 | child.stdout.on('data', (chunk) => { |
| 30 | fromAgent(chunk); |
| 31 | process.stdout.write(chunk); |
| 32 | }); |
| 33 | child.on('exit', (code) => process.exit(code ?? 0)); |
| 34 |