acrylblends 0.6.1 → 0.6.3

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.
Files changed (217) hide show
  1. package/README.md +11 -6
  2. package/docs/framework/README.md +3 -2
  3. package/docs/framework/UPSTREAM.md +1 -1
  4. package/docs/framework/api/presets.md +0 -3
  5. package/docs/framework/cordis/acryl-in-a-cordis-app.md +9 -0
  6. package/docs/framework/getting-started/basic.md +5 -4
  7. package/docs/framework/getting-started/desktop.md +41 -0
  8. package/docs/framework/guides/jsx-dom.md +0 -3
  9. package/docs/framework/guides/jsx.md +1 -4
  10. package/docs/framework/guides/middleware.md +1 -1
  11. package/docs/framework/helpers/cookie.md +1 -1
  12. package/docs/framework/helpers/css.md +0 -3
  13. package/docs/framework/helpers/dev.md +0 -3
  14. package/docs/framework/helpers/jwt.md +1 -1
  15. package/docs/framework/helpers/proxy.md +0 -3
  16. package/docs/framework/middleware/builtin/basic-auth.md +6 -6
  17. package/docs/framework/middleware/builtin/cache.md +0 -3
  18. package/docs/framework/middleware/builtin/combine.md +1 -1
  19. package/docs/framework/middleware/builtin/ip-restriction.md +1 -1
  20. package/docs/framework/middleware/builtin/logger.md +1 -1
  21. package/docs/framework/middleware/builtin/mount.md +0 -3
  22. package/docs/framework/upstream.json +3 -3
  23. package/lib/accepts.d.ts +1 -1
  24. package/lib/acryl-base.d.ts +1 -1
  25. package/lib/adapter.d.ts +1 -1
  26. package/lib/{app-CmAYW1Al.d.ts → app-wFwpg-bt.d.ts} +1 -1
  27. package/lib/base-BpQy2Rk7.d.ts +783 -0
  28. package/lib/base-ddiVmiE7.js +721 -0
  29. package/lib/basic-auth.d.ts +1 -1
  30. package/lib/bearer-auth.d.ts +1 -1
  31. package/lib/body-limit.d.ts +1 -1
  32. package/lib/cache.d.ts +43 -0
  33. package/lib/cache.js +169 -0
  34. package/lib/client.d.ts +2 -2
  35. package/lib/combine.d.ts +1 -1
  36. package/lib/common-D8Z4JRCw.d.ts +46 -0
  37. package/lib/components-Ce5FRyHU.js +268 -0
  38. package/lib/components-DYZdxJIJ.js +24 -0
  39. package/lib/compress.d.ts +1 -1
  40. package/lib/conninfo.d.ts +2 -2
  41. package/lib/constants-BR9jM9BC.js +9 -0
  42. package/lib/{context-Bm7rI-oK.d.ts → context-DVQBjuns.d.ts} +1 -1
  43. package/lib/context-DxFdY_un.d.ts +250 -0
  44. package/lib/context-storage.d.ts +1 -1
  45. package/lib/{cookie-ClMaKDQf.d.ts → cookie-CCelEqky.d.ts} +1 -1
  46. package/lib/cookie.d.ts +3 -3
  47. package/lib/cors.d.ts +1 -1
  48. package/lib/csrf.d.ts +1 -1
  49. package/lib/css-D2jb7X8b.js +320 -0
  50. package/lib/css.d.ts +82 -0
  51. package/lib/css.js +134 -0
  52. package/lib/dev.d.ts +19 -0
  53. package/lib/dev.js +50 -0
  54. package/lib/etag.d.ts +1 -1
  55. package/lib/factory.d.ts +2 -2
  56. package/lib/hooks-Bq5q9IMl.js +762 -0
  57. package/lib/html-SllAe0YT.d.ts +46 -0
  58. package/lib/html.d.ts +1 -1
  59. package/lib/index-C850MeBA.d.ts +94 -0
  60. package/lib/{index-oOClm_Xk.d.ts → index-CUTfU69R.d.ts} +3 -3
  61. package/lib/{index-B-3F4wt2.d.ts → index-Cqcdluyn.d.ts} +2 -2
  62. package/lib/{index-UTBmM3GX.d.ts → index-CsvB-F9j.d.ts} +2 -2
  63. package/lib/index.d.ts +4 -4
  64. package/lib/ip-restriction.d.ts +2 -2
  65. package/lib/jsx/dom/client.d.ts +30 -0
  66. package/lib/jsx/dom/client.js +49 -0
  67. package/lib/jsx/dom/css.d.ts +84 -0
  68. package/lib/jsx/dom/css.js +2 -0
  69. package/lib/jsx/dom/jsx-dev-runtime.d.ts +3 -0
  70. package/lib/jsx/dom/jsx-dev-runtime.js +2 -0
  71. package/lib/jsx/dom/jsx-runtime.d.ts +3 -0
  72. package/lib/jsx/dom/jsx-runtime.js +2 -0
  73. package/lib/jsx/dom/server.d.ts +105 -0
  74. package/lib/jsx/dom/server.js +37 -0
  75. package/lib/jsx/dom.d.ts +5 -0
  76. package/lib/jsx/dom.js +74 -0
  77. package/lib/jsx/jsx-dev-runtime.d.ts +6 -0
  78. package/lib/jsx/jsx-dev-runtime.js +14 -0
  79. package/lib/jsx/jsx-runtime.d.ts +10 -0
  80. package/lib/jsx/jsx-runtime.js +30 -0
  81. package/lib/jsx/streaming.d.ts +2 -0
  82. package/lib/jsx/streaming.js +2 -0
  83. package/lib/jsx-dev-runtime-B22O-Npk.d.ts +7 -0
  84. package/lib/jsx-dev-runtime-kAhR_4C7.js +354 -0
  85. package/lib/jsx-runtime-BVVgDSdq.d.ts +1 -0
  86. package/lib/jsx.d.ts +3 -0
  87. package/lib/jsx.js +47 -0
  88. package/lib/jwk.d.ts +4 -4
  89. package/lib/{jwt-Dg493Tai.d.ts → jwt-BARhotDz.d.ts} +1 -1
  90. package/lib/jwt.d.ts +4 -4
  91. package/lib/language.d.ts +1 -1
  92. package/lib/linear-router-C3FPib6h.js +96 -0
  93. package/lib/logger.d.ts +1 -1
  94. package/lib/loopback-only.d.ts +1 -1
  95. package/lib/method-not-allowed.d.ts +2 -2
  96. package/lib/method-override.d.ts +2 -2
  97. package/lib/mount.d.ts +44 -0
  98. package/lib/mount.js +67 -0
  99. package/lib/node-server/conninfo.d.ts +1 -1
  100. package/lib/node-server/serve-static.d.ts +1 -1
  101. package/lib/node-server/serve-static.js +1 -1
  102. package/lib/node-server.d.ts +1 -1
  103. package/lib/node-server.js +1 -1
  104. package/lib/pattern-router-HZxjDGEQ.js +41 -0
  105. package/lib/powered-by.d.ts +1 -1
  106. package/lib/pretty-json.d.ts +1 -1
  107. package/lib/proxy.d.ts +72 -0
  108. package/lib/proxy.js +121 -0
  109. package/lib/quick.d.ts +8 -0
  110. package/lib/quick.js +17 -0
  111. package/lib/request-id.d.ts +1 -1
  112. package/lib/request.d.ts +1 -1
  113. package/lib/route.d.ts +1 -1
  114. package/lib/router/linear-router.js +1 -95
  115. package/lib/router/pattern-router.js +1 -40
  116. package/lib/schema-validator.d.ts +1 -1
  117. package/lib/secure-headers.d.ts +1 -1
  118. package/lib/{serve-static-Dw_m6KxR.js → serve-static-Cw23shXv.js} +1 -0
  119. package/lib/serve-static.d.ts +1 -1
  120. package/lib/streaming.d.ts +1 -1
  121. package/lib/testing.d.ts +4 -4
  122. package/lib/timeout.d.ts +1 -1
  123. package/lib/timing.d.ts +1 -1
  124. package/lib/tiny.d.ts +8 -0
  125. package/lib/tiny.js +15 -0
  126. package/lib/trailing-slash.d.ts +1 -1
  127. package/lib/{types-CHesD1KG.d.ts → types-CQiGmsWW.d.ts} +3 -3
  128. package/lib/{types-CwrQjlaM.d.ts → types-Lqx5BxQF.d.ts} +10 -1
  129. package/lib/{types-CDO6I3B0.d.ts → types-nEL8VPDC.d.ts} +1 -1
  130. package/lib/types.d.ts +1 -1
  131. package/lib/utils/body.d.ts +1 -1
  132. package/lib/utils/cookie.d.ts +1 -1
  133. package/lib/utils/crypto.d.ts +1 -1
  134. package/lib/utils/html.d.ts +1 -45
  135. package/lib/utils/ipaddr.d.ts +1 -1
  136. package/lib/utils/jwt.d.ts +1 -1
  137. package/lib/utils/types.d.ts +2 -2
  138. package/lib/validator.d.ts +2 -2
  139. package/lib/ws.d.ts +1 -1
  140. package/package.json +69 -1
  141. package/plugins/acryl-inspect/upstream.json +1 -1
  142. package/plugins/acryl-node-server/upstream.json +1 -1
  143. package/plugins/acryl-router/src/core/helper/css/common.case.test.tsx +506 -0
  144. package/plugins/acryl-router/src/core/helper/css/common.ts +337 -0
  145. package/plugins/acryl-router/src/core/helper/css/index.test.tsx +678 -0
  146. package/plugins/acryl-router/src/core/helper/css/index.ts +262 -0
  147. package/plugins/acryl-router/src/core/helper/dev/index.test.ts +181 -0
  148. package/plugins/acryl-router/src/core/helper/dev/index.ts +79 -0
  149. package/plugins/acryl-router/src/core/helper/proxy/index.test.ts +356 -0
  150. package/plugins/acryl-router/src/core/helper/proxy/index.ts +203 -0
  151. package/plugins/acryl-router/src/core/helper/streaming/sse.test.tsx +557 -0
  152. package/plugins/acryl-router/src/core/jsx/base.test.tsx +97 -0
  153. package/plugins/acryl-router/src/core/jsx/base.ts +490 -0
  154. package/plugins/acryl-router/src/core/jsx/children.test.ts +52 -0
  155. package/plugins/acryl-router/src/core/jsx/children.ts +20 -0
  156. package/plugins/acryl-router/src/core/jsx/components.test.tsx +936 -0
  157. package/plugins/acryl-router/src/core/jsx/components.ts +257 -0
  158. package/plugins/acryl-router/src/core/jsx/constants.ts +6 -0
  159. package/plugins/acryl-router/src/core/jsx/context-isolation.test.ts +655 -0
  160. package/plugins/acryl-router/src/core/jsx/context.ts +266 -0
  161. package/plugins/acryl-router/src/core/jsx/dom/client.test.tsx +128 -0
  162. package/plugins/acryl-router/src/core/jsx/dom/client.ts +89 -0
  163. package/plugins/acryl-router/src/core/jsx/dom/components.test.tsx +330 -0
  164. package/plugins/acryl-router/src/core/jsx/dom/components.ts +39 -0
  165. package/plugins/acryl-router/src/core/jsx/dom/context.test.tsx +184 -0
  166. package/plugins/acryl-router/src/core/jsx/dom/context.ts +52 -0
  167. package/plugins/acryl-router/src/core/jsx/dom/css.test.tsx +149 -0
  168. package/plugins/acryl-router/src/core/jsx/dom/css.ts +263 -0
  169. package/plugins/acryl-router/src/core/jsx/dom/hooks/index.test.tsx +172 -0
  170. package/plugins/acryl-router/src/core/jsx/dom/hooks/index.ts +91 -0
  171. package/plugins/acryl-router/src/core/jsx/dom/index.test.tsx +3681 -0
  172. package/plugins/acryl-router/src/core/jsx/dom/index.ts +170 -0
  173. package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.test.tsx +1344 -0
  174. package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.ts +438 -0
  175. package/plugins/acryl-router/src/core/jsx/dom/jsx-dev-runtime.ts +23 -0
  176. package/plugins/acryl-router/src/core/jsx/dom/jsx-runtime.ts +8 -0
  177. package/plugins/acryl-router/src/core/jsx/dom/render.ts +879 -0
  178. package/plugins/acryl-router/src/core/jsx/dom/server.test.tsx +199 -0
  179. package/plugins/acryl-router/src/core/jsx/dom/server.ts +76 -0
  180. package/plugins/acryl-router/src/core/jsx/dom/utils.ts +7 -0
  181. package/plugins/acryl-router/src/core/jsx/hooks/dom.test.tsx +859 -0
  182. package/plugins/acryl-router/src/core/jsx/hooks/index.ts +437 -0
  183. package/plugins/acryl-router/src/core/jsx/hooks/string.test.tsx +40 -0
  184. package/plugins/acryl-router/src/core/jsx/index.test.tsx +1744 -0
  185. package/plugins/acryl-router/src/core/jsx/index.ts +115 -0
  186. package/plugins/acryl-router/src/core/jsx/intrinsic-element/common.ts +23 -0
  187. package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.test.tsx +666 -0
  188. package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.ts +210 -0
  189. package/plugins/acryl-router/src/core/jsx/intrinsic-elements.ts +938 -0
  190. package/plugins/acryl-router/src/core/jsx/jsx-dev-runtime.ts +26 -0
  191. package/plugins/acryl-router/src/core/jsx/jsx-runtime.test.tsx +85 -0
  192. package/plugins/acryl-router/src/core/jsx/jsx-runtime.ts +51 -0
  193. package/plugins/acryl-router/src/core/jsx/streaming.test.tsx +1281 -0
  194. package/plugins/acryl-router/src/core/jsx/streaming.ts +231 -0
  195. package/plugins/acryl-router/src/core/jsx/types.ts +41 -0
  196. package/plugins/acryl-router/src/core/jsx/utils.test.ts +366 -0
  197. package/plugins/acryl-router/src/core/jsx/utils.ts +202 -0
  198. package/plugins/acryl-router/src/core/middleware/cache/index.test.ts +1211 -0
  199. package/plugins/acryl-router/src/core/middleware/cache/index.ts +323 -0
  200. package/plugins/acryl-router/src/core/middleware/mount/index.test.ts +334 -0
  201. package/plugins/acryl-router/src/core/middleware/mount/index.ts +108 -0
  202. package/plugins/acryl-router/src/core/preset/quick.test.ts +42 -0
  203. package/plugins/acryl-router/src/core/preset/quick.ts +24 -0
  204. package/plugins/acryl-router/src/core/preset/tiny.test.ts +42 -0
  205. package/plugins/acryl-router/src/core/preset/tiny.ts +20 -0
  206. package/plugins/acryl-router/src/core/utils/types.ts +9 -0
  207. package/plugins/acryl-router/src/helpers/index.ts +3 -0
  208. package/plugins/acryl-router/src/middleware/index.ts +2 -0
  209. package/plugins/acryl-router/src/service.ts +22 -5
  210. package/plugins/acryl-router/tests/jsx.spec.tsx +63 -0
  211. package/plugins/acryl-router/upstream.json +3 -3
  212. package/plugins/acryl-router-http/upstream.json +1 -1
  213. package/plugins/acryl-state/upstream.json +1 -1
  214. package/plugins/desktop/upstream.json +1 -1
  215. package/plugins/desktop-web/upstream.json +1 -1
  216. package/plugins/save/plugin.json +1 -1
  217. 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
+ }