@alecsibilia/ui-kit 0.0.0-stage → 0.1.0-alpha.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 (283) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +201 -0
  3. package/README.md +233 -2
  4. package/bin/ui-kit.js +12 -0
  5. package/dist/alchemy/app.d.ts +34 -0
  6. package/dist/alchemy/app.js +88 -0
  7. package/dist/alchemy/index.d.ts +11 -0
  8. package/dist/alchemy/index.js +11 -0
  9. package/dist/alchemy/live-rooms.d.ts +21 -0
  10. package/dist/alchemy/live-rooms.js +31 -0
  11. package/dist/alchemy/migrations.d.ts +28 -0
  12. package/dist/alchemy/migrations.js +56 -0
  13. package/dist/alchemy/stage.d.ts +20 -0
  14. package/dist/alchemy/stage.js +20 -0
  15. package/dist/alchemy/wrangler-config.d.ts +76 -0
  16. package/dist/alchemy/wrangler-config.js +82 -0
  17. package/dist/build/app-modules.d.ts +11 -0
  18. package/dist/build/app-modules.js +40 -0
  19. package/dist/build/check.d.ts +4 -0
  20. package/dist/build/check.js +170 -0
  21. package/dist/build/cli.d.ts +1 -0
  22. package/dist/build/cli.js +79 -0
  23. package/dist/build/feature.d.ts +8 -0
  24. package/dist/build/feature.js +24 -0
  25. package/dist/build/index.d.ts +13 -0
  26. package/dist/build/index.js +13 -0
  27. package/dist/build/measure-cpu.d.ts +2 -0
  28. package/dist/build/measure-cpu.js +196 -0
  29. package/dist/build/prerender.d.ts +5 -0
  30. package/dist/build/prerender.js +109 -0
  31. package/dist/build/preview.d.ts +7 -0
  32. package/dist/build/preview.js +41 -0
  33. package/dist/build/seo-files.d.ts +27 -0
  34. package/dist/build/seo-files.js +24 -0
  35. package/dist/client/head-swap.d.ts +45 -0
  36. package/dist/client/head-swap.js +183 -0
  37. package/dist/client/hydration-pass.d.ts +56 -0
  38. package/dist/client/hydration-pass.js +186 -0
  39. package/dist/client/hydration-reset.d.ts +82 -0
  40. package/dist/client/hydration-reset.js +154 -0
  41. package/dist/client/index.d.ts +37 -0
  42. package/dist/client/index.js +65 -0
  43. package/dist/client/nav-client.d.ts +35 -0
  44. package/dist/client/nav-client.js +371 -0
  45. package/dist/live/collab/doc-provider.d.ts +74 -0
  46. package/dist/live/collab/doc-provider.js +412 -0
  47. package/dist/live/collab/index.d.ts +51 -0
  48. package/dist/live/collab/index.jsx +142 -0
  49. package/dist/live/collab-status.d.ts +25 -0
  50. package/dist/live/collab-status.js +17 -0
  51. package/dist/live/cursors/cursor-engine.d.ts +57 -0
  52. package/dist/live/cursors/cursor-engine.js +243 -0
  53. package/dist/live/cursors/index.d.ts +46 -0
  54. package/dist/live/cursors/index.jsx +110 -0
  55. package/dist/live/declaration.d.ts +113 -0
  56. package/dist/live/declaration.js +94 -0
  57. package/dist/live/index.d.ts +8 -0
  58. package/dist/live/index.js +8 -0
  59. package/dist/live/protocol.d.ts +224 -0
  60. package/dist/live/protocol.js +272 -0
  61. package/dist/live/server/doc-store.d.ts +68 -0
  62. package/dist/live/server/doc-store.js +187 -0
  63. package/dist/live/server/index.d.ts +60 -0
  64. package/dist/live/server/index.js +77 -0
  65. package/dist/live/server/room-presence.d.ts +24 -0
  66. package/dist/live/server/room-presence.js +123 -0
  67. package/dist/live/server/room-sockets.d.ts +65 -0
  68. package/dist/live/server/room-sockets.js +97 -0
  69. package/dist/live/server/room.d.ts +20 -0
  70. package/dist/live/server/room.js +348 -0
  71. package/dist/live/server/route.d.ts +56 -0
  72. package/dist/live/server/route.js +159 -0
  73. package/dist/live/server/types.d.ts +163 -0
  74. package/dist/live/server/types.js +2 -0
  75. package/dist/logic/agent-actions.d.ts +133 -0
  76. package/dist/logic/agent-actions.js +150 -0
  77. package/dist/logic/command-types.d.ts +53 -0
  78. package/dist/logic/command-types.js +0 -0
  79. package/dist/logic/dev.d.ts +9 -0
  80. package/dist/logic/dev.js +10 -0
  81. package/dist/logic/draft-rebase.d.ts +59 -0
  82. package/dist/logic/draft-rebase.js +71 -0
  83. package/dist/logic/form-state.d.ts +105 -0
  84. package/dist/logic/form-state.js +93 -0
  85. package/dist/logic/head.d.ts +145 -0
  86. package/dist/logic/head.js +140 -0
  87. package/dist/logic/index.d.ts +27 -0
  88. package/dist/logic/index.js +25 -0
  89. package/dist/logic/island-def.d.ts +89 -0
  90. package/dist/logic/island-def.js +59 -0
  91. package/dist/logic/kit-commands.d.ts +104 -0
  92. package/dist/logic/kit-commands.js +72 -0
  93. package/dist/logic/kit-errors.d.ts +25 -0
  94. package/dist/logic/kit-errors.js +6 -0
  95. package/dist/logic/listen.d.ts +99 -0
  96. package/dist/logic/listen.js +92 -0
  97. package/dist/logic/mutation-def.d.ts +65 -0
  98. package/dist/logic/mutation-def.js +43 -0
  99. package/dist/logic/navigation-data.d.ts +50 -0
  100. package/dist/logic/navigation-data.js +55 -0
  101. package/dist/logic/navigation-policy.d.ts +74 -0
  102. package/dist/logic/navigation-policy.js +127 -0
  103. package/dist/logic/page-model-def.d.ts +108 -0
  104. package/dist/logic/page-model-def.js +31 -0
  105. package/dist/logic/path.d.ts +31 -0
  106. package/dist/logic/path.js +48 -0
  107. package/dist/logic/query-def.d.ts +56 -0
  108. package/dist/logic/query-def.js +37 -0
  109. package/dist/logic/query-key.d.ts +24 -0
  110. package/dist/logic/query-key.js +35 -0
  111. package/dist/logic/query-types.d.ts +28 -0
  112. package/dist/logic/query-types.js +2 -0
  113. package/dist/logic/resources.d.ts +163 -0
  114. package/dist/logic/resources.js +62 -0
  115. package/dist/logic/result.d.ts +30 -0
  116. package/dist/logic/result.js +23 -0
  117. package/dist/logic/route-model.d.ts +139 -0
  118. package/dist/logic/route-model.js +207 -0
  119. package/dist/logic/route-params.d.ts +80 -0
  120. package/dist/logic/route-params.js +114 -0
  121. package/dist/logic/route-table.d.ts +308 -0
  122. package/dist/logic/route-table.js +206 -0
  123. package/dist/logic/seo.d.ts +61 -0
  124. package/dist/logic/seo.js +101 -0
  125. package/dist/logic/site.d.ts +12 -0
  126. package/dist/logic/site.js +0 -0
  127. package/dist/logic/subscriptions.d.ts +177 -0
  128. package/dist/logic/subscriptions.js +152 -0
  129. package/dist/render/index.d.ts +32 -0
  130. package/dist/render/index.js +52 -0
  131. package/dist/runtime/island-wake.d.ts +38 -0
  132. package/dist/runtime/island-wake.js +147 -0
  133. package/dist/runtime/query-scope.d.ts +224 -0
  134. package/dist/runtime/query-scope.js +222 -0
  135. package/dist/runtime/room-hub.d.ts +107 -0
  136. package/dist/runtime/room-hub.js +307 -0
  137. package/dist/runtime/subscription-runner.d.ts +71 -0
  138. package/dist/runtime/subscription-runner.js +240 -0
  139. package/dist/server/bindings.d.ts +41 -0
  140. package/dist/server/bindings.js +74 -0
  141. package/dist/server/browser.d.ts +9 -0
  142. package/dist/server/browser.js +9 -0
  143. package/dist/server/edge-cache.d.ts +28 -0
  144. package/dist/server/edge-cache.js +35 -0
  145. package/dist/server/errors.d.ts +45 -0
  146. package/dist/server/errors.js +36 -0
  147. package/dist/server/form-actions-setup.d.ts +1 -0
  148. package/dist/server/form-actions-setup.js +15 -0
  149. package/dist/server/form-actions.d.ts +121 -0
  150. package/dist/server/form-actions.js +211 -0
  151. package/dist/server/index.d.ts +36 -0
  152. package/dist/server/index.js +35 -0
  153. package/dist/server/inputs.d.ts +45 -0
  154. package/dist/server/inputs.js +77 -0
  155. package/dist/server/rate-limit.d.ts +29 -0
  156. package/dist/server/rate-limit.js +32 -0
  157. package/dist/server/read-cache.d.ts +74 -0
  158. package/dist/server/read-cache.js +58 -0
  159. package/dist/server/request-info.d.ts +17 -0
  160. package/dist/server/request-info.js +11 -0
  161. package/dist/server/request.d.ts +52 -0
  162. package/dist/server/request.js +73 -0
  163. package/dist/server/response-cookies.d.ts +23 -0
  164. package/dist/server/response-cookies.js +24 -0
  165. package/dist/server/run-effect.d.ts +42 -0
  166. package/dist/server/run-effect.js +92 -0
  167. package/dist/server/run.d.ts +63 -0
  168. package/dist/server/run.js +66 -0
  169. package/dist/server/session.d.ts +125 -0
  170. package/dist/server/session.js +247 -0
  171. package/dist/test/fake-bindings.d.ts +32 -0
  172. package/dist/test/fake-bindings.js +108 -0
  173. package/dist/test/fake-room.d.ts +66 -0
  174. package/dist/test/fake-room.js +216 -0
  175. package/dist/test/index.d.ts +19 -0
  176. package/dist/test/index.js +44 -0
  177. package/dist/test/probe.d.ts +11 -0
  178. package/dist/test/probe.js +11 -0
  179. package/dist/test/solid-jsx.d.ts +23 -0
  180. package/dist/test/solid-jsx.js +28 -0
  181. package/dist/test/solid-preload.d.ts +1 -0
  182. package/dist/test/solid-preload.js +29 -0
  183. package/dist/ui/code-editor/index.d.ts +21 -0
  184. package/dist/ui/code-editor/index.jsx +286 -0
  185. package/dist/ui/collab-text/index.d.ts +29 -0
  186. package/dist/ui/collab-text/index.jsx +96 -0
  187. package/dist/ui/components/controls.d.ts +69 -0
  188. package/dist/ui/components/controls.jsx +81 -0
  189. package/dist/ui/components/feedback.d.ts +26 -0
  190. package/dist/ui/components/feedback.jsx +80 -0
  191. package/dist/ui/components/layout.d.ts +74 -0
  192. package/dist/ui/components/layout.jsx +146 -0
  193. package/dist/ui/components/lists.d.ts +69 -0
  194. package/dist/ui/components/lists.jsx +133 -0
  195. package/dist/ui/components/navigation.d.ts +73 -0
  196. package/dist/ui/components/navigation.jsx +112 -0
  197. package/dist/ui/components/text.d.ts +34 -0
  198. package/dist/ui/components/text.jsx +163 -0
  199. package/dist/ui/index.d.ts +7 -0
  200. package/dist/ui/index.js +7 -0
  201. package/dist/ui/jsx-runtime.d.ts +19 -0
  202. package/dist/ui/jsx-runtime.js +0 -0
  203. package/dist/ui/list-keys.d.ts +31 -0
  204. package/dist/ui/list-keys.js +45 -0
  205. package/dist/ui/styles/base.css +38 -0
  206. package/dist/ui/styles/components.css +608 -0
  207. package/dist/ui/styles/tokens.css +27 -0
  208. package/dist/ui/styles/transitions.css +23 -0
  209. package/dist/ui/styles/workbench.css +428 -0
  210. package/dist/ui/styles.css +9 -0
  211. package/dist/view/agent-actions-view.d.ts +30 -0
  212. package/dist/view/agent-actions-view.js +27 -0
  213. package/dist/view/agent-surface.d.ts +42 -0
  214. package/dist/view/agent-surface.js +123 -0
  215. package/dist/view/app-scope.d.ts +51 -0
  216. package/dist/view/app-scope.js +75 -0
  217. package/dist/view/boundary.d.ts +18 -0
  218. package/dist/view/boundary.jsx +26 -0
  219. package/dist/view/command-runner.d.ts +16 -0
  220. package/dist/view/command-runner.js +245 -0
  221. package/dist/view/form.d.ts +82 -0
  222. package/dist/view/form.jsx +109 -0
  223. package/dist/view/head-view.d.ts +31 -0
  224. package/dist/view/head-view.js +46 -0
  225. package/dist/view/index.d.ts +41 -0
  226. package/dist/view/index.js +40 -0
  227. package/dist/view/island-root.d.ts +30 -0
  228. package/dist/view/island-root.js +36 -0
  229. package/dist/view/island.d.ts +102 -0
  230. package/dist/view/island.jsx +154 -0
  231. package/dist/view/match.d.ts +41 -0
  232. package/dist/view/match.js +111 -0
  233. package/dist/view/model.d.ts +92 -0
  234. package/dist/view/model.js +162 -0
  235. package/dist/view/navigation-head.d.ts +29 -0
  236. package/dist/view/navigation-head.jsx +38 -0
  237. package/dist/view/page-model.d.ts +46 -0
  238. package/dist/view/page-model.js +146 -0
  239. package/dist/view/query-effects.d.ts +87 -0
  240. package/dist/view/query-effects.js +147 -0
  241. package/dist/view/query-ok.d.ts +45 -0
  242. package/dist/view/query-ok.js +96 -0
  243. package/dist/view/query.d.ts +115 -0
  244. package/dist/view/query.jsx +179 -0
  245. package/dist/view/result-view.d.ts +14 -0
  246. package/dist/view/result-view.jsx +15 -0
  247. package/dist/view/route-pages.d.ts +43 -0
  248. package/dist/view/route-pages.js +59 -0
  249. package/dist/view/router.d.ts +44 -0
  250. package/dist/view/router.js +123 -0
  251. package/dist/vite/dev-middleware.d.ts +12 -0
  252. package/dist/vite/dev-middleware.js +39 -0
  253. package/dist/vite/dev-objects.d.ts +25 -0
  254. package/dist/vite/dev-objects.js +127 -0
  255. package/dist/vite/dev-platform.d.ts +19 -0
  256. package/dist/vite/dev-platform.js +77 -0
  257. package/dist/vite/dev-seo-files.d.ts +11 -0
  258. package/dist/vite/dev-seo-files.js +40 -0
  259. package/dist/vite/index.d.ts +53 -0
  260. package/dist/vite/index.js +148 -0
  261. package/dist/vite/local-config.d.ts +34 -0
  262. package/dist/vite/local-config.js +72 -0
  263. package/dist/vite/worker-build.d.ts +24 -0
  264. package/dist/vite/worker-build.js +84 -0
  265. package/dist/worker/create-worker.d.ts +52 -0
  266. package/dist/worker/create-worker.js +90 -0
  267. package/dist/worker/index.d.ts +12 -0
  268. package/dist/worker/index.js +12 -0
  269. package/dist/worker/page-routes.d.ts +88 -0
  270. package/dist/worker/page-routes.js +100 -0
  271. package/dist/worker/page-status.d.ts +41 -0
  272. package/dist/worker/page-status.js +23 -0
  273. package/dist/worker/request-prelude.d.ts +69 -0
  274. package/dist/worker/request-prelude.js +107 -0
  275. package/dist/worker/security.d.ts +76 -0
  276. package/dist/worker/security.js +137 -0
  277. package/dist/worker/server-app.d.ts +54 -0
  278. package/dist/worker/server-app.js +76 -0
  279. package/guards/browser-only.js +5 -0
  280. package/guards/node-only.js +6 -0
  281. package/guards/server-only.js +5 -0
  282. package/migrations/0001-sessions.sql +22 -0
  283. package/package.json +233 -5
@@ -0,0 +1,162 @@
1
+ import { isServer } from '@solidjs/web';
2
+ import { createSignal, createStore, getOwner, onSettled, reconcile, snapshot, untrack, } from 'solid-js';
3
+ import { useKitScope } from './app-scope.js';
4
+ import { createCommandRunner, isCommandTuple, isPlainObject, } from './command-runner.js';
5
+ import { subscribeUrlChanges } from './router.js';
6
+ import {} from '../logic/kit-commands.js';
7
+ import { messageForOutcome } from '../logic/listen.js';
8
+ import {} from '../logic/result.js';
9
+ import { browserRunnerDeps, createSubscriptionRunner, } from '../runtime/subscription-runner.js';
10
+ /** Kit-internal: the component starter (see `ModelStarter`). */
11
+ export const componentStarter = (work) => {
12
+ onSettled(() => {
13
+ if (isServer)
14
+ return;
15
+ return work();
16
+ });
17
+ };
18
+ /**
19
+ * Kit-internal: the page-model starter (see `ModelStarter`). With `life`,
20
+ * the cleanup `work` returns is kept (`life.stops`) so disposing the page
21
+ * model (a client navigation) stops its listens and subscriptions, and
22
+ * work for a model disposed before its page hydrated never starts.
23
+ */
24
+ export function pageStarter(scope, life) {
25
+ return (work) => {
26
+ if (isServer)
27
+ return;
28
+ scope.page.afterHydration(() => {
29
+ if (life && !life.alive())
30
+ return;
31
+ const stop = work();
32
+ if (typeof stop === 'function')
33
+ life?.stops.push(stop);
34
+ });
35
+ };
36
+ }
37
+ /**
38
+ * Subscribes a model to query outcomes declared with `listen(...)` (Way 2).
39
+ * Client only, and only once the model starts (see `ModelStarter`), so SSR
40
+ * output and the hydration pass are never affected by listener Messages. The channel
41
+ * replays the latest outcome per query on subscribe, which may flip the model
42
+ * once right after hydration (e.g. a query that already failed). Outcomes are
43
+ * keyed by query id + input, so registries and inputs never cross-talk. Scoped to the
44
+ * nearest `<AppScope>` (one per request / page), never module-global.
45
+ */
46
+ function wireListens(scope, listens, send, start) {
47
+ if (!scope) {
48
+ throw new Error('createModel({ listens }) must be called inside <AppScope> (wrap the app root).');
49
+ }
50
+ start(() => scope.outcomes.subscribe((event) => {
51
+ const msg = messageForOutcome(listens, event);
52
+ if (msg !== undefined)
53
+ send(msg);
54
+ }));
55
+ }
56
+ /**
57
+ * Feeds URL changes (Back / Forward, `Navigate`) into the model as a Message. Client only,
58
+ * once the model starts, so SSR and the hydration pass never see it.
59
+ */
60
+ function wireUrlChanges(urlChanged, send, start) {
61
+ start(() => subscribeUrlChanges((url) => send(urlChanged(url))));
62
+ }
63
+ /**
64
+ * Starts a model's `subscriptions` once the model starts (client only) and
65
+ * returns the function `send` calls after every Message to re-diff them.
66
+ */
67
+ function wireSubscriptions(subscriptions, current, send, start, scope) {
68
+ let runner;
69
+ start(() => {
70
+ const started = createSubscriptionRunner(send, browserRunnerDeps(scope ? scope.rooms : undefined));
71
+ runner = started;
72
+ started.update(subscriptions(current()));
73
+ return () => {
74
+ runner = undefined;
75
+ started.stop();
76
+ };
77
+ });
78
+ return () => runner?.update(subscriptions(current()));
79
+ }
80
+ /**
81
+ * Runs a model's init Commands once, in the browser, once the model starts
82
+ * (registered after listens and subscriptions, so those are live when the
83
+ * first result Message arrives). Never on the server: SSR output and the
84
+ * hydration pass see only the initial model.
85
+ */
86
+ function wireInitCommands(runInit, start) {
87
+ if (start === componentStarter && !getOwner()) {
88
+ if (!isServer)
89
+ runInit();
90
+ return;
91
+ }
92
+ // Component models: `onSettled` on both sides (it allocates a hydration
93
+ // id); the work is client-only. Skipping the call on the server shifts
94
+ // every id after it.
95
+ start(() => {
96
+ runInit();
97
+ });
98
+ }
99
+ export function createModel(options) {
100
+ return createModelIn(options, useKitScope(), componentStarter);
101
+ }
102
+ /**
103
+ * Kit-internal: `createModel` with an explicit scope and starter. Page
104
+ * models (`usePageModel`) are created in a detached root (no context), so
105
+ * they pass the page's scope and the page starter.
106
+ */
107
+ export function createModelIn(options, scope, start) {
108
+ // A thunk is the intentional one-time capture of reactive inputs (props,
109
+ // a `<Query>` ok view), so it runs untracked. Either way `init` may embed
110
+ // live store views: snapshot to plain data so the model never aliases a
111
+ // store that changes without a Message, and `structuredClone` can copy it.
112
+ const initOut = typeof options.init === 'function'
113
+ ? untrack(options.init)
114
+ : options.init;
115
+ const [initial, initCommands] = isCommandTuple(initOut) ? initOut : [initOut, []];
116
+ let latest = snapshot(initial);
117
+ let state;
118
+ let write;
119
+ if (isPlainObject(latest)) {
120
+ const [store, setStore] = createStore(structuredClone(latest));
121
+ state = () => store;
122
+ write = (next) => {
123
+ setStore(reconcile(next));
124
+ };
125
+ }
126
+ else {
127
+ // Solid's createSignal overloads key on `Function`: a model is never one.
128
+ // oxlint-disable-next-line typescript/no-unsafe-function-type
129
+ const [sig, setSig] = createSignal(latest);
130
+ state = sig;
131
+ write = (next) => {
132
+ // oxlint-disable-next-line typescript/no-unsafe-function-type
133
+ setSig(() => next);
134
+ };
135
+ }
136
+ let resubscribe = () => { };
137
+ const send = (msg) => {
138
+ const out = options.update(latest, msg);
139
+ const [next, cmds] = isCommandTuple(out)
140
+ ? out
141
+ : [out, []];
142
+ latest = next;
143
+ write(next);
144
+ options.onTrace?.({ message: msg, commands: cmds, at: Date.now() });
145
+ resubscribe();
146
+ runner.run(cmds);
147
+ };
148
+ const runner = createCommandRunner(options.commands ?? {}, send, scope);
149
+ if (options.listens) {
150
+ wireListens(scope, options.listens, send, start);
151
+ }
152
+ if (options.urlChanged) {
153
+ wireUrlChanges(options.urlChanged, send, start);
154
+ }
155
+ if (options.subscriptions) {
156
+ resubscribe = wireSubscriptions(options.subscriptions, () => latest, send, start, scope);
157
+ }
158
+ if (initCommands.length > 0) {
159
+ wireInitCommands(() => runner.run(initCommands), start);
160
+ }
161
+ return [state, send];
162
+ }
@@ -0,0 +1,29 @@
1
+ /** @jsxImportSource @solidjs/web */
2
+ import type { Element as SolidElement } from 'solid-js';
3
+ import type { RouteTable } from '../logic/route-table.ts';
4
+ /**
5
+ * The document head's page-transition tags, from the route table
6
+ * (`docs/guides/ui-kit-page-transitions.md`). Server-only (the document shell is static
7
+ * HTML); put it in the document `<head>` next to `<IslandScripts>`.
8
+ *
9
+ * - **Speculation rules** (layer 1, every page): one
10
+ * `<script type="speculationrules">`, hover prefetch for public page
11
+ * links, prerender for routes that opt in, never `auth: "required"`
12
+ * routes or `/_server`. It carries the request's CSP nonce on SSR pages;
13
+ * on prerendered pages the build hashes it into `_headers` like any inline
14
+ * script. It is not executable JS, so a page with no island keeps it and
15
+ * still ships 0 bytes of JS.
16
+ * - **Client navigation data** (layer 2): on an SSR route in a client
17
+ * section (`navigation: { client: "app" }`), a JSON data block with the
18
+ * section name and that section's routes (no other section's). The island client
19
+ * entry loads the navigation runtime only when it finds it, so other
20
+ * pages (and apps that never opt in) pay nothing.
21
+ *
22
+ * @example
23
+ * ```tsx
24
+ * <DocumentShell lang="en" head={<><PageNavigation routes={routes} /><IslandScripts entry={CLIENT_ENTRY} /></>}>
25
+ * ```
26
+ */
27
+ export declare function PageNavigation(props: {
28
+ readonly routes: RouteTable;
29
+ }): SolidElement;
@@ -0,0 +1,38 @@
1
+ /** @jsxImportSource @solidjs/web */
2
+ import { getRequestEvent } from '@solidjs/web';
3
+ import { CLIENT_NAV_DATA_TYPE, clientNavigationFor, inlineJson, speculationRulesFor, } from '../logic/navigation-policy.js';
4
+ /**
5
+ * The document head's page-transition tags, from the route table
6
+ * (`docs/guides/ui-kit-page-transitions.md`). Server-only (the document shell is static
7
+ * HTML); put it in the document `<head>` next to `<IslandScripts>`.
8
+ *
9
+ * - **Speculation rules** (layer 1, every page): one
10
+ * `<script type="speculationrules">`, hover prefetch for public page
11
+ * links, prerender for routes that opt in, never `auth: "required"`
12
+ * routes or `/_server`. It carries the request's CSP nonce on SSR pages;
13
+ * on prerendered pages the build hashes it into `_headers` like any inline
14
+ * script. It is not executable JS, so a page with no island keeps it and
15
+ * still ships 0 bytes of JS.
16
+ * - **Client navigation data** (layer 2): on an SSR route in a client
17
+ * section (`navigation: { client: "app" }`), a JSON data block with the
18
+ * section name and that section's routes (no other section's). The island client
19
+ * entry loads the navigation runtime only when it finds it, so other
20
+ * pages (and apps that never opt in) pay nothing.
21
+ *
22
+ * @example
23
+ * ```tsx
24
+ * <DocumentShell lang="en" head={<><PageNavigation routes={routes} /><IslandScripts entry={CLIENT_ENTRY} /></>}>
25
+ * ```
26
+ */
27
+ export function PageNavigation(props) {
28
+ const event = getRequestEvent();
29
+ const rules = speculationRulesFor(props.routes);
30
+ const url = event ? new URL(event.request.url) : undefined;
31
+ const client = url
32
+ ? clientNavigationFor(props.routes, `${url.pathname}${url.search}`)
33
+ : undefined;
34
+ return (<>
35
+ {rules ? (<script type="speculationrules" nonce={event?.nonce} innerHTML={inlineJson(rules)}/>) : null}
36
+ {client ? (<script type={CLIENT_NAV_DATA_TYPE} innerHTML={inlineJson(client)}/>) : null}
37
+ </>);
38
+ }
@@ -0,0 +1,46 @@
1
+ import { type Accessor } from 'solid-js';
2
+ import type { PageModelDef } from '../logic/page-model-def.ts';
3
+ import type { Tagged } from '../logic/result.ts';
4
+ import type { KitScope } from '../runtime/query-scope.ts';
5
+ type Instance<Model, Msg> = readonly [
6
+ state: Accessor<Model>,
7
+ send: (msg: Msg) => void
8
+ ];
9
+ /**
10
+ * Kit-internal: the page's instance of `def` in `scope`, created on first
11
+ * use in a detached root named `pm-<name>`.
12
+ *
13
+ * Why detached: an island root's owner ids must be the same on the server
14
+ * and in the browser. Which island asks first can differ (the server renders
15
+ * islands in tree order; the browser hydrates them in DOM order; a lazy
16
+ * island may ask later), so creating the model under the asking island's
17
+ * owner would shift that island's ids. A detached root with its own id
18
+ * namespace allocates nothing in any island.
19
+ */
20
+ export declare function pageModelIn<Model, Msg, Cmd extends Tagged>(scope: KitScope, def: PageModelDef<Model, Msg, Cmd>): Instance<Model, Msg>;
21
+ /**
22
+ * The page's shared model for `def`: `[state, send]`, the same pair in
23
+ * every island (and in the static HTML around them) on this page.
24
+ *
25
+ * - Server (SSR, prerender): created once per request from `init`; the
26
+ * static HTML and every island render from that state.
27
+ * - Browser: created once per page, by the first island that asks, from
28
+ * the same `init` (so hydration matches). Its init Commands, `listens`,
29
+ * `subscriptions` and `urlChanged` start after every island on the page
30
+ * has hydrated (never during the hydration pass).
31
+ * - Static HTML outside islands is rendered once on the server and never
32
+ * updates: put what changes with the model in an island.
33
+ *
34
+ * @throws Outside `<AppScope>` (the page root, or an island root).
35
+ *
36
+ * @example
37
+ * ```tsx
38
+ * export default function TraceIsland() {
39
+ * const [trace, sendTrace] = usePageModel(tracePage);
40
+ * const [plan] = usePageModel(plannerPage);
41
+ * return <Badge>{traceBadge(trace(), plan())}</Badge>;
42
+ * }
43
+ * ```
44
+ */
45
+ export declare function usePageModel<Model, Msg, Cmd extends Tagged>(def: PageModelDef<Model, Msg, Cmd>): Instance<Model, Msg>;
46
+ export {};
@@ -0,0 +1,146 @@
1
+ import { isServer } from '@solidjs/web';
2
+ import { createRoot, createSignal, onCleanup, runWithOwner, untrack, } from 'solid-js';
3
+ import { useKitScope } from './app-scope.js';
4
+ import { createModelIn, pageStarter } from './model.js';
5
+ import { currentUrl } from './router.js';
6
+ /**
7
+ * A page model's `init` as `createModel` takes it: a function `init` gets
8
+ * the page (`{ url }`: path and query, the same on the server and in the
9
+ * browser), so page models need no view import to read the URL.
10
+ */
11
+ function pageInit(init) {
12
+ if (typeof init !== 'function')
13
+ return init;
14
+ // `init` is the spec's function form (a value form is never a function).
15
+ const read = init;
16
+ return () => read({ url: currentUrl() });
17
+ }
18
+ /** The model `init` describes (without its Commands): what the server rendered. */
19
+ function initModelOf(init) {
20
+ const out = typeof init === 'function' ? untrack(init) : init;
21
+ const isTuple = Array.isArray(out) &&
22
+ out.length === 2 &&
23
+ Array.isArray(out[1]) &&
24
+ !Array.isArray(out[0]);
25
+ return (isTuple ? out[0] : out);
26
+ }
27
+ /**
28
+ * A persisted page model as a later page's islands see it during that
29
+ * page's hydration pass: `init` until the pass ends (the server rendered
30
+ * the new page from `init`, so hydration matches), then the live state.
31
+ * One mask per instance per page (`scope.pageModelMasks`).
32
+ */
33
+ function maskedUntilHydrated(scope, page, def, live, init) {
34
+ let masks = scope.pageModelMasks.get(page);
35
+ if (!masks) {
36
+ masks = new Map();
37
+ scope.pageModelMasks.set(page, masks);
38
+ }
39
+ const existing = masks.get(def);
40
+ if (existing)
41
+ return existing;
42
+ const initView = initModelOf(init);
43
+ // Detached: a signal created under the asking island's owner must not
44
+ // cost it a hydration id (the server never masks).
45
+ const [shown, setShown] = runWithOwner(null, () => createSignal('init'));
46
+ page.afterHydration(() => setShown('live'));
47
+ const masked = () => shown() === 'live' ? live() : initView;
48
+ masks.set(def, masked);
49
+ return masked;
50
+ }
51
+ /**
52
+ * Kit-internal: the page's instance of `def` in `scope`, created on first
53
+ * use in a detached root named `pm-<name>`.
54
+ *
55
+ * Why detached: an island root's owner ids must be the same on the server
56
+ * and in the browser. Which island asks first can differ (the server renders
57
+ * islands in tree order; the browser hydrates them in DOM order; a lazy
58
+ * island may ask later), so creating the model under the asking island's
59
+ * owner would shift that island's ids. A detached root with its own id
60
+ * namespace allocates nothing in any island.
61
+ */
62
+ export function pageModelIn(scope, def) {
63
+ const existing = scope.pageModels.get(def);
64
+ if (existing) {
65
+ // A persisted model from an earlier page, asked for during this page's
66
+ // hydration pass: show `init` until the pass ends.
67
+ const life = scope.pageModelLives.get(def);
68
+ if (life && life.page !== scope.page && !scope.page.isHydrated()) {
69
+ return [
70
+ maskedUntilHydrated(scope, scope.page, def, existing[0], pageInit(def.spec.init)),
71
+ existing[1],
72
+ ];
73
+ }
74
+ return existing;
75
+ }
76
+ const spec = def.spec;
77
+ const peers = {
78
+ send: (other, msg) => pageModelIn(scope, other)[1](msg),
79
+ };
80
+ const options = {
81
+ init: pageInit(spec.init),
82
+ update: spec.update,
83
+ commands: spec.commands,
84
+ listens: spec.listens,
85
+ urlChanged: spec.urlChanged,
86
+ subscriptions: spec.subscriptions,
87
+ onTrace: spec.onTrace
88
+ ? (entry) => spec.onTrace?.(entry, peers)
89
+ : undefined,
90
+ };
91
+ const page = scope.page;
92
+ const created = runWithOwner(null, () => createRoot((dispose) => {
93
+ if (isServer)
94
+ return createModelIn(options, scope, pageStarter(scope));
95
+ // Browser: disposable (client navigation disposes every page model
96
+ // that isn't `persist`), and what its start returned (listens,
97
+ // subscriptions) stops with it.
98
+ const stops = [];
99
+ let alive = true;
100
+ onCleanup(() => {
101
+ alive = false;
102
+ for (const stop of stops.splice(0))
103
+ stop();
104
+ });
105
+ scope.pageModelLives.set(def, {
106
+ dispose,
107
+ persist: spec.persist === true,
108
+ page,
109
+ section: scope.section,
110
+ });
111
+ return createModelIn(options, scope, pageStarter(scope, { stops, alive: () => alive }));
112
+ }, { id: `pm-${spec.name}` }));
113
+ scope.pageModels.set(def, created);
114
+ return created;
115
+ }
116
+ /**
117
+ * The page's shared model for `def`: `[state, send]`, the same pair in
118
+ * every island (and in the static HTML around them) on this page.
119
+ *
120
+ * - Server (SSR, prerender): created once per request from `init`; the
121
+ * static HTML and every island render from that state.
122
+ * - Browser: created once per page, by the first island that asks, from
123
+ * the same `init` (so hydration matches). Its init Commands, `listens`,
124
+ * `subscriptions` and `urlChanged` start after every island on the page
125
+ * has hydrated (never during the hydration pass).
126
+ * - Static HTML outside islands is rendered once on the server and never
127
+ * updates: put what changes with the model in an island.
128
+ *
129
+ * @throws Outside `<AppScope>` (the page root, or an island root).
130
+ *
131
+ * @example
132
+ * ```tsx
133
+ * export default function TraceIsland() {
134
+ * const [trace, sendTrace] = usePageModel(tracePage);
135
+ * const [plan] = usePageModel(plannerPage);
136
+ * return <Badge>{traceBadge(trace(), plan())}</Badge>;
137
+ * }
138
+ * ```
139
+ */
140
+ export function usePageModel(def) {
141
+ const scope = useKitScope();
142
+ if (!scope) {
143
+ throw new Error(`usePageModel(${def.spec.name}) needs <AppScope> (the page root) or an island root.`);
144
+ }
145
+ return pageModelIn(scope, def);
146
+ }
@@ -0,0 +1,87 @@
1
+ import { type Accessor } from 'solid-js';
2
+ import { type UnexpectedQueryError } from '../logic/query-types.ts';
3
+ import type { Result as ResultValue, Tagged } from '../logic/result.ts';
4
+ import type { KitScope } from '../runtime/query-scope.ts';
5
+ /**
6
+ * Client-side side effects of `<Query>` (outcome publishing and Unexpected
7
+ * recovery), kept JSX-free so `tests/probes/query-hydration-ids.probe.ts`
8
+ * can run them under both the server and the browser build of Solid.
9
+ *
10
+ * Hydration rule: every primitive here is created on BOTH server and client.
11
+ * Solid 2 names owners (and DOM hydration keys) by their position among the
12
+ * parent owner's children, and the server's `createEffect` / `onSettled`
13
+ * allocate an id too. Gating creation on `isServer` shifts the ids of
14
+ * everything created after it under the same owner, and the client then
15
+ * misses the server's DOM ("Hydration key miss", detached, inert subtree).
16
+ * Gate the WORK inside the callbacks instead; effect halves never run on the
17
+ * server anyway.
18
+ *
19
+ * `ssrSource: "client"` would also keep ids aligned and skip the server
20
+ * compute, but in solid-js 2.0.4 its hydration wrapper calls an
21
+ * `{ effect, error }` bundle as a function ("effectFn is not a function").
22
+ */
23
+ /**
24
+ * The async read behind `<Query>`: an async memo over `run()` that shares
25
+ * one execution per `cacheKey` through the scope's dedupe cache (Way 1).
26
+ * Re-runs when `cacheKey` or `attempt` changes; releases its lease on
27
+ * re-run / disposal.
28
+ *
29
+ * While hydrating, Solid runs the compute only to track dependencies (with
30
+ * `fetch` / `Promise` mocked) and adopts the server's serialized answer, so
31
+ * the promise `run()` returns then never settles. It is NOT cached: a later
32
+ * `<Query>` sharing the key would otherwise receive that dead promise and
33
+ * stay pending forever. (Such a reader makes its own call instead.)
34
+ *
35
+ * @param scope - The nearest `<AppScope>`'s scope, or `null` (no dedupe).
36
+ * @param cacheKey - Dedupe key accessor (query id + input + generation).
37
+ * @param attempt - Retry counter; bumping it re-runs the read.
38
+ * @param run - Executes the query (a server function call).
39
+ * @returns The memo accessor (pending under `<Loading>`, throws to `<Errored>`).
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * const data = createSharedRead(scope, cacheKey, attempt, () => def.execute(input));
44
+ * ```
45
+ */
46
+ export declare function createSharedRead<V>(scope: KitScope | null, cacheKey: Accessor<string>, attempt: Accessor<number>, run: () => Promise<V> | V): Accessor<V>;
47
+ /** The `{ _tag: "Unexpected" }` value `<Query>` reports for thrown failures. */
48
+ export declare const UNEXPECTED: UnexpectedQueryError;
49
+ /**
50
+ * Publishes every settled outcome of a `<Query>` read on the scope's
51
+ * channel (Way 2). Client only: the effect half never runs during SSR, and
52
+ * the first client publish happens after hydration with the hydrated value.
53
+ *
54
+ * @param scope - The nearest `<AppScope>`'s scope.
55
+ * @param queryId - Query id (`"<registry>/<name>"`).
56
+ * @param inputKey - Serialized input accessor.
57
+ * @param cacheKey - Dedupe key accessor (query id + input + generation).
58
+ * @param data - The `<Query>`'s async result memo.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * createOutcomePublisher(scope, "planner/character", inputKey, cacheKey, data);
63
+ * ```
64
+ */
65
+ export declare function createOutcomePublisher(scope: KitScope, queryId: string, inputKey: Accessor<string>, cacheKey: Accessor<string>, data: Accessor<ResultValue<unknown, Tagged>>): void;
66
+ /**
67
+ * Wiring for a `<Query>`'s Unexpected (thrown) failure view, created inside
68
+ * the `<Errored>` fallback:
69
+ * - publishes `{ _tag: "Failed", error: Unexpected }` once settled (client
70
+ * only, and only inside `<AppScope>`);
71
+ * - resets the boundary when `cacheKey` moves off `failedKey` (a Message
72
+ * moved on: a `Refetch` Command or a new `input`: that is how app code
73
+ * retries).
74
+ *
75
+ * @param scope - The nearest `<AppScope>`'s scope, or `null` outside one.
76
+ * @param queryId - Query id (`"<registry>/<name>"`).
77
+ * @param failedInputKey - The input key that failed (read once, untracked).
78
+ * @param cacheKey - Live dedupe key accessor.
79
+ * @param failedKey - The key that failed (read once, untracked).
80
+ * @param reset - The `<Errored>` fallback's `reset`.
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * createUnexpectedWatchers(scope, "planner/simStats", untrack(inputKey), cacheKey, untrack(cacheKey), reset);
85
+ * ```
86
+ */
87
+ export declare function createUnexpectedWatchers(scope: KitScope | null, queryId: string, failedInputKey: string, cacheKey: Accessor<string>, failedKey: string, reset: () => void): void;
@@ -0,0 +1,147 @@
1
+ import { isServer } from '@solidjs/web';
2
+ import { createEffect, createMemo, isHydrating, onCleanup, onSettled, untrack, } from 'solid-js';
3
+ import { UNEXPECTED_QUERY_MESSAGE, } from '../logic/query-types.js';
4
+ /**
5
+ * Client-side side effects of `<Query>` (outcome publishing and Unexpected
6
+ * recovery), kept JSX-free so `tests/probes/query-hydration-ids.probe.ts`
7
+ * can run them under both the server and the browser build of Solid.
8
+ *
9
+ * Hydration rule: every primitive here is created on BOTH server and client.
10
+ * Solid 2 names owners (and DOM hydration keys) by their position among the
11
+ * parent owner's children, and the server's `createEffect` / `onSettled`
12
+ * allocate an id too. Gating creation on `isServer` shifts the ids of
13
+ * everything created after it under the same owner, and the client then
14
+ * misses the server's DOM ("Hydration key miss", detached, inert subtree).
15
+ * Gate the WORK inside the callbacks instead; effect halves never run on the
16
+ * server anyway.
17
+ *
18
+ * `ssrSource: "client"` would also keep ids aligned and skip the server
19
+ * compute, but in solid-js 2.0.4 its hydration wrapper calls an
20
+ * `{ effect, error }` bundle as a function ("effectFn is not a function").
21
+ */
22
+ /**
23
+ * The async read behind `<Query>`: an async memo over `run()` that shares
24
+ * one execution per `cacheKey` through the scope's dedupe cache (Way 1).
25
+ * Re-runs when `cacheKey` or `attempt` changes; releases its lease on
26
+ * re-run / disposal.
27
+ *
28
+ * While hydrating, Solid runs the compute only to track dependencies (with
29
+ * `fetch` / `Promise` mocked) and adopts the server's serialized answer, so
30
+ * the promise `run()` returns then never settles. It is NOT cached: a later
31
+ * `<Query>` sharing the key would otherwise receive that dead promise and
32
+ * stay pending forever. (Such a reader makes its own call instead.)
33
+ *
34
+ * @param scope - The nearest `<AppScope>`'s scope, or `null` (no dedupe).
35
+ * @param cacheKey - Dedupe key accessor (query id + input + generation).
36
+ * @param attempt - Retry counter; bumping it re-runs the read.
37
+ * @param run - Executes the query (a server function call).
38
+ * @returns The memo accessor (pending under `<Loading>`, throws to `<Errored>`).
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * const data = createSharedRead(scope, cacheKey, attempt, () => def.execute(input));
43
+ * ```
44
+ */
45
+ export function createSharedRead(scope, cacheKey, attempt, run) {
46
+ return createMemo(() => {
47
+ void attempt();
48
+ const key = cacheKey();
49
+ if (!scope || isHydrating())
50
+ return run();
51
+ const lease = scope.queries.acquire(key, run);
52
+ onCleanup(lease.release);
53
+ // A fresh promise per reader: Solid stamps a settled promise it has
54
+ // seen, and a memo handed an already-stamped promise adopts it
55
+ // synchronously without serializing it. A reader inside an island
56
+ // sharing a read with the static page around it (or another island)
57
+ // must still put the value in its own hydration payload.
58
+ const value = lease.value;
59
+ return isThenable(value) ? value.then((v) => v) : value;
60
+ });
61
+ }
62
+ function isThenable(value) {
63
+ return typeof value?.then === 'function';
64
+ }
65
+ /** The `{ _tag: "Unexpected" }` value `<Query>` reports for thrown failures. */
66
+ export const UNEXPECTED = {
67
+ _tag: 'Unexpected',
68
+ message: UNEXPECTED_QUERY_MESSAGE,
69
+ };
70
+ function toOutcome(result) {
71
+ return result._tag === 'Ok'
72
+ ? { _tag: 'Succeeded', value: result.value }
73
+ : { _tag: 'Failed', error: result.error };
74
+ }
75
+ /**
76
+ * Publishes every settled outcome of a `<Query>` read on the scope's
77
+ * channel (Way 2). Client only: the effect half never runs during SSR, and
78
+ * the first client publish happens after hydration with the hydrated value.
79
+ *
80
+ * @param scope - The nearest `<AppScope>`'s scope.
81
+ * @param queryId - Query id (`"<registry>/<name>"`).
82
+ * @param inputKey - Serialized input accessor.
83
+ * @param cacheKey - Dedupe key accessor (query id + input + generation).
84
+ * @param data - The `<Query>`'s async result memo.
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * createOutcomePublisher(scope, "planner/character", inputKey, cacheKey, data);
89
+ * ```
90
+ */
91
+ export function createOutcomePublisher(scope, queryId, inputKey, cacheKey, data) {
92
+ createEffect(() => ({ key: cacheKey(), input: inputKey(), result: data() }), {
93
+ effect: ({ key, input, result }) => {
94
+ scope.outcomes.publish({
95
+ queryId,
96
+ inputKey: input,
97
+ key,
98
+ outcome: toOutcome(result),
99
+ });
100
+ },
101
+ error: () => {
102
+ scope.outcomes.publish({
103
+ queryId,
104
+ inputKey: untrack(inputKey),
105
+ key: untrack(cacheKey),
106
+ outcome: { _tag: 'Failed', error: UNEXPECTED },
107
+ });
108
+ },
109
+ });
110
+ }
111
+ /**
112
+ * Wiring for a `<Query>`'s Unexpected (thrown) failure view, created inside
113
+ * the `<Errored>` fallback:
114
+ * - publishes `{ _tag: "Failed", error: Unexpected }` once settled (client
115
+ * only, and only inside `<AppScope>`);
116
+ * - resets the boundary when `cacheKey` moves off `failedKey` (a Message
117
+ * moved on: a `Refetch` Command or a new `input`: that is how app code
118
+ * retries).
119
+ *
120
+ * @param scope - The nearest `<AppScope>`'s scope, or `null` outside one.
121
+ * @param queryId - Query id (`"<registry>/<name>"`).
122
+ * @param failedInputKey - The input key that failed (read once, untracked).
123
+ * @param cacheKey - Live dedupe key accessor.
124
+ * @param failedKey - The key that failed (read once, untracked).
125
+ * @param reset - The `<Errored>` fallback's `reset`.
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * createUnexpectedWatchers(scope, "planner/simStats", untrack(inputKey), cacheKey, untrack(cacheKey), reset);
130
+ * ```
131
+ */
132
+ export function createUnexpectedWatchers(scope, queryId, failedInputKey, cacheKey, failedKey, reset) {
133
+ onSettled(() => {
134
+ if (isServer || !scope)
135
+ return;
136
+ scope.outcomes.publish({
137
+ queryId,
138
+ inputKey: failedInputKey,
139
+ key: failedKey,
140
+ outcome: { _tag: 'Failed', error: UNEXPECTED },
141
+ });
142
+ });
143
+ createEffect(cacheKey, (key) => {
144
+ if (key !== failedKey)
145
+ reset();
146
+ });
147
+ }