|
1 | | -import type * as http from 'node:http' |
| 1 | +import * as http from 'node:http' |
| 2 | +import type { Socket } from 'node:net' |
2 | 3 | import type { ServerOptions, WebSocket } from 'ws' |
3 | | -import { WebSocketServer as Server } from 'ws' |
| 4 | +import { WebSocketServer } from 'ws' |
4 | 5 |
|
5 | 6 | export interface TinyWSRequest extends http.IncomingMessage { |
6 | 7 | ws: () => Promise<WebSocket> |
7 | 8 | } |
8 | 9 |
|
| 10 | +export interface TinyWSOptions extends ServerOptions { |
| 11 | + paths?: string | string[] |
| 12 | +} |
| 13 | + |
9 | 14 | /** |
10 | 15 | * tinyws - adds `req.ws` method that resolves when websocket request appears |
11 | | - * @param wsOptions |
| 16 | + * @param app - The application instance with a handler function |
| 17 | + * @param server - The HTTP server instance |
| 18 | + * @param options - Optional WebSocket server options and paths to restrict WebSocket handling |
| 19 | + * @param wss - Optional existing WebSocketServer instance |
| 20 | + * @returns The WebSocketServer instance |
12 | 21 | */ |
13 | | -export const tinyws = |
14 | | - (wsOptions?: ServerOptions, wss: Server = new Server({ ...wsOptions, noServer: true })) => |
15 | | - async (req: TinyWSRequest, _: unknown, next: () => void | Promise<void>) => { |
16 | | - const upgradeHeader = (req.headers.upgrade || '').split(',').map((s) => s.trim()) |
17 | | - |
18 | | - // When upgrade header contains "websocket" it's index is 0 |
19 | | - if (upgradeHeader.indexOf('websocket') === 0) { |
20 | | - req.ws = () => |
| 22 | +export const tinyws = ( |
| 23 | + app: { handler: (req: any, res: any) => void }, |
| 24 | + server: http.Server, |
| 25 | + options?: TinyWSOptions, |
| 26 | + wss: WebSocketServer = new WebSocketServer({ ...options, noServer: true }) |
| 27 | +) => { |
| 28 | + const { paths, ...wsOptions } = options || {} |
| 29 | + const allowedPaths = paths ? (Array.isArray(paths) ? paths : [paths]) : null |
| 30 | + |
| 31 | + const upgradeHandler = (request: http.IncomingMessage, socket: Socket, head: Buffer) => { |
| 32 | + const response = new http.ServerResponse(request) |
| 33 | + response.assignSocket(socket) |
| 34 | + |
| 35 | + // Copy the head buffer to avoid keeping the entire slab buffer alive |
| 36 | + const copyOfHead = Buffer.alloc(head.length) |
| 37 | + head.copy(copyOfHead) |
| 38 | + |
| 39 | + response.on('finish', () => { |
| 40 | + if (response.socket !== null) { |
| 41 | + response.socket.destroy() |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + const upgradeHeader = (request.headers.upgrade || '').split(',').map((s) => s.trim()) |
| 46 | + const requestPath = request.url?.split('?')[0] || '/' |
| 47 | + |
| 48 | + const pathMatches = allowedPaths === null || allowedPaths.some((p) => requestPath.startsWith(p)) |
| 49 | + |
| 50 | + if (upgradeHeader.indexOf('websocket') === 0 && pathMatches) { |
| 51 | + ;(request as TinyWSRequest).ws = () => |
21 | 52 | new Promise((resolve) => { |
22 | | - wss.handleUpgrade(req, req.socket, Buffer.alloc(0), (ws) => { |
23 | | - wss.emit('connection', ws, req) |
| 53 | + wss.handleUpgrade(request, socket, copyOfHead, (ws) => { |
| 54 | + wss.emit('connection', ws, request) |
24 | 55 | resolve(ws) |
25 | 56 | }) |
26 | 57 | }) |
27 | 58 | } |
28 | 59 |
|
29 | | - await next() |
| 60 | + app.handler(request, response) |
30 | 61 | } |
| 62 | + |
| 63 | + server.on('upgrade', upgradeHandler) |
| 64 | + return wss |
| 65 | +} |
0 commit comments