@terreno/api 57.4.0 → 57.6.1
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/README.md +5 -5
- package/bunfig.toml +1 -1
- package/dist/actions.js +197 -368
- package/dist/adminModelRouter.js +88 -173
- package/dist/api.js +617 -852
- package/dist/audit/auditApp.js +48 -89
- package/dist/audit/auditEventModel.js +15 -16
- package/dist/audit/cloudTasksEnqueue.js +56 -149
- package/dist/audit/diff.js +70 -160
- package/dist/audit/processQueue.js +25 -70
- package/dist/audit/rbacSink.js +25 -66
- package/dist/audit/record.js +129 -251
- package/dist/auth.js +432 -681
- package/dist/authRecovery.js +155 -269
- package/dist/authTokens.js +43 -118
- 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.js +32 -47
- 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 +2 -0
- package/dist/index.js +7 -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.js +56 -150
- 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.js +2 -42
- package/dist/migrations/fixtures/all-down/20260910120001-two.js +2 -42
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +1 -39
- package/dist/migrations/fixtures/bad-name/not-a-migration.js +1 -39
- package/dist/migrations/fixtures/valid/20260910120000-alpha.js +1 -39
- package/dist/migrations/fixtures/valid/20260910120001-beta.js +2 -42
- package/dist/migrations/generate.js +107 -229
- package/dist/migrations/load.js +75 -201
- package/dist/migrations/lock.js +54 -126
- package/dist/migrations/runner.js +192 -446
- package/dist/migrations/schemaCatalog.js +122 -263
- 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.js +6 -6
- package/dist/models/notificationPreference.js +6 -6
- package/dist/models/versionConfig.js +5 -5
- package/dist/notifications/notificationService.js +133 -273
- package/dist/notifications/notificationsApp.js +85 -179
- package/dist/notifications/notificationsBeforeSend.js +19 -64
- 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.js +107 -122
- package/dist/openApiBuilder.js +117 -159
- package/dist/openApiCompat.js +81 -148
- package/dist/openApiEtag.js +7 -7
- package/dist/openApiValidator.js +177 -286
- package/dist/orgs/orgContext.js +87 -157
- package/dist/orgs/orgPermissions.js +29 -68
- package/dist/orgs/orgPlugin.js +9 -27
- package/dist/orgs/organizationModel.js +65 -166
- package/dist/orgs/organizationSettings.js +29 -36
- package/dist/orgs/orgsApp.js +346 -608
- package/dist/permissions.js +88 -181
- 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.js +48 -99
- package/dist/rateLimit/mongoStore.js +54 -112
- package/dist/rateLimit/policies.js +41 -75
- package/dist/rateLimit/redisStore.js +39 -133
- package/dist/rbac/access.js +119 -267
- 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.js +28 -99
- package/dist/rbac/middleware.js +30 -96
- package/dist/rbac/modelRouterAccess.js +130 -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 +148 -342
- package/dist/rbac/roleManager.js +318 -700
- package/dist/rbac/roleModel.js +44 -171
- package/dist/rbac/routes.js +93 -196
- package/dist/rbac/scopes.js +39 -133
- package/dist/rbac/statements.js +23 -91
- package/dist/rbac/userPlugin.js +3 -29
- package/dist/realtime/changeStreamWatcher.js +489 -800
- package/dist/realtime/deprecation.js +8 -10
- package/dist/realtime/queryMatcher.js +110 -191
- package/dist/realtime/queryStore.js +35 -118
- package/dist/realtime/realtimeApp.js +247 -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 +179 -274
- 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.js +58 -170
- package/dist/sync/adminWindowAccess.js +15 -86
- package/dist/sync/adminWindowMutation.js +110 -181
- package/dist/sync/executors.js +425 -629
- package/dist/sync/loadHarness.js +392 -689
- package/dist/sync/models.js +181 -341
- package/dist/sync/mutationHandler.js +533 -801
- package/dist/sync/registrationSideEffects.js +41 -95
- package/dist/sync/registry.js +29 -49
- package/dist/sync/routes.js +484 -817
- package/dist/sync/scripts/compactTombstones.js +83 -228
- package/dist/sync/serialize.js +18 -58
- package/dist/sync/socketHandlers.js +253 -478
- package/dist/sync/streams.js +49 -117
- package/dist/sync/syncApp.js +12 -15
- package/dist/sync/syncBatch.js +63 -168
- package/dist/sync/syncSeqPlugin.js +430 -702
- package/dist/syncConsents.js +113 -253
- package/dist/terrenoApp.js +166 -276
- package/dist/tests/createTestData.js +146 -256
- package/dist/tests/models.js +14 -54
- package/dist/tests/mongoTestSetup.js +42 -103
- package/dist/tests/testHelper.js +31 -93
- package/dist/tests.js +12 -51
- package/dist/transformers.js +61 -164
- 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.js +9 -9
- package/dist/webhooks/rawBody.js +1 -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 +6 -6
- package/src/__tests__/browserLogsRoute.test.ts +165 -0
- package/src/api.arrayOperations.test.ts +44 -0
- package/src/api.ts +64 -39
- package/src/audit/auditApp.test.ts +5 -5
- package/src/audit/auditEventModel.ts +7 -3
- package/src/audit/auditRouter.test.ts +5 -5
- package/src/audit/rbacSink.test.ts +2 -2
- package/src/audit/record.test.ts +2 -2
- package/src/audit/record.ts +3 -2
- 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/browserLogsRoute.ts +110 -0
- package/src/configurationPlugin.test.ts +1 -11
- package/src/expressServer.ts +10 -0
- package/src/frameworkModelNames.test.ts +62 -0
- package/src/frameworkModelNames.ts +33 -0
- package/src/index.ts +7 -0
- package/src/isolated/migrateCliConnect.isolated.ts +51 -0
- package/src/mcp/auth.test.ts +34 -1
- package/src/migrations/cli.test.ts +0 -23
- package/src/models/mcpServiceToken.ts +4 -3
- package/src/models/notification.ts +3 -2
- package/src/models/notificationPreference.ts +1 -1
- package/src/notifications/notificationsApp.test.ts +24 -1
- package/src/notifiers/slackNotifier.test.ts +186 -1
- package/src/notifiers/slackNotifier.ts +167 -8
- package/src/orgs/orgPlugin.ts +1 -1
- package/src/orgs/organizationModel.ts +11 -6
- package/src/plugins.test.ts +5 -0
- package/src/plugins.ts +3 -0
- package/src/realtime/realtimeApp.ts +1 -1
- package/src/sync/integration.test.ts +2 -2
- package/src/terrenoApp.ts +3 -0
- package/src/types.d.ts +1 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +4 -3
- package/tsconfig.json +13 -54
- package/tsconfig.typedoc.json +2 -0
package/dist/sync/routes.js
CHANGED
|
@@ -1,107 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
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);
|
|
24
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
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;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
50
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
51
|
-
if (!m) return o;
|
|
52
|
-
var i = m.call(o), r, ar = [], e;
|
|
53
|
-
try {
|
|
54
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
55
|
-
}
|
|
56
|
-
catch (error) { e = { error: error }; }
|
|
57
|
-
finally {
|
|
58
|
-
try {
|
|
59
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
60
|
-
}
|
|
61
|
-
finally { if (e) throw e.error; }
|
|
62
|
-
}
|
|
63
|
-
return ar;
|
|
64
|
-
};
|
|
65
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
66
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
67
|
-
if (ar || !(i in from)) {
|
|
68
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
69
|
-
ar[i] = from[i];
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
73
|
-
};
|
|
74
|
-
var __values = (this && this.__values) || function(o) {
|
|
75
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
76
|
-
if (m) return m.call(o);
|
|
77
|
-
if (o && typeof o.length === "number") return {
|
|
78
|
-
next: function () {
|
|
79
|
-
if (o && i >= o.length) o = void 0;
|
|
80
|
-
return { value: o && o[i++], done: !o };
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
84
|
-
};
|
|
85
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
86
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
87
4
|
};
|
|
88
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
89
6
|
exports.addSyncRoutes = exports.MAX_ENTITY_FETCH = exports.buildSnapshotScopeFilter = exports.serializeSyncDoc = exports.MAX_SYNC_HTTP_MUTATIONS_PER_SECOND = void 0;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
7
|
+
const express_1 = __importDefault(require("express"));
|
|
8
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
9
|
+
const api_1 = require("../api");
|
|
10
|
+
const auth_1 = require("../auth");
|
|
11
|
+
const errors_1 = require("../errors");
|
|
12
|
+
const logger_1 = require("../logger");
|
|
13
|
+
const permissions_1 = require("../permissions");
|
|
14
|
+
const plugins_1 = require("../plugins");
|
|
15
|
+
const adminBroadcastScope_1 = require("./adminBroadcastScope");
|
|
16
|
+
const adminWindowAccess_1 = require("./adminWindowAccess");
|
|
17
|
+
const models_1 = require("./models");
|
|
18
|
+
const registry_1 = require("./registry");
|
|
19
|
+
const serialize_1 = require("./serialize");
|
|
20
|
+
const streams_1 = require("./streams");
|
|
21
|
+
const syncBatch_1 = require("./syncBatch");
|
|
105
22
|
/**
|
|
106
23
|
* Maximum mutations per user per second on the HTTP mutate routes.
|
|
107
24
|
*
|
|
@@ -112,17 +29,15 @@ var syncBatch_1 = require("./syncBatch");
|
|
|
112
29
|
*/
|
|
113
30
|
var syncBatch_2 = require("./syncBatch");
|
|
114
31
|
Object.defineProperty(exports, "MAX_SYNC_HTTP_MUTATIONS_PER_SECOND", { enumerable: true, get: function () { return syncBatch_2.MAX_SYNC_MUTATIONS_PER_SECOND; } });
|
|
115
|
-
|
|
116
|
-
|
|
32
|
+
const MAX_SNAPSHOT_LIMIT = 100;
|
|
33
|
+
const DEFAULT_SNAPSHOT_LIMIT = 100;
|
|
117
34
|
/**
|
|
118
35
|
* Look up a registered model by name as a `SyncedModel`. `mongoose.model(name)` has no
|
|
119
36
|
* schema to infer from, so its static type carries none of the synced-document fields.
|
|
120
37
|
*/
|
|
121
|
-
|
|
122
|
-
return mongoose_1.default.model(modelName);
|
|
123
|
-
};
|
|
38
|
+
const getSyncedModel = (modelName) => mongoose_1.default.model(modelName);
|
|
124
39
|
/** HTTP status for each nack code returned by `POST /sync/mutate`. */
|
|
125
|
-
|
|
40
|
+
const NACK_HTTP_STATUS = {
|
|
126
41
|
conflict: 409,
|
|
127
42
|
error: 500,
|
|
128
43
|
rate_limited: 429,
|
|
@@ -134,12 +49,7 @@ var NACK_HTTP_STATUS = {
|
|
|
134
49
|
* sync responseHandler > modelRouter responseHandler > toJSON.
|
|
135
50
|
* Delegates to the shared `serializeSyncPayload` (also used for `sync:delta` emission).
|
|
136
51
|
*/
|
|
137
|
-
|
|
138
|
-
var entry = _b.entry, doc = _b.doc, req = _b.req;
|
|
139
|
-
return __generator(this, function (_c) {
|
|
140
|
-
return [2 /*return*/, (0, serialize_1.serializeSyncPayload)({ doc: doc, entry: entry, req: req })];
|
|
141
|
-
});
|
|
142
|
-
}); };
|
|
52
|
+
const serializeSyncDoc = async ({ entry, doc, req, }) => (0, serialize_1.serializeSyncPayload)({ doc: doc, entry, req });
|
|
143
53
|
exports.serializeSyncDoc = serializeSyncDoc;
|
|
144
54
|
/**
|
|
145
55
|
* C2: build the server-enforced scope filter for a SINGLE stream. The stream's scope
|
|
@@ -154,21 +64,19 @@ exports.serializeSyncDoc = serializeSyncDoc;
|
|
|
154
64
|
* consumer asked for. It is now composed with the scope clause via `$and` (never
|
|
155
65
|
* spread-merged, which would let one clobber the other).
|
|
156
66
|
*/
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
var entry = _a.entry, scopeValue = _a.scopeValue, snapshotFilterResult = _a.snapshotFilterResult;
|
|
160
|
-
var scope = entry.config.scope;
|
|
67
|
+
const buildSnapshotScopeFilter = ({ entry, scopeValue, snapshotFilterResult, }) => {
|
|
68
|
+
const { scope } = entry.config;
|
|
161
69
|
if (typeof scope === "function") {
|
|
162
70
|
// Custom scope: snapshotFilter is required at registration.
|
|
163
71
|
if (!snapshotFilterResult) {
|
|
164
72
|
throw new errors_1.APIError({
|
|
165
73
|
status: 500,
|
|
166
|
-
title:
|
|
74
|
+
title: `Sync collection ${entry.collectionTag} has a custom scope without a snapshotFilter`,
|
|
167
75
|
});
|
|
168
76
|
}
|
|
169
77
|
return snapshotFilterResult;
|
|
170
78
|
}
|
|
171
|
-
|
|
79
|
+
const scopeClause = scope.type === "broadcast" ? {} : { [(0, streams_1.getScopeField)(scope)]: scopeValue };
|
|
172
80
|
if (!snapshotFilterResult) {
|
|
173
81
|
return scopeClause;
|
|
174
82
|
}
|
|
@@ -191,45 +99,33 @@ exports.buildSnapshotScopeFilter = buildSnapshotScopeFilter;
|
|
|
191
99
|
* which sync mirrors by serving an empty page rather than erroring. A throwing filter is
|
|
192
100
|
* denied the same way the realtime path denies it: fail closed, never fall through open.
|
|
193
101
|
*/
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
userId: String(user.id),
|
|
216
|
-
});
|
|
217
|
-
return [2 /*return*/, { denied: true }];
|
|
218
|
-
case 4:
|
|
219
|
-
if (resolved === null) {
|
|
220
|
-
return [2 /*return*/, { denied: true }];
|
|
221
|
-
}
|
|
222
|
-
return [2 /*return*/, { denied: false, filter: Object.keys(resolved).length > 0 ? resolved : undefined }];
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
}); };
|
|
102
|
+
const resolveSyncQueryFilter = async ({ entry, user, }) => {
|
|
103
|
+
if (!entry.options.queryFilter) {
|
|
104
|
+
return { denied: false };
|
|
105
|
+
}
|
|
106
|
+
let resolved;
|
|
107
|
+
try {
|
|
108
|
+
resolved = await entry.options.queryFilter(user, {});
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
logger_1.logger.error("[sync] queryFilter threw for a sync read; denying the read", {
|
|
112
|
+
collection: entry.collectionTag,
|
|
113
|
+
error: String(error),
|
|
114
|
+
userId: String(user.id),
|
|
115
|
+
});
|
|
116
|
+
return { denied: true };
|
|
117
|
+
}
|
|
118
|
+
if (resolved === null) {
|
|
119
|
+
return { denied: true };
|
|
120
|
+
}
|
|
121
|
+
return { denied: false, filter: Object.keys(resolved).length > 0 ? resolved : undefined };
|
|
122
|
+
};
|
|
226
123
|
/**
|
|
227
124
|
* `$and` the resolved `queryFilter` onto a sync read's scope filter. Spread-merging would
|
|
228
125
|
* let either side clobber a same-named key (e.g. both constraining `ownerId`), so both
|
|
229
126
|
* clauses are kept and both must match.
|
|
230
127
|
*/
|
|
231
|
-
|
|
232
|
-
var queryFilter = _a.queryFilter, scopeFilter = _a.scopeFilter;
|
|
128
|
+
const composeSyncReadFilter = ({ queryFilter, scopeFilter, }) => {
|
|
233
129
|
if (!queryFilter) {
|
|
234
130
|
return scopeFilter;
|
|
235
131
|
}
|
|
@@ -243,17 +139,17 @@ var composeSyncReadFilter = function (_a) {
|
|
|
243
139
|
* and `"1e9"` used to silently become 12 and 1 — a client with a malformed cursor got a
|
|
244
140
|
* plausible-looking page instead of an error, and resumed paging from the wrong seq.
|
|
245
141
|
*/
|
|
246
|
-
|
|
142
|
+
const parseNonNegativeInt = (raw, name, fallback) => {
|
|
247
143
|
if (raw === undefined) {
|
|
248
144
|
return fallback;
|
|
249
145
|
}
|
|
250
|
-
|
|
146
|
+
const text = String(raw).trim();
|
|
251
147
|
if (!/^\d+$/.test(text)) {
|
|
252
|
-
throw new errors_1.APIError({ status: 400, title:
|
|
148
|
+
throw new errors_1.APIError({ status: 400, title: `Invalid ${name}: ${String(raw)}` });
|
|
253
149
|
}
|
|
254
|
-
|
|
150
|
+
const value = Number.parseInt(text, 10);
|
|
255
151
|
if (!Number.isSafeInteger(value)) {
|
|
256
|
-
throw new errors_1.APIError({ status: 400, title:
|
|
152
|
+
throw new errors_1.APIError({ status: 400, title: `Invalid ${name}: ${String(raw)}` });
|
|
257
153
|
}
|
|
258
154
|
return value;
|
|
259
155
|
};
|
|
@@ -265,15 +161,13 @@ var parseNonNegativeInt = function (raw, name, fallback) {
|
|
|
265
161
|
* exhausted after one page and the remaining documents were never bootstrapped.
|
|
266
162
|
* Only ObjectId-keyed models get the cast; a malformed cursor for one is a 400, not a 500.
|
|
267
163
|
*/
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
var model = _a.model, legacyCursorIn = _a.legacyCursorIn;
|
|
271
|
-
var idInstance = (_c = (_b = model.schema) === null || _b === void 0 ? void 0 : _b.path("_id")) === null || _c === void 0 ? void 0 : _c.instance;
|
|
164
|
+
const legacyCursorClause = ({ model, legacyCursorIn, }) => {
|
|
165
|
+
const idInstance = model.schema?.path("_id")?.instance;
|
|
272
166
|
if (idInstance !== "ObjectId") {
|
|
273
167
|
return { _id: { $gt: legacyCursorIn } };
|
|
274
168
|
}
|
|
275
169
|
if (!mongoose_1.default.isValidObjectId(legacyCursorIn)) {
|
|
276
|
-
throw new errors_1.APIError({ status: 400, title:
|
|
170
|
+
throw new errors_1.APIError({ status: 400, title: `Invalid legacyCursor: ${legacyCursorIn}` });
|
|
277
171
|
}
|
|
278
172
|
return { _id: { $gt: new mongoose_1.default.Types.ObjectId(legacyCursorIn) } };
|
|
279
173
|
};
|
|
@@ -284,93 +178,52 @@ var legacyCursorClause = function (_a) {
|
|
|
284
178
|
* which point the caller switches to normal seq paging. Runs the same per-doc read
|
|
285
179
|
* check as the seq page (C6/M2).
|
|
286
180
|
*/
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
entities = [];
|
|
314
|
-
_g.label = 2;
|
|
315
|
-
case 2:
|
|
316
|
-
_g.trys.push([2, 8, 9, 10]);
|
|
317
|
-
page_1 = __values(page), page_1_1 = page_1.next();
|
|
318
|
-
_g.label = 3;
|
|
319
|
-
case 3:
|
|
320
|
-
if (!!page_1_1.done) return [3 /*break*/, 7];
|
|
321
|
-
doc = page_1_1.value;
|
|
322
|
-
return [4 /*yield*/, (0, permissions_1.checkPermissions)("read", entry.options.permissions.read, user, doc)];
|
|
323
|
-
case 4:
|
|
324
|
-
if (!(_g.sent())) {
|
|
325
|
-
return [3 /*break*/, 6];
|
|
326
|
-
}
|
|
327
|
-
_d = (_c = entities).push;
|
|
328
|
-
_f = {};
|
|
329
|
-
return [4 /*yield*/, (0, exports.serializeSyncDoc)({ doc: doc, entry: entry, req: req })];
|
|
330
|
-
case 5:
|
|
331
|
-
_d.apply(_c, [(_f.data = _g.sent(),
|
|
332
|
-
_f.deleted = Boolean(doc.deleted),
|
|
333
|
-
_f.id = String(doc._id),
|
|
334
|
-
_f.seq = 0,
|
|
335
|
-
_f)]);
|
|
336
|
-
_g.label = 6;
|
|
337
|
-
case 6:
|
|
338
|
-
page_1_1 = page_1.next();
|
|
339
|
-
return [3 /*break*/, 3];
|
|
340
|
-
case 7: return [3 /*break*/, 10];
|
|
341
|
-
case 8:
|
|
342
|
-
e_1_1 = _g.sent();
|
|
343
|
-
e_1 = { error: e_1_1 };
|
|
344
|
-
return [3 /*break*/, 10];
|
|
345
|
-
case 9:
|
|
346
|
-
try {
|
|
347
|
-
if (page_1_1 && !page_1_1.done && (_e = page_1.return)) _e.call(page_1);
|
|
348
|
-
}
|
|
349
|
-
finally { if (e_1) throw e_1.error; }
|
|
350
|
-
return [7 /*endfinally*/];
|
|
351
|
-
case 10:
|
|
352
|
-
lastId = String(page[page.length - 1]._id);
|
|
353
|
-
return [2 /*return*/, { entities: entities, legacyCursor: lastId }];
|
|
181
|
+
const pageLegacyStratum = async ({ model, scopeFilter, legacyCursorIn, limit, entry, req, }) => {
|
|
182
|
+
const user = req.user;
|
|
183
|
+
// `deleted` MUST stay a TOP-LEVEL key so isDeletedPlugin does not re-inject its
|
|
184
|
+
// {deleted: {$ne: true}} exclusion (which only fires when the top-level filter has no
|
|
185
|
+
// `deleted` key) and hide legacy tombstones. See the seq-page query for the full note.
|
|
186
|
+
const legacyFilter = {
|
|
187
|
+
$and: [
|
|
188
|
+
scopeFilter,
|
|
189
|
+
{ _syncSeq: { $in: [null, 0] } },
|
|
190
|
+
...(legacyCursorIn ? [legacyCursorClause({ legacyCursorIn, model })] : []),
|
|
191
|
+
],
|
|
192
|
+
deleted: { $in: [true, false] },
|
|
193
|
+
};
|
|
194
|
+
const docs = await model
|
|
195
|
+
.find(legacyFilter)
|
|
196
|
+
.sort({ _id: 1 })
|
|
197
|
+
.limit(limit + 1);
|
|
198
|
+
if (docs.length === 0) {
|
|
199
|
+
// Stratum exhausted (or never had legacy docs) — caller proceeds by seq.
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
const page = docs.slice(0, limit);
|
|
203
|
+
const entities = [];
|
|
204
|
+
for (const doc of page) {
|
|
205
|
+
if (!(await (0, permissions_1.checkPermissions)("read", entry.options.permissions.read, user, doc))) {
|
|
206
|
+
continue;
|
|
354
207
|
}
|
|
355
|
-
|
|
356
|
-
|
|
208
|
+
entities.push({
|
|
209
|
+
data: await (0, exports.serializeSyncDoc)({ doc, entry, req }),
|
|
210
|
+
deleted: Boolean(doc.deleted),
|
|
211
|
+
id: String(doc._id),
|
|
212
|
+
seq: 0,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
const lastId = String(page[page.length - 1]._id);
|
|
216
|
+
return { entities, legacyCursor: lastId };
|
|
217
|
+
};
|
|
357
218
|
/**
|
|
358
219
|
* C1: true when the stream's head (highest claimed seq) exceeds the stable frontier —
|
|
359
220
|
* i.e. committed seqs are still coming once the in-flight writes below the frontier land.
|
|
360
221
|
*/
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
return
|
|
365
|
-
|
|
366
|
-
case 0: return [4 /*yield*/, (0, plugins_1.findOneOrNoneFor)(models_1.SyncCounter, { stream: streamKey })];
|
|
367
|
-
case 1:
|
|
368
|
-
counter = _b.sent();
|
|
369
|
-
head = (_a = counter === null || counter === void 0 ? void 0 : counter.seq) !== null && _a !== void 0 ? _a : 0;
|
|
370
|
-
return [2 /*return*/, head > frontierSeq];
|
|
371
|
-
}
|
|
372
|
-
});
|
|
373
|
-
}); };
|
|
222
|
+
const frontierBelowStreamHead = async (streamKey, frontierSeq) => {
|
|
223
|
+
const counter = await (0, plugins_1.findOneOrNoneFor)(models_1.SyncCounter, { stream: streamKey });
|
|
224
|
+
const head = counter?.seq ?? 0;
|
|
225
|
+
return head > frontierSeq;
|
|
226
|
+
};
|
|
374
227
|
/**
|
|
375
228
|
* Page the normal seq stratum: documents with `_syncSeq` in `(cursor, frontierSeq]`, unioned
|
|
376
229
|
* with the `SyncScopeMove` tombstones for the same seq range (C4). Mirrors
|
|
@@ -379,107 +232,83 @@ var frontierBelowStreamHead = function (streamKey, frontierSeq) { return __await
|
|
|
379
232
|
*
|
|
380
233
|
* Returns the page's entities plus the cursor to resume from and whether more remains.
|
|
381
234
|
*/
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
doc = page_2_1.value;
|
|
424
|
-
return [4 /*yield*/, (0, permissions_1.checkPermissions)("read", entry.options.permissions.read, user, doc)];
|
|
425
|
-
case 5:
|
|
426
|
-
allowed = _k.sent();
|
|
427
|
-
if (!allowed) {
|
|
428
|
-
return [3 /*break*/, 9];
|
|
429
|
-
}
|
|
430
|
-
isTombstone = Boolean(doc.deleted);
|
|
431
|
-
_d = (_c = docEntities).push;
|
|
432
|
-
_g = {};
|
|
433
|
-
if (!isTombstone) return [3 /*break*/, 6];
|
|
434
|
-
_e = null;
|
|
435
|
-
return [3 /*break*/, 8];
|
|
436
|
-
case 6: return [4 /*yield*/, (0, exports.serializeSyncDoc)({ doc: doc, entry: entry, req: req })];
|
|
437
|
-
case 7:
|
|
438
|
-
_e = _k.sent();
|
|
439
|
-
_k.label = 8;
|
|
440
|
-
case 8:
|
|
441
|
-
_d.apply(_c, [(
|
|
442
|
-
// C7: tombstones carry no data (privacy + payload growth) — only id/seq/deleted.
|
|
443
|
-
_g.data = _e,
|
|
444
|
-
_g.deleted = isTombstone,
|
|
445
|
-
_g.id = String(doc._id),
|
|
446
|
-
_g.seq = (_j = doc._syncSeq) !== null && _j !== void 0 ? _j : 0,
|
|
447
|
-
_g)]);
|
|
448
|
-
_k.label = 9;
|
|
449
|
-
case 9:
|
|
450
|
-
page_2_1 = page_2.next();
|
|
451
|
-
return [3 /*break*/, 4];
|
|
452
|
-
case 10: return [3 /*break*/, 13];
|
|
453
|
-
case 11:
|
|
454
|
-
e_2_1 = _k.sent();
|
|
455
|
-
e_2 = { error: e_2_1 };
|
|
456
|
-
return [3 /*break*/, 13];
|
|
457
|
-
case 12:
|
|
458
|
-
try {
|
|
459
|
-
if (page_2_1 && !page_2_1.done && (_f = page_2.return)) _f.call(page_2);
|
|
460
|
-
}
|
|
461
|
-
finally { if (e_2) throw e_2.error; }
|
|
462
|
-
return [7 /*endfinally*/];
|
|
463
|
-
case 13:
|
|
464
|
-
markerEntities = markerPage.map(function (m) { return ({
|
|
465
|
-
data: null,
|
|
466
|
-
deleted: true,
|
|
467
|
-
id: m.entityId,
|
|
468
|
-
seq: m.seq,
|
|
469
|
-
}); });
|
|
470
|
-
merged = __spreadArray(__spreadArray([], __read(docEntities), false), __read(markerEntities), false).sort(function (a, b) { return a.seq - b.seq; })
|
|
471
|
-
.filter(function (entity) { return entity.seq <= coveredSeq; });
|
|
472
|
-
entities = merged.slice(0, limit);
|
|
473
|
-
entitiesTruncated = merged.length > entities.length;
|
|
474
|
-
return [4 /*yield*/, frontierBelowStreamHead(streamKey, frontierSeq)];
|
|
475
|
-
case 14:
|
|
476
|
-
frontierBelowHead = _k.sent();
|
|
477
|
-
hasMore = docsHaveMore || markersHaveMore || entitiesTruncated || frontierBelowHead;
|
|
478
|
-
advanceTo = entitiesTruncated ? entities[entities.length - 1].seq : coveredSeq;
|
|
479
|
-
return [2 /*return*/, { cursor: Math.max(cursor, Math.min(advanceTo, frontierSeq)), entities: entities, hasMore: hasMore }];
|
|
235
|
+
const pageSeqStratum = async ({ model, scopeFilter, cursor, limit, frontierSeq, streamKey, entry, req, }) => {
|
|
236
|
+
const user = req.user;
|
|
237
|
+
// C1: never page past the stable frontier — a cursor must not cross an uncommitted hole.
|
|
238
|
+
const seqFilter = { _syncSeq: { $gt: cursor, $lte: frontierSeq } };
|
|
239
|
+
// M1: compose the scope + seq clauses with $and (never spread-merge, which lets a
|
|
240
|
+
// scopeFilter $or clobber the seq clause). `deleted` MUST stay a TOP-LEVEL key:
|
|
241
|
+
// isDeletedPlugin injects {deleted: {$ne: true}} only when the top-level filter has
|
|
242
|
+
// no `deleted` key — burying it inside $and would let the plugin re-inject its
|
|
243
|
+
// exclusion and hide the tombstones catch-up depends on.
|
|
244
|
+
const query = { $and: [scopeFilter, seqFilter], deleted: { $in: [true, false] } };
|
|
245
|
+
const docs = await model
|
|
246
|
+
.find(query)
|
|
247
|
+
.sort({ _syncSeq: 1 })
|
|
248
|
+
.limit(limit + 1);
|
|
249
|
+
// C4: merge SyncScopeMove markers for THIS (old) stream into the page as tombstones,
|
|
250
|
+
// so an offline old-stream client learns the doc left its stream.
|
|
251
|
+
const markers = await models_1.SyncScopeMove.find({
|
|
252
|
+
fromStream: streamKey,
|
|
253
|
+
seq: { $gt: cursor, $lte: frontierSeq },
|
|
254
|
+
})
|
|
255
|
+
.sort({ seq: 1 })
|
|
256
|
+
.limit(limit + 1)
|
|
257
|
+
.lean();
|
|
258
|
+
const page = docs.slice(0, limit);
|
|
259
|
+
const markerPage = markers.slice(0, limit);
|
|
260
|
+
const docsHaveMore = docs.length > limit;
|
|
261
|
+
const markersHaveMore = markers.length > limit;
|
|
262
|
+
// The highest seq this page has fully examined. Docs and markers are fetched with
|
|
263
|
+
// independent limits, so coverage stops at the lower of the two: past that seq, the
|
|
264
|
+
// truncated side may still hold rows this page never saw. When a side was not
|
|
265
|
+
// truncated, everything up to the frontier is covered.
|
|
266
|
+
const lastDocSeq = page.length > 0 ? (page[page.length - 1]._syncSeq ?? 0) : 0;
|
|
267
|
+
const lastMarkerSeq = markerPage.length > 0 ? markerPage[markerPage.length - 1].seq : 0;
|
|
268
|
+
const coveredSeq = Math.min(docsHaveMore ? lastDocSeq : frontierSeq, markersHaveMore ? lastMarkerSeq : frontierSeq);
|
|
269
|
+
// C6 (M2): run the same per-doc read permission the delta path uses; drop denied
|
|
270
|
+
// docs but still advance the cursor past them (parity with delta behavior).
|
|
271
|
+
const docEntities = [];
|
|
272
|
+
for (const doc of page) {
|
|
273
|
+
const allowed = await (0, permissions_1.checkPermissions)("read", entry.options.permissions.read, user, doc);
|
|
274
|
+
if (!allowed) {
|
|
275
|
+
continue;
|
|
480
276
|
}
|
|
481
|
-
|
|
482
|
-
|
|
277
|
+
const isTombstone = Boolean(doc.deleted);
|
|
278
|
+
docEntities.push({
|
|
279
|
+
// C7: tombstones carry no data (privacy + payload growth) — only id/seq/deleted.
|
|
280
|
+
data: isTombstone ? null : await (0, exports.serializeSyncDoc)({ doc, entry, req }),
|
|
281
|
+
deleted: isTombstone,
|
|
282
|
+
id: String(doc._id),
|
|
283
|
+
seq: doc._syncSeq ?? 0,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
const markerEntities = markerPage.map((m) => ({
|
|
287
|
+
data: null,
|
|
288
|
+
deleted: true,
|
|
289
|
+
id: m.entityId,
|
|
290
|
+
seq: m.seq,
|
|
291
|
+
}));
|
|
292
|
+
// Union doc page + marker tombstones, sort by seq, and drop anything beyond the
|
|
293
|
+
// covered range (it is re-fetched on the next page, in order).
|
|
294
|
+
const merged = [...docEntities, ...markerEntities]
|
|
295
|
+
.sort((a, b) => a.seq - b.seq)
|
|
296
|
+
.filter((entity) => entity.seq <= coveredSeq);
|
|
297
|
+
const entities = merged.slice(0, limit);
|
|
298
|
+
const entitiesTruncated = merged.length > entities.length;
|
|
299
|
+
// hasMore when: a full doc page was returned, extra markers remain, included
|
|
300
|
+
// entities were truncated by the limit, or the frontier sits below the head (more
|
|
301
|
+
// committed seqs are coming once in-flight writes land).
|
|
302
|
+
const frontierBelowHead = await frontierBelowStreamHead(streamKey, frontierSeq);
|
|
303
|
+
const hasMore = docsHaveMore || markersHaveMore || entitiesTruncated || frontierBelowHead;
|
|
304
|
+
// Advance the cursor over every seq this page CONSUMED, not just the entities it
|
|
305
|
+
// returned: a page whose docs were all read-denied returns nothing, and stalling the
|
|
306
|
+
// cursor there loops the client forever. Truncation by `limit` still pins the cursor
|
|
307
|
+
// to the last delivered entity so nothing undelivered is skipped, and the cursor
|
|
308
|
+
// never crosses the stable frontier (C1) nor moves backwards.
|
|
309
|
+
const advanceTo = entitiesTruncated ? entities[entities.length - 1].seq : coveredSeq;
|
|
310
|
+
return { cursor: Math.max(cursor, Math.min(advanceTo, frontierSeq)), entities, hasMore };
|
|
311
|
+
};
|
|
483
312
|
/** Hard cap on ids per `GET /sync/entities` request. */
|
|
484
313
|
exports.MAX_ENTITY_FETCH = 100;
|
|
485
314
|
/**
|
|
@@ -488,21 +317,19 @@ exports.MAX_ENTITY_FETCH = 100;
|
|
|
488
317
|
* caller input. Validate up front and name the offending ids in a 400. Models with
|
|
489
318
|
* client-minted string `_id`s (the syncdb default, see `legacyCursorClause`) take any shape.
|
|
490
319
|
*/
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
var model = _a.model, ids = _a.ids;
|
|
494
|
-
var idInstance = (_c = (_b = model.schema) === null || _b === void 0 ? void 0 : _b.path("_id")) === null || _c === void 0 ? void 0 : _c.instance;
|
|
320
|
+
const castEntityIds = ({ model, ids, }) => {
|
|
321
|
+
const idInstance = model.schema?.path("_id")?.instance;
|
|
495
322
|
if (idInstance !== "ObjectId") {
|
|
496
323
|
return ids;
|
|
497
324
|
}
|
|
498
|
-
|
|
325
|
+
const invalid = ids.filter((id) => !mongoose_1.default.isValidObjectId(id));
|
|
499
326
|
if (invalid.length > 0) {
|
|
500
327
|
throw new errors_1.APIError({
|
|
501
328
|
status: 400,
|
|
502
|
-
title:
|
|
329
|
+
title: `Invalid ids for ${model.modelName}: ${invalid.join(",")}`,
|
|
503
330
|
});
|
|
504
331
|
}
|
|
505
|
-
return ids.map(
|
|
332
|
+
return ids.map((id) => new mongoose_1.default.Types.ObjectId(id));
|
|
506
333
|
};
|
|
507
334
|
/**
|
|
508
335
|
* Mount the SyncDB HTTP routes:
|
|
@@ -511,485 +338,325 @@ var castEntityIds = function (_a) {
|
|
|
511
338
|
* - POST /sync/mutate — HTTP fallback mutation channel over applySyncMutation
|
|
512
339
|
* - GET /sync/key — per-user key material for the default encryption KeyProvider
|
|
513
340
|
*/
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
var router = express_1.default.Router();
|
|
341
|
+
const addSyncRoutes = (app, options = {}) => {
|
|
342
|
+
const router = express_1.default.Router();
|
|
517
343
|
// C2: authoritative membership discovery. Runs against the full req.user (D2) so
|
|
518
344
|
// tenant memberships resolve from current organizationIds.
|
|
519
|
-
router.get("/sync/streams", (0, auth_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
}
|
|
529
|
-
streams = [];
|
|
530
|
-
_e.label = 1;
|
|
531
|
-
case 1:
|
|
532
|
-
_e.trys.push([1, 9, 10, 11]);
|
|
533
|
-
_a = __values((0, registry_1.getSyncRegistry)()), _b = _a.next();
|
|
534
|
-
_e.label = 2;
|
|
535
|
-
case 2:
|
|
536
|
-
if (!!_b.done) return [3 /*break*/, 8];
|
|
537
|
-
entry = _b.value;
|
|
538
|
-
return [4 /*yield*/, (0, permissions_1.checkPermissions)("list", entry.options.permissions.list, user)];
|
|
539
|
-
case 3:
|
|
540
|
-
if (!(_e.sent())) {
|
|
541
|
-
return [3 /*break*/, 7];
|
|
542
|
-
}
|
|
543
|
-
_e.label = 4;
|
|
544
|
-
case 4:
|
|
545
|
-
_e.trys.push([4, 6, , 7]);
|
|
546
|
-
return [4 /*yield*/, (0, streams_1.resolveUserStreamsForEntry)({
|
|
547
|
-
entry: entry,
|
|
548
|
-
getUserScopes: options.getUserScopes,
|
|
549
|
-
user: user,
|
|
550
|
-
})];
|
|
551
|
-
case 5:
|
|
552
|
-
entryStreams = _e.sent();
|
|
553
|
-
try {
|
|
554
|
-
for (entryStreams_1 = (e_4 = void 0, __values(entryStreams)), entryStreams_1_1 = entryStreams_1.next(); !entryStreams_1_1.done; entryStreams_1_1 = entryStreams_1.next()) {
|
|
555
|
-
stream = entryStreams_1_1.value;
|
|
556
|
-
streams.push({ collection: entry.collectionTag, stream: stream });
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
560
|
-
finally {
|
|
561
|
-
try {
|
|
562
|
-
if (entryStreams_1_1 && !entryStreams_1_1.done && (_d = entryStreams_1.return)) _d.call(entryStreams_1);
|
|
563
|
-
}
|
|
564
|
-
finally { if (e_4) throw e_4.error; }
|
|
565
|
-
}
|
|
566
|
-
return [3 /*break*/, 7];
|
|
567
|
-
case 6:
|
|
568
|
-
error_2 = _e.sent();
|
|
569
|
-
throw new errors_1.APIError({
|
|
570
|
-
status: 500,
|
|
571
|
-
title: "Failed to resolve streams for ".concat(entry.collectionTag, ": ").concat(String(error_2)),
|
|
572
|
-
});
|
|
573
|
-
case 7:
|
|
574
|
-
_b = _a.next();
|
|
575
|
-
return [3 /*break*/, 2];
|
|
576
|
-
case 8: return [3 /*break*/, 11];
|
|
577
|
-
case 9:
|
|
578
|
-
e_3_1 = _e.sent();
|
|
579
|
-
e_3 = { error: e_3_1 };
|
|
580
|
-
return [3 /*break*/, 11];
|
|
581
|
-
case 10:
|
|
582
|
-
try {
|
|
583
|
-
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
584
|
-
}
|
|
585
|
-
finally { if (e_3) throw e_3.error; }
|
|
586
|
-
return [7 /*endfinally*/];
|
|
587
|
-
case 11:
|
|
588
|
-
response = { streams: streams };
|
|
589
|
-
return [2 /*return*/, res.json(response)];
|
|
345
|
+
router.get("/sync/streams", (0, auth_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(async (req, res) => {
|
|
346
|
+
const user = req.user;
|
|
347
|
+
if (!user) {
|
|
348
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
349
|
+
}
|
|
350
|
+
const streams = [];
|
|
351
|
+
for (const entry of (0, registry_1.getSyncRegistry)()) {
|
|
352
|
+
if (!(await (0, permissions_1.checkPermissions)("list", entry.options.permissions.list, user))) {
|
|
353
|
+
continue;
|
|
590
354
|
}
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
if (!user) {
|
|
601
|
-
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
602
|
-
}
|
|
603
|
-
streamKey = String((_b = req.query.stream) !== null && _b !== void 0 ? _b : "");
|
|
604
|
-
if (!streamKey) {
|
|
605
|
-
throw new errors_1.APIError({ status: 400, title: "stream query parameter is required" });
|
|
606
|
-
}
|
|
607
|
-
parsed = (0, streams_1.parseStreamKey)(streamKey);
|
|
608
|
-
if (!parsed) {
|
|
609
|
-
throw new errors_1.APIError({ status: 400, title: "Invalid stream key: ".concat(streamKey) });
|
|
610
|
-
}
|
|
611
|
-
entry = (0, registry_1.findSyncEntryByCollectionTag)(parsed.collectionTag);
|
|
612
|
-
if (!entry) {
|
|
613
|
-
throw new errors_1.APIError({
|
|
614
|
-
status: 404,
|
|
615
|
-
title: "Unknown sync collection: ".concat(parsed.collectionTag),
|
|
616
|
-
});
|
|
617
|
-
}
|
|
618
|
-
return [4 /*yield*/, (0, permissions_1.checkPermissions)("list", entry.options.permissions.list, user)];
|
|
619
|
-
case 1:
|
|
620
|
-
if (!(_d.sent())) {
|
|
621
|
-
throw new errors_1.APIError({
|
|
622
|
-
status: 403,
|
|
623
|
-
title: "Access to sync snapshot for ".concat(parsed.collectionTag, " denied for ").concat(user.id),
|
|
624
|
-
});
|
|
625
|
-
}
|
|
626
|
-
return [4 /*yield*/, (0, streams_1.resolveUserStreamsForEntry)({
|
|
627
|
-
entry: entry,
|
|
628
|
-
getUserScopes: options.getUserScopes,
|
|
629
|
-
user: user,
|
|
630
|
-
})];
|
|
631
|
-
case 2:
|
|
632
|
-
memberStreams = _d.sent();
|
|
633
|
-
if (!memberStreams.includes(streamKey)) {
|
|
634
|
-
throw new errors_1.APIError({
|
|
635
|
-
status: 403,
|
|
636
|
-
title: "User ".concat(user.id, " does not belong to stream ").concat(streamKey),
|
|
637
|
-
});
|
|
638
|
-
}
|
|
639
|
-
cursor = parseNonNegativeInt(req.query.cursor, "cursor", 0);
|
|
640
|
-
requestedLimit = parseNonNegativeInt(req.query.limit, "limit", (_c = options.defaultSnapshotLimit) !== null && _c !== void 0 ? _c : DEFAULT_SNAPSHOT_LIMIT);
|
|
641
|
-
limit = Math.min(Math.max(requestedLimit, 1), MAX_SNAPSHOT_LIMIT);
|
|
642
|
-
legacyCursorIn = typeof req.query.legacyCursor === "string" && req.query.legacyCursor.length > 0
|
|
643
|
-
? req.query.legacyCursor
|
|
644
|
-
: undefined;
|
|
645
|
-
if (!entry.config.snapshotFilter) return [3 /*break*/, 4];
|
|
646
|
-
return [4 /*yield*/, entry.config.snapshotFilter({ id: String(user.id) })];
|
|
647
|
-
case 3:
|
|
648
|
-
_a = _d.sent();
|
|
649
|
-
return [3 /*break*/, 5];
|
|
650
|
-
case 4:
|
|
651
|
-
_a = undefined;
|
|
652
|
-
_d.label = 5;
|
|
653
|
-
case 5:
|
|
654
|
-
snapshotFilterResult = _a;
|
|
655
|
-
return [4 /*yield*/, resolveSyncQueryFilter({ entry: entry, user: user })];
|
|
656
|
-
case 6:
|
|
657
|
-
queryFilterResult = _d.sent();
|
|
658
|
-
scopeFilter = composeSyncReadFilter({
|
|
659
|
-
queryFilter: queryFilterResult.filter,
|
|
660
|
-
scopeFilter: (0, exports.buildSnapshotScopeFilter)({
|
|
661
|
-
entry: entry,
|
|
662
|
-
scopeValue: parsed.scopeValue,
|
|
663
|
-
snapshotFilterResult: snapshotFilterResult,
|
|
664
|
-
}),
|
|
665
|
-
});
|
|
666
|
-
model = getSyncedModel(entry.modelName);
|
|
667
|
-
return [4 /*yield*/, (0, models_1.computeStableFrontier)({ stream: streamKey })];
|
|
668
|
-
case 7:
|
|
669
|
-
frontierSeq = _d.sent();
|
|
670
|
-
return [4 /*yield*/, (0, models_1.getCompactedThroughSeq)({ stream: streamKey })];
|
|
671
|
-
case 8:
|
|
672
|
-
oldestRetainedSeq = _d.sent();
|
|
673
|
-
// Task 9.19: a `queryFilter` that denies this caller means "no visible rows", exactly
|
|
674
|
-
// as REST's list endpoint answers. Serve a terminal empty page (cursor at the
|
|
675
|
-
// frontier, hasMore false) so the client settles instead of paging forever.
|
|
676
|
-
if (queryFilterResult.denied) {
|
|
677
|
-
deniedResponse = {
|
|
678
|
-
cursor: frontierSeq,
|
|
679
|
-
entities: [],
|
|
680
|
-
frontierSeq: frontierSeq,
|
|
681
|
-
hasMore: false,
|
|
682
|
-
oldestRetainedSeq: oldestRetainedSeq,
|
|
683
|
-
stream: streamKey,
|
|
684
|
-
};
|
|
685
|
-
return [2 /*return*/, res.json(deniedResponse)];
|
|
686
|
-
}
|
|
687
|
-
if (!(cursor === 0)) return [3 /*break*/, 10];
|
|
688
|
-
return [4 /*yield*/, pageLegacyStratum({
|
|
689
|
-
entry: entry,
|
|
690
|
-
legacyCursorIn: legacyCursorIn,
|
|
691
|
-
limit: limit,
|
|
692
|
-
model: model,
|
|
693
|
-
req: req,
|
|
694
|
-
scopeFilter: scopeFilter,
|
|
695
|
-
})];
|
|
696
|
-
case 9:
|
|
697
|
-
legacyResult = _d.sent();
|
|
698
|
-
if (legacyResult) {
|
|
699
|
-
response_1 = {
|
|
700
|
-
cursor: 0,
|
|
701
|
-
entities: legacyResult.entities,
|
|
702
|
-
frontierSeq: frontierSeq,
|
|
703
|
-
hasMore: true,
|
|
704
|
-
legacyCursor: legacyResult.legacyCursor,
|
|
705
|
-
oldestRetainedSeq: oldestRetainedSeq,
|
|
706
|
-
stream: streamKey,
|
|
707
|
-
};
|
|
708
|
-
return [2 /*return*/, res.json(response_1)];
|
|
709
|
-
}
|
|
710
|
-
_d.label = 10;
|
|
711
|
-
case 10: return [4 /*yield*/, pageSeqStratum({
|
|
712
|
-
cursor: cursor,
|
|
713
|
-
entry: entry,
|
|
714
|
-
frontierSeq: frontierSeq,
|
|
715
|
-
limit: limit,
|
|
716
|
-
model: model,
|
|
717
|
-
req: req,
|
|
718
|
-
scopeFilter: scopeFilter,
|
|
719
|
-
streamKey: streamKey,
|
|
720
|
-
})];
|
|
721
|
-
case 11:
|
|
722
|
-
seqResult = _d.sent();
|
|
723
|
-
response = {
|
|
724
|
-
cursor: seqResult.cursor,
|
|
725
|
-
entities: seqResult.entities,
|
|
726
|
-
frontierSeq: frontierSeq,
|
|
727
|
-
hasMore: seqResult.hasMore,
|
|
728
|
-
oldestRetainedSeq: oldestRetainedSeq,
|
|
729
|
-
stream: streamKey,
|
|
730
|
-
};
|
|
731
|
-
return [2 /*return*/, res.json(response)];
|
|
355
|
+
try {
|
|
356
|
+
const entryStreams = await (0, streams_1.resolveUserStreamsForEntry)({
|
|
357
|
+
entry,
|
|
358
|
+
getUserScopes: options.getUserScopes,
|
|
359
|
+
user,
|
|
360
|
+
});
|
|
361
|
+
for (const stream of entryStreams) {
|
|
362
|
+
streams.push({ collection: entry.collectionTag, stream });
|
|
363
|
+
}
|
|
732
364
|
}
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
var _p, _q;
|
|
739
|
-
return __generator(this, function (_r) {
|
|
740
|
-
switch (_r.label) {
|
|
741
|
-
case 0:
|
|
742
|
-
user = req.user;
|
|
743
|
-
if (!user) {
|
|
744
|
-
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
745
|
-
}
|
|
746
|
-
collection = String((_p = req.query.collection) !== null && _p !== void 0 ? _p : "").trim();
|
|
747
|
-
if (!collection) {
|
|
748
|
-
throw new errors_1.APIError({ status: 400, title: "collection query parameter is required" });
|
|
749
|
-
}
|
|
750
|
-
idsParam = req.query.ids;
|
|
751
|
-
ids = typeof idsParam === "string"
|
|
752
|
-
? idsParam
|
|
753
|
-
.split(",")
|
|
754
|
-
.map(function (id) { return id.trim(); })
|
|
755
|
-
.filter(function (id) { return id.length > 0; })
|
|
756
|
-
: [];
|
|
757
|
-
if (ids.length === 0) {
|
|
758
|
-
throw new errors_1.APIError({ status: 400, title: "ids query parameter is required" });
|
|
759
|
-
}
|
|
760
|
-
// Task 9.27: this used to `.slice(0, MAX_ENTITY_FETCH)`, so an over-cap repair fetch
|
|
761
|
-
// came back 200 with a partial body and the client treated the entities it never
|
|
762
|
-
// received as absent. Reject instead, so the caller re-requests in chunks.
|
|
763
|
-
if (ids.length > exports.MAX_ENTITY_FETCH) {
|
|
764
|
-
throw new errors_1.APIError({
|
|
765
|
-
status: 400,
|
|
766
|
-
title: "Too many ids: ".concat(ids.length, " requested, limit is ").concat(exports.MAX_ENTITY_FETCH),
|
|
767
|
-
});
|
|
768
|
-
}
|
|
769
|
-
entry = (0, registry_1.findSyncEntryByCollectionTag)(collection);
|
|
770
|
-
if (!entry) {
|
|
771
|
-
throw new errors_1.APIError({
|
|
772
|
-
status: 404,
|
|
773
|
-
title: "Unknown sync collection: ".concat(collection),
|
|
774
|
-
});
|
|
775
|
-
}
|
|
776
|
-
return [4 /*yield*/, (0, permissions_1.checkPermissions)("list", entry.options.permissions.list, user)];
|
|
777
|
-
case 1:
|
|
778
|
-
if (!(_r.sent())) {
|
|
779
|
-
throw new errors_1.APIError({
|
|
780
|
-
status: 403,
|
|
781
|
-
title: "Access to sync entities for ".concat(collection, " denied for ").concat(user.id),
|
|
782
|
-
});
|
|
783
|
-
}
|
|
784
|
-
_a = entry.config.adminBroadcast === true;
|
|
785
|
-
if (!_a) return [3 /*break*/, 3];
|
|
786
|
-
return [4 /*yield*/, (0, adminWindowAccess_1.canUseAdminBroadcastWindow)({
|
|
787
|
-
accessControl: options.accessControl,
|
|
788
|
-
canOpenAdminWindow: options.canOpenAdminWindow,
|
|
789
|
-
user: user,
|
|
790
|
-
})];
|
|
791
|
-
case 2:
|
|
792
|
-
_a = (_r.sent());
|
|
793
|
-
_r.label = 3;
|
|
794
|
-
case 3:
|
|
795
|
-
isAdminWindow = _a;
|
|
796
|
-
adminScope = isAdminWindow ? (0, adminBroadcastScope_1.getAdminBroadcastScope)(entry.modelName) : undefined;
|
|
797
|
-
_b = adminScope;
|
|
798
|
-
if (!_b) return [3 /*break*/, 5];
|
|
799
|
-
return [4 /*yield*/, (0, adminBroadcastScope_1.canListAdminBroadcastScope)({ scope: adminScope, user: user })];
|
|
800
|
-
case 4:
|
|
801
|
-
_b = !(_r.sent());
|
|
802
|
-
_r.label = 5;
|
|
803
|
-
case 5:
|
|
804
|
-
if (_b) {
|
|
805
|
-
deniedResponse = { entities: [] };
|
|
806
|
-
return [2 /*return*/, res.json(deniedResponse)];
|
|
807
|
-
}
|
|
808
|
-
if (!isAdminWindow) return [3 /*break*/, 6];
|
|
809
|
-
_c = [];
|
|
810
|
-
return [3 /*break*/, 8];
|
|
811
|
-
case 6: return [4 /*yield*/, (0, streams_1.resolveUserStreamsForEntry)({
|
|
812
|
-
entry: entry,
|
|
813
|
-
getUserScopes: options.getUserScopes,
|
|
814
|
-
user: user,
|
|
815
|
-
})];
|
|
816
|
-
case 7:
|
|
817
|
-
_c = _r.sent();
|
|
818
|
-
_r.label = 8;
|
|
819
|
-
case 8:
|
|
820
|
-
memberStreams = _c;
|
|
821
|
-
memberSet = new Set(memberStreams);
|
|
822
|
-
if (!adminScope) return [3 /*break*/, 10];
|
|
823
|
-
return [4 /*yield*/, (0, adminBroadcastScope_1.resolveAdminBroadcastQueryFilter)({ scope: adminScope, user: user })];
|
|
824
|
-
case 9:
|
|
825
|
-
_d = _r.sent();
|
|
826
|
-
return [3 /*break*/, 14];
|
|
827
|
-
case 10:
|
|
828
|
-
if (!isAdminWindow) return [3 /*break*/, 11];
|
|
829
|
-
_e = { denied: false, filter: undefined };
|
|
830
|
-
return [3 /*break*/, 13];
|
|
831
|
-
case 11: return [4 /*yield*/, resolveSyncQueryFilter({ entry: entry, user: user })];
|
|
832
|
-
case 12:
|
|
833
|
-
_e = _r.sent();
|
|
834
|
-
_r.label = 13;
|
|
835
|
-
case 13:
|
|
836
|
-
_d = _e;
|
|
837
|
-
_r.label = 14;
|
|
838
|
-
case 14:
|
|
839
|
-
queryFilterResult = _d;
|
|
840
|
-
if (queryFilterResult.denied) {
|
|
841
|
-
deniedResponse = { entities: [] };
|
|
842
|
-
return [2 /*return*/, res.json(deniedResponse)];
|
|
843
|
-
}
|
|
844
|
-
model = getSyncedModel(entry.modelName);
|
|
845
|
-
castIds = castEntityIds({ ids: ids, model: model });
|
|
846
|
-
return [4 /*yield*/, model.find(__assign(__assign({}, composeSyncReadFilter({
|
|
847
|
-
queryFilter: queryFilterResult.filter,
|
|
848
|
-
scopeFilter: { _id: { $in: castIds } },
|
|
849
|
-
})), { deleted: { $in: [true, false] } }))];
|
|
850
|
-
case 15:
|
|
851
|
-
docs = _r.sent();
|
|
852
|
-
entities = [];
|
|
853
|
-
_r.label = 16;
|
|
854
|
-
case 16:
|
|
855
|
-
_r.trys.push([16, 27, 28, 29]);
|
|
856
|
-
_f = __values(docs), _g = _f.next();
|
|
857
|
-
_r.label = 17;
|
|
858
|
-
case 17:
|
|
859
|
-
if (!!_g.done) return [3 /*break*/, 26];
|
|
860
|
-
doc = _g.value;
|
|
861
|
-
if (!adminScope) return [3 /*break*/, 19];
|
|
862
|
-
return [4 /*yield*/, (0, permissions_1.checkPermissions)("read", adminScope.readPermissions, user, doc)];
|
|
863
|
-
case 18:
|
|
864
|
-
_h = _r.sent();
|
|
865
|
-
return [3 /*break*/, 21];
|
|
866
|
-
case 19: return [4 /*yield*/, (0, permissions_1.checkPermissions)("read", entry.options.permissions.read, user, doc)];
|
|
867
|
-
case 20:
|
|
868
|
-
_h = _r.sent();
|
|
869
|
-
_r.label = 21;
|
|
870
|
-
case 21:
|
|
871
|
-
allowed = _h;
|
|
872
|
-
if (!allowed) {
|
|
873
|
-
return [3 /*break*/, 25];
|
|
874
|
-
}
|
|
875
|
-
docObj = doc.toObject();
|
|
876
|
-
stream = (0, streams_1.resolveStreamForDoc)({
|
|
877
|
-
collectionTag: entry.collectionTag,
|
|
878
|
-
doc: docObj,
|
|
879
|
-
scope: entry.config.scope,
|
|
880
|
-
});
|
|
881
|
-
if (!isAdminWindow && !memberSet.has(stream)) {
|
|
882
|
-
return [3 /*break*/, 25];
|
|
883
|
-
}
|
|
884
|
-
isTombstone = Boolean(docObj.deleted);
|
|
885
|
-
_k = (_j = entities).push;
|
|
886
|
-
_o = {};
|
|
887
|
-
if (!isTombstone) return [3 /*break*/, 22];
|
|
888
|
-
_l = null;
|
|
889
|
-
return [3 /*break*/, 24];
|
|
890
|
-
case 22: return [4 /*yield*/, (0, exports.serializeSyncDoc)({ doc: doc, entry: entry, req: req })];
|
|
891
|
-
case 23:
|
|
892
|
-
_l = _r.sent();
|
|
893
|
-
_r.label = 24;
|
|
894
|
-
case 24:
|
|
895
|
-
_k.apply(_j, [(_o.data = _l,
|
|
896
|
-
_o.deleted = isTombstone,
|
|
897
|
-
_o.id = String(doc._id),
|
|
898
|
-
_o.seq = (_q = docObj._syncSeq) !== null && _q !== void 0 ? _q : 0,
|
|
899
|
-
_o)]);
|
|
900
|
-
_r.label = 25;
|
|
901
|
-
case 25:
|
|
902
|
-
_g = _f.next();
|
|
903
|
-
return [3 /*break*/, 17];
|
|
904
|
-
case 26: return [3 /*break*/, 29];
|
|
905
|
-
case 27:
|
|
906
|
-
e_5_1 = _r.sent();
|
|
907
|
-
e_5 = { error: e_5_1 };
|
|
908
|
-
return [3 /*break*/, 29];
|
|
909
|
-
case 28:
|
|
910
|
-
try {
|
|
911
|
-
if (_g && !_g.done && (_m = _f.return)) _m.call(_f);
|
|
912
|
-
}
|
|
913
|
-
finally { if (e_5) throw e_5.error; }
|
|
914
|
-
return [7 /*endfinally*/];
|
|
915
|
-
case 29:
|
|
916
|
-
response = { entities: entities };
|
|
917
|
-
return [2 /*return*/, res.json(response)];
|
|
365
|
+
catch (error) {
|
|
366
|
+
throw new errors_1.APIError({
|
|
367
|
+
status: 500,
|
|
368
|
+
title: `Failed to resolve streams for ${entry.collectionTag}: ${String(error)}`,
|
|
369
|
+
});
|
|
918
370
|
}
|
|
371
|
+
}
|
|
372
|
+
const response = { streams };
|
|
373
|
+
return res.json(response);
|
|
374
|
+
}));
|
|
375
|
+
router.get("/sync/snapshot", (0, auth_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(async (req, res) => {
|
|
376
|
+
const user = req.user;
|
|
377
|
+
if (!user) {
|
|
378
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
379
|
+
}
|
|
380
|
+
const streamKey = String(req.query.stream ?? "");
|
|
381
|
+
if (!streamKey) {
|
|
382
|
+
throw new errors_1.APIError({ status: 400, title: "stream query parameter is required" });
|
|
383
|
+
}
|
|
384
|
+
const parsed = (0, streams_1.parseStreamKey)(streamKey);
|
|
385
|
+
if (!parsed) {
|
|
386
|
+
throw new errors_1.APIError({ status: 400, title: `Invalid stream key: ${streamKey}` });
|
|
387
|
+
}
|
|
388
|
+
const entry = (0, registry_1.findSyncEntryByCollectionTag)(parsed.collectionTag);
|
|
389
|
+
if (!entry) {
|
|
390
|
+
throw new errors_1.APIError({
|
|
391
|
+
status: 404,
|
|
392
|
+
title: `Unknown sync collection: ${parsed.collectionTag}`,
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
if (!(await (0, permissions_1.checkPermissions)("list", entry.options.permissions.list, user))) {
|
|
396
|
+
throw new errors_1.APIError({
|
|
397
|
+
status: 403,
|
|
398
|
+
title: `Access to sync snapshot for ${parsed.collectionTag} denied for ${user.id}`,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
// C2: a client must not snapshot a stream it does not belong to.
|
|
402
|
+
const memberStreams = await (0, streams_1.resolveUserStreamsForEntry)({
|
|
403
|
+
entry,
|
|
404
|
+
getUserScopes: options.getUserScopes,
|
|
405
|
+
user,
|
|
919
406
|
});
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
return [2 /*return*/, res.status(NACK_HTTP_STATUS[outcome.nack.code]).json({ nack: outcome.nack })];
|
|
944
|
-
}
|
|
407
|
+
if (!memberStreams.includes(streamKey)) {
|
|
408
|
+
throw new errors_1.APIError({
|
|
409
|
+
status: 403,
|
|
410
|
+
title: `User ${user.id} does not belong to stream ${streamKey}`,
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
const cursor = parseNonNegativeInt(req.query.cursor, "cursor", 0);
|
|
414
|
+
const requestedLimit = parseNonNegativeInt(req.query.limit, "limit", options.defaultSnapshotLimit ?? DEFAULT_SNAPSHOT_LIMIT);
|
|
415
|
+
const limit = Math.min(Math.max(requestedLimit, 1), MAX_SNAPSHOT_LIMIT);
|
|
416
|
+
const legacyCursorIn = typeof req.query.legacyCursor === "string" && req.query.legacyCursor.length > 0
|
|
417
|
+
? req.query.legacyCursor
|
|
418
|
+
: undefined;
|
|
419
|
+
const snapshotFilterResult = entry.config.snapshotFilter
|
|
420
|
+
? await entry.config.snapshotFilter({ id: String(user.id) })
|
|
421
|
+
: undefined;
|
|
422
|
+
const queryFilterResult = await resolveSyncQueryFilter({ entry, user });
|
|
423
|
+
const scopeFilter = composeSyncReadFilter({
|
|
424
|
+
queryFilter: queryFilterResult.filter,
|
|
425
|
+
scopeFilter: (0, exports.buildSnapshotScopeFilter)({
|
|
426
|
+
entry,
|
|
427
|
+
scopeValue: parsed.scopeValue,
|
|
428
|
+
snapshotFilterResult,
|
|
429
|
+
}),
|
|
945
430
|
});
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
431
|
+
const model = getSyncedModel(entry.modelName);
|
|
432
|
+
const frontierSeq = await (0, models_1.computeStableFrontier)({ stream: streamKey });
|
|
433
|
+
// C7 (Task 9.15): the retention signal is the durable compaction watermark, not
|
|
434
|
+
// min(retained seq). The old computation was pinned low forever by any early doc that
|
|
435
|
+
// was never deleted, so a stale cursor sitting above a compacted tombstone's seq
|
|
436
|
+
// passed the check and silently never learned about that deletion.
|
|
437
|
+
const oldestRetainedSeq = await (0, models_1.getCompactedThroughSeq)({ stream: streamKey });
|
|
438
|
+
// Task 9.19: a `queryFilter` that denies this caller means "no visible rows", exactly
|
|
439
|
+
// as REST's list endpoint answers. Serve a terminal empty page (cursor at the
|
|
440
|
+
// frontier, hasMore false) so the client settles instead of paging forever.
|
|
441
|
+
if (queryFilterResult.denied) {
|
|
442
|
+
const deniedResponse = {
|
|
443
|
+
cursor: frontierSeq,
|
|
444
|
+
entities: [],
|
|
445
|
+
frontierSeq,
|
|
446
|
+
hasMore: false,
|
|
447
|
+
oldestRetainedSeq,
|
|
448
|
+
stream: streamKey,
|
|
449
|
+
};
|
|
450
|
+
return res.json(deniedResponse);
|
|
451
|
+
}
|
|
452
|
+
// C3: legacy (seq-0) stratum, paged by _id. Drained fully before seq paging begins.
|
|
453
|
+
if (cursor === 0) {
|
|
454
|
+
const legacyResult = await pageLegacyStratum({
|
|
455
|
+
entry,
|
|
456
|
+
legacyCursorIn,
|
|
457
|
+
limit,
|
|
458
|
+
model,
|
|
459
|
+
req,
|
|
460
|
+
scopeFilter,
|
|
461
|
+
});
|
|
462
|
+
if (legacyResult) {
|
|
463
|
+
const response = {
|
|
464
|
+
cursor: 0,
|
|
465
|
+
entities: legacyResult.entities,
|
|
466
|
+
frontierSeq,
|
|
467
|
+
hasMore: true,
|
|
468
|
+
legacyCursor: legacyResult.legacyCursor,
|
|
469
|
+
oldestRetainedSeq,
|
|
470
|
+
stream: streamKey,
|
|
471
|
+
};
|
|
472
|
+
return res.json(response);
|
|
974
473
|
}
|
|
474
|
+
}
|
|
475
|
+
const seqResult = await pageSeqStratum({
|
|
476
|
+
cursor,
|
|
477
|
+
entry,
|
|
478
|
+
frontierSeq,
|
|
479
|
+
limit,
|
|
480
|
+
model,
|
|
481
|
+
req,
|
|
482
|
+
scopeFilter,
|
|
483
|
+
streamKey,
|
|
484
|
+
});
|
|
485
|
+
const response = {
|
|
486
|
+
cursor: seqResult.cursor,
|
|
487
|
+
entities: seqResult.entities,
|
|
488
|
+
frontierSeq,
|
|
489
|
+
hasMore: seqResult.hasMore,
|
|
490
|
+
oldestRetainedSeq,
|
|
491
|
+
stream: streamKey,
|
|
492
|
+
};
|
|
493
|
+
return res.json(response);
|
|
494
|
+
}));
|
|
495
|
+
router.get("/sync/entities", (0, auth_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(async (req, res) => {
|
|
496
|
+
const user = req.user;
|
|
497
|
+
if (!user) {
|
|
498
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
499
|
+
}
|
|
500
|
+
const collection = String(req.query.collection ?? "").trim();
|
|
501
|
+
if (!collection) {
|
|
502
|
+
throw new errors_1.APIError({ status: 400, title: "collection query parameter is required" });
|
|
503
|
+
}
|
|
504
|
+
const idsParam = req.query.ids;
|
|
505
|
+
const ids = typeof idsParam === "string"
|
|
506
|
+
? idsParam
|
|
507
|
+
.split(",")
|
|
508
|
+
.map((id) => id.trim())
|
|
509
|
+
.filter((id) => id.length > 0)
|
|
510
|
+
: [];
|
|
511
|
+
if (ids.length === 0) {
|
|
512
|
+
throw new errors_1.APIError({ status: 400, title: "ids query parameter is required" });
|
|
513
|
+
}
|
|
514
|
+
// Task 9.27: this used to `.slice(0, MAX_ENTITY_FETCH)`, so an over-cap repair fetch
|
|
515
|
+
// came back 200 with a partial body and the client treated the entities it never
|
|
516
|
+
// received as absent. Reject instead, so the caller re-requests in chunks.
|
|
517
|
+
if (ids.length > exports.MAX_ENTITY_FETCH) {
|
|
518
|
+
throw new errors_1.APIError({
|
|
519
|
+
status: 400,
|
|
520
|
+
title: `Too many ids: ${ids.length} requested, limit is ${exports.MAX_ENTITY_FETCH}`,
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
const entry = (0, registry_1.findSyncEntryByCollectionTag)(collection);
|
|
524
|
+
if (!entry) {
|
|
525
|
+
throw new errors_1.APIError({
|
|
526
|
+
status: 404,
|
|
527
|
+
title: `Unknown sync collection: ${collection}`,
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
if (!(await (0, permissions_1.checkPermissions)("list", entry.options.permissions.list, user))) {
|
|
531
|
+
throw new errors_1.APIError({
|
|
532
|
+
status: 403,
|
|
533
|
+
title: `Access to sync entities for ${collection} denied for ${user.id}`,
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
const isAdminWindow = entry.config.adminBroadcast === true &&
|
|
537
|
+
(await (0, adminWindowAccess_1.canUseAdminBroadcastWindow)({
|
|
538
|
+
accessControl: options.accessControl,
|
|
539
|
+
canOpenAdminWindow: options.canOpenAdminWindow,
|
|
540
|
+
user,
|
|
541
|
+
}));
|
|
542
|
+
const adminScope = isAdminWindow ? (0, adminBroadcastScope_1.getAdminBroadcastScope)(entry.modelName) : undefined;
|
|
543
|
+
if (adminScope && !(await (0, adminBroadcastScope_1.canListAdminBroadcastScope)({ scope: adminScope, user }))) {
|
|
544
|
+
const deniedResponse = { entities: [] };
|
|
545
|
+
return res.json(deniedResponse);
|
|
546
|
+
}
|
|
547
|
+
const memberStreams = isAdminWindow
|
|
548
|
+
? []
|
|
549
|
+
: await (0, streams_1.resolveUserStreamsForEntry)({
|
|
550
|
+
entry,
|
|
551
|
+
getUserScopes: options.getUserScopes,
|
|
552
|
+
user,
|
|
553
|
+
});
|
|
554
|
+
const memberSet = new Set(memberStreams);
|
|
555
|
+
// Task 9.19: mirror the REST list endpoint's row-level scoping. Without this, a
|
|
556
|
+
// collection that relies on `queryFilter` (rather than a per-doc read permission)
|
|
557
|
+
// served every requested id here, whatever the caller was allowed to list.
|
|
558
|
+
// Admin window hydrate skips the *product* filter so REST-selected ids can load
|
|
559
|
+
// across owners, then applies AdminApp list/read/`queryFilter` when registered.
|
|
560
|
+
const queryFilterResult = adminScope
|
|
561
|
+
? await (0, adminBroadcastScope_1.resolveAdminBroadcastQueryFilter)({ scope: adminScope, user })
|
|
562
|
+
: isAdminWindow
|
|
563
|
+
? { denied: false, filter: undefined }
|
|
564
|
+
: await resolveSyncQueryFilter({ entry, user });
|
|
565
|
+
if (queryFilterResult.denied) {
|
|
566
|
+
const deniedResponse = { entities: [] };
|
|
567
|
+
return res.json(deniedResponse);
|
|
568
|
+
}
|
|
569
|
+
const model = getSyncedModel(entry.modelName);
|
|
570
|
+
const castIds = castEntityIds({ ids, model });
|
|
571
|
+
const docs = await model.find({
|
|
572
|
+
// `deleted` MUST stay a TOP-LEVEL key so isDeletedPlugin does not re-inject its
|
|
573
|
+
// {deleted: {$ne: true}} exclusion and hide the tombstones a re-fetching client needs.
|
|
574
|
+
...composeSyncReadFilter({
|
|
575
|
+
queryFilter: queryFilterResult.filter,
|
|
576
|
+
scopeFilter: { _id: { $in: castIds } },
|
|
577
|
+
}),
|
|
578
|
+
deleted: { $in: [true, false] },
|
|
975
579
|
});
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
580
|
+
const entities = [];
|
|
581
|
+
for (const doc of docs) {
|
|
582
|
+
const allowed = adminScope
|
|
583
|
+
? await (0, permissions_1.checkPermissions)("read", adminScope.readPermissions, user, doc)
|
|
584
|
+
: await (0, permissions_1.checkPermissions)("read", entry.options.permissions.read, user, doc);
|
|
585
|
+
if (!allowed) {
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
const docObj = doc.toObject();
|
|
589
|
+
const stream = (0, streams_1.resolveStreamForDoc)({
|
|
590
|
+
collectionTag: entry.collectionTag,
|
|
591
|
+
doc: docObj,
|
|
592
|
+
scope: entry.config.scope,
|
|
593
|
+
});
|
|
594
|
+
if (!isAdminWindow && !memberSet.has(stream)) {
|
|
595
|
+
continue;
|
|
990
596
|
}
|
|
597
|
+
const isTombstone = Boolean(docObj.deleted);
|
|
598
|
+
entities.push({
|
|
599
|
+
data: isTombstone ? null : await (0, exports.serializeSyncDoc)({ doc, entry, req }),
|
|
600
|
+
deleted: isTombstone,
|
|
601
|
+
id: String(doc._id),
|
|
602
|
+
seq: docObj._syncSeq ?? 0,
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
const response = { entities };
|
|
606
|
+
return res.json(response);
|
|
607
|
+
}));
|
|
608
|
+
router.post("/sync/mutate", (0, auth_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(async (req, res) => {
|
|
609
|
+
const user = req.user;
|
|
610
|
+
if (!user) {
|
|
611
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
612
|
+
}
|
|
613
|
+
// Task 9.20: rate limiting + apply live in the shared runner, so this route and
|
|
614
|
+
// `sync:mutate` behave identically under the same inputs.
|
|
615
|
+
const { outcome } = await (0, syncBatch_1.runSyncMutation)({
|
|
616
|
+
mutation: req.body,
|
|
617
|
+
req,
|
|
618
|
+
scopeResolver: options.getUserScopes,
|
|
619
|
+
syncOptions: options,
|
|
620
|
+
user,
|
|
991
621
|
});
|
|
992
|
-
|
|
622
|
+
if (outcome.type === "ack") {
|
|
623
|
+
return res.json({ ack: outcome.ack });
|
|
624
|
+
}
|
|
625
|
+
// Duplicate deliveries reading a recorded outcome map to the same statuses.
|
|
626
|
+
return res.status(NACK_HTTP_STATUS[outcome.nack.code]).json({ nack: outcome.nack });
|
|
627
|
+
}));
|
|
628
|
+
router.post("/sync/mutate/batch", (0, auth_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(async (req, res) => {
|
|
629
|
+
const user = req.user;
|
|
630
|
+
if (!user) {
|
|
631
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
632
|
+
}
|
|
633
|
+
const body = req.body;
|
|
634
|
+
const mutations = Array.isArray(body?.mutations) ? body.mutations : [];
|
|
635
|
+
// Task 9.20: validate -> rate limit -> apply, shared with `sync:mutateBatch`. An
|
|
636
|
+
// invalid batch (oversized, duplicate ids) is rejected before any budget is charged.
|
|
637
|
+
const { response, stage } = await (0, syncBatch_1.runSyncBatch)({
|
|
638
|
+
mutations,
|
|
639
|
+
req,
|
|
640
|
+
scopeResolver: options.getUserScopes,
|
|
641
|
+
syncOptions: options,
|
|
642
|
+
user,
|
|
643
|
+
});
|
|
644
|
+
if (stage === "validation") {
|
|
645
|
+
return res.status(422).json(response);
|
|
646
|
+
}
|
|
647
|
+
if (stage === "rate_limited") {
|
|
648
|
+
return res.status(NACK_HTTP_STATUS.rate_limited).json(response);
|
|
649
|
+
}
|
|
650
|
+
return res.json(response);
|
|
651
|
+
}));
|
|
652
|
+
router.get("/sync/key", (0, auth_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(async (req, res) => {
|
|
653
|
+
const user = req.user;
|
|
654
|
+
if (!user) {
|
|
655
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
656
|
+
}
|
|
657
|
+
const keyMaterial = await (0, models_1.getOrCreateSyncKeyMaterial)({ userId: String(user.id) });
|
|
658
|
+
return res.json({ keyMaterial });
|
|
659
|
+
}));
|
|
993
660
|
// Authenticate middleware rejects with Error("Unauthorized"); convert that to
|
|
994
661
|
// 401 before the generic error handler (and before Express's default 500).
|
|
995
662
|
router.use(errors_1.apiUnauthorizedMiddleware);
|