-
-
Notifications
You must be signed in to change notification settings - Fork 666
/
Copy pathindex.ts
136 lines (123 loc) · 3.77 KB
/
index.ts
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
/**
* @module
* Proxy Helper for Hono.
*/
import type { RequestHeader } from '../../utils/headers'
// https://datatracker.ietf.org/doc/html/rfc2616#section-13.5.1
const hopByHopHeaders = [
'connection',
'keep-alive',
'proxy-authenticate',
'proxy-authorization',
'te',
'trailers',
'transfer-encoding',
]
interface ProxyRequestInit extends Omit<RequestInit, 'headers'> {
raw?: Request
headers?:
| HeadersInit
| [string, string][]
| Record<RequestHeader, string | undefined>
| Record<string, string | undefined>
}
interface ProxyFetch {
(input: string | URL | Request, init?: ProxyRequestInit): Promise<Response>
}
const buildRequestInitFromRequest = (
request: Request | undefined
): RequestInit & { duplex?: 'half' } => {
if (!request) {
return {}
}
const headers = new Headers(request.headers)
hopByHopHeaders.forEach((header) => {
headers.delete(header)
})
return {
method: request.method,
body: request.body,
duplex: request.body ? 'half' : undefined,
headers,
signal: request.signal,
}
}
const preprocessRequestInit = (requestInit: RequestInit): RequestInit => {
if (
!requestInit.headers ||
Array.isArray(requestInit.headers) ||
requestInit.headers instanceof Headers
) {
return requestInit
}
const headers = new Headers()
for (const [key, value] of Object.entries(requestInit.headers)) {
if (value == null) {
// delete header if value is null or undefined
headers.delete(key)
} else {
headers.set(key, value)
}
}
requestInit.headers = headers
return requestInit
}
/**
* Fetch API wrapper for proxy.
* The parameters and return value are the same as for `fetch` (except for the proxy-specific options).
*
* The “Accept-Encoding” header is replaced with an encoding that the current runtime can handle.
* Unnecessary response headers are deleted and a Response object is returned that can be returned
* as is as a response from the handler.
*
* @example
* ```ts
* app.get('/proxy/:path', (c) => {
* return proxy(`http://${originServer}/${c.req.param('path')}`, {
* headers: {
* ...c.req.header(), // optional, specify only when forwarding all the request data (including credentials) is necessary.
* 'X-Forwarded-For': '127.0.0.1',
* 'X-Forwarded-Host': c.req.header('host'),
* Authorization: undefined, // do not propagate request headers contained in c.req.header('Authorization')
* },
* }).then((res) => {
* res.headers.delete('Set-Cookie')
* return res
* })
* })
*
* app.all('/proxy/:path', (c) => {
* return proxy(`http://${originServer}/${c.req.param('path')}`, {
* ...c.req, // optional, specify only when forwarding all the request data (including credentials) is necessary.
* headers: {
* ...c.req.header(),
* 'X-Forwarded-For': '127.0.0.1',
* 'X-Forwarded-Host': c.req.header('host'),
* Authorization: undefined, // do not propagate request headers contained in c.req.header('Authorization')
* },
* })
* })
* ```
*/
export const proxy: ProxyFetch = async (input, proxyInit) => {
const { raw, ...requestInit } = proxyInit ?? {}
const req = new Request(input, {
...buildRequestInitFromRequest(raw),
...preprocessRequestInit(requestInit as RequestInit),
})
req.headers.delete('accept-encoding')
const res = await fetch(req)
const resHeaders = new Headers(res.headers)
hopByHopHeaders.forEach((header) => {
resHeaders.delete(header)
})
if (resHeaders.has('content-encoding')) {
resHeaders.delete('content-encoding')
// Content-Length is the size of the compressed content, not the size of the original content
resHeaders.delete('content-length')
}
return new Response(res.body, {
...res,
headers: resHeaders,
})
}