Add HTTP server mode with native node:http

Reintroduce server mode (removed in 3014cf9) using node:http instead of
Express — zero new dependencies. Accepts JSON commands via POST to /,
returns JSON responses with CORS support. Screenshot command returns
base64 data when no path is specified.

- Add src/server.ts with BrowserServer class using node:http
- Add -s/--server <port> CLI flag (default 13373)
- Export BrowserServer and startServer from index.ts

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-25 16:23:16 +02:00
parent a80224df36
commit 1426ec9173
14 changed files with 228 additions and 33 deletions
Vendored
+12
View File
@@ -5,6 +5,7 @@ import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';
import { ClaudeBrowser } from './browser.js';
import * as image from './image.js';
import { startServer } from './server.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const pkg = JSON.parse(readFileSync(resolve(__dirname, '../package.json'), 'utf-8'));
const { values, positionals } = parseArgs({
@@ -24,6 +25,7 @@ const { values, positionals } = parseArgs({
json: { type: 'boolean', short: 'j', default: false },
click: { type: 'string', short: 'c', multiple: true },
type: { type: 'string', short: 't', multiple: true },
server: { type: 'string', short: 's' },
help: { type: 'boolean', default: false },
version: { type: 'boolean', short: 'v', default: false },
// Image processing options
@@ -51,6 +53,7 @@ Options:
-j, --json Output query results as JSON
-c, --click <selector> Click on element (can be repeated for multiple clicks)
-t, --type <sel>=<text> Type text into input (can be repeated)
-s, --server <port> Start HTTP server mode (default port: 13373)
-v, --version Show version
--help Show this help
@@ -78,6 +81,10 @@ Image processing examples:
browse https://example.com --resize 800x600
browse https://example.com --compress 60
Server mode:
browse -s 13373 # Start HTTP server on port 13373
curl localhost:13373 -d '{"cmd":"goto","url":"https://example.com"}'
MCP Server (for Claude Code integration):
browse-mcp # Run as MCP server (stdio transport)
`;
@@ -219,6 +226,11 @@ async function main() {
console.log(HELP);
process.exit(0);
}
if (values.server !== undefined) {
const port = Number.parseInt(values.server) || 13373;
await startServer({ port });
return;
}
if (positionals.length === 0) {
console.log(HELP);
process.exit(0);