@@ -50,7 +50,27 @@ const {readFile} = require('fs').promises;
50
50
51
51
const React = require ( 'react' ) ;
52
52
53
- async function renderApp ( res , returnValue , formState , noCache ) {
53
+ const activeDebugChannels =
54
+ process . env . NODE_ENV === 'development' ? new Map ( ) : null ;
55
+
56
+ function getDebugChannel ( req ) {
57
+ if ( process . env . NODE_ENV !== 'development' ) {
58
+ return undefined ;
59
+ }
60
+ const requestId = req . get ( 'rsc-request-id' ) ;
61
+ if ( ! requestId ) {
62
+ return undefined ;
63
+ }
64
+ return activeDebugChannels . get ( requestId ) ;
65
+ }
66
+
67
+ async function renderApp (
68
+ res ,
69
+ returnValue ,
70
+ formState ,
71
+ noCache ,
72
+ promiseForDebugChannel
73
+ ) {
54
74
const { renderToPipeableStream} = await import (
55
75
'react-server-dom-webpack/server'
56
76
) ;
@@ -101,7 +121,9 @@ async function renderApp(res, returnValue, formState, noCache) {
101
121
) ;
102
122
// For client-invoked server actions we refresh the tree and return a return value.
103
123
const payload = { root, returnValue, formState} ;
104
- const { pipe} = renderToPipeableStream ( payload , moduleMap ) ;
124
+ const { pipe} = renderToPipeableStream ( payload , moduleMap , {
125
+ debugChannel : await promiseForDebugChannel ,
126
+ } ) ;
105
127
pipe ( res ) ;
106
128
}
107
129
@@ -166,7 +188,7 @@ app.get('/', async function (req, res) {
166
188
if ( 'prerender' in req . query ) {
167
189
await prerenderApp ( res , null , null , noCache ) ;
168
190
} else {
169
- await renderApp ( res , null , null , noCache ) ;
191
+ await renderApp ( res , null , null , noCache , getDebugChannel ( req ) ) ;
170
192
}
171
193
} ) ;
172
194
@@ -204,7 +226,7 @@ app.post('/', bodyParser.text(), async function (req, res) {
204
226
// We handle the error on the client
205
227
}
206
228
// Refresh the client and return the value
207
- renderApp ( res , result , null , noCache ) ;
229
+ renderApp ( res , result , null , noCache , getDebugChannel ( req ) ) ;
208
230
} else {
209
231
// This is the progressive enhancement case
210
232
const UndiciRequest = require ( 'undici' ) . Request ;
@@ -220,11 +242,11 @@ app.post('/', bodyParser.text(), async function (req, res) {
220
242
// Wait for any mutations
221
243
const result = await action ( ) ;
222
244
const formState = decodeFormState ( result , formData ) ;
223
- renderApp ( res , null , formState , noCache ) ;
245
+ renderApp ( res , null , formState , noCache , undefined ) ;
224
246
} catch ( x ) {
225
247
const { setServerState} = await import ( '../src/ServerState.js' ) ;
226
248
setServerState ( 'Error: ' + x . message ) ;
227
- renderApp ( res , null , null , noCache ) ;
249
+ renderApp ( res , null , null , noCache , undefined ) ;
228
250
}
229
251
}
230
252
} ) ;
@@ -324,7 +346,7 @@ if (process.env.NODE_ENV === 'development') {
324
346
} ) ;
325
347
}
326
348
327
- app . listen ( 3001 , ( ) => {
349
+ const httpServer = app . listen ( 3001 , ( ) => {
328
350
console . log ( 'Regional Flight Server listening on port 3001...' ) ;
329
351
} ) ;
330
352
@@ -346,3 +368,27 @@ app.on('error', function (error) {
346
368
throw error ;
347
369
}
348
370
} ) ;
371
+
372
+ if ( process . env . NODE_ENV === 'development' ) {
373
+ // Open a websocket server for Debug information
374
+ const WebSocket = require ( 'ws' ) ;
375
+ const webSocketServer = new WebSocket . Server ( { noServer : true } ) ;
376
+
377
+ httpServer . on ( 'upgrade' , ( request , socket , head ) => {
378
+ const DEBUG_CHANNEL_PATH = '/debug-channel?' ;
379
+ if ( request . url . startsWith ( DEBUG_CHANNEL_PATH ) ) {
380
+ const requestId = request . url . slice ( DEBUG_CHANNEL_PATH . length ) ;
381
+ const promiseForWs = new Promise ( resolve => {
382
+ webSocketServer . handleUpgrade ( request , socket , head , ws => {
383
+ ws . on ( 'close' , ( ) => {
384
+ activeDebugChannels . delete ( requestId ) ;
385
+ } ) ;
386
+ resolve ( ws ) ;
387
+ } ) ;
388
+ } ) ;
389
+ activeDebugChannels . set ( requestId , promiseForWs ) ;
390
+ } else {
391
+ socket . destroy ( ) ;
392
+ }
393
+ } ) ;
394
+ }
0 commit comments