11import type { ReadableStream } from "node:stream/web" ;
22
33import type { InternalEvent , InternalResult } from "types/open-next" ;
4- import {
5- awaitAllDetachedPromise ,
6- provideNextAfterProvider ,
7- } from "utils/promise" ;
4+ import { runWithOpenNextRequestContext } from "utils/promise" ;
85import { emptyReadableStream } from "utils/stream" ;
96
107// We import it like that so that the edge plugin can replace it
@@ -14,7 +11,6 @@ import {
1411 convertBodyToReadableStream ,
1512 convertToQueryString ,
1613} from "../core/routing/util" ;
17- import { generateOpenNextRequestContext } from "./util" ;
1814
1915globalThis . __als = new AsyncLocalStorage ( ) ;
2016
@@ -23,64 +19,55 @@ const defaultHandler = async (
2319) : Promise < InternalResult > => {
2420 globalThis . isEdgeRuntime = true ;
2521
26- const { requestId, pendingPromiseRunner, isISRRevalidation } =
27- generateOpenNextRequestContext ( ) ;
28-
2922 // We run everything in the async local storage context so that it is available in edge runtime functions
30- return globalThis . __als . run (
31- { requestId, pendingPromiseRunner, isISRRevalidation } ,
32- async ( ) => {
33- provideNextAfterProvider ( ) ;
34- const host = internalEvent . headers . host
35- ? `https://${ internalEvent . headers . host } `
36- : "http://localhost:3000" ;
37- const initialUrl = new URL ( internalEvent . rawPath , host ) ;
38- initialUrl . search = convertToQueryString ( internalEvent . query ) ;
39- const url = initialUrl . toString ( ) ;
40-
41- // @ts -expect-error - This is bundled
42- const handler = await import ( `./middleware.mjs` ) ;
23+ return runWithOpenNextRequestContext ( false , async ( ) => {
24+ const host = internalEvent . headers . host
25+ ? `https://${ internalEvent . headers . host } `
26+ : "http://localhost:3000" ;
27+ const initialUrl = new URL ( internalEvent . rawPath , host ) ;
28+ initialUrl . search = convertToQueryString ( internalEvent . query ) ;
29+ const url = initialUrl . toString ( ) ;
4330
44- const response : Response = await handler . default ( {
45- headers : internalEvent . headers ,
46- method : internalEvent . method || "GET" ,
47- nextConfig : {
48- basePath : NextConfig . basePath ,
49- i18n : NextConfig . i18n ,
50- trailingSlash : NextConfig . trailingSlash ,
51- } ,
52- url,
53- body : convertBodyToReadableStream (
54- internalEvent . method ,
55- internalEvent . body ,
56- ) ,
57- } ) ;
58- const responseHeaders : Record < string , string | string [ ] > = { } ;
59- response . headers . forEach ( ( value , key ) => {
60- if ( key . toLowerCase ( ) === "set-cookie" ) {
61- responseHeaders [ key ] = responseHeaders [ key ]
62- ? [ ...responseHeaders [ key ] , value ]
63- : [ value ] ;
64- } else {
65- responseHeaders [ key ] = value ;
66- }
67- } ) ;
31+ // @ts -expect-error - This is bundled
32+ const handler = await import ( `./middleware.mjs` ) ;
6833
69- const body =
70- ( response . body as ReadableStream < Uint8Array > ) ?? emptyReadableStream ( ) ;
34+ const response : Response = await handler . default ( {
35+ headers : internalEvent . headers ,
36+ method : internalEvent . method || "GET" ,
37+ nextConfig : {
38+ basePath : NextConfig . basePath ,
39+ i18n : NextConfig . i18n ,
40+ trailingSlash : NextConfig . trailingSlash ,
41+ } ,
42+ url,
43+ body : convertBodyToReadableStream (
44+ internalEvent . method ,
45+ internalEvent . body ,
46+ ) ,
47+ } ) ;
48+ const responseHeaders : Record < string , string | string [ ] > = { } ;
49+ response . headers . forEach ( ( value , key ) => {
50+ if ( key . toLowerCase ( ) === "set-cookie" ) {
51+ responseHeaders [ key ] = responseHeaders [ key ]
52+ ? [ ...responseHeaders [ key ] , value ]
53+ : [ value ] ;
54+ } else {
55+ responseHeaders [ key ] = value ;
56+ }
57+ } ) ;
7158
72- await awaitAllDetachedPromise ( ) ;
59+ const body =
60+ ( response . body as ReadableStream < Uint8Array > ) ?? emptyReadableStream ( ) ;
7361
74- return {
75- type : "core" ,
76- statusCode : response . status ,
77- headers : responseHeaders ,
78- body : body ,
79- // Do we need to handle base64 encoded response?
80- isBase64Encoded : false ,
81- } ;
82- } ,
83- ) ;
62+ return {
63+ type : "core" ,
64+ statusCode : response . status ,
65+ headers : responseHeaders ,
66+ body : body ,
67+ // Do we need to handle base64 encoded response?
68+ isBase64Encoded : false ,
69+ } ;
70+ } ) ;
8471} ;
8572
8673export const handler = await createGenericHandler ( {
0 commit comments