Commit 436e2d45 authored by Jimmy Lai's avatar Jimmy Lai

feat: Node.js native stream rendering for App Router

parent 7ef10df5
......@@ -1045,5 +1045,18 @@
"1044": "No router instance found.\nYou should only use \"next/router\" on the client side of your app.\n",
"1045": "LRUCache: calculateSize returned %s%s, but size must be > 0. Items with size 0 would never be evicted, causing unbounded cache growth.",
"1046": "`draftMode` must not be used within a Client Component. Next.js should be preventing `draftMode` from being included in Client Components statically, but did not in this case.",
"1047": "Extra keys returned from getStaticPaths in %s (%s) Expected: { paths: [], fallback: boolean }\\nSee here for more info: https://nextjs.org/docs/messages/invalid-getstaticpaths-value"
"1047": "Extra keys returned from getStaticPaths in %s (%s) Expected: { paths: [], fallback: boolean }\\nSee here for more info: https://nextjs.org/docs/messages/invalid-getstaticpaths-value",
"1048": "failed to pipe node readable to response",
"1049": "Node.js Readable responses should use the pipeToNodeResponse path directly",
"1050": "Node.js Readable responses cannot be coerced to web ReadableStream arrays",
"1051": "createRuntimePrefetchNodeTransform is not supported in edge runtime",
"1052": "createInlinedDataNodeStream is not supported in edge runtime",
"1053": "NODE_STREAMS_PATH_REACHED",
"1054": "processNodePrelude is not supported in edge runtime",
"1055": "createUnclosingNodeStream is not supported in edge runtime",
"1056": "createClosingNodeStream is not supported in edge runtime",
"1057": "Cannot tee a Node.js stream in the edge runtime",
"1058": "toNodeDebugChannel can only be used in Node.js environments, this is a bug in the Next.js codebase",
"1059": "teeNodeReadable is not supported in edge runtime",
"1060": "Node stream tee buffered too much pending data while waiting for drain"
}
......@@ -119,10 +119,18 @@ const bundleTypes = {
* @param {boolean} options.turbo
* @param {keyof typeof bundleTypes} options.bundleType
* @param {boolean} options.experimental
* @param {boolean} options.nodeStreams
* @param {Partial<webpack.Configuration>} options.rest
* @returns {webpack.Configuration}
*/
module.exports = ({ dev, turbo, bundleType, experimental, ...rest }) => {
module.exports = ({
dev,
turbo,
bundleType,
experimental,
nodeStreams,
...rest
}) => {
const externalHandler = ({ context, request, getResolve }, callback) => {
;(async () => {
if (
......@@ -182,7 +190,7 @@ module.exports = ({ dev, turbo, bundleType, experimental, ...rest }) => {
path: path.join(__dirname, 'dist/compiled/next-server'),
filename: `[name]${turbo ? '-turbo' : ''}${
experimental ? '-experimental' : ''
}.runtime.${dev ? 'dev' : 'prod'}.js`,
}${nodeStreams ? '-nodestreams' : ''}.runtime.${dev ? 'dev' : 'prod'}.js`,
libraryTarget: 'commonjs2',
},
devtool: 'source-map',
......@@ -217,6 +225,15 @@ module.exports = ({ dev, turbo, bundleType, experimental, ...rest }) => {
'process.env.__NEXT_EXPERIMENTAL_REACT': JSON.stringify(
experimental ? true : false
),
// Only define for app bundles where it controls render code paths.
// The server bundle sets this env var at runtime from config.
...(bundleType === 'app'
? {
'process.env.__NEXT_USE_NODE_STREAMS': JSON.stringify(
nodeStreams ? true : false
),
}
: {}),
'process.env.NEXT_RUNTIME': JSON.stringify('nodejs'),
'process.turbopack': JSON.stringify(turbo),
'process.env.TURBOPACK': JSON.stringify(turbo),
......
......@@ -123,6 +123,7 @@ export function getDefineEnv({
const isPPREnabled = checkIsAppPPREnabled(config.experimental.ppr)
const isCacheComponentsEnabled = !!config.cacheComponents
const isUseCacheEnabled = !!config.experimental.useCache
const isUseNodeStreamsEnabled = !!config.experimental.useNodeStreams
const defineEnv: DefineEnv = {
// internal field to identify the plugin config
......@@ -169,6 +170,9 @@ export function getDefineEnv({
'process.env.__NEXT_PPR': isPPREnabled,
'process.env.__NEXT_CACHE_COMPONENTS': isCacheComponentsEnabled,
'process.env.__NEXT_USE_CACHE': isUseCacheEnabled,
'process.env.__NEXT_USE_NODE_STREAMS': isEdgeServer
? false
: isUseNodeStreamsEnabled,
...(config.experimental?.useSkewCookie || !config.deploymentId
? {
......
......@@ -1495,6 +1495,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 = entryBase.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
// that's between the static and dynamic parts so we can compare the
// chunks and add assertions.
......
......@@ -27,3 +27,9 @@ exports.renderToStaticMarkup = error
if (b.resume) {
exports.resume = b.resume
}
if (b.renderToPipeableStream) {
exports.renderToPipeableStream = b.renderToPipeableStream
}
if (b.resumeToPipeableStream) {
exports.resumeToPipeableStream = b.resumeToPipeableStream
}
......@@ -27,3 +27,9 @@ exports.renderToStaticMarkup = error
if (b.resume) {
exports.resume = b.resume
}
if (b.renderToPipeableStream) {
exports.renderToPipeableStream = b.renderToPipeableStream
}
if (b.resumeToPipeableStream) {
exports.resumeToPipeableStream = b.resumeToPipeableStream
}
......@@ -58,6 +58,7 @@ export interface ExportPageInput {
debugOutput?: boolean
nextConfigOutput?: NextConfigComplete['output']
enableExperimentalReact?: boolean
enableNodeStreams?: boolean
sriEnabled: boolean
renderResumeDataCache: RenderResumeDataCache | undefined
}
......
......@@ -80,6 +80,7 @@ async function exportPageImpl(
disableOptimizedLoading,
debugOutput = false,
enableExperimentalReact,
enableNodeStreams,
trailingSlash,
sriEnabled,
renderOpts: commonRenderOpts,
......@@ -92,6 +93,9 @@ async function exportPageImpl(
if (enableExperimentalReact) {
process.env.__NEXT_EXPERIMENTAL_REACT = 'true'
}
if (enableNodeStreams) {
process.env.__NEXT_USE_NODE_STREAMS = 'true'
}
const {
path,
......@@ -421,6 +425,7 @@ export async function exportPages(
httpAgentOptions: nextConfig.httpAgentOptions,
debugOutput: options.debugOutput,
enableExperimentalReact: needsExperimentalReact(nextConfig),
enableNodeStreams: !!nextConfig.experimental.useNodeStreams,
sriEnabled: Boolean(nextConfig.experimental.sri?.algorithm),
buildId: input.buildId,
deploymentId: input.deploymentId,
......
import type { Readable as NodeReadable } from 'node:stream'
import { InvariantError } from '../../shared/lib/invariant-error'
import { createAtomicTimerGroup } from './app-render-scheduling'
import { teeNodeReadable } from './node-stream-tee'
import {
DANGEROUSLY_runPendingImmediatesAfterCurrentTask,
expectNoPendingImmediates,
......@@ -97,24 +99,42 @@ export function prerenderAndAbortInSequentialTasksWithStages<R>(
// 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.
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._runInContext = runInContext ?? ((fn) => fn())
}
tee() {
tee(): ReadableStream<Uint8Array> | NodeReadable {
if (this._stream === null) {
throw new Error(
'Cannot tee a ReactServerResult that has already been consumed'
)
}
const tee = this._stream.tee()
this._stream = tee[0]
return tee[1]
if (this._stream instanceof ReadableStream) {
const tee = this._stream.tee()
this._stream = tee[0]
return tee[1]
} else if (process.env.NEXT_RUNTIME !== 'edge') {
return this._runInContext(() => {
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) {
throw new Error(
'Cannot consume a ReactServerResult that has already been consumed'
......@@ -130,6 +150,10 @@ export type ReactServerPrerenderResolveToType = {
prelude: ReadableStream<Uint8Array>
}
export type ReactServerPrerenderNodeResolveToType = {
prelude: NodeReadable
}
export async function createReactServerPrerenderResult(
underlying: Promise<ReactServerPrerenderResolveToType>
): Promise<ReactServerPrerenderResult> {
......@@ -146,17 +170,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(
underlying: ReadableStream<Uint8Array>
underlying: ReadableStream<Uint8Array> | NodeReadable
): Promise<ReactServerPrerenderResult> {
const chunks: Array<Uint8Array> = []
const reader = underlying.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) {
break
} else {
chunks.push(value)
if (underlying instanceof ReadableStream) {
const reader = underlying.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) {
break
} 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)
......@@ -206,6 +270,53 @@ export class ReactServerPrerenderResult {
const chunks = this.consumeChunks('consumeAsStream()')
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(
......@@ -239,6 +350,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(
unprocessedPrelude: ReadableStream<Uint8Array>
) {
......@@ -252,3 +395,32 @@ export async function processPrelude(
return { prelude, preludeIsEmpty }
}
export async function processNodePrelude(unprocessedPrelude: NodeReadable) {
if (process.env.NEXT_RUNTIME === 'edge') {
throw new Error('processNodePrelude is not supported in edge runtime')
} else {
const [prelude, peek] = teeNodeReadable(unprocessedPrelude)
// Read the first chunk from the peek stream to check if it's empty
const firstChunk = await new Promise<Uint8Array | null>(
(resolve, reject) => {
peek.once('data', (chunk) => {
peek.destroy()
resolve(chunk)
})
peek.once('end', () => {
resolve(null)
})
peek.once('error', (err) => {
if (!prelude.destroyed) prelude.destroy(err)
reject(err)
})
}
)
const preludeIsEmpty = firstChunk === null
return { prelude, preludeIsEmpty }
}
}
export type DebugChannelPair = {
// serverSide is always in the web shape ({ writable: WritableStream }).
// Consumers that need a Node Writable (for renderToPipeableStream) should
// call toNodeDebugChannel() to convert.
serverSide: DebugChannelServer
clientSide: DebugChannelClient
}
// Always the web shape: React's renderToReadableStream reads .writable from it.
// For renderToPipeableStream (Node), use toNodeDebugChannel() to convert.
export type DebugChannelServer = {
readable?: ReadableStream<Uint8Array>
writable: WritableStream<Uint8Array>
......@@ -12,6 +17,43 @@ export type DebugChannelClient = {
writable?: WritableStream<Uint8Array>
}
/**
* Converts a web-shaped debug channel to a Node Writable for use with
* React's renderToPipeableStream, which checks typeof debugChannel.write
* to find its debug destination.
*/
export function toNodeDebugChannel(
webDebugChannel: DebugChannelServer
): import('node:stream').Writable {
// Guard so webpack can DCE node:stream require for edge builds.
if (!process.env.__NEXT_USE_NODE_STREAMS) {
throw new Error(
'toNodeDebugChannel can only be used in Node.js environments, this is a bug in the Next.js codebase'
)
} else {
const { Writable } = require('node:stream') as typeof import('node:stream')
const writer = webDebugChannel.writable.getWriter()
return new Writable({
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)
},
destroy(_err, callback) {
writer.abort(_err).then(
() => callback(_err),
() => callback(_err)
)
},
})
}
}
export function createDebugChannel(): DebugChannelPair | undefined {
if (process.env.NODE_ENV === 'production') {
return undefined
......@@ -19,7 +61,7 @@ export function createDebugChannel(): DebugChannelPair | undefined {
let readableController: ReadableStreamDefaultController | undefined
let clientSideReadable = new ReadableStream<Uint8Array>({
const clientSideReadable = new ReadableStream<Uint8Array>({
start(controller) {
readableController = controller
},
......
......@@ -7,9 +7,47 @@ export {
decodeFormState,
} from 'react-server-dom-webpack/server'
// Use dynamic require to avoid Turbopack failing to resolve this as a named
// export from the vendored CJS module.
/* eslint-disable import/no-extraneous-dependencies */
export const renderToPipeableStream:
| typeof import('react-server-dom-webpack/server.node').renderToPipeableStream
| undefined =
process.env.NEXT_RUNTIME !== 'edge'
? (
require('react-server-dom-webpack/server.node') as typeof import('react-server-dom-webpack/server.node')
).renderToPipeableStream
: undefined
/* eslint-enable import/no-extraneous-dependencies */
// eslint-disable-next-line import/no-extraneous-dependencies
export { prerender } from 'react-server-dom-webpack/static'
// Use dynamic require to avoid Turbopack failing to resolve this as a named
// export from the vendored CJS module.
/* eslint-disable import/no-extraneous-dependencies */
export const prerenderToNodeStream:
| typeof import('react-server-dom-webpack/static').prerenderToNodeStream
| undefined =
process.env.NEXT_RUNTIME !== 'edge'
? (
require('react-server-dom-webpack/static') as typeof import('react-server-dom-webpack/static')
).prerenderToNodeStream
: undefined
/* eslint-enable import/no-extraneous-dependencies */
// chainNodeStreams is used by the app-page.ts build template for PPR resume.
// We export it from here (pre-compiled runtime bundle) because the template
// cannot use relative requires (webpack resolves them from the project root).
export const chainNodeStreams:
| typeof import('../stream-utils/node-stream-helpers').chainNodeStreams
| undefined =
process.env.NEXT_RUNTIME !== 'edge'
? (
require('../stream-utils/node-stream-helpers') as typeof import('../stream-utils/node-stream-helpers')
).chainNodeStreams
: undefined
// TODO: Just re-export `* as ReactServer`
export { captureOwnerStack, createElement, Fragment } from 'react'
......
import type { Readable } from 'node:stream'
import { RSC_CONTENT_TYPE_HEADER } from '../../client/components/app-router-headers'
import RenderResult, { type RenderResultMetadata } from '../render-result'
......@@ -6,7 +7,7 @@ import RenderResult, { type RenderResultMetadata } from '../render-result'
*/
export class FlightRenderResult extends RenderResult {
constructor(
response: string | ReadableStream<Uint8Array>,
response: string | ReadableStream<Uint8Array> | Readable,
metadata: RenderResultMetadata = {},
waitUntil?: Promise<unknown>
) {
......
......@@ -39,8 +39,7 @@ import { createDebugChannel } from '../debug-channel-server'
// eslint-disable-next-line import/no-extraneous-dependencies
import { createFromNodeStream } from 'react-server-dom-webpack/client'
// eslint-disable-next-line import/no-extraneous-dependencies
import { renderToReadableStream } from 'react-server-dom-webpack/server'
// renderToReadableStream is now lazy-loaded conditionally below
const filterStackFrame =
process.env.NODE_ENV !== 'production'
......@@ -446,40 +445,92 @@ export async function collectStagedSegmentData(
? createDebugChannel()
: undefined
const segmentStream = renderToReadableStream(
segmentData,
clientReferenceManifest.clientModules,
{
filterStackFrame,
debugChannel: segmentDebugChannel?.serverSide,
environmentName,
startTime,
onError(error: unknown) {
const digest = getDigestForWellKnownError(error)
if (digest) {
return digest
}
// We don't need to log the errors because we would have already done that
// when generating the original Flight stream for the whole page.
if (
process.env.NEXT_DEBUG_BUILD ||
process.env.__NEXT_VERBOSE_LOGGING
) {
const workStore = workAsyncStorage.getStore()
printDebugThrownValueForProspectiveRender(
error,
workStore?.route ?? 'unknown route',
Phase.InstantValidation
)
}
},
}
)
let segmentStream:
| ReadableStream<Uint8Array>
| import('node:stream').Readable
let streamIterator: AsyncIterable<Uint8Array>
if (process.env.__NEXT_USE_NODE_STREAMS) {
const { renderToFlightPipeableStream } =
require('../pipeable-stream-wrappers') as typeof import('../pipeable-stream-wrappers')
const ComponentMod =
// eslint-disable-next-line import/no-extraneous-dependencies
require('react-server-dom-webpack/server.node') as typeof import('react-server-dom-webpack/server.node')
segmentStream = renderToFlightPipeableStream(
ComponentMod.renderToPipeableStream,
segmentData,
clientReferenceManifest.clientModules,
{
filterStackFrame,
debugChannel: segmentDebugChannel?.serverSide,
environmentName,
startTime,
onError(error: unknown) {
const digest = getDigestForWellKnownError(error)
if (digest) {
return digest
}
// We don't need to log the errors because we would have already done that
// when generating the original Flight stream for the whole page.
if (
process.env.NEXT_DEBUG_BUILD ||
process.env.__NEXT_VERBOSE_LOGGING
) {
const workStore = workAsyncStorage.getStore()
printDebugThrownValueForProspectiveRender(
error,
workStore?.route ?? 'unknown route',
Phase.InstantValidation
)
}
},
}
)
streamIterator = segmentStream
} else {
const { renderToReadableStream } =
// eslint-disable-next-line import/no-extraneous-dependencies
require('react-server-dom-webpack/server') as typeof import('react-server-dom-webpack/server')
segmentStream = renderToReadableStream(
segmentData,
clientReferenceManifest.clientModules,
{
filterStackFrame,
debugChannel: segmentDebugChannel?.serverSide,
environmentName,
startTime,
onError(error: unknown) {
const digest = getDigestForWellKnownError(error)
if (digest) {
return digest
}
// We don't need to log the errors because we would have already done that
// when generating the original Flight stream for the whole page.
if (
process.env.NEXT_DEBUG_BUILD ||
process.env.__NEXT_VERBOSE_LOGGING
) {
const workStore = workAsyncStorage.getStore()
printDebugThrownValueForProspectiveRender(
error,
workStore?.route ?? 'unknown route',
Phase.InstantValidation
)
}
},
}
)
streamIterator = (
segmentStream as ReadableStream<Uint8Array>
).values()
}
await Promise.all([
// accumulate Flight chunks
(async () => {
for await (const chunk of segmentStream.values()) {
for await (const chunk of streamIterator) {
writeChunk(
segmentCacheItem.chunks,
controller.currentStage,
......@@ -671,39 +722,86 @@ export async function createCombinedPayloadStream(
await scheduleInSequentialTasks(
() => {
const stream = renderToReadableStream(
payload,
clientReferenceManifest.clientModules,
{
filterStackFrame,
debugChannel: debugChannel?.serverSide,
startTime,
onError(error: unknown) {
const digest = getDigestForWellKnownError(error)
if (digest) {
return digest
}
// We don't need to log the errors because we would have already done that
// when generating the original Flight stream for the whole page.
if (
process.env.NEXT_DEBUG_BUILD ||
process.env.__NEXT_VERBOSE_LOGGING
) {
const workStore = workAsyncStorage.getStore()
printDebugThrownValueForProspectiveRender(
error,
workStore?.route ?? 'unknown route',
Phase.InstantValidation
)
}
},
}
)
let stream: ReadableStream<Uint8Array> | import('node:stream').Readable
let streamIterator: AsyncIterable<Uint8Array>
if (process.env.__NEXT_USE_NODE_STREAMS) {
const { renderToFlightPipeableStream } =
require('../pipeable-stream-wrappers') as typeof import('../pipeable-stream-wrappers')
const ComponentMod =
// eslint-disable-next-line import/no-extraneous-dependencies
require('react-server-dom-webpack/server.node') as typeof import('react-server-dom-webpack/server.node')
stream = renderToFlightPipeableStream(
ComponentMod.renderToPipeableStream,
payload,
clientReferenceManifest.clientModules,
{
filterStackFrame,
debugChannel: debugChannel?.serverSide,
startTime,
onError(error: unknown) {
const digest = getDigestForWellKnownError(error)
if (digest) {
return digest
}
// We don't need to log the errors because we would have already done that
// when generating the original Flight stream for the whole page.
if (
process.env.NEXT_DEBUG_BUILD ||
process.env.__NEXT_VERBOSE_LOGGING
) {
const workStore = workAsyncStorage.getStore()
printDebugThrownValueForProspectiveRender(
error,
workStore?.route ?? 'unknown route',
Phase.InstantValidation
)
}
},
}
)
streamIterator = stream
} else {
const { renderToReadableStream } =
// eslint-disable-next-line import/no-extraneous-dependencies
require('react-server-dom-webpack/server') as typeof import('react-server-dom-webpack/server')
stream = renderToReadableStream(
payload,
clientReferenceManifest.clientModules,
{
filterStackFrame,
debugChannel: debugChannel?.serverSide,
startTime,
onError(error: unknown) {
const digest = getDigestForWellKnownError(error)
if (digest) {
return digest
}
// We don't need to log the errors because we would have already done that
// when generating the original Flight stream for the whole page.
if (
process.env.NEXT_DEBUG_BUILD ||
process.env.__NEXT_VERBOSE_LOGGING
) {
const workStore = workAsyncStorage.getStore()
printDebugThrownValueForProspectiveRender(
error,
workStore?.route ?? 'unknown route',
Phase.InstantValidation
)
}
},
}
)
streamIterator = (stream as ReadableStream<Uint8Array>).values()
}
streamFinished = Promise.all([
// Accumulate Flight chunks
(async () => {
for await (const chunk of stream.values()) {
for await (const chunk of streamIterator) {
allChunks.push(chunk)
if (isRenderable) {
renderableChunks.push(chunk)
......
import type { Readable as NodeReadable } from 'node:stream'
const MAX_NODE_TEE_PENDING_BYTES = 8 * 1024 * 1024
type NodeTeeChunk = Buffer | Uint8Array
type NodeTeeState = {
closed: boolean
pending: Array<NodeTeeChunk>
pendingBytes: number
waitingDrain: boolean
stream: import('node:stream').PassThrough
}
function chunkByteLength(chunk: NodeTeeChunk): number {
return Buffer.isBuffer(chunk) ? chunk.length : chunk.byteLength
}
/**
* Tees a Node Readable into two Readables without coupling backpressure
* across branches.
*/
export function teeNodeReadable(
source: NodeReadable,
runInContext: <T>(fn: () => T) => T = (fn) => fn()
): [NodeReadable, NodeReadable] {
if (process.env.NEXT_RUNTIME === 'edge') {
throw new Error('teeNodeReadable is not supported in edge runtime')
} else {
const { PassThrough } =
require('node:stream') as typeof import('node:stream')
const left = new PassThrough()
const right = new PassThrough()
const states: [NodeTeeState, NodeTeeState] = [
{
stream: left,
closed: false,
pending: [],
pendingBytes: 0,
waitingDrain: false,
},
{
stream: right,
closed: false,
pending: [],
pendingBytes: 0,
waitingDrain: false,
},
]
let sourceEnded = false
let failed = false
function totalPendingBytes(): number {
return states[0].pendingBytes + states[1].pendingBytes
}
function maybePauseSource() {
if (
!source.isPaused() &&
totalPendingBytes() >= MAX_NODE_TEE_PENDING_BYTES
) {
source.pause()
}
}
function maybeResumeSource() {
if (
source.isPaused() &&
!sourceEnded &&
totalPendingBytes() < MAX_NODE_TEE_PENDING_BYTES / 2
) {
source.resume()
}
}
function closeBranch(state: NodeTeeState) {
if (state.closed) return
state.closed = true
state.pending.length = 0
state.pendingBytes = 0
state.waitingDrain = false
maybeResumeSource()
if (states[0].closed && states[1].closed) {
cleanup()
if (!source.destroyed) {
source.destroy()
}
}
}
function fail(error: Error) {
if (failed) return
failed = true
cleanup()
for (const state of states) {
if (!state.closed && !state.stream.destroyed) {
state.stream.destroy(error)
}
}
if (!source.destroyed) {
source.destroy(error)
}
}
function maybeEnd() {
if (!sourceEnded || failed) return
for (const state of states) {
if (!state.closed && (state.waitingDrain || state.pending.length > 0)) {
return
}
}
cleanup()
for (const state of states) {
if (!state.closed && !state.stream.destroyed) {
state.stream.end()
}
}
}
function enqueue(state: NodeTeeState, chunk: NodeTeeChunk) {
if (state.closed) return
state.pending.push(chunk)
state.pendingBytes += chunkByteLength(chunk)
if (state.pendingBytes > MAX_NODE_TEE_PENDING_BYTES) {
fail(
new Error(
'Node stream tee buffered too much pending data while waiting for drain'
)
)
return
}
maybePauseSource()
}
function writeOrQueue(state: NodeTeeState, chunk: NodeTeeChunk) {
if (state.closed) return
if (state.waitingDrain || state.pending.length > 0) {
enqueue(state, chunk)
return
}
if (!state.stream.write(chunk)) {
state.waitingDrain = true
}
}
function flushPending(state: NodeTeeState) {
if (state.closed || failed) return
while (!state.waitingDrain && state.pending.length > 0) {
const chunk = state.pending.shift()!
state.pendingBytes -= chunkByteLength(chunk)
if (!state.stream.write(chunk)) {
state.waitingDrain = true
}
}
maybeResumeSource()
maybeEnd()
}
const onSourceData = (chunk: NodeTeeChunk) => {
runInContext(() => {
if (failed) return
writeOrQueue(states[0], chunk)
writeOrQueue(states[1], chunk)
maybePauseSource()
})
}
const onSourceEnd = () => {
runInContext(() => {
sourceEnded = true
maybeEnd()
})
}
const onSourceError = (error: Error) => {
runInContext(() => {
fail(error)
})
}
const onLeftDrain = () => {
runInContext(() => {
states[0].waitingDrain = false
flushPending(states[0])
})
}
const onRightDrain = () => {
runInContext(() => {
states[1].waitingDrain = false
flushPending(states[1])
})
}
const onLeftClose = () => {
runInContext(() => {
closeBranch(states[0])
})
}
const onRightClose = () => {
runInContext(() => {
closeBranch(states[1])
})
}
function cleanup() {
source.off('data', onSourceData)
source.off('end', onSourceEnd)
source.off('error', onSourceError)
states[0].stream.off('drain', onLeftDrain)
states[1].stream.off('drain', onRightDrain)
states[0].stream.off('close', onLeftClose)
states[1].stream.off('close', onRightClose)
}
source.on('data', onSourceData)
source.on('end', onSourceEnd)
source.on('error', onSourceError)
states[0].stream.on('drain', onLeftDrain)
states[1].stream.on('drain', onRightDrain)
states[0].stream.on('close', onLeftClose)
states[1].stream.on('close', onRightClose)
return [left, right]
}
}
import type { Writable, Readable } from 'node:stream'
// Lazy require to avoid webpack trying to resolve node:stream at parse time.
// When __NEXT_USE_NODE_STREAMS is false, DCE removes all call sites so this
// require() is never reached at runtime.
function getNodeStream(): typeof import('node:stream') {
return require('node:stream') as typeof import('node:stream')
}
import type {
RenderToPipeableStreamOptions,
PipeableStream,
} from 'react-dom/server'
import { DetachedPromise } from '../../lib/detached-promise'
import { getTracer } from '../lib/trace/tracer'
import { AppRenderSpan } from '../lib/trace/constants'
export interface FizzPipeableStreamResult {
stream: Readable
allReady: Promise<void>
abort: (reason?: unknown) => void
}
/**
* Wraps react-dom/server renderToPipeableStream in an async/await interface
* that matches the existing renderToReadableStream usage patterns.
*
* The returned promise resolves when the shell is ready (onShellReady),
* equivalent to when renderToReadableStream's promise resolves.
*/
export function renderToFizzPipeableStream(
renderToPipeableStream: (
children: React.ReactElement,
options?: RenderToPipeableStreamOptions
) => PipeableStream,
element: React.ReactElement,
options?: RenderToPipeableStreamOptions
): Promise<FizzPipeableStreamResult> {
return getTracer().trace(AppRenderSpan.renderToReadableStream, async () => {
const shellReady = new DetachedPromise<FizzPipeableStreamResult>()
const allReady = new DetachedPromise<void>()
const { PassThrough } = getNodeStream()
const passthrough = new PassThrough()
const originalOnShellReady = options?.onShellReady
const originalOnAllReady = options?.onAllReady
const originalOnShellError = options?.onShellError
// renderToPipeableStream calls onHeaders with a plain object descriptor
// (e.g. { Link: "..." }), while renderToReadableStream wraps it in a
// Headers instance. We need to do the same wrapping here so that
// callers can use headers.forEach().
const originalOnHeaders = options?.onHeaders
const wrappedOnHeaders = originalOnHeaders
? (headersDescriptor: any) => {
originalOnHeaders(new Headers(headersDescriptor))
}
: undefined
const { pipe, abort } = renderToPipeableStream(element, {
...options,
onHeaders: wrappedOnHeaders as any,
onShellReady() {
pipe(passthrough as unknown as Writable)
originalOnShellReady?.()
shellReady.resolve({
stream: passthrough,
allReady: allReady.promise,
abort,
})
},
onAllReady() {
originalOnAllReady?.()
allReady.resolve()
},
onShellError(error: unknown) {
originalOnShellError?.(error)
shellReady.reject(error)
},
})
return shellReady.promise
})
}
/**
* Wraps react-dom/server resumeToPipeableStream in an async/await interface.
*/
export function resumeToFizzPipeableStream(
resumeToPipeableStreamFn: (
children: React.ReactElement,
postponedState: any,
options?: any
) => Promise<PipeableStream>,
element: React.ReactElement,
postponedState: any,
options?: any
): Promise<FizzPipeableStreamResult> {
return getTracer().trace(AppRenderSpan.renderToReadableStream, async () => {
const allReady = new DetachedPromise<void>()
const { PassThrough } = getNodeStream()
const passthrough = new PassThrough()
const originalOnAllReady = options?.onAllReady
// Same onHeaders wrapping as renderToFizzPipeableStream
const originalOnHeaders = options?.onHeaders
const wrappedOnHeaders = originalOnHeaders
? (headersDescriptor: any) => {
originalOnHeaders(new Headers(headersDescriptor))
}
: undefined
const { pipe, abort } = await resumeToPipeableStreamFn(
element,
postponedState,
{
...options,
onHeaders: wrappedOnHeaders,
onAllReady() {
originalOnAllReady?.()
allReady.resolve()
},
}
)
pipe(passthrough as unknown as Writable)
return {
stream: passthrough,
allReady: allReady.promise,
abort,
}
})
}
/**
* 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.
*/
export function renderToFlightPipeableStream(
renderToPipeableStreamFn: (
model: any,
webpackMap: any,
options?: any
) => PipeableStream,
model: any,
webpackMap: any,
options?: any,
runInContext?: <T>(fn: () => T) => T
): Readable {
const { PassThrough } = getNodeStream()
const passthrough = new PassThrough()
// React's renderToPipeableStream checks typeof debugChannel.write to find
// its debug destination. The web-shaped { writable: WritableStream } doesn't
// have .write() on the object, so convert to a Node Writable.
if (options?.debugChannel) {
const { toNodeDebugChannel } =
require('./debug-channel-server') as typeof import('./debug-channel-server')
options = {
...options,
debugChannel: toNodeDebugChannel(options.debugChannel),
}
}
const run = runInContext ?? ((fn: () => any) => fn())
const { pipe } = run(() =>
renderToPipeableStreamFn(model, webpackMap, options)
)
run(() => pipe(passthrough as unknown as Writable))
return passthrough
}
This diff is collapsed.
import type { BinaryStreamOf } from './app-render'
import type { Readable } from 'node:stream'
import type { Readable, Transform } from 'node:stream'
import { htmlEscapeJsonString } from '../htmlescape'
import { workUnitAsyncStorage } from './work-unit-async-storage.external'
......@@ -45,6 +45,7 @@ export function getFlightStream<T>(
getClientReferenceManifest()
let newResponse: Promise<T>
let waitForDebugEnd: Promise<void> | undefined
if (flightStream instanceof ReadableStream) {
// The types of flightStream and debugStream should match.
if (debugStream && !(debugStream instanceof ReadableStream)) {
......@@ -76,9 +77,27 @@ export function getFlightStream<T>(
const { Readable } =
require('node:stream') as typeof import('node:stream')
// The types of flightStream and debugStream should match.
if (debugStream && !(debugStream instanceof Readable)) {
throw new InvariantError('Expected debug stream to be a Readable')
// Debug channel always creates web ReadableStreams (dev-only).
// Convert to Node.js Readable when using the node stream path.
let nodeDebugStream: import('node:stream').Readable | undefined
if (debugStream) {
if (debugStream instanceof Readable) {
nodeDebugStream = debugStream
} else {
// Cast through any: global ReadableStream and stream/web.ReadableStream
// have a minor type mismatch (Promise<void> vs Promise<undefined>).
nodeDebugStream = Readable.fromWeb(debugStream as any)
}
// With createFromNodeStream + debugChannel, the response can resolve
// before debug stream completion. In restart-on-cache-miss flows this
// can leave metadata in the body instead of the head for the request
// render. Wait for debug completion for request work units.
waitForDebugEnd = new Promise<void>((resolve, reject) => {
nodeDebugStream!.once('end', resolve)
nodeDebugStream!.once('close', resolve)
nodeDebugStream!.once('error', reject)
})
}
// react-server-dom-webpack/client.edge must not be hoisted for require cache clearing to work correctly
......@@ -96,7 +115,7 @@ export function getFlightStream<T>(
{
findSourceMapURL,
nonce,
debugChannel: debugStream,
debugChannel: nodeDebugStream,
endTime: debugEndTime,
}
)
......@@ -126,6 +145,12 @@ export function getFlightStream<T>(
case 'prerender-ppr':
case 'prerender-legacy':
case 'request':
if (waitForDebugEnd) {
newResponse = Promise.all([newResponse, waitForDebugEnd]).then(
([flightResponse]) => flightResponse
)
}
break
case 'cache':
case 'private-cache':
case 'unstable-cache':
......@@ -252,3 +277,109 @@ function writeFlightDataInstruction(
)
)
}
function encodeFlightDataChunk(
scriptStart: string,
chunk: string | Uint8Array
): Uint8Array {
let htmlInlinedData: string
if (typeof chunk === 'string') {
htmlInlinedData = htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_DATA, chunk])
)
} else {
const base64 = btoa(String.fromCodePoint(...chunk))
htmlInlinedData = htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_BINARY, base64])
)
}
return encoder.encode(
`${scriptStart}self.__next_f.push(${htmlInlinedData})</script>`
)
}
/**
* Creates a Node.js Readable that provides inline script tag chunks for writing
* hydration data to the client outside the React render itself.
*
* This is the Node.js stream equivalent of createInlinedDataReadableStream.
*/
export function createInlinedDataNodeStream(
nonce: string | undefined,
formState: unknown | null
): Transform {
if (process.env.NEXT_RUNTIME === 'edge') {
throw new Error(
'createInlinedDataNodeStream is not supported in edge runtime'
)
} else {
const startScriptTag = nonce
? `<script nonce=${JSON.stringify(nonce)}>`
: '<script>'
const decoder = new TextDecoder('utf-8', { fatal: true })
let bootstrapWritten = false
const { Transform: NodeTransform } =
require('node:stream') as typeof import('node:stream')
return new NodeTransform({
transform(
chunk: Uint8Array,
_encoding: string,
callback: (error?: Error) => void
) {
try {
if (!bootstrapWritten) {
bootstrapWritten = true
let scriptContents = `(self.__next_f=self.__next_f||[]).push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_BOOTSTRAP])
)})`
if (formState != null) {
scriptContents += `;self.__next_f.push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_FORM_STATE, formState])
)})`
}
this.push(
encoder.encode(`${startScriptTag}${scriptContents}</script>`)
)
}
try {
const decodedString = decoder.decode(chunk, { stream: true })
this.push(encodeFlightDataChunk(startScriptTag, decodedString))
} catch {
this.push(encodeFlightDataChunk(startScriptTag, chunk))
}
callback()
} catch (error) {
callback(error as Error)
}
},
flush(callback: (error?: Error) => void) {
// If no data was ever received, still write the bootstrap
if (!bootstrapWritten) {
let scriptContents = `(self.__next_f=self.__next_f||[]).push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_BOOTSTRAP])
)})`
if (formState != null) {
scriptContents += `;self.__next_f.push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_FORM_STATE, formState])
)})`
}
this.push(
encoder.encode(`${startScriptTag}${scriptContents}</script>`)
)
}
callback()
},
})
}
}
......@@ -349,6 +349,7 @@ export const experimentalSchema = {
serverComponentsHmrCache: z.boolean().optional(),
authInterrupts: z.boolean().optional(),
useCache: z.boolean().optional(),
useNodeStreams: z.boolean().optional(),
slowModuleDetection: z
.object({
buildTimeThresholdMs: z.number().int(),
......
......@@ -810,6 +810,13 @@ export interface ExperimentalConfig {
*/
useCache?: boolean
/**
* Use Node.js native streams instead of web streams for the App Router
* rendering pipeline on the Node.js runtime. This can improve performance
* by avoiding the overhead of web stream wrappers.
*/
useNodeStreams?: boolean
/**
* Enables detection and reporting of slow modules during development builds.
* Enabling this may impact build performance to ensure accurate measurements.
......@@ -1784,6 +1791,7 @@ export interface NextConfigRuntime {
| 'maxPostponedStateSize'
| 'devCacheControlNoCache'
| 'exposeTestingApiInProductionBuild'
| 'useNodeStreams'
> & {
// Pick on @internal fields generates invalid .d.ts files
/** @internal */
......@@ -1848,6 +1856,7 @@ export function getNextConfigRuntime(
maxPostponedStateSize: ex.maxPostponedStateSize,
devCacheControlNoCache: ex.devCacheControlNoCache,
exposeTestingApiInProductionBuild: ex.exposeTestingApiInProductionBuild,
useNodeStreams: ex.useNodeStreams,
trustHostHeader: ex.trustHostHeader,
isExperimentalCompile: ex.isExperimentalCompile,
......
......@@ -1906,6 +1906,15 @@ function enforceExperimentalFeatures(
config.cacheComponents = true
}
// Enable node streams via env var (for CI testing).
if (
process.env.__NEXT_USE_NODE_STREAMS === 'true' &&
(config.experimental.useNodeStreams === undefined ||
(isDefaultConfig && !config.experimental.useNodeStreams))
) {
config.experimental.useNodeStreams = true
}
// TODO: Remove this once using the debug channel is the default.
if (
process.env.__NEXT_EXPERIMENTAL_DEBUG_CHANNEL === 'true' &&
......
......@@ -216,6 +216,9 @@ export default class NextNodeServer extends BaseServer<
if (this.renderOpts.nextScriptWorkers) {
process.env.__NEXT_SCRIPT_WORKERS = JSON.stringify(true)
}
if (this.nextConfig.experimental.useNodeStreams) {
process.env.__NEXT_USE_NODE_STREAMS = 'true'
}
if (!this.minimalMode) {
this.imageResponseCache = new ResponseCache(this.minimalMode)
......
......@@ -144,3 +144,140 @@ export async function pipeToNodeResponse(
throw new Error('failed to pipe response', { cause: err })
}
}
/**
* Pipes a Node.js Readable stream directly to a ServerResponse.
* This avoids the overhead of wrapping in web WritableStream.
*/
export async function pipeNodeReadableToResponse(
readable: import('node:stream').Readable,
res: ServerResponse,
waitUntilForEnd?: Promise<unknown>
) {
// Guard so webpack can DCE node:stream require for edge builds.
// pipe-readable.ts is imported by render-result.ts which is bundled into edge.
if (process.env.NEXT_RUNTIME !== 'edge') {
try {
const { errored, destroyed } = res
if (errored || destroyed) return
let started = false
const finished = new DetachedPromise<void>()
const { Writable: NodeWritable } =
require('node:stream') as typeof import('node:stream')
const writable = new NodeWritable({
write(chunk: Uint8Array, _encoding, callback) {
if (!started) {
started = true
if (
'performance' in globalThis &&
process.env.NEXT_OTEL_PERFORMANCE_PREFIX
) {
const metrics = getClientComponentLoaderMetrics()
if (metrics) {
performance.measure(
`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-client-component-loading`,
{
start: metrics.clientComponentLoadStart,
end:
metrics.clientComponentLoadStart +
metrics.clientComponentLoadTimes,
}
)
}
}
res.flushHeaders()
getTracer().trace(
NextNodeServerSpan.startResponse,
{
spanName: 'start response',
},
() => undefined
)
}
try {
const ok = res.write(chunk)
// Added by the `compression` middleware, this is a function that will
// flush the partially-compressed response to the client.
if ('flush' in res && typeof res.flush === 'function') {
res.flush()
}
if (!ok) {
res.once('drain', callback)
} else {
callback()
}
} catch (err) {
if (!res.writableFinished) {
res.end()
}
// Destroy the readable to stop it from pushing more data
if (!readable.destroyed) {
readable.destroy()
}
callback(
new Error('failed to write chunk to response', { cause: err })
)
}
},
final(callback) {
if (waitUntilForEnd) {
const finalize = () => {
if (!res.writableFinished) {
res.end()
}
callback()
finished.resolve()
}
waitUntilForEnd.then(finalize, finalize)
} else {
if (!res.writableFinished) {
res.end()
}
callback()
finished.resolve()
}
},
destroy(err, callback) {
if (err && !res.writableFinished) {
res.destroy(err)
}
callback(err)
finished.resolve()
},
})
// Handle client disconnect
const onClose = () => {
if (!readable.destroyed) {
readable.destroy()
}
}
res.once('close', onClose)
// Forward errors since Node.js .pipe() does not propagate them.
readable.on('error', (err) => {
if (!writable.destroyed) {
writable.destroy(err)
}
})
readable.pipe(writable)
await finished.promise
res.off('close', onClose)
} catch (err: any) {
if (isAbortError(err)) return
throw new Error('failed to pipe node readable to response', {
cause: err,
})
}
}
}
import type { OutgoingHttpHeaders, ServerResponse } from 'http'
import type { Readable as NodeReadable } from 'node:stream'
import type { CacheControl } from './lib/cache-control'
import type { FetchMetrics } from './base-http'
......@@ -73,6 +74,7 @@ export type RenderResultMetadata = AppPageRenderResultMetadata &
export type RenderResultResponse =
| ReadableStream<Uint8Array>[]
| ReadableStream<Uint8Array>
| NodeReadable
| string
| Buffer
| null
......@@ -173,6 +175,19 @@ export default class RenderResult<
* @param stream Whether or not to return a promise if the response is dynamic
* @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: true): Promise<string>
public toUnchunkedString(stream = false): Promise<string> | string {
......@@ -189,6 +204,15 @@ export default class RenderResult<
)
}
if (
process.env.NEXT_RUNTIME !== 'edge' &&
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)
}
......@@ -196,7 +220,8 @@ export default class RenderResult<
}
/**
* 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> {
if (this.response === null) {
......@@ -217,9 +242,16 @@ export default class RenderResult<
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)) {
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
......@@ -241,11 +273,15 @@ export default class RenderResult<
if (typeof this.response === 'string') {
return [streamFromString(this.response)]
} else if (Array.isArray(this.response)) {
return this.response
return this.response as ReadableStream<Uint8Array>[]
} else if (Buffer.isBuffer(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 {
return [this.response]
return [this.response as ReadableStream<Uint8Array>]
}
}
......@@ -329,6 +365,44 @@ export default class RenderResult<
* @param res
*/
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_RUNTIME !== 'edge' &&
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)
}
/**
* 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_RUNTIME !== 'edge') {
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
)
}
}
}
if (process.env.NEXT_RUNTIME === 'edge') {
module.exports = require('next/dist/server/route-modules/app-page/module.js')
} else {
// Keep this as explicit nested conditionals with branch-local `require()` calls.
// Collapsing this into a map/object with eager requires pulls all runtime
// variants into the module graph and breaks env-based dead-code elimination.
if (process.env.__NEXT_EXPERIMENTAL_REACT) {
if (process.env.__NEXT_USE_NODE_STREAMS) {
if (process.env.NODE_ENV === 'development') {
if (process.env.TURBOPACK) {
module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental-nodestreams.runtime.dev.js')
} else {
module.exports = require('next/dist/compiled/next-server/app-page-experimental-nodestreams.runtime.dev.js')
}
} else {
if (process.env.TURBOPACK) {
module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental-nodestreams.runtime.prod.js')
} else {
module.exports = require('next/dist/compiled/next-server/app-page-experimental-nodestreams.runtime.prod.js')
}
}
} else {
if (process.env.NODE_ENV === 'development') {
if (process.env.TURBOPACK) {
module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental.runtime.dev.js')
} else {
module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.dev.js')
}
} else {
if (process.env.TURBOPACK) {
module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental.runtime.prod.js')
} else {
module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.prod.js')
}
}
}
} else if (process.env.__NEXT_USE_NODE_STREAMS) {
if (process.env.NODE_ENV === 'development') {
if (process.env.TURBOPACK) {
module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental.runtime.dev.js')
module.exports = require('next/dist/compiled/next-server/app-page-turbo-nodestreams.runtime.dev.js')
} else {
module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.dev.js')
module.exports = require('next/dist/compiled/next-server/app-page-nodestreams.runtime.dev.js')
}
} else {
if (process.env.TURBOPACK) {
module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental.runtime.prod.js')
module.exports = require('next/dist/compiled/next-server/app-page-turbo-nodestreams.runtime.prod.js')
} else {
module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.prod.js')
module.exports = require('next/dist/compiled/next-server/app-page-nodestreams.runtime.prod.js')
}
}
} else {
......
This diff is collapsed.
This diff is collapsed.
......@@ -194,7 +194,9 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/compiled/is-wsl/index.js",
"/node_modules/next/dist/compiled/jsonwebtoken/index.js",
"/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-nodestreams.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/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