@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.
- package/README.md +2 -2
- package/dist/eslint.js +777 -210
- package/editors/zed/extension.toml +2 -2
- package/llms/action-protection.md +2 -2
- package/llms/actions-from-islands.md +2 -2
- package/llms/actions-refused-fields.md +22 -0
- package/llms/actions.md +24 -41
- package/llms/analytics.md +1 -1
- package/llms/api-from-islands.md +1 -1
- package/llms/api-routes.md +6 -6
- package/llms/browser-only.md +1 -1
- package/llms/check-and-tools.md +1 -1
- package/llms/code-lints.md +2 -2
- package/llms/compiler-api.md +1 -1
- package/llms/component-libraries.md +1 -0
- package/llms/components.md +1 -1
- package/llms/content-cms.md +1 -1
- package/llms/content-markdown-to-caching.md +1 -1
- package/llms/content-preview.md +2 -2
- package/llms/content.md +21 -21
- package/llms/deprecated-lints-1-105-libraries.md +2 -0
- package/llms/deprecated-lints-1-105.md +3 -3
- package/llms/deprecated-lints.md +4 -4
- package/llms/dev-tool.md +2 -2
- package/llms/environment.md +22 -22
- package/llms/files.md +5 -5
- package/llms/island-and-form-lints.md +1 -0
- package/llms/islands-state-to-ids.md +2 -1
- package/llms/islands.md +10 -1
- package/llms/local-collections.md +2 -2
- package/llms/mcp.md +2 -1
- package/llms/navigation-from-code.md +1 -1
- package/llms/notifications.md +4 -2
- package/llms/project-new-and-add.md +1 -1
- package/llms/project.md +3 -3
- package/llms/rate-limits.md +1 -1
- package/llms/recipes.md +16 -16
- package/llms/rooms.md +4 -4
- package/llms/routing-endpoints-and-base.md +1 -1
- package/llms/routing.md +5 -5
- package/llms/schemas.md +14 -7
- package/llms/server.md +7 -7
- package/llms/site.md +9 -9
- package/llms/sitemap-search-fonts.md +2 -0
- package/llms/styles-and-assets.md +2 -1
- package/llms/templates-elements-slots-snippets.md +2 -2
- package/llms/testing-browser.md +20 -0
- package/llms/testing.md +1 -1
- package/llms/upgrading-1-105-config.md +2 -2
- package/llms/upgrading.md +37 -4
- package/llms/whats-new-archive.md +35 -66
- package/llms/whats-new.md +47 -28
- package/llms.txt +1 -1
- package/package.json +24 -11
- package/src/add.ts +1 -1
- package/src/build/class-table.ts +2 -2
- package/src/build/client.ts +27 -30
- package/src/build/entry-files.ts +19 -0
- package/src/build/lazy-components.ts +130 -0
- package/src/build/lazy-manifest.ts +56 -0
- package/src/build/lazy-stubs.ts +31 -0
- package/src/build/plugin.ts +16 -11
- package/src/build/runtime-chunk.ts +1 -1
- package/src/build/styles.ts +3 -3
- package/src/build/transform.ts +19 -7
- package/src/build/ui-layers.ts +30 -0
- package/src/build/ui.ts +108 -11
- package/src/build/variants.ts +125 -46
- package/src/build/worker-bundle.ts +6 -0
- package/src/check/action-options.ts +56 -0
- package/src/check/boot.ts +22 -1
- package/src/check/call-result.ts +242 -0
- package/src/check/config-objects.ts +17 -2
- package/src/check/diagnose.ts +22 -0
- package/src/check/handler-events.ts +149 -0
- package/src/check/index.ts +6 -2
- package/src/check/link-flag-tests.ts +130 -0
- package/src/check/project.ts +4 -3
- package/src/check/refusal-calls.ts +2 -2
- package/src/check/regroup.ts +5 -3
- package/src/check/renamed-packages.ts +58 -14
- package/src/check/schema-builders.ts +256 -0
- package/src/check/schema-json.ts +226 -0
- package/src/check/secret-keys.ts +15 -2
- package/src/check/shadow-types.ts +13 -9
- package/src/check/template-hints.ts +1 -1
- package/src/check/tool-labels.ts +11 -9
- package/src/check/ui-types.ts +4 -3
- package/src/check/ui.ts +14 -11
- package/src/commands/env-declare.ts +18 -6
- package/src/commands/routes.ts +1 -1
- package/src/compiler/a11y.ts +8 -1
- package/src/compiler/ast.ts +1 -1
- package/src/compiler/bound-props.ts +1 -1
- package/src/compiler/codegen.ts +13 -23
- package/src/compiler/deprecated-lints-1-105.ts +41 -0
- package/src/compiler/deprecated-lints-1-106.ts +14 -0
- package/src/compiler/deprecated-lints-1-107.ts +14 -0
- package/src/compiler/deprecated-lints.ts +17 -60
- package/src/compiler/deprecated-typed-lints.ts +23 -0
- package/src/compiler/dom-here.ts +72 -0
- package/src/compiler/dom.ts +23 -23
- package/src/compiler/fence-checks.ts +12 -11
- package/src/compiler/field-described.ts +2 -2
- package/src/compiler/helper-imports.ts +1 -1
- package/src/compiler/html-fields.ts +92 -0
- package/src/compiler/html-initial.ts +5 -3
- package/src/compiler/html.ts +14 -31
- package/src/compiler/import-edits.ts +15 -0
- package/src/compiler/index.ts +10 -3
- package/src/compiler/island-declaration.ts +15 -0
- package/src/compiler/items.ts +5 -5
- package/src/compiler/kay-exports.ts +1 -0
- package/src/compiler/lazy-imports.ts +357 -0
- package/src/compiler/link-flags.ts +6 -5
- package/src/compiler/lints.ts +12 -12
- package/src/compiler/parser.ts +2 -2
- package/src/compiler/rest-omits.ts +15 -0
- package/src/compiler/route-lints.ts +2 -0
- package/src/compiler/schema-lints.ts +13 -0
- package/src/compiler/script-server.ts +6 -3
- package/src/config-loader.ts +10 -2
- package/src/config-resolve.ts +3 -1
- package/src/config-shape.ts +2 -2
- package/src/config.ts +20 -4
- package/src/content/markdown.ts +2 -3
- package/src/content/validate.ts +1 -1
- package/src/generate-resource.ts +9 -9
- package/src/generate.ts +1 -1
- package/src/mcp/guards.ts +1 -1
- package/src/mcp/tools.ts +1 -1
- package/src/migrate/content.ts +61 -55
- package/src/new.ts +6 -2
- package/src/runtime/action-shared.ts +2 -2
- package/src/runtime/action.ts +51 -4
- package/src/runtime/client.ts +10 -0
- package/src/runtime/dom-lazy.ts +276 -0
- package/src/runtime/field-errors.ts +58 -0
- package/src/runtime/field-refusal.ts +179 -0
- package/src/runtime/form-types.ts +99 -0
- package/src/runtime/form.ts +25 -9
- package/src/runtime/html.ts +42 -6
- package/src/runtime/index.ts +2 -0
- package/src/runtime/island.server.ts +36 -2
- package/src/runtime/markdown-autolink.ts +69 -0
- package/src/runtime/markdown-render.client.ts +39 -3
- package/src/runtime/markdown.client.ts +6 -3
- package/src/runtime/markdown.server.ts +6 -3
- package/src/runtime/markdown.ts +125 -1
- package/src/runtime/navigate-api.ts +10 -5
- package/src/runtime/persist.ts +7 -7
- package/src/runtime/query-param.ts +1 -1
- package/src/runtime/reactive.ts +14 -0
- package/src/runtime/sanitize.ts +3 -1
- package/src/runtime/schema-record.ts +32 -0
- package/src/runtime/schema.client.ts +101 -14
- package/src/runtime/schema.ts +328 -26
- package/src/runtime/standard-schema.ts +32 -0
- package/src/runtime/swap-focus.ts +59 -11
- package/src/runtime/validate-run.client.ts +179 -0
- package/src/runtime/validate.client.ts +11 -0
- package/src/runtime/validate.ts +4 -137
- package/src/server/action-module.ts +16 -0
- package/src/server/actions.ts +54 -57
- package/src/server/answer.ts +102 -0
- package/src/server/api-input.ts +166 -0
- package/src/server/api-modules.ts +1 -1
- package/src/server/api.ts +12 -32
- package/src/server/app-stages.ts +38 -26
- package/src/server/app.ts +5 -2
- package/src/server/client.ts +2 -0
- package/src/server/dev-ui.ts +2 -2
- package/src/server/dev.ts +7 -9
- package/src/server/env-state.ts +1 -1
- package/src/server/env.ts +11 -3
- package/src/server/favicon.ts +47 -1
- package/src/server/file-limit.ts +7 -1
- package/src/server/forms.ts +1 -1
- package/src/server/host-allowlist.ts +7 -7
- package/src/server/image-metadata-bmff.ts +1 -1
- package/src/server/index.ts +2 -0
- package/src/server/kay-routes.ts +221 -0
- package/src/server/kay-server.ts +228 -0
- package/src/server/kept-exports.ts +1 -0
- package/src/server/kept-pages.ts +3 -3
- package/src/server/library.ts +2 -2
- package/src/server/mcp.ts +14 -13
- package/src/server/notifications.ts +4 -2
- package/src/server/offline.ts +24 -7
- package/src/server/openapi.ts +12 -3
- package/src/server/phases.ts +249 -0
- package/src/server/protection.ts +6 -6
- package/src/server/proxy-key.ts +8 -8
- package/src/server/push-settings.ts +16 -2
- package/src/server/push.ts +12 -4
- package/src/server/query.ts +17 -31
- package/src/server/receive.ts +43 -0
- package/src/server/render-document.ts +13 -3
- package/src/server/render.ts +1 -1
- package/src/server/request-context.ts +137 -0
- package/src/server/schema-check.ts +1 -1
- package/src/server/scope.ts +20 -13
- package/src/server/signals.ts +4 -4
- package/src/server/socket.ts +19 -47
- package/src/server/stream.ts +1 -1
- package/src/server/validate.ts +346 -0
- package/src/template-ws.ts +1 -1
- package/src/test/a11y-dom.ts +70 -0
- package/src/test/html-snapshot.ts +103 -0
- package/src/test/index.ts +2 -1
- package/src/test/mount.ts +14 -3
- package/src/test/page-checks.ts +12 -0
- package/src/test/screenshot-match.ts +2 -2
- package/src/test/shared-modules.ts +27 -5
- package/src/test/site-types.ts +5 -1
- package/types/build/class-table.d.ts +1 -1
- package/types/build/class-table.d.ts.map +1 -1
- package/types/build/client.d.ts +3 -2
- package/types/build/client.d.ts.map +1 -1
- package/types/build/entry-files.d.ts +4 -0
- package/types/build/entry-files.d.ts.map +1 -0
- package/types/build/lazy-components.d.ts +6 -0
- package/types/build/lazy-components.d.ts.map +1 -0
- package/types/build/lazy-manifest.d.ts +19 -0
- package/types/build/lazy-manifest.d.ts.map +1 -0
- package/types/build/lazy-stubs.d.ts +5 -0
- package/types/build/lazy-stubs.d.ts.map +1 -0
- package/types/build/plugin.d.ts +1 -0
- package/types/build/plugin.d.ts.map +1 -1
- package/types/build/transform.d.ts +4 -1
- package/types/build/transform.d.ts.map +1 -1
- package/types/build/ui-layers.d.ts +5 -0
- package/types/build/ui-layers.d.ts.map +1 -0
- package/types/build/ui.d.ts +7 -0
- package/types/build/ui.d.ts.map +1 -1
- package/types/build/variants.d.ts +1 -1
- package/types/build/variants.d.ts.map +1 -1
- package/types/build/worker-bundle.d.ts +1 -0
- package/types/build/worker-bundle.d.ts.map +1 -1
- package/types/check/action-options.d.ts +3 -0
- package/types/check/action-options.d.ts.map +1 -0
- package/types/check/boot.d.ts.map +1 -1
- package/types/check/call-result.d.ts +7 -0
- package/types/check/call-result.d.ts.map +1 -0
- package/types/check/config-objects.d.ts.map +1 -1
- package/types/check/diagnose.d.ts.map +1 -1
- package/types/check/handler-events.d.ts +9 -0
- package/types/check/handler-events.d.ts.map +1 -0
- package/types/check/index.d.ts +1 -0
- package/types/check/index.d.ts.map +1 -1
- package/types/check/link-flag-tests.d.ts +4 -0
- package/types/check/link-flag-tests.d.ts.map +1 -0
- package/types/check/project.d.ts.map +1 -1
- package/types/check/regroup.d.ts +1 -1
- package/types/check/regroup.d.ts.map +1 -1
- package/types/check/renamed-packages.d.ts +1 -0
- package/types/check/renamed-packages.d.ts.map +1 -1
- package/types/check/schema-builders.d.ts +3 -0
- package/types/check/schema-builders.d.ts.map +1 -0
- package/types/check/schema-json.d.ts +3 -0
- package/types/check/schema-json.d.ts.map +1 -0
- package/types/check/secret-keys.d.ts.map +1 -1
- package/types/check/shadow-types.d.ts.map +1 -1
- package/types/check/template-hints.d.ts +1 -0
- package/types/check/template-hints.d.ts.map +1 -1
- package/types/check/tool-labels.d.ts.map +1 -1
- package/types/check/ui-types.d.ts.map +1 -1
- package/types/check/ui.d.ts.map +1 -1
- package/types/commands/env-declare.d.ts.map +1 -1
- package/types/compiler/a11y.d.ts.map +1 -1
- package/types/compiler/bound-props.d.ts +1 -0
- package/types/compiler/bound-props.d.ts.map +1 -1
- package/types/compiler/codegen.d.ts +3 -0
- package/types/compiler/codegen.d.ts.map +1 -1
- package/types/compiler/deprecated-lints-1-105.d.ts +39 -0
- package/types/compiler/deprecated-lints-1-105.d.ts.map +1 -0
- package/types/compiler/deprecated-lints-1-106.d.ts +12 -0
- package/types/compiler/deprecated-lints-1-106.d.ts.map +1 -0
- package/types/compiler/deprecated-lints-1-107.d.ts +12 -0
- package/types/compiler/deprecated-lints-1-107.d.ts.map +1 -0
- package/types/compiler/deprecated-lints.d.ts +41 -22
- package/types/compiler/deprecated-lints.d.ts.map +1 -1
- package/types/compiler/deprecated-typed-lints.d.ts +21 -0
- package/types/compiler/deprecated-typed-lints.d.ts.map +1 -0
- package/types/compiler/dom-here.d.ts +14 -0
- package/types/compiler/dom-here.d.ts.map +1 -0
- package/types/compiler/dom.d.ts +1 -0
- package/types/compiler/dom.d.ts.map +1 -1
- package/types/compiler/fence-checks.d.ts.map +1 -1
- package/types/compiler/field-described.d.ts +1 -1
- package/types/compiler/field-described.d.ts.map +1 -1
- package/types/compiler/html-fields.d.ts +19 -0
- package/types/compiler/html-fields.d.ts.map +1 -0
- package/types/compiler/html-initial.d.ts +3 -2
- package/types/compiler/html-initial.d.ts.map +1 -1
- package/types/compiler/html.d.ts.map +1 -1
- package/types/compiler/import-edits.d.ts +1 -0
- package/types/compiler/import-edits.d.ts.map +1 -1
- package/types/compiler/index.d.ts +3 -0
- package/types/compiler/index.d.ts.map +1 -1
- package/types/compiler/island-declaration.d.ts +5 -0
- package/types/compiler/island-declaration.d.ts.map +1 -1
- package/types/compiler/items.d.ts +4 -1
- package/types/compiler/items.d.ts.map +1 -1
- package/types/compiler/kay-exports.d.ts.map +1 -1
- package/types/compiler/lazy-imports.d.ts +33 -0
- package/types/compiler/lazy-imports.d.ts.map +1 -0
- package/types/compiler/link-flags.d.ts +6 -0
- package/types/compiler/link-flags.d.ts.map +1 -1
- package/types/compiler/lints.d.ts +61 -34
- package/types/compiler/lints.d.ts.map +1 -1
- package/types/compiler/rest-omits.d.ts +4 -0
- package/types/compiler/rest-omits.d.ts.map +1 -0
- package/types/compiler/route-lints.d.ts +8 -0
- package/types/compiler/route-lints.d.ts.map +1 -1
- package/types/compiler/schema-lints.d.ts +11 -0
- package/types/compiler/schema-lints.d.ts.map +1 -0
- package/types/compiler/script-server.d.ts.map +1 -1
- package/types/config-loader.d.ts.map +1 -1
- package/types/config-resolve.d.ts.map +1 -1
- package/types/config.d.ts +15 -2
- package/types/config.d.ts.map +1 -1
- package/types/content/markdown.d.ts.map +1 -1
- package/types/migrate/content.d.ts.map +1 -1
- package/types/new.d.ts +1 -0
- package/types/new.d.ts.map +1 -1
- package/types/runtime/action-shared.d.ts +1 -1
- package/types/runtime/action-shared.d.ts.map +1 -1
- package/types/runtime/action.d.ts +29 -1
- package/types/runtime/action.d.ts.map +1 -1
- package/types/runtime/client.d.ts.map +1 -1
- package/types/runtime/dom-lazy.d.ts +6 -0
- package/types/runtime/dom-lazy.d.ts.map +1 -0
- package/types/runtime/field-errors.d.ts +4 -0
- package/types/runtime/field-errors.d.ts.map +1 -0
- package/types/runtime/field-refusal.d.ts +37 -0
- package/types/runtime/field-refusal.d.ts.map +1 -0
- package/types/runtime/form-types.d.ts +6 -0
- package/types/runtime/form-types.d.ts.map +1 -0
- package/types/runtime/form.d.ts +4 -2
- package/types/runtime/form.d.ts.map +1 -1
- package/types/runtime/html.d.ts +3 -0
- package/types/runtime/html.d.ts.map +1 -1
- package/types/runtime/index.d.ts +2 -0
- package/types/runtime/index.d.ts.map +1 -1
- package/types/runtime/island.server.d.ts +2 -0
- package/types/runtime/island.server.d.ts.map +1 -1
- package/types/runtime/markdown-autolink.d.ts +11 -0
- package/types/runtime/markdown-autolink.d.ts.map +1 -0
- package/types/runtime/markdown-render.client.d.ts +2 -1
- package/types/runtime/markdown-render.client.d.ts.map +1 -1
- package/types/runtime/markdown.client.d.ts +3 -1
- package/types/runtime/markdown.client.d.ts.map +1 -1
- package/types/runtime/markdown.d.ts +11 -0
- package/types/runtime/markdown.d.ts.map +1 -1
- package/types/runtime/markdown.server.d.ts +3 -1
- package/types/runtime/markdown.server.d.ts.map +1 -1
- package/types/runtime/navigate-api.d.ts +1 -1
- package/types/runtime/navigate-api.d.ts.map +1 -1
- package/types/runtime/persist.d.ts +2 -2
- package/types/runtime/persist.d.ts.map +1 -1
- package/types/runtime/reactive.d.ts +3 -0
- package/types/runtime/reactive.d.ts.map +1 -1
- package/types/runtime/sanitize.d.ts.map +1 -1
- package/types/runtime/schema-record.d.ts +9 -0
- package/types/runtime/schema-record.d.ts.map +1 -0
- package/types/runtime/schema.client.d.ts.map +1 -1
- package/types/runtime/schema.d.ts +99 -10
- package/types/runtime/schema.d.ts.map +1 -1
- package/types/runtime/standard-schema.d.ts +37 -0
- package/types/runtime/standard-schema.d.ts.map +1 -0
- package/types/runtime/swap-focus.d.ts.map +1 -1
- package/types/runtime/validate-run.client.d.ts +4 -0
- package/types/runtime/validate-run.client.d.ts.map +1 -0
- package/types/runtime/validate.client.d.ts +4 -0
- package/types/runtime/validate.client.d.ts.map +1 -0
- package/types/runtime/validate.d.ts +1 -21
- package/types/runtime/validate.d.ts.map +1 -1
- package/types/server/action-module.d.ts +2 -0
- package/types/server/action-module.d.ts.map +1 -0
- package/types/server/actions.d.ts +1 -0
- package/types/server/actions.d.ts.map +1 -1
- package/types/server/answer.d.ts +6 -0
- package/types/server/answer.d.ts.map +1 -0
- package/types/server/api-input.d.ts +19 -0
- package/types/server/api-input.d.ts.map +1 -0
- package/types/server/api.d.ts.map +1 -1
- package/types/server/app-stages.d.ts +7 -3
- package/types/server/app-stages.d.ts.map +1 -1
- package/types/server/app.d.ts +2 -0
- package/types/server/app.d.ts.map +1 -1
- package/types/server/client.d.ts +1 -0
- package/types/server/client.d.ts.map +1 -1
- package/types/server/dev.d.ts +1 -1
- package/types/server/dev.d.ts.map +1 -1
- package/types/server/env-state.d.ts.map +1 -1
- package/types/server/env.d.ts +5 -1
- package/types/server/env.d.ts.map +1 -1
- package/types/server/favicon.d.ts +3 -1
- package/types/server/favicon.d.ts.map +1 -1
- package/types/server/file-limit.d.ts.map +1 -1
- package/types/server/host-allowlist.d.ts +3 -3
- package/types/server/host-allowlist.d.ts.map +1 -1
- package/types/server/index.d.ts +2 -0
- package/types/server/index.d.ts.map +1 -1
- package/types/server/kay-routes.d.ts +47 -0
- package/types/server/kay-routes.d.ts.map +1 -0
- package/types/server/kay-server.d.ts +25 -0
- package/types/server/kay-server.d.ts.map +1 -0
- package/types/server/kept-exports.d.ts.map +1 -1
- package/types/server/kept-pages.d.ts +1 -1
- package/types/server/kept-pages.d.ts.map +1 -1
- package/types/server/library.d.ts +2 -1
- package/types/server/library.d.ts.map +1 -1
- package/types/server/mcp.d.ts +1 -1
- package/types/server/mcp.d.ts.map +1 -1
- package/types/server/notifications.d.ts +2 -1
- package/types/server/notifications.d.ts.map +1 -1
- package/types/server/offline.d.ts.map +1 -1
- package/types/server/openapi.d.ts.map +1 -1
- package/types/server/phases.d.ts +36 -0
- package/types/server/phases.d.ts.map +1 -0
- package/types/server/protection.d.ts +3 -2
- package/types/server/protection.d.ts.map +1 -1
- package/types/server/proxy-key.d.ts +3 -3
- package/types/server/proxy-key.d.ts.map +1 -1
- package/types/server/push-settings.d.ts +1 -1
- package/types/server/push-settings.d.ts.map +1 -1
- package/types/server/push.d.ts +1 -0
- package/types/server/push.d.ts.map +1 -1
- package/types/server/query.d.ts.map +1 -1
- package/types/server/receive.d.ts +9 -0
- package/types/server/receive.d.ts.map +1 -0
- package/types/server/render-document.d.ts.map +1 -1
- package/types/server/request-context.d.ts +29 -0
- package/types/server/request-context.d.ts.map +1 -0
- package/types/server/schema-check.d.ts +1 -0
- package/types/server/schema-check.d.ts.map +1 -1
- package/types/server/scope.d.ts +1 -1
- package/types/server/scope.d.ts.map +1 -1
- package/types/server/socket.d.ts +3 -5
- package/types/server/socket.d.ts.map +1 -1
- package/types/server/validate.d.ts +26 -0
- package/types/server/validate.d.ts.map +1 -0
- package/types/test/a11y-dom.d.ts +20 -0
- package/types/test/a11y-dom.d.ts.map +1 -0
- package/types/test/html-snapshot.d.ts +4 -0
- package/types/test/html-snapshot.d.ts.map +1 -0
- package/types/test/index.d.ts.map +1 -1
- package/types/test/mount.d.ts +3 -0
- package/types/test/mount.d.ts.map +1 -1
- package/types/test/page-checks.d.ts +6 -0
- package/types/test/page-checks.d.ts.map +1 -0
- package/types/test/screenshot-match.d.ts +1 -1
- package/types/test/screenshot-match.d.ts.map +1 -1
- package/types/test/shared-modules.d.ts.map +1 -1
- package/types/test/site-types.d.ts +3 -1
- package/types/test/site-types.d.ts.map +1 -1
- package/src/check/schema-type.ts +0 -9
- package/src/compiler/deprecated-script-fence.ts +0 -26
- package/src/server/field-errors.ts +0 -136
- package/types/check/schema-type.d.ts +0 -3
- package/types/check/schema-type.d.ts.map +0 -1
- package/types/compiler/deprecated-script-fence.d.ts +0 -3
- package/types/compiler/deprecated-script-fence.d.ts.map +0 -1
- package/types/server/field-errors.d.ts +0 -6
- package/types/server/field-errors.d.ts.map +0 -1
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import type { Issue, Refusal, Source, Validated, ValidateOptions } from '../server/validate'
|
|
2
|
+
import type { FieldRules } from './field-refusal'
|
|
3
|
+
import { errorText, ruleOf } from './field-errors'
|
|
4
|
+
import { issueOf } from './field-refusal'
|
|
5
|
+
import { formInput } from './form'
|
|
6
|
+
import { schemaOf } from './schema-record'
|
|
7
|
+
import { isStandardSchema, jsonSchemaOf } from './standard-schema'
|
|
8
|
+
import { wording } from './words.client'
|
|
9
|
+
|
|
10
|
+
export type { Issue, Refusal, Source, Validated, ValidateOptions }
|
|
11
|
+
|
|
12
|
+
type Node = Record<string, unknown> & FieldRules & { properties?: Record<string, Node>, required?: string[], additionalProperties?: unknown, items?: Node, default?: unknown }
|
|
13
|
+
|
|
14
|
+
const isNode = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
15
|
+
|
|
16
|
+
// A missing field takes its schema's `default`, a nested object's too.
|
|
17
|
+
function withDefaults(schema: Node, value: unknown): unknown {
|
|
18
|
+
if (!isNode(value) || schema.properties === undefined)
|
|
19
|
+
return value
|
|
20
|
+
|
|
21
|
+
const fields: Record<string, unknown> = { ...value }
|
|
22
|
+
|
|
23
|
+
for (const [name, field] of Object.entries(schema.properties)) {
|
|
24
|
+
if (fields[name] === undefined && field.default !== undefined && field.format !== 'binary')
|
|
25
|
+
fields[name] = structuredClone(field.default)
|
|
26
|
+
else if (fields[name] !== undefined)
|
|
27
|
+
fields[name] = withDefaults(field, fields[name])
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return fields
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// A form or a query posts text and a blank list is the empty one: the top-level fields are read as their schema expects, as the server reads them.
|
|
34
|
+
function readText(schema: Node, value: unknown, form: boolean, trim: boolean): unknown {
|
|
35
|
+
const input = isNode(value) ? { ...value } : {}
|
|
36
|
+
|
|
37
|
+
for (const [name, field] of Object.entries(schema.properties ?? {})) {
|
|
38
|
+
if (form && input[name] === '' && field.type === 'array')
|
|
39
|
+
input[name] = []
|
|
40
|
+
else if (form && input[name] === '' && field.default !== undefined && !schema.required?.includes(name))
|
|
41
|
+
delete input[name]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return formInput(schema as never, withDefaults(schema, input), form, form && trim)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const join = (path: string, step: string): string => path === '' ? step : `${path}.${step}`
|
|
48
|
+
|
|
49
|
+
function issue(path: string, key: Issue['key'], english: string, field: Node): Issue {
|
|
50
|
+
return { path, key, params: {}, message: errorText(field.error, ruleOf(key.slice('kay.invalid.'.length))) ?? wording(key, english) }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Every place the value fails the schema, once each, down to the fields of nested objects and the items of lists.
|
|
54
|
+
function walk(field: Node, value: unknown, path: string, source: Source, issues: Issue[]): void {
|
|
55
|
+
if (value === undefined) {
|
|
56
|
+
issues.push(issue(path, 'kay.invalid.required', 'Expected required property', field))
|
|
57
|
+
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// A file is read by its bytes on the server alone.
|
|
62
|
+
if (field.format === 'binary' || field.items?.format === 'binary')
|
|
63
|
+
return
|
|
64
|
+
|
|
65
|
+
if (field.anyOf?.some(member => member.type === 'object' || member.type === 'array')) {
|
|
66
|
+
const tries = field.anyOf.map((member) => {
|
|
67
|
+
const found: Issue[] = []
|
|
68
|
+
|
|
69
|
+
walk(member as Node, value, path, source, found)
|
|
70
|
+
|
|
71
|
+
return found
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
if (!tries.some(found => found.length === 0))
|
|
75
|
+
issues.push(issue(path, 'kay.invalid.type', 'Expected union value', field))
|
|
76
|
+
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (field.type === 'object' && field.anyOf === undefined) {
|
|
81
|
+
if (!isNode(value)) {
|
|
82
|
+
issues.push(issue(path, 'kay.invalid.type', 'Expected object', field))
|
|
83
|
+
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const shape = field.properties ?? {}
|
|
88
|
+
|
|
89
|
+
for (const [name, member] of Object.entries(shape)) {
|
|
90
|
+
if (value[name] !== undefined)
|
|
91
|
+
walk(member, value[name], join(path, name), source, issues)
|
|
92
|
+
else if (field.required?.includes(name))
|
|
93
|
+
issues.push(issue(join(path, name), 'kay.invalid.required', 'Expected required property', member))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
for (const [name, member] of Object.entries(value)) {
|
|
97
|
+
if (Object.hasOwn(shape, name))
|
|
98
|
+
continue
|
|
99
|
+
|
|
100
|
+
if (isNode(field.additionalProperties))
|
|
101
|
+
walk(field.additionalProperties, member, join(path, name), source, issues)
|
|
102
|
+
else if (field.additionalProperties === false)
|
|
103
|
+
issues.push(issue(join(path, name), 'kay.invalid.type', 'Unexpected property', field))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const found = issueOf(path, field, value)
|
|
110
|
+
|
|
111
|
+
if (found !== undefined) {
|
|
112
|
+
issues.push({ path, ...found })
|
|
113
|
+
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (field.type === 'array' && field.items !== undefined && Array.isArray(value)) {
|
|
118
|
+
for (const [index, item] of value.entries())
|
|
119
|
+
walk(field.items, item, join(path, String(index)), source, issues)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// What an object keeps of a value: a form's unknown top-level fields are dropped, as a browser posts controls the app does not own, and an object that opted into `extra: 'strip'` loses its unknown keys.
|
|
124
|
+
function stripped(schema: Node, value: unknown, dropUnknown = false): unknown {
|
|
125
|
+
if (Array.isArray(value))
|
|
126
|
+
return schema.items === undefined ? value : value.map(item => stripped(schema.items!, item))
|
|
127
|
+
|
|
128
|
+
if (!isNode(value) || schema.properties === undefined)
|
|
129
|
+
return value
|
|
130
|
+
|
|
131
|
+
const shape = schema.properties
|
|
132
|
+
const keep = schema['x-kay-extra'] !== 'strip' && !dropUnknown
|
|
133
|
+
|
|
134
|
+
return Object.fromEntries(Object.entries(value).filter(([name]) => keep || Object.hasOwn(shape, name)).map(([name, member]) => [name, Object.hasOwn(shape, name) ? stripped(shape[name]!, member) : member]))
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const refused = (errors: Issue[]): Refusal => ({ errors, status: 422 })
|
|
138
|
+
|
|
139
|
+
interface StandardResult {
|
|
140
|
+
value?: unknown
|
|
141
|
+
issues?: readonly { message?: string, path?: unknown }[]
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function standard(schema: object, source: Source, value: unknown, trim: boolean): Promise<Validated> {
|
|
145
|
+
const rules = source === 'json' ? undefined : jsonSchemaOf(schema) as Node | undefined
|
|
146
|
+
const read = rules === undefined ? value : readText(rules, value, source !== 'env' && source !== 'params', trim)
|
|
147
|
+
const result = await (schema as { '~standard': { validate: (value: unknown) => StandardResult | Promise<StandardResult> } })['~standard'].validate(read)
|
|
148
|
+
|
|
149
|
+
if (result.issues === undefined)
|
|
150
|
+
return { value: result.value }
|
|
151
|
+
|
|
152
|
+
const seen = new Set<string>()
|
|
153
|
+
const errors: Issue[] = []
|
|
154
|
+
|
|
155
|
+
for (const found of result.issues) {
|
|
156
|
+
const path = Array.isArray(found.path) ? found.path.map(step => String(isNode(step) ? step.key : step)).join('.') : ''
|
|
157
|
+
|
|
158
|
+
if (!seen.has(path)) {
|
|
159
|
+
seen.add(path)
|
|
160
|
+
errors.push({ path, key: 'kay.invalid.type', params: {}, message: found.message ?? wording('kay.invalid.type', 'Invalid value') })
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return refused(errors)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// The value a schema reads from `source`, or where and why it is refused: the server's `validate` in the browser, for a form before it posts or a row a local collection takes. A date stays its text, and a file is checked by the server alone, which reads its bytes; the messages are the page's locale.
|
|
168
|
+
export async function validate<Value = unknown>(schema: object, value: unknown, { source, trim = true }: ValidateOptions): Promise<Validated<Value>> {
|
|
169
|
+
if (isStandardSchema(schema))
|
|
170
|
+
return await standard(schema, source, value, trim) as Validated<Value>
|
|
171
|
+
|
|
172
|
+
const rules = schemaOf(schema as never) as unknown as Node
|
|
173
|
+
const read = stripped(rules, source === 'json' ? withDefaults(rules, value) : readText(rules, value, source !== 'env' && source !== 'params', trim), source === 'form')
|
|
174
|
+
const issues: Issue[] = []
|
|
175
|
+
|
|
176
|
+
walk(rules, read, '', source, issues)
|
|
177
|
+
|
|
178
|
+
return issues.length === 0 ? { value: read as Value } : refused(issues)
|
|
179
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Validated, ValidateOptions } from '../server/validate'
|
|
2
|
+
|
|
3
|
+
export type { Issue, Refusal, Source, Validated, ValidateOptions } from '../server/validate'
|
|
4
|
+
|
|
5
|
+
// The server's `validate` in the browser. The validator is a chunk of its own, loaded by the first call, so a page whose islands never call `validate` does not fetch it and one that does fetches it once.
|
|
6
|
+
// kept: free
|
|
7
|
+
export async function validate<Value = unknown>(schema: object, value: unknown, options: ValidateOptions): Promise<Validated<Value>> {
|
|
8
|
+
const { validate: run } = await import('./validate-run.client')
|
|
9
|
+
|
|
10
|
+
return run<Value>(schema, value, options)
|
|
11
|
+
}
|
package/src/runtime/validate.ts
CHANGED
|
@@ -1,47 +1,17 @@
|
|
|
1
1
|
import type { TSchema } from 'elysia'
|
|
2
|
-
import type {
|
|
3
|
-
import {
|
|
2
|
+
import type { FormRules } from './field-refusal'
|
|
3
|
+
import { fieldMessage } from './field-refusal'
|
|
4
4
|
import { focusInvalid } from './focus'
|
|
5
|
-
import { formInput
|
|
5
|
+
import { formInput } from './form'
|
|
6
6
|
import { controlsOf } from './form-controls'
|
|
7
7
|
import { markInvalid } from './invalid'
|
|
8
8
|
import { ISLAND } from './markers'
|
|
9
9
|
import { uid } from './uid'
|
|
10
|
-
import { wording } from './words.client'
|
|
11
|
-
|
|
12
|
-
// 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.
|
|
13
|
-
export interface FieldRules {
|
|
14
|
-
type?: string | undefined
|
|
15
|
-
format?: string | undefined
|
|
16
|
-
pattern?: string | undefined
|
|
17
|
-
minLength?: number | undefined
|
|
18
|
-
maxLength?: number | undefined
|
|
19
|
-
minimum?: number | undefined
|
|
20
|
-
maximum?: number | undefined
|
|
21
|
-
exclusiveMinimum?: number | undefined
|
|
22
|
-
exclusiveMaximum?: number | undefined
|
|
23
|
-
minItems?: number | undefined
|
|
24
|
-
maxItems?: number | undefined
|
|
25
|
-
const?: unknown | undefined
|
|
26
|
-
items?: FieldRules | undefined
|
|
27
|
-
anyOf?: FieldRules[] | undefined
|
|
28
|
-
error?: string | undefined
|
|
29
|
-
}
|
|
30
10
|
|
|
31
|
-
export
|
|
32
|
-
properties: Record<string, FieldRules>
|
|
33
|
-
required: string[]
|
|
34
|
-
}
|
|
11
|
+
export type { FieldRules, FormRules } from './field-refusal'
|
|
35
12
|
|
|
36
13
|
// The schemas of the actions a page's forms post to, by action path, in one JSON script of the page's head, and one more after each streamed block bringing forms the page lacked.
|
|
37
14
|
export const FORMS_SCRIPT = 'script[data-kay-forms]'
|
|
38
|
-
type Format = RegExp | ((value: string) => boolean)
|
|
39
|
-
|
|
40
|
-
// 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.
|
|
41
|
-
const FORMATS: Record<string, Format> = {
|
|
42
|
-
email: isEmail,
|
|
43
|
-
uuid: /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i,
|
|
44
|
-
}
|
|
45
15
|
|
|
46
16
|
// Checks a form against the schema of the action it posts to, as the server would, reporting each refused field with the message the server would give; true when it may post.
|
|
47
17
|
export function checkForm(form: HTMLFormElement, action: string): boolean {
|
|
@@ -205,106 +175,3 @@ function controlOf(form: HTMLFormElement, name: string): HTMLInputElement | HTML
|
|
|
205
175
|
|
|
206
176
|
return control instanceof HTMLInputElement || control instanceof HTMLSelectElement || control instanceof HTMLTextAreaElement ? control : undefined
|
|
207
177
|
}
|
|
208
|
-
|
|
209
|
-
function formatFails(format: Format | undefined, value: string): boolean {
|
|
210
|
-
if (format === undefined)
|
|
211
|
-
return false
|
|
212
|
-
|
|
213
|
-
return typeof format === 'function' ? !format(value) : !format.test(value)
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// 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; a site without the key reads Elysia's English.
|
|
217
|
-
function fieldMessage(name: string, field: FieldRules, value: unknown): string | undefined {
|
|
218
|
-
const message = refusal(name, field, value)
|
|
219
|
-
|
|
220
|
-
if (message !== undefined)
|
|
221
|
-
return field.error ?? wording(message.kind, message.english, message.params)
|
|
222
|
-
|
|
223
|
-
// An item is named by its index, as the server names it, and refused with its own `error`.
|
|
224
|
-
return field.items && Array.isArray(value) ? value.map((item, index) => fieldMessage(`${name}.${index}`, field.items!, item)).find(found => found !== undefined) : undefined
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// 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.
|
|
228
|
-
interface Refusal {
|
|
229
|
-
kind: InvalidWord
|
|
230
|
-
english: string
|
|
231
|
-
params?: Record<string, string | number>
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function typeRefusal(english: string): Refusal {
|
|
235
|
-
return { kind: 'kay.invalid.type', english }
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function refusal(name: string, field: FieldRules, value: unknown): Refusal | undefined {
|
|
239
|
-
if (field.anyOf) {
|
|
240
|
-
const bound = bounds(field, value)
|
|
241
|
-
|
|
242
|
-
if (bound === undefined && field.anyOf.some(member => refusal(name, member, value) === undefined))
|
|
243
|
-
return undefined
|
|
244
|
-
|
|
245
|
-
// A union is summarized by its members, though a bound of its own gives the kind, as the server words it.
|
|
246
|
-
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 } }
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (field.const !== undefined)
|
|
250
|
-
return value === field.const ? undefined : typeRefusal(`Expected ${typeof field.const === 'string' ? `'${field.const}'` : String(field.const)}`)
|
|
251
|
-
|
|
252
|
-
switch (field.type) {
|
|
253
|
-
case 'string':
|
|
254
|
-
if (typeof value !== 'string')
|
|
255
|
-
return typeRefusal('Expected string')
|
|
256
|
-
|
|
257
|
-
if (field.format !== undefined && NUMERIC_FORMATS.has(field.format))
|
|
258
|
-
return typeRefusal('Expected string')
|
|
259
|
-
|
|
260
|
-
if (field.minLength !== undefined && value.length < field.minLength)
|
|
261
|
-
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 } }
|
|
262
|
-
|
|
263
|
-
if (field.maxLength !== undefined && value.length > field.maxLength)
|
|
264
|
-
return { kind: 'kay.invalid.tooLong', english: `Expected string length less or equal to ${field.maxLength}`, params: { max: field.maxLength } }
|
|
265
|
-
|
|
266
|
-
if (field.pattern !== undefined && !new RegExp(field.pattern).test(value))
|
|
267
|
-
return { kind: 'kay.invalid.pattern', english: `Expected string to match '${field.pattern}'` }
|
|
268
|
-
|
|
269
|
-
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
|
|
270
|
-
case 'number':
|
|
271
|
-
case 'integer':
|
|
272
|
-
if (typeof value !== 'number' || Number.isNaN(value))
|
|
273
|
-
return typeRefusal(`Expected ${field.type}`)
|
|
274
|
-
|
|
275
|
-
// A bound a number fails is worded before its type, as the server does.
|
|
276
|
-
return bounds(field, value) ?? (field.type === 'integer' && !Number.isInteger(value) ? typeRefusal('Expected integer') : undefined)
|
|
277
|
-
case 'boolean':
|
|
278
|
-
return typeof value === 'boolean' ? undefined : typeRefusal('Expected boolean')
|
|
279
|
-
case 'array':
|
|
280
|
-
if (!Array.isArray(value))
|
|
281
|
-
return typeRefusal('Expected array')
|
|
282
|
-
|
|
283
|
-
if (field.minItems !== undefined && value.length < field.minItems)
|
|
284
|
-
return { kind: 'kay.invalid.tooFewItems', english: `Expected array length to be greater or equal to ${field.minItems}`, params: { min: field.minItems } }
|
|
285
|
-
|
|
286
|
-
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
|
|
287
|
-
case undefined:
|
|
288
|
-
default:
|
|
289
|
-
return undefined
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// An exclusive bound is worded as a type refusal, as the server does.
|
|
294
|
-
function bounds(field: FieldRules, value: unknown): Refusal | undefined {
|
|
295
|
-
if (typeof value !== 'number')
|
|
296
|
-
return undefined
|
|
297
|
-
|
|
298
|
-
const type = field.type ?? 'number'
|
|
299
|
-
|
|
300
|
-
if (field.minimum !== undefined && value < field.minimum)
|
|
301
|
-
return { kind: 'kay.invalid.tooSmall', english: `Expected ${type} to be greater or equal to ${field.minimum}`, params: { min: field.minimum } }
|
|
302
|
-
|
|
303
|
-
if (field.maximum !== undefined && value > field.maximum)
|
|
304
|
-
return { kind: 'kay.invalid.tooBig', english: `Expected ${type} to be less or equal to ${field.maximum}`, params: { max: field.maximum } }
|
|
305
|
-
|
|
306
|
-
if (field.exclusiveMinimum !== undefined && value <= field.exclusiveMinimum)
|
|
307
|
-
return typeRefusal(`Expected ${type} to be greater than ${field.exclusiveMinimum}`)
|
|
308
|
-
|
|
309
|
-
return field.exclusiveMaximum !== undefined && value >= field.exclusiveMaximum ? typeRefusal(`Expected ${type} to be less than ${field.exclusiveMaximum}`) : undefined
|
|
310
|
-
}
|
|
@@ -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
|
+
}
|
package/src/server/actions.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CookieMap } from 'bun'
|
|
2
|
-
import type { Elysia, TSchema
|
|
2
|
+
import type { Elysia, TSchema } from 'elysia'
|
|
3
3
|
import type { Server } from 'elysia/universal'
|
|
4
4
|
import type { ActionsConfig } from '../config'
|
|
5
5
|
import type { GuardContext, Locals, RouteParams } from '../runtime'
|
|
@@ -18,16 +18,17 @@ import type { I18n } from './i18n'
|
|
|
18
18
|
import type { DocumentedAction } from './openapi'
|
|
19
19
|
import type { Unmatched } from './page-routes'
|
|
20
20
|
import type { ToolOutputs } from './tool-outputs'
|
|
21
|
+
import type { Source } from './validate'
|
|
21
22
|
import { relative } from 'node:path'
|
|
22
23
|
import { KayError } from '../compiler/errors'
|
|
23
|
-
import { decodeCall, failure, FORM_CALL, FORM_FIELD, isAction, isFailure, protectionFields } from '../runtime/action'
|
|
24
|
+
import { decodeCall, failure, FORM_CALL, FORM_FIELD, honeypotField, isAction, isFailure, protectionFields, STAMP_FIELD } from '../runtime/action'
|
|
24
25
|
import { joinBase } from '../runtime/base'
|
|
25
26
|
import { escapeAttribute } from '../runtime/escape'
|
|
26
|
-
import { formInput } from '../runtime/form'
|
|
27
27
|
import { localeState, runInLocale } from '../runtime/locale-state'
|
|
28
28
|
import { thrownRefusal } from '../runtime/refuse'
|
|
29
29
|
import { withDefaultBounds } from '../runtime/schema-bounds'
|
|
30
30
|
import { sizeText } from '../runtime/size-text'
|
|
31
|
+
import { isStandardSchema } from '../runtime/standard-schema'
|
|
31
32
|
import { FAILURE_HEADER, NAVIGATE_HEADER, PAGE_HEADER, REDIRECT_HEADER } from '../runtime/swap'
|
|
32
33
|
import { ACTION_PREFIX, encodeValue, FIELDS_PREFIX, unsendable } from '../runtime/wire'
|
|
33
34
|
import { pageLocale } from '../runtime/words.server'
|
|
@@ -35,12 +36,11 @@ import { actionClash } from './action-clash'
|
|
|
35
36
|
import { hooksOf, refusedBeforeRun, tellError, tellSuccess, withActionDefaults } from './action-hooks'
|
|
36
37
|
import { withFixedFields, withoutEmptyFiles } from './action-input'
|
|
37
38
|
import { gathering, isAsyncIterable, lastOf, started, streamed } from './action-iterable'
|
|
39
|
+
import { actionModule } from './action-module'
|
|
38
40
|
import { routerPath } from './base'
|
|
39
|
-
import { importModule } from './bundle'
|
|
40
41
|
import { timeoutMs } from './config-limits'
|
|
41
42
|
import { cookieJar } from './cookies'
|
|
42
43
|
import { BACK_PARAMETER, crossSiteResponse, isCrossSite, postedFrom, TEXT } from './cross-site'
|
|
43
|
-
import { fieldMessage, refusedFields, validated } from './field-errors'
|
|
44
44
|
import { fileBodyLimit } from './file-limit'
|
|
45
45
|
import { handOver, submittedValues, writeSuccess } from './flash'
|
|
46
46
|
import { requiredGuards, runGuards, unguarded } from './guards'
|
|
@@ -61,6 +61,7 @@ import { carryContext } from './security'
|
|
|
61
61
|
import { actionStoppers, Denied, NotFound, Redirect, stoppers } from './signals'
|
|
62
62
|
import { toolSettings } from './tool-settings'
|
|
63
63
|
import { answered, replyTrace, SERVER_SPAN, traced } from './trace'
|
|
64
|
+
import { isRefusal, messagesOf, validate } from './validate'
|
|
64
65
|
import { visitAs } from './visit'
|
|
65
66
|
import { kayWord } from './words'
|
|
66
67
|
import { onceWritten } from './writes'
|
|
@@ -94,9 +95,6 @@ const HEAD_START = /<head\b[^>]*>/i
|
|
|
94
95
|
// The `action` of a form posting to an action, the site's `base` before it if any.
|
|
95
96
|
const FORM_ACTION = /(<form\b[^>]*?\saction=")([^"]*\/_kay\/action\/[^"#]*)/gi
|
|
96
97
|
|
|
97
|
-
// Elysia names a file it refuses `body.<field>`.
|
|
98
|
-
const BODY_PREFIX = /^body\./
|
|
99
|
-
|
|
100
98
|
export interface ActionOptions {
|
|
101
99
|
// Shows a failed action's message instead of a generic one.
|
|
102
100
|
dev?: boolean | undefined
|
|
@@ -138,29 +136,22 @@ type Back = (request: Request, headers: Headers, flash: Flash, server: Server |
|
|
|
138
136
|
const pageBound = new WeakMap<object, boolean>()
|
|
139
137
|
|
|
140
138
|
// 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[] }> {
|
|
139
|
+
export async function registerActionRoutes(app: Elysia, routes: ServedRoute[], match: RouteMatch, options: ActionOptions): Promise<{ limits: Map<string, BodyLimit>, tools: DocumentedAction[], broken: Map<string, string> }> {
|
|
142
140
|
const back: Back = async (request, headers, flash, server) => renderBack(app, options.base ?? '', request, headers, flash, server)
|
|
143
141
|
const served = new Map<string, { file: string, action: ServerAction<TSchema, unknown> }>()
|
|
144
142
|
const limits = new Map<string, BodyLimit>()
|
|
145
143
|
const tools: DocumentedAction[] = []
|
|
144
|
+
// The files `skipBroken` passed over, with why.
|
|
145
|
+
const broken = new Map<string, string>()
|
|
146
146
|
|
|
147
147
|
for (const [file, { guards, ...page }] of actionFiles(routes, options.requireGuards)) {
|
|
148
|
-
|
|
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)) {
|
|
148
|
+
for (const [name, exported] of Object.entries(await actionModule(file, options.skipBroken, broken))) {
|
|
159
149
|
if (!isAction(exported))
|
|
160
150
|
continue
|
|
161
151
|
|
|
162
152
|
// Every string field with no bound of its own takes the default, for the route's validation, the form's `maxlength` and the tool's schema alike.
|
|
163
|
-
|
|
153
|
+
if (!isStandardSchema(exported.schema))
|
|
154
|
+
Object.assign(exported, { schema: withDefaultBounds(exported.schema) })
|
|
164
155
|
|
|
165
156
|
const taken = served.get(exported.url)
|
|
166
157
|
|
|
@@ -208,7 +199,6 @@ export async function registerActionRoutes(app: Elysia, routes: ServedRoute[], m
|
|
|
208
199
|
|
|
209
200
|
return carryContext(context.set, response)
|
|
210
201
|
}, context.request), {
|
|
211
|
-
body: validated(value.schema),
|
|
212
202
|
transform: async (context) => {
|
|
213
203
|
refuseCrossSite(context.request)
|
|
214
204
|
|
|
@@ -234,36 +224,37 @@ export async function registerActionRoutes(app: Elysia, routes: ServedRoute[], m
|
|
|
234
224
|
if (typeof form === 'string')
|
|
235
225
|
requestRecord(context.request).postedForm = form
|
|
236
226
|
|
|
237
|
-
context.body = asForm ?
|
|
227
|
+
context.body = asForm ? withFixedFields(withoutEmptyFiles(context.body), context.request) : callInput(context.body)
|
|
238
228
|
},
|
|
239
|
-
//
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
error: async (context) => {
|
|
248
|
-
const { code, error, request, body, server, set } = context
|
|
229
|
+
// The guards first, so a caller they refuse learns nothing of the input, then the input against the schema: the handler reads the value it reads.
|
|
230
|
+
beforeHandle: async (context: { request: Request, body: unknown, server: Server | null }) => {
|
|
231
|
+
const { pageParams = {}, pageUrl } = requestRecord(context.request)
|
|
232
|
+
const { request } = context
|
|
233
|
+
|
|
234
|
+
return inLocale(request, options, async () => {
|
|
235
|
+
if (guards.length > 0) {
|
|
236
|
+
const refusal = await admit(guards, sharedContext(request, () => ({ ...context })), pageParams, pageUrl, options)
|
|
249
237
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
238
|
+
if (refusal)
|
|
239
|
+
return refusal
|
|
240
|
+
}
|
|
253
241
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
242
|
+
// A call with no input posts no body, which an object schema reads as no fields.
|
|
243
|
+
const posted = context.body === undefined && (value.schema as { type?: string }).type === 'object' ? {} : context.body
|
|
244
|
+
const result = await validate(value.schema, isCall(request) && !isFormCall(request) ? posted : withoutKayFields(posted, value), { source: sourceOfCall(request) })
|
|
257
245
|
|
|
258
|
-
|
|
259
|
-
|
|
246
|
+
if (isRefusal(result))
|
|
247
|
+
return failed(request, new Headers(), cookieJar(request).cookies, { action: value.url, status: 422, values: isCall(request) ? {} : submittedValues(posted), errors: messagesOf(result.errors) }, context.server, back)
|
|
260
248
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
249
|
+
// The copy the guards were handed is the one the handler is run with.
|
|
250
|
+
context.body = result.value
|
|
251
|
+
sharedContext(request, () => ({ ...context })).body = result.value
|
|
264
252
|
|
|
265
|
-
return
|
|
266
|
-
}
|
|
253
|
+
return undefined
|
|
254
|
+
})
|
|
255
|
+
},
|
|
256
|
+
error: async (context) => {
|
|
257
|
+
const { error, set } = context
|
|
267
258
|
|
|
268
259
|
// Read as unknown: some apps' Elysia types narrow the error to an intersection holding `number`.
|
|
269
260
|
const refusal: unknown = error
|
|
@@ -282,7 +273,7 @@ export async function registerActionRoutes(app: Elysia, routes: ServedRoute[], m
|
|
|
282
273
|
}
|
|
283
274
|
}
|
|
284
275
|
|
|
285
|
-
return { limits, tools }
|
|
276
|
+
return { limits, tools, broken }
|
|
286
277
|
}
|
|
287
278
|
|
|
288
279
|
// Runs the action under its timeout; one that ran out is reported as a server error and answered as a failed post, 504.
|
|
@@ -444,9 +435,14 @@ function callInput(body: unknown): unknown {
|
|
|
444
435
|
}
|
|
445
436
|
}
|
|
446
437
|
|
|
447
|
-
// The
|
|
448
|
-
function
|
|
449
|
-
|
|
438
|
+
// The post without the fields kay adds to a form itself, which the schema never names: the form it came from, the stamp `minTime` reads and the honeypot.
|
|
439
|
+
function withoutKayFields(body: unknown, action: ServerAction<TSchema, unknown>): unknown {
|
|
440
|
+
if (typeof body !== 'object' || body === null || Array.isArray(body))
|
|
441
|
+
return body
|
|
442
|
+
|
|
443
|
+
const honeypot = honeypotField(action.protection)
|
|
444
|
+
|
|
445
|
+
return Object.fromEntries(Object.entries(body).filter(([name]) => name !== FORM_FIELD && name !== STAMP_FIELD && name !== honeypot))
|
|
450
446
|
}
|
|
451
447
|
|
|
452
448
|
// Browsers send `Origin` on every POST, so a form another site submits on the user's behalf is refused before anything else runs.
|
|
@@ -780,13 +776,6 @@ function encodeOrExplain(value: unknown, what: string): string {
|
|
|
780
776
|
}
|
|
781
777
|
}
|
|
782
778
|
|
|
783
|
-
// Input the schema refused: a message per field, the schema's own `error` when it has one, a function `error` called in the request's locale.
|
|
784
|
-
async function refused(action: ServerAction<TSchema, unknown>, error: Readonly<ValidationError>, request: Request, server: Server | null, back: Back): Promise<Response> {
|
|
785
|
-
const errors = refusedFields(action.schema, error)
|
|
786
|
-
|
|
787
|
-
return failed(request, new Headers(), cookieJar(request).cookies, { action: action.url, status: 422, values: submittedValues(error.value), errors }, server, back)
|
|
788
|
-
}
|
|
789
|
-
|
|
790
779
|
// A call gets the failure as JSON; a plain form post gets the page it came from, rendered with the failure.
|
|
791
780
|
async function failed(request: Request, headers: Headers, cookies: CookieMap, flash: Flash, server: Server | null, back: Back): Promise<Response> {
|
|
792
781
|
for (const cookie of cookies.toSetCookieHeaders())
|
|
@@ -885,3 +874,11 @@ function visitHeaders(request: Request): Headers {
|
|
|
885
874
|
function redirectHeader(request: Request): string {
|
|
886
875
|
return isCall(request) || request.headers.get(NAVIGATE_HEADER) === '1' ? REDIRECT_HEADER : 'location'
|
|
887
876
|
}
|
|
877
|
+
|
|
878
|
+
// A call's JSON keeps its types, except one from an MCP client, whose arguments may be the text of a number or a boolean as a form's are.
|
|
879
|
+
function sourceOfCall(request: Request): Source {
|
|
880
|
+
if (!isCall(request) || isFormCall(request))
|
|
881
|
+
return 'form'
|
|
882
|
+
|
|
883
|
+
return request.headers.get('x-kay-tool') === '1' ? 'query' : 'json'
|
|
884
|
+
}
|