@kayjs/kay 1.106.0 → 1.108.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 +477 -204
- package/editors/zed/extension.toml +2 -2
- package/llms/action-protection.md +1 -1
- package/llms/actions-from-islands.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/component-libraries.md +1 -1
- package/llms/components.md +1 -1
- package/llms/deprecated-lints.md +2 -4
- package/llms/island-and-form-lints.md +1 -0
- package/llms/islands.md +11 -2
- 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 +25 -1
- package/llms/whats-new-archive.md +36 -1
- package/llms/whats-new.md +18 -35
- package/llms.txt +1 -1
- package/package.json +15 -6
- package/src/build/client.ts +23 -26
- package/src/build/entry-files.ts +19 -0
- package/src/build/icon-slots.ts +140 -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/libraries.ts +24 -2
- package/src/build/plugin.ts +13 -1
- package/src/build/runtime-chunk.ts +3 -3
- package/src/build/source-files.ts +8 -3
- package/src/build/transform.ts +17 -7
- package/src/build/variants.ts +97 -5
- package/src/check/action-options.ts +56 -0
- package/src/check/project.ts +4 -4
- package/src/check/renamed-packages.ts +3 -63
- package/src/check/schema-json.ts +226 -0
- package/src/check/shadow-types.ts +15 -27
- package/src/compiler/codegen.ts +7 -17
- package/src/compiler/dead-code.ts +22 -0
- 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 +8 -65
- package/src/compiler/dom-property.ts +23 -0
- package/src/compiler/dom.ts +15 -15
- 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/package-names.ts +1 -1
- package/src/runtime/action-call.ts +39 -327
- package/src/runtime/action-run.ts +357 -0
- 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/icon-slots.d.ts +7 -0
- package/types/build/icon-slots.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/libraries.d.ts +2 -0
- package/types/build/libraries.d.ts.map +1 -1
- package/types/build/plugin.d.ts +2 -0
- package/types/build/plugin.d.ts.map +1 -1
- package/types/build/source-files.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/renamed-packages.d.ts +0 -3
- package/types/check/renamed-packages.d.ts.map +1 -1
- 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/dead-code.d.ts +9 -0
- package/types/compiler/dead-code.d.ts.map +1 -0
- 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 -53
- package/types/compiler/deprecated-lints.d.ts.map +1 -1
- package/types/compiler/dom-property.d.ts +9 -0
- package/types/compiler/dom-property.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/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 -53
- package/types/compiler/lints.d.ts.map +1 -1
- 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-call.d.ts.map +1 -1
- package/types/runtime/action-run.d.ts +23 -0
- package/types/runtime/action-run.d.ts.map +1 -0
- 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,357 @@
|
|
|
1
|
+
import type { Caller } from './action-owner'
|
|
2
|
+
import type { ActionStream, CallOptions, CallQueue, Outcome } from './action-shared'
|
|
3
|
+
import type { Duration } from './duration'
|
|
4
|
+
import type { ConnectionStatus } from './live'
|
|
5
|
+
import { ActionError, ActionRedirectError } from './action-error'
|
|
6
|
+
import { networkFailures, superseding } from './action-marks'
|
|
7
|
+
import { refreshed, refreshedIn, refreshedUnlessFollowed } from './action-navigate'
|
|
8
|
+
import { onStreamEnd, SETTLED } from './action-owner'
|
|
9
|
+
import { post } from './action-post'
|
|
10
|
+
import { apiErrorOf } from './api-error'
|
|
11
|
+
import { durationSeconds, isZeroDuration } from './duration'
|
|
12
|
+
import { navigation } from './navigation'
|
|
13
|
+
import { released } from './optimistic'
|
|
14
|
+
import { randomId } from './random-id'
|
|
15
|
+
import { untrack } from './reactive'
|
|
16
|
+
|
|
17
|
+
// What runs a call of an action: its queue, timeout and retries, the post and what follows the answer. An action shows its state to a page before any call, so that part stays with the action (`action-call.ts`) and a page that calls nothing does not carry this one, which the first call loads.
|
|
18
|
+
|
|
19
|
+
const RETRY_DELAY_MS = 500
|
|
20
|
+
|
|
21
|
+
// The calls of an action, or of one `track()` handle, that `queue` orders: `tail` settles when the last `serial` call is done, `latest` is the call in flight a newer one aborts.
|
|
22
|
+
export interface Lane {
|
|
23
|
+
tail: Promise<unknown>
|
|
24
|
+
queue?: CallQueue | undefined
|
|
25
|
+
latest?: AbortController | undefined
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// What the action keeps for a call to change: its URL, the calls in flight, the outcome of the last call and the progress of an upload.
|
|
29
|
+
export interface Host {
|
|
30
|
+
url: () => string
|
|
31
|
+
shift: (by: number) => void
|
|
32
|
+
called: (page: number, outcome: Outcome | null, turn: number) => unknown
|
|
33
|
+
progress: (sent: number | undefined) => void
|
|
34
|
+
// Whether the call numbered `turn` is the one started last, which owns the outcome and the progress.
|
|
35
|
+
latest: (turn: number) => boolean
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// What the call captured when it was made, before this code was loaded: the page it was made on, the visits so far, who called, and its number.
|
|
39
|
+
export interface Start {
|
|
40
|
+
page: number
|
|
41
|
+
visits: number | undefined
|
|
42
|
+
caller: Caller
|
|
43
|
+
turn: number
|
|
44
|
+
// Puts back what the call's `optimistic` change wrote, which the action applied before this code loaded.
|
|
45
|
+
undo?: (() => void) | undefined
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function idempotencyKey(): string {
|
|
49
|
+
return randomId()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// One signal for several, aborting with the reason of the first to abort. A lone one is returned as it is, so a browser that has no `AbortSignal.any` still runs the call; `stop` lets go of the listeners, and `always` makes a signal of its own whose sources can be let go of before the call ends.
|
|
53
|
+
function join(sources: (AbortSignal | undefined)[], always = false): { signal: AbortSignal | undefined, stop: (only?: AbortSignal) => void } {
|
|
54
|
+
const list = sources.filter(source => source !== undefined)
|
|
55
|
+
|
|
56
|
+
if (list.length < 2 && !always)
|
|
57
|
+
return { signal: list[0], stop: () => undefined }
|
|
58
|
+
|
|
59
|
+
const controller = new AbortController()
|
|
60
|
+
const forwards = list.map(source => [source, () => controller.abort(source.reason)] as const)
|
|
61
|
+
|
|
62
|
+
for (const [source, forward] of forwards) {
|
|
63
|
+
if (source.aborted) {
|
|
64
|
+
controller.abort(source.reason)
|
|
65
|
+
|
|
66
|
+
break
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
source.addEventListener('abort', forward, { once: true })
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
signal: controller.signal,
|
|
74
|
+
stop: (only) => {
|
|
75
|
+
for (const [source, forward] of forwards) {
|
|
76
|
+
if (only === undefined || only === source)
|
|
77
|
+
source.removeEventListener('abort', forward)
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Waits for `ms`, or for the calls ahead when given a promise, giving up at once when the signal aborts meanwhile.
|
|
84
|
+
async function waitFor(wait: number | Promise<unknown>, signal: AbortSignal | undefined): Promise<void> {
|
|
85
|
+
signal?.throwIfAborted()
|
|
86
|
+
|
|
87
|
+
let stop = () => undefined as void
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
await new Promise<unknown>((resolve, reject) => {
|
|
91
|
+
const abort = () => reject(signal!.reason)
|
|
92
|
+
const id = typeof wait === 'number' ? setTimeout(resolve, wait) : undefined
|
|
93
|
+
|
|
94
|
+
if (typeof wait !== 'number') {
|
|
95
|
+
void (async () => {
|
|
96
|
+
try {
|
|
97
|
+
resolve(await wait)
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
reject(error)
|
|
101
|
+
}
|
|
102
|
+
})()
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
signal?.addEventListener('abort', abort, { once: true })
|
|
106
|
+
stop = () => {
|
|
107
|
+
clearTimeout(id)
|
|
108
|
+
signal?.removeEventListener('abort', abort)
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
stop()
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// How long a wait of the call's lasts: nothing for a zero, a clear error for text that is no duration.
|
|
118
|
+
function waitOf(value: Duration | undefined, what: string): number | undefined {
|
|
119
|
+
return value === undefined || isZeroDuration(value) ? undefined : durationSeconds(value, what) * 1000
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Whether a failure is one a retry may repeat: the server's answer or the network's failure, never a redirect, an abort or something thrown before the post was sent. Unless `when` says otherwise, a 503 (the server turned the call away) and a failure before the post was sent are, and nothing else, as a post that reached the server may have run.
|
|
123
|
+
function retryOf(retry: CallOptions['retry']): { times: number, delay: number, again: (error: unknown, sent: boolean) => boolean } {
|
|
124
|
+
const options = typeof retry === 'number' ? { times: retry } : retry
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
times: options?.times ?? 0,
|
|
128
|
+
delay: waitOf(options?.delay, 'retry.delay') ?? RETRY_DELAY_MS,
|
|
129
|
+
again: (error, sent) => {
|
|
130
|
+
if (error instanceof ActionRedirectError || !(error instanceof ActionError || networkFailures.has(error as object) || (error as { name?: unknown } | null)?.name === 'TimeoutError'))
|
|
131
|
+
return false
|
|
132
|
+
|
|
133
|
+
const failure = apiErrorOf(error)
|
|
134
|
+
|
|
135
|
+
return options?.when ? options.when(failure) : failure.status === 503 || (!sent && (failure.status === 0 || failure.status === 502))
|
|
136
|
+
},
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// A hook never fails the call it observes: a throw, or a rejection, is logged.
|
|
141
|
+
async function hooked<Event>(hook: ((event: Event) => unknown) | undefined, event: Event): Promise<void> {
|
|
142
|
+
try {
|
|
143
|
+
await hook?.(event)
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
console.error(error)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// A call of an action `with()` fixed fields of is made with them, over the ones it is given, as a form posting to its URL does: a field fixed to `null` or `undefined` is left out there, so the given one stays.
|
|
151
|
+
function withFixed(input: unknown, fixed: Record<string, unknown> | undefined): unknown {
|
|
152
|
+
if (fixed === undefined)
|
|
153
|
+
return input
|
|
154
|
+
|
|
155
|
+
const set = Object.entries(fixed).filter(([name, value]) => name !== '__proto__' && value !== undefined && value !== null)
|
|
156
|
+
|
|
157
|
+
if (!(input instanceof FormData))
|
|
158
|
+
return { ...input as object | undefined, ...Object.fromEntries(set) }
|
|
159
|
+
|
|
160
|
+
const names = new Set(set.map(([name]) => name))
|
|
161
|
+
const copy = new FormData()
|
|
162
|
+
|
|
163
|
+
for (const [name, value] of input) {
|
|
164
|
+
if (!names.has(name))
|
|
165
|
+
copy.append(name, value)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
for (const [name, value] of set) {
|
|
169
|
+
for (const item of Array.isArray(value) ? value : [value])
|
|
170
|
+
copy.append(name, item instanceof Date ? item.toISOString() : String(item))
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return copy
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Refreshes what a call changed once it is done, started before the caller goes on, so a navigation it starts next supersedes the refresh rather than the other way round; a site with `navigation: false` shows a call's effects itself.
|
|
177
|
+
function refresher(options: CallOptions | undefined, caller: Caller, left: () => boolean): () => void {
|
|
178
|
+
return () => {
|
|
179
|
+
const scope = options?.refresh
|
|
180
|
+
|
|
181
|
+
if (scope === false || left())
|
|
182
|
+
return
|
|
183
|
+
|
|
184
|
+
if (scope === undefined || scope === true) {
|
|
185
|
+
if (navigation.hooks)
|
|
186
|
+
void (scope === true ? refreshed() : refreshedUnlessFollowed(caller.invalidated))
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
const asked = [scope].flat()
|
|
190
|
+
const names = asked.filter(source => typeof source === 'string')
|
|
191
|
+
|
|
192
|
+
if (names.length > 0 && navigation.hooks)
|
|
193
|
+
void refreshed(names)
|
|
194
|
+
|
|
195
|
+
void refreshedIn(asked.filter(source => typeof source !== 'string'))
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Posts the call, again as its retry option allows. What the post found out of the call's own, whether it was sent and whether its timeout passed, is left in `flags`.
|
|
201
|
+
async function postTries(host: Host, input: unknown, options: CallOptions | undefined, signal: AbortSignal | undefined, caller: Caller, left: () => boolean, flags: { sent: boolean, timedOut: boolean }): Promise<unknown> {
|
|
202
|
+
const timeoutOption = options?.timeout
|
|
203
|
+
const timeout = waitOf(timeoutOption, 'timeout')
|
|
204
|
+
const attempts = retryOf(options?.retry)
|
|
205
|
+
// One key for every try of the call, which a handler reads from `ctx.request` to tell a repeat from a new call.
|
|
206
|
+
const key = attempts.times > 0 ? idempotencyKey() : undefined
|
|
207
|
+
|
|
208
|
+
for (let attempt = 0; ; attempt++) {
|
|
209
|
+
// The wait for the answer counts from the moment the post is sent, and each try has its own.
|
|
210
|
+
const limit = timeout === undefined ? undefined : new AbortController()
|
|
211
|
+
const attemptSignal = join([signal, limit?.signal])
|
|
212
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
213
|
+
let sentNow = false
|
|
214
|
+
|
|
215
|
+
try {
|
|
216
|
+
return await post(host.url(), input, host.progress, attemptSignal.signal, () => {
|
|
217
|
+
flags.sent = sentNow = true
|
|
218
|
+
clearTimeout(timer)
|
|
219
|
+
timer = limit && setTimeout(() => {
|
|
220
|
+
flags.timedOut = true
|
|
221
|
+
limit.abort(new DOMException(`The action call timeout of ${String(timeoutOption)} passed before the server answered`, 'TimeoutError'))
|
|
222
|
+
}, timeout)
|
|
223
|
+
}, left, caller, { key, options })
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
// Only the upload path says `sent: false`, as `fetch` gives no such signal and `onSent` has run before it: the default retry of an unsent post covers an upload alone.
|
|
227
|
+
if (attempt >= attempts.times || signal?.aborted === true || !attempts.again(error, sentNow && (error as { sent?: unknown } | null)?.sent !== false))
|
|
228
|
+
throw error
|
|
229
|
+
|
|
230
|
+
await waitFor(attempts.delay * 2 ** attempt, signal)
|
|
231
|
+
}
|
|
232
|
+
finally {
|
|
233
|
+
clearTimeout(timer)
|
|
234
|
+
attemptSignal.stop()
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// A call of a `latest` queue aborts the one in flight before it.
|
|
240
|
+
function supersede(lane: Lane, later: AbortController): void {
|
|
241
|
+
const reason = new DOMException('Superseded by a later call', 'AbortError')
|
|
242
|
+
|
|
243
|
+
superseding.add(reason)
|
|
244
|
+
lane.latest?.abort(reason)
|
|
245
|
+
lane.latest = later
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Runs a call in its lane, whose in flight count the action started and this ends.
|
|
249
|
+
export async function run(host: Host, start: Start, lane: Lane, given: unknown, fixed: Record<string, unknown> | undefined, options?: CallOptions): Promise<unknown> {
|
|
250
|
+
const { page, visits, caller, turn, undo } = start
|
|
251
|
+
const input = withFixed(given, fixed)
|
|
252
|
+
const left = () => navigation.visits !== visits
|
|
253
|
+
const mode = options?.queue ?? lane.queue ?? 'parallel'
|
|
254
|
+
|
|
255
|
+
void hooked(options?.onStart, { input })
|
|
256
|
+
|
|
257
|
+
let posted = false
|
|
258
|
+
const flags = { sent: false, timedOut: false }
|
|
259
|
+
// A stream keeps the call in flight until it ends.
|
|
260
|
+
let streaming = false
|
|
261
|
+
let answer: unknown
|
|
262
|
+
let failure: unknown
|
|
263
|
+
let release: (() => void) | undefined
|
|
264
|
+
const refreshPage = refresher(options, caller, left)
|
|
265
|
+
const later = mode === 'latest' ? new AbortController() : undefined
|
|
266
|
+
// What aborts the call as a whole: its own signal, and a later call of a `latest` queue, which stops mattering once the post is answered.
|
|
267
|
+
const whole = join([options?.signal, later?.signal], later !== undefined)
|
|
268
|
+
|
|
269
|
+
try {
|
|
270
|
+
if (later)
|
|
271
|
+
supersede(lane, later)
|
|
272
|
+
|
|
273
|
+
if (mode === 'serial') {
|
|
274
|
+
const before = lane.tail
|
|
275
|
+
|
|
276
|
+
lane.tail = Promise.all([before, new Promise<void>((resolve) => {
|
|
277
|
+
release = resolve
|
|
278
|
+
})])
|
|
279
|
+
await waitFor(before, whole.signal)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
answer = await postTries(host, input, options, whole.signal, caller, left, flags)
|
|
283
|
+
|
|
284
|
+
posted = true
|
|
285
|
+
whole.stop(later?.signal)
|
|
286
|
+
release?.()
|
|
287
|
+
release = undefined
|
|
288
|
+
|
|
289
|
+
const streamEnded = (status: ConnectionStatus) => {
|
|
290
|
+
if (status === 'done') {
|
|
291
|
+
host.called(page, { action: host.url(), succeeded: true, data: untrack((answer as ActionStream<unknown>).last) }, turn)
|
|
292
|
+
refreshPage()
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
host.shift(-1)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
streaming = onStreamEnd(answer, streamEnded)
|
|
299
|
+
|
|
300
|
+
if (streaming) {
|
|
301
|
+
// A retry of the stream is the call running again.
|
|
302
|
+
caller.retried = (again) => {
|
|
303
|
+
host.shift(1)
|
|
304
|
+
again[SETTLED](streamEnded)
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (!streaming) {
|
|
309
|
+
host.called(page, { action: host.url(), succeeded: true, data: answer }, turn)
|
|
310
|
+
refreshPage()
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
void hooked(options?.onSuccess, { data: answer })
|
|
314
|
+
|
|
315
|
+
return answer
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
failure = error
|
|
319
|
+
|
|
320
|
+
// A redirect leaves the page as it is until the next one shows.
|
|
321
|
+
if (!posted && !(error instanceof ActionRedirectError))
|
|
322
|
+
undo?.()
|
|
323
|
+
|
|
324
|
+
// The server may have run a call aborted or timed out once it was sent, which the page then shows as it stands.
|
|
325
|
+
if (flags.sent && (options?.signal?.aborted === true || flags.timedOut))
|
|
326
|
+
refreshPage()
|
|
327
|
+
|
|
328
|
+
if (error instanceof ActionError && !(error instanceof ActionRedirectError))
|
|
329
|
+
host.called(page, { action: host.url(), status: error.status, values: {}, errors: error.errors, data: error.data }, turn)
|
|
330
|
+
|
|
331
|
+
if (!(error instanceof ActionRedirectError) && whole.signal?.aborted !== true)
|
|
332
|
+
void hooked(options?.onError, { error: apiErrorOf(error) })
|
|
333
|
+
|
|
334
|
+
throw error
|
|
335
|
+
}
|
|
336
|
+
finally {
|
|
337
|
+
// A stream is aborted by the call's own signal until it ends.
|
|
338
|
+
if (!streaming)
|
|
339
|
+
whole.stop()
|
|
340
|
+
|
|
341
|
+
release?.()
|
|
342
|
+
|
|
343
|
+
if (undo)
|
|
344
|
+
released(undo)
|
|
345
|
+
|
|
346
|
+
if (!streaming)
|
|
347
|
+
host.shift(-1)
|
|
348
|
+
|
|
349
|
+
if (host.latest(turn))
|
|
350
|
+
host.progress(undefined)
|
|
351
|
+
|
|
352
|
+
if (lane.latest === later)
|
|
353
|
+
lane.latest = undefined
|
|
354
|
+
|
|
355
|
+
void hooked(options?.onSettled, failure === undefined ? { data: answer } : { error: apiErrorOf(failure) })
|
|
356
|
+
}
|
|
357
|
+
}
|
package/src/runtime/action.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import type { t } from 'elysia'
|
|
2
2
|
import type { ActionContext, ActionProtection, Answer, Called, CallOptions, OutputSchema, ServerAction, Settled, Success } from './action-shared'
|
|
3
3
|
import type { RouteParams } from './checks'
|
|
4
|
-
import type { Schema, Static } from './schema'
|
|
4
|
+
import type { Schema, StandardAs, Static } from './schema'
|
|
5
5
|
import type { ObjectOf, SchemaShape } from './schema-record'
|
|
6
|
+
import type { StandardSchema } from './standard-schema'
|
|
6
7
|
import { callable } from './action-call'
|
|
7
8
|
import { minTimeOf, resolveProtection, workOf } from './action-protection'
|
|
8
9
|
import { ACTION, isAction } from './action-shared'
|
|
9
10
|
import { durationSeconds } from './duration'
|
|
10
11
|
import { KayError } from './kay-error'
|
|
11
|
-
import { schemaOf } from './schema-record'
|
|
12
|
+
import { isSchema, schemaOf } from './schema-record'
|
|
13
|
+
import { isStandardSchema } from './standard-schema'
|
|
12
14
|
import { ACTION_PREFIX } from './wire'
|
|
13
15
|
|
|
14
16
|
export { actionStub, OUTCOME_SCRIPT, setRequestOutcome } from './action-call'
|
|
@@ -44,14 +46,24 @@ const emptySchema = () => ({ [Symbol.for('TypeBox.Kind')]: 'Object', type: 'obje
|
|
|
44
46
|
|
|
45
47
|
// `action` as `kay check` reads it in a page's server fence, whose `ctx.params` are those of the page's route: not something to write.
|
|
46
48
|
export interface ActionFor<Params extends Readonly<Record<string, unknown>>, Guarded extends object = object> {
|
|
49
|
+
<const Honeypot extends string = never, Output = never, Shape extends OutputSchema = OutputSchema>(options: ActionProtection<Honeypot, Success<Settled<Output>>, Shape> & { input?: undefined }, handler: (ctx: ActionContext<Params, string, Guarded>, ctxAfterEmptyInput: ActionContext<Params, string, Guarded>) => Output | Promise<Output>): NoInputAction<Settled<Output>, Honeypot>
|
|
50
|
+
<Spec extends Schema, Output, const Honeypot extends string = never, Shape extends OutputSchema = OutputSchema>(options: ActionProtection<Honeypot, Success<Settled<Output>>, Shape> & { input: Spec }, handler: (input: Static<Spec>, ctx: ActionContext<Params, Extract<keyof Static<Spec>, string>, Guarded>) => Output | Promise<Output>): ServerAction<Spec, Settled<Output>, Honeypot>
|
|
51
|
+
<Validator extends StandardSchema, Output, const Honeypot extends string = never, Shape extends OutputSchema = OutputSchema>(options: ActionProtection<Honeypot, Success<Settled<Output>>, Shape> & { input: Validator }, handler: (input: Static<StandardAs<Validator>>, ctx: ActionContext<Params, Extract<keyof Static<StandardAs<Validator>>, string>, Guarded>) => Output | Promise<Output>): ServerAction<StandardAs<Validator>, Settled<Output>, Honeypot>
|
|
52
|
+
<const Fields extends SchemaShape, Output, const Honeypot extends string = never, Shape extends OutputSchema = OutputSchema>(options: ActionProtection<Honeypot, Success<Settled<Output>>, Shape> & { input: Fields }, handler: (input: Static<ObjectOf<Fields>>, ctx: ActionContext<Params, Extract<keyof Static<ObjectOf<Fields>>, string>, Guarded>) => Output | Promise<Output>): ServerAction<ObjectOf<Fields>, Settled<Output>, Honeypot>
|
|
47
53
|
<Output, const Honeypot extends string = never, Shape extends OutputSchema = OutputSchema>(handler: (ctx: ActionContext<Params, string, Guarded>) => Output | Promise<Output>, protection?: ActionProtection<Honeypot, Success<Settled<Output>>, Shape>): NoInputAction<Settled<Output>, Honeypot>
|
|
48
54
|
<Spec extends Schema, Output, const Honeypot extends string = never, Shape extends OutputSchema = OutputSchema>(schema: Spec, handler: (input: Static<Spec>, ctx: ActionContext<Params, Extract<keyof Static<Spec>, string>, Guarded>) => Output | Promise<Output>, protection?: ActionProtection<Honeypot, Success<Settled<Output>>, Shape>): ServerAction<Spec, Settled<Output>, Honeypot>
|
|
49
55
|
<const Fields extends SchemaShape, Output, const Honeypot extends string = never, Shape extends OutputSchema = OutputSchema>(fields: Fields, handler: (input: Static<ObjectOf<Fields>>, ctx: ActionContext<Params, Extract<keyof Static<ObjectOf<Fields>>, string>, Guarded>) => Output | Promise<Output>, protection?: ActionProtection<Honeypot, Success<Settled<Output>>, Shape>): ServerAction<ObjectOf<Fields>, Settled<Output>, Honeypot>
|
|
50
56
|
}
|
|
51
57
|
|
|
52
|
-
// Declares a server action in a page's `--- server` fence:
|
|
53
|
-
// Without
|
|
58
|
+
// Declares a server action in a page's `--- server` fence: the options come first, as an API route's do, `input` a plain record of fields or any schema that kay checks the post against before `handler` runs.
|
|
59
|
+
// Without an `input` the action takes no input, as with an empty object schema, and its handler gets the context alone.
|
|
60
|
+
// The form before 1.107, `action(schema, handler, options)`, works until 2.0: the `deprecated-action-options` lint rewrites it.
|
|
54
61
|
// kept: free, it registers a route answering a POST, which the page's own render never runs, and a kept page is refreshed after the first request that can write.
|
|
62
|
+
// A handler of two parameters is the form before 1.107, `action({}, (input, ctx) => ...)`, handed the empty input first: its second is the context, as the first of one taking the context alone is.
|
|
63
|
+
export function action<const Honeypot extends string = never, Output = never, Shape extends OutputSchema = OutputSchema>(options: ActionProtection<Honeypot, Success<Settled<Output>>, Shape> & { input?: undefined }, handler: (ctx: ActionContext, ctxAfterEmptyInput: ActionContext) => Output | Promise<Output>): NoInputAction<Settled<Output>, Honeypot>
|
|
64
|
+
export function action<Spec extends Schema, Output, const Honeypot extends string = never, Shape extends OutputSchema = OutputSchema>(options: ActionProtection<Honeypot, Success<Settled<Output>>, Shape> & { input: Spec }, handler: (input: Static<Spec>, ctx: ActionContext<RouteParams, Extract<keyof Static<Spec>, string>>) => Output | Promise<Output>): ServerAction<Spec, Settled<Output>, Honeypot>
|
|
65
|
+
export function action<Validator extends StandardSchema, Output, const Honeypot extends string = never, Shape extends OutputSchema = OutputSchema>(options: ActionProtection<Honeypot, Success<Settled<Output>>, Shape> & { input: Validator }, handler: (input: Static<StandardAs<Validator>>, ctx: ActionContext<RouteParams, Extract<keyof Static<StandardAs<Validator>>, string>>) => Output | Promise<Output>): ServerAction<StandardAs<Validator>, Settled<Output>, Honeypot>
|
|
66
|
+
export function action<const Fields extends SchemaShape, Output, const Honeypot extends string = never, Shape extends OutputSchema = OutputSchema>(options: ActionProtection<Honeypot, Success<Settled<Output>>, Shape> & { input: Fields }, handler: (input: Static<ObjectOf<Fields>>, ctx: ActionContext<RouteParams, Extract<keyof Static<ObjectOf<Fields>>, string>>) => Output | Promise<Output>): ServerAction<ObjectOf<Fields>, Settled<Output>, Honeypot>
|
|
55
67
|
export function action<Output, const Honeypot extends string = never, Shape extends OutputSchema = OutputSchema>(handler: (ctx: ActionContext) => Output | Promise<Output>, protection?: ActionProtection<Honeypot, Success<Settled<Output>>, Shape>): NoInputAction<Settled<Output>, Honeypot>
|
|
56
68
|
export function action<Spec extends Schema, Output, const Honeypot extends string = never, Shape extends OutputSchema = OutputSchema>(schema: Spec, handler: (input: Static<Spec>, ctx: ActionContext<RouteParams, Extract<keyof Static<Spec>, string>>) => Output | Promise<Output>, protection?: ActionProtection<Honeypot, Success<Settled<Output>>, Shape>): ServerAction<Spec, Settled<Output>, Honeypot>
|
|
57
69
|
export function action<const Fields extends SchemaShape, Output, const Honeypot extends string = never, Shape extends OutputSchema = OutputSchema>(fields: Fields, handler: (input: Static<ObjectOf<Fields>>, ctx: ActionContext<RouteParams, Extract<keyof Static<ObjectOf<Fields>>, string>>) => Output | Promise<Output>, protection?: ActionProtection<Honeypot, Success<Settled<Output>>, Shape>): ServerAction<ObjectOf<Fields>, Settled<Output>, Honeypot>
|
|
@@ -62,9 +74,40 @@ export function action(first: unknown, second?: unknown, third?: ActionProtectio
|
|
|
62
74
|
return declare(emptySchema(), (_input, ctx) => handler(ctx), second as ActionProtection<string> | undefined)
|
|
63
75
|
}
|
|
64
76
|
|
|
77
|
+
if (third === undefined && isOptions(first)) {
|
|
78
|
+
const { input, ...options } = first as { input?: Schema | SchemaShape }
|
|
79
|
+
const handler = second as (...args: unknown[]) => unknown
|
|
80
|
+
|
|
81
|
+
// Without an `input` the handler takes the context alone, though an empty record before 1.107 gave it `(input, ctx)`.
|
|
82
|
+
return declare(input === undefined ? emptySchema() : schemaOf(input), input === undefined && handler.length < 2 ? (_input, ctx) => handler(ctx) : handler, options)
|
|
83
|
+
}
|
|
84
|
+
|
|
65
85
|
return declare(schemaOf(first as Schema | SchemaShape), second as (input: unknown, ctx: ActionContext) => unknown, third)
|
|
66
86
|
}
|
|
67
87
|
|
|
88
|
+
// Whether the first argument is the options of the form since 1.107, not the schema or the record of fields of the form before: an object that names an `input` which is a record or an object schema, or names no field at all.
|
|
89
|
+
function isOptions(first: unknown): boolean {
|
|
90
|
+
if (typeof first !== 'object' || first === null || isSchema(first) || isStandardSchema(first))
|
|
91
|
+
return false
|
|
92
|
+
|
|
93
|
+
const entries = Object.entries(first)
|
|
94
|
+
|
|
95
|
+
if ('input' in first) {
|
|
96
|
+
const { input } = first
|
|
97
|
+
|
|
98
|
+
// An `input` that is a schema or a record is options; so is one left undefined. A legacy record holding a field called `input` has a schema beside it, which options never do.
|
|
99
|
+
if (input === undefined)
|
|
100
|
+
return true
|
|
101
|
+
|
|
102
|
+
if (typeof input !== 'object' || input === null)
|
|
103
|
+
return false
|
|
104
|
+
|
|
105
|
+
return !entries.some(([name, value]) => name !== 'input' && (isSchema(value) || isStandardSchema(value)))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return !entries.some(([, value]) => isSchema(value) || isStandardSchema(value))
|
|
109
|
+
}
|
|
110
|
+
|
|
68
111
|
export function declare(schema: Schema, handler: (input: unknown, ctx: ActionContext) => unknown, options: ActionProtection<string> | undefined): ServerAction<Schema, unknown> {
|
|
69
112
|
const protection = resolveProtection(options)
|
|
70
113
|
|
package/src/runtime/client.ts
CHANGED
|
@@ -229,6 +229,12 @@ async function hydrateIsland(registry: IslandRegistry, element: HTMLElement): Pr
|
|
|
229
229
|
|
|
230
230
|
try {
|
|
231
231
|
loaded = await Promise.all([load(), staticSlots(registry, element), livePropsOf(element), reviveActionsIn(element.dataset.props)])
|
|
232
|
+
|
|
233
|
+
// What the server drew of its lazy components loads first, so the markup is claimed as it stands.
|
|
234
|
+
const drawn = (globalThis as { $kayLoadDrawn?: (island: HTMLElement) => Promise<void> }).$kayLoadDrawn
|
|
235
|
+
|
|
236
|
+
if (drawn !== undefined)
|
|
237
|
+
await drawn(element)
|
|
232
238
|
}
|
|
233
239
|
catch (error) {
|
|
234
240
|
// A chunk that failed to load is dropped like a missing one.
|
|
@@ -355,6 +361,10 @@ async function hotSwap(registry: IslandRegistry, swap: HotSwap): Promise<boolean
|
|
|
355
361
|
const chunk = withBase(chunks[0]!)
|
|
356
362
|
|
|
357
363
|
if (!document.querySelector(`${ISLAND}[data-component="${CSS.escape(id)}"]`)) {
|
|
364
|
+
// An island's code an island loaded lazily is not in the page as an island of its own, and keeps the version it loaded: only a reload brings the new one.
|
|
365
|
+
if ((globalThis as { $kayLazyLoaded?: boolean }).$kayLazyLoaded === true)
|
|
366
|
+
return false
|
|
367
|
+
|
|
358
368
|
// Not on this page: a later one rendering it loads the new chunk, unless that would bring code the page runs again.
|
|
359
369
|
if (fits(chunks))
|
|
360
370
|
registry[id] = async () => import(chunk) as ReturnType<IslandRegistry[string]>
|