@kayjs/kay 1.106.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 +428 -176
- package/editors/zed/extension.toml +2 -2
- package/llms/action-protection.md +1 -1
- package/llms/actions-refused-fields.md +22 -0
- package/llms/actions.md +2 -22
- package/llms/browser-only.md +1 -1
- package/llms/compiler-api.md +1 -1
- package/llms/components.md +1 -1
- package/llms/deprecated-lints.md +2 -3
- package/llms/island-and-form-lints.md +1 -0
- package/llms/islands.md +10 -1
- package/llms/project-new-and-add.md +1 -1
- package/llms/project.md +2 -2
- package/llms/schemas.md +6 -4
- package/llms/upgrading.md +19 -1
- package/llms/whats-new.md +14 -1
- package/package.json +15 -6
- package/src/build/client.ts +20 -24
- 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 +3 -1
- package/src/build/runtime-chunk.ts +1 -1
- package/src/build/transform.ts +17 -7
- package/src/build/variants.ts +7 -4
- package/src/check/action-options.ts +56 -0
- package/src/check/schema-json.ts +226 -0
- package/src/check/shadow-types.ts +11 -9
- package/src/compiler/codegen.ts +7 -17
- package/src/compiler/deprecated-lints-1-105.ts +41 -0
- package/src/compiler/deprecated-lints-1-106.ts +1 -1
- package/src/compiler/deprecated-lints-1-107.ts +14 -0
- package/src/compiler/deprecated-lints.ts +7 -55
- package/src/compiler/import-edits.ts +15 -0
- package/src/compiler/index.ts +8 -3
- package/src/compiler/island-declaration.ts +15 -0
- package/src/compiler/kay-exports.ts +1 -0
- package/src/compiler/lazy-imports.ts +357 -0
- package/src/compiler/parser.ts +2 -2
- 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/content/validate.ts +1 -1
- package/src/new.ts +6 -2
- package/src/runtime/action.ts +47 -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.ts +3 -2
- package/src/runtime/html.ts +6 -1
- package/src/runtime/index.ts +2 -0
- package/src/runtime/island.server.ts +36 -2
- package/src/runtime/schema-record.ts +2 -1
- package/src/runtime/schema.client.ts +104 -14
- package/src/runtime/schema.ts +9 -2
- package/src/runtime/standard-schema.ts +32 -0
- 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/actions.ts +48 -43
- package/src/server/answer.ts +102 -0
- package/src/server/api-input.ts +166 -0
- package/src/server/api.ts +12 -44
- package/src/server/app-stages.ts +15 -8
- package/src/server/app.ts +5 -2
- package/src/server/client.ts +2 -0
- package/src/server/dev.ts +7 -9
- 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/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/library.ts +0 -2
- package/src/server/mcp.ts +1 -1
- 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/query.ts +4 -25
- package/src/server/receive.ts +43 -0
- package/src/server/render-document.ts +13 -3
- package/src/server/request-context.ts +137 -0
- package/src/server/schema-check.ts +1 -1
- package/src/server/socket.ts +19 -47
- package/src/server/stream.ts +1 -1
- package/src/server/validate.ts +346 -0
- package/src/test/mount.ts +6 -2
- 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 +3 -1
- package/types/build/transform.d.ts.map +1 -1
- package/types/build/variants.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/schema-json.d.ts +3 -0
- package/types/check/schema-json.d.ts.map +1 -0
- package/types/check/shadow-types.d.ts.map +1 -1
- package/types/compiler/codegen.d.ts +2 -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 +1 -1
- package/types/compiler/deprecated-lints-1-106.d.ts.map +1 -1
- 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 +36 -44
- package/types/compiler/deprecated-lints.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 +2 -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/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/lints.d.ts +44 -44
- 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/new.d.ts +1 -0
- package/types/new.d.ts.map +1 -1
- package/types/runtime/action.d.ts +26 -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.d.ts +1 -1
- package/types/runtime/form.d.ts.map +1 -1
- 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/schema-record.d.ts.map +1 -1
- package/types/runtime/schema.client.d.ts.map +1 -1
- package/types/runtime/schema.d.ts +6 -1
- 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/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/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 +4 -1
- 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/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/library.d.ts +0 -1
- package/types/server/library.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/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/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/mount.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,276 @@
|
|
|
1
|
+
import type { Owner } from './reactive'
|
|
2
|
+
import { skew } from './build'
|
|
3
|
+
import { blockIn, claiming, HydrationMismatchError, islandOf, Range, Region, release, swap } from './dom'
|
|
4
|
+
import { ISLAND, PART_END, PART_START } from './markers'
|
|
5
|
+
import { getOwner, onCleanup, reportError, runWithOwner, unrecorded } from './reactive'
|
|
6
|
+
import { savesDataInBrowser } from './save-data'
|
|
7
|
+
|
|
8
|
+
// A component whose code loads the first time it draws: its own module, as it weighs on a page whose islands draw every component at once.
|
|
9
|
+
|
|
10
|
+
type Component = (props: never) => unknown
|
|
11
|
+
|
|
12
|
+
interface Loadable {
|
|
13
|
+
load: () => Promise<{ default: unknown }>
|
|
14
|
+
component?: Component | undefined
|
|
15
|
+
loading?: Promise<{ default: unknown }> | undefined
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// How long to wait before each new try of a chunk that failed to load.
|
|
19
|
+
const RETRY_WAITS_MS = [1000, 3000]
|
|
20
|
+
|
|
21
|
+
// One entry per key, which every module importing that component shares: a module that loaded it is a load for all.
|
|
22
|
+
const loadables = new Map<string, Loadable>()
|
|
23
|
+
// The keys each island's module imports lazily, by island id, which a visitor reaching for the island loads ahead.
|
|
24
|
+
const scopes = new Map<string, Set<string>>()
|
|
25
|
+
// The keys a failed intent load left, tried once: a hover must not ask again and again for a chunk that is not there.
|
|
26
|
+
const refused = new Set<string>()
|
|
27
|
+
|
|
28
|
+
// Loads a component's chunk, the same load for every caller; a failure is dropped so the next call tries again.
|
|
29
|
+
async function ensure(loadable: Loadable): Promise<Component> {
|
|
30
|
+
if (loadable.component)
|
|
31
|
+
return loadable.component
|
|
32
|
+
|
|
33
|
+
const attempt = loadable.loading ??= loadable.load()
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
loadable.component = (await attempt).default as Component
|
|
37
|
+
;(globalThis as { $kayLazyLoaded?: boolean }).$kayLazyLoaded = true
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
// Only its own attempt is dropped: another caller may have started a new one since.
|
|
41
|
+
if (loadable.loading === attempt)
|
|
42
|
+
loadable.loading = undefined
|
|
43
|
+
|
|
44
|
+
throw error
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return loadable.component
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let watching = false
|
|
51
|
+
// Bumped whenever a module registers keys, so an island warmed before it is warmed again.
|
|
52
|
+
let version = 0
|
|
53
|
+
const warmed = new Map<string, number>()
|
|
54
|
+
|
|
55
|
+
// The first sign a visitor reaches for something in an island loads the lazy parts of that island, so a click finds the code in. A visitor saving data loads a part when it opens.
|
|
56
|
+
function warmOnIntent(): void {
|
|
57
|
+
if (watching || typeof document === 'undefined')
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
watching = true
|
|
61
|
+
|
|
62
|
+
const warm = (event: Event) => {
|
|
63
|
+
const island = event.target instanceof Element ? event.target.closest<HTMLElement>(ISLAND) : null
|
|
64
|
+
|
|
65
|
+
if (island?.dataset.component !== undefined && !savesDataInBrowser())
|
|
66
|
+
void warmScope(island.dataset.component)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (const name of ['pointerover', 'pointerdown', 'focusin'])
|
|
70
|
+
document.addEventListener(name, warm, { capture: true, passive: true })
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// The keys of an island and of the modules that are no island on the page, which are the components it draws.
|
|
74
|
+
function keysFor(id: string): string[] {
|
|
75
|
+
const own = new Set<string>()
|
|
76
|
+
|
|
77
|
+
for (const [scope, keys] of scopes) {
|
|
78
|
+
if (scope === id || !document.querySelector(`${ISLAND}[data-component="${CSS.escape(scope)}"]`)) {
|
|
79
|
+
for (const key of keys) {
|
|
80
|
+
own.add(key)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return [...own]
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function warmScope(id: string): Promise<void> {
|
|
89
|
+
if (warmed.get(id) === version)
|
|
90
|
+
return
|
|
91
|
+
|
|
92
|
+
await Promise.all(keysFor(id).map(async (key) => {
|
|
93
|
+
const loadable = loadables.get(key)
|
|
94
|
+
|
|
95
|
+
if (!loadable || loadable.component || loadable.loading || refused.has(key))
|
|
96
|
+
return
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
await ensure(loadable)
|
|
100
|
+
// What the loaded module imports lazily in turn belongs to the same island.
|
|
101
|
+
await warmScope(id)
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
// Drawn when it opens, which tries again and reports.
|
|
105
|
+
refused.add(key)
|
|
106
|
+
}
|
|
107
|
+
}))
|
|
108
|
+
warmed.set(id, version)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// A component imported as `KayLazy(key, () => import('./it.kay'))`: it draws as the component does once loaded, and before that a place the component renders into when it arrives.
|
|
112
|
+
// A visitor reaching for the island loads it ahead, and one the server drew is loaded before its island hydrates (`loadDrawn`), so the markup is claimed as it stands.
|
|
113
|
+
export function lazy(key: string, load: () => Promise<{ default: unknown }>): (props: never) => unknown {
|
|
114
|
+
let loadable = loadables.get(key)
|
|
115
|
+
|
|
116
|
+
if (!loadable)
|
|
117
|
+
loadables.set(key, loadable = { load })
|
|
118
|
+
|
|
119
|
+
const entry = loadable
|
|
120
|
+
|
|
121
|
+
warmOnIntent()
|
|
122
|
+
|
|
123
|
+
return (props: never) => {
|
|
124
|
+
if (entry.component)
|
|
125
|
+
return entry.component(props)
|
|
126
|
+
|
|
127
|
+
// The island was told the keys the server drew, which it loads first: one left is a markup the code was not there to claim, so the island draws afresh.
|
|
128
|
+
if (claiming())
|
|
129
|
+
throw new HydrationMismatchError(`the server drew ${key} but its code did not load before the island hydrated`)
|
|
130
|
+
|
|
131
|
+
return placeholder(entry, props, key)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Hands an error to the nearest `catch` block above `owner`, or reports it when there is none, as an effect of the owner would.
|
|
136
|
+
function handleError(owner: Owner | undefined, error: unknown): void {
|
|
137
|
+
let catcher = owner
|
|
138
|
+
|
|
139
|
+
while (catcher && !catcher.catcher)
|
|
140
|
+
catcher = catcher.owner
|
|
141
|
+
|
|
142
|
+
if (!catcher) {
|
|
143
|
+
reportError(error)
|
|
144
|
+
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
catcher.catcher!(error)
|
|
150
|
+
}
|
|
151
|
+
catch (failure) {
|
|
152
|
+
reportError(failure)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// An empty range, the component rendered between its ends once its code is in.
|
|
157
|
+
function placeholder(entry: Loadable, props: never, key: string): Range {
|
|
158
|
+
const start = document.createComment(PART_START)
|
|
159
|
+
const end = document.createComment(PART_END)
|
|
160
|
+
const parent = getOwner()
|
|
161
|
+
|
|
162
|
+
// The ends are siblings, as a range's nodes are, so the parent places both and what draws between them.
|
|
163
|
+
document.createDocumentFragment().append(start, end)
|
|
164
|
+
|
|
165
|
+
const block = blockIn(new Region(null, start, end))
|
|
166
|
+
let gone = false
|
|
167
|
+
|
|
168
|
+
onCleanup(() => {
|
|
169
|
+
gone = true
|
|
170
|
+
release(block)
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
void track(land(entry, key, start, () => gone, (component) => {
|
|
174
|
+
// A part taken out of its parent has nowhere to draw.
|
|
175
|
+
if (!start.parentNode)
|
|
176
|
+
return
|
|
177
|
+
|
|
178
|
+
// A render error goes to the nearest `catch` above the place, as an eager component's does, and is no load failure.
|
|
179
|
+
try {
|
|
180
|
+
runWithOwner(parent, () => unrecorded(() => swap(block, () => component(props), true)))
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
handleError(parent, error)
|
|
184
|
+
}
|
|
185
|
+
}))
|
|
186
|
+
|
|
187
|
+
return new Range(start, end)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// The places waiting for their component, which a test's `settle()` waits for through `$kayLazyIdle`.
|
|
191
|
+
const inflight = new Set<Promise<void>>()
|
|
192
|
+
|
|
193
|
+
async function track(drawing: Promise<void>): Promise<void> {
|
|
194
|
+
inflight.add(drawing)
|
|
195
|
+
|
|
196
|
+
try {
|
|
197
|
+
await drawing
|
|
198
|
+
}
|
|
199
|
+
finally {
|
|
200
|
+
inflight.delete(drawing)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Answers whether it waited for any, so a settle with nothing loading takes no extra turn.
|
|
205
|
+
;(globalThis as { $kayLazyIdle?: () => Promise<boolean> }).$kayLazyIdle = async () => {
|
|
206
|
+
const waited = inflight.size > 0
|
|
207
|
+
|
|
208
|
+
while (inflight.size > 0)
|
|
209
|
+
await Promise.allSettled(inflight)
|
|
210
|
+
|
|
211
|
+
return waited
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Loads the component, trying again after a failure, then draws it; a render error is no load failure and goes where any render error of the branch goes. The one that failed to load for good is reported, and a deploy that dropped the chunk reloads the page.
|
|
215
|
+
async function land(entry: Loadable, key: string, anchor: Node, gone: () => boolean, draw: (component: Component) => void): Promise<void> {
|
|
216
|
+
let component: Component | undefined
|
|
217
|
+
let failure: unknown
|
|
218
|
+
|
|
219
|
+
for (const wait of [0, ...RETRY_WAITS_MS]) {
|
|
220
|
+
if (wait > 0)
|
|
221
|
+
await new Promise(resolve => setTimeout(resolve, wait))
|
|
222
|
+
|
|
223
|
+
if (gone())
|
|
224
|
+
return
|
|
225
|
+
|
|
226
|
+
try {
|
|
227
|
+
component = await ensure(entry)
|
|
228
|
+
|
|
229
|
+
break
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
failure = error
|
|
233
|
+
|
|
234
|
+
if (await skew())
|
|
235
|
+
return
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (component === undefined) {
|
|
240
|
+
reportError(failure, islandOf(anchor))
|
|
241
|
+
console.error(`[kay] ${key} failed to load, it draws when it is opened again`)
|
|
242
|
+
|
|
243
|
+
return
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (!gone())
|
|
247
|
+
draw(component)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Loads the keys an island's server markup drew before it hydrates, not in a module's top level, where a chunk importing its asker would wait on itself. A failed chunk is left to its branch, which draws afresh, retries and reports.
|
|
251
|
+
export async function loadDrawn(island: HTMLElement): Promise<void> {
|
|
252
|
+
const asked = [...island.querySelectorAll<HTMLElement>('[data-lazy]'), island].flatMap(element => element.dataset.lazy?.split(' ') ?? [])
|
|
253
|
+
const tried = new Set<string>()
|
|
254
|
+
|
|
255
|
+
// The modules loaded may register keys the markup drew, which load in the next round.
|
|
256
|
+
for (;;) {
|
|
257
|
+
const round = asked.filter(key => loadables.has(key) && !tried.has(key) && !loadables.get(key)!.component)
|
|
258
|
+
|
|
259
|
+
if (round.length === 0)
|
|
260
|
+
return
|
|
261
|
+
|
|
262
|
+
for (const key of round)
|
|
263
|
+
tried.add(key)
|
|
264
|
+
|
|
265
|
+
await Promise.allSettled(round.map(async key => ensure(loadables.get(key)!)))
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Read by the island's hydration, which this module's loading precedes: a page with no lazy import never loads it.
|
|
270
|
+
;(globalThis as { $kayLoadDrawn?: typeof loadDrawn }).$kayLoadDrawn = loadDrawn
|
|
271
|
+
|
|
272
|
+
// What a module with lazy imports calls at its top level: it records the keys it imports lazily for the island `id`, which a visitor reaching for the island loads ahead.
|
|
273
|
+
export function ready(id: string, keys: readonly string[]): void {
|
|
274
|
+
scopes.set(id, new Set([...scopes.get(id) ?? [], ...keys]))
|
|
275
|
+
version++
|
|
276
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// The `error` option of a rule: `min` for a text too short, a list too short or a number too small, `max` for the other bound, else the kind itself (`pattern`, `format`, `required`, `type`).
|
|
2
|
+
const RULES: Record<string, string> = { tooShort: 'min', tooFewItems: 'min', tooSmall: 'min', tooLong: 'max', tooManyItems: 'max', tooBig: 'max' }
|
|
3
|
+
|
|
4
|
+
export function ruleOf(kind: string): string {
|
|
5
|
+
return RULES[kind] ?? kind
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// The text an `error` option gives a field: a string, the string a function returns, or for a record the entry of the rule the field failed, else its `default`; a function that throws or returns anything else gives none, so the field keeps its default wording.
|
|
9
|
+
// A function is called where the caller runs, which is inside the request's locale.
|
|
10
|
+
export function errorText(error: unknown, rule?: string): string | undefined {
|
|
11
|
+
if (typeof error === 'function') {
|
|
12
|
+
try {
|
|
13
|
+
const text = (error as () => unknown)()
|
|
14
|
+
|
|
15
|
+
return typeof text === 'string' ? text : undefined
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return undefined
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (typeof error === 'object' && error !== null && !Array.isArray(error)) {
|
|
23
|
+
const words = error as Record<string, unknown>
|
|
24
|
+
|
|
25
|
+
return errorText((rule === undefined ? undefined : words[rule]) ?? words.default)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return typeof error === 'string' ? error : undefined
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const isNode = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
32
|
+
|
|
33
|
+
// The first `error` found on the way down: a field's own, else a branch of its `anyOf`.
|
|
34
|
+
function find(node: unknown, segments: string[], rule: string | undefined): string | undefined {
|
|
35
|
+
if (!isNode(node))
|
|
36
|
+
return undefined
|
|
37
|
+
|
|
38
|
+
const [head, ...rest] = segments
|
|
39
|
+
const named = head !== undefined && isNode(node.properties) && Object.hasOwn(node.properties, head)
|
|
40
|
+
const found = head === undefined ? errorText(node.error, rule) : find(named ? (node.properties as Record<string, unknown>)[head] : node.items, rest, rule)
|
|
41
|
+
|
|
42
|
+
if (found !== undefined || !Array.isArray(node.anyOf))
|
|
43
|
+
return found
|
|
44
|
+
|
|
45
|
+
for (const branch of node.anyOf) {
|
|
46
|
+
const inBranch = find(branch, segments, rule)
|
|
47
|
+
|
|
48
|
+
if (inBranch !== undefined)
|
|
49
|
+
return inBranch
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return undefined
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// The `error` option at a JSON pointer of the schema, a field's, an item's or one of a union's branches, read in the current locale.
|
|
56
|
+
export function fieldMessage(schema: unknown, pointer: string, rule?: string): string | undefined {
|
|
57
|
+
return find(schema, pointer.split('/').slice(1).map(segment => segment.replaceAll('~1', '/').replaceAll('~0', '~')), rule)
|
|
58
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import type { InvalidWord } from '../server/words'
|
|
2
|
+
import { isEmail } from './email'
|
|
3
|
+
import { errorText, ruleOf } from './field-errors'
|
|
4
|
+
import { NUMERIC_FORMATS } from './form'
|
|
5
|
+
import { wording } from './words.client'
|
|
6
|
+
|
|
7
|
+
// What the server sends of an action's schema, for the forms of a page to be checked before they post: the top-level fields, the keywords the browser checks, and their `error` messages.
|
|
8
|
+
export interface FieldRules {
|
|
9
|
+
type?: string | undefined
|
|
10
|
+
format?: string | undefined
|
|
11
|
+
pattern?: string | undefined
|
|
12
|
+
minLength?: number | undefined
|
|
13
|
+
maxLength?: number | undefined
|
|
14
|
+
minimum?: number | undefined
|
|
15
|
+
maximum?: number | undefined
|
|
16
|
+
exclusiveMinimum?: number | undefined
|
|
17
|
+
exclusiveMaximum?: number | undefined
|
|
18
|
+
minItems?: number | undefined
|
|
19
|
+
maxItems?: number | undefined
|
|
20
|
+
const?: unknown | undefined
|
|
21
|
+
items?: FieldRules | undefined
|
|
22
|
+
anyOf?: FieldRules[] | undefined
|
|
23
|
+
enum?: unknown[] | undefined
|
|
24
|
+
// A text, a function or a record by rule, as the field's `error` option; the server sends the text it resolved.
|
|
25
|
+
error?: unknown
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface FormRules {
|
|
29
|
+
properties: Record<string, FieldRules>
|
|
30
|
+
required: string[]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type Format = RegExp | ((value: string) => boolean)
|
|
34
|
+
|
|
35
|
+
// The formats the browser checks too, as the server does (`email` is kay's own, the rest Elysia's); any other format is left to the server.
|
|
36
|
+
const FORMATS: Record<string, Format> = {
|
|
37
|
+
'email': isEmail,
|
|
38
|
+
'uuid': /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i,
|
|
39
|
+
'url': isUrl,
|
|
40
|
+
'date-time': /^\d{4}-\d\d-\d\d(?:T\d\d:\d\d(?::\d\d(?:\.\d+)?)?(?:Z|[+-]\d\d:\d\d)?)?$/i,
|
|
41
|
+
'date': /^\d{4}-\d\d-\d\d$/,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function formatFails(format: Format | undefined, value: string): boolean {
|
|
45
|
+
if (format === undefined)
|
|
46
|
+
return false
|
|
47
|
+
|
|
48
|
+
return typeof format === 'function' ? !format(value) : !format.test(value)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const HTTP_URL = /^https?:/i
|
|
52
|
+
|
|
53
|
+
// An `http:` or `https:` address, as the server's `url` format reads it.
|
|
54
|
+
function isUrl(value: string): boolean {
|
|
55
|
+
if (!HTTP_URL.test(value))
|
|
56
|
+
return false
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
void new URL(value)
|
|
60
|
+
|
|
61
|
+
return true
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return false
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Where a value is refused and why: the `kay.invalid.*` key, the limit it names and the text, the field's own `error` else the key's wording; a site without the key reads Elysia's English.
|
|
69
|
+
export function issueOf(name: string, field: FieldRules, value: unknown): { key: InvalidWord, params: Record<string, string | number>, message: string } | undefined {
|
|
70
|
+
const found = refusal(name, field, value)
|
|
71
|
+
|
|
72
|
+
if (found === undefined)
|
|
73
|
+
return undefined
|
|
74
|
+
|
|
75
|
+
const custom = errorText(field.error, ruleOf(found.kind.slice('kay.invalid.'.length)))
|
|
76
|
+
const params = found.params ?? {}
|
|
77
|
+
|
|
78
|
+
return { key: found.kind, params, message: custom ?? wording(found.kind, found.english, params) }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// The schema's own `error` for a refused field, else the `kay.invalid.*` text of the keyword it fails, which the server words the same way.
|
|
82
|
+
export function fieldMessage(name: string, field: FieldRules, value: unknown): string | undefined {
|
|
83
|
+
const issue = issueOf(name, field, value)
|
|
84
|
+
|
|
85
|
+
if (issue !== undefined)
|
|
86
|
+
return issue.message
|
|
87
|
+
|
|
88
|
+
// An item is named by its index, as the server names it, and refused with its own `error`.
|
|
89
|
+
return field.items && Array.isArray(value) ? value.map((item, index) => fieldMessage(`${name}.${index}`, field.items!, item)).find(found => found !== undefined) : undefined
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// What a field's value fails: the `kay.invalid.*` key the server's refusal of it would use, the limit it names, and Elysia's own summary, which is its English.
|
|
93
|
+
interface Refusal {
|
|
94
|
+
kind: InvalidWord
|
|
95
|
+
english: string
|
|
96
|
+
params?: Record<string, string | number>
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function typeRefusal(english: string): Refusal {
|
|
100
|
+
return { kind: 'kay.invalid.type', english }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function stringRefusal(name: string, field: FieldRules, value: unknown): Refusal | undefined {
|
|
104
|
+
if (typeof value !== 'string' || (field.format !== undefined && NUMERIC_FORMATS.has(field.format)))
|
|
105
|
+
return typeRefusal('Expected string')
|
|
106
|
+
|
|
107
|
+
if (field.minLength !== undefined && value.length < field.minLength)
|
|
108
|
+
return { kind: value === '' && field.minLength === 1 ? 'kay.invalid.required' : 'kay.invalid.tooShort', english: `Expected string length greater or equal to ${field.minLength}`, params: { min: field.minLength } }
|
|
109
|
+
|
|
110
|
+
if (field.maxLength !== undefined && value.length > field.maxLength)
|
|
111
|
+
return { kind: 'kay.invalid.tooLong', english: `Expected string length less or equal to ${field.maxLength}`, params: { max: field.maxLength } }
|
|
112
|
+
|
|
113
|
+
if (field.pattern !== undefined && !new RegExp(field.pattern).test(value))
|
|
114
|
+
return { kind: 'kay.invalid.pattern', english: `Expected string to match '${field.pattern}'` }
|
|
115
|
+
|
|
116
|
+
return field.format !== undefined && formatFails(FORMATS[field.format], value) ? { kind: 'kay.invalid.format', english: `Property '${name}' should be ${field.format}`, params: { format: field.format } } : undefined
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function refusal(name: string, field: FieldRules, value: unknown): Refusal | undefined {
|
|
120
|
+
if (field.anyOf) {
|
|
121
|
+
const bound = bounds(field, value)
|
|
122
|
+
|
|
123
|
+
if (bound === undefined && field.anyOf.some(member => refusal(name, member, value) === undefined))
|
|
124
|
+
return undefined
|
|
125
|
+
|
|
126
|
+
// A union is summarized by its members, though a bound of its own gives the kind, as the server words it.
|
|
127
|
+
return { kind: bound?.kind ?? 'kay.invalid.type', english: `Property '${name}' should be one of: ${field.anyOf.map(member => `'${member.format ?? member.type}'`).join(', ')}`, ...bound?.params && { params: bound.params } }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (field.enum !== undefined && field.type !== 'array' && !field.enum.includes(value))
|
|
131
|
+
return typeRefusal('Expected union value')
|
|
132
|
+
|
|
133
|
+
if (field.const !== undefined)
|
|
134
|
+
return value === field.const ? undefined : typeRefusal(`Expected ${typeof field.const === 'string' ? `'${field.const}'` : String(field.const)}`)
|
|
135
|
+
|
|
136
|
+
switch (field.type) {
|
|
137
|
+
case 'string':
|
|
138
|
+
return stringRefusal(name, field, value)
|
|
139
|
+
case 'number':
|
|
140
|
+
case 'integer':
|
|
141
|
+
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
142
|
+
return typeRefusal(`Expected ${field.type}`)
|
|
143
|
+
|
|
144
|
+
// A bound a number fails is worded before its type, as the server does.
|
|
145
|
+
return bounds(field, value) ?? (field.type === 'integer' && !Number.isInteger(value) ? typeRefusal('Expected integer') : undefined)
|
|
146
|
+
case 'boolean':
|
|
147
|
+
return typeof value === 'boolean' ? undefined : typeRefusal('Expected boolean')
|
|
148
|
+
case 'array':
|
|
149
|
+
if (!Array.isArray(value))
|
|
150
|
+
return typeRefusal('Expected array')
|
|
151
|
+
|
|
152
|
+
if (field.minItems !== undefined && value.length < field.minItems)
|
|
153
|
+
return { kind: 'kay.invalid.tooFewItems', english: `Expected array length to be greater or equal to ${field.minItems}`, params: { min: field.minItems } }
|
|
154
|
+
|
|
155
|
+
return field.maxItems !== undefined && value.length > field.maxItems ? { kind: 'kay.invalid.tooManyItems', english: `Expected array length to be less or equal to ${field.maxItems}`, params: { max: field.maxItems } } : undefined
|
|
156
|
+
case undefined:
|
|
157
|
+
default:
|
|
158
|
+
return undefined
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// An exclusive bound is worded as a type refusal, as the server does.
|
|
163
|
+
function bounds(field: FieldRules, value: unknown): Refusal | undefined {
|
|
164
|
+
if (typeof value !== 'number')
|
|
165
|
+
return undefined
|
|
166
|
+
|
|
167
|
+
const type = field.type ?? 'number'
|
|
168
|
+
|
|
169
|
+
if (field.minimum !== undefined && value < field.minimum)
|
|
170
|
+
return { kind: 'kay.invalid.tooSmall', english: `Expected ${type} to be greater or equal to ${field.minimum}`, params: { min: field.minimum } }
|
|
171
|
+
|
|
172
|
+
if (field.maximum !== undefined && value > field.maximum)
|
|
173
|
+
return { kind: 'kay.invalid.tooBig', english: `Expected ${type} to be less or equal to ${field.maximum}`, params: { max: field.maximum } }
|
|
174
|
+
|
|
175
|
+
if (field.exclusiveMinimum !== undefined && value <= field.exclusiveMinimum)
|
|
176
|
+
return typeRefusal(`Expected ${type} to be greater than ${field.exclusiveMinimum}`)
|
|
177
|
+
|
|
178
|
+
return field.exclusiveMaximum !== undefined && value >= field.exclusiveMaximum ? typeRefusal(`Expected ${type} to be less than ${field.exclusiveMaximum}`) : undefined
|
|
179
|
+
}
|
package/src/runtime/form.ts
CHANGED
|
@@ -38,7 +38,8 @@ const TRAILING_DIGITS = /\d+$/
|
|
|
38
38
|
// A form posts text, as a query string is, and each field once per value or not at all: a top-level field is read the way its schema expects before it is validated.
|
|
39
39
|
// The server reads every post this way, and the browser a form it checks before posting it.
|
|
40
40
|
// `form` reads the text as a form sent it: a required list or boolean absent as empty or unticked, which only a form means by leaving it out, the spaces around what was typed dropped, and an optional field left blank as left out.
|
|
41
|
-
|
|
41
|
+
// `trim` false keeps the spaces around a text, as an API route reads what it is posted.
|
|
42
|
+
export function formInput(schema: TSchema, body: unknown, form = true, trim = form): unknown {
|
|
42
43
|
const { properties, required = [] } = schema as { properties?: Record<string, FieldSchema>, required?: string[] }
|
|
43
44
|
|
|
44
45
|
if (!properties || typeof body !== 'object' || body === null)
|
|
@@ -59,7 +60,7 @@ export function formInput(schema: TSchema, body: unknown, form = true): unknown
|
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
// Spaces around what was typed are no part of it, so `minLength` counts the text.
|
|
62
|
-
const text =
|
|
63
|
+
const text = trim && field.trim !== false && !isSecret(name) ? trimmed(value) : value
|
|
63
64
|
|
|
64
65
|
if (optional && text === '' && (form || !acceptsBlank(field)))
|
|
65
66
|
delete fields[name]
|
package/src/runtime/html.ts
CHANGED
|
@@ -287,6 +287,11 @@ export function fieldDescribed(own?: unknown, type?: unknown, ownRequired?: unkn
|
|
|
287
287
|
return attribute('aria-describedby', fieldDescription(own)) + requiredAttributes(fieldRequired(), type, ownRequired, ownNative) + invalidAttribute(type, ownInvalid)
|
|
288
288
|
}
|
|
289
289
|
|
|
290
|
+
// A property of a source that a later source sets again is the later one's, and a spread `children` the element's own lines replace is not read at all, as reading it renders it: a closed dialog spreading its other props would draw what it does not show.
|
|
291
|
+
function isSkipped(sources: readonly object[], index: number, name: string, inner: unknown, isVoid: boolean): boolean {
|
|
292
|
+
return sources.some((later, position) => position > index && later != null && Object.hasOwn(later, name)) || ((inner !== undefined || isVoid) && CONTENT_PROPERTIES.has(name))
|
|
293
|
+
}
|
|
294
|
+
|
|
290
295
|
// An element whose attributes are only known at render time: spreads, or a component tag that names an element.
|
|
291
296
|
// An attribute set by several sources takes the last one's value and place; `content`, the markup of the lines under the tag, wins over a `children` or `innerHTML` property.
|
|
292
297
|
export function element(written: string, sources: readonly object[], content?: () => string): Html {
|
|
@@ -306,7 +311,7 @@ export function element(written: string, sources: readonly object[], content?: (
|
|
|
306
311
|
continue
|
|
307
312
|
|
|
308
313
|
for (const name of Object.keys(source)) {
|
|
309
|
-
if (sources
|
|
314
|
+
if (isSkipped(sources, index, name, inner, isVoid))
|
|
310
315
|
continue
|
|
311
316
|
|
|
312
317
|
const value = (source as Record<string, unknown>)[name]
|
package/src/runtime/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { s } from '#schema'
|
|
2
2
|
export type { Schema, Static } from '#schema'
|
|
3
|
+
export type { Issue, Refusal, Validated, ValidateOptions, Source as ValidateSource } from '#validate'
|
|
4
|
+
export { validate } from '#validate'
|
|
3
5
|
export type { CookieDeleteOptions } from '../server/cookies'
|
|
4
6
|
export type { Action, ActionContext, ActionDone, ActionErrorDetail, ActionFailed, ActionFor, ActionForm, ActionHooks, ActionProtect, ActionProtection, ActionRateLimit, ActionRun, ActionStartDetail, ActionStream, ActionSuccessDetail, Answer, Called, CallOptions, CallPromise, CallQueue, CallResult, Failure, FailureData, FailureOf, FieldErrors, FieldName, FormFailure, FormSuccess, HoneypotOptions, InputOf, MinTimeOptions, MinTimeRefusal, NoInputAction, ProofOfWorkOptions, ProofOfWorkRefusal, ProtectionMessages, RateKey, RateKeyContext, RateLimit, RateLimitRefusal, RateStore, RateWindow, Refreshable, ResolvedProtection, RetryOptions, ServerAction, Success, SuccessOf, TrackedCall, TrackHooks } from './action'
|
|
5
7
|
export { action, ActionError, ActionRedirectError, carrySchema, formErrors, postedChecked, postedValue, postedValues } from './action'
|
|
@@ -40,6 +40,8 @@ export interface RenderedIslands {
|
|
|
40
40
|
chunks?: Record<string, string> | undefined
|
|
41
41
|
// The message keys the islands use, which go to the browser with them.
|
|
42
42
|
messages: Set<string>
|
|
43
|
+
// The keys of the components an eager island drew that load on their own (`lazyNote`), whose chunks the page preloads.
|
|
44
|
+
lazies: Set<string>
|
|
43
45
|
// Set to what the first island handed an action says of it, which a build that left out the code reviving one cannot serve.
|
|
44
46
|
actions?: string | undefined
|
|
45
47
|
}
|
|
@@ -57,6 +59,7 @@ export function islandCheckpoint(): (() => void) | undefined {
|
|
|
57
59
|
const immediate = islands.immediate.size
|
|
58
60
|
const waiting = islands.waiting.size
|
|
59
61
|
const messages = islands.messages.size
|
|
62
|
+
const lazies = islands.lazies.size
|
|
60
63
|
const actions = islands.actions
|
|
61
64
|
|
|
62
65
|
return () => {
|
|
@@ -68,6 +71,7 @@ export function islandCheckpoint(): (() => void) | undefined {
|
|
|
68
71
|
trim(islands.immediate, immediate)
|
|
69
72
|
trim(islands.waiting, waiting)
|
|
70
73
|
trim(islands.messages, messages)
|
|
74
|
+
trim(islands.lazies, lazies)
|
|
71
75
|
}
|
|
72
76
|
}
|
|
73
77
|
|
|
@@ -89,7 +93,7 @@ function trim(set: Set<string>, size: number): void {
|
|
|
89
93
|
|
|
90
94
|
// Runs a synchronous page render and lists the islands it rendered.
|
|
91
95
|
export function collectIslands<T>(render: () => T, salt?: string, chunks?: Record<string, string>): { result: T, islands: RenderedIslands } {
|
|
92
|
-
const islands: RenderedIslands = { used: new Set(), eager: new Set(), immediate: new Set(), waiting: new Set(), lazy: 0, count: 0, messages: new Set(), chunks }
|
|
96
|
+
const islands: RenderedIslands = { used: new Set(), eager: new Set(), immediate: new Set(), waiting: new Set(), lazy: 0, count: 0, messages: new Set(), lazies: new Set(), chunks }
|
|
93
97
|
|
|
94
98
|
rendered = islands
|
|
95
99
|
|
|
@@ -101,6 +105,29 @@ export function collectIslands<T>(render: () => T, salt?: string, chunks?: Recor
|
|
|
101
105
|
}
|
|
102
106
|
}
|
|
103
107
|
|
|
108
|
+
// The keys of the components the island being rendered draws that load on their own in the browser.
|
|
109
|
+
let drawn: Set<string> | undefined
|
|
110
|
+
|
|
111
|
+
// A component the browser loads when it draws (`KayLazy` in the island's code): drawing it here is the same call, which tells the island its code is needed before it hydrates.
|
|
112
|
+
// The component is read when it draws, as a module importing it back may still be loading when this one is.
|
|
113
|
+
export function lazyNote<P>(key: string, component: () => (props: P) => unknown): (props: P) => unknown {
|
|
114
|
+
return (props) => {
|
|
115
|
+
drawn?.add(key)
|
|
116
|
+
|
|
117
|
+
return component()(props)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// The components the island drew that load on their own, which the browser loads before it hydrates it, and the page preloads unless the island waits.
|
|
122
|
+
function lazyAttribute(keys: Set<string>, waits: boolean): string {
|
|
123
|
+
if (!waits) {
|
|
124
|
+
for (const key of keys)
|
|
125
|
+
rendered?.lazies.add(key)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return keys.size === 0 ? '' : ` data-lazy="${escapeAttribute([...keys].join(' '))}"`
|
|
129
|
+
}
|
|
130
|
+
|
|
104
131
|
// What renders inside an island renders in the browser as well, so its markup carries the markers hydration claims.
|
|
105
132
|
export function insideIsland(): boolean {
|
|
106
133
|
return depth > 0
|
|
@@ -212,6 +239,11 @@ export function island<P extends object>(id: string, component: ((props: P) => J
|
|
|
212
239
|
}
|
|
213
240
|
}
|
|
214
241
|
|
|
242
|
+
const keys = new Set<string>()
|
|
243
|
+
const outer = drawn
|
|
244
|
+
|
|
245
|
+
drawn = keys
|
|
246
|
+
|
|
215
247
|
try {
|
|
216
248
|
// Detached, as in the browser, where an island reads nothing the page's components provide.
|
|
217
249
|
const { result: html, count } = browserOnly ? { result: placeholder((props as Record<string, unknown>)[USE_FALLBACK] as JSX.Element | undefined, ownFallback), count: 0 } : withFieldMarks(undefined, () => renderOnServer(() => scope(() => countIds(prefix, () => issueScope(undefined, () => child(component!(given)))), true)))
|
|
@@ -221,12 +253,14 @@ export function island<P extends object>(id: string, component: ((props: P) => J
|
|
|
221
253
|
// `load` is written out too, so a visitor asking to save data, whose islands wait by default, sees the one that asked to go at once.
|
|
222
254
|
const marked = waits || when === 'load' ? ` data-hydrate="${escapeAttribute(when!)}"` : ''
|
|
223
255
|
const chunk = rendered?.chunks?.[id]
|
|
224
|
-
const
|
|
256
|
+
const lazies = lazyAttribute(keys, waits)
|
|
257
|
+
const markup: Html = renderedHtml(`<slot data-component="${escapeAttribute(id)}" data-props=${quotedAttribute(serialized)}${lazies}${cause === undefined ? '' : ` data-why=${quotedAttribute(cause)}`}${marked}${titled}${shipped}${browserOnly ? ' data-client' : ''}${count === 0 ? '' : ` data-uid="${escapeAttribute(prefix)}"`}${persist === undefined ? '' : ` data-persist="${escapeAttribute(persist)}"`}${chunk === undefined ? '' : ` data-chunk="${escapeAttribute(chunk)}"`}>${html}</slot>`)
|
|
225
258
|
|
|
226
259
|
return markup as unknown as JSX.Element
|
|
227
260
|
}
|
|
228
261
|
finally {
|
|
229
262
|
depth--
|
|
263
|
+
drawn = outer
|
|
230
264
|
}
|
|
231
265
|
}
|
|
232
266
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { t } from 'elysia'
|
|
2
2
|
import type { Schema } from './schema'
|
|
3
|
+
import { isStandardSchema } from './standard-schema'
|
|
3
4
|
|
|
4
5
|
// A plain record of schemas, which every API taking a schema reads as the object of those fields.
|
|
5
6
|
export type SchemaShape = Record<string, Schema>
|
|
@@ -22,7 +23,7 @@ export function isSchema(value: unknown): value is Schema {
|
|
|
22
23
|
|
|
23
24
|
// A schema as it is, a record as the strict object of its fields: a key the record lacks is refused, as `s.object(shape)` does.
|
|
24
25
|
export function schemaOf<Input extends SchemaInput>(input: Input): SchemaOf<Input> {
|
|
25
|
-
if (isSchema(input))
|
|
26
|
+
if (isSchema(input) || isStandardSchema(input))
|
|
26
27
|
return input as SchemaOf<Input>
|
|
27
28
|
|
|
28
29
|
const required = Object.entries(input).filter(([, field]) => !(OPTIONAL in field)).map(([name]) => name)
|