@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,73 @@
|
|
|
1
|
+
import type { Model } from "mongoose";
|
|
2
|
+
import type { ModelRouterOptions } from "../api";
|
|
3
|
+
import type { SyncConfig } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* A registered model with SyncDB local-first sync configuration.
|
|
6
|
+
*/
|
|
7
|
+
export interface SyncRegistryEntry {
|
|
8
|
+
/** Mongoose model name (e.g. "Todo") */
|
|
9
|
+
modelName: string;
|
|
10
|
+
/** Route path (e.g. "/todos") */
|
|
11
|
+
routePath: string;
|
|
12
|
+
/** Collection tag used in the sync protocol (route path without the leading slash) */
|
|
13
|
+
collectionTag: string;
|
|
14
|
+
/** MongoDB collection name (e.g. "todos") */
|
|
15
|
+
collectionName: string;
|
|
16
|
+
/** Sync configuration from modelRouter options */
|
|
17
|
+
config: SyncConfig;
|
|
18
|
+
/** Full modelRouter options (for responseHandler, permissions, etc.) */
|
|
19
|
+
options: ModelRouterOptions<any>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Enqueue an index-creation promise for `ensureSyncIndexes()` to await at startup. Used by
|
|
23
|
+
* `SyncApp.register` for the bookkeeping-model indexes (`SyncCounter`, `SyncMutation`,
|
|
24
|
+
* `SyncScopeMove`, `SyncKey`), which are correctness-critical and must not depend on
|
|
25
|
+
* Mongoose `autoIndex` being enabled.
|
|
26
|
+
*/
|
|
27
|
+
export declare const trackSyncIndexCreation: (promise: Promise<void>) => void;
|
|
28
|
+
/**
|
|
29
|
+
* Register a model for local-first sync. Called automatically by modelRouter when the
|
|
30
|
+
* `sync` option is provided. Validates the schema contract at startup and throws with
|
|
31
|
+
* an actionable message when it is not met:
|
|
32
|
+
* - soft delete (`isDeletedPlugin`) is required so deletes remain queryable tombstones;
|
|
33
|
+
* - `syncPlugin` is required so every write stamps a per-stream `_syncSeq`;
|
|
34
|
+
* - owner/tenant scope fields must exist on the schema.
|
|
35
|
+
*/
|
|
36
|
+
export declare const registerSync: ({ model, routePath, config, options, }: {
|
|
37
|
+
model: Model<any>;
|
|
38
|
+
routePath: string;
|
|
39
|
+
config: SyncConfig;
|
|
40
|
+
options: ModelRouterOptions<any>;
|
|
41
|
+
}) => void;
|
|
42
|
+
/**
|
|
43
|
+
* C8: await every enqueued sync index creation — per-model snapshot indexes from
|
|
44
|
+
* `registerSync` plus the bookkeeping-model indexes from `SyncApp.register` — throwing on
|
|
45
|
+
* the first failure. Called at server startup by `TerrenoApp.start()` (after all models
|
|
46
|
+
* and plugins register) so a missing index fails the boot loudly rather than silently
|
|
47
|
+
* degrading the snapshot query to a table scan or breaking mutation idempotency.
|
|
48
|
+
*/
|
|
49
|
+
export declare const ensureSyncIndexes: () => Promise<void>;
|
|
50
|
+
/** Get all registered sync models. */
|
|
51
|
+
export declare const getSyncRegistry: () => SyncRegistryEntry[];
|
|
52
|
+
/**
|
|
53
|
+
* Task 9.21: warn loudly at startup when a tenant/custom-scoped collection is registered
|
|
54
|
+
* but no `userModel` is configured for socket handshakes.
|
|
55
|
+
*
|
|
56
|
+
* Without one, `getSocketUser` falls back to the synthetic decoded-token user: `admin`
|
|
57
|
+
* comes from a JWT claim rather than the database, and `getUserScopes` sees no
|
|
58
|
+
* `organizationIds`, so tenant subscriptions silently resolve to no streams (a client that
|
|
59
|
+
* appears connected but never receives data). This warns rather than throws so an existing
|
|
60
|
+
* deployment cannot be bricked by an upgrade; the message names the collections and the
|
|
61
|
+
* fix. Returns the offending collection tags for tests and callers that want to escalate.
|
|
62
|
+
*/
|
|
63
|
+
export declare const warnOnSyncScopesWithoutUserModel: ({ userModel }: {
|
|
64
|
+
userModel?: unknown;
|
|
65
|
+
}) => string[];
|
|
66
|
+
/** Find a sync registry entry by Mongoose model name. */
|
|
67
|
+
export declare const findSyncEntryByModelName: (modelName: string) => SyncRegistryEntry | undefined;
|
|
68
|
+
/** Find a sync registry entry by collection tag (e.g. "todos"). */
|
|
69
|
+
export declare const findSyncEntryByCollectionTag: (collectionTag: string) => SyncRegistryEntry | undefined;
|
|
70
|
+
/** Find a sync registry entry by MongoDB collection name. */
|
|
71
|
+
export declare const findSyncEntryByCollectionName: (collectionName: string) => SyncRegistryEntry | undefined;
|
|
72
|
+
/** Clear the registry (for testing). */
|
|
73
|
+
export declare const clearSyncRegistry: () => void;
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.clearSyncRegistry = exports.findSyncEntryByCollectionName = exports.findSyncEntryByCollectionTag = exports.findSyncEntryByModelName = exports.warnOnSyncScopesWithoutUserModel = exports.getSyncRegistry = exports.ensureSyncIndexes = exports.registerSync = exports.trackSyncIndexCreation = void 0;
|
|
40
|
+
var logger_1 = require("../logger");
|
|
41
|
+
var streams_1 = require("./streams");
|
|
42
|
+
var syncRegistry = [];
|
|
43
|
+
/**
|
|
44
|
+
* C8: index creation is kicked off (fire-and-forget) at registration, but its failure must
|
|
45
|
+
* be a STARTUP error, not a swallowed warning (a missing index table-scans the snapshot
|
|
46
|
+
* query under load, and a missing unique index breaks mutation idempotency). Each
|
|
47
|
+
* registration records its index promise here; `ensureSyncIndexes()` awaits them and
|
|
48
|
+
* throws on any failure so server startup can fail loudly. A detached failure is also
|
|
49
|
+
* logged so it is never silent even if startup never calls `ensureSyncIndexes`.
|
|
50
|
+
*/
|
|
51
|
+
var indexCreationPromises = [];
|
|
52
|
+
/**
|
|
53
|
+
* Enqueue an index-creation promise for `ensureSyncIndexes()` to await at startup. Used by
|
|
54
|
+
* `SyncApp.register` for the bookkeeping-model indexes (`SyncCounter`, `SyncMutation`,
|
|
55
|
+
* `SyncScopeMove`, `SyncKey`), which are correctness-critical and must not depend on
|
|
56
|
+
* Mongoose `autoIndex` being enabled.
|
|
57
|
+
*/
|
|
58
|
+
var trackSyncIndexCreation = function (promise) {
|
|
59
|
+
indexCreationPromises.push(promise);
|
|
60
|
+
// Swallow the detached rejection (the failure is logged where it happens and rethrown
|
|
61
|
+
// by `ensureSyncIndexes()` via the retained promise above).
|
|
62
|
+
promise.catch(function () { });
|
|
63
|
+
};
|
|
64
|
+
exports.trackSyncIndexCreation = trackSyncIndexCreation;
|
|
65
|
+
/**
|
|
66
|
+
* Register a model for local-first sync. Called automatically by modelRouter when the
|
|
67
|
+
* `sync` option is provided. Validates the schema contract at startup and throws with
|
|
68
|
+
* an actionable message when it is not met:
|
|
69
|
+
* - soft delete (`isDeletedPlugin`) is required so deletes remain queryable tombstones;
|
|
70
|
+
* - `syncPlugin` is required so every write stamps a per-stream `_syncSeq`;
|
|
71
|
+
* - owner/tenant scope fields must exist on the schema.
|
|
72
|
+
*/
|
|
73
|
+
var registerSync = function (_a) {
|
|
74
|
+
var _b;
|
|
75
|
+
var model = _a.model, routePath = _a.routePath, config = _a.config, options = _a.options;
|
|
76
|
+
var name = model.modelName;
|
|
77
|
+
var deletedPath = model.schema.path("deleted");
|
|
78
|
+
if ((deletedPath === null || deletedPath === void 0 ? void 0 : deletedPath.instance) !== "Boolean") {
|
|
79
|
+
throw new Error("Model ".concat(name, " has a sync config but no soft delete support. ") +
|
|
80
|
+
"Apply isDeletedPlugin to the schema — sync catch-up requires delete tombstones.");
|
|
81
|
+
}
|
|
82
|
+
if (!model.schema.path("_syncSeq")) {
|
|
83
|
+
throw new Error("Model ".concat(name, " has a sync config but syncPlugin is not applied to its schema. ") +
|
|
84
|
+
"Apply syncPlugin so every write stamps a per-stream _syncSeq.");
|
|
85
|
+
}
|
|
86
|
+
var scopeField = (0, streams_1.getScopeField)(config.scope);
|
|
87
|
+
if (scopeField && !model.schema.path(scopeField)) {
|
|
88
|
+
throw new Error("Model ".concat(name, " has a sync scope on field \"").concat(scopeField, "\" but the schema has no such path."));
|
|
89
|
+
}
|
|
90
|
+
if (typeof config.scope === "function" && !config.snapshotFilter) {
|
|
91
|
+
throw new Error("Model ".concat(name, " uses a custom sync scope resolver, which requires a snapshotFilter ") +
|
|
92
|
+
"so the snapshot endpoint can restrict queries to the caller's documents.");
|
|
93
|
+
}
|
|
94
|
+
if (syncRegistry.some(function (entry) { return entry.modelName === name; })) {
|
|
95
|
+
throw new Error("Model ".concat(name, " is already registered for sync."));
|
|
96
|
+
}
|
|
97
|
+
// C8: a duplicate collectionTag would make two models share sync streams and route
|
|
98
|
+
// snapshots/deltas ambiguously — reject it loudly at registration.
|
|
99
|
+
var collectionTag = routePath.replace(/^\//, "");
|
|
100
|
+
if (syncRegistry.some(function (entry) { return entry.collectionTag === collectionTag; })) {
|
|
101
|
+
throw new Error("Sync collection tag \"".concat(collectionTag, "\" is already registered (routePath ").concat(routePath, "). ") +
|
|
102
|
+
"Each synced model must have a unique route path.");
|
|
103
|
+
}
|
|
104
|
+
syncRegistry.push({
|
|
105
|
+
collectionName: model.collection.collectionName,
|
|
106
|
+
collectionTag: collectionTag,
|
|
107
|
+
config: config,
|
|
108
|
+
modelName: name,
|
|
109
|
+
options: options,
|
|
110
|
+
routePath: routePath,
|
|
111
|
+
});
|
|
112
|
+
// `Model.bulkWrite` bypasses Mongoose middleware entirely, so `syncPlugin`'s query
|
|
113
|
+
// guards never see it: writes land with no `_syncSeq`, invisible to both delta emission
|
|
114
|
+
// and snapshot catch-up, and clients silently never learn about them. The restriction
|
|
115
|
+
// used to be documentation only — replace the static so it throws the same way the
|
|
116
|
+
// guarded multi-document paths do. Idempotent: the replacement never delegates, so a
|
|
117
|
+
// re-registration (tests clearing the registry) re-patching it is harmless.
|
|
118
|
+
model.bulkWrite = function () {
|
|
119
|
+
throw new Error("bulkWrite is not supported on sync-enabled model ".concat(name, ": it bypasses Mongoose ") +
|
|
120
|
+
"middleware, so writes are never stamped with a per-stream _syncSeq and become " +
|
|
121
|
+
"invisible to sync delta emission and snapshot catch-up. Loop per document instead.");
|
|
122
|
+
};
|
|
123
|
+
// Compound index for snapshot/catch-up queries: {scopeField, _syncSeq}. Created
|
|
124
|
+
// directly on the collection because the model is already compiled at registration.
|
|
125
|
+
// C8: track the promise so `ensureSyncIndexes()` (server startup) can fail loudly on a
|
|
126
|
+
// createIndex error — a missing index table-scans the snapshot query under load. The
|
|
127
|
+
// detached path only logs (never throws into an orphaned promise).
|
|
128
|
+
var indexSpec = scopeField ? (_b = {}, _b[scopeField] = 1, _b._syncSeq = 1, _b) : { _syncSeq: 1 };
|
|
129
|
+
var indexPromise = model.collection
|
|
130
|
+
.createIndex(indexSpec)
|
|
131
|
+
.then(function () { })
|
|
132
|
+
.catch(function (error) {
|
|
133
|
+
logger_1.logger.error("[sync] Failed to create sync index for ".concat(name), { error: String(error) });
|
|
134
|
+
throw new Error("Failed to create sync snapshot index for ".concat(name, ": ").concat(String(error), ". ") +
|
|
135
|
+
"The snapshot/catch-up query requires this index; fix the schema/DB and restart.");
|
|
136
|
+
});
|
|
137
|
+
indexCreationPromises.push(indexPromise);
|
|
138
|
+
// Swallow the detached rejection (already logged) so it is not an unhandled rejection;
|
|
139
|
+
// `ensureSyncIndexes()` still observes it via the retained promise above.
|
|
140
|
+
indexPromise.catch(function () { });
|
|
141
|
+
};
|
|
142
|
+
exports.registerSync = registerSync;
|
|
143
|
+
/**
|
|
144
|
+
* C8: await every enqueued sync index creation — per-model snapshot indexes from
|
|
145
|
+
* `registerSync` plus the bookkeeping-model indexes from `SyncApp.register` — throwing on
|
|
146
|
+
* the first failure. Called at server startup by `TerrenoApp.start()` (after all models
|
|
147
|
+
* and plugins register) so a missing index fails the boot loudly rather than silently
|
|
148
|
+
* degrading the snapshot query to a table scan or breaking mutation idempotency.
|
|
149
|
+
*/
|
|
150
|
+
var ensureSyncIndexes = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
151
|
+
return __generator(this, function (_a) {
|
|
152
|
+
switch (_a.label) {
|
|
153
|
+
case 0: return [4 /*yield*/, Promise.all(indexCreationPromises)];
|
|
154
|
+
case 1:
|
|
155
|
+
_a.sent();
|
|
156
|
+
return [2 /*return*/];
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}); };
|
|
160
|
+
exports.ensureSyncIndexes = ensureSyncIndexes;
|
|
161
|
+
/** Get all registered sync models. */
|
|
162
|
+
var getSyncRegistry = function () { return syncRegistry; };
|
|
163
|
+
exports.getSyncRegistry = getSyncRegistry;
|
|
164
|
+
/**
|
|
165
|
+
* Collection tags whose scope can only be resolved from the FULL user document —
|
|
166
|
+
* tenant scopes read `organizationIds` (or whatever `getUserScopes` looks at) and custom
|
|
167
|
+
* resolvers may read anything. The synthetic `{_id, admin, id}` user built from JWT claims
|
|
168
|
+
* carries none of that.
|
|
169
|
+
*/
|
|
170
|
+
var scopesRequiringFullUser = function () {
|
|
171
|
+
return syncRegistry
|
|
172
|
+
.filter(function (entry) { return typeof entry.config.scope === "function" || entry.config.scope.type === "tenant"; })
|
|
173
|
+
.map(function (entry) { return entry.collectionTag; });
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* Task 9.21: warn loudly at startup when a tenant/custom-scoped collection is registered
|
|
177
|
+
* but no `userModel` is configured for socket handshakes.
|
|
178
|
+
*
|
|
179
|
+
* Without one, `getSocketUser` falls back to the synthetic decoded-token user: `admin`
|
|
180
|
+
* comes from a JWT claim rather than the database, and `getUserScopes` sees no
|
|
181
|
+
* `organizationIds`, so tenant subscriptions silently resolve to no streams (a client that
|
|
182
|
+
* appears connected but never receives data). This warns rather than throws so an existing
|
|
183
|
+
* deployment cannot be bricked by an upgrade; the message names the collections and the
|
|
184
|
+
* fix. Returns the offending collection tags for tests and callers that want to escalate.
|
|
185
|
+
*/
|
|
186
|
+
var warnOnSyncScopesWithoutUserModel = function (_a) {
|
|
187
|
+
var userModel = _a.userModel;
|
|
188
|
+
if (userModel) {
|
|
189
|
+
return [];
|
|
190
|
+
}
|
|
191
|
+
var collections = scopesRequiringFullUser();
|
|
192
|
+
if (collections.length === 0) {
|
|
193
|
+
return [];
|
|
194
|
+
}
|
|
195
|
+
logger_1.logger.error("[sync] Tenant/custom-scoped sync collections are registered but RealtimeApp has no " +
|
|
196
|
+
"userModel: socket authorization will fall back to the synthetic JWT-claim user, which " +
|
|
197
|
+
"carries no membership fields, so tenant streams resolve to nothing and `admin` is " +
|
|
198
|
+
"trusted from the token instead of the database. Pass `userModel` in RealtimeAppOptions " +
|
|
199
|
+
"(TerrenoApp does this automatically for its own userModel).", { collections: collections });
|
|
200
|
+
return collections;
|
|
201
|
+
};
|
|
202
|
+
exports.warnOnSyncScopesWithoutUserModel = warnOnSyncScopesWithoutUserModel;
|
|
203
|
+
/** Find a sync registry entry by Mongoose model name. */
|
|
204
|
+
var findSyncEntryByModelName = function (modelName) {
|
|
205
|
+
return syncRegistry.find(function (entry) { return entry.modelName === modelName; });
|
|
206
|
+
};
|
|
207
|
+
exports.findSyncEntryByModelName = findSyncEntryByModelName;
|
|
208
|
+
/** Find a sync registry entry by collection tag (e.g. "todos"). */
|
|
209
|
+
var findSyncEntryByCollectionTag = function (collectionTag) {
|
|
210
|
+
return syncRegistry.find(function (entry) { return entry.collectionTag === collectionTag; });
|
|
211
|
+
};
|
|
212
|
+
exports.findSyncEntryByCollectionTag = findSyncEntryByCollectionTag;
|
|
213
|
+
/** Find a sync registry entry by MongoDB collection name. */
|
|
214
|
+
var findSyncEntryByCollectionName = function (collectionName) {
|
|
215
|
+
return syncRegistry.find(function (entry) { return entry.collectionName === collectionName; });
|
|
216
|
+
};
|
|
217
|
+
exports.findSyncEntryByCollectionName = findSyncEntryByCollectionName;
|
|
218
|
+
/** Clear the registry (for testing). */
|
|
219
|
+
var clearSyncRegistry = function () {
|
|
220
|
+
syncRegistry.length = 0;
|
|
221
|
+
indexCreationPromises.length = 0;
|
|
222
|
+
};
|
|
223
|
+
exports.clearSyncRegistry = clearSyncRegistry;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
import { type User } from "../auth";
|
|
4
|
+
import { type SyncRegistryEntry } from "./registry";
|
|
5
|
+
/**
|
|
6
|
+
* Maximum mutations per user per second on the HTTP mutate routes.
|
|
7
|
+
*
|
|
8
|
+
* Task 9.20: an alias for the shared budget — HTTP and socket mutations now draw from ONE
|
|
9
|
+
* per-user window (see `syncBatch.ts`), so there is no longer a separate HTTP allowance.
|
|
10
|
+
* Re-exported (rather than copied) so the two names can never drift; a live re-export also
|
|
11
|
+
* keeps this module's evaluation independent of `syncBatch`, which imports back into it.
|
|
12
|
+
*/
|
|
13
|
+
export { MAX_SYNC_MUTATIONS_PER_SECOND as MAX_SYNC_HTTP_MUTATIONS_PER_SECOND } from "./syncBatch";
|
|
14
|
+
/** Options for the SyncApp plugin's HTTP routes. */
|
|
15
|
+
export interface SyncAppOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Resolve the scope values a user belongs to for tenant-scoped models
|
|
18
|
+
* (e.g. the user's organization ids). Required when any registered model uses a
|
|
19
|
+
* tenant scope.
|
|
20
|
+
*/
|
|
21
|
+
getUserScopes?: (user: User, entry: SyncRegistryEntry) => Promise<string[]> | string[];
|
|
22
|
+
/** Default page size for snapshots (default 100, max 100). */
|
|
23
|
+
defaultSnapshotLimit?: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Serialize a document for a sync payload through the fallback chain:
|
|
27
|
+
* sync responseHandler > modelRouter responseHandler > toJSON.
|
|
28
|
+
* Delegates to the shared `serializeSyncPayload` (also used for `sync:delta` emission).
|
|
29
|
+
*/
|
|
30
|
+
export declare const serializeSyncDoc: ({ entry, doc, req, }: {
|
|
31
|
+
entry: SyncRegistryEntry;
|
|
32
|
+
doc: mongoose.Document;
|
|
33
|
+
req: express.Request;
|
|
34
|
+
}) => Promise<unknown>;
|
|
35
|
+
/**
|
|
36
|
+
* C2: build the server-enforced scope filter for a SINGLE stream. The stream's scope
|
|
37
|
+
* value has already been verified against the user's membership set by the caller, so
|
|
38
|
+
* this filters to exactly that one value (`{field: value}`), never an `$in`.
|
|
39
|
+
*
|
|
40
|
+
* Custom-resolver scopes cannot be inverted into a query field, so they still route
|
|
41
|
+
* through the required `snapshotFilter` (parameterized by the user, as before).
|
|
42
|
+
*
|
|
43
|
+
* Task 9.19: for owner/tenant/broadcast scopes a consumer-supplied `snapshotFilter` used
|
|
44
|
+
* to be computed and then discarded — silently widening the snapshot past what the
|
|
45
|
+
* consumer asked for. It is now composed with the scope clause via `$and` (never
|
|
46
|
+
* spread-merged, which would let one clobber the other).
|
|
47
|
+
*/
|
|
48
|
+
export declare const buildSnapshotScopeFilter: ({ entry, scopeValue, snapshotFilterResult, }: {
|
|
49
|
+
entry: SyncRegistryEntry;
|
|
50
|
+
scopeValue: string | null;
|
|
51
|
+
snapshotFilterResult?: Record<string, unknown>;
|
|
52
|
+
}) => Record<string, unknown>;
|
|
53
|
+
/** Hard cap on ids per `GET /sync/entities` request. */
|
|
54
|
+
export declare const MAX_ENTITY_FETCH = 100;
|
|
55
|
+
/**
|
|
56
|
+
* Mount the SyncDB HTTP routes:
|
|
57
|
+
* - GET /sync/streams — the authoritative set of streams the caller belongs to (C2)
|
|
58
|
+
* - GET /sync/snapshot — per-stream bootstrap/catch-up with server-enforced scoping
|
|
59
|
+
* - POST /sync/mutate — HTTP fallback mutation channel over applySyncMutation
|
|
60
|
+
* - GET /sync/key — per-user key material for the default encryption KeyProvider
|
|
61
|
+
*/
|
|
62
|
+
export declare const addSyncRoutes: (app: express.Application, options?: SyncAppOptions) => void;
|