Commit 8b14fc19 authored by Jimmy Lai's avatar Jimmy Lai

fix: bridge web debug channel to Node Writable for renderToPipeableStream

React's Node API (renderToPipeableStream) expects debugChannel to be a
Node.js Writable (typeof .write === 'function'), Duplex, or WebSocket.
Next.js creates the debug channel with a web WritableStream inside
{ writable: WritableStream }, which the web API extracts via
options.debugChannel.writable, but the Node API checks the object itself.

When the Node API sees debugChannel !== undefined but can't find .write(),
it enables debug info production (pendingDebugChunks++) without setting
up a debug destination. Debug chunks accumulate indefinitely, the request
never completes, and the flight stream hangs.

The fix: in renderToFlightPipeableStream, detect the web-style debug
channel shape ({ writable: WritableStream }) and bridge it to a Node.js
Writable that forwards chunks via the web WritableStream's locked writer.
React then sees .write() on the object and uses it as the debug
destination, allowing pendingDebugChunks to drain and the request to
complete normally.

Re-enables debugChannel at all five renderToFlightPipeableStream call
sites that were temporarily disabled.
parent 192d9884
......@@ -722,9 +722,7 @@ async function generateDynamicFlightRenderResult(
onError,
temporaryReferences: options?.temporaryReferences,
filterStackFrame,
// TODO: The debug channel uses web WritableStream which keeps
// renderToPipeableStream from ending. Skip it for now.
// debugChannel: debugChannel?.serverSide,
debugChannel: debugChannel?.serverSide,
}
)
......@@ -819,9 +817,6 @@ async function stagedRenderToReadableStreamWithoutCachesInDev(
if (process.env.__NEXT_USE_NODE_STREAMS) {
const { renderToFlightPipeableStream } =
require('./pipeable-stream-wrappers') as typeof import('./pipeable-stream-wrappers')
// Strip debugChannel from options: the debug channel uses web WritableStream
// which keeps renderToPipeableStream from ending.
const { debugChannel: _dc, ...nodeOptions } = options || ({} as any)
return await workUnitAsyncStorage.run(
requestStore,
scheduleInSequentialTasks,
......@@ -832,7 +827,7 @@ async function stagedRenderToReadableStreamWithoutCachesInDev(
rscPayload,
clientModules,
{
...nodeOptions,
...options,
environmentName,
}
)
......@@ -3077,9 +3072,7 @@ async function renderToStream(
{
filterStackFrame,
onError: serverComponentsErrorHandler,
// TODO: The debug channel uses web WritableStream which keeps
// renderToPipeableStream from ending. Skip it for now.
// debugChannel: debugChannel?.serverSide,
debugChannel: debugChannel?.serverSide,
}
)
)
......@@ -3806,10 +3799,7 @@ async function renderWithRestartOnCacheMissInDev(
environmentName,
startTime,
filterStackFrame,
// TODO: The debug channel uses web WritableStream which keeps
// renderToPipeableStream from ending. Skip it for now in the
// node streams path until we have a Node-compatible debug channel.
// debugChannel: debugChannel?.serverSide,
debugChannel: debugChannel?.serverSide,
signal: initialReactController.signal,
}
)
......@@ -4018,10 +4008,7 @@ async function renderWithRestartOnCacheMissInDev(
environmentName,
startTime,
filterStackFrame,
// TODO: The debug channel uses web WritableStream which keeps
// renderToPipeableStream from ending. Skip it for now in the
// node streams path until we have a Node-compatible debug channel.
// debugChannel: debugChannel?.serverSide,
debugChannel: debugChannel?.serverSide,
}
)
......
......@@ -136,6 +136,12 @@ export function resumeToFizzPipeableStream(
* Wraps react-server-dom-webpack/server.node renderToPipeableStream
* for Flight (RSC) rendering. Since Flight rendering starts immediately
* (no shell concept), we pipe to a PassThrough right away.
*
* When options.debugChannel is a web-style { writable: WritableStream },
* it is bridged to a Node.js Writable so that React's Node API can use it.
* React's renderToPipeableStream expects debugChannel to be a Node Writable
* (has .write()), Duplex (has .read()), or WebSocket (has .send()), not the
* web { readable?, writable } shape used by renderToReadableStream.
*/
export function renderToFlightPipeableStream(
renderToPipeableStreamFn: (
......@@ -147,9 +153,37 @@ export function renderToFlightPipeableStream(
webpackMap: any,
options?: any
): Readable {
const { PassThrough } = getNodeStream()
const { PassThrough, Writable: NodeWritable } = getNodeStream()
const passthrough = new PassThrough()
const { pipe } = renderToPipeableStreamFn(model, webpackMap, options)
// Bridge web debug channel to Node.js Writable.
// React's Node API checks typeof debugChannel.write === 'function' to find
// the debug destination. A web WritableStream doesn't have .write() on the
// object itself, so React skips setting up the debug destination while still
// enabling debug info (because debugChannel !== undefined). This causes
// pendingDebugChunks to accumulate without ever being flushed, preventing
// the flight stream from completing.
let nodeOptions = options
const dc = options?.debugChannel
if (dc && 'writable' in dc && typeof dc.write !== 'function') {
const webWritable: WritableStream<Uint8Array> = dc.writable
const writer = webWritable.getWriter()
const nodeDebugWritable = new NodeWritable({
write(
chunk: Buffer | Uint8Array,
_encoding: string,
callback: (error?: Error | null) => void
) {
writer.write(chunk).then(() => callback(), callback)
},
final(callback: (error?: Error | null) => void) {
writer.close().then(() => callback(), callback)
},
})
nodeOptions = { ...options, debugChannel: nodeDebugWritable }
}
const { pipe } = renderToPipeableStreamFn(model, webpackMap, nodeOptions)
pipe(passthrough as unknown as Writable)
return passthrough
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment