33 */
44
55import type { IncomingMessage , ServerResponse } from "node:http"
6- import http from "node:http "
6+ import { AsyncLocalStorage } from "node:async_hooks "
77import logger from "../utils/logger"
8- import { WHIP_INTERNAL_PORT } from "./api/apiState"
98import {
109 handleCreateSession ,
1110 handleGetSession ,
@@ -85,25 +84,7 @@ const routes: Route[] = [
8584 } ,
8685]
8786
88- function startWhipInternalServer ( ) : void {
89- const server = http . createServer ( ( req , res ) => {
90- const url = new URL ( req . url ?? "" , `http://127.0.0.1:${ WHIP_INTERNAL_PORT } ` )
91- if ( req . method === "POST" && url . pathname === "/api/webrtc/whip" ) {
92- handleWhipSignalingExchange ( req , res )
93- } else {
94- res . writeHead ( 404 )
95- res . end ( )
96- }
97- } )
98- server . listen ( WHIP_INTERNAL_PORT , "127.0.0.1" , ( ) => {
99- logger . info (
100- `WHIP internal endpoint listening on port ${ WHIP_INTERNAL_PORT } ` ,
101- )
102- } )
103- server . on ( "error" , ( err ) => {
104- logger . error ( `WHIP internal server failed to start: ${ String ( err ) } ` )
105- } )
106- }
87+ const reinStorage = new AsyncLocalStorage < boolean > ( )
10788
10889export function attachSignalingRoutes (
10990 server : NonNullable < import ( "vite" ) . ViteDevServer [ "httpServer" ] > ,
@@ -124,30 +105,61 @@ export function attachSignalingRoutes(
124105 const match = pathname . match ( route . pattern )
125106 if ( ! match ) continue
126107
108+ const anyRes = res as ServerResponse & { __handledByRein ?: boolean }
109+ anyRes . __handledByRein = true
110+
127111 const originalSetHeader = res . setHeader . bind ( res )
128112 const originalWriteHead = res . writeHead . bind ( res )
129- res . setHeader = ( ...args : Parameters < typeof res . setHeader > ) => {
113+ const originalWrite = res . write . bind ( res )
114+ const originalEnd = res . end . bind ( res )
115+
116+ res . setHeader = ( ( ...args : Parameters < typeof res . setHeader > ) => {
117+ if ( anyRes . __handledByRein && ! reinStorage . getStore ( ) ) {
118+ return res
119+ }
130120 if ( res . headersSent ) return res
131121 return originalSetHeader ( ...args )
132- }
122+ } ) as typeof res . setHeader
123+
133124 res . writeHead = ( ( ...args : unknown [ ] ) => {
125+ if ( anyRes . __handledByRein && ! reinStorage . getStore ( ) ) {
126+ return res
127+ }
134128 if ( res . writableEnded ) return res
135129 return ( originalWriteHead as ( ...a : unknown [ ] ) => ServerResponse ) (
136130 ...args ,
137131 )
138132 } ) as typeof res . writeHead
139133
134+ res . write = ( ( ...args : unknown [ ] ) => {
135+ if ( anyRes . __handledByRein && ! reinStorage . getStore ( ) ) {
136+ return true
137+ }
138+ return ( originalWrite as ( ...a : unknown [ ] ) => boolean ) ( ...args )
139+ } ) as typeof res . write
140+
141+ res . end = ( ( ...args : unknown [ ] ) => {
142+ if ( anyRes . __handledByRein && ! reinStorage . getStore ( ) ) {
143+ return res
144+ }
145+ return ( originalEnd as ( ...a : unknown [ ] ) => ServerResponse ) ( ...args )
146+ } ) as typeof res . end
147+
140148 const params = match . slice ( 1 )
141- Promise . resolve ( route . handler ( req , res , ...params ) ) . catch ( ( err ) => {
149+ Promise . resolve (
150+ reinStorage . run ( true , ( ) => route . handler ( req , res , ...params ) ) ,
151+ ) . catch ( ( err ) => {
142152 logger . error ( `Signaling route error: ${ String ( err ) } ` )
143- if ( ! res . headersSent )
144- json ( res , 500 , { error : "Internal server error" } )
153+ if ( ! res . headersSent ) {
154+ reinStorage . run ( true , ( ) => {
155+ json ( res , 500 , { error : "Internal server error" } )
156+ } )
157+ }
145158 } )
146159 return
147160 }
148161 } ,
149162 )
150- startWhipInternalServer ( )
151163 logger . info ( "Signaling HTTP routes attached" )
152164}
153165
0 commit comments