acrylblends 0.6.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -6
- package/dist/index.d.ts +2 -0
- package/dist/index.js +13 -1
- package/dist/index.js.map +1 -1
- package/docs/framework/UPSTREAM.md +1 -1
- package/docs/framework/api/presets.md +0 -3
- package/docs/framework/cordis/acryl-in-a-cordis-app.md +3 -0
- package/docs/framework/getting-started/basic.md +4 -4
- package/docs/framework/guides/jsx.md +1 -1
- package/docs/framework/guides/middleware.md +1 -1
- package/docs/framework/helpers/cookie.md +1 -1
- package/docs/framework/helpers/dev.md +0 -3
- package/docs/framework/helpers/jwt.md +1 -1
- package/docs/framework/helpers/proxy.md +0 -3
- package/docs/framework/middleware/builtin/basic-auth.md +6 -6
- package/docs/framework/middleware/builtin/cache.md +0 -3
- package/docs/framework/middleware/builtin/combine.md +1 -1
- package/docs/framework/middleware/builtin/ip-restriction.md +1 -1
- package/docs/framework/middleware/builtin/logger.md +1 -1
- package/docs/framework/middleware/builtin/mount.md +0 -3
- package/docs/framework/upstream.json +2 -2
- package/lib/accepts.d.ts +1 -1
- package/lib/acryl-base.d.ts +1 -1
- package/lib/adapter.d.ts +1 -1
- package/lib/{app-CmAYW1Al.d.ts → app-BdrVHBlJ.d.ts} +1 -1
- package/lib/basic-auth.d.ts +1 -1
- package/lib/bearer-auth.d.ts +1 -1
- package/lib/body-limit.d.ts +1 -1
- package/lib/cache.d.ts +43 -0
- package/lib/cache.js +169 -0
- package/lib/client.d.ts +2 -2
- package/lib/combine.d.ts +1 -1
- package/lib/compress.d.ts +1 -1
- package/lib/conninfo.d.ts +2 -2
- package/lib/{context-Bm7rI-oK.d.ts → context-B5Y1lLha.d.ts} +1 -1
- package/lib/context-storage.d.ts +1 -1
- package/lib/{cookie-ClMaKDQf.d.ts → cookie-D9_nQbsM.d.ts} +1 -1
- package/lib/cookie.d.ts +3 -3
- package/lib/cors.d.ts +1 -1
- package/lib/csrf.d.ts +1 -1
- package/lib/dev.d.ts +19 -0
- package/lib/dev.js +50 -0
- package/lib/etag.d.ts +1 -1
- package/lib/factory.d.ts +2 -2
- package/lib/{index-B-3F4wt2.d.ts → index-DtEoak1i.d.ts} +2 -2
- package/lib/{index-UTBmM3GX.d.ts → index-Kqou8Fjh.d.ts} +2 -2
- package/lib/{index-oOClm_Xk.d.ts → index-m12zcmph.d.ts} +3 -3
- package/lib/index.d.ts +4 -4
- package/lib/ip-restriction.d.ts +2 -2
- package/lib/jwk.d.ts +4 -4
- package/lib/{jwt-Dg493Tai.d.ts → jwt-BA65QhnJ.d.ts} +1 -1
- package/lib/jwt.d.ts +4 -4
- package/lib/language.d.ts +1 -1
- package/lib/linear-router-C3FPib6h.js +96 -0
- package/lib/logger.d.ts +1 -1
- package/lib/loopback-only.d.ts +1 -1
- package/lib/method-not-allowed.d.ts +2 -2
- package/lib/method-override.d.ts +2 -2
- package/lib/mount.d.ts +44 -0
- package/lib/mount.js +67 -0
- package/lib/node-server/conninfo.d.ts +1 -1
- package/lib/node-server/serve-static.d.ts +1 -1
- package/lib/node-server.d.ts +1 -1
- package/lib/pattern-router-HZxjDGEQ.js +41 -0
- package/lib/powered-by.d.ts +1 -1
- package/lib/pretty-json.d.ts +1 -1
- package/lib/proxy.d.ts +72 -0
- package/lib/proxy.js +121 -0
- package/lib/quick.d.ts +8 -0
- package/lib/quick.js +17 -0
- package/lib/request-id.d.ts +1 -1
- package/lib/request.d.ts +1 -1
- package/lib/route.d.ts +1 -1
- package/lib/router/linear-router.js +1 -95
- package/lib/router/pattern-router.js +1 -40
- package/lib/schema-validator.d.ts +1 -1
- package/lib/secure-headers.d.ts +1 -1
- package/lib/serve-static.d.ts +1 -1
- package/lib/streaming.d.ts +1 -1
- package/lib/testing.d.ts +4 -4
- package/lib/timeout.d.ts +1 -1
- package/lib/timing.d.ts +1 -1
- package/lib/tiny.d.ts +8 -0
- package/lib/tiny.js +15 -0
- package/lib/trailing-slash.d.ts +1 -1
- package/lib/{types-CHesD1KG.d.ts → types-Bd29gh2o.d.ts} +3 -3
- package/lib/{types-CwrQjlaM.d.ts → types-D9DEXMjE.d.ts} +3 -1
- package/lib/{types-CDO6I3B0.d.ts → types-ZM9gIiKh.d.ts} +1 -1
- package/lib/types.d.ts +1 -1
- package/lib/utils/body.d.ts +1 -1
- package/lib/utils/cookie.d.ts +1 -1
- package/lib/utils/crypto.d.ts +1 -1
- package/lib/utils/ipaddr.d.ts +1 -1
- package/lib/utils/jwt.d.ts +1 -1
- package/lib/utils/types.d.ts +2 -2
- package/lib/validator.d.ts +2 -2
- package/lib/ws.d.ts +1 -1
- package/package.json +25 -1
- package/plugins/acryl-inspect/upstream.json +1 -1
- package/plugins/acryl-node-server/upstream.json +1 -1
- package/plugins/acryl-router/src/core/helper/dev/index.test.ts +181 -0
- package/plugins/acryl-router/src/core/helper/dev/index.ts +79 -0
- package/plugins/acryl-router/src/core/helper/proxy/index.test.ts +356 -0
- package/plugins/acryl-router/src/core/helper/proxy/index.ts +203 -0
- package/plugins/acryl-router/src/core/middleware/cache/index.test.ts +1211 -0
- package/plugins/acryl-router/src/core/middleware/cache/index.ts +323 -0
- package/plugins/acryl-router/src/core/middleware/mount/index.test.ts +334 -0
- package/plugins/acryl-router/src/core/middleware/mount/index.ts +108 -0
- package/plugins/acryl-router/src/core/preset/quick.test.ts +42 -0
- package/plugins/acryl-router/src/core/preset/quick.ts +24 -0
- package/plugins/acryl-router/src/core/preset/tiny.test.ts +42 -0
- package/plugins/acryl-router/src/core/preset/tiny.ts +20 -0
- package/plugins/acryl-router/src/core/utils/types.ts +3 -0
- package/plugins/acryl-router/src/helpers/index.ts +2 -0
- package/plugins/acryl-router/src/middleware/index.ts +2 -0
- package/plugins/acryl-router/src/service.ts +22 -5
- package/plugins/acryl-router/upstream.json +2 -2
- package/plugins/acryl-router-http/upstream.json +1 -1
- package/plugins/acryl-state/upstream.json +1 -1
- package/plugins/desktop/upstream.json +1 -1
- package/plugins/desktop-web/main.ts +92 -0
- package/plugins/desktop-web/plugin.json +17 -0
- package/plugins/desktop-web/shutdown.ts +177 -0
- package/plugins/desktop-web/upstream.json +12 -0
- package/plugins/desktop-web/user-data.ts +38 -0
- package/plugins/desktop-web/window.ts +29 -0
- package/plugins/tsconfig.json +2 -0
- package/templates/todo/upstream.json +1 -1
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import { proxy } from './index.ts'
|
|
2
|
+
import { Acryl } from '../../app.ts'
|
|
3
|
+
|
|
4
|
+
describe('Proxy Middleware', () => {
|
|
5
|
+
describe('proxy', () => {
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
global.fetch = vi.fn().mockImplementation(async (req) => {
|
|
8
|
+
if (req.url === 'https://example.com/ok') {
|
|
9
|
+
return Promise.resolve(new Response('ok'))
|
|
10
|
+
} else if (req.url === 'https://example.com/disconnect') {
|
|
11
|
+
const reader = req.body.getReader()
|
|
12
|
+
let response
|
|
13
|
+
|
|
14
|
+
req.signal.addEventListener('abort', () => {
|
|
15
|
+
response = req.signal.reason
|
|
16
|
+
reader.cancel()
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
await reader.read()
|
|
20
|
+
|
|
21
|
+
return Promise.resolve(new Response(response))
|
|
22
|
+
} else if (req.url === 'https://example.com/compressed') {
|
|
23
|
+
return Promise.resolve(
|
|
24
|
+
new Response('ok', {
|
|
25
|
+
headers: {
|
|
26
|
+
'Content-Encoding': 'gzip',
|
|
27
|
+
'Content-Length': '1',
|
|
28
|
+
'Content-Range': 'bytes 0-2/1024',
|
|
29
|
+
'X-Response-Id': '456',
|
|
30
|
+
},
|
|
31
|
+
})
|
|
32
|
+
)
|
|
33
|
+
} else if (req.url === 'https://example.com/uncompressed') {
|
|
34
|
+
return Promise.resolve(
|
|
35
|
+
new Response('ok', {
|
|
36
|
+
headers: {
|
|
37
|
+
'Content-Length': '2',
|
|
38
|
+
'Content-Range': 'bytes 0-2/1024',
|
|
39
|
+
'X-Response-Id': '456',
|
|
40
|
+
},
|
|
41
|
+
})
|
|
42
|
+
)
|
|
43
|
+
} else if (req.url === 'https://example.com/post' && req.method === 'POST') {
|
|
44
|
+
return Promise.resolve(new Response(`request body: ${await req.text()}`))
|
|
45
|
+
} else if (req.url === 'https://example.com/hop-by-hop') {
|
|
46
|
+
return Promise.resolve(
|
|
47
|
+
new Response('ok', {
|
|
48
|
+
headers: {
|
|
49
|
+
'Transfer-Encoding': 'chunked',
|
|
50
|
+
},
|
|
51
|
+
})
|
|
52
|
+
)
|
|
53
|
+
} else if (req.url === 'https://example.com/connection-listed') {
|
|
54
|
+
return Promise.resolve(
|
|
55
|
+
new Response('ok', {
|
|
56
|
+
headers: {
|
|
57
|
+
Connection: 'x-connection-scoped, bad name',
|
|
58
|
+
'X-Connection-Scoped': 'should not be forwarded',
|
|
59
|
+
'X-Normal': 'kept',
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
)
|
|
63
|
+
} else if (req.url === 'https://example.com/set-cookie') {
|
|
64
|
+
return Promise.resolve(
|
|
65
|
+
new Response('ok', {
|
|
66
|
+
headers: {
|
|
67
|
+
'Set-Cookie': 'test=123',
|
|
68
|
+
},
|
|
69
|
+
})
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
return Promise.resolve(new Response('not found', { status: 404 }))
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('compressed', async () => {
|
|
77
|
+
const app = new Acryl()
|
|
78
|
+
app.get('/proxy/:path', (c) =>
|
|
79
|
+
proxy(
|
|
80
|
+
new Request(`https://example.com/${c.req.param('path')}`, {
|
|
81
|
+
headers: {
|
|
82
|
+
'X-Request-Id': '123',
|
|
83
|
+
'Accept-Encoding': 'gzip',
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
const res = await app.request('/proxy/compressed')
|
|
89
|
+
const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
|
|
90
|
+
|
|
91
|
+
expect(req.url).toBe('https://example.com/compressed')
|
|
92
|
+
expect(req.headers.get('X-Request-Id')).toBe('123')
|
|
93
|
+
expect(req.headers.get('Accept-Encoding')).toBeNull()
|
|
94
|
+
|
|
95
|
+
expect(res.status).toBe(200)
|
|
96
|
+
expect(res.headers.get('X-Response-Id')).toBe('456')
|
|
97
|
+
expect(res.headers.get('Content-Encoding')).toBeNull()
|
|
98
|
+
expect(res.headers.get('Content-Length')).toBeNull()
|
|
99
|
+
expect(res.headers.get('Content-Range')).toBe('bytes 0-2/1024')
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('uncompressed', async () => {
|
|
103
|
+
const app = new Acryl()
|
|
104
|
+
app.get('/proxy/:path', (c) =>
|
|
105
|
+
proxy(
|
|
106
|
+
new Request(`https://example.com/${c.req.param('path')}`, {
|
|
107
|
+
headers: {
|
|
108
|
+
'X-Request-Id': '123',
|
|
109
|
+
'Accept-Encoding': 'gzip',
|
|
110
|
+
},
|
|
111
|
+
})
|
|
112
|
+
)
|
|
113
|
+
)
|
|
114
|
+
const res = await app.request('/proxy/uncompressed')
|
|
115
|
+
const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
|
|
116
|
+
|
|
117
|
+
expect(req.url).toBe('https://example.com/uncompressed')
|
|
118
|
+
expect(req.headers.get('X-Request-Id')).toBe('123')
|
|
119
|
+
expect(req.headers.get('Accept-Encoding')).toBeNull()
|
|
120
|
+
|
|
121
|
+
expect(res.status).toBe(200)
|
|
122
|
+
expect(res.headers.get('X-Response-Id')).toBe('456')
|
|
123
|
+
expect(res.headers.get('Content-Length')).toBe('2')
|
|
124
|
+
expect(res.headers.get('Content-Range')).toBe('bytes 0-2/1024')
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it('POST request', async () => {
|
|
128
|
+
const app = new Acryl()
|
|
129
|
+
app.all('/proxy/:path', (c) => {
|
|
130
|
+
return proxy(`https://example.com/${c.req.param('path')}`, {
|
|
131
|
+
...c.req,
|
|
132
|
+
headers: {
|
|
133
|
+
...c.req.header(),
|
|
134
|
+
'X-Request-Id': '123',
|
|
135
|
+
'Accept-Encoding': 'gzip',
|
|
136
|
+
},
|
|
137
|
+
})
|
|
138
|
+
})
|
|
139
|
+
const res = await app.request('/proxy/post', {
|
|
140
|
+
method: 'POST',
|
|
141
|
+
body: 'test',
|
|
142
|
+
})
|
|
143
|
+
const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
|
|
144
|
+
|
|
145
|
+
expect(req.url).toBe('https://example.com/post')
|
|
146
|
+
|
|
147
|
+
expect(res.status).toBe(200)
|
|
148
|
+
expect(await res.text()).toBe('request body: test')
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it('remove hop-by-hop headers', async () => {
|
|
152
|
+
const app = new Acryl()
|
|
153
|
+
app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`, c.req))
|
|
154
|
+
|
|
155
|
+
const res = await app.request('/proxy/hop-by-hop', {
|
|
156
|
+
headers: {
|
|
157
|
+
Host: 'example.com',
|
|
158
|
+
Connection: 'keep-alive, custom-header',
|
|
159
|
+
'Keep-Alive': 'timeout=5, max=1000',
|
|
160
|
+
'Proxy-Authorization': 'Basic 123456',
|
|
161
|
+
'Custom-Header': 'test',
|
|
162
|
+
'Allowed-Custom-Header': 'test',
|
|
163
|
+
},
|
|
164
|
+
})
|
|
165
|
+
const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
|
|
166
|
+
|
|
167
|
+
expect(req.headers.get('Host')).toBe('example.com')
|
|
168
|
+
expect(req.headers.get('Connection')).toBeNull()
|
|
169
|
+
expect(req.headers.get('Keep-Alive')).toBeNull()
|
|
170
|
+
expect(req.headers.get('Proxy-Authorization')).toBeNull()
|
|
171
|
+
expect(req.headers.get('Custom-Header')).toBe('test')
|
|
172
|
+
expect(req.headers.get('Allowed-Custom-Header')).toBe('test')
|
|
173
|
+
|
|
174
|
+
expect(res.headers.get('Transfer-Encoding')).toBeNull()
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
it('remove hop-by-hop headers with strictConnectionProcessing', async () => {
|
|
178
|
+
const app = new Acryl()
|
|
179
|
+
app.get('/proxy/:path', (c) =>
|
|
180
|
+
proxy(`https://example.com/${c.req.param('path')}`, {
|
|
181
|
+
...c.req,
|
|
182
|
+
strictConnectionProcessing: true,
|
|
183
|
+
})
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
const res = await app.request('/proxy/hop-by-hop', {
|
|
187
|
+
headers: {
|
|
188
|
+
Host: 'example.com',
|
|
189
|
+
Connection: 'keep-alive, custom-header',
|
|
190
|
+
'Keep-Alive': 'timeout=5, max=1000',
|
|
191
|
+
'Proxy-Authorization': 'Basic 123456',
|
|
192
|
+
'Custom-Header': 'test',
|
|
193
|
+
'Allowed-Custom-Header': 'test',
|
|
194
|
+
},
|
|
195
|
+
})
|
|
196
|
+
const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
|
|
197
|
+
|
|
198
|
+
expect(req.headers.get('Host')).toBe('example.com')
|
|
199
|
+
expect(req.headers.get('Connection')).toBeNull()
|
|
200
|
+
expect(req.headers.get('Keep-Alive')).toBeNull()
|
|
201
|
+
expect(req.headers.get('Proxy-Authorization')).toBeNull()
|
|
202
|
+
expect(req.headers.get('Custom-Header')).toBeNull()
|
|
203
|
+
expect(req.headers.get('Allowed-Custom-Header')).toBe('test')
|
|
204
|
+
|
|
205
|
+
expect(res.headers.get('Transfer-Encoding')).toBeNull()
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
it('invalid hop-by-hop headers with strictConnectionProcessing', async () => {
|
|
209
|
+
const app = new Acryl()
|
|
210
|
+
app.get('/proxy/:path', (c) =>
|
|
211
|
+
proxy(`https://example.com/${c.req.param('path')}`, {
|
|
212
|
+
...c.req,
|
|
213
|
+
strictConnectionProcessing: true,
|
|
214
|
+
})
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
const res = await app.request('/proxy/hop-by-hop', {
|
|
218
|
+
headers: {
|
|
219
|
+
Host: 'example.com',
|
|
220
|
+
Connection: 'keep-alive, invalid-header invalid-header',
|
|
221
|
+
'Keep-Alive': 'timeout=5, max=1000',
|
|
222
|
+
},
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
expect(res.status).toBe(400)
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
it('specify hop-by-hop header by options', async () => {
|
|
229
|
+
const app = new Acryl()
|
|
230
|
+
app.get('/proxy/:path', (c) =>
|
|
231
|
+
proxy(`https://example.com/${c.req.param('path')}`, {
|
|
232
|
+
headers: {
|
|
233
|
+
'Proxy-Authorization': 'Basic 123456',
|
|
234
|
+
},
|
|
235
|
+
})
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
const res = await app.request('/proxy/hop-by-hop')
|
|
239
|
+
const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
|
|
240
|
+
|
|
241
|
+
expect(req.headers.get('Proxy-Authorization')).toBe('Basic 123456')
|
|
242
|
+
|
|
243
|
+
expect(res.headers.get('Transfer-Encoding')).toBeNull()
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
it('remove response headers listed in the Connection header', async () => {
|
|
247
|
+
const app = new Acryl()
|
|
248
|
+
app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`))
|
|
249
|
+
const res = await app.request('/proxy/connection-listed')
|
|
250
|
+
expect(res.status).toBe(200)
|
|
251
|
+
expect(res.headers.get('Connection')).toBeNull()
|
|
252
|
+
expect(res.headers.get('X-Connection-Scoped')).toBeNull()
|
|
253
|
+
expect(res.headers.get('X-Normal')).toBe('kept')
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
it('modify header', async () => {
|
|
257
|
+
const app = new Acryl()
|
|
258
|
+
app.get('/proxy/:path', (c) =>
|
|
259
|
+
proxy(`https://example.com/${c.req.param('path')}`, {
|
|
260
|
+
headers: {
|
|
261
|
+
'Set-Cookie': 'test=123',
|
|
262
|
+
},
|
|
263
|
+
}).then((res) => {
|
|
264
|
+
res.headers.delete('Set-Cookie')
|
|
265
|
+
res.headers.set('X-Response-Id', '456')
|
|
266
|
+
return res
|
|
267
|
+
})
|
|
268
|
+
)
|
|
269
|
+
const res = await app.request('/proxy/set-cookie')
|
|
270
|
+
expect(res.headers.get('Set-Cookie')).toBeNull()
|
|
271
|
+
expect(res.headers.get('X-Response-Id')).toBe('456')
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
it('does not propagate undefined request headers', async () => {
|
|
275
|
+
const app = new Acryl()
|
|
276
|
+
app.get('/proxy/:path', (c) =>
|
|
277
|
+
proxy(`https://example.com/${c.req.param('path')}`, {
|
|
278
|
+
headers: {
|
|
279
|
+
...c.req.header(),
|
|
280
|
+
Authorization: undefined,
|
|
281
|
+
},
|
|
282
|
+
})
|
|
283
|
+
)
|
|
284
|
+
await app.request('/proxy/ok', {
|
|
285
|
+
headers: {
|
|
286
|
+
Authorization: 'Bearer 123',
|
|
287
|
+
},
|
|
288
|
+
})
|
|
289
|
+
const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
|
|
290
|
+
expect(req.headers.get('Authorization')).toBeNull()
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
it('client disconnect', async () => {
|
|
294
|
+
const app = new Acryl()
|
|
295
|
+
const controller = new AbortController()
|
|
296
|
+
app.post('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`, c.req))
|
|
297
|
+
const resPromise = app.request('/proxy/disconnect', {
|
|
298
|
+
method: 'POST',
|
|
299
|
+
body: 'test',
|
|
300
|
+
signal: controller.signal,
|
|
301
|
+
})
|
|
302
|
+
controller.abort('client disconnect')
|
|
303
|
+
const res = await resPromise
|
|
304
|
+
expect(await res.text()).toBe('client disconnect')
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
it('not found', async () => {
|
|
308
|
+
const app = new Acryl()
|
|
309
|
+
app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`))
|
|
310
|
+
const res = await app.request('/proxy/404')
|
|
311
|
+
expect(res.status).toBe(404)
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
it('pass a Request object to proxyInit', async () => {
|
|
315
|
+
const app = new Acryl()
|
|
316
|
+
app.get('/proxy/:path', (c) => {
|
|
317
|
+
const req = new Request(c.req.raw, {
|
|
318
|
+
headers: {
|
|
319
|
+
'X-Request-Id': '123',
|
|
320
|
+
'Accept-Encoding': 'gzip',
|
|
321
|
+
},
|
|
322
|
+
})
|
|
323
|
+
return proxy(`https://example.com/${c.req.param('path')}`, req)
|
|
324
|
+
})
|
|
325
|
+
const res = await app.request('/proxy/compressed')
|
|
326
|
+
const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
|
|
327
|
+
|
|
328
|
+
expect(req.url).toBe('https://example.com/compressed')
|
|
329
|
+
expect(req.headers.get('X-Request-Id')).toBe('123')
|
|
330
|
+
expect(req.headers.get('Accept-Encoding')).toBeNull()
|
|
331
|
+
|
|
332
|
+
expect(res.status).toBe(200)
|
|
333
|
+
expect(await res.text()).toBe('ok')
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
it('Should call the custom fetch method when specified', async () => {
|
|
337
|
+
const customFetch = vi.fn().mockImplementation(async (req: Request) => {
|
|
338
|
+
const text = await req.text()
|
|
339
|
+
return new Response('custom fetch response. message:' + text)
|
|
340
|
+
})
|
|
341
|
+
const app = new Acryl()
|
|
342
|
+
app.post('/', (c) => {
|
|
343
|
+
return proxy(`https://example.com/`, {
|
|
344
|
+
customFetch,
|
|
345
|
+
...c.req,
|
|
346
|
+
})
|
|
347
|
+
})
|
|
348
|
+
const res = await app.request('/', {
|
|
349
|
+
method: 'POST',
|
|
350
|
+
body: 'hi',
|
|
351
|
+
})
|
|
352
|
+
expect(res.status).toBe(200)
|
|
353
|
+
expect(await res.text()).toBe('custom fetch response. message:hi')
|
|
354
|
+
})
|
|
355
|
+
})
|
|
356
|
+
})
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
* Proxy Helper for Acryl.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { HTTPException } from '../../http-exception.ts'
|
|
7
|
+
import type { RequestHeader } from '../../utils/headers.ts'
|
|
8
|
+
import type { HeadersInit } from '../../utils/types.ts'
|
|
9
|
+
|
|
10
|
+
// https://datatracker.ietf.org/doc/html/rfc2616#section-13.5.1
|
|
11
|
+
const hopByHopHeaders = [
|
|
12
|
+
'connection',
|
|
13
|
+
'keep-alive',
|
|
14
|
+
'proxy-authenticate',
|
|
15
|
+
'proxy-authorization',
|
|
16
|
+
'te',
|
|
17
|
+
'trailer',
|
|
18
|
+
'transfer-encoding',
|
|
19
|
+
'upgrade',
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
// https://datatracker.ietf.org/doc/html/rfc9110#section-5.6.2
|
|
23
|
+
const ALLOWED_TOKEN_PATTERN = /^[!#$%&'*+\-.0-9A-Z^_`a-z|~]+$/
|
|
24
|
+
|
|
25
|
+
interface ProxyRequestInit extends Omit<RequestInit, 'headers'> {
|
|
26
|
+
raw?: Request
|
|
27
|
+
headers?:
|
|
28
|
+
| HeadersInit
|
|
29
|
+
| [string, string][]
|
|
30
|
+
| Record<RequestHeader, string | undefined>
|
|
31
|
+
| Record<string, string | undefined>
|
|
32
|
+
customFetch?: (request: Request) => Promise<Response>
|
|
33
|
+
/**
|
|
34
|
+
* Enable strict RFC 9110 compliance for Connection header processing.
|
|
35
|
+
*
|
|
36
|
+
* - `false` (default): Ignores Connection header to prevent potential
|
|
37
|
+
* Hop-by-Hop Header Injection attacks. Recommended for untrusted clients.
|
|
38
|
+
* - `true`: Processes Connection header per RFC 9110 and removes listed headers.
|
|
39
|
+
* Only use in trusted environments.
|
|
40
|
+
*
|
|
41
|
+
* @default false
|
|
42
|
+
* @see https://datatracker.ietf.org/doc/html/rfc9110#section-7.6.1
|
|
43
|
+
*/
|
|
44
|
+
strictConnectionProcessing?: boolean
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface ProxyFetch {
|
|
48
|
+
(input: string | URL | Request, init?: ProxyRequestInit): Promise<Response>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const buildRequestInitFromRequest = (
|
|
52
|
+
request: Request | undefined,
|
|
53
|
+
strictConnectionProcessing: boolean
|
|
54
|
+
): RequestInit & { duplex?: 'half' } => {
|
|
55
|
+
if (!request) {
|
|
56
|
+
return {}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const headers = new Headers(request.headers)
|
|
60
|
+
|
|
61
|
+
if (strictConnectionProcessing) {
|
|
62
|
+
// https://datatracker.ietf.org/doc/html/rfc9110#section-7.6.1
|
|
63
|
+
// Parse Connection header and remove listed headers (MUST per RFC 9110)
|
|
64
|
+
const connectionValue = headers.get('connection')
|
|
65
|
+
if (connectionValue) {
|
|
66
|
+
const headerNames = connectionValue.split(',').map((h) => h.trim())
|
|
67
|
+
// Validate header names per RFC 9110 Section 5.6.2 (token syntax)
|
|
68
|
+
const invalidHeaders = headerNames.filter((h) => !ALLOWED_TOKEN_PATTERN.test(h))
|
|
69
|
+
|
|
70
|
+
if (invalidHeaders.length > 0) {
|
|
71
|
+
throw new HTTPException(400, {
|
|
72
|
+
message: `Invalid Connection header value: ${invalidHeaders.join(', ')}`,
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
headerNames.forEach((headerName) => {
|
|
76
|
+
headers.delete(headerName)
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
hopByHopHeaders.forEach((header) => {
|
|
82
|
+
headers.delete(header)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
method: request.method,
|
|
87
|
+
body: request.body,
|
|
88
|
+
duplex: request.body ? 'half' : undefined,
|
|
89
|
+
headers,
|
|
90
|
+
signal: request.signal,
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const preprocessRequestInit = (requestInit: RequestInit): RequestInit => {
|
|
95
|
+
if (
|
|
96
|
+
!requestInit.headers ||
|
|
97
|
+
Array.isArray(requestInit.headers) ||
|
|
98
|
+
requestInit.headers instanceof Headers
|
|
99
|
+
) {
|
|
100
|
+
return requestInit
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const headers = new Headers()
|
|
104
|
+
for (const [key, value] of Object.entries(requestInit.headers)) {
|
|
105
|
+
if (value == null) {
|
|
106
|
+
// delete header if value is null or undefined
|
|
107
|
+
headers.delete(key)
|
|
108
|
+
} else {
|
|
109
|
+
headers.set(key, value)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
requestInit.headers = headers
|
|
113
|
+
return requestInit
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Fetch API wrapper for proxy.
|
|
118
|
+
* The parameters and return value are the same as for `fetch` (except for the proxy-specific options).
|
|
119
|
+
*
|
|
120
|
+
* The "Accept-Encoding" header is replaced with an encoding that the current runtime can handle.
|
|
121
|
+
* Unnecessary response headers are deleted and a Response object is returned that can be returned
|
|
122
|
+
* as is as a response from the handler.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* app.get('/proxy/:path', (c) => {
|
|
127
|
+
* return proxy(`http://${originServer}/${c.req.param('path')}`, {
|
|
128
|
+
* headers: {
|
|
129
|
+
* ...c.req.header(), // optional, specify only when forwarding all the request data (including credentials) is necessary.
|
|
130
|
+
* 'X-Forwarded-For': '127.0.0.1',
|
|
131
|
+
* 'X-Forwarded-Host': c.req.header('host'),
|
|
132
|
+
* Authorization: undefined, // do not propagate request headers contained in c.req.header('Authorization')
|
|
133
|
+
* },
|
|
134
|
+
* }).then((res) => {
|
|
135
|
+
* res.headers.delete('Set-Cookie')
|
|
136
|
+
* return res
|
|
137
|
+
* })
|
|
138
|
+
* })
|
|
139
|
+
*
|
|
140
|
+
* app.all('/proxy/:path', (c) => {
|
|
141
|
+
* return proxy(`http://${originServer}/${c.req.param('path')}`, {
|
|
142
|
+
* ...c.req, // optional, specify only when forwarding all the request data (including credentials) is necessary.
|
|
143
|
+
* headers: {
|
|
144
|
+
* ...c.req.header(),
|
|
145
|
+
* 'X-Forwarded-For': '127.0.0.1',
|
|
146
|
+
* 'X-Forwarded-Host': c.req.header('host'),
|
|
147
|
+
* Authorization: undefined, // do not propagate request headers contained in c.req.header('Authorization')
|
|
148
|
+
* },
|
|
149
|
+
* })
|
|
150
|
+
* })
|
|
151
|
+
*
|
|
152
|
+
* // Strict RFC compliance mode (use only in trusted environments)
|
|
153
|
+
* app.get('/internal-proxy/:path', (c) => {
|
|
154
|
+
* return proxy(`http://${internalServer}/${c.req.param('path')}`, {
|
|
155
|
+
* ...c.req,
|
|
156
|
+
* strictConnectionProcessing: true,
|
|
157
|
+
* })
|
|
158
|
+
* })
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
export const proxy: ProxyFetch = async (input, proxyInit) => {
|
|
162
|
+
const {
|
|
163
|
+
raw,
|
|
164
|
+
customFetch,
|
|
165
|
+
strictConnectionProcessing = false,
|
|
166
|
+
...requestInit
|
|
167
|
+
} = proxyInit instanceof Request ? { raw: proxyInit } : (proxyInit ?? {})
|
|
168
|
+
|
|
169
|
+
const req = new Request(input, {
|
|
170
|
+
...buildRequestInitFromRequest(raw, strictConnectionProcessing),
|
|
171
|
+
...preprocessRequestInit(requestInit as RequestInit),
|
|
172
|
+
})
|
|
173
|
+
req.headers.delete('accept-encoding')
|
|
174
|
+
|
|
175
|
+
const res = await (customFetch || fetch)(req)
|
|
176
|
+
const resHeaders = new Headers(res.headers)
|
|
177
|
+
|
|
178
|
+
// https://datatracker.ietf.org/doc/html/rfc9110#section-7.6.1
|
|
179
|
+
// Remove headers listed in the response's own Connection header (MUST per RFC 9110)
|
|
180
|
+
const connectionValue = resHeaders.get('connection')
|
|
181
|
+
if (connectionValue) {
|
|
182
|
+
connectionValue
|
|
183
|
+
.split(',')
|
|
184
|
+
.map((h) => h.trim())
|
|
185
|
+
.filter((h) => ALLOWED_TOKEN_PATTERN.test(h))
|
|
186
|
+
.forEach((h) => resHeaders.delete(h))
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
hopByHopHeaders.forEach((header) => {
|
|
190
|
+
resHeaders.delete(header)
|
|
191
|
+
})
|
|
192
|
+
if (resHeaders.has('content-encoding')) {
|
|
193
|
+
resHeaders.delete('content-encoding')
|
|
194
|
+
// Content-Length is the size of the compressed content, not the size of the original content
|
|
195
|
+
resHeaders.delete('content-length')
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return new Response(res.body, {
|
|
199
|
+
status: res.status,
|
|
200
|
+
statusText: res.statusText,
|
|
201
|
+
headers: resHeaders,
|
|
202
|
+
})
|
|
203
|
+
}
|