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