orez-sync-cf-host 0.4.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts ADDED
@@ -0,0 +1,153 @@
1
+ export type JsonPrimitive = string | number | boolean | null
2
+ export type JsonValue = JsonPrimitive | JsonValue[] | { [key: string]: JsonValue }
3
+
4
+ export type NormalizedClaims = {
5
+ /** Stable consumer user id used for client-group ownership. */
6
+ userID: string
7
+ [claim: string]: JsonValue
8
+ }
9
+
10
+ export type ZeroSchemaConfig = {
11
+ readonly tables: Readonly<
12
+ Record<
13
+ string,
14
+ {
15
+ readonly columns: Readonly<Record<string, { readonly type: string }>>
16
+ readonly primaryKey: readonly string[]
17
+ }
18
+ >
19
+ >
20
+ }
21
+
22
+ export interface SyncSql {
23
+ exec(sql: string, params?: readonly unknown[]): void
24
+ query<Row extends Record<string, unknown> = Record<string, unknown>>(
25
+ sql: string,
26
+ params?: readonly unknown[]
27
+ ): Row[]
28
+ }
29
+
30
+ export interface MutatorSql {
31
+ exec(sql: string, params?: readonly unknown[]): Promise<void>
32
+ query<Row extends Record<string, unknown> = Record<string, unknown>>(
33
+ sql: string,
34
+ params?: readonly unknown[]
35
+ ): Promise<Row[]>
36
+ /** Execute a validated Zero AST inside the current application transaction. */
37
+ queryAst<Row extends Record<string, unknown> = Record<string, unknown>>(
38
+ ast: JsonValue
39
+ ): Promise<Row[]>
40
+ }
41
+
42
+ export type DeferredEffect = () => void | Promise<void>
43
+
44
+ export type MutatorContext = {
45
+ claims: NormalizedClaims
46
+ /** Current Zero mutation identity, when invoked by the production host. */
47
+ clientID?: string
48
+ mutationID?: string | number
49
+ defer(effect: DeferredEffect): void
50
+ }
51
+
52
+ export type RegisteredMutator = (
53
+ tx: MutatorSql,
54
+ args: JsonValue,
55
+ context: MutatorContext
56
+ ) => void | Promise<void>
57
+
58
+ export type MutatorRegistry = Readonly<Record<string, RegisteredMutator>>
59
+
60
+ /** Preserve mutator names while making the host-facing registry immutable. */
61
+ export function registerMutators<
62
+ const Registry extends Record<string, RegisteredMutator>,
63
+ >(registry: Registry): Readonly<Registry> {
64
+ return Object.freeze({ ...registry })
65
+ }
66
+
67
+ export class MutationApplicationError extends Error {
68
+ constructor(
69
+ readonly details: string,
70
+ message = details
71
+ ) {
72
+ super(message)
73
+ this.name = 'MutationApplicationError'
74
+ }
75
+ }
76
+
77
+ export type VisibilityFilter = {
78
+ /** SQL WHERE fragment only, without the WHERE keyword. */
79
+ sql: string
80
+ params?: readonly JsonPrimitive[]
81
+ }
82
+
83
+ export type VisibilityConfig = {
84
+ /** True only when every predicate depends on the selected row alone. */
85
+ rowLocal: boolean | ((claims: NormalizedClaims) => boolean)
86
+ filter(table: string, claims: NormalizedClaims): VisibilityFilter | undefined
87
+ }
88
+
89
+ export type QueryResolver = (
90
+ name: string,
91
+ args: readonly JsonValue[],
92
+ claims: NormalizedClaims,
93
+ env: SyncHostEnv
94
+ ) => JsonValue | Promise<JsonValue>
95
+
96
+ export type PullCaps = {
97
+ maxChangeRows: number
98
+ maxChangeBytes: number
99
+ }
100
+
101
+ export interface SyncHostEnv {
102
+ SYNC_DO: DurableObjectNamespace
103
+ ADMIN_KEY?: string
104
+ }
105
+
106
+ export type ServiceBinding = {
107
+ fetch(input: string | Request, init?: RequestInit): Promise<Response>
108
+ }
109
+
110
+ export type UpstreamConfig = {
111
+ /** Env key for the service binding that owns the app write endpoint and feed. */
112
+ binding: string
113
+ /** Path to this namespace on the bound service (for example `/data/<id>`). */
114
+ namespacePath: string | ((namespace: string) => string)
115
+ /** Feed page size; the cursor loop continues until the reported head is reached. */
116
+ changeLimit?: number
117
+ /** Durable Object alarm safety net. Defaults to 15 seconds. */
118
+ intervalMs?: number
119
+ }
120
+
121
+ export type SyncHostConfig<Env extends SyncHostEnv = SyncHostEnv> = {
122
+ hostVersion: string
123
+ schema: ZeroSchemaConfig
124
+ mutators?: MutatorRegistry
125
+ /** Relative app push endpoint on the upstream service binding. */
126
+ mutateUrl?: string
127
+ /** Env binding for delegated pushes; defaults to upstream.binding. */
128
+ mutateBinding?: string
129
+ /** Required for delegated push; forbidden with local mutators (no dual apply). */
130
+ upstream?: UpstreamConfig
131
+ /** Application DDL and optional seed, called before sync-core schema init. */
132
+ initialize(sql: SyncSql): void
133
+ authenticate(
134
+ request: Request,
135
+ env: Env
136
+ ): NormalizedClaims | null | Promise<NormalizedClaims | null>
137
+ /** Resolve the first path component or another consumer-defined namespace. */
138
+ namespace(request: Request): string | null
139
+ visibility?: VisibilityConfig
140
+ /** Enable desired-query pulls for this namespace and resolve named queries
141
+ * into validated Zero ASTs before they reach sync-core. */
142
+ queryAware?: boolean | ((claims: NormalizedClaims) => boolean)
143
+ resolveQuery?: QueryResolver
144
+ /** Server-owned invalidation epoch for permission/schema transforms. */
145
+ queryTransformVersion?: number | ((claims: NormalizedClaims) => number)
146
+ /** Enable consumer visibility from the first request. Defaults to false for harnesses. */
147
+ visibilityEnabled?: boolean
148
+ retainChanges?: number
149
+ caps?: Partial<PullCaps>
150
+ idleTeardownMs?: number
151
+ wakeCoalesceMs?: number
152
+ authorizeAdmin?: (request: Request, env: Env) => boolean | Promise<boolean>
153
+ }