-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.ts
More file actions
55 lines (49 loc) · 1.63 KB
/
Copy pathcli.ts
File metadata and controls
55 lines (49 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env node
import { handleJsonRequest, MAX_INPUT_SIZE } from './json';
async function readStdin(): Promise<string> {
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
let totalBytes = 0;
// Don't set encoding - keep as Buffer for accurate byte counting
process.stdin.on('data', (chunk: Buffer) => {
totalBytes += chunk.length; // Buffer.length is actual bytes
// SECURITY: Enforce size limit during streaming
if (totalBytes > MAX_INPUT_SIZE) {
reject(new Error('Input exceeds maximum size'));
return;
}
chunks.push(chunk);
});
process.stdin.on('end', () => {
const data = Buffer.concat(chunks).toString('utf8');
resolve(data);
});
process.stdin.on('error', reject);
});
}
async function main(): Promise<void> {
try {
const input = await readStdin();
const output = handleJsonRequest(input);
// Wait for stdout to flush before exiting. process.exit() drops any
// unflushed buffer when stdout is a pipe, truncating large responses
// (e.g. getSupportedYieldIds) at the ~64KB pipe-buffer boundary.
process.stdout.write(output + '\n', () => process.exit(0));
} catch (error) {
// SECURITY: Output valid JSON even on catastrophic failure
const errorResponse = {
ok: false,
apiVersion: '1.0',
error: {
code: 'INTERNAL_ERROR',
message: 'Failed to process request',
},
meta: { requestHash: 'unavailable' },
};
process.stdin.destroy();
process.stdout.write(JSON.stringify(errorResponse) + '\n', () =>
process.exit(1),
);
}
}
void main();