diff --git a/src/http.ts b/src/http.ts index a9253d6..86b05b4 100644 --- a/src/http.ts +++ b/src/http.ts @@ -26,6 +26,8 @@ export interface WsOpenCtx { } export interface WsRouteDef { pattern: string; open: (ctx: WsOpenCtx) => Promise> } +export interface MeshControl { path: string; handle(ws: any, req: http.IncomingMessage): void } + const PING_MS = 15_000; export function matchPattern(pattern: string, path: string): Record | undefined { @@ -106,12 +108,17 @@ function loadWss(): any | null { try { return require('ws').WebSocketServer; } catch { return null; } } -function attachWs(server: http.Server, wsRoutes: WsRouteDef[], bus?: Bus): void { - if (wsRoutes.length === 0) return; +function attachWs(server: http.Server, wsRoutes: WsRouteDef[], bus?: Bus, meshControl?: MeshControl): void { + if (wsRoutes.length === 0 && !meshControl) return; const WSS = loadWss(); const wss = WSS ? new WSS({ noServer: true }) : null; server.on('upgrade', (req, socket, head) => { const path = (req.url ?? '/').split('?')[0]; + if (meshControl && path === meshControl.path) { + if (!wss) { socket.write('HTTP/1.1 501 Not Implemented\r\n\r\n'); socket.destroy(); return; } + wss.handleUpgrade(req, socket, head, (ws: any) => meshControl.handle(ws, req)); + return; + } const route = wsRoutes.map((r) => ({ r, params: matchPattern(r.pattern, path) })).find((x) => x.params); if (!route) { socket.destroy(); return; } if (!wss) { socket.write('HTTP/1.1 501 Not Implemented\r\n\r\n'); socket.destroy(); return; } @@ -147,7 +154,7 @@ function attachWs(server: http.Server, wsRoutes: WsRouteDef[], bus?: Bus): void }); } -export function createHttpServer(routes: RouteDef[], wsRoutes: WsRouteDef[] = [], bus?: Bus): http.Server { +export function createHttpServer(routes: RouteDef[], wsRoutes: WsRouteDef[] = [], bus?: Bus, meshControl?: MeshControl): http.Server { const server = http.createServer(async (req, res) => { const url = req.url ?? '/'; const path = url.split('?')[0]; @@ -193,6 +200,6 @@ export function createHttpServer(routes: RouteDef[], wsRoutes: WsRouteDef[] = [] res.writeHead(r.status, r.headers); res.end(r.body); }); - attachWs(server, wsRoutes, bus); + attachWs(server, wsRoutes, bus, meshControl); return server; } diff --git a/test/mesh/control-upgrade.test.ts b/test/mesh/control-upgrade.test.ts new file mode 100644 index 0000000..cd50118 --- /dev/null +++ b/test/mesh/control-upgrade.test.ts @@ -0,0 +1,18 @@ +import { describe, it, expect } from 'vitest'; +import WebSocket from 'ws'; +import { createHttpServer, type MeshControl } from '../../src/http'; + +describe('mesh control upgrade', () => { + it('routes the reserved path to meshControl.handle and leaves wsRoutes intact', async () => { + let handled = false; + const meshControl: MeshControl = { path: '/__mesh__/control', handle: (ws) => { handled = true; ws.send('hi'); } }; + const server = createHttpServer([], [], undefined, meshControl); + await new Promise((r) => server.listen(0, r)); + const port = (server.address() as any).port; + const client = new WebSocket(`ws://127.0.0.1:${port}/__mesh__/control`); + const msg = await new Promise((res) => { client.on('message', (d) => res(d.toString())); }); + expect(msg).toBe('hi'); + expect(handled).toBe(true); + client.close(); server.close(); + }); +});