@terreno/api 57.1.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.
- package/README.md +17 -7
- package/dist/actions.js +19 -7
- package/dist/adminTypes.d.ts +26 -0
- package/dist/api.d.ts +4 -0
- package/dist/api.js +12 -24
- package/dist/auth.d.ts +23 -10
- package/dist/auth.js +97 -32
- package/dist/authRecovery.d.ts +5 -0
- package/dist/authRecovery.js +350 -0
- package/dist/authTokens.d.ts +8 -0
- package/dist/authTokens.js +162 -0
- package/dist/betterAuth.d.ts +33 -0
- package/dist/betterAuthApp.d.ts +4 -0
- package/dist/betterAuthApp.js +26 -8
- package/dist/betterAuthSetup.d.ts +44 -2
- package/dist/betterAuthSetup.js +288 -18
- package/dist/collectionRegistry.d.ts +38 -0
- package/dist/collectionRegistry.js +72 -0
- package/dist/configurationApp.d.ts +11 -2
- package/dist/configurationApp.js +2 -4
- package/dist/docLoader.js +35 -6
- package/dist/expressServer.d.ts +26 -3
- package/dist/expressServer.js +79 -11
- package/dist/index.d.ts +16 -0
- package/dist/index.js +38 -2
- package/dist/mcp/auth.d.ts +1 -0
- package/dist/mcp/auth.js +47 -25
- package/dist/mcp/handlers.d.ts +1 -1
- package/dist/mcp/handlers.js +67 -233
- package/dist/mcp/query.d.ts +2 -2
- package/dist/mcp/registry.d.ts +1 -5
- package/dist/mcp/registry.js +31 -20
- package/dist/mcp/schemaGenerator.d.ts +2 -7
- package/dist/mcp/schemaGenerator.js +41 -116
- package/dist/mcp/server.d.ts +1 -0
- package/dist/mcp/server.js +1 -0
- package/dist/mcp/serviceTokens.d.ts +16 -0
- package/dist/mcp/serviceTokens.js +345 -0
- package/dist/mcp/types.d.ts +2 -2
- package/dist/models/mcpServiceToken.d.ts +3 -0
- package/dist/models/mcpServiceToken.js +208 -0
- package/dist/openApiBuilder.d.ts +5 -4
- package/dist/openApiBuilder.js +61 -8
- package/dist/openApiCompat.js +1 -1
- package/dist/openApiValidator.js +0 -2
- package/dist/plugins.d.ts +14 -1
- package/dist/plugins.js +17 -6
- package/dist/populate.d.ts +2 -2
- package/dist/populate.js +47 -42
- package/dist/rateLimit/applyTrustProxy.d.ts +3 -0
- package/dist/rateLimit/applyTrustProxy.js +17 -0
- package/dist/rateLimit/createStore.d.ts +2 -0
- package/dist/rateLimit/createStore.js +28 -0
- package/dist/rateLimit/memoryStore.d.ts +2 -0
- package/dist/rateLimit/memoryStore.js +108 -0
- package/dist/rateLimit/middleware.d.ts +11 -0
- package/dist/rateLimit/middleware.js +110 -0
- package/dist/rateLimit/mongoStore.d.ts +3 -0
- package/dist/rateLimit/mongoStore.js +122 -0
- package/dist/rateLimit/policies.d.ts +14 -0
- package/dist/rateLimit/policies.js +151 -0
- package/dist/rateLimit/redisStore.d.ts +3 -0
- package/dist/rateLimit/redisStore.js +155 -0
- package/dist/rateLimit/types.d.ts +54 -0
- package/dist/rateLimit/types.js +9 -0
- package/dist/rbac/access.js +5 -1
- package/dist/rbac/assertAllowed.d.ts +9 -0
- package/dist/rbac/assertAllowed.js +182 -0
- package/dist/rbac/modelRouterAccess.d.ts +1 -0
- package/dist/rbac/modelRouterAccess.js +8 -4
- package/dist/rbac/permissionUtils.js +3 -2
- package/dist/rbac/resolve.js +5 -4
- package/dist/rbac/routes.js +15 -6
- package/dist/rbac/statements.d.ts +10 -0
- package/dist/rbac/statements.js +13 -2
- package/dist/realtime/deprecation.d.ts +12 -0
- package/dist/realtime/deprecation.js +32 -0
- package/dist/realtime/realtimeApp.d.ts +1 -1
- package/dist/realtime/realtimeApp.js +10 -11
- package/dist/realtime/registry.d.ts +0 -31
- package/dist/realtime/registry.js +44 -31
- package/dist/realtime/socketAuth.d.ts +5 -5
- package/dist/realtime/socketAuth.js +11 -1
- package/dist/realtime/socketUser.d.ts +5 -1
- package/dist/realtime/types.d.ts +14 -4
- package/dist/schemaMetadata.d.ts +56 -0
- package/dist/schemaMetadata.js +513 -0
- package/dist/seedRunner.d.ts +88 -0
- package/dist/seedRunner.js +627 -0
- package/dist/sync/executors.d.ts +12 -2
- package/dist/sync/executors.js +8 -10
- package/dist/sync/loadHarness.js +28 -19
- package/dist/sync/models.js +26 -24
- package/dist/sync/mutationHandler.d.ts +9 -0
- package/dist/sync/mutationHandler.js +11 -6
- package/dist/sync/registrationSideEffects.d.ts +35 -0
- package/dist/sync/registrationSideEffects.js +151 -0
- package/dist/sync/registry.d.ts +2 -52
- package/dist/sync/registry.js +38 -187
- package/dist/sync/routes.js +3 -2
- package/dist/sync/scripts/compactTombstones.js +0 -2
- package/dist/sync/syncApp.d.ts +3 -3
- package/dist/sync/syncApp.js +4 -4
- package/dist/sync/syncBatch.js +3 -2
- package/dist/sync/syncSeqPlugin.d.ts +1 -1
- package/dist/sync/syncSeqPlugin.js +20 -16
- package/dist/terrenoApp.d.ts +19 -5
- package/dist/terrenoApp.js +66 -18
- package/dist/tests/models.d.ts +2 -0
- package/dist/tests/models.js +6 -0
- package/dist/types/authToken.d.ts +33 -0
- package/dist/types/authToken.js +2 -0
- package/dist/types/mcpServiceToken.d.ts +41 -0
- package/dist/types/mcpServiceToken.js +2 -0
- package/dist/webhooks/headerValue.d.ts +2 -0
- package/dist/webhooks/headerValue.js +14 -0
- package/dist/webhooks/idempotency/memoryStore.d.ts +10 -0
- package/dist/webhooks/idempotency/memoryStore.js +65 -0
- package/dist/webhooks/idempotency/mongoStore.d.ts +4 -0
- package/dist/webhooks/idempotency/mongoStore.js +150 -0
- package/dist/webhooks/idempotency/webhookReceipt.d.ts +62 -0
- package/dist/webhooks/idempotency/webhookReceipt.js +39 -0
- package/dist/webhooks/rawBody.d.ts +14 -0
- package/dist/webhooks/rawBody.js +22 -0
- package/dist/webhooks/timingSafeEqual.d.ts +4 -0
- package/dist/webhooks/timingSafeEqual.js +17 -0
- package/dist/webhooks/verifiers/hmac.d.ts +15 -0
- package/dist/webhooks/verifiers/hmac.js +49 -0
- package/dist/webhooks/verifiers/sendgrid.d.ts +10 -0
- package/dist/webhooks/verifiers/sendgrid.js +56 -0
- package/dist/webhooks/verifiers/stripe.d.ts +9 -0
- package/dist/webhooks/verifiers/stripe.js +99 -0
- package/dist/webhooks/verifiers/twilio.d.ts +9 -0
- package/dist/webhooks/verifiers/twilio.js +112 -0
- package/dist/webhooks/webhooksApp.d.ts +33 -0
- package/dist/webhooks/webhooksApp.js +195 -0
- package/package.json +5 -6
- package/src/__snapshots__/openApi.test.ts.snap +270 -0
- package/src/__snapshots__/openApiBuilder.test.ts.snap +30 -12
- package/src/__snapshots__/schemaMetadata.test.ts.snap +197 -0
- package/src/actions.ts +20 -13
- package/src/adminModelRouter.test.ts +133 -0
- package/src/adminTypes.ts +23 -0
- package/src/api.arrayOperations.test.ts +43 -31
- package/src/api.hooks.test.ts +38 -29
- package/src/api.query.test.ts +31 -31
- package/src/api.test.ts +95 -96
- package/src/api.ts +16 -27
- package/src/auth.test.ts +107 -51
- package/src/auth.ts +98 -29
- package/src/authRecovery.test.ts +281 -0
- package/src/authRecovery.ts +279 -0
- package/src/authTokens.test.ts +94 -0
- package/src/authTokens.ts +118 -0
- package/src/authVerification.test.ts +293 -0
- package/src/betterAuth.ts +24 -0
- package/src/betterAuthApp.test.ts +1 -0
- package/src/betterAuthApp.ts +27 -9
- package/src/betterAuthSetup.test.ts +568 -38
- package/src/betterAuthSetup.ts +260 -5
- package/src/collectionRegistry.test.ts +165 -0
- package/src/collectionRegistry.ts +115 -0
- package/src/configurationApp.ts +13 -7
- package/src/docLoader.test.ts +45 -1
- package/src/docLoader.ts +37 -6
- package/src/express.d.ts +1 -0
- package/src/expressServer.test.ts +130 -66
- package/src/expressServer.ts +75 -19
- package/src/githubAuth.test.ts +60 -55
- package/src/index.ts +58 -0
- package/src/mcp/auth.test.ts +47 -0
- package/src/mcp/auth.ts +32 -11
- package/src/mcp/handlers.ts +45 -172
- package/src/mcp/integration.test.ts +59 -9
- package/src/mcp/query.ts +2 -4
- package/src/mcp/registry.ts +29 -20
- package/src/mcp/schemaGenerator.test.ts +8 -0
- package/src/mcp/schemaGenerator.ts +49 -131
- package/src/mcp/server.ts +2 -0
- package/src/mcp/serviceTokens.test.ts +330 -0
- package/src/mcp/serviceTokens.ts +307 -0
- package/src/mcp/toolGenerator.test.ts +1 -1
- package/src/mcp/types.ts +2 -6
- package/src/models/mcpServiceToken.test.ts +129 -0
- package/src/models/mcpServiceToken.ts +158 -0
- package/src/openApi.test.ts +80 -64
- package/src/openApiBuilder.test.ts +95 -7
- package/src/openApiBuilder.ts +46 -9
- package/src/openApiCompat.ts +1 -1
- package/src/openApiValidator.test.ts +49 -54
- package/src/openApiValidator.ts +1 -3
- package/src/plugins.test.ts +14 -0
- package/src/plugins.ts +34 -9
- package/src/populate.test.ts +34 -0
- package/src/populate.ts +40 -29
- package/src/rateLimit/applyTrustProxy.ts +17 -0
- package/src/rateLimit/createStore.ts +25 -0
- package/src/rateLimit/memoryStore.ts +40 -0
- package/src/rateLimit/middleware.ts +79 -0
- package/src/rateLimit/mongoStore.test.ts +80 -0
- package/src/rateLimit/mongoStore.ts +76 -0
- package/src/rateLimit/policies.ts +130 -0
- package/src/rateLimit/rateLimit.test.ts +328 -0
- package/src/rateLimit/redisStore.ioredis.test.ts +29 -0
- package/src/rateLimit/redisStore.test.ts +114 -0
- package/src/rateLimit/redisStore.ts +85 -0
- package/src/rateLimit/types.ts +64 -0
- package/src/rbac/access.ts +5 -1
- package/src/rbac/assertAllowed.test.ts +325 -0
- package/src/rbac/assertAllowed.ts +157 -0
- package/src/rbac/backfillAdmins.test.ts +124 -0
- package/src/rbac/fieldViews.test.ts +89 -0
- package/src/rbac/modelRouterAccess.test.ts +126 -0
- package/src/rbac/modelRouterAccess.ts +5 -2
- package/src/rbac/permissionUtils.ts +3 -2
- package/src/rbac/realtimeAccess.test.ts +109 -1
- package/src/rbac/resolve.ts +5 -4
- package/src/rbac/roleManager.test.ts +169 -0
- package/src/rbac/routes.test.ts +233 -0
- package/src/rbac/routes.ts +16 -6
- package/src/rbac/statements.test.ts +2 -0
- package/src/rbac/statements.ts +12 -1
- package/src/realtime/deprecation.test.ts +60 -0
- package/src/realtime/deprecation.ts +32 -0
- package/src/realtime/realtime.test.ts +8 -4
- package/src/realtime/realtimeApp.ts +10 -11
- package/src/realtime/realtimeAppSetup.test.ts +32 -5
- package/src/realtime/registry.ts +48 -42
- package/src/realtime/sessionRevalidation.test.ts +57 -44
- package/src/realtime/socketAuth.test.ts +75 -23
- package/src/realtime/socketAuth.ts +17 -6
- package/src/realtime/socketUser.ts +5 -3
- package/src/realtime/types.ts +14 -4
- package/src/schemaMetadata.test.ts +405 -0
- package/src/schemaMetadata.ts +593 -0
- package/src/scrubAdminFields.test.ts +105 -0
- package/src/seedRunner.test.ts +591 -0
- package/src/seedRunner.ts +464 -0
- package/src/sync/docMatchesMutationData.test.ts +16 -0
- package/src/sync/ensureSyncIndexes.test.ts +54 -15
- package/src/sync/executors.test.ts +40 -0
- package/src/sync/executors.ts +24 -12
- package/src/sync/integration.test.ts +15 -7
- package/src/sync/loadHarness.ts +28 -19
- package/src/sync/models.ts +32 -18
- package/src/sync/mutationHandler.test.ts +16 -16
- package/src/sync/mutationHandler.ts +9 -7
- package/src/sync/registrationSideEffects.test.ts +126 -0
- package/src/sync/registrationSideEffects.ts +131 -0
- package/src/sync/registry.ts +45 -171
- package/src/sync/routes.ts +3 -2
- package/src/sync/scripts/compactTombstones.ts +0 -2
- package/src/sync/sessionRevalidationIntegration.test.ts +12 -11
- package/src/sync/sync.test.ts +25 -23
- package/src/sync/syncApp.ts +4 -4
- package/src/sync/syncBatch.ts +3 -2
- package/src/sync/syncFrontier.test.ts +17 -15
- package/src/sync/syncPhaseC.test.ts +54 -43
- package/src/sync/syncRoutes.test.ts +51 -36
- package/src/sync/syncSeqPlugin.ts +66 -44
- package/src/sync/syncSocket.test.ts +112 -81
- package/src/terrenoApp.mcpServiceTokens.test.ts +173 -0
- package/src/terrenoApp.ts +87 -15
- package/src/tests/models.ts +14 -1
- package/src/types/authToken.ts +43 -0
- package/src/types/mcpServiceToken.ts +54 -0
- package/src/webhooks/headerValue.test.ts +22 -0
- package/src/webhooks/headerValue.ts +12 -0
- package/src/webhooks/idempotency/memoryStore.ts +34 -0
- package/src/webhooks/idempotency/mongoStore.test.ts +274 -0
- package/src/webhooks/idempotency/mongoStore.ts +83 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +70 -0
- package/src/webhooks/rawBody.test.ts +74 -0
- package/src/webhooks/rawBody.ts +23 -0
- package/src/webhooks/timingSafeEqual.ts +16 -0
- package/src/webhooks/verifiers/hmac.test.ts +110 -0
- package/src/webhooks/verifiers/hmac.ts +54 -0
- package/src/webhooks/verifiers/providerVerifiers.test.ts +388 -0
- package/src/webhooks/verifiers/sendgrid.ts +59 -0
- package/src/webhooks/verifiers/stripe.ts +67 -0
- package/src/webhooks/verifiers/twilio.ts +69 -0
- package/src/webhooks/webhooksApp.test.ts +221 -0
- package/src/webhooks/webhooksApp.ts +144 -0
package/src/sync/executors.ts
CHANGED
|
@@ -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
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
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
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
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 {
|
|
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
|
|
211
|
-
addAuthRoutes(app
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
package/src/sync/loadHarness.ts
CHANGED
|
@@ -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 =
|
|
160
|
-
while (
|
|
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
|
|
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 =
|
|
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 =
|
|
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(
|
|
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(
|
|
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 =
|
|
405
|
+
const loadEnd = DateTime.now().plus({seconds: config.durationSec}).toMillis();
|
|
401
406
|
|
|
402
|
-
while (
|
|
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 =
|
|
413
|
+
const startedAt = DateTime.now().toMillis();
|
|
409
414
|
const {mutationId} = hc.client.mutate({
|
|
410
415
|
collection: COLLECTION,
|
|
411
|
-
data: {
|
|
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 =
|
|
426
|
+
const startedAt = DateTime.now().toMillis();
|
|
420
427
|
const {mutationId} = owner.client.mutate({
|
|
421
428
|
collection: COLLECTION,
|
|
422
|
-
data: {title: `updated-${
|
|
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-${
|
|
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-${
|
|
452
|
-
mutationId: `load-dup-${
|
|
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 =
|
|
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 =
|
|
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(
|
|
513
|
+
fanoutLagsMs.push(DateTime.now().toMillis() - writeStart);
|
|
505
514
|
})
|
|
506
515
|
.catch(() => {})
|
|
507
516
|
);
|
package/src/sync/models.ts
CHANGED
|
@@ -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: () =>
|
|
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 =
|
|
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
|
|
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 =
|
|
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 =
|
|
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: () =>
|
|
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: () =>
|
|
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: () =>
|
|
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: () =>
|
|
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
|
-
|
|
572
|
-
|
|
573
|
-
|
|
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
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
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:
|
|
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
|
-
}
|
|
49
|
+
};
|
|
52
50
|
|
|
53
|
-
const registerMutStuff = (
|
|
51
|
+
const registerMutStuff = (
|
|
52
|
+
overrides: {config?: Partial<SyncConfig>; options?: ModelRouterOptions<MutStuff>} = {}
|
|
53
|
+
): void => {
|
|
54
54
|
clearSyncRegistry();
|
|
55
55
|
registerSync({
|
|
56
|
-
config: {scope: {type: "owner"}, ...
|
|
57
|
-
model: MutStuffModel
|
|
58
|
-
options:
|
|
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
|
-
}
|
|
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
|
-
}
|
|
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
|
|
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
|
-
}
|
|
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
|
-
}
|
|
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
|
-
}
|
|
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:
|
|
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:
|
|
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
|
-
|
|
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:
|
|
736
|
+
claimedAt: DateTime.now().toJSDate(),
|
|
735
737
|
mutationId,
|
|
736
738
|
status: "pending",
|
|
737
739
|
userId: String(user.id),
|