forked from vercel/next.js
/
_middleware.js
144 lines (131 loc) · 3.58 KB
/
_middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* global globalThis */
import { NextResponse } from 'next/server'
export async function middleware(request) {
const url = request.nextUrl
if (url.pathname.endsWith('/globalthis')) {
return new NextResponse(JSON.stringify(Object.keys(globalThis)), {
headers: {
'content-type': 'application/json; charset=utf-8',
},
})
}
if (url.pathname.endsWith('/fetchURL')) {
const response = {}
try {
await fetch(new URL('http://localhost'))
} catch (err) {
response.error = {
name: err.name,
message: err.message,
}
} finally {
return new NextResponse(JSON.stringify(response), {
headers: {
'content-type': 'application/json; charset=utf-8',
},
})
}
}
if (url.pathname.includes('/fetchUserAgentDefault')) {
try {
const apiRoute = new URL(url)
apiRoute.pathname = '/api/headers'
const res = await fetch(apiRoute)
return new Response(await res.text(), {
status: 200,
headers: {
'content-type': 'application/json',
},
})
} catch (err) {
return new Response(JSON.stringify({ error: err.message }), {
status: 500,
headers: {
'content-type': 'application/json',
},
})
}
}
if (url.pathname.includes('/fetchUserAgentCustom')) {
try {
const apiRoute = new URL(url)
apiRoute.pathname = '/api/headers'
const res = await fetch(apiRoute, {
headers: {
'user-agent': 'custom-agent',
},
})
return new Response(await res.text(), {
status: 200,
headers: {
'content-type': 'application/json',
},
})
} catch (err) {
return new Response(JSON.stringify({ error: err.message }), {
status: 500,
headers: {
'content-type': 'application/json',
},
})
}
}
if (url.pathname.endsWith('/webcrypto')) {
const response = {}
try {
const algorithm = {
name: 'RSA-PSS',
hash: 'SHA-256',
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
modulusLength: 2048,
}
const keyUsages = ['sign', 'verify']
await crypto.subtle.generateKey(algorithm, false, keyUsages)
} catch (err) {
response.error = true
} finally {
return new NextResponse(JSON.stringify(response), {
headers: {
'content-type': 'application/json; charset=utf-8',
},
})
}
}
if (url.pathname.endsWith('/root-subrequest')) {
return fetch(url)
}
if (url.pathname.endsWith('/abort-controller')) {
const controller = new AbortController()
const signal = controller.signal
controller.abort()
const response = {}
try {
await fetch('https://example.com', { signal })
} catch (err) {
response.error = {
name: err.name,
message: err.message,
}
} finally {
return new NextResponse(JSON.stringify(response), {
headers: {
'content-type': 'application/json; charset=utf-8',
},
})
}
}
if (url.pathname.endsWith('/dynamic-replace')) {
url.pathname = '/_interface/dynamic-path'
return NextResponse.rewrite(url)
}
return new Response(null, {
headers: {
'req-url-basepath': request.nextUrl.basePath,
'req-url-pathname': request.nextUrl.pathname,
'req-url-params': JSON.stringify(request.page.params),
'req-url-page': request.page.name,
'req-url-query': request.nextUrl.searchParams.get('foo'),
'req-url-locale': request.nextUrl.locale,
},
})
}