1
+ import { default as fetch , Headers , Request , Response } from "node-fetch" ;
2
+ Object . assign ( globalThis , {
3
+ Request,
4
+ Response,
5
+ fetch,
6
+ Headers,
7
+ self : { }
8
+ } ) ;
9
+ const index = await ( ( ) => import ( "./middleware.js" ) ) ( ) ;
10
+
11
+ // TODO
12
+ //console.log(self);
13
+ //handler({
14
+ // Records: [
15
+ // {
16
+ // cf: {
17
+ // request: {
18
+ // uri: "https://sst.dev/_next/data/5fCVTp6Xr7VQpZ-m8Wxxq/middleware-redirect.json",
19
+ // method: "GET",
20
+ // headers: {
21
+ // host: [{ value: "sst.dev" }]
22
+ // },
23
+ // querystring: "",
24
+ // },
25
+ // }
26
+ // }
27
+ // ]
28
+ //}).then((res) => console.log(JSON.stringify(res, null, 2)));
29
+
30
+ export async function handler ( event ) {
31
+ // Convert CloudFront request to Node request
32
+ const request = event . Records [ 0 ] . cf . request ;
33
+ const { uri, method, headers, querystring, body } = request ;
34
+ console . log ( uri ) ;
35
+ console . log ( request ) ;
36
+ const requestHeaders = new Headers ( ) ;
37
+ for ( const [ key , values ] of Object . entries ( headers ) ) {
38
+ for ( const { value } of values ) {
39
+ if ( value ) {
40
+ requestHeaders . append ( key , value )
41
+ }
42
+ }
43
+ }
44
+ const host = headers [ "host" ] [ 0 ] . value ;
45
+ const qs = querystring . length > 0 ? `?${ querystring } ` : "" ;
46
+ const url = new URL ( `${ uri } ${ qs } ` , `https://${ host } ` ) ;
47
+ const nodeRequest = new Request ( url . toString ( ) , {
48
+ method,
49
+ headers : requestHeaders ,
50
+ body : body ?. data
51
+ ? body . encoding === "base64"
52
+ ? Buffer . from ( body . data , "base64" ) . toString ( )
53
+ : body . data
54
+ : undefined ,
55
+ } ) ;
56
+
57
+ // Process request
58
+ const response = await index . default ( nodeRequest , {
59
+ waitUntil : ( ) => { } ,
60
+ } ) ;
61
+
62
+ // Build headers
63
+ ( response . headers . get ( "x-middleware-override-headers" ) || "" )
64
+ . split ( "," )
65
+ . forEach ( key => {
66
+ headers [ key ] = [ {
67
+ key,
68
+ value : response . headers . get ( `x-middleware-request-${ key } ` )
69
+ } ] ;
70
+ } ) ;
71
+
72
+ if ( response . headers . get ( "x-middleware-next" ) === "1" ) {
73
+ headers [ "x-wahaha" ] = [ { key : "x-wahaha" , value : "wahaha" } ] ;
74
+ headers [ "wahaha" ] = [ { key : "wahaha" , value : "wahaha" } ] ;
75
+ console . log ( "== conitnue to origin ==" , request )
76
+ return request ;
77
+ }
78
+
79
+ console . log ( "== do not hit origin ==" , {
80
+ status : response . status ,
81
+ headers,
82
+ } ) ;
83
+ return {
84
+ status : response . status ,
85
+ headers,
86
+ }
87
+ }
88
+
89
+ /**
90
+ * middleware-fetch
91
+ *
92
+ * nextresponse [response] {
93
+ size: 0,
94
+ [symbol(body internals)]: {
95
+ body: null,
96
+ stream: null,
97
+ boundary: null,
98
+ disturbed: false,
99
+ error: null
100
+ },
101
+ [symbol(response internals)]: {
102
+ type: 'default',
103
+ url: undefined,
104
+ status: 200,
105
+ statustext: '',
106
+ headers: { 'x-middleware-next': '1' },
107
+ counter: undefined,
108
+ highwatermark: undefined
109
+ },
110
+ [Symbol(internal response)]: {
111
+ cookies: ResponseCookies { _parsed: Map(0) {}, _headers: [Object] },
112
+ url: undefined
113
+ }
114
+ }
115
+ */
116
+ /**
117
+ * middleware-set-header
118
+ *
119
+ * NextResponse [Response] {
120
+ size: 0,
121
+ [Symbol(Body internals)]: {
122
+ body: null,
123
+ stream: null,
124
+ boundary: null,
125
+ disturbed: false,
126
+ error: null
127
+ },
128
+ [Symbol(Response internals)]: {
129
+ type: 'default',
130
+ url: undefined,
131
+ status: 200,
132
+ statusText: '',
133
+ headers: {
134
+ 'x-hello-from-middleware2': 'hello',
135
+ 'x-middleware-next': '1',
136
+ 'x-middleware-override-headers': 'x-hello-from-middleware1',
137
+ 'x-middleware-request-x-hello-from-middleware1': 'hello'
138
+ },
139
+ counter: undefined,
140
+ highWaterMark: undefined
141
+ },
142
+ [Symbol(internal response)]: {
143
+ cookies: ResponseCookies { _parsed: Map(0) {}, _headers: [Object] },
144
+ url: undefined
145
+ }
146
+ }
147
+ */
148
+ /**
149
+ * middleware-redirect
150
+ *
151
+ * Response {
152
+ size: 0,
153
+ [Symbol(Body internals)]: {
154
+ body: null,
155
+ stream: null,
156
+ boundary: null,
157
+ disturbed: false,
158
+ error: null
159
+ },
160
+ [Symbol(Response internals)]: {
161
+ type: 'default',
162
+ url: '',
163
+ status: 307,
164
+ statusText: '',
165
+ headers: { location: 'https://sst.dev/ssr' },
166
+ counter: undefined,
167
+ highWaterMark: undefined
168
+ }
169
+ }
170
+ */
0 commit comments