acrylblends 0.3.0 → 0.4.1

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 (341) hide show
  1. package/dist/index.d.ts +6 -1
  2. package/dist/index.js +84 -5
  3. package/dist/index.js.map +1 -1
  4. package/docs/framework/LICENSE +23 -0
  5. package/docs/framework/README.md +63 -0
  6. package/docs/framework/UPSTREAM.md +3 -0
  7. package/docs/framework/api-summary.md +138 -0
  8. package/docs/framework/concepts.md +105 -0
  9. package/docs/framework/context.md +129 -0
  10. package/docs/framework/getting-started.md +146 -0
  11. package/docs/framework/helpers.md +118 -0
  12. package/docs/framework/middleware.md +154 -0
  13. package/docs/framework/migration-from-0-3.md +108 -0
  14. package/docs/framework/migration-from-dsh-webserver.md +103 -0
  15. package/docs/framework/node-server.md +75 -0
  16. package/docs/framework/plugin-lifecycle.md +111 -0
  17. package/docs/framework/request.md +106 -0
  18. package/docs/framework/routing.md +188 -0
  19. package/docs/framework/surfaces.md +71 -0
  20. package/docs/framework/upstream.json +9 -0
  21. package/docs/framework/validation.md +135 -0
  22. package/package.json +4 -2
  23. package/plugins/acryl-inspect/plugin.json +2 -1
  24. package/plugins/acryl-inspect/src/report.ts +21 -13
  25. package/plugins/acryl-inspect/src/service.ts +9 -7
  26. package/plugins/acryl-inspect/tests/report.spec.ts +13 -8
  27. package/plugins/acryl-inspect/tests/service.spec.ts +11 -10
  28. package/plugins/acryl-inspect/upstream.json +2 -2
  29. package/plugins/acryl-node-server/LICENSE +26 -0
  30. package/plugins/acryl-node-server/plugin.json +2 -1
  31. package/plugins/acryl-node-server/src/adapter/conninfo.ts +23 -0
  32. package/plugins/acryl-node-server/src/adapter/early-hints.ts +55 -0
  33. package/plugins/acryl-node-server/src/adapter/error.ts +11 -0
  34. package/plugins/acryl-node-server/src/adapter/headers.ts +249 -0
  35. package/plugins/acryl-node-server/src/adapter/index.ts +6 -0
  36. package/plugins/acryl-node-server/src/adapter/listener.ts +540 -0
  37. package/plugins/acryl-node-server/src/adapter/request.ts +797 -0
  38. package/plugins/acryl-node-server/src/adapter/response.ts +186 -0
  39. package/plugins/acryl-node-server/src/adapter/serve-static.ts +238 -0
  40. package/plugins/acryl-node-server/src/adapter/server.ts +36 -0
  41. package/plugins/acryl-node-server/src/adapter/types.ts +82 -0
  42. package/plugins/acryl-node-server/src/adapter/url.ts +42 -0
  43. package/plugins/acryl-node-server/src/adapter/utils/response/constants.ts +1 -0
  44. package/plugins/acryl-node-server/src/adapter/utils/response.ts +4 -0
  45. package/plugins/acryl-node-server/src/adapter/utils/stream.ts +88 -0
  46. package/plugins/acryl-node-server/src/adapter/utils.ts +96 -0
  47. package/plugins/acryl-node-server/src/adapter/websocket-types.ts +45 -0
  48. package/plugins/acryl-node-server/src/adapter/websocket.ts +375 -0
  49. package/plugins/acryl-node-server/src/index.ts +19 -6
  50. package/plugins/acryl-node-server/tests/adapter/app.ts +76 -0
  51. package/plugins/acryl-node-server/tests/adapter/assets/.static/plain.txt +1 -0
  52. package/plugins/acryl-node-server/tests/adapter/assets/favicon.ico +0 -0
  53. package/plugins/acryl-node-server/tests/adapter/assets/secret.txt +1 -0
  54. package/plugins/acryl-node-server/tests/adapter/assets/static/acryl.html +1 -0
  55. package/plugins/acryl-node-server/tests/adapter/assets/static/admin/secret.txt +1 -0
  56. package/plugins/acryl-node-server/tests/adapter/assets/static/data.json +5 -0
  57. package/plugins/acryl-node-server/tests/adapter/assets/static/extensionless +1 -0
  58. package/plugins/acryl-node-server/tests/adapter/assets/static/foo..bar.txt +0 -0
  59. package/plugins/acryl-node-server/tests/adapter/assets/static/index.html +1 -0
  60. package/plugins/acryl-node-server/tests/adapter/assets/static/plain.txt +1 -0
  61. package/plugins/acryl-node-server/tests/adapter/assets/static-with-context-path/plain.txt +1 -0
  62. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.bin +1 -0
  63. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.bin.br +1 -0
  64. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt +1 -0
  65. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt.br +1 -0
  66. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt.zst +1 -0
  67. package/plugins/acryl-node-server/tests/adapter/conninfo.test.ts +34 -0
  68. package/plugins/acryl-node-server/tests/adapter/content-length.test.ts +248 -0
  69. package/plugins/acryl-node-server/tests/adapter/early-hints.test.ts +457 -0
  70. package/plugins/acryl-node-server/tests/adapter/fixtures/keys/agent1-cert.pem +23 -0
  71. package/plugins/acryl-node-server/tests/adapter/fixtures/keys/agent1-key.pem +27 -0
  72. package/plugins/acryl-node-server/tests/adapter/headers.test.ts +252 -0
  73. package/plugins/acryl-node-server/tests/adapter/helpers/request.ts +190 -0
  74. package/plugins/acryl-node-server/tests/adapter/listener.test.ts +727 -0
  75. package/plugins/acryl-node-server/tests/adapter/request-client-disconnect.test.ts +189 -0
  76. package/plugins/acryl-node-server/tests/adapter/request.test.ts +1230 -0
  77. package/plugins/acryl-node-server/tests/adapter/response.test.ts +285 -0
  78. package/plugins/acryl-node-server/tests/adapter/serve-static.test.ts +849 -0
  79. package/plugins/acryl-node-server/tests/adapter/server-socket.test.ts +790 -0
  80. package/plugins/acryl-node-server/tests/adapter/server.test.ts +1302 -0
  81. package/plugins/acryl-node-server/tests/adapter/setup.ts +16 -0
  82. package/plugins/acryl-node-server/tests/adapter/url.test.ts +76 -0
  83. package/plugins/acryl-node-server/tests/adapter/utils/response.test.ts +23 -0
  84. package/plugins/acryl-node-server/tests/adapter/utils/stream.test.ts +75 -0
  85. package/plugins/acryl-node-server/tests/adapter/utils.test.ts +235 -0
  86. package/plugins/acryl-node-server/tests/adapter/websocket.test.ts +260 -0
  87. package/plugins/acryl-node-server/tests/server.spec.ts +44 -8
  88. package/plugins/acryl-node-server/upstream.json +3 -3
  89. package/plugins/acryl-router/LICENSE +26 -0
  90. package/plugins/acryl-router/plugin.json +2 -1
  91. package/plugins/acryl-router/src/client.ts +2 -0
  92. package/plugins/acryl-router/src/connection.ts +42 -0
  93. package/plugins/acryl-router/src/core/app-base.ts +544 -0
  94. package/plugins/acryl-router/src/core/app.test.ts +3833 -0
  95. package/plugins/acryl-router/src/core/app.ts +34 -0
  96. package/plugins/acryl-router/src/core/client/client.test.ts +2313 -0
  97. package/plugins/acryl-router/src/core/client/client.ts +249 -0
  98. package/plugins/acryl-router/src/core/client/fetch-result-please.ts +95 -0
  99. package/plugins/acryl-router/src/core/client/index.ts +17 -0
  100. package/plugins/acryl-router/src/core/client/types.test.ts +149 -0
  101. package/plugins/acryl-router/src/core/client/types.ts +393 -0
  102. package/plugins/acryl-router/src/core/client/utils.test.ts +405 -0
  103. package/plugins/acryl-router/src/core/client/utils.ts +117 -0
  104. package/plugins/acryl-router/src/core/compose.test.ts +751 -0
  105. package/plugins/acryl-router/src/core/compose.ts +73 -0
  106. package/plugins/acryl-router/src/core/context.test.ts +629 -0
  107. package/plugins/acryl-router/src/core/context.ts +781 -0
  108. package/plugins/acryl-router/src/core/helper/accepts/accepts.test.ts +344 -0
  109. package/plugins/acryl-router/src/core/helper/accepts/accepts.ts +96 -0
  110. package/plugins/acryl-router/src/core/helper/accepts/index.ts +6 -0
  111. package/plugins/acryl-router/src/core/helper/adapter/index.test.ts +55 -0
  112. package/plugins/acryl-router/src/core/helper/adapter/index.ts +90 -0
  113. package/plugins/acryl-router/src/core/helper/conninfo/index.ts +6 -0
  114. package/plugins/acryl-router/src/core/helper/conninfo/types.ts +45 -0
  115. package/plugins/acryl-router/src/core/helper/cookie/index.test.ts +510 -0
  116. package/plugins/acryl-router/src/core/helper/cookie/index.ts +146 -0
  117. package/plugins/acryl-router/src/core/helper/factory/index.test.ts +434 -0
  118. package/plugins/acryl-router/src/core/helper/factory/index.ts +375 -0
  119. package/plugins/acryl-router/src/core/helper/html/index.test.ts +78 -0
  120. package/plugins/acryl-router/src/core/helper/html/index.ts +57 -0
  121. package/plugins/acryl-router/src/core/helper/route/index.test.ts +259 -0
  122. package/plugins/acryl-router/src/core/helper/route/index.ts +144 -0
  123. package/plugins/acryl-router/src/core/helper/streaming/index.ts +9 -0
  124. package/plugins/acryl-router/src/core/helper/streaming/sse.ts +101 -0
  125. package/plugins/acryl-router/src/core/helper/streaming/stream.test.ts +128 -0
  126. package/plugins/acryl-router/src/core/helper/streaming/stream.ts +45 -0
  127. package/plugins/acryl-router/src/core/helper/streaming/text.test.ts +34 -0
  128. package/plugins/acryl-router/src/core/helper/streaming/text.ts +15 -0
  129. package/plugins/acryl-router/src/core/helper/streaming/utils.ts +11 -0
  130. package/plugins/acryl-router/src/core/helper/testing/index.test.ts +42 -0
  131. package/plugins/acryl-router/src/core/helper/testing/index.ts +28 -0
  132. package/plugins/acryl-router/src/core/helper/websocket/index.test.ts +124 -0
  133. package/plugins/acryl-router/src/core/helper/websocket/index.ts +140 -0
  134. package/plugins/acryl-router/src/core/http-exception.test.ts +45 -0
  135. package/plugins/acryl-router/src/core/http-exception.ts +77 -0
  136. package/plugins/acryl-router/src/core/index.ts +53 -0
  137. package/plugins/acryl-router/src/core/middleware/basic-auth/index.test.ts +434 -0
  138. package/plugins/acryl-router/src/core/middleware/basic-auth/index.ts +152 -0
  139. package/plugins/acryl-router/src/core/middleware/bearer-auth/index.test.ts +1018 -0
  140. package/plugins/acryl-router/src/core/middleware/bearer-auth/index.ts +220 -0
  141. package/plugins/acryl-router/src/core/middleware/body-limit/index.test.ts +273 -0
  142. package/plugins/acryl-router/src/core/middleware/body-limit/index.ts +110 -0
  143. package/plugins/acryl-router/src/core/middleware/combine/index.test.ts +355 -0
  144. package/plugins/acryl-router/src/core/middleware/combine/index.ts +171 -0
  145. package/plugins/acryl-router/src/core/middleware/compress/index.test.ts +535 -0
  146. package/plugins/acryl-router/src/core/middleware/compress/index.ts +142 -0
  147. package/plugins/acryl-router/src/core/middleware/context-storage/index.test.ts +43 -0
  148. package/plugins/acryl-router/src/core/middleware/context-storage/index.ts +58 -0
  149. package/plugins/acryl-router/src/core/middleware/cors/index.test.ts +532 -0
  150. package/plugins/acryl-router/src/core/middleware/cors/index.ts +163 -0
  151. package/plugins/acryl-router/src/core/middleware/csrf/index.test.ts +534 -0
  152. package/plugins/acryl-router/src/core/middleware/csrf/index.ts +150 -0
  153. package/plugins/acryl-router/src/core/middleware/etag/digest.test.ts +21 -0
  154. package/plugins/acryl-router/src/core/middleware/etag/digest.ts +95 -0
  155. package/plugins/acryl-router/src/core/middleware/etag/index.test.ts +503 -0
  156. package/plugins/acryl-router/src/core/middleware/etag/index.ts +135 -0
  157. package/plugins/acryl-router/src/core/middleware/ip-restriction/index.test.ts +217 -0
  158. package/plugins/acryl-router/src/core/middleware/ip-restriction/index.ts +278 -0
  159. package/plugins/acryl-router/src/core/middleware/jwk/index.test.ts +1135 -0
  160. package/plugins/acryl-router/src/core/middleware/jwk/index.ts +1 -0
  161. package/plugins/acryl-router/src/core/middleware/jwk/jwk.ts +195 -0
  162. package/plugins/acryl-router/src/core/middleware/jwk/keys.test.json +48 -0
  163. package/plugins/acryl-router/src/core/middleware/jwt/index.test.ts +802 -0
  164. package/plugins/acryl-router/src/core/middleware/jwt/index.ts +9 -0
  165. package/plugins/acryl-router/src/core/middleware/jwt/jwt.ts +189 -0
  166. package/plugins/acryl-router/src/core/middleware/language/index.test.ts +552 -0
  167. package/plugins/acryl-router/src/core/middleware/language/index.ts +12 -0
  168. package/plugins/acryl-router/src/core/middleware/language/language.ts +314 -0
  169. package/plugins/acryl-router/src/core/middleware/logger/index.test.ts +204 -0
  170. package/plugins/acryl-router/src/core/middleware/logger/index.ts +95 -0
  171. package/plugins/acryl-router/src/core/middleware/method-not-allowed/index.test.ts +292 -0
  172. package/plugins/acryl-router/src/core/middleware/method-not-allowed/index.ts +141 -0
  173. package/plugins/acryl-router/src/core/middleware/method-override/index.test.ts +221 -0
  174. package/plugins/acryl-router/src/core/middleware/method-override/index.ts +147 -0
  175. package/plugins/acryl-router/src/core/middleware/powered-by/index.test.ts +37 -0
  176. package/plugins/acryl-router/src/core/middleware/powered-by/index.ts +35 -0
  177. package/plugins/acryl-router/src/core/middleware/pretty-json/index.test.ts +218 -0
  178. package/plugins/acryl-router/src/core/middleware/pretty-json/index.ts +67 -0
  179. package/plugins/acryl-router/src/core/middleware/request-id/index.test.ts +168 -0
  180. package/plugins/acryl-router/src/core/middleware/request-id/index.ts +7 -0
  181. package/plugins/acryl-router/src/core/middleware/request-id/request-id.ts +59 -0
  182. package/plugins/acryl-router/src/core/middleware/secure-headers/index.test.ts +671 -0
  183. package/plugins/acryl-router/src/core/middleware/secure-headers/index.ts +8 -0
  184. package/plugins/acryl-router/src/core/middleware/secure-headers/permissions-policy.ts +101 -0
  185. package/plugins/acryl-router/src/core/middleware/secure-headers/secure-headers.ts +340 -0
  186. package/plugins/acryl-router/src/core/middleware/serve-static/index.test.ts +450 -0
  187. package/plugins/acryl-router/src/core/middleware/serve-static/index.ts +135 -0
  188. package/plugins/acryl-router/src/core/middleware/serve-static/path.test.ts +50 -0
  189. package/plugins/acryl-router/src/core/middleware/serve-static/path.ts +25 -0
  190. package/plugins/acryl-router/src/core/middleware/timeout/index.test.ts +70 -0
  191. package/plugins/acryl-router/src/core/middleware/timeout/index.ts +58 -0
  192. package/plugins/acryl-router/src/core/middleware/timing/index.test.ts +310 -0
  193. package/plugins/acryl-router/src/core/middleware/timing/index.ts +8 -0
  194. package/plugins/acryl-router/src/core/middleware/timing/timing.ts +256 -0
  195. package/plugins/acryl-router/src/core/middleware/trailing-slash/index.test.ts +381 -0
  196. package/plugins/acryl-router/src/core/middleware/trailing-slash/index.ts +156 -0
  197. package/plugins/acryl-router/src/core/request/constants.ts +1 -0
  198. package/plugins/acryl-router/src/core/request.test.ts +751 -0
  199. package/plugins/acryl-router/src/core/request.ts +502 -0
  200. package/plugins/acryl-router/src/core/router/common.case.test.ts +1132 -0
  201. package/plugins/acryl-router/src/core/router/linear-router/index.ts +6 -0
  202. package/plugins/acryl-router/src/core/router/linear-router/router.test.ts +80 -0
  203. package/plugins/acryl-router/src/core/router/linear-router/router.ts +152 -0
  204. package/plugins/acryl-router/src/core/router/pattern-router/index.ts +6 -0
  205. package/plugins/acryl-router/src/core/router/pattern-router/router.test.ts +46 -0
  206. package/plugins/acryl-router/src/core/router/pattern-router/router.ts +54 -0
  207. package/plugins/acryl-router/src/core/router/reg-exp-router/index.ts +7 -0
  208. package/plugins/acryl-router/src/core/router/reg-exp-router/matcher.ts +33 -0
  209. package/plugins/acryl-router/src/core/router/reg-exp-router/node.ts +170 -0
  210. package/plugins/acryl-router/src/core/router/reg-exp-router/prepared-router.test.ts +154 -0
  211. package/plugins/acryl-router/src/core/router/reg-exp-router/prepared-router.ts +165 -0
  212. package/plugins/acryl-router/src/core/router/reg-exp-router/router.test.ts +265 -0
  213. package/plugins/acryl-router/src/core/router/reg-exp-router/router.ts +174 -0
  214. package/plugins/acryl-router/src/core/router/reg-exp-router/trie.ts +84 -0
  215. package/plugins/acryl-router/src/core/router/smart-router/index.ts +6 -0
  216. package/plugins/acryl-router/src/core/router/smart-router/router.test.ts +13 -0
  217. package/plugins/acryl-router/src/core/router/smart-router/router.ts +70 -0
  218. package/plugins/acryl-router/src/core/router/trie-router/index.ts +6 -0
  219. package/plugins/acryl-router/src/core/router/trie-router/node.test.ts +896 -0
  220. package/plugins/acryl-router/src/core/router/trie-router/node.ts +221 -0
  221. package/plugins/acryl-router/src/core/router/trie-router/router.test.ts +8 -0
  222. package/plugins/acryl-router/src/core/router/trie-router/router.ts +18 -0
  223. package/plugins/acryl-router/src/core/router/utils.ts +1 -0
  224. package/plugins/acryl-router/src/core/router.ts +103 -0
  225. package/plugins/acryl-router/src/core/types.test.ts +4134 -0
  226. package/plugins/acryl-router/src/core/types.ts +2782 -0
  227. package/plugins/acryl-router/src/core/utils/accept.test.ts +299 -0
  228. package/plugins/acryl-router/src/core/utils/accept.ts +268 -0
  229. package/plugins/acryl-router/src/core/utils/basic-auth.test.ts +56 -0
  230. package/plugins/acryl-router/src/core/utils/basic-auth.ts +26 -0
  231. package/plugins/acryl-router/src/core/utils/body.test.ts +496 -0
  232. package/plugins/acryl-router/src/core/utils/body.ts +263 -0
  233. package/plugins/acryl-router/src/core/utils/buffer.test.ts +147 -0
  234. package/plugins/acryl-router/src/core/utils/buffer.ts +117 -0
  235. package/plugins/acryl-router/src/core/utils/color.test.ts +23 -0
  236. package/plugins/acryl-router/src/core/utils/color.ts +56 -0
  237. package/plugins/acryl-router/src/core/utils/compress.ts +10 -0
  238. package/plugins/acryl-router/src/core/utils/concurrent.test.ts +69 -0
  239. package/plugins/acryl-router/src/core/utils/concurrent.ts +55 -0
  240. package/plugins/acryl-router/src/core/utils/constants.ts +4 -0
  241. package/plugins/acryl-router/src/core/utils/cookie.test.ts +460 -0
  242. package/plugins/acryl-router/src/core/utils/cookie.ts +281 -0
  243. package/plugins/acryl-router/src/core/utils/crypto.test.ts +43 -0
  244. package/plugins/acryl-router/src/core/utils/crypto.ts +58 -0
  245. package/plugins/acryl-router/src/core/utils/encode.test.ts +61 -0
  246. package/plugins/acryl-router/src/core/utils/encode.ts +34 -0
  247. package/plugins/acryl-router/src/core/utils/filepath.test.ts +78 -0
  248. package/plugins/acryl-router/src/core/utils/filepath.ts +60 -0
  249. package/plugins/acryl-router/src/core/utils/handler.ts +15 -0
  250. package/plugins/acryl-router/src/core/utils/headers.ts +355 -0
  251. package/plugins/acryl-router/src/core/utils/html.test.ts +16 -0
  252. package/plugins/acryl-router/src/core/utils/html.ts +182 -0
  253. package/plugins/acryl-router/src/core/utils/http-status.ts +72 -0
  254. package/plugins/acryl-router/src/core/utils/ipaddr.test.ts +149 -0
  255. package/plugins/acryl-router/src/core/utils/ipaddr.ts +374 -0
  256. package/plugins/acryl-router/src/core/utils/jwt/index.ts +7 -0
  257. package/plugins/acryl-router/src/core/utils/jwt/jwa.test.ts +86 -0
  258. package/plugins/acryl-router/src/core/utils/jwt/jwa.ts +38 -0
  259. package/plugins/acryl-router/src/core/utils/jwt/jws.ts +235 -0
  260. package/plugins/acryl-router/src/core/utils/jwt/jwt.test.ts +1788 -0
  261. package/plugins/acryl-router/src/core/utils/jwt/jwt.ts +292 -0
  262. package/plugins/acryl-router/src/core/utils/jwt/types.ts +157 -0
  263. package/plugins/acryl-router/src/core/utils/jwt/utf8.ts +7 -0
  264. package/plugins/acryl-router/src/core/utils/mime.test.ts +64 -0
  265. package/plugins/acryl-router/src/core/utils/mime.ts +94 -0
  266. package/plugins/acryl-router/src/core/utils/stream.test.ts +212 -0
  267. package/plugins/acryl-router/src/core/utils/stream.ts +107 -0
  268. package/plugins/acryl-router/src/core/utils/types.test.ts +295 -0
  269. package/plugins/acryl-router/src/core/utils/types.ts +123 -0
  270. package/plugins/acryl-router/src/core/utils/url.test.ts +384 -0
  271. package/plugins/acryl-router/src/core/utils/url.ts +337 -0
  272. package/plugins/acryl-router/src/core/validator/index.ts +8 -0
  273. package/plugins/acryl-router/src/core/validator/utils.test.ts +82 -0
  274. package/plugins/acryl-router/src/core/validator/utils.ts +69 -0
  275. package/plugins/acryl-router/src/core/validator/validator.test.ts +1488 -0
  276. package/plugins/acryl-router/src/core/validator/validator.ts +185 -0
  277. package/plugins/acryl-router/src/helpers/index.ts +13 -3
  278. package/plugins/acryl-router/src/helpers/schema-validator.ts +35 -0
  279. package/plugins/acryl-router/src/index.ts +6 -6
  280. package/plugins/acryl-router/src/middleware/index.ts +25 -6
  281. package/plugins/acryl-router/src/middleware/loopback-only.ts +53 -14
  282. package/plugins/acryl-router/src/service.ts +262 -64
  283. package/plugins/acryl-router/tests/docs-examples.spec.ts +152 -0
  284. package/plugins/acryl-router/tests/live-install.spec.ts +2 -2
  285. package/plugins/acryl-router/tests/loader.spec.ts +3 -2
  286. package/plugins/acryl-router/tests/loopback-only.spec.ts +80 -0
  287. package/plugins/acryl-router/tests/service.spec.ts +281 -66
  288. package/plugins/acryl-router/tests/shadow-state.spec.ts +79 -0
  289. package/plugins/acryl-router/upstream.json +2 -2
  290. package/plugins/acryl-router-http/plugin.json +2 -1
  291. package/plugins/acryl-router-http/src/bridge.ts +3 -1
  292. package/plugins/acryl-router-http/tests/adapter.spec.ts +16 -12
  293. package/plugins/acryl-router-http/tests/surfaces.spec.ts +12 -10
  294. package/plugins/acryl-router-http/upstream.json +3 -3
  295. package/plugins/acryl-state/NOTICE +30 -0
  296. package/plugins/acryl-state/plugin.json +1 -1
  297. package/plugins/acryl-state/src/cell.ts +1 -1
  298. package/plugins/acryl-state/src/service.ts +33 -15
  299. package/plugins/acryl-state/upstream.json +6 -4
  300. package/plugins/builder/script.ts +4 -7
  301. package/plugins/extensions/index.ts +6 -5
  302. package/plugins/extensions/resolve.ts +28 -3
  303. package/plugins/openai/prompt.ts +9 -7
  304. package/plugins/tsconfig.json +5 -4
  305. package/shared/AGENTS.core.md +54 -0
  306. package/templates/agent/AGENTS.md +6 -0
  307. package/templates/agent/README.md +2 -2
  308. package/templates/agent/blend.yaml +1 -1
  309. package/templates/agent/src/routes.ts +37 -27
  310. package/templates/agent/test/model.test.ts +2 -1
  311. package/templates/agent/tsconfig.json +8 -5
  312. package/templates/blank/AGENTS.md +5 -0
  313. package/templates/blank/README.md +1 -1
  314. package/templates/blank/src/routes.ts +6 -4
  315. package/templates/blank/tsconfig.json +2 -4
  316. package/templates/todo/AGENTS.md +5 -0
  317. package/templates/todo/src/page.ts +1 -1
  318. package/templates/todo/src/todo.ts +58 -29
  319. package/templates/todo/tsconfig.json +2 -4
  320. package/templates/todo/upstream.json +2 -2
  321. package/plugins/acryl-router/src/compose.ts +0 -21
  322. package/plugins/acryl-router/src/context.ts +0 -59
  323. package/plugins/acryl-router/src/helpers/read-body.ts +0 -59
  324. package/plugins/acryl-router/src/helpers/sse.ts +0 -83
  325. package/plugins/acryl-router/src/helpers/validate.ts +0 -44
  326. package/plugins/acryl-router/src/http-error.ts +0 -21
  327. package/plugins/acryl-router/src/middleware/body-limit.ts +0 -25
  328. package/plugins/acryl-router/src/middleware/cors.ts +0 -46
  329. package/plugins/acryl-router/src/middleware/logger.ts +0 -21
  330. package/plugins/acryl-router/src/middleware/request-id.ts +0 -24
  331. package/plugins/acryl-router/src/middleware/secure-headers.ts +0 -23
  332. package/plugins/acryl-router/src/pattern.ts +0 -89
  333. package/plugins/acryl-router/src/registrar.ts +0 -90
  334. package/plugins/acryl-router/src/table.ts +0 -156
  335. package/plugins/acryl-router/src/types.ts +0 -73
  336. package/plugins/acryl-router/src/util.ts +0 -23
  337. package/plugins/acryl-router/tests/api.spec.ts +0 -223
  338. package/plugins/acryl-router/tests/conformance.ts +0 -170
  339. package/plugins/acryl-router/tests/helpers.spec.ts +0 -172
  340. package/plugins/acryl-router/tests/middleware.spec.ts +0 -183
  341. package/plugins/acryl-router/tests/table.spec.ts +0 -47
@@ -0,0 +1,1302 @@
1
+ import { App } from '../../../acryl-router/src/index.ts'
2
+ import { basicAuth } from '../../../acryl-router/src/middleware/index.ts'
3
+ import { compress } from '../../../acryl-router/src/middleware/index.ts'
4
+ import { etag } from '../../../acryl-router/src/middleware/index.ts'
5
+ import { poweredBy } from '../../../acryl-router/src/middleware/index.ts'
6
+ import { stream } from '../../../acryl-router/src/helpers/index.ts'
7
+ import fs from 'node:fs'
8
+ import { createServer as createHttp2Server } from 'node:http2'
9
+ import { createServer as createHTTPSServer } from 'node:https'
10
+ import { gunzipSync, inflateSync } from 'node:zlib'
11
+ import { GlobalHeaders } from '../../src/adapter/headers.ts'
12
+ import { GlobalRequest, Request as LightweightRequest, getAbortController } from '../../src/adapter/request.ts'
13
+ import { GlobalResponse, Response as LightweightResponse } from '../../src/adapter/response.ts'
14
+ import { createAdaptorServer, serve } from '../../src/adapter/server.ts'
15
+ import type { HttpBindings, ServerType } from '../../src/adapter/types.ts'
16
+ import { app } from './app.ts'
17
+ import { requestServer, requestServerChunked, requestServerHttp2 } from './helpers/request.ts'
18
+
19
+ describe('Basic', () => {
20
+ const server = createAdaptorServer(app)
21
+
22
+ it('Should return 200 response - GET /', async () => {
23
+ const res = await requestServer(server, { method: 'GET', path: '/' })
24
+ expect(res.status).toBe(200)
25
+ expect(res.headers.get('content-type')).toMatch('text/plain')
26
+ expect(await res.text()).toBe('Hello! Node!')
27
+ })
28
+
29
+ it('Should return 200 response - GET /url', async () => {
30
+ const res = await requestServer(server, { method: 'GET', path: '/url' })
31
+ expect(res.status).toBe(200)
32
+ expect(res.headers.get('content-type')).toMatch('text/plain')
33
+ const url = new URL(await res.text())
34
+ expect(url.pathname).toBe('/url')
35
+ expect(url.hostname).toBe('127.0.0.1')
36
+ expect(url.protocol).toBe('http:')
37
+ })
38
+
39
+ it('Should return 200 response - GET /posts?page=2', async () => {
40
+ const res = await requestServer(server, { method: 'GET', path: '/posts?page=2' })
41
+ expect(res.status).toBe(200)
42
+ expect(await res.text()).toBe('Page 2')
43
+ })
44
+
45
+ it('Should return 200 response - GET /user-agent', async () => {
46
+ const res = await requestServer(server, {
47
+ method: 'GET',
48
+ path: '/user-agent',
49
+ headers: { 'user-agent': 'App' },
50
+ })
51
+ expect(res.status).toBe(200)
52
+ expect(res.headers.get('content-type')).toMatch('text/plain')
53
+ expect(await res.text()).toBe('App')
54
+ })
55
+
56
+ it('Should return 302 response - POST /posts', async () => {
57
+ const res = await requestServer(server, { method: 'POST', path: '/posts' })
58
+ expect(res.status).toBe(302)
59
+ expect(res.headers.get('location')).toBe('/posts')
60
+ })
61
+
62
+ it('Should return 200 response - POST /no-body-consumed', async () => {
63
+ const res = await requestServer(server, {
64
+ method: 'POST',
65
+ path: '/no-body-consumed',
66
+ headers: { 'content-length': '0' },
67
+ body: '',
68
+ })
69
+ expect(res.status).toBe(200)
70
+ expect(await res.text()).toBe('No body consumed')
71
+ })
72
+
73
+ it('Should return 200 response - POST /body-cancelled', async () => {
74
+ const res = await requestServer(server, {
75
+ method: 'POST',
76
+ path: '/body-cancelled',
77
+ headers: { 'content-length': '0' },
78
+ body: '',
79
+ })
80
+ expect(res.status).toBe(200)
81
+ expect(await res.text()).toBe('Body cancelled')
82
+ })
83
+
84
+ it('Should return 200 response - POST /partially-consumed', async () => {
85
+ const buffer = Buffer.alloc(1024 * 10) // large buffer
86
+ const res = await requestServer(server, {
87
+ method: 'POST',
88
+ path: '/partially-consumed',
89
+ headers: { 'content-length': buffer.length.toString() },
90
+ body: buffer,
91
+ })
92
+
93
+ expect(res.status).toBe(200)
94
+ expect(await res.text()).toBe('Partially consumed')
95
+ })
96
+
97
+ it('Should return 200 response - POST /partially-consumed-and-cancelled', async () => {
98
+ const buffer = Buffer.alloc(1) // A large buffer will not make the test go far, so keep it small because it won't go far.
99
+ const res = await requestServer(server, {
100
+ method: 'POST',
101
+ path: '/partially-consumed-and-cancelled',
102
+ headers: { 'content-length': buffer.length.toString() },
103
+ body: buffer,
104
+ })
105
+
106
+ expect(res.status).toBe(200)
107
+ expect(await res.text()).toBe('Partially consumed and cancelled')
108
+ })
109
+
110
+ it('Should return 201 response - DELETE /posts/123', async () => {
111
+ const res = await requestServer(server, { method: 'DELETE', path: '/posts/123' })
112
+ expect(res.status).toBe(200)
113
+ expect(await res.text()).toBe('DELETE 123')
114
+ })
115
+
116
+ it('Should return 500 response - GET /invalid', async () => {
117
+ const res = await requestServer(server, { method: 'GET', path: '/invalid' })
118
+ expect(res.status).toBe(500)
119
+ expect(res.headers.get('content-type')).toEqual('text/plain')
120
+ })
121
+
122
+ it('Should return 200 response - GET /ponyfill', async () => {
123
+ const res = await requestServer(server, { method: 'GET', path: '/ponyfill' })
124
+ expect(res.status).toBe(200)
125
+ expect(res.headers.get('content-type')).toMatch('text/plain')
126
+ expect(await res.text()).toBe('Pony')
127
+ })
128
+
129
+ it('Should not raise error for TRACE method', async () => {
130
+ const res = await requestServer(server, { method: 'TRACE', path: '/' })
131
+ expect(await res.text()).toBe('headers: {}')
132
+ })
133
+ })
134
+
135
+ describe.each([
136
+ { protocol: 'HTTP/1.1', serverOptions: {}, request: requestServer },
137
+ {
138
+ protocol: 'HTTP/2',
139
+ serverOptions: { createServer: createHttp2Server },
140
+ request: requestServerHttp2,
141
+ },
142
+ ])('Caller-owned response headers over $protocol', ({ serverOptions, request }) => {
143
+ it('should allow reusing the headers passed to c.body()', async () => {
144
+ const headers = { 'content-type': 'text/plain' }
145
+ const testApp = new App()
146
+ testApp.get('/', (c) => c.body(new Uint8Array([104, 105]), 200, headers))
147
+ const server = createAdaptorServer({ fetch: testApp.fetch, ...serverOptions })
148
+
149
+ for (let i = 0; i < 2; i++) {
150
+ const res = await request(server, { method: 'GET', path: '/' })
151
+ expect(res.status).toBe(200)
152
+ expect(await res.text()).toBe('hi')
153
+ expect(headers).toEqual({ 'content-type': 'text/plain' })
154
+ }
155
+ })
156
+
157
+ describe.each([false, true])('frozen headers: %s', (frozen) => {
158
+ it.each([
159
+ { name: 'string', makeBody: (text: string) => text },
160
+ { name: 'Uint8Array', makeBody: (text: string) => new TextEncoder().encode(text) },
161
+ { name: 'Blob', makeBody: (text: string) => new Blob([text]) },
162
+ ])('should preserve shared headers with a $name body', async ({ makeBody }) => {
163
+ const headers = { 'content-type': 'text/plain' }
164
+ if (frozen) {
165
+ Object.freeze(headers)
166
+ }
167
+ const bodies = ['hi', 'hello', '']
168
+ let index = 0
169
+ const testApp = new App()
170
+ testApp.get('/', () => new Response(makeBody(bodies[index++]), { headers }))
171
+ const server = createAdaptorServer({ fetch: testApp.fetch, ...serverOptions })
172
+
173
+ for (const body of bodies) {
174
+ const res = await request(server, { method: 'GET', path: '/' })
175
+ expect(res.status).toBe(200)
176
+ expect(res.headers.get('content-length')).toBe(String(Buffer.byteLength(body)))
177
+ expect(await res.text()).toBe(body)
178
+ expect(headers).toEqual({ 'content-type': 'text/plain' })
179
+ }
180
+ })
181
+ })
182
+ })
183
+
184
+ describe('various response body types', () => {
185
+ const runner = (Response: typeof GlobalResponse) => {
186
+ const largeText = 'a'.repeat(1024 * 1024 * 10)
187
+ let server: ServerType
188
+ let resolveReadableStreamPromise: () => void
189
+ let resolveEventStreamPromise: () => void
190
+ let resolveEventStreamWithoutTransferEncodingPromise: () => void
191
+ beforeAll(() => {
192
+ const app = new App()
193
+ app.use('*', async (c, next) => {
194
+ await next()
195
+
196
+ // generate internal response object
197
+ const status = c.res.status
198
+ if (status > 999) {
199
+ c.res = new Response('Internal Server Error', { status: 500 })
200
+ }
201
+ })
202
+ app.get('/', () => {
203
+ const response = new Response('Hello! Node!')
204
+ return response
205
+ })
206
+ app.get('/large', () => {
207
+ // 10MB text
208
+ const response = new Response(largeText)
209
+ return response
210
+ })
211
+ app.get('/uint8array', () => {
212
+ const response = new Response(new Uint8Array([1, 2, 3]), {
213
+ headers: { 'content-type': 'application/octet-stream' },
214
+ })
215
+ return response
216
+ })
217
+ app.get('/blob', () => {
218
+ const response = new Response(new Blob([new Uint8Array([1, 2, 3])]), {
219
+ headers: { 'content-type': 'application/octet-stream' },
220
+ })
221
+ return response
222
+ })
223
+ const readableStreamPromise = new Promise<void>((resolve) => {
224
+ resolveReadableStreamPromise = resolve
225
+ })
226
+ app.get('/readable-stream', () => {
227
+ const stream = new ReadableStream({
228
+ async start(controller) {
229
+ await readableStreamPromise
230
+ controller.enqueue('Hello!')
231
+ controller.enqueue(' Node!')
232
+ controller.close()
233
+ },
234
+ })
235
+ return new Response(stream)
236
+ })
237
+ app.get('/readable-stream-with-transfer-encoding', () => {
238
+ const stream = new ReadableStream({
239
+ async start(controller) {
240
+ controller.enqueue('Hello!') // send one chunk synchronously
241
+ controller.close()
242
+ },
243
+ })
244
+ return new Response(stream, {
245
+ headers: {
246
+ 'content-type': 'text/plain; charset=UTF-8',
247
+ 'transfer-encoding': 'chunked',
248
+ },
249
+ })
250
+ })
251
+ const eventStreamPromise = new Promise<void>((resolve) => {
252
+ resolveEventStreamPromise = resolve
253
+ })
254
+ app.get('/event-stream', () => {
255
+ const stream = new ReadableStream({
256
+ async start(controller) {
257
+ controller.enqueue('data: First!\n\n')
258
+ await eventStreamPromise
259
+ controller.enqueue('data: Second!\n\n')
260
+ controller.close()
261
+ },
262
+ })
263
+ return new Response(stream, {
264
+ headers: {
265
+ 'content-type': 'text/event-stream',
266
+ 'transfer-encoding': 'chunked',
267
+ },
268
+ })
269
+ })
270
+ const eventStreamWithoutTransferEncodingPromise = new Promise<void>((resolve) => {
271
+ resolveEventStreamWithoutTransferEncodingPromise = resolve
272
+ })
273
+ app.get('/event-stream-without-transfer-encoding', () => {
274
+ const stream = new ReadableStream({
275
+ async start(controller) {
276
+ controller.enqueue('data: First!\n\n')
277
+ await eventStreamWithoutTransferEncodingPromise
278
+ controller.enqueue('data: Second!\n\n')
279
+ controller.close()
280
+ },
281
+ })
282
+ return new Response(stream, {
283
+ headers: {
284
+ 'content-type': 'text/event-stream',
285
+ },
286
+ })
287
+ })
288
+ app.get('/buffer', () => {
289
+ const response = new Response(Buffer.from('Hello App!'), {
290
+ headers: { 'content-type': 'text/plain' },
291
+ })
292
+ return response
293
+ })
294
+ app.get('/text-with-content-length-object', () => {
295
+ const response = new Response('Hello App!', {
296
+ headers: { 'content-type': 'text/plain', 'content-length': '00010' },
297
+ })
298
+ return response
299
+ })
300
+ app.get('/text-with-content-length-headers', () => {
301
+ const response = new Response('Hello App!', {
302
+ headers: new Headers({ 'content-type': 'text/plain', 'content-length': '00010' }),
303
+ })
304
+ return response
305
+ })
306
+ app.get('/text-with-content-length-array', () => {
307
+ const response = new Response('Hello App!', {
308
+ headers: [
309
+ ['content-type', 'text/plain'],
310
+ ['content-length', '00010'],
311
+ ],
312
+ })
313
+ return response
314
+ })
315
+ app.get('/text-with-set-cookie-array', () => {
316
+ const response = new Response('Hello App!', {
317
+ headers: [
318
+ ['content-type', 'text/plain'],
319
+ ['set-cookie', 'a=1'],
320
+ ['set-cookie', 'b=2'],
321
+ ],
322
+ })
323
+ return response
324
+ })
325
+
326
+ app.use('/etag/*', etag())
327
+ app.get('/etag/buffer', () => {
328
+ const response = new Response(Buffer.from('Hello App!'), {
329
+ headers: { 'content-type': 'text/plain' },
330
+ })
331
+ return response
332
+ })
333
+
334
+ server = createAdaptorServer(app)
335
+ })
336
+
337
+ it('Should return 200 response - GET /', async () => {
338
+ const res = await requestServer(server, { method: 'GET', path: '/' })
339
+ expect(res.status).toBe(200)
340
+ expect(res.headers.get('content-type')).toMatch('text/plain')
341
+ expect(res.headers.get('content-length')).toMatch('12')
342
+ expect(await res.text()).toBe('Hello! Node!')
343
+ })
344
+
345
+ it('Should return 200 response - GET /large', async () => {
346
+ const res = await requestServer(server, { method: 'GET', path: '/large' })
347
+ expect(res.status).toBe(200)
348
+ expect(res.headers.get('content-type')).toMatch('text/plain')
349
+ expect(res.headers.get('content-length')).toMatch(largeText.length.toString())
350
+ expect(await res.text()).toBe(largeText)
351
+ })
352
+
353
+ it('Should return 200 response - GET /uint8array', async () => {
354
+ const res = await requestServer(server, { method: 'GET', path: '/uint8array' })
355
+ expect(res.status).toBe(200)
356
+ expect(res.headers.get('content-type')).toMatch('application/octet-stream')
357
+ expect(res.headers.get('content-length')).toMatch('3')
358
+ expect(Buffer.from(await res.arrayBuffer())).toEqual(Buffer.from([1, 2, 3]))
359
+ })
360
+
361
+ it('Should return 200 response - GET /blob', async () => {
362
+ const res = await requestServer(server, { method: 'GET', path: '/blob' })
363
+ expect(res.status).toBe(200)
364
+ expect(res.headers.get('content-type')).toMatch('application/octet-stream')
365
+ expect(res.headers.get('content-length')).toMatch('3')
366
+ expect(Buffer.from(await res.arrayBuffer())).toEqual(Buffer.from([1, 2, 3]))
367
+ })
368
+
369
+ it('Should return 200 response - GET /readable-stream', async () => {
370
+ const { chunks, response: res } = await requestServerChunked(
371
+ server,
372
+ { method: 'GET', path: '/readable-stream' },
373
+ {
374
+ onHeaders: (headers) => {
375
+ // response header should be sent before sending data.
376
+ expect(headers.get('transfer-encoding')).toBe('chunked')
377
+ resolveReadableStreamPromise()
378
+ },
379
+ }
380
+ )
381
+ expect(res.status).toBe(200)
382
+ expect(res.headers.get('content-type')).toMatch('text/plain; charset=UTF-8')
383
+ expect(res.headers.get('content-length')).toBeNull()
384
+ expect(chunks.map((chunk) => chunk.toString())).toEqual(['Hello!', ' Node!'])
385
+ })
386
+
387
+ it('Should return 200 response - GET /readable-stream-with-transfer-encoding', async () => {
388
+ const res = await requestServer(server, {
389
+ method: 'GET',
390
+ path: '/readable-stream-with-transfer-encoding',
391
+ })
392
+ expect(res.status).toBe(200)
393
+ expect(res.headers.get('content-type')).toMatch('text/plain; charset=UTF-8')
394
+ expect(res.headers.get('transfer-encoding')).toBe('chunked')
395
+ expect(res.headers.get('content-length')).toBeNull()
396
+ })
397
+
398
+ it('Should return 200 response - GET /event-stream', async () => {
399
+ const { chunks, response: res } = await requestServerChunked(
400
+ server,
401
+ { method: 'GET', path: '/event-stream' },
402
+ {
403
+ onHeaders: (headers) => {
404
+ // response header should be sent before sending data.
405
+ expect(headers.get('transfer-encoding')).toBe('chunked')
406
+ resolveEventStreamPromise()
407
+ },
408
+ }
409
+ )
410
+ expect(res.status).toBe(200)
411
+ expect(res.headers.get('content-type')).toMatch('text/event-stream')
412
+ expect(res.headers.get('content-length')).toBeNull()
413
+ expect(chunks.map((chunk) => chunk.toString())).toEqual([
414
+ 'data: First!\n\n',
415
+ 'data: Second!\n\n',
416
+ ])
417
+ })
418
+
419
+ it('Should return 200 response - GET /event-stream-without-transfer-encoding', async () => {
420
+ const { chunks, response: res } = await requestServerChunked(
421
+ server,
422
+ { method: 'GET', path: '/event-stream-without-transfer-encoding' },
423
+ {
424
+ onChunk: (_chunk, index) => {
425
+ if (index === 0) {
426
+ // receive first chunk
427
+ resolveEventStreamWithoutTransferEncodingPromise()
428
+ }
429
+ },
430
+ }
431
+ )
432
+ expect(res.status).toBe(200)
433
+ expect(res.headers.get('content-type')).toMatch('text/event-stream')
434
+ expect(res.headers.get('content-length')).toBeNull()
435
+ expect(chunks.map((chunk) => chunk.toString())).toEqual([
436
+ 'data: First!\n\n',
437
+ 'data: Second!\n\n',
438
+ ])
439
+ })
440
+
441
+ it('Should return 200 response - GET /buffer', async () => {
442
+ const res = await requestServer(server, { method: 'GET', path: '/buffer' })
443
+ expect(res.status).toBe(200)
444
+ expect(res.headers.get('content-type')).toMatch('text/plain')
445
+ expect(res.headers.get('content-length')).toMatch('10')
446
+ expect(await res.text()).toBe('Hello App!')
447
+ })
448
+
449
+ it('Should return 200 response - GET /text-with-content-length-object', async () => {
450
+ const res = await requestServer(server, {
451
+ method: 'GET',
452
+ path: '/text-with-content-length-object',
453
+ })
454
+ expect(res.status).toBe(200)
455
+ expect(res.headers.get('content-type')).toMatch('text/plain')
456
+ expect(res.headers.get('content-length')).toBe('00010')
457
+ expect(await res.text()).toBe('Hello App!')
458
+ })
459
+
460
+ it('Should return 200 response - GET /text-with-content-length-headers', async () => {
461
+ const res = await requestServer(server, {
462
+ method: 'GET',
463
+ path: '/text-with-content-length-headers',
464
+ })
465
+ expect(res.status).toBe(200)
466
+ expect(res.headers.get('content-type')).toMatch('text/plain')
467
+ expect(res.headers.get('content-length')).toBe('00010')
468
+ expect(await res.text()).toBe('Hello App!')
469
+ })
470
+
471
+ it('Should return 200 response - GET /text-with-content-length-array', async () => {
472
+ const res = await requestServer(server, {
473
+ method: 'GET',
474
+ path: '/text-with-content-length-array',
475
+ })
476
+ expect(res.status).toBe(200)
477
+ expect(res.headers.get('content-type')).toMatch('text/plain')
478
+ expect(res.headers.get('content-length')).toBe('00010')
479
+ expect(await res.text()).toBe('Hello App!')
480
+ })
481
+
482
+ it('Should return 200 response - GET /text-with-set-cookie-array', async () => {
483
+ const res = await requestServer(server, {
484
+ method: 'GET',
485
+ path: '/text-with-set-cookie-array',
486
+ })
487
+ expect(res.status).toBe(200)
488
+ expect(res.headers.get('content-type')).toMatch('text/plain')
489
+ expect(res.headers.getSetCookie()).toEqual(['a=1', 'b=2'])
490
+ expect(await res.text()).toBe('Hello App!')
491
+ })
492
+
493
+ it('Should return 200 response - GET /etag/buffer', async () => {
494
+ const res = await requestServer(server, { method: 'GET', path: '/etag/buffer' })
495
+ expect(res.status).toBe(200)
496
+ expect(res.headers.get('content-type')).toMatch('text/plain')
497
+ expect(res.headers.get('etag')).toMatch('"a4b794176ec9cd86063d6a0075c27385b32ec9b4"')
498
+ expect(res.headers.get('content-length')).toMatch('10')
499
+ expect(await res.text()).toBe('Hello App!')
500
+ })
501
+ }
502
+
503
+ describe('GlobalResponse', () => {
504
+ runner(GlobalResponse)
505
+ })
506
+
507
+ describe('LightweightResponse', () => {
508
+ runner(LightweightResponse as unknown as typeof GlobalResponse)
509
+ })
510
+ })
511
+
512
+ describe('Routing', () => {
513
+ describe('Nested Route', () => {
514
+ const book = new App()
515
+ book.get('/', (c) => c.text('get /book'))
516
+ book.get('/:id', (c) => {
517
+ return c.text('get /book/' + c.req.param('id'))
518
+ })
519
+ book.post('/', (c) => c.text('post /book'))
520
+
521
+ const app = new App()
522
+ app.route('/book', book)
523
+
524
+ const server = createAdaptorServer(app)
525
+
526
+ it('Should return responses from `/book/*`', async () => {
527
+ let res = await requestServer(server, { method: 'GET', path: '/book' })
528
+ expect(res.status).toBe(200)
529
+ expect(await res.text()).toBe('get /book')
530
+
531
+ res = await requestServer(server, { method: 'GET', path: '/book/123' })
532
+ expect(res.status).toBe(200)
533
+ expect(await res.text()).toBe('get /book/123')
534
+
535
+ res = await requestServer(server, { method: 'POST', path: '/book' })
536
+ expect(res.status).toBe(200)
537
+ expect(await res.text()).toBe('post /book')
538
+ })
539
+ })
540
+
541
+ describe('Chained route', () => {
542
+ const app = new App()
543
+
544
+ app
545
+ .get('/chained/:abc', (c) => {
546
+ const abc = c.req.param('abc')
547
+ return c.text(`GET for ${abc}`)
548
+ })
549
+ .post((c) => {
550
+ const abc = c.req.param('abc')
551
+ return c.text(`POST for ${abc}`)
552
+ })
553
+ const server = createAdaptorServer(app)
554
+
555
+ it('Should return responses from `/chained/*`', async () => {
556
+ let res = await requestServer(server, { method: 'GET', path: '/chained/abc' })
557
+ expect(res.status).toBe(200)
558
+ expect(await res.text()).toBe('GET for abc')
559
+
560
+ res = await requestServer(server, { method: 'POST', path: '/chained/abc' })
561
+ expect(res.status).toBe(200)
562
+ expect(await res.text()).toBe('POST for abc')
563
+
564
+ res = await requestServer(server, { method: 'PUT', path: '/chained/abc' })
565
+ expect(res.status).toBe(404)
566
+ })
567
+ })
568
+ })
569
+
570
+ describe('Request body', () => {
571
+ const app = new App()
572
+ app.post('/json', async (c) => {
573
+ const data = await c.req.json()
574
+ return c.json(data)
575
+ })
576
+ app.post('/form', async (c) => {
577
+ const data = await c.req.parseBody()
578
+ return c.json(data)
579
+ })
580
+ const server = createAdaptorServer(app)
581
+
582
+ it('Should handle JSON body', async () => {
583
+ const res = await requestServer(server, {
584
+ method: 'POST',
585
+ path: '/json',
586
+ headers: { 'content-type': 'application/json' },
587
+ body: JSON.stringify({ foo: 'bar' }),
588
+ })
589
+ expect(res.status).toBe(200)
590
+ expect(await res.json()).toEqual({ foo: 'bar' })
591
+ })
592
+
593
+ it('Should handle form body', async () => {
594
+ const res = await requestServer(server, {
595
+ method: 'POST',
596
+ path: '/form',
597
+ headers: { 'content-type': 'application/x-www-form-urlencoded' },
598
+ body: 'foo=bar',
599
+ })
600
+ expect(res.status).toBe(200)
601
+ expect(await res.json()).toEqual({ foo: 'bar' })
602
+ })
603
+ })
604
+
605
+ describe('Response body', () => {
606
+ describe('Cached Response', () => {
607
+ const app = new App()
608
+ app.get('/json', (c) => {
609
+ return c.json({ foo: 'bar' })
610
+ })
611
+ app.get('/json-async', async (c) => {
612
+ return c.json({ foo: 'async' })
613
+ })
614
+ app.get('/html', (c) => {
615
+ return c.html('<h1>Hello!</h1>')
616
+ })
617
+ app.get('/html-async', async (c) => {
618
+ return c.html('<h1>Hello!</h1>')
619
+ })
620
+ const server = createAdaptorServer(app)
621
+
622
+ it('Should return JSON body', async () => {
623
+ const res = await requestServer(server, { method: 'GET', path: '/json' })
624
+ expect(res.status).toBe(200)
625
+ expect(res.headers.get('content-type')).toMatch('application/json')
626
+ expect(await res.json()).toEqual({ foo: 'bar' })
627
+ })
628
+
629
+ it('Should return JSON body from /json-async', async () => {
630
+ const res = await requestServer(server, { method: 'GET', path: '/json-async' })
631
+ expect(res.status).toBe(200)
632
+ expect(res.headers.get('content-type')).toMatch('application/json')
633
+ expect(await res.json()).toEqual({ foo: 'async' })
634
+ })
635
+
636
+ it('Should return HTML', async () => {
637
+ const res = await requestServer(server, { method: 'GET', path: '/html' })
638
+ expect(res.status).toBe(200)
639
+ expect(res.headers.get('content-type')).toMatch('text/html')
640
+ expect(await res.text()).toBe('<h1>Hello!</h1>')
641
+ })
642
+
643
+ it('Should return HTML from /html-async', async () => {
644
+ const res = await requestServer(server, { method: 'GET', path: '/html-async' })
645
+ expect(res.status).toBe(200)
646
+ expect(res.headers.get('content-type')).toMatch('text/html')
647
+ expect(await res.text()).toBe('<h1>Hello!</h1>')
648
+ })
649
+ })
650
+
651
+ describe('Fallback to global.Response', () => {
652
+ const app = new App()
653
+
654
+ app.get('/json-blob', async () => {
655
+ return new Response(new Blob([JSON.stringify({ foo: 'blob' })]), {
656
+ headers: { 'content-type': 'application/json' },
657
+ })
658
+ })
659
+
660
+ app.get('/json-buffer', async () => {
661
+ return new Response(new TextEncoder().encode(JSON.stringify({ foo: 'buffer' })).buffer, {
662
+ headers: { 'content-type': 'application/json' },
663
+ })
664
+ })
665
+
666
+ const server = createAdaptorServer(app)
667
+
668
+ it('Should return JSON body from /json-blob', async () => {
669
+ const res = await requestServer(server, { method: 'GET', path: '/json-blob' })
670
+ expect(res.status).toBe(200)
671
+ expect(res.headers.get('content-type')).toMatch('application/json')
672
+ expect(await res.json()).toEqual({ foo: 'blob' })
673
+ })
674
+
675
+ it('Should return JSON body from /json-buffer', async () => {
676
+ const res = await requestServer(server, { method: 'GET', path: '/json-buffer' })
677
+ expect(res.status).toBe(200)
678
+ expect(res.headers.get('content-type')).toMatch('application/json')
679
+ expect(await res.json()).toEqual({ foo: 'buffer' })
680
+ })
681
+ })
682
+ })
683
+
684
+ describe('Middleware', () => {
685
+ const app = new App<{ Variables: { foo: string } }>()
686
+ app.use('*', poweredBy())
687
+ app.use('*', async (c, next) => {
688
+ c.set('foo', 'bar')
689
+ await next()
690
+ c.header('foo', c.get('foo'))
691
+ })
692
+ app.get('/', (c) => c.text('Hello! Middleware!'))
693
+ const server = createAdaptorServer(app)
694
+
695
+ it('Should have correct header values', async () => {
696
+ const res = await requestServer(server, { method: 'GET', path: '/' })
697
+ expect(res.status).toBe(200)
698
+ expect(res.headers.get('x-powered-by')).toBe('ACRYL')
699
+ expect(res.headers.get('foo')).toBe('bar')
700
+ })
701
+ })
702
+
703
+ describe('Error handling', () => {
704
+ const app = new App()
705
+ app.notFound((c) => {
706
+ return c.text('Custom NotFound', 404)
707
+ })
708
+ app.onError((_, c) => {
709
+ return c.text('Custom Error!', 500)
710
+ })
711
+ app.get('/error', () => {
712
+ throw new Error()
713
+ })
714
+ const server = createAdaptorServer(app)
715
+
716
+ it('Should return 404 response', async () => {
717
+ const res = await requestServer(server, { method: 'GET', path: '/' })
718
+ expect(res.status).toBe(404)
719
+ expect(await res.text()).toBe('Custom NotFound')
720
+ })
721
+
722
+ it('Should return 500 response', async () => {
723
+ const res = await requestServer(server, { method: 'GET', path: '/error' })
724
+ expect(res.status).toBe(500)
725
+ expect(await res.text()).toBe('Custom Error!')
726
+ })
727
+
728
+ it('Should return 404 response - PURGE method', async () => {
729
+ const res = await requestServer(server, { method: 'PURGE', path: '/' })
730
+ expect(res.status).toBe(404)
731
+ })
732
+ })
733
+
734
+ describe('Basic Auth Middleware', () => {
735
+ const app = new App()
736
+ const username = 'acryl-user-a'
737
+ const password = 'acryl-password-a'
738
+ const unicodePassword = '炎'
739
+
740
+ app.use(
741
+ '/auth/*',
742
+ basicAuth({
743
+ username,
744
+ password,
745
+ })
746
+ )
747
+
748
+ app.use(
749
+ '/auth-unicode/*',
750
+ basicAuth({
751
+ username: username,
752
+ password: unicodePassword,
753
+ })
754
+ )
755
+
756
+ app.get('/auth/*', (c) => c.text('auth'))
757
+ app.get('/auth-unicode/*', (c) => c.text('auth'))
758
+
759
+ const server = createAdaptorServer(app)
760
+
761
+ it('Should not authorized', async () => {
762
+ const res = await requestServer(server, { method: 'GET', path: '/auth/a' })
763
+ expect(res.status).toBe(401)
764
+ expect(await res.text()).toBe('Unauthorized')
765
+ })
766
+
767
+ it('Should authorized', async () => {
768
+ const credential = Buffer.from(username + ':' + password).toString('base64')
769
+ const res = await requestServer(server, {
770
+ method: 'GET',
771
+ path: '/auth/a',
772
+ headers: { authorization: `Basic ${credential}` },
773
+ })
774
+ expect(res.status).toBe(200)
775
+ expect(await res.text()).toBe('auth')
776
+ })
777
+
778
+ it('Should authorize Unicode', async () => {
779
+ const credential = Buffer.from(username + ':' + unicodePassword).toString('base64')
780
+ const res = await requestServer(server, {
781
+ method: 'GET',
782
+ path: '/auth-unicode/a',
783
+ headers: { authorization: `Basic ${credential}` },
784
+ })
785
+ expect(res.status).toBe(200)
786
+ expect(await res.text()).toBe('auth')
787
+ })
788
+ })
789
+
790
+ describe('Stream and non-stream response', () => {
791
+ const app = new App()
792
+
793
+ app.get('/json', (c) => c.json({ foo: 'bar' }))
794
+ app.get('/text', (c) => c.text('Hello!'))
795
+ app.get('/json-stream', (c) => {
796
+ c.header('x-accel-buffering', 'no')
797
+ c.header('content-type', 'application/json')
798
+ return stream(c, async (stream) => {
799
+ stream.write(JSON.stringify({ foo: 'bar' }))
800
+ })
801
+ })
802
+ app.get('/stream', (c) => {
803
+ const stream = new ReadableStream({
804
+ async start(controller) {
805
+ controller.enqueue('data: Hello!\n\n')
806
+ await new Promise((resolve) => setTimeout(resolve, 100))
807
+ controller.enqueue('data: end\n\n')
808
+ controller.close()
809
+ },
810
+ })
811
+
812
+ c.header('Content-Type', 'text/event-stream; charset=utf-8')
813
+ return c.body(stream)
814
+ })
815
+
816
+ app.get('/error-stream', (c) => {
817
+ const stream = new ReadableStream({
818
+ async start(controller) {
819
+ controller.enqueue('data: Hello!\n\n')
820
+ await new Promise((resolve) => setTimeout(resolve, 100))
821
+ controller.enqueue('data: end\n\n')
822
+ controller.error(new Error('test'))
823
+ },
824
+ })
825
+
826
+ c.header('Content-Type', 'text/event-stream; charset=utf-8')
827
+ return c.body(stream)
828
+ })
829
+
830
+ const server = createAdaptorServer(app)
831
+
832
+ it('Should return JSON body', async () => {
833
+ const res = await requestServer(server, { method: 'GET', path: '/json' })
834
+ expect(res.status).toBe(200)
835
+ expect(res.headers.get('content-length')).toMatch('13')
836
+ expect(res.headers.get('content-type')).toMatch('application/json')
837
+ expect(await res.json()).toEqual({ foo: 'bar' })
838
+ })
839
+
840
+ it('Should return text body', async () => {
841
+ const res = await requestServer(server, { method: 'GET', path: '/text' })
842
+ expect(res.status).toBe(200)
843
+ expect(res.headers.get('content-length')).toMatch('6')
844
+ expect(res.headers.get('content-type')).toMatch('text/plain')
845
+ expect(await res.text()).toBe('Hello!')
846
+ })
847
+
848
+ it('Should return JSON body - stream', async () => {
849
+ const res = await requestServer(server, { method: 'GET', path: '/json-stream' })
850
+ expect(res.status).toBe(200)
851
+ expect(res.headers.get('content-length')).toBeNull()
852
+ expect(res.headers.get('content-type')).toMatch('application/json')
853
+ expect(res.headers.get('transfer-encoding')).toMatch('chunked')
854
+ expect(await res.json()).toEqual({ foo: 'bar' })
855
+ })
856
+
857
+ it('Should return text body - stream', async () => {
858
+ const { chunks, response: res } = await requestServerChunked(server, {
859
+ method: 'GET',
860
+ path: '/stream',
861
+ })
862
+ expect(res.status).toBe(200)
863
+ expect(res.headers.get('content-length')).toBeNull()
864
+ expect(res.headers.get('content-type')).toMatch('text/event-stream')
865
+ expect(res.headers.get('transfer-encoding')).toMatch('chunked')
866
+ expect(chunks.map((chunk) => chunk.toString())).toEqual(['data: Hello!\n\n', 'data: end\n\n'])
867
+ })
868
+
869
+ it('Should return error - stream without app crashing', async () => {
870
+ const result = requestServer(server, { method: 'GET', path: '/error-stream' })
871
+ await expect(result).rejects.toThrow('aborted')
872
+ })
873
+ })
874
+
875
+ describe('SSL', () => {
876
+ const app = new App()
877
+ app.get('/', (c) => c.text('Hello! Node!'))
878
+ app.get('/url', (c) => c.text(c.req.url))
879
+
880
+ const server = createAdaptorServer({
881
+ fetch: app.fetch,
882
+ createServer: createHTTPSServer,
883
+ serverOptions: {
884
+ key: fs.readFileSync('tests/adapter/fixtures/keys/agent1-key.pem'),
885
+ cert: fs.readFileSync('tests/adapter/fixtures/keys/agent1-cert.pem'),
886
+ },
887
+ })
888
+
889
+ it('Should return 200 response - GET /', async () => {
890
+ const res = await requestServer(server, { method: 'GET', path: '/' })
891
+ expect(res.status).toBe(200)
892
+ expect(res.headers.get('content-type')).toMatch('text/plain')
893
+ expect(await res.text()).toBe('Hello! Node!')
894
+ })
895
+
896
+ it('Should return 200 response - GET /url', async () => {
897
+ const res = await requestServer(server, { method: 'GET', path: '/url' })
898
+ expect(res.status).toBe(200)
899
+ expect(res.headers.get('content-type')).toMatch('text/plain')
900
+ const url = new URL(await res.text())
901
+ expect(url.pathname).toBe('/url')
902
+ expect(url.hostname).toBe('127.0.0.1')
903
+ expect(url.protocol).toBe('https:')
904
+ })
905
+ })
906
+
907
+ describe('HTTP2', () => {
908
+ const app = new App()
909
+ app.get('/', (c) => c.text('Hello! Node!'))
910
+ app.get('/headers', (c) => {
911
+ // call newRequestFromIncoming
912
+ c.req.header('Accept')
913
+ return c.text('Hello! Node!')
914
+ })
915
+ app.get('/url', (c) => c.text(c.req.url))
916
+
917
+ const server = createAdaptorServer({
918
+ fetch: app.fetch,
919
+ createServer: createHttp2Server,
920
+ })
921
+
922
+ it('Should return 200 response - GET /', async () => {
923
+ const res = await requestServerHttp2(server, { method: 'GET', path: '/' })
924
+ expect(res.status).toBe(200)
925
+ expect(res.headers.get('content-type')).toMatch('text/plain')
926
+ expect(await res.text()).toBe('Hello! Node!')
927
+ })
928
+
929
+ it('Should return 200 response - GET /headers', async () => {
930
+ const res = await requestServerHttp2(server, { method: 'GET', path: '/headers' })
931
+ expect(res.status).toBe(200)
932
+ expect(res.headers.get('content-type')).toMatch('text/plain')
933
+ expect(await res.text()).toBe('Hello! Node!')
934
+ })
935
+
936
+ // Use :authority as the host for the url.
937
+ it('Should return 200 response - GET /url', async () => {
938
+ const res = await requestServerHttp2(server, {
939
+ method: 'GET',
940
+ path: '/url',
941
+ headers: { ':authority': '127.0.0.1', ':scheme': 'https' },
942
+ })
943
+ expect(res.status).toBe(200)
944
+ expect(res.headers.get('content-type')).toMatch('text/plain')
945
+ const url = new URL(await res.text())
946
+ expect(url.pathname).toBe('/url')
947
+ expect(url.hostname).toBe('127.0.0.1')
948
+ expect(url.protocol).toBe('https:')
949
+ })
950
+ })
951
+
952
+ describe('App compression default gzip', () => {
953
+ const app = new App()
954
+ app.use('*', compress())
955
+
956
+ app.notFound((c) => {
957
+ return c.text('Custom NotFound', 404)
958
+ })
959
+
960
+ app.onError((_, c) => {
961
+ return c.text('Custom Error!', 500)
962
+ })
963
+
964
+ app.get('/error', () => {
965
+ throw new Error()
966
+ })
967
+
968
+ app.get('/one', async (c) => {
969
+ let body = 'one'
970
+
971
+ for (let index = 0; index < 1000 * 1000; index++) {
972
+ body += ' one'
973
+ }
974
+ return c.text(body)
975
+ })
976
+
977
+ it('should return 200 response - GET /one', async () => {
978
+ const server = createAdaptorServer(app)
979
+ const res = await requestServer(server, {
980
+ method: 'GET',
981
+ path: '/one',
982
+ headers: { 'accept-encoding': 'gzip, deflate' },
983
+ })
984
+ expect(res.status).toBe(200)
985
+ expect(res.headers.get('content-type')).toMatch('text/plain')
986
+ expect(res.headers.get('content-encoding')).toMatch('gzip')
987
+ })
988
+
989
+ it('should return 404 Custom NotFound', async () => {
990
+ const server = createAdaptorServer(app)
991
+ const res = await requestServer(server, {
992
+ method: 'GET',
993
+ path: '/err',
994
+ headers: { 'accept-encoding': 'gzip, deflate' },
995
+ })
996
+ expect(res.status).toBe(404)
997
+ expect(gunzipSync(Buffer.from(await res.arrayBuffer())).toString()).toEqual('Custom NotFound')
998
+ expect(res.headers.get('content-type')).toEqual('text/plain; charset=UTF-8')
999
+ expect(res.headers.get('content-encoding')).toMatch('gzip')
1000
+ })
1001
+
1002
+ it('should return 500 Custom Error!', async () => {
1003
+ const server = createAdaptorServer(app)
1004
+ const res = await requestServer(server, {
1005
+ method: 'GET',
1006
+ path: '/error',
1007
+ headers: { 'accept-encoding': 'gzip, deflate' },
1008
+ })
1009
+ expect(res.status).toBe(500)
1010
+ expect(gunzipSync(Buffer.from(await res.arrayBuffer())).toString()).toEqual('Custom Error!')
1011
+ expect(res.headers.get('content-type')).toEqual('text/plain; charset=UTF-8')
1012
+ expect(res.headers.get('content-encoding')).toMatch('gzip')
1013
+ })
1014
+ })
1015
+
1016
+ describe('App compression deflate', () => {
1017
+ const app = new App()
1018
+ app.use('*', compress({ encoding: 'deflate' }))
1019
+
1020
+ app.notFound((c) => {
1021
+ return c.text('Custom NotFound', 404)
1022
+ })
1023
+
1024
+ app.onError((_, c) => {
1025
+ return c.text('Custom Error!', 500)
1026
+ })
1027
+
1028
+ app.get('/error', () => {
1029
+ throw new Error()
1030
+ })
1031
+
1032
+ app.get('/one', async (c) => {
1033
+ let body = 'one'
1034
+
1035
+ for (let index = 0; index < 1000 * 1000; index++) {
1036
+ body += ' one'
1037
+ }
1038
+ return c.text(body)
1039
+ })
1040
+
1041
+ it('should return 200 response - GET /one', async () => {
1042
+ const server = createAdaptorServer(app)
1043
+ const res = await requestServer(server, {
1044
+ method: 'GET',
1045
+ path: '/one',
1046
+ headers: { 'accept-encoding': 'gzip, deflate' },
1047
+ })
1048
+ expect(res.status).toBe(200)
1049
+ expect(res.headers.get('content-type')).toMatch('text/plain')
1050
+ expect(res.headers.get('content-encoding')).toMatch('deflate')
1051
+ })
1052
+
1053
+ it('should return 404 Custom NotFound', async () => {
1054
+ const server = createAdaptorServer(app)
1055
+ const res = await requestServer(server, {
1056
+ method: 'GET',
1057
+ path: '/err',
1058
+ headers: { 'accept-encoding': 'gzip, deflate' },
1059
+ })
1060
+ expect(res.status).toBe(404)
1061
+ expect(inflateSync(Buffer.from(await res.arrayBuffer())).toString()).toEqual('Custom NotFound')
1062
+ expect(res.headers.get('content-type')).toEqual('text/plain; charset=UTF-8')
1063
+ expect(res.headers.get('content-encoding')).toMatch('deflate')
1064
+ })
1065
+
1066
+ it('should return 500 Custom Error!', async () => {
1067
+ const server = createAdaptorServer(app)
1068
+ const res = await requestServer(server, {
1069
+ method: 'GET',
1070
+ path: '/error',
1071
+ headers: { 'accept-encoding': 'gzip, deflate' },
1072
+ })
1073
+ expect(res.status).toBe(500)
1074
+ expect(inflateSync(Buffer.from(await res.arrayBuffer())).toString()).toEqual('Custom Error!')
1075
+ expect(res.headers.get('content-type')).toEqual('text/plain; charset=UTF-8')
1076
+ expect(res.headers.get('content-encoding')).toMatch('deflate')
1077
+ })
1078
+ })
1079
+
1080
+ describe('set child response to c.res', () => {
1081
+ const app = new App()
1082
+ app.use('*', async (c, next) => {
1083
+ await next()
1084
+ c.res = new Response('', c.res)
1085
+ c.res.headers // If this is set, test fails
1086
+ })
1087
+
1088
+ app.get('/json', async (c) => {
1089
+ return c.json({})
1090
+ })
1091
+
1092
+ it('Should return 200 response - GET /json', async () => {
1093
+ const server = createAdaptorServer(app)
1094
+ const res = await requestServer(server, { method: 'GET', path: '/json' })
1095
+ expect(res.status).toBe(200)
1096
+ expect(res.headers.get('content-type')).toMatch('application/json')
1097
+ })
1098
+ })
1099
+
1100
+ describe('Headers appended to a raw Response after construction (issue #304)', () => {
1101
+ // Regression test: a handler returning `new Response(body, init)` and
1102
+ // appending headers (e.g. `Set-Cookie`) afterwards must not lose those
1103
+ // headers when middleware later clones the response via `new Response(...)`.
1104
+ const app = new App()
1105
+ app.use('*', async (c, next) => {
1106
+ await next()
1107
+ // Mimics what middleware such as `cors`/`compress` does internally.
1108
+ c.res = new Response(c.res.body, c.res)
1109
+ })
1110
+ app.post('/test', () => {
1111
+ const res = new Response('hello', {
1112
+ status: 200,
1113
+ headers: { 'Content-Type': 'text/plain' },
1114
+ })
1115
+ res.headers.append('Set-Cookie', 'session=abc; Path=/; HttpOnly')
1116
+ return res
1117
+ })
1118
+
1119
+ it('Should preserve the appended Set-Cookie header', async () => {
1120
+ const server = createAdaptorServer(app)
1121
+ const res = await requestServer(server, { method: 'POST', path: '/test' })
1122
+ expect(res.status).toBe(200)
1123
+ expect(await res.text()).toBe('hello')
1124
+ expect(res.headers.get('content-type')).toMatch('text/plain')
1125
+ expect(res.headers.getSetCookie()).toEqual(['session=abc; Path=/; HttpOnly'])
1126
+ })
1127
+ })
1128
+
1129
+ describe('forwarding IncomingMessage and ServerResponse in env', () => {
1130
+ const app = new App<{ Bindings: HttpBindings }>()
1131
+ app.get('/', (c) =>
1132
+ c.json({
1133
+ incoming: c.env.incoming.constructor.name,
1134
+ url: c.env.incoming.url,
1135
+ outgoing: c.env.outgoing.constructor.name,
1136
+ status: c.env.outgoing.statusCode,
1137
+ })
1138
+ )
1139
+
1140
+ it('Should add `incoming` and `outgoing` to env', async () => {
1141
+ const server = createAdaptorServer(app)
1142
+ const res = await requestServer(server, { method: 'GET', path: '/' })
1143
+
1144
+ expect(res.status).toBe(200)
1145
+ const body = await res.json()
1146
+ expect(body.incoming).toBe('IncomingMessage')
1147
+ expect(body.url).toBe('/')
1148
+ expect(body.outgoing).toBe('ServerResponse')
1149
+ expect(body.status).toBe(200)
1150
+ })
1151
+ })
1152
+
1153
+ describe('overrideGlobalObjects', () => {
1154
+ const app = new App()
1155
+
1156
+ beforeEach(() => {
1157
+ Object.defineProperty(global, 'Headers', {
1158
+ value: GlobalHeaders,
1159
+ writable: true,
1160
+ })
1161
+ Object.defineProperty(global, 'Request', {
1162
+ value: GlobalRequest,
1163
+ writable: true,
1164
+ })
1165
+ Object.defineProperty(global, 'Response', {
1166
+ value: GlobalResponse,
1167
+ writable: true,
1168
+ })
1169
+ })
1170
+
1171
+ describe('default', () => {
1172
+ it('Should be overridden', () => {
1173
+ createAdaptorServer(app)
1174
+ expect(global.Headers).toBe(GlobalHeaders)
1175
+ expect(global.Request).toBe(LightweightRequest)
1176
+ expect(global.Response).toBe(LightweightResponse)
1177
+ })
1178
+ })
1179
+
1180
+ describe('overrideGlobalObjects: true', () => {
1181
+ it('Should be overridden', () => {
1182
+ createAdaptorServer({ overrideGlobalObjects: true, fetch: app.fetch })
1183
+ expect(global.Headers).toBe(GlobalHeaders)
1184
+ expect(global.Request).toBe(LightweightRequest)
1185
+ expect(global.Response).toBe(LightweightResponse)
1186
+ })
1187
+ })
1188
+
1189
+ describe('overrideGlobalObjects: false', () => {
1190
+ it('Should not be overridden', () => {
1191
+ createAdaptorServer({ overrideGlobalObjects: false, fetch: app.fetch })
1192
+ expect(global.Headers).toBe(GlobalHeaders)
1193
+ expect(global.Request).toBe(GlobalRequest)
1194
+ expect(global.Response).toBe(GlobalResponse)
1195
+ })
1196
+ })
1197
+ })
1198
+
1199
+ describe('Memory leak test', () => {
1200
+ let counter = 0
1201
+ const registry = new FinalizationRegistry(() => {
1202
+ counter--
1203
+ })
1204
+ const app = new App()
1205
+ const server = createAdaptorServer(app)
1206
+
1207
+ let onAbort: () => void
1208
+ let reqReadyResolve: () => void
1209
+ let reqReadyPromise: Promise<void>
1210
+
1211
+ app.use(async (c, next) => {
1212
+ counter++
1213
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1214
+ registry.register((c.req.raw as any)[getAbortController](), 'abortController')
1215
+ await next()
1216
+ })
1217
+ app.get('/', (c) => c.text('Hello! Node!'))
1218
+ app.post('/', async (c) => c.json(await c.req.json()))
1219
+ app.get('/abort', async (c) => {
1220
+ c.req.raw.signal.addEventListener('abort', () => onAbort())
1221
+ reqReadyResolve?.()
1222
+ await new Promise(() => {}) // never resolve
1223
+ })
1224
+
1225
+ beforeEach(() => {
1226
+ counter = 0
1227
+ reqReadyPromise = new Promise<void>((r) => {
1228
+ reqReadyResolve = r
1229
+ })
1230
+ })
1231
+
1232
+ // keep the server up across all tests so every request shares the same
1233
+ // environment, as the GC assertions are sensitive to their surroundings
1234
+ beforeAll(
1235
+ () =>
1236
+ new Promise<void>((resolve, reject) => {
1237
+ server.once('error', reject)
1238
+ server.listen(0, '127.0.0.1', resolve)
1239
+ })
1240
+ )
1241
+
1242
+ afterAll(() => {
1243
+ server.close()
1244
+ })
1245
+
1246
+ it('Should not have memory leak - GET /', async () => {
1247
+ await requestServer(server, { method: 'GET', path: '/' })
1248
+ global.gc?.()
1249
+ await new Promise((resolve) => setTimeout(resolve, 10))
1250
+ expect(counter).toBe(0)
1251
+ })
1252
+
1253
+ it('Should not have memory leak - POST /', async () => {
1254
+ await requestServer(server, {
1255
+ method: 'POST',
1256
+ path: '/',
1257
+ headers: { 'content-type': 'application/json' },
1258
+ body: JSON.stringify({ foo: 'bar' }),
1259
+ })
1260
+ global.gc?.()
1261
+ await new Promise((resolve) => setTimeout(resolve, 10))
1262
+ expect(counter).toBe(0)
1263
+ })
1264
+
1265
+ it('Should not have memory leak - GET /abort', async () => {
1266
+ const abortedPromise = new Promise<void>((resolve) => {
1267
+ onAbort = resolve
1268
+ })
1269
+
1270
+ const controller = new AbortController()
1271
+ const resPromise = requestServer(server, {
1272
+ method: 'GET',
1273
+ path: '/abort',
1274
+ signal: controller.signal,
1275
+ }).catch(() => {})
1276
+ await reqReadyPromise
1277
+ controller.abort()
1278
+ await abortedPromise
1279
+ await resPromise
1280
+ await new Promise((resolve) => setTimeout(resolve, 10))
1281
+
1282
+ global.gc?.()
1283
+ await new Promise((resolve) => setTimeout(resolve, 10))
1284
+ expect(counter).toBe(0)
1285
+ })
1286
+ })
1287
+
1288
+ describe('serve', () => {
1289
+ const app = new App()
1290
+ app.get('/', (c) => c.newResponse(null, 200))
1291
+ serve(app)
1292
+
1293
+ it('should serve on ipv4', async () => {
1294
+ const response = await fetch('http://localhost:3000')
1295
+ expect(response.status).toBe(200)
1296
+ })
1297
+
1298
+ it('should serve on ipv6', async () => {
1299
+ const response = await fetch('http://[::1]:3000')
1300
+ expect(response.status).toBe(200)
1301
+ })
1302
+ })