|
|
import type { NextRequest } from './spec-extension/request' |
|
|
import type { |
|
|
AppRouteRouteHandlerContext, |
|
|
AppRouteRouteModule, |
|
|
} from '../route-modules/app-route/module' |
|
|
|
|
|
import './globals' |
|
|
|
|
|
import { adapter, type AdapterOptions } from './adapter' |
|
|
import { IncrementalCache } from '../lib/incremental-cache' |
|
|
import { RouteMatcher } from '../route-matchers/route-matcher' |
|
|
import type { NextFetchEvent } from './spec-extension/fetch-event' |
|
|
import { internal_getCurrentFunctionWaitUntil } from './internal-edge-wait-until' |
|
|
import { getServerUtils } from '../server-utils' |
|
|
import { searchParamsToUrlQuery } from '../../shared/lib/router/utils/querystring' |
|
|
import { CloseController, trackStreamConsumed } from './web-on-close' |
|
|
import { getEdgePreviewProps } from './get-edge-preview-props' |
|
|
import type { NextConfigComplete } from '../config-shared' |
|
|
|
|
|
export interface WrapOptions { |
|
|
nextConfig: NextConfigComplete |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class EdgeRouteModuleWrapper { |
|
|
private readonly matcher: RouteMatcher |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private constructor( |
|
|
private readonly routeModule: AppRouteRouteModule, |
|
|
private readonly nextConfig: NextConfigComplete |
|
|
) { |
|
|
|
|
|
this.matcher = new RouteMatcher(routeModule.definition) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static wrap(routeModule: AppRouteRouteModule, options: WrapOptions) { |
|
|
|
|
|
const wrapper = new EdgeRouteModuleWrapper(routeModule, options.nextConfig) |
|
|
|
|
|
|
|
|
return (opts: AdapterOptions) => { |
|
|
return adapter({ |
|
|
...opts, |
|
|
IncrementalCache, |
|
|
|
|
|
handler: wrapper.handler.bind(wrapper), |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
private async handler( |
|
|
request: NextRequest, |
|
|
evt: NextFetchEvent |
|
|
): Promise<Response> { |
|
|
const utils = getServerUtils({ |
|
|
pageIsDynamic: this.matcher.isDynamic, |
|
|
page: this.matcher.definition.pathname, |
|
|
basePath: request.nextUrl.basePath, |
|
|
|
|
|
rewrites: {}, |
|
|
|
|
|
caseSensitive: false, |
|
|
}) |
|
|
|
|
|
const { params } = utils.normalizeDynamicRouteParams( |
|
|
searchParamsToUrlQuery(request.nextUrl.searchParams), |
|
|
false |
|
|
) |
|
|
|
|
|
const waitUntil = evt.waitUntil.bind(evt) |
|
|
const closeController = new CloseController() |
|
|
|
|
|
const previewProps = getEdgePreviewProps() |
|
|
|
|
|
|
|
|
|
|
|
const context: AppRouteRouteHandlerContext = { |
|
|
params, |
|
|
prerenderManifest: { |
|
|
version: 4, |
|
|
routes: {}, |
|
|
dynamicRoutes: {}, |
|
|
preview: previewProps, |
|
|
notFoundRoutes: [], |
|
|
}, |
|
|
renderOpts: { |
|
|
supportsDynamicResponse: true, |
|
|
waitUntil, |
|
|
onClose: closeController.onClose.bind(closeController), |
|
|
onAfterTaskError: undefined, |
|
|
experimental: { |
|
|
cacheComponents: !!process.env.__NEXT_CACHE_COMPONENTS, |
|
|
authInterrupts: !!process.env.__NEXT_EXPERIMENTAL_AUTH_INTERRUPTS, |
|
|
}, |
|
|
cacheLifeProfiles: this.nextConfig.experimental.cacheLife, |
|
|
}, |
|
|
sharedContext: { |
|
|
buildId: '', |
|
|
}, |
|
|
} |
|
|
|
|
|
|
|
|
let res = await this.routeModule.handle(request, context) |
|
|
|
|
|
const waitUntilPromises = [internal_getCurrentFunctionWaitUntil()] |
|
|
if (context.renderOpts.pendingWaitUntil) { |
|
|
waitUntilPromises.push(context.renderOpts.pendingWaitUntil) |
|
|
} |
|
|
evt.waitUntil(Promise.all(waitUntilPromises)) |
|
|
|
|
|
if (!res.body) { |
|
|
|
|
|
|
|
|
setTimeout(() => closeController.dispatchClose(), 0) |
|
|
} else { |
|
|
|
|
|
|
|
|
const trackedBody = trackStreamConsumed(res.body, () => |
|
|
closeController.dispatchClose() |
|
|
) |
|
|
res = new Response(trackedBody, { |
|
|
status: res.status, |
|
|
statusText: res.statusText, |
|
|
headers: res.headers, |
|
|
}) |
|
|
} |
|
|
|
|
|
return res |
|
|
} |
|
|
} |
|
|
|