(request: IncomingMessage)
| 404 | } |
| 405 | |
| 406 | async function readJson<T>(request: IncomingMessage): Promise<T> { |
| 407 | const chunks: Buffer[] = []; |
| 408 | for await (const chunk of request) { |
| 409 | chunks.push(chunk as Buffer); |
| 410 | } |
| 411 | const text = Buffer.concat(chunks).toString("utf8"); |
| 412 | if (text.length === 0) return {} as T; |
| 413 | return JSON.parse(text) as T; |
| 414 | } |
| 415 | |
| 416 | function toWebSocketUrl(input: string): string { |
| 417 | if (input.startsWith("ws://") || input.startsWith("wss://")) return input; |