@kayjs/kay 1.105.0 → 1.107.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 (467) hide show
  1. package/README.md +2 -2
  2. package/dist/eslint.js +777 -210
  3. package/editors/zed/extension.toml +2 -2
  4. package/llms/action-protection.md +2 -2
  5. package/llms/actions-from-islands.md +2 -2
  6. package/llms/actions-refused-fields.md +22 -0
  7. package/llms/actions.md +24 -41
  8. package/llms/analytics.md +1 -1
  9. package/llms/api-from-islands.md +1 -1
  10. package/llms/api-routes.md +6 -6
  11. package/llms/browser-only.md +1 -1
  12. package/llms/check-and-tools.md +1 -1
  13. package/llms/code-lints.md +2 -2
  14. package/llms/compiler-api.md +1 -1
  15. package/llms/component-libraries.md +1 -0
  16. package/llms/components.md +1 -1
  17. package/llms/content-cms.md +1 -1
  18. package/llms/content-markdown-to-caching.md +1 -1
  19. package/llms/content-preview.md +2 -2
  20. package/llms/content.md +21 -21
  21. package/llms/deprecated-lints-1-105-libraries.md +2 -0
  22. package/llms/deprecated-lints-1-105.md +3 -3
  23. package/llms/deprecated-lints.md +4 -4
  24. package/llms/dev-tool.md +2 -2
  25. package/llms/environment.md +22 -22
  26. package/llms/files.md +5 -5
  27. package/llms/island-and-form-lints.md +1 -0
  28. package/llms/islands-state-to-ids.md +2 -1
  29. package/llms/islands.md +10 -1
  30. package/llms/local-collections.md +2 -2
  31. package/llms/mcp.md +2 -1
  32. package/llms/navigation-from-code.md +1 -1
  33. package/llms/notifications.md +4 -2
  34. package/llms/project-new-and-add.md +1 -1
  35. package/llms/project.md +3 -3
  36. package/llms/rate-limits.md +1 -1
  37. package/llms/recipes.md +16 -16
  38. package/llms/rooms.md +4 -4
  39. package/llms/routing-endpoints-and-base.md +1 -1
  40. package/llms/routing.md +5 -5
  41. package/llms/schemas.md +14 -7
  42. package/llms/server.md +7 -7
  43. package/llms/site.md +9 -9
  44. package/llms/sitemap-search-fonts.md +2 -0
  45. package/llms/styles-and-assets.md +2 -1
  46. package/llms/templates-elements-slots-snippets.md +2 -2
  47. package/llms/testing-browser.md +20 -0
  48. package/llms/testing.md +1 -1
  49. package/llms/upgrading-1-105-config.md +2 -2
  50. package/llms/upgrading.md +37 -4
  51. package/llms/whats-new-archive.md +35 -66
  52. package/llms/whats-new.md +47 -28
  53. package/llms.txt +1 -1
  54. package/package.json +24 -11
  55. package/src/add.ts +1 -1
  56. package/src/build/class-table.ts +2 -2
  57. package/src/build/client.ts +27 -30
  58. package/src/build/entry-files.ts +19 -0
  59. package/src/build/lazy-components.ts +130 -0
  60. package/src/build/lazy-manifest.ts +56 -0
  61. package/src/build/lazy-stubs.ts +31 -0
  62. package/src/build/plugin.ts +16 -11
  63. package/src/build/runtime-chunk.ts +1 -1
  64. package/src/build/styles.ts +3 -3
  65. package/src/build/transform.ts +19 -7
  66. package/src/build/ui-layers.ts +30 -0
  67. package/src/build/ui.ts +108 -11
  68. package/src/build/variants.ts +125 -46
  69. package/src/build/worker-bundle.ts +6 -0
  70. package/src/check/action-options.ts +56 -0
  71. package/src/check/boot.ts +22 -1
  72. package/src/check/call-result.ts +242 -0
  73. package/src/check/config-objects.ts +17 -2
  74. package/src/check/diagnose.ts +22 -0
  75. package/src/check/handler-events.ts +149 -0
  76. package/src/check/index.ts +6 -2
  77. package/src/check/link-flag-tests.ts +130 -0
  78. package/src/check/project.ts +4 -3
  79. package/src/check/refusal-calls.ts +2 -2
  80. package/src/check/regroup.ts +5 -3
  81. package/src/check/renamed-packages.ts +58 -14
  82. package/src/check/schema-builders.ts +256 -0
  83. package/src/check/schema-json.ts +226 -0
  84. package/src/check/secret-keys.ts +15 -2
  85. package/src/check/shadow-types.ts +13 -9
  86. package/src/check/template-hints.ts +1 -1
  87. package/src/check/tool-labels.ts +11 -9
  88. package/src/check/ui-types.ts +4 -3
  89. package/src/check/ui.ts +14 -11
  90. package/src/commands/env-declare.ts +18 -6
  91. package/src/commands/routes.ts +1 -1
  92. package/src/compiler/a11y.ts +8 -1
  93. package/src/compiler/ast.ts +1 -1
  94. package/src/compiler/bound-props.ts +1 -1
  95. package/src/compiler/codegen.ts +13 -23
  96. package/src/compiler/deprecated-lints-1-105.ts +41 -0
  97. package/src/compiler/deprecated-lints-1-106.ts +14 -0
  98. package/src/compiler/deprecated-lints-1-107.ts +14 -0
  99. package/src/compiler/deprecated-lints.ts +17 -60
  100. package/src/compiler/deprecated-typed-lints.ts +23 -0
  101. package/src/compiler/dom-here.ts +72 -0
  102. package/src/compiler/dom.ts +23 -23
  103. package/src/compiler/fence-checks.ts +12 -11
  104. package/src/compiler/field-described.ts +2 -2
  105. package/src/compiler/helper-imports.ts +1 -1
  106. package/src/compiler/html-fields.ts +92 -0
  107. package/src/compiler/html-initial.ts +5 -3
  108. package/src/compiler/html.ts +14 -31
  109. package/src/compiler/import-edits.ts +15 -0
  110. package/src/compiler/index.ts +10 -3
  111. package/src/compiler/island-declaration.ts +15 -0
  112. package/src/compiler/items.ts +5 -5
  113. package/src/compiler/kay-exports.ts +1 -0
  114. package/src/compiler/lazy-imports.ts +357 -0
  115. package/src/compiler/link-flags.ts +6 -5
  116. package/src/compiler/lints.ts +12 -12
  117. package/src/compiler/parser.ts +2 -2
  118. package/src/compiler/rest-omits.ts +15 -0
  119. package/src/compiler/route-lints.ts +2 -0
  120. package/src/compiler/schema-lints.ts +13 -0
  121. package/src/compiler/script-server.ts +6 -3
  122. package/src/config-loader.ts +10 -2
  123. package/src/config-resolve.ts +3 -1
  124. package/src/config-shape.ts +2 -2
  125. package/src/config.ts +20 -4
  126. package/src/content/markdown.ts +2 -3
  127. package/src/content/validate.ts +1 -1
  128. package/src/generate-resource.ts +9 -9
  129. package/src/generate.ts +1 -1
  130. package/src/mcp/guards.ts +1 -1
  131. package/src/mcp/tools.ts +1 -1
  132. package/src/migrate/content.ts +61 -55
  133. package/src/new.ts +6 -2
  134. package/src/runtime/action-shared.ts +2 -2
  135. package/src/runtime/action.ts +51 -4
  136. package/src/runtime/client.ts +10 -0
  137. package/src/runtime/dom-lazy.ts +276 -0
  138. package/src/runtime/field-errors.ts +58 -0
  139. package/src/runtime/field-refusal.ts +179 -0
  140. package/src/runtime/form-types.ts +99 -0
  141. package/src/runtime/form.ts +25 -9
  142. package/src/runtime/html.ts +42 -6
  143. package/src/runtime/index.ts +2 -0
  144. package/src/runtime/island.server.ts +36 -2
  145. package/src/runtime/markdown-autolink.ts +69 -0
  146. package/src/runtime/markdown-render.client.ts +39 -3
  147. package/src/runtime/markdown.client.ts +6 -3
  148. package/src/runtime/markdown.server.ts +6 -3
  149. package/src/runtime/markdown.ts +125 -1
  150. package/src/runtime/navigate-api.ts +10 -5
  151. package/src/runtime/persist.ts +7 -7
  152. package/src/runtime/query-param.ts +1 -1
  153. package/src/runtime/reactive.ts +14 -0
  154. package/src/runtime/sanitize.ts +3 -1
  155. package/src/runtime/schema-record.ts +32 -0
  156. package/src/runtime/schema.client.ts +101 -14
  157. package/src/runtime/schema.ts +328 -26
  158. package/src/runtime/standard-schema.ts +32 -0
  159. package/src/runtime/swap-focus.ts +59 -11
  160. package/src/runtime/validate-run.client.ts +179 -0
  161. package/src/runtime/validate.client.ts +11 -0
  162. package/src/runtime/validate.ts +4 -137
  163. package/src/server/action-module.ts +16 -0
  164. package/src/server/actions.ts +54 -57
  165. package/src/server/answer.ts +102 -0
  166. package/src/server/api-input.ts +166 -0
  167. package/src/server/api-modules.ts +1 -1
  168. package/src/server/api.ts +12 -32
  169. package/src/server/app-stages.ts +38 -26
  170. package/src/server/app.ts +5 -2
  171. package/src/server/client.ts +2 -0
  172. package/src/server/dev-ui.ts +2 -2
  173. package/src/server/dev.ts +7 -9
  174. package/src/server/env-state.ts +1 -1
  175. package/src/server/env.ts +11 -3
  176. package/src/server/favicon.ts +47 -1
  177. package/src/server/file-limit.ts +7 -1
  178. package/src/server/forms.ts +1 -1
  179. package/src/server/host-allowlist.ts +7 -7
  180. package/src/server/image-metadata-bmff.ts +1 -1
  181. package/src/server/index.ts +2 -0
  182. package/src/server/kay-routes.ts +221 -0
  183. package/src/server/kay-server.ts +228 -0
  184. package/src/server/kept-exports.ts +1 -0
  185. package/src/server/kept-pages.ts +3 -3
  186. package/src/server/library.ts +2 -2
  187. package/src/server/mcp.ts +14 -13
  188. package/src/server/notifications.ts +4 -2
  189. package/src/server/offline.ts +24 -7
  190. package/src/server/openapi.ts +12 -3
  191. package/src/server/phases.ts +249 -0
  192. package/src/server/protection.ts +6 -6
  193. package/src/server/proxy-key.ts +8 -8
  194. package/src/server/push-settings.ts +16 -2
  195. package/src/server/push.ts +12 -4
  196. package/src/server/query.ts +17 -31
  197. package/src/server/receive.ts +43 -0
  198. package/src/server/render-document.ts +13 -3
  199. package/src/server/render.ts +1 -1
  200. package/src/server/request-context.ts +137 -0
  201. package/src/server/schema-check.ts +1 -1
  202. package/src/server/scope.ts +20 -13
  203. package/src/server/signals.ts +4 -4
  204. package/src/server/socket.ts +19 -47
  205. package/src/server/stream.ts +1 -1
  206. package/src/server/validate.ts +346 -0
  207. package/src/template-ws.ts +1 -1
  208. package/src/test/a11y-dom.ts +70 -0
  209. package/src/test/html-snapshot.ts +103 -0
  210. package/src/test/index.ts +2 -1
  211. package/src/test/mount.ts +14 -3
  212. package/src/test/page-checks.ts +12 -0
  213. package/src/test/screenshot-match.ts +2 -2
  214. package/src/test/shared-modules.ts +27 -5
  215. package/src/test/site-types.ts +5 -1
  216. package/types/build/class-table.d.ts +1 -1
  217. package/types/build/class-table.d.ts.map +1 -1
  218. package/types/build/client.d.ts +3 -2
  219. package/types/build/client.d.ts.map +1 -1
  220. package/types/build/entry-files.d.ts +4 -0
  221. package/types/build/entry-files.d.ts.map +1 -0
  222. package/types/build/lazy-components.d.ts +6 -0
  223. package/types/build/lazy-components.d.ts.map +1 -0
  224. package/types/build/lazy-manifest.d.ts +19 -0
  225. package/types/build/lazy-manifest.d.ts.map +1 -0
  226. package/types/build/lazy-stubs.d.ts +5 -0
  227. package/types/build/lazy-stubs.d.ts.map +1 -0
  228. package/types/build/plugin.d.ts +1 -0
  229. package/types/build/plugin.d.ts.map +1 -1
  230. package/types/build/transform.d.ts +4 -1
  231. package/types/build/transform.d.ts.map +1 -1
  232. package/types/build/ui-layers.d.ts +5 -0
  233. package/types/build/ui-layers.d.ts.map +1 -0
  234. package/types/build/ui.d.ts +7 -0
  235. package/types/build/ui.d.ts.map +1 -1
  236. package/types/build/variants.d.ts +1 -1
  237. package/types/build/variants.d.ts.map +1 -1
  238. package/types/build/worker-bundle.d.ts +1 -0
  239. package/types/build/worker-bundle.d.ts.map +1 -1
  240. package/types/check/action-options.d.ts +3 -0
  241. package/types/check/action-options.d.ts.map +1 -0
  242. package/types/check/boot.d.ts.map +1 -1
  243. package/types/check/call-result.d.ts +7 -0
  244. package/types/check/call-result.d.ts.map +1 -0
  245. package/types/check/config-objects.d.ts.map +1 -1
  246. package/types/check/diagnose.d.ts.map +1 -1
  247. package/types/check/handler-events.d.ts +9 -0
  248. package/types/check/handler-events.d.ts.map +1 -0
  249. package/types/check/index.d.ts +1 -0
  250. package/types/check/index.d.ts.map +1 -1
  251. package/types/check/link-flag-tests.d.ts +4 -0
  252. package/types/check/link-flag-tests.d.ts.map +1 -0
  253. package/types/check/project.d.ts.map +1 -1
  254. package/types/check/regroup.d.ts +1 -1
  255. package/types/check/regroup.d.ts.map +1 -1
  256. package/types/check/renamed-packages.d.ts +1 -0
  257. package/types/check/renamed-packages.d.ts.map +1 -1
  258. package/types/check/schema-builders.d.ts +3 -0
  259. package/types/check/schema-builders.d.ts.map +1 -0
  260. package/types/check/schema-json.d.ts +3 -0
  261. package/types/check/schema-json.d.ts.map +1 -0
  262. package/types/check/secret-keys.d.ts.map +1 -1
  263. package/types/check/shadow-types.d.ts.map +1 -1
  264. package/types/check/template-hints.d.ts +1 -0
  265. package/types/check/template-hints.d.ts.map +1 -1
  266. package/types/check/tool-labels.d.ts.map +1 -1
  267. package/types/check/ui-types.d.ts.map +1 -1
  268. package/types/check/ui.d.ts.map +1 -1
  269. package/types/commands/env-declare.d.ts.map +1 -1
  270. package/types/compiler/a11y.d.ts.map +1 -1
  271. package/types/compiler/bound-props.d.ts +1 -0
  272. package/types/compiler/bound-props.d.ts.map +1 -1
  273. package/types/compiler/codegen.d.ts +3 -0
  274. package/types/compiler/codegen.d.ts.map +1 -1
  275. package/types/compiler/deprecated-lints-1-105.d.ts +39 -0
  276. package/types/compiler/deprecated-lints-1-105.d.ts.map +1 -0
  277. package/types/compiler/deprecated-lints-1-106.d.ts +12 -0
  278. package/types/compiler/deprecated-lints-1-106.d.ts.map +1 -0
  279. package/types/compiler/deprecated-lints-1-107.d.ts +12 -0
  280. package/types/compiler/deprecated-lints-1-107.d.ts.map +1 -0
  281. package/types/compiler/deprecated-lints.d.ts +41 -22
  282. package/types/compiler/deprecated-lints.d.ts.map +1 -1
  283. package/types/compiler/deprecated-typed-lints.d.ts +21 -0
  284. package/types/compiler/deprecated-typed-lints.d.ts.map +1 -0
  285. package/types/compiler/dom-here.d.ts +14 -0
  286. package/types/compiler/dom-here.d.ts.map +1 -0
  287. package/types/compiler/dom.d.ts +1 -0
  288. package/types/compiler/dom.d.ts.map +1 -1
  289. package/types/compiler/fence-checks.d.ts.map +1 -1
  290. package/types/compiler/field-described.d.ts +1 -1
  291. package/types/compiler/field-described.d.ts.map +1 -1
  292. package/types/compiler/html-fields.d.ts +19 -0
  293. package/types/compiler/html-fields.d.ts.map +1 -0
  294. package/types/compiler/html-initial.d.ts +3 -2
  295. package/types/compiler/html-initial.d.ts.map +1 -1
  296. package/types/compiler/html.d.ts.map +1 -1
  297. package/types/compiler/import-edits.d.ts +1 -0
  298. package/types/compiler/import-edits.d.ts.map +1 -1
  299. package/types/compiler/index.d.ts +3 -0
  300. package/types/compiler/index.d.ts.map +1 -1
  301. package/types/compiler/island-declaration.d.ts +5 -0
  302. package/types/compiler/island-declaration.d.ts.map +1 -1
  303. package/types/compiler/items.d.ts +4 -1
  304. package/types/compiler/items.d.ts.map +1 -1
  305. package/types/compiler/kay-exports.d.ts.map +1 -1
  306. package/types/compiler/lazy-imports.d.ts +33 -0
  307. package/types/compiler/lazy-imports.d.ts.map +1 -0
  308. package/types/compiler/link-flags.d.ts +6 -0
  309. package/types/compiler/link-flags.d.ts.map +1 -1
  310. package/types/compiler/lints.d.ts +61 -34
  311. package/types/compiler/lints.d.ts.map +1 -1
  312. package/types/compiler/rest-omits.d.ts +4 -0
  313. package/types/compiler/rest-omits.d.ts.map +1 -0
  314. package/types/compiler/route-lints.d.ts +8 -0
  315. package/types/compiler/route-lints.d.ts.map +1 -1
  316. package/types/compiler/schema-lints.d.ts +11 -0
  317. package/types/compiler/schema-lints.d.ts.map +1 -0
  318. package/types/compiler/script-server.d.ts.map +1 -1
  319. package/types/config-loader.d.ts.map +1 -1
  320. package/types/config-resolve.d.ts.map +1 -1
  321. package/types/config.d.ts +15 -2
  322. package/types/config.d.ts.map +1 -1
  323. package/types/content/markdown.d.ts.map +1 -1
  324. package/types/migrate/content.d.ts.map +1 -1
  325. package/types/new.d.ts +1 -0
  326. package/types/new.d.ts.map +1 -1
  327. package/types/runtime/action-shared.d.ts +1 -1
  328. package/types/runtime/action-shared.d.ts.map +1 -1
  329. package/types/runtime/action.d.ts +29 -1
  330. package/types/runtime/action.d.ts.map +1 -1
  331. package/types/runtime/client.d.ts.map +1 -1
  332. package/types/runtime/dom-lazy.d.ts +6 -0
  333. package/types/runtime/dom-lazy.d.ts.map +1 -0
  334. package/types/runtime/field-errors.d.ts +4 -0
  335. package/types/runtime/field-errors.d.ts.map +1 -0
  336. package/types/runtime/field-refusal.d.ts +37 -0
  337. package/types/runtime/field-refusal.d.ts.map +1 -0
  338. package/types/runtime/form-types.d.ts +6 -0
  339. package/types/runtime/form-types.d.ts.map +1 -0
  340. package/types/runtime/form.d.ts +4 -2
  341. package/types/runtime/form.d.ts.map +1 -1
  342. package/types/runtime/html.d.ts +3 -0
  343. package/types/runtime/html.d.ts.map +1 -1
  344. package/types/runtime/index.d.ts +2 -0
  345. package/types/runtime/index.d.ts.map +1 -1
  346. package/types/runtime/island.server.d.ts +2 -0
  347. package/types/runtime/island.server.d.ts.map +1 -1
  348. package/types/runtime/markdown-autolink.d.ts +11 -0
  349. package/types/runtime/markdown-autolink.d.ts.map +1 -0
  350. package/types/runtime/markdown-render.client.d.ts +2 -1
  351. package/types/runtime/markdown-render.client.d.ts.map +1 -1
  352. package/types/runtime/markdown.client.d.ts +3 -1
  353. package/types/runtime/markdown.client.d.ts.map +1 -1
  354. package/types/runtime/markdown.d.ts +11 -0
  355. package/types/runtime/markdown.d.ts.map +1 -1
  356. package/types/runtime/markdown.server.d.ts +3 -1
  357. package/types/runtime/markdown.server.d.ts.map +1 -1
  358. package/types/runtime/navigate-api.d.ts +1 -1
  359. package/types/runtime/navigate-api.d.ts.map +1 -1
  360. package/types/runtime/persist.d.ts +2 -2
  361. package/types/runtime/persist.d.ts.map +1 -1
  362. package/types/runtime/reactive.d.ts +3 -0
  363. package/types/runtime/reactive.d.ts.map +1 -1
  364. package/types/runtime/sanitize.d.ts.map +1 -1
  365. package/types/runtime/schema-record.d.ts +9 -0
  366. package/types/runtime/schema-record.d.ts.map +1 -0
  367. package/types/runtime/schema.client.d.ts.map +1 -1
  368. package/types/runtime/schema.d.ts +99 -10
  369. package/types/runtime/schema.d.ts.map +1 -1
  370. package/types/runtime/standard-schema.d.ts +37 -0
  371. package/types/runtime/standard-schema.d.ts.map +1 -0
  372. package/types/runtime/swap-focus.d.ts.map +1 -1
  373. package/types/runtime/validate-run.client.d.ts +4 -0
  374. package/types/runtime/validate-run.client.d.ts.map +1 -0
  375. package/types/runtime/validate.client.d.ts +4 -0
  376. package/types/runtime/validate.client.d.ts.map +1 -0
  377. package/types/runtime/validate.d.ts +1 -21
  378. package/types/runtime/validate.d.ts.map +1 -1
  379. package/types/server/action-module.d.ts +2 -0
  380. package/types/server/action-module.d.ts.map +1 -0
  381. package/types/server/actions.d.ts +1 -0
  382. package/types/server/actions.d.ts.map +1 -1
  383. package/types/server/answer.d.ts +6 -0
  384. package/types/server/answer.d.ts.map +1 -0
  385. package/types/server/api-input.d.ts +19 -0
  386. package/types/server/api-input.d.ts.map +1 -0
  387. package/types/server/api.d.ts.map +1 -1
  388. package/types/server/app-stages.d.ts +7 -3
  389. package/types/server/app-stages.d.ts.map +1 -1
  390. package/types/server/app.d.ts +2 -0
  391. package/types/server/app.d.ts.map +1 -1
  392. package/types/server/client.d.ts +1 -0
  393. package/types/server/client.d.ts.map +1 -1
  394. package/types/server/dev.d.ts +1 -1
  395. package/types/server/dev.d.ts.map +1 -1
  396. package/types/server/env-state.d.ts.map +1 -1
  397. package/types/server/env.d.ts +5 -1
  398. package/types/server/env.d.ts.map +1 -1
  399. package/types/server/favicon.d.ts +3 -1
  400. package/types/server/favicon.d.ts.map +1 -1
  401. package/types/server/file-limit.d.ts.map +1 -1
  402. package/types/server/host-allowlist.d.ts +3 -3
  403. package/types/server/host-allowlist.d.ts.map +1 -1
  404. package/types/server/index.d.ts +2 -0
  405. package/types/server/index.d.ts.map +1 -1
  406. package/types/server/kay-routes.d.ts +47 -0
  407. package/types/server/kay-routes.d.ts.map +1 -0
  408. package/types/server/kay-server.d.ts +25 -0
  409. package/types/server/kay-server.d.ts.map +1 -0
  410. package/types/server/kept-exports.d.ts.map +1 -1
  411. package/types/server/kept-pages.d.ts +1 -1
  412. package/types/server/kept-pages.d.ts.map +1 -1
  413. package/types/server/library.d.ts +2 -1
  414. package/types/server/library.d.ts.map +1 -1
  415. package/types/server/mcp.d.ts +1 -1
  416. package/types/server/mcp.d.ts.map +1 -1
  417. package/types/server/notifications.d.ts +2 -1
  418. package/types/server/notifications.d.ts.map +1 -1
  419. package/types/server/offline.d.ts.map +1 -1
  420. package/types/server/openapi.d.ts.map +1 -1
  421. package/types/server/phases.d.ts +36 -0
  422. package/types/server/phases.d.ts.map +1 -0
  423. package/types/server/protection.d.ts +3 -2
  424. package/types/server/protection.d.ts.map +1 -1
  425. package/types/server/proxy-key.d.ts +3 -3
  426. package/types/server/proxy-key.d.ts.map +1 -1
  427. package/types/server/push-settings.d.ts +1 -1
  428. package/types/server/push-settings.d.ts.map +1 -1
  429. package/types/server/push.d.ts +1 -0
  430. package/types/server/push.d.ts.map +1 -1
  431. package/types/server/query.d.ts.map +1 -1
  432. package/types/server/receive.d.ts +9 -0
  433. package/types/server/receive.d.ts.map +1 -0
  434. package/types/server/render-document.d.ts.map +1 -1
  435. package/types/server/request-context.d.ts +29 -0
  436. package/types/server/request-context.d.ts.map +1 -0
  437. package/types/server/schema-check.d.ts +1 -0
  438. package/types/server/schema-check.d.ts.map +1 -1
  439. package/types/server/scope.d.ts +1 -1
  440. package/types/server/scope.d.ts.map +1 -1
  441. package/types/server/socket.d.ts +3 -5
  442. package/types/server/socket.d.ts.map +1 -1
  443. package/types/server/validate.d.ts +26 -0
  444. package/types/server/validate.d.ts.map +1 -0
  445. package/types/test/a11y-dom.d.ts +20 -0
  446. package/types/test/a11y-dom.d.ts.map +1 -0
  447. package/types/test/html-snapshot.d.ts +4 -0
  448. package/types/test/html-snapshot.d.ts.map +1 -0
  449. package/types/test/index.d.ts.map +1 -1
  450. package/types/test/mount.d.ts +3 -0
  451. package/types/test/mount.d.ts.map +1 -1
  452. package/types/test/page-checks.d.ts +6 -0
  453. package/types/test/page-checks.d.ts.map +1 -0
  454. package/types/test/screenshot-match.d.ts +1 -1
  455. package/types/test/screenshot-match.d.ts.map +1 -1
  456. package/types/test/shared-modules.d.ts.map +1 -1
  457. package/types/test/site-types.d.ts +3 -1
  458. package/types/test/site-types.d.ts.map +1 -1
  459. package/src/check/schema-type.ts +0 -9
  460. package/src/compiler/deprecated-script-fence.ts +0 -26
  461. package/src/server/field-errors.ts +0 -136
  462. package/types/check/schema-type.d.ts +0 -3
  463. package/types/check/schema-type.d.ts.map +0 -1
  464. package/types/compiler/deprecated-script-fence.d.ts +0 -3
  465. package/types/compiler/deprecated-script-fence.d.ts.map +0 -1
  466. package/types/server/field-errors.d.ts +0 -6
  467. package/types/server/field-errors.d.ts.map +0 -1
package/src/server/dev.ts CHANGED
@@ -33,6 +33,7 @@ import { hostName, isLocalHost } from './host-allowlist'
33
33
  import { localeCopies } from './i18n'
34
34
  import { compileErrorPage } from './overlay'
35
35
  import { requestId } from './protection'
36
+ import { bridgeReceive, receiving } from './receive'
36
37
  import { apiRoutePath, endpointPath, ROUTE_METHODS } from './routes'
37
38
 
38
39
  export const DEV_EVENTS = '/_kay/dev/events'
@@ -572,9 +573,9 @@ function broadcast(event: string): void {
572
573
 
573
574
  // Under `kay dev`, a request naming a host other than localhost, an IP address or the site is refused, as a page of another site renaming itself to this machine's address (DNS rebinding) would send it.
574
575
  // `KAY_DEV_HOSTS` lists other names the server answers to, comma-separated, such as a tunnel's; `kay dev --host` adds this machine's own names, as a phone on the network reaches it by.
575
- export function refuseForeignHosts(app: Elysia, site: string | undefined, security?: Record<string, string>): void {
576
+ export function refuseForeignHosts(site: string | undefined, security?: Record<string, string>): ((request: Request) => Response | undefined) | undefined {
576
577
  if (!underKayDev())
577
- return
578
+ return undefined
578
579
 
579
580
  const allowed = new Set((process.env.KAY_DEV_HOSTS ?? '').split(',').map(name => name.trim().toLowerCase()).filter(Boolean))
580
581
  const siteName = site === undefined ? undefined : URL.parse(site)?.hostname
@@ -586,15 +587,12 @@ export function refuseForeignHosts(app: Elysia, site: string | undefined, securi
586
587
  if (listening !== 'localhost')
587
588
  allowed.add(listening.toLowerCase()).add(hostname().toLowerCase()).add(`${hostname().toLowerCase()}.local`)
588
589
 
589
- app.onRequest(({ request }) => {
590
+ return (request) => {
590
591
  const host = request.headers.get('host')
591
592
  const name = host === null ? undefined : hostName(host)
592
593
 
593
- if (host === null || (name !== undefined && (isLocalHost(name) || allowed.has(name))))
594
- return
595
-
596
- return new Response(`kay dev answers to localhost, an IP address or the site, and ${host} is none of them; list it in KAY_DEV_HOSTS to reach the server by that name\n`, { status: 421, headers: { ...security, 'content-type': 'text/plain; charset=utf-8' } })
597
- })
594
+ return host === null || (name !== undefined && (isLocalHost(name) || allowed.has(name))) ? undefined : new Response(`kay dev answers to localhost, an IP address or the site, and ${host} is none of them; list it in KAY_DEV_HOSTS to reach the server by that name\n`, { status: 421, headers: { ...security, 'content-type': 'text/plain; charset=utf-8' } })
595
+ }
598
596
  }
599
597
 
600
598
  const DEV_PREFIX = '/_kay/dev/'
@@ -632,7 +630,7 @@ export function bootFailure(error: unknown, root: string, tools = true): Elysia
632
630
  const app = new Elysia({ name: 'kay/pages', seed: root })
633
631
 
634
632
  rememberBoot(true)
635
- refuseForeignHosts(app, siteFromEnv())
633
+ bridgeReceive(app, receiving([refuseForeignHosts(siteFromEnv())]))
636
634
 
637
635
  if (tools) {
638
636
  refuseRemoteDevTools(app)
@@ -100,7 +100,7 @@ export function building(): boolean {
100
100
  // The module the island entry imports the public variables from, served from the environment the server runs in, so an executable or a prerendered page reads today's values rather than those of the build.
101
101
  export const PUBLIC_ENV_PATH = '/_kay/env.js'
102
102
 
103
- export const PUBLIC_FIELD = /\bs\.Public\s*\(/
103
+ export const PUBLIC_FIELD = /\bs\.[Pp]ublic\s*\(/
104
104
  export const TEST_FILE = /\.(?:test|spec)\.[cm]?[jt]sx?$/
105
105
  // What each file read last held, by its size and time, so a command asking for the files several times reads each one once.
106
106
  const scanned = new Map<string, { stamp: string, declares: boolean }>()
package/src/server/env.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  import type { Elysia } from 'elysia'
2
2
  import type { PublicFieldsOf, WithPublicFields } from '../runtime/env'
3
3
  import type { Schema, Static } from '../runtime/schema'
4
+ import type { ObjectOf, SchemaShape } from '../runtime/schema-record'
4
5
  import type { EnvSchemaRecord } from './env-state'
5
6
  import { readFileSync } from 'node:fs'
6
7
  import process from 'node:process'
7
8
  import { t } from 'elysia'
8
9
  import { KayError } from '../compiler/errors'
9
10
  import { PUBLIC_ENV } from '../runtime/env'
11
+ import { schemaOf } from '../runtime/schema-record'
10
12
  import { bundled } from './bundle'
11
13
  import { fixedReply } from './compress'
12
14
  import { devSecret } from './dev-secret'
@@ -44,6 +46,8 @@ function loadSamples(length: number, pattern: unknown): string[] {
44
46
  // A `s.Object` of the variables, each read on its own.
45
47
  export type EnvSchema = Schema & { properties: Record<string, Schema> }
46
48
 
49
+ type EnvValues<Spec extends EnvSchema> = Static<Spec> & (keyof PublicFieldsOf<Spec> extends never ? unknown : WithPublicFields<Spec>)
50
+
47
51
  // The text of the file `<NAME>_FILE` names, trimmed and read once, as the server boots; a file that cannot be read or holds nothing is an error naming the variable and the path, never the content.
48
52
  function readSecretFile(name: string, file: string, path: string): string {
49
53
  try {
@@ -64,7 +68,11 @@ function readSecretFile(name: string, file: string, path: string): string {
64
68
  // A field marked `s.Public` reaches islands as `publicEnv` from `kay`, with no config key; a name such as `STRIPE_SECRET` or `DATABASE_URL` is refused.
65
69
  // A string field, `s.Public` ones apart, also reads `<NAME>_FILE`, the path of a file holding the value, trimmed and read once, as a compose secret hands it; both set, or a file that cannot be read or is empty, stops the boot naming them, unless the field is `s.Optional`, which is left unset with one warning naming the file.
66
70
  // A field with `requiredInProduction: true` is required in production with its default dropped, and outside production takes its default, stays `undefined` under `s.Optional`, or is `''` when it has neither; `s.Secret()` is one that development and tests give a value derived from the project path; `kay env` lists each as required in production.
67
- export function defineEnv<Spec extends EnvSchema>(declared: Spec, source: Record<string, string | undefined> = process.env): Static<Spec> & (keyof PublicFieldsOf<Spec> extends never ? unknown : WithPublicFields<Spec>) {
71
+ export function defineEnv<Spec extends EnvSchema>(declared: Spec, source?: Record<string, string | undefined>): EnvValues<Spec>
72
+ export function defineEnv<const Shape extends SchemaShape>(declared: Shape, source?: Record<string, string | undefined>): EnvValues<ObjectOf<Shape>>
73
+ export function defineEnv(given: EnvSchema | SchemaShape, source: Record<string, string | undefined> = process.env): unknown {
74
+ const declared = schemaOf(given) as EnvSchema
75
+
68
76
  recordEnv(declared, defineEnv)
69
77
 
70
78
  const production = source.NODE_ENV === 'production'
@@ -114,7 +122,7 @@ export function defineEnv<Spec extends EnvSchema>(declared: Spec, source: Record
114
122
 
115
123
  registerPublic(declared, values)
116
124
 
117
- return values as ReturnType<typeof defineEnv<Spec>>
125
+ return values
118
126
  }
119
127
 
120
128
  if (!building())
@@ -144,7 +152,7 @@ export function defineEnv<Spec extends EnvSchema>(declared: Spec, source: Record
144
152
 
145
153
  registerPublic(declared, env)
146
154
 
147
- return env as ReturnType<typeof defineEnv<Spec>>
155
+ return env
148
156
  }
149
157
 
150
158
  // What `--load` gives a variable the build lacks or refuses: a fixed string of 32 characters or its `minLength`, which a session secret's own check accepts and writes nothing to disk; `undefined` for a field of another type.
@@ -1,5 +1,5 @@
1
1
  import type { Elysia } from 'elysia'
2
- import type { FaviconConfig, ManifestConfig } from '../config'
2
+ import type { FaviconConfig, ManifestConfig, ManifestShareTarget } from '../config'
3
3
  import type { HeadTag } from '../runtime/head'
4
4
  import type { Localized } from '../runtime/localized'
5
5
  import type { ServedPaths } from './served-paths'
@@ -7,6 +7,7 @@ import { mkdir } from 'node:fs/promises'
7
7
  import { join, resolve } from 'node:path'
8
8
  import { SVG_FILE } from '../build/files'
9
9
  import { KayError } from '../compiler/errors'
10
+ import { actionUrl } from '../runtime/action'
10
11
  import { localizedText } from '../runtime/localized'
11
12
  import { kayLog } from '../runtime/log'
12
13
  import { revalidated } from './public'
@@ -316,9 +317,54 @@ export function manifestJson(name: string | undefined, description: string | und
316
317
  theme_color: typeof manifest.themeColor === 'object' ? manifest.themeColor.light : manifest.themeColor,
317
318
  background_color: manifest.backgroundColor,
318
319
  shortcuts: manifest.shortcuts?.map((shortcut, index) => ({ name: shortcut.name, short_name: shortcut.shortName, description: shortcut.description, url: page(shortcut.url, `shortcuts[${index}].url`) })),
320
+ share_target: manifest.shareTarget === undefined ? undefined : shareTargetJson(manifest.shareTarget, page),
319
321
  })
320
322
  }
321
323
 
324
+ // An action written as its page file and export name is served at the URL `actionUrl` gives it; a path is a route of the site.
325
+ const ACTION_REFERENCE = /^(?:\.\/)?([^#\\]+\.kay)#([A-Z_a-z$][\w$]*)$/
326
+
327
+ // What is wrong with a share target naming a page file that is no route or layout of the app, or an export of it that is no action: `actions` holds the URLs the app serves, `files` the page files relative to the root, and `broken` those that failed to compile, with the failure.
328
+ export function shareActionProblem(share: ManifestShareTarget | undefined, actions: ReadonlySet<string>, files: ReadonlySet<string>, broken: ReadonlyMap<string, string> = new Map()): string | undefined {
329
+ const reference = share === undefined || share.action.startsWith('/') ? null : ACTION_REFERENCE.exec(share.action)
330
+
331
+ if (reference === null || actions.has(actionUrl(reference[1]!, reference[2]!)))
332
+ return undefined
333
+
334
+ const [, file, name] = reference
335
+ const start = `kay: the manifest's shareTarget.action, ${share!.action}, `
336
+
337
+ if (broken.has(file!))
338
+ return `${start}names an action of ${file}, which failed to compile: ${broken.get(file!)}`
339
+
340
+ return files.has(file!) ? `${start}names no action: ${file} exports no action called ${name}` : `${start}names a file that is no page or layout of the app`
341
+ }
342
+
343
+ // Stops the boot on a share target naming an action the app does not have.
344
+ export function checkShareAction(share: ManifestShareTarget | undefined, actions: ReadonlySet<string>, files: ReadonlySet<string>, broken?: ReadonlyMap<string, string>): void {
345
+ const problem = shareActionProblem(share, actions, files, broken)
346
+
347
+ if (problem !== undefined)
348
+ throw new KayError('favicon-invalid', problem)
349
+ }
350
+
351
+ function shareTargetJson(share: ManifestShareTarget, page: (path: string, what: string) => string): object {
352
+ const reference = share.action.startsWith('/') ? null : ACTION_REFERENCE.exec(share.action)
353
+
354
+ if (reference === null && !SITE_PATH.test(share.action))
355
+ throw new KayError('favicon-invalid', `kay: the manifest's shareTarget.action is an action as 'src/routes/share.kay#receive' or a path of the site such as '/share', not ${JSON.stringify(share.action)}`)
356
+
357
+ const files = share.files ?? []
358
+ const { title = 'title', text = 'text', url = 'url', files: field = 'files' } = share.params ?? {}
359
+
360
+ return {
361
+ action: page(reference === null ? share.action : actionUrl(reference[1]!, reference[2]!), 'shareTarget.action'),
362
+ method: 'POST',
363
+ enctype: files.length > 0 ? 'multipart/form-data' : 'application/x-www-form-urlencoded',
364
+ params: { title, text, url, ...files.length > 0 && { files: [{ name: field, accept: files }] } },
365
+ }
366
+ }
367
+
322
368
  function sitePath(path: string, what: string): string {
323
369
  if (!SITE_PATH.test(path))
324
370
  throw new KayError('favicon-invalid', `kay: the manifest's ${what} is a path of the site, starting with a single /, not ${path}`)
@@ -1,5 +1,6 @@
1
1
  import type { TSchema } from 'elysia'
2
2
  import { byteSize } from '../runtime/byte-size'
3
+ import { jsonSchemaOf } from '../runtime/standard-schema'
3
4
 
4
5
  const ELYSIA_SIZE = /^(\d+(?:\.\d+)?)([km])$/
5
6
 
@@ -56,7 +57,12 @@ function weigh(node: Node | undefined): number {
56
57
 
57
58
  // The body an action taking sized files needs at least, so a `maxSize` written on the field raises the action's limit when the action sets none; undefined when its files have no size.
58
59
  export function fileBodyLimit(schema: TSchema): number | undefined {
59
- const files = weigh(schema as Node)
60
+ const described = jsonSchemaOf(schema)
61
+
62
+ if (described === undefined)
63
+ return undefined
64
+
65
+ const files = weigh(described)
60
66
 
61
67
  return files > 0 ? Math.ceil(files) + FRAMING : undefined
62
68
  }
@@ -2,8 +2,8 @@ import type { TSchema } from 'elysia'
2
2
  import type { Action, ServerAction } from '../runtime/action'
3
3
  import type { FieldRules, FormRules } from '../runtime/validate'
4
4
  import { isAction } from '../runtime/action'
5
+ import { errorText } from '../runtime/field-errors'
5
6
  import { localeState } from '../runtime/locale-state'
6
- import { errorText } from './field-errors'
7
7
 
8
8
  // The keywords the browser checks a field against, with the `error` message the server shows for it.
9
9
  const CHECKED = new Set(['type', 'format', 'pattern', 'minLength', 'maxLength', 'minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum', 'minItems', 'maxItems', 'const', 'items', 'anyOf', 'error'])
@@ -1,4 +1,4 @@
1
- import type { Elysia } from 'elysia'
1
+ import type { ReceiveStep } from './receive'
2
2
  import { KayError } from '../compiler/errors'
3
3
  import { kayLog } from '../runtime/log'
4
4
  import { nodeNet } from './builtins'
@@ -26,7 +26,7 @@ export function isLocalHost(name: string): boolean {
26
26
  // Names other than a site's own are refused in production when `security.hosts` is set, as a page of another site renaming itself to this server's address (DNS rebinding) would reach it by its own name.
27
27
  // Localhost and IP addresses are always let in, as a health check or a collector on the private network reaches the server by them and a rebinding page cannot make a browser send one.
28
28
  // A `*.example.com` entry answers any one label under the domain, never the bare domain.
29
- export function refuseUnknownHosts(app: Elysia, options: { site: string | undefined, hosts: string[] | true, localeHosts: string[], security: Record<string, string> | undefined, health?: string[] | undefined }): void {
29
+ export function refuseUnknownHosts(options: { site: string | undefined, hosts: string[] | true, localeHosts: string[], security: Record<string, string> | undefined, health?: string[] | undefined }): ReceiveStep {
30
30
  const listed = [...options.hosts === true ? [] : options.hosts, ...options.localeHosts]
31
31
  const wildcards = listed.filter(name => name.startsWith('*.')).map(name => name.slice(1).toLowerCase())
32
32
  const allowed = new Set(listed.filter(name => !name.startsWith('*.')).map(name => hostName(name) ?? name.toLowerCase()))
@@ -38,20 +38,20 @@ export function refuseUnknownHosts(app: Elysia, options: { site: string | undefi
38
38
  if (allowed.size === 0 && wildcards.length === 0)
39
39
  throw new KayError('config-invalid', 'kay: security.hosts answers to no host name, so every named host would get a 421: set `site` in kay.config.ts (or SITE_URL), or list the names in `security.hosts`')
40
40
 
41
- app.onRequest(({ request }) => {
41
+ return (request) => {
42
42
  const host = request.headers.get('host')
43
43
 
44
44
  if (host === null)
45
- return
45
+ return undefined
46
46
 
47
47
  const name = hostName(host)
48
48
 
49
49
  if (name !== undefined && (allowed.has(name) || isLocalHost(name) || wildcards.some(suffix => name.endsWith(suffix) && !name.slice(0, -suffix.length).includes('.') && name.length > suffix.length)))
50
- return
50
+ return undefined
51
51
 
52
52
  // The container's own probe reaches the server by its service name.
53
53
  if (options.health?.includes(requestPath(request)))
54
- return
54
+ return undefined
55
55
 
56
56
  if (!warned.has(host) && warned.size < 100) {
57
57
  warned.add(host)
@@ -59,5 +59,5 @@ export function refuseUnknownHosts(app: Elysia, options: { site: string | undefi
59
59
  }
60
60
 
61
61
  return new Response(`Misdirected request: this host is not one the site answers to\n${FIX}\n`, { status: 421, headers: { ...options.security, 'content-type': 'text/plain; charset=utf-8' } })
62
- })
62
+ }
63
63
  }
@@ -144,7 +144,7 @@ function locations(view: DataView, iloc: Box, wanted: Set<number>): Map<number,
144
144
  const value = unsigned(view, at, size)
145
145
 
146
146
  if (!Number.isSafeInteger(value))
147
- throw new Error('a size it cannot read')
147
+ throw new TypeError('a size it cannot read')
148
148
 
149
149
  at += size
150
150
 
@@ -89,6 +89,8 @@ export type { FileHooks, FileStore, PutOptions, StoredFile, UrlOptions } from '.
89
89
  export { diskFiles, files, memoryFiles, s3Files } from './storage'
90
90
  export type { RenderToStringOptions } from './string'
91
91
  export { renderLocale, renderToString } from './string'
92
+ export type { Issue, Refusal, Source, Validated, ValidateOptions } from './validate'
93
+ export { validate } from './validate'
92
94
  export { CspViolationError } from './violations'
93
95
  export type { BunWebSocketConstructor } from './web-socket'
94
96
  export { WebSocket } from './web-socket'
@@ -0,0 +1,221 @@
1
+ import type { TrailingSlash } from '../config'
2
+ import type { RequestContext } from './request-context'
3
+ import { decodePath } from './routes'
4
+
5
+ export type RouteKind = 'page' | 'action' | 'endpoint' | 'api' | 'socket' | 'file' | 'kay' | 'mounted'
6
+
7
+ // What a route reads of the request, each part a schema the `input` phase validates against.
8
+ export interface RouteInput {
9
+ params?: unknown
10
+ query?: unknown
11
+ body?: unknown
12
+ headers?: unknown
13
+ }
14
+
15
+ // One route kay serves, whatever it is: the table holds nothing else. `path` is Bun's syntax (`/blog/:slug`, `/docs/*`) as the router sees it, before the mount point and the slash the app asks for.
16
+ export interface RouteRecord {
17
+ method: string
18
+ path: string
19
+ kind: RouteKind
20
+ input?: RouteInput | undefined
21
+ output?: unknown
22
+ // Settings read by the phases (`maxSize`, `timeout`, `rateLimit`, `cors`...) and by plugins; resolved nearest first, which the slices moving routes onto the table fill in.
23
+ options: Record<string, unknown>
24
+ // The app, a library's name, or `kay`.
25
+ owner: string
26
+ // The locale a prefixed or translated path serves in.
27
+ locale?: string | undefined
28
+ handle?: ((context: RequestContext) => unknown) | undefined
29
+ // A Response known at boot, which `Bun.serve` answers with no JS while no phase has anything to apply.
30
+ static?: Response | undefined
31
+ }
32
+
33
+ export interface RouteEnv {
34
+ base: string
35
+ trailingSlash: TrailingSlash
36
+ }
37
+
38
+ // A record under one spelling of its path.
39
+ export interface Compiled {
40
+ record: RouteRecord
41
+ // The path as registered, the mount point and the slash included.
42
+ path: string
43
+ segments: string[]
44
+ // Set for the other spelling of a page's path, which answers with a redirect to the served one.
45
+ redirect: boolean
46
+ }
47
+
48
+ type Params = Record<string, string>
49
+
50
+ const EDGE_SLASHES = /^\/+/
51
+
52
+ // Every spelling a record is served under: a proxy may strip the mount point or not, so both are served; a page follows `trailingSlash`, the other spelling redirecting to it, and every other kind is served as written.
53
+ export function compileRecord(record: RouteRecord, env: RouteEnv): Compiled[] {
54
+ const bases = env.base === '' ? [''] : ['', env.base]
55
+ const catchAll = record.path.endsWith('/*')
56
+ const policy = record.kind === 'page' && !catchAll ? env.trailingSlash : 'exact'
57
+ const compiled: Compiled[] = []
58
+
59
+ for (const base of bases) {
60
+ const joined = record.path === '/' ? base : `${base}${record.path}`
61
+ const bare = joined === '' ? '/' : joined
62
+ const slashed = joined === '' ? '/' : `${joined}/`
63
+ const spelling = (path: string, redirect: boolean) => compiled.push({ record, path, segments: path.split('/'), redirect })
64
+
65
+ if (policy === 'exact' || (bare === '/' && slashed === '/')) {
66
+ spelling(bare, false)
67
+
68
+ continue
69
+ }
70
+
71
+ if (policy === 'ignore') {
72
+ spelling(bare, false)
73
+ spelling(slashed, false)
74
+
75
+ continue
76
+ }
77
+
78
+ spelling(policy === 'always' ? slashed : bare, false)
79
+ spelling(policy === 'always' ? bare : slashed, true)
80
+ }
81
+
82
+ return compiled
83
+ }
84
+
85
+ // Whether a path has params, which only a request for it needs read from the path.
86
+ export function isDynamic(path: string): boolean {
87
+ return path.includes(':') || path.includes('*')
88
+ }
89
+
90
+ // The params a path gives the route, each decoded once as the path writes it: `%2F` is a slash in the value and never a segment. `null` when the path is not the route's, undefined when a value holds an escape that does not decode.
91
+ export function extractParams(segments: readonly string[], pathname: string): Params | null | undefined {
92
+ const given = pathname.split('/')
93
+ const params: Params = {}
94
+
95
+ for (const [index, segment] of segments.entries()) {
96
+ if (segment === '*') {
97
+ // A catch-all's own directory (`/docs` for `/docs/*`) is not one of its paths.
98
+ if (given.length <= index)
99
+ return null
100
+
101
+ const value = decodePath(given.slice(index).join('/'))
102
+
103
+ if (value === undefined)
104
+ return undefined
105
+
106
+ params['*'] = value
107
+
108
+ return params
109
+ }
110
+
111
+ const value = given[index]
112
+
113
+ if (value === undefined)
114
+ return null
115
+
116
+ if (segment.startsWith(':')) {
117
+ if (value === '')
118
+ return null
119
+
120
+ const decoded = decodePath(value)
121
+
122
+ if (decoded === undefined)
123
+ return undefined
124
+
125
+ params[segment.slice(1)] = decoded
126
+
127
+ continue
128
+ }
129
+
130
+ if (value !== segment)
131
+ return null
132
+ }
133
+
134
+ return segments.length === given.length ? params : null
135
+ }
136
+
137
+ // How early a spelling is tried among the ones with params: a static segment before a param, a param before a catch-all.
138
+ function rank(segment: string | undefined): number {
139
+ if (segment === undefined)
140
+ return 3
141
+
142
+ if (segment === '*')
143
+ return 2
144
+
145
+ return segment.startsWith(':') ? 1 : 0
146
+ }
147
+
148
+ function bySpecificity(a: Compiled, b: Compiled): number {
149
+ for (let index = 0; index < Math.max(a.segments.length, b.segments.length); index++) {
150
+ const difference = rank(a.segments[index]) - rank(b.segments[index])
151
+
152
+ if (difference !== 0)
153
+ return difference
154
+ }
155
+
156
+ return 0
157
+ }
158
+
159
+ type Methods = Map<string, Compiled>
160
+
161
+ export interface Match {
162
+ compiled: Compiled
163
+ // Undefined for a path whose param held an escape that does not decode.
164
+ params: Params | undefined
165
+ }
166
+
167
+ // The records by method and path, for a request `Bun.serve`'s router did not match for kay: a rewritten one, or one that came through `fetch` alone.
168
+ export class RouteIndex {
169
+ readonly #exact = new Map<string, Methods>()
170
+ readonly #dynamic = new Map<string, Methods>()
171
+
172
+ constructor(compiled: readonly Compiled[]) {
173
+ // Sorted so the dynamic paths are tried most specific first, a Map keeping their order.
174
+ for (const entry of [...compiled].sort(bySpecificity)) {
175
+ const table = isDynamic(entry.path) ? this.#dynamic : this.#exact
176
+ const methods = table.get(entry.path) ?? new Map<string, Compiled>()
177
+
178
+ table.set(entry.path, methods)
179
+
180
+ // The first record of a method keeps it, as the router tries them in order.
181
+ if (!methods.has(entry.record.method))
182
+ methods.set(entry.record.method, entry)
183
+ }
184
+ }
185
+
186
+ match(method: string, pathname: string): Match | undefined {
187
+ const exact = this.#exact.get(pathname)
188
+
189
+ if (exact !== undefined) {
190
+ const compiled = pick(exact, method)
191
+
192
+ return compiled && { compiled, params: {} }
193
+ }
194
+
195
+ for (const [path, methods] of this.#dynamic) {
196
+ const params = extractParams(path.split('/'), pathname)
197
+
198
+ if (params === null)
199
+ continue
200
+
201
+ const compiled = pick(methods, method)
202
+
203
+ if (compiled !== undefined)
204
+ return { compiled, params }
205
+ }
206
+
207
+ return undefined
208
+ }
209
+ }
210
+
211
+ // The record for a method: its own, else `ALL`, else a GET one for a HEAD.
212
+ function pick(methods: Methods, method: string): Compiled | undefined {
213
+ return methods.get(method) ?? methods.get('ALL') ?? (method === 'HEAD' ? methods.get('GET') : undefined)
214
+ }
215
+
216
+ // The path a redirect from the other slash spelling goes to, from the one it was asked at: one slash more or less, and no leading `//`, which a browser reads as another host.
217
+ export function slashed(pathname: string, search: string): string {
218
+ const path = `/${pathname.replace(EDGE_SLASHES, '')}`
219
+
220
+ return `${path.endsWith('/') ? path.slice(0, -1) : `${path}/`}${search}`
221
+ }