|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import process from 'node:process'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { Command } from 'commander'; |
| 6 | +import { startServer } from '../src/live-server.js'; |
| 7 | + |
| 8 | +const program = new Command(); |
| 9 | + |
| 10 | +program |
| 11 | + .name('codex-viewer') |
| 12 | + .description('Direct-proxy web viewer for Codex CLI') |
| 13 | + .version('0.4.0'); |
| 14 | + |
| 15 | +program |
| 16 | + .option('-p, --port <port>', 'server port', '3789') |
| 17 | + .option('-H, --host <host>', 'server host', '127.0.0.1') |
| 18 | + .option('--codex-bin <path>', 'codex executable path', 'codex') |
| 19 | + .option('--max-events <n>', 'max buffered structured events', '3000') |
| 20 | + .option('--proxy-port <port>', 'direct proxy listen port (0 = random free port)', '0') |
| 21 | + .option('--proxy-max-body <bytes>', 'max captured body bytes per request/response', String(512 * 1024)) |
| 22 | + .option('--proxy-max-ws <bytes>', 'max captured websocket frame bytes', String(128 * 1024)) |
| 23 | + .option('--proxy-max-json <bytes>', 'max parsed json bytes per payload', String(256 * 1024)) |
| 24 | + .option('-C, --cd <dir>', 'default working directory') |
| 25 | + .action(async (options) => { |
| 26 | + const port = Number.parseInt(options.port, 10); |
| 27 | + if (!Number.isFinite(port) || port <= 0) { |
| 28 | + throw new Error(`Invalid port: ${options.port}`); |
| 29 | + } |
| 30 | + |
| 31 | + const host = String(options.host || '127.0.0.1'); |
| 32 | + const workdir = options.cd ? path.resolve(String(options.cd)) : process.cwd(); |
| 33 | + const maxEvents = Number.parseInt(String(options.maxEvents || '3000'), 10); |
| 34 | + if (!Number.isFinite(maxEvents) || maxEvents < 100) { |
| 35 | + throw new Error(`Invalid max-events: ${options.maxEvents}`); |
| 36 | + } |
| 37 | + |
| 38 | + const proxyPort = Number.parseInt(String(options.proxyPort || '0'), 10); |
| 39 | + if (!Number.isFinite(proxyPort) || proxyPort < 0) { |
| 40 | + throw new Error(`Invalid proxy-port: ${options.proxyPort}`); |
| 41 | + } |
| 42 | + |
| 43 | + const proxyMaxBodyBytes = Number.parseInt(String(options.proxyMaxBody || 512 * 1024), 10); |
| 44 | + if (!Number.isFinite(proxyMaxBodyBytes) || proxyMaxBodyBytes < 4096) { |
| 45 | + throw new Error(`Invalid proxy-max-body: ${options.proxyMaxBody}`); |
| 46 | + } |
| 47 | + |
| 48 | + const proxyMaxWsBytes = Number.parseInt(String(options.proxyMaxWs || 128 * 1024), 10); |
| 49 | + if (!Number.isFinite(proxyMaxWsBytes) || proxyMaxWsBytes < 1024) { |
| 50 | + throw new Error(`Invalid proxy-max-ws: ${options.proxyMaxWs}`); |
| 51 | + } |
| 52 | + |
| 53 | + const proxyMaxJsonBytes = Number.parseInt(String(options.proxyMaxJson || 256 * 1024), 10); |
| 54 | + if (!Number.isFinite(proxyMaxJsonBytes) || proxyMaxJsonBytes < 1024) { |
| 55 | + throw new Error(`Invalid proxy-max-json: ${options.proxyMaxJson}`); |
| 56 | + } |
| 57 | + |
| 58 | + const service = await startServer({ |
| 59 | + host, |
| 60 | + port, |
| 61 | + options: { |
| 62 | + codexBin: options.codexBin, |
| 63 | + workdir, |
| 64 | + maxEvents, |
| 65 | + proxy: true, |
| 66 | + proxyPort, |
| 67 | + proxyMaxBodyBytes, |
| 68 | + proxyMaxWsBytes, |
| 69 | + proxyMaxJsonBytes |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + console.log(`codex-viewer: http://${host}:${port}`); |
| 74 | + |
| 75 | + await new Promise((resolve) => { |
| 76 | + let closed = false; |
| 77 | + |
| 78 | + async function shutdown() { |
| 79 | + if (closed) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + closed = true; |
| 84 | + service.stopRunner(); |
| 85 | + service.closeSockets(); |
| 86 | + await service.stopProxy?.(); |
| 87 | + service.server.close(() => resolve()); |
| 88 | + } |
| 89 | + |
| 90 | + process.on('SIGINT', shutdown); |
| 91 | + process.on('SIGTERM', shutdown); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | +program.parseAsync(process.argv).catch((error) => { |
| 96 | + console.error(`codex-viewer error: ${error.message}`); |
| 97 | + process.exitCode = 1; |
| 98 | +}); |
0 commit comments