acrylblends 0.4.0 → 0.5.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 (426) hide show
  1. package/LICENSE +73 -0
  2. package/dist/index.js +56 -2
  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/agents/index.md +24 -0
  8. package/docs/framework/api/acryl.md +259 -0
  9. package/docs/framework/api/context.md +527 -0
  10. package/docs/framework/api/exception.md +80 -0
  11. package/docs/framework/api/index.md +12 -0
  12. package/docs/framework/api/presets.md +65 -0
  13. package/docs/framework/api/request.md +396 -0
  14. package/docs/framework/api/routing.md +303 -0
  15. package/docs/framework/api/summary.md +140 -0
  16. package/docs/framework/concepts/benchmarks.md +26 -0
  17. package/docs/framework/concepts/developer-experience.md +5 -0
  18. package/docs/framework/concepts/middleware.md +21 -0
  19. package/docs/framework/concepts/motivation.md +14 -0
  20. package/docs/framework/concepts/routers.md +63 -0
  21. package/docs/framework/concepts/stacks.md +235 -0
  22. package/docs/framework/concepts/web-standard.md +37 -0
  23. package/docs/framework/cordis/acryl-in-a-cordis-app.md +146 -0
  24. package/docs/framework/cordis/concepts.md +105 -0
  25. package/docs/framework/cordis/middleware.md +8 -0
  26. package/docs/framework/cordis/migration-from-0-3.md +111 -0
  27. package/docs/framework/cordis/migration-from-0-4.md +14 -0
  28. package/docs/framework/cordis/migration-from-dsh-webserver.md +103 -0
  29. package/docs/framework/cordis/mounting.md +85 -0
  30. package/docs/framework/cordis/node-server.md +75 -0
  31. package/docs/framework/cordis/plugin-lifecycle.md +111 -0
  32. package/docs/framework/cordis/surfaces.md +71 -0
  33. package/docs/framework/cordis/transport-and-cancellation.md +57 -0
  34. package/docs/framework/cordis/validation.md +135 -0
  35. package/docs/framework/getting-started/basic.md +196 -0
  36. package/docs/framework/getting-started/bun.md +221 -0
  37. package/docs/framework/getting-started/nodejs.md +344 -0
  38. package/docs/framework/getting-started/other-runtimes.md +23 -0
  39. package/docs/framework/guides/best-practices.md +231 -0
  40. package/docs/framework/guides/create-acrylblends.md +79 -0
  41. package/docs/framework/guides/examples.md +3 -0
  42. package/docs/framework/guides/faq.md +19 -0
  43. package/docs/framework/guides/helpers.md +33 -0
  44. package/docs/framework/guides/jsx-dom.md +299 -0
  45. package/docs/framework/guides/jsx.md +440 -0
  46. package/docs/framework/guides/middleware.md +253 -0
  47. package/docs/framework/guides/rpc.md +886 -0
  48. package/docs/framework/guides/testing.md +133 -0
  49. package/docs/framework/guides/validation.md +383 -0
  50. package/docs/framework/helpers/accepts.md +60 -0
  51. package/docs/framework/helpers/adapter.md +87 -0
  52. package/docs/framework/helpers/conninfo.md +112 -0
  53. package/docs/framework/helpers/cookie.md +232 -0
  54. package/docs/framework/helpers/css.md +285 -0
  55. package/docs/framework/helpers/dev.md +67 -0
  56. package/docs/framework/helpers/factory.md +180 -0
  57. package/docs/framework/helpers/html.md +123 -0
  58. package/docs/framework/helpers/jwt.md +184 -0
  59. package/docs/framework/helpers/proxy.md +118 -0
  60. package/docs/framework/helpers/route.md +143 -0
  61. package/docs/framework/helpers/ssg.md +393 -0
  62. package/docs/framework/helpers/streaming.md +119 -0
  63. package/docs/framework/helpers/testing.md +101 -0
  64. package/docs/framework/helpers/websocket.md +215 -0
  65. package/docs/framework/middleware/builtin/basic-auth.md +165 -0
  66. package/docs/framework/middleware/builtin/bearer-auth.md +138 -0
  67. package/docs/framework/middleware/builtin/body-limit.md +71 -0
  68. package/docs/framework/middleware/builtin/cache.md +122 -0
  69. package/docs/framework/middleware/builtin/combine.md +94 -0
  70. package/docs/framework/middleware/builtin/compress.md +59 -0
  71. package/docs/framework/middleware/builtin/context-storage.md +78 -0
  72. package/docs/framework/middleware/builtin/cors.md +135 -0
  73. package/docs/framework/middleware/builtin/csrf.md +105 -0
  74. package/docs/framework/middleware/builtin/etag.md +54 -0
  75. package/docs/framework/middleware/builtin/ip-restriction.md +69 -0
  76. package/docs/framework/middleware/builtin/jsx-renderer.md +286 -0
  77. package/docs/framework/middleware/builtin/jwk.md +192 -0
  78. package/docs/framework/middleware/builtin/jwt.md +145 -0
  79. package/docs/framework/middleware/builtin/language.md +232 -0
  80. package/docs/framework/middleware/builtin/logger.md +74 -0
  81. package/docs/framework/middleware/builtin/loopback-only.md +31 -0
  82. package/docs/framework/middleware/builtin/method-not-allowed.md +48 -0
  83. package/docs/framework/middleware/builtin/method-override.md +80 -0
  84. package/docs/framework/middleware/builtin/mount.md +106 -0
  85. package/docs/framework/middleware/builtin/pretty-json.md +53 -0
  86. package/docs/framework/middleware/builtin/request-id.md +94 -0
  87. package/docs/framework/middleware/builtin/secure-headers.md +247 -0
  88. package/docs/framework/middleware/builtin/timeout.md +98 -0
  89. package/docs/framework/middleware/builtin/timing.md +101 -0
  90. package/docs/framework/middleware/builtin/trailing-slash.md +74 -0
  91. package/docs/framework/middleware/third-party.md +4 -0
  92. package/docs/framework/upstream.json +9 -0
  93. package/lib/accepts-XBuZPVRI.js +52 -0
  94. package/lib/accepts.d.ts +33 -0
  95. package/lib/accepts.js +2 -0
  96. package/lib/acryl-base.d.ts +2 -0
  97. package/lib/acryl-base.js +332 -0
  98. package/lib/adapter.d.ts +12 -0
  99. package/lib/adapter.js +37 -0
  100. package/lib/app-BM0rTCp7.js +26 -0
  101. package/lib/app-CmAYW1Al.d.ts +21 -0
  102. package/lib/basic-auth.d.ts +72 -0
  103. package/lib/basic-auth.js +95 -0
  104. package/lib/bearer-auth.d.ts +89 -0
  105. package/lib/bearer-auth.js +94 -0
  106. package/lib/body-limit.d.ts +42 -0
  107. package/lib/body-limit.js +66 -0
  108. package/lib/client-BHTr318g.js +266 -0
  109. package/lib/client.d.ts +3 -0
  110. package/lib/client.js +2 -0
  111. package/lib/combine.d.ts +85 -0
  112. package/lib/combine.js +137 -0
  113. package/lib/compose-BuW2B6PZ.js +50 -0
  114. package/lib/compress.d.ts +38 -0
  115. package/lib/compress.js +79 -0
  116. package/lib/connection-C7xgP1hP.js +24 -0
  117. package/lib/conninfo.d.ts +25 -0
  118. package/lib/conninfo.js +2 -0
  119. package/lib/constants-Dy7ZvN1H.js +4 -0
  120. package/lib/context-Bm7rI-oK.d.ts +1234 -0
  121. package/lib/context-DDO0cfuP.js +365 -0
  122. package/lib/context-storage.d.ts +38 -0
  123. package/lib/context-storage.js +52 -0
  124. package/lib/cookie-ClMaKDQf.d.ts +40 -0
  125. package/lib/cookie.d.ts +24 -0
  126. package/lib/cookie.js +82 -0
  127. package/lib/cors.d.ts +52 -0
  128. package/lib/cors.js +106 -0
  129. package/lib/csrf.d.ts +75 -0
  130. package/lib/csrf.js +101 -0
  131. package/lib/etag.d.ts +40 -0
  132. package/lib/etag.js +135 -0
  133. package/lib/factory.d.ts +35 -0
  134. package/lib/factory.js +30 -0
  135. package/lib/headers-tu2YYN8T.d.ts +11 -0
  136. package/lib/html.d.ts +6 -0
  137. package/lib/html.js +31 -0
  138. package/lib/http-exception.d.ts +57 -0
  139. package/lib/http-exception.js +54 -0
  140. package/lib/http-status-DaAwGAFL.d.ts +35 -0
  141. package/lib/index-B-3F4wt2.d.ts +12 -0
  142. package/lib/index-UTBmM3GX.d.ts +68 -0
  143. package/lib/index-oOClm_Xk.d.ts +45 -0
  144. package/lib/index.d.ts +5 -0
  145. package/lib/index.js +28 -0
  146. package/lib/ip-restriction.d.ts +68 -0
  147. package/lib/ip-restriction.js +166 -0
  148. package/lib/jwk.d.ts +52 -0
  149. package/lib/jwk.js +117 -0
  150. package/lib/jwt-BGJBw5AF.js +403 -0
  151. package/lib/jwt-Dg493Tai.d.ts +51 -0
  152. package/lib/jwt.d.ts +72 -0
  153. package/lib/jwt.js +115 -0
  154. package/lib/language.d.ts +88 -0
  155. package/lib/language.js +198 -0
  156. package/lib/logger.d.ts +22 -0
  157. package/lib/logger.js +54 -0
  158. package/lib/loopback-only.d.ts +45 -0
  159. package/lib/loopback-only.js +90 -0
  160. package/lib/method-not-allowed.d.ts +47 -0
  161. package/lib/method-not-allowed.js +82 -0
  162. package/lib/method-override.d.ts +46 -0
  163. package/lib/method-override.js +103 -0
  164. package/lib/mime-BQOYHl-l.d.ts +72 -0
  165. package/lib/node-server/conninfo.d.ts +11 -0
  166. package/lib/node-server/conninfo.js +19 -0
  167. package/lib/node-server/serve-static.d.ts +19 -0
  168. package/lib/node-server/serve-static.js +2 -0
  169. package/lib/node-server.d.ts +103 -0
  170. package/lib/node-server.js +1325 -0
  171. package/lib/powered-by.d.ts +28 -0
  172. package/lib/powered-by.js +24 -0
  173. package/lib/pretty-json.d.ts +40 -0
  174. package/lib/pretty-json.js +39 -0
  175. package/lib/reg-exp-router-DC1R3qoY.js +363 -0
  176. package/lib/request-id.d.ts +45 -0
  177. package/lib/request-id.js +34 -0
  178. package/lib/request.d.ts +2 -0
  179. package/lib/request.js +323 -0
  180. package/lib/route.d.ts +78 -0
  181. package/lib/route.js +126 -0
  182. package/lib/router/linear-router.d.ts +11 -0
  183. package/lib/router/linear-router.js +96 -0
  184. package/lib/router/pattern-router.d.ts +11 -0
  185. package/lib/router/pattern-router.js +41 -0
  186. package/lib/router/reg-exp-router.d.ts +35 -0
  187. package/lib/router/reg-exp-router.js +2 -0
  188. package/lib/router/smart-router.d.ts +15 -0
  189. package/lib/router/smart-router.js +2 -0
  190. package/lib/router/trie-router.d.ts +11 -0
  191. package/lib/router/trie-router.js +2 -0
  192. package/lib/router-CvUxUTOq.d.ts +99 -0
  193. package/lib/router.d.ts +2 -0
  194. package/lib/router.js +35 -0
  195. package/lib/schema-validator.d.ts +28 -0
  196. package/lib/schema-validator.js +22 -0
  197. package/lib/secure-headers.d.ts +125 -0
  198. package/lib/secure-headers.js +163 -0
  199. package/lib/serve-static-Dw_m6KxR.js +213 -0
  200. package/lib/serve-static.d.ts +31 -0
  201. package/lib/serve-static.js +77 -0
  202. package/lib/smart-router-C5CZ5XSM.js +49 -0
  203. package/lib/stream-CPdqZ0Fp.d.ts +34 -0
  204. package/lib/streaming-DHST3Scg.js +96 -0
  205. package/lib/streaming.d.ts +23 -0
  206. package/lib/streaming.js +2 -0
  207. package/lib/testing.d.ts +10 -0
  208. package/lib/testing.js +17 -0
  209. package/lib/timeout.d.ts +30 -0
  210. package/lib/timeout.js +42 -0
  211. package/lib/timing.d.ts +130 -0
  212. package/lib/timing.js +183 -0
  213. package/lib/trailing-slash.d.ts +91 -0
  214. package/lib/trailing-slash.js +85 -0
  215. package/lib/trie-router-CfFbapzL.js +146 -0
  216. package/lib/types-CDO6I3B0.d.ts +40 -0
  217. package/lib/types-CHesD1KG.d.ts +195 -0
  218. package/lib/types-CwrQjlaM.d.ts +105 -0
  219. package/lib/types-DR95J00T.d.ts +29 -0
  220. package/lib/types.d.ts +2 -0
  221. package/lib/types.js +4 -0
  222. package/lib/utils/accept.d.ts +9 -0
  223. package/lib/utils/accept.js +193 -0
  224. package/lib/utils/basic-auth.d.ts +8 -0
  225. package/lib/utils/basic-auth.js +20 -0
  226. package/lib/utils/body.d.ts +2 -0
  227. package/lib/utils/body.js +94 -0
  228. package/lib/utils/buffer.d.ts +19 -0
  229. package/lib/utils/buffer.js +51 -0
  230. package/lib/utils/color.d.ts +24 -0
  231. package/lib/utils/color.js +37 -0
  232. package/lib/utils/compress.d.ts +11 -0
  233. package/lib/utils/compress.js +11 -0
  234. package/lib/utils/concurrent.d.ts +17 -0
  235. package/lib/utils/concurrent.js +30 -0
  236. package/lib/utils/constants.d.ts +7 -0
  237. package/lib/utils/constants.js +7 -0
  238. package/lib/utils/cookie.d.ts +2 -0
  239. package/lib/utils/cookie.js +125 -0
  240. package/lib/utils/crypto.d.ts +14 -0
  241. package/lib/utils/crypto.js +34 -0
  242. package/lib/utils/encode.d.ts +11 -0
  243. package/lib/utils/encode.js +33 -0
  244. package/lib/utils/filepath.d.ts +14 -0
  245. package/lib/utils/filepath.js +25 -0
  246. package/lib/utils/handler.d.ts +9 -0
  247. package/lib/utils/handler.js +12 -0
  248. package/lib/utils/headers.d.ts +2 -0
  249. package/lib/utils/headers.js +1 -0
  250. package/lib/utils/html.d.ts +46 -0
  251. package/lib/utils/html.js +103 -0
  252. package/lib/utils/http-status.d.ts +2 -0
  253. package/lib/utils/http-status.js +1 -0
  254. package/lib/utils/ipaddr.d.ts +56 -0
  255. package/lib/utils/ipaddr.js +238 -0
  256. package/lib/utils/jwt.d.ts +24 -0
  257. package/lib/utils/jwt.js +2 -0
  258. package/lib/utils/mime.d.ts +2 -0
  259. package/lib/utils/mime.js +77 -0
  260. package/lib/utils/stream.d.ts +2 -0
  261. package/lib/utils/stream.js +88 -0
  262. package/lib/utils/types.d.ts +2 -0
  263. package/lib/utils/types.js +1 -0
  264. package/lib/utils/url.d.ts +46 -0
  265. package/lib/utils/url.js +197 -0
  266. package/lib/utils-dw8lxqjL.js +4 -0
  267. package/lib/validator-D84mq-dO.js +65 -0
  268. package/lib/validator.d.ts +42 -0
  269. package/lib/validator.js +2 -0
  270. package/lib/ws.d.ts +2 -0
  271. package/lib/ws.js +53 -0
  272. package/package.json +315 -15
  273. package/plugins/acryl-inspect/src/report.ts +15 -10
  274. package/plugins/acryl-inspect/src/service.ts +2 -2
  275. package/plugins/acryl-inspect/tests/report.spec.ts +9 -3
  276. package/plugins/acryl-inspect/tests/service.spec.ts +3 -3
  277. package/plugins/acryl-inspect/upstream.json +2 -2
  278. package/plugins/acryl-node-server/tests/adapter/app.ts +2 -2
  279. package/plugins/acryl-node-server/tests/adapter/assets/static/acryl.html +1 -1
  280. package/plugins/acryl-node-server/tests/adapter/assets/static/index.html +1 -1
  281. package/plugins/acryl-node-server/tests/adapter/conninfo.test.ts +2 -2
  282. package/plugins/acryl-node-server/tests/adapter/content-length.test.ts +4 -4
  283. package/plugins/acryl-node-server/tests/adapter/early-hints.test.ts +5 -5
  284. package/plugins/acryl-node-server/tests/adapter/request-client-disconnect.test.ts +2 -2
  285. package/plugins/acryl-node-server/tests/adapter/serve-static.test.ts +20 -20
  286. package/plugins/acryl-node-server/tests/adapter/server-socket.test.ts +2 -2
  287. package/plugins/acryl-node-server/tests/adapter/server.test.ts +50 -50
  288. package/plugins/acryl-node-server/tests/adapter/utils/response.test.ts +4 -4
  289. package/plugins/acryl-node-server/tests/adapter/websocket.test.ts +9 -9
  290. package/plugins/acryl-node-server/tests/server.spec.ts +6 -6
  291. package/plugins/acryl-node-server/upstream.json +2 -2
  292. package/plugins/acryl-router/src/client.ts +1 -1
  293. package/plugins/acryl-router/src/connection.ts +2 -2
  294. package/plugins/acryl-router/src/core/app-base.ts +29 -29
  295. package/plugins/acryl-router/src/core/app.test.ts +175 -175
  296. package/plugins/acryl-router/src/core/app.ts +8 -8
  297. package/plugins/acryl-router/src/core/client/client.test.ts +76 -76
  298. package/plugins/acryl-router/src/core/client/client.ts +2 -2
  299. package/plugins/acryl-router/src/core/client/fetch-result-please.ts +1 -1
  300. package/plugins/acryl-router/src/core/client/index.ts +1 -1
  301. package/plugins/acryl-router/src/core/client/types.test.ts +6 -6
  302. package/plugins/acryl-router/src/core/client/types.ts +16 -16
  303. package/plugins/acryl-router/src/core/client/utils.test.ts +2 -2
  304. package/plugins/acryl-router/src/core/context.test.ts +4 -4
  305. package/plugins/acryl-router/src/core/context.ts +6 -6
  306. package/plugins/acryl-router/src/core/helper/accepts/accepts.test.ts +3 -3
  307. package/plugins/acryl-router/src/core/helper/accepts/index.ts +1 -1
  308. package/plugins/acryl-router/src/core/helper/adapter/index.test.ts +6 -6
  309. package/plugins/acryl-router/src/core/helper/adapter/index.ts +1 -1
  310. package/plugins/acryl-router/src/core/helper/conninfo/index.ts +1 -1
  311. package/plugins/acryl-router/src/core/helper/cookie/index.test.ts +8 -8
  312. package/plugins/acryl-router/src/core/helper/cookie/index.ts +1 -1
  313. package/plugins/acryl-router/src/core/helper/factory/index.test.ts +9 -9
  314. package/plugins/acryl-router/src/core/helper/factory/index.ts +10 -10
  315. package/plugins/acryl-router/src/core/helper/html/index.test.ts +4 -4
  316. package/plugins/acryl-router/src/core/helper/html/index.ts +1 -1
  317. package/plugins/acryl-router/src/core/helper/route/index.test.ts +7 -7
  318. package/plugins/acryl-router/src/core/helper/route/index.ts +8 -8
  319. package/plugins/acryl-router/src/core/helper/streaming/index.ts +1 -1
  320. package/plugins/acryl-router/src/core/helper/testing/index.test.ts +6 -6
  321. package/plugins/acryl-router/src/core/helper/testing/index.ts +4 -4
  322. package/plugins/acryl-router/src/core/helper/websocket/index.ts +2 -1
  323. package/plugins/acryl-router/src/core/http-exception.ts +1 -1
  324. package/plugins/acryl-router/src/core/index.ts +9 -9
  325. package/plugins/acryl-router/src/core/middleware/basic-auth/index.test.ts +6 -6
  326. package/plugins/acryl-router/src/core/middleware/basic-auth/index.ts +3 -3
  327. package/plugins/acryl-router/src/core/middleware/bearer-auth/index.test.ts +5 -5
  328. package/plugins/acryl-router/src/core/middleware/bearer-auth/index.ts +3 -3
  329. package/plugins/acryl-router/src/core/middleware/body-limit/index.test.ts +6 -6
  330. package/plugins/acryl-router/src/core/middleware/body-limit/index.ts +3 -3
  331. package/plugins/acryl-router/src/core/middleware/combine/index.test.ts +9 -9
  332. package/plugins/acryl-router/src/core/middleware/combine/index.ts +7 -7
  333. package/plugins/acryl-router/src/core/middleware/compress/index.test.ts +9 -9
  334. package/plugins/acryl-router/src/core/middleware/compress/index.ts +3 -3
  335. package/plugins/acryl-router/src/core/middleware/context-storage/index.test.ts +5 -5
  336. package/plugins/acryl-router/src/core/middleware/context-storage/index.ts +4 -4
  337. package/plugins/acryl-router/src/core/middleware/cors/index.test.ts +7 -7
  338. package/plugins/acryl-router/src/core/middleware/cors/index.ts +3 -3
  339. package/plugins/acryl-router/src/core/middleware/csrf/index.test.ts +10 -10
  340. package/plugins/acryl-router/src/core/middleware/csrf/index.ts +3 -3
  341. package/plugins/acryl-router/src/core/middleware/etag/index.test.ts +42 -42
  342. package/plugins/acryl-router/src/core/middleware/etag/index.ts +4 -4
  343. package/plugins/acryl-router/src/core/middleware/ip-restriction/index.test.ts +4 -4
  344. package/plugins/acryl-router/src/core/middleware/ip-restriction/index.ts +6 -6
  345. package/plugins/acryl-router/src/core/middleware/jwk/index.test.ts +16 -16
  346. package/plugins/acryl-router/src/core/middleware/jwk/jwk.ts +7 -6
  347. package/plugins/acryl-router/src/core/middleware/jwt/index.test.ts +16 -16
  348. package/plugins/acryl-router/src/core/middleware/jwt/jwt.ts +4 -3
  349. package/plugins/acryl-router/src/core/middleware/language/index.test.ts +3 -3
  350. package/plugins/acryl-router/src/core/middleware/language/language.ts +3 -3
  351. package/plugins/acryl-router/src/core/middleware/logger/index.test.ts +5 -5
  352. package/plugins/acryl-router/src/core/middleware/logger/index.ts +4 -4
  353. package/plugins/acryl-router/src/core/middleware/method-not-allowed/index.test.ts +24 -24
  354. package/plugins/acryl-router/src/core/middleware/method-not-allowed/index.ts +8 -8
  355. package/plugins/acryl-router/src/core/middleware/method-override/index.test.ts +5 -5
  356. package/plugins/acryl-router/src/core/middleware/method-override/index.ts +6 -6
  357. package/plugins/acryl-router/src/core/middleware/powered-by/index.test.ts +4 -4
  358. package/plugins/acryl-router/src/core/middleware/powered-by/index.ts +6 -6
  359. package/plugins/acryl-router/src/core/middleware/pretty-json/index.test.ts +30 -30
  360. package/plugins/acryl-router/src/core/middleware/pretty-json/index.ts +4 -4
  361. package/plugins/acryl-router/src/core/middleware/request-id/index.test.ts +15 -15
  362. package/plugins/acryl-router/src/core/middleware/request-id/request-id.ts +3 -3
  363. package/plugins/acryl-router/src/core/middleware/secure-headers/index.test.ts +35 -35
  364. package/plugins/acryl-router/src/core/middleware/secure-headers/secure-headers.ts +3 -3
  365. package/plugins/acryl-router/src/core/middleware/serve-static/index.test.ts +23 -23
  366. package/plugins/acryl-router/src/core/middleware/serve-static/index.ts +1 -1
  367. package/plugins/acryl-router/src/core/middleware/timeout/index.test.ts +2 -2
  368. package/plugins/acryl-router/src/core/middleware/timeout/index.ts +3 -3
  369. package/plugins/acryl-router/src/core/middleware/timing/index.test.ts +14 -14
  370. package/plugins/acryl-router/src/core/middleware/timing/timing.ts +3 -3
  371. package/plugins/acryl-router/src/core/middleware/trailing-slash/index.test.ts +7 -7
  372. package/plugins/acryl-router/src/core/middleware/trailing-slash/index.ts +7 -7
  373. package/plugins/acryl-router/src/core/request.test.ts +42 -42
  374. package/plugins/acryl-router/src/core/request.ts +6 -6
  375. package/plugins/acryl-router/src/core/router/linear-router/index.ts +1 -1
  376. package/plugins/acryl-router/src/core/router/pattern-router/index.ts +1 -1
  377. package/plugins/acryl-router/src/core/router/reg-exp-router/index.ts +1 -1
  378. package/plugins/acryl-router/src/core/router/smart-router/index.ts +1 -1
  379. package/plugins/acryl-router/src/core/router/trie-router/index.ts +1 -1
  380. package/plugins/acryl-router/src/core/types.test.ts +115 -115
  381. package/plugins/acryl-router/src/core/types.ts +72 -72
  382. package/plugins/acryl-router/src/core/utils/body.ts +8 -8
  383. package/plugins/acryl-router/src/core/utils/html.ts +1 -1
  384. package/plugins/acryl-router/src/core/utils/jwt/jws.ts +3 -3
  385. package/plugins/acryl-router/src/core/utils/jwt/jwt.test.ts +2 -2
  386. package/plugins/acryl-router/src/core/utils/jwt/jwt.ts +3 -3
  387. package/plugins/acryl-router/src/core/utils/jwt/types.ts +1 -1
  388. package/plugins/acryl-router/src/core/utils/types.ts +42 -0
  389. package/plugins/acryl-router/src/core/utils/url.test.ts +2 -2
  390. package/plugins/acryl-router/src/core/validator/index.ts +1 -1
  391. package/plugins/acryl-router/src/core/validator/validator.test.ts +28 -28
  392. package/plugins/acryl-router/src/deprecated.ts +15 -0
  393. package/plugins/acryl-router/src/index.ts +3 -2
  394. package/plugins/acryl-router/src/middleware/index.ts +1 -1
  395. package/plugins/acryl-router/src/middleware/loopback-only.ts +34 -1
  396. package/plugins/acryl-router/src/service.ts +19 -13
  397. package/plugins/acryl-router/tests/docs-examples.spec.ts +11 -11
  398. package/plugins/acryl-router/tests/live-install.spec.ts +2 -2
  399. package/plugins/acryl-router/tests/loader.spec.ts +3 -3
  400. package/plugins/acryl-router/tests/loopback-only.spec.ts +80 -0
  401. package/plugins/acryl-router/tests/no-dom/consumer.ts +11 -0
  402. package/plugins/acryl-router/tests/no-dom/tsconfig.json +13 -0
  403. package/plugins/acryl-router/tests/service.spec.ts +39 -22
  404. package/plugins/acryl-router/upstream.json +3 -3
  405. package/plugins/acryl-router-http/tests/adapter.spec.ts +3 -3
  406. package/plugins/acryl-router-http/tests/surfaces.spec.ts +2 -2
  407. package/plugins/acryl-router-http/upstream.json +2 -2
  408. package/plugins/acryl-state/src/service.ts +2 -2
  409. package/plugins/acryl-state/upstream.json +2 -2
  410. package/plugins/builder/script.ts +3 -3
  411. package/plugins/extensions/index.ts +4 -3
  412. package/plugins/extensions/resolve.ts +70 -10
  413. package/plugins/openai/prompt.ts +7 -7
  414. package/shared/AGENTS.core.md +61 -0
  415. package/templates/agent/AGENTS.md +6 -0
  416. package/templates/agent/README.md +2 -2
  417. package/templates/agent/blend.yaml +1 -1
  418. package/templates/agent/src/routes.ts +2 -2
  419. package/templates/agent/test/model.test.ts +2 -2
  420. package/templates/agent/tsconfig.json +40 -0
  421. package/templates/blank/AGENTS.md +5 -0
  422. package/templates/blank/README.md +1 -1
  423. package/templates/blank/src/routes.ts +2 -2
  424. package/templates/todo/AGENTS.md +5 -0
  425. package/templates/todo/src/todo.ts +6 -4
  426. package/templates/todo/upstream.json +2 -2
@@ -0,0 +1,33 @@
1
+ import { t as Context } from "./context-Bm7rI-oK.js";
2
+ import { t as AcceptHeader } from "./headers-tu2YYN8T.js";
3
+
4
+ //#region ../../plugins/acryl-router/src/core/helper/accepts/accepts.d.ts
5
+ interface Accept {
6
+ type: string;
7
+ params: Record<string, string>;
8
+ q: number;
9
+ }
10
+ interface acceptsConfig {
11
+ header: AcceptHeader;
12
+ supports: string[];
13
+ default: string;
14
+ }
15
+ interface acceptsOptions extends acceptsConfig {
16
+ match?: (accepts: Accept[], config: acceptsConfig) => string;
17
+ }
18
+ /**
19
+ * Match the accept header with the given options.
20
+ * @example
21
+ * ```ts
22
+ * app.get('/users', (c) => {
23
+ * const lang = accepts(c, {
24
+ * header: 'Accept-Language',
25
+ * supports: ['en', 'zh'],
26
+ * default: 'en',
27
+ * })
28
+ * })
29
+ * ```
30
+ */
31
+ declare const accepts: (c: Context, options: acceptsOptions) => string;
32
+ //#endregion
33
+ export { accepts };
package/lib/accepts.js ADDED
@@ -0,0 +1,2 @@
1
+ import { t as accepts } from "./accepts-XBuZPVRI.js";
2
+ export { accepts };
@@ -0,0 +1,2 @@
1
+ import { $ as Acryl, et as AcrylOptions } from "./context-Bm7rI-oK.js";
2
+ export { Acryl as AcrylBase, AcrylOptions };
@@ -0,0 +1,332 @@
1
+ import { t as compose } from "./compose-BuW2B6PZ.js";
2
+ import { getPath, getPathNoStrict, mergePath } from "./utils/url.js";
3
+ import { t as Context } from "./context-DDO0cfuP.js";
4
+ import { METHODS } from "./router.js";
5
+ import { COMPOSED_HANDLER } from "./utils/constants.js";
6
+ //#region ../../plugins/acryl-router/src/core/app-base.ts
7
+ /**
8
+ * @module
9
+ * This module is the base module for the Acryl object.
10
+ */
11
+ const notFoundHandler = (c) => {
12
+ return c.text("404 Not Found", 404);
13
+ };
14
+ const errorHandler = (err, c) => {
15
+ if ("getResponse" in err) {
16
+ const res = err.getResponse();
17
+ return c.newResponse(res.body, res);
18
+ }
19
+ console.error(err);
20
+ return c.text("Internal Server Error", 500);
21
+ };
22
+ var Acryl = class Acryl {
23
+ get;
24
+ post;
25
+ put;
26
+ delete;
27
+ options;
28
+ patch;
29
+ query;
30
+ all;
31
+ on;
32
+ use;
33
+ router;
34
+ getPath;
35
+ _basePath = "/";
36
+ #path = "/";
37
+ routes = [];
38
+ constructor(options = {}) {
39
+ [...METHODS, "all"].forEach((method) => {
40
+ this[method] = (args1, ...args) => {
41
+ const methodName = method.toUpperCase();
42
+ if (typeof args1 === "string") this.#path = args1;
43
+ else this.#addRoute(methodName, this.#path, args1);
44
+ args.forEach((handler) => {
45
+ this.#addRoute(methodName, this.#path, handler);
46
+ });
47
+ return this;
48
+ };
49
+ });
50
+ this.on = (method, path, ...handlers) => {
51
+ for (const p of [path].flat()) {
52
+ this.#path = p;
53
+ for (const m of [method].flat()) {
54
+ const methodName = m.toUpperCase();
55
+ for (const handler of handlers) this.#addRoute(methodName, this.#path, handler);
56
+ }
57
+ }
58
+ return this;
59
+ };
60
+ this.use = (arg1, ...handlers) => {
61
+ if (typeof arg1 === "string") this.#path = arg1;
62
+ else {
63
+ this.#path = "*";
64
+ handlers.unshift(arg1);
65
+ }
66
+ handlers.forEach((handler) => {
67
+ this.#addRoute("ALL", this.#path, handler);
68
+ });
69
+ return this;
70
+ };
71
+ const { strict, ...optionsWithoutStrict } = options;
72
+ Object.assign(this, optionsWithoutStrict);
73
+ this.getPath = strict ?? true ? options.getPath ?? getPath : getPathNoStrict;
74
+ }
75
+ #clone() {
76
+ const clone = new Acryl({
77
+ router: this.router,
78
+ getPath: this.getPath
79
+ });
80
+ clone.errorHandler = this.errorHandler;
81
+ clone.#notFoundHandler = this.#notFoundHandler;
82
+ clone.routes = this.routes;
83
+ return clone;
84
+ }
85
+ #notFoundHandler = notFoundHandler;
86
+ errorHandler = errorHandler;
87
+ /**
88
+ * `.route()` allows grouping other Acryl instance in routes.
89
+ *
90
+ *
91
+ * @param {string} path - base Path
92
+ * @param {Acryl} app - other Acryl instance
93
+ * @returns {Acryl} routed Acryl instance
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * const app = new Acryl()
98
+ * const app2 = new Acryl()
99
+ *
100
+ * app2.get("/user", (c) => c.text("user"))
101
+ * app.route("/api", app2) // GET /api/user
102
+ * ```
103
+ */
104
+ route(path, app) {
105
+ const subApp = this.basePath(path);
106
+ app.routes.map((r) => {
107
+ let handler;
108
+ if (app.errorHandler === errorHandler) handler = r.handler;
109
+ else {
110
+ handler = async (c, next) => (await compose([], app.errorHandler)(c, () => r.handler(c, next))).res;
111
+ handler[COMPOSED_HANDLER] = r.handler;
112
+ }
113
+ subApp.#addRoute(r.method, r.path, handler, r.basePath);
114
+ });
115
+ return this;
116
+ }
117
+ /**
118
+ * `.basePath()` allows base paths to be specified.
119
+ *
120
+ *
121
+ * @param {string} path - base Path
122
+ * @returns {Acryl} changed Acryl instance
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * const api = new Acryl().basePath('/api')
127
+ * ```
128
+ */
129
+ basePath(path) {
130
+ const subApp = this.#clone();
131
+ subApp._basePath = mergePath(this._basePath, path);
132
+ return subApp;
133
+ }
134
+ /**
135
+ * `.onError()` handles an error and returns a customized Response.
136
+ *
137
+ *
138
+ * @param {ErrorHandler} handler - request Handler for error
139
+ * @returns {Acryl} changed Acryl instance
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * app.onError((err, c) => {
144
+ * console.error(`${err}`)
145
+ * return c.text('Custom Error Message', 500)
146
+ * })
147
+ * ```
148
+ */
149
+ onError = (handler) => {
150
+ this.errorHandler = handler;
151
+ return this;
152
+ };
153
+ /**
154
+ * `.notFound()` allows you to customize a Not Found Response.
155
+ *
156
+ *
157
+ * @param {NotFoundHandler} handler - request handler for not-found
158
+ * @returns {Acryl} changed Acryl instance
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * app.notFound((c) => {
163
+ * return c.text('Custom 404 Message', 404)
164
+ * })
165
+ * ```
166
+ */
167
+ notFound = (handler) => {
168
+ this.#notFoundHandler = handler;
169
+ return this;
170
+ };
171
+ /**
172
+ * `.mount()` allows you to mount applications built with other frameworks into your Acryl application.
173
+ *
174
+ * @deprecated Use `mount()` from `acryl-router/mount` instead. `.mount()` will be removed in v5.
175
+ *
176
+ *
177
+ * @param {string} path - base Path
178
+ * @param {Function} applicationHandler - other Request Handler
179
+ * @param {MountOptions} [options] - options of `.mount()`
180
+ * @returns {Acryl} mounted Acryl instance
181
+ *
182
+ * @example
183
+ * ```ts
184
+ * import { Router as IttyRouter } from 'itty-router'
185
+ * import { Acryl } from 'acrylblends'
186
+ * // Create itty-router application
187
+ * const ittyRouter = IttyRouter()
188
+ * // GET /itty-router/hello
189
+ * ittyRouter.get('/hello', () => new Response('Hello from itty-router'))
190
+ *
191
+ * const app = new Acryl()
192
+ * app.mount('/itty-router', ittyRouter.handle)
193
+ * ```
194
+ *
195
+ * @example
196
+ * ```ts
197
+ * const app = new Acryl()
198
+ * // Send the request to another application without modification.
199
+ * app.mount('/app', anotherApp, {
200
+ * replaceRequest: (req) => req,
201
+ * })
202
+ * ```
203
+ */
204
+ mount(path, applicationHandler, options) {
205
+ let replaceRequest;
206
+ let optionHandler;
207
+ if (options) if (typeof options === "function") optionHandler = options;
208
+ else {
209
+ optionHandler = options.optionHandler;
210
+ if (options.replaceRequest === false) replaceRequest = (request) => request;
211
+ else replaceRequest = options.replaceRequest;
212
+ }
213
+ const getOptions = optionHandler ? (c) => {
214
+ const options = optionHandler(c);
215
+ return Array.isArray(options) ? options : [options];
216
+ } : (c) => {
217
+ let executionContext = void 0;
218
+ try {
219
+ executionContext = c.executionCtx;
220
+ } catch {}
221
+ return [c.env, executionContext];
222
+ };
223
+ replaceRequest ||= (() => {
224
+ const mergedPath = mergePath(this._basePath, path);
225
+ const pathPrefixLength = mergedPath === "/" ? 0 : mergedPath.length;
226
+ return (request) => {
227
+ const url = new URL(request.url);
228
+ url.pathname = this.getPath(request).slice(pathPrefixLength) || "/";
229
+ return new Request(url, request);
230
+ };
231
+ })();
232
+ const handler = async (c, next) => {
233
+ const res = await applicationHandler(replaceRequest(c.req.raw), ...getOptions(c));
234
+ if (res) return res;
235
+ await next();
236
+ };
237
+ this.#addRoute("ALL", mergePath(path, "*"), handler);
238
+ return this;
239
+ }
240
+ #addRoute(method, path, handler, baseRoutePath) {
241
+ path = mergePath(this._basePath, path);
242
+ const r = {
243
+ basePath: baseRoutePath !== void 0 ? mergePath(this._basePath, baseRoutePath) : this._basePath,
244
+ path,
245
+ method,
246
+ handler
247
+ };
248
+ this.router.add(method, path, [handler, r]);
249
+ this.routes.push(r);
250
+ }
251
+ #handleError(err, c) {
252
+ if (err instanceof Error) return this.errorHandler(err, c);
253
+ throw err;
254
+ }
255
+ #dispatch(request, executionCtx, env, method) {
256
+ if (method === "HEAD") return (async () => new Response(null, await this.#dispatch(request, executionCtx, env, "GET")))();
257
+ const path = this.getPath(request, { env });
258
+ const matchResult = this.router.match(method, path);
259
+ const c = new Context(request, {
260
+ path,
261
+ matchResult,
262
+ env,
263
+ executionCtx,
264
+ notFoundHandler: this.#notFoundHandler
265
+ });
266
+ if (matchResult[0].length === 1) {
267
+ let res;
268
+ try {
269
+ res = matchResult[0][0][0][0](c, async () => {
270
+ c.res = await this.#notFoundHandler(c);
271
+ });
272
+ } catch (err) {
273
+ return this.#handleError(err, c);
274
+ }
275
+ return res instanceof Promise ? res.then((resolved) => resolved || (c.finalized ? c.res : this.#notFoundHandler(c))).catch((err) => this.#handleError(err, c)) : res ?? this.#notFoundHandler(c);
276
+ }
277
+ const composed = compose(matchResult[0], this.errorHandler, this.#notFoundHandler);
278
+ return (async () => {
279
+ try {
280
+ const context = await composed(c);
281
+ if (!context.finalized) throw new Error("Context is not finalized. Did you forget to return a Response object or `await next()`?");
282
+ return context.res;
283
+ } catch (err) {
284
+ return this.#handleError(err, c);
285
+ }
286
+ })();
287
+ }
288
+ /**
289
+ * `.fetch()` will be entry point of your app.
290
+ *
291
+ *
292
+ * @param {Request} request - request Object of request
293
+ * @param {Env} env - env Object
294
+ * @param {ExecutionContext} executionCtx - context of execution
295
+ * @returns {Response | Promise<Response>} response of request
296
+ *
297
+ */
298
+ fetch = (request, ...rest) => {
299
+ return this.#dispatch(request, rest[1], rest[0], request.method);
300
+ };
301
+ /**
302
+ * `.request()` is a useful method for testing.
303
+ * You can pass a URL or pathname to send a GET request.
304
+ * app will return a Response object.
305
+ * ```ts
306
+ * test('GET /hello is ok', async () => {
307
+ * const res = await app.request('/hello')
308
+ * expect(res.status).toBe(200)
309
+ * })
310
+ * ```
311
+ */
312
+ request = (input, requestInit, Env, executionCtx) => {
313
+ if (input instanceof Request) return this.fetch(requestInit ? new Request(input, requestInit) : input, Env, executionCtx);
314
+ input = input.toString();
315
+ return this.fetch(new Request(/^https?:\/\//.test(input) ? input : `http://localhost${mergePath("/", input)}`, requestInit), Env, executionCtx);
316
+ };
317
+ /**
318
+ * `.fire()` automatically adds a global fetch event listener.
319
+ * This can be useful for environments that adhere to the Service Worker API, such as non-ES module Cloudflare Workers.
320
+ * @deprecated
321
+ * ACRYL ships no service-worker adapter: mount the app with `ctx.router.route(path, app)` and serve it through an ACRYL adapter instead.
322
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API
323
+ * @see https://developers.cloudflare.com/workers/reference/migrate-to-module-workers/
324
+ */
325
+ fire = () => {
326
+ addEventListener("fetch", (event) => {
327
+ event.respondWith(this.#dispatch(event.request, event, void 0, event.request.method));
328
+ });
329
+ };
330
+ };
331
+ //#endregion
332
+ export { Acryl as AcrylBase };
@@ -0,0 +1,12 @@
1
+ import { t as Context } from "./context-Bm7rI-oK.js";
2
+
3
+ //#region ../../plugins/acryl-router/src/core/helper/adapter/index.d.ts
4
+ type Runtime = 'node' | 'deno' | 'bun' | 'workerd' | 'fastly' | 'edge-light' | 'other';
5
+ declare const env: <T extends Record<string, unknown>, C extends Context = Context<{
6
+ Bindings: T;
7
+ }>>(c: T extends Record<string, unknown> ? Context : C, runtime?: Runtime) => T & C["env"];
8
+ declare const knownUserAgents: Partial<Record<Runtime, string>>;
9
+ declare const getRuntimeKey: () => Runtime;
10
+ declare const checkUserAgentEquals: (platform: string) => boolean;
11
+ //#endregion
12
+ export { Runtime, checkUserAgentEquals, env, getRuntimeKey, knownUserAgents };
package/lib/adapter.js ADDED
@@ -0,0 +1,37 @@
1
+ //#region ../../plugins/acryl-router/src/core/helper/adapter/index.ts
2
+ const env = (c, runtime) => {
3
+ const globalEnv = globalThis?.process?.env;
4
+ runtime ??= getRuntimeKey();
5
+ return {
6
+ bun: () => globalEnv,
7
+ node: () => globalEnv,
8
+ "edge-light": () => globalEnv,
9
+ deno: () => {
10
+ return Deno.env.toObject();
11
+ },
12
+ workerd: () => c.env,
13
+ fastly: () => ({}),
14
+ other: () => ({})
15
+ }[runtime]();
16
+ };
17
+ const knownUserAgents = {
18
+ deno: "Deno",
19
+ bun: "Bun",
20
+ workerd: "Cloudflare-Workers",
21
+ node: "Node.js"
22
+ };
23
+ const getRuntimeKey = () => {
24
+ const global = globalThis;
25
+ if (typeof navigator !== "undefined" && typeof navigator.userAgent === "string") {
26
+ for (const [runtimeKey, userAgent] of Object.entries(knownUserAgents)) if (checkUserAgentEquals(userAgent)) return runtimeKey;
27
+ }
28
+ if (typeof global?.EdgeRuntime === "string") return "edge-light";
29
+ if (global?.fastly !== void 0) return "fastly";
30
+ if (global?.process?.release?.name === "node") return "node";
31
+ return "other";
32
+ };
33
+ const checkUserAgentEquals = (platform) => {
34
+ return navigator.userAgent.startsWith(platform);
35
+ };
36
+ //#endregion
37
+ export { checkUserAgentEquals, env, getRuntimeKey, knownUserAgents };
@@ -0,0 +1,26 @@
1
+ import { AcrylBase as Acryl$1 } from "./acryl-base.js";
2
+ import { i as RegExpRouter } from "./reg-exp-router-DC1R3qoY.js";
3
+ import { t as SmartRouter } from "./smart-router-C5CZ5XSM.js";
4
+ import { t as TrieRouter } from "./trie-router-CfFbapzL.js";
5
+ //#region ../../plugins/acryl-router/src/core/app.ts
6
+ /**
7
+ * The Acryl class extends the functionality of the AcrylBase class.
8
+ * It sets up routing and allows for custom options to be passed.
9
+ *
10
+ * @template E - The environment type.
11
+ * @template S - The schema type.
12
+ * @template BasePath - The base path type.
13
+ */
14
+ var Acryl = class extends Acryl$1 {
15
+ /**
16
+ * Creates an instance of the Acryl class.
17
+ *
18
+ * @param options - Optional configuration options for the Acryl instance.
19
+ */
20
+ constructor(options = {}) {
21
+ super(options);
22
+ this.router = options.router ?? new SmartRouter({ routers: [new RegExpRouter(), new TrieRouter()] });
23
+ }
24
+ };
25
+ //#endregion
26
+ export { Acryl as t };
@@ -0,0 +1,21 @@
1
+ import { $ as Acryl$1, J as Schema, _ as Env, et as AcrylOptions, h as BlankSchema, p as BlankEnv } from "./context-Bm7rI-oK.js";
2
+
3
+ //#region ../../plugins/acryl-router/src/core/app.d.ts
4
+ /**
5
+ * The Acryl class extends the functionality of the AcrylBase class.
6
+ * It sets up routing and allows for custom options to be passed.
7
+ *
8
+ * @template E - The environment type.
9
+ * @template S - The schema type.
10
+ * @template BasePath - The base path type.
11
+ */
12
+ declare class Acryl<E extends Env = BlankEnv, S extends Schema = BlankSchema, BasePath extends string = '/'> extends Acryl$1<E, S, BasePath> {
13
+ /**
14
+ * Creates an instance of the Acryl class.
15
+ *
16
+ * @param options - Optional configuration options for the Acryl instance.
17
+ */
18
+ constructor(options?: AcrylOptions<E>);
19
+ }
20
+ //#endregion
21
+ export { Acryl as t };
@@ -0,0 +1,72 @@
1
+ import { I as MiddlewareHandler, t as Context } from "./context-Bm7rI-oK.js";
2
+
3
+ //#region ../../plugins/acryl-router/src/core/middleware/basic-auth/index.d.ts
4
+ type MessageFunction = (c: Context) => string | object | Promise<string | object>;
5
+ type BasicAuthOptions = {
6
+ username: string;
7
+ password: string;
8
+ realm?: string;
9
+ hashFunction?: Function;
10
+ invalidUserMessage?: string | object | MessageFunction;
11
+ onAuthSuccess?: (c: Context, username: string) => void | Promise<void>;
12
+ } | {
13
+ verifyUser: (username: string, password: string, c: Context) => boolean | Promise<boolean>;
14
+ realm?: string;
15
+ hashFunction?: Function;
16
+ invalidUserMessage?: string | object | MessageFunction;
17
+ onAuthSuccess?: (c: Context, username: string) => void | Promise<void>;
18
+ };
19
+ /**
20
+ * Basic Auth Middleware for Acryl.
21
+ *
22
+ *
23
+ * @param {BasicAuthOptions} options - The options for the basic authentication middleware.
24
+ * @param {string} options.username - The username for authentication.
25
+ * @param {string} options.password - The password for authentication.
26
+ * @param {string} [options.realm="Secure Area"] - The realm attribute for the WWW-Authenticate header.
27
+ * @param {Function} [options.hashFunction] - The hash function used for secure comparison.
28
+ * @param {Function} [options.verifyUser] - The function to verify user credentials.
29
+ * @param {string | object | MessageFunction} [options.invalidUserMessage="Unauthorized"] - The invalid user message.
30
+ * @param {Function} [options.onAuthSuccess] - Callback function called on successful authentication.
31
+ * @returns {MiddlewareHandler} The middleware handler function.
32
+ * @throws {HTTPException} If neither "username and password" nor "verifyUser" options are provided.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * const app = new Acryl()
37
+ *
38
+ * app.use(
39
+ * '/auth/*',
40
+ * basicAuth({
41
+ * username: process.env.BASIC_AUTH_USERNAME,
42
+ * password: process.env.BASIC_AUTH_PASSWORD,
43
+ * })
44
+ * )
45
+ *
46
+ * app.get('/auth/page', (c) => {
47
+ * return c.text('You are authorized')
48
+ * })
49
+ * ```
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * // With onAuthSuccess callback
54
+ * app.use(
55
+ * '/auth/*',
56
+ * basicAuth({
57
+ * username: process.env.BASIC_AUTH_USERNAME,
58
+ * password: process.env.BASIC_AUTH_PASSWORD,
59
+ * onAuthSuccess: (c, username) => {
60
+ * c.set('user', { name: username, role: 'admin' })
61
+ * console.log(`User ${username} authenticated`)
62
+ * },
63
+ * })
64
+ * )
65
+ * ```
66
+ */
67
+ declare const basicAuth: (options: BasicAuthOptions, ...users: {
68
+ username: string;
69
+ password: string;
70
+ }[]) => MiddlewareHandler;
71
+ //#endregion
72
+ export { basicAuth };
@@ -0,0 +1,95 @@
1
+ import { HTTPException } from "./http-exception.js";
2
+ import { timingSafeEqual } from "./utils/buffer.js";
3
+ import { auth } from "./utils/basic-auth.js";
4
+ //#region ../../plugins/acryl-router/src/core/middleware/basic-auth/index.ts
5
+ /**
6
+ * Basic Auth Middleware for Acryl.
7
+ *
8
+ *
9
+ * @param {BasicAuthOptions} options - The options for the basic authentication middleware.
10
+ * @param {string} options.username - The username for authentication.
11
+ * @param {string} options.password - The password for authentication.
12
+ * @param {string} [options.realm="Secure Area"] - The realm attribute for the WWW-Authenticate header.
13
+ * @param {Function} [options.hashFunction] - The hash function used for secure comparison.
14
+ * @param {Function} [options.verifyUser] - The function to verify user credentials.
15
+ * @param {string | object | MessageFunction} [options.invalidUserMessage="Unauthorized"] - The invalid user message.
16
+ * @param {Function} [options.onAuthSuccess] - Callback function called on successful authentication.
17
+ * @returns {MiddlewareHandler} The middleware handler function.
18
+ * @throws {HTTPException} If neither "username and password" nor "verifyUser" options are provided.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const app = new Acryl()
23
+ *
24
+ * app.use(
25
+ * '/auth/*',
26
+ * basicAuth({
27
+ * username: process.env.BASIC_AUTH_USERNAME,
28
+ * password: process.env.BASIC_AUTH_PASSWORD,
29
+ * })
30
+ * )
31
+ *
32
+ * app.get('/auth/page', (c) => {
33
+ * return c.text('You are authorized')
34
+ * })
35
+ * ```
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * // With onAuthSuccess callback
40
+ * app.use(
41
+ * '/auth/*',
42
+ * basicAuth({
43
+ * username: process.env.BASIC_AUTH_USERNAME,
44
+ * password: process.env.BASIC_AUTH_PASSWORD,
45
+ * onAuthSuccess: (c, username) => {
46
+ * c.set('user', { name: username, role: 'admin' })
47
+ * console.log(`User ${username} authenticated`)
48
+ * },
49
+ * })
50
+ * )
51
+ * ```
52
+ */
53
+ const basicAuth = (options, ...users) => {
54
+ const usernamePasswordInOptions = "username" in options && "password" in options;
55
+ const verifyUserInOptions = "verifyUser" in options;
56
+ if (!(usernamePasswordInOptions || verifyUserInOptions)) throw new Error("basic auth middleware requires options for \"username and password\" or \"verifyUser\"");
57
+ if (!options.realm) options.realm = "Secure Area";
58
+ if (!options.invalidUserMessage) options.invalidUserMessage = "Unauthorized";
59
+ if (usernamePasswordInOptions) users.unshift({
60
+ username: options.username,
61
+ password: options.password
62
+ });
63
+ return async function basicAuth(ctx, next) {
64
+ const requestUser = auth(ctx.req.raw);
65
+ if (requestUser) if (verifyUserInOptions) {
66
+ if (await options.verifyUser(requestUser.username, requestUser.password, ctx)) {
67
+ if (options.onAuthSuccess) await options.onAuthSuccess(ctx, requestUser.username);
68
+ await next();
69
+ return;
70
+ }
71
+ } else for (const user of users) {
72
+ const [usernameEqual, passwordEqual] = await Promise.all([timingSafeEqual(user.username, requestUser.username, options.hashFunction), timingSafeEqual(user.password, requestUser.password, options.hashFunction)]);
73
+ if (usernameEqual && passwordEqual) {
74
+ if (options.onAuthSuccess) await options.onAuthSuccess(ctx, requestUser.username);
75
+ await next();
76
+ return;
77
+ }
78
+ }
79
+ const status = 401;
80
+ const headers = { "WWW-Authenticate": "Basic realm=\"" + options.realm?.replace(/"/g, "\\\"") + "\"" };
81
+ const responseMessage = typeof options.invalidUserMessage === "function" ? await options.invalidUserMessage(ctx) : options.invalidUserMessage;
82
+ throw new HTTPException(status, { res: typeof responseMessage === "string" ? new Response(responseMessage, {
83
+ status,
84
+ headers
85
+ }) : new Response(JSON.stringify(responseMessage), {
86
+ status,
87
+ headers: {
88
+ ...headers,
89
+ "content-type": "application/json"
90
+ }
91
+ }) });
92
+ };
93
+ };
94
+ //#endregion
95
+ export { basicAuth };