@terreno/api 57.2.0 → 57.3.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 (277) hide show
  1. package/README.md +17 -7
  2. package/dist/actions.js +19 -7
  3. package/dist/api.d.ts +4 -0
  4. package/dist/api.js +12 -24
  5. package/dist/auth.d.ts +23 -10
  6. package/dist/auth.js +97 -32
  7. package/dist/authRecovery.d.ts +5 -0
  8. package/dist/authRecovery.js +350 -0
  9. package/dist/authTokens.d.ts +8 -0
  10. package/dist/authTokens.js +162 -0
  11. package/dist/betterAuth.d.ts +33 -0
  12. package/dist/betterAuthApp.d.ts +4 -0
  13. package/dist/betterAuthApp.js +26 -8
  14. package/dist/betterAuthSetup.d.ts +44 -2
  15. package/dist/betterAuthSetup.js +288 -18
  16. package/dist/collectionRegistry.d.ts +38 -0
  17. package/dist/collectionRegistry.js +72 -0
  18. package/dist/configurationApp.d.ts +11 -2
  19. package/dist/configurationApp.js +2 -4
  20. package/dist/docLoader.js +35 -6
  21. package/dist/expressServer.d.ts +26 -3
  22. package/dist/expressServer.js +79 -11
  23. package/dist/index.d.ts +16 -0
  24. package/dist/index.js +38 -2
  25. package/dist/mcp/auth.d.ts +1 -0
  26. package/dist/mcp/auth.js +47 -25
  27. package/dist/mcp/handlers.d.ts +1 -1
  28. package/dist/mcp/handlers.js +67 -233
  29. package/dist/mcp/query.d.ts +2 -2
  30. package/dist/mcp/registry.d.ts +1 -5
  31. package/dist/mcp/registry.js +31 -20
  32. package/dist/mcp/schemaGenerator.d.ts +2 -7
  33. package/dist/mcp/schemaGenerator.js +41 -116
  34. package/dist/mcp/server.d.ts +1 -0
  35. package/dist/mcp/server.js +1 -0
  36. package/dist/mcp/serviceTokens.d.ts +16 -0
  37. package/dist/mcp/serviceTokens.js +345 -0
  38. package/dist/mcp/types.d.ts +2 -2
  39. package/dist/models/mcpServiceToken.d.ts +3 -0
  40. package/dist/models/mcpServiceToken.js +208 -0
  41. package/dist/openApiBuilder.d.ts +5 -4
  42. package/dist/openApiBuilder.js +61 -8
  43. package/dist/openApiCompat.js +1 -1
  44. package/dist/openApiValidator.js +0 -2
  45. package/dist/plugins.d.ts +14 -1
  46. package/dist/plugins.js +17 -6
  47. package/dist/populate.d.ts +2 -2
  48. package/dist/populate.js +47 -42
  49. package/dist/rateLimit/applyTrustProxy.d.ts +3 -0
  50. package/dist/rateLimit/applyTrustProxy.js +17 -0
  51. package/dist/rateLimit/createStore.d.ts +2 -0
  52. package/dist/rateLimit/createStore.js +28 -0
  53. package/dist/rateLimit/memoryStore.d.ts +2 -0
  54. package/dist/rateLimit/memoryStore.js +108 -0
  55. package/dist/rateLimit/middleware.d.ts +11 -0
  56. package/dist/rateLimit/middleware.js +110 -0
  57. package/dist/rateLimit/mongoStore.d.ts +3 -0
  58. package/dist/rateLimit/mongoStore.js +122 -0
  59. package/dist/rateLimit/policies.d.ts +14 -0
  60. package/dist/rateLimit/policies.js +151 -0
  61. package/dist/rateLimit/redisStore.d.ts +3 -0
  62. package/dist/rateLimit/redisStore.js +155 -0
  63. package/dist/rateLimit/types.d.ts +54 -0
  64. package/dist/rateLimit/types.js +9 -0
  65. package/dist/rbac/access.js +5 -1
  66. package/dist/rbac/assertAllowed.d.ts +9 -0
  67. package/dist/rbac/assertAllowed.js +182 -0
  68. package/dist/rbac/modelRouterAccess.d.ts +1 -0
  69. package/dist/rbac/modelRouterAccess.js +8 -4
  70. package/dist/rbac/permissionUtils.js +3 -2
  71. package/dist/rbac/resolve.js +5 -4
  72. package/dist/rbac/routes.js +15 -6
  73. package/dist/realtime/deprecation.d.ts +12 -0
  74. package/dist/realtime/deprecation.js +32 -0
  75. package/dist/realtime/realtimeApp.d.ts +1 -1
  76. package/dist/realtime/realtimeApp.js +10 -11
  77. package/dist/realtime/registry.d.ts +0 -31
  78. package/dist/realtime/registry.js +44 -31
  79. package/dist/realtime/socketAuth.d.ts +5 -5
  80. package/dist/realtime/socketAuth.js +11 -1
  81. package/dist/realtime/socketUser.d.ts +5 -1
  82. package/dist/realtime/types.d.ts +14 -4
  83. package/dist/schemaMetadata.d.ts +56 -0
  84. package/dist/schemaMetadata.js +513 -0
  85. package/dist/seedRunner.d.ts +88 -0
  86. package/dist/seedRunner.js +627 -0
  87. package/dist/sync/executors.d.ts +12 -2
  88. package/dist/sync/executors.js +8 -10
  89. package/dist/sync/loadHarness.js +28 -19
  90. package/dist/sync/models.js +26 -24
  91. package/dist/sync/mutationHandler.d.ts +9 -0
  92. package/dist/sync/mutationHandler.js +11 -6
  93. package/dist/sync/registrationSideEffects.d.ts +35 -0
  94. package/dist/sync/registrationSideEffects.js +151 -0
  95. package/dist/sync/registry.d.ts +2 -52
  96. package/dist/sync/registry.js +38 -187
  97. package/dist/sync/routes.js +3 -2
  98. package/dist/sync/scripts/compactTombstones.js +0 -2
  99. package/dist/sync/syncApp.d.ts +3 -3
  100. package/dist/sync/syncApp.js +4 -4
  101. package/dist/sync/syncBatch.js +3 -2
  102. package/dist/sync/syncSeqPlugin.d.ts +1 -1
  103. package/dist/sync/syncSeqPlugin.js +20 -16
  104. package/dist/terrenoApp.d.ts +19 -5
  105. package/dist/terrenoApp.js +66 -18
  106. package/dist/tests/models.d.ts +2 -0
  107. package/dist/tests/models.js +6 -0
  108. package/dist/types/authToken.d.ts +33 -0
  109. package/dist/types/authToken.js +2 -0
  110. package/dist/types/mcpServiceToken.d.ts +41 -0
  111. package/dist/types/mcpServiceToken.js +2 -0
  112. package/dist/webhooks/headerValue.d.ts +2 -0
  113. package/dist/webhooks/headerValue.js +14 -0
  114. package/dist/webhooks/idempotency/memoryStore.d.ts +10 -0
  115. package/dist/webhooks/idempotency/memoryStore.js +65 -0
  116. package/dist/webhooks/idempotency/mongoStore.d.ts +4 -0
  117. package/dist/webhooks/idempotency/mongoStore.js +150 -0
  118. package/dist/webhooks/idempotency/webhookReceipt.d.ts +62 -0
  119. package/dist/webhooks/idempotency/webhookReceipt.js +39 -0
  120. package/dist/webhooks/rawBody.d.ts +14 -0
  121. package/dist/webhooks/rawBody.js +22 -0
  122. package/dist/webhooks/timingSafeEqual.d.ts +4 -0
  123. package/dist/webhooks/timingSafeEqual.js +17 -0
  124. package/dist/webhooks/verifiers/hmac.d.ts +15 -0
  125. package/dist/webhooks/verifiers/hmac.js +49 -0
  126. package/dist/webhooks/verifiers/sendgrid.d.ts +10 -0
  127. package/dist/webhooks/verifiers/sendgrid.js +56 -0
  128. package/dist/webhooks/verifiers/stripe.d.ts +9 -0
  129. package/dist/webhooks/verifiers/stripe.js +99 -0
  130. package/dist/webhooks/verifiers/twilio.d.ts +9 -0
  131. package/dist/webhooks/verifiers/twilio.js +112 -0
  132. package/dist/webhooks/webhooksApp.d.ts +33 -0
  133. package/dist/webhooks/webhooksApp.js +195 -0
  134. package/package.json +4 -5
  135. package/src/__snapshots__/openApi.test.ts.snap +270 -0
  136. package/src/__snapshots__/openApiBuilder.test.ts.snap +30 -12
  137. package/src/__snapshots__/schemaMetadata.test.ts.snap +197 -0
  138. package/src/actions.ts +20 -13
  139. package/src/adminModelRouter.test.ts +133 -0
  140. package/src/api.arrayOperations.test.ts +43 -31
  141. package/src/api.hooks.test.ts +38 -29
  142. package/src/api.query.test.ts +31 -31
  143. package/src/api.test.ts +95 -96
  144. package/src/api.ts +16 -27
  145. package/src/auth.test.ts +107 -51
  146. package/src/auth.ts +98 -29
  147. package/src/authRecovery.test.ts +281 -0
  148. package/src/authRecovery.ts +279 -0
  149. package/src/authTokens.test.ts +94 -0
  150. package/src/authTokens.ts +118 -0
  151. package/src/authVerification.test.ts +293 -0
  152. package/src/betterAuth.ts +24 -0
  153. package/src/betterAuthApp.test.ts +1 -0
  154. package/src/betterAuthApp.ts +27 -9
  155. package/src/betterAuthSetup.test.ts +568 -38
  156. package/src/betterAuthSetup.ts +260 -5
  157. package/src/collectionRegistry.test.ts +165 -0
  158. package/src/collectionRegistry.ts +115 -0
  159. package/src/configurationApp.ts +13 -7
  160. package/src/docLoader.test.ts +45 -1
  161. package/src/docLoader.ts +37 -6
  162. package/src/express.d.ts +1 -0
  163. package/src/expressServer.test.ts +130 -66
  164. package/src/expressServer.ts +75 -19
  165. package/src/githubAuth.test.ts +60 -55
  166. package/src/index.ts +58 -0
  167. package/src/mcp/auth.test.ts +47 -0
  168. package/src/mcp/auth.ts +32 -11
  169. package/src/mcp/handlers.ts +45 -172
  170. package/src/mcp/integration.test.ts +59 -9
  171. package/src/mcp/query.ts +2 -4
  172. package/src/mcp/registry.ts +29 -20
  173. package/src/mcp/schemaGenerator.test.ts +8 -0
  174. package/src/mcp/schemaGenerator.ts +49 -131
  175. package/src/mcp/server.ts +2 -0
  176. package/src/mcp/serviceTokens.test.ts +330 -0
  177. package/src/mcp/serviceTokens.ts +307 -0
  178. package/src/mcp/toolGenerator.test.ts +1 -1
  179. package/src/mcp/types.ts +2 -6
  180. package/src/models/mcpServiceToken.test.ts +129 -0
  181. package/src/models/mcpServiceToken.ts +158 -0
  182. package/src/openApi.test.ts +80 -64
  183. package/src/openApiBuilder.test.ts +95 -7
  184. package/src/openApiBuilder.ts +46 -9
  185. package/src/openApiCompat.ts +1 -1
  186. package/src/openApiValidator.test.ts +49 -54
  187. package/src/openApiValidator.ts +1 -3
  188. package/src/plugins.test.ts +14 -0
  189. package/src/plugins.ts +34 -9
  190. package/src/populate.test.ts +34 -0
  191. package/src/populate.ts +40 -29
  192. package/src/rateLimit/applyTrustProxy.ts +17 -0
  193. package/src/rateLimit/createStore.ts +25 -0
  194. package/src/rateLimit/memoryStore.ts +40 -0
  195. package/src/rateLimit/middleware.ts +79 -0
  196. package/src/rateLimit/mongoStore.test.ts +80 -0
  197. package/src/rateLimit/mongoStore.ts +76 -0
  198. package/src/rateLimit/policies.ts +130 -0
  199. package/src/rateLimit/rateLimit.test.ts +328 -0
  200. package/src/rateLimit/redisStore.ioredis.test.ts +29 -0
  201. package/src/rateLimit/redisStore.test.ts +114 -0
  202. package/src/rateLimit/redisStore.ts +85 -0
  203. package/src/rateLimit/types.ts +64 -0
  204. package/src/rbac/access.ts +5 -1
  205. package/src/rbac/assertAllowed.test.ts +325 -0
  206. package/src/rbac/assertAllowed.ts +157 -0
  207. package/src/rbac/backfillAdmins.test.ts +124 -0
  208. package/src/rbac/fieldViews.test.ts +89 -0
  209. package/src/rbac/modelRouterAccess.test.ts +126 -0
  210. package/src/rbac/modelRouterAccess.ts +5 -2
  211. package/src/rbac/permissionUtils.ts +3 -2
  212. package/src/rbac/realtimeAccess.test.ts +109 -1
  213. package/src/rbac/resolve.ts +5 -4
  214. package/src/rbac/roleManager.test.ts +169 -0
  215. package/src/rbac/routes.test.ts +233 -0
  216. package/src/rbac/routes.ts +16 -6
  217. package/src/realtime/deprecation.test.ts +60 -0
  218. package/src/realtime/deprecation.ts +32 -0
  219. package/src/realtime/realtime.test.ts +8 -4
  220. package/src/realtime/realtimeApp.ts +10 -11
  221. package/src/realtime/realtimeAppSetup.test.ts +32 -5
  222. package/src/realtime/registry.ts +48 -42
  223. package/src/realtime/sessionRevalidation.test.ts +57 -44
  224. package/src/realtime/socketAuth.test.ts +75 -23
  225. package/src/realtime/socketAuth.ts +17 -6
  226. package/src/realtime/socketUser.ts +5 -3
  227. package/src/realtime/types.ts +14 -4
  228. package/src/schemaMetadata.test.ts +405 -0
  229. package/src/schemaMetadata.ts +593 -0
  230. package/src/scrubAdminFields.test.ts +105 -0
  231. package/src/seedRunner.test.ts +591 -0
  232. package/src/seedRunner.ts +464 -0
  233. package/src/sync/docMatchesMutationData.test.ts +16 -0
  234. package/src/sync/ensureSyncIndexes.test.ts +54 -15
  235. package/src/sync/executors.test.ts +40 -0
  236. package/src/sync/executors.ts +24 -12
  237. package/src/sync/integration.test.ts +15 -7
  238. package/src/sync/loadHarness.ts +28 -19
  239. package/src/sync/models.ts +32 -18
  240. package/src/sync/mutationHandler.test.ts +16 -16
  241. package/src/sync/mutationHandler.ts +9 -7
  242. package/src/sync/registrationSideEffects.test.ts +126 -0
  243. package/src/sync/registrationSideEffects.ts +131 -0
  244. package/src/sync/registry.ts +45 -171
  245. package/src/sync/routes.ts +3 -2
  246. package/src/sync/scripts/compactTombstones.ts +0 -2
  247. package/src/sync/sessionRevalidationIntegration.test.ts +12 -11
  248. package/src/sync/sync.test.ts +25 -23
  249. package/src/sync/syncApp.ts +4 -4
  250. package/src/sync/syncBatch.ts +3 -2
  251. package/src/sync/syncFrontier.test.ts +17 -15
  252. package/src/sync/syncPhaseC.test.ts +54 -43
  253. package/src/sync/syncRoutes.test.ts +51 -36
  254. package/src/sync/syncSeqPlugin.ts +66 -44
  255. package/src/sync/syncSocket.test.ts +112 -81
  256. package/src/terrenoApp.mcpServiceTokens.test.ts +173 -0
  257. package/src/terrenoApp.ts +87 -15
  258. package/src/tests/models.ts +14 -1
  259. package/src/types/authToken.ts +43 -0
  260. package/src/types/mcpServiceToken.ts +54 -0
  261. package/src/webhooks/headerValue.test.ts +22 -0
  262. package/src/webhooks/headerValue.ts +12 -0
  263. package/src/webhooks/idempotency/memoryStore.ts +34 -0
  264. package/src/webhooks/idempotency/mongoStore.test.ts +274 -0
  265. package/src/webhooks/idempotency/mongoStore.ts +83 -0
  266. package/src/webhooks/idempotency/webhookReceipt.ts +70 -0
  267. package/src/webhooks/rawBody.test.ts +74 -0
  268. package/src/webhooks/rawBody.ts +23 -0
  269. package/src/webhooks/timingSafeEqual.ts +16 -0
  270. package/src/webhooks/verifiers/hmac.test.ts +110 -0
  271. package/src/webhooks/verifiers/hmac.ts +54 -0
  272. package/src/webhooks/verifiers/providerVerifiers.test.ts +388 -0
  273. package/src/webhooks/verifiers/sendgrid.ts +59 -0
  274. package/src/webhooks/verifiers/stripe.ts +67 -0
  275. package/src/webhooks/verifiers/twilio.ts +69 -0
  276. package/src/webhooks/webhooksApp.test.ts +221 -0
  277. package/src/webhooks/webhooksApp.ts +144 -0
@@ -1,12 +1,10 @@
1
- // noExplicitAny: test model typing
2
- // biome-ignore-all lint/suspicious/noExplicitAny: test model typing
3
1
  import {beforeAll, beforeEach, describe, expect, it} from "bun:test";
4
2
  import type express from "express";
5
- import {model, Schema, Types} from "mongoose";
3
+ import {type Model, model, Schema, Types} from "mongoose";
6
4
  import supertest from "supertest";
7
5
  import type TestAgent from "supertest/lib/agent";
8
6
  import {type ModelRouterOptions, modelRouter} from "../api";
9
- import {addAuthRoutes, setupAuth} from "../auth";
7
+ import {type UserModel as AuthUserModel, addAuthRoutes, setupAuth} from "../auth";
10
8
  import {APIError} from "../errors";
11
9
  import {OwnerQueryFilter, Permissions} from "../permissions";
12
10
  import {createdUpdatedPlugin, type IsDeleted, isDeletedPlugin} from "../plugins";
@@ -69,6 +67,17 @@ routeBannerSchema.plugin(createdUpdatedPlugin);
69
67
  routeBannerSchema.plugin(syncPlugin);
70
68
  const RouteBannerModel = model<RouteBanner>("SyncRouteBanner", routeBannerSchema);
71
69
 
70
+ interface SnapshotEntity {
71
+ id: string;
72
+ seq: number;
73
+ data: {name: string};
74
+ }
75
+
76
+ interface MutationResult {
77
+ type: string;
78
+ ack: {seq: number};
79
+ }
80
+
72
81
  const authedOptions = {
73
82
  permissions: {
74
83
  create: [Permissions.IsAuthenticated],
@@ -77,7 +86,7 @@ const authedOptions = {
77
86
  read: [Permissions.IsAuthenticated],
78
87
  update: [Permissions.IsAuthenticated],
79
88
  },
80
- } as unknown as ModelRouterOptions<any>;
89
+ } as unknown as ModelRouterOptions<unknown>;
81
90
 
82
91
  const adminOnlyOptions = {
83
92
  permissions: {
@@ -87,7 +96,7 @@ const adminOnlyOptions = {
87
96
  read: [Permissions.IsAdmin],
88
97
  update: [Permissions.IsAdmin],
89
98
  },
90
- } as unknown as ModelRouterOptions<any>;
99
+ } as unknown as ModelRouterOptions<unknown>;
91
100
 
92
101
  // The shared test database can be dropped by another test file mid-suite
93
102
  // (configurationPlugin.test.ts drops it in an afterAll); rebuild the unique indexes the
@@ -116,13 +125,13 @@ describe("sync routes", () => {
116
125
  clearSyncRegistry();
117
126
  registerSync({
118
127
  config: {scope: {type: "owner"}},
119
- model: RouteStuffModel as any,
128
+ model: RouteStuffModel as unknown as Model<unknown>,
120
129
  options: authedOptions,
121
130
  routePath: "/routeStuff",
122
131
  });
123
132
  registerSync({
124
133
  config: {scope: {field: "orgId", type: "tenant"}},
125
- model: RouteProjectModel as any,
134
+ model: RouteProjectModel as unknown as Model<unknown>,
126
135
  options: authedOptions,
127
136
  routePath: "/routeProjects",
128
137
  });
@@ -136,8 +145,8 @@ describe("sync routes", () => {
136
145
  ]);
137
146
 
138
147
  app = getBaseServer();
139
- setupAuth(app as any, UserModel as any);
140
- addAuthRoutes(app as any, UserModel as any);
148
+ setupAuth(app, UserModel as unknown as AuthUserModel);
149
+ addAuthRoutes(app, UserModel as unknown as AuthUserModel);
141
150
  new SyncApp({
142
151
  getUserScopes: () => ["org1"],
143
152
  }).register(app);
@@ -193,7 +202,7 @@ describe("sync routes", () => {
193
202
  clearSyncRegistry();
194
203
  registerSync({
195
204
  config: {scope: {type: "owner"}},
196
- model: RouteStuffModel as any,
205
+ model: RouteStuffModel as unknown as Model<unknown>,
197
206
  options: adminOnlyOptions,
198
207
  routePath: "/routeStuff",
199
208
  });
@@ -217,8 +226,11 @@ describe("sync routes", () => {
217
226
  .expect(200);
218
227
  expect(res.body.stream).toBe(ownerStream());
219
228
  expect(res.body.entities).toHaveLength(2);
220
- expect(res.body.entities.map((e: any) => e.data.name)).toEqual(["mine 1", "mine 2"]);
221
- expect(res.body.entities.map((e: any) => e.seq)).toEqual([1, 2]);
229
+ expect(res.body.entities.map((e: SnapshotEntity) => e.data.name)).toEqual([
230
+ "mine 1",
231
+ "mine 2",
232
+ ]);
233
+ expect(res.body.entities.map((e: SnapshotEntity) => e.seq)).toEqual([1, 2]);
222
234
  expect(res.body.cursor).toBe(2);
223
235
  expect(res.body.hasMore).toBe(false);
224
236
  // C1/C7 response fields present.
@@ -244,8 +256,8 @@ describe("sync routes", () => {
244
256
  .get(`/sync/snapshot?stream=${encodeURIComponent(ownerStream())}&cursor=${cursor}`)
245
257
  .expect(200);
246
258
  expect(res.body.entities).toHaveLength(2);
247
- const updated = res.body.entities.find((e: any) => e.id === String(doc1._id));
248
- const tombstone = res.body.entities.find((e: any) => e.id === String(doc2._id));
259
+ const updated = res.body.entities.find((e: SnapshotEntity) => e.id === String(doc1._id));
260
+ const tombstone = res.body.entities.find((e: SnapshotEntity) => e.id === String(doc2._id));
249
261
  expect(updated.data.name).toBe("first updated");
250
262
  expect(updated.deleted).toBe(false);
251
263
  expect(tombstone.deleted).toBe(true);
@@ -276,7 +288,7 @@ describe("sync routes", () => {
276
288
  expect(page3.body.hasMore).toBe(false);
277
289
 
278
290
  const names = [...page1.body.entities, ...page2.body.entities, ...page3.body.entities].map(
279
- (e: any) => e.data.name
291
+ (e: SnapshotEntity) => e.data.name
280
292
  );
281
293
  expect(names).toEqual(["item 1", "item 2", "item 3", "item 4", "item 5"]);
282
294
  });
@@ -304,8 +316,8 @@ describe("sync routes", () => {
304
316
 
305
317
  it("500s for tenant collections when no getUserScopes resolver is configured", async () => {
306
318
  const bareApp = getBaseServer();
307
- setupAuth(bareApp as any, UserModel as any);
308
- addAuthRoutes(bareApp as any, UserModel as any);
319
+ setupAuth(bareApp, UserModel as unknown as AuthUserModel);
320
+ addAuthRoutes(bareApp, UserModel as unknown as AuthUserModel);
309
321
  new SyncApp().register(bareApp);
310
322
  const bareAgent = await authAsUser(bareApp, "notAdmin");
311
323
  await bareAgent.get("/sync/snapshot?stream=routeProjects%7Ctenant%3Aorg1").expect(500);
@@ -315,10 +327,10 @@ describe("sync routes", () => {
315
327
  clearSyncRegistry();
316
328
  registerSync({
317
329
  config: {
318
- responseHandler: (doc) => ({redactedName: `x-${(doc as any).name}`}),
330
+ responseHandler: (doc) => ({redactedName: `x-${(doc as RouteStuff).name}`}),
319
331
  scope: {type: "owner"},
320
332
  },
321
- model: RouteStuffModel as any,
333
+ model: RouteStuffModel as unknown as Model<unknown>,
322
334
  options: authedOptions,
323
335
  routePath: "/routeStuff",
324
336
  });
@@ -500,7 +512,7 @@ describe("sync routes", () => {
500
512
  clearSyncRegistry();
501
513
  registerSync({
502
514
  config: {scope: {type: "owner"}},
503
- model: RouteStuffModel as any,
515
+ model: RouteStuffModel as unknown as Model<unknown>,
504
516
  options: adminOnlyOptions,
505
517
  routePath: "/routeStuff",
506
518
  });
@@ -556,13 +568,13 @@ describe("sync routes", () => {
556
568
  clearSyncRegistry();
557
569
  registerSync({
558
570
  config: {scope: {type: "owner"}},
559
- model: RouteStuffModel as any,
571
+ model: RouteStuffModel as unknown as Model<unknown>,
560
572
  options: {
561
573
  ...authedOptions,
562
574
  preCreate: () => {
563
575
  throw new APIError({status: 500, title: "database exploded"});
564
576
  },
565
- } as unknown as ModelRouterOptions<any>,
577
+ } as unknown as ModelRouterOptions<unknown>,
566
578
  routePath: "/routeStuff",
567
579
  });
568
580
  const res = await agent
@@ -624,8 +636,8 @@ describe("sync routes", () => {
624
636
  const mutations = Array.from({length: 5}, (_v, i) => create(`batch-http-${i}`, `item ${i}`));
625
637
  const res = await agent.post("/sync/mutate/batch").send({mutations}).expect(200);
626
638
  expect(res.body.results).toHaveLength(5);
627
- expect(res.body.results.every((r: any) => r.type === "ack")).toBe(true);
628
- const seqs = res.body.results.map((r: any) => r.ack.seq);
639
+ expect(res.body.results.every((r: MutationResult) => r.type === "ack")).toBe(true);
640
+ const seqs = res.body.results.map((r: MutationResult) => r.ack.seq);
629
641
  expect(seqs).toEqual([1, 2, 3, 4, 5]);
630
642
  });
631
643
 
@@ -737,7 +749,7 @@ describe("sync routes", () => {
737
749
  const atCap = await agent
738
750
  .get(`/sync/entities?collection=routeStuff&ids=${ids.slice(0, MAX_ENTITY_FETCH).join(",")}`)
739
751
  .expect(200);
740
- expect(atCap.body.entities.map((e: any) => e.id)).toEqual([String(doc._id)]);
752
+ expect(atCap.body.entities.map((e: SnapshotEntity) => e.id)).toEqual([String(doc._id)]);
741
753
  });
742
754
  });
743
755
 
@@ -768,20 +780,23 @@ describe("sync routes", () => {
768
780
 
769
781
  /** Register the banner model for sync + REST with the given queryFilter and re-auth. */
770
782
  const setupBanners = async (
771
- queryFilter: ModelRouterOptions<any>["queryFilter"]
783
+ queryFilter: ModelRouterOptions<unknown>["queryFilter"]
772
784
  ): Promise<{bannerAgent: TestAgent}> => {
773
- const bannerOptions = {...authedOptions, queryFilter} as ModelRouterOptions<any>;
785
+ const bannerOptions = {...authedOptions, queryFilter} as ModelRouterOptions<unknown>;
774
786
  clearSyncRegistry();
775
787
  registerSync({
776
788
  config: {scope: {type: "broadcast"}},
777
- model: RouteBannerModel as any,
789
+ model: RouteBannerModel as unknown as Model<unknown>,
778
790
  options: bannerOptions,
779
791
  routePath: "/routeBanners",
780
792
  });
781
793
  const bannerApp = getBaseServer();
782
- setupAuth(bannerApp as any, UserModel as any);
783
- addAuthRoutes(bannerApp as any, UserModel as any);
784
- bannerApp.use("/routeBanners", modelRouter(RouteBannerModel as any, bannerOptions));
794
+ setupAuth(bannerApp, UserModel as unknown as AuthUserModel);
795
+ addAuthRoutes(bannerApp, UserModel as unknown as AuthUserModel);
796
+ bannerApp.use(
797
+ "/routeBanners",
798
+ modelRouter(RouteBannerModel as unknown as Model<unknown>, bannerOptions)
799
+ );
785
800
  new SyncApp().register(bannerApp);
786
801
  return {bannerAgent: await authAsUser(bannerApp, "notAdmin")};
787
802
  };
@@ -796,13 +811,13 @@ describe("sync routes", () => {
796
811
  await RouteBannerModel.create({name: "theirs", ownerId: "someoneElse"});
797
812
 
798
813
  const rest = await bannerAgent.get("/routeBanners").expect(200);
799
- const restIds = rest.body.data.map((d: any) => String(d._id));
814
+ const restIds = rest.body.data.map((d: RouteBanner) => String(d._id));
800
815
  expect(restIds).toEqual([String(mine._id)]);
801
816
 
802
817
  const snapshot = await bannerAgent
803
818
  .get(`/sync/snapshot?stream=${encodeURIComponent(bannerStream)}`)
804
819
  .expect(200);
805
- expect(snapshot.body.entities.map((e: any) => e.id)).toEqual(restIds);
820
+ expect(snapshot.body.entities.map((e: SnapshotEntity) => e.id)).toEqual(restIds);
806
821
  });
807
822
 
808
823
  it("filters GET /sync/entities by the queryFilter", async () => {
@@ -852,7 +867,7 @@ describe("sync routes", () => {
852
867
  scope: {type: "owner"},
853
868
  snapshotFilter: () => ({name: "keep"}),
854
869
  },
855
- model: RouteStuffModel as any,
870
+ model: RouteStuffModel as unknown as Model<unknown>,
856
871
  options: authedOptions,
857
872
  routePath: "/routeStuff",
858
873
  });
@@ -862,7 +877,7 @@ describe("sync routes", () => {
862
877
  const res = await agent
863
878
  .get(`/sync/snapshot?stream=${encodeURIComponent(`routeStuff|owner:${notAdminId}`)}`)
864
879
  .expect(200);
865
- expect(res.body.entities.map((e: any) => e.data.name)).toEqual(["keep"]);
880
+ expect(res.body.entities.map((e: SnapshotEntity) => e.data.name)).toEqual(["keep"]);
866
881
  });
867
882
  });
868
883
  });
@@ -1,6 +1,5 @@
1
- // noExplicitAny: Schema/Query generics must be loose to accept arbitrary consumer schemas
2
- // biome-ignore-all lint/suspicious/noExplicitAny: Schema/Query generics must be loose to accept arbitrary consumer schemas
3
- import type {ClientSession, Model, Query, Schema} from "mongoose";
1
+ import type {ClientSession, Query, Schema} from "mongoose";
2
+ import {APIError} from "../errors";
4
3
  import {logger} from "../logger";
5
4
  import {claimSyncSeqs, confirmSyncSeqs, releaseSyncSeqs, SyncScopeMove} from "./models";
6
5
  import {findSyncEntryByModelName, type SyncRegistryEntry} from "./registry";
@@ -222,6 +221,17 @@ const writeScopeMoveMarker = async ({
222
221
  }
223
222
  };
224
223
 
224
+ /** The subset of a Mongoose model the raw-collection re-stamp write needs. */
225
+ interface RestampableModel {
226
+ modelName: string;
227
+ collection: {
228
+ updateOne(
229
+ filter: Record<string, unknown>,
230
+ update: Record<string, unknown>
231
+ ): Promise<{matchedCount?: number}>;
232
+ };
233
+ }
234
+
225
235
  /**
226
236
  * Task 9.17: the confirm found no pending entry, meaning this writer's claim was reaped as
227
237
  * abandoned (it stalled past {@link PENDING_CLAIM_LEASE_MS} without crashing) and the
@@ -239,7 +249,7 @@ const restampReapedSeq = async ({
239
249
  stream,
240
250
  reapedSeq,
241
251
  }: {
242
- model: Model<any>;
252
+ model: RestampableModel;
243
253
  /** The document's RAW `_id` — the native driver does no casting, so a stringified
244
254
  * ObjectId would match nothing and silently leave the doc below the frontier. */
245
255
  id: unknown;
@@ -253,10 +263,7 @@ const restampReapedSeq = async ({
253
263
  );
254
264
  try {
255
265
  const claim = await claimSyncSeqs({stream});
256
- const result = await model.collection.updateOne(
257
- {_id: id as any},
258
- {$set: {_syncSeq: claim.lastSeq}}
259
- );
266
+ const result = await model.collection.updateOne({_id: id}, {$set: {_syncSeq: claim.lastSeq}});
260
267
  if ((result.matchedCount ?? 0) === 0) {
261
268
  logger.error("[sync] Re-stamp matched no document; releasing the re-stamp claim", {
262
269
  entityId: String(id),
@@ -287,16 +294,18 @@ const restampReapedSeq = async ({
287
294
  }
288
295
  };
289
296
 
290
- export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
297
+ export const syncPlugin = (schema: Schema): void => {
291
298
  // Task 9.13: `doc.save()` must be conditional on the version the document was loaded at,
292
299
  // so the `baseVersion` comparison in executeUpdate cannot be defeated by a concurrent
293
300
  // save landing between the read and the write.
294
301
  if (!schema.options.versionKey) {
295
- throw new Error(
296
- "syncPlugin requires a versionKey on the schema: sync relies on Mongoose " +
302
+ throw new APIError({
303
+ status: 500,
304
+ title:
305
+ "syncPlugin requires a versionKey on the schema: sync relies on Mongoose " +
297
306
  "optimisticConcurrency to make the baseVersion conflict check atomic. " +
298
- "Remove `versionKey: false` from the schema options."
299
- );
307
+ "Remove `versionKey: false` from the schema options.",
308
+ });
300
309
  }
301
310
  schema.set("optimisticConcurrency", true);
302
311
 
@@ -318,7 +327,9 @@ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
318
327
  // Capture the stream the document belonged to when it was loaded, so scope moves can
319
328
  // be detected at save time without re-querying.
320
329
  schema.post("init", function () {
321
- const entry = findSyncEntryByModelName((this.constructor as Model<any>).modelName);
330
+ const entry = findSyncEntryByModelName(
331
+ (this.constructor as unknown as {modelName: string}).modelName
332
+ );
322
333
  if (!entry) {
323
334
  return;
324
335
  }
@@ -338,7 +349,9 @@ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
338
349
  }
339
350
 
340
351
  schema.pre("save", async function () {
341
- const entry = findSyncEntryByModelName((this.constructor as Model<any>).modelName);
352
+ const entry = findSyncEntryByModelName(
353
+ (this.constructor as unknown as {modelName: string}).modelName
354
+ );
342
355
  if (!entry) {
343
356
  return;
344
357
  }
@@ -409,7 +422,7 @@ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
409
422
  // running, so the frontier already passed the seq we just committed.
410
423
  const restamped = await restampReapedSeq({
411
424
  id: this._id,
412
- model: this.constructor as Model<any>,
425
+ model: this.constructor as unknown as RestampableModel,
413
426
  reapedSeq: pending.claim.seqs[pending.claim.seqs.length - 1],
414
427
  stream: pending.currentStream,
415
428
  });
@@ -446,8 +459,11 @@ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
446
459
  // Keep arity ≥ 3 so Kareem still classifies this as error middleware.
447
460
  schema.post(
448
461
  "save",
449
- async function (error: any, doc: any, _next: (err?: Error) => void): Promise<void> {
450
- const locals = (this?.$locals ?? doc?.$locals) as Record<string, unknown> | undefined;
462
+ async function (error: unknown, doc: unknown, _next: (err?: Error) => void): Promise<void> {
463
+ const locals = (this?.$locals ??
464
+ (doc as {$locals?: Record<string, unknown>} | undefined)?.$locals) as
465
+ | Record<string, unknown>
466
+ | undefined;
451
467
  const pending = locals?.[PENDING_COMMIT_KEY] as PendingSaveCommit | undefined;
452
468
  if (locals) {
453
469
  locals[PENDING_COMMIT_KEY] = undefined;
@@ -469,11 +485,11 @@ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
469
485
  // Mongoose 9 insertMany pre hooks are async (no `next` callback) and receive docs + options.
470
486
  schema.pre(
471
487
  "insertMany",
472
- async function (docs: any | Array<any>, options?: {session?: ClientSession | null}) {
488
+ async function (docs: unknown, options?: {session?: ClientSession | null}) {
473
489
  const docsArray = Array.isArray(docs)
474
490
  ? (docs as Record<string, unknown>[])
475
491
  : [docs as Record<string, unknown>];
476
- const model = this as unknown as Model<any>;
492
+ const model = this as unknown as {modelName: string};
477
493
  const entry = findSyncEntryByModelName(model.modelName);
478
494
  if (!entry || docsArray.length === 0) {
479
495
  return;
@@ -509,22 +525,21 @@ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
509
525
  return;
510
526
  }
511
527
  const modelName = ((docs[0] as {constructor?: {modelName?: string}})?.constructor?.modelName ??
512
- (this as unknown as Model<any>).modelName) as string | undefined;
528
+ (this as unknown as {modelName?: string}).modelName) as string | undefined;
513
529
  const entry = modelName ? findSyncEntryByModelName(modelName) : undefined;
514
530
  if (!entry) {
515
531
  return;
516
532
  }
517
533
  const seqsByStream = new Map<string, number[]>();
518
- for (const doc of docs as Record<string, any>[]) {
534
+ for (const doc of docs as Array<
535
+ Record<string, unknown> & {$session?: () => unknown; toObject?: () => Record<string, unknown>}
536
+ >) {
519
537
  // A session-backed claim registered nothing to confirm, and a `$pull` issued outside
520
538
  // the caller's still-open transaction would conflict with its counter write.
521
539
  if (typeof doc?.$session === "function" && doc.$session()) {
522
540
  continue;
523
541
  }
524
- const obj = (typeof doc?.toObject === "function" ? doc.toObject() : doc) as Record<
525
- string,
526
- unknown
527
- >;
542
+ const obj = typeof doc?.toObject === "function" ? doc.toObject() : doc;
528
543
  const seq = obj._syncSeq;
529
544
  if (typeof seq !== "number") {
530
545
  continue;
@@ -547,8 +562,18 @@ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
547
562
 
548
563
  // Single-document query writes: fetch the target to resolve its stream (and detect
549
564
  // scope moves), claim a seq, and merge the stamp into the update.
550
- const preQueryWrite = async function (this: Query<any, any>): Promise<void> {
551
- const model = this.model as Model<any>;
565
+ /** What a query-write's post hook must finish once the write commits. */
566
+ interface PendingQueryConfirm {
567
+ claim: {registered: boolean; seqs: number[]};
568
+ currentStream: string;
569
+ entityId: string;
570
+ entry: SyncRegistryEntry;
571
+ prevStream: string | null;
572
+ session: ClientSession | null;
573
+ }
574
+
575
+ const preQueryWrite = async function (this: Query<unknown, never>): Promise<void> {
576
+ const model = this.model;
552
577
  const entry = findSyncEntryByModelName(model.modelName);
553
578
  if (!entry) {
554
579
  return;
@@ -582,7 +607,7 @@ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
582
607
  const targetObj = target.toObject() as Record<string, unknown>;
583
608
  const previousStream = streamForObject(entry, targetObj);
584
609
 
585
- const rawUpdate = (this.getUpdate() ?? {}) as Record<string, any>;
610
+ const rawUpdate = (this.getUpdate() ?? {}) as Record<string, unknown>;
586
611
  // Only replaceOne/findOneAndReplace replace the document. A plain object passed to
587
612
  // updateOne/findOneAndUpdate is an IMPLICIT $set in Mongoose — treating it as a
588
613
  // replacement would resolve the scope value as undefined and claim the wrong stream.
@@ -653,7 +678,7 @@ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
653
678
  }
654
679
  this.setUpdate(rawUpdate);
655
680
  // Stash the claim + move info so the query post hook confirms/records after commit.
656
- (this as unknown as {_syncPendingConfirm?: unknown})._syncPendingConfirm = {
681
+ (this as unknown as {_syncPendingConfirm?: PendingQueryConfirm})._syncPendingConfirm = {
657
682
  claim,
658
683
  currentStream,
659
684
  entityId: String(target._id),
@@ -665,20 +690,15 @@ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
665
690
 
666
691
  // C1/C4: after the query write commits, confirm the claimed seq and record the
667
692
  // scope-move marker. Query post middleware `this` is the Query (not a document).
668
- const postQueryWrite = async function (this: Query<any, any>): Promise<void> {
669
- const pending = (this as unknown as {_syncPendingConfirm?: any})._syncPendingConfirm;
693
+ const postQueryWrite = async function (this: Query<unknown, never>): Promise<void> {
694
+ const pending = (this as unknown as {_syncPendingConfirm?: PendingQueryConfirm})
695
+ ._syncPendingConfirm;
670
696
  if (!pending) {
671
697
  return;
672
698
  }
673
- (this as unknown as {_syncPendingConfirm?: unknown})._syncPendingConfirm = undefined;
674
- const {claim, currentStream, entityId, entry, prevStream, session} = pending as {
675
- claim: {registered: boolean; seqs: number[]};
676
- currentStream: string;
677
- entityId: string;
678
- entry: SyncRegistryEntry;
679
- prevStream: string | null;
680
- session: ClientSession | null;
681
- };
699
+ (this as unknown as {_syncPendingConfirm?: PendingQueryConfirm})._syncPendingConfirm =
700
+ undefined;
701
+ const {claim, currentStream, entityId, entry, prevStream, session} = pending;
682
702
  if (claim.registered) {
683
703
  await confirmSyncSeqs({seqs: claim.seqs, stream: currentStream}).catch((error: unknown) => {
684
704
  logger.error("[sync] Failed to confirm seq after query write", {
@@ -711,8 +731,8 @@ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
711
731
 
712
732
  // Unsupported multi-document / hard-delete paths throw for registered models.
713
733
  const guardQuery = (operation: string) =>
714
- function (this: Query<any, any>): void {
715
- const model = this.model as Model<any>;
734
+ function (this: Query<unknown, never>): void {
735
+ const model = this.model;
716
736
  if (findSyncEntryByModelName(model.modelName)) {
717
737
  throw unsupportedWrite(model.modelName, operation);
718
738
  }
@@ -722,7 +742,9 @@ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
722
742
  schema.pre("deleteOne", {document: false, query: true}, guardQuery("deleteOne"));
723
743
  schema.pre("findOneAndDelete", guardQuery("findOneAndDelete"));
724
744
  schema.pre("deleteOne", {document: true, query: false}, function () {
725
- const entry = findSyncEntryByModelName((this.constructor as Model<any>).modelName);
745
+ const entry = findSyncEntryByModelName(
746
+ (this.constructor as unknown as {modelName: string}).modelName
747
+ );
726
748
  if (entry) {
727
749
  throw unsupportedWrite(entry.modelName, "document deleteOne (hard delete)");
728
750
  }