acrylblends 0.6.3 → 0.7.0

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 (285) hide show
  1. package/README.md +4 -3
  2. package/docs/framework/README.md +2 -2
  3. package/docs/framework/UPSTREAM.md +1 -1
  4. package/docs/framework/guides/jsx.md +51 -1
  5. package/docs/framework/helpers/ssg.md +3 -6
  6. package/docs/framework/middleware/builtin/jsx-renderer.md +0 -3
  7. package/docs/framework/upstream.json +2 -2
  8. package/lib/accepts.d.ts +1 -1
  9. package/lib/acryl-base.d.ts +1 -1
  10. package/lib/adapter.d.ts +1 -1
  11. package/lib/{app-wFwpg-bt.d.ts → app-D8riatvv.d.ts} +1 -1
  12. package/lib/{base-ddiVmiE7.js → base-NSN_SNv-.js} +1 -0
  13. package/lib/basic-auth.d.ts +1 -1
  14. package/lib/bearer-auth.d.ts +1 -1
  15. package/lib/body-limit.d.ts +1 -1
  16. package/lib/cache.d.ts +1 -1
  17. package/lib/cache.js +4 -4
  18. package/lib/{client-BHTr318g.js → client-DDLXwhBo.js} +2 -125
  19. package/lib/client.d.ts +2 -2
  20. package/lib/client.js +2 -1
  21. package/lib/combine.d.ts +1 -1
  22. package/lib/{components-DYZdxJIJ.js → components-JXVm45kK.js} +1 -1
  23. package/lib/{components-Ce5FRyHU.js → components-cEW3ovK0.js} +3 -3
  24. package/lib/compress.d.ts +1 -1
  25. package/lib/conninfo.d.ts +2 -2
  26. package/lib/{context-DVQBjuns.d.ts → context-DS3fYyG-.d.ts} +1 -1
  27. package/lib/context-storage.d.ts +1 -1
  28. package/lib/cookie.d.ts +1 -1
  29. package/lib/cors.d.ts +1 -1
  30. package/lib/csrf.d.ts +1 -1
  31. package/lib/dev.d.ts +2 -2
  32. package/lib/etag.d.ts +1 -1
  33. package/lib/factory.d.ts +2 -2
  34. package/lib/{hooks-Bq5q9IMl.js → hooks-w72iLeFR.js} +6 -6
  35. package/lib/{index-CUTfU69R.d.ts → index-Bb7YJKCq.d.ts} +2 -2
  36. package/lib/{index-CsvB-F9j.d.ts → index-BvFh1Gfx.d.ts} +1 -1
  37. package/lib/{index-Cqcdluyn.d.ts → index-CTy4N7ng.d.ts} +2 -2
  38. package/lib/index.d.ts +4 -4
  39. package/lib/ip-restriction.d.ts +2 -2
  40. package/lib/jsx/dom/client.js +1 -1
  41. package/lib/jsx/dom/jsx-dev-runtime.js +1 -1
  42. package/lib/jsx/dom/jsx-runtime.js +1 -1
  43. package/lib/jsx/dom/server.js +2 -2
  44. package/lib/jsx/dom.js +4 -4
  45. package/lib/jsx/jsx-dev-runtime.js +1 -1
  46. package/lib/jsx/jsx-runtime.js +1 -1
  47. package/lib/jsx/streaming.js +1 -1
  48. package/lib/{jsx-dev-runtime-kAhR_4C7.js → jsx-dev-runtime-CWLRA2F3.js} +2 -2
  49. package/lib/jsx-renderer.d.ts +75 -0
  50. package/lib/jsx-renderer.js +99 -0
  51. package/lib/jsx.js +4 -4
  52. package/lib/jwk.d.ts +1 -1
  53. package/lib/jwt.d.ts +1 -1
  54. package/lib/language.d.ts +1 -1
  55. package/lib/logger.d.ts +1 -1
  56. package/lib/loopback-only.d.ts +1 -1
  57. package/lib/method-not-allowed.d.ts +2 -2
  58. package/lib/method-override.d.ts +2 -2
  59. package/lib/mount.d.ts +1 -1
  60. package/lib/node-server/conninfo.d.ts +1 -1
  61. package/lib/node-server/register.d.ts +1 -0
  62. package/lib/node-server/register.js +73 -0
  63. package/lib/node-server/serve-static.d.ts +1 -1
  64. package/lib/node-server/serve-static.js +1 -1
  65. package/lib/node-server.d.ts +1 -1
  66. package/lib/node-server.js +1 -1
  67. package/lib/powered-by.d.ts +1 -1
  68. package/lib/pretty-json.d.ts +1 -1
  69. package/lib/quick.d.ts +1 -1
  70. package/lib/request-id.d.ts +1 -1
  71. package/lib/request.d.ts +1 -1
  72. package/lib/route.d.ts +1 -1
  73. package/lib/schema-validator.d.ts +1 -1
  74. package/lib/secure-headers.d.ts +1 -1
  75. package/lib/{serve-static-Cw23shXv.js → serve-static-B14MF1Va.js} +1 -0
  76. package/lib/serve-static.d.ts +1 -1
  77. package/lib/ssg-BwjcA-Ge.js +389 -0
  78. package/lib/ssg.d.ts +156 -0
  79. package/lib/ssg.js +2 -0
  80. package/lib/streaming.d.ts +1 -1
  81. package/lib/testing.d.ts +3 -3
  82. package/lib/testing.js +1 -1
  83. package/lib/timeout.d.ts +1 -1
  84. package/lib/timing.d.ts +1 -1
  85. package/lib/tiny.d.ts +1 -1
  86. package/lib/trailing-slash.d.ts +1 -1
  87. package/lib/{types-nEL8VPDC.d.ts → types-Dv_nV2cv.d.ts} +1 -1
  88. package/lib/{types-CQiGmsWW.d.ts → types-zNxyEpG2.d.ts} +2 -2
  89. package/lib/types.d.ts +1 -1
  90. package/lib/utils/body.d.ts +1 -1
  91. package/lib/utils/ipaddr.d.ts +1 -1
  92. package/lib/utils-Cx61GFMC.js +125 -0
  93. package/lib/validator.d.ts +1 -1
  94. package/lib/ws.d.ts +1 -1
  95. package/package.json +13 -1
  96. package/plugins/acryl-inspect/upstream.json +1 -1
  97. package/plugins/acryl-node-server/src/register.ts +86 -0
  98. package/plugins/acryl-node-server/upstream.json +5 -3
  99. package/plugins/acryl-router/src/core/helper/ssg/index.ts +14 -0
  100. package/plugins/acryl-router/src/core/helper/ssg/middleware.ts +83 -0
  101. package/plugins/acryl-router/src/core/helper/ssg/plugins.ts +72 -0
  102. package/plugins/acryl-router/src/core/helper/ssg/ssg.ts +471 -0
  103. package/plugins/acryl-router/src/core/helper/ssg/utils.ts +118 -0
  104. package/plugins/acryl-router/src/core/jsx/dom/render.ts +6 -6
  105. package/plugins/acryl-router/src/core/middleware/cache/index.ts +4 -4
  106. package/plugins/acryl-router/src/core/middleware/jsx-renderer/index.ts +162 -0
  107. package/plugins/acryl-router/src/helpers/index.ts +1 -0
  108. package/plugins/acryl-router/src/middleware/index.ts +1 -0
  109. package/plugins/acryl-router/upstream.json +2 -2
  110. package/plugins/acryl-router-http/upstream.json +1 -1
  111. package/plugins/acryl-state/upstream.json +1 -1
  112. package/plugins/desktop/main.ts +4 -1
  113. package/plugins/desktop/upstream.json +1 -1
  114. package/plugins/desktop-web/main.ts +3 -1
  115. package/plugins/desktop-web/upstream.json +1 -1
  116. package/plugins/extensions/resolve.ts +4 -0
  117. package/shared/AGENTS.core.md +3 -0
  118. package/templates/agent/package.json +7 -3
  119. package/templates/agent/tsconfig.json +10 -2
  120. package/templates/blank/package.json +7 -3
  121. package/templates/blank/src/about.tsx +14 -0
  122. package/templates/blank/src/routes.ts +2 -0
  123. package/templates/blank/test/app.test.ts +15 -0
  124. package/templates/blank/tsconfig.json +3 -1
  125. package/templates/todo/package.json +7 -3
  126. package/templates/todo/tsconfig.json +3 -1
  127. package/templates/todo/upstream.json +1 -1
  128. package/plugins/acryl-inspect/tests/report.spec.ts +0 -69
  129. package/plugins/acryl-inspect/tests/service.spec.ts +0 -121
  130. package/plugins/acryl-node-server/tests/adapter/app.ts +0 -76
  131. package/plugins/acryl-node-server/tests/adapter/assets/.static/plain.txt +0 -1
  132. package/plugins/acryl-node-server/tests/adapter/assets/favicon.ico +0 -0
  133. package/plugins/acryl-node-server/tests/adapter/assets/secret.txt +0 -1
  134. package/plugins/acryl-node-server/tests/adapter/assets/static/acryl.html +0 -1
  135. package/plugins/acryl-node-server/tests/adapter/assets/static/admin/secret.txt +0 -1
  136. package/plugins/acryl-node-server/tests/adapter/assets/static/data.json +0 -5
  137. package/plugins/acryl-node-server/tests/adapter/assets/static/extensionless +0 -1
  138. package/plugins/acryl-node-server/tests/adapter/assets/static/foo..bar.txt +0 -0
  139. package/plugins/acryl-node-server/tests/adapter/assets/static/index.html +0 -1
  140. package/plugins/acryl-node-server/tests/adapter/assets/static/plain.txt +0 -1
  141. package/plugins/acryl-node-server/tests/adapter/assets/static-with-context-path/plain.txt +0 -1
  142. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.bin +0 -1
  143. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.bin.br +0 -1
  144. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt +0 -1
  145. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt.br +0 -1
  146. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt.zst +0 -1
  147. package/plugins/acryl-node-server/tests/adapter/conninfo.test.ts +0 -34
  148. package/plugins/acryl-node-server/tests/adapter/content-length.test.ts +0 -248
  149. package/plugins/acryl-node-server/tests/adapter/early-hints.test.ts +0 -457
  150. package/plugins/acryl-node-server/tests/adapter/fixtures/keys/agent1-cert.pem +0 -23
  151. package/plugins/acryl-node-server/tests/adapter/fixtures/keys/agent1-key.pem +0 -27
  152. package/plugins/acryl-node-server/tests/adapter/headers.test.ts +0 -252
  153. package/plugins/acryl-node-server/tests/adapter/helpers/request.ts +0 -190
  154. package/plugins/acryl-node-server/tests/adapter/listener.test.ts +0 -727
  155. package/plugins/acryl-node-server/tests/adapter/request-client-disconnect.test.ts +0 -189
  156. package/plugins/acryl-node-server/tests/adapter/request.test.ts +0 -1230
  157. package/plugins/acryl-node-server/tests/adapter/response.test.ts +0 -285
  158. package/plugins/acryl-node-server/tests/adapter/serve-static.test.ts +0 -849
  159. package/plugins/acryl-node-server/tests/adapter/server-socket.test.ts +0 -790
  160. package/plugins/acryl-node-server/tests/adapter/server.test.ts +0 -1312
  161. package/plugins/acryl-node-server/tests/adapter/setup.ts +0 -16
  162. package/plugins/acryl-node-server/tests/adapter/url.test.ts +0 -76
  163. package/plugins/acryl-node-server/tests/adapter/utils/response.test.ts +0 -23
  164. package/plugins/acryl-node-server/tests/adapter/utils/stream.test.ts +0 -75
  165. package/plugins/acryl-node-server/tests/adapter/utils.test.ts +0 -235
  166. package/plugins/acryl-node-server/tests/adapter/websocket.test.ts +0 -260
  167. package/plugins/acryl-node-server/tests/server.spec.ts +0 -141
  168. package/plugins/acryl-router/src/core/app.test.ts +0 -3833
  169. package/plugins/acryl-router/src/core/client/client.test.ts +0 -2313
  170. package/plugins/acryl-router/src/core/client/types.test.ts +0 -149
  171. package/plugins/acryl-router/src/core/client/utils.test.ts +0 -405
  172. package/plugins/acryl-router/src/core/compose.test.ts +0 -751
  173. package/plugins/acryl-router/src/core/context.test.ts +0 -629
  174. package/plugins/acryl-router/src/core/helper/accepts/accepts.test.ts +0 -344
  175. package/plugins/acryl-router/src/core/helper/adapter/index.test.ts +0 -55
  176. package/plugins/acryl-router/src/core/helper/cookie/index.test.ts +0 -510
  177. package/plugins/acryl-router/src/core/helper/css/common.case.test.tsx +0 -506
  178. package/plugins/acryl-router/src/core/helper/css/index.test.tsx +0 -678
  179. package/plugins/acryl-router/src/core/helper/dev/index.test.ts +0 -181
  180. package/plugins/acryl-router/src/core/helper/factory/index.test.ts +0 -434
  181. package/plugins/acryl-router/src/core/helper/html/index.test.ts +0 -78
  182. package/plugins/acryl-router/src/core/helper/proxy/index.test.ts +0 -356
  183. package/plugins/acryl-router/src/core/helper/route/index.test.ts +0 -259
  184. package/plugins/acryl-router/src/core/helper/streaming/sse.test.tsx +0 -557
  185. package/plugins/acryl-router/src/core/helper/streaming/stream.test.ts +0 -128
  186. package/plugins/acryl-router/src/core/helper/streaming/text.test.ts +0 -34
  187. package/plugins/acryl-router/src/core/helper/testing/index.test.ts +0 -42
  188. package/plugins/acryl-router/src/core/helper/websocket/index.test.ts +0 -124
  189. package/plugins/acryl-router/src/core/http-exception.test.ts +0 -45
  190. package/plugins/acryl-router/src/core/jsx/base.test.tsx +0 -97
  191. package/plugins/acryl-router/src/core/jsx/children.test.ts +0 -52
  192. package/plugins/acryl-router/src/core/jsx/components.test.tsx +0 -936
  193. package/plugins/acryl-router/src/core/jsx/context-isolation.test.ts +0 -655
  194. package/plugins/acryl-router/src/core/jsx/dom/client.test.tsx +0 -128
  195. package/plugins/acryl-router/src/core/jsx/dom/components.test.tsx +0 -330
  196. package/plugins/acryl-router/src/core/jsx/dom/context.test.tsx +0 -184
  197. package/plugins/acryl-router/src/core/jsx/dom/css.test.tsx +0 -149
  198. package/plugins/acryl-router/src/core/jsx/dom/hooks/index.test.tsx +0 -172
  199. package/plugins/acryl-router/src/core/jsx/dom/index.test.tsx +0 -3681
  200. package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.test.tsx +0 -1344
  201. package/plugins/acryl-router/src/core/jsx/dom/server.test.tsx +0 -199
  202. package/plugins/acryl-router/src/core/jsx/hooks/dom.test.tsx +0 -859
  203. package/plugins/acryl-router/src/core/jsx/hooks/string.test.tsx +0 -40
  204. package/plugins/acryl-router/src/core/jsx/index.test.tsx +0 -1744
  205. package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.test.tsx +0 -666
  206. package/plugins/acryl-router/src/core/jsx/jsx-runtime.test.tsx +0 -85
  207. package/plugins/acryl-router/src/core/jsx/streaming.test.tsx +0 -1281
  208. package/plugins/acryl-router/src/core/jsx/utils.test.ts +0 -366
  209. package/plugins/acryl-router/src/core/middleware/basic-auth/index.test.ts +0 -434
  210. package/plugins/acryl-router/src/core/middleware/bearer-auth/index.test.ts +0 -1018
  211. package/plugins/acryl-router/src/core/middleware/body-limit/index.test.ts +0 -273
  212. package/plugins/acryl-router/src/core/middleware/cache/index.test.ts +0 -1211
  213. package/plugins/acryl-router/src/core/middleware/combine/index.test.ts +0 -355
  214. package/plugins/acryl-router/src/core/middleware/compress/index.test.ts +0 -535
  215. package/plugins/acryl-router/src/core/middleware/context-storage/index.test.ts +0 -43
  216. package/plugins/acryl-router/src/core/middleware/cors/index.test.ts +0 -532
  217. package/plugins/acryl-router/src/core/middleware/csrf/index.test.ts +0 -534
  218. package/plugins/acryl-router/src/core/middleware/etag/digest.test.ts +0 -21
  219. package/plugins/acryl-router/src/core/middleware/etag/index.test.ts +0 -503
  220. package/plugins/acryl-router/src/core/middleware/ip-restriction/index.test.ts +0 -217
  221. package/plugins/acryl-router/src/core/middleware/jwk/index.test.ts +0 -1135
  222. package/plugins/acryl-router/src/core/middleware/jwk/keys.test.json +0 -48
  223. package/plugins/acryl-router/src/core/middleware/jwt/index.test.ts +0 -802
  224. package/plugins/acryl-router/src/core/middleware/language/index.test.ts +0 -552
  225. package/plugins/acryl-router/src/core/middleware/logger/index.test.ts +0 -204
  226. package/plugins/acryl-router/src/core/middleware/method-not-allowed/index.test.ts +0 -292
  227. package/plugins/acryl-router/src/core/middleware/method-override/index.test.ts +0 -221
  228. package/plugins/acryl-router/src/core/middleware/mount/index.test.ts +0 -334
  229. package/plugins/acryl-router/src/core/middleware/powered-by/index.test.ts +0 -37
  230. package/plugins/acryl-router/src/core/middleware/pretty-json/index.test.ts +0 -218
  231. package/plugins/acryl-router/src/core/middleware/request-id/index.test.ts +0 -168
  232. package/plugins/acryl-router/src/core/middleware/secure-headers/index.test.ts +0 -671
  233. package/plugins/acryl-router/src/core/middleware/serve-static/index.test.ts +0 -450
  234. package/plugins/acryl-router/src/core/middleware/serve-static/path.test.ts +0 -50
  235. package/plugins/acryl-router/src/core/middleware/timeout/index.test.ts +0 -70
  236. package/plugins/acryl-router/src/core/middleware/timing/index.test.ts +0 -310
  237. package/plugins/acryl-router/src/core/middleware/trailing-slash/index.test.ts +0 -381
  238. package/plugins/acryl-router/src/core/preset/quick.test.ts +0 -42
  239. package/plugins/acryl-router/src/core/preset/tiny.test.ts +0 -42
  240. package/plugins/acryl-router/src/core/request.test.ts +0 -751
  241. package/plugins/acryl-router/src/core/router/common.case.test.ts +0 -1132
  242. package/plugins/acryl-router/src/core/router/linear-router/router.test.ts +0 -80
  243. package/plugins/acryl-router/src/core/router/pattern-router/router.test.ts +0 -46
  244. package/plugins/acryl-router/src/core/router/reg-exp-router/prepared-router.test.ts +0 -154
  245. package/plugins/acryl-router/src/core/router/reg-exp-router/router.test.ts +0 -265
  246. package/plugins/acryl-router/src/core/router/smart-router/router.test.ts +0 -13
  247. package/plugins/acryl-router/src/core/router/trie-router/node.test.ts +0 -896
  248. package/plugins/acryl-router/src/core/router/trie-router/router.test.ts +0 -8
  249. package/plugins/acryl-router/src/core/types.test.ts +0 -4134
  250. package/plugins/acryl-router/src/core/utils/accept.test.ts +0 -299
  251. package/plugins/acryl-router/src/core/utils/basic-auth.test.ts +0 -56
  252. package/plugins/acryl-router/src/core/utils/body.test.ts +0 -496
  253. package/plugins/acryl-router/src/core/utils/buffer.test.ts +0 -147
  254. package/plugins/acryl-router/src/core/utils/color.test.ts +0 -23
  255. package/plugins/acryl-router/src/core/utils/concurrent.test.ts +0 -69
  256. package/plugins/acryl-router/src/core/utils/cookie.test.ts +0 -460
  257. package/plugins/acryl-router/src/core/utils/crypto.test.ts +0 -43
  258. package/plugins/acryl-router/src/core/utils/encode.test.ts +0 -61
  259. package/plugins/acryl-router/src/core/utils/filepath.test.ts +0 -78
  260. package/plugins/acryl-router/src/core/utils/html.test.ts +0 -16
  261. package/plugins/acryl-router/src/core/utils/ipaddr.test.ts +0 -149
  262. package/plugins/acryl-router/src/core/utils/jwt/jwa.test.ts +0 -86
  263. package/plugins/acryl-router/src/core/utils/jwt/jwt.test.ts +0 -1788
  264. package/plugins/acryl-router/src/core/utils/mime.test.ts +0 -64
  265. package/plugins/acryl-router/src/core/utils/stream.test.ts +0 -212
  266. package/plugins/acryl-router/src/core/utils/types.test.ts +0 -295
  267. package/plugins/acryl-router/src/core/utils/url.test.ts +0 -384
  268. package/plugins/acryl-router/src/core/validator/utils.test.ts +0 -82
  269. package/plugins/acryl-router/src/core/validator/validator.test.ts +0 -1488
  270. package/plugins/acryl-router/tests/docs-examples.spec.ts +0 -152
  271. package/plugins/acryl-router/tests/jsx.spec.tsx +0 -63
  272. package/plugins/acryl-router/tests/live-install.spec.ts +0 -64
  273. package/plugins/acryl-router/tests/loader.spec.ts +0 -72
  274. package/plugins/acryl-router/tests/loopback-only.spec.ts +0 -80
  275. package/plugins/acryl-router/tests/no-dom/consumer.ts +0 -11
  276. package/plugins/acryl-router/tests/no-dom/tsconfig.json +0 -13
  277. package/plugins/acryl-router/tests/service.spec.ts +0 -426
  278. package/plugins/acryl-router/tests/shadow-state.spec.ts +0 -79
  279. package/plugins/acryl-router-http/tests/adapter.spec.ts +0 -174
  280. package/plugins/acryl-router-http/tests/stream.spec.ts +0 -133
  281. package/plugins/acryl-router-http/tests/surfaces.spec.ts +0 -101
  282. package/plugins/acryl-state/src/react.ts +0 -11
  283. package/plugins/acryl-state/tests/cell.spec.ts +0 -71
  284. package/plugins/acryl-state/tests/react.spec.ts +0 -39
  285. package/plugins/acryl-state/tests/service.spec.ts +0 -132
@@ -1,1211 +0,0 @@
1
- import { vi } from 'vitest'
2
- import { cache } from './index.ts'
3
- import type { ExecutionContext } from '../../context.ts'
4
- import { Acryl } from '../../app.ts'
5
-
6
- // Mock
7
- type StoreMap = Map<string | Request, Response>
8
-
9
- class MockCache {
10
- name: string
11
- store: StoreMap
12
-
13
- constructor(name: string, store: StoreMap) {
14
- this.name = name
15
- this.store = store
16
- }
17
-
18
- async match(key: Request | string): Promise<Response | null> {
19
- return this.store.get(key) || null
20
- }
21
-
22
- async keys() {
23
- return this.store.keys()
24
- }
25
-
26
- async put(key: Request | string, response: Response): Promise<void> {
27
- this.store.set(key, response)
28
- }
29
- }
30
-
31
- const globalStore: StoreMap = new Map()
32
-
33
- vi.stubGlobal('caches', {
34
- open: (name: string) => {
35
- return new MockCache(name, globalStore)
36
- },
37
- })
38
-
39
- class Context implements ExecutionContext {
40
- passThroughOnException(): void {
41
- throw new Error('Method not implemented.')
42
- }
43
- async waitUntil(promise: Promise<unknown>): Promise<void> {
44
- await promise
45
- }
46
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
47
- props: any
48
- }
49
-
50
- describe('Customizing Caching Keys', () => {
51
- const app = new Acryl()
52
-
53
- const dynamicCacheName = 'dynamic-cache-name'
54
- app.use(
55
- '/dynamic-cache-name/*',
56
- cache({
57
- cacheName: async () => dynamicCacheName,
58
- wait: true,
59
- cacheControl: 'max-age=10',
60
- })
61
- )
62
- app.get('/dynamic-cache-name/', (c) => {
63
- return c.text('cached')
64
- })
65
-
66
- const dynamicCacheKey = 'dynamic-cache-key'
67
- app.use(
68
- '/dynamic-cache-key/*',
69
- cache({
70
- cacheName: 'my-app-v1',
71
- wait: true,
72
- cacheControl: 'max-age=10',
73
- keyGenerator: async () => dynamicCacheKey,
74
- })
75
- )
76
- app.get('/dynamic-cache-key/', (c) => {
77
- return c.text('cached')
78
- })
79
-
80
- app.use(
81
- '/dynamic-cache/*',
82
- cache({
83
- cacheName: async () => dynamicCacheName,
84
- cacheControl: 'max-age=10',
85
- keyGenerator: async () => dynamicCacheKey,
86
- })
87
- )
88
- app.get('/dynamic-cache/', (c) => {
89
- return c.text('cached')
90
- })
91
-
92
- const ctx = new Context()
93
-
94
- it('Should use dynamically generated cache name', async () => {
95
- await app.request('http://localhost/dynamic-cache-name/', undefined, ctx)
96
- const cache = await caches.open(dynamicCacheName)
97
- const keys = Array.from(await cache.keys())
98
- expect(keys.length).toBe(1)
99
- })
100
-
101
- it('Should use dynamically generated cache key', async () => {
102
- await app.request('http://localhost/dynamic-cache-key/')
103
- const cache = await caches.open('my-app-v1')
104
- const response = await cache.match(
105
- 'http://localhost/.acryl-router/cache?__hono_cache_key=dynamic-cache-key&__hono_cache_method=GET'
106
- )
107
- expect(response).not.toBeNull()
108
- })
109
-
110
- it('Should retrieve cached response with dynamic cache name and key', async () => {
111
- await app.request('http://localhost/dynamic-cache/', undefined, undefined, ctx)
112
- const res = await app.request('http://localhost/dynamic-cache/', undefined, undefined, ctx)
113
- expect(res).not.toBeNull()
114
- expect(res.status).toBe(200)
115
- expect(res.headers.get('cache-control')).toBe('max-age=10')
116
- })
117
- })
118
-
119
- describe('Cache Middleware', () => {
120
- const app = new Acryl()
121
-
122
- let count = 1
123
- // wait, because this is test.
124
- // You don't have to set `wait: true`.
125
- app.use('/wait/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' }))
126
- app.get('/wait/', (c) => {
127
- c.header('X-Count', `${count}`)
128
- count++
129
- return c.text('cached')
130
- })
131
-
132
- // Default, use `waitUntil`
133
- app.use('/not-wait/*', cache({ cacheName: 'my-app-v1', cacheControl: 'max-age=10' }))
134
- app.get('/not-wait/', (c) => {
135
- return c.text('not cached')
136
- })
137
-
138
- app.use('/wait2/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' }))
139
- app.use('/wait2/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'Max-Age=20' }))
140
- app.get('/wait2/', (c) => {
141
- return c.text('cached')
142
- })
143
-
144
- app.use('/wait3/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' }))
145
- app.use(
146
- '/wait3/private/*',
147
- cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'private' })
148
- )
149
- app.get('/wait3/private/', (c) => {
150
- return c.text('cached')
151
- })
152
-
153
- app.use('/wait4/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' }))
154
- app.get('/wait4/', (c) => {
155
- c.header('Cache-Control', 'private')
156
- return c.text('cached')
157
- })
158
-
159
- app.use('/wait5/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' }))
160
- app.get('/wait5/', (c) => {
161
- c.header('Cache-Control', 'Max-Age=3600')
162
- return c.text('cached')
163
- })
164
-
165
- app.use('/vary1/*', cache({ cacheName: 'my-app-v1', wait: true, vary: ['Accept'] }))
166
- app.get('/vary1/', (c) => {
167
- return c.text('cached')
168
- })
169
-
170
- app.use('/vary2/*', cache({ cacheName: 'my-app-v1', wait: true, vary: ['Accept'] }))
171
- app.get('/vary2/', (c) => {
172
- c.header('Vary', 'Accept-Encoding')
173
- return c.text('cached')
174
- })
175
-
176
- app.use(
177
- '/vary3/*',
178
- cache({ cacheName: 'my-app-v1', wait: true, vary: ['Accept', 'Accept-Encoding'] })
179
- )
180
- app.get('/vary3/', (c) => {
181
- c.header('Vary', 'Accept-Language')
182
- return c.text('cached')
183
- })
184
-
185
- app.use(
186
- '/vary4/*',
187
- cache({ cacheName: 'my-app-v1', wait: true, vary: ['Accept', 'Accept-Encoding'] })
188
- )
189
- app.get('/vary4/', (c) => {
190
- c.header('Vary', 'Accept, Accept-Language')
191
- return c.text('cached')
192
- })
193
-
194
- app.use('/vary5/*', cache({ cacheName: 'my-app-v1', wait: true, vary: 'Accept' }))
195
- app.get('/vary5/', (c) => {
196
- return c.text('cached with Accept and Accept-Encoding headers')
197
- })
198
-
199
- app.use(
200
- '/vary6/*',
201
- cache({ cacheName: 'my-app-v1', wait: true, vary: 'Accept, Accept-Encoding' })
202
- )
203
- app.get('/vary6/', (c) => {
204
- c.header('Vary', 'Accept, Accept-Language')
205
- return c.text('cached with Accept and Accept-Encoding headers as array')
206
- })
207
-
208
- app.use('/vary7/*', cache({ cacheName: 'my-app-v1', wait: true, vary: ['Accept'] }))
209
- app.get('/vary7/', (c) => {
210
- c.header('Vary', '*')
211
- return c.text('cached')
212
- })
213
-
214
- app.use('/default/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' }))
215
- app.all('/default/:code/', (c) => {
216
- const code = parseInt(c.req.param('code'))
217
- // Intended to avoid the following error: `RangeError: init[“status”] must be in the range of 200 to 599, inclusive.`
218
- const res = {
219
- status: code,
220
- headers: new Headers(),
221
- clone: () => res,
222
- } as Response
223
- return res
224
- })
225
-
226
- app.use(
227
- '/custom/*',
228
- cache({
229
- cacheName: 'my-app-v1',
230
- wait: true,
231
- cacheControl: 'max-age=10',
232
- cacheableStatusCodes: [200, 201],
233
- })
234
- )
235
- app.get('/custom/:code/', (c) => {
236
- const code = parseInt(c.req.param('code'))
237
- // Intended to avoid the following error: `RangeError: init[“status”] must be in the range of 200 to 599, inclusive.`
238
- const res = {
239
- status: code,
240
- headers: new Headers(),
241
- clone: () => res,
242
- } as Response
243
- return res
244
- })
245
-
246
- const ctx = new Context()
247
-
248
- it('Should return cached response', async () => {
249
- await app.request('http://localhost/wait/')
250
- const res = await app.request('http://localhost/wait/')
251
- expect(res).not.toBeNull()
252
- expect(res.status).toBe(200)
253
- expect(res.headers.get('cache-control')).toBe('max-age=10')
254
- expect(res.headers.get('x-count')).toBe('1')
255
- })
256
-
257
- it('Should not return cached response', async () => {
258
- await app.fetch(new Request('http://localhost/not-wait/'), undefined, ctx)
259
- const res = await app.fetch(new Request('http://localhost/not-wait/'), undefined, ctx)
260
- expect(res).not.toBeNull()
261
- expect(res.status).toBe(200)
262
- expect(res.headers.get('cache-control')).toBe('max-age=10')
263
- })
264
-
265
- it('Should not return duplicate header values', async () => {
266
- const res = await app.request('/wait2/')
267
- expect(res).not.toBeNull()
268
- expect(res.status).toBe(200)
269
- expect(res.headers.get('cache-control')).toBe('max-age=20')
270
- })
271
-
272
- it('Should return composed middleware header values', async () => {
273
- const res = await app.request('/wait3/private/')
274
- expect(res).not.toBeNull()
275
- expect(res.status).toBe(200)
276
- expect(res.headers.get('cache-control')).toBe('private, max-age=10')
277
- })
278
-
279
- it('Should return composed handler header values', async () => {
280
- const res = await app.request('/wait4/')
281
- expect(res).not.toBeNull()
282
- expect(res.status).toBe(200)
283
- expect(res.headers.get('cache-control')).toBe('private, max-age=10')
284
- })
285
-
286
- it('Should not duplicate a Cache-Control directive that differs only in case', async () => {
287
- const res = await app.request('/wait5/')
288
- expect(res).not.toBeNull()
289
- expect(res.status).toBe(200)
290
- // Directive names are case-insensitive (RFC 7234 §5.2); the handler's
291
- // `Max-Age` must suppress the middleware's configured `max-age`.
292
- expect(res.headers.get('cache-control')).toBe('Max-Age=3600')
293
- })
294
-
295
- it('Should correctly apply a single Vary header from middleware', async () => {
296
- const res = await app.request('http://localhost/vary1/')
297
- expect(res).not.toBeNull()
298
- expect(res.status).toBe(200)
299
- expect(res.headers.get('vary')).toBe('accept')
300
- })
301
-
302
- it('Should merge Vary headers from middleware and handler without duplicating', async () => {
303
- const res = await app.request('http://localhost/vary2/')
304
- expect(res).not.toBeNull()
305
- expect(res.status).toBe(200)
306
- expect(res.headers.get('vary')).toBe('accept, accept-encoding')
307
- })
308
-
309
- it('Should deduplicate while merging multiple Vary headers from middleware and handler', async () => {
310
- const res = await app.request('http://localhost/vary3/')
311
- expect(res.headers.get('vary')).toBe('accept, accept-encoding, accept-language')
312
- })
313
-
314
- it('Should prevent duplication of Vary headers when identical ones are set by both middleware and handler', async () => {
315
- const res = await app.request('http://localhost/vary4/')
316
- expect(res.headers.get('vary')).toBe('accept, accept-encoding, accept-language')
317
- })
318
-
319
- it('Should correctly apply and return a single Vary header with Accept specified by middleware', async () => {
320
- const res = await app.request('http://localhost/vary5/')
321
- expect(res).not.toBeNull()
322
- expect(res.status).toBe(200)
323
- expect(res.headers.get('vary')).toBe('accept')
324
- })
325
-
326
- it('Should merge Vary headers specified by middleware as a string with additional headers added by handler', async () => {
327
- const res = await app.request('http://localhost/vary6/')
328
- expect(res).not.toBeNull()
329
- expect(res.status).toBe(200)
330
- expect(res.headers.get('vary')).toBe('accept, accept-encoding, accept-language')
331
- })
332
-
333
- it('Should prioritize the "*" Vary header from handler over any set by middleware', async () => {
334
- const res = await app.request('http://localhost/vary7/')
335
- expect(res).not.toBeNull()
336
- expect(res.status).toBe(200)
337
- expect(res.headers.get('vary')).toBe('*')
338
- })
339
-
340
- it('Should not allow "*" as a Vary header in middleware configuration due to its impact on caching effectiveness', async () => {
341
- expect(() => cache({ cacheName: 'my-app-v1', wait: true, vary: ['*'] })).toThrow()
342
- expect(() => cache({ cacheName: 'my-app-v1', wait: true, vary: '*' })).toThrow()
343
- })
344
-
345
- it.each([200])('Should cache %i in default cacheable status codes', async (code) => {
346
- await app.request(`http://localhost/default/${code}/`)
347
- const res = await app.request(`http://localhost/default/${code}/`)
348
- expect(res).not.toBeNull()
349
- expect(res.status).toBe(code)
350
- expect(res.headers.get('cache-control')).toBe('max-age=10')
351
- })
352
-
353
- it.each([
354
- 100, 101, 102, 103, 201, 202, 205, 207, 208, 226, 302, 303, 304, 307, 308, 400, 401, 402, 403,
355
- 406, 407, 408, 409, 411, 412, 413, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429,
356
- 431, 451, 500, 502, 503, 504, 505, 506, 507, 508, 510, 511,
357
- ])('Should not cache %i in default cacheable status codes', async (code) => {
358
- await app.request(`http://localhost/default/${code}/`)
359
- const res = await app.request(`http://localhost/default/${code}/`)
360
- expect(res).not.toBeNull()
361
- expect(res.status).toBe(code)
362
- expect(res.headers.get('cache-control')).not.toBe('max-age=10')
363
- })
364
-
365
- it.each([200, 201])('Should cache %i in custom cacheable status codes', async (code) => {
366
- await app.request(`http://localhost/custom/${code}/`)
367
- const res = await app.request(`http://localhost/custom/${code}/`)
368
- expect(res).not.toBeNull()
369
- expect(res.status).toBe(code)
370
- expect(res.headers.get('cache-control')).toBe('max-age=10')
371
- })
372
-
373
- it.each([
374
- 100, 101, 102, 103, 202, 205, 207, 208, 226, 302, 303, 304, 307, 308, 400, 401, 402, 403, 406,
375
- 407, 408, 409, 411, 412, 413, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429, 431,
376
- 451, 500, 502, 503, 504, 505, 506, 507, 508, 510, 511,
377
- ])('Should not cache %i in custom cacheable status codes', async (code) => {
378
- await app.request(`http://localhost/custom/${code}/`)
379
- const res = await app.request(`http://localhost/custom/${code}/`)
380
- expect(res).not.toBeNull()
381
- expect(res.status).toBe(code)
382
- expect(res.headers.get('cache-control')).not.toBe('max-age=10')
383
- })
384
-
385
- it('Should not be enabled if caches is not defined', async () => {
386
- vi.stubGlobal('caches', undefined)
387
- const app = new Acryl()
388
- app.use(cache({ cacheName: 'my-app-v1', cacheControl: 'max-age=10' }))
389
- app.get('/', (c) => {
390
- return c.text('cached')
391
- })
392
- expect(caches).toBeUndefined()
393
- const res = await app.request('/')
394
- expect(res).not.toBeNull()
395
- expect(res.status).toBe(200)
396
- expect(res.headers.get('cache-control')).toBe(null)
397
- })
398
-
399
- it('Should call onCacheNotAvailable when caches is not defined', async () => {
400
- vi.stubGlobal('caches', undefined)
401
- const spy = vi.fn()
402
- const app = new Acryl()
403
- app.use(cache({ cacheName: 'my-app-v1', onCacheNotAvailable: spy }))
404
- app.get('/', (c) => {
405
- return c.text('cached')
406
- })
407
- const res = await app.request('/')
408
- expect(res.status).toBe(200)
409
- expect(spy).toHaveBeenCalledOnce()
410
- expect(spy).toHaveBeenCalledWith(
411
- 'Cache Middleware is not enabled because caches is not defined.'
412
- )
413
- vi.unstubAllGlobals()
414
- })
415
-
416
- it('Should suppress default log when onCacheNotAvailable is false', async () => {
417
- vi.stubGlobal('caches', undefined)
418
- const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
419
- const app = new Acryl()
420
- app.use(cache({ cacheName: 'my-app-v1', onCacheNotAvailable: false }))
421
- app.get('/', (c) => {
422
- return c.text('cached')
423
- })
424
- const res = await app.request('/')
425
- expect(res.status).toBe(200)
426
- expect(logSpy).not.toHaveBeenCalledWith(
427
- 'Cache Middleware is not enabled because caches is not defined.'
428
- )
429
- logSpy.mockRestore()
430
- vi.unstubAllGlobals()
431
- })
432
- })
433
-
434
- describe('Cache Skipping Logic', () => {
435
- const stubStoreBackedCache = () => {
436
- const store = new Map<string | Request, Response>()
437
- const mockCache = {
438
- match: vi.fn((key: string | Request) => Promise.resolve(store.get(key)?.clone())),
439
- put: vi.fn((key: string | Request, response: Response) => {
440
- store.set(key, response.clone())
441
- return Promise.resolve()
442
- }),
443
- keys: vi.fn(() => Promise.resolve(Array.from(store.keys()))),
444
- }
445
-
446
- vi.stubGlobal('caches', {
447
- open: vi.fn().mockResolvedValue(mockCache),
448
- })
449
-
450
- return { mockCache }
451
- }
452
-
453
- let putSpy: ReturnType<typeof vi.fn>
454
- let matchSpy: ReturnType<typeof vi.fn>
455
-
456
- beforeEach(() => {
457
- matchSpy = vi.fn().mockResolvedValue(undefined)
458
- putSpy = vi.fn()
459
- const mockCache = {
460
- match: matchSpy, // Always miss unless a test overrides it
461
- put: putSpy, // We spy on this
462
- keys: vi.fn().mockResolvedValue([]),
463
- }
464
-
465
- vi.stubGlobal('caches', {
466
- open: vi.fn().mockResolvedValue(mockCache),
467
- })
468
- })
469
-
470
- afterEach(() => {
471
- vi.restoreAllMocks()
472
- })
473
-
474
- it('Should NOT cache response if Cache-Control contains "private"', async () => {
475
- const app = new Acryl()
476
- app.use('*', cache({ cacheName: 'skip-test', wait: true }))
477
- app.get('/', (c) => {
478
- c.header('Cache-Control', 'private, max-age=3600')
479
- return c.text('response')
480
- })
481
-
482
- const res = await app.request('/')
483
- expect(res.status).toBe(200)
484
- // IMPORTANT: put() should NOT be called
485
- expect(putSpy).not.toHaveBeenCalled()
486
- })
487
-
488
- it('Should NOT cache response if Cache-Control contains "no-store"', async () => {
489
- const app = new Acryl()
490
- app.use('*', cache({ cacheName: 'skip-test', wait: true }))
491
- app.get('/', (c) => {
492
- c.header('Cache-Control', 'no-store')
493
- return c.text('response')
494
- })
495
-
496
- await app.request('/')
497
- expect(putSpy).not.toHaveBeenCalled()
498
- })
499
-
500
- it('Should NOT cache response if Cache-Control contains no-cache="Set-Cookie"', async () => {
501
- const app = new Acryl()
502
- app.use('*', cache({ cacheName: 'skip-test', wait: true }))
503
- app.get('/', (c) => {
504
- c.header('Cache-Control', 'no-cache="Set-Cookie"')
505
- return c.text('response')
506
- })
507
-
508
- await app.request('/')
509
- expect(putSpy).not.toHaveBeenCalled()
510
- })
511
-
512
- it('Should NOT cache response if Set-Cookie header is present', async () => {
513
- const app = new Acryl()
514
- app.use('*', cache({ cacheName: 'skip-test', wait: true }))
515
- app.get('/', (c) => {
516
- c.header('Set-Cookie', 'session=secret')
517
- return c.text('response')
518
- })
519
-
520
- await app.request('/')
521
- expect(putSpy).not.toHaveBeenCalled()
522
- })
523
-
524
- it('Should cache normal responses (Control Test)', async () => {
525
- const app = new Acryl()
526
- app.use('*', cache({ cacheName: 'skip-test', wait: true }))
527
- app.get('/', (c) => {
528
- return c.text('response')
529
- })
530
-
531
- await app.request('/')
532
- // IMPORTANT: put() SHOULD be called for normal responses
533
- expect(putSpy).toHaveBeenCalled()
534
- })
535
-
536
- it.each(['Accept-Language', 'Authorization', 'X-Tenant'])(
537
- 'Should NOT cache response if Vary: %s is set',
538
- async (vary) => {
539
- const app = new Acryl()
540
- app.use('*', cache({ cacheName: 'skip-test', wait: true }))
541
- app.get('/', (c) => {
542
- c.header('Vary', vary)
543
- return c.text('response')
544
- })
545
-
546
- await app.request('/')
547
- expect(putSpy).not.toHaveBeenCalled()
548
- }
549
- )
550
-
551
- it('Should set configured Vary header and cache by the configured request header value', async () => {
552
- const { mockCache } = stubStoreBackedCache()
553
- const app = new Acryl()
554
- let count = 0
555
- app.use('*', cache({ cacheName: 'skip-test', wait: true, vary: 'Accept' }))
556
- app.get('/', (c) => {
557
- count++
558
- c.header('X-Count', `${count}`)
559
- return c.text('response')
560
- })
561
-
562
- await app.request('/')
563
- const res = await app.request('/')
564
- expect(res.headers.get('Vary')).toBe('accept')
565
- expect(res.headers.get('X-Count')).toBe('1')
566
- expect(mockCache.put).toHaveBeenCalledOnce()
567
- })
568
-
569
- it('Should store separate variants for different Accept-Language request values', async () => {
570
- const { mockCache } = stubStoreBackedCache()
571
- const app = new Acryl()
572
- let count = 0
573
- app.use('*', cache({ cacheName: 'vary-language-test', wait: true, vary: 'Accept-Language' }))
574
- app.get('/', (c) => {
575
- count++
576
- c.header('X-Count', `${count}`)
577
- return c.text(c.req.header('Accept-Language') ?? 'missing')
578
- })
579
-
580
- await app.request('/', {
581
- headers: {
582
- 'Accept-Language': 'en',
583
- },
584
- })
585
- const en = await app.request('/', {
586
- headers: {
587
- 'Accept-Language': 'en',
588
- },
589
- })
590
- await app.request('/', {
591
- headers: {
592
- 'Accept-Language': 'ja',
593
- },
594
- })
595
- const ja = await app.request('/', {
596
- headers: {
597
- 'Accept-Language': 'ja',
598
- },
599
- })
600
-
601
- expect(await en.text()).toBe('en')
602
- expect(en.headers.get('X-Count')).toBe('1')
603
- expect(await ja.text()).toBe('ja')
604
- expect(ja.headers.get('X-Count')).toBe('2')
605
- expect(mockCache.put).toHaveBeenCalledTimes(2)
606
- })
607
-
608
- it('Should keep missing and present Vary request header values in separate variants', async () => {
609
- stubStoreBackedCache()
610
- const app = new Acryl()
611
- let count = 0
612
- app.use('*', cache({ cacheName: 'vary-missing-test', wait: true, vary: 'Accept-Language' }))
613
- app.get('/', (c) => {
614
- count++
615
- c.header('X-Count', `${count}`)
616
- return c.text(c.req.header('Accept-Language') ?? 'missing')
617
- })
618
-
619
- await app.request('/')
620
- await app.request('/', {
621
- headers: {
622
- 'Accept-Language': 'en',
623
- },
624
- })
625
- const missing = await app.request('/')
626
- const present = await app.request('/', {
627
- headers: {
628
- 'Accept-Language': 'en',
629
- },
630
- })
631
-
632
- expect(await missing.text()).toBe('missing')
633
- expect(missing.headers.get('X-Count')).toBe('1')
634
- expect(await present.text()).toBe('en')
635
- expect(present.headers.get('X-Count')).toBe('2')
636
- })
637
-
638
- it('Should store separate variants using multiple configured request headers', async () => {
639
- const { mockCache } = stubStoreBackedCache()
640
- const app = new Acryl()
641
- let count = 0
642
- app.use(
643
- '*',
644
- cache({ cacheName: 'vary-multiple-test', wait: true, vary: ['Accept', 'Accept-Encoding'] })
645
- )
646
- app.get('/', (c) => {
647
- count++
648
- c.header('X-Count', `${count}`)
649
- return c.text(`${c.req.header('Accept')}:${c.req.header('Accept-Encoding')}`)
650
- })
651
-
652
- await app.request('/', {
653
- headers: {
654
- Accept: 'text/plain',
655
- 'Accept-Encoding': 'gzip',
656
- },
657
- })
658
- const same = await app.request('/', {
659
- headers: {
660
- Accept: 'text/plain',
661
- 'Accept-Encoding': 'gzip',
662
- },
663
- })
664
- await app.request('/', {
665
- headers: {
666
- Accept: 'application/json',
667
- 'Accept-Encoding': 'gzip',
668
- },
669
- })
670
- const differentAccept = await app.request('/', {
671
- headers: {
672
- Accept: 'application/json',
673
- 'Accept-Encoding': 'gzip',
674
- },
675
- })
676
- await app.request('/', {
677
- headers: {
678
- Accept: 'text/plain',
679
- 'Accept-Encoding': 'br',
680
- },
681
- })
682
- const differentEncoding = await app.request('/', {
683
- headers: {
684
- Accept: 'text/plain',
685
- 'Accept-Encoding': 'br',
686
- },
687
- })
688
-
689
- expect(same.headers.get('X-Count')).toBe('1')
690
- expect(differentAccept.headers.get('X-Count')).toBe('2')
691
- expect(differentEncoding.headers.get('X-Count')).toBe('3')
692
- expect(mockCache.put).toHaveBeenCalledTimes(3)
693
- })
694
-
695
- it('Should store when the handler Vary header is covered by configured Vary headers', async () => {
696
- const { mockCache } = stubStoreBackedCache()
697
- const app = new Acryl()
698
- app.use('*', cache({ cacheName: 'vary-covered-test', wait: true, vary: 'Accept-Language' }))
699
- app.get('/', (c) => {
700
- c.header('Vary', 'Accept-Language')
701
- return c.text('response')
702
- })
703
-
704
- await app.request('/')
705
- expect(mockCache.put).toHaveBeenCalledOnce()
706
- })
707
-
708
- it('Should not store when the handler adds Vary headers not covered by configured Vary headers', async () => {
709
- const app = new Acryl()
710
- app.use('*', cache({ cacheName: 'skip-test', wait: true, vary: 'Accept-Language' }))
711
- app.get('/', (c) => {
712
- c.header('Vary', 'Accept-Language, Cookie')
713
- return c.text('response')
714
- })
715
-
716
- await app.request('/')
717
- expect(putSpy).not.toHaveBeenCalled()
718
- })
719
-
720
- it('Should not store Vary: * responses even when Vary headers are configured', async () => {
721
- const app = new Acryl()
722
- app.use('*', cache({ cacheName: 'skip-test', wait: true, vary: 'Accept-Language' }))
723
- app.get('/', (c) => {
724
- c.header('Vary', '*')
725
- return c.text('response')
726
- })
727
-
728
- await app.request('/')
729
- expect(putSpy).not.toHaveBeenCalled()
730
- })
731
-
732
- it('Should bypass cache for Authorization requests', async () => {
733
- const app = new Acryl()
734
- app.use('*', cache({ cacheName: 'skip-test', wait: true, cacheControl: 'max-age=10' }))
735
- app.get('/', (c) => {
736
- return c.text('fresh')
737
- })
738
-
739
- const res = await app.request('/', {
740
- headers: {
741
- Authorization: 'Bearer token',
742
- },
743
- })
744
- expect(await res.text()).toBe('fresh')
745
- expect(caches.open).not.toHaveBeenCalled()
746
- expect(putSpy).not.toHaveBeenCalled()
747
- })
748
-
749
- it('Should cache request URLs containing a fragment', async () => {
750
- const { mockCache } = stubStoreBackedCache()
751
- const app = new Acryl()
752
- let requestCount = 0
753
- app.use('*', cache({ cacheName: 'fragment-request-test', wait: true }))
754
- app.get('/', (c) => {
755
- requestCount++
756
- return c.text(c.req.query('name') ?? 'default')
757
- })
758
-
759
- expect(await (await app.request('http://localhost/#?name=probe')).text()).toBe('default')
760
- expect(await (await app.request('http://localhost/#?name=probe')).text()).toBe('default')
761
- expect(requestCount).toBe(1)
762
- expect(mockCache.put).toHaveBeenCalledOnce()
763
- expect(mockCache.put.mock.calls[0][0]).toBe(
764
- 'http://localhost/.acryl-router/cache?__hono_cache_key=http%3A%2F%2Flocalhost%2F%23%3Fname%3Dprobe&__hono_cache_method=GET'
765
- )
766
- })
767
-
768
- it('Should cache QUERY responses without consuming the handler request body', async () => {
769
- const { mockCache } = stubStoreBackedCache()
770
- const app = new Acryl()
771
- let queryCount = 0
772
- app.use('*', cache({ cacheName: 'query-test', wait: true, cacheControl: 'max-age=10' }))
773
- app.on('QUERY', '/resource', async (c) => {
774
- queryCount++
775
- c.header('X-Count', `${queryCount}`)
776
- return c.text(await c.req.raw.text())
777
- })
778
-
779
- const request = () =>
780
- app.request('/resource', {
781
- method: 'QUERY',
782
- headers: { 'Content-Type': 'application/json' },
783
- body: '{"query":"acryl"}',
784
- })
785
-
786
- await request()
787
- const res = await request()
788
-
789
- expect(await res.text()).toBe('{"query":"acryl"}')
790
- expect(res.headers.get('X-Count')).toBe('1')
791
- expect(res.headers.get('Cache-Control')).toBe('max-age=10')
792
- expect(mockCache.put).toHaveBeenCalledOnce()
793
- })
794
-
795
- it('Should bypass QUERY caching when the measured body exceeds the configured limit', async () => {
796
- const { mockCache } = stubStoreBackedCache()
797
- const app = new Acryl()
798
- let queryCount = 0
799
- app.use('*', cache({ cacheName: 'query-body-limit-test', wait: true, maxQueryBodySize: 4 }))
800
- app.on('QUERY', '/resource', async (c) => {
801
- queryCount++
802
- c.header('X-Count', `${queryCount}`)
803
- return c.text(await c.req.text())
804
- })
805
-
806
- const request = () => {
807
- const body = new ReadableStream({
808
- start(controller) {
809
- controller.enqueue(new TextEncoder().encode('ab'))
810
- controller.enqueue(new TextEncoder().encode('cde'))
811
- controller.close()
812
- },
813
- })
814
- return app.request('/resource', {
815
- method: 'QUERY',
816
- headers: {
817
- 'Content-Length': '1',
818
- 'Content-Type': 'text/plain',
819
- },
820
- body,
821
- duplex: 'half',
822
- } as RequestInit & { duplex: 'half' })
823
- }
824
-
825
- await request()
826
- const res = await request()
827
-
828
- expect(await res.text()).toBe('abcde')
829
- expect(res.headers.get('X-Count')).toBe('2')
830
- expect(caches.open).not.toHaveBeenCalled()
831
- expect(mockCache.put).not.toHaveBeenCalled()
832
- })
833
-
834
- it('Should cache QUERY requests at the configured body size limit', async () => {
835
- const { mockCache } = stubStoreBackedCache()
836
- const app = new Acryl()
837
- let queryCount = 0
838
- app.use(
839
- '*',
840
- cache({ cacheName: 'query-body-limit-boundary-test', wait: true, maxQueryBodySize: 4 })
841
- )
842
- app.on('QUERY', '/resource', async (c) => {
843
- queryCount++
844
- c.header('X-Count', `${queryCount}`)
845
- return c.text(await c.req.text())
846
- })
847
-
848
- const request = () =>
849
- app.request('/resource', {
850
- method: 'QUERY',
851
- headers: { 'Content-Type': 'text/plain' },
852
- body: 'abcd',
853
- })
854
-
855
- await request()
856
- const res = await request()
857
-
858
- expect(await res.text()).toBe('abcd')
859
- expect(res.headers.get('X-Count')).toBe('1')
860
- expect(mockCache.put).toHaveBeenCalledOnce()
861
- })
862
-
863
- it('Should keep QUERY request content and Content-Type in separate cache variants', async () => {
864
- const { mockCache } = stubStoreBackedCache()
865
- const app = new Acryl()
866
- let queryCount = 0
867
- app.use(
868
- '*',
869
- cache({ cacheName: 'query-variant-test', wait: true, keyGenerator: () => 'query-key' })
870
- )
871
- app.on('QUERY', '/resource', async (c) => {
872
- queryCount++
873
- c.header('X-Count', `${queryCount}`)
874
- return c.text(`${c.req.header('Content-Type')}:${await c.req.text()}`)
875
- })
876
-
877
- const request = (body: string, contentType: string) =>
878
- app.request('/resource', {
879
- method: 'QUERY',
880
- headers: { 'Content-Type': contentType },
881
- body,
882
- })
883
-
884
- await request('one', 'text/plain')
885
- const same = await request('one', 'text/plain')
886
- await request('two', 'text/plain')
887
- const differentContent = await request('two', 'text/plain')
888
- await request('one', 'application/json')
889
- const differentContentType = await request('one', 'application/json')
890
-
891
- expect(same.headers.get('X-Count')).toBe('1')
892
- expect(differentContent.headers.get('X-Count')).toBe('2')
893
- expect(differentContentType.headers.get('X-Count')).toBe('3')
894
- expect(mockCache.put).toHaveBeenCalledTimes(3)
895
- })
896
-
897
- it('Should keep GET and QUERY responses in separate cache entries', async () => {
898
- const { mockCache } = stubStoreBackedCache()
899
- const app = new Acryl()
900
- app.use('*', cache({ cacheName: 'method-test', wait: true }))
901
- app.get('/resource', (c) => c.text('get response'))
902
- app.on('QUERY', '/resource', (c) => c.text('query response'))
903
-
904
- await app.request('/resource')
905
- await app.request('/resource', {
906
- method: 'QUERY',
907
- headers: { 'Content-Type': 'application/json' },
908
- body: '{}',
909
- })
910
- const getRes = await app.request('/resource')
911
- const queryRes = await app.request('/resource', {
912
- method: 'QUERY',
913
- headers: { 'Content-Type': 'application/json' },
914
- body: '{}',
915
- })
916
-
917
- expect(await getRes.text()).toBe('get response')
918
- expect(await queryRes.text()).toBe('query response')
919
- expect(mockCache.put).toHaveBeenCalledTimes(2)
920
- })
921
-
922
- it('Should not let a GET URL alias an internally generated QUERY cache key', async () => {
923
- const { mockCache } = stubStoreBackedCache()
924
- const app = new Acryl()
925
- app.use('*', cache({ cacheName: 'method-collision-test', wait: true }))
926
- app.get('/resource', (c) => c.text('get response'))
927
- app.get('*', (c) => c.text('get response'))
928
- app.on('QUERY', '/resource', (c) => c.text('query response'))
929
-
930
- await app.request('/resource', {
931
- method: 'QUERY',
932
- headers: { 'Content-Type': 'application/json' },
933
- body: '{}',
934
- })
935
-
936
- const queryKey = mockCache.put.mock.calls[0][0] as string
937
- const getRes = await app.request(queryKey)
938
- const queryRes = await app.request('/resource', {
939
- method: 'QUERY',
940
- headers: { 'Content-Type': 'application/json' },
941
- body: '{}',
942
- })
943
-
944
- expect(await getRes.text()).toBe('get response')
945
- expect(await queryRes.text()).toBe('query response')
946
- expect(mockCache.put).toHaveBeenCalledTimes(2)
947
- })
948
-
949
- it('Should not resolve a custom cache key as a request URL', async () => {
950
- const { mockCache } = stubStoreBackedCache()
951
- const app = new Acryl()
952
- app.use(
953
- '/resource',
954
- cache({
955
- cacheName: 'custom-key-collision-test',
956
- wait: true,
957
- keyGenerator: () => 'query-key',
958
- })
959
- )
960
- app.use('/query-key', cache({ cacheName: 'custom-key-collision-test', wait: true }))
961
- app.get('/resource', (c) => c.text('custom key response'))
962
- app.get('/query-key', (c) => c.text('request URL response'))
963
-
964
- expect(await (await app.request('/resource')).text()).toBe('custom key response')
965
- expect(await (await app.request('/query-key')).text()).toBe('request URL response')
966
- expect(mockCache.put).toHaveBeenCalledTimes(2)
967
- })
968
-
969
- it('Should preserve fragments in custom cache keys', async () => {
970
- const { mockCache } = stubStoreBackedCache()
971
- const app = new Acryl()
972
- let requestCount = 0
973
- app.use(
974
- '/report',
975
- cache({
976
- cacheName: 'custom-fragment-key-test',
977
- wait: true,
978
- keyGenerator: (c) => `${c.req.path}#${c.req.header('X-Tenant-Id')}`,
979
- })
980
- )
981
- app.get('/report', (c) => {
982
- requestCount++
983
- return c.text(`report for ${c.req.header('X-Tenant-Id')}`)
984
- })
985
-
986
- const request = (tenant: string) =>
987
- app.request('/report', { headers: { 'X-Tenant-Id': tenant } })
988
-
989
- expect(await (await request('acme')).text()).toBe('report for acme')
990
- expect(await (await request('globex')).text()).toBe('report for globex')
991
- expect(await (await request('acme')).text()).toBe('report for acme')
992
- expect(requestCount).toBe(2)
993
- expect(mockCache.put).toHaveBeenCalledTimes(2)
994
- expect(mockCache.put.mock.calls.map(([key]) => key)).toEqual([
995
- 'http://localhost/.acryl-router/cache?__hono_cache_key=%2Freport%23acme&__hono_cache_method=GET',
996
- 'http://localhost/.acryl-router/cache?__hono_cache_key=%2Freport%23globex&__hono_cache_method=GET',
997
- ])
998
- })
999
-
1000
- it('Should keep QUERY digest and vary values with a fragmented custom cache key', async () => {
1001
- const { mockCache } = stubStoreBackedCache()
1002
- const app = new Acryl()
1003
- let queryCount = 0
1004
- app.use(
1005
- '*',
1006
- cache({
1007
- cacheName: 'query-fragment-test',
1008
- wait: true,
1009
- vary: 'X-Variant',
1010
- keyGenerator: (c) => `${c.req.url}#custom-fragment`,
1011
- })
1012
- )
1013
- app.on('QUERY', '/resource', async (c) => {
1014
- queryCount++
1015
- return c.text(`${await c.req.text()}:${c.req.header('X-Variant')}`)
1016
- })
1017
-
1018
- const request = (body: string, variant: string) =>
1019
- app.request('/resource', {
1020
- method: 'QUERY',
1021
- headers: { 'Content-Type': 'text/plain', 'X-Variant': variant },
1022
- body,
1023
- })
1024
-
1025
- expect(await (await request('one', 'a')).text()).toBe('one:a')
1026
- expect(await (await request('two', 'a')).text()).toBe('two:a')
1027
- expect(await (await request('one', 'b')).text()).toBe('one:b')
1028
- expect(await (await request('one', 'a')).text()).toBe('one:a')
1029
- expect(queryCount).toBe(3)
1030
- expect(mockCache.put).toHaveBeenCalledTimes(3)
1031
- })
1032
-
1033
- it('Should bypass QUERY caching when Web Crypto is not available', async () => {
1034
- const originalCrypto = globalThis.crypto
1035
- vi.stubGlobal('crypto', undefined)
1036
-
1037
- try {
1038
- const { mockCache } = stubStoreBackedCache()
1039
- const app = new Acryl()
1040
- let queryCount = 0
1041
- let getCount = 0
1042
- const onCacheNotAvailable = vi.fn()
1043
- app.use(
1044
- '*',
1045
- cache({
1046
- cacheName: 'query-no-crypto-test',
1047
- wait: true,
1048
- onCacheNotAvailable,
1049
- })
1050
- )
1051
- app.get('/resource', (c) => {
1052
- getCount++
1053
- c.header('X-Count', `${getCount}`)
1054
- return c.text('get response')
1055
- })
1056
- app.on('QUERY', '/resource', (c) => {
1057
- queryCount++
1058
- c.header('X-Count', `${queryCount}`)
1059
- return c.text('query response')
1060
- })
1061
-
1062
- expect(onCacheNotAvailable).toHaveBeenCalledOnce()
1063
- expect(onCacheNotAvailable).toHaveBeenCalledWith(
1064
- 'Cache Middleware cannot cache QUERY requests because Web Crypto is not available.'
1065
- )
1066
-
1067
- const request = () =>
1068
- app.request('/resource', {
1069
- method: 'QUERY',
1070
- headers: { 'Content-Type': 'application/json' },
1071
- body: '{}',
1072
- })
1073
-
1074
- await request()
1075
- const res = await request()
1076
-
1077
- expect(res.headers.get('X-Count')).toBe('2')
1078
- expect(caches.open).not.toHaveBeenCalled()
1079
- expect(onCacheNotAvailable).toHaveBeenCalledOnce()
1080
-
1081
- await app.request('/resource')
1082
- const getRes = await app.request('/resource')
1083
-
1084
- expect(getRes.headers.get('X-Count')).toBe('1')
1085
- expect(mockCache.put).toHaveBeenCalledOnce()
1086
- } finally {
1087
- vi.stubGlobal('crypto', originalCrypto)
1088
- }
1089
- })
1090
-
1091
- it('Should bypass QUERY caching when the request content cannot be read', async () => {
1092
- const app = new Acryl()
1093
- let queryCount = 0
1094
- app.use('*', async (c, next) => {
1095
- await c.req.raw.text()
1096
- await next()
1097
- })
1098
- app.use('*', cache({ cacheName: 'query-consumed-body-test', wait: true }))
1099
- app.on('QUERY', '/resource', (c) => {
1100
- queryCount++
1101
- c.header('X-Count', `${queryCount}`)
1102
- return c.text('query response')
1103
- })
1104
-
1105
- const request = () =>
1106
- app.request('/resource', {
1107
- method: 'QUERY',
1108
- headers: { 'Content-Type': 'application/json' },
1109
- body: '{}',
1110
- })
1111
-
1112
- await request()
1113
- const res = await request()
1114
-
1115
- expect(res.headers.get('X-Count')).toBe('2')
1116
- expect(caches.open).not.toHaveBeenCalled()
1117
- })
1118
-
1119
- it('Should bypass QUERY caching after the body was read directly as FormData', async () => {
1120
- const { mockCache } = stubStoreBackedCache()
1121
- const app = new Acryl()
1122
- let queryCount = 0
1123
- app.use('*', async (c, next) => {
1124
- expect((await c.req.formData()).get('query')).toBe('acryl')
1125
- await next()
1126
- })
1127
- app.use('*', cache({ cacheName: 'query-cached-form-data-test', wait: true }))
1128
- app.on('QUERY', '/resource', (c) => {
1129
- queryCount++
1130
- c.header('X-Count', `${queryCount}`)
1131
- return c.text('query response')
1132
- })
1133
-
1134
- const body = [
1135
- '--acryl-boundary',
1136
- 'Content-Disposition: form-data; name="query"',
1137
- '',
1138
- 'acryl',
1139
- '--acryl-boundary--',
1140
- '',
1141
- ].join('\r\n')
1142
- const request = () =>
1143
- app.request('/resource', {
1144
- method: 'QUERY',
1145
- headers: { 'Content-Type': 'multipart/form-data; boundary=acryl-boundary' },
1146
- body,
1147
- })
1148
-
1149
- await request()
1150
- const res = await request()
1151
-
1152
- expect(res.headers.get('X-Count')).toBe('2')
1153
- expect(caches.open).not.toHaveBeenCalled()
1154
- expect(mockCache.put).not.toHaveBeenCalled()
1155
- })
1156
-
1157
- it('Should cache QUERY after the body was read through AcrylRequest', async () => {
1158
- const { mockCache } = stubStoreBackedCache()
1159
- const app = new Acryl()
1160
- let queryCount = 0
1161
- app.use('*', async (c, next) => {
1162
- expect(await c.req.text()).toBe('{}')
1163
- await next()
1164
- })
1165
- app.use('*', cache({ cacheName: 'query-cached-body-test', wait: true }))
1166
- app.on('QUERY', '/resource', async (c) => {
1167
- queryCount++
1168
- c.header('X-Count', `${queryCount}`)
1169
- return c.text(await c.req.text())
1170
- })
1171
-
1172
- const request = () =>
1173
- app.request('/resource', {
1174
- method: 'QUERY',
1175
- headers: { 'Content-Type': 'application/json' },
1176
- body: '{}',
1177
- })
1178
-
1179
- await request()
1180
- const res = await request()
1181
-
1182
- expect(await res.text()).toBe('{}')
1183
- expect(res.headers.get('X-Count')).toBe('1')
1184
- expect(mockCache.put).toHaveBeenCalledOnce()
1185
- })
1186
-
1187
- it('Should not use or store cache for POST requests', async () => {
1188
- const app = new Acryl()
1189
- let postCount = 0
1190
- app.use('*', cache({ cacheName: 'skip-test', wait: true, cacheControl: 'max-age=10' }))
1191
- app.get('/resource', (c) => {
1192
- return c.text('get response')
1193
- })
1194
- app.post('/resource', (c) => {
1195
- postCount++
1196
- c.header('X-Count', `${postCount}`)
1197
- return c.text('post response')
1198
- })
1199
-
1200
- await app.request('/resource')
1201
- putSpy.mockClear()
1202
- matchSpy.mockClear()
1203
-
1204
- await app.request('/resource', { method: 'POST' })
1205
- const res = await app.request('/resource', { method: 'POST' })
1206
- expect(res.headers.get('X-Count')).toBe('2')
1207
- expect(res.headers.get('Cache-Control')).toBeNull()
1208
- expect(matchSpy).not.toHaveBeenCalled()
1209
- expect(putSpy).not.toHaveBeenCalled()
1210
- })
1211
- })