@terreno/api 0.31.0 → 56.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +122 -2
- package/LICENSE +202 -21
- package/dist/api.d.ts +12 -3
- package/dist/api.js +110 -361
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +100 -1
- package/dist/betterAuth.d.ts +5 -0
- package/dist/betterAuthSetup.d.ts +1 -15
- package/dist/betterAuthSetup.js +29 -8
- package/dist/configurationPlugin.js +2 -2
- package/dist/docLoader.js +12 -9
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +29 -1
- package/dist/expressServer.js +5 -11
- package/dist/githubAuth.js +4 -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 +15 -3
- package/dist/plugins.js +36 -6
- package/dist/realtime/changeStreamWatcher.d.ts +100 -3
- package/dist/realtime/changeStreamWatcher.js +542 -137
- package/dist/realtime/realtimeApp.d.ts +3 -1
- package/dist/realtime/realtimeApp.js +75 -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 +40 -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 +903 -0
- package/dist/sync/types.d.ts +254 -0
- package/dist/sync/types.js +9 -0
- package/dist/terrenoApp.js +90 -27
- package/dist/tests/models.d.ts +11 -27
- package/dist/tests/models.js +3 -5
- package/dist/tests/testHelper.d.ts +3 -9
- package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
- package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
- package/package.json +10 -8
- package/src/__tests__/openApiCompat.test.ts +20 -0
- package/src/actions.test.ts +12 -19
- package/src/actions.ts +2 -2
- package/src/api.coverage.test.ts +9 -7
- package/src/api.errorPaths.test.ts +1 -47
- package/src/api.test.ts +12 -0
- package/src/api.ts +98 -317
- package/src/auth.test.ts +128 -176
- package/src/auth.ts +109 -0
- package/src/betterAuth.ts +6 -0
- package/src/betterAuthSetup.test.ts +34 -0
- package/src/betterAuthSetup.ts +27 -8
- package/src/configurationPlugin.ts +2 -2
- package/src/consentApp.test.ts +12 -12
- package/src/docLoader.ts +3 -1
- package/src/errors.ts +33 -0
- package/src/expressServer.test.ts +7 -1
- package/src/expressServer.ts +5 -11
- package/src/githubAuth.ts +3 -1
- package/src/index.ts +42 -0
- package/src/middleware.test.ts +40 -1
- package/src/middleware.ts +15 -0
- package/src/models/versionConfig.ts +1 -1
- package/src/openApiBuilder.test.ts +9 -24
- package/src/openApiCompat.ts +99 -45
- package/src/openApiValidator.test.ts +0 -41
- package/src/permissions.test.ts +75 -1
- package/src/permissions.ts +49 -0
- package/src/plugins.test.ts +54 -14
- package/src/plugins.ts +47 -9
- package/src/populate.test.ts +16 -25
- package/src/realtime/changeStreamWatcher.ts +552 -114
- package/src/realtime/realtime.test.ts +157 -239
- package/src/realtime/realtimeApp.ts +80 -19
- 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 +40 -0
- package/src/secretProviders.test.ts +7 -1
- package/src/secretProviders.ts +15 -3
- package/src/sync/ensureSyncIndexes.test.ts +144 -0
- package/src/sync/executors.test.ts +882 -0
- package/src/sync/executors.ts +703 -0
- package/src/sync/integration.test.ts +1160 -0
- package/src/sync/loadHarness.ts +618 -0
- package/src/sync/models.ts +640 -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 +738 -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/src/tests/models.ts +6 -8
- 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/src/realtime/realtimeAppSetup.test.ts +0 -161
- /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
|
@@ -0,0 +1,936 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
86
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
87
|
+
};
|
|
88
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
89
|
+
exports.addSyncRoutes = exports.MAX_ENTITY_FETCH = exports.buildSnapshotScopeFilter = exports.serializeSyncDoc = exports.MAX_SYNC_HTTP_MUTATIONS_PER_SECOND = void 0;
|
|
90
|
+
// noExplicitAny: sync routes operate generically across registered models
|
|
91
|
+
// biome-ignore-all lint/suspicious/noExplicitAny: sync routes operate generically across registered models
|
|
92
|
+
var express_1 = __importDefault(require("express"));
|
|
93
|
+
var mongoose_1 = __importDefault(require("mongoose"));
|
|
94
|
+
var api_1 = require("../api");
|
|
95
|
+
var auth_1 = require("../auth");
|
|
96
|
+
var errors_1 = require("../errors");
|
|
97
|
+
var logger_1 = require("../logger");
|
|
98
|
+
var permissions_1 = require("../permissions");
|
|
99
|
+
var models_1 = require("./models");
|
|
100
|
+
var registry_1 = require("./registry");
|
|
101
|
+
var serialize_1 = require("./serialize");
|
|
102
|
+
var streams_1 = require("./streams");
|
|
103
|
+
var syncBatch_1 = require("./syncBatch");
|
|
104
|
+
/**
|
|
105
|
+
* Maximum mutations per user per second on the HTTP mutate routes.
|
|
106
|
+
*
|
|
107
|
+
* Task 9.20: an alias for the shared budget — HTTP and socket mutations now draw from ONE
|
|
108
|
+
* per-user window (see `syncBatch.ts`), so there is no longer a separate HTTP allowance.
|
|
109
|
+
* Re-exported (rather than copied) so the two names can never drift; a live re-export also
|
|
110
|
+
* keeps this module's evaluation independent of `syncBatch`, which imports back into it.
|
|
111
|
+
*/
|
|
112
|
+
var syncBatch_2 = require("./syncBatch");
|
|
113
|
+
Object.defineProperty(exports, "MAX_SYNC_HTTP_MUTATIONS_PER_SECOND", { enumerable: true, get: function () { return syncBatch_2.MAX_SYNC_MUTATIONS_PER_SECOND; } });
|
|
114
|
+
var MAX_SNAPSHOT_LIMIT = 100;
|
|
115
|
+
var DEFAULT_SNAPSHOT_LIMIT = 100;
|
|
116
|
+
/** HTTP status for each nack code returned by `POST /sync/mutate`. */
|
|
117
|
+
var NACK_HTTP_STATUS = {
|
|
118
|
+
conflict: 409,
|
|
119
|
+
error: 500,
|
|
120
|
+
rate_limited: 429,
|
|
121
|
+
unauthorized: 403,
|
|
122
|
+
validation: 422,
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Serialize a document for a sync payload through the fallback chain:
|
|
126
|
+
* sync responseHandler > modelRouter responseHandler > toJSON.
|
|
127
|
+
* Delegates to the shared `serializeSyncPayload` (also used for `sync:delta` emission).
|
|
128
|
+
*/
|
|
129
|
+
var serializeSyncDoc = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
130
|
+
var entry = _b.entry, doc = _b.doc, req = _b.req;
|
|
131
|
+
return __generator(this, function (_c) {
|
|
132
|
+
return [2 /*return*/, (0, serialize_1.serializeSyncPayload)({ doc: doc, entry: entry, req: req })];
|
|
133
|
+
});
|
|
134
|
+
}); };
|
|
135
|
+
exports.serializeSyncDoc = serializeSyncDoc;
|
|
136
|
+
/**
|
|
137
|
+
* C2: build the server-enforced scope filter for a SINGLE stream. The stream's scope
|
|
138
|
+
* value has already been verified against the user's membership set by the caller, so
|
|
139
|
+
* this filters to exactly that one value (`{field: value}`), never an `$in`.
|
|
140
|
+
*
|
|
141
|
+
* Custom-resolver scopes cannot be inverted into a query field, so they still route
|
|
142
|
+
* through the required `snapshotFilter` (parameterized by the user, as before).
|
|
143
|
+
*
|
|
144
|
+
* Task 9.19: for owner/tenant/broadcast scopes a consumer-supplied `snapshotFilter` used
|
|
145
|
+
* to be computed and then discarded — silently widening the snapshot past what the
|
|
146
|
+
* consumer asked for. It is now composed with the scope clause via `$and` (never
|
|
147
|
+
* spread-merged, which would let one clobber the other).
|
|
148
|
+
*/
|
|
149
|
+
var buildSnapshotScopeFilter = function (_a) {
|
|
150
|
+
var _b;
|
|
151
|
+
var entry = _a.entry, scopeValue = _a.scopeValue, snapshotFilterResult = _a.snapshotFilterResult;
|
|
152
|
+
var scope = entry.config.scope;
|
|
153
|
+
if (typeof scope === "function") {
|
|
154
|
+
// Custom scope: snapshotFilter is required at registration.
|
|
155
|
+
if (!snapshotFilterResult) {
|
|
156
|
+
throw new errors_1.APIError({
|
|
157
|
+
status: 500,
|
|
158
|
+
title: "Sync collection ".concat(entry.collectionTag, " has a custom scope without a snapshotFilter"),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return snapshotFilterResult;
|
|
162
|
+
}
|
|
163
|
+
var scopeClause = scope.type === "broadcast" ? {} : (_b = {}, _b[(0, streams_1.getScopeField)(scope)] = scopeValue, _b);
|
|
164
|
+
if (!snapshotFilterResult) {
|
|
165
|
+
return scopeClause;
|
|
166
|
+
}
|
|
167
|
+
return { $and: [scopeClause, snapshotFilterResult] };
|
|
168
|
+
};
|
|
169
|
+
exports.buildSnapshotScopeFilter = buildSnapshotScopeFilter;
|
|
170
|
+
/**
|
|
171
|
+
* Task 9.19: resolve the modelRouter `queryFilter` for a sync read.
|
|
172
|
+
*
|
|
173
|
+
* The most common REST shape is `read: [IsAuthenticated]` plus a `queryFilter` (e.g.
|
|
174
|
+
* `OwnerQueryFilter`) doing the actual row-level scoping. Sync built only scope + seq
|
|
175
|
+
* clauses, so for a `broadcast`/`custom`-scoped collection every document in the stream
|
|
176
|
+
* leaked through the snapshot and `GET /sync/entities` even though the REST list endpoint
|
|
177
|
+
* hid it. Both read paths now `$and` the resolved filter into their query, giving sync and
|
|
178
|
+
* REST the same visible row set.
|
|
179
|
+
*
|
|
180
|
+
* `queryFilter` is called with an empty query object: a sync read carries no user-supplied
|
|
181
|
+
* query params, so there is nothing to validate — only the filter clause to apply.
|
|
182
|
+
* Returning `null` means "this caller may see nothing" (REST answers with an empty list),
|
|
183
|
+
* which sync mirrors by serving an empty page rather than erroring. A throwing filter is
|
|
184
|
+
* denied the same way the realtime path denies it: fail closed, never fall through open.
|
|
185
|
+
*/
|
|
186
|
+
var resolveSyncQueryFilter = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
187
|
+
var resolved, error_1;
|
|
188
|
+
var entry = _b.entry, user = _b.user;
|
|
189
|
+
return __generator(this, function (_c) {
|
|
190
|
+
switch (_c.label) {
|
|
191
|
+
case 0:
|
|
192
|
+
if (!entry.options.queryFilter) {
|
|
193
|
+
return [2 /*return*/, { denied: false }];
|
|
194
|
+
}
|
|
195
|
+
_c.label = 1;
|
|
196
|
+
case 1:
|
|
197
|
+
_c.trys.push([1, 3, , 4]);
|
|
198
|
+
return [4 /*yield*/, entry.options.queryFilter(user, {})];
|
|
199
|
+
case 2:
|
|
200
|
+
resolved = _c.sent();
|
|
201
|
+
return [3 /*break*/, 4];
|
|
202
|
+
case 3:
|
|
203
|
+
error_1 = _c.sent();
|
|
204
|
+
logger_1.logger.error("[sync] queryFilter threw for a sync read; denying the read", {
|
|
205
|
+
collection: entry.collectionTag,
|
|
206
|
+
error: String(error_1),
|
|
207
|
+
userId: String(user.id),
|
|
208
|
+
});
|
|
209
|
+
return [2 /*return*/, { denied: true }];
|
|
210
|
+
case 4:
|
|
211
|
+
if (resolved === null) {
|
|
212
|
+
return [2 /*return*/, { denied: true }];
|
|
213
|
+
}
|
|
214
|
+
return [2 /*return*/, { denied: false, filter: Object.keys(resolved).length > 0 ? resolved : undefined }];
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}); };
|
|
218
|
+
/**
|
|
219
|
+
* `$and` the resolved `queryFilter` onto a sync read's scope filter. Spread-merging would
|
|
220
|
+
* let either side clobber a same-named key (e.g. both constraining `ownerId`), so both
|
|
221
|
+
* clauses are kept and both must match.
|
|
222
|
+
*/
|
|
223
|
+
var composeSyncReadFilter = function (_a) {
|
|
224
|
+
var queryFilter = _a.queryFilter, scopeFilter = _a.scopeFilter;
|
|
225
|
+
if (!queryFilter) {
|
|
226
|
+
return scopeFilter;
|
|
227
|
+
}
|
|
228
|
+
if (Object.keys(scopeFilter).length === 0) {
|
|
229
|
+
return queryFilter;
|
|
230
|
+
}
|
|
231
|
+
return { $and: [scopeFilter, queryFilter] };
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* Task 9.27: parse strictly. `Number.parseInt` stops at the first non-digit, so `"12abc"`
|
|
235
|
+
* and `"1e9"` used to silently become 12 and 1 — a client with a malformed cursor got a
|
|
236
|
+
* plausible-looking page instead of an error, and resumed paging from the wrong seq.
|
|
237
|
+
*/
|
|
238
|
+
var parseNonNegativeInt = function (raw, name, fallback) {
|
|
239
|
+
if (raw === undefined) {
|
|
240
|
+
return fallback;
|
|
241
|
+
}
|
|
242
|
+
var text = String(raw).trim();
|
|
243
|
+
if (!/^\d+$/.test(text)) {
|
|
244
|
+
throw new errors_1.APIError({ status: 400, title: "Invalid ".concat(name, ": ").concat(String(raw)) });
|
|
245
|
+
}
|
|
246
|
+
var value = Number.parseInt(text, 10);
|
|
247
|
+
if (!Number.isSafeInteger(value)) {
|
|
248
|
+
throw new errors_1.APIError({ status: 400, title: "Invalid ".concat(name, ": ").concat(String(raw)) });
|
|
249
|
+
}
|
|
250
|
+
return value;
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* Task 9.14: the legacy cursor is a raw `_id` string, so it must be compared in the
|
|
254
|
+
* model's OWN `_id` BSON type. Synced models are designed around client-minted string
|
|
255
|
+
* `_id`s: casting those to `ObjectId` either throws (non-hex ids) or — worse — silently
|
|
256
|
+
* compares across BSON types and matches nothing, so the legacy stratum reported itself
|
|
257
|
+
* exhausted after one page and the remaining documents were never bootstrapped.
|
|
258
|
+
* Only ObjectId-keyed models get the cast; a malformed cursor for one is a 400, not a 500.
|
|
259
|
+
*/
|
|
260
|
+
var legacyCursorClause = function (_a) {
|
|
261
|
+
var _b, _c;
|
|
262
|
+
var model = _a.model, legacyCursorIn = _a.legacyCursorIn;
|
|
263
|
+
var idInstance = (_c = (_b = model.schema) === null || _b === void 0 ? void 0 : _b.path("_id")) === null || _c === void 0 ? void 0 : _c.instance;
|
|
264
|
+
if (idInstance !== "ObjectId") {
|
|
265
|
+
return { _id: { $gt: legacyCursorIn } };
|
|
266
|
+
}
|
|
267
|
+
if (!mongoose_1.default.isValidObjectId(legacyCursorIn)) {
|
|
268
|
+
throw new errors_1.APIError({ status: 400, title: "Invalid legacyCursor: ".concat(legacyCursorIn) });
|
|
269
|
+
}
|
|
270
|
+
return { _id: { $gt: new mongoose_1.default.Types.ObjectId(legacyCursorIn) } };
|
|
271
|
+
};
|
|
272
|
+
/**
|
|
273
|
+
* C3: page the legacy (seq-0) stratum by `_id`. Legacy documents predate `syncPlugin`
|
|
274
|
+
* and carry no `_syncSeq` (or a literal 0). Returns a page + a forward `legacyCursor`
|
|
275
|
+
* while the stratum has more; returns `undefined` once the stratum is exhausted, at
|
|
276
|
+
* which point the caller switches to normal seq paging. Runs the same per-doc read
|
|
277
|
+
* check as the seq page (C6/M2).
|
|
278
|
+
*/
|
|
279
|
+
var pageLegacyStratum = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
280
|
+
var user, legacyFilter, docs, page, entities, page_1, page_1_1, doc, _c, _d, e_1_1, lastId;
|
|
281
|
+
var e_1, _e, _f;
|
|
282
|
+
var model = _b.model, scopeFilter = _b.scopeFilter, legacyCursorIn = _b.legacyCursorIn, limit = _b.limit, entry = _b.entry, req = _b.req;
|
|
283
|
+
return __generator(this, function (_g) {
|
|
284
|
+
switch (_g.label) {
|
|
285
|
+
case 0:
|
|
286
|
+
user = req.user;
|
|
287
|
+
legacyFilter = {
|
|
288
|
+
$and: __spreadArray([
|
|
289
|
+
scopeFilter,
|
|
290
|
+
{ _syncSeq: { $in: [null, 0] } }
|
|
291
|
+
], __read((legacyCursorIn ? [legacyCursorClause({ legacyCursorIn: legacyCursorIn, model: model })] : [])), false),
|
|
292
|
+
deleted: { $in: [true, false] },
|
|
293
|
+
};
|
|
294
|
+
return [4 /*yield*/, model
|
|
295
|
+
.find(legacyFilter)
|
|
296
|
+
.sort({ _id: 1 })
|
|
297
|
+
.limit(limit + 1)];
|
|
298
|
+
case 1:
|
|
299
|
+
docs = _g.sent();
|
|
300
|
+
if (docs.length === 0) {
|
|
301
|
+
// Stratum exhausted (or never had legacy docs) — caller proceeds by seq.
|
|
302
|
+
return [2 /*return*/, undefined];
|
|
303
|
+
}
|
|
304
|
+
page = docs.slice(0, limit);
|
|
305
|
+
entities = [];
|
|
306
|
+
_g.label = 2;
|
|
307
|
+
case 2:
|
|
308
|
+
_g.trys.push([2, 8, 9, 10]);
|
|
309
|
+
page_1 = __values(page), page_1_1 = page_1.next();
|
|
310
|
+
_g.label = 3;
|
|
311
|
+
case 3:
|
|
312
|
+
if (!!page_1_1.done) return [3 /*break*/, 7];
|
|
313
|
+
doc = page_1_1.value;
|
|
314
|
+
return [4 /*yield*/, (0, permissions_1.checkPermissions)("read", entry.options.permissions.read, user, doc)];
|
|
315
|
+
case 4:
|
|
316
|
+
if (!(_g.sent())) {
|
|
317
|
+
return [3 /*break*/, 6];
|
|
318
|
+
}
|
|
319
|
+
_d = (_c = entities).push;
|
|
320
|
+
_f = {};
|
|
321
|
+
return [4 /*yield*/, (0, exports.serializeSyncDoc)({ doc: doc, entry: entry, req: req })];
|
|
322
|
+
case 5:
|
|
323
|
+
_d.apply(_c, [(_f.data = _g.sent(),
|
|
324
|
+
_f.deleted = Boolean(doc.deleted),
|
|
325
|
+
_f.id = String(doc._id),
|
|
326
|
+
_f.seq = 0,
|
|
327
|
+
_f)]);
|
|
328
|
+
_g.label = 6;
|
|
329
|
+
case 6:
|
|
330
|
+
page_1_1 = page_1.next();
|
|
331
|
+
return [3 /*break*/, 3];
|
|
332
|
+
case 7: return [3 /*break*/, 10];
|
|
333
|
+
case 8:
|
|
334
|
+
e_1_1 = _g.sent();
|
|
335
|
+
e_1 = { error: e_1_1 };
|
|
336
|
+
return [3 /*break*/, 10];
|
|
337
|
+
case 9:
|
|
338
|
+
try {
|
|
339
|
+
if (page_1_1 && !page_1_1.done && (_e = page_1.return)) _e.call(page_1);
|
|
340
|
+
}
|
|
341
|
+
finally { if (e_1) throw e_1.error; }
|
|
342
|
+
return [7 /*endfinally*/];
|
|
343
|
+
case 10:
|
|
344
|
+
lastId = String(page[page.length - 1]._id);
|
|
345
|
+
return [2 /*return*/, { entities: entities, legacyCursor: lastId }];
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
}); };
|
|
349
|
+
/**
|
|
350
|
+
* C1: true when the stream's head (highest claimed seq) exceeds the stable frontier —
|
|
351
|
+
* i.e. committed seqs are still coming once the in-flight writes below the frontier land.
|
|
352
|
+
*/
|
|
353
|
+
var frontierBelowStreamHead = function (streamKey, frontierSeq) { return __awaiter(void 0, void 0, void 0, function () {
|
|
354
|
+
var counter, head;
|
|
355
|
+
var _a;
|
|
356
|
+
return __generator(this, function (_b) {
|
|
357
|
+
switch (_b.label) {
|
|
358
|
+
case 0: return [4 /*yield*/, models_1.SyncCounter.findOne({ stream: streamKey }).select({ seq: 1 }).lean()];
|
|
359
|
+
case 1:
|
|
360
|
+
counter = _b.sent();
|
|
361
|
+
head = counter ? ((_a = counter.seq) !== null && _a !== void 0 ? _a : 0) : 0;
|
|
362
|
+
return [2 /*return*/, head > frontierSeq];
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
}); };
|
|
366
|
+
/**
|
|
367
|
+
* Page the normal seq stratum: documents with `_syncSeq` in `(cursor, frontierSeq]`, unioned
|
|
368
|
+
* with the `SyncScopeMove` tombstones for the same seq range (C4). Mirrors
|
|
369
|
+
* `pageLegacyStratum` as the other half of the snapshot's paging: the caller drains the
|
|
370
|
+
* legacy stratum first, then hands every subsequent page to this helper.
|
|
371
|
+
*
|
|
372
|
+
* Returns the page's entities plus the cursor to resume from and whether more remains.
|
|
373
|
+
*/
|
|
374
|
+
var pageSeqStratum = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
375
|
+
var user, seqFilter, query, docs, markers, page, markerPage, docsHaveMore, markersHaveMore, lastDocSeq, lastMarkerSeq, coveredSeq, docEntities, _c, _d, doc, allowed, isTombstone, _e, _f, _g, e_2_1, markerEntities, merged, entities, entitiesTruncated, frontierBelowHead, hasMore, advanceTo;
|
|
376
|
+
var e_2, _h, _j;
|
|
377
|
+
var _k, _l;
|
|
378
|
+
var model = _b.model, scopeFilter = _b.scopeFilter, cursor = _b.cursor, limit = _b.limit, frontierSeq = _b.frontierSeq, streamKey = _b.streamKey, entry = _b.entry, req = _b.req;
|
|
379
|
+
return __generator(this, function (_m) {
|
|
380
|
+
switch (_m.label) {
|
|
381
|
+
case 0:
|
|
382
|
+
user = req.user;
|
|
383
|
+
seqFilter = { _syncSeq: { $gt: cursor, $lte: frontierSeq } };
|
|
384
|
+
query = { $and: [scopeFilter, seqFilter], deleted: { $in: [true, false] } };
|
|
385
|
+
return [4 /*yield*/, model
|
|
386
|
+
.find(query)
|
|
387
|
+
.sort({ _syncSeq: 1 })
|
|
388
|
+
.limit(limit + 1)];
|
|
389
|
+
case 1:
|
|
390
|
+
docs = _m.sent();
|
|
391
|
+
return [4 /*yield*/, models_1.SyncScopeMove.find({
|
|
392
|
+
fromStream: streamKey,
|
|
393
|
+
seq: { $gt: cursor, $lte: frontierSeq },
|
|
394
|
+
})
|
|
395
|
+
.sort({ seq: 1 })
|
|
396
|
+
.limit(limit + 1)
|
|
397
|
+
.lean()];
|
|
398
|
+
case 2:
|
|
399
|
+
markers = _m.sent();
|
|
400
|
+
page = docs.slice(0, limit);
|
|
401
|
+
markerPage = markers.slice(0, limit);
|
|
402
|
+
docsHaveMore = docs.length > limit;
|
|
403
|
+
markersHaveMore = markers.length > limit;
|
|
404
|
+
lastDocSeq = page.length > 0 ? ((_k = page[page.length - 1]._syncSeq) !== null && _k !== void 0 ? _k : 0) : 0;
|
|
405
|
+
lastMarkerSeq = markerPage.length > 0 ? markerPage[markerPage.length - 1].seq : 0;
|
|
406
|
+
coveredSeq = Math.min(docsHaveMore ? lastDocSeq : frontierSeq, markersHaveMore ? lastMarkerSeq : frontierSeq);
|
|
407
|
+
docEntities = [];
|
|
408
|
+
_m.label = 3;
|
|
409
|
+
case 3:
|
|
410
|
+
_m.trys.push([3, 11, 12, 13]);
|
|
411
|
+
_c = __values(page), _d = _c.next();
|
|
412
|
+
_m.label = 4;
|
|
413
|
+
case 4:
|
|
414
|
+
if (!!_d.done) return [3 /*break*/, 10];
|
|
415
|
+
doc = _d.value;
|
|
416
|
+
return [4 /*yield*/, (0, permissions_1.checkPermissions)("read", entry.options.permissions.read, user, doc)];
|
|
417
|
+
case 5:
|
|
418
|
+
allowed = _m.sent();
|
|
419
|
+
if (!allowed) {
|
|
420
|
+
return [3 /*break*/, 9];
|
|
421
|
+
}
|
|
422
|
+
isTombstone = Boolean(doc.deleted);
|
|
423
|
+
_f = (_e = docEntities).push;
|
|
424
|
+
_j = {};
|
|
425
|
+
if (!isTombstone) return [3 /*break*/, 6];
|
|
426
|
+
_g = null;
|
|
427
|
+
return [3 /*break*/, 8];
|
|
428
|
+
case 6: return [4 /*yield*/, (0, exports.serializeSyncDoc)({ doc: doc, entry: entry, req: req })];
|
|
429
|
+
case 7:
|
|
430
|
+
_g = _m.sent();
|
|
431
|
+
_m.label = 8;
|
|
432
|
+
case 8:
|
|
433
|
+
_f.apply(_e, [(
|
|
434
|
+
// C7: tombstones carry no data (privacy + payload growth) — only id/seq/deleted.
|
|
435
|
+
_j.data = _g,
|
|
436
|
+
_j.deleted = isTombstone,
|
|
437
|
+
_j.id = String(doc._id),
|
|
438
|
+
_j.seq = (_l = doc._syncSeq) !== null && _l !== void 0 ? _l : 0,
|
|
439
|
+
_j)]);
|
|
440
|
+
_m.label = 9;
|
|
441
|
+
case 9:
|
|
442
|
+
_d = _c.next();
|
|
443
|
+
return [3 /*break*/, 4];
|
|
444
|
+
case 10: return [3 /*break*/, 13];
|
|
445
|
+
case 11:
|
|
446
|
+
e_2_1 = _m.sent();
|
|
447
|
+
e_2 = { error: e_2_1 };
|
|
448
|
+
return [3 /*break*/, 13];
|
|
449
|
+
case 12:
|
|
450
|
+
try {
|
|
451
|
+
if (_d && !_d.done && (_h = _c.return)) _h.call(_c);
|
|
452
|
+
}
|
|
453
|
+
finally { if (e_2) throw e_2.error; }
|
|
454
|
+
return [7 /*endfinally*/];
|
|
455
|
+
case 13:
|
|
456
|
+
markerEntities = markerPage.map(function (m) { return ({
|
|
457
|
+
data: null,
|
|
458
|
+
deleted: true,
|
|
459
|
+
id: m.entityId,
|
|
460
|
+
seq: m.seq,
|
|
461
|
+
}); });
|
|
462
|
+
merged = __spreadArray(__spreadArray([], __read(docEntities), false), __read(markerEntities), false).sort(function (a, b) { return a.seq - b.seq; })
|
|
463
|
+
.filter(function (entity) { return entity.seq <= coveredSeq; });
|
|
464
|
+
entities = merged.slice(0, limit);
|
|
465
|
+
entitiesTruncated = merged.length > entities.length;
|
|
466
|
+
return [4 /*yield*/, frontierBelowStreamHead(streamKey, frontierSeq)];
|
|
467
|
+
case 14:
|
|
468
|
+
frontierBelowHead = _m.sent();
|
|
469
|
+
hasMore = docsHaveMore || markersHaveMore || entitiesTruncated || frontierBelowHead;
|
|
470
|
+
advanceTo = entitiesTruncated ? entities[entities.length - 1].seq : coveredSeq;
|
|
471
|
+
return [2 /*return*/, { cursor: Math.max(cursor, Math.min(advanceTo, frontierSeq)), entities: entities, hasMore: hasMore }];
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
}); };
|
|
475
|
+
/** Hard cap on ids per `GET /sync/entities` request. */
|
|
476
|
+
exports.MAX_ENTITY_FETCH = 100;
|
|
477
|
+
/**
|
|
478
|
+
* Task 9.27: `_id: {$in: ids}` leaves the cast to Mongoose, so one non-hex id against an
|
|
479
|
+
* ObjectId-keyed model threw a CastError that surfaced as a 500 for what is purely bad
|
|
480
|
+
* caller input. Validate up front and name the offending ids in a 400. Models with
|
|
481
|
+
* client-minted string `_id`s (the syncdb default, see `legacyCursorClause`) take any shape.
|
|
482
|
+
*/
|
|
483
|
+
var castEntityIds = function (_a) {
|
|
484
|
+
var _b, _c;
|
|
485
|
+
var model = _a.model, ids = _a.ids;
|
|
486
|
+
var idInstance = (_c = (_b = model.schema) === null || _b === void 0 ? void 0 : _b.path("_id")) === null || _c === void 0 ? void 0 : _c.instance;
|
|
487
|
+
if (idInstance !== "ObjectId") {
|
|
488
|
+
return ids;
|
|
489
|
+
}
|
|
490
|
+
var invalid = ids.filter(function (id) { return !mongoose_1.default.isValidObjectId(id); });
|
|
491
|
+
if (invalid.length > 0) {
|
|
492
|
+
throw new errors_1.APIError({
|
|
493
|
+
status: 400,
|
|
494
|
+
title: "Invalid ids for ".concat(String(model.modelName), ": ").concat(invalid.join(",")),
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
return ids.map(function (id) { return new mongoose_1.default.Types.ObjectId(id); });
|
|
498
|
+
};
|
|
499
|
+
/**
|
|
500
|
+
* Mount the SyncDB HTTP routes:
|
|
501
|
+
* - GET /sync/streams — the authoritative set of streams the caller belongs to (C2)
|
|
502
|
+
* - GET /sync/snapshot — per-stream bootstrap/catch-up with server-enforced scoping
|
|
503
|
+
* - POST /sync/mutate — HTTP fallback mutation channel over applySyncMutation
|
|
504
|
+
* - GET /sync/key — per-user key material for the default encryption KeyProvider
|
|
505
|
+
*/
|
|
506
|
+
var addSyncRoutes = function (app, options) {
|
|
507
|
+
if (options === void 0) { options = {}; }
|
|
508
|
+
var router = express_1.default.Router();
|
|
509
|
+
// C2: authoritative membership discovery. Runs against the full req.user (D2) so
|
|
510
|
+
// tenant memberships resolve from current organizationIds.
|
|
511
|
+
router.get("/sync/streams", (0, auth_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
512
|
+
var user, streams, _a, _b, entry, entryStreams, entryStreams_1, entryStreams_1_1, stream, error_2, e_3_1, response;
|
|
513
|
+
var e_3, _c, e_4, _d;
|
|
514
|
+
return __generator(this, function (_e) {
|
|
515
|
+
switch (_e.label) {
|
|
516
|
+
case 0:
|
|
517
|
+
user = req.user;
|
|
518
|
+
if (!user) {
|
|
519
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
520
|
+
}
|
|
521
|
+
streams = [];
|
|
522
|
+
_e.label = 1;
|
|
523
|
+
case 1:
|
|
524
|
+
_e.trys.push([1, 9, 10, 11]);
|
|
525
|
+
_a = __values((0, registry_1.getSyncRegistry)()), _b = _a.next();
|
|
526
|
+
_e.label = 2;
|
|
527
|
+
case 2:
|
|
528
|
+
if (!!_b.done) return [3 /*break*/, 8];
|
|
529
|
+
entry = _b.value;
|
|
530
|
+
return [4 /*yield*/, (0, permissions_1.checkPermissions)("list", entry.options.permissions.list, user)];
|
|
531
|
+
case 3:
|
|
532
|
+
if (!(_e.sent())) {
|
|
533
|
+
return [3 /*break*/, 7];
|
|
534
|
+
}
|
|
535
|
+
_e.label = 4;
|
|
536
|
+
case 4:
|
|
537
|
+
_e.trys.push([4, 6, , 7]);
|
|
538
|
+
return [4 /*yield*/, (0, streams_1.resolveUserStreamsForEntry)({
|
|
539
|
+
entry: entry,
|
|
540
|
+
getUserScopes: options.getUserScopes,
|
|
541
|
+
user: user,
|
|
542
|
+
})];
|
|
543
|
+
case 5:
|
|
544
|
+
entryStreams = _e.sent();
|
|
545
|
+
try {
|
|
546
|
+
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()) {
|
|
547
|
+
stream = entryStreams_1_1.value;
|
|
548
|
+
streams.push({ collection: entry.collectionTag, stream: stream });
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
552
|
+
finally {
|
|
553
|
+
try {
|
|
554
|
+
if (entryStreams_1_1 && !entryStreams_1_1.done && (_d = entryStreams_1.return)) _d.call(entryStreams_1);
|
|
555
|
+
}
|
|
556
|
+
finally { if (e_4) throw e_4.error; }
|
|
557
|
+
}
|
|
558
|
+
return [3 /*break*/, 7];
|
|
559
|
+
case 6:
|
|
560
|
+
error_2 = _e.sent();
|
|
561
|
+
throw new errors_1.APIError({
|
|
562
|
+
status: 500,
|
|
563
|
+
title: "Failed to resolve streams for ".concat(entry.collectionTag, ": ").concat(String(error_2)),
|
|
564
|
+
});
|
|
565
|
+
case 7:
|
|
566
|
+
_b = _a.next();
|
|
567
|
+
return [3 /*break*/, 2];
|
|
568
|
+
case 8: return [3 /*break*/, 11];
|
|
569
|
+
case 9:
|
|
570
|
+
e_3_1 = _e.sent();
|
|
571
|
+
e_3 = { error: e_3_1 };
|
|
572
|
+
return [3 /*break*/, 11];
|
|
573
|
+
case 10:
|
|
574
|
+
try {
|
|
575
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
576
|
+
}
|
|
577
|
+
finally { if (e_3) throw e_3.error; }
|
|
578
|
+
return [7 /*endfinally*/];
|
|
579
|
+
case 11:
|
|
580
|
+
response = { streams: streams };
|
|
581
|
+
return [2 /*return*/, res.json(response)];
|
|
582
|
+
}
|
|
583
|
+
});
|
|
584
|
+
}); }));
|
|
585
|
+
router.get("/sync/snapshot", (0, auth_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
586
|
+
var user, streamKey, parsed, entry, memberStreams, cursor, requestedLimit, limit, legacyCursorIn, snapshotFilterResult, _a, queryFilterResult, scopeFilter, model, frontierSeq, oldestRetainedSeq, deniedResponse, legacyResult, response_1, seqResult, response;
|
|
587
|
+
var _b, _c;
|
|
588
|
+
return __generator(this, function (_d) {
|
|
589
|
+
switch (_d.label) {
|
|
590
|
+
case 0:
|
|
591
|
+
user = req.user;
|
|
592
|
+
if (!user) {
|
|
593
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
594
|
+
}
|
|
595
|
+
streamKey = String((_b = req.query.stream) !== null && _b !== void 0 ? _b : "");
|
|
596
|
+
if (!streamKey) {
|
|
597
|
+
throw new errors_1.APIError({ status: 400, title: "stream query parameter is required" });
|
|
598
|
+
}
|
|
599
|
+
parsed = (0, streams_1.parseStreamKey)(streamKey);
|
|
600
|
+
if (!parsed) {
|
|
601
|
+
throw new errors_1.APIError({ status: 400, title: "Invalid stream key: ".concat(streamKey) });
|
|
602
|
+
}
|
|
603
|
+
entry = (0, registry_1.findSyncEntryByCollectionTag)(parsed.collectionTag);
|
|
604
|
+
if (!entry) {
|
|
605
|
+
throw new errors_1.APIError({
|
|
606
|
+
status: 404,
|
|
607
|
+
title: "Unknown sync collection: ".concat(parsed.collectionTag),
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
return [4 /*yield*/, (0, permissions_1.checkPermissions)("list", entry.options.permissions.list, user)];
|
|
611
|
+
case 1:
|
|
612
|
+
if (!(_d.sent())) {
|
|
613
|
+
throw new errors_1.APIError({
|
|
614
|
+
status: 403,
|
|
615
|
+
title: "Access to sync snapshot for ".concat(parsed.collectionTag, " denied for ").concat(user.id),
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
return [4 /*yield*/, (0, streams_1.resolveUserStreamsForEntry)({
|
|
619
|
+
entry: entry,
|
|
620
|
+
getUserScopes: options.getUserScopes,
|
|
621
|
+
user: user,
|
|
622
|
+
})];
|
|
623
|
+
case 2:
|
|
624
|
+
memberStreams = _d.sent();
|
|
625
|
+
if (!memberStreams.includes(streamKey)) {
|
|
626
|
+
throw new errors_1.APIError({
|
|
627
|
+
status: 403,
|
|
628
|
+
title: "User ".concat(user.id, " does not belong to stream ").concat(streamKey),
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
cursor = parseNonNegativeInt(req.query.cursor, "cursor", 0);
|
|
632
|
+
requestedLimit = parseNonNegativeInt(req.query.limit, "limit", (_c = options.defaultSnapshotLimit) !== null && _c !== void 0 ? _c : DEFAULT_SNAPSHOT_LIMIT);
|
|
633
|
+
limit = Math.min(Math.max(requestedLimit, 1), MAX_SNAPSHOT_LIMIT);
|
|
634
|
+
legacyCursorIn = typeof req.query.legacyCursor === "string" && req.query.legacyCursor.length > 0
|
|
635
|
+
? req.query.legacyCursor
|
|
636
|
+
: undefined;
|
|
637
|
+
if (!entry.config.snapshotFilter) return [3 /*break*/, 4];
|
|
638
|
+
return [4 /*yield*/, entry.config.snapshotFilter({ id: String(user.id) })];
|
|
639
|
+
case 3:
|
|
640
|
+
_a = _d.sent();
|
|
641
|
+
return [3 /*break*/, 5];
|
|
642
|
+
case 4:
|
|
643
|
+
_a = undefined;
|
|
644
|
+
_d.label = 5;
|
|
645
|
+
case 5:
|
|
646
|
+
snapshotFilterResult = _a;
|
|
647
|
+
return [4 /*yield*/, resolveSyncQueryFilter({ entry: entry, user: user })];
|
|
648
|
+
case 6:
|
|
649
|
+
queryFilterResult = _d.sent();
|
|
650
|
+
scopeFilter = composeSyncReadFilter({
|
|
651
|
+
queryFilter: queryFilterResult.filter,
|
|
652
|
+
scopeFilter: (0, exports.buildSnapshotScopeFilter)({
|
|
653
|
+
entry: entry,
|
|
654
|
+
scopeValue: parsed.scopeValue,
|
|
655
|
+
snapshotFilterResult: snapshotFilterResult,
|
|
656
|
+
}),
|
|
657
|
+
});
|
|
658
|
+
model = mongoose_1.default.model(entry.modelName);
|
|
659
|
+
return [4 /*yield*/, (0, models_1.computeStableFrontier)({ stream: streamKey })];
|
|
660
|
+
case 7:
|
|
661
|
+
frontierSeq = _d.sent();
|
|
662
|
+
return [4 /*yield*/, (0, models_1.getCompactedThroughSeq)({ stream: streamKey })];
|
|
663
|
+
case 8:
|
|
664
|
+
oldestRetainedSeq = _d.sent();
|
|
665
|
+
// Task 9.19: a `queryFilter` that denies this caller means "no visible rows", exactly
|
|
666
|
+
// as REST's list endpoint answers. Serve a terminal empty page (cursor at the
|
|
667
|
+
// frontier, hasMore false) so the client settles instead of paging forever.
|
|
668
|
+
if (queryFilterResult.denied) {
|
|
669
|
+
deniedResponse = {
|
|
670
|
+
cursor: frontierSeq,
|
|
671
|
+
entities: [],
|
|
672
|
+
frontierSeq: frontierSeq,
|
|
673
|
+
hasMore: false,
|
|
674
|
+
oldestRetainedSeq: oldestRetainedSeq,
|
|
675
|
+
stream: streamKey,
|
|
676
|
+
};
|
|
677
|
+
return [2 /*return*/, res.json(deniedResponse)];
|
|
678
|
+
}
|
|
679
|
+
if (!(cursor === 0)) return [3 /*break*/, 10];
|
|
680
|
+
return [4 /*yield*/, pageLegacyStratum({
|
|
681
|
+
entry: entry,
|
|
682
|
+
legacyCursorIn: legacyCursorIn,
|
|
683
|
+
limit: limit,
|
|
684
|
+
model: model,
|
|
685
|
+
req: req,
|
|
686
|
+
scopeFilter: scopeFilter,
|
|
687
|
+
})];
|
|
688
|
+
case 9:
|
|
689
|
+
legacyResult = _d.sent();
|
|
690
|
+
if (legacyResult) {
|
|
691
|
+
response_1 = {
|
|
692
|
+
cursor: 0,
|
|
693
|
+
entities: legacyResult.entities,
|
|
694
|
+
frontierSeq: frontierSeq,
|
|
695
|
+
hasMore: true,
|
|
696
|
+
legacyCursor: legacyResult.legacyCursor,
|
|
697
|
+
oldestRetainedSeq: oldestRetainedSeq,
|
|
698
|
+
stream: streamKey,
|
|
699
|
+
};
|
|
700
|
+
return [2 /*return*/, res.json(response_1)];
|
|
701
|
+
}
|
|
702
|
+
_d.label = 10;
|
|
703
|
+
case 10: return [4 /*yield*/, pageSeqStratum({
|
|
704
|
+
cursor: cursor,
|
|
705
|
+
entry: entry,
|
|
706
|
+
frontierSeq: frontierSeq,
|
|
707
|
+
limit: limit,
|
|
708
|
+
model: model,
|
|
709
|
+
req: req,
|
|
710
|
+
scopeFilter: scopeFilter,
|
|
711
|
+
streamKey: streamKey,
|
|
712
|
+
})];
|
|
713
|
+
case 11:
|
|
714
|
+
seqResult = _d.sent();
|
|
715
|
+
response = {
|
|
716
|
+
cursor: seqResult.cursor,
|
|
717
|
+
entities: seqResult.entities,
|
|
718
|
+
frontierSeq: frontierSeq,
|
|
719
|
+
hasMore: seqResult.hasMore,
|
|
720
|
+
oldestRetainedSeq: oldestRetainedSeq,
|
|
721
|
+
stream: streamKey,
|
|
722
|
+
};
|
|
723
|
+
return [2 /*return*/, res.json(response)];
|
|
724
|
+
}
|
|
725
|
+
});
|
|
726
|
+
}); }));
|
|
727
|
+
router.get("/sync/entities", (0, auth_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
728
|
+
var user, collection, idsParam, ids, entry, memberStreams, memberSet, queryFilterResult, deniedResponse, model, castIds, docs, entities, _a, _b, doc, allowed, docObj, stream, isTombstone, _c, _d, _e, e_5_1, response;
|
|
729
|
+
var e_5, _f, _g;
|
|
730
|
+
var _h, _j;
|
|
731
|
+
return __generator(this, function (_k) {
|
|
732
|
+
switch (_k.label) {
|
|
733
|
+
case 0:
|
|
734
|
+
user = req.user;
|
|
735
|
+
if (!user) {
|
|
736
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
737
|
+
}
|
|
738
|
+
collection = String((_h = req.query.collection) !== null && _h !== void 0 ? _h : "").trim();
|
|
739
|
+
if (!collection) {
|
|
740
|
+
throw new errors_1.APIError({ status: 400, title: "collection query parameter is required" });
|
|
741
|
+
}
|
|
742
|
+
idsParam = req.query.ids;
|
|
743
|
+
ids = typeof idsParam === "string"
|
|
744
|
+
? idsParam
|
|
745
|
+
.split(",")
|
|
746
|
+
.map(function (id) { return id.trim(); })
|
|
747
|
+
.filter(function (id) { return id.length > 0; })
|
|
748
|
+
: [];
|
|
749
|
+
if (ids.length === 0) {
|
|
750
|
+
throw new errors_1.APIError({ status: 400, title: "ids query parameter is required" });
|
|
751
|
+
}
|
|
752
|
+
// Task 9.27: this used to `.slice(0, MAX_ENTITY_FETCH)`, so an over-cap repair fetch
|
|
753
|
+
// came back 200 with a partial body and the client treated the entities it never
|
|
754
|
+
// received as absent. Reject instead, so the caller re-requests in chunks.
|
|
755
|
+
if (ids.length > exports.MAX_ENTITY_FETCH) {
|
|
756
|
+
throw new errors_1.APIError({
|
|
757
|
+
status: 400,
|
|
758
|
+
title: "Too many ids: ".concat(ids.length, " requested, limit is ").concat(exports.MAX_ENTITY_FETCH),
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
entry = (0, registry_1.findSyncEntryByCollectionTag)(collection);
|
|
762
|
+
if (!entry) {
|
|
763
|
+
throw new errors_1.APIError({
|
|
764
|
+
status: 404,
|
|
765
|
+
title: "Unknown sync collection: ".concat(collection),
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
return [4 /*yield*/, (0, permissions_1.checkPermissions)("list", entry.options.permissions.list, user)];
|
|
769
|
+
case 1:
|
|
770
|
+
if (!(_k.sent())) {
|
|
771
|
+
throw new errors_1.APIError({
|
|
772
|
+
status: 403,
|
|
773
|
+
title: "Access to sync entities for ".concat(collection, " denied for ").concat(user.id),
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
return [4 /*yield*/, (0, streams_1.resolveUserStreamsForEntry)({
|
|
777
|
+
entry: entry,
|
|
778
|
+
getUserScopes: options.getUserScopes,
|
|
779
|
+
user: user,
|
|
780
|
+
})];
|
|
781
|
+
case 2:
|
|
782
|
+
memberStreams = _k.sent();
|
|
783
|
+
memberSet = new Set(memberStreams);
|
|
784
|
+
return [4 /*yield*/, resolveSyncQueryFilter({ entry: entry, user: user })];
|
|
785
|
+
case 3:
|
|
786
|
+
queryFilterResult = _k.sent();
|
|
787
|
+
if (queryFilterResult.denied) {
|
|
788
|
+
deniedResponse = { entities: [] };
|
|
789
|
+
return [2 /*return*/, res.json(deniedResponse)];
|
|
790
|
+
}
|
|
791
|
+
model = mongoose_1.default.model(entry.modelName);
|
|
792
|
+
castIds = castEntityIds({ ids: ids, model: model });
|
|
793
|
+
return [4 /*yield*/, model.find(__assign(__assign({}, composeSyncReadFilter({
|
|
794
|
+
queryFilter: queryFilterResult.filter,
|
|
795
|
+
scopeFilter: { _id: { $in: castIds } },
|
|
796
|
+
})), { deleted: { $in: [true, false] } }))];
|
|
797
|
+
case 4:
|
|
798
|
+
docs = _k.sent();
|
|
799
|
+
entities = [];
|
|
800
|
+
_k.label = 5;
|
|
801
|
+
case 5:
|
|
802
|
+
_k.trys.push([5, 13, 14, 15]);
|
|
803
|
+
_a = __values(docs), _b = _a.next();
|
|
804
|
+
_k.label = 6;
|
|
805
|
+
case 6:
|
|
806
|
+
if (!!_b.done) return [3 /*break*/, 12];
|
|
807
|
+
doc = _b.value;
|
|
808
|
+
return [4 /*yield*/, (0, permissions_1.checkPermissions)("read", entry.options.permissions.read, user, doc)];
|
|
809
|
+
case 7:
|
|
810
|
+
allowed = _k.sent();
|
|
811
|
+
if (!allowed) {
|
|
812
|
+
return [3 /*break*/, 11];
|
|
813
|
+
}
|
|
814
|
+
docObj = doc.toObject();
|
|
815
|
+
stream = (0, streams_1.resolveStreamForDoc)({
|
|
816
|
+
collectionTag: entry.collectionTag,
|
|
817
|
+
doc: docObj,
|
|
818
|
+
scope: entry.config.scope,
|
|
819
|
+
});
|
|
820
|
+
if (!memberSet.has(stream)) {
|
|
821
|
+
return [3 /*break*/, 11];
|
|
822
|
+
}
|
|
823
|
+
isTombstone = Boolean(docObj.deleted);
|
|
824
|
+
_d = (_c = entities).push;
|
|
825
|
+
_g = {};
|
|
826
|
+
if (!isTombstone) return [3 /*break*/, 8];
|
|
827
|
+
_e = null;
|
|
828
|
+
return [3 /*break*/, 10];
|
|
829
|
+
case 8: return [4 /*yield*/, (0, exports.serializeSyncDoc)({ doc: doc, entry: entry, req: req })];
|
|
830
|
+
case 9:
|
|
831
|
+
_e = _k.sent();
|
|
832
|
+
_k.label = 10;
|
|
833
|
+
case 10:
|
|
834
|
+
_d.apply(_c, [(_g.data = _e,
|
|
835
|
+
_g.deleted = isTombstone,
|
|
836
|
+
_g.id = String(doc._id),
|
|
837
|
+
_g.seq = (_j = docObj._syncSeq) !== null && _j !== void 0 ? _j : 0,
|
|
838
|
+
_g)]);
|
|
839
|
+
_k.label = 11;
|
|
840
|
+
case 11:
|
|
841
|
+
_b = _a.next();
|
|
842
|
+
return [3 /*break*/, 6];
|
|
843
|
+
case 12: return [3 /*break*/, 15];
|
|
844
|
+
case 13:
|
|
845
|
+
e_5_1 = _k.sent();
|
|
846
|
+
e_5 = { error: e_5_1 };
|
|
847
|
+
return [3 /*break*/, 15];
|
|
848
|
+
case 14:
|
|
849
|
+
try {
|
|
850
|
+
if (_b && !_b.done && (_f = _a.return)) _f.call(_a);
|
|
851
|
+
}
|
|
852
|
+
finally { if (e_5) throw e_5.error; }
|
|
853
|
+
return [7 /*endfinally*/];
|
|
854
|
+
case 15:
|
|
855
|
+
response = { entities: entities };
|
|
856
|
+
return [2 /*return*/, res.json(response)];
|
|
857
|
+
}
|
|
858
|
+
});
|
|
859
|
+
}); }));
|
|
860
|
+
router.post("/sync/mutate", (0, auth_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
861
|
+
var user, outcome;
|
|
862
|
+
return __generator(this, function (_a) {
|
|
863
|
+
switch (_a.label) {
|
|
864
|
+
case 0:
|
|
865
|
+
user = req.user;
|
|
866
|
+
if (!user) {
|
|
867
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
868
|
+
}
|
|
869
|
+
return [4 /*yield*/, (0, syncBatch_1.runSyncMutation)({
|
|
870
|
+
mutation: req.body,
|
|
871
|
+
req: req,
|
|
872
|
+
scopeResolver: options.getUserScopes,
|
|
873
|
+
user: user,
|
|
874
|
+
})];
|
|
875
|
+
case 1:
|
|
876
|
+
outcome = (_a.sent()).outcome;
|
|
877
|
+
if (outcome.type === "ack") {
|
|
878
|
+
return [2 /*return*/, res.json({ ack: outcome.ack })];
|
|
879
|
+
}
|
|
880
|
+
// Duplicate deliveries reading a recorded outcome map to the same statuses.
|
|
881
|
+
return [2 /*return*/, res.status(NACK_HTTP_STATUS[outcome.nack.code]).json({ nack: outcome.nack })];
|
|
882
|
+
}
|
|
883
|
+
});
|
|
884
|
+
}); }));
|
|
885
|
+
router.post("/sync/mutate/batch", (0, auth_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
886
|
+
var user, body, mutations, _a, response, stage;
|
|
887
|
+
return __generator(this, function (_b) {
|
|
888
|
+
switch (_b.label) {
|
|
889
|
+
case 0:
|
|
890
|
+
user = req.user;
|
|
891
|
+
if (!user) {
|
|
892
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
893
|
+
}
|
|
894
|
+
body = req.body;
|
|
895
|
+
mutations = Array.isArray(body === null || body === void 0 ? void 0 : body.mutations) ? body.mutations : [];
|
|
896
|
+
return [4 /*yield*/, (0, syncBatch_1.runSyncBatch)({
|
|
897
|
+
mutations: mutations,
|
|
898
|
+
req: req,
|
|
899
|
+
scopeResolver: options.getUserScopes,
|
|
900
|
+
user: user,
|
|
901
|
+
})];
|
|
902
|
+
case 1:
|
|
903
|
+
_a = _b.sent(), response = _a.response, stage = _a.stage;
|
|
904
|
+
if (stage === "validation") {
|
|
905
|
+
return [2 /*return*/, res.status(422).json(response)];
|
|
906
|
+
}
|
|
907
|
+
if (stage === "rate_limited") {
|
|
908
|
+
return [2 /*return*/, res.status(NACK_HTTP_STATUS.rate_limited).json(response)];
|
|
909
|
+
}
|
|
910
|
+
return [2 /*return*/, res.json(response)];
|
|
911
|
+
}
|
|
912
|
+
});
|
|
913
|
+
}); }));
|
|
914
|
+
router.get("/sync/key", (0, auth_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
915
|
+
var user, keyMaterial;
|
|
916
|
+
return __generator(this, function (_a) {
|
|
917
|
+
switch (_a.label) {
|
|
918
|
+
case 0:
|
|
919
|
+
user = req.user;
|
|
920
|
+
if (!user) {
|
|
921
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
922
|
+
}
|
|
923
|
+
return [4 /*yield*/, (0, models_1.getOrCreateSyncKeyMaterial)({ userId: String(user.id) })];
|
|
924
|
+
case 1:
|
|
925
|
+
keyMaterial = _a.sent();
|
|
926
|
+
return [2 /*return*/, res.json({ keyMaterial: keyMaterial })];
|
|
927
|
+
}
|
|
928
|
+
});
|
|
929
|
+
}); }));
|
|
930
|
+
// Authenticate middleware rejects with Error("Unauthorized"); convert that to
|
|
931
|
+
// 401 before the generic error handler (and before Express's default 500).
|
|
932
|
+
router.use(errors_1.apiUnauthorizedMiddleware);
|
|
933
|
+
router.use(errors_1.apiErrorMiddleware);
|
|
934
|
+
app.use(router);
|
|
935
|
+
};
|
|
936
|
+
exports.addSyncRoutes = addSyncRoutes;
|