Commit 523fce89 authored by Jimmy Lai's avatar Jimmy Lai

refactor: create node-native debug channel instead of bridging web streams

When __NEXT_USE_NODE_STREAMS is enabled, createDebugChannel() now creates
a Node.js Writable directly as the server side instead of a web
WritableStream. This eliminates the web-to-node bridge that was needed
in renderToFlightPipeableStream.

React's renderToPipeableStream (Node API) expects debugChannel to be a
Node Writable (checks typeof .write === 'function'). Previously, passing
{ writable: WritableStream } caused React to enable debug info production
without a drain destination, hanging the flight stream.

Also updates vendored React types in $$compiled.internal.d.ts to add
the debugChannel option to renderToPipeableStream and widen the web
API types to accept the DebugChannelServer union.
parent 8b14fc19
export type DebugChannelPair = { export type DebugChannelPair = {
// serverSide is passed opaquely as options.debugChannel to React's
// renderToReadableStream (web) or renderToPipeableStream (Node).
// The two APIs expect different shapes:
// - Web: { readable?, writable: WritableStream }
// - Node: a Writable stream (has .write() directly on the object)
// Consumers should not access properties on serverSide directly.
serverSide: DebugChannelServer serverSide: DebugChannelServer
clientSide: DebugChannelClient clientSide: DebugChannelClient
} }
export type DebugChannelServer = { // Passed directly as options.debugChannel to React's render functions.
// React's web API (renderToReadableStream) reads .writable from it.
// React's Node API (renderToPipeableStream) checks .write() on it directly.
export type DebugChannelServer =
| {
readable?: ReadableStream<Uint8Array> readable?: ReadableStream<Uint8Array>
writable: WritableStream<Uint8Array> writable: WritableStream<Uint8Array>
} }
| import('node:stream').Writable
export type DebugChannelClient = { export type DebugChannelClient = {
readable: ReadableStream<Uint8Array> readable: ReadableStream<Uint8Array>
writable?: WritableStream<Uint8Array> writable?: WritableStream<Uint8Array>
...@@ -19,12 +30,39 @@ export function createDebugChannel(): DebugChannelPair | undefined { ...@@ -19,12 +30,39 @@ export function createDebugChannel(): DebugChannelPair | undefined {
let readableController: ReadableStreamDefaultController | undefined let readableController: ReadableStreamDefaultController | undefined
let clientSideReadable = new ReadableStream<Uint8Array>({ const clientSideReadable = new ReadableStream<Uint8Array>({
start(controller) { start(controller) {
readableController = controller readableController = controller
}, },
}) })
// When node streams are enabled, create a Node.js Writable as the server
// side. React's renderToPipeableStream checks typeof debugChannel.write
// to find its debug destination. A web WritableStream doesn't have .write()
// on the object, so it must be a Node Writable.
if (process.env.__NEXT_USE_NODE_STREAMS) {
const { Writable } = require('node:stream') as typeof import('node:stream')
const serverSide = new Writable({
write(
chunk: Buffer | Uint8Array,
_encoding: string,
callback: (error?: Error | null) => void
) {
readableController?.enqueue(chunk)
callback()
},
final(callback: (error?: Error | null) => void) {
readableController?.close()
callback()
},
destroy(_err, callback) {
readableController?.error(_err)
callback(_err)
},
})
return { serverSide, clientSide: { readable: clientSideReadable } }
}
return { return {
serverSide: { serverSide: {
writable: new WritableStream<Uint8Array>({ writable: new WritableStream<Uint8Array>({
......
...@@ -136,12 +136,6 @@ export function resumeToFizzPipeableStream( ...@@ -136,12 +136,6 @@ export function resumeToFizzPipeableStream(
* Wraps react-server-dom-webpack/server.node renderToPipeableStream * Wraps react-server-dom-webpack/server.node renderToPipeableStream
* for Flight (RSC) rendering. Since Flight rendering starts immediately * for Flight (RSC) rendering. Since Flight rendering starts immediately
* (no shell concept), we pipe to a PassThrough right away. * (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( export function renderToFlightPipeableStream(
renderToPipeableStreamFn: ( renderToPipeableStreamFn: (
...@@ -153,37 +147,9 @@ export function renderToFlightPipeableStream( ...@@ -153,37 +147,9 @@ export function renderToFlightPipeableStream(
webpackMap: any, webpackMap: any,
options?: any options?: any
): Readable { ): Readable {
const { PassThrough, Writable: NodeWritable } = getNodeStream() const { PassThrough } = getNodeStream()
const passthrough = new PassThrough() 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) pipe(passthrough as unknown as Writable)
return passthrough return passthrough
} }
...@@ -114,7 +114,9 @@ declare module 'react-server-dom-webpack/client.browser' { ...@@ -114,7 +114,9 @@ declare module 'react-server-dom-webpack/client.browser' {
findSourceMapURL: FindSourceMapURLCallback | undefined findSourceMapURL: FindSourceMapURLCallback | undefined
replayConsoleLogs?: boolean replayConsoleLogs?: boolean
temporaryReferences?: TemporaryReferenceSet temporaryReferences?: TemporaryReferenceSet
debugChannel?: { readable?: ReadableStream; writable?: WritableStream } debugChannel?:
| { readable?: ReadableStream; writable?: WritableStream }
| import('node:stream').Writable
startTime?: number startTime?: number
endTime?: number endTime?: number
} }
...@@ -168,7 +170,9 @@ declare module 'react-server-dom-webpack/server.edge' { ...@@ -168,7 +170,9 @@ declare module 'react-server-dom-webpack/server.edge' {
| undefined | undefined
onError?: (error: unknown) => void onError?: (error: unknown) => void
signal?: AbortSignal signal?: AbortSignal
debugChannel?: { readable?: ReadableStream; writable?: WritableStream } debugChannel?:
| { readable?: ReadableStream; writable?: WritableStream }
| import('node:stream').Writable
startTime?: number startTime?: number
} }
): ReadableStream<Uint8Array> ): ReadableStream<Uint8Array>
...@@ -240,6 +244,11 @@ declare module 'react-server-dom-webpack/server.node' { ...@@ -240,6 +244,11 @@ declare module 'react-server-dom-webpack/server.node' {
| undefined | undefined
onError?: (error: unknown) => void onError?: (error: unknown) => void
signal?: AbortSignal signal?: AbortSignal
// React's Node API expects debugChannel to be a Node.js Writable
// (has .write()), Duplex (has .read()), or WebSocket (has .send()).
// This differs from the web API which expects { readable?, writable? }.
debugChannel?: import('node:stream').Writable
startTime?: number
} }
): { ): {
pipe<Writable extends NodeJS.WritableStream>( pipe<Writable extends NodeJS.WritableStream>(
......
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