Commit fe044c9f authored by Jimmy Lai's avatar Jimmy Lai

fix: preserve request async storage in node flight streams

parent 164ccd1c
......@@ -99,9 +99,14 @@ export function prerenderAndAbortInSequentialTasksWithStages<R>(
// in one that doesn't close even when the underlying is complete.
export class ReactServerResult {
private _stream: null | ReadableStream<Uint8Array> | NodeReadable
private readonly _runInContext: <T>(fn: () => T) => T
constructor(stream: ReadableStream<Uint8Array> | NodeReadable) {
constructor(
stream: ReadableStream<Uint8Array> | NodeReadable,
runInContext?: <T>(fn: () => T) => T
) {
this._stream = stream
this._runInContext = runInContext ?? ((fn) => fn())
}
tee(): ReadableStream<Uint8Array> | NodeReadable {
......@@ -121,25 +126,33 @@ export class ReactServerResult {
// buffer fills (e.g. flight data not pulling yet due to
// delayDataUntilFirstHtmlChunk), pipe pauses the source, starving
// the other side (SSR), causing a deadlock.
const { PassThrough } =
require('node:stream') as typeof import('node:stream')
const pt1 = new PassThrough()
const pt2 = new PassThrough()
const source = this._stream
source.on('data', (chunk: Buffer | Uint8Array) => {
pt1.write(chunk)
pt2.write(chunk)
})
source.on('end', () => {
pt1.end()
pt2.end()
})
source.on('error', (err) => {
if (!pt1.destroyed) pt1.destroy(err)
if (!pt2.destroyed) pt2.destroy(err)
return this._runInContext(() => {
const { PassThrough } =
require('node:stream') as typeof import('node:stream')
const pt1 = new PassThrough()
const pt2 = new PassThrough()
const source = this._stream as NodeReadable
source.on('data', (chunk: Buffer | Uint8Array) => {
this._runInContext(() => {
pt1.write(chunk)
pt2.write(chunk)
})
})
source.on('end', () => {
this._runInContext(() => {
pt1.end()
pt2.end()
})
})
source.on('error', (err) => {
this._runInContext(() => {
if (!pt1.destroyed) pt1.destroy(err)
if (!pt2.destroyed) pt2.destroy(err)
})
})
this._stream = pt1
return pt2
})
this._stream = pt1
return pt2
} else {
throw new Error('Cannot tee a Node.js stream in the edge runtime')
}
......
......@@ -703,7 +703,7 @@ async function generateDynamicFlightRenderResult(
options
)
if (process.env.NEXT_RUNTIME !== 'edge') {
if (process.env.__NEXT_USE_NODE_STREAMS) {
const { renderToPipeableStream: flightRenderToPipeableStream } =
ctx.componentMod as any
if (flightRenderToPipeableStream) {
......@@ -723,7 +723,8 @@ async function generateDynamicFlightRenderResult(
temporaryReferences: options?.temporaryReferences,
filterStackFrame,
debugChannel: debugChannel?.serverSide,
}
},
(fn) => workUnitAsyncStorage.run(requestStore, fn)
)
return new FlightRenderResult(
......@@ -829,7 +830,8 @@ async function stagedRenderToReadableStreamWithoutCachesInDev(
{
...options,
environmentName,
}
},
(fn) => workUnitAsyncStorage.run(requestStore, fn)
)
},
() => {
......@@ -2992,7 +2994,9 @@ async function renderToStream(
validationDebugChannelClient
)
reactServerResult = new ReactServerResult(serverStream)
reactServerResult = new ReactServerResult(serverStream, (fn) =>
workUnitAsyncStorage.run(finalRequestStore, fn)
)
requestStore = finalRequestStore
debugChannel = returnedDebugChannel
} else {
......@@ -3012,7 +3016,9 @@ async function renderToStream(
debugChannel: debugChannel?.serverSide,
}
)
reactServerResult = new ReactServerResult(serverStream)
reactServerResult = new ReactServerResult(serverStream, (fn) =>
workUnitAsyncStorage.run(requestStore, fn)
)
}
if (debugChannel && setReactDebugChannel) {
......@@ -3073,8 +3079,10 @@ async function renderToStream(
filterStackFrame,
onError: serverComponentsErrorHandler,
debugChannel: debugChannel?.serverSide,
}
)
},
(fn) => workUnitAsyncStorage.run(requestStore, fn)
),
(fn) => workUnitAsyncStorage.run(requestStore, fn)
)
} else {
reactServerResult = new ReactServerResult(
......@@ -3088,7 +3096,8 @@ async function renderToStream(
onError: serverComponentsErrorHandler,
debugChannel: debugChannel?.serverSide,
}
)
),
(fn) => workUnitAsyncStorage.run(requestStore, fn)
)
}
} else {
......@@ -3103,7 +3112,8 @@ async function renderToStream(
onError: serverComponentsErrorHandler,
debugChannel: debugChannel?.serverSide,
}
)
),
(fn) => workUnitAsyncStorage.run(requestStore, fn)
)
}
}
......@@ -3498,7 +3508,8 @@ async function renderToStream(
{
filterStackFrame,
onError: serverComponentsErrorHandler,
}
},
(fn) => workUnitAsyncStorage.run(requestStore, fn)
)
} else {
errorServerStream = workUnitAsyncStorage.run(
......@@ -3801,7 +3812,8 @@ async function renderWithRestartOnCacheMissInDev(
filterStackFrame,
debugChannel: debugChannel?.serverSide,
signal: initialReactController.signal,
}
},
(fn) => workUnitAsyncStorage.run(requestStore, fn)
)
initialReactController.signal.addEventListener('abort', () => {
initialDataController.abort(initialReactController.signal.reason)
......@@ -4009,7 +4021,8 @@ async function renderWithRestartOnCacheMissInDev(
startTime,
filterStackFrame,
debugChannel: debugChannel?.serverSide,
}
},
(fn) => workUnitAsyncStorage.run(requestStore, fn)
)
// Node stream tee: forward data to two PassThrough streams.
......
......@@ -145,7 +145,8 @@ export function renderToFlightPipeableStream(
) => PipeableStream,
model: any,
webpackMap: any,
options?: any
options?: any,
runInContext?: <T>(fn: () => T) => T
): Readable {
const { PassThrough } = getNodeStream()
const passthrough = new PassThrough()
......@@ -162,7 +163,10 @@ export function renderToFlightPipeableStream(
}
}
const { pipe } = renderToPipeableStreamFn(model, webpackMap, options)
pipe(passthrough as unknown as Writable)
const run = runInContext ?? ((fn: () => any) => fn())
const { pipe } = run(() =>
renderToPipeableStreamFn(model, webpackMap, options)
)
run(() => pipe(passthrough as unknown as Writable))
return passthrough
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment