Commit 62575295 authored by Jimmy Lai's avatar Jimmy Lai Committed by Tim Neutkens

feat(node-streams): wire node stream primitives into render pipeline

Integrate the node stream building blocks (from prior PR) into the
actual render paths, all gated behind experimental.useNodeStreams:

- app-render.tsx: add useNodeStreams branching throughout render paths,
  teeDebugChannelForSsrAndBrowser and debugChannelClientForBrowser helpers
- app-render-prerender-utils.ts: ReactServerResult accepts NodeReadable,
  createReactServerPrerenderResultFromPrerender dispatcher
- instant-validation.tsx: node stream branches for validation renders
- render-result.ts: accept Node Readable as response body with piping
- flight-render-result.ts: accept Node Readable in constructor
- app-page.ts: PPR resume path for node streams
- stream-ops.ts/debug-channel-server.ts: widen types to include Readable
parent 558f4248
...@@ -71,6 +71,7 @@ import { ...@@ -71,6 +71,7 @@ import {
getPostponedStateExceededErrorMessage, getPostponedStateExceededErrorMessage,
readBodyWithSizeLimit, readBodyWithSizeLimit,
} from '../../server/lib/postponed-request-body' } from '../../server/lib/postponed-request-body'
import { chainNodeStreams } from '../../server/stream-utils/chain-node-streams'
// These are injected by the loader afterwards. // These are injected by the loader afterwards.
...@@ -1495,6 +1496,64 @@ export async function handler( ...@@ -1495,6 +1496,64 @@ export async function handler(
}) })
} }
if (process.env.__NEXT_USE_NODE_STREAMS) {
// Node streams path: construct the response as a single Node Readable
// chain (cached HTML + optional sentinel + resume render) to avoid
// web stream interop issues with Readable.toWeb().
const { PassThrough, Readable: NodeReadable } =
require('node:stream') as typeof import('node:stream')
const streams: import('node:stream').Readable[] = [
NodeReadable.from(body.toUnchunkedString()),
]
if (process.env.__NEXT_TEST_MODE) {
streams.push(NodeReadable.from('<!-- PPR_BOUNDARY_SENTINEL -->'))
}
const bridge = new PassThrough()
streams.push(bridge)
const combined = chainNodeStreams(...streams)
const result = new RenderResult(combined, {
contentType: body.contentType,
metadata: body.metadata as any,
})
doRender({
span,
postponed: cachedData.postponed,
fallbackRouteParams: null,
forceStaticRender: false,
})
.then(async (renderResult) => {
if (!renderResult) {
throw new Error('Invariant: expected a result to be returned')
}
if (renderResult.value?.kind !== CachedRouteKind.APP_PAGE) {
throw new Error(
`Invariant: expected a page response, got ${renderResult.value?.kind}`
)
}
await renderResult.value.html.pipeToNodeWritable(bridge)
})
.catch((err) => {
bridge.destroy(err instanceof Error ? err : new Error(String(err)))
})
return sendRenderResult({
req,
res,
generateEtags: nextConfig.generateEtags,
poweredByHeader: nextConfig.poweredByHeader,
result,
cacheControl: { revalidate: 0, expire: undefined },
})
}
// If we're in test mode, we should add a sentinel chunk to the response // If we're in test mode, we should add a sentinel chunk to the response
// that's between the static and dynamic parts so we can compare the // that's between the static and dynamic parts so we can compare the
// chunks and add assertions. // chunks and add assertions.
......
import type { Readable as NodeReadable } from 'node:stream' import type { Readable as NodeReadable } from 'node:stream'
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
import { teeNodeReadable } from './node-stream-tee'
// React's RSC prerender function will emit an incomplete flight stream when using `prerender`. If the connection // React's RSC prerender function will emit an incomplete flight stream when using `prerender`. If the connection
// closes then whatever hanging chunks exist will be errored. This is because prerender (an experimental feature) // closes then whatever hanging chunks exist will be errored. This is because prerender (an experimental feature)
// has not yet implemented a concept of resume. For now we will simulate a paused connection by wrapping the stream // has not yet implemented a concept of resume. For now we will simulate a paused connection by wrapping the stream
// in one that doesn't close even when the underlying is complete. // in one that doesn't close even when the underlying is complete.
export class ReactServerResult { export class ReactServerResult {
private _stream: null | ReadableStream<Uint8Array> private _stream: null | ReadableStream<Uint8Array> | NodeReadable
private readonly _runInContext: <T>(fn: () => T) => T
constructor(stream: ReadableStream<Uint8Array>) { constructor(
stream: ReadableStream<Uint8Array> | NodeReadable,
runInContext?: <T>(fn: () => T) => T
) {
this._stream = stream this._stream = stream
this._runInContext = runInContext ?? ((fn) => fn())
} }
tee() { tee(): ReadableStream<Uint8Array> | NodeReadable {
if (this._stream === null) { if (this._stream === null) {
throw new Error( throw new Error(
'Cannot tee a ReactServerResult that has already been consumed' 'Cannot tee a ReactServerResult that has already been consumed'
) )
} }
const tee = this._stream.tee() if (this._stream instanceof ReadableStream) {
this._stream = tee[0] const tee = this._stream.tee()
return tee[1] this._stream = tee[0]
return tee[1]
} else if (process.env.NEXT_RUNTIME !== 'edge') {
// Node tee callback handlers run on event boundaries, so keep request ALS
// context stable for whichever branch is consumed later.
const [primary, secondary] = teeNodeReadable(
this._stream as NodeReadable,
this._runInContext
)
this._stream = primary
return secondary
} else {
throw new Error('Cannot tee a Node.js stream in the edge runtime')
}
} }
consume() { consume(): ReadableStream<Uint8Array> | NodeReadable {
if (this._stream === null) { if (this._stream === null) {
throw new Error( throw new Error(
'Cannot consume a ReactServerResult that has already been consumed' 'Cannot consume a ReactServerResult that has already been consumed'
...@@ -39,6 +58,10 @@ export type ReactServerPrerenderResolveToType = { ...@@ -39,6 +58,10 @@ export type ReactServerPrerenderResolveToType = {
prelude: ReadableStream<Uint8Array> prelude: ReadableStream<Uint8Array>
} }
export type ReactServerPrerenderNodeResolveToType = {
prelude: NodeReadable
}
export async function createReactServerPrerenderResult( export async function createReactServerPrerenderResult(
underlying: Promise<ReactServerPrerenderResolveToType> underlying: Promise<ReactServerPrerenderResolveToType>
): Promise<ReactServerPrerenderResult> { ): Promise<ReactServerPrerenderResult> {
...@@ -55,17 +78,57 @@ export async function createReactServerPrerenderResult( ...@@ -55,17 +78,57 @@ export async function createReactServerPrerenderResult(
} }
} }
export async function createReactServerPrerenderResultFromNodeStream(
underlying: Promise<ReactServerPrerenderNodeResolveToType>
): Promise<ReactServerPrerenderResult> {
const chunks: Array<Uint8Array> = []
const { prelude } = await underlying
for await (const chunk of prelude) {
chunks.push(
Buffer.isBuffer(chunk) ? new Uint8Array(chunk) : (chunk as Uint8Array)
)
}
return new ReactServerPrerenderResult(chunks)
}
/**
* Compile-time unified factory: creates a ReactServerPrerenderResult from a
* prerender promise that resolves to { prelude }, using either the web or node
* stream consumer based on __NEXT_USE_NODE_STREAMS.
*/
export async function createReactServerPrerenderResultFromPrerender(
underlying: Promise<{ prelude: ReadableStream<Uint8Array> | NodeReadable }>
): Promise<ReactServerPrerenderResult> {
if (process.env.__NEXT_USE_NODE_STREAMS) {
return createReactServerPrerenderResultFromNodeStream(
underlying as Promise<ReactServerPrerenderNodeResolveToType>
)
}
return createReactServerPrerenderResult(
underlying as Promise<ReactServerPrerenderResolveToType>
)
}
export async function createReactServerPrerenderResultFromRender( export async function createReactServerPrerenderResultFromRender(
underlying: ReadableStream<Uint8Array> underlying: ReadableStream<Uint8Array> | NodeReadable
): Promise<ReactServerPrerenderResult> { ): Promise<ReactServerPrerenderResult> {
const chunks: Array<Uint8Array> = [] const chunks: Array<Uint8Array> = []
const reader = underlying.getReader() if (underlying instanceof ReadableStream) {
while (true) { const reader = underlying.getReader()
const { done, value } = await reader.read() while (true) {
if (done) { const { done, value } = await reader.read()
break if (done) {
} else { break
chunks.push(value) } else {
chunks.push(value)
}
}
} else {
// Node.js Readable
for await (const chunk of underlying) {
chunks.push(
Buffer.isBuffer(chunk) ? new Uint8Array(chunk) : (chunk as Uint8Array)
)
} }
} }
return new ReactServerPrerenderResult(chunks) return new ReactServerPrerenderResult(chunks)
...@@ -115,6 +178,53 @@ export class ReactServerPrerenderResult { ...@@ -115,6 +178,53 @@ export class ReactServerPrerenderResult {
const chunks = this.consumeChunks('consumeAsStream()') const chunks = this.consumeChunks('consumeAsStream()')
return createClosingStream(chunks) return createClosingStream(chunks)
} }
asNodeStream(): NodeReadable {
const chunks = this.assertChunks('asNodeStream()')
return createClosingNodeStream(chunks)
}
consumeAsNodeStream(): NodeReadable {
const chunks = this.consumeChunks('consumeAsNodeStream()')
return createClosingNodeStream(chunks)
}
asUnclosingNodeStream(): NodeReadable {
const chunks = this.assertChunks('asUnclosingNodeStream()')
return createUnclosingNodeStream(chunks)
}
consumeAsUnclosingNodeStream(): NodeReadable {
const chunks = this.consumeChunks('consumeAsUnclosingNodeStream()')
return createUnclosingNodeStream(chunks)
}
// Compile-time unified methods: use __NEXT_USE_NODE_STREAMS to pick
// the right stream type without branching at the call site.
asFlightStream(): ReadableStream<Uint8Array> | NodeReadable {
return process.env.__NEXT_USE_NODE_STREAMS
? this.asNodeStream()
: this.asStream()
}
consumeAsFlightStream(): ReadableStream<Uint8Array> | NodeReadable {
return process.env.__NEXT_USE_NODE_STREAMS
? this.consumeAsNodeStream()
: this.consumeAsStream()
}
asUnclosingFlightStream(): ReadableStream<Uint8Array> | NodeReadable {
return process.env.__NEXT_USE_NODE_STREAMS
? this.asUnclosingNodeStream()
: this.asUnclosingStream()
}
consumeAsUnclosingFlightStream(): ReadableStream<Uint8Array> | NodeReadable {
return process.env.__NEXT_USE_NODE_STREAMS
? this.consumeAsUnclosingNodeStream()
: this.consumeAsUnclosingStream()
}
} }
function createUnclosingStream( function createUnclosingStream(
...@@ -148,6 +258,38 @@ function createClosingStream( ...@@ -148,6 +258,38 @@ function createClosingStream(
}) })
} }
function createClosingNodeStream(chunks: Array<Uint8Array>): NodeReadable {
if (process.env.NEXT_RUNTIME === 'edge') {
throw new Error('createClosingNodeStream is not supported in edge runtime')
} else {
const { PassThrough } =
require('node:stream') as typeof import('node:stream')
const pt = new PassThrough()
for (const chunk of chunks) {
pt.write(chunk)
}
pt.end()
return pt
}
}
function createUnclosingNodeStream(chunks: Array<Uint8Array>): NodeReadable {
if (process.env.NEXT_RUNTIME === 'edge') {
throw new Error(
'createUnclosingNodeStream is not supported in edge runtime'
)
} else {
const { PassThrough } =
require('node:stream') as typeof import('node:stream')
const pt = new PassThrough()
for (const chunk of chunks) {
pt.write(chunk)
}
// intentionally do not end the stream
return pt
}
}
export async function processPrelude( export async function processPrelude(
unprocessedPrelude: ReadableStream<Uint8Array> unprocessedPrelude: ReadableStream<Uint8Array>
) { ) {
...@@ -166,8 +308,6 @@ export async function processNodePrelude(unprocessedPrelude: NodeReadable) { ...@@ -166,8 +308,6 @@ export async function processNodePrelude(unprocessedPrelude: NodeReadable) {
if (process.env.NEXT_RUNTIME === 'edge') { if (process.env.NEXT_RUNTIME === 'edge') {
throw new Error('processNodePrelude is not supported in edge runtime') throw new Error('processNodePrelude is not supported in edge runtime')
} else { } else {
const { teeNodeReadable } =
require('./node-stream-tee') as typeof import('./node-stream-tee')
const [prelude, peek] = teeNodeReadable(unprocessedPrelude) const [prelude, peek] = teeNodeReadable(unprocessedPrelude)
// Read the first chunk from the peek stream to check if it's empty // Read the first chunk from the peek stream to check if it's empty
......
...@@ -2,7 +2,7 @@ import type { ...@@ -2,7 +2,7 @@ import type {
DebugChannelPair, DebugChannelPair,
DebugChannelServer, DebugChannelServer,
DebugChannelClient, DebugChannelClient,
} from './debug-channel-server.web' } from './debug-channel-server.node'
type DebugChannelRuntimeModule = { type DebugChannelRuntimeModule = {
createDebugChannel: () => DebugChannelPair | undefined createDebugChannel: () => DebugChannelPair | undefined
...@@ -18,7 +18,7 @@ if (process.env.NEXT_RUNTIME === 'edge') { ...@@ -18,7 +18,7 @@ if (process.env.NEXT_RUNTIME === 'edge') {
require('./debug-channel-server.web') as typeof import('./debug-channel-server.web') require('./debug-channel-server.web') as typeof import('./debug-channel-server.web')
} else if (process.env.__NEXT_USE_NODE_STREAMS) { } else if (process.env.__NEXT_USE_NODE_STREAMS) {
debugChannelRuntimeModule = debugChannelRuntimeModule =
require('./debug-channel-server.node') as typeof import('./debug-channel-server.node') as unknown as DebugChannelRuntimeModule require('./debug-channel-server.node') as typeof import('./debug-channel-server.node')
} else { } else {
debugChannelRuntimeModule = debugChannelRuntimeModule =
require('./debug-channel-server.web') as typeof import('./debug-channel-server.web') require('./debug-channel-server.web') as typeof import('./debug-channel-server.web')
......
/** import type {
* Web debug channel implementation. DebugChannelPair,
* Loaded by debug-channel-server.ts. DebugChannelServer,
*/ } from './debug-channel-server.node'
// Types defined inline for now; will move to debug-channel-server.node.ts later.
export type DebugChannelPair = {
serverSide: DebugChannelServer
clientSide: DebugChannelClient
}
export type DebugChannelServer = { export type { DebugChannelPair, DebugChannelServer }
readable?: ReadableStream<Uint8Array>
writable: WritableStream<Uint8Array>
}
export type DebugChannelClient = {
readable: ReadableStream<Uint8Array>
writable?: WritableStream<Uint8Array>
}
export function createDebugChannel(): DebugChannelPair | undefined { export function createDebugChannel(): DebugChannelPair | undefined {
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
......
import type { Readable } from 'node:stream'
import { RSC_CONTENT_TYPE_HEADER } from '../../client/components/app-router-headers' import { RSC_CONTENT_TYPE_HEADER } from '../../client/components/app-router-headers'
import RenderResult, { type RenderResultMetadata } from '../render-result' import RenderResult, { type RenderResultMetadata } from '../render-result'
...@@ -6,7 +7,7 @@ import RenderResult, { type RenderResultMetadata } from '../render-result' ...@@ -6,7 +7,7 @@ import RenderResult, { type RenderResultMetadata } from '../render-result'
*/ */
export class FlightRenderResult extends RenderResult { export class FlightRenderResult extends RenderResult {
constructor( constructor(
response: string | ReadableStream<Uint8Array>, response: string | ReadableStream<Uint8Array> | Readable,
metadata: RenderResultMetadata = {}, metadata: RenderResultMetadata = {},
waitUntil?: Promise<unknown> waitUntil?: Promise<unknown>
) { ) {
......
...@@ -10,7 +10,7 @@ import type { ...@@ -10,7 +10,7 @@ import type {
FlightPayload, FlightPayload,
FlightClientModules, FlightClientModules,
FlightRenderOptions, FlightRenderOptions,
} from './stream-ops.web' } from './stream-ops.node'
export type { export type {
AnyStream, AnyStream,
...@@ -23,7 +23,7 @@ export type { ...@@ -23,7 +23,7 @@ export type {
FlightPayload, FlightPayload,
FlightClientModules, FlightClientModules,
FlightRenderOptions, FlightRenderOptions,
} from './stream-ops.web' } from './stream-ops.node'
export type FizzStreamResult = { export type FizzStreamResult = {
stream: AnyStream stream: AnyStream
...@@ -58,7 +58,9 @@ type StreamOpsRuntimeModule = { ...@@ -58,7 +58,9 @@ type StreamOpsRuntimeModule = {
prelude: AnyStream prelude: AnyStream
preludeIsEmpty: boolean preludeIsEmpty: boolean
}> }>
createDocumentClosingStream: () => AnyStream createDocumentClosingStream: () =>
| ReadableStream<Uint8Array>
| import('node:stream').Readable
createInlinedDataStream: ( createInlinedDataStream: (
source: AnyStream, source: AnyStream,
nonce: string | undefined, nonce: string | undefined,
...@@ -109,7 +111,10 @@ type StreamOpsRuntimeModule = { ...@@ -109,7 +111,10 @@ type StreamOpsRuntimeModule = {
let streamOpsRuntimeModule: StreamOpsRuntimeModule let streamOpsRuntimeModule: StreamOpsRuntimeModule
if (process.env.__NEXT_USE_NODE_STREAMS) { if (process.env.NEXT_RUNTIME === 'edge') {
streamOpsRuntimeModule =
require('./stream-ops.web') as typeof import('./stream-ops.web') as unknown as StreamOpsRuntimeModule
} else if (process.env.__NEXT_USE_NODE_STREAMS) {
streamOpsRuntimeModule = streamOpsRuntimeModule =
require('./stream-ops.node') as typeof import('./stream-ops.node') as unknown as StreamOpsRuntimeModule require('./stream-ops.node') as typeof import('./stream-ops.node') as unknown as StreamOpsRuntimeModule
} else { } else {
......
/** /**
* Web stream operations for the rendering pipeline. * Web stream operations for the rendering pipeline.
* Loaded by stream-ops.ts (re-export in this PR, conditional switcher later). * Loaded by stream-ops.js when __NEXT_USE_NODE_STREAMS is false/undefined.
*/ */
import type {
AnyStream,
ContinueFizzStreamOptions,
ContinueStaticPrerenderOptions,
ContinueStreamSharedOptions,
ContinueDynamicHTMLResumeOptions,
FlightComponentMod,
ServerPrerenderComponentMod,
FlightPayload,
FlightClientModules,
FlightRenderOptions,
} from './stream-ops.node'
// Re-export shared types so they're available to consumers
export type {
AnyStream,
ContinueFizzStreamOptions,
ContinueStaticPrerenderOptions,
ContinueStreamSharedOptions,
ContinueDynamicHTMLResumeOptions,
FlightComponentMod,
ServerPrerenderComponentMod,
FlightPayload,
FlightClientModules,
FlightRenderOptions,
}
import type { PostponedState, PrerenderOptions } from 'react-dom/static' import type { PostponedState, PrerenderOptions } from 'react-dom/static'
import { resume, renderToReadableStream } from 'react-dom/server' import { resume, renderToReadableStream } from 'react-dom/server'
import { prerender } from 'react-dom/static' import { prerender } from 'react-dom/static'
...@@ -11,68 +38,15 @@ import { ...@@ -11,68 +38,15 @@ import {
renderToInitialFizzStream, renderToInitialFizzStream,
streamToString as webStreamToString, streamToString as webStreamToString,
createRuntimePrefetchTransformStream, createRuntimePrefetchTransformStream,
continueFizzStream as webContinueFizzStream,
} from '../stream-utils/node-web-streams-helper' } from '../stream-utils/node-web-streams-helper'
import { createInlinedDataReadableStream } from './use-flight-response' import { createInlinedDataReadableStream } from './use-flight-response'
// ---------------------------------------------------------------------------
// Shared types (web-only for now; will move to stream-ops.node.ts later)
// ---------------------------------------------------------------------------
type FlightRenderToReadableStream = (
model: any,
webpackMap: any,
options?: any
) => ReadableStream<Uint8Array>
export type AnyStream = ReadableStream<Uint8Array>
export type ContinueStreamSharedOptions = {
deploymentId: string | undefined
getServerInsertedHTML: () => Promise<string>
getServerInsertedMetadata: () => Promise<string>
}
export type ContinueFizzStreamOptions = ContinueStreamSharedOptions & {
inlinedDataStream: AnyStream | undefined
isStaticGeneration: boolean
allReady?: Promise<void>
validateRootLayout?: boolean
suffix?: string
}
export type ContinueStaticPrerenderOptions = ContinueStreamSharedOptions & {
inlinedDataStream: AnyStream
}
export type ContinueDynamicHTMLResumeOptions = ContinueStreamSharedOptions & {
inlinedDataStream: AnyStream
delayDataUntilFirstHtmlChunk: boolean
}
export type FlightComponentMod = {
renderToReadableStream: FlightRenderToReadableStream
}
export type ServerPrerenderComponentMod = {
prerender: (...args: any[]) => Promise<any>
}
export type FlightPayload = Parameters<FlightRenderToReadableStream>[0]
export type FlightClientModules = Parameters<FlightRenderToReadableStream>[1]
export type FlightRenderOptions = Parameters<FlightRenderToReadableStream>[2]
export type FizzStreamResult = {
stream: AnyStream
allReady: Promise<void>
abort?: (reason?: unknown) => void
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Continue functions // Continue functions
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export { export {
continueFizzStream,
continueStaticPrerender, continueStaticPrerender,
continueDynamicPrerender, continueDynamicPrerender,
continueStaticFallbackPrerender, continueStaticFallbackPrerender,
...@@ -84,18 +58,6 @@ export { ...@@ -84,18 +58,6 @@ export {
export { processPrelude } from './app-render-prerender-utils' export { processPrelude } from './app-render-prerender-utils'
/**
* Wrapper for continueFizzStream that accepts AnyStream.
* The underlying implementation expects ReactDOMServerReadableStream but at
* the stream-ops boundary we only expose AnyStream.
*/
export function continueFizzStream(
renderStream: AnyStream,
opts: ContinueFizzStreamOptions
): Promise<ReadableStream<Uint8Array>> {
return webContinueFizzStream(renderStream as any, opts)
}
// Not available in web bundles // Not available in web bundles
export const nodeReadableToWeb: export const nodeReadableToWeb:
| ((readable: import('node:stream').Readable) => ReadableStream<Uint8Array>) | ((readable: import('node:stream').Readable) => ReadableStream<Uint8Array>)
...@@ -160,6 +122,12 @@ export async function streamToString(stream: AnyStream): Promise<string> { ...@@ -160,6 +122,12 @@ export async function streamToString(stream: AnyStream): Promise<string> {
return webStreamToString(stream as ReadableStream<Uint8Array>) return webStreamToString(stream as ReadableStream<Uint8Array>)
} }
export type FizzStreamResult = {
stream: AnyStream
allReady: Promise<void>
abort?: (reason?: unknown) => void
}
export async function renderToFizzStream( export async function renderToFizzStream(
element: React.ReactElement, element: React.ReactElement,
streamOptions: any, streamOptions: any,
......
import type { OutgoingHttpHeaders, ServerResponse } from 'http' import type { OutgoingHttpHeaders, ServerResponse } from 'http'
import type { Readable as NodeReadable } from 'node:stream'
import type { CacheControl } from './lib/cache-control' import type { CacheControl } from './lib/cache-control'
import type { FetchMetrics } from './base-http' import type { FetchMetrics } from './base-http'
...@@ -73,6 +74,7 @@ export type RenderResultMetadata = AppPageRenderResultMetadata & ...@@ -73,6 +74,7 @@ export type RenderResultMetadata = AppPageRenderResultMetadata &
export type RenderResultResponse = export type RenderResultResponse =
| ReadableStream<Uint8Array>[] | ReadableStream<Uint8Array>[]
| ReadableStream<Uint8Array> | ReadableStream<Uint8Array>
| NodeReadable
| string | string
| Buffer | Buffer
| null | null
...@@ -173,6 +175,19 @@ export default class RenderResult< ...@@ -173,6 +175,19 @@ export default class RenderResult<
* @param stream Whether or not to return a promise if the response is dynamic * @param stream Whether or not to return a promise if the response is dynamic
* @returns The response as a string * @returns The response as a string
*/ */
private isNodeReadable(
response: RenderResultResponse
): response is NodeReadable {
return (
response !== null &&
typeof response === 'object' &&
!Array.isArray(response) &&
!Buffer.isBuffer(response) &&
!(response instanceof ReadableStream) &&
typeof (response as any).pipe === 'function'
)
}
public toUnchunkedString(stream?: false): string public toUnchunkedString(stream?: false): string
public toUnchunkedString(stream: true): Promise<string> public toUnchunkedString(stream: true): Promise<string>
public toUnchunkedString(stream = false): Promise<string> | string { public toUnchunkedString(stream = false): Promise<string> | string {
...@@ -189,14 +204,24 @@ export default class RenderResult< ...@@ -189,14 +204,24 @@ export default class RenderResult<
) )
} }
return streamToString(this.readable) if (process.env.__NEXT_USE_NODE_STREAMS) {
if (this.isNodeReadable(this.response)) {
const { nodeStreamToString } =
require('./stream-utils/node-stream-helpers') as typeof import('./stream-utils/node-stream-helpers')
return nodeStreamToString(this.response)
}
return streamToString(this.readable)
} else {
return streamToString(this.readable)
}
} }
return this.response return this.response
} }
/** /**
* Returns a readable stream of the response. * Returns a web readable stream of the response.
* For Node.js Readable responses, use pipeToNodeResponse directly.
*/ */
private get readable(): ReadableStream<Uint8Array> { private get readable(): ReadableStream<Uint8Array> {
if (this.response === null) { if (this.response === null) {
...@@ -217,9 +242,16 @@ export default class RenderResult< ...@@ -217,9 +242,16 @@ export default class RenderResult<
return streamFromBuffer(this.response) return streamFromBuffer(this.response)
} }
// If the response is an array of streams, then chain them together. // If the response is an array of web streams, then chain them together.
if (Array.isArray(this.response)) { if (Array.isArray(this.response)) {
return chainStreams(...this.response) return chainStreams(...(this.response as ReadableStream<Uint8Array>[]))
}
// Node.js Readable: should not reach here, use pipeToNodeResponse path
if (this.isNodeReadable(this.response)) {
throw new InvariantError(
'Node.js Readable responses should use the pipeToNodeResponse path directly'
)
} }
return this.response return this.response
...@@ -241,11 +273,15 @@ export default class RenderResult< ...@@ -241,11 +273,15 @@ export default class RenderResult<
if (typeof this.response === 'string') { if (typeof this.response === 'string') {
return [streamFromString(this.response)] return [streamFromString(this.response)]
} else if (Array.isArray(this.response)) { } else if (Array.isArray(this.response)) {
return this.response return this.response as ReadableStream<Uint8Array>[]
} else if (Buffer.isBuffer(this.response)) { } else if (Buffer.isBuffer(this.response)) {
return [streamFromBuffer(this.response)] return [streamFromBuffer(this.response)]
} else if (this.isNodeReadable(this.response)) {
throw new InvariantError(
'Node.js Readable responses cannot be coerced to web ReadableStream arrays'
)
} else { } else {
return [this.response] return [this.response as ReadableStream<Uint8Array>]
} }
} }
...@@ -329,6 +365,44 @@ export default class RenderResult< ...@@ -329,6 +365,44 @@ export default class RenderResult<
* @param res * @param res
*/ */
public async pipeToNodeResponse(res: ServerResponse) { public async pipeToNodeResponse(res: ServerResponse) {
// If the response is a Node.js Readable, pipe directly without
// going through web stream conversion.
if (
process.env.__NEXT_USE_NODE_STREAMS &&
this.isNodeReadable(this.response)
) {
const { pipeNodeReadableToResponse } =
require('./pipe-readable') as typeof import('./pipe-readable')
await pipeNodeReadableToResponse(this.response, res, this.waitUntil)
return
}
await pipeToNodeResponse(this.readable, res, this.waitUntil) await pipeToNodeResponse(this.readable, res, this.waitUntil)
} }
/**
* Pipes a Node.js Readable response to a Node.js Writable stream
* (e.g. a PassThrough used as a bridge). Unlike pipeToNodeResponse,
* this does not assume ServerResponse-specific APIs like flushHeaders().
*/
public async pipeToNodeWritable(
writable: import('node:stream').Writable
): Promise<void> {
if (process.env.__NEXT_USE_NODE_STREAMS) {
const { pipeline } =
require('node:stream/promises') as typeof import('node:stream/promises')
if (this.isNodeReadable(this.response)) {
await pipeline(this.response, writable)
return
}
const { Readable } =
require('node:stream') as typeof import('node:stream')
await pipeline(
Readable.fromWeb(this.readable as import('stream/web').ReadableStream),
writable
)
}
}
} }
...@@ -133,18 +133,22 @@ describe('react-dom/server in React Server environment', () => { ...@@ -133,18 +133,22 @@ describe('react-dom/server in React Server environment', () => {
expect(await browser.elementByCss('main').text()).toMatchInlineSnapshot(` expect(await browser.elementByCss('main').text()).toMatchInlineSnapshot(`
"{ "{
"default": [ "default": [
"renderToPipeableStream",
"renderToReadableStream", "renderToReadableStream",
"renderToStaticMarkup", "renderToStaticMarkup",
"renderToString", "renderToString",
"resume", "resume",
"resumeToPipeableStream",
"version" "version"
], ],
"named": [ "named": [
"default", "default",
"renderToPipeableStream",
"renderToReadableStream", "renderToReadableStream",
"renderToStaticMarkup", "renderToStaticMarkup",
"renderToString", "renderToString",
"resume", "resume",
"resumeToPipeableStream",
"version" "version"
] ]
}" }"
...@@ -481,18 +485,22 @@ describe('react-dom/server in React Server environment', () => { ...@@ -481,18 +485,22 @@ describe('react-dom/server in React Server environment', () => {
"{ "{
"default": { "default": {
"default": [ "default": [
"renderToPipeableStream",
"renderToReadableStream", "renderToReadableStream",
"renderToStaticMarkup", "renderToStaticMarkup",
"renderToString", "renderToString",
"resume", "resume",
"resumeToPipeableStream",
"version" "version"
], ],
"named": [ "named": [
"default", "default",
"renderToPipeableStream",
"renderToReadableStream", "renderToReadableStream",
"renderToStaticMarkup", "renderToStaticMarkup",
"renderToString", "renderToString",
"resume", "resume",
"resumeToPipeableStream",
"version" "version"
] ]
} }
......
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
describe('user bundle - node stream guard', () => {
const { next, isNextDev, isTurbopack, skipped } = nextTestSetup({
// Reuse this fixture because it exercises app-page resume/render bundling.
files: path.join(__dirname, '../ppr-partial-hydration'),
skipDeployment: true,
skipStart: true,
})
if (isNextDev || skipped || isTurbopack) {
it.skip('only testable in webpack production (non-deployment)', () => {})
return
}
it('should keep node-stream-only helpers out of default server chunks', async () => {
const { exitCode } = await next.build()
expect(exitCode).toBe(0)
const pageTrace = JSON.parse(
await next.readFile(
'.next/server/app/with-shell/without-metadata/page.js.nft.json'
)
) as { files: string[] }
const tracedFiles = pageTrace.files.join('\n')
expect(tracedFiles).not.toContain(
'next/dist/server/stream-utils/node-stream-helpers.js'
)
const tracedChunkFiles = pageTrace.files.filter(
(file) => file.startsWith('../../../chunks/') && file.endsWith('.js')
)
const chunkSources = (
await Promise.all(
tracedChunkFiles.map((file) =>
next.readFile(
path.join('.next/server/app/with-shell/without-metadata', file)
)
)
)
).join('\n')
expect(chunkSources).not.toContain('stream-utils/node-stream-helpers')
expect(chunkSources).not.toContain('node:stream/promises')
})
})
...@@ -194,7 +194,9 @@ async function readNormalizedNFT(next, name) { ...@@ -194,7 +194,9 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/compiled/is-wsl/index.js", "/node_modules/next/dist/compiled/is-wsl/index.js",
"/node_modules/next/dist/compiled/jsonwebtoken/index.js", "/node_modules/next/dist/compiled/jsonwebtoken/index.js",
"/node_modules/next/dist/compiled/nanoid/index.cjs", "/node_modules/next/dist/compiled/nanoid/index.cjs",
"/node_modules/next/dist/compiled/next-server/app-page-turbo-experimental-nodestreams.runtime.prod.js",
"/node_modules/next/dist/compiled/next-server/app-page-turbo-experimental.runtime.prod.js", "/node_modules/next/dist/compiled/next-server/app-page-turbo-experimental.runtime.prod.js",
"/node_modules/next/dist/compiled/next-server/app-page-turbo-nodestreams.runtime.prod.js",
"/node_modules/next/dist/compiled/next-server/app-page-turbo.runtime.prod.js", "/node_modules/next/dist/compiled/next-server/app-page-turbo.runtime.prod.js",
"/node_modules/next/dist/compiled/next-server/pages-turbo.runtime.prod.js", "/node_modules/next/dist/compiled/next-server/pages-turbo.runtime.prod.js",
"/node_modules/next/dist/compiled/p-limit/index.js", "/node_modules/next/dist/compiled/p-limit/index.js",
......
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