@kayjs/kay 1.105.0 → 1.106.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 (310) hide show
  1. package/dist/eslint.js +435 -120
  2. package/editors/zed/extension.toml +2 -2
  3. package/llms/action-protection.md +1 -1
  4. package/llms/actions-from-islands.md +2 -2
  5. package/llms/actions.md +22 -19
  6. package/llms/analytics.md +1 -1
  7. package/llms/api-from-islands.md +1 -1
  8. package/llms/api-routes.md +6 -6
  9. package/llms/check-and-tools.md +1 -1
  10. package/llms/code-lints.md +2 -2
  11. package/llms/compiler-api.md +1 -1
  12. package/llms/component-libraries.md +1 -0
  13. package/llms/content-cms.md +1 -1
  14. package/llms/content-markdown-to-caching.md +1 -1
  15. package/llms/content-preview.md +2 -2
  16. package/llms/content.md +21 -21
  17. package/llms/deprecated-lints-1-105-libraries.md +2 -0
  18. package/llms/deprecated-lints-1-105.md +3 -3
  19. package/llms/deprecated-lints.md +3 -2
  20. package/llms/dev-tool.md +2 -2
  21. package/llms/environment.md +22 -22
  22. package/llms/files.md +5 -5
  23. package/llms/islands-state-to-ids.md +2 -1
  24. package/llms/local-collections.md +2 -2
  25. package/llms/mcp.md +2 -1
  26. package/llms/navigation-from-code.md +1 -1
  27. package/llms/notifications.md +4 -2
  28. package/llms/project.md +1 -1
  29. package/llms/rate-limits.md +1 -1
  30. package/llms/recipes.md +16 -16
  31. package/llms/rooms.md +4 -4
  32. package/llms/routing-endpoints-and-base.md +1 -1
  33. package/llms/routing.md +5 -5
  34. package/llms/schemas.md +10 -5
  35. package/llms/server.md +7 -7
  36. package/llms/site.md +9 -9
  37. package/llms/sitemap-search-fonts.md +2 -0
  38. package/llms/styles-and-assets.md +2 -1
  39. package/llms/templates-elements-slots-snippets.md +2 -2
  40. package/llms/testing-browser.md +20 -0
  41. package/llms/testing.md +1 -1
  42. package/llms/upgrading-1-105-config.md +2 -2
  43. package/llms/upgrading.md +18 -3
  44. package/llms/whats-new-archive.md +35 -66
  45. package/llms/whats-new.md +34 -28
  46. package/llms.txt +1 -1
  47. package/package.json +15 -11
  48. package/src/add.ts +1 -1
  49. package/src/build/class-table.ts +2 -2
  50. package/src/build/client.ts +7 -6
  51. package/src/build/plugin.ts +14 -11
  52. package/src/build/styles.ts +3 -3
  53. package/src/build/transform.ts +3 -1
  54. package/src/build/ui-layers.ts +30 -0
  55. package/src/build/ui.ts +108 -11
  56. package/src/build/variants.ts +119 -43
  57. package/src/build/worker-bundle.ts +6 -0
  58. package/src/check/boot.ts +22 -1
  59. package/src/check/call-result.ts +242 -0
  60. package/src/check/config-objects.ts +17 -2
  61. package/src/check/diagnose.ts +22 -0
  62. package/src/check/handler-events.ts +149 -0
  63. package/src/check/index.ts +6 -2
  64. package/src/check/link-flag-tests.ts +130 -0
  65. package/src/check/project.ts +4 -3
  66. package/src/check/refusal-calls.ts +2 -2
  67. package/src/check/regroup.ts +5 -3
  68. package/src/check/renamed-packages.ts +58 -14
  69. package/src/check/schema-builders.ts +256 -0
  70. package/src/check/secret-keys.ts +15 -2
  71. package/src/check/shadow-types.ts +2 -0
  72. package/src/check/template-hints.ts +1 -1
  73. package/src/check/tool-labels.ts +11 -9
  74. package/src/check/ui-types.ts +4 -3
  75. package/src/check/ui.ts +14 -11
  76. package/src/commands/env-declare.ts +18 -6
  77. package/src/commands/routes.ts +1 -1
  78. package/src/compiler/a11y.ts +8 -1
  79. package/src/compiler/ast.ts +1 -1
  80. package/src/compiler/bound-props.ts +1 -1
  81. package/src/compiler/codegen.ts +6 -6
  82. package/src/compiler/deprecated-lints-1-106.ts +14 -0
  83. package/src/compiler/deprecated-lints.ts +10 -5
  84. package/src/compiler/deprecated-typed-lints.ts +23 -0
  85. package/src/compiler/dom-here.ts +72 -0
  86. package/src/compiler/dom.ts +23 -23
  87. package/src/compiler/fence-checks.ts +12 -11
  88. package/src/compiler/field-described.ts +2 -2
  89. package/src/compiler/helper-imports.ts +1 -1
  90. package/src/compiler/html-fields.ts +92 -0
  91. package/src/compiler/html-initial.ts +5 -3
  92. package/src/compiler/html.ts +14 -31
  93. package/src/compiler/index.ts +3 -1
  94. package/src/compiler/items.ts +5 -5
  95. package/src/compiler/link-flags.ts +6 -5
  96. package/src/compiler/lints.ts +12 -12
  97. package/src/compiler/rest-omits.ts +15 -0
  98. package/src/config-loader.ts +10 -2
  99. package/src/config-resolve.ts +3 -1
  100. package/src/config-shape.ts +2 -2
  101. package/src/config.ts +20 -4
  102. package/src/content/markdown.ts +2 -3
  103. package/src/generate-resource.ts +9 -9
  104. package/src/generate.ts +1 -1
  105. package/src/mcp/guards.ts +1 -1
  106. package/src/mcp/tools.ts +1 -1
  107. package/src/migrate/content.ts +61 -55
  108. package/src/runtime/action-shared.ts +2 -2
  109. package/src/runtime/action.ts +6 -2
  110. package/src/runtime/form-types.ts +99 -0
  111. package/src/runtime/form.ts +22 -7
  112. package/src/runtime/html.ts +36 -5
  113. package/src/runtime/markdown-autolink.ts +69 -0
  114. package/src/runtime/markdown-render.client.ts +39 -3
  115. package/src/runtime/markdown.client.ts +6 -3
  116. package/src/runtime/markdown.server.ts +6 -3
  117. package/src/runtime/markdown.ts +125 -1
  118. package/src/runtime/navigate-api.ts +10 -5
  119. package/src/runtime/persist.ts +7 -7
  120. package/src/runtime/query-param.ts +1 -1
  121. package/src/runtime/reactive.ts +14 -0
  122. package/src/runtime/sanitize.ts +3 -1
  123. package/src/runtime/schema-record.ts +31 -0
  124. package/src/runtime/schema.client.ts +1 -4
  125. package/src/runtime/schema.ts +321 -26
  126. package/src/runtime/swap-focus.ts +59 -11
  127. package/src/server/action-module.ts +16 -0
  128. package/src/server/actions.ts +6 -14
  129. package/src/server/api-modules.ts +1 -1
  130. package/src/server/api.ts +13 -1
  131. package/src/server/app-stages.ts +23 -18
  132. package/src/server/dev-ui.ts +2 -2
  133. package/src/server/env-state.ts +1 -1
  134. package/src/server/env.ts +11 -3
  135. package/src/server/favicon.ts +47 -1
  136. package/src/server/image-metadata-bmff.ts +1 -1
  137. package/src/server/kept-pages.ts +3 -3
  138. package/src/server/library.ts +2 -0
  139. package/src/server/mcp.ts +13 -12
  140. package/src/server/notifications.ts +4 -2
  141. package/src/server/offline.ts +24 -7
  142. package/src/server/push-settings.ts +16 -2
  143. package/src/server/push.ts +12 -4
  144. package/src/server/query.ts +13 -6
  145. package/src/server/render.ts +1 -1
  146. package/src/server/scope.ts +20 -13
  147. package/src/server/signals.ts +4 -4
  148. package/src/template-ws.ts +1 -1
  149. package/src/test/a11y-dom.ts +70 -0
  150. package/src/test/html-snapshot.ts +103 -0
  151. package/src/test/index.ts +2 -1
  152. package/src/test/mount.ts +8 -1
  153. package/src/test/page-checks.ts +12 -0
  154. package/src/test/screenshot-match.ts +2 -2
  155. package/src/test/shared-modules.ts +27 -5
  156. package/src/test/site-types.ts +5 -1
  157. package/types/build/class-table.d.ts +1 -1
  158. package/types/build/class-table.d.ts.map +1 -1
  159. package/types/build/client.d.ts.map +1 -1
  160. package/types/build/plugin.d.ts.map +1 -1
  161. package/types/build/transform.d.ts +1 -0
  162. package/types/build/transform.d.ts.map +1 -1
  163. package/types/build/ui-layers.d.ts +5 -0
  164. package/types/build/ui-layers.d.ts.map +1 -0
  165. package/types/build/ui.d.ts +7 -0
  166. package/types/build/ui.d.ts.map +1 -1
  167. package/types/build/variants.d.ts +1 -1
  168. package/types/build/variants.d.ts.map +1 -1
  169. package/types/build/worker-bundle.d.ts +1 -0
  170. package/types/build/worker-bundle.d.ts.map +1 -1
  171. package/types/check/boot.d.ts.map +1 -1
  172. package/types/check/call-result.d.ts +7 -0
  173. package/types/check/call-result.d.ts.map +1 -0
  174. package/types/check/config-objects.d.ts.map +1 -1
  175. package/types/check/diagnose.d.ts.map +1 -1
  176. package/types/check/handler-events.d.ts +9 -0
  177. package/types/check/handler-events.d.ts.map +1 -0
  178. package/types/check/index.d.ts +1 -0
  179. package/types/check/index.d.ts.map +1 -1
  180. package/types/check/link-flag-tests.d.ts +4 -0
  181. package/types/check/link-flag-tests.d.ts.map +1 -0
  182. package/types/check/project.d.ts.map +1 -1
  183. package/types/check/regroup.d.ts +1 -1
  184. package/types/check/regroup.d.ts.map +1 -1
  185. package/types/check/renamed-packages.d.ts +1 -0
  186. package/types/check/renamed-packages.d.ts.map +1 -1
  187. package/types/check/schema-builders.d.ts +3 -0
  188. package/types/check/schema-builders.d.ts.map +1 -0
  189. package/types/check/secret-keys.d.ts.map +1 -1
  190. package/types/check/shadow-types.d.ts.map +1 -1
  191. package/types/check/template-hints.d.ts +1 -0
  192. package/types/check/template-hints.d.ts.map +1 -1
  193. package/types/check/tool-labels.d.ts.map +1 -1
  194. package/types/check/ui-types.d.ts.map +1 -1
  195. package/types/check/ui.d.ts.map +1 -1
  196. package/types/commands/env-declare.d.ts.map +1 -1
  197. package/types/compiler/a11y.d.ts.map +1 -1
  198. package/types/compiler/bound-props.d.ts +1 -0
  199. package/types/compiler/bound-props.d.ts.map +1 -1
  200. package/types/compiler/codegen.d.ts +1 -0
  201. package/types/compiler/codegen.d.ts.map +1 -1
  202. package/types/compiler/deprecated-lints-1-106.d.ts +12 -0
  203. package/types/compiler/deprecated-lints-1-106.d.ts.map +1 -0
  204. package/types/compiler/deprecated-lints.d.ts +32 -5
  205. package/types/compiler/deprecated-lints.d.ts.map +1 -1
  206. package/types/compiler/deprecated-typed-lints.d.ts +21 -0
  207. package/types/compiler/deprecated-typed-lints.d.ts.map +1 -0
  208. package/types/compiler/dom-here.d.ts +14 -0
  209. package/types/compiler/dom-here.d.ts.map +1 -0
  210. package/types/compiler/dom.d.ts +1 -0
  211. package/types/compiler/dom.d.ts.map +1 -1
  212. package/types/compiler/fence-checks.d.ts.map +1 -1
  213. package/types/compiler/field-described.d.ts +1 -1
  214. package/types/compiler/field-described.d.ts.map +1 -1
  215. package/types/compiler/html-fields.d.ts +19 -0
  216. package/types/compiler/html-fields.d.ts.map +1 -0
  217. package/types/compiler/html-initial.d.ts +3 -2
  218. package/types/compiler/html-initial.d.ts.map +1 -1
  219. package/types/compiler/html.d.ts.map +1 -1
  220. package/types/compiler/index.d.ts +1 -0
  221. package/types/compiler/index.d.ts.map +1 -1
  222. package/types/compiler/items.d.ts +4 -1
  223. package/types/compiler/items.d.ts.map +1 -1
  224. package/types/compiler/link-flags.d.ts +6 -0
  225. package/types/compiler/link-flags.d.ts.map +1 -1
  226. package/types/compiler/lints.d.ts +44 -17
  227. package/types/compiler/lints.d.ts.map +1 -1
  228. package/types/compiler/rest-omits.d.ts +4 -0
  229. package/types/compiler/rest-omits.d.ts.map +1 -0
  230. package/types/config-loader.d.ts.map +1 -1
  231. package/types/config-resolve.d.ts.map +1 -1
  232. package/types/config.d.ts +15 -2
  233. package/types/config.d.ts.map +1 -1
  234. package/types/content/markdown.d.ts.map +1 -1
  235. package/types/migrate/content.d.ts.map +1 -1
  236. package/types/runtime/action-shared.d.ts +1 -1
  237. package/types/runtime/action-shared.d.ts.map +1 -1
  238. package/types/runtime/action.d.ts +3 -0
  239. package/types/runtime/action.d.ts.map +1 -1
  240. package/types/runtime/form-types.d.ts +6 -0
  241. package/types/runtime/form-types.d.ts.map +1 -0
  242. package/types/runtime/form.d.ts +3 -1
  243. package/types/runtime/form.d.ts.map +1 -1
  244. package/types/runtime/html.d.ts +3 -0
  245. package/types/runtime/html.d.ts.map +1 -1
  246. package/types/runtime/markdown-autolink.d.ts +11 -0
  247. package/types/runtime/markdown-autolink.d.ts.map +1 -0
  248. package/types/runtime/markdown-render.client.d.ts +2 -1
  249. package/types/runtime/markdown-render.client.d.ts.map +1 -1
  250. package/types/runtime/markdown.client.d.ts +3 -1
  251. package/types/runtime/markdown.client.d.ts.map +1 -1
  252. package/types/runtime/markdown.d.ts +11 -0
  253. package/types/runtime/markdown.d.ts.map +1 -1
  254. package/types/runtime/markdown.server.d.ts +3 -1
  255. package/types/runtime/markdown.server.d.ts.map +1 -1
  256. package/types/runtime/navigate-api.d.ts +1 -1
  257. package/types/runtime/navigate-api.d.ts.map +1 -1
  258. package/types/runtime/persist.d.ts +2 -2
  259. package/types/runtime/persist.d.ts.map +1 -1
  260. package/types/runtime/reactive.d.ts +3 -0
  261. package/types/runtime/reactive.d.ts.map +1 -1
  262. package/types/runtime/sanitize.d.ts.map +1 -1
  263. package/types/runtime/schema-record.d.ts +9 -0
  264. package/types/runtime/schema-record.d.ts.map +1 -0
  265. package/types/runtime/schema.client.d.ts.map +1 -1
  266. package/types/runtime/schema.d.ts +94 -10
  267. package/types/runtime/schema.d.ts.map +1 -1
  268. package/types/runtime/swap-focus.d.ts.map +1 -1
  269. package/types/server/action-module.d.ts +2 -0
  270. package/types/server/action-module.d.ts.map +1 -0
  271. package/types/server/actions.d.ts +1 -0
  272. package/types/server/actions.d.ts.map +1 -1
  273. package/types/server/api.d.ts.map +1 -1
  274. package/types/server/app-stages.d.ts +3 -2
  275. package/types/server/app-stages.d.ts.map +1 -1
  276. package/types/server/env-state.d.ts.map +1 -1
  277. package/types/server/env.d.ts +5 -1
  278. package/types/server/env.d.ts.map +1 -1
  279. package/types/server/favicon.d.ts +3 -1
  280. package/types/server/favicon.d.ts.map +1 -1
  281. package/types/server/kept-pages.d.ts +1 -1
  282. package/types/server/kept-pages.d.ts.map +1 -1
  283. package/types/server/library.d.ts +2 -0
  284. package/types/server/library.d.ts.map +1 -1
  285. package/types/server/mcp.d.ts +1 -1
  286. package/types/server/mcp.d.ts.map +1 -1
  287. package/types/server/notifications.d.ts +2 -1
  288. package/types/server/notifications.d.ts.map +1 -1
  289. package/types/server/offline.d.ts.map +1 -1
  290. package/types/server/push-settings.d.ts +1 -1
  291. package/types/server/push-settings.d.ts.map +1 -1
  292. package/types/server/push.d.ts +1 -0
  293. package/types/server/push.d.ts.map +1 -1
  294. package/types/server/query.d.ts.map +1 -1
  295. package/types/server/scope.d.ts +1 -1
  296. package/types/server/scope.d.ts.map +1 -1
  297. package/types/test/a11y-dom.d.ts +20 -0
  298. package/types/test/a11y-dom.d.ts.map +1 -0
  299. package/types/test/html-snapshot.d.ts +4 -0
  300. package/types/test/html-snapshot.d.ts.map +1 -0
  301. package/types/test/index.d.ts.map +1 -1
  302. package/types/test/mount.d.ts +3 -0
  303. package/types/test/mount.d.ts.map +1 -1
  304. package/types/test/page-checks.d.ts +6 -0
  305. package/types/test/page-checks.d.ts.map +1 -0
  306. package/types/test/screenshot-match.d.ts +1 -1
  307. package/types/test/screenshot-match.d.ts.map +1 -1
  308. package/types/test/shared-modules.d.ts.map +1 -1
  309. package/types/test/site-types.d.ts +3 -1
  310. package/types/test/site-types.d.ts.map +1 -1
@@ -5,26 +5,166 @@ import { FormatRegistry } from 'elysia/type-system'
5
5
  import { byteSize } from './byte-size'
6
6
  import { isEmail } from './email'
7
7
  import { KayError } from './kay-error'
8
+ import { DEFAULT_MAX_LENGTH } from './schema-bounds'
8
9
 
9
10
  type Builder = typeof schema
10
11
 
11
- // What `s` builds and every API taking a schema accepts: an action's input, an env, a collection, an API route's `query`. In kay 1.x it is the TypeBox schema of Elysia, so a `t.Object(...)` of Elysia is one too, and `s.typebox` is the builder behind it; neither is part of kay's stable API.
12
+ // What `s` builds and every API taking a schema accepts: an action's input, an env, a collection, an API route's `query`. In kay 1.x it is a TypeBox schema, so a `t.Object(...)` of Elysia is one too; neither is part of kay's stable API.
12
13
  export interface Schema extends TSchema {}
13
14
 
14
- // The value a schema describes.
15
+ // The value a schema describes. Kept as `Static`, the name an app already reads it by, since a second name would only duplicate it.
15
16
  export type Static<Type extends Schema, Params extends unknown[] = []> = TypeBoxStatic<Type, Params>
16
17
 
17
18
  // The `email` format is kay's own check, the one the browser, auth and mail share, in place of Elysia's.
18
19
  FormatRegistry.Set('email', isEmail)
20
+ FormatRegistry.Set('url', isUrl)
19
21
 
20
- // A string of at least 32 characters that production must set, and development and tests derive from the project path, so a secret needs no placeholder in the app: read it with `defineEnv`.
21
- function Secret(options: Parameters<Builder['String']>[0] = {}): ReturnType<Builder['String']> {
22
- return schema.String({ ...options, minLength: Math.max(32, (options as { minLength?: number }).minLength ?? 0), requiredInProduction: true, secret: true })
22
+ const HTTP_URL = /^https?:/i
23
+
24
+ // `new URL` in a try, as URL.canParse is past the browsers kay supports (Safari 17).
25
+ function isUrl(value: string): boolean {
26
+ if (!HTTP_URL.test(value))
27
+ return false
28
+
29
+ try {
30
+ void new URL(value)
31
+
32
+ return true
33
+ }
34
+ catch {
35
+ return false
36
+ }
23
37
  }
24
38
 
25
- // A field of `defineEnv` an island may read, as `publicEnv` from `kay`, such as a map key or an analytics id; a name such as `STRIPE_SECRET` or `DATABASE_URL` is refused at boot, since every visitor reads it.
26
- function Public<Field extends Schema>(field: Field): Field & { public: true } {
27
- return { ...field, public: true }
39
+ // The most items a list takes when its builder is given no `max`.
40
+ export const DEFAULT_MAX_ITEMS = 1000
41
+
42
+ const SLUG = '^[a-z0-9]+(?:-[a-z0-9]+)*$'
43
+
44
+ // What every builder takes beside its own options: the field is `optional`, takes a `default` when the value is left out, words its refusal with `error` (a text or a function called in the request's locale), and carries a `label`, a `description` and an `example` for the form, OpenAPI and MCP.
45
+ export interface FieldBase<Value = unknown> {
46
+ optional?: boolean | undefined
47
+ default?: Value | undefined
48
+ error?: string | (() => string) | undefined
49
+ label?: string | undefined
50
+ description?: string | undefined
51
+ example?: Value | undefined
52
+ }
53
+
54
+ // Any other JSON Schema keyword, `format` or `x-privacy`, goes through to the schema as it is.
55
+ export type FieldOptions<Value = unknown> = FieldBase<Value> & Record<string, unknown>
56
+
57
+ interface Flagged { optional?: boolean | undefined }
58
+
59
+ // A field is optional in the shape that holds it when its options say `optional: true`.
60
+ const optionalOf = <Field extends TSchema>(field: Field) => schema.Optional(field)
61
+
62
+ type Wrapped<Field extends Schema, Options> = Options extends { optional: true } ? ReturnType<typeof optionalOf<Field>> : Field
63
+
64
+ // The TypeBox keywords a field option stands for, left alone when it is not given.
65
+ function keywords(options: FieldOptions): Record<string, unknown> {
66
+ const { default: fallback, error, label, description, example } = options
67
+
68
+ return {
69
+ ...fallback !== undefined && { default: fallback },
70
+ ...error !== undefined && { error },
71
+ ...label !== undefined && { title: label },
72
+ ...description !== undefined && { description },
73
+ ...example !== undefined && { examples: [example] },
74
+ }
75
+ }
76
+
77
+ function wrapped<Field extends Schema, Options extends Flagged>(field: Field, options: Options | undefined): Wrapped<Field, Options> {
78
+ return (options?.optional === true ? optionalOf(field) : field) as Wrapped<Field, Options>
79
+ }
80
+
81
+ // The options of a builder without the field options, which `keywords` has turned into TypeBox's.
82
+ function rest<Options extends object>(options: Options | undefined, own: readonly string[]): Record<string, unknown> {
83
+ return Object.fromEntries(Object.entries(options ?? {}).filter(([key]) => !own.includes(key)))
84
+ }
85
+
86
+ const FIELD_KEYS = ['optional', 'default', 'error', 'label', 'description', 'example'] as const
87
+
88
+ // What `s.email()`, `s.url()` and `s.slug()` take: their own format is theirs, so no pattern, no format and no other keyword.
89
+ export interface FormatOptions extends FieldBase<string> {
90
+ min?: number | undefined
91
+ max?: number | undefined
92
+ }
93
+
94
+ export interface StringOptions extends FieldOptions<string> {
95
+ min?: number | undefined
96
+ max?: number | undefined
97
+ pattern?: RegExp | string | undefined
98
+ // A format TypeBox knows, such as `'uuid'`; `s.email()`, `s.url()` and `s.slug()` set theirs.
99
+ format?: string | undefined
100
+ }
101
+
102
+ function stringOf(options: StringOptions | undefined, extra: Record<string, unknown> = {}): ReturnType<Builder['String']> {
103
+ const pattern = options?.pattern instanceof RegExp ? options.pattern.source : options?.pattern
104
+
105
+ return schema.String({
106
+ ...rest(options, [...FIELD_KEYS, 'min', 'max', 'pattern']),
107
+ ...keywords(options ?? {}),
108
+ ...extra,
109
+ ...options?.min !== undefined && { minLength: options.min },
110
+ maxLength: options?.max ?? (typeof options?.maxLength === 'number' ? options.maxLength : DEFAULT_MAX_LENGTH),
111
+ ...pattern !== undefined && { pattern },
112
+ })
113
+ }
114
+
115
+ type Built<Field extends Schema, Options> = Wrapped<Field, Options>
116
+
117
+ // A text, at most `max` characters long (10,000 when not given), `min` at least, matching `pattern`.
118
+ function string<const Options extends StringOptions>(options?: Options): Built<ReturnType<Builder['String']>, Options> {
119
+ return wrapped(stringOf(options), options)
120
+ }
121
+
122
+ // An address `isEmail` accepts, the check the browser, auth and mail share, at most 254 characters long.
123
+ function email<const Options extends FormatOptions>(options?: Options): Built<ReturnType<Builder['String']>, Options> {
124
+ return wrapped(stringOf({ max: 254, ...options }, { format: 'email' }), options)
125
+ }
126
+
127
+ // An `http:` or `https:` address.
128
+ function url<const Options extends FormatOptions>(options?: Options): Built<ReturnType<Builder['String']>, Options> {
129
+ return wrapped(stringOf({ max: 2048, ...options }, { format: 'url' }), options)
130
+ }
131
+
132
+ // Lowercase words and digits joined by single hyphens.
133
+ function slug<const Options extends FormatOptions>(options?: Options): Built<ReturnType<Builder['String']>, Options> {
134
+ return wrapped(stringOf({ max: 200, ...options, pattern: SLUG }), options)
135
+ }
136
+
137
+ export interface NumberOptions extends FieldOptions<number> {
138
+ min?: number | undefined
139
+ max?: number | undefined
140
+ }
141
+
142
+ function bounds(options: NumberOptions | undefined): Record<string, unknown> {
143
+ return {
144
+ ...rest(options, [...FIELD_KEYS, 'min', 'max']),
145
+ ...keywords(options ?? {}),
146
+ ...options?.min !== undefined && { minimum: options.min },
147
+ ...options?.max !== undefined && { maximum: options.max },
148
+ }
149
+ }
150
+
151
+ // A whole number. A form, a query, a param or an env value arrives as text, which the text types of Elysia read as the number it spells.
152
+ function int<const Options extends NumberOptions>(options?: Options): Built<ReturnType<Builder['Integer']>, Options> {
153
+ return wrapped(schema.Integer(bounds(options)), options)
154
+ }
155
+
156
+ // A finite number.
157
+ function number<const Options extends NumberOptions>(options?: Options): Built<ReturnType<Builder['Numeric']>, Options> {
158
+ return wrapped(schema.Numeric(bounds(options)), options)
159
+ }
160
+
161
+ function boolean<const Options extends FieldOptions<boolean>>(options?: Options): Built<ReturnType<Builder['BooleanString']>, Options> {
162
+ return wrapped(schema.BooleanString({ ...rest(options, FIELD_KEYS), ...keywords(options ?? {}) }), options)
163
+ }
164
+
165
+ // A `Date`, read from the text or the number of one as Elysia's `Date` does, until `validate()` reads dates alone.
166
+ function date<const Options extends FieldOptions<Date>>(options?: Options): Built<ReturnType<Builder['Date']>, Options> {
167
+ return wrapped(schema.Date({ ...rest(options, FIELD_KEYS), ...keywords(options ?? {}) }), options)
28
168
  }
29
169
 
30
170
  // Elysia's `UnionEnum` fills the first value in as `default`, so an optional field read as that value when left out and a required one was never refused; kay leaves `default` out unless the options give one.
@@ -37,36 +177,191 @@ const UnionEnum: Builder['UnionEnum'] = (values, options) => {
37
177
  return field
38
178
  }
39
179
 
40
- // A `UnionEnum` of a record's keys, typed as their union, for a `select` whose options come from a record of labels.
41
- function UnionKeys<const Keyed extends object>(record: Keyed, options?: Parameters<Builder['UnionEnum']>[1]): ReturnType<typeof schema.UnionEnum<[keyof Keyed & string, ...(keyof Keyed & string)[]]>> {
42
- const keys = Object.keys(record)
180
+ type EnumValue = string | number
43
181
 
44
- if (keys.length === 0)
45
- throw new KayError('argument-invalid', 'kay: s.UnionKeys() takes a record with at least one key')
182
+ type EnumValues = readonly [EnumValue, ...EnumValue[]]
46
183
 
47
- return UnionEnum(keys as [keyof Keyed & string, ...(keyof Keyed & string)[]], options)
184
+ // One of the values, or of a record's keys, as `select` draws its options from a record of labels.
185
+ function enumeration<const Values extends EnumValues, const Options extends FieldOptions<Values[number]> = FieldOptions<Values[number]>>(values: Values, options?: Options): Built<ReturnType<typeof schema.UnionEnum<Values>>, Options>
186
+ function enumeration<const Keyed extends object, const Options extends FieldOptions<keyof Keyed & string> = FieldOptions<keyof Keyed & string>>(record: Keyed, options?: Options): Built<ReturnType<typeof schema.UnionEnum<[keyof Keyed & string, ...(keyof Keyed & string)[]]>>, Options>
187
+
188
+ function enumeration(source: EnumValues | object, options?: FieldOptions): Schema {
189
+ const values = Array.isArray(source) ? source as unknown as EnumValues : Object.keys(source)
190
+
191
+ if (values.length === 0)
192
+ throw new KayError('argument-invalid', 'kay: s.enum() takes at least one value, or a record with at least one key')
193
+
194
+ // The texts of a record word the options of a `select(field=...)`.
195
+ const texts = Array.isArray(source) ? [] : Object.entries(source).filter(([, text]) => typeof text === 'string')
196
+ const labels = texts.length === 0 ? {} : { 'x-labels': Object.fromEntries(texts) }
197
+
198
+ return wrapped(UnionEnum(values as unknown as EnumValues, { ...rest(options, FIELD_KEYS), ...keywords(options ?? {}), ...labels }), options)
199
+ }
200
+
201
+ function literal<const Value extends string | number | boolean, const Options extends FieldOptions<Value>>(value: Value, options?: Options): Built<ReturnType<typeof schema.Literal<Value>>, Options> {
202
+ return wrapped(schema.Literal(value, { ...rest(options, FIELD_KEYS), ...keywords(options ?? {}) }), options)
48
203
  }
49
204
 
50
- type FileOptions = Omit<NonNullable<Parameters<Builder['File']>[0]>, 'minSize' | 'maxSize'> & { minSize?: ByteSize | undefined, maxSize?: ByteSize | undefined }
51
- type FilesOptions = Omit<NonNullable<Parameters<Builder['Files']>[0]>, 'minSize' | 'maxSize'> & { minSize?: ByteSize | undefined, maxSize?: ByteSize | undefined }
205
+ function union<const Members extends readonly [Schema, Schema, ...Schema[]], const Options extends FieldOptions>(members: Members, options?: Options): Built<ReturnType<typeof schema.Union<[...Members]>>, Options> {
206
+ return wrapped(schema.Union([...members], { ...rest(options, FIELD_KEYS), ...keywords(options ?? {}) }), options) as never
207
+ }
52
208
 
53
- // Elysia reads `'2m'` and `'600k'`, kay everywhere else `'2mb'` and `'600kb'`: the sizes of a file field are read as the action's `maxSize` is and handed to Elysia as bytes.
54
- function sized<Options extends { minSize?: ByteSize | undefined, maxSize?: ByteSize | undefined }>(options: Options | undefined, name: string): (Omit<Options, 'minSize' | 'maxSize'> & { minSize?: number, maxSize?: number }) | undefined {
55
- if (options === undefined)
56
- return undefined
209
+ export interface ObjectOptions extends FieldOptions {
210
+ // What an object does with a key its shape lacks: `'refuse'` (the default) answers 422, `'strip'` drops it.
211
+ extra?: 'refuse' | 'strip' | undefined
212
+ }
213
+
214
+ // The fields of an object, `shape` a record of schemas, which is refused with a 422 when it carries a key the shape lacks; `{ extra: 'strip' }` drops it instead. Anywhere kay takes a schema, a plain record is this object, so `s.object()` is rarely written.
215
+ function object<Shape extends Record<string, Schema>, const Options extends ObjectOptions>(shape: Shape, options?: Options): Built<ReturnType<typeof schema.Object<Shape>>, Options> {
216
+ return wrapped(schema.Object(shape, { ...options?.extra !== 'strip' && { additionalProperties: false }, ...rest(options, [...FIELD_KEYS, 'extra']), ...keywords(options ?? {}) }), options)
217
+ }
218
+
219
+ // An object of any keys, each holding `value`.
220
+ function record<Value extends Schema, const Options extends FieldOptions>(value: Value, options?: Options): Built<ReturnType<typeof schema.Record<ReturnType<Builder['String']>, Value>>, Options> {
221
+ return wrapped(schema.Record(schema.String({ maxLength: 200 }), value, { ...rest(options, FIELD_KEYS), ...keywords(options ?? {}) }), options)
222
+ }
223
+
224
+ export interface ListOptions extends FieldOptions<unknown[]> {
225
+ min?: number | undefined
226
+ max?: number | undefined
227
+ }
228
+
229
+ // A list of files is bounded by `max` alone, as the body limit of an upload reads it, so a list of any other item is the one capped at 1,000.
230
+ function maxItemsOf(item: Schema, options: ListOptions | undefined): Record<string, unknown> {
231
+ const max = options?.max ?? (typeof options?.maxItems === 'number' ? options.maxItems : undefined)
232
+
233
+ if (max !== undefined)
234
+ return { maxItems: max }
235
+
236
+ return (item as { format?: unknown }).format === 'binary' ? {} : { maxItems: DEFAULT_MAX_ITEMS }
237
+ }
238
+
239
+ // A list of `item`, at most `max` long (1,000 when not given).
240
+ function list<Item extends Schema, const Options extends ListOptions>(item: Item, options?: Options): Built<ReturnType<typeof schema.Array<Item>>, Options> {
241
+ return wrapped(schema.Array(item, {
242
+ ...rest(options, [...FIELD_KEYS, 'min', 'max']),
243
+ ...keywords(options ?? {}),
244
+ ...options?.min !== undefined && { minItems: options.min },
245
+ ...maxItemsOf(item, options),
246
+ }), options)
247
+ }
248
+
249
+ export interface FileOptions extends FieldOptions<never> {
250
+ // The media type the file must have, `'application/pdf'`, or a family such as `'image/*'`, or a list of them.
251
+ type?: string | string[] | undefined
252
+ minSize?: ByteSize | undefined
253
+ maxSize?: ByteSize | undefined
254
+ }
255
+
256
+ export interface FilesOptions extends FileOptions {
257
+ min?: number | undefined
258
+ max?: number | undefined
259
+ }
57
260
 
58
- const { minSize, maxSize, ...rest } = options
261
+ // The sizes of a file field are read as the action's `maxSize` is: `'2mb'`, `'600kb'` or bytes, handed to Elysia as bytes.
262
+ function sized(options: FileOptions | undefined, name: string): Record<string, unknown> {
263
+ const { minSize, maxSize } = options ?? {}
59
264
 
60
265
  return {
61
- ...rest,
266
+ ...rest(options, [...FIELD_KEYS, 'minSize', 'maxSize']),
267
+ ...keywords(options ?? {}),
62
268
  ...minSize !== undefined && { minSize: byteSize(minSize, `${name}({ minSize })`) },
63
269
  ...maxSize !== undefined && { maxSize: byteSize(maxSize, `${name}({ maxSize })`) },
64
270
  }
65
271
  }
66
272
 
67
- const File = (options?: FileOptions): ReturnType<Builder['File']> => schema.File(sized(options, 's.File'))
68
- const Files = (options?: FilesOptions): ReturnType<Builder['Files']> => schema.Files(sized(options, 's.Files'))
273
+ function file<const Options extends FileOptions>(options?: Options): Built<ReturnType<Builder['File']>, Options> {
274
+ return wrapped(schema.File(sized(options, 's.file') as never), options)
275
+ }
276
+
277
+ function files<const Options extends FilesOptions>(options?: Options): Built<ReturnType<Builder['Files']>, Options> {
278
+ const { min, max } = options ?? {}
279
+
280
+ return wrapped(schema.Files({ ...sized(options, 's.files'), ...min !== undefined && { minItems: min }, ...max !== undefined && { maxItems: max } }), options)
281
+ }
282
+
283
+ // A string of at least 32 characters that production must set, and development and tests derive from the project path, so a secret needs no placeholder in the app: read it with `defineEnv`.
284
+ function secret<const Options extends StringOptions>(options?: Options): Built<ReturnType<Builder['String']>, Options> {
285
+ return wrapped(stringOf({ ...options, min: Math.max(32, options?.min ?? 0) }, { requiredInProduction: true, secret: true }), options)
286
+ }
287
+
288
+ // A field of `defineEnv` an island may read, as `publicEnv` from `kay`, such as a map key or an analytics id; a name such as `STRIPE_SECRET` or `DATABASE_URL` is refused at boot, since every visitor reads it.
289
+ function publicField<Field extends Schema>(field: Field): Field & { public: true } {
290
+ return { ...field, public: true }
291
+ }
292
+
293
+ // A `UnionEnum` of a record's keys, typed as their union, for a `select` whose options come from a record of labels.
294
+ function UnionKeys<const Keyed extends object>(record: Keyed, options?: Parameters<Builder['UnionEnum']>[1]): ReturnType<typeof schema.UnionEnum<[keyof Keyed & string, ...(keyof Keyed & string)[]]>> {
295
+ const keys = Object.keys(record)
296
+
297
+ if (keys.length === 0)
298
+ throw new KayError('argument-invalid', 'kay: s.UnionKeys() takes a record with at least one key')
299
+
300
+ return UnionEnum(keys as [keyof Keyed & string, ...(keyof Keyed & string)[]], options)
301
+ }
69
302
 
70
- // Elysia's schema builder, re-exported so an action's schema needs no import of its own, with `Secret` and `Public` for `defineEnv`, `UnionKeys`, and `File` and `Files` taking a size as an action's `maxSize` does.
303
+ function Secret(options: Parameters<Builder['String']>[0] = {}): ReturnType<Builder['String']> {
304
+ return schema.String({ ...options, minLength: Math.max(32, (options as { minLength?: number }).minLength ?? 0), requiredInProduction: true, secret: true })
305
+ }
306
+
307
+ type OldFileOptions = Omit<NonNullable<Parameters<Builder['File']>[0]>, 'minSize' | 'maxSize'> & { minSize?: ByteSize | undefined, maxSize?: ByteSize | undefined }
308
+ type OldFilesOptions = Omit<NonNullable<Parameters<Builder['Files']>[0]>, 'minSize' | 'maxSize'> & { minSize?: ByteSize | undefined, maxSize?: ByteSize | undefined }
309
+
310
+ const File = (options?: OldFileOptions): ReturnType<Builder['File']> => schema.File(sized(options, 's.File'))
311
+ const Files = (options?: OldFilesOptions): ReturnType<Builder['Files']> => schema.Files(sized(options, 's.Files'))
312
+
313
+ // kay's schema builders, so an action's schema needs no import of its own, over TypeBox, so a schema stays JSON Schema data for OpenAPI, MCP and the form rules.
314
+ // The PascalCase builders, `s.Object`, `s.Optional`, `s.UnionKeys`, `s.Numeric` and the rest, are Elysia's under their old names and stay until 2.0: the `deprecated-schema-builders` lint rewrites the ones kay has a builder for.
71
315
  // kept: free
72
- export const s: Omit<Builder, 'File' | 'Files'> & { File: typeof File, Files: typeof Files, Secret: typeof Secret, Public: typeof Public, UnionKeys: typeof UnionKeys, readonly typebox: Builder } = { ...schema, UnionEnum, File, Files, Secret, Public, UnionKeys, typebox: schema }
316
+ export const s: Omit<Builder, 'File' | 'Files' | 'UnionEnum' | 'Secret'> & {
317
+ UnionEnum: typeof UnionEnum
318
+ UnionKeys: typeof UnionKeys
319
+ File: typeof File
320
+ Files: typeof Files
321
+ Secret: typeof Secret
322
+ Public: typeof publicField
323
+ string: typeof string
324
+ email: typeof email
325
+ url: typeof url
326
+ slug: typeof slug
327
+ int: typeof int
328
+ number: typeof number
329
+ boolean: typeof boolean
330
+ date: typeof date
331
+ enum: typeof enumeration
332
+ literal: typeof literal
333
+ union: typeof union
334
+ object: typeof object
335
+ record: typeof record
336
+ list: typeof list
337
+ file: typeof file
338
+ files: typeof files
339
+ secret: typeof secret
340
+ public: typeof publicField
341
+ } = {
342
+ ...schema,
343
+ UnionEnum,
344
+ UnionKeys,
345
+ File,
346
+ Files,
347
+ Secret,
348
+ Public: publicField,
349
+ string,
350
+ email,
351
+ url,
352
+ slug,
353
+ int,
354
+ number,
355
+ boolean,
356
+ date,
357
+ enum: enumeration,
358
+ literal,
359
+ union,
360
+ object,
361
+ record,
362
+ list,
363
+ file,
364
+ files,
365
+ secret,
366
+ public: publicField,
367
+ }
@@ -4,6 +4,17 @@ import { announce } from './announce'
4
4
  import { HYDRATED, ISLAND } from './markers'
5
5
  import { hashTarget } from './navigation'
6
6
 
7
+ const WATCH_BOUND_MS = 30_000
8
+ // What ends each pending replay of kept keys, and the fields whose island was told them, so it hears them once.
9
+ const replaying = new Set<() => void>()
10
+ const told = new WeakSet<Element>()
11
+
12
+ type Island = HTMLElement & { _$k?: boolean }
13
+ type Field = HTMLInputElement | HTMLTextAreaElement
14
+ interface Typed { value: string, start: number | null, end: number | null, direction: 'forward' | 'backward' | 'none', replay?: boolean }
15
+
16
+ const noteOf = (field: Field): Typed => ({ value: field.value, start: field.selectionStart, end: field.selectionEnd, direction: field.selectionDirection ?? 'none' })
17
+
7
18
  function byName(element: Element | null): Element | null {
8
19
  const name = element?.getAttribute('name') ?? ''
9
20
 
@@ -12,6 +23,10 @@ function byName(element: Element | null): Element | null {
12
23
 
13
24
  // The element focused before, found again on a refreshed page or on Back and Forward between entries of one path; on a new page, the element its hash points at, else its autofocus field, its heading or its main, so a screen reader starts there, the title announced. Back and Forward skip the autofocus field, which the visitor left already, and a position an entry saved leaves the hash alone, as it does the scroll.
14
25
  export async function moveFocus(swap: Swap, policy: Policy, hash: string): Promise<void> {
26
+ // The replays of an older swap's keys end here, as this swap's own are registered after.
27
+ for (const end of replaying)
28
+ end()
29
+
15
30
  if (policy.focus === 'refused' && swap.refused && await showRefusal(document.forms[swap.refused.form], swap.refused.control))
16
31
  return
17
32
 
@@ -55,16 +70,55 @@ function carryCaret(from: Element | null, to: HTMLElement, held: string | undefi
55
70
  if (!(from instanceof HTMLInputElement || from instanceof HTMLTextAreaElement) || !(to instanceof HTMLInputElement || to instanceof HTMLTextAreaElement))
56
71
  return
57
72
 
58
- const { selectionStart, selectionEnd, selectionDirection, value } = from
73
+ const { value } = from
59
74
 
60
- // Keys typed while the page was fetched are kept, and the island is told so it asks for them next.
75
+ // Keys typed while the page was fetched are kept, and the island is told so it asks for them next, once it has hydrated: its own write of the value it was given (kayjs-ui's TextInput) would otherwise land over them.
61
76
  const typed = held !== undefined && value !== held && value !== to.value
62
77
 
78
+ const island = typed ? to.closest<Island>(ISLAND) : null
79
+ const waits = island !== null && !island._$k
80
+
63
81
  if (typed) {
64
82
  to.value = value
65
- to.dispatchEvent(new InputEvent('input', { bubbles: true }))
83
+
84
+ if (!waits)
85
+ to.dispatchEvent(new InputEvent('input', { bubbles: true }))
86
+ }
87
+
88
+ placeCaret(from, to)
89
+
90
+ if (waits)
91
+ replayOnHydrate(to, island)
92
+ }
93
+
94
+ // The island hears the kept keys once it has hydrated, by this field's own listener so that no path of the swap can drop them: the text is put back if hydration wrote the server's over it, and `input` fires either way. The latest text typed meanwhile is the one kept, and the bound of 30 s ends the wait for an island that never hydrates.
95
+ function replayOnHydrate(field: Field, island: Island): void {
96
+ let typed = noteOf(field)
97
+ const heard = new AbortController()
98
+ let timer: ReturnType<typeof setTimeout> | undefined
99
+ const end = () => {
100
+ clearTimeout(timer)
101
+ heard.abort()
102
+ replaying.delete(end)
66
103
  }
67
104
 
105
+ timer = setTimeout(end, WATCH_BOUND_MS)
106
+ replaying.add(end)
107
+ island.addEventListener('input', (event) => {
108
+ if (event.target === field)
109
+ typed = noteOf(field)
110
+ }, { signal: heard.signal, capture: true })
111
+ island.addEventListener(HYDRATED, () => {
112
+ end()
113
+
114
+ if (island.isConnected)
115
+ putBack(found(field), { ...typed, replay: true })
116
+ }, { signal: heard.signal, once: true })
117
+ }
118
+
119
+ function placeCaret(from: Field, to: Field): void {
120
+ const { selectionStart, selectionEnd, selectionDirection, value } = from
121
+
68
122
  // A number or email field has no selection, so no caret.
69
123
  if (selectionStart === null || selectionEnd === null || to.selectionStart === null)
70
124
  return
@@ -101,21 +155,15 @@ async function showRefusal(form: HTMLFormElement | undefined, control: number):
101
155
  return refusal.showRefusal(form, control, announce)
102
156
  }
103
157
 
104
- const WATCH_BOUND_MS = 30_000
105
158
  // What ends each watch, so a newer swap leaves the ones before it restoring nothing.
106
159
  const watching = new Set<() => void>()
107
160
 
108
- type Island = HTMLElement & { _$k?: boolean }
109
- type Field = HTMLInputElement | HTMLTextAreaElement
110
- interface Typed { value: string, start: number | null, end: number | null, direction: 'forward' | 'backward' | 'none' }
111
-
112
- const noteOf = (field: Field): Typed => ({ value: field.value, start: field.selectionStart, end: field.selectionEnd, direction: field.selectionDirection ?? 'none' })
113
-
114
161
  // Hydration kept what was typed when the field already holds it, and the island heard it through the events queued for it. A number or email field has no selection, so no caret.
115
162
  function putBack(now: Element | null, typed: Typed | undefined): void {
116
- if (!typed || !(now instanceof HTMLInputElement || now instanceof HTMLTextAreaElement) || now.value === typed.value)
163
+ if (!typed || !(now instanceof HTMLInputElement || now instanceof HTMLTextAreaElement) || (now.value === typed.value && !(typed.replay && !told.has(now))))
117
164
  return
118
165
 
166
+ told.add(now)
119
167
  now.value = typed.value
120
168
  now.dispatchEvent(new InputEvent('input', { bubbles: true }))
121
169
 
@@ -0,0 +1,16 @@
1
+ import { importModule } from './bundle'
2
+
3
+ // The exports of a page file. With `skipBroken`, a file that fails to compile has none, and its failure goes into `broken` for the checks naming one of its actions.
4
+ export async function actionModule(file: string, skipBroken: boolean | undefined, broken: Map<string, string>): Promise<Record<string, unknown>> {
5
+ try {
6
+ return await importModule<Record<string, unknown>>(file)
7
+ }
8
+ catch (error) {
9
+ if (skipBroken !== true || !(error instanceof Error) || error.name !== 'KayCompileError')
10
+ throw error
11
+
12
+ broken.set(file, error.message)
13
+
14
+ return {}
15
+ }
16
+ }
@@ -35,8 +35,8 @@ import { actionClash } from './action-clash'
35
35
  import { hooksOf, refusedBeforeRun, tellError, tellSuccess, withActionDefaults } from './action-hooks'
36
36
  import { withFixedFields, withoutEmptyFiles } from './action-input'
37
37
  import { gathering, isAsyncIterable, lastOf, started, streamed } from './action-iterable'
38
+ import { actionModule } from './action-module'
38
39
  import { routerPath } from './base'
39
- import { importModule } from './bundle'
40
40
  import { timeoutMs } from './config-limits'
41
41
  import { cookieJar } from './cookies'
42
42
  import { BACK_PARAMETER, crossSiteResponse, isCrossSite, postedFrom, TEXT } from './cross-site'
@@ -138,24 +138,16 @@ type Back = (request: Request, headers: Headers, flash: Flash, server: Server |
138
138
  const pageBound = new WeakMap<object, boolean>()
139
139
 
140
140
  // Registers every action the pages and layouts export as a POST route whose body Elysia validates against the action's schema, giving back the body limit of each by its path and the actions that opted in to being MCP tools.
141
- export async function registerActionRoutes(app: Elysia, routes: ServedRoute[], match: RouteMatch, options: ActionOptions): Promise<{ limits: Map<string, BodyLimit>, tools: DocumentedAction[] }> {
141
+ export async function registerActionRoutes(app: Elysia, routes: ServedRoute[], match: RouteMatch, options: ActionOptions): Promise<{ limits: Map<string, BodyLimit>, tools: DocumentedAction[], broken: Map<string, string> }> {
142
142
  const back: Back = async (request, headers, flash, server) => renderBack(app, options.base ?? '', request, headers, flash, server)
143
143
  const served = new Map<string, { file: string, action: ServerAction<TSchema, unknown> }>()
144
144
  const limits = new Map<string, BodyLimit>()
145
145
  const tools: DocumentedAction[] = []
146
+ // The files `skipBroken` passed over, with why.
147
+ const broken = new Map<string, string>()
146
148
 
147
149
  for (const [file, { guards, ...page }] of actionFiles(routes, options.requireGuards)) {
148
- let module: Record<string, unknown> = {}
149
-
150
- try {
151
- module = await importModule<Record<string, unknown>>(file)
152
- }
153
- catch (error) {
154
- if (options.skipBroken !== true || !(error instanceof Error) || error.name !== 'KayCompileError')
155
- throw error
156
- }
157
-
158
- for (const [name, exported] of Object.entries(module)) {
150
+ for (const [name, exported] of Object.entries(await actionModule(file, options.skipBroken, broken))) {
159
151
  if (!isAction(exported))
160
152
  continue
161
153
 
@@ -282,7 +274,7 @@ export async function registerActionRoutes(app: Elysia, routes: ServedRoute[], m
282
274
  }
283
275
  }
284
276
 
285
- return { limits, tools }
277
+ return { limits, tools, broken }
286
278
  }
287
279
 
288
280
  // Runs the action under its timeout; one that ran out is reported as a server error and answered as a failed post, 504.
@@ -46,7 +46,7 @@ export const REFERENCE_PATH = `${API_PREFIX}/reference`
46
46
  export function apiPathOf(value: string | undefined): string {
47
47
  return value === undefined ? API_PREFIX : servedPath(value, 'endpoints.api')
48
48
  }
49
- // Where `mcp` serves the app's tools, and where it did before 1.105, which redirects there until 1.108.
49
+ // Where `mcp` serves the app's tools, and where it did before 1.105, which is answered in place until 2.0.
50
50
  export const MCP_PATH = '/_kay/mcp'
51
51
  export const FORMER_MCP_PATH = '/mcp'
52
52
  // Where an MCP client finds how to sign in, which the endpoint's 401 points to.
package/src/server/api.ts CHANGED
@@ -67,6 +67,18 @@ const TIMEOUT = Symbol.for('kay.timeout')
67
67
  const INPUTS = ['body', 'query', 'params', 'headers', 'cookie']
68
68
  const BODY_PREFIX = /^body\./
69
69
 
70
+ // Elysia reads the whole jar of a request against the `cookie` schema, where the query and the headers it strips, so a strict one would refuse every cookie a third party sets.
71
+ function inputSchema(input: string, schema: TSchema): TSchema {
72
+ if (input !== 'cookie' || !('additionalProperties' in schema) || schema.additionalProperties !== false)
73
+ return schema
74
+
75
+ const copy = { ...schema } as Record<PropertyKey, unknown>
76
+
77
+ delete copy.additionalProperties
78
+
79
+ return copy as unknown as TSchema
80
+ }
81
+
70
82
  // The requests the guards of their module let through, whose later errors are no refused input.
71
83
  const admitted = new WeakSet<Request>()
72
84
 
@@ -152,7 +164,7 @@ export async function serveApi(app: Elysia, modules: ApiModule[], options: Actio
152
164
  const schema = (hooks as Record<string, unknown> | undefined)?.[input]
153
165
 
154
166
  if (route.method !== 'WS' && typeof schema === 'object' && schema !== null && !('~standard' in schema))
155
- (hooks as Record<string, unknown>)[input] = validated(schema as TSchema)
167
+ (hooks as Record<string, unknown>)[input] = validated(inputSchema(input, schema as TSchema))
156
168
  }
157
169
 
158
170
  // Whichever way the module chains its `.ws()` (in a group, a guard or a used plugin), every websocket route is held to its limits.