Commit 0f6ea101 authored by Janka Uryga's avatar Janka Uryga

[after] fix waitUntil in edge runtime sandbox (pages/api) (#71309)

Fixes an issue where using `after` in a `runtime = "edge"` page would
throw an error saying that `waitUntil` is missing. This was because we
weren't correctly passing `waitUntil` into the edge runtime sandbox.

(More precisely, the WebServer we run for those app-page/app-route
handlers wasn't receiving `waitUntil` -- middleware goes through a
different codepath and thus was fine)

This PR (ab)uses the `"@next/request-context"` global ALS (intended for
platforms to provide `waitUntil` to Next.js) to fix this -- it works
because the wrapped WebServer will read `"@next/request-context"` and
thus use the correct `waitUntil`.
parent 85a24a84
import { createAsyncLocalStorage } from '../app-render/async-local-storage'
import type { CacheHandler } from '../lib/incremental-cache'
export function getBuiltinRequestContext():
......@@ -29,8 +30,24 @@ export type BuiltinRequestContext = {
get(): BuiltinRequestContextValue | undefined
}
export type RunnableBuiltinRequestContext = BuiltinRequestContext & {
run<T>(value: BuiltinRequestContextValue, callback: () => T): T
}
export type BuiltinRequestContextValue = {
waitUntil?: WaitUntil
NextCacheHandler?: typeof CacheHandler
}
export type WaitUntil = (promise: Promise<any>) => void
/** "@next/request-context" has a different signature from AsyncLocalStorage,
* matching [AsyncContext.Variable](https://github.com/tc39/proposal-async-context).
* We don't need a full AsyncContext adapter here, just having `.get()` is enough
*/
export function createLocalRequestContext(): RunnableBuiltinRequestContext {
const storage = createAsyncLocalStorage<BuiltinRequestContextValue>()
return {
get: () => storage.getStore(),
run: (value, callback) => storage.run(value, callback),
}
}
......@@ -1776,25 +1776,27 @@ export default abstract class Server<
)
}
private getWaitUntil(): WaitUntil | undefined {
protected getWaitUntil(): WaitUntil | undefined {
const builtinRequestContext = getBuiltinRequestContext()
if (builtinRequestContext) {
// the platform provided a request context.
// use the `waitUntil` from there, whether actually present or not --
// if not present, `unstable_after` will error.
return builtinRequestContext.waitUntil
}
if (process.env.__NEXT_TEST_MODE) {
// we're in a test, use a no-op.
return Server.noopWaitUntil
// NOTE: if we're in an edge runtime sandbox, this context will be used to forward the outer waitUntil.
return builtinRequestContext.waitUntil
}
if (this.minimalMode || process.env.NEXT_RUNTIME === 'edge') {
if (this.minimalMode) {
// we're built for a serverless environment, and `waitUntil` is not available,
// but using a noop would likely lead to incorrect behavior,
// because we have no way of keeping the invocation alive.
// return nothing, and `unstable_after` will error if used.
//
// NOTE: for edge functions, `NextWebServer` always runs in minimal mode.
//
// NOTE: if we're in an edge runtime sandbox, waitUntil will be passed in using "@next/request-context",
// so we won't get here.
return undefined
}
......
......@@ -1466,6 +1466,7 @@ export default class NextNodeServer extends BaseServer<
page,
body: getRequestMeta(params.request, 'clonableBody'),
signal: signalFromNodeResponse(params.response.originalResponse),
waitUntil: this.getWaitUntil(),
},
useCache: true,
onWarning: params.onWarning,
......@@ -1769,6 +1770,7 @@ export default class NextNodeServer extends BaseServer<
},
body: getRequestMeta(params.req, 'clonableBody'),
signal: signalFromNodeResponse(params.res.originalResponse),
waitUntil: this.getWaitUntil(),
},
useCache: true,
onError: params.onError,
......
......@@ -2,11 +2,13 @@ import type { RequestData, FetchEventResult } from './types'
import type { RequestInit } from './spec-extension/request'
import { PageSignatureError } from './error'
import { fromNodeOutgoingHttpHeaders, normalizeNextQueryParam } from './utils'
import { NextFetchEvent } from './spec-extension/fetch-event'
import {
NextFetchEvent,
getWaitUntilPromiseFromEvent,
} from './spec-extension/fetch-event'
import { NextRequest } from './spec-extension/request'
import { NextResponse } from './spec-extension/response'
import { relativizeURL } from '../../shared/lib/router/utils/relativize-url'
import { waitUntilSymbol } from './spec-extension/fetch-event'
import { NextURL } from './next-url'
import { stripInternalSearchParams } from '../internal-utils'
import { normalizeRscURL } from '../../shared/lib/router/utils/app-paths'
......@@ -25,6 +27,7 @@ import type { TextMapGetter } from 'next/dist/compiled/@opentelemetry/api'
import { MiddlewareSpan } from '../lib/trace/constants'
import { CloseController } from './web-on-close'
import { getEdgePreviewProps } from './get-edge-preview-props'
import { getBuiltinRequestContext } from '../after/builtin-request-context'
export class NextRequestHint extends NextRequest {
sourcePage: string
......@@ -199,7 +202,16 @@ export async function adapter(
})
}
const event = new NextFetchEvent({ request, page: params.page })
// if we're in an edge runtime sandbox, we should use the waitUntil
// that we receive from the enclosing NextServer
const outerWaitUntil =
params.request.waitUntil ?? getBuiltinRequestContext()?.waitUntil
const event = new NextFetchEvent({
request,
page: params.page,
context: outerWaitUntil ? { waitUntil: outerWaitUntil } : undefined,
})
let response
let cookiesFromResponse
......@@ -416,7 +428,7 @@ export async function adapter(
return {
response: finalResponse,
waitUntil: Promise.all(event[waitUntilSymbol]),
waitUntil: getWaitUntilPromiseFromEvent(event) ?? Promise.resolve(),
fetchMetrics: request.fetchMetrics,
}
}
......@@ -21,6 +21,7 @@ import AssertImplementation from 'node:assert'
import UtilImplementation from 'node:util'
import AsyncHooksImplementation from 'node:async_hooks'
import { intervalsManager, timeoutsManager } from './resource-managers'
import { createLocalRequestContext } from '../../after/builtin-request-context'
interface ModuleContext {
runtime: EdgeRuntime
......@@ -243,6 +244,8 @@ export const requestStore = new AsyncLocalStorage<{
headers: Headers
}>()
export const edgeSandboxNextRequestContext = createLocalRequestContext()
/**
* Create a module cache specific for the provided parameters. It includes
* a runtime context, require cache and paths cache.
......@@ -452,6 +455,16 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`),
context.clearTimeout = (timeout: number) =>
timeoutsManager.remove(timeout)
// Duplicated from packages/next/src/server/after/builtin-request-context.ts
// because we need to use the sandboxed `Symbol.for`, not the one from the outside
const NEXT_REQUEST_CONTEXT_SYMBOL = context.Symbol.for(
'@next/request-context'
)
Object.defineProperty(context, NEXT_REQUEST_CONTEXT_SYMBOL, {
enumerable: false,
value: edgeSandboxNextRequestContext,
})
return context
},
})
......
import type { NodejsRequestData, FetchEventResult, RequestData } from '../types'
import type { EdgeFunctionDefinition } from '../../../build/webpack/plugins/middleware-plugin'
import type { EdgeRuntime } from 'next/dist/compiled/edge-runtime'
import { getModuleContext, requestStore } from './context'
import {
getModuleContext,
requestStore,
edgeSandboxNextRequestContext,
} from './context'
import { requestToBodyStream } from '../../body-streams'
import { NEXT_RSC_UNION_QUERY } from '../../../client/components/app-router-headers'
import type { ServerComponentsHmrCache } from '../../response-cache'
import {
getBuiltinRequestContext,
type BuiltinRequestContextValue,
} from '../../after/builtin-request-context'
export const ErrorSource = Symbol('SandboxError')
......@@ -128,18 +136,30 @@ export const run = withTaggedErrors(async function runWithTaggedErrors(params) {
try {
let result: FetchEventResult | undefined = undefined
await requestStore.run({ headers }, async () => {
result = await edgeFunction({
request: {
...params.request,
body:
cloned && requestToBodyStream(runtime.context, KUint8Array, cloned),
},
const builtinRequestCtx: BuiltinRequestContextValue = {
...getBuiltinRequestContext(),
// FIXME(after):
// arguably, this is an abuse of "@next/request-context" --
// it'd make more sense to simply forward its existing value into the sandbox (in `createModuleContext`)
// but here we're using it to just pass in `waitUntil` regardless if we were running in this context or not.
waitUntil: params.request.waitUntil,
}
await edgeSandboxNextRequestContext.run(builtinRequestCtx, () =>
requestStore.run({ headers }, async () => {
result = await edgeFunction({
request: {
...params.request,
body:
cloned &&
requestToBodyStream(runtime.context, KUint8Array, cloned),
},
})
for (const headerName of FORBIDDEN_HEADERS) {
result.response.headers.delete(headerName)
}
})
for (const headerName of FORBIDDEN_HEADERS) {
result.response.headers.delete(headerName)
}
})
)
if (!result) throw new Error('Edge function did not return a response')
return result
} finally {
......
import type { WaitUntil } from '../../after/builtin-request-context'
import { PageSignatureError } from '../error'
import type { NextRequest } from './request'
const responseSymbol = Symbol('response')
const passThroughSymbol = Symbol('passThrough')
export const waitUntilSymbol = Symbol('waitUntil')
const waitUntilSymbol = Symbol('waitUntil')
class FetchEvent {
readonly [waitUntilSymbol]: Promise<any>[] = [];
// TODO(after): get rid of the 'internal' variant and always use an external waitUntil
// (this means removing `FetchEventResult.waitUntil` which also requires a builder change)
readonly [waitUntilSymbol]:
| { kind: 'internal'; promises: Promise<any>[] }
| { kind: 'external'; function: WaitUntil };
[responseSymbol]?: Promise<Response>;
[passThroughSymbol] = false
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
constructor(_request: Request) {}
constructor(_request: Request, waitUntil?: WaitUntil) {
this[waitUntilSymbol] = waitUntil
? { kind: 'external', function: waitUntil }
: { kind: 'internal', promises: [] }
}
// TODO: is this dead code? NextFetchEvent never lets this get called
respondWith(response: Response | Promise<Response>): void {
if (!this[responseSymbol]) {
this[responseSymbol] = Promise.resolve(response)
}
}
// TODO: is this dead code? passThroughSymbol is unused
passThroughOnException(): void {
this[passThroughSymbol] = true
}
waitUntil(promise: Promise<any>): void {
this[waitUntilSymbol].push(promise)
if (this[waitUntilSymbol].kind === 'external') {
// if we received an external waitUntil, we delegate to it
// TODO(after): this will make us not go through `getServerError(error, 'edge-server')` in `sandbox`
const waitUntil = this[waitUntilSymbol].function
return waitUntil(promise)
} else {
// if we didn't receive an external waitUntil, we make it work on our own
// (and expect the caller to do something with the promises)
this[waitUntilSymbol].promises.push(promise)
}
}
}
export function getWaitUntilPromiseFromEvent(
event: FetchEvent
): Promise<void> | undefined {
return event[waitUntilSymbol].kind === 'internal'
? Promise.all(event[waitUntilSymbol].promises).then(() => {})
: undefined
}
export class NextFetchEvent extends FetchEvent {
sourcePage: string
constructor(params: { request: NextRequest; page: string }) {
super(params.request)
constructor(params: {
request: NextRequest
page: string
context: { waitUntil: WaitUntil } | undefined
}) {
super(params.request, params.context?.waitUntil)
this.sourcePage = params.page
}
......
......@@ -24,6 +24,8 @@ export interface RequestData {
url: string
body?: ReadableStream<Uint8Array>
signal: AbortSignal
/** passed in when running in edge runtime sandbox */
waitUntil?: (promise: Promise<any>) => void
}
export type NodejsRequestData = Omit<RequestData, 'body'> & {
......
export { default } from '../../nodejs/provided-request-context/page'
export { default } from '../../../nodejs/provided-request-context/page/page'
export { GET } from '../../../nodejs/provided-request-context/route/route'
export const runtime = 'edge'
export const dynamic = 'force-dynamic'
import { unstable_after as after } from 'next/server'
import { unstable_after as after, connection } from 'next/server'
import { cache } from 'react'
import { cliLog } from '../../../utils/log'
const thing = cache(() => Symbol('cache me please'))
export default function Index({ params }) {
export default async function Index({ params }) {
await connection()
const valueFromRender = thing()
after(async () => {
......
import { unstable_after as after } from 'next/server'
import { cliLog } from '../../../utils/log'
export default function Page() {
after(() => {
cliLog({ source: '[page] /provided-request-context' })
})
return <div>provided-request-context</div>
}
import { unstable_after as after, connection } from 'next/server'
import { cliLog } from '../../../../utils/log'
export default async function Page() {
await connection()
after(() => {
cliLog({ source: '[page] /provided-request-context/page' })
})
return <div>provided-request-context</div>
}
import { unstable_after as after } from 'next/server'
import { cliLog } from '../../../../utils/log'
export const runtime = 'nodejs'
export const dynamic = 'force-dynamic'
export async function GET() {
const data = { message: 'Hello, world!' }
after(() => {
cliLog({ source: '[route handler] /provided-request-context/route' })
})
return Response.json({ data })
}
......@@ -19,9 +19,13 @@ describe.each(runtimes)('unstable_after() in %s runtime', (runtimeValue) => {
const pathPrefix = '/' + runtimeValue
let currentCliOutputIndex = 0
beforeEach(() => {
const ignorePreviousLogs = () => {
currentCliOutputIndex = next.cliOutput.length
})
}
const resetLogIsolation = () => {
currentCliOutputIndex = 0
}
const getLogs = () => {
if (next.cliOutput.length < currentCliOutputIndex) {
......@@ -31,6 +35,10 @@ describe.each(runtimes)('unstable_after() in %s runtime', (runtimeValue) => {
return Log.readCliLogs(next.cliOutput.slice(currentCliOutputIndex))
}
beforeEach(() => {
ignorePreviousLogs()
})
it('runs in dynamic pages', async () => {
const response = await next.fetch(pathPrefix + '/123/dynamic')
expect(response.status).toBe(200)
......@@ -261,38 +269,68 @@ describe.each(runtimes)('unstable_after() in %s runtime', (runtimeValue) => {
}
})
it('uses waitUntil from request context if available', async () => {
const { cleanup } = await sandbox(
next,
new Map([
[
// this needs to be injected as early as possible, before the server tries to read the context
// (which may be even before we load the page component in dev mode)
'instrumentation.js',
outdent`
describe('uses waitUntil from request context if available', () => {
let sanboxed: Awaited<ReturnType<typeof sandbox>>
beforeAll(async () => {
resetLogIsolation() // sandbox resets `next.cliOutput` to empty
sanboxed = await sandbox(
next,
new Map([
[
// this needs to be injected as early as possible, before the server tries to read the context
// (which may be even before we load the page component in dev mode)
'instrumentation.js',
outdent`
import { injectRequestContext } from './utils/provided-request-context'
export function register() {
if (process.env.NEXT_RUNTIME === 'edge') {
// these tests only run 'next dev/start', and for edge things,
// instrumentation runs *again* inside the sandbox.
// we don't want that, because the sandbox wouldn't have access to globals from outside
// and thus wouldn't normally see the request context
return;
}
injectRequestContext();
}
`,
],
]),
pathPrefix + '/provided-request-context'
)
],
])
)
})
afterAll(async () => {
await sanboxed.cleanup()
})
try {
it.each([
{
name: 'in a page',
path: '/provided-request-context/page',
expectedLog: { source: '[page] /provided-request-context/page' },
},
{
name: 'in a route handler',
path: '/provided-request-context/route',
expectedLog: {
source: '[route handler] /provided-request-context/route',
},
},
{
name: 'in middleware',
path: '/provided-request-context/middleware',
expectedLog: {
source: '[middleware] /provided-request-context/middleware',
},
},
])('$name', async ({ path, expectedLog }) => {
await next.browser(pathPrefix + path)
await retry(() => {
const logs = getLogs()
expect(logs).toContainEqual(
'waitUntil from "@next/request-context" was called'
)
expect(logs).toContainEqual({
source: '[page] /provided-request-context',
})
expect(logs).toContainEqual(expectedLog)
})
} finally {
await cleanup()
}
})
})
})
......
......@@ -7,26 +7,48 @@ export async function middleware(
) {
const url = new URL(request.url)
const match = url.pathname.match(
/^(?<prefix>\/[^/]+?)\/middleware\/redirect-source/
)
if (match) {
const prefix = match.groups.prefix
const requestId = url.searchParams.get('requestId')
const cookieStore = await cookies()
after(async () => {
cliLog({
source: '[middleware] /middleware/redirect-source',
requestId,
cookies: { testCookie: cookieStore.get('testCookie')?.value },
{
const match = url.pathname.match(
/^(?<prefix>\/[^/]+?)\/middleware\/redirect-source/
)
if (match) {
const prefix = match.groups.prefix
const requestId = url.searchParams.get('requestId')
const cookieStore = await cookies()
after(async () => {
cliLog({
source: '[middleware] /middleware/redirect-source',
requestId,
cookies: { testCookie: cookieStore.get('testCookie')?.value },
})
})
})
return NextResponse.redirect(
new URL(prefix + '/middleware/redirect', request.url)
return NextResponse.redirect(
new URL(prefix + '/middleware/redirect', request.url)
)
}
}
{
const match = url.pathname.match(
/^(?<prefix>\/[^/]+?)\/provided-request-context\/middleware/
)
if (match) {
const prefix = match.groups.prefix
after(() => {
cliLog({
source: '[middleware] /provided-request-context/middleware',
})
})
return NextResponse.redirect(
new URL(prefix + '/middleware/redirect', request.url)
)
}
}
}
export const config = {
matcher: ['/:prefix/middleware/:path*'],
matcher: [
'/:prefix/middleware/:path*',
'/:prefix/provided-request-context/middleware',
],
}
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