@spfn/core 0.3.0-beta.3 → 0.3.0-beta.31

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 (104) hide show
  1. package/README.md +304 -12
  2. package/dist/app-config/index.d.ts +39 -1
  3. package/dist/app-config/index.js.map +1 -1
  4. package/dist/authz/index.js +47 -391
  5. package/dist/authz/index.js.map +1 -1
  6. package/dist/{boss-D16fO2oG.d.ts → boss-BiDCbbAQ.d.ts} +60 -7
  7. package/dist/codegen/index.d.ts +157 -16
  8. package/dist/codegen/index.js +772 -166
  9. package/dist/codegen/index.js.map +1 -1
  10. package/dist/config/index.js +4 -4
  11. package/dist/config/index.js.map +1 -1
  12. package/dist/contract/index.d.ts +39 -16
  13. package/dist/contract/index.js +147 -23
  14. package/dist/contract/index.js.map +1 -1
  15. package/dist/db/index.d.ts +230 -35
  16. package/dist/db/index.js +316 -160
  17. package/dist/db/index.js.map +1 -1
  18. package/dist/env/index.d.ts +6 -442
  19. package/dist/env/index.js +13 -16
  20. package/dist/env/index.js.map +1 -1
  21. package/dist/env/loader.js +24 -1
  22. package/dist/env/loader.js.map +1 -1
  23. package/dist/error-CIZlYJlL.d.ts +157 -0
  24. package/dist/errors/index.d.ts +18 -1
  25. package/dist/errors/index.js +47 -391
  26. package/dist/errors/index.js.map +1 -1
  27. package/dist/event/index.d.ts +5 -5
  28. package/dist/event/index.js +202 -3
  29. package/dist/event/index.js.map +1 -1
  30. package/dist/event/sse/client.d.ts +18 -4
  31. package/dist/event/sse/client.js +94 -18
  32. package/dist/event/sse/client.js.map +1 -1
  33. package/dist/event/sse/index.d.ts +18 -5
  34. package/dist/event/sse/index.js +452 -114
  35. package/dist/event/sse/index.js.map +1 -1
  36. package/dist/event/ws/client.d.ts +3 -2
  37. package/dist/event/ws/index.d.ts +4 -3
  38. package/dist/job/index.d.ts +48 -3
  39. package/dist/job/index.js +131 -52
  40. package/dist/job/index.js.map +1 -1
  41. package/dist/logger/index.js +0 -12
  42. package/dist/logger/index.js.map +1 -1
  43. package/dist/middleware/index.js +52 -397
  44. package/dist/middleware/index.js.map +1 -1
  45. package/dist/nextjs/index.d.ts +75 -14
  46. package/dist/nextjs/index.js +200 -84
  47. package/dist/nextjs/index.js.map +1 -1
  48. package/dist/nextjs/server.d.ts +46 -8
  49. package/dist/nextjs/server.js +15 -1
  50. package/dist/nextjs/server.js.map +1 -1
  51. package/dist/ops/index.d.ts +61 -42
  52. package/dist/ops/index.js +648 -97
  53. package/dist/ops/index.js.map +1 -1
  54. package/dist/registry-C2KQCh0m.d.ts +473 -0
  55. package/dist/route/index.d.ts +5 -2
  56. package/dist/route/index.js +107 -28
  57. package/dist/route/index.js.map +1 -1
  58. package/dist/route/types.d.ts +7 -1
  59. package/dist/{router-Cy7rAfmj.d.ts → route-builder-DRW-MSCJ.d.ts} +51 -126
  60. package/dist/router-C57y-vz1.d.ts +184 -0
  61. package/dist/router-CX5uVjmU.d.ts +406 -0
  62. package/dist/server/index.d.ts +130 -7
  63. package/dist/server/index.js +1174 -164
  64. package/dist/server/index.js.map +1 -1
  65. package/dist/state/client.d.ts +206 -0
  66. package/dist/state/client.js +678 -0
  67. package/dist/state/client.js.map +1 -0
  68. package/dist/state/index.d.ts +443 -0
  69. package/dist/state/index.js +644 -0
  70. package/dist/state/index.js.map +1 -0
  71. package/dist/state/react.d.ts +49 -0
  72. package/dist/state/react.js +50 -0
  73. package/dist/state/react.js.map +1 -0
  74. package/dist/surface/index.d.ts +115 -0
  75. package/dist/surface/index.js +1013 -0
  76. package/dist/surface/index.js.map +1 -0
  77. package/dist/token-manager-Ckjt2-qq.d.ts +122 -0
  78. package/dist/{types-CfYVhIQ9.d.ts → types-5Rl11_Nb.d.ts} +43 -4
  79. package/dist/types-CKH0D3Dy.d.ts +441 -0
  80. package/dist/{types-ZQODsBft.d.ts → types-CurMab2Q.d.ts} +52 -2
  81. package/dist/{types-2AbaW4Ie.d.ts → types-DncqXfWN.d.ts} +4 -3
  82. package/docs/file-upload.md +195 -333
  83. package/package.json +41 -11
  84. package/src/cache/README.md +330 -0
  85. package/src/codegen/README.md +721 -0
  86. package/src/config/README.md +333 -0
  87. package/src/contract/README.md +442 -0
  88. package/src/db/README.md +589 -0
  89. package/src/db/manager/README.md +500 -0
  90. package/src/db/schema/README.md +354 -0
  91. package/src/db/transaction/README.md +845 -0
  92. package/src/env/README.md +787 -0
  93. package/src/errors/README.md +445 -0
  94. package/src/event/README.md +994 -0
  95. package/src/job/README.md +613 -0
  96. package/src/logger/README.md +321 -0
  97. package/src/middleware/README.md +634 -0
  98. package/src/nextjs/README.md +756 -0
  99. package/src/route/README.md +763 -0
  100. package/src/security/README.md +100 -0
  101. package/src/server/README.md +704 -0
  102. package/src/state/README.md +806 -0
  103. package/dist/token-manager-BT5EnUAR.d.ts +0 -278
  104. package/dist/types-9oszaJqp.d.ts +0 -250
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/state/client-store.ts","../../src/state/client.ts"],"names":["notify"],"mappings":";;;;;AAiBO,SAAS,UAAU,GAAA,EAC1B;AACI,EAAA,IAAI,CAAC,GAAA,EACL;AACI,IAAA,OAAO,EAAA;AAAA,EACX;AAEA,EAAA,OAAO,KAAK,SAAA,CAAU,MAAA,CAAO,IAAA,CAAK,GAAG,EAAE,IAAA,EAAK,CAAE,GAAA,CAAI,CAAA,KAAA,KAAS,CAAC,KAAA,EAAO,GAAA,CAAI,KAAK,CAAC,CAAC,CAAC,CAAA;AACnF;AASA,SAAS,QAAA,CAAS,GAAY,CAAA,EAC9B;AACI,EAAA,IAAI,MAAM,CAAA,EACV;AACI,IAAA,OAAO,IAAA;AAAA,EACX;AAEA,EAAA,IAAI,OAAO,CAAA,KAAM,QAAA,IAAY,OAAO,CAAA,KAAM,YAAY,CAAA,KAAM,IAAA,IAAQ,CAAA,KAAM,IAAA,IAAQ,MAAM,OAAA,CAAQ,CAAC,MAAM,KAAA,CAAM,OAAA,CAAQ,CAAC,CAAA,EACtH;AACI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A;AAEpC,IAAA,IAAI,IAAA,CAAK,OAAA,CAAQ,GAAA,CAAI,IAAI,CAAA,EACzB;AACI,MAAA,IAAA,CAAK,KAAA,CAAM,IAAA,EAAM,KAAA,EAAO,MAAA,CAAO,UAAU,CAAA;AAAA,IAC7C;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASQ,oBAAA,CAAqB,QAAoC,MAAA,EACjE;AACI,IAAA,MAAM,KAAK,wBAAA,CAAyB,IAAA,CAAK,MAAA,CAAO,KAAe,IAAI,CAAC,CAAA;AAEpE,IAAA,IAAI,OAAO,MAAA,IAAa,CAAC,IAAA,CAAK,QAAA,CAAU,EAAE,CAAA,EAC1C;AACI,MAAA;AAAA,IACJ;AAEA,IAAA,MAAM,GAAA,GAAM,IAAA,CAAK,KAAA,CAAM,EAAA,EAAI,OAAO,KAAK,CAAA;AACvC,IAAA,MAAM,IAAA,GAAO,IAAA,CAAK,IAAA,CAAK,EAAA,EAAI,GAAG,CAAA;AAE9B,IAAA,IAAI,MAAA,CAAO,WAAW,SAAA,EACtB;AACI,MAAA,IAAA,CAAK,KAAA,CAAM,MAAA,CAAO,IAAA,EAAM,MAAA,CAAO,UAAU,CAAA;AAEzC,MAAA;AAAA,IACJ;AAEA,IAAA,IAAA,CAAK,MAAA,CAAO,GAAA,CAAI,IAAA,EAAM,EAAE,KAAK,KAAK,CAAA;AAClC,IAAA,IAAA,CAAK,KAAA,CAAM,MAAM,IAAI,CAAA;AAErB,IAAA,IAAI,IAAA,CAAK,OAAA,CAAQ,GAAA,CAAI,IAAI,CAAA,EACzB;AACI,MAAA,IAAA,CAAK,IAAI,EAAA,EAAI,GAAG,CAAA,CAAE,KAAA,CAAM,KAAK,WAAW,CAAA;AAAA,IAC5C;AAAA,EACJ;AACJ,CAAA","file":"client.js","sourcesContent":["/**\n * State Client Store\n *\n * One frozen entry per `(id, key)` and the ordering rule that decides whether\n * an arrival — a snapshot entry, a frame, a read or a write answer — replaces\n * it. An entry is replaced only when its status, value or data changes, so\n * `current()` returns the same object until then: `useSyncExternalStore`\n * compares by reference and would re-render forever on a fresh object per\n * call. A newer `at` alone advances the slot's ordering stamp, not the entry.\n */\n\nimport type { StateEntry, StateFrame, StateListener } from './client-types';\n\n/** Key fields, flat strings by construction (`.key()` refuses other fields). */\nexport type StateKeyFields = Record<string, string>;\n\n/** A key as text with its fields sorted, so equal keys written in any order name one entry. */\nexport function stableKey(key: StateKeyFields | undefined): string\n{\n if (!key)\n {\n return '';\n }\n\n return JSON.stringify(Object.keys(key).sort().map(field => [field, key[field]]));\n}\n\n/** The inverse of `stableKey`. */\nexport function parseKey(text: string): StateKeyFields | undefined\n{\n return text ? Object.fromEntries(JSON.parse(text) as [string, string][]) : undefined;\n}\n\n/** Deep equality of JSON values, which is all a frame carries. */\nfunction sameJson(a: unknown, b: unknown): boolean\n{\n if (a === b)\n {\n return true;\n }\n\n if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null || Array.isArray(a) !== Array.isArray(b))\n {\n return false;\n }\n\n const aKeys = Object.keys(a);\n const bRecord = b as Record<string, unknown>;\n\n return aKeys.length === Object.keys(b).length\n && aKeys.every(field => field in bRecord && sameJson((a as Record<string, unknown>)[field], bRecord[field]));\n}\n\n/**\n * The ordering rule for the value: an arrival older than the slot's stamp —\n * the newest `at` seen — is dropped. An equal `at` is by design the same\n * value (one read stamps one `at`), so it is not older and `changes` decides.\n */\nfunction isOlder(stamp: string | undefined, arrival: StateFrame): boolean\n{\n return stamp !== undefined && arrival.at < stamp;\n}\n\n/**\n * Whether an arrival says something the entry does not. An entry marked\n * revoked before any value arrived has no `at`: anything changes it.\n */\nfunction changes(current: StateEntry | undefined, arrival: StateFrame): boolean\n{\n return current?.at === undefined || !sameJson(current.value, arrival.value) || !sameJson(current.data, arrival.data);\n}\n\nfunction entryOf(frame: StateFrame, status: StateStatus): StateEntry\n{\n const { at, value, data } = frame;\n\n return Object.freeze(data === undefined ? { at, value, status } : { at, value, data, status });\n}\n\ntype StateStatus = StateEntry['status'];\n\nexport class StateStore\n{\n private readonly entries = new Map<string, StateEntry>();\n\n /**\n * The newest `at` seen per slot, kept apart from the entry: an arrival\n * that changes nothing still orders the ones after it, while the entry's\n * `at` stays the `at` of the read that last changed the value.\n */\n private readonly stamps = new Map<string, string>();\n\n private readonly listeners = new Map<string, Set<StateListener>>();\n\n /**\n * The slots the server stopped serving, with the generation of the\n * connection that said so. Kept apart from the entries: a revoke can\n * arrive before the slot has a value, and an arrival afterwards supplies\n * the value without clearing the mark.\n */\n private readonly revoked = new Map<string, number>();\n\n /** The store slot of `(id, key)`. */\n static slot(id: string, keyText: string): string\n {\n return `${id}\\u0000${keyText}`;\n }\n\n current(slot: string): StateEntry | undefined\n {\n return this.entries.get(slot);\n }\n\n /**\n * Apply an arrival: the value under the ordering rule, the status from the\n * revoked mark. Listeners hear only a changed status, value or data.\n */\n apply(slot: string, frame: StateFrame): void\n {\n const current = this.entries.get(slot);\n const status: StateStatus = this.revoked.has(slot) ? 'revoked' : 'ready';\n const ordered = !isOlder(this.stamps.get(slot), frame);\n\n if (ordered)\n {\n this.stamps.set(slot, frame.at);\n }\n\n if (ordered && changes(current, frame))\n {\n this.replace(slot, entryOf(frame, status));\n }\n else if (current!.status !== status)\n {\n // Not applied, so the current entry has a value: only its status changes.\n this.replace(slot, entryOf(current as StateFrame, status));\n }\n }\n\n /** Mark `slot` revoked by the connection of `generation`; its value, if any, is kept. */\n revoke(slot: string, generation: number): void\n {\n this.revoked.set(slot, generation);\n const entry = this.entries.get(slot);\n\n if (entry?.status !== 'revoked')\n {\n this.replace(slot, Object.freeze(entry ? { ...entry, status: 'revoked' as const } : { status: 'revoked' as const }));\n }\n }\n\n /** The generation of the connection that revoked `slot`, or `undefined` while it is served. */\n revokedBy(slot: string): number | undefined\n {\n return this.revoked.get(slot);\n }\n\n /** Clear the revoked mark; the next arrival for `slot` is `'ready'`. */\n allow(slot: string): void\n {\n this.revoked.delete(slot);\n }\n\n listen(slot: string, listener: StateListener): void\n {\n const set = this.listeners.get(slot) ?? new Set();\n set.add(listener);\n this.listeners.set(slot, set);\n }\n\n unlisten(slot: string, listener?: StateListener): void\n {\n const set = this.listeners.get(slot);\n\n if (listener)\n {\n set?.delete(listener);\n }\n\n if (!listener || set?.size === 0)\n {\n this.listeners.delete(slot);\n }\n }\n\n clearListeners(): void\n {\n this.listeners.clear();\n }\n\n /** Replace the entry and notify; a throwing listener is logged and does not stop the others. */\n private replace(slot: string, entry: StateEntry): void\n {\n this.entries.set(slot, entry);\n\n for (const listener of [...this.listeners.get(slot) ?? []])\n {\n notify(slot, listener);\n }\n }\n}\n\nfunction notify(slot: string, listener: StateListener): void\n{\n try\n {\n listener();\n }\n catch (error)\n {\n // The state id only: the key is the app's data.\n console.warn(`[state] A listener of state \"${slot.split('\\u0000')[0]}\" threw:`, error);\n }\n}\n","/**\n * State Client\n *\n * The browser side of a state router (`@spfn/core/state`): it calls the derived\n * read, write and snapshot routes, keeps one SSE stream open for every state of\n * the router, and orders snapshots against frames per `(id, key)`. It knows the\n * router by type — `typeof states` — and at runtime by the manifest every\n * snapshot carries; it imports no server code.\n *\n * @example\n * ```typescript\n * import { createStateClient } from '@spfn/core/state/client';\n * import { createAuthSSEClient } from '@spfn/core/event/sse/client';\n * import type { states } from '@/server/states';\n *\n * export const state = createStateClient<typeof states>({ sse: createAuthSSEClient() });\n *\n * const stop = state.subscribe('runtime', { workspaceId }, () => render(state.current('runtime', { workspaceId })));\n * ```\n */\n\nimport type { ConnectedPayload, SSESubscriptionChange } from '@spfn/core/event/sse';\nimport { createApi } from '@spfn/core/nextjs';\nimport { stableKey, StateStore, type StateKeyFields } from './client-store';\nimport type {\n AnyStateRouter,\n ConnectionListener,\n ConnectionStatus,\n StateClient,\n StateClientConfig,\n StateClientInitial,\n StateEntry,\n StateFrame,\n StateListener,\n} from './client-types';\n\nexport type {\n AnyStateRouter,\n ConnectionListener,\n ConnectionStatus,\n InferStateData,\n StateCatalog,\n StateFrameOf,\n InferStateInput,\n InferStateKey,\n InferStateValue,\n StateClient,\n StateClientConfig,\n StateClientInitial,\n StateEntry,\n StateFrame,\n StateId,\n StateKeyArgs,\n StateListener,\n WritableStateId,\n} from './client-types';\n\n/** A frame as the SSE event carries it. */\ntype WireFrame = StateFrame & { key?: StateKeyFields; subject?: string };\n\n/** Every state of the router, in declaration order: its key fields and whether it has a write. */\ntype Manifest = Record<string, { key: string[]; write: boolean }>;\n\ninterface Snapshot\n{\n at: string;\n states: Record<string, { value: unknown; data?: unknown }>;\n manifest: Manifest;\n}\n\ntype RouteCalls = Record<string, { call(input: object): Promise<unknown> }>;\n\n/** One `(id, key)` someone watches, and how many subscriptions hold it. */\ninterface Watch\n{\n id: string;\n key: StateKeyFields | undefined;\n count: number;\n}\n\n/**\n * The stream as one `sse.subscribe` opened it; `generation` names its current\n * connection. `closedByClient`: the client closed it itself, so its `onClose`\n * is no failure; `failed`: the SSE client gave up on it.\n */\ninterface Stream\n{\n query: StateKeyFields;\n text: string;\n generation: number;\n closedByClient: boolean;\n failed: boolean;\n close: () => void;\n}\n\n/** When an automatic read was started: the connection generation and the client's clock. */\ninterface ReadTag\n{\n generation: number;\n tick: number;\n}\n\nconst CONNECTED: ConnectionStatus = Object.freeze({ status: 'connected' });\nconst FAILED: ConnectionStatus = Object.freeze({ status: 'failed' });\nconst STARTING: ConnectionStatus = Object.freeze({ status: 'reconnecting', attempt: 0 });\n\nfunction reconnecting(attempt: number): ConnectionStatus\n{\n return attempt === 0 ? STARTING : Object.freeze({ status: 'reconnecting', attempt });\n}\n\nfunction sameStatus(a: ConnectionStatus, b: ConnectionStatus): boolean\n{\n return a.status === b.status && (a as { attempt?: number }).attempt === (b as { attempt?: number }).attempt;\n}\n\n/** `get<Id>State` / `put<Id>State`, the names the server router derives. */\nfunction routeName(method: 'get' | 'put', id: string): string\n{\n return `${method}${id[0].toUpperCase()}${id.slice(1)}State`;\n}\n\n/** The wire name of a state's SSE event, as the server derives it. */\nfunction stateEventName(id: string): string\n{\n return `state.${id}.changed`;\n}\n\n/** Whether `query` carries every field of `key` with its value. */\nfunction carries(query: StateKeyFields, key: StateKeyFields): boolean\n{\n return Object.entries(key).every(([field, value]) => query[field] === value);\n}\n\nfunction sortedFields(fields: string[]): string\n{\n return [...fields].sort().join(', ');\n}\n\n/**\n * Build the client for `typeof states`. The first subscription (or call)\n * reads the snapshot, whose manifest names every state — or takes it from\n * `initial`, the snapshot the server rendered with; the stream then\n * requests every state event — the server's `grant` can only activate an\n * event the connection requested. The router type parameter is what types\n * `initial.snapshot`; without it the client is untyped, as before.\n */\nexport function createStateClient<TStates extends AnyStateRouter>(config: StateClientConfig<TStates>): StateClient<TStates>\n{\n return new BrowserStateClient(config) as unknown as StateClient<TStates>;\n}\n\nclass BrowserStateClient\n{\n private readonly store = new StateStore();\n private readonly api: RouteCalls;\n private readonly watched = new Map<string, Watch>();\n private readonly reportError: (error: unknown) => void;\n private manifest: Manifest | undefined;\n private manifestRead: Promise<void> | undefined;\n private stream: Stream | undefined;\n private syncQueued = false;\n\n private status = STARTING;\n private readonly connectionListeners = new Set<ConnectionListener>();\n private readonly onOnline = (): void => this.retry();\n\n /** The retries of the first snapshot since its last start, against `maxRetries`. */\n private retries = 0;\n\n /** The lifetime whose subscriptions wait on the first snapshot — its read in flight, or its retry timer set. */\n private waitingLifetime: number | undefined;\n private retryTimer: ReturnType<typeof setTimeout> | undefined;\n\n /** Bumped by `close()`: a snapshot started before it is discarded. */\n private lifetime = 0;\n\n /** Bumped on every connection open; tags the snapshot read for it. */\n private generation = 0;\n\n /** Bumped on every automatic read and every grant, to tell which came first. */\n private clock = 0;\n\n /** The clock at the latest grant of each slot. */\n private readonly grants = new Map<string, number>();\n\n constructor(private readonly config: StateClientConfig)\n {\n this.api = createApi<any>(config.api) as unknown as RouteCalls;\n this.reportError = config.onError ?? (error => console.error('[state] A refresh failed:', error));\n\n if (config.initial)\n {\n this.hydrate(config.initial);\n }\n\n // Absent in Node and in a server render.\n if (typeof globalThis.addEventListener === 'function')\n {\n globalThis.addEventListener('online', this.onOnline);\n }\n }\n\n async get(id: string, key?: StateKeyFields): Promise<StateFrame>\n {\n await this.requireManifest();\n const frame = await this.api[routeName('get', id)].call(key ? { query: key } : {}) as StateFrame;\n this.store.apply(this.slot(id, key), frame);\n\n return frame;\n }\n\n async write(id: string, input: unknown, key?: StateKeyFields): Promise<StateFrame>\n {\n if (!(await this.requireManifest())[id]?.write)\n {\n throw new Error(`State client: state \"${id}\" declares no .write(), so it has no write route.`);\n }\n\n const frame = await this.api[routeName('put', id)].call({ body: input, ...(key ? { query: key } : {}) }) as StateFrame;\n this.store.apply(this.slot(id, key), frame);\n\n return frame;\n }\n\n snapshot(query: StateKeyFields = this.streamQuery()): Promise<Snapshot>\n {\n return this.readSnapshot(query, this.tag());\n }\n\n subscribe(id: string, ...args: [StateListener] | [StateKeyFields, StateListener]): () => void\n {\n const listener = args[args.length - 1] as StateListener;\n const key = args.length === 2 ? args[0] as StateKeyFields : undefined;\n const refusal = this.manifest && this.refusal(id, key);\n\n if (refusal)\n {\n this.reportError(refusal);\n\n return () => undefined;\n }\n\n // One wrapper per subscription: the same listener subscribed twice is two subscriptions.\n const notify = () => listener();\n const slot = this.slot(id, key);\n this.store.listen(slot, notify);\n this.watch(slot, id, key, 1);\n let live = true;\n\n return () =>\n {\n if (live)\n {\n live = false;\n this.store.unlisten(slot, notify);\n this.watch(slot, id, key, -1);\n }\n };\n }\n\n current(id: string, key?: StateKeyFields): StateEntry | undefined\n {\n return this.store.current(this.slot(id, key));\n }\n\n connection(): ConnectionStatus\n {\n return this.status;\n }\n\n subscribeConnection(listener: ConnectionListener): () => void\n {\n // One wrapper per subscription: the same listener subscribed twice is two subscriptions.\n const notify: ConnectionListener = status => listener(status);\n this.connectionListeners.add(notify);\n\n return () =>\n {\n this.connectionListeners.delete(notify);\n };\n }\n\n /**\n * Restart what gave up or is still retrying, now: the first snapshot\n * while the manifest is unknown, else the stream — a new `sse.subscribe`,\n * whose attempt count starts at 0. Nothing to do when connected or with\n * nothing watched — so after `close()`, until a new `subscribe`.\n */\n retry(): void\n {\n if (this.status === CONNECTED || this.watched.size === 0)\n {\n return;\n }\n\n if (this.manifest)\n {\n this.openStream(this.streamQuery());\n }\n else\n {\n this.rereadManifest();\n }\n }\n\n close(): void\n {\n this.lifetime++;\n clearTimeout(this.retryTimer);\n this.retryTimer = undefined;\n this.retries = 0;\n this.closeStream();\n this.watched.clear();\n this.store.clearListeners();\n this.connectionListeners.clear();\n this.status = STARTING;\n\n if (typeof globalThis.removeEventListener === 'function')\n {\n globalThis.removeEventListener('online', this.onOnline);\n }\n }\n\n /** The status is `next` from now; each connection listener hears it, a throwing one logged. */\n private setConnection(next: ConnectionStatus): void\n {\n if (sameStatus(next, this.status))\n {\n return;\n }\n\n this.status = next;\n\n for (const listener of [...this.connectionListeners])\n {\n try\n {\n listener(next);\n }\n catch (error)\n {\n console.warn('[state] A connection listener threw:', error);\n }\n }\n }\n\n /**\n * Start from the snapshot the server rendered with: its manifest, then\n * each state present, as a read snapshot applies them, under generation 0\n * — no connection yet. Nothing is marked `'revoked'`: nothing is watched,\n * and an omission in the render's snapshot is not a denial by the stream.\n */\n private hydrate(initial: StateClientInitial): void\n {\n const snapshot = initial.snapshot as Snapshot;\n this.installManifest(snapshot.manifest);\n this.applyPresent(snapshot, initial.query ?? {}, this.tag());\n }\n\n private slot(id: string, key: StateKeyFields | undefined): string\n {\n return StateStore.slot(id, stableKey(key));\n }\n\n private tag(): ReadTag\n {\n return { generation: this.generation, tick: ++this.clock };\n }\n\n /** The key of state `id` the query names; `undefined` for a keyless state. */\n private keyIn(id: string, query: StateKeyFields): StateKeyFields | undefined\n {\n const fields = this.manifest![id].key;\n\n return fields.length ? Object.fromEntries(fields.map(field => [field, query[field]])) : undefined;\n }\n\n /** Why the manifest refuses `subscribe(id, key)` — an unknown id, or key fields not the state's. */\n private refusal(id: string, key: StateKeyFields | undefined): Error | undefined\n {\n const state = this.manifest![id];\n\n if (!state)\n {\n return new Error(`State client: the server's manifest has no state \"${id}\".`);\n }\n\n const expected = sortedFields(state.key);\n const given = sortedFields(Object.keys(key ?? {}));\n\n return expected === given\n ? undefined\n : new Error(`State client: state \"${id}\" takes the key fields [${expected}], given [${given}].`);\n }\n\n /** The first snapshot, read once however many callers wait for it. */\n private ensureManifest(): Promise<void>\n {\n if (this.manifest)\n {\n return Promise.resolve();\n }\n\n this.manifestRead ??= this.snapshot()\n .then(() => undefined)\n .finally(() =>\n {\n this.manifestRead = undefined;\n });\n\n return this.manifestRead;\n }\n\n /** The manifest for a call the app made; one whose wait outlived `close()` is refused. */\n private async requireManifest(): Promise<Manifest>\n {\n await this.ensureManifest();\n\n if (!this.manifest)\n {\n throw new Error('State client: the client was closed while the first snapshot was in flight.');\n }\n\n return this.manifest;\n }\n\n /**\n * Read the snapshot and apply it: the manifest first — `keyIn` and the\n * key checks need it — then each state present, then each watched state\n * it omits. A snapshot that resolves after `close()` is discarded.\n */\n private async readSnapshot(query: StateKeyFields, tag: ReadTag): Promise<Snapshot>\n {\n const lifetime = this.lifetime;\n const snapshot = await this.api.getStates.call({ query }) as Snapshot;\n\n if (lifetime === this.lifetime)\n {\n this.installManifest(snapshot.manifest);\n this.applyPresent(snapshot, query, tag);\n this.applyOmitted(snapshot, query, tag);\n }\n\n return snapshot;\n }\n\n /**\n * The manifest is static; the first one stays. Subscriptions made before\n * it are checked now, and the stream opens for those it accepts —\n * whichever call (`subscribe`, `get`, `write`, `snapshot`) brought it.\n */\n private installManifest(manifest: Manifest): void\n {\n if (this.manifest)\n {\n return;\n }\n\n this.manifest = manifest;\n\n for (const [slot, watch] of this.watched)\n {\n const refusal = this.refusal(watch.id, watch.key);\n\n if (refusal)\n {\n this.watched.delete(slot);\n this.store.unlisten(slot);\n this.reportError(refusal);\n }\n }\n\n if (this.watched.size > 0)\n {\n this.queueSync();\n }\n }\n\n /**\n * An arrival from a read started for the connection of `tag`: a\n * connection newer than the one that revoked the slot lifts the mark, an\n * older or the same connection leaves `'revoked'`. The lift is by\n * generation alone — a frame or a write answer of the new connection may\n * land first with a newer `at` — and the ordering rule decides only the value.\n */\n private serve(slot: string, frame: StateFrame, generation: number): void\n {\n if ((this.store.revokedBy(slot) ?? Infinity) < generation)\n {\n this.store.allow(slot);\n }\n\n this.store.apply(slot, frame);\n }\n\n /**\n * A state present is served, under the generation rule of `serve`. A state\n * the manifest does not describe, or a keyed one whose key fields the query\n * lacks, is skipped: the server never serves either, but a hand-built\n * `initial` might.\n */\n private applyPresent(snapshot: Snapshot, query: StateKeyFields, tag: ReadTag): void\n {\n for (const [id, result] of Object.entries(snapshot.states))\n {\n if (this.manifest![id]?.key.every(field => field in query))\n {\n this.serve(this.slot(id, this.keyIn(id, query)), { at: snapshot.at, ...result }, tag.generation);\n }\n }\n }\n\n /**\n * A watched state whose key the query carried (or a keyless one) and that\n * the snapshot omits is not served to this subject: `'revoked'`. Not when\n * a newer connection exists, nor when a grant came after the read started.\n */\n private applyOmitted(snapshot: Snapshot, query: StateKeyFields, tag: ReadTag): void\n {\n if (tag.generation !== this.generation)\n {\n return;\n }\n\n for (const [slot, watch] of this.watched)\n {\n const asked = !watch.key || carries(query, watch.key);\n const grantedSince = (this.grants.get(slot) ?? 0) > tag.tick;\n\n if (asked && !(watch.id in snapshot.states) && !grantedSince)\n {\n this.store.revoke(slot, tag.generation);\n }\n }\n }\n\n private watch(slot: string, id: string, key: StateKeyFields | undefined, delta: 1 | -1): void\n {\n const count = (this.watched.get(slot)?.count ?? 0) + delta;\n\n if (count > 0)\n {\n this.watched.set(slot, { id, key, count });\n }\n else\n {\n this.watched.delete(slot);\n }\n\n // A removal that leaves subscribers keeps a failed client failed; the last removal closes and resets.\n if (delta === 1 || this.watched.size === 0 || this.status !== FAILED)\n {\n this.queueSync();\n }\n }\n\n /**\n * One stream for the client, its query the union of the subscribed keys.\n * Batched to the end of the task, so components mounting together (or a\n * StrictMode unmount-remount) open the stream once.\n */\n private queueSync(): void\n {\n if (!this.syncQueued)\n {\n this.syncQueued = true;\n queueMicrotask(() =>\n {\n this.syncQueued = false;\n this.sync();\n });\n }\n }\n\n /**\n * Until the manifest is known, subscriptions wait for the first snapshot;\n * the stream opens after it, never before — its `events=` comes from it.\n * A client built with `initial` knows it from the start. With nothing\n * watched the stream closes, the retry count starts over for the next\n * wait, and the status is the start value: no stream, nothing answered.\n */\n private sync(): void\n {\n if (this.watched.size === 0)\n {\n this.retries = 0;\n this.closeStream();\n this.setConnection(STARTING);\n\n return;\n }\n\n if (!this.manifest)\n {\n this.awaitManifest();\n\n return;\n }\n\n // A stream the SSE client gave up on counts as absent.\n const query = this.streamQuery();\n\n if (this.stream?.failed || this.stream?.text !== stableKey(query))\n {\n this.openStream(query);\n }\n }\n\n /**\n * One waiter per lifetime on the first snapshot. A failure goes to\n * `onError`, and the read is retried after `retryDelay` — the stream that\n * would reconnect on its own does not exist yet. A retry stops on\n * `close()`, when nothing is watched (`sync` closes instead), or after\n * `maxRetries`; a wait started after that last one restarts the count.\n */\n private awaitManifest(): void\n {\n const lifetime = this.lifetime;\n\n if (this.waitingLifetime === lifetime)\n {\n return;\n }\n\n if (this.status === FAILED)\n {\n this.retries = 0;\n this.setConnection(STARTING);\n }\n\n this.waitingLifetime = lifetime;\n this.ensureManifest().then(\n () => lifetime === this.lifetime && this.resync(),\n error =>\n {\n this.reportError(error);\n\n if (lifetime === this.lifetime)\n {\n this.retryLater();\n }\n },\n );\n }\n\n /** The wait is over; `sync` opens the stream, or waits again when a closed lifetime's read was discarded. */\n private resync(): void\n {\n this.waitingLifetime = undefined;\n this.sync();\n }\n\n /**\n * Read the first snapshot again after `retryDelay`, as the SSE client\n * reconnects a dropped stream — or, with `maxRetries` spent, give up:\n * `'failed'`, no timer, no wait.\n */\n private retryLater(): void\n {\n const maxRetries = this.config.maxRetries ?? 0;\n\n if (maxRetries > 0 && this.retries >= maxRetries)\n {\n this.waitingLifetime = undefined;\n this.setConnection(FAILED);\n\n return;\n }\n\n this.retries++;\n this.retryTimer = setTimeout(() =>\n {\n this.retryTimer = undefined;\n this.resync();\n }, this.config.retryDelay ?? 3000);\n // A timer in Node has `unref`: a pending retry does not keep the process alive.\n (this.retryTimer as { unref?: () => void }).unref?.();\n }\n\n /**\n * `retry()` before the manifest: the count starts over and the first\n * snapshot is read now, its pending timer cleared. A read in flight is\n * already the retry; it is not doubled.\n */\n private rereadManifest(): void\n {\n this.retries = 0;\n\n if (this.waitingLifetime === this.lifetime && this.retryTimer === undefined)\n {\n return;\n }\n\n clearTimeout(this.retryTimer);\n this.retryTimer = undefined;\n this.setConnection(STARTING);\n this.resync();\n }\n\n /**\n * Every subscribed key's fields; one connection carries one value per\n * field, so on a conflict the latest subscribed key wins. The keys it\n * shadows are read once when the stream opens and get no frames.\n */\n private streamQuery(): StateKeyFields\n {\n return Object.assign({}, ...[...this.watched.values()].map(watch => watch.key ?? {}));\n }\n\n /**\n * Open (or reopen) the stream. `events` is every state of the manifest, in\n * its order, never only the subscribed ones: a state subscribed later would\n * otherwise be unrequested, and the server's `grant` cannot activate it.\n * Unless connected, the status is `'reconnecting'` 0 until it opens: a\n * new subscription counts its attempts from 0. A reopen on a key change\n * while connected stays `'connected'`.\n */\n private openStream(query: StateKeyFields): void\n {\n this.closeStream();\n\n if (this.status !== CONNECTED)\n {\n this.setConnection(STARTING);\n }\n\n const ids = Object.keys(this.manifest!);\n const stream: Stream = { query, text: stableKey(query), generation: 0, closedByClient: false, failed: false, close: () => undefined };\n stream.close = this.config.sse.subscribe({\n events: ids.map(stateEventName),\n handlers: Object.fromEntries(ids.map(id => [stateEventName(id), (frame: WireFrame) => this.onFrame(id, frame, stream)])),\n query,\n onOpen: () =>\n {\n stream.generation = ++this.generation;\n this.streamStatus(stream, CONNECTED);\n },\n onReconnect: attempt => this.streamStatus(stream, reconnecting(attempt)),\n onClose: () => this.onStreamClose(stream),\n onConnected: connected => this.onConnected(connected, stream),\n onSubscriptionChange: change => this.onSubscriptionChange(change, stream),\n });\n\n this.stream = stream;\n }\n\n /** Flagged first: the SSE client fires `onClose` on this teardown too, and it is no failure. */\n private closeStream(): void\n {\n if (this.stream)\n {\n this.stream.closedByClient = true;\n this.stream.close();\n this.stream = undefined;\n }\n }\n\n /** The status of a callback of `stream`, when no other stream replaced it. */\n private streamStatus(stream: Stream, status: ConnectionStatus): void\n {\n if (stream === this.stream)\n {\n this.setConnection(status);\n }\n }\n\n /**\n * The SSE client fires `onClose` on every teardown; one the client did\n * not start is the reconnect attempts exhausted: `'failed'`, and the\n * stream counts as absent for the next `sync`.\n */\n private onStreamClose(stream: Stream): void\n {\n if (!stream.closedByClient && stream === this.stream)\n {\n stream.failed = true;\n this.setConnection(FAILED);\n }\n }\n\n /**\n * Every connection — the first, a reconnect, a reopen on a key change —\n * must cover the frames sent while no stream was open. The server says in\n * `connected` which states it served their current frame right after it;\n * the snapshot is read only when one it did not serve is neither denied\n * nor served — or when the server predates `initial`. A denied state the\n * client watches is `'revoked'`, as an omission from that snapshot would\n * be. A connection already replaced is ignored.\n */\n private onConnected(connected: ConnectedPayload, stream: Stream): void\n {\n if (stream !== this.stream || stream.generation !== this.generation)\n {\n return;\n }\n\n const denied = new Set(connected.deniedEvents);\n const served = new Set(connected.initial ?? []);\n const unserved = Object.keys(this.manifest!).map(stateEventName).filter(name => !denied.has(name) && !served.has(name));\n\n this.revokeDenied(denied, stream);\n\n if (connected.initial && unserved.length === 0)\n {\n this.readShadowedKeys(stream.query);\n }\n else\n {\n this.refresh(stream.query);\n }\n }\n\n /** Each watched state the connection denies, on the key it carries, marked `'revoked'` by it. */\n private revokeDenied(denied: Set<string>, stream: Stream): void\n {\n for (const id of Object.keys(this.manifest!))\n {\n const slot = this.slot(id, this.keyIn(id, stream.query));\n\n if (denied.has(stateEventName(id)) && this.watched.has(slot))\n {\n this.store.revoke(slot, stream.generation);\n }\n }\n }\n\n /** The snapshot for the stream's query, and one read for each subscribed key the query shadows. */\n private refresh(query: StateKeyFields): void\n {\n this.readSnapshot(query, this.tag()).catch(this.reportError);\n this.readShadowedKeys(query);\n }\n\n /** One read for each subscribed key the query shadows: the stream serves one key per field. */\n private readShadowedKeys(query: StateKeyFields): void\n {\n for (const watch of this.watched.values())\n {\n if (watch.key && !carries(query, watch.key))\n {\n this.readShadowed(watch.id, watch.key, this.tag()).catch(this.reportError);\n }\n }\n }\n\n /**\n * A shadowed key gets no frames, no grant and no snapshot entry; the\n * server answering its own read is the evidence of access instead.\n */\n private async readShadowed(id: string, key: StateKeyFields, tag: ReadTag): Promise<void>\n {\n const frame = await this.api[routeName('get', id)].call({ query: key }) as StateFrame;\n this.serve(this.slot(id, key), frame, tag.generation);\n }\n\n /**\n * A frame for a key nobody here subscribed is dropped — the server's filter\n * is the first guard, this the second. A frame is served under its\n * connection's generation: the server sends one only on a connection that\n * allows the state, so a newer connection's frame lifts an older revoke.\n */\n private onFrame(id: string, frame: WireFrame, stream: Stream): void\n {\n const slot = this.slot(id, frame.key);\n\n if (this.watched.has(slot))\n {\n this.serve(slot, frame, stream.generation);\n }\n }\n\n /**\n * A revoke or a grant concerns the key the connection carries — the\n * server scopes both by the connect query — and so does the read after a\n * grant. Revoked: the entry keeps its value, marked `'revoked'`, until a\n * grant. Granted: the mark is cleared and the key read once; the server\n * sends a frame too, in either order, and both arrive `'ready'`.\n */\n private onSubscriptionChange(change: SSESubscriptionChange<any>, stream: Stream): void\n {\n const id = /^state\\.(.+)\\.changed$/.exec(change.event as string)?.[1];\n\n if (id === undefined || !this.manifest![id])\n {\n return;\n }\n\n const key = this.keyIn(id, stream.query);\n const slot = this.slot(id, key);\n\n if (change.status === 'revoked')\n {\n this.store.revoke(slot, stream.generation);\n\n return;\n }\n\n this.grants.set(slot, ++this.clock);\n this.store.allow(slot);\n\n if (this.watched.has(slot))\n {\n this.get(id, key).catch(this.reportError);\n }\n }\n}\n"]}
@@ -0,0 +1,443 @@
1
+ import { TObject, TSchema, Static } from '@sinclair/typebox';
2
+ import { MiddlewareHandler, Context } from 'hono';
3
+ import { EventDef } from '@spfn/core/event';
4
+ import { NamedMiddleware, RouteAuthProfile, Router, RouteDef } from '@spfn/core/route';
5
+
6
+ /**
7
+ * State Types
8
+ *
9
+ * A state's shape is tracked as one phantom record, `StateShape`, which each
10
+ * builder call rewrites. Every derived type — the read context, the result,
11
+ * the change signal's payload, the frame, the routes — is computed from it.
12
+ */
13
+
14
+ /**
15
+ * What a builder has declared so far, at the type level only.
16
+ */
17
+ interface StateShape {
18
+ key: TObject | undefined;
19
+ shared: boolean;
20
+ value: TSchema | undefined;
21
+ data: TSchema | undefined;
22
+ input: TSchema | undefined;
23
+ read: boolean;
24
+ /** Who the callbacks' `principal` is, from `.principal<P>()`; `unknown` until declared. */
25
+ principal: unknown;
26
+ }
27
+ /** The shape of `defineState(id)` before any call. */
28
+ interface EmptyStateShape extends StateShape {
29
+ key: undefined;
30
+ shared: false;
31
+ value: undefined;
32
+ data: undefined;
33
+ input: undefined;
34
+ read: false;
35
+ principal: unknown;
36
+ }
37
+ /** `T` with the fields of `U` replaced. */
38
+ type WithShape<T extends StateShape, U extends Partial<StateShape>> = {
39
+ [K in keyof StateShape]: K extends keyof U ? U[K] : T[K];
40
+ };
41
+ type Simplify<T> = {
42
+ [K in keyof T]: T[K];
43
+ } & {};
44
+ /** The key object of a keyed state, `undefined` for a keyless one. */
45
+ type StateKey<T extends StateShape> = T['key'] extends TObject ? Static<T['key']> : undefined;
46
+ /** The value type, `unknown` until `.value()` is declared. */
47
+ type StateValue<T extends StateShape> = T['value'] extends TSchema ? Static<T['value']> : unknown;
48
+ /**
49
+ * What `read()` returns: always an object, so a value can never be mistaken
50
+ * for `{ value, data }`. With `.data()` declared, `data` is part of every
51
+ * result — the router checks each one against the declaration.
52
+ */
53
+ type StateResult<T extends StateShape> = T['data'] extends TSchema ? {
54
+ value: StateValue<T>;
55
+ data: Static<T['data']>;
56
+ } : {
57
+ value: StateValue<T>;
58
+ };
59
+ /** What the read and write routes answer: the result and the time its read started. */
60
+ type StateSnapshot<T extends StateShape> = Simplify<{
61
+ at: string;
62
+ } & StateResult<T>>;
63
+ /**
64
+ * What `.canRead()` decides on. `principal` may be `undefined`: at SSE connect
65
+ * it is what `resolvePrincipal` returned.
66
+ */
67
+ type PermissionContext<T extends StateShape> = T['key'] extends TObject ? {
68
+ subject: string;
69
+ principal: T['principal'] | undefined;
70
+ key: Static<T['key']>;
71
+ } : {
72
+ subject: string;
73
+ principal: T['principal'] | undefined;
74
+ };
75
+ /**
76
+ * What `.canWrite()` decides on. Only the write route asks it, and with
77
+ * `.principal()` declared that route refuses a request whose `auth` is
78
+ * `undefined` or `null` (401).
79
+ */
80
+ type WritePermissionContext<T extends StateShape> = Simplify<Omit<PermissionContext<T>, 'principal'> & {
81
+ principal: T['principal'];
82
+ }>;
83
+ /**
84
+ * What `read()` receives. A shared state's value does not depend on the
85
+ * subject, so it gets none. `principal` may be `undefined`: the change
86
+ * signal's re-read has no request.
87
+ */
88
+ type ReadContext<T extends StateShape> = T['key'] extends TObject ? T['shared'] extends true ? {
89
+ principal: T['principal'] | undefined;
90
+ key: Static<T['key']>;
91
+ } : PermissionContext<T> : PermissionContext<T>;
92
+ /** What `write()` receives: the read context, the write route's principal and the validated input. */
93
+ type WriteContext<T extends StateShape, I extends TSchema> = Simplify<Omit<ReadContext<T>, 'principal'> & {
94
+ principal: T['principal'];
95
+ input: Static<I>;
96
+ }>;
97
+ /** The payload of `state.changed.emit()`: exactly what names the value to re-read. */
98
+ type ChangedPayload<T extends StateShape> = T['key'] extends TObject ? T['shared'] extends true ? Simplify<Static<T['key']>> : Simplify<Static<T['key']> & {
99
+ subject: string;
100
+ }> : {
101
+ subject: string;
102
+ };
103
+ /** The payload of the derived SSE event `state.<id>.changed`. */
104
+ type StateFrame<T extends StateShape> = Simplify<StateSnapshot<T> & (T['key'] extends TObject ? {
105
+ key: Static<T['key']>;
106
+ } : {}) & (T['key'] extends TObject ? (T['shared'] extends true ? {} : {
107
+ subject: string;
108
+ }) : {
109
+ subject: string;
110
+ })>;
111
+ /** Whom `grant()` reaches, and for a keyed state the one key it opens and sends the current value of. */
112
+ type GrantTarget<T extends StateShape> = T['key'] extends TObject ? {
113
+ subject: string;
114
+ key: Static<T['key']>;
115
+ } : {
116
+ subject: string;
117
+ };
118
+ /** Whom `revoke()` reaches: for a keyed state, the connections on `key`, or every one without it. */
119
+ type RevokeTarget<T extends StateShape> = T['key'] extends TObject ? {
120
+ subject: string;
121
+ key?: Static<T['key']>;
122
+ } : {
123
+ subject: string;
124
+ };
125
+ /**
126
+ * The contract one `.contract()` declares for the read route, the write route
127
+ * and the derived event. The response schema is derived, never declared.
128
+ */
129
+ interface StateContract {
130
+ since: string;
131
+ auth?: RouteAuthProfile;
132
+ requiresSession?: boolean;
133
+ deprecatedIn?: string;
134
+ removedIn?: string;
135
+ }
136
+ /** Version markers of a write added, deprecated or removed apart from its read. */
137
+ interface StateWriteOptions {
138
+ since?: string;
139
+ deprecatedIn?: string;
140
+ removedIn?: string;
141
+ }
142
+ /** Any read / permission / write context, as the runtime handles it. */
143
+ interface StateCallContext {
144
+ subject?: string;
145
+ principal: unknown;
146
+ key?: Record<string, string>;
147
+ input?: unknown;
148
+ }
149
+ type StatePredicate = (ctx: StateCallContext) => boolean | Promise<boolean>;
150
+ type StateReader = (ctx: StateCallContext) => unknown;
151
+ type StateWriter = (ctx: StateCallContext) => void | Promise<void>;
152
+ /**
153
+ * Everything one builder declared, as the router reads it.
154
+ */
155
+ interface StateDefinition {
156
+ id: string;
157
+ key?: TObject;
158
+ shared: boolean;
159
+ /** Whether `.principal()` was declared: the write route then refuses a request whose `auth` is `undefined` or `null`. */
160
+ principal: boolean;
161
+ value?: TSchema;
162
+ data?: TSchema;
163
+ middlewares: (MiddlewareHandler | NamedMiddleware<string>)[];
164
+ skipMiddlewares?: string[] | '*';
165
+ contract?: StateContract;
166
+ canRead?: StatePredicate;
167
+ canWrite?: StatePredicate;
168
+ read?: StateReader;
169
+ write?: {
170
+ input: TSchema;
171
+ fn: StateWriter;
172
+ options: StateWriteOptions;
173
+ };
174
+ changedBy?: EventDef<any>[];
175
+ }
176
+ /**
177
+ * What `defineStateRouter` accepts: a builder with `.value()` and `.read()`
178
+ * declared. A builder without either is not assignable here.
179
+ */
180
+ interface RoutableState {
181
+ readonly id: string;
182
+ readonly _types: StateShape & {
183
+ value: TSchema;
184
+ read: true;
185
+ };
186
+ readonly _definition: Readonly<StateDefinition>;
187
+ readonly _event: EventDef<any>;
188
+ readonly changed: EventDef<any>;
189
+ }
190
+
191
+ /**
192
+ * State Builder
193
+ *
194
+ * `defineState(id)` starts an immutable builder: every call returns a new one,
195
+ * order is free and there is no terminal method. `defineStateRouter` reads
196
+ * what a builder declared and derives its routes and SSE event from it.
197
+ */
198
+
199
+ /**
200
+ * A server-owned value declared once. See the module README.
201
+ */
202
+ declare class StateBuilder<TId extends string, T extends StateShape> {
203
+ readonly id: TId;
204
+ readonly _definition: Readonly<StateDefinition>;
205
+ /** Type inference helper: what this builder declared. Never set at runtime. */
206
+ readonly _types: T;
207
+ private changedSignal?;
208
+ private stateEvent?;
209
+ constructor(id: TId, _definition: Readonly<StateDefinition>);
210
+ /**
211
+ * Declare the key: one value per key (and per subject, unless `shared`).
212
+ * Fields are required strings: they travel as query parameters.
213
+ */
214
+ key<K extends TObject, S extends boolean = false>(schema: K, options?: {
215
+ shared?: S;
216
+ }): StateBuilder<TId, WithShape<T, {
217
+ key: K;
218
+ shared: S;
219
+ }>>;
220
+ /** Declare the value schema. Required. */
221
+ value<V extends TSchema>(schema: V): StateBuilder<TId, WithShape<T, {
222
+ value: V;
223
+ }>>;
224
+ /** Declare data attached to the value. Optional. */
225
+ data<D extends TSchema>(schema: D): StateBuilder<TId, WithShape<T, {
226
+ data: D;
227
+ }>>;
228
+ /**
229
+ * Declare who the callbacks' `principal` is — what the app's auth
230
+ * middleware sets as `c.get('auth')`. Type-only, but for one rule: the write
231
+ * route refuses a request without a principal (401), so `.canWrite()` and
232
+ * `.write()` receive `P`; `.read()` and `.canRead()` receive `P | undefined`.
233
+ */
234
+ principal<P>(): StateBuilder<TId, WithShape<T, {
235
+ principal: P;
236
+ }>>;
237
+ /**
238
+ * Middleware for the two per-state routes, `get<Id>State` / `put<Id>State`.
239
+ * Not authorization: the snapshot and the SSE stream cannot run it, so a
240
+ * state with `.use()` must declare `.canRead()`. Calls add up.
241
+ */
242
+ use(middlewares: (MiddlewareHandler | NamedMiddleware<string>)[]): StateBuilder<TId, T>;
243
+ /** Server-level named middlewares the two per-state routes skip. Calls add up; `'*'` wins. Needs `.canRead()`, as `.use()`. */
244
+ skip(middlewareNames: string[] | '*'): StateBuilder<TId, T>;
245
+ /** Publish the read route, the write route and the event in the contract. */
246
+ contract(contract: StateContract): StateBuilder<TId, T>;
247
+ /** Who may read. Required on a keyed state; checked on HTTP and at SSE connect. */
248
+ canRead(predicate: (ctx: PermissionContext<T>) => boolean | Promise<boolean>): StateBuilder<TId, T>;
249
+ /** Who may write. Absent → `canRead` decides. */
250
+ canWrite(predicate: (ctx: WritePermissionContext<T>) => boolean | Promise<boolean>): StateBuilder<TId, T>;
251
+ /** The only source of a value. Required. */
252
+ read(fn: (ctx: ReadContext<T>) => StateResult<T> | Promise<StateResult<T>>): StateBuilder<TId, WithShape<T, {
253
+ read: true;
254
+ }>>;
255
+ /**
256
+ * Derive `PUT <path>/<id>`. `fn` runs the command; the value the route
257
+ * answers and broadcasts is re-read afterwards, never taken from `input`.
258
+ */
259
+ write<I extends TSchema>(input: I, fn: (ctx: WriteContext<T, I>) => void | Promise<void>, options?: StateWriteOptions): StateBuilder<TId, WithShape<T, {
260
+ input: I;
261
+ }>>;
262
+ /**
263
+ * Domain events whose payload names a changed value of this state, treated
264
+ * like `changed.emit` with that payload: keyless `subject`, keyed the key
265
+ * fields and `subject`, shared the key fields. The router subscribes them
266
+ * and refuses one whose payload schema lacks a field.
267
+ */
268
+ changedBy(events: EventDef<any>[]): StateBuilder<TId, T>;
269
+ /**
270
+ * The change signal. Emit it after the change is committed; the module
271
+ * re-reads the value and sends the frame. Refused on a builder no router
272
+ * grouped — export the instance you passed to `defineStateRouter`.
273
+ */
274
+ get changed(): EventDef<ChangedPayload<T>>;
275
+ /** Internal: the SSE event `state.<id>.changed` (`defineStateRouter(...).events`). */
276
+ get _event(): EventDef<StateFrame<T>>;
277
+ /**
278
+ * Activate the state's event on `subject`'s open connections and send it
279
+ * the current value at once. On a keyed state only the connections that
280
+ * declared `key` are opened. Does not consult `canRead`.
281
+ */
282
+ grant(target: GrantTarget<T>): Promise<void>;
283
+ /**
284
+ * Deactivate the state's event on `subject`'s open connections: on a keyed
285
+ * state those that declared `key`, or every one when no key is given.
286
+ */
287
+ revoke(target: RevokeTarget<T>): Promise<void>;
288
+ /** A keyed grant opens one key: without it there is no key to open and no value to send. */
289
+ private assertGrantKey;
290
+ /**
291
+ * The declared key fields of `key`, checked against `.key()`: an empty or
292
+ * partial key would match every connection of the subject. A field the
293
+ * declaration does not name is dropped, never matched on — so the check
294
+ * runs on a fresh object of the declared fields, never `.key()` itself: a
295
+ * key declared `additionalProperties: false` would otherwise refuse it.
296
+ * The fresh object keeps `.key()`'s `required`, and a key that still picks
297
+ * to nothing is refused: `undefined` here would mean every connection.
298
+ */
299
+ private declaredKey;
300
+ private refuseKey;
301
+ private with;
302
+ private assertOnce;
303
+ }
304
+ /**
305
+ * Declare a server-owned state.
306
+ *
307
+ * @example
308
+ * ```typescript
309
+ * export const consent = defineState('consent')
310
+ * .value(Type.Union([Type.Literal('pending'), Type.Literal('agreed')]))
311
+ * .read(({ subject }) => consents.current(subject));
312
+ * ```
313
+ */
314
+ declare function defineState<TId extends string>(id: TId): StateBuilder<TId, EmptyStateShape>;
315
+
316
+ /**
317
+ * State Routes
318
+ *
319
+ * The read, write and snapshot routes a state router derives. They are
320
+ * ordinary `RouteDef`s registered through `registerRoutes`, so a state's
321
+ * `.use()` middleware runs inside the route's own chain after the app's
322
+ * global middleware.
323
+ */
324
+
325
+ /** Who a request is for. `null` answers 401. */
326
+ type SubjectResolver = (c: Context) => string | null;
327
+ /**
328
+ * What the router gives the snapshot route `GET <path>` alone. A per-state
329
+ * route keeps its own state's `.use()` / `.skip()` / `.contract()`, and the
330
+ * SSE stream runs none of them.
331
+ */
332
+ interface SnapshotRouteOptions {
333
+ /** Middleware for the snapshot route only. Not authorization: the stream never runs it (see README). */
334
+ use?: (MiddlewareHandler | NamedMiddleware<string>)[];
335
+ /** Server-level named middlewares the snapshot route skips; `'*'` skips every one. */
336
+ skip?: string[] | '*';
337
+ /** Publishes the snapshot route in the contract; the response schema is derived. */
338
+ contract?: StateContract;
339
+ }
340
+ /** What the browser cannot read from a type: every state, in declaration order, its key fields and whether it has a write. */
341
+ type StatesManifest = Record<string, {
342
+ key: string[];
343
+ write: boolean;
344
+ }>;
345
+
346
+ /**
347
+ * State Router
348
+ *
349
+ * Groups states, derives their routes into one `Router` the app mounts with
350
+ * `.packages()`, collects their SSE events for the app's event router, and
351
+ * subscribes each state's change signal — once, here.
352
+ */
353
+
354
+ /**
355
+ * Where the router mounts, whom a request is for, and the snapshot route's own
356
+ * `use` / `skip` / `contract` (`SnapshotRouteOptions`) — those reach `GET <path>`
357
+ * only, never a per-state route or the SSE stream.
358
+ */
359
+ interface StateRouterOptions extends SnapshotRouteOptions {
360
+ /**
361
+ * Where the routes are mounted: `GET <path>`, `GET|PUT <path>/<id>`.
362
+ * @default '/state'
363
+ */
364
+ path?: string;
365
+ /**
366
+ * Who an HTTP request is for. `null` answers 401.
367
+ * @default (c) => c.get('auth')?.userId ?? null
368
+ */
369
+ subject?: SubjectResolver;
370
+ }
371
+ type Shape<S extends RoutableState> = S['_types'];
372
+ type KeyQuery<S extends RoutableState> = Shape<S>['key'] extends TObject ? {
373
+ query: Shape<S>['key'];
374
+ } : {};
375
+ type ReadRoutes<S extends Record<string, RoutableState>> = {
376
+ [K in keyof S as `get${Capitalize<S[K]['id']>}State`]: RouteDef<KeyQuery<S[K]>, {}, StateSnapshot<Shape<S[K]>>>;
377
+ };
378
+ type WriteInput<S extends RoutableState> = KeyQuery<S> & {
379
+ body: Shape<S>['input'];
380
+ };
381
+ type WriteRoutes<S extends Record<string, RoutableState>> = {
382
+ [K in keyof S as Shape<S[K]>['input'] extends TSchema ? `put${Capitalize<S[K]['id']>}State` : never]: RouteDef<WriteInput<S[K]>, {}, StateSnapshot<Shape<S[K]>>>;
383
+ };
384
+ /**
385
+ * What `GET <path>` answers: every state the subject may read and whose key
386
+ * the query carries, and the manifest of every state — its key fields and
387
+ * whether it has a write — in declaration order.
388
+ */
389
+ type StatesSnapshot<S extends Record<string, RoutableState>> = {
390
+ at: string;
391
+ states: {
392
+ [K in keyof S as S[K]['id']]?: StateResult<Shape<S[K]>>;
393
+ };
394
+ manifest: {
395
+ [K in keyof S as S[K]['id']]: {
396
+ key: string[];
397
+ write: boolean;
398
+ };
399
+ };
400
+ };
401
+ /** The routes one state router derives, typed for `createApi` and the route map. */
402
+ type StateRoutes<S extends Record<string, RoutableState>> = ReadRoutes<S> & WriteRoutes<S> & {
403
+ getStates: RouteDef<{
404
+ query: TObject;
405
+ }, {}, StatesSnapshot<S>>;
406
+ };
407
+ /** The SSE events, keyed by their wire names, for the app's `defineEventRouter`. */
408
+ type StateEvents<S extends Record<string, RoutableState>> = {
409
+ [K in keyof S as `state.${S[K]['id']}.changed`]: EventDef<StateFrame<Shape<S[K]>>>;
410
+ };
411
+ interface StateRouter<S extends Record<string, RoutableState>> {
412
+ /** The derived routes. Mount with `appRouter.packages([states.router])`. */
413
+ router: Router<StateRoutes<S>>;
414
+ /** The derived SSE events. Spread into `defineEventRouter({ ...appEvents, ...states.events })`. */
415
+ events: StateEvents<S>;
416
+ /** The declarations, as given. */
417
+ states: S;
418
+ }
419
+ type StateById<R extends StateRouter<any>, TId extends string> = Extract<R['states'][keyof R['states']], {
420
+ id: TId;
421
+ }>;
422
+ /** The value type of state `TId` of a state router. */
423
+ type InferStateValue<R extends StateRouter<any>, TId extends string> = StateValue<Shape<StateById<R, TId>>>;
424
+ /** The key type of state `TId`, `undefined` when it is keyless. */
425
+ type InferStateKey<R extends StateRouter<any>, TId extends string> = StateKey<Shape<StateById<R, TId>>>;
426
+ /** The write input of state `TId`, `never` when it declares no `.write()`. */
427
+ type InferStateInput<R extends StateRouter<any>, TId extends string> = Shape<StateById<R, TId>>['input'] extends TSchema ? Static<Shape<StateById<R, TId>>['input']> : never;
428
+ /**
429
+ * Group states, derive their routes and SSE events, and subscribe their change
430
+ * signals.
431
+ *
432
+ * @example
433
+ * ```typescript
434
+ * export const states = defineStateRouter({ consent, runtime, member });
435
+ *
436
+ * // server.config.ts
437
+ * // .routes(appRouter.packages([states.router]))
438
+ * // .events(defineEventRouter({ ...appEvents, ...states.events }), { auth: { enabled: true } })
439
+ * ```
440
+ */
441
+ declare function defineStateRouter<S extends Record<string, RoutableState>>(states: S, options?: StateRouterOptions): StateRouter<S>;
442
+
443
+ export { type ChangedPayload, type GrantTarget, type InferStateInput, type InferStateKey, type InferStateValue, type PermissionContext, type ReadContext, type RevokeTarget, type RoutableState, type SnapshotRouteOptions, StateBuilder, type StateContract, type StateEvents, type StateFrame, type StateKey, type StateResult, type StateRouter, type StateRouterOptions, type StateRoutes, type StateShape, type StateSnapshot, type StateValue, type StateWriteOptions, type StatesManifest, type StatesSnapshot, type SubjectResolver, type WriteContext, type WritePermissionContext, defineState, defineStateRouter };