@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,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
|
|
|
@@ -2803,13 +2952,13 @@ describe("RealtimeApp.onServerCreated", () => {
|
|
|
2803
2952
|
// Mock mongoose.connection.db for changeStreamWatcher
|
|
2804
2953
|
const originalDb = mongoose.connection.db;
|
|
2805
2954
|
const mockStream = {close: async () => {}, on: () => mockStream};
|
|
2806
|
-
(
|
|
2955
|
+
setConnectionDb({watch: () => mockStream});
|
|
2807
2956
|
|
|
2808
2957
|
app.onServerCreated(server);
|
|
2809
2958
|
expect(app.getIo()).toBeDefined();
|
|
2810
2959
|
|
|
2811
2960
|
await app.close();
|
|
2812
|
-
(
|
|
2961
|
+
setConnectionDb(originalDb);
|
|
2813
2962
|
});
|
|
2814
2963
|
});
|
|
2815
2964
|
|
|
@@ -2838,7 +2987,7 @@ describe("RealtimeApp.setupAdapter (private, via onServerCreated config)", () =>
|
|
|
2838
2987
|
process.env.REDIS_URL = "";
|
|
2839
2988
|
|
|
2840
2989
|
const mockStream = {close: async () => {}, on: () => mockStream};
|
|
2841
|
-
(
|
|
2990
|
+
setConnectionDb({watch: () => mockStream});
|
|
2842
2991
|
|
|
2843
2992
|
const app = new RealtimeApp({
|
|
2844
2993
|
adapter: "redis",
|
|
@@ -2851,7 +3000,7 @@ describe("RealtimeApp.setupAdapter (private, via onServerCreated config)", () =>
|
|
|
2851
3000
|
|
|
2852
3001
|
process.env.VALKEY_URL = originalValkey;
|
|
2853
3002
|
process.env.REDIS_URL = originalRedis;
|
|
2854
|
-
(
|
|
3003
|
+
setConnectionDb(originalDb);
|
|
2855
3004
|
});
|
|
2856
3005
|
|
|
2857
3006
|
it("logs info when redis adapter has a URL", async () => {
|
|
@@ -2860,7 +3009,7 @@ describe("RealtimeApp.setupAdapter (private, via onServerCreated config)", () =>
|
|
|
2860
3009
|
process.env.VALKEY_URL = "redis://user:pass@localhost:6379/0";
|
|
2861
3010
|
|
|
2862
3011
|
const mockStream = {close: async () => {}, on: () => mockStream};
|
|
2863
|
-
(
|
|
3012
|
+
setConnectionDb({watch: () => mockStream});
|
|
2864
3013
|
|
|
2865
3014
|
const app = new RealtimeApp({
|
|
2866
3015
|
adapter: "redis",
|
|
@@ -2873,13 +3022,13 @@ describe("RealtimeApp.setupAdapter (private, via onServerCreated config)", () =>
|
|
|
2873
3022
|
await app.close();
|
|
2874
3023
|
|
|
2875
3024
|
process.env.VALKEY_URL = originalValkey;
|
|
2876
|
-
(
|
|
3025
|
+
setConnectionDb(originalDb);
|
|
2877
3026
|
});
|
|
2878
3027
|
|
|
2879
3028
|
it("no-op adapter mode 'none'", async () => {
|
|
2880
3029
|
const originalDb = mongoose.connection.db;
|
|
2881
3030
|
const mockStream = {close: async () => {}, on: () => mockStream};
|
|
2882
|
-
(
|
|
3031
|
+
setConnectionDb({watch: () => mockStream});
|
|
2883
3032
|
|
|
2884
3033
|
const app = new RealtimeApp({
|
|
2885
3034
|
adapter: "none",
|
|
@@ -2891,7 +3040,7 @@ describe("RealtimeApp.setupAdapter (private, via onServerCreated config)", () =>
|
|
|
2891
3040
|
expect(app.getIo()).toBeDefined();
|
|
2892
3041
|
|
|
2893
3042
|
await app.close();
|
|
2894
|
-
(
|
|
3043
|
+
setConnectionDb(originalDb);
|
|
2895
3044
|
});
|
|
2896
3045
|
});
|
|
2897
3046
|
|
|
@@ -2936,10 +3085,10 @@ describe("ensureApiId", () => {
|
|
|
2936
3085
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
2937
3086
|
|
|
2938
3087
|
describe("startChangeStreamWatcher & stopChangeStreamWatcher", () => {
|
|
2939
|
-
const makeMockIo = ():
|
|
2940
|
-
const emissions:
|
|
3088
|
+
const makeMockIo = (): Server & {emissions: unknown[]} => {
|
|
3089
|
+
const emissions: unknown[] = [];
|
|
2941
3090
|
const rooms = new Map<string, Set<string>>();
|
|
2942
|
-
const sockets = new Map<string,
|
|
3091
|
+
const sockets = new Map<string, MockRoomSocket>();
|
|
2943
3092
|
return {
|
|
2944
3093
|
emissions,
|
|
2945
3094
|
sockets: {
|
|
@@ -2949,7 +3098,7 @@ describe("startChangeStreamWatcher & stopChangeStreamWatcher", () => {
|
|
|
2949
3098
|
to: (_room: string) => ({
|
|
2950
3099
|
emit: (): void => {},
|
|
2951
3100
|
}),
|
|
2952
|
-
};
|
|
3101
|
+
} as unknown as Server & {emissions: unknown[]};
|
|
2953
3102
|
};
|
|
2954
3103
|
|
|
2955
3104
|
afterEach(async () => {
|
|
@@ -3024,21 +3173,37 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3024
3173
|
{collection: "realtimetests", strict: "throw"}
|
|
3025
3174
|
);
|
|
3026
3175
|
|
|
3027
|
-
|
|
3176
|
+
interface RealtimeTestDoc {
|
|
3177
|
+
deleted?: boolean;
|
|
3178
|
+
name?: string;
|
|
3179
|
+
ownerId?: string;
|
|
3180
|
+
}
|
|
3181
|
+
|
|
3182
|
+
let RealtimeTestModel: Model<RealtimeTestDoc>;
|
|
3028
3183
|
try {
|
|
3029
3184
|
RealtimeTestModel = mongoose.model("RealtimeTest");
|
|
3030
3185
|
} catch {
|
|
3031
3186
|
RealtimeTestModel = mongoose.model("RealtimeTest", realtimeTestSchema);
|
|
3032
3187
|
}
|
|
3033
3188
|
|
|
3034
|
-
|
|
3035
|
-
|
|
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[] = [];
|
|
3036
3201
|
const rooms = new Map<string, Set<string>>();
|
|
3037
|
-
const sockets = new Map<string,
|
|
3202
|
+
const sockets = new Map<string, MockRoomSocket>();
|
|
3038
3203
|
|
|
3039
3204
|
const addSocketToRoom = (
|
|
3040
3205
|
room: string,
|
|
3041
|
-
decodedToken:
|
|
3206
|
+
decodedToken: DecodedRealtimeToken = {admin: true, id: "admin"}
|
|
3042
3207
|
): void => {
|
|
3043
3208
|
const socketId = `socket-${Math.random().toString(36).slice(2, 9)}`;
|
|
3044
3209
|
if (!rooms.has(room)) {
|
|
@@ -3048,7 +3213,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3048
3213
|
sockets.set(socketId, {
|
|
3049
3214
|
decodedToken,
|
|
3050
3215
|
emit: (event: string, payload: unknown): void => {
|
|
3051
|
-
emissions.push({event, payload, room, socketId});
|
|
3216
|
+
emissions.push({event, payload: payload as RealtimeEvent, room, socketId});
|
|
3052
3217
|
},
|
|
3053
3218
|
id: socketId,
|
|
3054
3219
|
});
|
|
@@ -3063,9 +3228,12 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3063
3228
|
},
|
|
3064
3229
|
to: (room: string) => ({
|
|
3065
3230
|
emit: (event: string, payload: unknown): void => {
|
|
3066
|
-
emissions.push({event, payload, room});
|
|
3231
|
+
emissions.push({event, payload: payload as RealtimeEvent, room});
|
|
3067
3232
|
},
|
|
3068
3233
|
}),
|
|
3234
|
+
} as unknown as Server & {
|
|
3235
|
+
addSocketToRoom: (room: string, decodedToken?: DecodedRealtimeToken) => void;
|
|
3236
|
+
emissions: TrackedEmission[];
|
|
3069
3237
|
};
|
|
3070
3238
|
};
|
|
3071
3239
|
|
|
@@ -3102,7 +3270,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3102
3270
|
read: [() => true],
|
|
3103
3271
|
update: [() => true],
|
|
3104
3272
|
},
|
|
3105
|
-
}
|
|
3273
|
+
},
|
|
3106
3274
|
routePath: "/realtimetests",
|
|
3107
3275
|
});
|
|
3108
3276
|
|
|
@@ -3116,7 +3284,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3116
3284
|
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
3117
3285
|
|
|
3118
3286
|
const createEmissions = io.emissions.filter(
|
|
3119
|
-
(e
|
|
3287
|
+
(e) => e.event === "sync" && e.payload.method === "create"
|
|
3120
3288
|
);
|
|
3121
3289
|
expect(createEmissions.length).toBeGreaterThanOrEqual(1);
|
|
3122
3290
|
await stopChangeStreamWatcher();
|
|
@@ -3138,7 +3306,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3138
3306
|
read: [() => true],
|
|
3139
3307
|
update: [() => true],
|
|
3140
3308
|
},
|
|
3141
|
-
}
|
|
3309
|
+
},
|
|
3142
3310
|
routePath: "/realtimetests",
|
|
3143
3311
|
});
|
|
3144
3312
|
|
|
@@ -3153,7 +3321,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3153
3321
|
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
3154
3322
|
|
|
3155
3323
|
const updateEmissions = io.emissions.filter(
|
|
3156
|
-
(e
|
|
3324
|
+
(e) => e.event === "sync" && e.payload?.method === "update"
|
|
3157
3325
|
);
|
|
3158
3326
|
expect(updateEmissions.length).toBeGreaterThanOrEqual(1);
|
|
3159
3327
|
await stopChangeStreamWatcher();
|
|
@@ -3175,7 +3343,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3175
3343
|
read: [() => true],
|
|
3176
3344
|
update: [() => true],
|
|
3177
3345
|
},
|
|
3178
|
-
}
|
|
3346
|
+
},
|
|
3179
3347
|
routePath: "/realtimetests",
|
|
3180
3348
|
});
|
|
3181
3349
|
|
|
@@ -3190,7 +3358,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3190
3358
|
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
3191
3359
|
|
|
3192
3360
|
const deleteEmissions = io.emissions.filter(
|
|
3193
|
-
(e
|
|
3361
|
+
(e) => e.event === "sync" && e.payload?.method === "delete"
|
|
3194
3362
|
);
|
|
3195
3363
|
expect(deleteEmissions.length).toBeGreaterThanOrEqual(1);
|
|
3196
3364
|
await stopChangeStreamWatcher();
|
|
@@ -3212,7 +3380,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3212
3380
|
read: [() => true],
|
|
3213
3381
|
update: [() => true],
|
|
3214
3382
|
},
|
|
3215
|
-
}
|
|
3383
|
+
},
|
|
3216
3384
|
routePath: "/realtimetests",
|
|
3217
3385
|
});
|
|
3218
3386
|
|
|
@@ -3227,7 +3395,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3227
3395
|
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
3228
3396
|
|
|
3229
3397
|
const deleteEmissions = io.emissions.filter(
|
|
3230
|
-
(e
|
|
3398
|
+
(e) => e.event === "sync" && e.payload?.method === "delete"
|
|
3231
3399
|
);
|
|
3232
3400
|
expect(deleteEmissions.length).toBeGreaterThanOrEqual(1);
|
|
3233
3401
|
await stopChangeStreamWatcher();
|
|
@@ -3249,7 +3417,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3249
3417
|
read: [() => true],
|
|
3250
3418
|
update: [() => true],
|
|
3251
3419
|
},
|
|
3252
|
-
}
|
|
3420
|
+
},
|
|
3253
3421
|
routePath: "/realtimetests",
|
|
3254
3422
|
});
|
|
3255
3423
|
|
|
@@ -3266,7 +3434,7 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3266
3434
|
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
3267
3435
|
|
|
3268
3436
|
const updateEmissions = io.emissions.filter(
|
|
3269
|
-
(e
|
|
3437
|
+
(e) => e.event === "sync" && e.payload?.method === "update"
|
|
3270
3438
|
);
|
|
3271
3439
|
expect(updateEmissions.length).toBeGreaterThanOrEqual(1);
|
|
3272
3440
|
if (updateEmissions.length > 0) {
|
|
@@ -3282,19 +3450,23 @@ describe("startChangeStreamWatcher — change event integration", () => {
|
|
|
3282
3450
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
3283
3451
|
|
|
3284
3452
|
describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
3285
|
-
const makeIoSimple = ():
|
|
3453
|
+
const makeIoSimple = (): Server & {
|
|
3454
|
+
emissions: Array<{room: string; event: string; payload: unknown}>;
|
|
3455
|
+
} => {
|
|
3286
3456
|
const emissions: Array<{room: string; event: string; payload: unknown}> = [];
|
|
3287
3457
|
return {
|
|
3288
3458
|
emissions,
|
|
3289
3459
|
sockets: {
|
|
3290
|
-
adapter: {rooms: new Map()},
|
|
3291
|
-
sockets: new Map(),
|
|
3460
|
+
adapter: {rooms: new Map<string, Set<string>>()},
|
|
3461
|
+
sockets: new Map<string, MockRoomSocket>(),
|
|
3292
3462
|
},
|
|
3293
3463
|
to: (room: string) => ({
|
|
3294
3464
|
emit: (event: string, payload: unknown): void => {
|
|
3295
3465
|
emissions.push({event, payload, room});
|
|
3296
3466
|
},
|
|
3297
3467
|
}),
|
|
3468
|
+
} as unknown as Server & {
|
|
3469
|
+
emissions: Array<{room: string; event: string; payload: unknown}>;
|
|
3298
3470
|
};
|
|
3299
3471
|
};
|
|
3300
3472
|
|
|
@@ -3308,7 +3480,7 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3308
3480
|
|
|
3309
3481
|
it("emits to document room via io.to when no entry is provided", async () => {
|
|
3310
3482
|
const io = makeIoSimple();
|
|
3311
|
-
const event:
|
|
3483
|
+
const event: RealtimeEvent = {
|
|
3312
3484
|
collection: "items",
|
|
3313
3485
|
id: "doc-1",
|
|
3314
3486
|
method: "update",
|
|
@@ -3316,14 +3488,14 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3316
3488
|
timestamp: 1,
|
|
3317
3489
|
};
|
|
3318
3490
|
await emitToDocumentAndQueryRooms(io, "items", event, {}, () => {});
|
|
3319
|
-
expect(io.emissions.some((e
|
|
3491
|
+
expect(io.emissions.some((e) => e.room === "document:items:doc-1")).toBe(true);
|
|
3320
3492
|
});
|
|
3321
3493
|
|
|
3322
3494
|
it("emits hard deletes to query rooms via io.to when no entry", async () => {
|
|
3323
3495
|
const queryId = computeQueryId("items", {status: "active"});
|
|
3324
3496
|
addQuerySubscription("socket-a", "items", {status: "active"}, queryId);
|
|
3325
3497
|
const io = makeIoSimple();
|
|
3326
|
-
const event:
|
|
3498
|
+
const event: RealtimeEvent = {
|
|
3327
3499
|
collection: "items",
|
|
3328
3500
|
id: "doc-1",
|
|
3329
3501
|
method: "delete",
|
|
@@ -3331,14 +3503,14 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3331
3503
|
timestamp: 1,
|
|
3332
3504
|
};
|
|
3333
3505
|
await emitToDocumentAndQueryRooms(io, "items", event, undefined, () => {});
|
|
3334
|
-
expect(io.emissions.some((e
|
|
3506
|
+
expect(io.emissions.some((e) => e.room === `query:${queryId}`)).toBe(true);
|
|
3335
3507
|
});
|
|
3336
3508
|
|
|
3337
3509
|
it("emits soft delete to query rooms via io.to when no entry and doc matches", async () => {
|
|
3338
3510
|
const queryId = computeQueryId("items", {status: "active"});
|
|
3339
3511
|
addQuerySubscription("socket-a", "items", {status: "active"}, queryId);
|
|
3340
3512
|
const io = makeIoSimple();
|
|
3341
|
-
const event:
|
|
3513
|
+
const event: RealtimeEvent = {
|
|
3342
3514
|
collection: "items",
|
|
3343
3515
|
id: "doc-1",
|
|
3344
3516
|
method: "delete",
|
|
@@ -3346,14 +3518,14 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3346
3518
|
timestamp: 1,
|
|
3347
3519
|
};
|
|
3348
3520
|
await emitToDocumentAndQueryRooms(io, "items", event, {status: "active"}, () => {});
|
|
3349
|
-
expect(io.emissions.some((e
|
|
3521
|
+
expect(io.emissions.some((e) => e.room === `query:${queryId}`)).toBe(true);
|
|
3350
3522
|
});
|
|
3351
3523
|
|
|
3352
3524
|
it("emits create events to query rooms via io.to when no entry and doc matches", async () => {
|
|
3353
3525
|
const queryId = computeQueryId("items", {status: "active"});
|
|
3354
3526
|
addQuerySubscription("socket-a", "items", {status: "active"}, queryId);
|
|
3355
3527
|
const io = makeIoSimple();
|
|
3356
|
-
const event:
|
|
3528
|
+
const event: RealtimeEvent = {
|
|
3357
3529
|
collection: "items",
|
|
3358
3530
|
id: "doc-1",
|
|
3359
3531
|
method: "create",
|
|
@@ -3361,14 +3533,14 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3361
3533
|
timestamp: 1,
|
|
3362
3534
|
};
|
|
3363
3535
|
await emitToDocumentAndQueryRooms(io, "items", event, {status: "active"}, () => {});
|
|
3364
|
-
expect(io.emissions.some((e
|
|
3536
|
+
expect(io.emissions.some((e) => e.room === `query:${queryId}`)).toBe(true);
|
|
3365
3537
|
});
|
|
3366
3538
|
|
|
3367
3539
|
it("emits update events to query rooms via io.to when no entry and doc matches", async () => {
|
|
3368
3540
|
const queryId = computeQueryId("items", {status: "active"});
|
|
3369
3541
|
addQuerySubscription("socket-a", "items", {status: "active"}, queryId);
|
|
3370
3542
|
const io = makeIoSimple();
|
|
3371
|
-
const event:
|
|
3543
|
+
const event: RealtimeEvent = {
|
|
3372
3544
|
collection: "items",
|
|
3373
3545
|
id: "doc-1",
|
|
3374
3546
|
method: "update",
|
|
@@ -3376,14 +3548,14 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3376
3548
|
timestamp: 1,
|
|
3377
3549
|
};
|
|
3378
3550
|
await emitToDocumentAndQueryRooms(io, "items", event, {status: "active"}, () => {});
|
|
3379
|
-
expect(io.emissions.some((e
|
|
3551
|
+
expect(io.emissions.some((e) => e.room === `query:${queryId}`)).toBe(true);
|
|
3380
3552
|
});
|
|
3381
3553
|
|
|
3382
3554
|
it("emits delete to query rooms via io.to when update no longer matches and no entry", async () => {
|
|
3383
3555
|
const queryId = computeQueryId("items", {status: "active"});
|
|
3384
3556
|
addQuerySubscription("socket-a", "items", {status: "active"}, queryId);
|
|
3385
3557
|
const io = makeIoSimple();
|
|
3386
|
-
const event:
|
|
3558
|
+
const event: RealtimeEvent = {
|
|
3387
3559
|
collection: "items",
|
|
3388
3560
|
id: "doc-1",
|
|
3389
3561
|
method: "update",
|
|
@@ -3391,7 +3563,7 @@ describe("emitToDocumentAndQueryRooms — no registry entry", () => {
|
|
|
3391
3563
|
timestamp: 1,
|
|
3392
3564
|
};
|
|
3393
3565
|
await emitToDocumentAndQueryRooms(io, "items", event, {status: "inactive"}, () => {});
|
|
3394
|
-
const queryEmissions = io.emissions.filter((e
|
|
3566
|
+
const queryEmissions = io.emissions.filter((e) => e.room === `query:${queryId}`);
|
|
3395
3567
|
expect(queryEmissions.length).toBe(1);
|
|
3396
3568
|
expect(queryEmissions[0].payload).toMatchObject({method: "delete"});
|
|
3397
3569
|
});
|