@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
@@ -0,0 +1,249 @@
1
+ import type { LocaleState } from '../runtime/locale-state'
2
+ import type { RouteRecord } from './kay-routes'
3
+ import type { Received, ReceiveServer, ReceiveStep } from './receive'
4
+ import { runInLocale } from '../runtime/locale-state'
5
+ import { kayLog } from '../runtime/log'
6
+ import { answer } from './answer'
7
+ import { answeringRequest } from './log'
8
+ import { runReceive } from './receive'
9
+ import { inContext, RequestContext } from './request-context'
10
+ import { Denied, NotFound, Redirect } from './signals'
11
+ import { SERVER_SPAN, traced } from './trace'
12
+
13
+ type Outcome = Response | undefined | void
14
+
15
+ // A step of `limit`, `admit` or `input`: it answers with a Response to end the request there, or returns nothing.
16
+ export type Step = (context: RequestContext) => Outcome | Promise<Outcome>
17
+
18
+ // A step of `respond`, which takes the Response and gives the one that goes on; the order is the order of the list.
19
+ export type RespondStep = (context: RequestContext, response: Response) => Response | Promise<Response>
20
+
21
+ // A step of `after`, run once the answer is made.
22
+ export type AfterStep = (context: RequestContext, response: Response) => void | Promise<void>
23
+
24
+ // A step of `error`, which answers a failure with a Response or leaves it to the next, kay's own last.
25
+ export type ErrorStep = (context: RequestContext, error: unknown) => Outcome | Promise<Outcome>
26
+
27
+ // What runs in each phase, in this order for every kind of route: `receive`, `limit`, `admit`, `input`, `handle`, `answer`, `respond`, `after`, with `error` taking over a failure of any of them. Each list holds what extensions and kay add, in the order they are added.
28
+ export interface Phases {
29
+ receive: ReceiveStep[]
30
+ limit: Step[]
31
+ admit: Step[]
32
+ input: Step[]
33
+ respond: RespondStep[]
34
+ after: AfterStep[]
35
+ error: ErrorStep[]
36
+ }
37
+
38
+ export function emptyPhases(): Phases {
39
+ return { receive: [], limit: [], admit: [], input: [], respond: [], after: [], error: [] }
40
+ }
41
+
42
+ export interface PipelineOptions {
43
+ // Whether an unexpected failure's message goes in the 500 answer, as `kay dev` shows it.
44
+ dev?: boolean | undefined
45
+ // The locale a request is answered in, so the handler, the log lines and the stream it opens read it.
46
+ locale?: ((context: RequestContext) => LocaleState | undefined) | undefined
47
+ // Where an unexpected failure is reported, the route's own bug.
48
+ report?: ((error: unknown, request: Request) => void) | undefined
49
+ }
50
+
51
+ function text(status: number, body: string): Response {
52
+ return new Response(body, { status, headers: { 'content-type': 'text/plain; charset=utf-8' } })
53
+ }
54
+
55
+ // What a handler wrote to `set`, folded into the answer: its headers go on where the answer has none of that name, a cookie added to the others.
56
+ function folded(response: Response, context: RequestContext): Response {
57
+ const written = context.written
58
+
59
+ if (written === undefined || [...written.headers].length === 0)
60
+ return response
61
+
62
+ const headers = new Headers(response.headers)
63
+
64
+ for (const [name, value] of written.headers) {
65
+ if (name === 'set-cookie')
66
+ headers.append(name, value)
67
+ else if (!headers.has(name))
68
+ headers.set(name, value)
69
+ }
70
+
71
+ return new Response(response.body, { status: response.status, statusText: response.statusText, headers })
72
+ }
73
+
74
+ // A body that goes unread under `HEAD`, the headers as the GET would send them.
75
+ function headOf(response: Response): Response {
76
+ return new Response(null, { status: response.status, statusText: response.statusText, headers: response.headers })
77
+ }
78
+
79
+ // The phases around a request, the same for every route kind, so a record kay serves and the Elysia app behind it as a fallback are answered the same way.
80
+ export class Pipeline {
81
+ readonly phases: Phases
82
+ readonly #options: PipelineOptions
83
+ // The requests `receive` ran for, so it runs once however many times a request is handed on, as it is to the fallback app which still has its own bridge to it.
84
+ readonly #received = new WeakSet<Request>()
85
+
86
+ constructor(phases: Phases = emptyPhases(), options: PipelineOptions = {}) {
87
+ this.phases = phases
88
+ this.#options = options
89
+ }
90
+
91
+ // `receive`: the request to go on with, or the answer. A request already received is returned as it is.
92
+ receive(request: Request, server: ReceiveServer): Received {
93
+ if (this.#received.has(request))
94
+ return request
95
+
96
+ this.#received.add(request)
97
+
98
+ const received = runReceive(this.phases.receive, request, server)
99
+
100
+ if (received instanceof Request)
101
+ this.#received.add(received)
102
+
103
+ return received
104
+ }
105
+
106
+ // Whether a phase has a step: with none, a static Response can go to `Bun.serve` as it is, as nothing is left to apply to it.
107
+ get empty(): boolean {
108
+ const { phases } = this
109
+
110
+ return phases.receive.length === 0 && phases.respond.length === 0 && phases.after.length === 0 && phases.limit.length === 0 && phases.admit.length === 0
111
+ }
112
+
113
+ // Answers a request for a record, from `limit` to `after`.
114
+ async serve(record: RouteRecord, params: Record<string, string> | undefined, request: Request): Promise<Response> {
115
+ const context = new RequestContext(request, record, params)
116
+
117
+ return this.#inScope(context, async () => {
118
+ let response: Response
119
+
120
+ try {
121
+ response = await this.#handle(context, record)
122
+ }
123
+ catch (error) {
124
+ response = await this.#fail(context, error)
125
+ }
126
+
127
+ return this.#finish(context, response)
128
+ })
129
+ }
130
+
131
+ // Runs `respond` and `after` over an answer made before any record was reached: a refusal of `receive`, or the fallback app's.
132
+ async settle(request: Request, response: Response): Promise<Response> {
133
+ // The fallback app's answer, as it is, when no step is left to run over it: Bun leaves a HEAD answer's body out itself, with the length the body gave. An upgraded socket has no answer to run them over.
134
+ if ((this.phases.respond.length === 0 && this.phases.after.length === 0) || response === undefined)
135
+ return response
136
+
137
+ const context = new RequestContext(request, undefined, {})
138
+
139
+ return this.#inScope(context, async () => this.#finish(context, response))
140
+ }
141
+
142
+ // Answers a failure that came before any record was reached, through the same chain as a handler's.
143
+ async failed(request: Request, error: unknown): Promise<Response> {
144
+ const context = new RequestContext(request, undefined, {})
145
+
146
+ return this.#inScope(context, async () => this.#finish(context, await this.#fail(context, error)))
147
+ }
148
+
149
+ async #inScope<T>(context: RequestContext, run: () => Promise<T>): Promise<T> {
150
+ const { request } = context
151
+
152
+ return inContext(context, async () => answeringRequest(request, async () => {
153
+ const inside = async () => traced(() => `${request.method} ${context.route?.path ?? context.url.pathname}`, () => ({ kind: SERVER_SPAN }), run, request)
154
+ const state = this.#options.locale?.(context)
155
+
156
+ return state === undefined ? inside() : runInLocale(state, inside)
157
+ }))
158
+ }
159
+
160
+ async #handle(context: RequestContext, record: RouteRecord): Promise<Response> {
161
+ const refused = await this.#run(this.phases.limit, context) ?? await this.#run(this.phases.admit, context) ?? this.#malformed(context) ?? await this.#run(this.phases.input, context)
162
+
163
+ return refused !== undefined ? refused : answer(await record.handle!(context), context)
164
+ }
165
+
166
+ // A param that does not decode refuses the path as a client's mistake, in `input` after the guards, which read nothing of it.
167
+ #malformed(context: RequestContext): Response | undefined {
168
+ return context.malformed ? text(400, 'Bad Request: the path holds an escape that does not decode\n') : undefined
169
+ }
170
+
171
+ async #run(steps: readonly Step[], context: RequestContext): Promise<Response | undefined> {
172
+ for (const step of steps) {
173
+ const result = await step(context)
174
+
175
+ if (result instanceof Response)
176
+ return result
177
+ }
178
+
179
+ return undefined
180
+ }
181
+
182
+ // One chain: the steps of `error`, then kay's own kinds of failure, so what an app or an extension answers wins over the default and a message never leaks unless `dev` says so.
183
+ async #fail(context: RequestContext, error: unknown): Promise<Response> {
184
+ try {
185
+ for (const step of this.phases.error) {
186
+ const result = await step(context, error)
187
+
188
+ if (result instanceof Response)
189
+ return result
190
+ }
191
+ }
192
+ catch (thrown) {
193
+ error = thrown
194
+ }
195
+
196
+ if (error instanceof Redirect)
197
+ return new Response(null, { status: error.status, headers: { location: error.location } })
198
+
199
+ if (error instanceof NotFound)
200
+ return text(404, 'Not Found\n')
201
+
202
+ if (error instanceof Denied)
203
+ return text(error.status, `${error.message}\n`)
204
+
205
+ this.#report(error, context.request)
206
+
207
+ return text(500, this.#options.dev === true && error instanceof Error ? `${error.message}\n` : 'Internal Server Error\n')
208
+ }
209
+
210
+ #report(error: unknown, request: Request): void {
211
+ if (this.#options.report === undefined)
212
+ kayLog('error', `${request.method} ${new URL(request.url).pathname} failed`, error)
213
+ else
214
+ this.#options.report(error, request)
215
+ }
216
+
217
+ // `respond` in its order, then the `HEAD` body dropped, then `after` on a later turn, once the answer has gone.
218
+ async #finish(context: RequestContext, answered: Response): Promise<Response> {
219
+ let response = folded(answered, context)
220
+
221
+ try {
222
+ for (const step of this.phases.respond)
223
+ response = await step(context, response)
224
+ }
225
+ catch (error) {
226
+ response = text(500, 'Internal Server Error\n')
227
+ this.#report(error, context.request)
228
+ }
229
+
230
+ if (context.request.method === 'HEAD' && response.body !== null)
231
+ response = headOf(response)
232
+
233
+ if (this.phases.after.length > 0)
234
+ setImmediate(() => void this.#after(context, response))
235
+
236
+ return response
237
+ }
238
+
239
+ async #after(context: RequestContext, response: Response): Promise<void> {
240
+ for (const step of this.phases.after) {
241
+ try {
242
+ await step(context, response)
243
+ }
244
+ catch (error) {
245
+ this.#report(error, context.request)
246
+ }
247
+ }
248
+ }
249
+ }
@@ -1,9 +1,10 @@
1
1
  import type { Server } from 'bun'
2
- import type { Elysia, TSchema } from 'elysia'
2
+ import type { TSchema } from 'elysia'
3
3
  import type { BlockList } from 'node:net'
4
4
  import type { ProxyConfig } from '../config'
5
5
  import type { ActionRateLimit, RateKey, RateKeyContext, RateLimit, RateStore, RateWindow, ServerAction } from '../runtime/action'
6
6
  import type { Duration } from '../runtime/duration'
7
+ import type { ReceiveStep } from './receive'
7
8
  import type { RequestRecord } from './request-record'
8
9
  import { linkSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
9
10
  import process from 'node:process'
@@ -903,11 +904,10 @@ export function checkProxy(setting: ProxyConfig | number | boolean | undefined):
903
904
 
904
905
  // Records the app's proxy settings on each request it gets, for `clientAddress` to read them wherever it is called from.
905
906
  // In production a `proxy` left out is one proxy in front, read from private addresses only: a server there is behind one far more often than it faces visitors, and `false` says it is not.
906
- export function trustProxy(app: Elysia, setting: ProxyConfig | number | boolean | undefined): void {
907
+ export function trustProxy(setting: ProxyConfig | number | boolean | undefined): ReceiveStep {
907
908
  const proxy = checkProxy(setting) ?? (setting === undefined && process.env.NODE_ENV === 'production' ? INFERRED_PROXY : undefined)
908
- const off = setting === false
909
909
 
910
- app.onRequest(({ request, server }) => {
910
+ return (request, server) => {
911
911
  const record = requestRecord(request)
912
912
 
913
913
  record.proxy = proxy
@@ -915,8 +915,8 @@ export function trustProxy(app: Elysia, setting: ProxyConfig | number | boolean
915
915
  if (server)
916
916
  record.server = server
917
917
 
918
- record.proxyOff = off ? true : undefined
919
- })
918
+ record.proxyOff = setting === false ? true : undefined
919
+ }
920
920
  }
921
921
 
922
922
  // The socket's address, unless it is a proxy on this machine or network the app's `proxy` setting trusts: then the client's address as that proxy passed it on.
@@ -1,5 +1,5 @@
1
- import type { Elysia } from 'elysia'
2
1
  import type { ProxyConfig } from '../config'
2
+ import type { ReceiveStep } from './receive'
3
3
  import type { Secret } from './secret'
4
4
  import process from 'node:process'
5
5
  import { KayError } from '../compiler/errors'
@@ -34,11 +34,11 @@ function isOpen(path: string, open: readonly string[]): boolean {
34
34
  // Refuses with 403 every request that does not carry the proxy's shared key in the header `proxy.key` names, so a process or a container reaching the port directly skips nothing the proxy does.
35
35
  // The `endpoint` is the MCP endpoint while `mcp.token` guards it: a POST to it carries its own credential, the bearer, and so skips the check.
36
36
  // Off under `kay dev`, in a static export, and outside production while no key is given (a warning says so, except in a test run); a production server with `key` and no key to compare with does not boot.
37
- export function requireProxyKey(app: Elysia, proxy: ProxyConfig | undefined, options: { root: string, dev: boolean, exported: boolean, base: string, health: string | undefined, metrics?: (() => readonly string[]) | undefined, endpoint?: (() => readonly string[]) | undefined, logRefused?: ((request: Request, status: number) => void) | undefined, security?: Record<string, string> | undefined }): void {
37
+ export function requireProxyKey(proxy: ProxyConfig | undefined, options: { root: string, dev: boolean, exported: boolean, base: string, health: string | undefined, metrics?: (() => readonly string[]) | undefined, endpoint?: (() => readonly string[]) | undefined, logRefused?: ((request: Request, status: number) => void) | undefined, security?: Record<string, string> | undefined }): ReceiveStep | undefined {
38
38
  const header = proxy?.key?.header
39
39
 
40
40
  if (header === undefined || options.dev || options.exported)
41
- return
41
+ return undefined
42
42
 
43
43
  const secret = proxyKeyValue(proxy?.key?.secret, options.root)
44
44
 
@@ -50,7 +50,7 @@ export function requireProxyKey(app: Elysia, proxy: ProxyConfig | undefined, opt
50
50
  if (process.env.NODE_ENV !== 'test')
51
51
  kayLog('warn', `proxy.key is set but ${source} gives none, so requests are not checked for the ${header} header; a production server would not boot`)
52
52
 
53
- return
53
+ return undefined
54
54
  }
55
55
 
56
56
  throw new KayError('config-invalid', `kay: proxy.key names the header ${header}, which carries a key read from ${source}, and it gives no non-empty one`)
@@ -58,22 +58,22 @@ export function requireProxyKey(app: Elysia, proxy: ProxyConfig | undefined, opt
58
58
 
59
59
  const open = [...(proxy?.open ?? []).map(entry => entry.length > 1 && entry.endsWith('/') ? entry.slice(0, -1) : entry), ...options.health === undefined ? [] : [options.health]]
60
60
 
61
- app.onRequest(({ request }) => {
61
+ return (request) => {
62
62
  const path = routerPath(pathOf(request.url), options.base)
63
63
  const metrics = options.metrics?.() ?? []
64
64
 
65
65
  // The metrics route answers by its own rule, so a collector on the private network needs no key.
66
66
  if (isInternal(request) || isOpen(path, open) || metrics.some(at => path === at || path === `${at}/`) || (request.method === 'POST' && options.endpoint?.().some(at => path === at || path === `${at}/`) === true))
67
- return
67
+ return undefined
68
68
 
69
69
  const given = request.headers.get(header)
70
70
 
71
71
  if (given !== null && sameText(given, secret))
72
- return
72
+ return undefined
73
73
 
74
74
  // This hook runs before the request id and request log hooks, which a response made here skips.
75
75
  options.logRefused?.(request, 403)
76
76
 
77
77
  return new Response('Forbidden\n', { status: 403, headers: { ...options.security, 'content-type': 'text/plain; charset=utf-8', [requestIdHeader()]: requestId(request) } })
78
- })
78
+ }
79
79
  }
@@ -37,10 +37,12 @@ export function pushLoose(): boolean {
37
37
  return underKayDev() || looseForTest
38
38
  }
39
39
 
40
+ const MOST_BADGE = 999_999
40
41
  const pending = new Set<Promise<void>>()
41
42
 
42
43
  // Pushes what `notify()` stored to the person's devices without making `notify` wait for the push services, and never fails it.
43
- export function pushAlongside(user: string, message: { title: string, url?: string | undefined }): void {
44
+ // `badge` reads the unread count the app badge shows, once a device is known to be listening.
45
+ export function pushAlongside(user: string, message: { title: string, url?: string | undefined }, badge?: () => Promise<number> | number): void {
44
46
  if (current === undefined)
45
47
  return
46
48
 
@@ -49,7 +51,7 @@ export function pushAlongside(user: string, message: { title: string, url?: stri
49
51
  const { sendPush } = await import('./push')
50
52
 
51
53
  if ((await pushStore().list(user)).length > 0)
52
- await sendPush(user, message)
54
+ await sendPush(user, badge === undefined ? message : { ...message, badge: await countOf(badge) })
53
55
  }
54
56
  catch (error) {
55
57
  kayLog('warn', `notify() could not push a notification to ${user}`, error)
@@ -61,6 +63,18 @@ export function pushAlongside(user: string, message: { title: string, url?: stri
61
63
  void job.finally(() => pending.delete(job))
62
64
  }
63
65
 
66
+ // The badge is a courtesy: a count that cannot be read leaves it out, and one past what a push carries is cut to it.
67
+ async function countOf(badge: () => Promise<number> | number): Promise<number | undefined> {
68
+ try {
69
+ return Math.min(await badge(), MOST_BADGE)
70
+ }
71
+ catch (error) {
72
+ kayLog('warn', 'notify() could not read the unread count for the app badge', error)
73
+
74
+ return undefined
75
+ }
76
+ }
77
+
64
78
  // Resolves once every push `notify()` started has been answered, for a test reading what a push service got.
65
79
  export async function pushesSettled(): Promise<void> {
66
80
  await Promise.all(pending)
@@ -42,6 +42,8 @@ export interface PushMessage {
42
42
  tag?: string | undefined
43
43
  // A path of the site or an http(s) URL of an image.
44
44
  icon?: string | undefined
45
+ // The unread count the installed app's badge shows, set by the worker (`navigator.setAppBadge`) when the push arrives, `0` clearing it; a whole number from 0 to 999999. `notify()` sends the person's unread count.
46
+ badge?: number | undefined
45
47
  }
46
48
 
47
49
  export interface PushSendOptions {
@@ -61,6 +63,7 @@ export interface PushResult {
61
63
 
62
64
  const MAX_BODY = 500
63
65
  const MAX_TAG = 64
66
+ const MAX_BADGE = 999_999
64
67
 
65
68
  export const MOST_DEVICES = 10
66
69
  const MAX_ENDPOINT = 2000
@@ -73,9 +76,9 @@ const DEFAULT_TTL = '1d'
73
76
  const URGENCIES = new Set(['very-low', 'low', 'normal', 'high'])
74
77
  const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]'])
75
78
 
76
- const PUSH_ENV = s.Object({
77
- VAPID_PUBLIC_KEY: s.String({ requiredInProduction: true, description: 'the VAPID public key push services check a push against, a base64url P-256 point; `kay push keys` prints a pair' }),
78
- VAPID_PRIVATE_KEY: s.Secret({ minLength: 43, description: 'the VAPID private key signing pushes, base64url; `kay push keys` prints a pair' }),
79
+ const PUSH_ENV = s.object({
80
+ VAPID_PUBLIC_KEY: s.string({ requiredInProduction: true, description: 'the VAPID public key push services check a push against, a base64url P-256 point; `kay push keys` prints a pair' }),
81
+ VAPID_PRIVATE_KEY: s.secret({ min: 43, description: 'the VAPID private key signing pushes, base64url; `kay push keys` prints a pair' }),
79
82
  })
80
83
 
81
84
  // Lets the site push to the devices of the people who opted in: `sendPush` sends, an island's `webPush.subscribe()` opts in, `notify()` pushes beside the inbox, and kay's service worker shows the notification and opens its link on a click.
@@ -258,7 +261,12 @@ function payloadOf(message: PushMessage): Uint8Array<ArrayBuffer> {
258
261
  const tag = text(message.tag, MAX_TAG, 'tag')
259
262
  const url = linked(message.url, 'url')
260
263
  const icon = linked(message.icon, 'icon')
261
- const bytes = new TextEncoder().encode(JSON.stringify({ title, ...body === undefined ? {} : { body }, ...url === undefined ? {} : { url }, ...tag === undefined ? {} : { tag }, ...icon === undefined ? {} : { icon } }))
264
+ const { badge } = message
265
+
266
+ if (badge !== undefined && (!Number.isSafeInteger(badge) || badge < 0 || badge > MAX_BADGE))
267
+ throw new KayError('argument-invalid', `kay: a push's badge is a whole number from 0 to ${MAX_BADGE}; got ${JSON.stringify(badge)}`)
268
+
269
+ const bytes = new TextEncoder().encode(JSON.stringify({ title, ...body === undefined ? {} : { body }, ...url === undefined ? {} : { url }, ...tag === undefined ? {} : { tag }, ...icon === undefined ? {} : { icon }, ...badge === undefined ? {} : { badge } }))
262
270
 
263
271
  if (bytes.length > MAX_PAYLOAD_BYTES)
264
272
  throw new KayError('argument-invalid', `kay: a push payload is at most ${MAX_PAYLOAD_BYTES} bytes; got ${bytes.length}`)
@@ -3,11 +3,9 @@ import type { ParamRule, QueryRules } from '../runtime/query-param'
3
3
  import { relative } from 'node:path'
4
4
  import process from 'node:process'
5
5
  import { ARRIVAL_PARAM } from '../runtime/arrival'
6
- import { formInput } from '../runtime/form'
7
- import { errorText } from './field-errors'
8
6
  import { pageSetting } from './page-settings'
9
- import { compiledCheck, fieldOf, refusals, summaryOf } from './schema-check'
10
7
  import { Denied } from './signals'
8
+ import { isRefusal, messagesOf, validateNow } from './validate'
11
9
 
12
10
  export type ReadFields = { value: Record<string, unknown> } | { refused: Map<string, string> }
13
11
 
@@ -97,13 +95,20 @@ function layoutDeclares(modules: object[]): boolean {
97
95
  return false
98
96
  }
99
97
 
98
+ // A search parameter the schema does not name is no one's to refuse: a link carries `utm_source`, a proxy adds its own. The schema reads the ones it declares, whatever its `extra`.
99
+ function declaredOnly(schema: TSchema, input: Record<string, unknown>): Record<string, unknown> {
100
+ const { properties } = schema as ObjectSchema
101
+
102
+ return properties === undefined ? input : Object.fromEntries(Object.entries(input).filter(([name]) => Object.hasOwn(properties, name)))
103
+ }
104
+
100
105
  // `ctx.query`: the search params read against the page's `query` schema, a refusal answering 400, except for a field marked `lenient`, which a value it refuses leaves to its default.
101
106
  // Without a schema, each parameter's text, a repeated one as a list.
102
107
  export function readQuery(schema: unknown, url: URL): Readonly<Record<string, unknown>> {
103
- const input = searchInput(url)
104
-
105
108
  if (!isSchema(schema))
106
- return input
109
+ return searchInput(url)
110
+
111
+ const input = declaredOnly(schema, searchInput(url))
107
112
 
108
113
  let read = readFields(schema, input)
109
114
 
@@ -124,10 +129,10 @@ export function readQuery(schema: unknown, url: URL): Readonly<Record<string, un
124
129
 
125
130
  // `ctx.query` for a page that answers a failure, where a layout's load runs although the route's own query was refused, or never read: a field the schema refuses reads as its default, or is left out, so a layout still gets what its types promise.
126
131
  export function readQueryLeniently(schema: unknown, url: URL): Readonly<Record<string, unknown>> {
127
- const input = searchInput(url)
128
-
129
132
  if (!isSchema(schema))
130
- return input
133
+ return searchInput(url)
134
+
135
+ const input = declaredOnly(schema, searchInput(url))
131
136
 
132
137
  const read = readFields(schema, input)
133
138
 
@@ -142,30 +147,11 @@ export function readQueryLeniently(schema: unknown, url: URL): Readonly<Record<s
142
147
  }
143
148
 
144
149
  // Text read the way a form's fields are, a top-level field left out taking its schema's default, then checked and decoded: the value, or a message per field refused.
145
- // `form` is `formInput`'s: a GET form leaves an unticked checkbox out of its query, while nothing leaves a variable out of the environment on purpose, nor pads it with spaces by mistake.
150
+ // `form` is the difference between a query and the environment: a GET form leaves an unticked checkbox out of its query, while nothing leaves a variable out of the environment on purpose, nor pads it with spaces by mistake.
146
151
  export function readFields(schema: TSchema, input: Record<string, unknown>, form = true): ReadFields {
147
- const filled = { ...input }
148
- const required = (schema as { required?: string[] }).required ?? []
149
-
150
- for (const [name, field] of Object.entries((schema as ObjectSchema).properties ?? {})) {
151
- // A blank list is the empty one, as `persisted.query` writes it, never one blank item; a blank optional field with a default is left out, so the default fills in. Only here: an action's body goes through `formInput` alone.
152
- if (form && filled[name] === '' && (field as { type?: unknown }).type === 'array')
153
- filled[name] = []
154
- else if (form && filled[name] === '' && field.default !== undefined && !required.includes(name))
155
- delete filled[name]
156
-
157
- if (filled[name] === undefined && field.default !== undefined)
158
- filled[name] = structuredClone(field.default)
159
- }
160
-
161
- const value = formInput(schema, filled, form) as Record<string, unknown>
162
- const check = compiledCheck(schema)
163
-
164
- // Decoding turns what a schema reads from text, such as an `s.Date()`, into the value its type says.
165
- if (check.Check(value))
166
- return { value: check.Decode<Record<string, unknown>>(value) }
152
+ const result = validateNow<Record<string, unknown>>(schema, input, { source: form ? 'query' : 'env' })
167
153
 
168
- return { refused: refusals(check, value, fieldOf, issue => errorText((issue.schema as { error?: unknown }).error) ?? summaryOf(issue)) }
154
+ return isRefusal(result) ? { refused: new Map(Object.entries(messagesOf(result.errors))) } : { value: result.value }
169
155
  }
170
156
 
171
157
  // The rules of a page's `query` schema, which its islands' `queryParam` reads its params by: a param the rules cannot tell the type of reads as its text.
@@ -0,0 +1,43 @@
1
+ import type { Server } from 'bun'
2
+ import type { Elysia } from 'elysia'
3
+
4
+ // What a `receive` step is given: the server the request came through, none under `app.handle()`.
5
+ export type ReceiveServer = Server<unknown> | null | undefined
6
+
7
+ // A step of the `receive` phase, which runs before routing: it answers with a Response, hands on a Request to go on with (a rewrite), or returns nothing to let the request through.
8
+ export type ReceiveStep = (request: Request, server: ReceiveServer) => Request | Response | undefined | void
9
+
10
+ // What `receive` ends with: the answer, or the request every later phase and the router see.
11
+ export type Received = Request | Response
12
+
13
+ // The steps run in turn; the first to answer ends it, and one that hands on another request leaves the later steps to read that one.
14
+ export function runReceive(steps: readonly ReceiveStep[], request: Request, server: ReceiveServer): Received {
15
+ let current = request
16
+
17
+ for (const step of steps) {
18
+ const result = step(current, server)
19
+
20
+ if (result instanceof Response)
21
+ return result
22
+
23
+ if (result instanceof Request)
24
+ current = result
25
+ }
26
+
27
+ return current
28
+ }
29
+
30
+ export function receiving(steps: readonly (ReceiveStep | undefined)[]): (request: Request, server: ReceiveServer) => Received {
31
+ const given = steps.filter(step => step !== undefined)
32
+
33
+ return (request, server) => runReceive(given, request, server)
34
+ }
35
+
36
+ // `receive` as the Elysia hook the app still routes through while its routes are not on kay's table: the hook runs before Elysia routes, and a Response it returns is the answer.
37
+ export function bridgeReceive(app: Elysia, receive: (request: Request, server: ReceiveServer) => Received): void {
38
+ app.onRequest(({ request, server }) => {
39
+ const received = receive(request, server)
40
+
41
+ return received instanceof Response ? received : undefined
42
+ })
43
+ }
@@ -255,11 +255,11 @@ function navigationScripts(client: ClientBuild): string {
255
255
  // A page without islands needs no hydration script, only the entry swapping pages.
256
256
  // `saving` is set where a page drops island-only preloads for a visitor saving data. The chunks of the islands hydrating at once, `immediate`, that page keeps: they are marked `data-load`, which the dropping no longer matches.
257
257
  // One also needed by an island that waits for that visitor, `waiting`, is marked `data-wait` too, where the code waiting for the viewport is preloaded for that visitor even though no link was dropped.
258
- function islandScripts(client: ClientBuild, islands: Set<string>, streamed: boolean, hydration: boolean, lazy: boolean, saving?: { immediate: Set<string>, waiting: Set<string> }): string {
258
+ function islandScripts(client: ClientBuild, islands: Set<string>, streamed: boolean, hydration: boolean, lazy: boolean, drawn: Set<string>, saving?: { immediate: Set<string>, waiting: Set<string> }): string {
259
259
  const immediate = saving?.immediate
260
260
  const waiting = saving?.waiting
261
261
 
262
- return islandScriptsOf(client, `${streamed}\n${hydration}\n${lazy}\n${[...islands].sort().join('\n')}\n\0${immediate === undefined ? '-' : [...immediate].sort().join('\n')}\n\0${waiting === undefined ? '-' : [...waiting].sort().join('\n')}`, () => {
262
+ return islandScriptsOf(client, `${streamed}\n${hydration}\n${lazy}\n${[...islands].sort().join('\n')}\n\0${[...drawn].sort().join('\n')}\n\0${immediate === undefined ? '-' : [...immediate].sort().join('\n')}\n\0${waiting === undefined ? '-' : [...waiting].sort().join('\n')}`, () => {
263
263
  const chunks = new Set(client.entryImports)
264
264
 
265
265
  // Waiting for an island to be visible or idle, or a deferred server island to near the viewport, needs its own chunk, which the entry would ask for only once it ran.
@@ -283,6 +283,16 @@ function islandScripts(client: ClientBuild, islands: Set<string>, streamed: bool
283
283
  }
284
284
  }
285
285
 
286
+ // The components an island draws at first paint that its own chunk loads lazily: loaded before it hydrates, as the island's own code is.
287
+ for (const key of drawn) {
288
+ for (const chunk of client.lazies?.[key] ?? []) {
289
+ chunks.add(chunk)
290
+
291
+ if (immediate !== undefined)
292
+ now.add(chunk)
293
+ }
294
+ }
295
+
286
296
  return `${[...chunks].map(chunk => `<link rel="modulepreload" href="${chunk}"${now.has(chunk) ? ` data-load${waits.has(chunk) ? ' data-wait' : ''}` : ''}>`).join('')}${hydration ? hydrationScript : ''}${streamed ? '' : `<script type="module" src="${client.entry}"></script>`}`
287
297
  })
288
298
  }
@@ -495,7 +505,7 @@ function documentOf(files: string[], modules: PageModule[], data: Record<string,
495
505
  // A production page only posting forms loads navigation alone; a streamed one, or a prerendered one holding a server island, may still render islands or forms per request, which the islands' entry runs.
496
506
  const navigates = posts && !hasIslands && !follows && !defers && !streamed && (scan?.modes.length ?? 0) === 0 && client.navEntry !== undefined
497
507
  const checked = posts ? formsScript(forms, options.dev) : ''
498
- const scripts = shipsEntry ? (navigates ? navigationScripts(client) : islandScripts(client, islands.eager, streamed, hasIslands, islands.lazy > 0 || defers, options.islandPreloads === undefined ? undefined : islands)) + checked + workScript(forms, options.prerendering === true) : ''
508
+ const scripts = shipsEntry ? (navigates ? navigationScripts(client) : islandScripts(client, islands.eager, streamed, hasIslands, islands.lazy > 0 || defers, islands.lazies, options.islandPreloads === undefined ? undefined : islands)) + checked + workScript(forms, options.prerendering === true) : ''
499
509
  const state = localeState()
500
510
  // Without `i18n` the page may say its own language; with it, the locale decides.
501
511
  const lang = (state === undefined || state.implicit ? head.lang : undefined) ?? state?.locale ?? options.lang ?? DEFAULT_LANG
@@ -495,7 +495,7 @@ async function runLoad(file: string, module: PageModule, ctx: PageContext<Record
495
495
  return {}
496
496
 
497
497
  const load = module.load
498
- const run = async () => traced(() => `kay.load ${projectPath(file)}`, {}, async () => inLoad(ctx.url, async () => await load(ctx) ?? {}))
498
+ const run = async () => traced(() => `kay.load ${projectPath(file)}`, {}, async () => inLoad(ctx.url, ctx.request, () => ctx.headers.append('vary', 'cookie'), async () => await load(ctx) ?? {}))
499
499
 
500
500
  if (!timings)
501
501
  return run()