@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,276 @@
1
+ import type { Owner } from './reactive'
2
+ import { skew } from './build'
3
+ import { blockIn, claiming, HydrationMismatchError, islandOf, Range, Region, release, swap } from './dom'
4
+ import { ISLAND, PART_END, PART_START } from './markers'
5
+ import { getOwner, onCleanup, reportError, runWithOwner, unrecorded } from './reactive'
6
+ import { savesDataInBrowser } from './save-data'
7
+
8
+ // A component whose code loads the first time it draws: its own module, as it weighs on a page whose islands draw every component at once.
9
+
10
+ type Component = (props: never) => unknown
11
+
12
+ interface Loadable {
13
+ load: () => Promise<{ default: unknown }>
14
+ component?: Component | undefined
15
+ loading?: Promise<{ default: unknown }> | undefined
16
+ }
17
+
18
+ // How long to wait before each new try of a chunk that failed to load.
19
+ const RETRY_WAITS_MS = [1000, 3000]
20
+
21
+ // One entry per key, which every module importing that component shares: a module that loaded it is a load for all.
22
+ const loadables = new Map<string, Loadable>()
23
+ // The keys each island's module imports lazily, by island id, which a visitor reaching for the island loads ahead.
24
+ const scopes = new Map<string, Set<string>>()
25
+ // The keys a failed intent load left, tried once: a hover must not ask again and again for a chunk that is not there.
26
+ const refused = new Set<string>()
27
+
28
+ // Loads a component's chunk, the same load for every caller; a failure is dropped so the next call tries again.
29
+ async function ensure(loadable: Loadable): Promise<Component> {
30
+ if (loadable.component)
31
+ return loadable.component
32
+
33
+ const attempt = loadable.loading ??= loadable.load()
34
+
35
+ try {
36
+ loadable.component = (await attempt).default as Component
37
+ ;(globalThis as { $kayLazyLoaded?: boolean }).$kayLazyLoaded = true
38
+ }
39
+ catch (error) {
40
+ // Only its own attempt is dropped: another caller may have started a new one since.
41
+ if (loadable.loading === attempt)
42
+ loadable.loading = undefined
43
+
44
+ throw error
45
+ }
46
+
47
+ return loadable.component
48
+ }
49
+
50
+ let watching = false
51
+ // Bumped whenever a module registers keys, so an island warmed before it is warmed again.
52
+ let version = 0
53
+ const warmed = new Map<string, number>()
54
+
55
+ // The first sign a visitor reaches for something in an island loads the lazy parts of that island, so a click finds the code in. A visitor saving data loads a part when it opens.
56
+ function warmOnIntent(): void {
57
+ if (watching || typeof document === 'undefined')
58
+ return
59
+
60
+ watching = true
61
+
62
+ const warm = (event: Event) => {
63
+ const island = event.target instanceof Element ? event.target.closest<HTMLElement>(ISLAND) : null
64
+
65
+ if (island?.dataset.component !== undefined && !savesDataInBrowser())
66
+ void warmScope(island.dataset.component)
67
+ }
68
+
69
+ for (const name of ['pointerover', 'pointerdown', 'focusin'])
70
+ document.addEventListener(name, warm, { capture: true, passive: true })
71
+ }
72
+
73
+ // The keys of an island and of the modules that are no island on the page, which are the components it draws.
74
+ function keysFor(id: string): string[] {
75
+ const own = new Set<string>()
76
+
77
+ for (const [scope, keys] of scopes) {
78
+ if (scope === id || !document.querySelector(`${ISLAND}[data-component="${CSS.escape(scope)}"]`)) {
79
+ for (const key of keys) {
80
+ own.add(key)
81
+ }
82
+ }
83
+ }
84
+
85
+ return [...own]
86
+ }
87
+
88
+ async function warmScope(id: string): Promise<void> {
89
+ if (warmed.get(id) === version)
90
+ return
91
+
92
+ await Promise.all(keysFor(id).map(async (key) => {
93
+ const loadable = loadables.get(key)
94
+
95
+ if (!loadable || loadable.component || loadable.loading || refused.has(key))
96
+ return
97
+
98
+ try {
99
+ await ensure(loadable)
100
+ // What the loaded module imports lazily in turn belongs to the same island.
101
+ await warmScope(id)
102
+ }
103
+ catch {
104
+ // Drawn when it opens, which tries again and reports.
105
+ refused.add(key)
106
+ }
107
+ }))
108
+ warmed.set(id, version)
109
+ }
110
+
111
+ // A component imported as `KayLazy(key, () => import('./it.kay'))`: it draws as the component does once loaded, and before that a place the component renders into when it arrives.
112
+ // A visitor reaching for the island loads it ahead, and one the server drew is loaded before its island hydrates (`loadDrawn`), so the markup is claimed as it stands.
113
+ export function lazy(key: string, load: () => Promise<{ default: unknown }>): (props: never) => unknown {
114
+ let loadable = loadables.get(key)
115
+
116
+ if (!loadable)
117
+ loadables.set(key, loadable = { load })
118
+
119
+ const entry = loadable
120
+
121
+ warmOnIntent()
122
+
123
+ return (props: never) => {
124
+ if (entry.component)
125
+ return entry.component(props)
126
+
127
+ // The island was told the keys the server drew, which it loads first: one left is a markup the code was not there to claim, so the island draws afresh.
128
+ if (claiming())
129
+ throw new HydrationMismatchError(`the server drew ${key} but its code did not load before the island hydrated`)
130
+
131
+ return placeholder(entry, props, key)
132
+ }
133
+ }
134
+
135
+ // Hands an error to the nearest `catch` block above `owner`, or reports it when there is none, as an effect of the owner would.
136
+ function handleError(owner: Owner | undefined, error: unknown): void {
137
+ let catcher = owner
138
+
139
+ while (catcher && !catcher.catcher)
140
+ catcher = catcher.owner
141
+
142
+ if (!catcher) {
143
+ reportError(error)
144
+
145
+ return
146
+ }
147
+
148
+ try {
149
+ catcher.catcher!(error)
150
+ }
151
+ catch (failure) {
152
+ reportError(failure)
153
+ }
154
+ }
155
+
156
+ // An empty range, the component rendered between its ends once its code is in.
157
+ function placeholder(entry: Loadable, props: never, key: string): Range {
158
+ const start = document.createComment(PART_START)
159
+ const end = document.createComment(PART_END)
160
+ const parent = getOwner()
161
+
162
+ // The ends are siblings, as a range's nodes are, so the parent places both and what draws between them.
163
+ document.createDocumentFragment().append(start, end)
164
+
165
+ const block = blockIn(new Region(null, start, end))
166
+ let gone = false
167
+
168
+ onCleanup(() => {
169
+ gone = true
170
+ release(block)
171
+ })
172
+
173
+ void track(land(entry, key, start, () => gone, (component) => {
174
+ // A part taken out of its parent has nowhere to draw.
175
+ if (!start.parentNode)
176
+ return
177
+
178
+ // A render error goes to the nearest `catch` above the place, as an eager component's does, and is no load failure.
179
+ try {
180
+ runWithOwner(parent, () => unrecorded(() => swap(block, () => component(props), true)))
181
+ }
182
+ catch (error) {
183
+ handleError(parent, error)
184
+ }
185
+ }))
186
+
187
+ return new Range(start, end)
188
+ }
189
+
190
+ // The places waiting for their component, which a test's `settle()` waits for through `$kayLazyIdle`.
191
+ const inflight = new Set<Promise<void>>()
192
+
193
+ async function track(drawing: Promise<void>): Promise<void> {
194
+ inflight.add(drawing)
195
+
196
+ try {
197
+ await drawing
198
+ }
199
+ finally {
200
+ inflight.delete(drawing)
201
+ }
202
+ }
203
+
204
+ // Answers whether it waited for any, so a settle with nothing loading takes no extra turn.
205
+ ;(globalThis as { $kayLazyIdle?: () => Promise<boolean> }).$kayLazyIdle = async () => {
206
+ const waited = inflight.size > 0
207
+
208
+ while (inflight.size > 0)
209
+ await Promise.allSettled(inflight)
210
+
211
+ return waited
212
+ }
213
+
214
+ // Loads the component, trying again after a failure, then draws it; a render error is no load failure and goes where any render error of the branch goes. The one that failed to load for good is reported, and a deploy that dropped the chunk reloads the page.
215
+ async function land(entry: Loadable, key: string, anchor: Node, gone: () => boolean, draw: (component: Component) => void): Promise<void> {
216
+ let component: Component | undefined
217
+ let failure: unknown
218
+
219
+ for (const wait of [0, ...RETRY_WAITS_MS]) {
220
+ if (wait > 0)
221
+ await new Promise(resolve => setTimeout(resolve, wait))
222
+
223
+ if (gone())
224
+ return
225
+
226
+ try {
227
+ component = await ensure(entry)
228
+
229
+ break
230
+ }
231
+ catch (error) {
232
+ failure = error
233
+
234
+ if (await skew())
235
+ return
236
+ }
237
+ }
238
+
239
+ if (component === undefined) {
240
+ reportError(failure, islandOf(anchor))
241
+ console.error(`[kay] ${key} failed to load, it draws when it is opened again`)
242
+
243
+ return
244
+ }
245
+
246
+ if (!gone())
247
+ draw(component)
248
+ }
249
+
250
+ // Loads the keys an island's server markup drew before it hydrates, not in a module's top level, where a chunk importing its asker would wait on itself. A failed chunk is left to its branch, which draws afresh, retries and reports.
251
+ export async function loadDrawn(island: HTMLElement): Promise<void> {
252
+ const asked = [...island.querySelectorAll<HTMLElement>('[data-lazy]'), island].flatMap(element => element.dataset.lazy?.split(' ') ?? [])
253
+ const tried = new Set<string>()
254
+
255
+ // The modules loaded may register keys the markup drew, which load in the next round.
256
+ for (;;) {
257
+ const round = asked.filter(key => loadables.has(key) && !tried.has(key) && !loadables.get(key)!.component)
258
+
259
+ if (round.length === 0)
260
+ return
261
+
262
+ for (const key of round)
263
+ tried.add(key)
264
+
265
+ await Promise.allSettled(round.map(async key => ensure(loadables.get(key)!)))
266
+ }
267
+ }
268
+
269
+ // Read by the island's hydration, which this module's loading precedes: a page with no lazy import never loads it.
270
+ ;(globalThis as { $kayLoadDrawn?: typeof loadDrawn }).$kayLoadDrawn = loadDrawn
271
+
272
+ // What a module with lazy imports calls at its top level: it records the keys it imports lazily for the island `id`, which a visitor reaching for the island loads ahead.
273
+ export function ready(id: string, keys: readonly string[]): void {
274
+ scopes.set(id, new Set([...scopes.get(id) ?? [], ...keys]))
275
+ version++
276
+ }
@@ -0,0 +1,58 @@
1
+ // The `error` option of a rule: `min` for a text too short, a list too short or a number too small, `max` for the other bound, else the kind itself (`pattern`, `format`, `required`, `type`).
2
+ const RULES: Record<string, string> = { tooShort: 'min', tooFewItems: 'min', tooSmall: 'min', tooLong: 'max', tooManyItems: 'max', tooBig: 'max' }
3
+
4
+ export function ruleOf(kind: string): string {
5
+ return RULES[kind] ?? kind
6
+ }
7
+
8
+ // The text an `error` option gives a field: a string, the string a function returns, or for a record the entry of the rule the field failed, else its `default`; a function that throws or returns anything else gives none, so the field keeps its default wording.
9
+ // A function is called where the caller runs, which is inside the request's locale.
10
+ export function errorText(error: unknown, rule?: string): string | undefined {
11
+ if (typeof error === 'function') {
12
+ try {
13
+ const text = (error as () => unknown)()
14
+
15
+ return typeof text === 'string' ? text : undefined
16
+ }
17
+ catch {
18
+ return undefined
19
+ }
20
+ }
21
+
22
+ if (typeof error === 'object' && error !== null && !Array.isArray(error)) {
23
+ const words = error as Record<string, unknown>
24
+
25
+ return errorText((rule === undefined ? undefined : words[rule]) ?? words.default)
26
+ }
27
+
28
+ return typeof error === 'string' ? error : undefined
29
+ }
30
+
31
+ const isNode = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value)
32
+
33
+ // The first `error` found on the way down: a field's own, else a branch of its `anyOf`.
34
+ function find(node: unknown, segments: string[], rule: string | undefined): string | undefined {
35
+ if (!isNode(node))
36
+ return undefined
37
+
38
+ const [head, ...rest] = segments
39
+ const named = head !== undefined && isNode(node.properties) && Object.hasOwn(node.properties, head)
40
+ const found = head === undefined ? errorText(node.error, rule) : find(named ? (node.properties as Record<string, unknown>)[head] : node.items, rest, rule)
41
+
42
+ if (found !== undefined || !Array.isArray(node.anyOf))
43
+ return found
44
+
45
+ for (const branch of node.anyOf) {
46
+ const inBranch = find(branch, segments, rule)
47
+
48
+ if (inBranch !== undefined)
49
+ return inBranch
50
+ }
51
+
52
+ return undefined
53
+ }
54
+
55
+ // The `error` option at a JSON pointer of the schema, a field's, an item's or one of a union's branches, read in the current locale.
56
+ export function fieldMessage(schema: unknown, pointer: string, rule?: string): string | undefined {
57
+ return find(schema, pointer.split('/').slice(1).map(segment => segment.replaceAll('~1', '/').replaceAll('~0', '~')), rule)
58
+ }
@@ -0,0 +1,179 @@
1
+ import type { InvalidWord } from '../server/words'
2
+ import { isEmail } from './email'
3
+ import { errorText, ruleOf } from './field-errors'
4
+ import { NUMERIC_FORMATS } from './form'
5
+ import { wording } from './words.client'
6
+
7
+ // What the server sends of an action's schema, for the forms of a page to be checked before they post: the top-level fields, the keywords the browser checks, and their `error` messages.
8
+ export interface FieldRules {
9
+ type?: string | undefined
10
+ format?: string | undefined
11
+ pattern?: string | undefined
12
+ minLength?: number | undefined
13
+ maxLength?: number | undefined
14
+ minimum?: number | undefined
15
+ maximum?: number | undefined
16
+ exclusiveMinimum?: number | undefined
17
+ exclusiveMaximum?: number | undefined
18
+ minItems?: number | undefined
19
+ maxItems?: number | undefined
20
+ const?: unknown | undefined
21
+ items?: FieldRules | undefined
22
+ anyOf?: FieldRules[] | undefined
23
+ enum?: unknown[] | undefined
24
+ // A text, a function or a record by rule, as the field's `error` option; the server sends the text it resolved.
25
+ error?: unknown
26
+ }
27
+
28
+ export interface FormRules {
29
+ properties: Record<string, FieldRules>
30
+ required: string[]
31
+ }
32
+
33
+ type Format = RegExp | ((value: string) => boolean)
34
+
35
+ // The formats the browser checks too, as the server does (`email` is kay's own, the rest Elysia's); any other format is left to the server.
36
+ const FORMATS: Record<string, Format> = {
37
+ 'email': isEmail,
38
+ 'uuid': /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i,
39
+ 'url': isUrl,
40
+ 'date-time': /^\d{4}-\d\d-\d\d(?:T\d\d:\d\d(?::\d\d(?:\.\d+)?)?(?:Z|[+-]\d\d:\d\d)?)?$/i,
41
+ 'date': /^\d{4}-\d\d-\d\d$/,
42
+ }
43
+
44
+ function formatFails(format: Format | undefined, value: string): boolean {
45
+ if (format === undefined)
46
+ return false
47
+
48
+ return typeof format === 'function' ? !format(value) : !format.test(value)
49
+ }
50
+
51
+ const HTTP_URL = /^https?:/i
52
+
53
+ // An `http:` or `https:` address, as the server's `url` format reads it.
54
+ function isUrl(value: string): boolean {
55
+ if (!HTTP_URL.test(value))
56
+ return false
57
+
58
+ try {
59
+ void new URL(value)
60
+
61
+ return true
62
+ }
63
+ catch {
64
+ return false
65
+ }
66
+ }
67
+
68
+ // Where a value is refused and why: the `kay.invalid.*` key, the limit it names and the text, the field's own `error` else the key's wording; a site without the key reads Elysia's English.
69
+ export function issueOf(name: string, field: FieldRules, value: unknown): { key: InvalidWord, params: Record<string, string | number>, message: string } | undefined {
70
+ const found = refusal(name, field, value)
71
+
72
+ if (found === undefined)
73
+ return undefined
74
+
75
+ const custom = errorText(field.error, ruleOf(found.kind.slice('kay.invalid.'.length)))
76
+ const params = found.params ?? {}
77
+
78
+ return { key: found.kind, params, message: custom ?? wording(found.kind, found.english, params) }
79
+ }
80
+
81
+ // The schema's own `error` for a refused field, else the `kay.invalid.*` text of the keyword it fails, which the server words the same way.
82
+ export function fieldMessage(name: string, field: FieldRules, value: unknown): string | undefined {
83
+ const issue = issueOf(name, field, value)
84
+
85
+ if (issue !== undefined)
86
+ return issue.message
87
+
88
+ // An item is named by its index, as the server names it, and refused with its own `error`.
89
+ return field.items && Array.isArray(value) ? value.map((item, index) => fieldMessage(`${name}.${index}`, field.items!, item)).find(found => found !== undefined) : undefined
90
+ }
91
+
92
+ // What a field's value fails: the `kay.invalid.*` key the server's refusal of it would use, the limit it names, and Elysia's own summary, which is its English.
93
+ interface Refusal {
94
+ kind: InvalidWord
95
+ english: string
96
+ params?: Record<string, string | number>
97
+ }
98
+
99
+ function typeRefusal(english: string): Refusal {
100
+ return { kind: 'kay.invalid.type', english }
101
+ }
102
+
103
+ function stringRefusal(name: string, field: FieldRules, value: unknown): Refusal | undefined {
104
+ if (typeof value !== 'string' || (field.format !== undefined && NUMERIC_FORMATS.has(field.format)))
105
+ return typeRefusal('Expected string')
106
+
107
+ if (field.minLength !== undefined && value.length < field.minLength)
108
+ return { kind: value === '' && field.minLength === 1 ? 'kay.invalid.required' : 'kay.invalid.tooShort', english: `Expected string length greater or equal to ${field.minLength}`, params: { min: field.minLength } }
109
+
110
+ if (field.maxLength !== undefined && value.length > field.maxLength)
111
+ return { kind: 'kay.invalid.tooLong', english: `Expected string length less or equal to ${field.maxLength}`, params: { max: field.maxLength } }
112
+
113
+ if (field.pattern !== undefined && !new RegExp(field.pattern).test(value))
114
+ return { kind: 'kay.invalid.pattern', english: `Expected string to match '${field.pattern}'` }
115
+
116
+ return field.format !== undefined && formatFails(FORMATS[field.format], value) ? { kind: 'kay.invalid.format', english: `Property '${name}' should be ${field.format}`, params: { format: field.format } } : undefined
117
+ }
118
+
119
+ export function refusal(name: string, field: FieldRules, value: unknown): Refusal | undefined {
120
+ if (field.anyOf) {
121
+ const bound = bounds(field, value)
122
+
123
+ if (bound === undefined && field.anyOf.some(member => refusal(name, member, value) === undefined))
124
+ return undefined
125
+
126
+ // A union is summarized by its members, though a bound of its own gives the kind, as the server words it.
127
+ return { kind: bound?.kind ?? 'kay.invalid.type', english: `Property '${name}' should be one of: ${field.anyOf.map(member => `'${member.format ?? member.type}'`).join(', ')}`, ...bound?.params && { params: bound.params } }
128
+ }
129
+
130
+ if (field.enum !== undefined && field.type !== 'array' && !field.enum.includes(value))
131
+ return typeRefusal('Expected union value')
132
+
133
+ if (field.const !== undefined)
134
+ return value === field.const ? undefined : typeRefusal(`Expected ${typeof field.const === 'string' ? `'${field.const}'` : String(field.const)}`)
135
+
136
+ switch (field.type) {
137
+ case 'string':
138
+ return stringRefusal(name, field, value)
139
+ case 'number':
140
+ case 'integer':
141
+ if (typeof value !== 'number' || !Number.isFinite(value))
142
+ return typeRefusal(`Expected ${field.type}`)
143
+
144
+ // A bound a number fails is worded before its type, as the server does.
145
+ return bounds(field, value) ?? (field.type === 'integer' && !Number.isInteger(value) ? typeRefusal('Expected integer') : undefined)
146
+ case 'boolean':
147
+ return typeof value === 'boolean' ? undefined : typeRefusal('Expected boolean')
148
+ case 'array':
149
+ if (!Array.isArray(value))
150
+ return typeRefusal('Expected array')
151
+
152
+ if (field.minItems !== undefined && value.length < field.minItems)
153
+ return { kind: 'kay.invalid.tooFewItems', english: `Expected array length to be greater or equal to ${field.minItems}`, params: { min: field.minItems } }
154
+
155
+ return field.maxItems !== undefined && value.length > field.maxItems ? { kind: 'kay.invalid.tooManyItems', english: `Expected array length to be less or equal to ${field.maxItems}`, params: { max: field.maxItems } } : undefined
156
+ case undefined:
157
+ default:
158
+ return undefined
159
+ }
160
+ }
161
+
162
+ // An exclusive bound is worded as a type refusal, as the server does.
163
+ function bounds(field: FieldRules, value: unknown): Refusal | undefined {
164
+ if (typeof value !== 'number')
165
+ return undefined
166
+
167
+ const type = field.type ?? 'number'
168
+
169
+ if (field.minimum !== undefined && value < field.minimum)
170
+ return { kind: 'kay.invalid.tooSmall', english: `Expected ${type} to be greater or equal to ${field.minimum}`, params: { min: field.minimum } }
171
+
172
+ if (field.maximum !== undefined && value > field.maximum)
173
+ return { kind: 'kay.invalid.tooBig', english: `Expected ${type} to be less or equal to ${field.maximum}`, params: { max: field.maximum } }
174
+
175
+ if (field.exclusiveMinimum !== undefined && value <= field.exclusiveMinimum)
176
+ return typeRefusal(`Expected ${type} to be greater than ${field.exclusiveMinimum}`)
177
+
178
+ return field.exclusiveMaximum !== undefined && value >= field.exclusiveMaximum ? typeRefusal(`Expected ${type} to be less than ${field.exclusiveMaximum}`) : undefined
179
+ }
@@ -0,0 +1,99 @@
1
+ import type { FieldSchema } from './form'
2
+ import { escape, escapeAttribute } from './escape'
3
+
4
+ // The `autocomplete` token a field name asks for, as the HTML spec words it: `firstName`, `first_name` and `given-name` are the one field.
5
+ const NAMED_AUTOCOMPLETE = new Map([
6
+ ['firstname', 'given-name'],
7
+ ['givenname', 'given-name'],
8
+ ['lastname', 'family-name'],
9
+ ['familyname', 'family-name'],
10
+ ['surname', 'family-name'],
11
+ ['username', 'username'],
12
+ ['email', 'email'],
13
+ ['phone', 'tel'],
14
+ ['tel', 'tel'],
15
+ ['telephone', 'tel'],
16
+ ['zip', 'postal-code'],
17
+ ['postalcode', 'postal-code'],
18
+ ['postcode', 'postal-code'],
19
+ ['currentpassword', 'current-password'],
20
+ ['newpassword', 'new-password'],
21
+ ['otp', 'one-time-code'],
22
+ ['birthday', 'bday'],
23
+ ])
24
+ const NAME_SEPARATORS = /[\s_-]+/g
25
+
26
+ // The input type a field's schema asks for: `email` and `url` for their formats, `number` for a number, `date` for a date, `checkbox` for a boolean and `file` for an upload, which also takes the `accept` of its media types and `multiple` for a list of files.
27
+ export function typeAttributes(field: FieldSchema, numeric: boolean, boolean: boolean): [name: string, value: string][] {
28
+ if (field.type === 'string' && field.format === 'binary')
29
+ return fileAttributes(field, false)
30
+
31
+ if (field.type === 'array' && field.items?.format === 'binary')
32
+ return fileAttributes(field, true)
33
+
34
+ if (field.type === 'string' && (field.format === 'email' || field.format === 'url'))
35
+ return [['type', field.format]]
36
+
37
+ if (boolean)
38
+ return [['type', 'checkbox']]
39
+
40
+ if (numeric)
41
+ return [['type', 'number'], ...integral(field) ? [] : [['step', 'any'] as [string, string]]]
42
+
43
+ return isDate(field) ? [['type', 'date']] : []
44
+ }
45
+
46
+ function fileAttributes(field: FieldSchema, multiple: boolean): [string, string][] {
47
+ const accepted = (field as { extension?: unknown }).extension
48
+ const accept = Array.isArray(accepted) ? accepted.filter(type => typeof type === 'string').join(',') : accepted
49
+
50
+ return [['type', 'file'], ...typeof accept === 'string' && accept !== '' ? [['accept', accept] as [string, string]] : [], ...multiple ? [['multiple', ''] as [string, string]] : []]
51
+ }
52
+
53
+ // A whole number takes the browser's own step of 1; any other number needs `any`, or the browser refuses a decimal.
54
+ function integral(field: FieldSchema): boolean {
55
+ return field.type === 'integer' || (field.anyOf?.some(member => member.type === 'integer' || member.format === 'integer') ?? false)
56
+ }
57
+
58
+ function isDate(field: FieldSchema): boolean {
59
+ return field.type === 'Date' || (field.anyOf?.some(member => member.type === 'Date') ?? false)
60
+ }
61
+
62
+ // What the field says itself with `autocomplete`, else what its format or its name asks for.
63
+ export function autocompleteOf(field: FieldSchema, name: string): string | undefined {
64
+ const own = (field as { autocomplete?: unknown }).autocomplete
65
+
66
+ if (typeof own === 'string')
67
+ return own
68
+
69
+ if (field.type !== 'string' || field.format === 'binary')
70
+ return undefined
71
+
72
+ return field.format === 'email' || field.format === 'url' ? field.format : autocompleteByName(name)
73
+ }
74
+
75
+ // The token a field name asks for, whatever its schema says.
76
+ export function autocompleteByName(name: string): string | undefined {
77
+ return NAMED_AUTOCOMPLETE.get(name.replace(NAME_SEPARATORS, '').toLowerCase())
78
+ }
79
+
80
+ // The options of a `select(field=...)` that writes none: one per value of the field's enum, worded by the `x-labels` an `s.enum` of a record keeps, and a blank first one for a field the form may leave out.
81
+ export function optionsOf(field: FieldSchema | undefined, required: boolean): string {
82
+ // An `s.enum`, or an `s.union` of `s.literal`s.
83
+ const values = (field as { enum?: unknown } | undefined)?.enum ?? (field?.anyOf?.every(member => member.const !== undefined) ? field.anyOf.map(member => member.const) : undefined)
84
+
85
+ if (!Array.isArray(values))
86
+ return ''
87
+
88
+ const labels = (field as { 'x-labels'?: Record<string, unknown> })['x-labels']
89
+ let markup = required ? '' : '<option value=""></option>'
90
+
91
+ for (const value of values) {
92
+ const text = String(value)
93
+ const label = labels?.[text]
94
+
95
+ markup += `<option value="${escapeAttribute(text).replaceAll('>', '&gt;')}">${escape(typeof label === 'string' ? label : text)}</option>`
96
+ }
97
+
98
+ return markup
99
+ }