@terreno/api 57.3.0 → 57.6.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/.ai/skills/mongoose-schema-safety/SKILL.md +23 -10
- package/README.md +11 -5
- package/bunfig.toml +1 -1
- package/dist/actions.js +197 -368
- package/dist/adminModelRouter.js +88 -173
- package/dist/adminTypes.d.ts +7 -0
- package/dist/adminTypes.js +6 -1
- package/dist/api.d.ts +10 -0
- package/dist/api.js +601 -792
- package/dist/audit/auditApp.d.ts +30 -0
- package/dist/audit/auditApp.js +114 -0
- package/dist/audit/auditEventModel.d.ts +29 -0
- package/dist/audit/auditEventModel.js +101 -0
- package/dist/audit/cloudTasksEnqueue.d.ts +32 -0
- package/dist/audit/cloudTasksEnqueue.js +69 -0
- package/dist/audit/diff.d.ts +10 -0
- package/dist/audit/diff.js +140 -0
- package/dist/audit/processQueue.d.ts +6 -0
- package/dist/audit/processQueue.js +56 -0
- package/dist/audit/rbacSink.d.ts +2 -0
- package/dist/audit/rbacSink.js +41 -0
- package/dist/audit/record.d.ts +56 -0
- package/dist/audit/record.js +251 -0
- package/dist/auth.js +432 -681
- package/dist/authRecovery.js +155 -269
- package/dist/authTokens.js +43 -118
- package/dist/betterAuth.d.ts +6 -0
- package/dist/betterAuthApp.js +25 -26
- package/dist/betterAuthSetup.js +258 -413
- package/dist/browserLogsRoute.d.ts +6 -0
- package/dist/browserLogsRoute.js +97 -0
- package/dist/collectionRegistry.d.ts +3 -1
- package/dist/collectionRegistry.js +43 -33
- package/dist/config.js +53 -122
- package/dist/configurationApp.js +180 -361
- package/dist/configurationPlugin.js +130 -302
- package/dist/consentApp.js +276 -421
- package/dist/docLoader.js +78 -126
- package/dist/envConfigurationPlugin.js +25 -147
- package/dist/errors.js +151 -247
- package/dist/example.js +28 -34
- package/dist/expressServer.d.ts +10 -0
- package/dist/expressServer.js +109 -255
- package/dist/frameworkModelNames.d.ts +24 -0
- package/dist/frameworkModelNames.js +31 -0
- package/dist/githubAuth.js +152 -216
- package/dist/httpClient.js +176 -264
- package/dist/index.d.ts +28 -1
- package/dist/index.js +53 -2
- package/dist/logger.js +145 -278
- package/dist/mcp/auth.js +76 -128
- package/dist/mcp/createMCPRequest.js +4 -5
- package/dist/mcp/handlers.js +268 -480
- package/dist/mcp/query.d.ts +0 -13
- package/dist/mcp/query.js +57 -151
- package/dist/mcp/registry.js +17 -30
- package/dist/mcp/schemaGenerator.js +95 -181
- package/dist/mcp/server.js +28 -100
- package/dist/mcp/serviceTokens.js +102 -191
- package/dist/mcp/toolGenerator.js +78 -191
- package/dist/middleware.js +15 -27
- package/dist/migrations/fixtures/all-down/20260910120000-one.d.ts +4 -0
- package/dist/migrations/fixtures/all-down/20260910120000-one.js +9 -0
- package/dist/migrations/fixtures/all-down/20260910120001-two.d.ts +4 -0
- package/dist/migrations/fixtures/all-down/20260910120001-two.js +9 -0
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.d.ts +3 -0
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +7 -0
- package/dist/migrations/fixtures/bad-name/not-a-migration.d.ts +3 -0
- package/dist/migrations/fixtures/bad-name/not-a-migration.js +7 -0
- package/dist/migrations/fixtures/missing-up/20260910120000-noup.d.ts +2 -0
- package/dist/migrations/fixtures/missing-up/20260910120000-noup.js +5 -0
- package/dist/migrations/fixtures/valid/20260910120000-alpha.d.ts +3 -0
- package/dist/migrations/fixtures/valid/20260910120000-alpha.js +7 -0
- package/dist/migrations/fixtures/valid/20260910120001-beta.d.ts +4 -0
- package/dist/migrations/fixtures/valid/20260910120001-beta.js +9 -0
- package/dist/migrations/generate.d.ts +17 -0
- package/dist/migrations/generate.js +155 -0
- package/dist/migrations/load.d.ts +20 -0
- package/dist/migrations/load.js +110 -0
- package/dist/migrations/lock.d.ts +13 -0
- package/dist/migrations/lock.js +73 -0
- package/dist/migrations/runner.d.ts +42 -0
- package/dist/migrations/runner.js +249 -0
- package/dist/migrations/schemaCatalog.d.ts +44 -0
- package/dist/migrations/schemaCatalog.js +185 -0
- package/dist/migrations/types.d.ts +87 -0
- package/dist/migrations/types.js +7 -0
- package/dist/models/consentForm.js +6 -6
- package/dist/models/consentResponse.js +5 -5
- package/dist/models/mcpServiceToken.js +66 -156
- package/dist/models/notification.d.ts +2 -0
- package/dist/models/notification.js +62 -0
- package/dist/models/notificationPreference.d.ts +2 -0
- package/dist/models/notificationPreference.js +49 -0
- package/dist/models/versionConfig.js +5 -5
- package/dist/notifications/notificationService.d.ts +40 -0
- package/dist/notifications/notificationService.js +183 -0
- package/dist/notifications/notificationsApp.d.ts +18 -0
- package/dist/notifications/notificationsApp.js +156 -0
- package/dist/notifications/notificationsBeforeSend.d.ts +14 -0
- package/dist/notifications/notificationsBeforeSend.js +32 -0
- package/dist/notifiers/googleChatNotifier.js +36 -117
- package/dist/notifiers/slackNotifier.d.ts +31 -2
- package/dist/notifiers/slackNotifier.js +170 -119
- package/dist/notifiers/zoomNotifier.js +60 -111
- package/dist/openApi.d.ts +1 -7
- package/dist/openApi.js +113 -127
- package/dist/openApiBuilder.d.ts +14 -3
- package/dist/openApiBuilder.js +117 -159
- package/dist/openApiCompat.js +81 -148
- package/dist/openApiEtag.js +7 -7
- package/dist/openApiValidator.d.ts +13 -1
- package/dist/openApiValidator.js +301 -246
- package/dist/orgs/orgContext.d.ts +23 -0
- package/dist/orgs/orgContext.js +128 -0
- package/dist/orgs/orgPermissions.d.ts +4 -0
- package/dist/orgs/orgPermissions.js +38 -0
- package/dist/orgs/orgPlugin.d.ts +13 -0
- package/dist/orgs/orgPlugin.js +66 -0
- package/dist/orgs/organizationModel.d.ts +13 -0
- package/dist/orgs/organizationModel.js +200 -0
- package/dist/orgs/organizationSettings.d.ts +19 -0
- package/dist/orgs/organizationSettings.js +113 -0
- package/dist/orgs/orgsApp.d.ts +28 -0
- package/dist/orgs/orgsApp.js +416 -0
- package/dist/permissions.d.ts +7 -13
- package/dist/permissions.js +95 -211
- package/dist/plugins.js +125 -218
- package/dist/populate.js +130 -240
- package/dist/rateLimit/applyTrustProxy.js +1 -1
- package/dist/rateLimit/createStore.js +7 -8
- package/dist/rateLimit/memoryStore.js +22 -100
- package/dist/rateLimit/middleware.d.ts +0 -8
- package/dist/rateLimit/middleware.js +49 -101
- package/dist/rateLimit/mongoStore.d.ts +1 -0
- package/dist/rateLimit/mongoStore.js +55 -112
- package/dist/rateLimit/policies.d.ts +0 -4
- package/dist/rateLimit/policies.js +44 -80
- package/dist/rateLimit/redisStore.d.ts +1 -0
- package/dist/rateLimit/redisStore.js +40 -133
- package/dist/rbac/access.js +122 -253
- package/dist/rbac/assertAllowed.js +95 -171
- package/dist/rbac/auditModel.js +14 -58
- package/dist/rbac/backfillAdmins.js +24 -130
- package/dist/rbac/fieldViews.js +46 -141
- package/dist/rbac/membershipOrgAdminSource.d.ts +6 -0
- package/dist/rbac/membershipOrgAdminSource.js +51 -0
- package/dist/rbac/middleware.js +30 -96
- package/dist/rbac/modelRouterAccess.d.ts +3 -0
- package/dist/rbac/modelRouterAccess.js +133 -307
- package/dist/rbac/permissionUtils.js +40 -179
- package/dist/rbac/realtimeAccess.js +81 -147
- package/dist/rbac/resolve.d.ts +1 -1
- package/dist/rbac/resolve.js +154 -319
- package/dist/rbac/roleManager.js +318 -700
- package/dist/rbac/roleModel.d.ts +2 -5
- package/dist/rbac/roleModel.js +60 -173
- package/dist/rbac/routes.js +93 -196
- package/dist/rbac/scopes.js +39 -133
- package/dist/rbac/statements.d.ts +10 -1
- package/dist/rbac/statements.js +42 -93
- package/dist/rbac/types.d.ts +5 -0
- package/dist/rbac/userPlugin.js +3 -29
- package/dist/realtime/changeStreamWatcher.d.ts +10 -4
- package/dist/realtime/changeStreamWatcher.js +499 -764
- package/dist/realtime/deprecation.d.ts +1 -0
- package/dist/realtime/deprecation.js +9 -10
- package/dist/realtime/queryMatcher.js +110 -191
- package/dist/realtime/queryStore.js +35 -118
- package/dist/realtime/realtimeApp.d.ts +1 -0
- package/dist/realtime/realtimeApp.js +248 -401
- package/dist/realtime/registry.js +19 -30
- package/dist/realtime/sessionRevalidation.js +132 -358
- package/dist/realtime/socketAuth.js +89 -208
- package/dist/realtime/socketUser.js +10 -56
- package/dist/requestContext.js +93 -139
- package/dist/schemaMetadata.js +181 -265
- package/dist/scriptRunner.js +96 -266
- package/dist/scrubAdminFields.js +36 -108
- package/dist/secretProviders.js +130 -299
- package/dist/seedRunner.js +221 -514
- package/dist/sync/adminBroadcastScope.d.ts +40 -0
- package/dist/sync/adminBroadcastScope.js +82 -0
- package/dist/sync/adminWindowAccess.d.ts +13 -0
- package/dist/sync/adminWindowAccess.js +25 -0
- package/dist/sync/adminWindowMutation.d.ts +88 -0
- package/dist/sync/adminWindowMutation.js +176 -0
- package/dist/sync/executors.d.ts +9 -3
- package/dist/sync/executors.js +425 -598
- package/dist/sync/loadHarness.js +392 -689
- package/dist/sync/models.js +181 -341
- package/dist/sync/mutationHandler.d.ts +5 -2
- package/dist/sync/mutationHandler.js +549 -697
- package/dist/sync/registrationSideEffects.js +41 -95
- package/dist/sync/registry.js +31 -47
- package/dist/sync/routes.d.ts +13 -0
- package/dist/sync/routes.js +484 -760
- package/dist/sync/scripts/compactTombstones.js +83 -228
- package/dist/sync/serialize.js +18 -58
- package/dist/sync/socketHandlers.d.ts +8 -2
- package/dist/sync/socketHandlers.js +271 -402
- package/dist/sync/streams.d.ts +2 -0
- package/dist/sync/streams.js +54 -116
- package/dist/sync/syncApp.d.ts +2 -1
- package/dist/sync/syncApp.js +14 -16
- package/dist/sync/syncBatch.d.ts +5 -2
- package/dist/sync/syncBatch.js +63 -168
- package/dist/sync/syncSeqPlugin.js +430 -702
- package/dist/sync/types.d.ts +18 -0
- package/dist/syncConsents.js +113 -253
- package/dist/terrenoApp.d.ts +29 -0
- package/dist/terrenoApp.js +184 -257
- package/dist/tests/createTestData.d.ts +1 -3
- package/dist/tests/createTestData.js +147 -259
- package/dist/tests/models.d.ts +1 -0
- package/dist/tests/models.js +23 -56
- package/dist/tests/mongoTestSetup.js +42 -103
- package/dist/tests/testHelper.d.ts +0 -5
- package/dist/tests/testHelper.js +32 -101
- package/dist/tests.js +12 -51
- package/dist/transformers.js +61 -164
- package/dist/types/authToken.d.ts +3 -3
- package/dist/types/mcpServiceToken.d.ts +2 -2
- package/dist/types/membership.d.ts +23 -0
- package/dist/types/membership.js +2 -0
- package/dist/types/notification.d.ts +28 -0
- package/dist/types/notification.js +2 -0
- package/dist/types/notificationPreference.d.ts +20 -0
- package/dist/types/notificationPreference.js +2 -0
- package/dist/types/organization.d.ts +36 -0
- package/dist/types/organization.js +2 -0
- package/dist/utils.js +31 -93
- package/dist/vendor/wesleytodd-openapi/index.js +37 -51
- package/dist/vendor/wesleytodd-openapi/lib/generate-doc.js +32 -40
- package/dist/vendor/wesleytodd-openapi/lib/layer-schema.js +3 -3
- package/dist/vendor/wesleytodd-openapi/lib/ui.js +50 -19
- package/dist/vendor/wesleytodd-openapi/lib/validate.js +19 -47
- package/dist/versionCheckPlugin.js +52 -100
- package/dist/webhooks/headerValue.js +2 -2
- package/dist/webhooks/idempotency/memoryStore.js +16 -59
- package/dist/webhooks/idempotency/mongoStore.js +55 -130
- package/dist/webhooks/idempotency/webhookReceipt.d.ts +4 -49
- package/dist/webhooks/idempotency/webhookReceipt.js +14 -14
- package/dist/webhooks/rawBody.d.ts +1 -0
- package/dist/webhooks/rawBody.js +2 -1
- package/dist/webhooks/timingSafeEqual.js +4 -5
- package/dist/webhooks/verifiers/hmac.js +17 -18
- package/dist/webhooks/verifiers/sendgrid.js +20 -22
- package/dist/webhooks/verifiers/stripe.js +31 -65
- package/dist/webhooks/verifiers/twilio.js +23 -73
- package/dist/webhooks/webhooksApp.js +78 -177
- package/dist/zodOpenApi.js +2 -2
- package/package.json +34 -28
- package/src/__snapshots__/openApi.test.ts.snap +192 -54
- package/src/__snapshots__/openApiBuilder.test.ts.snap +0 -18
- package/src/__snapshots__/schemaMetadata.test.ts.snap +0 -1
- package/src/__tests__/browserLogsRoute.test.ts +165 -0
- package/src/adminTypes.ts +8 -0
- package/src/api.test.ts +39 -2
- package/src/api.ts +68 -0
- package/src/audit/auditApp.test.ts +243 -0
- package/src/audit/auditApp.ts +139 -0
- package/src/audit/auditEventModel.ts +160 -0
- package/src/audit/auditRouter.test.ts +456 -0
- package/src/audit/cloudTasksEnqueue.test.ts +124 -0
- package/src/audit/cloudTasksEnqueue.ts +104 -0
- package/src/audit/diff.ts +163 -0
- package/src/audit/processQueue.ts +69 -0
- package/src/audit/rbacSink.test.ts +86 -0
- package/src/audit/rbacSink.ts +42 -0
- package/src/audit/record.test.ts +402 -0
- package/src/audit/record.ts +329 -0
- package/src/auth.test.ts +1 -1
- package/src/auth.ts +1 -1
- package/src/authRecovery.test.ts +43 -0
- package/src/authRecovery.ts +16 -10
- package/src/authTokens.ts +6 -2
- package/src/authVerification.test.ts +22 -0
- package/src/betterAuth.ts +7 -0
- package/src/betterAuthSetup.test.ts +27 -0
- package/src/betterAuthSetup.ts +1 -0
- package/src/browserLogsRoute.ts +110 -0
- package/src/collectionRegistry.test.ts +8 -9
- package/src/collectionRegistry.ts +21 -1
- package/src/configurationPlugin.test.ts +1 -11
- package/src/express.d.ts +2 -0
- package/src/expressServer.test.ts +50 -2
- package/src/expressServer.ts +10 -0
- package/src/frameworkModelNames.test.ts +62 -0
- package/src/frameworkModelNames.ts +33 -0
- package/src/index.ts +76 -0
- package/src/isolated/migrateCliConnect.isolated.ts +51 -0
- package/src/mcp/auth.test.ts +34 -1
- package/src/mcp/handlers.test.ts +275 -0
- package/src/mcp/query.ts +3 -3
- package/src/mcp/toolGenerator.test.ts +81 -0
- package/src/migrations/cli.test.ts +353 -0
- package/src/migrations/cli.ts +283 -0
- package/src/migrations/fixtures/all-down/20260910120000-one.ts +6 -0
- package/src/migrations/fixtures/all-down/20260910120001-two.ts +6 -0
- package/src/migrations/fixtures/bad-id/20260910120000-mismatch.ts +4 -0
- package/src/migrations/fixtures/bad-name/not-a-migration.ts +4 -0
- package/src/migrations/fixtures/missing-up/20260910120000-noup.ts +2 -0
- package/src/migrations/fixtures/valid/20260910120000-alpha.ts +4 -0
- package/src/migrations/fixtures/valid/20260910120001-beta.ts +6 -0
- package/src/migrations/generate.test.ts +333 -0
- package/src/migrations/generate.ts +216 -0
- package/src/migrations/load.test.ts +103 -0
- package/src/migrations/load.ts +124 -0
- package/src/migrations/lock.test.ts +133 -0
- package/src/migrations/lock.ts +118 -0
- package/src/migrations/runner.test.ts +504 -0
- package/src/migrations/runner.ts +358 -0
- package/src/migrations/schemaCatalog.test.ts +159 -0
- package/src/migrations/schemaCatalog.ts +262 -0
- package/src/migrations/types.ts +94 -0
- package/src/models/mcpServiceToken.ts +4 -3
- package/src/models/notification.ts +69 -0
- package/src/models/notificationPreference.ts +62 -0
- package/src/notifications/notificationService.ts +254 -0
- package/src/notifications/notificationsApp.test.ts +528 -0
- package/src/notifications/notificationsApp.ts +200 -0
- package/src/notifications/notificationsBeforeSend.ts +51 -0
- package/src/notifiers/slackNotifier.test.ts +186 -1
- package/src/notifiers/slackNotifier.ts +167 -8
- package/src/openApi.test.ts +0 -1
- package/src/openApi.ts +2 -1
- package/src/openApiBuilder.ts +14 -3
- package/src/openApiValidator.test.ts +221 -2
- package/src/openApiValidator.ts +218 -33
- package/src/orgs/orgAccessOptIn.test.ts +87 -0
- package/src/orgs/orgContext.test.ts +259 -0
- package/src/orgs/orgContext.ts +151 -0
- package/src/orgs/orgPermissions.test.ts +105 -0
- package/src/orgs/orgPermissions.ts +40 -0
- package/src/orgs/orgPlugin.test.ts +130 -0
- package/src/orgs/orgPlugin.ts +70 -0
- package/src/orgs/orgRbac.test.ts +267 -0
- package/src/orgs/orgSecurityDefects.test.ts +415 -0
- package/src/orgs/organizationModel.test.ts +205 -0
- package/src/orgs/organizationModel.ts +262 -0
- package/src/orgs/organizationSettings.test.ts +113 -0
- package/src/orgs/organizationSettings.ts +126 -0
- package/src/orgs/orgsApp.test.ts +382 -0
- package/src/orgs/orgsApp.ts +563 -0
- package/src/permissions.test.ts +62 -36
- package/src/permissions.ts +8 -30
- package/src/plugins.test.ts +5 -0
- package/src/plugins.ts +3 -0
- package/src/rateLimit/middleware.ts +1 -1
- package/src/rateLimit/mongoStore.ts +1 -0
- package/src/rateLimit/policies.ts +2 -2
- package/src/rateLimit/redisStore.ts +1 -0
- package/src/rbac/access.ts +11 -3
- package/src/rbac/membershipOrgAdminSource.ts +59 -0
- package/src/rbac/modelRouterAccess.ts +3 -0
- package/src/rbac/permissionUtils.test.ts +26 -5
- package/src/rbac/resolve.test.ts +34 -2
- package/src/rbac/resolve.ts +26 -7
- package/src/rbac/roleModel.test.ts +34 -1
- package/src/rbac/roleModel.ts +17 -1
- package/src/rbac/statements.test.ts +19 -0
- package/src/rbac/statements.ts +19 -1
- package/src/rbac/types.ts +5 -0
- package/src/realtime/changeStreamWatcher.ts +73 -33
- package/src/realtime/deprecation.ts +1 -0
- package/src/realtime/realtime.test.ts +357 -185
- package/src/realtime/realtimeApp.ts +2 -1
- package/src/schemaMetadata.test.ts +14 -0
- package/src/schemaMetadata.ts +5 -5
- package/src/sync/adminBroadcastConfig.test.ts +96 -0
- package/src/sync/adminBroadcastScope.test.ts +155 -0
- package/src/sync/adminBroadcastScope.ts +123 -0
- package/src/sync/adminWindowAccess.test.ts +57 -0
- package/src/sync/adminWindowAccess.ts +32 -0
- package/src/sync/adminWindowMutation.test.ts +450 -0
- package/src/sync/adminWindowMutation.ts +283 -0
- package/src/sync/executors.ts +33 -6
- package/src/sync/integration.test.ts +2 -2
- package/src/sync/mutationHandler.test.ts +277 -1
- package/src/sync/mutationHandler.ts +171 -29
- package/src/sync/registry.ts +4 -0
- package/src/sync/routes.ts +52 -8
- package/src/sync/socketHandlers.ts +154 -58
- package/src/sync/streams.ts +6 -0
- package/src/sync/sync.test.ts +3 -2
- package/src/sync/syncApp.ts +2 -1
- package/src/sync/syncBatch.ts +7 -2
- package/src/sync/syncRoutes.test.ts +297 -3
- package/src/sync/syncSocket.test.ts +229 -0
- package/src/sync/types.ts +20 -0
- package/src/terrenoApp.migrations.test.ts +114 -0
- package/src/terrenoApp.test.ts +41 -0
- package/src/terrenoApp.ts +97 -31
- package/src/tests/createTestData.ts +2 -2
- package/src/tests/models.ts +10 -2
- package/src/tests/testHelper.ts +1 -7
- package/src/types/authToken.ts +2 -4
- package/src/types/mcpServiceToken.ts +1 -7
- package/src/types/membership.ts +48 -0
- package/src/types/notification.ts +34 -0
- package/src/types/notificationPreference.ts +28 -0
- package/src/types/organization.ts +49 -0
- package/src/types.d.ts +1 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +8 -10
- package/src/webhooks/rawBody.test.ts +33 -0
- package/src/webhooks/rawBody.ts +1 -0
- package/tsconfig.json +13 -54
- package/tsconfig.typedoc.json +4 -1
- package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.d.ts +0 -2
- package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
- package/src/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
3
|
if (k2 === undefined) k2 = k;
|
|
15
4
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -43,99 +32,28 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
43
32
|
return result;
|
|
44
33
|
};
|
|
45
34
|
})();
|
|
46
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
47
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
48
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
49
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
50
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
51
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
52
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
53
|
-
});
|
|
54
|
-
};
|
|
55
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
56
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
57
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
58
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
59
|
-
function step(op) {
|
|
60
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
61
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
62
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
63
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
64
|
-
switch (op[0]) {
|
|
65
|
-
case 0: case 1: t = op; break;
|
|
66
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
67
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
68
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
69
|
-
default:
|
|
70
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
71
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
72
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
73
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
74
|
-
if (t[2]) _.ops.pop();
|
|
75
|
-
_.trys.pop(); continue;
|
|
76
|
-
}
|
|
77
|
-
op = body.call(thisArg, _);
|
|
78
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
79
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
var __values = (this && this.__values) || function(o) {
|
|
83
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
84
|
-
if (m) return m.call(o);
|
|
85
|
-
if (o && typeof o.length === "number") return {
|
|
86
|
-
next: function () {
|
|
87
|
-
if (o && i >= o.length) o = void 0;
|
|
88
|
-
return { value: o && o[i++], done: !o };
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
92
|
-
};
|
|
93
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
94
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
95
|
-
if (!m) return o;
|
|
96
|
-
var i = m.call(o), r, ar = [], e;
|
|
97
|
-
try {
|
|
98
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
99
|
-
}
|
|
100
|
-
catch (error) { e = { error: error }; }
|
|
101
|
-
finally {
|
|
102
|
-
try {
|
|
103
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
104
|
-
}
|
|
105
|
-
finally { if (e) throw e.error; }
|
|
106
|
-
}
|
|
107
|
-
return ar;
|
|
108
|
-
};
|
|
109
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
110
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
111
|
-
if (ar || !(i in from)) {
|
|
112
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
113
|
-
ar[i] = from[i];
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
117
|
-
};
|
|
118
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
119
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
120
37
|
};
|
|
121
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
122
|
-
exports.stopChangeStreamWatcher = exports.startChangeStreamWatcher = exports.emitSyncDeltaForChange = exports.emitToDocumentAndQueryRooms = exports.emitToAuthorizedRoom = exports.emitPayloadToAuthorizedRoom = exports.serializeDoc = exports.ensureApiId = exports.resolveRooms = exports.mapOperationType = exports.DEFAULT_IGNORED_COLLECTIONS = exports.SYNC_RESYNC_EVENT =
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
39
|
+
exports.stopChangeStreamWatcher = exports.startChangeStreamWatcher = exports.emitSyncDeltaForChange = exports.emitToDocumentAndQueryRooms = exports.emitToAuthorizedRoom = exports.emitPayloadToAuthorizedRoom = exports.serializeDoc = exports.ensureApiId = exports.resolveRooms = exports.mapOperationType = exports.DEFAULT_IGNORED_COLLECTIONS = exports.SYNC_RESYNC_EVENT = void 0;
|
|
40
|
+
const Sentry = __importStar(require("@sentry/bun"));
|
|
41
|
+
const luxon_1 = require("luxon");
|
|
42
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
43
|
+
const errors_1 = require("../errors");
|
|
44
|
+
const logger_1 = require("../logger");
|
|
45
|
+
const realtimeAccess_1 = require("../rbac/realtimeAccess");
|
|
46
|
+
const adminBroadcastScope_1 = require("../sync/adminBroadcastScope");
|
|
47
|
+
const models_1 = require("../sync/models");
|
|
48
|
+
const registry_1 = require("../sync/registry");
|
|
49
|
+
const serialize_1 = require("../sync/serialize");
|
|
50
|
+
const socketHandlers_1 = require("../sync/socketHandlers");
|
|
51
|
+
const streams_1 = require("../sync/streams");
|
|
52
|
+
const queryMatcher_1 = require("./queryMatcher");
|
|
53
|
+
const queryStore_1 = require("./queryStore");
|
|
54
|
+
const registry_2 = require("./registry");
|
|
55
|
+
const socketUser_1 = require("./socketUser");
|
|
56
|
+
let changeWatcher = null;
|
|
139
57
|
/**
|
|
140
58
|
* Task 9.16: restart state for the single process-wide watcher.
|
|
141
59
|
*
|
|
@@ -146,34 +64,35 @@ var changeWatcher = null;
|
|
|
146
64
|
* observed resume token so no events in the gap are missed.
|
|
147
65
|
*/
|
|
148
66
|
/** Resume token of the last change seen, replayed via `resumeAfter` on restart. */
|
|
149
|
-
|
|
67
|
+
let lastResumeToken;
|
|
150
68
|
/** Bumped on every open/stop so callbacks from a replaced stream are ignored. */
|
|
151
|
-
|
|
69
|
+
let watcherGeneration = 0;
|
|
152
70
|
/** True between `stopChangeStreamWatcher()` and the next `startChangeStreamWatcher()`. */
|
|
153
|
-
|
|
71
|
+
let watcherStopped = true;
|
|
154
72
|
/** Consecutive failed opens, driving the backoff delay. Reset on a successful change. */
|
|
155
|
-
|
|
156
|
-
|
|
73
|
+
let restartAttempts = 0;
|
|
74
|
+
let restartTimer = null;
|
|
157
75
|
/** First restart delay; doubles per consecutive attempt up to the max. */
|
|
158
|
-
|
|
76
|
+
const CHANGE_STREAM_RESTART_BASE_DELAY_MS = 250;
|
|
159
77
|
/** Ceiling for the restart backoff — a long outage retries once every 30s, forever. */
|
|
160
|
-
|
|
78
|
+
const CHANGE_STREAM_RESTART_MAX_DELAY_MS = 30_000;
|
|
161
79
|
/**
|
|
162
80
|
* Socket event telling clients their cached cursors may have gaps and they must
|
|
163
81
|
* re-bootstrap. Emitted when the change stream's oplog history is gone
|
|
164
82
|
* (`ChangeStreamHistoryLost`), which is the one failure a resume token cannot repair.
|
|
165
83
|
* Payload: {@link SyncResyncHint}.
|
|
166
84
|
*/
|
|
85
|
+
/** @internal */
|
|
167
86
|
exports.SYNC_RESYNC_EVENT = "sync:resync-required";
|
|
168
87
|
/** MongoDB error code / codeName for an unresumable change stream (oplog rolled past). */
|
|
169
|
-
|
|
88
|
+
const CHANGE_STREAM_HISTORY_LOST_CODE = 286;
|
|
170
89
|
/** True when the error means the resume token can no longer be used. */
|
|
171
|
-
|
|
172
|
-
|
|
90
|
+
const isHistoryLostError = (error) => {
|
|
91
|
+
const { code, codeName, message } = (error ?? {});
|
|
173
92
|
return (code === CHANGE_STREAM_HISTORY_LOST_CODE ||
|
|
174
93
|
codeName === "ChangeStreamHistoryLost" ||
|
|
175
|
-
Boolean(message
|
|
176
|
-
Boolean(message
|
|
94
|
+
Boolean(message?.includes("ChangeStreamHistoryLost")) ||
|
|
95
|
+
Boolean(message?.includes("resume point may no longer be in the oplog")));
|
|
177
96
|
};
|
|
178
97
|
/**
|
|
179
98
|
* C8: per-entity serialized dispatch. The change-stream `"change"` handler is async and
|
|
@@ -183,13 +102,12 @@ var isHistoryLostError = function (error) {
|
|
|
183
102
|
* (`{collection}:{docId}`) so same-entity deltas dispatch strictly in change-stream
|
|
184
103
|
* order, while different entities still run concurrently. Chains self-clean when idle.
|
|
185
104
|
*/
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
var next = prior.then(task, task);
|
|
105
|
+
const entityDispatchChains = new Map();
|
|
106
|
+
const serializePerEntity = (key, task) => {
|
|
107
|
+
const prior = entityDispatchChains.get(key) ?? Promise.resolve();
|
|
108
|
+
const next = prior.then(task, task);
|
|
191
109
|
entityDispatchChains.set(key, next);
|
|
192
|
-
void next.finally(
|
|
110
|
+
void next.finally(() => {
|
|
193
111
|
// Drop the chain once this was the last queued task, so the map does not grow.
|
|
194
112
|
if (entityDispatchChains.get(key) === next) {
|
|
195
113
|
entityDispatchChains.delete(key);
|
|
@@ -201,6 +119,7 @@ var serializePerEntity = function (key, task) {
|
|
|
201
119
|
* events are internal (counters/ledger/markers/keys), and processing them would emit
|
|
202
120
|
* spurious deltas or reprocess scope-move markers. Exported for testing.
|
|
203
121
|
*/
|
|
122
|
+
/** @internal */
|
|
204
123
|
exports.DEFAULT_IGNORED_COLLECTIONS = [
|
|
205
124
|
"socketio",
|
|
206
125
|
"sessions",
|
|
@@ -221,9 +140,8 @@ exports.DEFAULT_IGNORED_COLLECTIONS = [
|
|
|
221
140
|
*
|
|
222
141
|
* Exported for testing.
|
|
223
142
|
*/
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
if (enabledMethods === void 0) { enabledMethods = ["create", "update", "delete"]; }
|
|
143
|
+
/** @internal */
|
|
144
|
+
const mapOperationType = (operationType, change, enabledMethods = ["create", "update", "delete"]) => {
|
|
227
145
|
if (operationType === "insert") {
|
|
228
146
|
return "create";
|
|
229
147
|
}
|
|
@@ -231,8 +149,8 @@ var mapOperationType = function (operationType, change, enabledMethods) {
|
|
|
231
149
|
// Soft delete on an update event: the document was patched with deleted=true.
|
|
232
150
|
// `change` is typed as the full union (without operationType narrowing) because
|
|
233
151
|
// callers/tests pass change objects without setting `operationType` on the change.
|
|
234
|
-
|
|
235
|
-
|
|
152
|
+
const updateChange = change;
|
|
153
|
+
const isSoftDelete = operationType === "update" && updateChange.updateDescription?.updatedFields?.deleted === true;
|
|
236
154
|
if (isSoftDelete && enabledMethods.includes("delete")) {
|
|
237
155
|
return "delete";
|
|
238
156
|
}
|
|
@@ -248,43 +166,32 @@ exports.mapOperationType = mapOperationType;
|
|
|
248
166
|
* Get the collection tag from a route path (strips leading "/").
|
|
249
167
|
* E.g. "/todos" -> "todos"
|
|
250
168
|
*/
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
var socketIds = io.sockets.adapter.rooms.get(room);
|
|
169
|
+
const getCollectionTag = (routePath) => routePath.replace(/^\//, "");
|
|
170
|
+
const getSocketsInRoom = (io, room) => {
|
|
171
|
+
const socketIds = io.sockets.adapter.rooms.get(room);
|
|
255
172
|
if (!socketIds) {
|
|
256
173
|
return [];
|
|
257
174
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
if (socket) {
|
|
264
|
-
sockets.push(socket);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
269
|
-
finally {
|
|
270
|
-
try {
|
|
271
|
-
if (socketIds_1_1 && !socketIds_1_1.done && (_a = socketIds_1.return)) _a.call(socketIds_1);
|
|
175
|
+
const sockets = [];
|
|
176
|
+
for (const socketId of socketIds) {
|
|
177
|
+
const socket = io.sockets.sockets.get(socketId);
|
|
178
|
+
if (socket) {
|
|
179
|
+
sockets.push(socket);
|
|
272
180
|
}
|
|
273
|
-
finally { if (e_1) throw e_1.error; }
|
|
274
181
|
}
|
|
275
182
|
return sockets;
|
|
276
183
|
};
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}); }); };
|
|
184
|
+
const emitCollectionTag = (entry) => entry.collectionTag ?? (entry.routePath ? getCollectionTag(entry.routePath) : undefined);
|
|
185
|
+
const canReadDocument = async (entry, user, doc) => (0, realtimeAccess_1.canReadDocumentRealtime)(entry, user, doc);
|
|
280
186
|
/**
|
|
281
187
|
* Determine which Socket.io rooms to emit to based on the room strategy.
|
|
282
188
|
* Exported for testing.
|
|
283
189
|
*/
|
|
284
|
-
|
|
285
|
-
|
|
190
|
+
/** @internal */
|
|
191
|
+
const resolveRooms = (entry, doc, method) => {
|
|
192
|
+
const { roomStrategy } = entry.config;
|
|
286
193
|
// Use the collection tag (e.g. "todos") for model rooms, matching what the frontend subscribes to
|
|
287
|
-
|
|
194
|
+
const collectionTag = getCollectionTag(entry.routePath);
|
|
288
195
|
if (typeof roomStrategy === "function") {
|
|
289
196
|
// Custom room resolver — pass a minimal pseudo-request. The strategy only inspects
|
|
290
197
|
// doc/method; we never read fields off req here, so an empty cast is safe.
|
|
@@ -292,19 +199,19 @@ var resolveRooms = function (entry, doc, method) {
|
|
|
292
199
|
}
|
|
293
200
|
switch (roomStrategy) {
|
|
294
201
|
case "owner": {
|
|
295
|
-
|
|
202
|
+
const ownerId = doc?.ownerId != null ? String(doc.ownerId) : undefined;
|
|
296
203
|
if (ownerId) {
|
|
297
|
-
return [
|
|
204
|
+
return [`user:${ownerId}`];
|
|
298
205
|
}
|
|
299
206
|
// If no ownerId, fall back to model room
|
|
300
|
-
return [
|
|
207
|
+
return [`model:${collectionTag}`];
|
|
301
208
|
}
|
|
302
209
|
case "model":
|
|
303
|
-
return [
|
|
210
|
+
return [`model:${collectionTag}`];
|
|
304
211
|
case "broadcast":
|
|
305
212
|
return ["authenticated"];
|
|
306
213
|
default:
|
|
307
|
-
return [
|
|
214
|
+
return [`model:${collectionTag}`];
|
|
308
215
|
}
|
|
309
216
|
};
|
|
310
217
|
exports.resolveRooms = resolveRooms;
|
|
@@ -312,13 +219,14 @@ exports.resolveRooms = resolveRooms;
|
|
|
312
219
|
* Ensure serialized documents include `id` to match REST API responses.
|
|
313
220
|
* Change stream fullDocument payloads are raw BSON objects with `_id` only.
|
|
314
221
|
*/
|
|
315
|
-
|
|
222
|
+
/** @internal */
|
|
223
|
+
const ensureApiId = (data) => {
|
|
316
224
|
if (data == null || typeof data !== "object" || Array.isArray(data)) {
|
|
317
225
|
return data;
|
|
318
226
|
}
|
|
319
|
-
|
|
227
|
+
const serialized = data;
|
|
320
228
|
if (serialized._id != null && serialized.id == null) {
|
|
321
|
-
return
|
|
229
|
+
return { ...serialized, id: serialized._id };
|
|
322
230
|
}
|
|
323
231
|
return data;
|
|
324
232
|
};
|
|
@@ -339,53 +247,40 @@ exports.ensureApiId = ensureApiId;
|
|
|
339
247
|
* would risk leaking unsanitized fields (e.g. `hash`/`salt`) that the handler
|
|
340
248
|
* was supposed to strip.
|
|
341
249
|
*/
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
case 0:
|
|
348
|
-
if (!entry.config.realtimeResponseHandler) return [3 /*break*/, 4];
|
|
349
|
-
_c.label = 1;
|
|
350
|
-
case 1:
|
|
351
|
-
_c.trys.push([1, 3, , 4]);
|
|
352
|
-
_a = exports.ensureApiId;
|
|
353
|
-
return [4 /*yield*/, entry.config.realtimeResponseHandler(doc, method)];
|
|
354
|
-
case 2: return [2 /*return*/, _a.apply(void 0, [_c.sent()])];
|
|
355
|
-
case 3:
|
|
356
|
-
error_1 = _c.sent();
|
|
357
|
-
logger_1.logger.error("[realtime] realtimeResponseHandler threw for ".concat(entry.modelName, "/").concat(method, ": ").concat(error_1, ". ") +
|
|
358
|
-
"Dropping event to avoid leaking unsanitized data.");
|
|
359
|
-
throw error_1;
|
|
360
|
-
case 4:
|
|
361
|
-
responseHandler = (_b = entry.options) === null || _b === void 0 ? void 0 : _b.responseHandler;
|
|
362
|
-
if (!responseHandler) return [3 /*break*/, 9];
|
|
363
|
-
_c.label = 5;
|
|
364
|
-
case 5:
|
|
365
|
-
_c.trys.push([5, 8, , 9]);
|
|
366
|
-
restMethod = method === "delete" ? "read" : method;
|
|
367
|
-
syntheticReq = { params: {}, query: {}, user: user };
|
|
368
|
-
return [4 /*yield*/, responseHandler(doc, restMethod, syntheticReq, entry.options)];
|
|
369
|
-
case 6:
|
|
370
|
-
serialized = _c.sent();
|
|
371
|
-
return [4 /*yield*/, (0, realtimeAccess_1.maskRealtimeDocument)(entry, user, serialized, restMethod)];
|
|
372
|
-
case 7:
|
|
373
|
-
masked = _c.sent();
|
|
374
|
-
return [2 /*return*/, (0, exports.ensureApiId)(masked)];
|
|
375
|
-
case 8:
|
|
376
|
-
error_2 = _c.sent();
|
|
377
|
-
logger_1.logger.error("[realtime] modelRouter responseHandler threw during realtime serialization for " +
|
|
378
|
-
"".concat(entry.modelName, "/").concat(method, ": ").concat(error_2, ". Dropping event to avoid leaking unsanitized data."));
|
|
379
|
-
throw error_2;
|
|
380
|
-
case 9:
|
|
381
|
-
fallback = typeof doc.toJSON === "function" ? doc.toJSON() : doc;
|
|
382
|
-
return [4 /*yield*/, (0, realtimeAccess_1.maskRealtimeDocument)(entry, user, fallback, method === "delete" ? "read" : method)];
|
|
383
|
-
case 10:
|
|
384
|
-
maskedFallback = _c.sent();
|
|
385
|
-
return [2 /*return*/, (0, exports.ensureApiId)(maskedFallback)];
|
|
250
|
+
/** @internal */
|
|
251
|
+
const serializeDoc = async (entry, doc, method, user) => {
|
|
252
|
+
if (entry.config.realtimeResponseHandler) {
|
|
253
|
+
try {
|
|
254
|
+
return (0, exports.ensureApiId)(await entry.config.realtimeResponseHandler(doc, method));
|
|
386
255
|
}
|
|
387
|
-
|
|
388
|
-
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
logger_1.logger.error(`[realtime] realtimeResponseHandler threw for ${entry.modelName}/${method}: ${error}. ` +
|
|
258
|
+
"Dropping event to avoid leaking unsanitized data.");
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
const responseHandler = entry.options?.responseHandler;
|
|
263
|
+
if (responseHandler) {
|
|
264
|
+
try {
|
|
265
|
+
// The REST responseHandler signature expects a "list" | "create" | "read" | "update" method.
|
|
266
|
+
// Map "delete" → "read" so handlers that branch on method receive a sane value.
|
|
267
|
+
const restMethod = method === "delete" ? "read" : method;
|
|
268
|
+
// Synthesize the minimal request shape responseHandlers commonly inspect.
|
|
269
|
+
const syntheticReq = { params: {}, query: {}, user };
|
|
270
|
+
const serialized = await responseHandler(doc, restMethod, syntheticReq, entry.options);
|
|
271
|
+
const masked = await (0, realtimeAccess_1.maskRealtimeDocument)(entry, user, serialized, restMethod);
|
|
272
|
+
return (0, exports.ensureApiId)(masked);
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
logger_1.logger.error(`[realtime] modelRouter responseHandler threw during realtime serialization for ` +
|
|
276
|
+
`${entry.modelName}/${method}: ${error}. Dropping event to avoid leaking unsanitized data.`);
|
|
277
|
+
throw error;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
const fallback = typeof doc.toJSON === "function" ? doc.toJSON() : doc;
|
|
281
|
+
const maskedFallback = await (0, realtimeAccess_1.maskRealtimeDocument)(entry, user, fallback, method === "delete" ? "read" : method);
|
|
282
|
+
return (0, exports.ensureApiId)(maskedFallback);
|
|
283
|
+
};
|
|
389
284
|
exports.serializeDoc = serializeDoc;
|
|
390
285
|
/**
|
|
391
286
|
* Generalized authorized-room emitter: iterates the sockets in `room`, runs the per-socket
|
|
@@ -395,93 +290,60 @@ exports.serializeDoc = serializeDoc;
|
|
|
395
290
|
* `emitToAuthorizedRoom` (legacy "sync" events) and the sync layer's `sync:delta`
|
|
396
291
|
* emission both delegate here.
|
|
397
292
|
*/
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
user = _g.sent();
|
|
417
|
-
_g.label = 4;
|
|
418
|
-
case 4:
|
|
419
|
-
_g.trys.push([4, 7, , 8]);
|
|
420
|
-
permissionDocument = fullDocument !== null && fullDocument !== void 0 ? fullDocument : {};
|
|
421
|
-
return [4 /*yield*/, canReadDocument(entry, user, permissionDocument)];
|
|
422
|
-
case 5:
|
|
423
|
-
canRead = _g.sent();
|
|
424
|
-
if (!canRead) {
|
|
425
|
-
logDebug("[realtime] Skipped ".concat(room, " for ").concat(socket.id, ": read permission denied"));
|
|
426
|
-
return [3 /*break*/, 8];
|
|
293
|
+
const emitPayloadToAuthorizedRoom = async ({ io, room, eventName, entry, fullDocument, buildPayload, logDebug, }) => {
|
|
294
|
+
const sockets = getSocketsInRoom(io, room);
|
|
295
|
+
for (const socket of sockets) {
|
|
296
|
+
const user = await (0, socketUser_1.awaitSocketFullUser)(socket);
|
|
297
|
+
try {
|
|
298
|
+
// Hard deletes have no document context; use an empty object so object-scoped
|
|
299
|
+
// permission helpers fail closed instead of treating the check as preflight.
|
|
300
|
+
const permissionDocument = fullDocument ?? {};
|
|
301
|
+
const collectionTag = emitCollectionTag(entry);
|
|
302
|
+
if (collectionTag && (0, adminBroadcastScope_1.isAdminBroadcastSocketRoom)(room, collectionTag)) {
|
|
303
|
+
const adminDecision = await (0, adminBroadcastScope_1.authorizeAdminBroadcastDocument)({
|
|
304
|
+
doc: fullDocument,
|
|
305
|
+
modelName: entry.modelName,
|
|
306
|
+
user,
|
|
307
|
+
});
|
|
308
|
+
if (adminDecision === "deny") {
|
|
309
|
+
logDebug(`[realtime] Skipped ${room} for ${socket.id}: admin broadcast scope denied`);
|
|
310
|
+
continue;
|
|
427
311
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
case 6:
|
|
432
|
-
_d.apply(_c, _e.concat([_g.sent()]));
|
|
433
|
-
return [3 /*break*/, 8];
|
|
434
|
-
case 7:
|
|
435
|
-
error_3 = _g.sent();
|
|
436
|
-
logger_1.logger.error("[realtime] Failed to emit ".concat(eventName, " for ").concat(entry.modelName, " to socket ").concat(socket.id, ": ").concat(error_3));
|
|
437
|
-
Sentry.captureException(error_3);
|
|
438
|
-
return [3 /*break*/, 8];
|
|
439
|
-
case 8:
|
|
440
|
-
sockets_1_1 = sockets_1.next();
|
|
441
|
-
return [3 /*break*/, 2];
|
|
442
|
-
case 9: return [3 /*break*/, 12];
|
|
443
|
-
case 10:
|
|
444
|
-
e_2_1 = _g.sent();
|
|
445
|
-
e_2 = { error: e_2_1 };
|
|
446
|
-
return [3 /*break*/, 12];
|
|
447
|
-
case 11:
|
|
448
|
-
try {
|
|
449
|
-
if (sockets_1_1 && !sockets_1_1.done && (_f = sockets_1.return)) _f.call(sockets_1);
|
|
312
|
+
if (adminDecision === "allow") {
|
|
313
|
+
socket.emit(eventName, await buildPayload(user));
|
|
314
|
+
continue;
|
|
450
315
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
316
|
+
}
|
|
317
|
+
const canRead = await canReadDocument(entry, user, permissionDocument);
|
|
318
|
+
if (!canRead) {
|
|
319
|
+
logDebug(`[realtime] Skipped ${room} for ${socket.id}: read permission denied`);
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
socket.emit(eventName, await buildPayload(user));
|
|
454
323
|
}
|
|
455
|
-
|
|
456
|
-
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
logger_1.logger.error(`[realtime] Failed to emit ${eventName} for ${entry.modelName} to socket ${socket.id}: ${error}`);
|
|
326
|
+
Sentry.captureException(error);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
};
|
|
457
330
|
exports.emitPayloadToAuthorizedRoom = emitPayloadToAuthorizedRoom;
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
});
|
|
475
|
-
}); },
|
|
476
|
-
entry: entry,
|
|
477
|
-
eventName: "sync",
|
|
478
|
-
fullDocument: fullDocument,
|
|
479
|
-
io: io,
|
|
480
|
-
logDebug: logDebug,
|
|
481
|
-
room: room,
|
|
482
|
-
})];
|
|
483
|
-
});
|
|
484
|
-
}); };
|
|
331
|
+
/** @internal */
|
|
332
|
+
const emitToAuthorizedRoom = async (io, room, event, entry, fullDocument, logDebug) => (0, exports.emitPayloadToAuthorizedRoom)({
|
|
333
|
+
buildPayload: async (user) => {
|
|
334
|
+
if (!fullDocument) {
|
|
335
|
+
return event;
|
|
336
|
+
}
|
|
337
|
+
const data = await (0, exports.serializeDoc)(entry, fullDocument, event.method, user);
|
|
338
|
+
return { ...event, data };
|
|
339
|
+
},
|
|
340
|
+
entry,
|
|
341
|
+
eventName: "sync",
|
|
342
|
+
fullDocument,
|
|
343
|
+
io,
|
|
344
|
+
logDebug,
|
|
345
|
+
room,
|
|
346
|
+
});
|
|
485
347
|
exports.emitToAuthorizedRoom = emitToAuthorizedRoom;
|
|
486
348
|
/**
|
|
487
349
|
* Emit a sync event to document-specific and query rooms.
|
|
@@ -499,226 +361,170 @@ exports.emitToAuthorizedRoom = emitToAuthorizedRoom;
|
|
|
499
361
|
*
|
|
500
362
|
* Exported for testing.
|
|
501
363
|
*/
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
querySubscriptions = (0, queryStore_1.getQuerySubscriptionsForCollection)(collection);
|
|
521
|
-
_c.label = 4;
|
|
522
|
-
case 4:
|
|
523
|
-
_c.trys.push([4, 28, 29, 30]);
|
|
524
|
-
querySubscriptions_1 = __values(querySubscriptions), querySubscriptions_1_1 = querySubscriptions_1.next();
|
|
525
|
-
_c.label = 5;
|
|
526
|
-
case 5:
|
|
527
|
-
if (!!querySubscriptions_1_1.done) return [3 /*break*/, 27];
|
|
528
|
-
_a = querySubscriptions_1_1.value, queryId = _a.queryId, query = _a.query;
|
|
529
|
-
queryRoom = "query:".concat(queryId);
|
|
530
|
-
if (!(event.method === "delete")) return [3 /*break*/, 14];
|
|
531
|
-
if (!!fullDocument) return [3 /*break*/, 9];
|
|
364
|
+
/** @internal */
|
|
365
|
+
const emitToDocumentAndQueryRooms = async (io, collection, event, fullDocument, logDebug, entry) => {
|
|
366
|
+
// Emit to document-specific room
|
|
367
|
+
const docRoom = `document:${collection}:${event.id}`;
|
|
368
|
+
if (entry) {
|
|
369
|
+
await (0, exports.emitToAuthorizedRoom)(io, docRoom, event, entry, fullDocument, logDebug);
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
io.to(docRoom).emit("sync", event);
|
|
373
|
+
}
|
|
374
|
+
logDebug(`[realtime] Emitted ${event.method} to ${docRoom}`);
|
|
375
|
+
const isOwnerStrategy = entry?.config.roomStrategy === "owner";
|
|
376
|
+
// Evaluate query subscriptions
|
|
377
|
+
const querySubscriptions = (0, queryStore_1.getQuerySubscriptionsForCollection)(collection);
|
|
378
|
+
for (const { queryId, query } of querySubscriptions) {
|
|
379
|
+
const queryRoom = `query:${queryId}`;
|
|
380
|
+
if (event.method === "delete") {
|
|
381
|
+
if (!fullDocument) {
|
|
532
382
|
// Hard delete with no document context. For owner-strategy collections we can't
|
|
533
383
|
// tell which query rooms belong to the owner without leaking activity to others,
|
|
534
384
|
// so skip query fanout entirely — subscribers will reconcile on next fetch.
|
|
535
385
|
if (isOwnerStrategy) {
|
|
536
|
-
logDebug(
|
|
537
|
-
|
|
386
|
+
logDebug(`[realtime] Skipping hard delete fanout to ${queryRoom} (owner strategy, no fullDocument)`);
|
|
387
|
+
continue;
|
|
538
388
|
}
|
|
539
|
-
if (
|
|
540
|
-
|
|
541
|
-
case 6:
|
|
542
|
-
_c.sent();
|
|
543
|
-
return [3 /*break*/, 8];
|
|
544
|
-
case 7:
|
|
545
|
-
io.to(queryRoom).emit("sync", event);
|
|
546
|
-
_c.label = 8;
|
|
547
|
-
case 8:
|
|
548
|
-
logDebug("[realtime] Emitted hard delete to ".concat(queryRoom));
|
|
549
|
-
return [3 /*break*/, 26];
|
|
550
|
-
case 9:
|
|
551
|
-
if (!(0, queryMatcher_1.matchesQuery)(fullDocument, query)) return [3 /*break*/, 13];
|
|
552
|
-
if (!entry) return [3 /*break*/, 11];
|
|
553
|
-
return [4 /*yield*/, (0, exports.emitToAuthorizedRoom)(io, queryRoom, event, entry, fullDocument, logDebug)];
|
|
554
|
-
case 10:
|
|
555
|
-
_c.sent();
|
|
556
|
-
return [3 /*break*/, 12];
|
|
557
|
-
case 11:
|
|
558
|
-
io.to(queryRoom).emit("sync", event);
|
|
559
|
-
_c.label = 12;
|
|
560
|
-
case 12:
|
|
561
|
-
logDebug("[realtime] Emitted soft delete to ".concat(queryRoom, " (query matched)"));
|
|
562
|
-
_c.label = 13;
|
|
563
|
-
case 13: return [3 /*break*/, 26];
|
|
564
|
-
case 14:
|
|
565
|
-
if (!fullDocument) {
|
|
566
|
-
return [3 /*break*/, 26];
|
|
389
|
+
if (entry) {
|
|
390
|
+
await (0, exports.emitToAuthorizedRoom)(io, queryRoom, event, entry, fullDocument, logDebug);
|
|
567
391
|
}
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
392
|
+
else {
|
|
393
|
+
io.to(queryRoom).emit("sync", event);
|
|
394
|
+
}
|
|
395
|
+
logDebug(`[realtime] Emitted hard delete to ${queryRoom}`);
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
// Soft delete: only forward to query rooms whose filter the document satisfies.
|
|
399
|
+
if ((0, queryMatcher_1.matchesQuery)(fullDocument, query)) {
|
|
400
|
+
if (entry) {
|
|
401
|
+
await (0, exports.emitToAuthorizedRoom)(io, queryRoom, event, entry, fullDocument, logDebug);
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
io.to(queryRoom).emit("sync", event);
|
|
405
|
+
}
|
|
406
|
+
logDebug(`[realtime] Emitted soft delete to ${queryRoom} (query matched)`);
|
|
407
|
+
}
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
if (!fullDocument) {
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
const docMatches = (0, queryMatcher_1.matchesQuery)(fullDocument, query);
|
|
414
|
+
if (event.method === "create" && docMatches) {
|
|
415
|
+
// New document matches the query — send create event
|
|
416
|
+
if (entry) {
|
|
417
|
+
await (0, exports.emitToAuthorizedRoom)(io, queryRoom, event, entry, fullDocument, logDebug);
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
590
420
|
io.to(queryRoom).emit("sync", event);
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
if (
|
|
598
|
-
|
|
599
|
-
case 23:
|
|
600
|
-
_c.sent();
|
|
601
|
-
return [3 /*break*/, 25];
|
|
602
|
-
case 24:
|
|
603
|
-
io.to(queryRoom).emit("sync", removeEvent);
|
|
604
|
-
_c.label = 25;
|
|
605
|
-
case 25:
|
|
606
|
-
logDebug("[realtime] Emitted delete to ".concat(queryRoom, " (query no longer matched)"));
|
|
607
|
-
_c.label = 26;
|
|
608
|
-
case 26:
|
|
609
|
-
querySubscriptions_1_1 = querySubscriptions_1.next();
|
|
610
|
-
return [3 /*break*/, 5];
|
|
611
|
-
case 27: return [3 /*break*/, 30];
|
|
612
|
-
case 28:
|
|
613
|
-
e_3_1 = _c.sent();
|
|
614
|
-
e_3 = { error: e_3_1 };
|
|
615
|
-
return [3 /*break*/, 30];
|
|
616
|
-
case 29:
|
|
617
|
-
try {
|
|
618
|
-
if (querySubscriptions_1_1 && !querySubscriptions_1_1.done && (_b = querySubscriptions_1.return)) _b.call(querySubscriptions_1);
|
|
421
|
+
}
|
|
422
|
+
logDebug(`[realtime] Emitted create to ${queryRoom} (query matched)`);
|
|
423
|
+
}
|
|
424
|
+
else if (event.method === "update") {
|
|
425
|
+
if (docMatches) {
|
|
426
|
+
// Document still matches (or newly matches) — send update event
|
|
427
|
+
if (entry) {
|
|
428
|
+
await (0, exports.emitToAuthorizedRoom)(io, queryRoom, event, entry, fullDocument, logDebug);
|
|
619
429
|
}
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
430
|
+
else {
|
|
431
|
+
io.to(queryRoom).emit("sync", event);
|
|
432
|
+
}
|
|
433
|
+
logDebug(`[realtime] Emitted update to ${queryRoom} (query matched)`);
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
// Document no longer matches the query — send delete event so client removes it
|
|
437
|
+
const removeEvent = {
|
|
438
|
+
...event,
|
|
439
|
+
method: "delete",
|
|
440
|
+
};
|
|
441
|
+
if (entry) {
|
|
442
|
+
await (0, exports.emitToAuthorizedRoom)(io, queryRoom, removeEvent, entry, fullDocument, logDebug);
|
|
443
|
+
}
|
|
444
|
+
else {
|
|
445
|
+
io.to(queryRoom).emit("sync", removeEvent);
|
|
446
|
+
}
|
|
447
|
+
logDebug(`[realtime] Emitted delete to ${queryRoom} (query no longer matched)`);
|
|
448
|
+
}
|
|
623
449
|
}
|
|
624
|
-
}
|
|
625
|
-
}
|
|
450
|
+
}
|
|
451
|
+
};
|
|
626
452
|
exports.emitToDocumentAndQueryRooms = emitToDocumentAndQueryRooms;
|
|
627
453
|
/**
|
|
628
454
|
* Emit legacy realtime "sync" events for a change on a realtime-registered collection.
|
|
629
455
|
* Extracted verbatim from the change handler so sync-delta emission (which is keyed off
|
|
630
456
|
* the independent sync registry) runs even when the realtime path skips the change.
|
|
631
457
|
*/
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
else if (entry.config.roomStrategy === "broadcast") {
|
|
660
|
-
rooms = ["authenticated"];
|
|
661
|
-
}
|
|
662
|
-
else {
|
|
663
|
-
collectionTag = getCollectionTag(entry.routePath);
|
|
664
|
-
rooms = ["model:".concat(collectionTag)];
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
else {
|
|
668
|
-
rooms = (0, exports.resolveRooms)(entry, fullDocument !== null && fullDocument !== void 0 ? fullDocument : {}, method);
|
|
669
|
-
}
|
|
670
|
-
collection = getCollectionTag(entry.routePath);
|
|
671
|
-
event = __assign({ collection: collection, id: docId, method: method, model: entry.modelName, timestamp: luxon_1.DateTime.now().toMillis() }, (change.operationType === "update" && ((_d = change.updateDescription) === null || _d === void 0 ? void 0 : _d.updatedFields)
|
|
672
|
-
? { updatedFields: Object.keys(change.updateDescription.updatedFields) }
|
|
673
|
-
: {}));
|
|
674
|
-
_e.label = 1;
|
|
675
|
-
case 1:
|
|
676
|
-
_e.trys.push([1, 6, 7, 8]);
|
|
677
|
-
rooms_1 = __values(rooms), rooms_1_1 = rooms_1.next();
|
|
678
|
-
_e.label = 2;
|
|
679
|
-
case 2:
|
|
680
|
-
if (!!rooms_1_1.done) return [3 /*break*/, 5];
|
|
681
|
-
room = rooms_1_1.value;
|
|
682
|
-
return [4 /*yield*/, (0, exports.emitToAuthorizedRoom)(io, room, event, entry, fullDocument, logDebug)];
|
|
683
|
-
case 3:
|
|
684
|
-
_e.sent();
|
|
685
|
-
_e.label = 4;
|
|
686
|
-
case 4:
|
|
687
|
-
rooms_1_1 = rooms_1.next();
|
|
688
|
-
return [3 /*break*/, 2];
|
|
689
|
-
case 5: return [3 /*break*/, 8];
|
|
690
|
-
case 6:
|
|
691
|
-
e_4_1 = _e.sent();
|
|
692
|
-
e_4 = { error: e_4_1 };
|
|
693
|
-
return [3 /*break*/, 8];
|
|
694
|
-
case 7:
|
|
695
|
-
try {
|
|
696
|
-
if (rooms_1_1 && !rooms_1_1.done && (_c = rooms_1.return)) _c.call(rooms_1);
|
|
697
|
-
}
|
|
698
|
-
finally { if (e_4) throw e_4.error; }
|
|
699
|
-
return [7 /*endfinally*/];
|
|
700
|
-
case 8:
|
|
701
|
-
// Emit to document-specific and query rooms
|
|
702
|
-
return [4 /*yield*/, (0, exports.emitToDocumentAndQueryRooms)(io, collection, event, fullDocument, logDebug, entry)];
|
|
703
|
-
case 9:
|
|
704
|
-
// Emit to document-specific and query rooms
|
|
705
|
-
_e.sent();
|
|
706
|
-
logDebug("[realtime] Emitted ".concat(method, " for ").concat(entry.modelName, "/").concat(docId, " to rooms: ").concat(rooms.join(", ")));
|
|
707
|
-
metadata = {
|
|
708
|
-
collection: event.collection,
|
|
709
|
-
id: event.id,
|
|
710
|
-
method: event.method,
|
|
711
|
-
model: event.model,
|
|
712
|
-
timestamp: event.timestamp,
|
|
713
|
-
};
|
|
714
|
-
if (event.updatedFields) {
|
|
715
|
-
metadata.updatedFields = event.updatedFields;
|
|
716
|
-
}
|
|
717
|
-
logInfo("[realtime] sync event: ".concat(JSON.stringify(metadata)));
|
|
718
|
-
return [2 /*return*/];
|
|
458
|
+
const processRealtimeChange = async ({ io, entry, change, docId, logDebug, logInfo, }) => {
|
|
459
|
+
// Map to our method type. Pass enabledMethods so soft deletes only
|
|
460
|
+
// remap to "delete" when the model actually subscribes to deletes —
|
|
461
|
+
// otherwise the change is kept as "update" so update subscribers
|
|
462
|
+
// still receive it.
|
|
463
|
+
const method = (0, exports.mapOperationType)(change.operationType, change, entry.config.methods);
|
|
464
|
+
if (!method) {
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
// Check if this method is enabled for this model
|
|
468
|
+
if (!entry.config.methods.includes(method)) {
|
|
469
|
+
logDebug(`[realtime] Method ${method} not enabled for ${entry.modelName}`);
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
// fullDocument is present on insert/update/replace; absent on delete.
|
|
473
|
+
const fullDocument = change.operationType === "delete" ? undefined : change.fullDocument;
|
|
474
|
+
// For hard deletes, we don't have the full document
|
|
475
|
+
const isHardDelete = method === "delete" && !fullDocument;
|
|
476
|
+
// Determine target rooms
|
|
477
|
+
let rooms;
|
|
478
|
+
if (isHardDelete) {
|
|
479
|
+
// Hard delete: no fullDocument, so we can't resolve owner/custom rooms.
|
|
480
|
+
// For owner strategy we cannot safely fan out to a model room without
|
|
481
|
+
// leaking deletes across users — admins still receive the event via the
|
|
482
|
+
// admin room and any document-specific subscribers via document rooms.
|
|
483
|
+
if (entry.config.roomStrategy === "owner") {
|
|
484
|
+
rooms = ["admin"];
|
|
719
485
|
}
|
|
720
|
-
|
|
721
|
-
|
|
486
|
+
else if (entry.config.roomStrategy === "broadcast") {
|
|
487
|
+
rooms = ["authenticated"];
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
const collectionTag = getCollectionTag(entry.routePath);
|
|
491
|
+
rooms = [`model:${collectionTag}`];
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
else {
|
|
495
|
+
rooms = (0, exports.resolveRooms)(entry, fullDocument ?? {}, method);
|
|
496
|
+
}
|
|
497
|
+
const collection = getCollectionTag(entry.routePath);
|
|
498
|
+
const event = {
|
|
499
|
+
collection,
|
|
500
|
+
id: docId,
|
|
501
|
+
method,
|
|
502
|
+
model: entry.modelName,
|
|
503
|
+
timestamp: luxon_1.DateTime.now().toMillis(),
|
|
504
|
+
...(change.operationType === "update" && change.updateDescription?.updatedFields
|
|
505
|
+
? { updatedFields: Object.keys(change.updateDescription.updatedFields) }
|
|
506
|
+
: {}),
|
|
507
|
+
};
|
|
508
|
+
// Emit to strategy-based rooms (model/owner/broadcast)
|
|
509
|
+
for (const room of rooms) {
|
|
510
|
+
await (0, exports.emitToAuthorizedRoom)(io, room, event, entry, fullDocument, logDebug);
|
|
511
|
+
}
|
|
512
|
+
// Emit to document-specific and query rooms
|
|
513
|
+
await (0, exports.emitToDocumentAndQueryRooms)(io, collection, event, fullDocument, logDebug, entry);
|
|
514
|
+
logDebug(`[realtime] Emitted ${method} for ${entry.modelName}/${docId} to rooms: ${rooms.join(", ")}`);
|
|
515
|
+
// Log only metadata — never the document payload, which may contain sensitive fields.
|
|
516
|
+
const metadata = {
|
|
517
|
+
collection: event.collection,
|
|
518
|
+
id: event.id,
|
|
519
|
+
method: event.method,
|
|
520
|
+
model: event.model,
|
|
521
|
+
timestamp: event.timestamp,
|
|
522
|
+
};
|
|
523
|
+
if (event.updatedFields) {
|
|
524
|
+
metadata.updatedFields = event.updatedFields;
|
|
525
|
+
}
|
|
526
|
+
logInfo(`[realtime] sync event: ${JSON.stringify(metadata)}`);
|
|
527
|
+
};
|
|
722
528
|
/**
|
|
723
529
|
* Emit `sync:delta` events for a change on a sync-registered collection.
|
|
724
530
|
*
|
|
@@ -748,150 +554,103 @@ var processRealtimeChange = function (_a) { return __awaiter(void 0, [_a], void
|
|
|
748
554
|
*
|
|
749
555
|
* Exported for testing.
|
|
750
556
|
*/
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
deleted = fullDocument.deleted === true;
|
|
777
|
-
seq = typeof fullDocument._syncSeq === "number" ? fullDocument._syncSeq : 0;
|
|
778
|
-
stream = (0, streams_1.resolveStreamForDoc)({
|
|
779
|
-
collectionTag: entry.collectionTag,
|
|
780
|
-
doc: fullDocument,
|
|
781
|
-
scope: entry.config.scope,
|
|
782
|
-
});
|
|
783
|
-
return [4 /*yield*/, models_1.SyncScopeMove.find({
|
|
784
|
-
collectionTag: entry.collectionTag,
|
|
785
|
-
entityId: docId,
|
|
786
|
-
}).lean()];
|
|
787
|
-
case 1:
|
|
788
|
-
markers = _d.sent();
|
|
789
|
-
movedAway = false;
|
|
790
|
-
_loop_1 = function (marker) {
|
|
791
|
-
var markerFrontier, tombstone;
|
|
792
|
-
return __generator(this, function (_e) {
|
|
793
|
-
switch (_e.label) {
|
|
794
|
-
case 0:
|
|
795
|
-
if (marker.fromStream === stream) {
|
|
796
|
-
return [2 /*return*/, "continue"];
|
|
797
|
-
}
|
|
798
|
-
movedAway = true;
|
|
799
|
-
return [4 /*yield*/, (0, models_1.computeStableFrontier)({ stream: marker.fromStream })];
|
|
800
|
-
case 1:
|
|
801
|
-
markerFrontier = _e.sent();
|
|
802
|
-
tombstone = {
|
|
803
|
-
collection: entry.collectionTag,
|
|
804
|
-
deleted: true,
|
|
805
|
-
frontierSeq: markerFrontier,
|
|
806
|
-
id: docId,
|
|
807
|
-
method: "delete",
|
|
808
|
-
seq: marker.seq,
|
|
809
|
-
stream: marker.fromStream,
|
|
810
|
-
};
|
|
811
|
-
return [4 /*yield*/, (0, exports.emitPayloadToAuthorizedRoom)({
|
|
812
|
-
buildPayload: function () { return tombstone; },
|
|
813
|
-
entry: entry,
|
|
814
|
-
eventName: "sync:delta",
|
|
815
|
-
fullDocument: fullDocument,
|
|
816
|
-
io: io,
|
|
817
|
-
logDebug: logDebug,
|
|
818
|
-
room: (0, socketHandlers_1.syncRoomForStream)(marker.fromStream),
|
|
819
|
-
})];
|
|
820
|
-
case 2:
|
|
821
|
-
_e.sent();
|
|
822
|
-
logDebug("[sync] Emitted scope-move tombstone for ".concat(entry.collectionTag, "/").concat(docId, " to ").concat(marker.fromStream, " seq=").concat(marker.seq));
|
|
823
|
-
return [2 /*return*/];
|
|
824
|
-
}
|
|
825
|
-
});
|
|
826
|
-
};
|
|
827
|
-
_d.label = 2;
|
|
828
|
-
case 2:
|
|
829
|
-
_d.trys.push([2, 7, 8, 9]);
|
|
830
|
-
markers_1 = __values(markers), markers_1_1 = markers_1.next();
|
|
831
|
-
_d.label = 3;
|
|
832
|
-
case 3:
|
|
833
|
-
if (!!markers_1_1.done) return [3 /*break*/, 6];
|
|
834
|
-
marker = markers_1_1.value;
|
|
835
|
-
return [5 /*yield**/, _loop_1(marker)];
|
|
836
|
-
case 4:
|
|
837
|
-
_d.sent();
|
|
838
|
-
_d.label = 5;
|
|
839
|
-
case 5:
|
|
840
|
-
markers_1_1 = markers_1.next();
|
|
841
|
-
return [3 /*break*/, 3];
|
|
842
|
-
case 6: return [3 /*break*/, 9];
|
|
843
|
-
case 7:
|
|
844
|
-
e_5_1 = _d.sent();
|
|
845
|
-
e_5 = { error: e_5_1 };
|
|
846
|
-
return [3 /*break*/, 9];
|
|
847
|
-
case 8:
|
|
848
|
-
try {
|
|
849
|
-
if (markers_1_1 && !markers_1_1.done && (_c = markers_1.return)) _c.call(markers_1);
|
|
850
|
-
}
|
|
851
|
-
finally { if (e_5) throw e_5.error; }
|
|
852
|
-
return [7 /*endfinally*/];
|
|
853
|
-
case 9:
|
|
854
|
-
// The new stream sees a moved-away doc for the first time: report it as a create
|
|
855
|
-
// (unless the same write also soft-deleted the doc, in which case the tombstone wins).
|
|
856
|
-
if (movedAway && method !== "delete") {
|
|
857
|
-
method = "create";
|
|
858
|
-
}
|
|
859
|
-
return [4 /*yield*/, (0, models_1.computeStableFrontier)({ stream: stream })];
|
|
860
|
-
case 10:
|
|
861
|
-
frontierSeq = _d.sent();
|
|
862
|
-
delta = __assign({ collection: entry.collectionTag, frontierSeq: frontierSeq, id: docId, method: method, seq: seq, stream: stream }, (deleted ? { deleted: true } : {}));
|
|
863
|
-
return [4 /*yield*/, (0, exports.emitPayloadToAuthorizedRoom)({
|
|
864
|
-
// C7: tombstone deltas carry no data (only id/seq/deleted); live deltas serialize.
|
|
865
|
-
buildPayload: deleted
|
|
866
|
-
? function () { return delta; }
|
|
867
|
-
: function (user) { return __awaiter(void 0, void 0, void 0, function () {
|
|
868
|
-
var syntheticReq, data, _a;
|
|
869
|
-
return __generator(this, function (_b) {
|
|
870
|
-
switch (_b.label) {
|
|
871
|
-
case 0:
|
|
872
|
-
syntheticReq = { params: {}, query: {}, user: user };
|
|
873
|
-
_a = exports.ensureApiId;
|
|
874
|
-
return [4 /*yield*/, (0, serialize_1.serializeSyncPayload)({ doc: fullDocument, entry: entry, method: method, req: syntheticReq })];
|
|
875
|
-
case 1:
|
|
876
|
-
data = _a.apply(void 0, [_b.sent()]);
|
|
877
|
-
return [2 /*return*/, __assign(__assign({}, delta), { data: data })];
|
|
878
|
-
}
|
|
879
|
-
});
|
|
880
|
-
}); },
|
|
881
|
-
entry: entry,
|
|
882
|
-
eventName: "sync:delta",
|
|
883
|
-
fullDocument: fullDocument,
|
|
884
|
-
io: io,
|
|
885
|
-
logDebug: logDebug,
|
|
886
|
-
room: (0, socketHandlers_1.syncRoomForStream)(stream),
|
|
887
|
-
})];
|
|
888
|
-
case 11:
|
|
889
|
-
_d.sent();
|
|
890
|
-
logDebug("[sync] Emitted sync:delta ".concat(method, " for ").concat(entry.collectionTag, "/").concat(docId, " seq=").concat(seq, " stream=").concat(stream, " frontier=").concat(frontierSeq));
|
|
891
|
-
return [2 /*return*/];
|
|
892
|
-
}
|
|
557
|
+
const emitSyncDeltaForChange = async ({ io, entry, change, docId, logDebug, }) => {
|
|
558
|
+
if (change.operationType === "delete") {
|
|
559
|
+
// Hard deletes are blocked on synced models by syncPlugin; a direct collection-level
|
|
560
|
+
// delete has no post-image, so neither stream nor seq can be resolved. Clients
|
|
561
|
+
// reconcile via snapshot instead.
|
|
562
|
+
logDebug(`[sync] Skipping hard delete for synced collection ${entry.collectionTag}/${docId}: ` +
|
|
563
|
+
"no post-image to resolve stream/seq");
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
const fullDocument = change.fullDocument;
|
|
567
|
+
if (!fullDocument) {
|
|
568
|
+
logDebug(`[sync] Skipping ${entry.collectionTag}/${docId}: change has no fullDocument`);
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
const mapped = (0, exports.mapOperationType)(change.operationType, change);
|
|
572
|
+
if (!mapped) {
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
let method = mapped;
|
|
576
|
+
const deleted = fullDocument.deleted === true;
|
|
577
|
+
const seq = typeof fullDocument._syncSeq === "number" ? fullDocument._syncSeq : 0;
|
|
578
|
+
const stream = (0, streams_1.resolveStreamForDoc)({
|
|
579
|
+
collectionTag: entry.collectionTag,
|
|
580
|
+
doc: fullDocument,
|
|
581
|
+
scope: entry.config.scope,
|
|
893
582
|
});
|
|
894
|
-
|
|
583
|
+
// C4: emit old-stream tombstones from durable markers, not the racy _syncPrevStream.
|
|
584
|
+
const markers = await models_1.SyncScopeMove.find({
|
|
585
|
+
collectionTag: entry.collectionTag,
|
|
586
|
+
entityId: docId,
|
|
587
|
+
}).lean();
|
|
588
|
+
let movedAway = false;
|
|
589
|
+
for (const marker of markers) {
|
|
590
|
+
if (marker.fromStream === stream) {
|
|
591
|
+
continue;
|
|
592
|
+
}
|
|
593
|
+
movedAway = true;
|
|
594
|
+
const markerFrontier = await (0, models_1.computeStableFrontier)({ stream: marker.fromStream });
|
|
595
|
+
const tombstone = {
|
|
596
|
+
collection: entry.collectionTag,
|
|
597
|
+
deleted: true,
|
|
598
|
+
frontierSeq: markerFrontier,
|
|
599
|
+
id: docId,
|
|
600
|
+
method: "delete",
|
|
601
|
+
seq: marker.seq,
|
|
602
|
+
stream: marker.fromStream,
|
|
603
|
+
};
|
|
604
|
+
await (0, exports.emitPayloadToAuthorizedRoom)({
|
|
605
|
+
buildPayload: () => tombstone,
|
|
606
|
+
entry,
|
|
607
|
+
eventName: "sync:delta",
|
|
608
|
+
fullDocument,
|
|
609
|
+
io,
|
|
610
|
+
logDebug,
|
|
611
|
+
room: (0, socketHandlers_1.syncRoomForStream)(marker.fromStream),
|
|
612
|
+
});
|
|
613
|
+
logDebug(`[sync] Emitted scope-move tombstone for ${entry.collectionTag}/${docId} to ${marker.fromStream} seq=${marker.seq}`);
|
|
614
|
+
}
|
|
615
|
+
// The new stream sees a moved-away doc for the first time: report it as a create
|
|
616
|
+
// (unless the same write also soft-deleted the doc, in which case the tombstone wins).
|
|
617
|
+
if (movedAway && method !== "delete") {
|
|
618
|
+
method = "create";
|
|
619
|
+
}
|
|
620
|
+
const emitLiveDelta = async (targetStream) => {
|
|
621
|
+
const frontierSeq = await (0, models_1.computeStableFrontier)({ stream: targetStream });
|
|
622
|
+
const liveDelta = {
|
|
623
|
+
collection: entry.collectionTag,
|
|
624
|
+
frontierSeq,
|
|
625
|
+
id: docId,
|
|
626
|
+
method,
|
|
627
|
+
seq,
|
|
628
|
+
stream: targetStream,
|
|
629
|
+
...(deleted ? { deleted: true } : {}),
|
|
630
|
+
};
|
|
631
|
+
await (0, exports.emitPayloadToAuthorizedRoom)({
|
|
632
|
+
// C7: tombstone deltas carry no data (only id/seq/deleted); live deltas serialize.
|
|
633
|
+
buildPayload: deleted
|
|
634
|
+
? () => liveDelta
|
|
635
|
+
: async (user) => {
|
|
636
|
+
const syntheticReq = { params: {}, query: {}, user };
|
|
637
|
+
const data = (0, exports.ensureApiId)(await (0, serialize_1.serializeSyncPayload)({ doc: fullDocument, entry, method, req: syntheticReq }));
|
|
638
|
+
return { ...liveDelta, data };
|
|
639
|
+
},
|
|
640
|
+
entry,
|
|
641
|
+
eventName: "sync:delta",
|
|
642
|
+
fullDocument,
|
|
643
|
+
io,
|
|
644
|
+
logDebug,
|
|
645
|
+
room: (0, socketHandlers_1.syncRoomForStream)(targetStream),
|
|
646
|
+
});
|
|
647
|
+
logDebug(`[sync] Emitted sync:delta ${method} for ${entry.collectionTag}/${docId} seq=${seq} stream=${targetStream} frontier=${frontierSeq}`);
|
|
648
|
+
};
|
|
649
|
+
await emitLiveDelta(stream);
|
|
650
|
+
if (entry.config.adminBroadcast === true) {
|
|
651
|
+
await emitLiveDelta((0, streams_1.adminBroadcastStream)(entry.collectionTag));
|
|
652
|
+
}
|
|
653
|
+
};
|
|
895
654
|
exports.emitSyncDeltaForChange = emitSyncDeltaForChange;
|
|
896
655
|
/**
|
|
897
656
|
* Start watching MongoDB change streams and emitting real-time events.
|
|
@@ -900,13 +659,11 @@ exports.emitSyncDeltaForChange = emitSyncDeltaForChange;
|
|
|
900
659
|
* exponential backoff, resuming from the last seen token. Call
|
|
901
660
|
* {@link stopChangeStreamWatcher} to end supervision.
|
|
902
661
|
*/
|
|
903
|
-
|
|
904
|
-
if (config === void 0) { config = {}; }
|
|
905
|
-
if (debug === void 0) { debug = false; }
|
|
662
|
+
const startChangeStreamWatcher = (io, config = {}, debug = false) => {
|
|
906
663
|
watcherStopped = false;
|
|
907
664
|
restartAttempts = 0;
|
|
908
665
|
lastResumeToken = undefined;
|
|
909
|
-
openChangeStream({ config
|
|
666
|
+
openChangeStream({ config, debug, io });
|
|
910
667
|
};
|
|
911
668
|
exports.startChangeStreamWatcher = startChangeStreamWatcher;
|
|
912
669
|
/**
|
|
@@ -914,50 +671,46 @@ exports.startChangeStreamWatcher = startChangeStreamWatcher;
|
|
|
914
671
|
* was stopped, when a newer stream is already running (a failing stream can fire several
|
|
915
672
|
* terminal events), or when a restart is already pending.
|
|
916
673
|
*/
|
|
917
|
-
|
|
918
|
-
var _b;
|
|
919
|
-
var config = _a.config, debug = _a.debug, generation = _a.generation, io = _a.io, reason = _a.reason;
|
|
674
|
+
const scheduleChangeStreamRestart = ({ config, debug, generation, io, reason, }) => {
|
|
920
675
|
if (watcherStopped || generation !== watcherGeneration || restartTimer) {
|
|
921
676
|
return;
|
|
922
677
|
}
|
|
923
678
|
restartAttempts += 1;
|
|
924
|
-
|
|
925
|
-
logger_1.logger.warn(
|
|
926
|
-
restartTimer = setTimeout(
|
|
679
|
+
const delayMs = Math.min(CHANGE_STREAM_RESTART_BASE_DELAY_MS * 2 ** (restartAttempts - 1), CHANGE_STREAM_RESTART_MAX_DELAY_MS);
|
|
680
|
+
logger_1.logger.warn(`[realtime] Change stream ${reason}; reopening in ${delayMs}ms (attempt ${restartAttempts})`, { resumable: lastResumeToken !== undefined });
|
|
681
|
+
restartTimer = setTimeout(() => {
|
|
927
682
|
restartTimer = null;
|
|
928
683
|
if (watcherStopped) {
|
|
929
684
|
return;
|
|
930
685
|
}
|
|
931
|
-
openChangeStream({ config
|
|
686
|
+
openChangeStream({ config, debug, io, isReopen: true });
|
|
932
687
|
}, delayMs);
|
|
933
688
|
// Never hold the process open just to retry a watcher.
|
|
934
|
-
|
|
689
|
+
restartTimer.unref?.();
|
|
935
690
|
};
|
|
936
691
|
/**
|
|
937
692
|
* Open (or re-open) the change stream and wire its listeners. Throws only for a
|
|
938
693
|
* configuration-level failure on the FIRST open (no connection, `watch` returned nothing),
|
|
939
694
|
* which must fail startup loudly; a failure while reopening is retried with backoff.
|
|
940
695
|
*/
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
var config = _a.config, debug = _a.debug, io = _a.io, _f = _a.isReopen, isReopen = _f === void 0 ? false : _f;
|
|
944
|
-
var logInfo = function (message) {
|
|
696
|
+
const openChangeStream = ({ config, debug, io, isReopen = false, }) => {
|
|
697
|
+
const logInfo = (message) => {
|
|
945
698
|
if (debug) {
|
|
946
699
|
logger_1.logger.info(message);
|
|
947
700
|
}
|
|
948
701
|
};
|
|
949
|
-
|
|
702
|
+
const logDebug = (message) => {
|
|
950
703
|
if (debug) {
|
|
951
704
|
logger_1.logger.debug(message);
|
|
952
705
|
}
|
|
953
706
|
};
|
|
954
|
-
|
|
707
|
+
const generation = ++watcherGeneration;
|
|
955
708
|
try {
|
|
956
709
|
logInfo("[realtime] Initializing change stream watcher...");
|
|
957
|
-
|
|
958
|
-
|
|
710
|
+
const ignored = new Set([...exports.DEFAULT_IGNORED_COLLECTIONS, ...(config.ignoredCollections ?? [])]);
|
|
711
|
+
const ignoredOps = new Set(config.ignoredOperations ?? []);
|
|
959
712
|
// Build the change stream pipeline
|
|
960
|
-
|
|
713
|
+
const pipeline = [
|
|
961
714
|
{
|
|
962
715
|
$match: {
|
|
963
716
|
"ns.coll": { $nin: Array.from(ignored) },
|
|
@@ -974,104 +727,94 @@ var openChangeStream = function (_a) {
|
|
|
974
727
|
},
|
|
975
728
|
},
|
|
976
729
|
];
|
|
977
|
-
|
|
730
|
+
const nativeDb = mongoose_1.default.connection.db;
|
|
978
731
|
if (!nativeDb) {
|
|
979
732
|
throw new errors_1.APIError({
|
|
980
733
|
status: 500,
|
|
981
734
|
title: "MongoDB connection not available for change stream",
|
|
982
735
|
});
|
|
983
736
|
}
|
|
984
|
-
|
|
737
|
+
const options = {
|
|
738
|
+
batchSize: config.batchSize ?? 50,
|
|
739
|
+
fullDocument: config.fullDocument ?? "updateLookup",
|
|
740
|
+
fullDocumentBeforeChange: "off",
|
|
985
741
|
// How long the cursor waits for new events before yielding control.
|
|
986
742
|
// Lower values give more responsive updates at the cost of more frequent driver round-trips.
|
|
987
|
-
maxAwaitTimeMS: 1000
|
|
988
|
-
|
|
743
|
+
maxAwaitTimeMS: 1000,
|
|
744
|
+
// Task 9.16: resume where the previous stream died so events during the outage are
|
|
745
|
+
// delivered rather than lost. Absent on a first open and after history loss.
|
|
746
|
+
...(lastResumeToken !== undefined ? { resumeAfter: lastResumeToken } : {}),
|
|
747
|
+
};
|
|
748
|
+
const stream = nativeDb.watch(pipeline, options);
|
|
989
749
|
if (!stream) {
|
|
990
750
|
throw new errors_1.APIError({ status: 500, title: "Failed to create change stream watcher" });
|
|
991
751
|
}
|
|
992
752
|
changeWatcher = stream;
|
|
993
|
-
stream.on("change",
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
753
|
+
stream.on("change", async (rawChange) => {
|
|
754
|
+
try {
|
|
755
|
+
// Task 9.16: record the resume token FIRST, for every change — including ones this
|
|
756
|
+
// watcher ignores. A token only from processed events would rewind the stream to an
|
|
757
|
+
// old position after a restart and re-deliver everything since.
|
|
758
|
+
const token = rawChange._id;
|
|
759
|
+
if (token !== undefined) {
|
|
760
|
+
lastResumeToken = token;
|
|
761
|
+
}
|
|
762
|
+
// A stream that is delivering events has recovered; drop the accumulated backoff so
|
|
763
|
+
// the next unrelated failure retries promptly instead of at the previous ceiling.
|
|
764
|
+
restartAttempts = 0;
|
|
765
|
+
// The pipeline restricts operationType to a subset that always has ns/documentKey;
|
|
766
|
+
// narrow once here so downstream code doesn't need repeated casts.
|
|
767
|
+
if (rawChange.operationType !== "insert" &&
|
|
768
|
+
rawChange.operationType !== "update" &&
|
|
769
|
+
rawChange.operationType !== "replace" &&
|
|
770
|
+
rawChange.operationType !== "delete") {
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
773
|
+
const change = rawChange;
|
|
774
|
+
const collectionName = change.ns?.coll;
|
|
775
|
+
const docId = change.documentKey?._id?.toString();
|
|
776
|
+
if (!collectionName || !docId) {
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
// Check if this operation type is ignored
|
|
780
|
+
if (ignoredOps.has(change.operationType)) {
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
// Realtime and sync registries are independent: a model may be in either or both.
|
|
784
|
+
// A model with both configs emits both the legacy "sync" event and "sync:delta".
|
|
785
|
+
const entry = (0, registry_2.findRegistryEntryByCollection)(collectionName);
|
|
786
|
+
const syncEntry = (0, registry_1.findSyncEntryByCollectionName)(collectionName);
|
|
787
|
+
if (!entry && !syncEntry) {
|
|
788
|
+
// Not a registered realtime or sync model — skip
|
|
789
|
+
logDebug(`[realtime] No registry entry for collection: ${collectionName}`);
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
if (entry) {
|
|
793
|
+
await processRealtimeChange({ change, docId, entry, io, logDebug, logInfo });
|
|
794
|
+
}
|
|
795
|
+
if (syncEntry) {
|
|
796
|
+
// C8: serialize per entity so same-doc deltas emit in change-stream order;
|
|
797
|
+
// different docs still dispatch concurrently. See serializePerEntity + the
|
|
798
|
+
// per-entity LWW-by-seq contract documented in sync/types.ts (SyncDelta).
|
|
799
|
+
serializePerEntity(`${collectionName}:${docId}`, async () => {
|
|
800
|
+
try {
|
|
801
|
+
await (0, exports.emitSyncDeltaForChange)({ change, docId, entry: syncEntry, io, logDebug });
|
|
1031
802
|
}
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
_d.sent();
|
|
1036
|
-
_d.label = 2;
|
|
1037
|
-
case 2:
|
|
1038
|
-
if (syncEntry_1) {
|
|
1039
|
-
// C8: serialize per entity so same-doc deltas emit in change-stream order;
|
|
1040
|
-
// different docs still dispatch concurrently. See serializePerEntity + the
|
|
1041
|
-
// per-entity LWW-by-seq contract documented in sync/types.ts (SyncDelta).
|
|
1042
|
-
serializePerEntity("".concat(collectionName, ":").concat(docId_1), function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1043
|
-
var error_5;
|
|
1044
|
-
return __generator(this, function (_a) {
|
|
1045
|
-
switch (_a.label) {
|
|
1046
|
-
case 0:
|
|
1047
|
-
_a.trys.push([0, 2, , 3]);
|
|
1048
|
-
return [4 /*yield*/, (0, exports.emitSyncDeltaForChange)({ change: change_1, docId: docId_1, entry: syncEntry_1, io: io, logDebug: logDebug })];
|
|
1049
|
-
case 1:
|
|
1050
|
-
_a.sent();
|
|
1051
|
-
return [3 /*break*/, 3];
|
|
1052
|
-
case 2:
|
|
1053
|
-
error_5 = _a.sent();
|
|
1054
|
-
logger_1.logger.error("[sync] Error emitting sync delta: ".concat(error_5));
|
|
1055
|
-
Sentry.captureException(error_5);
|
|
1056
|
-
return [3 /*break*/, 3];
|
|
1057
|
-
case 3: return [2 /*return*/];
|
|
1058
|
-
}
|
|
1059
|
-
});
|
|
1060
|
-
}); });
|
|
803
|
+
catch (error) {
|
|
804
|
+
logger_1.logger.error(`[sync] Error emitting sync delta: ${error}`);
|
|
805
|
+
Sentry.captureException(error);
|
|
1061
806
|
}
|
|
1062
|
-
|
|
1063
|
-
case 3:
|
|
1064
|
-
error_4 = _d.sent();
|
|
1065
|
-
logger_1.logger.error("[realtime] Error processing change event: ".concat(error_4));
|
|
1066
|
-
Sentry.captureException(error_4);
|
|
1067
|
-
return [3 /*break*/, 4];
|
|
1068
|
-
case 4: return [2 /*return*/];
|
|
807
|
+
});
|
|
1069
808
|
}
|
|
1070
|
-
}
|
|
1071
|
-
|
|
1072
|
-
|
|
809
|
+
}
|
|
810
|
+
catch (error) {
|
|
811
|
+
logger_1.logger.error(`[realtime] Error processing change event: ${error}`);
|
|
812
|
+
Sentry.captureException(error);
|
|
813
|
+
}
|
|
814
|
+
});
|
|
815
|
+
stream.on("error", (err) => {
|
|
1073
816
|
Sentry.captureException(err);
|
|
1074
|
-
logger_1.logger.error(
|
|
817
|
+
logger_1.logger.error(`[realtime] Change stream error: ${err?.message || err}`);
|
|
1075
818
|
if (isHistoryLostError(err)) {
|
|
1076
819
|
// The oplog no longer contains the resume point, so the gap can never be replayed.
|
|
1077
820
|
// Reopen from NOW and tell clients to re-bootstrap: their cursors may sit below
|
|
@@ -1079,28 +822,28 @@ var openChangeStream = function (_a) {
|
|
|
1079
822
|
lastResumeToken = undefined;
|
|
1080
823
|
logger_1.logger.error("[sync] Change stream history lost; reopening without a resume token and asking " +
|
|
1081
824
|
"clients to resync");
|
|
1082
|
-
|
|
825
|
+
const hint = { reason: "history_lost" };
|
|
1083
826
|
io.emit(exports.SYNC_RESYNC_EVENT, hint);
|
|
1084
827
|
}
|
|
1085
|
-
scheduleChangeStreamRestart({ config
|
|
828
|
+
scheduleChangeStreamRestart({ config, debug, generation, io, reason: "errored" });
|
|
1086
829
|
});
|
|
1087
|
-
stream.on("close",
|
|
830
|
+
stream.on("close", () => {
|
|
1088
831
|
logger_1.logger.warn("[realtime] Change stream closed");
|
|
1089
|
-
scheduleChangeStreamRestart({ config
|
|
832
|
+
scheduleChangeStreamRestart({ config, debug, generation, io, reason: "closed" });
|
|
1090
833
|
});
|
|
1091
|
-
stream.on("end",
|
|
834
|
+
stream.on("end", () => {
|
|
1092
835
|
logger_1.logger.warn("[realtime] Change stream ended");
|
|
1093
|
-
scheduleChangeStreamRestart({ config
|
|
836
|
+
scheduleChangeStreamRestart({ config, debug, generation, io, reason: "ended" });
|
|
1094
837
|
});
|
|
1095
838
|
logInfo("[realtime] Change stream watcher initialized successfully");
|
|
1096
839
|
}
|
|
1097
840
|
catch (error) {
|
|
1098
|
-
logger_1.logger.error(
|
|
841
|
+
logger_1.logger.error(`[realtime] Failed to initialize change stream watcher: ${error}`);
|
|
1099
842
|
Sentry.captureException(error);
|
|
1100
843
|
// A first open must fail startup loudly; a failed REOPEN (the DB is down mid-flight)
|
|
1101
844
|
// has no caller to throw to, so keep retrying with backoff instead.
|
|
1102
845
|
if (isReopen && !watcherStopped) {
|
|
1103
|
-
scheduleChangeStreamRestart({ config
|
|
846
|
+
scheduleChangeStreamRestart({ config, debug, generation, io, reason: "failed to open" });
|
|
1104
847
|
return;
|
|
1105
848
|
}
|
|
1106
849
|
throw error;
|
|
@@ -1110,25 +853,17 @@ var openChangeStream = function (_a) {
|
|
|
1110
853
|
* Stop the change stream watcher and end Task 9.16's restart supervision (a pending
|
|
1111
854
|
* reopen is cancelled, and terminal events from the closing stream are ignored).
|
|
1112
855
|
*/
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
case 1:
|
|
1127
|
-
_a.sent();
|
|
1128
|
-
changeWatcher = null;
|
|
1129
|
-
_a.label = 2;
|
|
1130
|
-
case 2: return [2 /*return*/];
|
|
1131
|
-
}
|
|
1132
|
-
});
|
|
1133
|
-
}); };
|
|
856
|
+
const stopChangeStreamWatcher = async () => {
|
|
857
|
+
watcherStopped = true;
|
|
858
|
+
if (restartTimer) {
|
|
859
|
+
clearTimeout(restartTimer);
|
|
860
|
+
restartTimer = null;
|
|
861
|
+
}
|
|
862
|
+
restartAttempts = 0;
|
|
863
|
+
lastResumeToken = undefined;
|
|
864
|
+
if (changeWatcher) {
|
|
865
|
+
await changeWatcher.close();
|
|
866
|
+
changeWatcher = null;
|
|
867
|
+
}
|
|
868
|
+
};
|
|
1134
869
|
exports.stopChangeStreamWatcher = stopChangeStreamWatcher;
|