@terreno/api 0.31.0 → 56.0.0-beta.2
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/dist/api.d.ts +10 -0
- package/dist/api.js +110 -361
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +121 -26
- package/dist/betterAuth.d.ts +5 -0
- package/dist/betterAuthSetup.js +23 -4
- package/dist/docLoader.js +12 -9
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +29 -1
- package/dist/index.d.ts +17 -1
- package/dist/index.js +29 -1
- package/dist/middleware.d.ts +7 -0
- package/dist/middleware.js +16 -1
- package/dist/openApiCompat.d.ts +10 -1
- package/dist/openApiCompat.js +115 -47
- package/dist/permissions.d.ts +27 -0
- package/dist/permissions.js +42 -1
- package/dist/plugins.d.ts +13 -0
- package/dist/plugins.js +47 -1
- package/dist/realtime/changeStreamWatcher.d.ts +100 -3
- package/dist/realtime/changeStreamWatcher.js +542 -137
- package/dist/realtime/realtimeApp.d.ts +2 -0
- package/dist/realtime/realtimeApp.js +83 -27
- package/dist/realtime/sessionRevalidation.d.ts +90 -0
- package/dist/realtime/sessionRevalidation.js +438 -0
- package/dist/realtime/socketAuth.d.ts +97 -0
- package/dist/realtime/socketAuth.js +272 -0
- package/dist/realtime/socketUser.d.ts +54 -0
- package/dist/realtime/socketUser.js +75 -5
- package/dist/realtime/types.d.ts +54 -0
- package/dist/secretProviders.d.ts +23 -0
- package/dist/secretProviders.js +9 -3
- package/dist/sync/executors.d.ts +198 -0
- package/dist/sync/executors.js +670 -0
- package/dist/sync/loadHarness.js +807 -0
- package/dist/sync/models.d.ts +216 -0
- package/dist/sync/models.js +644 -0
- package/dist/sync/mutationHandler.d.ts +97 -0
- package/dist/sync/mutationHandler.js +886 -0
- package/dist/sync/registry.d.ts +73 -0
- package/dist/sync/registry.js +223 -0
- package/dist/sync/routes.d.ts +62 -0
- package/dist/sync/routes.js +936 -0
- package/dist/sync/scripts/compactTombstones.d.ts +47 -0
- package/dist/sync/scripts/compactTombstones.js +270 -0
- package/dist/sync/serialize.d.ts +20 -0
- package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
- package/dist/sync/socketHandlers.d.ts +54 -0
- package/dist/sync/socketHandlers.js +470 -0
- package/dist/sync/streams.d.ts +85 -0
- package/dist/sync/streams.js +221 -0
- package/dist/sync/syncApp.d.ts +29 -0
- package/dist/sync/syncApp.js +44 -0
- package/dist/sync/syncBatch.d.ts +71 -0
- package/dist/sync/syncBatch.js +252 -0
- package/dist/sync/syncSeqPlugin.d.ts +8 -0
- package/dist/sync/syncSeqPlugin.js +899 -0
- package/dist/sync/types.d.ts +254 -0
- package/dist/sync/types.js +9 -0
- package/dist/terrenoApp.js +90 -27
- package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
- package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
- package/package.json +7 -6
- package/src/__tests__/openApiCompat.test.ts +20 -0
- package/src/api.errors.test.ts +32 -23
- package/src/api.test.ts +12 -0
- package/src/api.ts +87 -296
- package/src/auth.test.ts +202 -0
- package/src/auth.ts +134 -23
- package/src/betterAuth.ts +6 -0
- package/src/betterAuthSetup.test.ts +34 -0
- package/src/betterAuthSetup.ts +20 -1
- package/src/configuration.test.ts +59 -38
- package/src/configurationPlugin.test.ts +72 -42
- package/src/docLoader.ts +3 -1
- package/src/errors.ts +33 -0
- package/src/expressServer.test.ts +7 -1
- package/src/index.ts +42 -0
- package/src/middleware.test.ts +56 -1
- package/src/middleware.ts +15 -0
- package/src/openApiCompat.ts +99 -45
- package/src/permissions.test.ts +101 -17
- package/src/permissions.ts +49 -0
- package/src/plugins.test.ts +64 -0
- package/src/plugins.ts +56 -0
- package/src/realtime/changeStreamWatcher.ts +552 -114
- package/src/realtime/realtime.test.ts +157 -0
- package/src/realtime/realtimeApp.ts +82 -14
- package/src/realtime/realtimeAppSetup.test.ts +25 -27
- package/src/realtime/sessionRevalidation.test.ts +423 -0
- package/src/realtime/sessionRevalidation.ts +273 -0
- package/src/realtime/socketAuth.test.ts +355 -0
- package/src/realtime/socketAuth.ts +228 -0
- package/src/realtime/socketUser.ts +69 -0
- package/src/realtime/types.ts +54 -0
- package/src/secretProviders.test.ts +7 -1
- package/src/secretProviders.ts +15 -3
- package/src/secretProvidersGcpClient.test.ts +66 -0
- package/src/sync/ensureSyncIndexes.test.ts +144 -0
- package/src/sync/executors.test.ts +882 -0
- package/src/sync/executors.ts +705 -0
- package/src/sync/integration.test.ts +1160 -0
- package/src/sync/loadHarness.ts +618 -0
- package/src/sync/models.ts +641 -0
- package/src/sync/mutationHandler.test.ts +1252 -0
- package/src/sync/mutationHandler.ts +850 -0
- package/src/sync/registry.ts +232 -0
- package/src/sync/routes.ts +812 -0
- package/src/sync/scripts/compactTombstones.test.ts +249 -0
- package/src/sync/scripts/compactTombstones.ts +145 -0
- package/src/sync/serialize.ts +48 -0
- package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
- package/src/sync/socketHandlers.ts +320 -0
- package/src/sync/streams.ts +188 -0
- package/src/sync/sync.test.ts +582 -0
- package/src/sync/syncApp.ts +44 -0
- package/src/sync/syncBatch.ts +219 -0
- package/src/sync/syncFrontier.test.ts +494 -0
- package/src/sync/syncPhaseC.test.ts +718 -0
- package/src/sync/syncRoutes.test.ts +865 -0
- package/src/sync/syncSeqPlugin.ts +730 -0
- package/src/sync/syncSocket.test.ts +1440 -0
- package/src/sync/types.ts +270 -0
- package/src/terrenoApp.ts +37 -17
- package/src/tests/bunSetup.ts +8 -0
- package/tsconfig.json +4 -4
- package/tsconfig.typedoc.json +7 -1
- package/dist/__tests__/openApiCompat.test.js +0 -317
- package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
- package/dist/__tests__/versionCheckPlugin.test.js +0 -597
- package/dist/actions.openApi.test.d.ts +0 -1
- package/dist/actions.openApi.test.js +0 -253
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -1087
- package/dist/api.arrayOperations.test.d.ts +0 -1
- package/dist/api.arrayOperations.test.js +0 -878
- package/dist/api.asyncHandler.test.d.ts +0 -1
- package/dist/api.asyncHandler.test.js +0 -236
- package/dist/api.coverage.test.d.ts +0 -1
- package/dist/api.coverage.test.js +0 -192
- package/dist/api.errorPaths.test.d.ts +0 -1
- package/dist/api.errorPaths.test.js +0 -887
- package/dist/api.errors.test.d.ts +0 -1
- package/dist/api.errors.test.js +0 -185
- package/dist/api.hooks.test.d.ts +0 -1
- package/dist/api.hooks.test.js +0 -950
- package/dist/api.query.test.d.ts +0 -1
- package/dist/api.query.test.js +0 -948
- package/dist/api.test.d.ts +0 -1
- package/dist/api.test.js +0 -2820
- package/dist/auth.compile.test.d.ts +0 -1
- package/dist/auth.compile.test.js +0 -201
- package/dist/auth.coverage.test.d.ts +0 -1
- package/dist/auth.coverage.test.js +0 -202
- package/dist/auth.test.d.ts +0 -1
- package/dist/auth.test.js +0 -2286
- package/dist/betterAuth.test.d.ts +0 -1
- package/dist/betterAuth.test.js +0 -181
- package/dist/betterAuthApp.test.d.ts +0 -1
- package/dist/betterAuthApp.test.js +0 -242
- package/dist/betterAuthSetup.test.d.ts +0 -1
- package/dist/betterAuthSetup.test.js +0 -762
- package/dist/config.test.d.ts +0 -1
- package/dist/config.test.js +0 -353
- package/dist/configuration.test.d.ts +0 -1
- package/dist/configuration.test.js +0 -980
- package/dist/configurationPlugin.test.d.ts +0 -1
- package/dist/configurationPlugin.test.js +0 -827
- package/dist/consentApp.test.d.ts +0 -1
- package/dist/consentApp.test.js +0 -1411
- package/dist/docLoader.test.d.ts +0 -1
- package/dist/docLoader.test.js +0 -137
- package/dist/envConfigurationPlugin.test.d.ts +0 -1
- package/dist/envConfigurationPlugin.test.js +0 -369
- package/dist/errors.test.d.ts +0 -1
- package/dist/errors.test.js +0 -607
- package/dist/expressServer.test.d.ts +0 -1
- package/dist/expressServer.test.js +0 -1249
- package/dist/githubAuth.test.d.ts +0 -1
- package/dist/githubAuth.test.js +0 -1142
- package/dist/httpClient.test.d.ts +0 -1
- package/dist/httpClient.test.js +0 -1471
- package/dist/logger.test.d.ts +0 -1
- package/dist/logger.test.js +0 -685
- package/dist/middleware.test.d.ts +0 -1
- package/dist/middleware.test.js +0 -241
- package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
- package/dist/notifiers/googleChatNotifier.test.js +0 -298
- package/dist/notifiers/slackNotifier.test.d.ts +0 -1
- package/dist/notifiers/slackNotifier.test.js +0 -297
- package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
- package/dist/notifiers/zoomNotifier.test.js +0 -373
- package/dist/openApi.test.d.ts +0 -1
- package/dist/openApi.test.js +0 -620
- package/dist/openApiBuilder.test.d.ts +0 -1
- package/dist/openApiBuilder.test.js +0 -599
- package/dist/openApiEtag.test.d.ts +0 -1
- package/dist/openApiEtag.test.js +0 -86
- package/dist/openApiValidator.test.d.ts +0 -1
- package/dist/openApiValidator.test.js +0 -795
- package/dist/permissions.middleware.test.d.ts +0 -1
- package/dist/permissions.middleware.test.js +0 -308
- package/dist/permissions.test.d.ts +0 -1
- package/dist/permissions.test.js +0 -439
- package/dist/plugins.test.d.ts +0 -1
- package/dist/plugins.test.js +0 -902
- package/dist/populate.test.d.ts +0 -1
- package/dist/populate.test.js +0 -370
- package/dist/realtime/realtime.test.d.ts +0 -10
- package/dist/realtime/realtime.test.js +0 -4213
- package/dist/realtime/realtimeAppSetup.test.d.ts +0 -1
- package/dist/realtime/realtimeAppSetup.test.js +0 -213
- package/dist/requestContext.test.d.ts +0 -1
- package/dist/requestContext.test.js +0 -412
- package/dist/scriptRunner.test.d.ts +0 -1
- package/dist/scriptRunner.test.js +0 -386
- package/dist/secretProviders.test.d.ts +0 -1
- package/dist/secretProviders.test.js +0 -726
- package/dist/syncConsents.test.d.ts +0 -1
- package/dist/syncConsents.test.js +0 -522
- package/dist/terrenoApp.test.d.ts +0 -1
- package/dist/terrenoApp.test.js +0 -489
- package/dist/tests/bunSetup.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.js +0 -37
- package/dist/tests/testEnv.d.ts +0 -0
- package/dist/tests/testEnv.js +0 -6
- package/dist/transformers.test.d.ts +0 -1
- package/dist/transformers.test.js +0 -423
- package/dist/utils.test.d.ts +0 -1
- package/dist/utils.test.js +0 -242
- /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// noExplicitAny: model router options are generic across all models
|
|
2
|
+
// biome-ignore-all lint/suspicious/noExplicitAny: model router options are generic across all models
|
|
3
|
+
import type {Model} from "mongoose";
|
|
4
|
+
import type {ModelRouterOptions} from "../api";
|
|
5
|
+
import {logger} from "../logger";
|
|
6
|
+
import {getScopeField} from "./streams";
|
|
7
|
+
import type {SyncConfig} from "./types";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A registered model with SyncDB local-first sync configuration.
|
|
11
|
+
*/
|
|
12
|
+
export interface SyncRegistryEntry {
|
|
13
|
+
/** Mongoose model name (e.g. "Todo") */
|
|
14
|
+
modelName: string;
|
|
15
|
+
/** Route path (e.g. "/todos") */
|
|
16
|
+
routePath: string;
|
|
17
|
+
/** Collection tag used in the sync protocol (route path without the leading slash) */
|
|
18
|
+
collectionTag: string;
|
|
19
|
+
/** MongoDB collection name (e.g. "todos") */
|
|
20
|
+
collectionName: string;
|
|
21
|
+
/** Sync configuration from modelRouter options */
|
|
22
|
+
config: SyncConfig;
|
|
23
|
+
/** Full modelRouter options (for responseHandler, permissions, etc.) */
|
|
24
|
+
options: ModelRouterOptions<any>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const syncRegistry: SyncRegistryEntry[] = [];
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* C8: index creation is kicked off (fire-and-forget) at registration, but its failure must
|
|
31
|
+
* be a STARTUP error, not a swallowed warning (a missing index table-scans the snapshot
|
|
32
|
+
* query under load, and a missing unique index breaks mutation idempotency). Each
|
|
33
|
+
* registration records its index promise here; `ensureSyncIndexes()` awaits them and
|
|
34
|
+
* throws on any failure so server startup can fail loudly. A detached failure is also
|
|
35
|
+
* logged so it is never silent even if startup never calls `ensureSyncIndexes`.
|
|
36
|
+
*/
|
|
37
|
+
const indexCreationPromises: Promise<void>[] = [];
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Enqueue an index-creation promise for `ensureSyncIndexes()` to await at startup. Used by
|
|
41
|
+
* `SyncApp.register` for the bookkeeping-model indexes (`SyncCounter`, `SyncMutation`,
|
|
42
|
+
* `SyncScopeMove`, `SyncKey`), which are correctness-critical and must not depend on
|
|
43
|
+
* Mongoose `autoIndex` being enabled.
|
|
44
|
+
*/
|
|
45
|
+
export const trackSyncIndexCreation = (promise: Promise<void>): void => {
|
|
46
|
+
indexCreationPromises.push(promise);
|
|
47
|
+
// Swallow the detached rejection (the failure is logged where it happens and rethrown
|
|
48
|
+
// by `ensureSyncIndexes()` via the retained promise above).
|
|
49
|
+
promise.catch(() => {});
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Register a model for local-first sync. Called automatically by modelRouter when the
|
|
54
|
+
* `sync` option is provided. Validates the schema contract at startup and throws with
|
|
55
|
+
* an actionable message when it is not met:
|
|
56
|
+
* - soft delete (`isDeletedPlugin`) is required so deletes remain queryable tombstones;
|
|
57
|
+
* - `syncPlugin` is required so every write stamps a per-stream `_syncSeq`;
|
|
58
|
+
* - owner/tenant scope fields must exist on the schema.
|
|
59
|
+
*/
|
|
60
|
+
export const registerSync = ({
|
|
61
|
+
model,
|
|
62
|
+
routePath,
|
|
63
|
+
config,
|
|
64
|
+
options,
|
|
65
|
+
}: {
|
|
66
|
+
model: Model<any>;
|
|
67
|
+
routePath: string;
|
|
68
|
+
config: SyncConfig;
|
|
69
|
+
options: ModelRouterOptions<any>;
|
|
70
|
+
}): void => {
|
|
71
|
+
const name = model.modelName;
|
|
72
|
+
const deletedPath = model.schema.path("deleted");
|
|
73
|
+
if (deletedPath?.instance !== "Boolean") {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`Model ${name} has a sync config but no soft delete support. ` +
|
|
76
|
+
"Apply isDeletedPlugin to the schema — sync catch-up requires delete tombstones."
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
if (!model.schema.path("_syncSeq")) {
|
|
80
|
+
throw new Error(
|
|
81
|
+
`Model ${name} has a sync config but syncPlugin is not applied to its schema. ` +
|
|
82
|
+
"Apply syncPlugin so every write stamps a per-stream _syncSeq."
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
const scopeField = getScopeField(config.scope);
|
|
86
|
+
if (scopeField && !model.schema.path(scopeField)) {
|
|
87
|
+
throw new Error(
|
|
88
|
+
`Model ${name} has a sync scope on field "${scopeField}" but the schema has no such path.`
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
if (typeof config.scope === "function" && !config.snapshotFilter) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
`Model ${name} uses a custom sync scope resolver, which requires a snapshotFilter ` +
|
|
94
|
+
"so the snapshot endpoint can restrict queries to the caller's documents."
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
if (syncRegistry.some((entry) => entry.modelName === name)) {
|
|
98
|
+
throw new Error(`Model ${name} is already registered for sync.`);
|
|
99
|
+
}
|
|
100
|
+
// C8: a duplicate collectionTag would make two models share sync streams and route
|
|
101
|
+
// snapshots/deltas ambiguously — reject it loudly at registration.
|
|
102
|
+
const collectionTag = routePath.replace(/^\//, "");
|
|
103
|
+
if (syncRegistry.some((entry) => entry.collectionTag === collectionTag)) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
`Sync collection tag "${collectionTag}" is already registered (routePath ${routePath}). ` +
|
|
106
|
+
"Each synced model must have a unique route path."
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
syncRegistry.push({
|
|
111
|
+
collectionName: model.collection.collectionName,
|
|
112
|
+
collectionTag,
|
|
113
|
+
config,
|
|
114
|
+
modelName: name,
|
|
115
|
+
options,
|
|
116
|
+
routePath,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// `Model.bulkWrite` bypasses Mongoose middleware entirely, so `syncPlugin`'s query
|
|
120
|
+
// guards never see it: writes land with no `_syncSeq`, invisible to both delta emission
|
|
121
|
+
// and snapshot catch-up, and clients silently never learn about them. The restriction
|
|
122
|
+
// used to be documentation only — replace the static so it throws the same way the
|
|
123
|
+
// guarded multi-document paths do. Idempotent: the replacement never delegates, so a
|
|
124
|
+
// re-registration (tests clearing the registry) re-patching it is harmless.
|
|
125
|
+
(model as unknown as {bulkWrite: () => never}).bulkWrite = (): never => {
|
|
126
|
+
throw new Error(
|
|
127
|
+
`bulkWrite is not supported on sync-enabled model ${name}: it bypasses Mongoose ` +
|
|
128
|
+
"middleware, so writes are never stamped with a per-stream _syncSeq and become " +
|
|
129
|
+
"invisible to sync delta emission and snapshot catch-up. Loop per document instead."
|
|
130
|
+
);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// Compound index for snapshot/catch-up queries: {scopeField, _syncSeq}. Created
|
|
134
|
+
// directly on the collection because the model is already compiled at registration.
|
|
135
|
+
// C8: track the promise so `ensureSyncIndexes()` (server startup) can fail loudly on a
|
|
136
|
+
// createIndex error — a missing index table-scans the snapshot query under load. The
|
|
137
|
+
// detached path only logs (never throws into an orphaned promise).
|
|
138
|
+
const indexSpec: Record<string, 1> = scopeField ? {[scopeField]: 1, _syncSeq: 1} : {_syncSeq: 1};
|
|
139
|
+
const indexPromise = model.collection
|
|
140
|
+
.createIndex(indexSpec)
|
|
141
|
+
.then(() => {})
|
|
142
|
+
.catch((error: unknown) => {
|
|
143
|
+
logger.error(`[sync] Failed to create sync index for ${name}`, {error: String(error)});
|
|
144
|
+
throw new Error(
|
|
145
|
+
`Failed to create sync snapshot index for ${name}: ${String(error)}. ` +
|
|
146
|
+
"The snapshot/catch-up query requires this index; fix the schema/DB and restart."
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
indexCreationPromises.push(indexPromise);
|
|
150
|
+
// Swallow the detached rejection (already logged) so it is not an unhandled rejection;
|
|
151
|
+
// `ensureSyncIndexes()` still observes it via the retained promise above.
|
|
152
|
+
indexPromise.catch(() => {});
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* C8: await every enqueued sync index creation — per-model snapshot indexes from
|
|
157
|
+
* `registerSync` plus the bookkeeping-model indexes from `SyncApp.register` — throwing on
|
|
158
|
+
* the first failure. Called at server startup by `TerrenoApp.start()` (after all models
|
|
159
|
+
* and plugins register) so a missing index fails the boot loudly rather than silently
|
|
160
|
+
* degrading the snapshot query to a table scan or breaking mutation idempotency.
|
|
161
|
+
*/
|
|
162
|
+
export const ensureSyncIndexes = async (): Promise<void> => {
|
|
163
|
+
await Promise.all(indexCreationPromises);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/** Get all registered sync models. */
|
|
167
|
+
export const getSyncRegistry = (): SyncRegistryEntry[] => syncRegistry;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Collection tags whose scope can only be resolved from the FULL user document —
|
|
171
|
+
* tenant scopes read `organizationIds` (or whatever `getUserScopes` looks at) and custom
|
|
172
|
+
* resolvers may read anything. The synthetic `{_id, admin, id}` user built from JWT claims
|
|
173
|
+
* carries none of that.
|
|
174
|
+
*/
|
|
175
|
+
const scopesRequiringFullUser = (): string[] =>
|
|
176
|
+
syncRegistry
|
|
177
|
+
.filter(
|
|
178
|
+
(entry) => typeof entry.config.scope === "function" || entry.config.scope.type === "tenant"
|
|
179
|
+
)
|
|
180
|
+
.map((entry) => entry.collectionTag);
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Task 9.21: warn loudly at startup when a tenant/custom-scoped collection is registered
|
|
184
|
+
* but no `userModel` is configured for socket handshakes.
|
|
185
|
+
*
|
|
186
|
+
* Without one, `getSocketUser` falls back to the synthetic decoded-token user: `admin`
|
|
187
|
+
* comes from a JWT claim rather than the database, and `getUserScopes` sees no
|
|
188
|
+
* `organizationIds`, so tenant subscriptions silently resolve to no streams (a client that
|
|
189
|
+
* appears connected but never receives data). This warns rather than throws so an existing
|
|
190
|
+
* deployment cannot be bricked by an upgrade; the message names the collections and the
|
|
191
|
+
* fix. Returns the offending collection tags for tests and callers that want to escalate.
|
|
192
|
+
*/
|
|
193
|
+
export const warnOnSyncScopesWithoutUserModel = ({userModel}: {userModel?: unknown}): string[] => {
|
|
194
|
+
if (userModel) {
|
|
195
|
+
return [];
|
|
196
|
+
}
|
|
197
|
+
const collections = scopesRequiringFullUser();
|
|
198
|
+
if (collections.length === 0) {
|
|
199
|
+
return [];
|
|
200
|
+
}
|
|
201
|
+
logger.error(
|
|
202
|
+
"[sync] Tenant/custom-scoped sync collections are registered but RealtimeApp has no " +
|
|
203
|
+
"userModel: socket authorization will fall back to the synthetic JWT-claim user, which " +
|
|
204
|
+
"carries no membership fields, so tenant streams resolve to nothing and `admin` is " +
|
|
205
|
+
"trusted from the token instead of the database. Pass `userModel` in RealtimeAppOptions " +
|
|
206
|
+
"(TerrenoApp does this automatically for its own userModel).",
|
|
207
|
+
{collections}
|
|
208
|
+
);
|
|
209
|
+
return collections;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
/** Find a sync registry entry by Mongoose model name. */
|
|
213
|
+
export const findSyncEntryByModelName = (modelName: string): SyncRegistryEntry | undefined =>
|
|
214
|
+
syncRegistry.find((entry) => entry.modelName === modelName);
|
|
215
|
+
|
|
216
|
+
/** Find a sync registry entry by collection tag (e.g. "todos"). */
|
|
217
|
+
export const findSyncEntryByCollectionTag = (
|
|
218
|
+
collectionTag: string
|
|
219
|
+
): SyncRegistryEntry | undefined =>
|
|
220
|
+
syncRegistry.find((entry) => entry.collectionTag === collectionTag);
|
|
221
|
+
|
|
222
|
+
/** Find a sync registry entry by MongoDB collection name. */
|
|
223
|
+
export const findSyncEntryByCollectionName = (
|
|
224
|
+
collectionName: string
|
|
225
|
+
): SyncRegistryEntry | undefined =>
|
|
226
|
+
syncRegistry.find((entry) => entry.collectionName === collectionName);
|
|
227
|
+
|
|
228
|
+
/** Clear the registry (for testing). */
|
|
229
|
+
export const clearSyncRegistry = (): void => {
|
|
230
|
+
syncRegistry.length = 0;
|
|
231
|
+
indexCreationPromises.length = 0;
|
|
232
|
+
};
|