@terreno/api 0.31.0 → 56.0.0-beta.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 (250) hide show
  1. package/CHANGELOG.md +122 -2
  2. package/LICENSE +202 -21
  3. package/dist/api.d.ts +12 -3
  4. package/dist/api.js +110 -361
  5. package/dist/auth.d.ts +26 -0
  6. package/dist/auth.js +100 -1
  7. package/dist/betterAuth.d.ts +5 -0
  8. package/dist/betterAuthSetup.d.ts +1 -15
  9. package/dist/betterAuthSetup.js +29 -8
  10. package/dist/configurationPlugin.js +2 -2
  11. package/dist/docLoader.js +12 -9
  12. package/dist/errors.d.ts +13 -0
  13. package/dist/errors.js +29 -1
  14. package/dist/expressServer.js +5 -11
  15. package/dist/githubAuth.js +4 -1
  16. package/dist/index.d.ts +17 -1
  17. package/dist/index.js +29 -1
  18. package/dist/middleware.d.ts +7 -0
  19. package/dist/middleware.js +16 -1
  20. package/dist/openApiCompat.d.ts +10 -1
  21. package/dist/openApiCompat.js +115 -47
  22. package/dist/permissions.d.ts +27 -0
  23. package/dist/permissions.js +42 -1
  24. package/dist/plugins.d.ts +15 -3
  25. package/dist/plugins.js +36 -6
  26. package/dist/realtime/changeStreamWatcher.d.ts +100 -3
  27. package/dist/realtime/changeStreamWatcher.js +542 -137
  28. package/dist/realtime/realtimeApp.d.ts +3 -1
  29. package/dist/realtime/realtimeApp.js +75 -27
  30. package/dist/realtime/sessionRevalidation.d.ts +90 -0
  31. package/dist/realtime/sessionRevalidation.js +438 -0
  32. package/dist/realtime/socketAuth.d.ts +97 -0
  33. package/dist/realtime/socketAuth.js +272 -0
  34. package/dist/realtime/socketUser.d.ts +54 -0
  35. package/dist/realtime/socketUser.js +75 -5
  36. package/dist/realtime/types.d.ts +40 -0
  37. package/dist/secretProviders.d.ts +23 -0
  38. package/dist/secretProviders.js +9 -3
  39. package/dist/sync/executors.d.ts +198 -0
  40. package/dist/sync/executors.js +670 -0
  41. package/dist/sync/loadHarness.js +807 -0
  42. package/dist/sync/models.d.ts +216 -0
  43. package/dist/sync/models.js +644 -0
  44. package/dist/sync/mutationHandler.d.ts +97 -0
  45. package/dist/sync/mutationHandler.js +886 -0
  46. package/dist/sync/registry.d.ts +73 -0
  47. package/dist/sync/registry.js +223 -0
  48. package/dist/sync/routes.d.ts +62 -0
  49. package/dist/sync/routes.js +936 -0
  50. package/dist/sync/scripts/compactTombstones.d.ts +47 -0
  51. package/dist/sync/scripts/compactTombstones.js +270 -0
  52. package/dist/sync/serialize.d.ts +20 -0
  53. package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
  54. package/dist/sync/socketHandlers.d.ts +54 -0
  55. package/dist/sync/socketHandlers.js +470 -0
  56. package/dist/sync/streams.d.ts +85 -0
  57. package/dist/sync/streams.js +221 -0
  58. package/dist/sync/syncApp.d.ts +29 -0
  59. package/dist/sync/syncApp.js +44 -0
  60. package/dist/sync/syncBatch.d.ts +71 -0
  61. package/dist/sync/syncBatch.js +252 -0
  62. package/dist/sync/syncSeqPlugin.d.ts +8 -0
  63. package/dist/sync/syncSeqPlugin.js +903 -0
  64. package/dist/sync/types.d.ts +254 -0
  65. package/dist/sync/types.js +9 -0
  66. package/dist/terrenoApp.js +90 -27
  67. package/dist/tests/models.d.ts +11 -27
  68. package/dist/tests/models.js +3 -5
  69. package/dist/tests/testHelper.d.ts +3 -9
  70. package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
  71. package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
  72. package/package.json +10 -8
  73. package/src/__tests__/openApiCompat.test.ts +20 -0
  74. package/src/actions.test.ts +12 -19
  75. package/src/actions.ts +2 -2
  76. package/src/api.coverage.test.ts +9 -7
  77. package/src/api.errorPaths.test.ts +1 -47
  78. package/src/api.test.ts +12 -0
  79. package/src/api.ts +98 -317
  80. package/src/auth.test.ts +128 -176
  81. package/src/auth.ts +109 -0
  82. package/src/betterAuth.ts +6 -0
  83. package/src/betterAuthSetup.test.ts +34 -0
  84. package/src/betterAuthSetup.ts +27 -8
  85. package/src/configurationPlugin.ts +2 -2
  86. package/src/consentApp.test.ts +12 -12
  87. package/src/docLoader.ts +3 -1
  88. package/src/errors.ts +33 -0
  89. package/src/expressServer.test.ts +7 -1
  90. package/src/expressServer.ts +5 -11
  91. package/src/githubAuth.ts +3 -1
  92. package/src/index.ts +42 -0
  93. package/src/middleware.test.ts +40 -1
  94. package/src/middleware.ts +15 -0
  95. package/src/models/versionConfig.ts +1 -1
  96. package/src/openApiBuilder.test.ts +9 -24
  97. package/src/openApiCompat.ts +99 -45
  98. package/src/openApiValidator.test.ts +0 -41
  99. package/src/permissions.test.ts +75 -1
  100. package/src/permissions.ts +49 -0
  101. package/src/plugins.test.ts +54 -14
  102. package/src/plugins.ts +47 -9
  103. package/src/populate.test.ts +16 -25
  104. package/src/realtime/changeStreamWatcher.ts +552 -114
  105. package/src/realtime/realtime.test.ts +157 -239
  106. package/src/realtime/realtimeApp.ts +80 -19
  107. package/src/realtime/sessionRevalidation.test.ts +423 -0
  108. package/src/realtime/sessionRevalidation.ts +273 -0
  109. package/src/realtime/socketAuth.test.ts +355 -0
  110. package/src/realtime/socketAuth.ts +228 -0
  111. package/src/realtime/socketUser.ts +69 -0
  112. package/src/realtime/types.ts +40 -0
  113. package/src/secretProviders.test.ts +7 -1
  114. package/src/secretProviders.ts +15 -3
  115. package/src/sync/ensureSyncIndexes.test.ts +144 -0
  116. package/src/sync/executors.test.ts +882 -0
  117. package/src/sync/executors.ts +703 -0
  118. package/src/sync/integration.test.ts +1160 -0
  119. package/src/sync/loadHarness.ts +618 -0
  120. package/src/sync/models.ts +640 -0
  121. package/src/sync/mutationHandler.test.ts +1252 -0
  122. package/src/sync/mutationHandler.ts +850 -0
  123. package/src/sync/registry.ts +232 -0
  124. package/src/sync/routes.ts +812 -0
  125. package/src/sync/scripts/compactTombstones.test.ts +249 -0
  126. package/src/sync/scripts/compactTombstones.ts +145 -0
  127. package/src/sync/serialize.ts +48 -0
  128. package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
  129. package/src/sync/socketHandlers.ts +320 -0
  130. package/src/sync/streams.ts +188 -0
  131. package/src/sync/sync.test.ts +582 -0
  132. package/src/sync/syncApp.ts +44 -0
  133. package/src/sync/syncBatch.ts +219 -0
  134. package/src/sync/syncFrontier.test.ts +494 -0
  135. package/src/sync/syncPhaseC.test.ts +718 -0
  136. package/src/sync/syncRoutes.test.ts +865 -0
  137. package/src/sync/syncSeqPlugin.ts +738 -0
  138. package/src/sync/syncSocket.test.ts +1440 -0
  139. package/src/sync/types.ts +270 -0
  140. package/src/terrenoApp.ts +37 -17
  141. package/src/tests/bunSetup.ts +8 -0
  142. package/src/tests/models.ts +6 -8
  143. package/tsconfig.json +4 -4
  144. package/tsconfig.typedoc.json +7 -1
  145. package/dist/__tests__/openApiCompat.test.js +0 -317
  146. package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
  147. package/dist/__tests__/versionCheckPlugin.test.js +0 -597
  148. package/dist/actions.openApi.test.d.ts +0 -1
  149. package/dist/actions.openApi.test.js +0 -253
  150. package/dist/actions.test.d.ts +0 -1
  151. package/dist/actions.test.js +0 -1087
  152. package/dist/api.arrayOperations.test.d.ts +0 -1
  153. package/dist/api.arrayOperations.test.js +0 -878
  154. package/dist/api.asyncHandler.test.d.ts +0 -1
  155. package/dist/api.asyncHandler.test.js +0 -236
  156. package/dist/api.coverage.test.d.ts +0 -1
  157. package/dist/api.coverage.test.js +0 -192
  158. package/dist/api.errorPaths.test.d.ts +0 -1
  159. package/dist/api.errorPaths.test.js +0 -887
  160. package/dist/api.errors.test.d.ts +0 -1
  161. package/dist/api.errors.test.js +0 -185
  162. package/dist/api.hooks.test.d.ts +0 -1
  163. package/dist/api.hooks.test.js +0 -950
  164. package/dist/api.query.test.d.ts +0 -1
  165. package/dist/api.query.test.js +0 -948
  166. package/dist/api.test.d.ts +0 -1
  167. package/dist/api.test.js +0 -2820
  168. package/dist/auth.compile.test.d.ts +0 -1
  169. package/dist/auth.compile.test.js +0 -201
  170. package/dist/auth.coverage.test.d.ts +0 -1
  171. package/dist/auth.coverage.test.js +0 -202
  172. package/dist/auth.test.d.ts +0 -1
  173. package/dist/auth.test.js +0 -2286
  174. package/dist/betterAuth.test.d.ts +0 -1
  175. package/dist/betterAuth.test.js +0 -181
  176. package/dist/betterAuthApp.test.d.ts +0 -1
  177. package/dist/betterAuthApp.test.js +0 -242
  178. package/dist/betterAuthSetup.test.d.ts +0 -1
  179. package/dist/betterAuthSetup.test.js +0 -762
  180. package/dist/config.test.d.ts +0 -1
  181. package/dist/config.test.js +0 -353
  182. package/dist/configuration.test.d.ts +0 -1
  183. package/dist/configuration.test.js +0 -980
  184. package/dist/configurationPlugin.test.d.ts +0 -1
  185. package/dist/configurationPlugin.test.js +0 -827
  186. package/dist/consentApp.test.d.ts +0 -1
  187. package/dist/consentApp.test.js +0 -1411
  188. package/dist/docLoader.test.d.ts +0 -1
  189. package/dist/docLoader.test.js +0 -137
  190. package/dist/envConfigurationPlugin.test.d.ts +0 -1
  191. package/dist/envConfigurationPlugin.test.js +0 -369
  192. package/dist/errors.test.d.ts +0 -1
  193. package/dist/errors.test.js +0 -607
  194. package/dist/expressServer.test.d.ts +0 -1
  195. package/dist/expressServer.test.js +0 -1249
  196. package/dist/githubAuth.test.d.ts +0 -1
  197. package/dist/githubAuth.test.js +0 -1142
  198. package/dist/httpClient.test.d.ts +0 -1
  199. package/dist/httpClient.test.js +0 -1471
  200. package/dist/logger.test.d.ts +0 -1
  201. package/dist/logger.test.js +0 -685
  202. package/dist/middleware.test.d.ts +0 -1
  203. package/dist/middleware.test.js +0 -241
  204. package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
  205. package/dist/notifiers/googleChatNotifier.test.js +0 -298
  206. package/dist/notifiers/slackNotifier.test.d.ts +0 -1
  207. package/dist/notifiers/slackNotifier.test.js +0 -297
  208. package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
  209. package/dist/notifiers/zoomNotifier.test.js +0 -373
  210. package/dist/openApi.test.d.ts +0 -1
  211. package/dist/openApi.test.js +0 -620
  212. package/dist/openApiBuilder.test.d.ts +0 -1
  213. package/dist/openApiBuilder.test.js +0 -599
  214. package/dist/openApiEtag.test.d.ts +0 -1
  215. package/dist/openApiEtag.test.js +0 -86
  216. package/dist/openApiValidator.test.d.ts +0 -1
  217. package/dist/openApiValidator.test.js +0 -795
  218. package/dist/permissions.middleware.test.d.ts +0 -1
  219. package/dist/permissions.middleware.test.js +0 -308
  220. package/dist/permissions.test.d.ts +0 -1
  221. package/dist/permissions.test.js +0 -439
  222. package/dist/plugins.test.d.ts +0 -1
  223. package/dist/plugins.test.js +0 -902
  224. package/dist/populate.test.d.ts +0 -1
  225. package/dist/populate.test.js +0 -370
  226. package/dist/realtime/realtime.test.d.ts +0 -10
  227. package/dist/realtime/realtime.test.js +0 -4213
  228. package/dist/realtime/realtimeAppSetup.test.d.ts +0 -1
  229. package/dist/realtime/realtimeAppSetup.test.js +0 -213
  230. package/dist/requestContext.test.d.ts +0 -1
  231. package/dist/requestContext.test.js +0 -412
  232. package/dist/scriptRunner.test.d.ts +0 -1
  233. package/dist/scriptRunner.test.js +0 -386
  234. package/dist/secretProviders.test.d.ts +0 -1
  235. package/dist/secretProviders.test.js +0 -726
  236. package/dist/syncConsents.test.d.ts +0 -1
  237. package/dist/syncConsents.test.js +0 -522
  238. package/dist/terrenoApp.test.d.ts +0 -1
  239. package/dist/terrenoApp.test.js +0 -489
  240. package/dist/tests/bunSetup.d.ts +0 -1
  241. package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
  242. package/dist/tests/fixtures/compileAuthEntry.js +0 -37
  243. package/dist/tests/testEnv.d.ts +0 -0
  244. package/dist/tests/testEnv.js +0 -6
  245. package/dist/transformers.test.d.ts +0 -1
  246. package/dist/transformers.test.js +0 -423
  247. package/dist/utils.test.d.ts +0 -1
  248. package/dist/utils.test.js +0 -242
  249. package/src/realtime/realtimeAppSetup.test.ts +0 -161
  250. /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
@@ -0,0 +1,254 @@
1
+ /**
2
+ * Shared types for the SyncDB local-first sync protocol.
3
+ *
4
+ * These types are the contract between @terreno/api (server) and @terreno/syncdb (client):
5
+ * scope/stream configuration, snapshot payloads, mutation requests, and delta events.
6
+ * See docs/implementationPlans/syncdb-local-first.md.
7
+ */
8
+ /** Scope a collection's streams by document owner (default field: `ownerId`). */
9
+ export interface SyncScopeOwner {
10
+ type: "owner";
11
+ /** Field holding the owning user's id. Defaults to "ownerId". */
12
+ field?: string;
13
+ }
14
+ /** Scope a collection's streams by a tenant/workspace field (e.g. `organizationId`). */
15
+ export interface SyncScopeTenant {
16
+ type: "tenant";
17
+ /** Field holding the tenant/workspace id. */
18
+ field: string;
19
+ }
20
+ /** A single shared stream for the whole collection (all authenticated users). */
21
+ export interface SyncScopeBroadcast {
22
+ type: "broadcast";
23
+ }
24
+ /**
25
+ * Custom scope resolver: given a document, return its scope value (e.g. a workspace id).
26
+ * The stream key becomes `{collection}|custom:{value}`.
27
+ */
28
+ export type SyncScopeResolver = (doc: Record<string, unknown>) => string;
29
+ export type SyncScope = SyncScopeOwner | SyncScopeTenant | SyncScopeBroadcast | SyncScopeResolver;
30
+ /**
31
+ * Configuration for local-first sync on a modelRouter, parallel to `realtime`.
32
+ * Requires the model schema to use `isDeletedPlugin` (soft delete) and `syncPlugin`
33
+ * (seq stamping) — validated at registration.
34
+ */
35
+ export interface SyncConfig {
36
+ /** Which stream a document belongs to. Multi-tenant by default via the tenant scope. */
37
+ scope: SyncScope;
38
+ /**
39
+ * Custom serializer for sync payloads (snapshot entities and deltas).
40
+ * Falls back to the modelRouter responseHandler, then the document's toJSON.
41
+ */
42
+ responseHandler?: (doc: Record<string, unknown>, method: SyncMutationOperation) => unknown;
43
+ /**
44
+ * Server-side query restricting snapshots to the caller's documents. Derived
45
+ * automatically for owner scopes ({field: user.id}) and tenant scopes
46
+ * ({field: {$in: getUserScopes(...)}}); REQUIRED for custom resolver scopes, whose
47
+ * stream function cannot be inverted into a Mongo query.
48
+ */
49
+ snapshotFilter?: (user: {
50
+ id: string;
51
+ }) => Record<string, unknown> | Promise<Record<string, unknown>>;
52
+ /**
53
+ * C7: tombstone retention window in days (default 90). Tombstones older than this may
54
+ * be hard-deleted by the `compactTombstones` maintenance script; a client whose cursor
55
+ * predates the retained floor re-bootstraps (see `oldestRetainedSeq`).
56
+ */
57
+ retentionDays?: number;
58
+ }
59
+ export type SyncMutationOperation = "create" | "update" | "delete";
60
+ /** A single entity in a snapshot response. */
61
+ export interface SyncEntityPayload {
62
+ /** Document id. */
63
+ id: string;
64
+ /** Serialized document data (present for tombstones too, so clients can render conflicts). */
65
+ data: unknown;
66
+ /** The document's `_syncSeq` — the client's per-entity version and cursor source. */
67
+ seq: number;
68
+ /** True when the entity is a soft-delete tombstone. */
69
+ deleted: boolean;
70
+ }
71
+ /**
72
+ * Response shape for `GET /sync/snapshot` (C2: one stream per request).
73
+ */
74
+ export interface SyncSnapshotResponse {
75
+ /** The stream this page belongs to (echoed from the request). */
76
+ stream: string;
77
+ entities: SyncEntityPayload[];
78
+ /**
79
+ * Highest seq included in this page (C1: never above `frontierSeq`); pass back as
80
+ * `cursor` to continue.
81
+ */
82
+ cursor: number;
83
+ /** True when more pages remain past `cursor` (more committed OR uncommitted seqs). */
84
+ hasMore: boolean;
85
+ /** C1: the stream's stable frontier — the client must not advance its cursor beyond this. */
86
+ frontierSeq: number;
87
+ /**
88
+ * C7: the lowest seq still retained for this stream after tombstone compaction. A
89
+ * client whose stored cursor is below this may have missed compacted tombstones and
90
+ * must re-bootstrap the stream from 0 (sanctioned wipe: retention gap, not auth).
91
+ */
92
+ oldestRetainedSeq: number;
93
+ /**
94
+ * C3: opaque forward token for paging the legacy (seq-0) stratum by `_id`. Present
95
+ * while unstamped legacy documents remain; absent once the stratum is exhausted and
96
+ * paging proceeds by seq. The client echoes it back verbatim.
97
+ */
98
+ legacyCursor?: string;
99
+ }
100
+ /** Response shape for `GET /sync/entities` (point lookup for repair). */
101
+ export interface SyncEntitiesResponse {
102
+ entities: SyncEntityPayload[];
103
+ }
104
+ /** One stream a user currently belongs to, from `GET /sync/streams`. */
105
+ export interface SyncStreamInfo {
106
+ /** The stream key (e.g. "todos|owner:123"). */
107
+ stream: string;
108
+ /** The collection tag the stream belongs to. */
109
+ collection: string;
110
+ }
111
+ /** Response shape for `GET /sync/streams`. */
112
+ export interface SyncStreamsResponse {
113
+ streams: SyncStreamInfo[];
114
+ }
115
+ /** A client mutation delivered via `sync:mutate` or `POST /sync/mutate`. */
116
+ export interface SyncMutateRequest {
117
+ /** Client-generated stable id; the idempotency key. */
118
+ mutationId: string;
119
+ /** Collection tag (e.g. "todos"). */
120
+ collection: string;
121
+ operation: SyncMutationOperation;
122
+ /** Target document id (required for update/delete; client-generated allowed for create). */
123
+ id?: string;
124
+ /** Fields to write (create/update). */
125
+ data?: Record<string, unknown>;
126
+ /** The `_syncSeq` the client last saw for this document; enables LWW conflict detection. */
127
+ baseVersion?: number;
128
+ }
129
+ /** Successful mutation acknowledgement. */
130
+ export interface SyncAck {
131
+ mutationId: string;
132
+ /** The document id (server-assigned for creates). */
133
+ id: string;
134
+ /** The document's new `_syncSeq`. */
135
+ seq: number;
136
+ /**
137
+ * C5 (FIX 6): set when the document write succeeded and the ledger
138
+ * finalized `applied`, but the model's post-hook (`postCreate`/
139
+ * `postUpdate`/`postDelete`) threw. The mutation is still a full success —
140
+ * this is informational only, never a reason to retry or roll back.
141
+ */
142
+ warning?: string;
143
+ }
144
+ export type SyncNackCode = "conflict" | "unauthorized" | "validation" | "error" | "rate_limited";
145
+ /** Rejected mutation. Conflict nacks carry the canonical server document. */
146
+ export interface SyncNack {
147
+ mutationId: string;
148
+ code: SyncNackCode;
149
+ /** Canonical serialized server document (conflict nacks). */
150
+ serverDoc?: unknown;
151
+ /** The server document's current `_syncSeq` (conflict nacks). */
152
+ serverSeq?: number;
153
+ message?: string;
154
+ /**
155
+ * Minimum time (ms) the client should wait before retrying, filled by the
156
+ * server with the remaining rate-limit window (`rate_limited` nacks only).
157
+ */
158
+ retryAfterMs?: number;
159
+ }
160
+ /**
161
+ * A batch of client mutations delivered via `sync:mutateBatch` or
162
+ * `POST /sync/mutate/batch`. The server MUST apply strictly in array order and
163
+ * stop at the first non-ack outcome (see `applySyncMutationBatch`).
164
+ */
165
+ export interface SyncMutateBatchRequest {
166
+ /** Ordered mutations; each still carries its own mutationId. */
167
+ mutations: SyncMutateRequest[];
168
+ /**
169
+ * Client-generated correlation id, socket transport only (ignored over
170
+ * HTTP). Echoed back immediately via `sync:batchReceived {batchId}` before
171
+ * processing begins, so the client can distinguish "the server has no
172
+ * sync:mutateBatch handler" (silence past the grace period, batching
173
+ * unsupported) from "the server is just slow to finish this batch" (a
174
+ * receipt arrived; keep waiting up to the full batch timeout).
175
+ */
176
+ batchId?: string;
177
+ }
178
+ /** One result per PROCESSED mutation in a batch, in request order. */
179
+ export type SyncMutateBatchResult = {
180
+ type: "ack";
181
+ ack: SyncAck;
182
+ } | {
183
+ type: "nack";
184
+ nack: SyncNack;
185
+ };
186
+ /**
187
+ * Response to a batch mutation request.
188
+ *
189
+ * `results.length < request.mutations.length` means the server halted at the
190
+ * first non-ack: `results[results.length - 1]` is that failing outcome, and
191
+ * every mutation after it was NOT attempted (not ledgered, not applied) —
192
+ * still safe to resend in a later batch (INV-3).
193
+ */
194
+ export interface SyncMutateBatchResponse {
195
+ results: SyncMutateBatchResult[];
196
+ }
197
+ /**
198
+ * A change event delivered to subscribed clients via `sync:delta`.
199
+ *
200
+ * ## Per-entity ordering & the LWW-by-seq contract (C8)
201
+ *
202
+ * The server serializes delta dispatch PER entity (`{collection}:{id}`): two deltas for
203
+ * the same document are always emitted in change-stream (commit) order, so their `seq`
204
+ * values arrive monotonically for that entity. Deltas for DIFFERENT entities may arrive
205
+ * in any order (they dispatch concurrently). Clients therefore apply last-writer-wins
206
+ * BY SEQ within an entity: a delta whose `seq` is at or below the entity's applied seq is
207
+ * an idempotent no-op; only a strictly higher `seq` mutates local state. Combined with
208
+ * the C1 frontier (`frontierSeq`), a cursor never advances past an uncommitted seq, so no
209
+ * committed delta is ever permanently skipped.
210
+ */
211
+ export interface SyncDelta {
212
+ /** Collection tag (e.g. "todos"). */
213
+ collection: string;
214
+ /** Document id. */
215
+ id: string;
216
+ method: SyncMutationOperation;
217
+ /** Serialized document data (omitted for tombstone deltas emitted to a previous stream). */
218
+ data?: unknown;
219
+ /** The document's `_syncSeq`. */
220
+ seq: number;
221
+ /** Stream key this delta belongs to (e.g. "todos|owner:123"). */
222
+ stream: string;
223
+ /**
224
+ * C1: the stream's stable frontier at emit time. The client advances its cursor to
225
+ * `min(seq, frontierSeq)` so a delta observed out of commit order never advances a
226
+ * cursor past an uncommitted hole.
227
+ */
228
+ frontierSeq?: number;
229
+ /** True when the entity is soft-deleted. */
230
+ deleted?: boolean;
231
+ }
232
+ /**
233
+ * Payload of the `sync:resync-required` socket event (Task 9.16).
234
+ *
235
+ * Emitted server-wide when the change stream could not be resumed — the oplog no longer
236
+ * contains the resume point, so the events in the gap can never be delivered. Any cursor a
237
+ * client holds may now sit below changes (including deletions) it will never be told about,
238
+ * so the only safe response is to re-bootstrap every stream. Clients that do not handle the
239
+ * event still converge on their next full bootstrap; handling it just makes that immediate.
240
+ */
241
+ export interface SyncResyncHint {
242
+ /** Why the resync is needed. Only `history_lost` exists today. */
243
+ reason: "history_lost";
244
+ }
245
+ /** Fields added to synced documents by `syncPlugin`. */
246
+ export interface SyncedDocumentFields {
247
+ /** Monotonic per-stream sequence stamped on every synced write. */
248
+ _syncSeq?: number;
249
+ /**
250
+ * The document's previous stream key, set when a write moved the document between
251
+ * scopes (owner/tenant change); null when the last write did not move it.
252
+ */
253
+ _syncPrevStream?: string | null;
254
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ /**
3
+ * Shared types for the SyncDB local-first sync protocol.
4
+ *
5
+ * These types are the contract between @terreno/api (server) and @terreno/syncdb (client):
6
+ * scope/stream configuration, snapshot payloads, mutation requests, and delta events.
7
+ * See docs/implementationPlans/syncdb-local-first.md.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -43,6 +43,42 @@ var __importStar = (this && this.__importStar) || (function () {
43
43
  return result;
44
44
  };
45
45
  })();
46
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
47
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
48
+ return new (P || (P = Promise))(function (resolve, reject) {
49
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
50
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
51
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
52
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
53
+ });
54
+ };
55
+ var __generator = (this && this.__generator) || function (thisArg, body) {
56
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
57
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
58
+ function verb(n) { return function (v) { return step([n, v]); }; }
59
+ function step(op) {
60
+ if (f) throw new TypeError("Generator is already executing.");
61
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
62
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
63
+ if (y = 0, t) op = [op[0] & 2, t.value];
64
+ switch (op[0]) {
65
+ case 0: case 1: t = op; break;
66
+ case 4: _.label++; return { value: op[1], done: false };
67
+ case 5: _.label++; y = op[1]; op = [0]; continue;
68
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
69
+ default:
70
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
71
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
72
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
73
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
74
+ if (t[2]) _.ops.pop();
75
+ _.trys.pop(); continue;
76
+ }
77
+ op = body.call(thisArg, _);
78
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
79
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
80
+ }
81
+ };
46
82
  var __values = (this && this.__values) || function(o) {
47
83
  var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
48
84
  if (m) return m.call(o);
@@ -75,6 +111,7 @@ var openApiCompat_1 = require("./openApiCompat");
75
111
  var openApiEtag_1 = require("./openApiEtag");
76
112
  var realtimeApp_1 = require("./realtime/realtimeApp");
77
113
  var requestContext_1 = require("./requestContext");
114
+ var registry_1 = require("./sync/registry");
78
115
  var index_1 = __importDefault(require("./vendor/wesleytodd-openapi/index"));
79
116
  /**
80
117
  * Fluent API for building Express applications with Terreno framework.
@@ -397,46 +434,72 @@ var TerrenoApp = /** @class */ (function () {
397
434
  * ```
398
435
  */
399
436
  TerrenoApp.prototype.start = function () {
400
- var e_3, _a;
401
437
  var _this = this;
438
+ var _a;
402
439
  // If realtime option is set, auto-register the RealtimeApp plugin
403
440
  if (this.options.realtime) {
404
441
  var hasRealtimePlugin = this.registrations.some(function (r) { return !_this.isModelRouterRegistration(r) && r instanceof realtimeApp_1.RealtimeApp; });
405
442
  if (!hasRealtimePlugin) {
406
443
  var realtimeConfig = typeof this.options.realtime === "object" ? this.options.realtime : {};
407
- this.register(new realtimeApp_1.RealtimeApp(realtimeConfig));
444
+ this.register(new realtimeApp_1.RealtimeApp(__assign(__assign({}, realtimeConfig), {
445
+ // Task 9.21 (D2): default the socket full-user load to this app's own user
446
+ // model. Without it socket authorization falls back to the synthetic
447
+ // JWT-claim user, which trusts the token's `admin` over the database and
448
+ // carries no membership fields for tenant-scoped sync.
449
+ userModel: (_a = realtimeConfig.userModel) !== null && _a !== void 0 ? _a : this.options.userModel })));
408
450
  }
409
451
  }
410
452
  var app = this.build();
411
453
  if (!this.options.skipListen) {
412
454
  var port_1 = process.env.PORT || "9000";
413
- try {
414
- var server = (0, node_http_1.createServer)(app);
415
- try {
416
- // Notify plugins that need access to the HTTP server (e.g. WebSocket plugins)
417
- for (var _b = __values(this.registrations), _c = _b.next(); !_c.done; _c = _b.next()) {
418
- var reg = _c.value;
419
- if (!this.isModelRouterRegistration(reg) && typeof reg.onServerCreated === "function") {
420
- reg.onServerCreated(server);
421
- }
455
+ // Await sync index creation before listening: the per-model snapshot indexes (a
456
+ // failed createIndex degrades the snapshot/catch-up query to a table scan) and the
457
+ // bookkeeping-model indexes enqueued by SyncApp (the unique mutationId index is what
458
+ // makes duplicate mutation deliveries idempotent, and the unique stream index is what
459
+ // keeps the counter upsert race from minting duplicate seqs). Either failure is a
460
+ // loud startup error rather than a silent correctness cliff. No-op when no sync
461
+ // models or SyncApp are registered. Detached because start() returns synchronously.
462
+ void (function () { return __awaiter(_this, void 0, void 0, function () {
463
+ var server, _a, _b, reg, error_1, stack;
464
+ var e_3, _c;
465
+ return __generator(this, function (_d) {
466
+ switch (_d.label) {
467
+ case 0:
468
+ _d.trys.push([0, 2, , 3]);
469
+ return [4 /*yield*/, (0, registry_1.ensureSyncIndexes)()];
470
+ case 1:
471
+ _d.sent();
472
+ server = (0, node_http_1.createServer)(app);
473
+ try {
474
+ // Notify plugins that need access to the HTTP server (e.g. WebSocket plugins)
475
+ for (_a = __values(this.registrations), _b = _a.next(); !_b.done; _b = _a.next()) {
476
+ reg = _b.value;
477
+ if (!this.isModelRouterRegistration(reg) && typeof reg.onServerCreated === "function") {
478
+ reg.onServerCreated(server);
479
+ }
480
+ }
481
+ }
482
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
483
+ finally {
484
+ try {
485
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
486
+ }
487
+ finally { if (e_3) throw e_3.error; }
488
+ }
489
+ server.listen(port_1, function () {
490
+ logger_1.logger.info("Listening on port ".concat(port_1));
491
+ });
492
+ return [3 /*break*/, 3];
493
+ case 2:
494
+ error_1 = _d.sent();
495
+ stack = error_1 instanceof Error ? error_1.stack : String(error_1);
496
+ logger_1.logger.error("Error trying to start HTTP server: ".concat(error_1, "\n").concat(stack));
497
+ process.exit(1);
498
+ return [3 /*break*/, 3];
499
+ case 3: return [2 /*return*/];
422
500
  }
423
- }
424
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
425
- finally {
426
- try {
427
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
428
- }
429
- finally { if (e_3) throw e_3.error; }
430
- }
431
- server.listen(port_1, function () {
432
- logger_1.logger.info("Listening on port ".concat(port_1));
433
501
  });
434
- }
435
- catch (error) {
436
- var stack = error instanceof Error ? error.stack : String(error);
437
- logger_1.logger.error("Error trying to start HTTP server: ".concat(error, "\n").concat(stack));
438
- process.exit(1);
439
- }
502
+ }); })();
440
503
  }
441
504
  return app;
442
505
  };
@@ -1,4 +1,4 @@
1
- import mongoose, { type Model } from "mongoose";
1
+ import mongoose, { type Model, Schema } from "mongoose";
2
2
  export interface User {
3
3
  admin: boolean;
4
4
  name?: string;
@@ -33,14 +33,14 @@ export interface Food {
33
33
  dateAdded?: string;
34
34
  };
35
35
  tags: string[];
36
- eatenBy: Array<mongoose.Types.ObjectId | User>;
36
+ eatenBy: [Schema.Types.ObjectId | User];
37
37
  lastEatenWith: {
38
38
  [name: string]: Date;
39
39
  };
40
40
  categories: FoodCategory[];
41
41
  expiration: string;
42
42
  likesIds: {
43
- userId: mongoose.Types.ObjectId | string;
43
+ userId: string;
44
44
  likes: boolean;
45
45
  }[];
46
46
  }
@@ -48,40 +48,24 @@ export interface RequiredField {
48
48
  name: string;
49
49
  about?: string;
50
50
  }
51
- export declare const UserModel: mongoose.Model<User, {}, {}, {}, mongoose.Document<unknown, {}, User, {}, mongoose.DefaultSchemaOptions> & User & {
51
+ export declare const UserModel: mongoose.Model<User, {}, {}, {}, mongoose.Document<unknown, {}, User, {}, {}> & User & {
52
52
  _id: mongoose.Types.ObjectId;
53
53
  } & {
54
54
  __v: number;
55
- } & {
56
- id: string;
57
- }, any, User>;
58
- export declare const SuperUserModel: mongoose.Model<User & SuperUser, {}, {}, {
59
- id: string;
60
- }, mongoose.Document<unknown, {}, User & SuperUser, {
61
- id: string;
62
- }, mongoose.DefaultSchemaOptions> & Omit<User & SuperUser & {
55
+ }, any>;
56
+ export declare const SuperUserModel: mongoose.Model<User & SuperUser, {}, {}, {}, mongoose.Document<unknown, {}, User & SuperUser, {}, {}> & User & SuperUser & {
63
57
  _id: mongoose.Types.ObjectId;
64
58
  } & {
65
59
  __v: number;
66
- }, "id"> & mongoose.HydratedDocumentOverrides<{
67
- id: string;
68
- }>, any, User & SuperUser>;
69
- export declare const StaffUserModel: mongoose.Model<User & StaffUser, {}, {}, {
70
- id: string;
71
- }, mongoose.Document<unknown, {}, User & StaffUser, {
72
- id: string;
73
- }, mongoose.DefaultSchemaOptions> & Omit<User & StaffUser & {
60
+ }, any>;
61
+ export declare const StaffUserModel: mongoose.Model<User & StaffUser, {}, {}, {}, mongoose.Document<unknown, {}, User & StaffUser, {}, {}> & User & StaffUser & {
74
62
  _id: mongoose.Types.ObjectId;
75
63
  } & {
76
64
  __v: number;
77
- }, "id"> & mongoose.HydratedDocumentOverrides<{
78
- id: string;
79
- }>, any, User & StaffUser>;
65
+ }, any>;
80
66
  export declare const FoodModel: Model<Food>;
81
- export declare const RequiredModel: mongoose.Model<RequiredField, {}, {}, {}, mongoose.Document<unknown, {}, RequiredField, {}, mongoose.DefaultSchemaOptions> & RequiredField & {
67
+ export declare const RequiredModel: mongoose.Model<RequiredField, {}, {}, {}, mongoose.Document<unknown, {}, RequiredField, {}, {}> & RequiredField & {
82
68
  _id: mongoose.Types.ObjectId;
83
69
  } & {
84
70
  __v: number;
85
- } & {
86
- id: string;
87
- }, any, RequiredField>;
71
+ }, any>;
@@ -101,11 +101,9 @@ var foodSchema = new mongoose_1.Schema({
101
101
  type: mongoose_1.Schema.Types.ObjectId,
102
102
  },
103
103
  ],
104
- expiration: {
105
- description: "Expiration date of the food",
106
- // DateOnly is a custom SchemaType registered on Schema.Types; it stores an ISO date string.
107
- type: plugins_1.DateOnly,
108
- },
104
+ // noExplicitAny: DateOnly is a custom SchemaType not recognized by Mongoose's built-in type definitions
105
+ // biome-ignore lint/suspicious/noExplicitAny: DateOnly is a custom SchemaType not recognized by Mongoose's built-in type definitions
106
+ expiration: { description: "Expiration date of the food", type: plugins_1.DateOnly },
109
107
  hidden: {
110
108
  default: false,
111
109
  description: "Whether this food is hidden from listings",
@@ -1,24 +1,18 @@
1
1
  import type { TestData } from "./types";
2
2
  export declare const applyTestAuthEnv: () => void;
3
3
  /** Seeds only the standard users (legacy helper). */
4
- export declare const setupDb: () => Promise<readonly [import("mongoose").Document<unknown, {}, import("./models").User, {}, import("mongoose").DefaultSchemaOptions> & import("./models").User & {
4
+ export declare const setupDb: () => Promise<readonly [import("mongoose").Document<unknown, {}, import("./models").User, {}, {}> & import("./models").User & {
5
5
  _id: import("mongoose").Types.ObjectId;
6
6
  } & {
7
7
  __v: number;
8
- } & {
9
- id: string;
10
- }, import("mongoose").Document<unknown, {}, import("./models").User, {}, import("mongoose").DefaultSchemaOptions> & import("./models").User & {
8
+ }, import("mongoose").Document<unknown, {}, import("./models").User, {}, {}> & import("./models").User & {
11
9
  _id: import("mongoose").Types.ObjectId;
12
10
  } & {
13
11
  __v: number;
14
- } & {
15
- id: string;
16
- }, import("mongoose").Document<unknown, {}, import("./models").User, {}, import("mongoose").DefaultSchemaOptions> & import("./models").User & {
12
+ }, import("mongoose").Document<unknown, {}, import("./models").User, {}, {}> & import("./models").User & {
17
13
  _id: import("mongoose").Types.ObjectId;
18
14
  } & {
19
15
  __v: number;
20
- } & {
21
- id: string;
22
16
  }]>;
23
17
  /** Seeds users, foods, and required docs — the recommended API test baseline. */
24
18
  export declare const setupTestData: () => Promise<TestData>;
@@ -16,7 +16,7 @@ declare function _exports(_routePrefix: any, _doc: any, _opts: any): {
16
16
  securitySchemes: (name?: any, description?: any) => any;
17
17
  links: (name?: any, description?: any) => any;
18
18
  callbacks: (name?: any, description?: any) => any;
19
- swaggerui(options: any): any[];
19
+ swaggerui(options: any): import("serve-static").RequestHandler<import("node:http").ServerResponse<import("node:http").IncomingMessage>>[];
20
20
  };
21
21
  declare namespace _exports {
22
22
  export { minimumViableDocument };
@@ -1 +1,2 @@
1
- export function serveSwaggerUI(documentUrl: any, opts?: {}): any[];
1
+ export function serveSwaggerUI(documentUrl: any, opts?: {}): serve.RequestHandler<import("node:http").ServerResponse<import("node:http").IncomingMessage>>[];
2
+ import serve = require("serve-static");
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "@asteasolutions/zod-to-openapi": "^8.5.0",
8
8
  "@sentry/bun": "^10.39.0",
9
9
  "@sentry/profiling-node": "^10.25.0",
10
- "@terreno/test": "0.31.0",
10
+ "@terreno/test": "56.0.0-beta.0",
11
11
  "@types/qs": "^6.14.0",
12
12
  "ajv": "8.20.0",
13
13
  "ajv-formats": "^3.0.1",
@@ -20,7 +20,7 @@
20
20
  "swagger-ui-dist": "^5.11.8",
21
21
  "yaml": "^2.4.0",
22
22
  "axios": "^1.13.2",
23
- "better-auth": "^1.2.8",
23
+ "better-auth": "^1.6.25",
24
24
  "cors": "^2.8.5",
25
25
  "cron": "^4.3.4",
26
26
  "expo-server-sdk": "^6.0.0",
@@ -47,6 +47,7 @@
47
47
  "description": "Styled after the Django & Django REST Framework, a batteries-include framework for building REST APIs with Node/Express/Mongoose.",
48
48
  "devDependencies": {
49
49
  "@biomejs/biome": "^2.3.6",
50
+ "@terreno/syncdb": "56.0.0-beta.0",
50
51
  "@types/bcrypt": "^6.0.0",
51
52
  "@types/bun": "^1.3.8",
52
53
  "@types/cors": "^2.8.17",
@@ -54,6 +55,7 @@
54
55
  "@types/express": "^5.0.6",
55
56
  "@types/jsonwebtoken": "^9.0.9",
56
57
  "@types/lodash": "^4.17.17",
58
+ "@types/mongodb": "^4.0.7",
57
59
  "@types/node": "^25.3.0",
58
60
  "@types/on-finished": "^2.3.4",
59
61
  "@types/passport": "^1.0.17",
@@ -64,11 +66,10 @@
64
66
  "@types/sinon": "^21.0.0",
65
67
  "@types/supertest": "^7.2.0",
66
68
  "mongodb-memory-server": "^11.0.1",
67
- "mongoose": "9.7.4",
69
+ "mongoose": "8.23.0",
68
70
  "sinon": "^22.0.0",
69
71
  "supertest": "^7.2.2",
70
- "typedoc": "^0.28.17",
71
- "typescript": "~5.9.2",
72
+ "typescript": "~6.0.3",
72
73
  "zod": "^4.4.3"
73
74
  },
74
75
  "homepage": "https://github.com/FlourishHealth/terreno#readme",
@@ -78,7 +79,7 @@
78
79
  "mongoose",
79
80
  "express"
80
81
  ],
81
- "license": "MIT",
82
+ "license": "Apache-2.0",
82
83
  "main": "dist/index.js",
83
84
  "exports": {
84
85
  ".": {
@@ -109,10 +110,11 @@
109
110
  "compile": "node ../.github/scripts/compile-workspace-deps.js && bun tsc",
110
111
  "compile:watch": "bun tsc -w",
111
112
  "dev": "bun tsc -w",
112
- "docs": "typedoc --out docs src/index.ts",
113
+ "docs": "bunx typedoc@0.28.19 --out docs src/index.ts",
113
114
  "lint": "biome check ./src",
114
115
  "lint:fix": "biome check --write ./src",
115
116
  "lint:unsafefix": "biome check --fix --unsafe ./src",
117
+ "load": "bun src/sync/loadHarness.ts",
116
118
  "test": "bun test --update-snapshots",
117
119
  "test:ci": "bun test",
118
120
  "test:coverage": "bun run ../scripts/check-coverage.ts",
@@ -122,5 +124,5 @@
122
124
  "updateSnapshot": "bun test --update-snapshots"
123
125
  },
124
126
  "types": "dist/index.d.ts",
125
- "version": "0.31.0"
127
+ "version": "56.0.0-beta.0"
126
128
  }
@@ -118,6 +118,26 @@ describe("openApiCompat", () => {
118
118
  }
119
119
  });
120
120
 
121
+ it("annotates nested router.use mounts with the mount path", () => {
122
+ const app = express();
123
+ patchAppUse(app);
124
+
125
+ const outer = express.Router();
126
+ const inner = express.Router();
127
+ inner.get("/", (_req, res) => {
128
+ res.json({ok: true});
129
+ });
130
+ outer.use("/gpt/histories", inner);
131
+ app.use("/", outer);
132
+
133
+ const stack = getRouterStack(app);
134
+ const nestedMount = findLayer(
135
+ stack,
136
+ (layer) => layer.__openApiMountPath === "/gpt/histories"
137
+ );
138
+ expect(nestedMount).toBeDefined();
139
+ });
140
+
121
141
  it("returns the underlying use() return value", () => {
122
142
  const app = express();
123
143
  patchAppUse(app);