Spaces:
Running
Running
Download server/http/apiRoutes.ts from evalstate/mcp-webcam: direct link, hf CLI and curl.
- Browser
- Download file 7.12 kB
-
https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/server/http/apiRoutes.ts
- Command line
-
hf download hf://spaces/evalstate/mcp-webcam/server/http/apiRoutes.ts
-
curl -L -o apiRoutes.ts https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/server/http/apiRoutes.ts
7.12 kB
| import type { Hono } from 'hono'; | |
| import { bodyLimit } from 'hono/body-limit'; | |
| import * as z from 'zod'; | |
| import type * as T from '../../shared/api-types.js'; | |
| import type { AppConfig } from '../config.js'; | |
| import type { RoomRegistryApi } from '../contracts.js'; | |
| import { parseImageDataUrl } from '../state/camera.js'; | |
| import { resolveUser } from './userParam.js'; | |
| const IMAGE_BODY_LIMIT = 25 * 1024 * 1024; // 25 MiB | |
| const FireEventSchema = z.object({ | |
| name: z.enum(['webcam.message', 'webcam.snapshot']), | |
| message: z.string().max(4000).optional(), | |
| snapshot: z.string().optional(), | |
| frozen: z.boolean().optional(), | |
| width: z.number().finite().int().positive().optional(), | |
| height: z.number().finite().int().positive().optional(), | |
| source: z.enum(['ui', 'auto', 'api']).optional(), | |
| }); | |
| const EventsConfigPatchSchema = z.object({ | |
| pushEnabled: z.boolean().optional(), | |
| pollEnabled: z.boolean().optional(), | |
| heartbeatMs: z.number().finite().positive().optional(), | |
| nextPollMs: z.number().finite().positive().optional(), | |
| }); | |
| export interface ApiRoutesDeps { | |
| rooms: RoomRegistryApi; | |
| config: AppConfig; | |
| version: string; | |
| } | |
| export function mountApiRoutes(app: Hono, deps: ApiRoutesDeps): void { | |
| const largeBodyLimit = bodyLimit({ | |
| maxSize: IMAGE_BODY_LIMIT, | |
| onError: (c) => c.json({ error: 'Payload too large' }, 413), | |
| }); | |
| app.get('/api/health', (c) => { | |
| return c.json({ status: 'ok', version: deps.version }); | |
| }); | |
| app.get('/api/config', (c) => { | |
| const view: T.AppConfigView = { | |
| version: deps.version, | |
| mode: deps.config.mode, | |
| mcpPath: '/mcp', | |
| mcpHost: deps.config.mcpHost, | |
| mcpHostConfigured: deps.config.mcpHostConfigured, | |
| modernProtocol: '2026-07-28', | |
| legacyFallback: true, | |
| ...(deps.config.mode === 'stdio' ? { stdioUser: deps.config.stdioUser } : {}), | |
| }; | |
| return c.json(view); | |
| }); | |
| app.get('/api/state', (c) => { | |
| const user = resolveUser(c.req.raw); | |
| return c.json(deps.rooms.get(user).snapshot()); | |
| }); | |
| app.get('/api/stats', (c) => { | |
| return c.json(deps.rooms.stats()); | |
| }); | |
| app.post('/api/capture/:requestId', largeBodyLimit, async (c) => { | |
| const user = resolveUser(c.req.raw); | |
| const requestId = c.req.param('requestId'); | |
| let body: T.CaptureResultRequest; | |
| try { | |
| body = (await c.req.json()) as T.CaptureResultRequest; | |
| } catch { | |
| return c.json({ error: 'Invalid JSON body' }, 400); | |
| } | |
| const ok = deps.rooms.get(user).camera.resolveCapture(requestId, body); | |
| if (!ok) { | |
| return c.json({ error: 'Unknown or expired capture request' }, 404); | |
| } | |
| return c.json({ ok: true }); | |
| }); | |
| app.post('/api/tabs/:tabId/primary', (c) => { | |
| const user = resolveUser(c.req.raw); | |
| const tabId = c.req.param('tabId'); | |
| const ok = deps.rooms.get(user).camera.setPrimary(tabId); | |
| if (!ok) { | |
| return c.json({ error: 'Unknown tab' }, 404); | |
| } | |
| return c.json({ ok: true }); | |
| }); | |
| app.post('/api/tabs/:tabId/status', async (c) => { | |
| const user = resolveUser(c.req.raw); | |
| const tabId = c.req.param('tabId'); | |
| let body: Partial<T.CameraTabView>; | |
| try { | |
| body = (await c.req.json()) as Partial<T.CameraTabView>; | |
| } catch { | |
| return c.json({ error: 'Invalid JSON body' }, 400); | |
| } | |
| deps.rooms.get(user).camera.setStatus(tabId, body); | |
| return c.json({ ok: true }); | |
| }); | |
| app.post('/api/events/fire', largeBodyLimit, async (c) => { | |
| const user = resolveUser(c.req.raw); | |
| let rawBody: unknown; | |
| try { | |
| rawBody = await c.req.json(); | |
| } catch { | |
| return c.json({ error: 'Invalid JSON body' }, 400); | |
| } | |
| const parsedBody = FireEventSchema.safeParse(rawBody); | |
| if (!parsedBody.success) { | |
| return c.json({ error: 'Invalid event payload', details: parsedBody.error.issues }, 400); | |
| } | |
| const body: T.FireEventRequest = parsedBody.data; | |
| let snapshotBytes: Buffer | undefined; | |
| let snapshotMime: string | undefined; | |
| if (body.snapshot) { | |
| try { | |
| const parsed = parseImageDataUrl(body.snapshot); | |
| snapshotBytes = Buffer.from(parsed.base64, 'base64'); | |
| snapshotMime = parsed.mimeType; | |
| } catch { | |
| return c.json( | |
| { error: 'Invalid snapshot data URL (only image/jpeg, image/png, image/webp allowed)' }, | |
| 400, | |
| ); | |
| } | |
| } | |
| const result = deps.rooms.get(user).events.fire({ | |
| ...body, | |
| snapshotBytes, | |
| snapshotMime, | |
| }); | |
| return c.json(result); | |
| }); | |
| app.get('/api/events/:eventId/snapshot', (c) => { | |
| const user = resolveUser(c.req.raw); | |
| const eventId = c.req.param('eventId'); | |
| const snap = deps.rooms.get(user).events.getSnapshot(eventId); | |
| if (!snap) { | |
| return c.json({ error: 'Snapshot not found' }, 404); | |
| } | |
| return new Response(new Uint8Array(snap.bytes), { | |
| status: 200, | |
| headers: { | |
| 'content-type': snap.mimeType, | |
| 'cache-control': 'private, max-age=300', | |
| 'x-content-type-options': 'nosniff', | |
| 'content-security-policy': 'sandbox', | |
| }, | |
| }); | |
| }); | |
| app.patch('/api/events/config', async (c) => { | |
| const user = resolveUser(c.req.raw); | |
| let rawBody: unknown; | |
| try { | |
| rawBody = await c.req.json(); | |
| } catch { | |
| return c.json({ error: 'Invalid JSON body' }, 400); | |
| } | |
| const parsedBody = EventsConfigPatchSchema.safeParse(rawBody); | |
| if (!parsedBody.success) { | |
| return c.json({ error: 'Invalid config patch', details: parsedBody.error.issues }, 400); | |
| } | |
| const updated = deps.rooms.get(user).events.setConfig(parsedBody.data); | |
| return c.json(updated); | |
| }); | |
| app.post('/api/push/:streamId/drop', (c) => { | |
| const user = resolveUser(c.req.raw); | |
| const streamId = c.req.param('streamId'); | |
| const ok = deps.rooms.get(user).events.drop(streamId); | |
| if (!ok) { | |
| return c.json({ error: 'Unknown push stream' }, 404); | |
| } | |
| return c.json({ ok: true }); | |
| }); | |
| app.post('/api/push/:streamId/revoke', async (c) => { | |
| const user = resolveUser(c.req.raw); | |
| const streamId = c.req.param('streamId'); | |
| let reason: string | undefined; | |
| try { | |
| const body = (await c.req.json()) as { reason?: string }; | |
| if (typeof body?.reason === 'string') { | |
| reason = body.reason; | |
| } | |
| } catch { | |
| // body is optional | |
| } | |
| const ok = deps.rooms.get(user).events.revokeStream(streamId, reason); | |
| if (!ok) { | |
| return c.json({ error: 'Unknown push stream' }, 404); | |
| } | |
| return c.json({ ok: true }); | |
| }); | |
| app.post('/api/pollers/:pollerId/revoke', (c) => { | |
| const user = resolveUser(c.req.raw); | |
| const pollerId = c.req.param('pollerId'); | |
| const ok = deps.rooms.get(user).events.revokePoller(pollerId); | |
| if (!ok) { | |
| return c.json({ error: 'Unknown poller' }, 404); | |
| } | |
| return c.json({ ok: true }); | |
| }); | |
| app.post('/api/activity/clear', (c) => { | |
| const user = resolveUser(c.req.raw); | |
| const room = deps.rooms.get(user); | |
| const act = room.activity as { clear?: () => void }; | |
| if (typeof act.clear === 'function') { | |
| act.clear(); | |
| } | |
| return c.json({ ok: true }); | |
| }); | |
| } | |