@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.
Files changed (254) hide show
  1. package/README.md +2 -2
  2. package/dist/eslint.js +477 -204
  3. package/editors/zed/extension.toml +2 -2
  4. package/llms/action-protection.md +1 -1
  5. package/llms/actions-from-islands.md +1 -1
  6. package/llms/actions-refused-fields.md +22 -0
  7. package/llms/actions.md +2 -22
  8. package/llms/browser-only.md +1 -1
  9. package/llms/compiler-api.md +1 -1
  10. package/llms/component-libraries.md +1 -1
  11. package/llms/components.md +1 -1
  12. package/llms/deprecated-lints.md +2 -4
  13. package/llms/island-and-form-lints.md +1 -0
  14. package/llms/islands.md +11 -2
  15. package/llms/project-new-and-add.md +1 -1
  16. package/llms/project.md +2 -2
  17. package/llms/schemas.md +6 -4
  18. package/llms/upgrading.md +25 -1
  19. package/llms/whats-new-archive.md +36 -1
  20. package/llms/whats-new.md +18 -35
  21. package/llms.txt +1 -1
  22. package/package.json +15 -6
  23. package/src/build/client.ts +23 -26
  24. package/src/build/entry-files.ts +19 -0
  25. package/src/build/icon-slots.ts +140 -0
  26. package/src/build/lazy-components.ts +130 -0
  27. package/src/build/lazy-manifest.ts +56 -0
  28. package/src/build/lazy-stubs.ts +31 -0
  29. package/src/build/libraries.ts +24 -2
  30. package/src/build/plugin.ts +13 -1
  31. package/src/build/runtime-chunk.ts +3 -3
  32. package/src/build/source-files.ts +8 -3
  33. package/src/build/transform.ts +17 -7
  34. package/src/build/variants.ts +97 -5
  35. package/src/check/action-options.ts +56 -0
  36. package/src/check/project.ts +4 -4
  37. package/src/check/renamed-packages.ts +3 -63
  38. package/src/check/schema-json.ts +226 -0
  39. package/src/check/shadow-types.ts +15 -27
  40. package/src/compiler/codegen.ts +7 -17
  41. package/src/compiler/dead-code.ts +22 -0
  42. package/src/compiler/deprecated-lints-1-105.ts +41 -0
  43. package/src/compiler/deprecated-lints-1-106.ts +1 -1
  44. package/src/compiler/deprecated-lints-1-107.ts +14 -0
  45. package/src/compiler/deprecated-lints.ts +8 -65
  46. package/src/compiler/dom-property.ts +23 -0
  47. package/src/compiler/dom.ts +15 -15
  48. package/src/compiler/import-edits.ts +15 -0
  49. package/src/compiler/index.ts +8 -3
  50. package/src/compiler/island-declaration.ts +15 -0
  51. package/src/compiler/kay-exports.ts +1 -0
  52. package/src/compiler/lazy-imports.ts +357 -0
  53. package/src/compiler/parser.ts +2 -2
  54. package/src/compiler/route-lints.ts +2 -0
  55. package/src/compiler/schema-lints.ts +13 -0
  56. package/src/compiler/script-server.ts +6 -3
  57. package/src/content/validate.ts +1 -1
  58. package/src/new.ts +6 -2
  59. package/src/package-names.ts +1 -1
  60. package/src/runtime/action-call.ts +39 -327
  61. package/src/runtime/action-run.ts +357 -0
  62. package/src/runtime/action.ts +47 -4
  63. package/src/runtime/client.ts +10 -0
  64. package/src/runtime/dom-lazy.ts +276 -0
  65. package/src/runtime/field-errors.ts +58 -0
  66. package/src/runtime/field-refusal.ts +179 -0
  67. package/src/runtime/form.ts +3 -2
  68. package/src/runtime/html.ts +6 -1
  69. package/src/runtime/index.ts +2 -0
  70. package/src/runtime/island.server.ts +36 -2
  71. package/src/runtime/schema-record.ts +2 -1
  72. package/src/runtime/schema.client.ts +104 -14
  73. package/src/runtime/schema.ts +9 -2
  74. package/src/runtime/standard-schema.ts +32 -0
  75. package/src/runtime/validate-run.client.ts +179 -0
  76. package/src/runtime/validate.client.ts +11 -0
  77. package/src/runtime/validate.ts +4 -137
  78. package/src/server/actions.ts +48 -43
  79. package/src/server/answer.ts +102 -0
  80. package/src/server/api-input.ts +166 -0
  81. package/src/server/api.ts +12 -44
  82. package/src/server/app-stages.ts +15 -8
  83. package/src/server/app.ts +5 -2
  84. package/src/server/client.ts +2 -0
  85. package/src/server/dev.ts +7 -9
  86. package/src/server/file-limit.ts +7 -1
  87. package/src/server/forms.ts +1 -1
  88. package/src/server/host-allowlist.ts +7 -7
  89. package/src/server/index.ts +2 -0
  90. package/src/server/kay-routes.ts +221 -0
  91. package/src/server/kay-server.ts +228 -0
  92. package/src/server/kept-exports.ts +1 -0
  93. package/src/server/library.ts +0 -2
  94. package/src/server/mcp.ts +1 -1
  95. package/src/server/openapi.ts +12 -3
  96. package/src/server/phases.ts +249 -0
  97. package/src/server/protection.ts +6 -6
  98. package/src/server/proxy-key.ts +8 -8
  99. package/src/server/query.ts +4 -25
  100. package/src/server/receive.ts +43 -0
  101. package/src/server/render-document.ts +13 -3
  102. package/src/server/request-context.ts +137 -0
  103. package/src/server/schema-check.ts +1 -1
  104. package/src/server/socket.ts +19 -47
  105. package/src/server/stream.ts +1 -1
  106. package/src/server/validate.ts +346 -0
  107. package/src/test/mount.ts +6 -2
  108. package/types/build/client.d.ts +3 -2
  109. package/types/build/client.d.ts.map +1 -1
  110. package/types/build/entry-files.d.ts +4 -0
  111. package/types/build/entry-files.d.ts.map +1 -0
  112. package/types/build/icon-slots.d.ts +7 -0
  113. package/types/build/icon-slots.d.ts.map +1 -0
  114. package/types/build/lazy-components.d.ts +6 -0
  115. package/types/build/lazy-components.d.ts.map +1 -0
  116. package/types/build/lazy-manifest.d.ts +19 -0
  117. package/types/build/lazy-manifest.d.ts.map +1 -0
  118. package/types/build/lazy-stubs.d.ts +5 -0
  119. package/types/build/lazy-stubs.d.ts.map +1 -0
  120. package/types/build/libraries.d.ts +2 -0
  121. package/types/build/libraries.d.ts.map +1 -1
  122. package/types/build/plugin.d.ts +2 -0
  123. package/types/build/plugin.d.ts.map +1 -1
  124. package/types/build/source-files.d.ts.map +1 -1
  125. package/types/build/transform.d.ts +3 -1
  126. package/types/build/transform.d.ts.map +1 -1
  127. package/types/build/variants.d.ts.map +1 -1
  128. package/types/check/action-options.d.ts +3 -0
  129. package/types/check/action-options.d.ts.map +1 -0
  130. package/types/check/renamed-packages.d.ts +0 -3
  131. package/types/check/renamed-packages.d.ts.map +1 -1
  132. package/types/check/schema-json.d.ts +3 -0
  133. package/types/check/schema-json.d.ts.map +1 -0
  134. package/types/check/shadow-types.d.ts.map +1 -1
  135. package/types/compiler/codegen.d.ts +2 -0
  136. package/types/compiler/codegen.d.ts.map +1 -1
  137. package/types/compiler/dead-code.d.ts +9 -0
  138. package/types/compiler/dead-code.d.ts.map +1 -0
  139. package/types/compiler/deprecated-lints-1-105.d.ts +39 -0
  140. package/types/compiler/deprecated-lints-1-105.d.ts.map +1 -0
  141. package/types/compiler/deprecated-lints-1-106.d.ts +1 -1
  142. package/types/compiler/deprecated-lints-1-106.d.ts.map +1 -1
  143. package/types/compiler/deprecated-lints-1-107.d.ts +12 -0
  144. package/types/compiler/deprecated-lints-1-107.d.ts.map +1 -0
  145. package/types/compiler/deprecated-lints.d.ts +36 -53
  146. package/types/compiler/deprecated-lints.d.ts.map +1 -1
  147. package/types/compiler/dom-property.d.ts +9 -0
  148. package/types/compiler/dom-property.d.ts.map +1 -0
  149. package/types/compiler/dom.d.ts +1 -0
  150. package/types/compiler/dom.d.ts.map +1 -1
  151. package/types/compiler/import-edits.d.ts +1 -0
  152. package/types/compiler/import-edits.d.ts.map +1 -1
  153. package/types/compiler/index.d.ts +2 -0
  154. package/types/compiler/index.d.ts.map +1 -1
  155. package/types/compiler/island-declaration.d.ts +5 -0
  156. package/types/compiler/island-declaration.d.ts.map +1 -1
  157. package/types/compiler/kay-exports.d.ts.map +1 -1
  158. package/types/compiler/lazy-imports.d.ts +33 -0
  159. package/types/compiler/lazy-imports.d.ts.map +1 -0
  160. package/types/compiler/lints.d.ts +44 -53
  161. package/types/compiler/lints.d.ts.map +1 -1
  162. package/types/compiler/route-lints.d.ts +8 -0
  163. package/types/compiler/route-lints.d.ts.map +1 -1
  164. package/types/compiler/schema-lints.d.ts +11 -0
  165. package/types/compiler/schema-lints.d.ts.map +1 -0
  166. package/types/compiler/script-server.d.ts.map +1 -1
  167. package/types/new.d.ts +1 -0
  168. package/types/new.d.ts.map +1 -1
  169. package/types/runtime/action-call.d.ts.map +1 -1
  170. package/types/runtime/action-run.d.ts +23 -0
  171. package/types/runtime/action-run.d.ts.map +1 -0
  172. package/types/runtime/action.d.ts +26 -1
  173. package/types/runtime/action.d.ts.map +1 -1
  174. package/types/runtime/client.d.ts.map +1 -1
  175. package/types/runtime/dom-lazy.d.ts +6 -0
  176. package/types/runtime/dom-lazy.d.ts.map +1 -0
  177. package/types/runtime/field-errors.d.ts +4 -0
  178. package/types/runtime/field-errors.d.ts.map +1 -0
  179. package/types/runtime/field-refusal.d.ts +37 -0
  180. package/types/runtime/field-refusal.d.ts.map +1 -0
  181. package/types/runtime/form.d.ts +1 -1
  182. package/types/runtime/form.d.ts.map +1 -1
  183. package/types/runtime/html.d.ts.map +1 -1
  184. package/types/runtime/index.d.ts +2 -0
  185. package/types/runtime/index.d.ts.map +1 -1
  186. package/types/runtime/island.server.d.ts +2 -0
  187. package/types/runtime/island.server.d.ts.map +1 -1
  188. package/types/runtime/schema-record.d.ts.map +1 -1
  189. package/types/runtime/schema.client.d.ts.map +1 -1
  190. package/types/runtime/schema.d.ts +6 -1
  191. package/types/runtime/schema.d.ts.map +1 -1
  192. package/types/runtime/standard-schema.d.ts +37 -0
  193. package/types/runtime/standard-schema.d.ts.map +1 -0
  194. package/types/runtime/validate-run.client.d.ts +4 -0
  195. package/types/runtime/validate-run.client.d.ts.map +1 -0
  196. package/types/runtime/validate.client.d.ts +4 -0
  197. package/types/runtime/validate.client.d.ts.map +1 -0
  198. package/types/runtime/validate.d.ts +1 -21
  199. package/types/runtime/validate.d.ts.map +1 -1
  200. package/types/server/actions.d.ts.map +1 -1
  201. package/types/server/answer.d.ts +6 -0
  202. package/types/server/answer.d.ts.map +1 -0
  203. package/types/server/api-input.d.ts +19 -0
  204. package/types/server/api-input.d.ts.map +1 -0
  205. package/types/server/api.d.ts.map +1 -1
  206. package/types/server/app-stages.d.ts +4 -1
  207. package/types/server/app-stages.d.ts.map +1 -1
  208. package/types/server/app.d.ts +2 -0
  209. package/types/server/app.d.ts.map +1 -1
  210. package/types/server/client.d.ts +1 -0
  211. package/types/server/client.d.ts.map +1 -1
  212. package/types/server/dev.d.ts +1 -1
  213. package/types/server/dev.d.ts.map +1 -1
  214. package/types/server/file-limit.d.ts.map +1 -1
  215. package/types/server/host-allowlist.d.ts +3 -3
  216. package/types/server/host-allowlist.d.ts.map +1 -1
  217. package/types/server/index.d.ts +2 -0
  218. package/types/server/index.d.ts.map +1 -1
  219. package/types/server/kay-routes.d.ts +47 -0
  220. package/types/server/kay-routes.d.ts.map +1 -0
  221. package/types/server/kay-server.d.ts +25 -0
  222. package/types/server/kay-server.d.ts.map +1 -0
  223. package/types/server/kept-exports.d.ts.map +1 -1
  224. package/types/server/library.d.ts +0 -1
  225. package/types/server/library.d.ts.map +1 -1
  226. package/types/server/openapi.d.ts.map +1 -1
  227. package/types/server/phases.d.ts +36 -0
  228. package/types/server/phases.d.ts.map +1 -0
  229. package/types/server/protection.d.ts +3 -2
  230. package/types/server/protection.d.ts.map +1 -1
  231. package/types/server/proxy-key.d.ts +3 -3
  232. package/types/server/proxy-key.d.ts.map +1 -1
  233. package/types/server/query.d.ts.map +1 -1
  234. package/types/server/receive.d.ts +9 -0
  235. package/types/server/receive.d.ts.map +1 -0
  236. package/types/server/render-document.d.ts.map +1 -1
  237. package/types/server/request-context.d.ts +29 -0
  238. package/types/server/request-context.d.ts.map +1 -0
  239. package/types/server/schema-check.d.ts +1 -0
  240. package/types/server/schema-check.d.ts.map +1 -1
  241. package/types/server/socket.d.ts +3 -5
  242. package/types/server/socket.d.ts.map +1 -1
  243. package/types/server/validate.d.ts +26 -0
  244. package/types/server/validate.d.ts.map +1 -0
  245. package/types/test/mount.d.ts.map +1 -1
  246. package/src/check/schema-type.ts +0 -9
  247. package/src/compiler/deprecated-script-fence.ts +0 -26
  248. package/src/server/field-errors.ts +0 -136
  249. package/types/check/schema-type.d.ts +0 -3
  250. package/types/check/schema-type.d.ts.map +0 -1
  251. package/types/compiler/deprecated-script-fence.d.ts +0 -3
  252. package/types/compiler/deprecated-script-fence.d.ts.map +0 -1
  253. package/types/server/field-errors.d.ts +0 -6
  254. package/types/server/field-errors.d.ts.map +0 -1
@@ -1,24 +1,20 @@
1
- import type { Caller } from './action-owner'
1
+ import type { Lane, Start } from './action-run'
2
2
  import type { Action, ActionErrorDetail, ActionStartDetail, ActionStream, ActionSuccessDetail, CallOptions, CallQueue, Outcome, TrackedCall } from './action-shared'
3
3
  import type { ApiError } from './api-error'
4
4
  import type { CallPromise, CallResult, TrackHooks } from './call-result'
5
- import type { Duration } from './duration'
6
- import type { ConnectionStatus, LoadStatus } from './live'
5
+ import type { LoadStatus } from './live'
7
6
  import { ActionError, ActionRedirectError } from './action-error'
8
7
  import { PAYLOAD_PARAM, payloadQuery, withFields } from './action-form'
9
8
  import { networkFailures, refusedAt, superseding } from './action-marks'
10
- import { refreshed, refreshedIn, refreshedUnlessFollowed } from './action-navigate'
11
- import { onStreamEnd, SETTLED } from './action-owner'
9
+ import { onStreamEnd } from './action-owner'
12
10
  import { ACTION } from './action-shared'
13
- import { actionFailed, apiErrorOf, failed, messageOf, unreachable } from './api-error'
11
+ import { actionFailed, failed, messageOf, unreachable } from './api-error'
14
12
  import { withoutBase } from './base'
15
13
  import { skew } from './build'
16
14
  import { settling, signalAborted, trackCalls, whenIdle } from './call-result'
17
- import { durationSeconds, isZeroDuration } from './duration'
18
15
  import { navigation } from './navigation'
19
16
  import { released, undoable } from './optimistic'
20
17
  import { pageData } from './page-data'
21
- import { randomId } from './random-id'
22
18
  import { getOwner, liveOwner, onCleanup, ownSignal, untrack } from './reactive'
23
19
  import { OUTCOME_SCRIPT } from './swap'
24
20
  import { ACTION_PREFIX, ACTION_TYPE, decodeServerValue, FIXED_FIELDS, malformed, serverWritten } from './wire'
@@ -30,122 +26,14 @@ export { OUTCOME_SCRIPT }
30
26
 
31
27
  const QUERY = /\?.*/
32
28
 
33
- // A property of the handle an action builds, readable and listed.
34
- const read = (value: unknown) => ({ value, enumerable: true })
35
-
36
- const RETRY_DELAY_MS = 500
37
29
  const CODE_RETRIES = 2
38
- // Once loaded, a call is posted in the same task, as one made from `pagehide` must be: a dynamic import resolves a task later, when the page may be gone.
39
- let posting: typeof import('./action-post') | undefined
40
-
41
- function idempotencyKey(): string {
42
- return randomId()
43
- }
44
-
45
- // 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.
46
- interface Lane {
47
- tail: Promise<unknown>
48
- queue?: CallQueue | undefined
49
- latest?: AbortController | undefined
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
- }
30
+ // Once loaded, a call is made in the same task, as one made from `pagehide` must be: a dynamic import resolves a task later, when the page may be gone.
31
+ let running: typeof import('./action-run') | undefined
32
+ // The load the calls made before it ends share, so they go on in the order they were made.
33
+ let loading: Promise<typeof import('./action-run')> | undefined
104
34
 
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
- }
35
+ // A property of the handle an action builds, readable and listed.
36
+ const read = (value: unknown) => ({ value, enumerable: true })
149
37
 
150
38
  // On the server, the form post the request being answered carries: null for none, undefined outside a request.
151
39
  let requestOutcome: (() => Outcome | null | undefined) | undefined
@@ -221,32 +109,6 @@ function lazySignal<T>(value: T): [read: () => T, write: (next: T) => void, peek
221
109
  }, () => current]
222
110
  }
223
111
 
224
- // 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.
225
- function withFixed(input: unknown, fixed: Record<string, unknown> | undefined): unknown {
226
- if (fixed === undefined)
227
- return input
228
-
229
- const set = Object.entries(fixed).filter(([name, value]) => name !== '__proto__' && value !== undefined && value !== null)
230
-
231
- if (!(input instanceof FormData))
232
- return { ...input as object | undefined, ...Object.fromEntries(set) }
233
-
234
- const names = new Set(set.map(([name]) => name))
235
- const copy = new FormData()
236
-
237
- for (const [name, value] of input) {
238
- if (!names.has(name))
239
- copy.append(name, value)
240
- }
241
-
242
- for (const [name, value] of set) {
243
- for (const item of Array.isArray(value) ? value : [value])
244
- copy.append(name, item instanceof Date ? item.toISOString() : String(item))
245
- }
246
-
247
- return copy
248
- }
249
-
250
112
  // Checks a form against the action's schema in the browser, as the server would, and hands `show` the messages to read as a refusal's, undefined for none; one field checked replaces that field's message and leaves the others.
251
113
  function validator(url: () => string, failed: () => { errors: Record<string, string> } | undefined, show: (errors: Record<string, string> | undefined) => unknown): (form: HTMLFormElement, field?: string) => Promise<boolean> {
252
114
  return async (form, field) => {
@@ -264,7 +126,7 @@ function validator(url: () => string, failed: () => { errors: Record<string, str
264
126
  }
265
127
  }
266
128
 
267
- // Posts a call, counting the ones in flight; a call that redirects rejects with `ActionRedirectError` once the browser is on its way, so the action stops being pending.
129
+ // Makes the call of an action, counting the ones in flight; a call that redirects rejects with `ActionRedirectError` once the browser is on its way, so the action stops being pending.
268
130
  // How a post went is the request's on the server; in the browser the last call's once the page shown made one, else the post the page answers.
269
131
  export function callable(url: () => string): { call: (input: never, options?: CallOptions) => CallPromise<CallResult<unknown, unknown>>, reads: PropertyDescriptorMap, bound: Bound } {
270
132
  const [inFlight, setInFlight, flying] = lazySignal(0)
@@ -294,187 +156,41 @@ export function callable(url: () => string): { call: (input: never, options?: Ca
294
156
  }
295
157
  const error = (field = '') => failed()?.errors[field]
296
158
  const validate = validator(url, failed, errors => called(navigation.page, errors === undefined ? null : { action: url(), status: 422, values: {}, errors, data: undefined }))
159
+ const host = { url, shift, called, progress: setProgress, latest: (turn: number) => turn === started }
297
160
 
298
- const run = async (lane: Lane, input: unknown, options?: CallOptions) => {
299
- // The page the call was made on, whose outcome it is even once the visitor went elsewhere.
300
- const page = navigation.page
301
- const visits = navigation.visits
302
- const left = () => navigation.visits !== visits
303
- // The island calling, from its setup or a handler, whose end ends a stream the call answers with; else the page, as long as no other path is shown.
304
- const caller: Caller = { owner: liveOwner(getOwner()), arrivals: navigation.arrivals }
305
- const mode = options?.queue ?? lane.queue ?? 'parallel'
306
-
307
- const turn = ++started
161
+ // The state a call changes is the action's own and is read from the first paint, so a call counts itself in flight at once and hands the rest to the code that runs it, which loads with the first call.
162
+ const run = async (lane: Lane, input: unknown, fixed: Record<string, unknown> | undefined, options?: CallOptions) => {
163
+ const start: Start = { page: navigation.page, visits: navigation.visits, caller: { owner: liveOwner(getOwner()), arrivals: navigation.arrivals }, turn: ++started }
308
164
 
309
165
  shift(1)
310
- called(page, null)
311
- void hooked(options?.onStart, { input })
312
-
313
- let undo: (() => void) | undefined
314
- let posted = false
315
- let sent = false
316
- let timedOut = false
317
- // A stream keeps the call in flight until it ends.
318
- let streaming = false
319
- let answer: unknown
320
- let failure: unknown
321
- let release: (() => void) | undefined
322
- // 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.
323
- const refreshPage = () => {
324
- const scope = options?.refresh
325
-
326
- if (scope === false || left())
327
- return
166
+ called(start.page, null)
328
167
 
329
- if (scope === undefined || scope === true) {
330
- if (navigation.hooks)
331
- void (scope === true ? refreshed() : refreshedUnlessFollowed(caller.invalidated))
332
- }
333
- else {
334
- const asked = [scope].flat()
335
- const names = asked.filter(source => typeof source === 'string')
336
-
337
- if (names.length > 0 && navigation.hooks)
338
- void refreshed(names)
339
-
340
- void refreshedIn(asked.filter(source => typeof source !== 'string'))
341
- }
342
- }
343
- const later = mode === 'latest' ? new AbortController() : undefined
344
- // 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.
345
- const whole = join([options?.signal, later?.signal], later !== undefined)
346
- const timeoutOption = options?.timeout
347
- const timeout = waitOf(timeoutOption, 'timeout')
168
+ let code: typeof import('./action-run')
169
+ let attempt: typeof loading
348
170
 
349
171
  try {
350
- undo = options?.optimistic ? undoable(options.optimistic) : undefined
351
-
352
- if (later) {
353
- const reason = new DOMException('Superseded by a later call', 'AbortError')
354
-
355
- superseding.add(reason)
356
- lane.latest?.abort(reason)
357
- lane.latest = later
358
- }
359
-
360
- if (mode === 'serial') {
361
- const before = lane.tail
362
-
363
- lane.tail = Promise.all([before, new Promise<void>((resolve) => {
364
- release = resolve
365
- })])
366
- await waitFor(before, whole.signal)
367
- }
368
-
369
- const attempts = retryOf(options?.retry)
370
- // One key for every try of the call, which a handler reads from `ctx.request` to tell a repeat from a new call.
371
- const key = attempts.times > 0 ? idempotencyKey() : undefined
372
-
373
- for (let attempt = 0; ; attempt++) {
374
- // The wait for the answer counts from the moment the post is sent, and each try has its own.
375
- const limit = timeout === undefined ? undefined : new AbortController()
376
- const attemptSignal = join([whole.signal, limit?.signal])
377
- let timer: ReturnType<typeof setTimeout> | undefined
378
- let sentNow = false
379
-
380
- try {
381
- answer = await (await postingCode()).post(url(), input, setProgress, attemptSignal.signal, () => {
382
- sent = sentNow = true
383
- clearTimeout(timer)
384
- timer = limit && setTimeout(() => {
385
- timedOut = true
386
- limit.abort(new DOMException(`The action call timeout of ${String(timeoutOption)} passed before the server answered`, 'TimeoutError'))
387
- }, timeout)
388
- }, left, caller, { key, options })
389
-
390
- break
391
- }
392
- catch (error) {
393
- // 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.
394
- if (attempt >= attempts.times || whole.signal?.aborted === true || !attempts.again(error, sentNow && (error as { sent?: unknown } | null)?.sent !== false))
395
- throw error
396
-
397
- await waitFor(attempts.delay * 2 ** attempt, whole.signal)
398
- }
399
- finally {
400
- clearTimeout(timer)
401
- attemptSignal.stop()
402
- }
403
- }
404
-
405
- posted = true
406
- whole.stop(later?.signal)
407
- release?.()
408
- release = undefined
409
-
410
- const streamEnded = (status: ConnectionStatus) => {
411
- if (status === 'done') {
412
- called(page, { action: url(), succeeded: true, data: untrack((answer as ActionStream<unknown>).last) }, turn)
413
- refreshPage()
414
- }
415
-
416
- shift(-1)
417
- }
418
-
419
- streaming = onStreamEnd(answer, streamEnded)
420
-
421
- if (streaming) {
422
- // A retry of the stream is the call running again.
423
- caller.retried = (again) => {
424
- shift(1)
425
- again[SETTLED](streamEnded)
426
- }
427
- }
428
-
429
- if (!streaming) {
430
- called(page, { action: url(), succeeded: true, data: answer }, turn)
431
- refreshPage()
432
- }
433
-
434
- void hooked(options?.onSuccess, { data: answer })
435
-
436
- return answer
172
+ // An optimistic change shows at once, as the call is pending at once.
173
+ start.undo = options?.optimistic ? undoable(options.optimistic) : undefined
174
+ code = running ?? await (attempt = loading ??= runCode())
437
175
  }
438
176
  catch (error) {
439
- failure = error
440
-
441
- // A redirect leaves the page as it is until the next one shows.
442
- if (!posted && !(error instanceof ActionRedirectError))
443
- undo?.()
177
+ if (loading === attempt)
178
+ loading = undefined
444
179
 
445
- // The server may have run a call aborted or timed out once it was sent, which the page then shows as it stands.
446
- if (sent && (options?.signal?.aborted === true || timedOut))
447
- refreshPage()
180
+ if (start.undo) {
181
+ start.undo()
182
+ released(start.undo)
183
+ }
448
184
 
449
- if (error instanceof ActionError && !(error instanceof ActionRedirectError))
450
- called(page, { action: url(), status: error.status, values: {}, errors: error.errors, data: error.data }, turn)
185
+ shift(-1)
451
186
 
452
- if (!(error instanceof ActionRedirectError) && whole.signal?.aborted !== true)
453
- void hooked(options?.onError, { error: apiErrorOf(error) })
187
+ if (host.latest(start.turn))
188
+ setProgress(undefined)
454
189
 
455
190
  throw error
456
191
  }
457
- finally {
458
- // A stream is aborted by the call's own signal until it ends.
459
- if (!streaming)
460
- whole.stop()
461
-
462
- release?.()
463
192
 
464
- if (undo)
465
- released(undo)
466
-
467
- if (!streaming)
468
- shift(-1)
469
-
470
- if (turn === started)
471
- setProgress(undefined)
472
-
473
- if (lane.latest === later)
474
- lane.latest = undefined
475
-
476
- void hooked(options?.onSettled, failure === undefined ? { data: answer } : { error: apiErrorOf(failure) })
477
- }
193
+ return code.run(host, start, lane, input, fixed, options)
478
194
  }
479
195
 
480
196
  const bound = binder(run, url)
@@ -542,16 +258,16 @@ function formHook<Detail extends { action: string }, Told>(url: () => string, st
542
258
  }
543
259
 
544
260
  // A call and `track()` carrying the fields given, over one action's state: a `with` of the action follows the same calls.
545
- function binder(run: (lane: Lane, input: unknown, options?: CallOptions) => Promise<unknown>, url: () => string): Bound {
261
+ function binder(run: (lane: Lane, input: unknown, fixed: Record<string, unknown> | undefined, options?: CallOptions) => Promise<unknown>, url: () => string): Bound {
546
262
  const actionLane: Lane = { tail: Promise.resolve() }
547
263
 
548
264
  return fields => ({
549
265
  // eslint-disable-next-line ts/promise-function-async -- an async function would wrap the call's promise, which carries `orThrow()`
550
- call: (input: unknown, options?: CallOptions) => settling(run(actionLane, withFixed(input, fields), options), error => signalAborted(options) ? undefined : refusalOf(url)(error)),
266
+ call: (input: unknown, options?: CallOptions) => settling(run(actionLane, input, fields, options), error => signalAborted(options) ? undefined : refusalOf(url)(error)),
551
267
  track: (handle) => {
552
268
  const lane: Lane = { tail: Promise.resolve(), queue: handle?.queue }
553
269
 
554
- return tracked(async (input, options) => run(lane, withFixed(input, fields), options), url, handle)
270
+ return tracked(async (input, options) => run(lane, input, fields, options), url, handle)
555
271
  },
556
272
  })
557
273
  }
@@ -599,7 +315,7 @@ function tracked(call: (input: never, options?: CallOptions) => Promise<unknown>
599
315
 
600
316
  let warming = false
601
317
 
602
- // An island holding an action caller loads the posting code once the browser is idle, so a call made as the page goes away (`pagehide`, which cannot wait for a chunk) finds it in; it stays out of the first load. A failure is left for the call itself to retry and report, so an idle load never reloads the page.
318
+ // An island holding an action caller loads the code running a call once the browser is idle, so a call made as the page goes away (`pagehide`, which cannot wait for a chunk) finds it in; it stays out of the first load. A failure is left for the call itself to retry and report, so an idle load never reloads the page.
603
319
  function warmCode(): void {
604
320
  if (warming || typeof window === 'undefined')
605
321
  return
@@ -608,7 +324,7 @@ function warmCode(): void {
608
324
 
609
325
  const load = async () => {
610
326
  try {
611
- posting = await import('./action-post')
327
+ running = await import('./action-run')
612
328
  }
613
329
  catch {}
614
330
  }
@@ -619,15 +335,11 @@ function warmCode(): void {
619
335
  setTimeout(() => void load(), 200)
620
336
  }
621
337
 
622
- function postingCode(): typeof import('./action-post') | Promise<typeof import('./action-post')> {
623
- return posting ?? postCode()
624
- }
625
-
626
- // A flaky connection gets the chunk retried after 1 s and 3 s, as a lazy component's is. The code that posts a call, loaded by the first call; a chunk a deploy removed asks the server whether the build changed, which reloads the page, and the call fails as any call that could not be sent.
627
- async function postCode(): Promise<typeof import('./action-post')> {
338
+ // A flaky connection gets the chunk retried after 1 s and 3 s, as a lazy component's is. The code that runs a call, loaded by the first call; a chunk a deploy removed asks the server whether the build changed, which reloads the page, and the call fails as any call that could not be sent.
339
+ async function runCode(): Promise<typeof import('./action-run')> {
628
340
  for (let attempt = 0; ; attempt++) {
629
341
  try {
630
- return posting = await import('./action-post')
342
+ return running = await import('./action-run')
631
343
  }
632
344
  catch (error) {
633
345
  if (attempt >= CODE_RETRIES) {