next.js/packages/next/src/server/app-render/debug-channel-server.node.ts
debug-channel-server.node.ts38 lines1.1 KB
/**
 * Node debug channel implementation.
 * Loaded by debug-channel-server.ts when __NEXT_USE_NODE_STREAMS is enabled.
 */

import { PassThrough, Writable } from 'node:stream'
import type { DebugChannelPair } from './debug-channel-server.web'

export { createWebDebugChannel } from './debug-channel-server.web'

/**
 * Creates a debug channel using Node.js streams.
 * Use with renderToNodeFlightStream (React's renderToPipeableStream),
 * which expects debugChannel to be a Node.js stream with a .write() method.
 */
export function createNodeDebugChannel(): DebugChannelPair {
  const readable = new PassThrough()

  // Use a plain Writable instead of exposing the PassThrough directly.
  // React's renderToPipeableStream detects .read() on the debugChannel and
  // enters bidirectional mode, reading its own output back as commands.
  const writable = new Writable({
    write(chunk, _encoding, callback) {
      readable.push(chunk)
      callback()
    },
    final(callback) {
      readable.push(null)
      callback()
    },
  })

  return {
    serverSide: writable,
    clientSide: { readable },
  }
}
Quest for Codev2.0.0
/
SIGN IN