@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
@@ -185,6 +185,7 @@ export const executeCreate = async <T>({
185
185
  body,
186
186
  req,
187
187
  skipPostHooks,
188
+ writeModelName,
188
189
  }: {
189
190
  model: Model<T>;
190
191
  options: ModelRouterOptions<T>;
@@ -192,6 +193,11 @@ export const executeCreate = async <T>({
192
193
  body: unknown;
193
194
  /** The real Express request when called over HTTP; hooks receive a `{user}` stub otherwise. */
194
195
  req?: express.Request;
196
+ /**
197
+ * Name used for User-role stripping after hooks. Defaults to `model.modelName`.
198
+ * MCP passes the registry `modelName` because compiled mongoose names can differ.
199
+ */
200
+ writeModelName?: string;
195
201
  /**
196
202
  * C5 (FIX 6): when true, skip the built-in `postCreate` call — the caller
197
203
  * (the sync mutation handler) runs it manually AFTER finalizing the
@@ -245,7 +251,7 @@ export const executeCreate = async <T>({
245
251
  }
246
252
  }
247
253
  cleanedBody = omitUserRolesFromWriteBody(
248
- model.modelName,
254
+ writeModelName ?? model.modelName,
249
255
  options.accessControl,
250
256
  cleanedBody,
251
257
  (request as express.Request & {terrenoAllowUserAdminWrite?: boolean})
@@ -277,11 +283,11 @@ export const executeCreate = async <T>({
277
283
 
278
284
  if (options.populatePaths) {
279
285
  try {
280
- // noExplicitAny: mongoose Query type varies based on populatePaths
281
- // biome-ignore lint/suspicious/noExplicitAny: mongoose Query type varies based on populatePaths
282
- let populateQuery: any = model.findById(data._id);
283
- populateQuery = addPopulateToQuery(populateQuery, options.populatePaths);
284
- data = await populateQuery.exec();
286
+ const populateQuery = addPopulateToQuery(
287
+ model.findById(data._id) as unknown as Parameters<typeof addPopulateToQuery>[0],
288
+ options.populatePaths
289
+ );
290
+ data = (await populateQuery.exec()) as unknown as ExecutorDoc<T>;
285
291
  } catch (error: unknown) {
286
292
  throw passthroughOrWrap(error, {
287
293
  code: "populate-error",
@@ -343,6 +349,7 @@ export const executeUpdate = async <T>({
343
349
  existingDoc,
344
350
  req,
345
351
  skipPostHooks,
352
+ writeModelName,
346
353
  }: {
347
354
  model: Model<T>;
348
355
  options: ModelRouterOptions<T>;
@@ -359,6 +366,11 @@ export const executeUpdate = async <T>({
359
366
  req?: express.Request;
360
367
  /** C5 (FIX 6): see `executeCreate`'s `skipPostHooks` doc comment. */
361
368
  skipPostHooks?: boolean;
369
+ /**
370
+ * Name used for User-role stripping after hooks. Defaults to `model.modelName`.
371
+ * MCP passes the registry `modelName` because compiled mongoose names can differ.
372
+ */
373
+ writeModelName?: string;
362
374
  }): Promise<ExecutorResult<T>> => {
363
375
  const request = req ?? stubRequest(user);
364
376
 
@@ -428,7 +440,7 @@ export const executeUpdate = async <T>({
428
440
  }
429
441
 
430
442
  cleanedBody = omitUserRolesFromWriteBody(
431
- model.modelName,
443
+ writeModelName ?? model.modelName,
432
444
  options.accessControl,
433
445
  cleanedBody,
434
446
  (request as express.Request & {terrenoAllowUserAdminWrite?: boolean})
@@ -536,11 +548,11 @@ export const executeUpdate = async <T>({
536
548
  }
537
549
 
538
550
  if (options.populatePaths) {
539
- // noExplicitAny: mongoose Query type varies based on populatePaths
540
- // biome-ignore lint/suspicious/noExplicitAny: mongoose Query type varies based on populatePaths
541
- let populateQuery: any = model.findById(doc._id);
542
- populateQuery = addPopulateToQuery(populateQuery, options.populatePaths);
543
- doc = await populateQuery.exec();
551
+ const populateQuery = addPopulateToQuery(
552
+ model.findById(doc._id) as unknown as Parameters<typeof addPopulateToQuery>[0],
553
+ options.populatePaths
554
+ );
555
+ doc = (await populateQuery.exec()) as unknown as ExecutorDoc<T>;
544
556
  }
545
557
 
546
558
  if (options.postUpdate && !skipPostHooks) {
@@ -1,5 +1,3 @@
1
- // noExplicitAny: the rig bridges generic model/router/test types
2
- // biome-ignore-all lint/suspicious/noExplicitAny: the rig bridges generic model/router/test types
3
1
  /**
4
2
  * End-to-end integration test (Task 5.5): a REAL @terreno/syncdb client speaking to a
5
3
  * REAL @terreno/api backend over HTTP + Socket.io.
@@ -38,7 +36,13 @@ import {
38
36
  import mongoose, {model, Schema} from "mongoose";
39
37
 
40
38
  import {modelRouter} from "../api";
41
- import {addAuthRoutes, generateTokens, setupAuth, type User} from "../auth";
39
+ import {
40
+ type UserModel as AuthUserModel,
41
+ addAuthRoutes,
42
+ generateTokens,
43
+ setupAuth,
44
+ type User,
45
+ } from "../auth";
42
46
  import {Permissions} from "../permissions";
43
47
  import {createdUpdatedPlugin, findOneOrNoneFor, type IsDeleted, isDeletedPlugin} from "../plugins";
44
48
  import {RealtimeApp} from "../realtime/realtimeApp";
@@ -207,8 +211,8 @@ describe("syncdb end-to-end integration", () => {
207
211
  });
208
212
 
209
213
  const app = getBaseServer();
210
- setupAuth(app as any, UserModel as any);
211
- addAuthRoutes(app as any, UserModel as any);
214
+ setupAuth(app, UserModel as unknown as AuthUserModel);
215
+ addAuthRoutes(app, UserModel as unknown as AuthUserModel);
212
216
  new SyncApp({}).register(app);
213
217
  app.use(registration.path, registration.router);
214
218
 
@@ -440,7 +444,9 @@ describe("syncdb end-to-end integration", () => {
440
444
  expect(entity?.data?.title).toBe("server wins");
441
445
  expect(entity?.seq).toBe(baseSeq + 1);
442
446
  expect(entity?.pendingMutationId).toBeUndefined();
443
- expect(a.client.getSyncStatus().conflictCount).toBe(0);
447
+ await waitFor(() => a.client.getSyncStatus().conflictCount === 0, {
448
+ label: "conflict cleared after persistence flush",
449
+ });
444
450
  // The server kept its own write.
445
451
  const settled = await IntTodoModel.findById(conflictDocId);
446
452
  expect(settled?.title).toBe("server wins");
@@ -481,7 +487,9 @@ describe("syncdb end-to-end integration", () => {
481
487
  // (it would replay the recorded conflict nack forever) — and resolveConflict kicks
482
488
  // a replay immediately, so the retry drains without waiting for another trigger.
483
489
  a.client.resolveConflict({mutationId, strategy: "keepMine"});
484
- expect(a.client.getSyncStatus().conflictCount).toBe(0);
490
+ await waitFor(() => a.client.getSyncStatus().conflictCount === 0, {
491
+ label: "conflict cleared after persistence flush",
492
+ });
485
493
  expect(a.client.outbox.getMutation({mutationId})).toBeUndefined();
486
494
 
487
495
  // ...and the replay applies the client's data server-side.
@@ -36,11 +36,13 @@ import {
36
36
  type SyncTransport,
37
37
  } from "@terreno/syncdb";
38
38
  import express from "express";
39
+ import {DateTime} from "luxon";
39
40
  import mongoose, {model, Schema} from "mongoose";
40
41
  import passportLocalMongoose from "passport-local-mongoose";
41
42
 
42
43
  import {modelRouter} from "../api";
43
44
  import {type UserModel as AuthUserModel, addAuthRoutes, generateTokens, setupAuth} from "../auth";
45
+ import {APIError} from "../errors";
44
46
  import {logger} from "../logger";
45
47
  import {Permissions} from "../permissions";
46
48
  import {createdUpdatedPlugin, type IsDeleted, isDeletedPlugin} from "../plugins";
@@ -156,14 +158,17 @@ const waitFor = async (
156
158
  intervalMs?: number;
157
159
  } = {}
158
160
  ): Promise<void> => {
159
- const start = Date.now();
160
- while (Date.now() - start < timeoutMs) {
161
+ const start = DateTime.now().toMillis();
162
+ while (DateTime.now().toMillis() - start < timeoutMs) {
161
163
  if (await predicate()) {
162
164
  return;
163
165
  }
164
166
  await new Promise((resolve) => setTimeout(resolve, intervalMs));
165
167
  }
166
- throw new Error(`Timed out after ${timeoutMs}ms waiting for ${label}`);
168
+ throw new APIError({
169
+ status: 500,
170
+ title: `Timed out after ${timeoutMs}ms waiting for ${label}`,
171
+ });
167
172
  };
168
173
 
169
174
  interface HarnessClient {
@@ -327,7 +332,7 @@ const main = async (): Promise<void> => {
327
332
  return {ownerId: owner.ownerId, title: `seed-${i}`};
328
333
  });
329
334
 
330
- const bootstrapStart = Date.now();
335
+ const bootstrapStart = DateTime.now().toMillis();
331
336
  if (seeds.length > 0) {
332
337
  await LoadTodoModel.insertMany(seeds);
333
338
  }
@@ -340,7 +345,7 @@ const main = async (): Promise<void> => {
340
345
  }),
341
346
  {label: `bootstrap convergence at ${config.seedDocs} docs`, timeoutMs: 120_000}
342
347
  );
343
- const bootstrapWallMs = Date.now() - bootstrapStart;
348
+ const bootstrapWallMs = DateTime.now().toMillis() - bootstrapStart;
344
349
  logger.info(`[loadHarness] bootstrap converged in ${bootstrapWallMs}ms`);
345
350
 
346
351
  // ── Driven mutation load ─────────────────────────────────────────────────
@@ -370,7 +375,7 @@ const main = async (): Promise<void> => {
370
375
  },
371
376
  {intervalMs: 8, label: `mutation ${mutationId} terminal`, timeoutMs: 15_000}
372
377
  );
373
- mutateLatenciesMs.push(Date.now() - startedAt);
378
+ mutateLatenciesMs.push(DateTime.now().toMillis() - startedAt);
374
379
  const mutation = hc.client.outbox.getMutation({mutationId});
375
380
  if (mutation?.status === "conflicted") {
376
381
  conflictCount += 1;
@@ -381,7 +386,7 @@ const main = async (): Promise<void> => {
381
386
  }
382
387
  } catch {
383
388
  // Timed out waiting; still record whatever elapsed so the report reflects it.
384
- mutateLatenciesMs.push(Date.now() - startedAt);
389
+ mutateLatenciesMs.push(DateTime.now().toMillis() - startedAt);
385
390
  }
386
391
  };
387
392
 
@@ -397,18 +402,20 @@ const main = async (): Promise<void> => {
397
402
 
398
403
  const loadPromises: Promise<void>[] = [];
399
404
  const intervalMs = 1000 / config.targetRate;
400
- const loadEnd = Date.now() + config.durationSec * 1000;
405
+ const loadEnd = DateTime.now().plus({seconds: config.durationSec}).toMillis();
401
406
 
402
- while (Date.now() < loadEnd) {
407
+ while (DateTime.now().toMillis() < loadEnd) {
403
408
  const hc = randomClient();
404
409
  const opRoll = Math.random();
405
410
 
406
411
  if (opRoll < 0.5 || knownEntityIds.length === 0) {
407
412
  // Create.
408
- const startedAt = Date.now();
413
+ const startedAt = DateTime.now().toMillis();
409
414
  const {mutationId} = hc.client.mutate({
410
415
  collection: COLLECTION,
411
- data: {title: `load-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`},
416
+ data: {
417
+ title: `load-${DateTime.now().toMillis()}-${Math.random().toString(36).slice(2, 8)}`,
418
+ },
412
419
  operation: "create",
413
420
  });
414
421
  loadPromises.push(trackMutation(hc, mutationId, startedAt));
@@ -416,10 +423,10 @@ const main = async (): Promise<void> => {
416
423
  // Update.
417
424
  const target = knownEntityIds[Math.floor(Math.random() * knownEntityIds.length)];
418
425
  const owner = harnessClients[target.clientIndex];
419
- const startedAt = Date.now();
426
+ const startedAt = DateTime.now().toMillis();
420
427
  const {mutationId} = owner.client.mutate({
421
428
  collection: COLLECTION,
422
- data: {title: `updated-${Date.now()}`},
429
+ data: {title: `updated-${DateTime.now().toMillis()}`},
423
430
  id: target.id,
424
431
  operation: "update",
425
432
  });
@@ -435,7 +442,7 @@ const main = async (): Promise<void> => {
435
442
  if (!doc) {
436
443
  return;
437
444
  }
438
- doc.title = `server-race-${Date.now()}`;
445
+ doc.title = `server-race-${DateTime.now().toMillis()}`;
439
446
  return doc.save();
440
447
  })
441
448
  .catch(() => {});
@@ -448,8 +455,10 @@ const main = async (): Promise<void> => {
448
455
  if (Math.random() < 0.05) {
449
456
  const dupRequest: SyncMutateRequest = {
450
457
  collection: COLLECTION,
451
- data: {title: `dup-${Date.now()}`},
452
- mutationId: `load-dup-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
458
+ data: {title: `dup-${DateTime.now().toMillis()}`},
459
+ mutationId: `load-dup-${DateTime.now().toMillis()}-${Math.random()
460
+ .toString(36)
461
+ .slice(2, 8)}`,
453
462
  operation: "create",
454
463
  };
455
464
  loadPromises.push(
@@ -467,7 +476,7 @@ const main = async (): Promise<void> => {
467
476
  const idx = Math.floor(Math.random() * knownEntityIds.length);
468
477
  const target = knownEntityIds[idx];
469
478
  const owner = harnessClients[target.clientIndex];
470
- const startedAt = Date.now();
479
+ const startedAt = DateTime.now().toMillis();
471
480
  const {mutationId} = owner.client.mutate({
472
481
  collection: COLLECTION,
473
482
  id: target.id,
@@ -481,7 +490,7 @@ const main = async (): Promise<void> => {
481
490
  // connected client's local store reflecting it ─────────────────────────────
482
491
  if (Math.random() < 0.02) {
483
492
  const anyOwner = randomClient();
484
- const writeStart = Date.now();
493
+ const writeStart = DateTime.now().toMillis();
485
494
  const doc = await LoadTodoModel.create({
486
495
  ownerId: anyOwner.ownerId,
487
496
  title: `fanout-${writeStart}`,
@@ -501,7 +510,7 @@ const main = async (): Promise<void> => {
501
510
  }
502
511
  )
503
512
  .then(() => {
504
- fanoutLagsMs.push(Date.now() - writeStart);
513
+ fanoutLagsMs.push(DateTime.now().toMillis() - writeStart);
505
514
  })
506
515
  .catch(() => {})
507
516
  );
@@ -1,5 +1,7 @@
1
1
  import crypto from "node:crypto";
2
+ import {DateTime} from "luxon";
2
3
  import mongoose, {type ClientSession, type Model, Schema} from "mongoose";
4
+ import {APIError, errorDetail} from "../errors";
3
5
  import {logger} from "../logger";
4
6
  import {findOneOrNoneFor} from "../plugins";
5
7
 
@@ -46,7 +48,7 @@ export const PENDING_CLAIM_LEASE_MS = 60 * 1_000;
46
48
  const syncPendingClaimSchema = new Schema<SyncPendingClaim>(
47
49
  {
48
50
  claimedAt: {
49
- default: () => new Date(),
51
+ default: () => DateTime.now().toJSDate(),
50
52
  description: "When this seq was claimed; a claim older than PENDING_CLAIM_LEASE_MS is stale",
51
53
  type: Date,
52
54
  },
@@ -155,7 +157,7 @@ export const claimSyncSeqs = async ({
155
157
  }
156
158
 
157
159
  const claim = async (): Promise<SyncSeqClaim> => {
158
- const now = new Date();
160
+ const now = DateTime.now().toJSDate();
159
161
  // Claim (`$inc`) AND register the pending entries in ONE atomic aggregation-pipeline
160
162
  // update, so there is never a window where the incremented head is visible without
161
163
  // its pending claims (which would let `computeStableFrontier` transiently report a
@@ -290,7 +292,7 @@ export const releaseSyncSeqs = async ({
290
292
  * opportunistically so a crash cannot freeze the frontier forever.
291
293
  */
292
294
  export const computeStableFrontier = async ({stream}: {stream: string}): Promise<number> => {
293
- const counter = await SyncCounter.findOne({stream});
295
+ const counter = await findOneOrNoneFor(SyncCounter, {stream});
294
296
  if (!counter) {
295
297
  return 0;
296
298
  }
@@ -298,11 +300,11 @@ export const computeStableFrontier = async ({stream}: {stream: string}): Promise
298
300
  if (pending.length === 0) {
299
301
  return counter.seq;
300
302
  }
301
- const cutoff = Date.now() - PENDING_CLAIM_LEASE_MS;
303
+ const cutoff = DateTime.now().minus({milliseconds: PENDING_CLAIM_LEASE_MS}).toMillis();
302
304
  const staleSeqs: number[] = [];
303
305
  let minLiveSeq = Number.POSITIVE_INFINITY;
304
306
  for (const claim of pending) {
305
- const claimedAtMs = new Date(claim.claimedAt).getTime();
307
+ const claimedAtMs = DateTime.fromJSDate(claim.claimedAt).toMillis();
306
308
  if (claimedAtMs < cutoff) {
307
309
  staleSeqs.push(claim.seq);
308
310
  continue;
@@ -368,7 +370,7 @@ const syncScopeMoveSchema = new Schema<SyncScopeMoveDocument>(
368
370
  type: String,
369
371
  },
370
372
  created: {
371
- default: () => new Date(),
373
+ default: () => DateTime.now().toJSDate(),
372
374
  description:
373
375
  "When the move was recorded; reaped by compactTombstones once older than the " +
374
376
  "owning model's retentionDays (deliberately NOT TTL-indexed — see the model doc)",
@@ -456,14 +458,14 @@ export const SYNC_MUTATION_LEASE_MS = 60 * 1_000;
456
458
  const syncMutationSchema = new Schema<SyncMutationDocument>(
457
459
  {
458
460
  claimedAt: {
459
- default: () => new Date(),
461
+ default: () => DateTime.now().toJSDate(),
460
462
  description:
461
463
  "When this delivery claimed the mutation (lease); a pending row older than " +
462
464
  "SYNC_MUTATION_LEASE_MS may be taken over by a fresh delivery via findOneAndUpdate",
463
465
  type: Date,
464
466
  },
465
467
  created: {
466
- default: () => new Date(),
468
+ default: () => DateTime.now().toJSDate(),
467
469
  description: "When the mutation was first claimed; TTL-indexed so rows expire after 30 days",
468
470
  type: Date,
469
471
  },
@@ -536,7 +538,7 @@ export interface SyncKeyDocument {
536
538
  const syncKeySchema = new Schema<SyncKeyDocument>(
537
539
  {
538
540
  created: {
539
- default: () => new Date(),
541
+ default: () => DateTime.now().toJSDate(),
540
542
  description: "When this key material was generated",
541
543
  type: Date,
542
544
  },
@@ -568,9 +570,17 @@ export const SyncKey: Model<SyncKeyDocument> =
568
570
  * builds them when Mongoose `autoIndex` is on — commonly disabled in production — so
569
571
  * startup builds them explicitly.
570
572
  */
571
- // noExplicitAny: a heterogeneous list of models is only used for ensureIndexes
572
- // biome-ignore lint/suspicious/noExplicitAny: a heterogeneous list of models is only used for ensureIndexes
573
- const SYNC_BOOKKEEPING_MODELS: Model<any>[] = [SyncCounter, SyncMutation, SyncScopeMove, SyncKey];
573
+ interface IndexableModel {
574
+ ensureIndexes: () => Promise<void>;
575
+ modelName: string;
576
+ }
577
+
578
+ const SYNC_BOOKKEEPING_MODELS: IndexableModel[] = [
579
+ SyncCounter,
580
+ SyncMutation,
581
+ SyncScopeMove,
582
+ SyncKey,
583
+ ];
574
584
 
575
585
  /**
576
586
  * Best-effort removal of the pre-Task-9.15 `SyncScopeMove` TTL index. Failure is only
@@ -614,11 +624,15 @@ export const ensureSyncModelIndexes = async (): Promise<void> => {
614
624
  logger.error(`[sync] Failed to ensure sync indexes for ${model.modelName}`, {
615
625
  error: String(error),
616
626
  });
617
- throw new Error(
618
- `Failed to ensure sync indexes for ${model.modelName}: ${String(error)}. ` +
619
- "The sync protocol depends on these indexes (unique mutationId for mutation " +
620
- "idempotency, unique stream for seq allocation); fix the DB and restart."
621
- );
627
+ throw new APIError({
628
+ cause: error,
629
+ code: "sync-indexes-failed",
630
+ detail:
631
+ `${errorDetail(error)}. The sync protocol depends on these indexes (unique mutationId for ` +
632
+ "mutation idempotency, unique stream for seq allocation); fix the DB and restart.",
633
+ status: 500,
634
+ title: `Failed to ensure sync indexes for ${model.modelName}`,
635
+ });
622
636
  }
623
637
  })
624
638
  );
@@ -634,7 +648,7 @@ export const getOrCreateSyncKeyMaterial = async ({userId}: {userId: string}): Pr
634
648
  const candidate = crypto.randomBytes(32).toString("base64");
635
649
  const doc = await SyncKey.findOneAndUpdate(
636
650
  {userId},
637
- {$setOnInsert: {created: new Date(), keyMaterial: candidate, userId}},
651
+ {$setOnInsert: {created: DateTime.now().toJSDate(), keyMaterial: candidate, userId}},
638
652
  {new: true, upsert: true}
639
653
  );
640
654
  return doc.keyMaterial;
@@ -1,5 +1,3 @@
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 mongoose, {model, Schema} from "mongoose";
5
3
  import type {ModelRouterOptions} from "../api";
@@ -18,7 +16,7 @@ import {
18
16
  } from "./mutationHandler";
19
17
  import {clearSyncRegistry, registerSync} from "./registry";
20
18
  import {syncPlugin} from "./syncSeqPlugin";
21
- import type {SyncMutateBatchResponse, SyncMutateRequest} from "./types";
19
+ import type {SyncConfig, SyncMutateBatchResponse, SyncMutateRequest} from "./types";
22
20
 
23
21
  interface MutStuff extends IsDeleted {
24
22
  _id: string;
@@ -40,7 +38,7 @@ const MutStuffModel = model<MutStuff>("MutStuff", mutStuffSchema);
40
38
  const owner = {_id: "mut-owner", admin: false, id: "mut-owner"} as unknown as User;
41
39
  const stranger = {_id: "mut-stranger", admin: false, id: "mut-stranger"} as unknown as User;
42
40
 
43
- const ownerOptions = {
41
+ const ownerOptions: ModelRouterOptions<MutStuff> = {
44
42
  permissions: {
45
43
  create: [Permissions.IsAuthenticated],
46
44
  delete: [Permissions.IsOwner],
@@ -48,14 +46,16 @@ const ownerOptions = {
48
46
  read: [Permissions.IsAny],
49
47
  update: [Permissions.IsOwner],
50
48
  },
51
- } as unknown as ModelRouterOptions<any>;
49
+ };
52
50
 
53
- const registerMutStuff = (overrides: Partial<Record<string, unknown>> = {}): void => {
51
+ const registerMutStuff = (
52
+ overrides: {config?: Partial<SyncConfig>; options?: ModelRouterOptions<MutStuff>} = {}
53
+ ): void => {
54
54
  clearSyncRegistry();
55
55
  registerSync({
56
- config: {scope: {type: "owner"}, ...(overrides.config as object)},
57
- model: MutStuffModel as any,
58
- options: (overrides.options as ModelRouterOptions<any>) ?? ownerOptions,
56
+ config: {scope: {type: "owner"}, ...overrides.config},
57
+ model: MutStuffModel,
58
+ options: overrides.options ?? ownerOptions,
59
59
  routePath: "/mutStuff",
60
60
  });
61
61
  };
@@ -743,7 +743,7 @@ describe("applySyncMutation", () => {
743
743
  read: [Permissions.IsAdmin],
744
744
  update: [Permissions.IsAdmin],
745
745
  },
746
- } as unknown as ModelRouterOptions<any>,
746
+ },
747
747
  });
748
748
  const nack = expectNack(
749
749
  await applySyncMutation({
@@ -892,7 +892,7 @@ describe("applySyncMutation", () => {
892
892
  preCreate: () => {
893
893
  throw new APIError({status: 500, title: "database exploded"});
894
894
  },
895
- } as unknown as ModelRouterOptions<any>,
895
+ },
896
896
  });
897
897
  const nack = expectNack(
898
898
  await applySyncMutation({
@@ -917,14 +917,14 @@ describe("applySyncMutation", () => {
917
917
  registerMutStuff({
918
918
  options: {
919
919
  ...ownerOptions,
920
- preCreate: (body: unknown) => {
920
+ preCreate: (body) => {
921
921
  attempts += 1;
922
922
  if (attempts === 1) {
923
923
  throw new APIError({status: 500, title: "transient DB hiccup"});
924
924
  }
925
- return body;
925
+ return body as MutStuff;
926
926
  },
927
- } as unknown as ModelRouterOptions<any>,
927
+ },
928
928
  });
929
929
  const mutation: SyncMutateRequest = {
930
930
  collection: "mutStuff",
@@ -953,7 +953,7 @@ describe("applySyncMutation", () => {
953
953
  attempts += 1;
954
954
  throw new APIError({status: 400, title: "name is not allowed"});
955
955
  },
956
- } as unknown as ModelRouterOptions<any>,
956
+ },
957
957
  });
958
958
  const mutation: SyncMutateRequest = {
959
959
  collection: "mutStuff",
@@ -982,7 +982,7 @@ describe("applySyncMutation", () => {
982
982
  read: [Permissions.IsAny],
983
983
  update: [Permissions.IsOwner],
984
984
  },
985
- } as unknown as ModelRouterOptions<any>,
985
+ },
986
986
  });
987
987
  const nack = expectNack(
988
988
  await applySyncMutation({
@@ -1,5 +1,3 @@
1
- // noExplicitAny: the mutation handler operates generically across registered models
2
- // biome-ignore-all lint/suspicious/noExplicitAny: the mutation handler operates generically across registered models
3
1
  import type express from "express";
4
2
  import {DateTime} from "luxon";
5
3
  import mongoose from "mongoose";
@@ -257,7 +255,7 @@ const takeoverStaleLease = async (
257
255
  const cutoff = DateTime.now().minus({milliseconds: SYNC_MUTATION_LEASE_MS}).toJSDate();
258
256
  const claimed = await SyncMutation.findOneAndUpdate(
259
257
  {claimedAt: {$lt: cutoff}, mutationId, status: "pending"},
260
- {$set: {claimedAt: new Date()}},
258
+ {$set: {claimedAt: DateTime.now().toJSDate()}},
261
259
  {new: true}
262
260
  );
263
261
  return claimed ?? undefined;
@@ -305,7 +303,7 @@ const waitForRecordedOutcome = async ({
305
303
  if (takenOver) {
306
304
  logger.warn("[sync] Took over a stale mutation lease", {
307
305
  mutationId,
308
- staleForMs: Date.now() - new Date(row.claimedAt).getTime(),
306
+ staleForMs: DateTime.now().diff(DateTime.fromJSDate(row.claimedAt)).toMillis(),
309
307
  });
310
308
  return onTakeover(takenOver);
311
309
  }
@@ -343,7 +341,7 @@ const isE11000Error = (error: unknown): boolean => {
343
341
  * different write landed and happens to have advanced the seq by exactly
344
342
  * one" (a real conflict; a seq match alone cannot tell these apart).
345
343
  */
346
- const docMatchesMutationData = (
344
+ export const docMatchesMutationData = (
347
345
  doc: unknown,
348
346
  data: Record<string, unknown> | undefined
349
347
  ): boolean => {
@@ -358,7 +356,11 @@ const docMatchesMutationData = (
358
356
  return Object.entries(data).every(([key, value]) => {
359
357
  const current = plain?.[key];
360
358
  if (current instanceof Date && typeof value === "string") {
361
- return current.toISOString() === new Date(value).toISOString();
359
+ const parsed = DateTime.fromISO(value, {zone: "utc"});
360
+ if (!parsed.isValid) {
361
+ return false;
362
+ }
363
+ return current.toISOString() === parsed.toJSDate().toISOString();
362
364
  }
363
365
  return JSON.stringify(current) === JSON.stringify(value);
364
366
  });
@@ -731,7 +733,7 @@ export const applySyncMutation = async ({
731
733
  let claimed: SyncMutationDocument;
732
734
  try {
733
735
  claimed = await SyncMutation.create({
734
- claimedAt: new Date(),
736
+ claimedAt: DateTime.now().toJSDate(),
735
737
  mutationId,
736
738
  status: "pending",
737
739
  userId: String(user.id),