@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,163 @@
1
+ import type { AnyLiveRoomsDecl } from '../live/declaration.ts';
2
+ /**
3
+ * The app's Cloudflare resources, declared once (`src/resources.ts`). Pure
4
+ * data: the Alchemy stack (`uiKitApp`, `@alecsibilia/ui-kit/alchemy`), the
5
+ * local workerd config (`vite dev` bindings and `ui-kit preview`) and the
6
+ * server's typed `env` (`bindingsOf`, `@alecsibilia/ui-kit/server`) are all
7
+ * derived from it, so deploy, dev and code can't drift apart.
8
+ *
9
+ * Kit features turn their resources on here:
10
+ * - `sessions`: the kit's `sessions` table (its migration is applied to the
11
+ * named D1 database next to the app's own) and the hourly cleanup cron.
12
+ * - `liveRooms`: the room Durable Object namespace and its connect limit,
13
+ * from the live rooms declaration (`declareLiveRooms`).
14
+ * - `readCache`: Workers Cache in front of the Worker (shared GET reads).
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * export const resources = defineResources({
19
+ * name: 'my-app',
20
+ * compatibility: { date: '2026-11-01', flags: ['nodejs_compat'] },
21
+ * d1: { DB: { migrations: 'migrations' } },
22
+ * r2: { FILES: {} },
23
+ * sessions: { database: 'DB' },
24
+ * readCache: true,
25
+ * })
26
+ * ```
27
+ */
28
+ /** The slice of Cloudflare's `D1Database` server code uses (structural, so tests pass fakes). */
29
+ export interface D1Binding {
30
+ prepare(query: string): D1Statement;
31
+ /** Runs the statements in one transaction (all or none). */
32
+ batch(statements: D1Statement[]): Promise<unknown[]>;
33
+ }
34
+ /** The slice of D1's prepared statement server code uses. */
35
+ export interface D1Statement {
36
+ bind(...values: unknown[]): D1Statement;
37
+ first<T = Record<string, unknown>>(): Promise<T | null>;
38
+ /** Runs a write; resolves when it committed. */
39
+ run(): Promise<unknown>;
40
+ /** Every row (a `SELECT`, or a write with `RETURNING`). */
41
+ all<T = Record<string, unknown>>(): Promise<{
42
+ readonly results: T[];
43
+ }>;
44
+ }
45
+ /** The slice of Cloudflare's `R2Bucket` server code uses. */
46
+ export interface R2Binding {
47
+ put(key: string, value: string, options?: {
48
+ readonly httpMetadata?: {
49
+ readonly contentType?: string;
50
+ };
51
+ }): Promise<unknown>;
52
+ head(key: string): Promise<unknown>;
53
+ }
54
+ /** The Workers Rate Limiting binding: counts one call for `key`. */
55
+ export interface RateLimitBinding {
56
+ limit(options: {
57
+ readonly key: string;
58
+ }): Promise<{
59
+ readonly success: boolean;
60
+ }>;
61
+ }
62
+ /** The slice of a Durable Object namespace the Worker uses to route to an object. */
63
+ export interface DurableObjectNamespaceBinding {
64
+ idFromName(name: string): unknown;
65
+ get(id: unknown): {
66
+ fetch(request: Request): Promise<Response>;
67
+ };
68
+ }
69
+ /** A D1 database. `migrations`: the app's SQL folder (relative to the app). */
70
+ export interface D1Decl {
71
+ readonly migrations?: string;
72
+ }
73
+ /** A Workers Rate Limiting binding (no backing resource). */
74
+ export interface RateLimitDecl {
75
+ /** Account-unique namespace id (a positive integer, as a string). */
76
+ readonly namespaceId: string;
77
+ readonly limit: number;
78
+ /** Seconds: Cloudflare allows 10 or 60. */
79
+ readonly period: 10 | 60;
80
+ }
81
+ /** A SQLite-backed Durable Object class the Worker exports. */
82
+ export interface DurableObjectDecl {
83
+ readonly className: string;
84
+ /** The class migration's tag (`v1`, `v2`, ...); migrations run in tag order. */
85
+ readonly migrationTag: string;
86
+ }
87
+ /** Everything an app declares. */
88
+ export interface ResourcesDecl {
89
+ /** The Worker's name and every resource's base name (stages add `-<stage>`). */
90
+ readonly name: string;
91
+ readonly compatibility: {
92
+ readonly date: string;
93
+ readonly flags: readonly string[];
94
+ };
95
+ readonly d1?: {
96
+ readonly [binding: string]: D1Decl;
97
+ };
98
+ readonly r2?: {
99
+ readonly [binding: string]: Readonly<Record<never, never>>;
100
+ };
101
+ readonly rateLimits?: {
102
+ readonly [binding: string]: RateLimitDecl;
103
+ };
104
+ readonly durableObjects?: {
105
+ readonly [binding: string]: DurableObjectDecl;
106
+ };
107
+ /** The app's own cron triggers (the sessions cleanup adds its own). */
108
+ readonly crons?: readonly string[];
109
+ /** Kit sessions: the D1 binding that holds the kit's `sessions` table. */
110
+ readonly sessions?: {
111
+ readonly database: string;
112
+ /** The expired-session cleanup's cron (default hourly at :17). */
113
+ readonly cron?: string;
114
+ };
115
+ readonly liveRooms?: AnyLiveRoomsDecl;
116
+ /** Workers Cache in front of the Worker. */
117
+ readonly readCache?: boolean;
118
+ }
119
+ /** What the sessions cleanup cron defaults to. */
120
+ export declare const DEFAULT_SESSIONS_CRON = "17 * * * *";
121
+ type KeysOf<T> = T extends Readonly<Record<string, unknown>> ? keyof T : never;
122
+ type LiveRoomBindings<L> = L extends {
123
+ readonly durableObject: {
124
+ readonly binding: infer B extends string;
125
+ };
126
+ } ? {
127
+ readonly [K in B]: DurableObjectNamespaceBinding;
128
+ } & (L extends {
129
+ readonly connectLimit: {
130
+ readonly binding: infer C extends string;
131
+ };
132
+ } ? {
133
+ readonly [K in C]: RateLimitBinding;
134
+ } : unknown) : unknown;
135
+ /** The Worker `env` a declaration gives: one typed binding per declared resource. */
136
+ export type EnvOf<R extends ResourcesDecl> = {
137
+ readonly [K in KeysOf<R['d1']>]: D1Binding;
138
+ } & {
139
+ readonly [K in KeysOf<R['r2']>]: R2Binding;
140
+ } & {
141
+ readonly [K in KeysOf<R['rateLimits']>]: RateLimitBinding;
142
+ } & {
143
+ readonly [K in KeysOf<R['durableObjects']>]: DurableObjectNamespaceBinding;
144
+ } & LiveRoomBindings<R['liveRooms']>;
145
+ /** One binding's kind, and a method that proves a value is one. */
146
+ export interface BindingSpec {
147
+ readonly kind: 'D1' | 'R2' | 'rate limit' | 'Durable Object namespace';
148
+ readonly method: string;
149
+ }
150
+ /** Every binding a declaration gives, by name (what `bindingsOf` checks). */
151
+ export declare function bindingSpecsOf(resources: ResourcesDecl): Readonly<Record<string, BindingSpec>>;
152
+ /**
153
+ * Checks a declaration: the sessions database must be a declared D1
154
+ * binding, and no binding name may be declared twice. Returns the problems
155
+ * (empty when none); `uiKitApp` and the Vite plugin fail on any.
156
+ */
157
+ export declare function resourceProblems(resources: ResourcesDecl): string[];
158
+ /**
159
+ * Declares the app's resources. Returns the declaration unchanged, typed
160
+ * exactly (`const`), so `EnvOf<typeof resources>` names every binding.
161
+ */
162
+ export declare function defineResources<const R extends ResourcesDecl>(resources: R): R;
163
+ export {};
@@ -0,0 +1,62 @@
1
+ /** What the sessions cleanup cron defaults to. */
2
+ export const DEFAULT_SESSIONS_CRON = '17 * * * *';
3
+ /** Every binding a declaration gives, by name (what `bindingsOf` checks). */
4
+ export function bindingSpecsOf(resources) {
5
+ const specs = {};
6
+ for (const name of Object.keys(resources.d1 ?? {}))
7
+ specs[name] = { kind: 'D1', method: 'prepare' };
8
+ for (const name of Object.keys(resources.r2 ?? {}))
9
+ specs[name] = { kind: 'R2', method: 'put' };
10
+ for (const name of Object.keys(resources.rateLimits ?? {}))
11
+ specs[name] = { kind: 'rate limit', method: 'limit' };
12
+ for (const name of Object.keys(resources.durableObjects ?? {}))
13
+ specs[name] = { kind: 'Durable Object namespace', method: 'idFromName' };
14
+ const rooms = resources.liveRooms;
15
+ if (rooms) {
16
+ specs[rooms.durableObject.binding] = {
17
+ kind: 'Durable Object namespace',
18
+ method: 'idFromName',
19
+ };
20
+ if (rooms.connectLimit)
21
+ specs[rooms.connectLimit.binding] = {
22
+ kind: 'rate limit',
23
+ method: 'limit',
24
+ };
25
+ }
26
+ return specs;
27
+ }
28
+ /**
29
+ * Checks a declaration: the sessions database must be a declared D1
30
+ * binding, and no binding name may be declared twice. Returns the problems
31
+ * (empty when none); `uiKitApp` and the Vite plugin fail on any.
32
+ */
33
+ export function resourceProblems(resources) {
34
+ const problems = [];
35
+ const seen = new Map();
36
+ const groups = [
37
+ ['d1', resources.d1],
38
+ ['r2', resources.r2],
39
+ ['rateLimits', resources.rateLimits],
40
+ ['durableObjects', resources.durableObjects],
41
+ ];
42
+ for (const [group, entries] of groups) {
43
+ for (const name of Object.keys(entries ?? {})) {
44
+ const before = seen.get(name);
45
+ if (before)
46
+ problems.push(`binding "${name}" is declared in both ${before} and ${group}`);
47
+ seen.set(name, group);
48
+ }
49
+ }
50
+ const db = resources.sessions?.database;
51
+ if (db !== undefined && !(db in (resources.d1 ?? {}))) {
52
+ problems.push(`sessions.database "${db}" is not a D1 binding declared in d1`);
53
+ }
54
+ return problems;
55
+ }
56
+ /**
57
+ * Declares the app's resources. Returns the declaration unchanged, typed
58
+ * exactly (`const`), so `EnvOf<typeof resources>` names every binding.
59
+ */
60
+ export function defineResources(resources) {
61
+ return resources;
62
+ }
@@ -0,0 +1,30 @@
1
+ export interface Tagged {
2
+ readonly _tag: string;
3
+ }
4
+ export type TagHandlers<T extends Tagged, R> = {
5
+ readonly [K in T['_tag']]: (value: Extract<T, {
6
+ readonly _tag: K;
7
+ }>) => R;
8
+ };
9
+ /**
10
+ * Exhaustive discriminated-union matcher over `_tag`.
11
+ * Zero runtime dependency on Effect — safe for browser bundles.
12
+ */
13
+ export declare function matchTag<T extends Tagged, R>(value: T, handlers: {
14
+ readonly [K in T['_tag']]: (value: Extract<T, {
15
+ readonly _tag: K;
16
+ }>) => R;
17
+ }): R;
18
+ export type Ok<T> = {
19
+ readonly _tag: 'Ok';
20
+ readonly value: T;
21
+ };
22
+ export type Err<E extends Tagged> = {
23
+ readonly _tag: 'Err';
24
+ readonly error: E;
25
+ };
26
+ export type Result<T, E extends Tagged> = Ok<T> | Err<E>;
27
+ export declare function ok<T>(value: T): Ok<T>;
28
+ export declare function err<E extends Tagged>(error: E): Err<E>;
29
+ export declare function isOk<T, E extends Tagged>(result: Result<T, E>): result is Ok<T>;
30
+ export declare function isErr<T, E extends Tagged>(result: Result<T, E>): result is Err<E>;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Exhaustive discriminated-union matcher over `_tag`.
3
+ * Zero runtime dependency on Effect — safe for browser bundles.
4
+ */
5
+ export function matchTag(value, handlers) {
6
+ const handler = handlers[value._tag];
7
+ if (!handler) {
8
+ throw new Error(`Unhandled tag: ${String(value._tag)}`);
9
+ }
10
+ return handler(value);
11
+ }
12
+ export function ok(value) {
13
+ return { _tag: 'Ok', value };
14
+ }
15
+ export function err(error) {
16
+ return { _tag: 'Err', error };
17
+ }
18
+ export function isOk(result) {
19
+ return result._tag === 'Ok';
20
+ }
21
+ export function isErr(result) {
22
+ return result._tag === 'Err';
23
+ }
@@ -0,0 +1,139 @@
1
+ import { type NavigateCommand } from './kit-commands.ts';
2
+ import { type Listens } from './listen.ts';
3
+ import type { AnyQueryRegistry, QueryDef, RegistryIdOf } from './query-def.ts';
4
+ import { type PathRouteOf, type PathTagOf, type RouteOf, type RoutePaths } from './route-table.ts';
5
+ /**
6
+ * The kit's route model: navigation and the client half of the auth guard,
7
+ * as a pure Elm-style `update` an app's shell model delegates to.
8
+ * Import-free (no Solid, no DOM), so a `.model.ts` can use it.
9
+ *
10
+ * - `Navigated` (a link) switches the route and returns the kit `Navigate`
11
+ * Command; `UrlChanged` (Back / Forward, or a `Navigate` from another
12
+ * model) decodes the URL with the table's matcher.
13
+ * - Auth is driven by the route table: a route with `auth: "required"`
14
+ * while the visitor is known to be anonymous becomes the sign-in route
15
+ * with `?next=` the page (the same URL the Worker's 303 sends). Signing
16
+ * out (or any read failing `Unauthorized`) on a protected page goes to
17
+ * sign-in; signing in on the sign-in page goes on to `next`, if it is a
18
+ * page of the table (never another origin), else to `auth.home`.
19
+ * - Who is looking arrives as `ViewerSeen`, from the one viewer query the
20
+ * app names in `routerListens`, plus every read that fails `Unauthorized`.
21
+ *
22
+ * The app keeps only what is its own (nav links, extra shell state) and
23
+ * names its sign-in route once (`RouterAuth`), as plain `init` data: a
24
+ * `.model.ts` cannot import the route table at runtime.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * // routes.ts
29
+ * export const routeAuth = { signIn: "SignIn", nextParam: "next", home: "Account" } as const;
30
+ * // route.model.ts
31
+ * export const initRouteModel = (paths: AppPaths, url: string) => initRouter(paths, routeAuth, url);
32
+ * export const updateRoute = (m: RouteModel, msg: RouteMsg) => updateRouter<AppRoutes, RouteModel>(m, msg);
33
+ * export const routeListens = routerListens<AppRoutes, typeof accountQueries>("account")(
34
+ * "viewer", (viewer) => viewer._tag === "SignedIn");
35
+ * ```
36
+ */
37
+ /** Who is looking, as far as the browser knows. `Unknown` until the viewer query lands. */
38
+ export declare const Who: {
39
+ readonly Unknown: 'Unknown';
40
+ readonly Anonymous: 'Anonymous';
41
+ readonly SignedIn: 'SignedIn';
42
+ };
43
+ export type Who = (typeof Who)[keyof typeof Who];
44
+ /**
45
+ * The app's sign-in route, named once. Its `nextParam` must be a text
46
+ * search param of that route (`searchParam.text()`).
47
+ */
48
+ export interface RouterAuth<T> {
49
+ /** Where `auth: "required"` routes send an anonymous visitor. */
50
+ readonly signIn: PathTagOf<T>;
51
+ /** The sign-in route's search param holding where to go after (`"next"`). */
52
+ readonly nextParam: string;
53
+ /** Where to go after sign-in when `next` is missing or not a page of the table. */
54
+ readonly home: PathTagOf<T>;
55
+ }
56
+ export interface RouterModel<T> {
57
+ readonly route: RouteOf<T>;
58
+ readonly paths: RoutePaths<T>;
59
+ readonly auth: RouterAuth<T>;
60
+ readonly who: Who;
61
+ }
62
+ export type RouterMsg<T> = {
63
+ readonly _tag: 'Navigated';
64
+ readonly to: PathRouteOf<T>;
65
+ } | {
66
+ readonly _tag: 'UrlChanged';
67
+ readonly url: string;
68
+ } | {
69
+ readonly _tag: 'ViewerSeen';
70
+ readonly who: Who;
71
+ };
72
+ /** The Message for a URL change (wire as `createModel({ urlChanged: routerUrlChanged })`). */
73
+ export declare function routerUrlChanged(url: string): {
74
+ readonly _tag: 'UrlChanged';
75
+ readonly url: string;
76
+ };
77
+ /**
78
+ * The route model's starting state: the route of `url` (the request URL
79
+ * during SSR, `location` in the browser), visitor `Unknown`.
80
+ */
81
+ export declare function initRouter<T>(paths: RoutePaths<T>, auth: RouterAuth<T>, url: string): RouterModel<T>;
82
+ /**
83
+ * The sign-in route that comes back to `route` (to `auth.home` from the
84
+ * sign-in page itself or an unknown page).
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * hrefFor(paths, signInRouteFor(model, model.route)); // "/sign-in?next=%2Fplans%2Fx"
89
+ * ```
90
+ */
91
+ export declare function signInRouteFor<T>(model: RouterModel<T>, route: RouteOf<T>): PathRouteOf<T>;
92
+ /**
93
+ * Where to go after signing in: `next` when it is a page of the table
94
+ * (a local path, not the sign-in page, not `NotFound`), else `auth.home`.
95
+ * Also what a no-JS sign-in form posts as `_next`.
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * afterSignIn(paths, auth, "/plans/x?tab=stats"); // the PlanDetail route
100
+ * afterSignIn(paths, auth, "//evil.example/x"); // the home route
101
+ * ```
102
+ */
103
+ export declare function afterSignIn<T>(paths: RoutePaths<T>, auth: RouterAuth<T>, next: string): PathRouteOf<T>;
104
+ /**
105
+ * The route model's `update`. Generic over the app's model (`M` may add
106
+ * its own fields, which are kept); returns only kit `Navigate` Commands.
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * updateRouter<AppRoutes, RouteModel>(model, { _tag: "Navigated", to: to.account() });
111
+ * // anonymous: [{ ...model, route: SignIn }, [{ _tag: "Navigate", href: "/sign-in?next=%2Faccount" }]]
112
+ * ```
113
+ */
114
+ export declare function updateRouter<T, M extends RouterModel<T> = RouterModel<T>>(model: M, msg: RouterMsg<T>): readonly [M, readonly NavigateCommand[]];
115
+ /** The value type of query `K` in registry `R`. */
116
+ type QueryValue<R extends AnyQueryRegistry, K extends keyof R> = R[K] extends QueryDef<any, infer V, any> ? V : never;
117
+ /**
118
+ * The route model's `listens`: the app names its viewer query once and
119
+ * says when its value means signed in; every read that fails
120
+ * `Unauthorized` (the session ended) also means anonymous. Delivered in
121
+ * the browser after hydration, like every `listen`.
122
+ *
123
+ * Curried like `listen`: the table and registry types are explicit, then
124
+ * the viewer query's name and a predicate on its (typed) value.
125
+ *
126
+ * @param registry - The viewer query's registry id (`import type` the registry).
127
+ * @returns `(query, signedIn) => Listens`: the viewer query's name in that
128
+ * registry, and whether a viewer value is a signed-in visitor.
129
+ *
130
+ * @example
131
+ * ```ts
132
+ * export const routeListens = routerListens<AppRoutes, typeof accountQueries>("account")(
133
+ * "viewer",
134
+ * (viewer) => viewer._tag === "SignedIn",
135
+ * );
136
+ * ```
137
+ */
138
+ export declare function routerListens<T, R extends AnyQueryRegistry>(registry: RegistryIdOf<R>): <K extends keyof R & string>(query: K, signedIn: (value: QueryValue<R, K>) => boolean) => Listens<RouterMsg<T>>;
139
+ export {};
@@ -0,0 +1,207 @@
1
+ import { navigate } from './kit-commands.js';
2
+ import { compiledListens, } from './listen.js';
3
+ import { queryIdOf } from './query-key.js';
4
+ import { matchTag } from './result.js';
5
+ import { hrefFor, matchRoute, sameRoute, } from './route-table.js';
6
+ /**
7
+ * The kit's route model: navigation and the client half of the auth guard,
8
+ * as a pure Elm-style `update` an app's shell model delegates to.
9
+ * Import-free (no Solid, no DOM), so a `.model.ts` can use it.
10
+ *
11
+ * - `Navigated` (a link) switches the route and returns the kit `Navigate`
12
+ * Command; `UrlChanged` (Back / Forward, or a `Navigate` from another
13
+ * model) decodes the URL with the table's matcher.
14
+ * - Auth is driven by the route table: a route with `auth: "required"`
15
+ * while the visitor is known to be anonymous becomes the sign-in route
16
+ * with `?next=` the page (the same URL the Worker's 303 sends). Signing
17
+ * out (or any read failing `Unauthorized`) on a protected page goes to
18
+ * sign-in; signing in on the sign-in page goes on to `next`, if it is a
19
+ * page of the table (never another origin), else to `auth.home`.
20
+ * - Who is looking arrives as `ViewerSeen`, from the one viewer query the
21
+ * app names in `routerListens`, plus every read that fails `Unauthorized`.
22
+ *
23
+ * The app keeps only what is its own (nav links, extra shell state) and
24
+ * names its sign-in route once (`RouterAuth`), as plain `init` data: a
25
+ * `.model.ts` cannot import the route table at runtime.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * // routes.ts
30
+ * export const routeAuth = { signIn: "SignIn", nextParam: "next", home: "Account" } as const;
31
+ * // route.model.ts
32
+ * export const initRouteModel = (paths: AppPaths, url: string) => initRouter(paths, routeAuth, url);
33
+ * export const updateRoute = (m: RouteModel, msg: RouteMsg) => updateRouter<AppRoutes, RouteModel>(m, msg);
34
+ * export const routeListens = routerListens<AppRoutes, typeof accountQueries>("account")(
35
+ * "viewer", (viewer) => viewer._tag === "SignedIn");
36
+ * ```
37
+ */
38
+ /** Who is looking, as far as the browser knows. `Unknown` until the viewer query lands. */
39
+ export const Who = {
40
+ Unknown: 'Unknown',
41
+ Anonymous: 'Anonymous',
42
+ SignedIn: 'SignedIn',
43
+ };
44
+ /** The Message for a URL change (wire as `createModel({ urlChanged: routerUrlChanged })`). */
45
+ export function routerUrlChanged(url) {
46
+ return { _tag: 'UrlChanged', url };
47
+ }
48
+ /**
49
+ * The route model's starting state: the route of `url` (the request URL
50
+ * during SSR, `location` in the browser), visitor `Unknown`.
51
+ */
52
+ export function initRouter(paths, auth, url) {
53
+ return { route: matchRoute(paths, url), paths, auth, who: Who.Unknown };
54
+ }
55
+ /** A route value for a tag with no params and the given search. */
56
+ function routeOf(tag, search) {
57
+ return { _tag: tag, params: {}, search };
58
+ }
59
+ /**
60
+ * The sign-in route that comes back to `route` (to `auth.home` from the
61
+ * sign-in page itself or an unknown page).
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * hrefFor(paths, signInRouteFor(model, model.route)); // "/sign-in?next=%2Fplans%2Fx"
66
+ * ```
67
+ */
68
+ export function signInRouteFor(model, route) {
69
+ const value = route;
70
+ const back = value._tag === 'NotFound' || value._tag === model.auth.signIn
71
+ ? routeOf(model.auth.home, {})
72
+ : route;
73
+ return routeOf(model.auth.signIn, {
74
+ [model.auth.nextParam]: hrefFor(model.paths, back),
75
+ });
76
+ }
77
+ /**
78
+ * Where to go after signing in: `next` when it is a page of the table
79
+ * (a local path, not the sign-in page, not `NotFound`), else `auth.home`.
80
+ * Also what a no-JS sign-in form posts as `_next`.
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * afterSignIn(paths, auth, "/plans/x?tab=stats"); // the PlanDetail route
85
+ * afterSignIn(paths, auth, "//evil.example/x"); // the home route
86
+ * ```
87
+ */
88
+ export function afterSignIn(paths, auth, next) {
89
+ const local = next.startsWith('/') && !next.startsWith('//') && !next.includes('\\');
90
+ const target = local
91
+ ? matchRoute(paths, next)
92
+ : undefined;
93
+ if (!target || target._tag === 'NotFound' || target._tag === auth.signIn) {
94
+ return routeOf(auth.home, {});
95
+ }
96
+ return target;
97
+ }
98
+ /** The `next` of a sign-in route (empty when the route is not the sign-in route). */
99
+ function nextOf(model, route) {
100
+ const value = route;
101
+ if (value._tag !== model.auth.signIn)
102
+ return '';
103
+ const next = value.search[model.auth.nextParam];
104
+ return typeof next === 'string' ? next : '';
105
+ }
106
+ /** `route`, or the sign-in route when it is `auth: "required"` and the visitor is anonymous. */
107
+ function guarded(model, route) {
108
+ const tag = route._tag;
109
+ if (tag === 'NotFound' || model.who !== Who.Anonymous)
110
+ return route;
111
+ const spec = model.paths[tag];
112
+ if (spec?.auth !== 'required')
113
+ return route;
114
+ return signInRouteFor(model, route);
115
+ }
116
+ /**
117
+ * The route model's `update`. Generic over the app's model (`M` may add
118
+ * its own fields, which are kept); returns only kit `Navigate` Commands.
119
+ *
120
+ * @example
121
+ * ```ts
122
+ * updateRouter<AppRoutes, RouteModel>(model, { _tag: "Navigated", to: to.account() });
123
+ * // anonymous: [{ ...model, route: SignIn }, [{ _tag: "Navigate", href: "/sign-in?next=%2Faccount" }]]
124
+ * ```
125
+ */
126
+ export function updateRouter(model, msg) {
127
+ const href = (route) => hrefFor(model.paths, route);
128
+ return matchTag(msg, {
129
+ Navigated: ({ to, }) => {
130
+ const target = guarded(model, to);
131
+ if (sameRoute(model.paths, model.route, target))
132
+ return [model, []];
133
+ return [{ ...model, route: target }, [navigate(href(target))]];
134
+ },
135
+ UrlChanged: ({ url, }) => {
136
+ const route = matchRoute(model.paths, url);
137
+ const target = guarded(model, route);
138
+ const redirect = target === route
139
+ ? []
140
+ : [navigate(href(target), { replace: true })];
141
+ if (sameRoute(model.paths, model.route, target))
142
+ return [model, redirect];
143
+ return [{ ...model, route: target }, redirect];
144
+ },
145
+ ViewerSeen: ({ who, }) => {
146
+ if (who === Who.Unknown || who === model.who)
147
+ return [model, []];
148
+ const next = { ...model, who };
149
+ // Signed out (or the session ended) on a protected page: to sign-in.
150
+ const target = guarded(next, model.route);
151
+ if (target !== model.route) {
152
+ return [
153
+ { ...next, route: target },
154
+ [navigate(href(target), { replace: true })],
155
+ ];
156
+ }
157
+ // Signed in on the sign-in page: on to where the visitor was headed.
158
+ if (who === Who.SignedIn &&
159
+ model.route._tag ===
160
+ model.auth.signIn) {
161
+ const after = afterSignIn(model.paths, model.auth, nextOf(model, model.route));
162
+ return [
163
+ { ...next, route: after },
164
+ [
165
+ navigate(href(after), {
166
+ replace: true,
167
+ }),
168
+ ],
169
+ ];
170
+ }
171
+ return [next, []];
172
+ },
173
+ });
174
+ }
175
+ /**
176
+ * The route model's `listens`: the app names its viewer query once and
177
+ * says when its value means signed in; every read that fails
178
+ * `Unauthorized` (the session ended) also means anonymous. Delivered in
179
+ * the browser after hydration, like every `listen`.
180
+ *
181
+ * Curried like `listen`: the table and registry types are explicit, then
182
+ * the viewer query's name and a predicate on its (typed) value.
183
+ *
184
+ * @param registry - The viewer query's registry id (`import type` the registry).
185
+ * @returns `(query, signedIn) => Listens`: the viewer query's name in that
186
+ * registry, and whether a viewer value is a signed-in visitor.
187
+ *
188
+ * @example
189
+ * ```ts
190
+ * export const routeListens = routerListens<AppRoutes, typeof accountQueries>("account")(
191
+ * "viewer",
192
+ * (viewer) => viewer._tag === "SignedIn",
193
+ * );
194
+ * ```
195
+ */
196
+ export function routerListens(registry) {
197
+ return (query, signedIn) => {
198
+ const seen = (who) => ({ _tag: 'ViewerSeen', who });
199
+ const viewer = {
200
+ succeeded: (value) => seen(signedIn(value)
201
+ ? Who.SignedIn
202
+ : Who.Anonymous),
203
+ inputKey: undefined,
204
+ };
205
+ return compiledListens(new Map([[queryIdOf(registry, query), viewer]]), (error) => error._tag === 'Unauthorized' ? seen(Who.Anonymous) : undefined);
206
+ };
207
+ }