@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,346 @@
|
|
|
1
|
+
import type { TSchema } from 'elysia'
|
|
2
|
+
import type { LocaleState } from '../runtime/locale-state'
|
|
3
|
+
import type { InvalidWord } from './words'
|
|
4
|
+
import { fileType } from 'elysia'
|
|
5
|
+
import { KayError } from '../compiler/errors'
|
|
6
|
+
import { errorText, fieldMessage, ruleOf } from '../runtime/field-errors'
|
|
7
|
+
import { formInput, NUMERIC_FORMATS } from '../runtime/form'
|
|
8
|
+
import { runInLocale } from '../runtime/locale-state'
|
|
9
|
+
import { isStandardSchema, jsonSchemaOf } from '../runtime/standard-schema'
|
|
10
|
+
import { compiledCheck, dottedPath, summaryOf, withoutBlockUnions } from './schema-check'
|
|
11
|
+
import { invalidWord, kayWord, schemaRefusal } from './words'
|
|
12
|
+
|
|
13
|
+
// Where a value comes from, which says how it is read before it is checked: text from a form, a query string, a path or the environment is read as the numbers, booleans and lists it spells, and a JSON body is never coerced.
|
|
14
|
+
export type Source = 'form' | 'query' | 'params' | 'env' | 'json'
|
|
15
|
+
|
|
16
|
+
// One place a value is refused: `path` is dotted (`tags.0`, empty for the value itself), `key` the `kay.invalid.*` message the text is worded under, `params` the limit it names, `message` the text in the request's locale.
|
|
17
|
+
export interface Issue {
|
|
18
|
+
path: string
|
|
19
|
+
key: InvalidWord
|
|
20
|
+
params: Record<string, string | number>
|
|
21
|
+
message: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface Refusal {
|
|
25
|
+
errors: Issue[]
|
|
26
|
+
status: 422
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type Validated<Value = unknown> = { value: Value } | Refusal
|
|
30
|
+
|
|
31
|
+
export interface ValidateOptions {
|
|
32
|
+
source: Source
|
|
33
|
+
// The locale state the messages are worded in, the running request's by default.
|
|
34
|
+
locale?: LocaleState | undefined
|
|
35
|
+
// `false` keeps the spaces around the text a form or a query sent, which an action drops: an API route reads what it is posted as it came.
|
|
36
|
+
trim?: boolean | undefined
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function isRefusal(result: Validated): result is Refusal {
|
|
40
|
+
return 'errors' in result
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// A refusal's messages by the first path segment of each issue, the form-wide one under '', the first per field: what an action's `form.error(field)` and an API route's `errors` read.
|
|
44
|
+
export function messagesOf(errors: readonly Issue[]): Record<string, string> {
|
|
45
|
+
const messages: Record<string, string> = Object.create(null) as Record<string, string>
|
|
46
|
+
|
|
47
|
+
for (const { path, message } of errors) {
|
|
48
|
+
const dot = path.indexOf('.')
|
|
49
|
+
|
|
50
|
+
messages[dot < 0 ? path : path.slice(0, dot)] ??= message
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return messages
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
type Node = Record<string, unknown>
|
|
57
|
+
|
|
58
|
+
const isNode = (value: unknown): value is Node => typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
59
|
+
const isPlain = (value: unknown): value is Record<string, unknown> => isNode(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)
|
|
60
|
+
|
|
61
|
+
// The text forms of a number or a boolean that Elysia's builders add beside the real type, which only a text source reads.
|
|
62
|
+
const isTextForm = (member: Node): boolean => member.type === 'string' && (NUMERIC_FORMATS.has(member.format) || member.format === 'boolean')
|
|
63
|
+
|
|
64
|
+
const strictViews = new WeakMap<object, TSchema>()
|
|
65
|
+
|
|
66
|
+
// The schema a JSON value is checked against: the builders' text forms (`'12'` for a number, `'true'` for a boolean) and a date's number are dropped, so a JSON body is never coerced.
|
|
67
|
+
function strictView(schema: TSchema): TSchema {
|
|
68
|
+
let view = strictViews.get(schema)
|
|
69
|
+
|
|
70
|
+
if (view === undefined)
|
|
71
|
+
strictViews.set(schema, view = strict(schema) as TSchema)
|
|
72
|
+
|
|
73
|
+
return view
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function strict(node: unknown): unknown {
|
|
77
|
+
if (Array.isArray(node))
|
|
78
|
+
return node.map(strict)
|
|
79
|
+
|
|
80
|
+
if (!isNode(node))
|
|
81
|
+
return node
|
|
82
|
+
|
|
83
|
+
const members = node.anyOf
|
|
84
|
+
|
|
85
|
+
if (Array.isArray(members)) {
|
|
86
|
+
const dates = members.some(member => isNode(member) && member.type === 'Date')
|
|
87
|
+
const plain = members.filter(member => !(isNode(member) && (isTextForm(member) || (dates && member.type === 'number'))))
|
|
88
|
+
|
|
89
|
+
if (plain.length < members.length) {
|
|
90
|
+
const outer = { ...node }
|
|
91
|
+
|
|
92
|
+
delete outer.anyOf
|
|
93
|
+
|
|
94
|
+
return plain.length === 1 && isNode(plain[0]) ? { ...outer, ...strict(plain[0]) as Node } : { ...outer, anyOf: strict(plain) }
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const copy: Node = { ...node }
|
|
99
|
+
|
|
100
|
+
for (const key of ['properties', 'patternProperties'] as const) {
|
|
101
|
+
if (isNode(node[key]))
|
|
102
|
+
copy[key] = Object.fromEntries(Object.entries(node[key]).map(([name, field]) => [name, strict(field)]))
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
for (const key of ['items', 'additionalProperties', 'not'] as const) {
|
|
106
|
+
if (isNode(node[key]))
|
|
107
|
+
copy[key] = strict(node[key])
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
for (const key of ['anyOf', 'oneOf', 'allOf', 'prefixItems'] as const) {
|
|
111
|
+
if (Array.isArray(node[key]))
|
|
112
|
+
copy[key] = strict(node[key])
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return copy
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const isFileField = (field: Node): boolean => field.format === 'binary' || (isNode(field.items) && field.items.format === 'binary')
|
|
119
|
+
|
|
120
|
+
// A missing field takes its schema's `default`, a nested object's too; a default is a whole value, filled as it is.
|
|
121
|
+
function withDefaults(schema: unknown, value: unknown): unknown {
|
|
122
|
+
if (!isNode(schema) || !isPlain(value) || !isNode(schema.properties))
|
|
123
|
+
return value
|
|
124
|
+
|
|
125
|
+
const fields: Record<string, unknown> = { ...value }
|
|
126
|
+
|
|
127
|
+
for (const [name, field] of Object.entries(schema.properties)) {
|
|
128
|
+
if (!isNode(field))
|
|
129
|
+
continue
|
|
130
|
+
|
|
131
|
+
// Elysia's `File` and `Files` carry their own name as a `default`, which no post is filled with.
|
|
132
|
+
if (fields[name] === undefined && field.default !== undefined && !isFileField(field))
|
|
133
|
+
fields[name] = structuredClone(field.default)
|
|
134
|
+
else if (fields[name] !== undefined)
|
|
135
|
+
fields[name] = withDefaults(field, fields[name])
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return fields
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// The keys an object that opted into `extra: 'strip'` is sent with that its shape lacks, dropped; `dropUnknown` does it for the top level unless the object takes any key, as `additionalProperties: true` or a schema for them says.
|
|
142
|
+
function stripped(schema: unknown, value: unknown, dropUnknown = false): unknown {
|
|
143
|
+
if (!isNode(schema))
|
|
144
|
+
return value
|
|
145
|
+
|
|
146
|
+
if (Array.isArray(value))
|
|
147
|
+
return isNode(schema.items) ? value.map(item => stripped(schema.items, item)) : value
|
|
148
|
+
|
|
149
|
+
if (!isPlain(value) || !isNode(schema.properties))
|
|
150
|
+
return value
|
|
151
|
+
|
|
152
|
+
const shape = schema.properties
|
|
153
|
+
const open = schema.additionalProperties === true || isNode(schema.additionalProperties)
|
|
154
|
+
const keep = schema['x-kay-extra'] !== 'strip' && (!dropUnknown || open)
|
|
155
|
+
|
|
156
|
+
return Object.fromEntries(Object.entries(value).filter(([name]) => keep || name in shape).map(([name, field]) => [name, name in shape ? stripped(shape[name], field) : field]))
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// What a form posts for a list, a boolean or a number is text, and a blank list is the empty one: a top-level field read as its schema expects, the defaults of what was left out filled in.
|
|
160
|
+
function readText(schema: unknown, value: unknown, form: boolean, trim: boolean): unknown {
|
|
161
|
+
const input = isPlain(value) ? { ...value } : {}
|
|
162
|
+
const { properties, required = [] } = (isNode(schema) ? schema : {}) as { properties?: Record<string, Node>, required?: string[] }
|
|
163
|
+
|
|
164
|
+
for (const [name, field] of Object.entries(properties ?? {})) {
|
|
165
|
+
// A blank list is the empty one, as `persisted.query` writes it, never one blank item; a blank optional field with a default is left out, so the default fills in.
|
|
166
|
+
if (form && input[name] === '' && field.type === 'array')
|
|
167
|
+
input[name] = []
|
|
168
|
+
else if (form && input[name] === '' && field.default !== undefined && !required.includes(name))
|
|
169
|
+
delete input[name]
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// The defaults go in before the text is read, so a list left out reads as its default and not as the empty list a form's missing list is.
|
|
173
|
+
return formInput(schema as TSchema, withDefaults(schema, input), form, form && trim)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function prepare(schema: unknown, value: unknown, source: Source, trim: boolean): unknown {
|
|
177
|
+
const read = source === 'json' ? withDefaults(schema, value) : readText(schema, value, source !== 'env' && source !== 'params', trim)
|
|
178
|
+
|
|
179
|
+
// A browser posts controls the app does not own, a named submit button or a widget: a form's unknown fields are dropped, where JSON and a query are strict.
|
|
180
|
+
return stripped(schema, read, source === 'form')
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Every place the value fails a schema, once each, the first reason for it.
|
|
184
|
+
// A value that passed, without the keys an object of the old builders, which names no `additionalProperties`, was sent beside its shape: they are dropped as Elysia dropped them, so a handler never reads a key its schema does not name. A union's value is cleaned by the member it matches.
|
|
185
|
+
function cleaned(schema: unknown, value: unknown): unknown {
|
|
186
|
+
if (!isNode(schema))
|
|
187
|
+
return value
|
|
188
|
+
|
|
189
|
+
if (Array.isArray(schema.anyOf)) {
|
|
190
|
+
const match = schema.anyOf.find(member => isNode(member) && matches(member, value))
|
|
191
|
+
|
|
192
|
+
return match === undefined ? value : cleaned(match, value)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (Array.isArray(value))
|
|
196
|
+
return isNode(schema.items) ? value.map(item => cleaned(schema.items, item)) : value
|
|
197
|
+
|
|
198
|
+
if (!isPlain(value) || !isNode(schema.properties))
|
|
199
|
+
return value
|
|
200
|
+
|
|
201
|
+
const shape = schema.properties
|
|
202
|
+
// `false` refused the extra keys already; `true` or a schema keeps them.
|
|
203
|
+
const open = schema.additionalProperties !== undefined
|
|
204
|
+
|
|
205
|
+
return Object.fromEntries(Object.entries(value).filter(([name]) => open || Object.hasOwn(shape, name)).map(([name, field]) => [name, Object.hasOwn(shape, name) ? cleaned(shape[name], field) : field]))
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function matches(member: Node, value: unknown): boolean {
|
|
209
|
+
try {
|
|
210
|
+
return compiledCheck(member as TSchema).Check(value)
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
// A member referring to a model by name compiles only with it: the value is then cleaned by none.
|
|
214
|
+
return false
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function checked(schema: TSchema, source: Source, value: unknown, trim = true): Validated {
|
|
219
|
+
const view = source === 'json' ? strictView(schema) : schema
|
|
220
|
+
const check = compiledCheck(view)
|
|
221
|
+
const read = prepare(view, value, source, trim)
|
|
222
|
+
|
|
223
|
+
if (check.Check(read)) {
|
|
224
|
+
try {
|
|
225
|
+
return { value: check.Decode(cleaned(view, read)) }
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
return refused([{ path: '', key: 'kay.invalid.type', params: {}, message: invalidWord('kay.invalid.type', error instanceof Error ? error.message : undefined) }])
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const seen = new Set<string>()
|
|
233
|
+
const errors: Issue[] = []
|
|
234
|
+
|
|
235
|
+
for (const issue of withoutBlockUnions(check.Errors(read))) {
|
|
236
|
+
const path = dottedPath(issue.path)
|
|
237
|
+
|
|
238
|
+
if (seen.has(path))
|
|
239
|
+
continue
|
|
240
|
+
|
|
241
|
+
seen.add(path)
|
|
242
|
+
|
|
243
|
+
const { kind, params = {} } = schemaRefusal(issue)
|
|
244
|
+
const key = `kay.invalid.${kind}` as InvalidWord
|
|
245
|
+
const own = (issue.schema as { error?: unknown } | undefined)?.error
|
|
246
|
+
const rule = ruleOf(kind)
|
|
247
|
+
const custom = fieldMessage(schema, issue.path, rule) ?? errorText(own, rule)
|
|
248
|
+
|
|
249
|
+
errors.push({ path, key, params, message: custom ?? invalidWord(key, summaryOf(issue), params) })
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return refused(errors)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// A file the schema gives a media type is checked by its bytes, as the browser's claim of its type is no proof: the fields of the top level, a list of files too.
|
|
256
|
+
async function wrongFiles(schema: TSchema, value: unknown): Promise<Issue[]> {
|
|
257
|
+
const { properties } = schema as { properties?: Record<string, Node & { items?: Node }> }
|
|
258
|
+
|
|
259
|
+
if (!isPlain(value) || properties === undefined)
|
|
260
|
+
return []
|
|
261
|
+
|
|
262
|
+
const wrong: Issue[] = []
|
|
263
|
+
|
|
264
|
+
for (const [name, field] of Object.entries(properties)) {
|
|
265
|
+
const extension = field.extension ?? field.items?.extension
|
|
266
|
+
|
|
267
|
+
if (extension === undefined || value[name] === undefined || (field.format !== 'binary' && field.items?.format !== 'binary'))
|
|
268
|
+
continue
|
|
269
|
+
|
|
270
|
+
try {
|
|
271
|
+
await fileType(value[name] as Blob, extension as string, `body.${name}`)
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
wrong.push({ path: name, key: 'kay.invalid.format', params: { format: 'file' }, message: fieldMessage(schema, `/${name}`, 'format') ?? kayWord('kay.action.invalidFile') })
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return wrong
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function refused(errors: Issue[]): Refusal {
|
|
282
|
+
return { errors, status: 422 }
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// The dotted path of an issue of a Standard Schema validator: its path is a list of keys or of `{ key }`.
|
|
286
|
+
function standardPath(path: unknown): string {
|
|
287
|
+
return Array.isArray(path) ? path.map(step => String(isNode(step) ? step.key : step)).join('.') : ''
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
interface StandardResult {
|
|
291
|
+
value?: unknown
|
|
292
|
+
issues?: readonly { message?: string, path?: unknown }[]
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Any Standard Schema validator: its text sources are read by its JSON Schema when it has one, then it validates the value itself.
|
|
296
|
+
async function standard(schema: object, source: Source, value: unknown, trim: boolean): Promise<Validated> {
|
|
297
|
+
const rules = source === 'json' ? undefined : jsonSchemaOf(schema)
|
|
298
|
+
const read = rules === undefined ? value : prepare(rules, value, source, trim)
|
|
299
|
+
const result = await (schema as { '~standard': { validate: (value: unknown) => StandardResult | Promise<StandardResult> } })['~standard'].validate(read)
|
|
300
|
+
|
|
301
|
+
if (result.issues === undefined)
|
|
302
|
+
return { value: result.value }
|
|
303
|
+
|
|
304
|
+
const seen = new Set<string>()
|
|
305
|
+
const errors: Issue[] = []
|
|
306
|
+
|
|
307
|
+
for (const issue of result.issues) {
|
|
308
|
+
const path = standardPath(issue.path)
|
|
309
|
+
|
|
310
|
+
if (seen.has(path))
|
|
311
|
+
continue
|
|
312
|
+
|
|
313
|
+
seen.add(path)
|
|
314
|
+
errors.push({ path, key: 'kay.invalid.type', params: {}, message: issue.message ?? invalidWord('kay.invalid.type', undefined) })
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return refused(errors)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// The value a schema reads from `source`, or where and why it is refused: the one path every input of kay goes through, an action's, an API route's, a socket's message, the environment and the browser's check of a form. A Standard Schema validator may be async, which a caller that cannot await reads with `validateNow`.
|
|
321
|
+
// kept: free, the locale and source come from its caller.
|
|
322
|
+
export async function validate<Value = unknown>(schema: object, value: unknown, { source, locale, trim = true }: ValidateOptions): Promise<Validated<Value>> {
|
|
323
|
+
const run = async (): Promise<Validated> => {
|
|
324
|
+
if (isStandardSchema(schema))
|
|
325
|
+
return standard(schema, source, value, trim)
|
|
326
|
+
|
|
327
|
+
const result = checked(schema as TSchema, source, value, trim)
|
|
328
|
+
|
|
329
|
+
if (isRefusal(result))
|
|
330
|
+
return result
|
|
331
|
+
|
|
332
|
+
const wrong = await wrongFiles(schema as TSchema, result.value)
|
|
333
|
+
|
|
334
|
+
return wrong.length > 0 ? refused(wrong) : result
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return await (locale === undefined ? run() : runInLocale(locale, run)) as Validated<Value>
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// `validate` for a caller that cannot await, such as the boot reading the environment; a Standard Schema validator answering with a promise is refused.
|
|
341
|
+
export function validateNow<Value = unknown>(schema: object, value: unknown, { source, locale, trim = true }: ValidateOptions): Validated<Value> {
|
|
342
|
+
if (isStandardSchema(schema))
|
|
343
|
+
throw new KayError('config-invalid', 'kay: validateNow reads a TypeBox schema, as a Standard Schema validator may answer with a promise: use validate')
|
|
344
|
+
|
|
345
|
+
return (locale === undefined ? checked(schema as TSchema, source, value, trim) : runInLocale(locale, () => checked(schema as TSchema, source, value, trim))) as Validated<Value>
|
|
346
|
+
}
|
package/src/template-ws.ts
CHANGED
|
@@ -10,7 +10,7 @@ let online = 0
|
|
|
10
10
|
|
|
11
11
|
// Served at /api/online: every open connection hears how many there are.${has('auth') ? ' Open to everyone: the root guard would otherwise close it.' : ''}
|
|
12
12
|
export default api(${has('auth') ? '{ guards: false }' : ''}).ws('/', {
|
|
13
|
-
response: s.
|
|
13
|
+
response: s.object({ online: s.number() }),
|
|
14
14
|
open(ws) {
|
|
15
15
|
online++
|
|
16
16
|
ws.subscribe('online')
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { AxeResults } from '../runtime/dev-a11y'
|
|
2
|
+
import type { A11yViolation } from './page-types'
|
|
3
|
+
import { MissingPeerError, resolvePeer } from '../peers'
|
|
4
|
+
import { withUnreadable } from '../runtime/dev-a11y'
|
|
5
|
+
|
|
6
|
+
type AxeViolation = Omit<A11yViolation, 'nodes'> & { nodes: { target: (string | string[])[], html: string }[] }
|
|
7
|
+
|
|
8
|
+
interface AxeWindow {
|
|
9
|
+
eval: (code: string) => unknown
|
|
10
|
+
axe?: { run: (context: unknown, options: unknown) => Promise<AxeResults<AxeViolation>> }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// What axe-core cannot judge without a browser's layout and painting: contrast, and the size and overlap of targets. A test of those runs `page.a11y()` of a browser page.
|
|
14
|
+
const NEEDS_LAYOUT = ['color-contrast', 'target-size', 'scrollable-region-focusable']
|
|
15
|
+
|
|
16
|
+
const axes = new WeakSet<object>()
|
|
17
|
+
let axe: string | undefined
|
|
18
|
+
|
|
19
|
+
// Runs axe-core in a happy-dom document, the whole of `window.document` or the elements `selector` matches, with the rules that need no layout.
|
|
20
|
+
export async function auditDocument(window: AxeWindow & { document: Document }, selector: string | undefined, within?: Element): Promise<A11yViolation[]> {
|
|
21
|
+
if (!axes.has(window)) {
|
|
22
|
+
axe ??= await Bun.file(resolvePeer('axe-core', 'axe-core/axe.min.js')).text()
|
|
23
|
+
window.eval(axe)
|
|
24
|
+
axes.add(window)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const root = within ?? window.document
|
|
28
|
+
const target = selector === undefined ? root : root.querySelector(selector)
|
|
29
|
+
|
|
30
|
+
if (target === null)
|
|
31
|
+
throw new Error(`kay: nothing matches ${selector} to check`)
|
|
32
|
+
|
|
33
|
+
const rules = Object.fromEntries(NEEDS_LAYOUT.map(id => [id, { enabled: false }]))
|
|
34
|
+
const results = await window.axe!.run(target, { rules, resultTypes: ['violations'] })
|
|
35
|
+
|
|
36
|
+
return withUnreadable(results).map(({ id, impact, help, helpUrl, tags, nodes }) => ({ id, impact, help, helpUrl, tags, nodes: nodes.map(node => ({ target: node.target.map(part => Array.isArray(part) ? part.join(' >>> ') : part).join(' '), html: node.html })) }))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Checks the markup of a visited page in a document of its own: its scripts never run, as a crawler reads it, and what only a browser lays out is left to `page.a11y()`.
|
|
40
|
+
export async function auditHtml(html: string, selector: string | undefined): Promise<A11yViolation[]> {
|
|
41
|
+
const { Window } = await loadHappyDom()
|
|
42
|
+
const window = new Window({ settings: { disableJavaScriptEvaluation: true, disableJavaScriptFileLoading: true, disableCSSFileLoading: true, handleDisabledFileLoadingAsSuccess: true } })
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
window.document.write(html)
|
|
46
|
+
|
|
47
|
+
// axe-core is evaluated in the window once the page's own scripts are gone.
|
|
48
|
+
for (const script of window.document.querySelectorAll('script'))
|
|
49
|
+
script.remove()
|
|
50
|
+
|
|
51
|
+
window.happyDOM.settings.disableJavaScriptEvaluation = false
|
|
52
|
+
|
|
53
|
+
return await auditDocument(window as unknown as AxeWindow & { document: Document }, selector)
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
await window.happyDOM.close()
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function loadHappyDom(): Promise<typeof import('happy-dom')> {
|
|
61
|
+
try {
|
|
62
|
+
return await import('happy-dom')
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
if ((error as { code?: string }).code !== 'ERR_MODULE_NOT_FOUND' && !String(error).includes('Cannot find'))
|
|
66
|
+
throw error
|
|
67
|
+
|
|
68
|
+
throw new MissingPeerError(`happy-dom is not installed, and \`page.a11y()\` of a visited page needs it: run \`bun add -d happy-dom\``)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, rmSync } from 'node:fs'
|
|
2
|
+
import { basename, dirname, join } from 'node:path'
|
|
3
|
+
import process from 'node:process'
|
|
4
|
+
import { callingTestFile } from './screenshot-match'
|
|
5
|
+
|
|
6
|
+
const NAME = /^[\w.-]+(?:\/[\w.-]+)*$/
|
|
7
|
+
const TOKEN = /<!--[\s\S]*?-->|<(script|style|pre|textarea)\b[\s\S]*?<\/\1\s*>|<(?:[^>"']|"[^"]*"|'[^']*')+>|[^<]+/gi
|
|
8
|
+
const COMMENT = /^<!--/
|
|
9
|
+
const OPEN_TAG = /^<([a-z][\w:-]*)/i
|
|
10
|
+
const CLOSE_TAG = /^<\/\s*[a-z]/i
|
|
11
|
+
const RAW = /^<(?:script|style|pre|textarea)\b/i
|
|
12
|
+
const WHITESPACE = /\s+/g
|
|
13
|
+
// Bun names a built file `<name>-<hash>.<ext>`, the hash eight letters and digits; only under `/_kay/`, so an app's own eight-letter slug stays.
|
|
14
|
+
const HASHED_FILE = /(\/_kay\/[\w./-]*?)-[\da-z]{8}(?=\.[a-z\d]+(?:[?"'\s)#]|$))/gi
|
|
15
|
+
// A `?v=` cache-busting version, such as a favicon's.
|
|
16
|
+
const VERSION = /([?&]v=)[\da-f]{6,}/gi
|
|
17
|
+
const NONCE = /(\bnonce=)(?:"[^"]*"|'[^']*'|[^\s>"']+)/gi
|
|
18
|
+
const STAMP_INPUT = /<input\s[^>]*\bname=["']?kay_since\b[^>]*>/gi
|
|
19
|
+
const STAMP_VALUE = /(\bvalue=)(?:"[^"]*"|'[^']*'|[^\s>]+)/i
|
|
20
|
+
const VOID = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'source', 'track', 'wbr'])
|
|
21
|
+
|
|
22
|
+
// A page's markup as a snapshot holds it, the same from one build to the next: comments (the hydration markers among them) left out, the hash of each built file's name and of a `?v=` version, each nonce and the stamp of a `minTime` form replaced by a fixed word, and one tag or run of text a line, indented by depth.
|
|
23
|
+
export function normalizeHtml(html: string): string {
|
|
24
|
+
const lines: string[] = []
|
|
25
|
+
let depth = 0
|
|
26
|
+
|
|
27
|
+
for (const token of html.replace(HASHED_FILE, '$1-HASH').replace(VERSION, '$1HASH').replace(NONCE, '$1"NONCE"').replace(STAMP_INPUT, input => input.replace(STAMP_VALUE, '$1"STAMP"')).match(TOKEN) ?? []) {
|
|
28
|
+
if (COMMENT.test(token))
|
|
29
|
+
continue
|
|
30
|
+
|
|
31
|
+
const indent = ' '.repeat(depth)
|
|
32
|
+
|
|
33
|
+
if (RAW.test(token)) {
|
|
34
|
+
lines.push(`${indent}${token.trim()}`)
|
|
35
|
+
|
|
36
|
+
continue
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!token.startsWith('<')) {
|
|
40
|
+
const text = token.replace(WHITESPACE, ' ').trim()
|
|
41
|
+
|
|
42
|
+
if (text !== '')
|
|
43
|
+
lines.push(`${indent}${text}`)
|
|
44
|
+
|
|
45
|
+
continue
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (CLOSE_TAG.test(token))
|
|
49
|
+
depth = Math.max(0, depth - 1)
|
|
50
|
+
|
|
51
|
+
lines.push(`${' '.repeat(depth)}${token.replace(WHITESPACE, ' ')}`)
|
|
52
|
+
|
|
53
|
+
const tag = OPEN_TAG.exec(token)?.[1]?.toLowerCase()
|
|
54
|
+
|
|
55
|
+
if (tag !== undefined && !VOID.has(tag) && !token.endsWith('/>'))
|
|
56
|
+
depth++
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return `${lines.join('\n')}\n`
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// What a page's `matchHtml` runs: the test file is read off `stack`, taken where the call began.
|
|
63
|
+
export async function matchHtml(stack: string | undefined, html: string, name: string): Promise<void> {
|
|
64
|
+
await matchHtmlFile(normalizeHtml(html), name, callingTestFile(stack, 'matchHtml'))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Compares normalised markup with the baseline `<test dir>/__html__/<test file>/<name>.html`.
|
|
68
|
+
// The baseline is written when there is none, or when `KAY_UPDATE_SNAPSHOTS` is set, and a missing one fails under `CI`; a difference writes `<name>.actual.html` beside it and fails, and a match removes it.
|
|
69
|
+
export async function matchHtmlFile(normalised: string, name: string, testFile: string): Promise<void> {
|
|
70
|
+
if (!NAME.test(name) || name.split('/').includes('..'))
|
|
71
|
+
throw new Error(`kay: matchHtml takes a name of letters, digits, dots, dashes and slashes, not ${JSON.stringify(name)}`)
|
|
72
|
+
|
|
73
|
+
const base = join(dirname(testFile), '__html__', basename(testFile), name)
|
|
74
|
+
const baseline = `${base}.html`
|
|
75
|
+
const actual = `${base}.actual.html`
|
|
76
|
+
const update = (process.env.KAY_UPDATE_SNAPSHOTS ?? '') !== '' && process.env.KAY_UPDATE_SNAPSHOTS !== '0'
|
|
77
|
+
|
|
78
|
+
rmSync(actual, { force: true })
|
|
79
|
+
|
|
80
|
+
if (update || !existsSync(baseline)) {
|
|
81
|
+
if (!update && (process.env.CI ?? '') !== '')
|
|
82
|
+
throw new Error(`kay: matchHtml has no baseline ${baseline} and a CI run writes none: run the test locally and commit it`)
|
|
83
|
+
|
|
84
|
+
mkdirSync(dirname(baseline), { recursive: true })
|
|
85
|
+
await Bun.write(baseline, normalised)
|
|
86
|
+
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const expected = await Bun.file(baseline).text()
|
|
91
|
+
|
|
92
|
+
if (expected === normalised)
|
|
93
|
+
return
|
|
94
|
+
|
|
95
|
+
await Bun.write(actual, normalised)
|
|
96
|
+
|
|
97
|
+
const before = expected.split('\n')
|
|
98
|
+
const after = normalised.split('\n')
|
|
99
|
+
const line = after.findIndex((text, at) => text !== before[at])
|
|
100
|
+
const at = line === -1 ? after.length : line
|
|
101
|
+
|
|
102
|
+
throw new Error(`kay: the HTML ${name} differs from ${baseline} from line ${at + 1}: expected ${JSON.stringify(before[at] ?? '(end)')}, got ${JSON.stringify(after[at] ?? '(end)')}; see ${actual}, and rewrite the baseline with KAY_UPDATE_SNAPSHOTS=1 if the change is meant`)
|
|
103
|
+
}
|
package/src/test/index.ts
CHANGED
|
@@ -51,6 +51,7 @@ import { checkOnly, lazyNativePlugins } from './lazy-plugins'
|
|
|
51
51
|
import { NativeRequest, NativeURL, withNatives } from './natives'
|
|
52
52
|
import { pruneTestOutputs, touchOutput, touchOutputIfOld } from './outputs'
|
|
53
53
|
import { readPage, settledHtml, sitemapUrls } from './page'
|
|
54
|
+
import { pageChecks } from './page-checks'
|
|
54
55
|
import { testRoot } from './root'
|
|
55
56
|
import { actionsOf, formUrl, renderedStamp } from './site-actions'
|
|
56
57
|
import { apiOf } from './site-api'
|
|
@@ -366,7 +367,7 @@ async function siteOf(options: TestOptions): Promise<TestSite> {
|
|
|
366
367
|
const html = await withNatives(async () => current.text())
|
|
367
368
|
|
|
368
369
|
let settled: Visit['settled'] | undefined
|
|
369
|
-
const reading = readPage(html)
|
|
370
|
+
const reading = { ...readPage(html), ...pageChecks(html) }
|
|
370
371
|
|
|
371
372
|
return {
|
|
372
373
|
url: path,
|
package/src/test/mount.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { WebStorage } from '../runtime/persist'
|
|
2
2
|
import type { Interactions } from './interact'
|
|
3
|
+
import type { A11yViolation } from './page-types'
|
|
3
4
|
import { mkdir, mkdtemp, rm, stat } from 'node:fs/promises'
|
|
4
5
|
import { dirname, join, resolve } from 'node:path'
|
|
5
6
|
import process from 'node:process'
|
|
@@ -14,6 +15,8 @@ import { COOKIE_NAME } from '../runtime/persist'
|
|
|
14
15
|
import { sendValue } from '../runtime/send'
|
|
15
16
|
import { decodeValue } from '../runtime/wire'
|
|
16
17
|
import { apiStubs, apiTrees } from '../server/api-modules'
|
|
18
|
+
import { auditDocument } from './a11y-dom'
|
|
19
|
+
import { matchHtml } from './html-snapshot'
|
|
17
20
|
import { interactions } from './interact'
|
|
18
21
|
import { lazyNativePlugins } from './lazy-plugins'
|
|
19
22
|
import { testRoot } from './root'
|
|
@@ -26,6 +29,10 @@ export interface Mounted<Props = Record<string, unknown>> extends Interactions {
|
|
|
26
29
|
setProps: (props: Partial<Props>) => Promise<void>
|
|
27
30
|
// Disposes of the component, running its cleanups once, and removes the element.
|
|
28
31
|
unmount: () => void
|
|
32
|
+
// Checks what the component rendered, or only the elements `selector` matches inside it, with axe-core: what it breaks, none when it passes. Rules about a whole page, such as a heading or a landmark, do not apply to a part, and what needs a browser's layout, such as colour contrast, is left out; needs `axe-core`.
|
|
33
|
+
a11y: (selector?: string) => Promise<A11yViolation[]>
|
|
34
|
+
// Compares the component's markup, normalised as `visit.matchHtml` does, with the baseline `__html__/<test file>/<name>.html`.
|
|
35
|
+
matchHtml: (name: string) => Promise<void>
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
interface MountModule {
|
|
@@ -36,9 +43,13 @@ const SAFE_HTML_MODULE = /(?:^|[/\\])safe-html(?:\.ts)?$/
|
|
|
36
43
|
// One bundle per component and version of its file, for the test process, evaluated again on every mount.
|
|
37
44
|
const bundles = new Map<string, Promise<string>>()
|
|
38
45
|
|
|
39
|
-
// Lets queued effects and timers due now land, as after a click, before the DOM is read.
|
|
46
|
+
// Lets queued effects and timers due now land, as after a click, before the DOM is read, and the components that load when they first draw arrive.
|
|
40
47
|
export async function settle(): Promise<void> {
|
|
41
48
|
await sleep(0)
|
|
49
|
+
|
|
50
|
+
// A second turn only after a component loaded, so a transition's first frame stays readable after a plain settle.
|
|
51
|
+
if (await (globalThis as { $kayLazyIdle?: () => Promise<boolean> }).$kayLazyIdle?.() === true)
|
|
52
|
+
await sleep(0)
|
|
42
53
|
}
|
|
43
54
|
|
|
44
55
|
// Keeps a value in localStorage as `persisted.local(key, initial)` does, or in sessionStorage as `persisted.session(key, initial)` does with `'session'`, encoded as island props are rather than as JSON, for a component mounted after to read.
|
|
@@ -129,7 +140,7 @@ export async function mountComponent(source: (props: never) => unknown, ...given
|
|
|
129
140
|
|
|
130
141
|
await settle()
|
|
131
142
|
|
|
132
|
-
return { root: element, ...interactions(element, settle), setProps: async (next) => {
|
|
143
|
+
return { root: element, ...interactions(element, settle), a11y: async selector => auditDocument(window, selector, element), matchHtml: async name => matchHtml(new Error('the call').stack, element.innerHTML, name), setProps: async (next) => {
|
|
133
144
|
if (!mounted)
|
|
134
145
|
return
|
|
135
146
|
|
|
@@ -175,7 +186,7 @@ async function bundle(root: string, file: string): Promise<string> {
|
|
|
175
186
|
|
|
176
187
|
const loaded = await loadConfig(root)
|
|
177
188
|
const trees = apiTrees(root, loaded)
|
|
178
|
-
const config = { entrypoints: [join(work, 'entry.ts')], target: 'browser', metafile: true, define: { KAY_FRENCH: writesFrench(loaded) ? 'true' : 'undefined', KAY_I18N: loaded.i18n === undefined ? 'undefined' : 'true' }, plugins: [apiStubs(trees), kay({ root, settings: loaded.librarySettings, api: trees }), sharedMarks, sharedModules(trees.map(({ dir }) => dir))], throw: false } satisfies Bun.BuildConfig
|
|
189
|
+
const config = { entrypoints: [join(work, 'entry.ts')], target: 'browser', metafile: true, define: { KAY_FRENCH: writesFrench(loaded) ? 'true' : 'undefined', KAY_I18N: loaded.i18n === undefined ? 'undefined' : 'true' }, plugins: [apiStubs(trees), kay({ root, settings: loaded.librarySettings, api: trees, lazy: false }), sharedMarks, sharedModules(trees.map(({ dir }) => dir))], throw: false } satisfies Bun.BuildConfig
|
|
179
190
|
const build = await Bun.build({ ...config, outdir: join(work, 'out') })
|
|
180
191
|
|
|
181
192
|
if (!build.success) {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { A11yViolation } from './page-types'
|
|
2
|
+
import { auditHtml } from './a11y-dom'
|
|
3
|
+
import { matchHtml } from './html-snapshot'
|
|
4
|
+
|
|
5
|
+
// What a visited page checks itself with: its accessibility, and its HTML against a baseline.
|
|
6
|
+
export function pageChecks(html: string): { a11y: (selector?: string) => Promise<A11yViolation[]>, matchHtml: (name: string) => Promise<void> } {
|
|
7
|
+
return {
|
|
8
|
+
a11y: async selector => auditHtml(html, selector),
|
|
9
|
+
// The call's stack is read here, where it began, to find the test file.
|
|
10
|
+
matchHtml: async name => matchHtml(new Error('the call').stack, html, name),
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -12,11 +12,11 @@ const NAME = /^[\w.-]+(?:\/[\w.-]+)*$/
|
|
|
12
12
|
const TEST_FILE = /^\s*at (?:.*\()?(?:file:\/\/)?(\/.+?\.test\.[cm]?[jt]sx?):\d+:\d+\)?$/m
|
|
13
13
|
|
|
14
14
|
// The test file calling `matchScreenshot`, read off the stack where the call began, as Bun gives no hook the file of the running test.
|
|
15
|
-
export function callingTestFile(stack: string | undefined): string {
|
|
15
|
+
export function callingTestFile(stack: string | undefined, caller = 'matchScreenshot'): string {
|
|
16
16
|
const found = TEST_FILE.exec(stack ?? '')?.[1]
|
|
17
17
|
|
|
18
18
|
if (found === undefined)
|
|
19
|
-
throw new Error(
|
|
19
|
+
throw new Error(`kay: ${caller} found no \`*.test.ts\` file among its callers to keep the baseline beside`)
|
|
20
20
|
|
|
21
21
|
return found
|
|
22
22
|
}
|