acrylblends 0.6.1 → 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.
Files changed (119) hide show
  1. package/README.md +8 -6
  2. package/docs/framework/UPSTREAM.md +1 -1
  3. package/docs/framework/api/presets.md +0 -3
  4. package/docs/framework/cordis/acryl-in-a-cordis-app.md +3 -0
  5. package/docs/framework/getting-started/basic.md +4 -4
  6. package/docs/framework/guides/jsx.md +1 -1
  7. package/docs/framework/guides/middleware.md +1 -1
  8. package/docs/framework/helpers/cookie.md +1 -1
  9. package/docs/framework/helpers/dev.md +0 -3
  10. package/docs/framework/helpers/jwt.md +1 -1
  11. package/docs/framework/helpers/proxy.md +0 -3
  12. package/docs/framework/middleware/builtin/basic-auth.md +6 -6
  13. package/docs/framework/middleware/builtin/cache.md +0 -3
  14. package/docs/framework/middleware/builtin/combine.md +1 -1
  15. package/docs/framework/middleware/builtin/ip-restriction.md +1 -1
  16. package/docs/framework/middleware/builtin/logger.md +1 -1
  17. package/docs/framework/middleware/builtin/mount.md +0 -3
  18. package/docs/framework/upstream.json +2 -2
  19. package/lib/accepts.d.ts +1 -1
  20. package/lib/acryl-base.d.ts +1 -1
  21. package/lib/adapter.d.ts +1 -1
  22. package/lib/{app-CmAYW1Al.d.ts → app-BdrVHBlJ.d.ts} +1 -1
  23. package/lib/basic-auth.d.ts +1 -1
  24. package/lib/bearer-auth.d.ts +1 -1
  25. package/lib/body-limit.d.ts +1 -1
  26. package/lib/cache.d.ts +43 -0
  27. package/lib/cache.js +169 -0
  28. package/lib/client.d.ts +2 -2
  29. package/lib/combine.d.ts +1 -1
  30. package/lib/compress.d.ts +1 -1
  31. package/lib/conninfo.d.ts +2 -2
  32. package/lib/{context-Bm7rI-oK.d.ts → context-B5Y1lLha.d.ts} +1 -1
  33. package/lib/context-storage.d.ts +1 -1
  34. package/lib/{cookie-ClMaKDQf.d.ts → cookie-D9_nQbsM.d.ts} +1 -1
  35. package/lib/cookie.d.ts +3 -3
  36. package/lib/cors.d.ts +1 -1
  37. package/lib/csrf.d.ts +1 -1
  38. package/lib/dev.d.ts +19 -0
  39. package/lib/dev.js +50 -0
  40. package/lib/etag.d.ts +1 -1
  41. package/lib/factory.d.ts +2 -2
  42. package/lib/{index-B-3F4wt2.d.ts → index-DtEoak1i.d.ts} +2 -2
  43. package/lib/{index-UTBmM3GX.d.ts → index-Kqou8Fjh.d.ts} +2 -2
  44. package/lib/{index-oOClm_Xk.d.ts → index-m12zcmph.d.ts} +3 -3
  45. package/lib/index.d.ts +4 -4
  46. package/lib/ip-restriction.d.ts +2 -2
  47. package/lib/jwk.d.ts +4 -4
  48. package/lib/{jwt-Dg493Tai.d.ts → jwt-BA65QhnJ.d.ts} +1 -1
  49. package/lib/jwt.d.ts +4 -4
  50. package/lib/language.d.ts +1 -1
  51. package/lib/linear-router-C3FPib6h.js +96 -0
  52. package/lib/logger.d.ts +1 -1
  53. package/lib/loopback-only.d.ts +1 -1
  54. package/lib/method-not-allowed.d.ts +2 -2
  55. package/lib/method-override.d.ts +2 -2
  56. package/lib/mount.d.ts +44 -0
  57. package/lib/mount.js +67 -0
  58. package/lib/node-server/conninfo.d.ts +1 -1
  59. package/lib/node-server/serve-static.d.ts +1 -1
  60. package/lib/node-server.d.ts +1 -1
  61. package/lib/pattern-router-HZxjDGEQ.js +41 -0
  62. package/lib/powered-by.d.ts +1 -1
  63. package/lib/pretty-json.d.ts +1 -1
  64. package/lib/proxy.d.ts +72 -0
  65. package/lib/proxy.js +121 -0
  66. package/lib/quick.d.ts +8 -0
  67. package/lib/quick.js +17 -0
  68. package/lib/request-id.d.ts +1 -1
  69. package/lib/request.d.ts +1 -1
  70. package/lib/route.d.ts +1 -1
  71. package/lib/router/linear-router.js +1 -95
  72. package/lib/router/pattern-router.js +1 -40
  73. package/lib/schema-validator.d.ts +1 -1
  74. package/lib/secure-headers.d.ts +1 -1
  75. package/lib/serve-static.d.ts +1 -1
  76. package/lib/streaming.d.ts +1 -1
  77. package/lib/testing.d.ts +4 -4
  78. package/lib/timeout.d.ts +1 -1
  79. package/lib/timing.d.ts +1 -1
  80. package/lib/tiny.d.ts +8 -0
  81. package/lib/tiny.js +15 -0
  82. package/lib/trailing-slash.d.ts +1 -1
  83. package/lib/{types-CHesD1KG.d.ts → types-Bd29gh2o.d.ts} +3 -3
  84. package/lib/{types-CwrQjlaM.d.ts → types-D9DEXMjE.d.ts} +3 -1
  85. package/lib/{types-CDO6I3B0.d.ts → types-ZM9gIiKh.d.ts} +1 -1
  86. package/lib/types.d.ts +1 -1
  87. package/lib/utils/body.d.ts +1 -1
  88. package/lib/utils/cookie.d.ts +1 -1
  89. package/lib/utils/crypto.d.ts +1 -1
  90. package/lib/utils/ipaddr.d.ts +1 -1
  91. package/lib/utils/jwt.d.ts +1 -1
  92. package/lib/utils/types.d.ts +2 -2
  93. package/lib/validator.d.ts +2 -2
  94. package/lib/ws.d.ts +1 -1
  95. package/package.json +25 -1
  96. package/plugins/acryl-inspect/upstream.json +1 -1
  97. package/plugins/acryl-node-server/upstream.json +1 -1
  98. package/plugins/acryl-router/src/core/helper/dev/index.test.ts +181 -0
  99. package/plugins/acryl-router/src/core/helper/dev/index.ts +79 -0
  100. package/plugins/acryl-router/src/core/helper/proxy/index.test.ts +356 -0
  101. package/plugins/acryl-router/src/core/helper/proxy/index.ts +203 -0
  102. package/plugins/acryl-router/src/core/middleware/cache/index.test.ts +1211 -0
  103. package/plugins/acryl-router/src/core/middleware/cache/index.ts +323 -0
  104. package/plugins/acryl-router/src/core/middleware/mount/index.test.ts +334 -0
  105. package/plugins/acryl-router/src/core/middleware/mount/index.ts +108 -0
  106. package/plugins/acryl-router/src/core/preset/quick.test.ts +42 -0
  107. package/plugins/acryl-router/src/core/preset/quick.ts +24 -0
  108. package/plugins/acryl-router/src/core/preset/tiny.test.ts +42 -0
  109. package/plugins/acryl-router/src/core/preset/tiny.ts +20 -0
  110. package/plugins/acryl-router/src/core/utils/types.ts +3 -0
  111. package/plugins/acryl-router/src/helpers/index.ts +2 -0
  112. package/plugins/acryl-router/src/middleware/index.ts +2 -0
  113. package/plugins/acryl-router/src/service.ts +22 -5
  114. package/plugins/acryl-router/upstream.json +2 -2
  115. package/plugins/acryl-router-http/upstream.json +1 -1
  116. package/plugins/acryl-state/upstream.json +1 -1
  117. package/plugins/desktop/upstream.json +1 -1
  118. package/plugins/desktop-web/upstream.json +1 -1
  119. package/templates/todo/upstream.json +1 -1
@@ -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
+ }