Unverified Commit faff9949 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Fix request context in streaming Route Handler callbacks

Co-authored-by: 's avatarAndrew Imm <494043+andrewimm@users.noreply.github.com>
parent 1b689957
...@@ -90,6 +90,7 @@ import { executeRevalidates } from '../../revalidation-utils' ...@@ -90,6 +90,7 @@ import { executeRevalidates } from '../../revalidation-utils'
import { trackPendingModules } from '../../app-render/module-loading/track-module-loading.external' import { trackPendingModules } from '../../app-render/module-loading/track-module-loading.external'
import { InvariantError } from '../../../shared/lib/invariant-error' import { InvariantError } from '../../../shared/lib/invariant-error'
import { LazyModule } from '../../lib/lazy-module' import { LazyModule } from '../../lib/lazy-module'
import { bindReadableStream } from '../../stream-utils/bind-readable-stream'
import { createPrerenderResumeDataCache } from '../../resume-data-cache/resume-data-cache' import { createPrerenderResumeDataCache } from '../../resume-data-cache/resume-data-cache'
import { import {
createRouteHandlerRequestInUseCacheError, createRouteHandlerRequestInUseCacheError,
...@@ -838,7 +839,7 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -838,7 +839,7 @@ export class AppRouteRouteModule extends RouteModule<
) )
} }
return this.finalizeResponse( const response = this.finalizeResponse(
res, res,
request, request,
workStore, workStore,
...@@ -846,6 +847,17 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -846,6 +847,17 @@ export class AppRouteRouteModule extends RouteModule<
null, null,
context context
) )
if (!response.body) return response
// The response is consumed outside the request scope. In particular, Web
// Streams can resume a backpressured upstream transform in the reader's
// context, so bind consumption here while the request stores are active.
return new Response(bindReadableStream(response.body), {
status: response.status,
statusText: response.statusText,
headers: response.headers,
})
} }
private async prepareExecution( private async prepareExecution(
......
import { bindSnapshot } from '../app-render/async-local-storage'
/** Consume a stream in the async context in which this adapter is created. */
export function bindReadableStream<T>(
stream: ReadableStream<T>
): ReadableStream<T> {
const reader = stream.getReader()
let cancelled = false
return new ReadableStream<T>(
{
pull: bindSnapshot(async (controller) => {
try {
const { done, value } = await reader.read()
// Cancellation also settles an outstanding read. Its controller has
// already closed, and cancel() will release the reader.
if (cancelled) return
if (done) {
reader.releaseLock()
controller.close()
} else {
controller.enqueue(value)
}
} catch (error) {
reader.releaseLock()
controller.error(error)
}
}),
cancel: bindSnapshot(async (reason) => {
cancelled = true
try {
await reader.cancel(reason)
} finally {
reader.releaseLock()
}
}),
},
// Do not start reading until the downstream consumer requests a chunk.
{ highWaterMark: 0 }
)
}
import { streamingRoute } from '../../../utils/streaming-route'
export const runtime = 'edge'
export const dynamic = 'force-dynamic'
export const GET = streamingRoute
import { streamingRoute } from '../../../utils/streaming-route'
export const runtime = 'nodejs'
export const dynamic = 'force-dynamic'
export const GET = streamingRoute
...@@ -55,11 +55,101 @@ describe.each(runtimes)('after() in %s runtime', (runtimeValue) => { ...@@ -55,11 +55,101 @@ describe.each(runtimes)('after() in %s runtime', (runtimeValue) => {
it('runs in dynamic route handlers', async () => { it('runs in dynamic route handlers', async () => {
const res = await next.fetch(pathPrefix + '/route') const res = await next.fetch(pathPrefix + '/route')
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(await res.json()).toEqual({ data: { message: 'Hello, world!' } })
await retry(() => { await retry(() => {
expect(getLogs()).toContainEqual({ source: '[route handler] /route' }) expect(getLogs()).toContainEqual({ source: '[route handler] /route' })
}) })
}) })
it('runs in streaming route handlers with backpressure and isolated requests', async () => {
const ids = ['stream-a', 'stream-b']
const responses = await Promise.all(
ids.map((id) =>
next.fetch(pathPrefix + '/streaming', {
headers: { 'x-stream-id': id, cookie: `stream-id=${id}` },
})
)
)
const streamingLogs = () =>
getLogs().filter((log) => log.source === '[route handler] /streaming')
const release = (id: string) =>
next.fetch(pathPrefix + '/streaming?release=' + id)
try {
for (const [index, response] of responses.entries()) {
expect(response.status).toBe(201)
expect(response.statusText).toBe('Streamed')
expect(response.headers.get('content-type')).toBe('text/plain')
expect(response.headers.get('x-stream-id')).toBe(ids[index])
expect(response.headers.get('set-cookie')).toContain(
'from-response=explicit'
)
expect(response.headers.get('set-cookie')).toContain(
`from-handler=${ids[index]}`
)
}
// Both responses are still open, so none of their after tasks may run.
expect(streamingLogs()).toEqual([])
// Finish B while A is still open, then finish A. Each callback must use
// its own request's stores, rather than those of the downstream reader.
for (const index of [1, 0]) {
const id = ids[index]
await release(id)
expect(await responses[index].text()).toBe('first\nsecond\nthird\n')
await retry(() => {
expect(streamingLogs().filter((log) => log.header === id)).toEqual(
['first', 'second', 'third', 'flush'].map((chunk) => ({
source: '[route handler] /streaming',
chunk,
header: id,
cookie: id,
}))
)
})
if (index === 1) {
expect(streamingLogs().some((log) => log.header === ids[0])).toBe(
false
)
}
}
expect(streamingLogs()).toHaveLength(8)
} finally {
await Promise.all(ids.map(release))
await Promise.allSettled(responses.map((response) => response.text()))
}
})
it.each([
{ method: 'GET', status: 200 },
{ method: 'GET', status: 204 },
{ method: 'GET', status: 304 },
{ method: 'HEAD', status: 200 },
])(
'preserves bodyless route handler responses ($method $status)',
async ({ method, status }) => {
const response = await next.fetch(
pathPrefix + '/streaming?status=' + status,
{ method }
)
expect(response.status).toBe(status)
expect(await response.text()).toBe('')
expect(response.headers.get('set-cookie')).toContain(
'from-response=explicit'
)
expect(response.headers.get('set-cookie')).toContain(
'from-handler=bodyless'
)
await retry(() => {
expect(getLogs()).toContainEqual({
source: '[route handler] /streaming-bodyless',
status,
method,
})
})
}
)
it('runs in server actions', async () => { it('runs in server actions', async () => {
const browser = await next.browser(pathPrefix + '/123/with-action') const browser = await next.browser(pathPrefix + '/123/with-action')
expect(getLogs()).toContainEqual({ expect(getLogs()).toContainEqual({
......
import { cookies, headers } from 'next/headers'
import { after } from 'next/server'
import { cliLog } from './log'
const pendingStreams = new Map()
export async function streamingRoute(request) {
const { searchParams } = new URL(request.url)
const releaseId = searchParams.get('release')
if (releaseId) {
pendingStreams.get(releaseId)?.()
return new Response('released')
}
const id = request.headers.get('x-stream-id') || 'bodyless'
const cookieStore = await cookies()
cookieStore.set('from-handler', id)
if (searchParams.has('status') || request.method === 'HEAD') {
const status = Number(searchParams.get('status') || 200)
after(() => {
cliLog({
source: '[route handler] /streaming-bodyless',
status,
method: request.method,
})
})
const body =
request.method === 'HEAD'
? new ReadableStream(
{
pull(controller) {
controller.enqueue(new TextEncoder().encode('HEAD body'))
controller.close()
},
},
{ highWaterMark: 0 }
)
: null
return new Response(body, {
status,
headers: { 'set-cookie': 'from-response=explicit; Path=/' },
})
}
const released = new Promise((resolve) => pendingStreams.set(id, resolve))
const encoder = new TextEncoder()
const registerAfter = (chunk) => {
after(async () => {
cliLog({
source: '[route handler] /streaming',
chunk,
header: (await headers()).get('x-stream-id'),
cookie: (await cookies()).get('stream-id')?.value,
})
})
}
const upstream = new TransformStream({
transform(chunk, controller) {
registerAfter(chunk)
controller.enqueue(encoder.encode(chunk + '\n'))
},
flush() {
registerAfter('flush')
},
})
const body = upstream.readable.pipeThrough(new TransformStream())
const writer = upstream.writable.getWriter()
// Settle start, then let one write reach the second transform. That transform
// cannot finish its write until the response body has a reader. The next
// upstream write is therefore parked under backpressure before we return.
await writer.ready
await writer.write('first')
const parkedWrite = writer.write('second')
void (async () => {
try {
await parkedWrite
// Keep this response open while another request finishes, so the test can
// check both request isolation and that after() waits for response close.
await released
await writer.write('third')
await writer.close()
} catch (error) {
// Transform errors also propagate through the response body.
console.error(error)
} finally {
pendingStreams.delete(id)
writer.releaseLock()
}
})()
return new Response(body, {
status: 201,
statusText: 'Streamed',
headers: {
'content-type': 'text/plain',
'x-stream-id': id,
'set-cookie': 'from-response=explicit; Path=/',
},
})
}
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