@terreno/api 57.2.0 → 57.4.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 +23 -7
- package/dist/actions.js +19 -7
- package/dist/adminTypes.d.ts +7 -0
- package/dist/adminTypes.js +6 -1
- package/dist/api.d.ts +14 -0
- package/dist/api.js +111 -60
- package/dist/audit/auditApp.d.ts +30 -0
- package/dist/audit/auditApp.js +155 -0
- package/dist/audit/auditEventModel.d.ts +29 -0
- package/dist/audit/auditEventModel.js +102 -0
- package/dist/audit/cloudTasksEnqueue.d.ts +32 -0
- package/dist/audit/cloudTasksEnqueue.js +162 -0
- package/dist/audit/diff.d.ts +10 -0
- package/dist/audit/diff.js +230 -0
- package/dist/audit/processQueue.d.ts +6 -0
- package/dist/audit/processQueue.js +101 -0
- package/dist/audit/rbacSink.d.ts +2 -0
- package/dist/audit/rbacSink.js +82 -0
- package/dist/audit/record.d.ts +56 -0
- package/dist/audit/record.js +373 -0
- 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 +39 -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 +40 -0
- package/dist/collectionRegistry.js +97 -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 +42 -1
- package/dist/index.js +84 -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 -15
- package/dist/mcp/query.js +8 -8
- 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/migrations/fixtures/all-down/20260910120000-one.d.ts +4 -0
- package/dist/migrations/fixtures/all-down/20260910120000-one.js +49 -0
- package/dist/migrations/fixtures/all-down/20260910120001-two.d.ts +4 -0
- package/dist/migrations/fixtures/all-down/20260910120001-two.js +49 -0
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.d.ts +3 -0
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +45 -0
- package/dist/migrations/fixtures/bad-name/not-a-migration.d.ts +3 -0
- package/dist/migrations/fixtures/bad-name/not-a-migration.js +45 -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 +45 -0
- package/dist/migrations/fixtures/valid/20260910120001-beta.d.ts +4 -0
- package/dist/migrations/fixtures/valid/20260910120001-beta.js +49 -0
- package/dist/migrations/generate.d.ts +17 -0
- package/dist/migrations/generate.js +277 -0
- package/dist/migrations/load.d.ts +20 -0
- package/dist/migrations/load.js +236 -0
- package/dist/migrations/lock.d.ts +13 -0
- package/dist/migrations/lock.js +145 -0
- package/dist/migrations/runner.d.ts +42 -0
- package/dist/migrations/runner.js +503 -0
- package/dist/migrations/schemaCatalog.d.ts +44 -0
- package/dist/migrations/schemaCatalog.js +326 -0
- package/dist/migrations/types.d.ts +87 -0
- package/dist/migrations/types.js +7 -0
- package/dist/models/mcpServiceToken.d.ts +3 -0
- package/dist/models/mcpServiceToken.js +208 -0
- 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/notifications/notificationService.d.ts +40 -0
- package/dist/notifications/notificationService.js +323 -0
- package/dist/notifications/notificationsApp.d.ts +18 -0
- package/dist/notifications/notificationsApp.js +250 -0
- package/dist/notifications/notificationsBeforeSend.d.ts +14 -0
- package/dist/notifications/notificationsBeforeSend.js +77 -0
- package/dist/openApi.d.ts +1 -7
- package/dist/openApi.js +7 -6
- package/dist/openApiBuilder.d.ts +19 -7
- package/dist/openApiBuilder.js +61 -8
- package/dist/openApiCompat.js +1 -1
- package/dist/openApiValidator.d.ts +13 -1
- package/dist/openApiValidator.js +200 -38
- package/dist/orgs/orgContext.d.ts +23 -0
- package/dist/orgs/orgContext.js +198 -0
- package/dist/orgs/orgPermissions.d.ts +4 -0
- package/dist/orgs/orgPermissions.js +77 -0
- package/dist/orgs/orgPlugin.d.ts +13 -0
- package/dist/orgs/orgPlugin.js +84 -0
- package/dist/orgs/organizationModel.d.ts +13 -0
- package/dist/orgs/organizationModel.js +301 -0
- package/dist/orgs/organizationSettings.d.ts +19 -0
- package/dist/orgs/organizationSettings.js +120 -0
- package/dist/orgs/orgsApp.d.ts +28 -0
- package/dist/orgs/orgsApp.js +678 -0
- package/dist/permissions.d.ts +7 -13
- package/dist/permissions.js +8 -31
- 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 +3 -0
- package/dist/rateLimit/middleware.js +109 -0
- package/dist/rateLimit/mongoStore.d.ts +4 -0
- package/dist/rateLimit/mongoStore.js +123 -0
- package/dist/rateLimit/policies.d.ts +10 -0
- package/dist/rateLimit/policies.js +149 -0
- package/dist/rateLimit/redisStore.d.ts +4 -0
- package/dist/rateLimit/redisStore.js +156 -0
- package/dist/rateLimit/types.d.ts +54 -0
- package/dist/rateLimit/types.js +9 -0
- package/dist/rbac/access.js +35 -14
- package/dist/rbac/assertAllowed.d.ts +9 -0
- package/dist/rbac/assertAllowed.js +182 -0
- package/dist/rbac/membershipOrgAdminSource.d.ts +6 -0
- package/dist/rbac/membershipOrgAdminSource.js +122 -0
- package/dist/rbac/modelRouterAccess.d.ts +4 -0
- package/dist/rbac/modelRouterAccess.js +11 -4
- package/dist/rbac/permissionUtils.js +3 -2
- package/dist/rbac/resolve.js +65 -35
- package/dist/rbac/roleModel.d.ts +2 -5
- package/dist/rbac/roleModel.js +18 -4
- package/dist/rbac/routes.js +15 -6
- package/dist/rbac/statements.d.ts +10 -1
- package/dist/rbac/statements.js +19 -2
- package/dist/rbac/types.d.ts +5 -0
- package/dist/realtime/changeStreamWatcher.d.ts +10 -4
- package/dist/realtime/changeStreamWatcher.js +110 -64
- package/dist/realtime/deprecation.d.ts +13 -0
- package/dist/realtime/deprecation.js +33 -0
- package/dist/realtime/realtimeApp.d.ts +2 -1
- package/dist/realtime/realtimeApp.js +11 -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 +524 -0
- package/dist/seedRunner.d.ts +88 -0
- package/dist/seedRunner.js +627 -0
- package/dist/sync/adminBroadcastScope.d.ts +40 -0
- package/dist/sync/adminBroadcastScope.js +194 -0
- package/dist/sync/adminWindowAccess.d.ts +13 -0
- package/dist/sync/adminWindowAccess.js +96 -0
- package/dist/sync/adminWindowMutation.d.ts +88 -0
- package/dist/sync/adminWindowMutation.js +247 -0
- package/dist/sync/executors.d.ts +19 -3
- package/dist/sync/executors.js +194 -165
- package/dist/sync/loadHarness.js +28 -19
- package/dist/sync/models.js +26 -24
- package/dist/sync/mutationHandler.d.ts +14 -2
- package/dist/sync/mutationHandler.js +201 -76
- 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 +42 -187
- package/dist/sync/routes.d.ts +13 -0
- package/dist/sync/routes.js +116 -58
- package/dist/sync/scripts/compactTombstones.js +0 -2
- package/dist/sync/socketHandlers.d.ts +8 -2
- package/dist/sync/socketHandlers.js +144 -50
- package/dist/sync/streams.d.ts +2 -0
- package/dist/sync/streams.js +7 -1
- package/dist/sync/syncApp.d.ts +4 -3
- package/dist/sync/syncApp.js +5 -4
- package/dist/sync/syncBatch.d.ts +5 -2
- package/dist/sync/syncBatch.js +7 -6
- package/dist/sync/syncSeqPlugin.d.ts +1 -1
- package/dist/sync/syncSeqPlugin.js +20 -16
- package/dist/sync/types.d.ts +18 -0
- package/dist/terrenoApp.d.ts +48 -5
- package/dist/terrenoApp.js +157 -72
- package/dist/tests/createTestData.d.ts +1 -3
- package/dist/tests/createTestData.js +3 -5
- package/dist/tests/models.d.ts +3 -0
- package/dist/tests/models.js +15 -2
- package/dist/tests/testHelper.d.ts +0 -5
- package/dist/tests/testHelper.js +2 -9
- 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/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/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 +17 -0
- package/dist/webhooks/idempotency/webhookReceipt.js +39 -0
- package/dist/webhooks/rawBody.d.ts +15 -0
- package/dist/webhooks/rawBody.js +23 -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 +32 -27
- package/src/__snapshots__/openApi.test.ts.snap +444 -36
- package/src/__snapshots__/openApiBuilder.test.ts.snap +24 -24
- package/src/__snapshots__/schemaMetadata.test.ts.snap +196 -0
- package/src/actions.ts +20 -13
- package/src/adminModelRouter.test.ts +133 -0
- package/src/adminTypes.ts +8 -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 +133 -97
- package/src/api.ts +84 -27
- package/src/audit/auditApp.test.ts +243 -0
- package/src/audit/auditApp.ts +139 -0
- package/src/audit/auditEventModel.ts +156 -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 +328 -0
- package/src/auth.test.ts +108 -52
- 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 +31 -0
- package/src/betterAuthApp.test.ts +1 -0
- package/src/betterAuthApp.ts +27 -9
- package/src/betterAuthSetup.test.ts +595 -38
- package/src/betterAuthSetup.ts +261 -5
- package/src/collectionRegistry.test.ts +164 -0
- package/src/collectionRegistry.ts +135 -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 +3 -0
- package/src/expressServer.test.ts +180 -68
- package/src/expressServer.ts +75 -19
- package/src/githubAuth.test.ts +60 -55
- package/src/index.ts +127 -0
- package/src/mcp/auth.test.ts +47 -0
- package/src/mcp/auth.ts +32 -11
- package/src/mcp/handlers.test.ts +275 -0
- package/src/mcp/handlers.ts +45 -172
- package/src/mcp/integration.test.ts +59 -9
- package/src/mcp/query.ts +5 -7
- 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 +82 -1
- package/src/mcp/types.ts +2 -6
- package/src/migrations/cli.test.ts +376 -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.test.ts +129 -0
- package/src/models/mcpServiceToken.ts +158 -0
- package/src/models/notification.ts +68 -0
- package/src/models/notificationPreference.ts +62 -0
- package/src/notifications/notificationService.ts +254 -0
- package/src/notifications/notificationsApp.test.ts +505 -0
- package/src/notifications/notificationsApp.ts +200 -0
- package/src/notifications/notificationsBeforeSend.ts +51 -0
- package/src/openApi.test.ts +80 -65
- package/src/openApi.ts +2 -1
- package/src/openApiBuilder.test.ts +95 -7
- package/src/openApiBuilder.ts +60 -12
- package/src/openApiCompat.ts +1 -1
- package/src/openApiValidator.test.ts +270 -56
- package/src/openApiValidator.ts +219 -36
- 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 +257 -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 +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 +77 -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 +86 -0
- package/src/rateLimit/types.ts +64 -0
- package/src/rbac/access.ts +16 -4
- 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/membershipOrgAdminSource.ts +59 -0
- package/src/rbac/modelRouterAccess.test.ts +126 -0
- package/src/rbac/modelRouterAccess.ts +8 -2
- package/src/rbac/permissionUtils.test.ts +26 -5
- package/src/rbac/permissionUtils.ts +3 -2
- package/src/rbac/realtimeAccess.test.ts +109 -1
- package/src/rbac/resolve.test.ts +34 -2
- package/src/rbac/resolve.ts +31 -11
- package/src/rbac/roleManager.test.ts +169 -0
- package/src/rbac/roleModel.test.ts +34 -1
- package/src/rbac/roleModel.ts +17 -1
- package/src/rbac/routes.test.ts +233 -0
- package/src/rbac/routes.ts +16 -6
- 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.test.ts +60 -0
- package/src/realtime/deprecation.ts +33 -0
- package/src/realtime/realtime.test.ts +365 -189
- package/src/realtime/realtimeApp.ts +11 -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 +419 -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/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/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 +57 -18
- 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 +293 -17
- package/src/sync/mutationHandler.ts +180 -36
- package/src/sync/registrationSideEffects.test.ts +126 -0
- package/src/sync/registrationSideEffects.ts +131 -0
- package/src/sync/registry.ts +49 -171
- package/src/sync/routes.ts +55 -10
- package/src/sync/scripts/compactTombstones.ts +0 -2
- package/src/sync/sessionRevalidationIntegration.test.ts +12 -11
- package/src/sync/socketHandlers.ts +154 -58
- package/src/sync/streams.ts +6 -0
- package/src/sync/sync.test.ts +28 -25
- package/src/sync/syncApp.ts +5 -4
- package/src/sync/syncBatch.ts +10 -4
- package/src/sync/syncFrontier.test.ts +17 -15
- package/src/sync/syncPhaseC.test.ts +54 -43
- package/src/sync/syncRoutes.test.ts +347 -38
- package/src/sync/syncSeqPlugin.ts +66 -44
- package/src/sync/syncSocket.test.ts +341 -81
- package/src/sync/types.ts +20 -0
- package/src/terrenoApp.mcpServiceTokens.test.ts +173 -0
- package/src/terrenoApp.migrations.test.ts +114 -0
- package/src/terrenoApp.test.ts +41 -0
- package/src/terrenoApp.ts +181 -46
- package/src/tests/createTestData.ts +2 -2
- package/src/tests/models.ts +24 -3
- package/src/tests/testHelper.ts +1 -7
- package/src/types/authToken.ts +41 -0
- package/src/types/mcpServiceToken.ts +48 -0
- 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/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 +67 -0
- package/src/webhooks/rawBody.test.ts +107 -0
- package/src/webhooks/rawBody.ts +24 -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/tsconfig.json +1 -1
- package/tsconfig.typedoc.json +2 -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,5 +1,3 @@
|
|
|
1
|
-
// noExplicitAny: test mocks use dynamic shapes for registry entries and documents
|
|
2
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mocks use dynamic shapes for registry entries and documents
|
|
3
1
|
/**
|
|
4
2
|
* Tests for the realtime module's pure functions and classes:
|
|
5
3
|
* - queryMatcher.ts (matchesQuery)
|
|
@@ -11,11 +9,14 @@
|
|
|
11
9
|
*/
|
|
12
10
|
|
|
13
11
|
import {afterEach, beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
12
|
+
import {assert} from "chai";
|
|
14
13
|
import express from "express";
|
|
15
|
-
import mongoose from "mongoose";
|
|
14
|
+
import mongoose, {type Model} from "mongoose";
|
|
15
|
+
import type {Server} from "socket.io";
|
|
16
16
|
|
|
17
|
-
import type {ModelRouterOptions} from "../api";
|
|
17
|
+
import type {JSONValue, ModelRouterOptions} from "../api";
|
|
18
18
|
|
|
19
|
+
import {clearAdminBroadcastScopes, registerAdminBroadcastScope} from "../sync/adminBroadcastScope";
|
|
19
20
|
import {
|
|
20
21
|
emitToAuthorizedRoom,
|
|
21
22
|
emitToDocumentAndQueryRooms,
|
|
@@ -52,7 +53,35 @@ import {
|
|
|
52
53
|
registerRealtime,
|
|
53
54
|
updateRealtimeRegistryOptions,
|
|
54
55
|
} from "./registry";
|
|
55
|
-
import type {
|
|
56
|
+
import type {DecodedRealtimeToken} from "./socketUser";
|
|
57
|
+
import type {RealtimeConfig, RealtimeEvent} from "./types";
|
|
58
|
+
|
|
59
|
+
type ChangeStreamDoc = Parameters<typeof mapOperationType>[1];
|
|
60
|
+
|
|
61
|
+
const asChange = (change: Record<string, unknown>): ChangeStreamDoc =>
|
|
62
|
+
change as unknown as ChangeStreamDoc;
|
|
63
|
+
|
|
64
|
+
type ResponseHandler = NonNullable<ModelRouterOptions<unknown>["responseHandler"]>;
|
|
65
|
+
|
|
66
|
+
interface MockRoomSocket {
|
|
67
|
+
decodedToken?: DecodedRealtimeToken;
|
|
68
|
+
emit: (event: string, payload: unknown) => void;
|
|
69
|
+
id: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const permissiveOptions: ModelRouterOptions<unknown> = {
|
|
73
|
+
permissions: {
|
|
74
|
+
create: [() => true],
|
|
75
|
+
delete: [() => true],
|
|
76
|
+
list: [() => true],
|
|
77
|
+
read: [() => true],
|
|
78
|
+
update: [() => true],
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const setConnectionDb = (db: unknown): void => {
|
|
83
|
+
(mongoose.connection as unknown as {db: unknown}).db = db;
|
|
84
|
+
};
|
|
56
85
|
|
|
57
86
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
58
87
|
// queryMatcher tests
|
|
@@ -337,10 +366,12 @@ describe("matchesQuery", () => {
|
|
|
337
366
|
describe("queryStore", () => {
|
|
338
367
|
beforeEach(() => {
|
|
339
368
|
clearQueryStore();
|
|
369
|
+
clearAdminBroadcastScopes();
|
|
340
370
|
});
|
|
341
371
|
|
|
342
372
|
afterEach(() => {
|
|
343
373
|
clearQueryStore();
|
|
374
|
+
clearAdminBroadcastScopes();
|
|
344
375
|
});
|
|
345
376
|
|
|
346
377
|
describe("computeQueryId", () => {
|
|
@@ -489,14 +520,14 @@ describe("queryStore", () => {
|
|
|
489
520
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
490
521
|
|
|
491
522
|
describe("realtimeRegistry", () => {
|
|
492
|
-
const makeEntry = (overrides: Partial<
|
|
523
|
+
const makeEntry = (overrides: Partial<RealtimeRegistryEntry> = {}): RealtimeRegistryEntry => ({
|
|
493
524
|
collectionName: "todos",
|
|
494
525
|
config: {
|
|
495
526
|
methods: ["create" as const, "update" as const, "delete" as const],
|
|
496
527
|
roomStrategy: "owner" as const,
|
|
497
528
|
},
|
|
498
529
|
modelName: "Todo",
|
|
499
|
-
options:
|
|
530
|
+
options: permissiveOptions,
|
|
500
531
|
routePath: "/todos",
|
|
501
532
|
...overrides,
|
|
502
533
|
});
|
|
@@ -663,14 +694,14 @@ describe("RealtimeApp", () => {
|
|
|
663
694
|
interface MockSocket extends RealtimeSocketLike {
|
|
664
695
|
rooms: Set<string>;
|
|
665
696
|
emitted: {event: string; payload: unknown}[];
|
|
666
|
-
listeners: Map<string, (...args:
|
|
667
|
-
trigger: (event: string, ...args:
|
|
697
|
+
listeners: Map<string, (...args: never[]) => void | Promise<void>>;
|
|
698
|
+
trigger: (event: string, ...args: unknown[]) => Promise<void>;
|
|
668
699
|
}
|
|
669
700
|
|
|
670
|
-
const createMockSocket = (decodedToken?:
|
|
701
|
+
const createMockSocket = (decodedToken?: DecodedRealtimeToken): MockSocket => {
|
|
671
702
|
const rooms = new Set<string>();
|
|
672
703
|
const emitted: {event: string; payload: unknown}[] = [];
|
|
673
|
-
const listeners = new Map<string, (...args:
|
|
704
|
+
const listeners = new Map<string, (...args: never[]) => void | Promise<void>>();
|
|
674
705
|
|
|
675
706
|
const socket: MockSocket = {
|
|
676
707
|
decodedToken,
|
|
@@ -691,7 +722,7 @@ const createMockSocket = (decodedToken?: {id?: string; admin?: boolean}): MockSo
|
|
|
691
722
|
},
|
|
692
723
|
rooms,
|
|
693
724
|
trigger: async (event, ...args) => {
|
|
694
|
-
const handler = listeners.get(event);
|
|
725
|
+
const handler = listeners.get(event) as ((...a: unknown[]) => unknown) | undefined;
|
|
695
726
|
if (handler) {
|
|
696
727
|
await handler(...args);
|
|
697
728
|
}
|
|
@@ -725,7 +756,7 @@ describe("installRealtimeSocketHandlers", () => {
|
|
|
725
756
|
read: [() => true],
|
|
726
757
|
update: [() => true],
|
|
727
758
|
},
|
|
728
|
-
}
|
|
759
|
+
},
|
|
729
760
|
routePath: "/todos",
|
|
730
761
|
});
|
|
731
762
|
};
|
|
@@ -743,7 +774,7 @@ describe("installRealtimeSocketHandlers", () => {
|
|
|
743
774
|
read: [() => true],
|
|
744
775
|
update: [() => true],
|
|
745
776
|
},
|
|
746
|
-
}
|
|
777
|
+
},
|
|
747
778
|
routePath: "/broadcasts",
|
|
748
779
|
});
|
|
749
780
|
};
|
|
@@ -761,7 +792,7 @@ describe("installRealtimeSocketHandlers", () => {
|
|
|
761
792
|
read: [(_method: string, user?: {admin?: boolean}) => user?.admin === true],
|
|
762
793
|
update: [(_method: string, user?: {admin?: boolean}) => user?.admin === true],
|
|
763
794
|
},
|
|
764
|
-
}
|
|
795
|
+
},
|
|
765
796
|
routePath: "/secrets",
|
|
766
797
|
});
|
|
767
798
|
};
|
|
@@ -847,8 +878,8 @@ describe("installRealtimeSocketHandlers", () => {
|
|
|
847
878
|
const socket = createMockSocket({id: "user1"});
|
|
848
879
|
installRealtimeSocketHandlers(socket);
|
|
849
880
|
await socket.trigger("subscribe:model", "");
|
|
850
|
-
await socket.trigger("subscribe:model", 123
|
|
851
|
-
await socket.trigger("subscribe:model", null
|
|
881
|
+
await socket.trigger("subscribe:model", 123);
|
|
882
|
+
await socket.trigger("subscribe:model", null);
|
|
852
883
|
const modelRooms = Array.from(socket.rooms).filter((r) => r.startsWith("model:"));
|
|
853
884
|
expect(modelRooms).toHaveLength(0);
|
|
854
885
|
});
|
|
@@ -868,7 +899,7 @@ describe("installRealtimeSocketHandlers", () => {
|
|
|
868
899
|
read: [() => true],
|
|
869
900
|
update: [() => true],
|
|
870
901
|
},
|
|
871
|
-
}
|
|
902
|
+
},
|
|
872
903
|
routePath: `/coll${i}`,
|
|
873
904
|
});
|
|
874
905
|
}
|
|
@@ -931,7 +962,7 @@ describe("installRealtimeSocketHandlers", () => {
|
|
|
931
962
|
await socket.trigger("subscribe:document", {});
|
|
932
963
|
await socket.trigger("subscribe:document", {collection: "broadcasts"});
|
|
933
964
|
await socket.trigger("subscribe:document", {id: "doc1"});
|
|
934
|
-
await socket.trigger("subscribe:document", {collection: 123
|
|
965
|
+
await socket.trigger("subscribe:document", {collection: 123, id: "doc1"});
|
|
935
966
|
const docRooms = Array.from(socket.rooms).filter((r) => r.startsWith("document:"));
|
|
936
967
|
expect(docRooms).toHaveLength(0);
|
|
937
968
|
});
|
|
@@ -961,7 +992,7 @@ describe("installRealtimeSocketHandlers", () => {
|
|
|
961
992
|
if (!subscribed) {
|
|
962
993
|
throw new Error("Expected a query subscription event");
|
|
963
994
|
}
|
|
964
|
-
expect((subscribed.payload as
|
|
995
|
+
expect((subscribed.payload as {queryId: string}).queryId).toContain("user1");
|
|
965
996
|
});
|
|
966
997
|
|
|
967
998
|
it("does NOT inject ownerId for admins (admins see all)", async () => {
|
|
@@ -977,7 +1008,7 @@ describe("installRealtimeSocketHandlers", () => {
|
|
|
977
1008
|
if (!subscribed) {
|
|
978
1009
|
throw new Error("Expected a query subscription event");
|
|
979
1010
|
}
|
|
980
|
-
expect((subscribed.payload as
|
|
1011
|
+
expect((subscribed.payload as {queryId: string}).queryId).not.toContain("admin1");
|
|
981
1012
|
});
|
|
982
1013
|
|
|
983
1014
|
it("ignores subscriptions when user has no id (anonymous) for owner strategy", async () => {
|
|
@@ -1035,7 +1066,7 @@ describe("installRealtimeSocketHandlers", () => {
|
|
|
1035
1066
|
update: [() => true],
|
|
1036
1067
|
},
|
|
1037
1068
|
queryFilter: () => ({tenantId: "tenant-1"}),
|
|
1038
|
-
}
|
|
1069
|
+
},
|
|
1039
1070
|
routePath: "/filtered",
|
|
1040
1071
|
});
|
|
1041
1072
|
const socket = createMockSocket({id: "user1"});
|
|
@@ -1065,7 +1096,7 @@ describe("installRealtimeSocketHandlers", () => {
|
|
|
1065
1096
|
queryFilter: () => {
|
|
1066
1097
|
throw new Error("tenant lookup failed");
|
|
1067
1098
|
},
|
|
1068
|
-
}
|
|
1099
|
+
},
|
|
1069
1100
|
routePath: "/filtered",
|
|
1070
1101
|
});
|
|
1071
1102
|
const socket = createMockSocket({id: "user1"});
|
|
@@ -1122,7 +1153,7 @@ describe("installRealtimeSocketHandlers", () => {
|
|
|
1122
1153
|
read: [() => true],
|
|
1123
1154
|
update: [() => true],
|
|
1124
1155
|
},
|
|
1125
|
-
}
|
|
1156
|
+
},
|
|
1126
1157
|
routePath: "/other",
|
|
1127
1158
|
});
|
|
1128
1159
|
const socket = createMockSocket({id: "user1"});
|
|
@@ -1204,11 +1235,11 @@ describe("installRealtimeSocketHandlers", () => {
|
|
|
1204
1235
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
1205
1236
|
|
|
1206
1237
|
describe("serializeDoc (change stream serializer)", () => {
|
|
1207
|
-
const makeEntry = (overrides:
|
|
1238
|
+
const makeEntry = (overrides: Partial<RealtimeRegistryEntry> = {}): RealtimeRegistryEntry => ({
|
|
1208
1239
|
collectionName: "users",
|
|
1209
|
-
config: {methods: ["create", "update", "delete"]
|
|
1240
|
+
config: {methods: ["create", "update", "delete"], roomStrategy: "model"},
|
|
1210
1241
|
modelName: "User",
|
|
1211
|
-
options:
|
|
1242
|
+
options: permissiveOptions,
|
|
1212
1243
|
routePath: "/users",
|
|
1213
1244
|
...overrides,
|
|
1214
1245
|
});
|
|
@@ -1217,23 +1248,26 @@ describe("serializeDoc (change stream serializer)", () => {
|
|
|
1217
1248
|
const entry = makeEntry({
|
|
1218
1249
|
config: {
|
|
1219
1250
|
methods: ["update"],
|
|
1220
|
-
realtimeResponseHandler: (doc
|
|
1251
|
+
realtimeResponseHandler: (doc) => ({customized: doc.name}),
|
|
1221
1252
|
roomStrategy: "model",
|
|
1222
1253
|
},
|
|
1223
1254
|
});
|
|
1224
|
-
const result = await serializeDoc(entry
|
|
1255
|
+
const result = await serializeDoc(entry, {name: "Alice", secret: "x"}, "update");
|
|
1225
1256
|
expect(result).toEqual({customized: "Alice"});
|
|
1226
1257
|
});
|
|
1227
1258
|
|
|
1228
1259
|
it("falls back to modelRouter responseHandler when no realtime handler is set", async () => {
|
|
1229
1260
|
// Mimics a stripping responseHandler like the example-backend users router.
|
|
1230
|
-
const
|
|
1231
|
-
const {
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1261
|
+
const handler: ResponseHandler = async (doc) => {
|
|
1262
|
+
const record = {...(doc as unknown as Record<string, unknown>)};
|
|
1263
|
+
delete record.hash;
|
|
1264
|
+
delete record.salt;
|
|
1265
|
+
return record as unknown as JSONValue;
|
|
1266
|
+
};
|
|
1267
|
+
const responseHandler = mock(handler);
|
|
1268
|
+
const entry = makeEntry({options: {...permissiveOptions, responseHandler}});
|
|
1235
1269
|
const result = await serializeDoc(
|
|
1236
|
-
entry
|
|
1270
|
+
entry,
|
|
1237
1271
|
{email: "a@b.com", hash: "h", name: "Alice", salt: "s"},
|
|
1238
1272
|
"update"
|
|
1239
1273
|
);
|
|
@@ -1243,24 +1277,24 @@ describe("serializeDoc (change stream serializer)", () => {
|
|
|
1243
1277
|
|
|
1244
1278
|
it("maps 'delete' method to 'read' when invoking the REST responseHandler", async () => {
|
|
1245
1279
|
let observedMethod: string | undefined;
|
|
1246
|
-
const responseHandler = async (doc
|
|
1280
|
+
const responseHandler: ResponseHandler = async (doc, method) => {
|
|
1247
1281
|
observedMethod = method;
|
|
1248
|
-
return doc;
|
|
1282
|
+
return doc as unknown as JSONValue;
|
|
1249
1283
|
};
|
|
1250
|
-
const entry = makeEntry({options: {responseHandler}});
|
|
1251
|
-
await serializeDoc(entry
|
|
1284
|
+
const entry = makeEntry({options: {...permissiveOptions, responseHandler}});
|
|
1285
|
+
await serializeDoc(entry, {name: "Alice"}, "delete");
|
|
1252
1286
|
expect(observedMethod).toBe("read");
|
|
1253
1287
|
});
|
|
1254
1288
|
|
|
1255
1289
|
it("re-throws when modelRouter responseHandler throws (event dropped, no leak)", async () => {
|
|
1256
1290
|
// Critical: do NOT fall back to toJSON, which would skip the handler's sanitization
|
|
1257
1291
|
// (e.g. stripping hash/salt) and leak the raw document.
|
|
1258
|
-
const responseHandler = async ()
|
|
1292
|
+
const responseHandler: ResponseHandler = async () => {
|
|
1259
1293
|
throw new Error("boom");
|
|
1260
1294
|
};
|
|
1261
|
-
const entry = makeEntry({options: {responseHandler}});
|
|
1295
|
+
const entry = makeEntry({options: {...permissiveOptions, responseHandler}});
|
|
1262
1296
|
const doc = {hash: "h", name: "Alice", salt: "s", toJSON: () => ({hash: "h", name: "Alice"})};
|
|
1263
|
-
await expect(serializeDoc(entry
|
|
1297
|
+
await expect(serializeDoc(entry, doc, "update")).rejects.toThrow("boom");
|
|
1264
1298
|
});
|
|
1265
1299
|
|
|
1266
1300
|
it("re-throws when realtimeResponseHandler throws (event dropped, no leak)", async () => {
|
|
@@ -1274,26 +1308,26 @@ describe("serializeDoc (change stream serializer)", () => {
|
|
|
1274
1308
|
},
|
|
1275
1309
|
});
|
|
1276
1310
|
const doc = {name: "Alice", toJSON: () => ({name: "Alice-json"})};
|
|
1277
|
-
await expect(serializeDoc(entry
|
|
1311
|
+
await expect(serializeDoc(entry, doc, "update")).rejects.toThrow("boom");
|
|
1278
1312
|
});
|
|
1279
1313
|
|
|
1280
1314
|
it("returns toJSON output when no handlers are configured", async () => {
|
|
1281
1315
|
const entry = makeEntry();
|
|
1282
1316
|
const doc = {name: "Alice", toJSON: () => ({id: "1", name: "Alice"})};
|
|
1283
|
-
const result = await serializeDoc(entry
|
|
1317
|
+
const result = await serializeDoc(entry, doc, "create");
|
|
1284
1318
|
expect(result).toEqual({id: "1", name: "Alice"});
|
|
1285
1319
|
});
|
|
1286
1320
|
|
|
1287
1321
|
it("returns raw doc when toJSON is missing and no handlers configured", async () => {
|
|
1288
1322
|
const entry = makeEntry();
|
|
1289
|
-
const result = await serializeDoc(entry
|
|
1323
|
+
const result = await serializeDoc(entry, {name: "Alice"}, "create");
|
|
1290
1324
|
expect(result).toEqual({name: "Alice"});
|
|
1291
1325
|
});
|
|
1292
1326
|
|
|
1293
1327
|
it("adds id from _id when handlers omit it (change stream raw document shape)", async () => {
|
|
1294
1328
|
const entry = makeEntry();
|
|
1295
1329
|
const result = await serializeDoc(
|
|
1296
|
-
entry
|
|
1330
|
+
entry,
|
|
1297
1331
|
{_id: "507f1f77bcf86cd799439011", name: "Alice"},
|
|
1298
1332
|
"create"
|
|
1299
1333
|
);
|
|
@@ -1311,24 +1345,22 @@ describe("serializeDoc (change stream serializer)", () => {
|
|
|
1311
1345
|
|
|
1312
1346
|
describe("mapOperationType", () => {
|
|
1313
1347
|
it("maps insert to create", () => {
|
|
1314
|
-
expect(mapOperationType("insert", {}
|
|
1348
|
+
expect(mapOperationType("insert", asChange({}))).toBe("create");
|
|
1315
1349
|
});
|
|
1316
1350
|
|
|
1317
1351
|
it("maps update to update by default", () => {
|
|
1318
1352
|
expect(
|
|
1319
|
-
mapOperationType("update", {
|
|
1320
|
-
updateDescription: {updatedFields: {title: "x"}},
|
|
1321
|
-
} as any)
|
|
1353
|
+
mapOperationType("update", asChange({updateDescription: {updatedFields: {title: "x"}}}))
|
|
1322
1354
|
).toBe("update");
|
|
1323
1355
|
});
|
|
1324
1356
|
|
|
1325
1357
|
it("maps replace to update", () => {
|
|
1326
|
-
expect(mapOperationType("replace", {}
|
|
1358
|
+
expect(mapOperationType("replace", asChange({}))).toBe("update");
|
|
1327
1359
|
});
|
|
1328
1360
|
|
|
1329
1361
|
it("maps update with deleted=true to delete (soft delete) when delete is enabled", () => {
|
|
1330
1362
|
expect(
|
|
1331
|
-
mapOperationType("update", {updateDescription: {updatedFields: {deleted: true}}}
|
|
1363
|
+
mapOperationType("update", asChange({updateDescription: {updatedFields: {deleted: true}}}), [
|
|
1332
1364
|
"create",
|
|
1333
1365
|
"update",
|
|
1334
1366
|
"delete",
|
|
@@ -1340,7 +1372,7 @@ describe("mapOperationType", () => {
|
|
|
1340
1372
|
// A model configured with methods: ["create", "update"] must still see
|
|
1341
1373
|
// soft-delete events as updates — otherwise they'd be silently dropped.
|
|
1342
1374
|
expect(
|
|
1343
|
-
mapOperationType("update", {updateDescription: {updatedFields: {deleted: true}}}
|
|
1375
|
+
mapOperationType("update", asChange({updateDescription: {updatedFields: {deleted: true}}}), [
|
|
1344
1376
|
"create",
|
|
1345
1377
|
"update",
|
|
1346
1378
|
])
|
|
@@ -1348,21 +1380,21 @@ describe("mapOperationType", () => {
|
|
|
1348
1380
|
});
|
|
1349
1381
|
|
|
1350
1382
|
it("maps delete to delete", () => {
|
|
1351
|
-
expect(mapOperationType("delete", {}
|
|
1383
|
+
expect(mapOperationType("delete", asChange({}))).toBe("delete");
|
|
1352
1384
|
});
|
|
1353
1385
|
|
|
1354
1386
|
it("returns null for unknown operation types", () => {
|
|
1355
|
-
expect(mapOperationType("invalidate", {}
|
|
1356
|
-
expect(mapOperationType("drop", {}
|
|
1387
|
+
expect(mapOperationType("invalidate", asChange({}))).toBeNull();
|
|
1388
|
+
expect(mapOperationType("drop", asChange({}))).toBeNull();
|
|
1357
1389
|
});
|
|
1358
1390
|
});
|
|
1359
1391
|
|
|
1360
1392
|
describe("resolveRooms", () => {
|
|
1361
|
-
const baseEntry:
|
|
1393
|
+
const baseEntry: RealtimeRegistryEntry = {
|
|
1362
1394
|
collectionName: "todos",
|
|
1363
|
-
config: {methods: ["create", "update", "delete"]},
|
|
1395
|
+
config: {methods: ["create", "update", "delete"], roomStrategy: "model"},
|
|
1364
1396
|
modelName: "Todo",
|
|
1365
|
-
options:
|
|
1397
|
+
options: permissiveOptions,
|
|
1366
1398
|
routePath: "/todos",
|
|
1367
1399
|
};
|
|
1368
1400
|
|
|
@@ -1398,17 +1430,23 @@ describe("resolveRooms", () => {
|
|
|
1398
1430
|
});
|
|
1399
1431
|
|
|
1400
1432
|
it("defaults to model room for unknown strategy", () => {
|
|
1401
|
-
const entry = {
|
|
1433
|
+
const entry = {
|
|
1434
|
+
...baseEntry,
|
|
1435
|
+
config: {
|
|
1436
|
+
...baseEntry.config,
|
|
1437
|
+
roomStrategy: "unknown" as unknown as RealtimeConfig["roomStrategy"],
|
|
1438
|
+
},
|
|
1439
|
+
};
|
|
1402
1440
|
const rooms = resolveRooms(entry, {}, "create");
|
|
1403
1441
|
expect(rooms).toEqual(["model:todos"]);
|
|
1404
1442
|
});
|
|
1405
1443
|
|
|
1406
1444
|
it("invokes custom function room resolver", () => {
|
|
1407
|
-
const entry = {
|
|
1445
|
+
const entry: RealtimeRegistryEntry = {
|
|
1408
1446
|
...baseEntry,
|
|
1409
1447
|
config: {
|
|
1410
1448
|
...baseEntry.config,
|
|
1411
|
-
roomStrategy: (doc
|
|
1449
|
+
roomStrategy: (doc, method): string[] => [`custom:${method}:${String(doc.id)}`],
|
|
1412
1450
|
},
|
|
1413
1451
|
};
|
|
1414
1452
|
const rooms = resolveRooms(entry, {id: "42"}, "update");
|
|
@@ -1417,28 +1455,18 @@ describe("resolveRooms", () => {
|
|
|
1417
1455
|
});
|
|
1418
1456
|
|
|
1419
1457
|
describe("emitToDocumentAndQueryRooms", () => {
|
|
1420
|
-
const permissiveOptions = {
|
|
1421
|
-
permissions: {
|
|
1422
|
-
create: [() => true],
|
|
1423
|
-
delete: [() => true],
|
|
1424
|
-
list: [() => true],
|
|
1425
|
-
read: [() => true],
|
|
1426
|
-
update: [() => true],
|
|
1427
|
-
},
|
|
1428
|
-
};
|
|
1429
|
-
|
|
1430
1458
|
const makeIo = (): {
|
|
1431
|
-
addSocketToRoom: (room: string, decodedToken?:
|
|
1459
|
+
addSocketToRoom: (room: string, decodedToken?: DecodedRealtimeToken) => void;
|
|
1432
1460
|
emissions: Array<{room: string; event: string; payload: unknown}>;
|
|
1433
|
-
io:
|
|
1461
|
+
io: Server;
|
|
1434
1462
|
} => {
|
|
1435
1463
|
const emissions: Array<{room: string; event: string; payload: unknown}> = [];
|
|
1436
1464
|
const roomSockets = new Map<string, Set<string>>();
|
|
1437
|
-
const sockets = new Map<string,
|
|
1465
|
+
const sockets = new Map<string, MockRoomSocket>();
|
|
1438
1466
|
let nextSocketId = 1;
|
|
1439
1467
|
const addSocketToRoom = (
|
|
1440
1468
|
room: string,
|
|
1441
|
-
decodedToken:
|
|
1469
|
+
decodedToken: DecodedRealtimeToken = {admin: true, id: "admin"}
|
|
1442
1470
|
): void => {
|
|
1443
1471
|
const socketId = `socket-${nextSocketId}`;
|
|
1444
1472
|
nextSocketId += 1;
|
|
@@ -1464,7 +1492,7 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1464
1492
|
emissions.push({event, payload, room});
|
|
1465
1493
|
},
|
|
1466
1494
|
}),
|
|
1467
|
-
};
|
|
1495
|
+
} as unknown as Server;
|
|
1468
1496
|
return {addSocketToRoom, emissions, io};
|
|
1469
1497
|
};
|
|
1470
1498
|
|
|
@@ -1478,7 +1506,7 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1478
1506
|
|
|
1479
1507
|
it("emits to the document room", async () => {
|
|
1480
1508
|
const {emissions, io} = makeIo();
|
|
1481
|
-
const event:
|
|
1509
|
+
const event: RealtimeEvent = {
|
|
1482
1510
|
collection: "todos",
|
|
1483
1511
|
id: "doc-1",
|
|
1484
1512
|
method: "update",
|
|
@@ -1494,14 +1522,14 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1494
1522
|
addQuerySubscription("socket-a", "todos", {priority: 1}, queryId);
|
|
1495
1523
|
const {addSocketToRoom, emissions, io} = makeIo();
|
|
1496
1524
|
addSocketToRoom(`query:${queryId}`);
|
|
1497
|
-
const event:
|
|
1525
|
+
const event: RealtimeEvent = {
|
|
1498
1526
|
collection: "todos",
|
|
1499
1527
|
id: "doc-1",
|
|
1500
1528
|
method: "delete",
|
|
1501
1529
|
model: "Todo",
|
|
1502
1530
|
timestamp: 1,
|
|
1503
1531
|
};
|
|
1504
|
-
const entry:
|
|
1532
|
+
const entry: RealtimeRegistryEntry = {
|
|
1505
1533
|
collectionName: "todos",
|
|
1506
1534
|
config: {methods: ["delete"], roomStrategy: "model"},
|
|
1507
1535
|
modelName: "Todo",
|
|
@@ -1518,14 +1546,14 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1518
1546
|
const {addSocketToRoom, emissions, io} = makeIo();
|
|
1519
1547
|
addSocketToRoom("document:todos:doc-1");
|
|
1520
1548
|
addSocketToRoom(`query:${queryId}`);
|
|
1521
|
-
const event:
|
|
1549
|
+
const event: RealtimeEvent = {
|
|
1522
1550
|
collection: "todos",
|
|
1523
1551
|
id: "doc-1",
|
|
1524
1552
|
method: "delete",
|
|
1525
1553
|
model: "Todo",
|
|
1526
1554
|
timestamp: 1,
|
|
1527
1555
|
};
|
|
1528
|
-
const entry:
|
|
1556
|
+
const entry: RealtimeRegistryEntry = {
|
|
1529
1557
|
collectionName: "todos",
|
|
1530
1558
|
config: {methods: ["delete"], roomStrategy: "owner"},
|
|
1531
1559
|
modelName: "Todo",
|
|
@@ -1547,7 +1575,7 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1547
1575
|
const {addSocketToRoom, emissions, io} = makeIo();
|
|
1548
1576
|
addSocketToRoom(`query:${matchingQueryId}`);
|
|
1549
1577
|
addSocketToRoom(`query:${nonMatchingQueryId}`);
|
|
1550
|
-
const event:
|
|
1578
|
+
const event: RealtimeEvent = {
|
|
1551
1579
|
collection: "todos",
|
|
1552
1580
|
data: {deleted: true, priority: 1},
|
|
1553
1581
|
id: "doc-1",
|
|
@@ -1555,7 +1583,7 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1555
1583
|
model: "Todo",
|
|
1556
1584
|
timestamp: 1,
|
|
1557
1585
|
};
|
|
1558
|
-
const entry:
|
|
1586
|
+
const entry: RealtimeRegistryEntry = {
|
|
1559
1587
|
collectionName: "todos",
|
|
1560
1588
|
config: {methods: ["delete"], roomStrategy: "owner"},
|
|
1561
1589
|
modelName: "Todo",
|
|
@@ -1578,7 +1606,7 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1578
1606
|
const queryId = computeQueryId("todos", {priority: 1});
|
|
1579
1607
|
addQuerySubscription("socket-a", "todos", {priority: 1}, queryId);
|
|
1580
1608
|
const {emissions, io} = makeIo();
|
|
1581
|
-
const event:
|
|
1609
|
+
const event: RealtimeEvent = {
|
|
1582
1610
|
collection: "todos",
|
|
1583
1611
|
id: "doc-1",
|
|
1584
1612
|
method: "create",
|
|
@@ -1593,7 +1621,7 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1593
1621
|
const queryId = computeQueryId("todos", {priority: 1});
|
|
1594
1622
|
addQuerySubscription("socket-a", "todos", {priority: 1}, queryId);
|
|
1595
1623
|
const {emissions, io} = makeIo();
|
|
1596
|
-
const event:
|
|
1624
|
+
const event: RealtimeEvent = {
|
|
1597
1625
|
collection: "todos",
|
|
1598
1626
|
id: "doc-1",
|
|
1599
1627
|
method: "create",
|
|
@@ -1610,7 +1638,7 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1610
1638
|
const queryId = computeQueryId("todos", {priority: 1});
|
|
1611
1639
|
addQuerySubscription("socket-a", "todos", {priority: 1}, queryId);
|
|
1612
1640
|
const {emissions, io} = makeIo();
|
|
1613
|
-
const event:
|
|
1641
|
+
const event: RealtimeEvent = {
|
|
1614
1642
|
collection: "todos",
|
|
1615
1643
|
id: "doc-1",
|
|
1616
1644
|
method: "create",
|
|
@@ -1625,7 +1653,7 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1625
1653
|
const queryId = computeQueryId("todos", {priority: 1});
|
|
1626
1654
|
addQuerySubscription("socket-a", "todos", {priority: 1}, queryId);
|
|
1627
1655
|
const {emissions, io} = makeIo();
|
|
1628
|
-
const event:
|
|
1656
|
+
const event: RealtimeEvent = {
|
|
1629
1657
|
collection: "todos",
|
|
1630
1658
|
id: "doc-1",
|
|
1631
1659
|
method: "update",
|
|
@@ -1642,7 +1670,7 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1642
1670
|
const queryId = computeQueryId("todos", {priority: 1});
|
|
1643
1671
|
addQuerySubscription("socket-a", "todos", {priority: 1}, queryId);
|
|
1644
1672
|
const {emissions, io} = makeIo();
|
|
1645
|
-
const event:
|
|
1673
|
+
const event: RealtimeEvent = {
|
|
1646
1674
|
collection: "todos",
|
|
1647
1675
|
id: "doc-1",
|
|
1648
1676
|
method: "update",
|
|
@@ -1659,7 +1687,7 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1659
1687
|
const {addSocketToRoom, emissions, io} = makeIo();
|
|
1660
1688
|
addSocketToRoom("model:todos", {id: "owner-1"});
|
|
1661
1689
|
addSocketToRoom("model:todos", {id: "other-user"});
|
|
1662
|
-
const entry:
|
|
1690
|
+
const entry: RealtimeRegistryEntry = {
|
|
1663
1691
|
collectionName: "todos",
|
|
1664
1692
|
config: {methods: ["update"], roomStrategy: "model"},
|
|
1665
1693
|
modelName: "Todo",
|
|
@@ -1669,16 +1697,19 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1669
1697
|
delete: [() => true],
|
|
1670
1698
|
list: [() => true],
|
|
1671
1699
|
read: [
|
|
1672
|
-
(_method: string, user?: {admin?: boolean; id?: string}, obj?:
|
|
1673
|
-
user?.admin === true || user?.id === obj?.ownerId,
|
|
1700
|
+
(_method: string, user?: {admin?: boolean; id?: string}, obj?: unknown) =>
|
|
1701
|
+
user?.admin === true || user?.id === (obj as {ownerId?: string} | undefined)?.ownerId,
|
|
1674
1702
|
],
|
|
1675
1703
|
update: [() => true],
|
|
1676
1704
|
},
|
|
1677
|
-
responseHandler: (doc
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1705
|
+
responseHandler: async (doc, _method, req) => {
|
|
1706
|
+
const record = doc as unknown as {_id?: unknown; title?: unknown};
|
|
1707
|
+
return {
|
|
1708
|
+
id: record._id,
|
|
1709
|
+
title: record.title,
|
|
1710
|
+
visibleTo: req.user?.id,
|
|
1711
|
+
} as unknown as JSONValue;
|
|
1712
|
+
},
|
|
1682
1713
|
},
|
|
1683
1714
|
routePath: "/todos",
|
|
1684
1715
|
};
|
|
@@ -1708,7 +1739,7 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1708
1739
|
const {addSocketToRoom, emissions, io} = makeIo();
|
|
1709
1740
|
addSocketToRoom("model:todos", {id: "bad-user"});
|
|
1710
1741
|
addSocketToRoom("model:todos", {id: "good-user"});
|
|
1711
|
-
const entry:
|
|
1742
|
+
const entry: RealtimeRegistryEntry = {
|
|
1712
1743
|
collectionName: "todos",
|
|
1713
1744
|
config: {methods: ["update"], roomStrategy: "model"},
|
|
1714
1745
|
modelName: "Todo",
|
|
@@ -1720,12 +1751,15 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1720
1751
|
read: [() => true],
|
|
1721
1752
|
update: [() => true],
|
|
1722
1753
|
},
|
|
1723
|
-
responseHandler: (doc
|
|
1754
|
+
responseHandler: async (doc, _method, req) => {
|
|
1724
1755
|
if (req.user?.id === "bad-user") {
|
|
1725
1756
|
throw new Error("cannot serialize for bad user");
|
|
1726
1757
|
}
|
|
1727
1758
|
|
|
1728
|
-
return {
|
|
1759
|
+
return {
|
|
1760
|
+
...(doc as Record<string, unknown>),
|
|
1761
|
+
visibleTo: req.user?.id,
|
|
1762
|
+
} as unknown as JSONValue;
|
|
1729
1763
|
},
|
|
1730
1764
|
},
|
|
1731
1765
|
routePath: "/todos",
|
|
@@ -1752,10 +1786,79 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1752
1786
|
});
|
|
1753
1787
|
});
|
|
1754
1788
|
|
|
1789
|
+
it("allows admin broadcast sockets without running product read permissions", async () => {
|
|
1790
|
+
const {addSocketToRoom, emissions, io} = makeIo();
|
|
1791
|
+
addSocketToRoom("sync:todos|admin", {admin: true, id: "admin-user"});
|
|
1792
|
+
registerAdminBroadcastScope("Todo", {
|
|
1793
|
+
listPermissions: [() => true],
|
|
1794
|
+
readPermissions: [() => true],
|
|
1795
|
+
});
|
|
1796
|
+
const entry: RealtimeRegistryEntry = {
|
|
1797
|
+
collectionName: "todos",
|
|
1798
|
+
config: {methods: ["update"], roomStrategy: "model"},
|
|
1799
|
+
modelName: "Todo",
|
|
1800
|
+
options: {
|
|
1801
|
+
...permissiveOptions,
|
|
1802
|
+
permissions: {...permissiveOptions.permissions, read: [() => false]},
|
|
1803
|
+
},
|
|
1804
|
+
routePath: "/todos",
|
|
1805
|
+
};
|
|
1806
|
+
|
|
1807
|
+
await emitToAuthorizedRoom(
|
|
1808
|
+
io,
|
|
1809
|
+
"sync:todos|admin",
|
|
1810
|
+
{
|
|
1811
|
+
collection: "todos",
|
|
1812
|
+
id: "todo-1",
|
|
1813
|
+
method: "update",
|
|
1814
|
+
model: "Todo",
|
|
1815
|
+
timestamp: 1,
|
|
1816
|
+
},
|
|
1817
|
+
entry,
|
|
1818
|
+
{_id: "todo-1", ownerId: "owner-1"},
|
|
1819
|
+
() => {}
|
|
1820
|
+
);
|
|
1821
|
+
|
|
1822
|
+
assert.lengthOf(emissions, 1);
|
|
1823
|
+
});
|
|
1824
|
+
|
|
1825
|
+
it("does not emit admin broadcast documents denied by the admin scope", async () => {
|
|
1826
|
+
const {addSocketToRoom, emissions, io} = makeIo();
|
|
1827
|
+
addSocketToRoom("sync:todos|admin", {admin: true, id: "admin-user"});
|
|
1828
|
+
registerAdminBroadcastScope("Todo", {
|
|
1829
|
+
listPermissions: [() => false],
|
|
1830
|
+
readPermissions: [() => true],
|
|
1831
|
+
});
|
|
1832
|
+
const entry: RealtimeRegistryEntry = {
|
|
1833
|
+
collectionName: "todos",
|
|
1834
|
+
config: {methods: ["update"], roomStrategy: "model"},
|
|
1835
|
+
modelName: "Todo",
|
|
1836
|
+
options: permissiveOptions,
|
|
1837
|
+
routePath: "/todos",
|
|
1838
|
+
};
|
|
1839
|
+
|
|
1840
|
+
await emitToAuthorizedRoom(
|
|
1841
|
+
io,
|
|
1842
|
+
"sync:todos|admin",
|
|
1843
|
+
{
|
|
1844
|
+
collection: "todos",
|
|
1845
|
+
id: "todo-1",
|
|
1846
|
+
method: "update",
|
|
1847
|
+
model: "Todo",
|
|
1848
|
+
timestamp: 1,
|
|
1849
|
+
},
|
|
1850
|
+
entry,
|
|
1851
|
+
{_id: "todo-1", ownerId: "owner-1"},
|
|
1852
|
+
() => {}
|
|
1853
|
+
);
|
|
1854
|
+
|
|
1855
|
+
assert.isEmpty(emissions);
|
|
1856
|
+
});
|
|
1857
|
+
|
|
1755
1858
|
it("does not emit hard delete metadata when read permission requires an object owner", async () => {
|
|
1756
1859
|
const {addSocketToRoom, emissions, io} = makeIo();
|
|
1757
1860
|
addSocketToRoom("model:todos", {id: "other-user"});
|
|
1758
|
-
const entry:
|
|
1861
|
+
const entry: RealtimeRegistryEntry = {
|
|
1759
1862
|
collectionName: "todos",
|
|
1760
1863
|
config: {methods: ["delete"], roomStrategy: "model"},
|
|
1761
1864
|
modelName: "Todo",
|
|
@@ -1765,8 +1868,8 @@ describe("emitToDocumentAndQueryRooms", () => {
|
|
|
1765
1868
|
delete: [() => true],
|
|
1766
1869
|
list: [() => true],
|
|
1767
1870
|
read: [
|
|
1768
|
-
(_method: string, user?: {admin?: boolean; id?: string}, obj?:
|
|
1769
|
-
user?.admin === true || user?.id === obj?.ownerId,
|
|
1871
|
+
(_method: string, user?: {admin?: boolean; id?: string}, obj?: unknown) =>
|
|
1872
|
+
user?.admin === true || user?.id === (obj as {ownerId?: string} | undefined)?.ownerId,
|
|
1770
1873
|
],
|
|
1771
1874
|
update: [() => true],
|
|
1772
1875
|
},
|
|
@@ -1930,7 +2033,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
1930
2033
|
});
|
|
1931
2034
|
|
|
1932
2035
|
afterEach(async () => {
|
|
1933
|
-
(
|
|
2036
|
+
setConnectionDb(originalDb);
|
|
1934
2037
|
clearRealtimeRegistry();
|
|
1935
2038
|
// Ensure the watcher is stopped between tests
|
|
1936
2039
|
const {stopChangeStreamWatcher: stop} = await import("./changeStreamWatcher");
|
|
@@ -1938,15 +2041,15 @@ describe("startChangeStreamWatcher", () => {
|
|
|
1938
2041
|
});
|
|
1939
2042
|
|
|
1940
2043
|
const createMockChangeStream = () => {
|
|
1941
|
-
const listeners = new Map<string, (...args:
|
|
2044
|
+
const listeners = new Map<string, (...args: unknown[]) => void>();
|
|
1942
2045
|
return {
|
|
1943
2046
|
close: mock(async () => {}),
|
|
1944
2047
|
listeners,
|
|
1945
|
-
on(event: string, handler: (...args:
|
|
2048
|
+
on(event: string, handler: (...args: unknown[]) => void) {
|
|
1946
2049
|
listeners.set(event, handler);
|
|
1947
2050
|
return this;
|
|
1948
2051
|
},
|
|
1949
|
-
trigger(event: string, ...args:
|
|
2052
|
+
trigger(event: string, ...args: unknown[]) {
|
|
1950
2053
|
const handler = listeners.get(event);
|
|
1951
2054
|
if (handler) {
|
|
1952
2055
|
handler(...args);
|
|
@@ -1968,14 +2071,14 @@ describe("startChangeStreamWatcher", () => {
|
|
|
1968
2071
|
|
|
1969
2072
|
const createMockIo = () => {
|
|
1970
2073
|
const rooms = new Map<string, Set<string>>();
|
|
1971
|
-
const sockets = new Map<string,
|
|
2074
|
+
const sockets = new Map<string, MockRoomSocket>();
|
|
1972
2075
|
return {
|
|
1973
2076
|
sockets: {
|
|
1974
2077
|
adapter: {rooms},
|
|
1975
2078
|
sockets,
|
|
1976
2079
|
},
|
|
1977
2080
|
to: (_room: string) => ({
|
|
1978
|
-
emit: (_event: string, _data:
|
|
2081
|
+
emit: (_event: string, _data: unknown) => {},
|
|
1979
2082
|
}),
|
|
1980
2083
|
} as unknown as import("socket.io").Server;
|
|
1981
2084
|
};
|
|
@@ -1985,7 +2088,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
1985
2088
|
const mockDb = {
|
|
1986
2089
|
watch: mock(() => mockStream),
|
|
1987
2090
|
};
|
|
1988
|
-
(
|
|
2091
|
+
setConnectionDb(mockDb);
|
|
1989
2092
|
|
|
1990
2093
|
const {startChangeStreamWatcher} = await import("./changeStreamWatcher");
|
|
1991
2094
|
const io = createMockIo();
|
|
@@ -2000,7 +2103,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2000
2103
|
});
|
|
2001
2104
|
|
|
2002
2105
|
it("throws when mongoose connection db is unavailable", async () => {
|
|
2003
|
-
(
|
|
2106
|
+
setConnectionDb(undefined);
|
|
2004
2107
|
|
|
2005
2108
|
const {startChangeStreamWatcher} = await import("./changeStreamWatcher");
|
|
2006
2109
|
const io = createMockIo();
|
|
@@ -2014,7 +2117,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2014
2117
|
const mockDb = {
|
|
2015
2118
|
watch: mock(() => null),
|
|
2016
2119
|
};
|
|
2017
|
-
(
|
|
2120
|
+
setConnectionDb(mockDb);
|
|
2018
2121
|
|
|
2019
2122
|
const {startChangeStreamWatcher} = await import("./changeStreamWatcher");
|
|
2020
2123
|
const io = createMockIo();
|
|
@@ -2027,7 +2130,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2027
2130
|
const mockDb = {
|
|
2028
2131
|
watch: mock(() => mockStream),
|
|
2029
2132
|
};
|
|
2030
|
-
(
|
|
2133
|
+
setConnectionDb(mockDb);
|
|
2031
2134
|
|
|
2032
2135
|
registerRealtime({
|
|
2033
2136
|
collectionName: "todos",
|
|
@@ -2067,7 +2170,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2067
2170
|
const mockDb = {
|
|
2068
2171
|
watch: mock(() => mockStream),
|
|
2069
2172
|
};
|
|
2070
|
-
(
|
|
2173
|
+
setConnectionDb(mockDb);
|
|
2071
2174
|
|
|
2072
2175
|
const {startChangeStreamWatcher} = await import("./changeStreamWatcher");
|
|
2073
2176
|
const io = createMockIo();
|
|
@@ -2088,7 +2191,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2088
2191
|
const mockDb = {
|
|
2089
2192
|
watch: mock(() => mockStream),
|
|
2090
2193
|
};
|
|
2091
|
-
(
|
|
2194
|
+
setConnectionDb(mockDb);
|
|
2092
2195
|
|
|
2093
2196
|
registerRealtime({
|
|
2094
2197
|
collectionName: "todos",
|
|
@@ -2129,7 +2232,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2129
2232
|
const mockDb = {
|
|
2130
2233
|
watch: mock(() => mockStream),
|
|
2131
2234
|
};
|
|
2132
|
-
(
|
|
2235
|
+
setConnectionDb(mockDb);
|
|
2133
2236
|
|
|
2134
2237
|
registerRealtime({
|
|
2135
2238
|
collectionName: "todos",
|
|
@@ -2168,7 +2271,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2168
2271
|
const mockDb = {
|
|
2169
2272
|
watch: mock(() => mockStream),
|
|
2170
2273
|
};
|
|
2171
|
-
(
|
|
2274
|
+
setConnectionDb(mockDb);
|
|
2172
2275
|
|
|
2173
2276
|
registerRealtime({
|
|
2174
2277
|
collectionName: "broadcasts",
|
|
@@ -2238,7 +2341,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2238
2341
|
const mockDb = {
|
|
2239
2342
|
watch: mock(() => mockStream),
|
|
2240
2343
|
};
|
|
2241
|
-
(
|
|
2344
|
+
setConnectionDb(mockDb);
|
|
2242
2345
|
|
|
2243
2346
|
registerRealtime({
|
|
2244
2347
|
collectionName: "todos",
|
|
@@ -2279,7 +2382,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2279
2382
|
const mockDb = {
|
|
2280
2383
|
watch: mock(() => mockStream),
|
|
2281
2384
|
};
|
|
2282
|
-
(
|
|
2385
|
+
setConnectionDb(mockDb);
|
|
2283
2386
|
|
|
2284
2387
|
registerRealtime({
|
|
2285
2388
|
collectionName: "todos",
|
|
@@ -2327,7 +2430,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2327
2430
|
const mockDb = {
|
|
2328
2431
|
watch: mock(() => mockStream),
|
|
2329
2432
|
};
|
|
2330
|
-
(
|
|
2433
|
+
setConnectionDb(mockDb);
|
|
2331
2434
|
|
|
2332
2435
|
registerRealtime({
|
|
2333
2436
|
collectionName: "todos",
|
|
@@ -2367,7 +2470,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2367
2470
|
const mockDb = {
|
|
2368
2471
|
watch: mock(() => mockStream),
|
|
2369
2472
|
};
|
|
2370
|
-
(
|
|
2473
|
+
setConnectionDb(mockDb);
|
|
2371
2474
|
|
|
2372
2475
|
const {startChangeStreamWatcher} = await import("./changeStreamWatcher");
|
|
2373
2476
|
const io = createMockIo();
|
|
@@ -2375,7 +2478,9 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2375
2478
|
startChangeStreamWatcher(io, {ignoredCollections: ["audit_logs"]}, true);
|
|
2376
2479
|
|
|
2377
2480
|
// Verify the pipeline passed to watch includes the ignored collections
|
|
2378
|
-
const pipeline = (mockDb.watch.mock.calls[0] as
|
|
2481
|
+
const pipeline = (mockDb.watch.mock.calls[0] as unknown[])[0] as {
|
|
2482
|
+
$match: {"ns.coll": {$nin: string[]}};
|
|
2483
|
+
}[];
|
|
2379
2484
|
const matchStage = pipeline[0].$match;
|
|
2380
2485
|
expect(matchStage["ns.coll"].$nin).toContain("audit_logs");
|
|
2381
2486
|
expect(matchStage["ns.coll"].$nin).toContain("socketio");
|
|
@@ -2387,7 +2492,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2387
2492
|
const mockDb = {
|
|
2388
2493
|
watch: mock(() => mockStream),
|
|
2389
2494
|
};
|
|
2390
|
-
(
|
|
2495
|
+
setConnectionDb(mockDb);
|
|
2391
2496
|
|
|
2392
2497
|
registerRealtime({
|
|
2393
2498
|
collectionName: "todos",
|
|
@@ -2427,7 +2532,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2427
2532
|
const mockDb = {
|
|
2428
2533
|
watch: mock(() => mockStream),
|
|
2429
2534
|
};
|
|
2430
|
-
(
|
|
2535
|
+
setConnectionDb(mockDb);
|
|
2431
2536
|
|
|
2432
2537
|
const {startChangeStreamWatcher} = await import("./changeStreamWatcher");
|
|
2433
2538
|
const io = createMockIo();
|
|
@@ -2453,7 +2558,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2453
2558
|
const mockDb = {
|
|
2454
2559
|
watch: mock(() => mockStream),
|
|
2455
2560
|
};
|
|
2456
|
-
(
|
|
2561
|
+
setConnectionDb(mockDb);
|
|
2457
2562
|
|
|
2458
2563
|
const {startChangeStreamWatcher} = await import("./changeStreamWatcher");
|
|
2459
2564
|
const io = createMockIo();
|
|
@@ -2466,12 +2571,49 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2466
2571
|
});
|
|
2467
2572
|
});
|
|
2468
2573
|
|
|
2574
|
+
it("returns early when the mapped realtime operation is null", async () => {
|
|
2575
|
+
const mockStream = createMockChangeStream();
|
|
2576
|
+
const mockDb = {
|
|
2577
|
+
watch: mock(() => mockStream),
|
|
2578
|
+
};
|
|
2579
|
+
setConnectionDb(mockDb);
|
|
2580
|
+
|
|
2581
|
+
registerRealtime({
|
|
2582
|
+
collectionName: "todos",
|
|
2583
|
+
config: {
|
|
2584
|
+
methods: ["create", "update", "delete"],
|
|
2585
|
+
roomStrategy: "model",
|
|
2586
|
+
},
|
|
2587
|
+
modelName: "Todo",
|
|
2588
|
+
options: {permissions: permissiveOptions.permissions},
|
|
2589
|
+
routePath: "/todos",
|
|
2590
|
+
});
|
|
2591
|
+
|
|
2592
|
+
const {startChangeStreamWatcher} = await import("./changeStreamWatcher");
|
|
2593
|
+
const {emissions, io} = createRecordingIo("model:todos");
|
|
2594
|
+
let operationTypeReads = 0;
|
|
2595
|
+
startChangeStreamWatcher(io, {}, true);
|
|
2596
|
+
|
|
2597
|
+
await invokeRegisteredChangeHandler(mockStream, {
|
|
2598
|
+
documentKey: {_id: "doc-1"},
|
|
2599
|
+
fullDocument: {_id: "doc-1"},
|
|
2600
|
+
ns: {coll: "todos"},
|
|
2601
|
+
get operationType(): string {
|
|
2602
|
+
operationTypeReads += 1;
|
|
2603
|
+
return operationTypeReads === 1 ? "insert" : "invalidate";
|
|
2604
|
+
},
|
|
2605
|
+
});
|
|
2606
|
+
|
|
2607
|
+
assert.isAtLeast(operationTypeReads, 2);
|
|
2608
|
+
assert.isEmpty(emissions);
|
|
2609
|
+
});
|
|
2610
|
+
|
|
2469
2611
|
it("handles error/close/end events gracefully", async () => {
|
|
2470
2612
|
const mockStream = createMockChangeStream();
|
|
2471
2613
|
const mockDb = {
|
|
2472
2614
|
watch: mock(() => mockStream),
|
|
2473
2615
|
};
|
|
2474
|
-
(
|
|
2616
|
+
setConnectionDb(mockDb);
|
|
2475
2617
|
|
|
2476
2618
|
const {startChangeStreamWatcher} = await import("./changeStreamWatcher");
|
|
2477
2619
|
const io = createMockIo();
|
|
@@ -2530,7 +2672,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2530
2672
|
const serverEmissions: {event: string; payload: unknown}[] = [];
|
|
2531
2673
|
const rooms = new Map<string, Set<string>>();
|
|
2532
2674
|
rooms.set("model:todos", new Set(["sock-1"]));
|
|
2533
|
-
const sockets = new Map<string,
|
|
2675
|
+
const sockets = new Map<string, MockRoomSocket>();
|
|
2534
2676
|
sockets.set("sock-1", {
|
|
2535
2677
|
decodedToken: {id: "user-1"},
|
|
2536
2678
|
emit: (event: string, payload: unknown) => {
|
|
@@ -2556,7 +2698,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2556
2698
|
const secondStream = createMockChangeStream();
|
|
2557
2699
|
const streams = [firstStream, secondStream];
|
|
2558
2700
|
const mockDb = {watch: mock(() => streams.shift() ?? secondStream)};
|
|
2559
|
-
(
|
|
2701
|
+
setConnectionDb(mockDb);
|
|
2560
2702
|
registerTodos();
|
|
2561
2703
|
|
|
2562
2704
|
const {startChangeStreamWatcher} = await import("./changeStreamWatcher");
|
|
@@ -2576,7 +2718,9 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2576
2718
|
firstStream.trigger("error", new Error("replica set election"));
|
|
2577
2719
|
await waitFor(() => mockDb.watch.mock.calls.length >= 2, "the stream to reopen");
|
|
2578
2720
|
|
|
2579
|
-
const reopenOptions = (mockDb.watch.mock.calls[1] as
|
|
2721
|
+
const reopenOptions = (mockDb.watch.mock.calls[1] as unknown[])[1] as {
|
|
2722
|
+
resumeAfter?: unknown;
|
|
2723
|
+
};
|
|
2580
2724
|
expect(reopenOptions.resumeAfter).toEqual({_data: "token-1"});
|
|
2581
2725
|
|
|
2582
2726
|
// The reopened stream is fully wired: later writes still fan out.
|
|
@@ -2595,7 +2739,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2595
2739
|
const secondStream = createMockChangeStream();
|
|
2596
2740
|
const streams = [firstStream, secondStream];
|
|
2597
2741
|
const mockDb = {watch: mock(() => streams.shift() ?? secondStream)};
|
|
2598
|
-
(
|
|
2742
|
+
setConnectionDb(mockDb);
|
|
2599
2743
|
|
|
2600
2744
|
const {SYNC_RESYNC_EVENT, startChangeStreamWatcher} = await import("./changeStreamWatcher");
|
|
2601
2745
|
const {io, serverEmissions} = createRecordingIo();
|
|
@@ -2620,13 +2764,15 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2620
2764
|
|
|
2621
2765
|
await waitFor(() => mockDb.watch.mock.calls.length >= 2, "the stream to reopen");
|
|
2622
2766
|
// An unusable token must not be replayed — the reopen starts from now.
|
|
2623
|
-
expect(
|
|
2767
|
+
expect(
|
|
2768
|
+
((mockDb.watch.mock.calls[1] as unknown[])[1] as {resumeAfter?: unknown}).resumeAfter
|
|
2769
|
+
).toBeUndefined();
|
|
2624
2770
|
});
|
|
2625
2771
|
|
|
2626
2772
|
it("does not reopen after the watcher is stopped", async () => {
|
|
2627
2773
|
const mockStream = createMockChangeStream();
|
|
2628
2774
|
const mockDb = {watch: mock(() => mockStream)};
|
|
2629
|
-
(
|
|
2775
|
+
setConnectionDb(mockDb);
|
|
2630
2776
|
|
|
2631
2777
|
const {startChangeStreamWatcher, stopChangeStreamWatcher: stop} = await import(
|
|
2632
2778
|
"./changeStreamWatcher"
|
|
@@ -2646,14 +2792,17 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2646
2792
|
const mockDb = {
|
|
2647
2793
|
watch: mock(() => mockStream),
|
|
2648
2794
|
};
|
|
2649
|
-
(
|
|
2795
|
+
setConnectionDb(mockDb);
|
|
2650
2796
|
|
|
2651
2797
|
const {startChangeStreamWatcher} = await import("./changeStreamWatcher");
|
|
2652
2798
|
const io = createMockIo();
|
|
2653
2799
|
|
|
2654
2800
|
startChangeStreamWatcher(io, {batchSize: 100, fullDocument: "whenAvailable"}, true);
|
|
2655
2801
|
|
|
2656
|
-
const options = (mockDb.watch.mock.calls[0] as
|
|
2802
|
+
const options = (mockDb.watch.mock.calls[0] as unknown[])[1] as {
|
|
2803
|
+
batchSize?: number;
|
|
2804
|
+
fullDocument?: string;
|
|
2805
|
+
};
|
|
2657
2806
|
expect(options.batchSize).toBe(100);
|
|
2658
2807
|
expect(options.fullDocument).toBe("whenAvailable");
|
|
2659
2808
|
});
|
|
@@ -2663,7 +2812,7 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2663
2812
|
const mockDb = {
|
|
2664
2813
|
watch: mock(() => mockStream),
|
|
2665
2814
|
};
|
|
2666
|
-
(
|
|
2815
|
+
setConnectionDb(mockDb);
|
|
2667
2816
|
|
|
2668
2817
|
// Register with a model that will throw during permission check
|
|
2669
2818
|
registerRealtime({
|
|
@@ -2692,17 +2841,17 @@ describe("startChangeStreamWatcher", () => {
|
|
|
2692
2841
|
const {startChangeStreamWatcher} = await import("./changeStreamWatcher");
|
|
2693
2842
|
|
|
2694
2843
|
// Create an IO with a socket in the target room
|
|
2695
|
-
const emissions:
|
|
2696
|
-
const mockSocket = {
|
|
2844
|
+
const emissions: Array<{_data: unknown; _event: string}> = [];
|
|
2845
|
+
const mockSocket: MockRoomSocket = {
|
|
2697
2846
|
decodedToken: {id: "user-1"},
|
|
2698
|
-
emit: (_event: string, _data:
|
|
2847
|
+
emit: (_event: string, _data: unknown) => {
|
|
2699
2848
|
emissions.push({_data, _event});
|
|
2700
2849
|
},
|
|
2701
2850
|
id: "sock-1",
|
|
2702
2851
|
};
|
|
2703
2852
|
const rooms = new Map<string, Set<string>>();
|
|
2704
2853
|
rooms.set("model:todos", new Set(["sock-1"]));
|
|
2705
|
-
const sockets = new Map<string,
|
|
2854
|
+
const sockets = new Map<string, MockRoomSocket>();
|
|
2706
2855
|
sockets.set("sock-1", mockSocket);
|
|
2707
2856
|
const io = {
|
|
2708
2857
|
sockets: {
|
|
@@ -2734,7 +2883,7 @@ describe("stopChangeStreamWatcher", () => {
|
|
|
2734
2883
|
watch: mock(() => mockStream),
|
|
2735
2884
|
};
|
|
2736
2885
|
const originalDb = mongoose.connection.db;
|
|
2737
|
-
(
|
|
2886
|
+
setConnectionDb(mockDb);
|
|
2738
2887
|
|
|
2739
2888
|
const {startChangeStreamWatcher, stopChangeStreamWatcher} = await import(
|
|
2740
2889
|
"./changeStreamWatcher"
|
|
@@ -2754,7 +2903,7 @@ describe("stopChangeStreamWatcher", () => {
|
|
|
2754
2903
|
// Calling again should be a no-op
|
|
2755
2904
|
await stopChangeStreamWatcher();
|
|
2756
2905
|
|
|
2757
|
-
(
|
|
2906
|
+
setConnectionDb(originalDb);
|
|
2758
2907
|
});
|
|
2759
2908
|
});
|
|
2760
2909
|
|
|
@@ -2779,14 +2928,16 @@ describe("RealtimeApp.onServerCreated", () => {
|
|
|
2779
2928
|
return server;
|
|
2780
2929
|
};
|
|
2781
2930
|
|
|
2782
|
-
it("throws when
|
|
2931
|
+
it("throws when no socket authentication method is configured", () => {
|
|
2783
2932
|
const originalSecret = process.env.TOKEN_SECRET;
|
|
2784
2933
|
process.env.TOKEN_SECRET = "";
|
|
2785
2934
|
|
|
2786
2935
|
const app = new RealtimeApp({tokenSecret: undefined});
|
|
2787
2936
|
const server = makeServer();
|
|
2788
2937
|
|
|
2789
|
-
expect(() => app.onServerCreated(server)).toThrow(
|
|
2938
|
+
expect(() => app.onServerCreated(server)).toThrow(
|
|
2939
|
+
"Socket authentication requires TOKEN_SECRET or Better Auth"
|
|
2940
|
+
);
|
|
2790
2941
|
|
|
2791
2942
|
process.env.TOKEN_SECRET = originalSecret;
|
|
2792
2943
|
});
|
|
@@ -2801,13 +2952,13 @@ describe("RealtimeApp.onServerCreated", () => {
|
|
|
2801
2952
|
// Mock mongoose.connection.db for changeStreamWatcher
|
|
2802
2953
|
const originalDb = mongoose.connection.db;
|
|
2803
2954
|
const mockStream = {close: async () => {}, on: () => mockStream};
|
|
2804
|
-
(
|
|
2955
|
+
setConnectionDb({watch: () => mockStream});
|
|
2805
2956
|
|
|
2806
2957
|
app.onServerCreated(server);
|
|
2807
2958
|
expect(app.getIo()).toBeDefined();
|
|
2808
2959
|
|
|
2809
2960
|
await app.close();
|
|
2810
|
-
(
|
|
2961
|
+
setConnectionDb(originalDb);
|
|
2811
2962
|
});
|
|
2812
2963
|
});
|
|
2813
2964
|
|
|
@@ -2836,7 +2987,7 @@ describe("RealtimeApp.setupAdapter (private, via onServerCreated config)", () =>
|
|
|
2836
2987
|
process.env.REDIS_URL = "";
|
|
2837
2988
|
|
|
2838
2989
|
const mockStream = {close: async () => {}, on: () => mockStream};
|
|
2839
|
-
(
|
|
2990
|
+
setConnectionDb({watch: () => mockStream});
|
|
2840
2991
|
|
|
2841
2992
|
const app = new RealtimeApp({
|
|
2842
2993
|
adapter: "redis",
|
|
@@ -2849,7 +3000,7 @@ describe("RealtimeApp.setupAdapter (private, via onServerCreated config)", () =>
|
|
|
2849
3000
|
|
|
2850
3001
|
process.env.VALKEY_URL = originalValkey;
|
|
2851
3002
|
process.env.REDIS_URL = originalRedis;
|
|
2852
|
-
(
|
|
3003
|
+
setConnectionDb(originalDb);
|
|
2853
3004
|
});
|
|
2854
3005
|
|
|
2855
3006
|
it("logs info when redis adapter has a URL", async () => {
|
|
@@ -2858,7 +3009,7 @@ describe("RealtimeApp.setupAdapter (private, via onServerCreated config)", () =>
|
|
|
2858
3009
|
process.env.VALKEY_URL = "redis://user:pass@localhost:6379/0";
|
|
2859
3010
|
|
|
2860
3011
|
const mockStream = {close: async () => {}, on: () => mockStream};
|
|
2861
|
-
(
|
|
3012
|
+
setConnectionDb({watch: () => mockStream});
|
|
2862
3013
|
|
|
2863
3014
|
const app = new RealtimeApp({
|
|
2864
3015
|
adapter: "redis",
|
|
@@ -2871,13 +3022,13 @@ describe("RealtimeApp.setupAdapter (private, via onServerCreated config)", () =>
|
|
|
2871
3022
|
await app.close();
|
|
2872
3023
|
|
|
2873
3024
|
process.env.VALKEY_URL = originalValkey;
|
|
2874
|
-
(
|
|
3025
|
+
setConnectionDb(originalDb);
|
|
2875
3026
|
});
|
|
2876
3027
|
|
|
2877
3028
|
it("no-op adapter mode 'none'", async () => {
|
|
2878
3029
|
const originalDb = mongoose.connection.db;
|
|
2879
3030
|
const mockStream = {close: async () => {}, on: () => mockStream};
|
|
2880
|
-
(
|
|
3031
|
+
setConnectionDb({watch: () => mockStream});
|
|
2881
3032
|
|
|
2882
3033
|
const app = new RealtimeApp({
|
|
2883
3034
|
adapter: "none",
|
|
@@ -2889,7 +3040,7 @@ describe("RealtimeApp.setupAdapter (private, via onServerCreated config)", () =>
|
|
|
2889
3040
|
expect(app.getIo()).toBeDefined();
|
|
2890
3041
|
|
|
2891
3042
|
await app.close();
|
|
2892
|
-
(
|
|
3043
|
+
setConnectionDb(originalDb);
|
|
2893
3044
|
});
|
|
2894
3045
|
});
|
|
2895
3046
|
|
|
@@ -2934,10 +3085,10 @@ describe("ensureApiId", () => {
|
|
|
2934
3085
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
2935
3086
|
|
|
2936
3087
|
describe("startChangeStreamWatcher & stopChangeStreamWatcher", () => {
|
|
2937
|
-
const makeMockIo = ():
|
|
2938
|
-
const emissions:
|
|
3088
|
+
const makeMockIo = (): Server & {emissions: unknown[]} => {
|
|
3089
|
+
const emissions: unknown[] = [];
|
|
2939
3090
|
const rooms = new Map<string, Set<string>>();
|
|
2940
|
-
const sockets = new Map<string,
|
|
3091
|
+
const sockets = new Map<string, MockRoomSocket>();
|
|
2941
3092
|
return {
|
|
2942
3093
|
emissions,
|
|
2943
3094
|
sockets: {
|
|
@@ -2947,7 +3098,7 @@ describe("startChangeStreamWatcher & stopChangeStreamWatcher", () => {
|
|
|
2947
3098
|
to: (_room: string) => ({
|
|
2948
3099
|
emit: (): void => {},
|
|
2949
3100
|
}),
|
|
2950
|
-
};
|
|
3101
|
+
} as unknown as Server & {emissions: unknown[]};
|
|
2951
3102
|
};
|
|
2952
3103
|
|
|
2953
3104
|
afterEach(async () => {
|
|
@@ -3022,21 +3173,37 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3022
3173
|
{collection: "realtimetests", strict: "throw"}
|
|
3023
3174
|
);
|
|
3024
3175
|
|
|
3025
|
-
|
|
3176
|
+
interface RealtimeTestDoc {
|
|
3177
|
+
deleted?: boolean;
|
|
3178
|
+
name?: string;
|
|
3179
|
+
ownerId?: string;
|
|
3180
|
+
}
|
|
3181
|
+
|
|
3182
|
+
let RealtimeTestModel: Model<RealtimeTestDoc>;
|
|
3026
3183
|
try {
|
|
3027
3184
|
RealtimeTestModel = mongoose.model("RealtimeTest");
|
|
3028
3185
|
} catch {
|
|
3029
3186
|
RealtimeTestModel = mongoose.model("RealtimeTest", realtimeTestSchema);
|
|
3030
3187
|
}
|
|
3031
3188
|
|
|
3032
|
-
|
|
3033
|
-
|
|
3189
|
+
interface TrackedEmission {
|
|
3190
|
+
event: string;
|
|
3191
|
+
payload: RealtimeEvent;
|
|
3192
|
+
room?: string;
|
|
3193
|
+
socketId?: string;
|
|
3194
|
+
}
|
|
3195
|
+
|
|
3196
|
+
const makeTrackedIo = (): Server & {
|
|
3197
|
+
addSocketToRoom: (room: string, decodedToken?: DecodedRealtimeToken) => void;
|
|
3198
|
+
emissions: TrackedEmission[];
|
|
3199
|
+
} => {
|
|
3200
|
+
const emissions: TrackedEmission[] = [];
|
|
3034
3201
|
const rooms = new Map<string, Set<string>>();
|
|
3035
|
-
const sockets = new Map<string,
|
|
3202
|
+
const sockets = new Map<string, MockRoomSocket>();
|
|
3036
3203
|
|
|
3037
3204
|
const addSocketToRoom = (
|
|
3038
3205
|
room: string,
|
|
3039
|
-
decodedToken:
|
|
3206
|
+
decodedToken: DecodedRealtimeToken = {admin: true, id: "admin"}
|
|
3040
3207
|
): void => {
|
|
3041
3208
|
const socketId = `socket-${Math.random().toString(36).slice(2, 9)}`;
|
|
3042
3209
|
if (!rooms.has(room)) {
|
|
@@ -3046,7 +3213,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3046
3213
|
sockets.set(socketId, {
|
|
3047
3214
|
decodedToken,
|
|
3048
3215
|
emit: (event: string, payload: unknown): void => {
|
|
3049
|
-
emissions.push({event, payload, room, socketId});
|
|
3216
|
+
emissions.push({event, payload: payload as RealtimeEvent, room, socketId});
|
|
3050
3217
|
},
|
|
3051
3218
|
id: socketId,
|
|
3052
3219
|
});
|
|
@@ -3061,9 +3228,12 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3061
3228
|
},
|
|
3062
3229
|
to: (room: string) => ({
|
|
3063
3230
|
emit: (event: string, payload: unknown): void => {
|
|
3064
|
-
emissions.push({event, payload, room});
|
|
3231
|
+
emissions.push({event, payload: payload as RealtimeEvent, room});
|
|
3065
3232
|
},
|
|
3066
3233
|
}),
|
|
3234
|
+
} as unknown as Server & {
|
|
3235
|
+
addSocketToRoom: (room: string, decodedToken?: DecodedRealtimeToken) => void;
|
|
3236
|
+
emissions: TrackedEmission[];
|
|
3067
3237
|
};
|
|
3068
3238
|
};
|
|
3069
3239
|
|
|
@@ -3100,7 +3270,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3100
3270
|
read: [() => true],
|
|
3101
3271
|
update: [() => true],
|
|
3102
3272
|
},
|
|
3103
|
-
}
|
|
3273
|
+
},
|
|
3104
3274
|
routePath: "/realtimetests",
|
|
3105
3275
|
});
|
|
3106
3276
|
|
|
@@ -3114,7 +3284,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3114
3284
|
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
3115
3285
|
|
|
3116
3286
|
const createEmissions = io.emissions.filter(
|
|
3117
|
-
(e
|
|
3287
|
+
(e) => e.event === "sync" && e.payload.method === "create"
|
|
3118
3288
|
);
|
|
3119
3289
|
expect(createEmissions.length).toBeGreaterThanOrEqual(1);
|
|
3120
3290
|
await stopChangeStreamWatcher();
|
|
@@ -3136,7 +3306,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3136
3306
|
read: [() => true],
|
|
3137
3307
|
update: [() => true],
|
|
3138
3308
|
},
|
|
3139
|
-
}
|
|
3309
|
+
},
|
|
3140
3310
|
routePath: "/realtimetests",
|
|
3141
3311
|
});
|
|
3142
3312
|
|
|
@@ -3151,7 +3321,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3151
3321
|
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
3152
3322
|
|
|
3153
3323
|
const updateEmissions = io.emissions.filter(
|
|
3154
|
-
(e
|
|
3324
|
+
(e) => e.event === "sync" && e.payload?.method === "update"
|
|
3155
3325
|
);
|
|
3156
3326
|
expect(updateEmissions.length).toBeGreaterThanOrEqual(1);
|
|
3157
3327
|
await stopChangeStreamWatcher();
|
|
@@ -3173,7 +3343,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3173
3343
|
read: [() => true],
|
|
3174
3344
|
update: [() => true],
|
|
3175
3345
|
},
|
|
3176
|
-
}
|
|
3346
|
+
},
|
|
3177
3347
|
routePath: "/realtimetests",
|
|
3178
3348
|
});
|
|
3179
3349
|
|
|
@@ -3188,7 +3358,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3188
3358
|
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
3189
3359
|
|
|
3190
3360
|
const deleteEmissions = io.emissions.filter(
|
|
3191
|
-
(e
|
|
3361
|
+
(e) => e.event === "sync" && e.payload?.method === "delete"
|
|
3192
3362
|
);
|
|
3193
3363
|
expect(deleteEmissions.length).toBeGreaterThanOrEqual(1);
|
|
3194
3364
|
await stopChangeStreamWatcher();
|
|
@@ -3210,7 +3380,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3210
3380
|
read: [() => true],
|
|
3211
3381
|
update: [() => true],
|
|
3212
3382
|
},
|
|
3213
|
-
}
|
|
3383
|
+
},
|
|
3214
3384
|
routePath: "/realtimetests",
|
|
3215
3385
|
});
|
|
3216
3386
|
|
|
@@ -3225,7 +3395,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3225
3395
|
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
3226
3396
|
|
|
3227
3397
|
const deleteEmissions = io.emissions.filter(
|
|
3228
|
-
(e
|
|
3398
|
+
(e) => e.event === "sync" && e.payload?.method === "delete"
|
|
3229
3399
|
);
|
|
3230
3400
|
expect(deleteEmissions.length).toBeGreaterThanOrEqual(1);
|
|
3231
3401
|
await stopChangeStreamWatcher();
|
|
@@ -3247,7 +3417,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3247
3417
|
read: [() => true],
|
|
3248
3418
|
update: [() => true],
|
|
3249
3419
|
},
|
|
3250
|
-
}
|
|
3420
|
+
},
|
|
3251
3421
|
routePath: "/realtimetests",
|
|
3252
3422
|
});
|
|
3253
3423
|
|
|
@@ -3264,7 +3434,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3264
3434
|
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
3265
3435
|
|
|
3266
3436
|
const updateEmissions = io.emissions.filter(
|
|
3267
|
-
(e
|
|
3437
|
+
(e) => e.event === "sync" && e.payload?.method === "update"
|
|
3268
3438
|
);
|
|
3269
3439
|
expect(updateEmissions.length).toBeGreaterThanOrEqual(1);
|
|
3270
3440
|
if (updateEmissions.length > 0) {
|
|
@@ -3280,19 +3450,23 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3280
3450
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
3281
3451
|
|
|
3282
3452
|
describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
3283
|
-
const makeIoSimple = ():
|
|
3453
|
+
const makeIoSimple = (): Server & {
|
|
3454
|
+
emissions: Array<{room: string; event: string; payload: unknown}>;
|
|
3455
|
+
} => {
|
|
3284
3456
|
const emissions: Array<{room: string; event: string; payload: unknown}> = [];
|
|
3285
3457
|
return {
|
|
3286
3458
|
emissions,
|
|
3287
3459
|
sockets: {
|
|
3288
|
-
adapter: {rooms: new Map()},
|
|
3289
|
-
sockets: new Map(),
|
|
3460
|
+
adapter: {rooms: new Map<string, Set<string>>()},
|
|
3461
|
+
sockets: new Map<string, MockRoomSocket>(),
|
|
3290
3462
|
},
|
|
3291
3463
|
to: (room: string) => ({
|
|
3292
3464
|
emit: (event: string, payload: unknown): void => {
|
|
3293
3465
|
emissions.push({event, payload, room});
|
|
3294
3466
|
},
|
|
3295
3467
|
}),
|
|
3468
|
+
} as unknown as Server & {
|
|
3469
|
+
emissions: Array<{room: string; event: string; payload: unknown}>;
|
|
3296
3470
|
};
|
|
3297
3471
|
};
|
|
3298
3472
|
|
|
@@ -3306,7 +3480,7 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3306
3480
|
|
|
3307
3481
|
it("emits to document room via io.to when no entry is provided", async () => {
|
|
3308
3482
|
const io = makeIoSimple();
|
|
3309
|
-
const event:
|
|
3483
|
+
const event: RealtimeEvent = {
|
|
3310
3484
|
collection: "items",
|
|
3311
3485
|
id: "doc-1",
|
|
3312
3486
|
method: "update",
|
|
@@ -3314,14 +3488,14 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3314
3488
|
timestamp: 1,
|
|
3315
3489
|
};
|
|
3316
3490
|
await emitToDocumentAndQueryRooms(io, "items", event, {}, () => {});
|
|
3317
|
-
expect(io.emissions.some((e
|
|
3491
|
+
expect(io.emissions.some((e) => e.room === "document:items:doc-1")).toBe(true);
|
|
3318
3492
|
});
|
|
3319
3493
|
|
|
3320
3494
|
it("emits hard deletes to query rooms via io.to when no entry", async () => {
|
|
3321
3495
|
const queryId = computeQueryId("items", {status: "active"});
|
|
3322
3496
|
addQuerySubscription("socket-a", "items", {status: "active"}, queryId);
|
|
3323
3497
|
const io = makeIoSimple();
|
|
3324
|
-
const event:
|
|
3498
|
+
const event: RealtimeEvent = {
|
|
3325
3499
|
collection: "items",
|
|
3326
3500
|
id: "doc-1",
|
|
3327
3501
|
method: "delete",
|
|
@@ -3329,14 +3503,14 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3329
3503
|
timestamp: 1,
|
|
3330
3504
|
};
|
|
3331
3505
|
await emitToDocumentAndQueryRooms(io, "items", event, undefined, () => {});
|
|
3332
|
-
expect(io.emissions.some((e
|
|
3506
|
+
expect(io.emissions.some((e) => e.room === `query:${queryId}`)).toBe(true);
|
|
3333
3507
|
});
|
|
3334
3508
|
|
|
3335
3509
|
it("emits soft delete to query rooms via io.to when no entry and doc matches", async () => {
|
|
3336
3510
|
const queryId = computeQueryId("items", {status: "active"});
|
|
3337
3511
|
addQuerySubscription("socket-a", "items", {status: "active"}, queryId);
|
|
3338
3512
|
const io = makeIoSimple();
|
|
3339
|
-
const event:
|
|
3513
|
+
const event: RealtimeEvent = {
|
|
3340
3514
|
collection: "items",
|
|
3341
3515
|
id: "doc-1",
|
|
3342
3516
|
method: "delete",
|
|
@@ -3344,14 +3518,14 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3344
3518
|
timestamp: 1,
|
|
3345
3519
|
};
|
|
3346
3520
|
await emitToDocumentAndQueryRooms(io, "items", event, {status: "active"}, () => {});
|
|
3347
|
-
expect(io.emissions.some((e
|
|
3521
|
+
expect(io.emissions.some((e) => e.room === `query:${queryId}`)).toBe(true);
|
|
3348
3522
|
});
|
|
3349
3523
|
|
|
3350
3524
|
it("emits create events to query rooms via io.to when no entry and doc matches", async () => {
|
|
3351
3525
|
const queryId = computeQueryId("items", {status: "active"});
|
|
3352
3526
|
addQuerySubscription("socket-a", "items", {status: "active"}, queryId);
|
|
3353
3527
|
const io = makeIoSimple();
|
|
3354
|
-
const event:
|
|
3528
|
+
const event: RealtimeEvent = {
|
|
3355
3529
|
collection: "items",
|
|
3356
3530
|
id: "doc-1",
|
|
3357
3531
|
method: "create",
|
|
@@ -3359,14 +3533,14 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3359
3533
|
timestamp: 1,
|
|
3360
3534
|
};
|
|
3361
3535
|
await emitToDocumentAndQueryRooms(io, "items", event, {status: "active"}, () => {});
|
|
3362
|
-
expect(io.emissions.some((e
|
|
3536
|
+
expect(io.emissions.some((e) => e.room === `query:${queryId}`)).toBe(true);
|
|
3363
3537
|
});
|
|
3364
3538
|
|
|
3365
3539
|
it("emits update events to query rooms via io.to when no entry and doc matches", async () => {
|
|
3366
3540
|
const queryId = computeQueryId("items", {status: "active"});
|
|
3367
3541
|
addQuerySubscription("socket-a", "items", {status: "active"}, queryId);
|
|
3368
3542
|
const io = makeIoSimple();
|
|
3369
|
-
const event:
|
|
3543
|
+
const event: RealtimeEvent = {
|
|
3370
3544
|
collection: "items",
|
|
3371
3545
|
id: "doc-1",
|
|
3372
3546
|
method: "update",
|
|
@@ -3374,14 +3548,14 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3374
3548
|
timestamp: 1,
|
|
3375
3549
|
};
|
|
3376
3550
|
await emitToDocumentAndQueryRooms(io, "items", event, {status: "active"}, () => {});
|
|
3377
|
-
expect(io.emissions.some((e
|
|
3551
|
+
expect(io.emissions.some((e) => e.room === `query:${queryId}`)).toBe(true);
|
|
3378
3552
|
});
|
|
3379
3553
|
|
|
3380
3554
|
it("emits delete to query rooms via io.to when update no longer matches and no entry", async () => {
|
|
3381
3555
|
const queryId = computeQueryId("items", {status: "active"});
|
|
3382
3556
|
addQuerySubscription("socket-a", "items", {status: "active"}, queryId);
|
|
3383
3557
|
const io = makeIoSimple();
|
|
3384
|
-
const event:
|
|
3558
|
+
const event: RealtimeEvent = {
|
|
3385
3559
|
collection: "items",
|
|
3386
3560
|
id: "doc-1",
|
|
3387
3561
|
method: "update",
|
|
@@ -3389,7 +3563,7 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3389
3563
|
timestamp: 1,
|
|
3390
3564
|
};
|
|
3391
3565
|
await emitToDocumentAndQueryRooms(io, "items", event, {status: "inactive"}, () => {});
|
|
3392
|
-
const queryEmissions = io.emissions.filter((e
|
|
3566
|
+
const queryEmissions = io.emissions.filter((e) => e.room === `query:${queryId}`);
|
|
3393
3567
|
expect(queryEmissions.length).toBe(1);
|
|
3394
3568
|
expect(queryEmissions[0].payload).toMatchObject({method: "delete"});
|
|
3395
3569
|
});
|
|
@@ -3440,7 +3614,7 @@ describe("RealtimeApp — onServerCreated and setupAdapter", () => {
|
|
|
3440
3614
|
server.close();
|
|
3441
3615
|
});
|
|
3442
3616
|
|
|
3443
|
-
it("onServerCreated throws when
|
|
3617
|
+
it("onServerCreated throws when no socket authentication method is configured", () => {
|
|
3444
3618
|
const http = require("node:http");
|
|
3445
3619
|
const origSecret = process.env.TOKEN_SECRET;
|
|
3446
3620
|
process.env.TOKEN_SECRET = "";
|
|
@@ -3449,7 +3623,9 @@ describe("RealtimeApp — onServerCreated and setupAdapter", () => {
|
|
|
3449
3623
|
app.register(expressApp);
|
|
3450
3624
|
const server = http.createServer(expressApp);
|
|
3451
3625
|
|
|
3452
|
-
expect(() => app.onServerCreated(server)).toThrow(
|
|
3626
|
+
expect(() => app.onServerCreated(server)).toThrow(
|
|
3627
|
+
"Socket authentication requires TOKEN_SECRET or Better Auth"
|
|
3628
|
+
);
|
|
3453
3629
|
process.env.TOKEN_SECRET = origSecret;
|
|
3454
3630
|
server.close();
|
|
3455
3631
|
});
|