@terreno/api 0.31.0 → 56.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.d.ts +10 -0
- package/dist/api.js +110 -361
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +121 -26
- package/dist/betterAuth.d.ts +5 -0
- package/dist/betterAuthSetup.js +23 -4
- package/dist/docLoader.js +12 -9
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +29 -1
- package/dist/index.d.ts +17 -1
- package/dist/index.js +29 -1
- package/dist/middleware.d.ts +7 -0
- package/dist/middleware.js +16 -1
- package/dist/openApiCompat.d.ts +10 -1
- package/dist/openApiCompat.js +115 -47
- package/dist/permissions.d.ts +27 -0
- package/dist/permissions.js +42 -1
- package/dist/plugins.d.ts +13 -0
- package/dist/plugins.js +47 -1
- package/dist/realtime/changeStreamWatcher.d.ts +100 -3
- package/dist/realtime/changeStreamWatcher.js +542 -137
- package/dist/realtime/realtimeApp.d.ts +2 -0
- package/dist/realtime/realtimeApp.js +83 -27
- package/dist/realtime/sessionRevalidation.d.ts +90 -0
- package/dist/realtime/sessionRevalidation.js +438 -0
- package/dist/realtime/socketAuth.d.ts +97 -0
- package/dist/realtime/socketAuth.js +272 -0
- package/dist/realtime/socketUser.d.ts +54 -0
- package/dist/realtime/socketUser.js +75 -5
- package/dist/realtime/types.d.ts +54 -0
- package/dist/secretProviders.d.ts +23 -0
- package/dist/secretProviders.js +9 -3
- package/dist/sync/executors.d.ts +198 -0
- package/dist/sync/executors.js +670 -0
- package/dist/sync/loadHarness.js +807 -0
- package/dist/sync/models.d.ts +216 -0
- package/dist/sync/models.js +644 -0
- package/dist/sync/mutationHandler.d.ts +97 -0
- package/dist/sync/mutationHandler.js +886 -0
- package/dist/sync/registry.d.ts +73 -0
- package/dist/sync/registry.js +223 -0
- package/dist/sync/routes.d.ts +62 -0
- package/dist/sync/routes.js +936 -0
- package/dist/sync/scripts/compactTombstones.d.ts +47 -0
- package/dist/sync/scripts/compactTombstones.js +270 -0
- package/dist/sync/serialize.d.ts +20 -0
- package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
- package/dist/sync/socketHandlers.d.ts +54 -0
- package/dist/sync/socketHandlers.js +470 -0
- package/dist/sync/streams.d.ts +85 -0
- package/dist/sync/streams.js +221 -0
- package/dist/sync/syncApp.d.ts +29 -0
- package/dist/sync/syncApp.js +44 -0
- package/dist/sync/syncBatch.d.ts +71 -0
- package/dist/sync/syncBatch.js +252 -0
- package/dist/sync/syncSeqPlugin.d.ts +8 -0
- package/dist/sync/syncSeqPlugin.js +899 -0
- package/dist/sync/types.d.ts +254 -0
- package/dist/sync/types.js +9 -0
- package/dist/terrenoApp.js +90 -27
- package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
- package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
- package/package.json +7 -6
- package/src/__tests__/openApiCompat.test.ts +20 -0
- package/src/api.errors.test.ts +32 -23
- package/src/api.test.ts +12 -0
- package/src/api.ts +87 -296
- package/src/auth.test.ts +202 -0
- package/src/auth.ts +134 -23
- package/src/betterAuth.ts +6 -0
- package/src/betterAuthSetup.test.ts +34 -0
- package/src/betterAuthSetup.ts +20 -1
- package/src/configuration.test.ts +59 -38
- package/src/configurationPlugin.test.ts +72 -42
- package/src/docLoader.ts +3 -1
- package/src/errors.ts +33 -0
- package/src/expressServer.test.ts +7 -1
- package/src/index.ts +42 -0
- package/src/middleware.test.ts +56 -1
- package/src/middleware.ts +15 -0
- package/src/openApiCompat.ts +99 -45
- package/src/permissions.test.ts +101 -17
- package/src/permissions.ts +49 -0
- package/src/plugins.test.ts +64 -0
- package/src/plugins.ts +56 -0
- package/src/realtime/changeStreamWatcher.ts +552 -114
- package/src/realtime/realtime.test.ts +157 -0
- package/src/realtime/realtimeApp.ts +82 -14
- package/src/realtime/realtimeAppSetup.test.ts +25 -27
- package/src/realtime/sessionRevalidation.test.ts +423 -0
- package/src/realtime/sessionRevalidation.ts +273 -0
- package/src/realtime/socketAuth.test.ts +355 -0
- package/src/realtime/socketAuth.ts +228 -0
- package/src/realtime/socketUser.ts +69 -0
- package/src/realtime/types.ts +54 -0
- package/src/secretProviders.test.ts +7 -1
- package/src/secretProviders.ts +15 -3
- package/src/secretProvidersGcpClient.test.ts +66 -0
- package/src/sync/ensureSyncIndexes.test.ts +144 -0
- package/src/sync/executors.test.ts +882 -0
- package/src/sync/executors.ts +705 -0
- package/src/sync/integration.test.ts +1160 -0
- package/src/sync/loadHarness.ts +618 -0
- package/src/sync/models.ts +641 -0
- package/src/sync/mutationHandler.test.ts +1252 -0
- package/src/sync/mutationHandler.ts +850 -0
- package/src/sync/registry.ts +232 -0
- package/src/sync/routes.ts +812 -0
- package/src/sync/scripts/compactTombstones.test.ts +249 -0
- package/src/sync/scripts/compactTombstones.ts +145 -0
- package/src/sync/serialize.ts +48 -0
- package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
- package/src/sync/socketHandlers.ts +320 -0
- package/src/sync/streams.ts +188 -0
- package/src/sync/sync.test.ts +582 -0
- package/src/sync/syncApp.ts +44 -0
- package/src/sync/syncBatch.ts +219 -0
- package/src/sync/syncFrontier.test.ts +494 -0
- package/src/sync/syncPhaseC.test.ts +718 -0
- package/src/sync/syncRoutes.test.ts +865 -0
- package/src/sync/syncSeqPlugin.ts +730 -0
- package/src/sync/syncSocket.test.ts +1440 -0
- package/src/sync/types.ts +270 -0
- package/src/terrenoApp.ts +37 -17
- package/src/tests/bunSetup.ts +8 -0
- package/tsconfig.json +4 -4
- package/tsconfig.typedoc.json +7 -1
- package/dist/__tests__/openApiCompat.test.js +0 -317
- package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
- package/dist/__tests__/versionCheckPlugin.test.js +0 -597
- package/dist/actions.openApi.test.d.ts +0 -1
- package/dist/actions.openApi.test.js +0 -253
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -1087
- package/dist/api.arrayOperations.test.d.ts +0 -1
- package/dist/api.arrayOperations.test.js +0 -878
- package/dist/api.asyncHandler.test.d.ts +0 -1
- package/dist/api.asyncHandler.test.js +0 -236
- package/dist/api.coverage.test.d.ts +0 -1
- package/dist/api.coverage.test.js +0 -192
- package/dist/api.errorPaths.test.d.ts +0 -1
- package/dist/api.errorPaths.test.js +0 -887
- package/dist/api.errors.test.d.ts +0 -1
- package/dist/api.errors.test.js +0 -185
- package/dist/api.hooks.test.d.ts +0 -1
- package/dist/api.hooks.test.js +0 -950
- package/dist/api.query.test.d.ts +0 -1
- package/dist/api.query.test.js +0 -948
- package/dist/api.test.d.ts +0 -1
- package/dist/api.test.js +0 -2820
- package/dist/auth.compile.test.d.ts +0 -1
- package/dist/auth.compile.test.js +0 -201
- package/dist/auth.coverage.test.d.ts +0 -1
- package/dist/auth.coverage.test.js +0 -202
- package/dist/auth.test.d.ts +0 -1
- package/dist/auth.test.js +0 -2286
- package/dist/betterAuth.test.d.ts +0 -1
- package/dist/betterAuth.test.js +0 -181
- package/dist/betterAuthApp.test.d.ts +0 -1
- package/dist/betterAuthApp.test.js +0 -242
- package/dist/betterAuthSetup.test.d.ts +0 -1
- package/dist/betterAuthSetup.test.js +0 -762
- package/dist/config.test.d.ts +0 -1
- package/dist/config.test.js +0 -353
- package/dist/configuration.test.d.ts +0 -1
- package/dist/configuration.test.js +0 -980
- package/dist/configurationPlugin.test.d.ts +0 -1
- package/dist/configurationPlugin.test.js +0 -827
- package/dist/consentApp.test.d.ts +0 -1
- package/dist/consentApp.test.js +0 -1411
- package/dist/docLoader.test.d.ts +0 -1
- package/dist/docLoader.test.js +0 -137
- package/dist/envConfigurationPlugin.test.d.ts +0 -1
- package/dist/envConfigurationPlugin.test.js +0 -369
- package/dist/errors.test.d.ts +0 -1
- package/dist/errors.test.js +0 -607
- package/dist/expressServer.test.d.ts +0 -1
- package/dist/expressServer.test.js +0 -1249
- package/dist/githubAuth.test.d.ts +0 -1
- package/dist/githubAuth.test.js +0 -1142
- package/dist/httpClient.test.d.ts +0 -1
- package/dist/httpClient.test.js +0 -1471
- package/dist/logger.test.d.ts +0 -1
- package/dist/logger.test.js +0 -685
- package/dist/middleware.test.d.ts +0 -1
- package/dist/middleware.test.js +0 -241
- package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
- package/dist/notifiers/googleChatNotifier.test.js +0 -298
- package/dist/notifiers/slackNotifier.test.d.ts +0 -1
- package/dist/notifiers/slackNotifier.test.js +0 -297
- package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
- package/dist/notifiers/zoomNotifier.test.js +0 -373
- package/dist/openApi.test.d.ts +0 -1
- package/dist/openApi.test.js +0 -620
- package/dist/openApiBuilder.test.d.ts +0 -1
- package/dist/openApiBuilder.test.js +0 -599
- package/dist/openApiEtag.test.d.ts +0 -1
- package/dist/openApiEtag.test.js +0 -86
- package/dist/openApiValidator.test.d.ts +0 -1
- package/dist/openApiValidator.test.js +0 -795
- package/dist/permissions.middleware.test.d.ts +0 -1
- package/dist/permissions.middleware.test.js +0 -308
- package/dist/permissions.test.d.ts +0 -1
- package/dist/permissions.test.js +0 -439
- package/dist/plugins.test.d.ts +0 -1
- package/dist/plugins.test.js +0 -902
- package/dist/populate.test.d.ts +0 -1
- package/dist/populate.test.js +0 -370
- package/dist/realtime/realtime.test.d.ts +0 -10
- package/dist/realtime/realtime.test.js +0 -4213
- package/dist/realtime/realtimeAppSetup.test.d.ts +0 -1
- package/dist/realtime/realtimeAppSetup.test.js +0 -213
- package/dist/requestContext.test.d.ts +0 -1
- package/dist/requestContext.test.js +0 -412
- package/dist/scriptRunner.test.d.ts +0 -1
- package/dist/scriptRunner.test.js +0 -386
- package/dist/secretProviders.test.d.ts +0 -1
- package/dist/secretProviders.test.js +0 -726
- package/dist/syncConsents.test.d.ts +0 -1
- package/dist/syncConsents.test.js +0 -522
- package/dist/terrenoApp.test.d.ts +0 -1
- package/dist/terrenoApp.test.js +0 -489
- package/dist/tests/bunSetup.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.js +0 -37
- package/dist/tests/testEnv.d.ts +0 -0
- package/dist/tests/testEnv.js +0 -6
- package/dist/transformers.test.d.ts +0 -1
- package/dist/transformers.test.js +0 -423
- package/dist/utils.test.d.ts +0 -1
- package/dist/utils.test.js +0 -242
- /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
|
@@ -0,0 +1,886 @@
|
|
|
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 __values = (this && this.__values) || function(o) {
|
|
66
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
67
|
+
if (m) return m.call(o);
|
|
68
|
+
if (o && typeof o.length === "number") return {
|
|
69
|
+
next: function () {
|
|
70
|
+
if (o && i >= o.length) o = void 0;
|
|
71
|
+
return { value: o && o[i++], done: !o };
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
75
|
+
};
|
|
76
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
77
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
78
|
+
};
|
|
79
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
80
|
+
exports.applySyncMutationBatch = exports.validateSyncMutationBatch = exports.applySyncMutation = exports.MAX_SYNC_MUTATIONS_PER_BATCH = void 0;
|
|
81
|
+
var luxon_1 = require("luxon");
|
|
82
|
+
var mongoose_1 = __importDefault(require("mongoose"));
|
|
83
|
+
var errors_1 = require("../errors");
|
|
84
|
+
var logger_1 = require("../logger");
|
|
85
|
+
var plugins_1 = require("../plugins");
|
|
86
|
+
var executors_1 = require("./executors");
|
|
87
|
+
var models_1 = require("./models");
|
|
88
|
+
var registry_1 = require("./registry");
|
|
89
|
+
var routes_1 = require("./routes");
|
|
90
|
+
var streams_1 = require("./streams");
|
|
91
|
+
/**
|
|
92
|
+
* Per-mutation success traces (`Mutation applied`) are high-volume. Gate them behind
|
|
93
|
+
* `SYNC_DEBUG=true` so they stay silent by default even when Winston's console level is
|
|
94
|
+
* `debug`.
|
|
95
|
+
*/
|
|
96
|
+
var syncDebugLog = (0, logger_1.createFeatureFlaggedLogger)({
|
|
97
|
+
isEnabled: function () { return process.env.SYNC_DEBUG === "true"; },
|
|
98
|
+
});
|
|
99
|
+
/**
|
|
100
|
+
* C6 (M6): enforce write scope at the sync boundary regardless of consumer `preCreate`
|
|
101
|
+
* quality. For `owner` strategy, force/verify the owner field equals the authenticated
|
|
102
|
+
* user id; for `tenant` strategy, verify the incoming tenant value is in the user's
|
|
103
|
+
* memberships. Throws a 403 APIError (mapped to `unauthorized`) otherwise. A no-op for
|
|
104
|
+
* broadcast/custom scopes and for update data that does not touch the scope field.
|
|
105
|
+
*/
|
|
106
|
+
var enforceWriteScope = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
107
|
+
var scope, field, incoming, memberships;
|
|
108
|
+
var entry = _b.entry, user = _b.user, operation = _b.operation, data = _b.data, scopeResolver = _b.scopeResolver;
|
|
109
|
+
return __generator(this, function (_c) {
|
|
110
|
+
switch (_c.label) {
|
|
111
|
+
case 0:
|
|
112
|
+
if (operation === "delete") {
|
|
113
|
+
return [2 /*return*/];
|
|
114
|
+
}
|
|
115
|
+
scope = entry.config.scope;
|
|
116
|
+
if (typeof scope === "function" || scope.type === "broadcast") {
|
|
117
|
+
return [2 /*return*/];
|
|
118
|
+
}
|
|
119
|
+
field = (0, streams_1.getScopeField)(scope);
|
|
120
|
+
incoming = data === null || data === void 0 ? void 0 : data[field];
|
|
121
|
+
if (scope.type === "owner") {
|
|
122
|
+
// Create must belong to the caller; an update may not move ownership to someone else.
|
|
123
|
+
if (operation === "create" && (incoming === undefined || incoming === null)) {
|
|
124
|
+
// preCreate typically injects ownerId; nothing to verify if absent here.
|
|
125
|
+
return [2 /*return*/];
|
|
126
|
+
}
|
|
127
|
+
if (incoming !== undefined && incoming !== null && String(incoming) !== String(user.id)) {
|
|
128
|
+
throw new errors_1.APIError({
|
|
129
|
+
status: 403,
|
|
130
|
+
title: "Write scope violation on ".concat(entry.collectionTag, ": ").concat(field, " must be the caller"),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return [2 /*return*/];
|
|
134
|
+
}
|
|
135
|
+
// Tenant: the incoming value (create) or the changed value (update) must be a membership.
|
|
136
|
+
if (incoming === undefined || incoming === null) {
|
|
137
|
+
// Nothing to check against memberships yet. A create may legitimately omit the tenant
|
|
138
|
+
// field for `preCreate` to inject from the authenticated user (see the example
|
|
139
|
+
// projects router), and an update that does not touch the scope field leaves it
|
|
140
|
+
// alone. Task 9.21's "never file a document under `tenant:undefined`" guarantee is
|
|
141
|
+
// enforced by resolveStreamForDoc on the write path instead, which sees the value
|
|
142
|
+
// AFTER preCreate has run — inferring intent from the mere presence of a preCreate
|
|
143
|
+
// hook would let an unrelated one (logging, timestamps) silently reopen that hole.
|
|
144
|
+
return [2 /*return*/];
|
|
145
|
+
}
|
|
146
|
+
if (!scopeResolver) {
|
|
147
|
+
throw new errors_1.APIError({
|
|
148
|
+
status: 500,
|
|
149
|
+
title: "Sync collection ".concat(entry.collectionTag, " is tenant-scoped but SyncApp has no getUserScopes resolver"),
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return [4 /*yield*/, scopeResolver(user, entry)];
|
|
153
|
+
case 1:
|
|
154
|
+
memberships = _c.sent();
|
|
155
|
+
if (!memberships.map(String).includes(String(incoming))) {
|
|
156
|
+
throw new errors_1.APIError({
|
|
157
|
+
status: 403,
|
|
158
|
+
title: "Write scope violation on ".concat(entry.collectionTag, ": ").concat(field, " ").concat(String(incoming), " is not a membership"),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return [2 /*return*/];
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}); };
|
|
165
|
+
var PENDING_POLL_ATTEMPTS = 10;
|
|
166
|
+
var PENDING_POLL_INTERVAL_MS = 100;
|
|
167
|
+
var VALID_OPERATIONS = new Set(["create", "update", "delete"]);
|
|
168
|
+
/** Maximum mutations accepted in a single `sync:mutateBatch` / `POST /sync/mutate/batch` request. */
|
|
169
|
+
exports.MAX_SYNC_MUTATIONS_PER_BATCH = 100;
|
|
170
|
+
var sleep = function (ms) { return new Promise(function (resolve) { return setTimeout(resolve, ms); }); };
|
|
171
|
+
var makeNack = function (nack) {
|
|
172
|
+
logger_1.logger.info("[sync] Mutation nacked", { code: nack.code, mutationId: nack.mutationId });
|
|
173
|
+
return { nack: nack, type: "nack" };
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* Load a document by id INCLUDING tombstones (`isDeletedPlugin` would otherwise filter a
|
|
177
|
+
* soft-deleted doc out) and serialize it for a sync payload. Returns undefined when the
|
|
178
|
+
* document is gone, the id does not cast, or the consumer's serializer throws — callers
|
|
179
|
+
* treat a missing `serverDoc` as "seq only", which is what the client needs to re-base.
|
|
180
|
+
*/
|
|
181
|
+
var serializeLiveDoc = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
182
|
+
var model, found, doc, error_1;
|
|
183
|
+
var entry = _b.entry, id = _b.id, request = _b.request;
|
|
184
|
+
return __generator(this, function (_c) {
|
|
185
|
+
switch (_c.label) {
|
|
186
|
+
case 0:
|
|
187
|
+
if (!id) {
|
|
188
|
+
return [2 /*return*/, undefined];
|
|
189
|
+
}
|
|
190
|
+
_c.label = 1;
|
|
191
|
+
case 1:
|
|
192
|
+
_c.trys.push([1, 4, , 5]);
|
|
193
|
+
model = mongoose_1.default.model(entry.modelName);
|
|
194
|
+
return [4 /*yield*/, model.find({ _id: id, deleted: { $in: [true, false] } }).limit(1)];
|
|
195
|
+
case 2:
|
|
196
|
+
found = _c.sent();
|
|
197
|
+
doc = found[0];
|
|
198
|
+
if (!doc) {
|
|
199
|
+
return [2 /*return*/, undefined];
|
|
200
|
+
}
|
|
201
|
+
return [4 /*yield*/, (0, routes_1.serializeSyncDoc)({ doc: doc, entry: entry, req: request })];
|
|
202
|
+
case 3: return [2 /*return*/, _c.sent()];
|
|
203
|
+
case 4:
|
|
204
|
+
error_1 = _c.sent();
|
|
205
|
+
logger_1.logger.warn("[sync] Failed to serialize the server doc for a recorded conflict", {
|
|
206
|
+
collection: entry.collectionTag,
|
|
207
|
+
error: String(error_1),
|
|
208
|
+
id: id,
|
|
209
|
+
});
|
|
210
|
+
return [2 /*return*/, undefined];
|
|
211
|
+
case 5: return [2 /*return*/];
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}); };
|
|
215
|
+
/**
|
|
216
|
+
* Reconstruct the outcome recorded on a finalized ledger row; undefined while pending.
|
|
217
|
+
*
|
|
218
|
+
* Task 9.21: the row stores no document data, so a recorded `conflicted` outcome
|
|
219
|
+
* re-serializes the LIVE document from `resultId`. Besides keeping PHI out of
|
|
220
|
+
* `syncmutations` for the ledger's 30-day TTL, this hands the client current server
|
|
221
|
+
* state rather than a snapshot frozen at first-conflict time.
|
|
222
|
+
*/
|
|
223
|
+
var outcomeFromLedgerRow = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
224
|
+
var _c;
|
|
225
|
+
var _d;
|
|
226
|
+
var _e, _f, _g;
|
|
227
|
+
var entry = _b.entry, request = _b.request, row = _b.row;
|
|
228
|
+
return __generator(this, function (_h) {
|
|
229
|
+
switch (_h.label) {
|
|
230
|
+
case 0:
|
|
231
|
+
if (row.status === "applied") {
|
|
232
|
+
return [2 /*return*/, {
|
|
233
|
+
ack: { id: (_e = row.resultId) !== null && _e !== void 0 ? _e : "", mutationId: row.mutationId, seq: (_f = row.resultSeq) !== null && _f !== void 0 ? _f : 0 },
|
|
234
|
+
type: "ack",
|
|
235
|
+
}];
|
|
236
|
+
}
|
|
237
|
+
if (!(row.status === "conflicted")) return [3 /*break*/, 2];
|
|
238
|
+
_c = makeNack;
|
|
239
|
+
_d = {
|
|
240
|
+
code: "conflict",
|
|
241
|
+
message: row.error,
|
|
242
|
+
mutationId: row.mutationId
|
|
243
|
+
};
|
|
244
|
+
return [4 /*yield*/, serializeLiveDoc({ entry: entry, id: row.resultId, request: request })];
|
|
245
|
+
case 1: return [2 /*return*/, _c.apply(void 0, [(_d.serverDoc = _h.sent(),
|
|
246
|
+
_d.serverSeq = row.resultSeq,
|
|
247
|
+
_d)])];
|
|
248
|
+
case 2:
|
|
249
|
+
if (row.status === "failed") {
|
|
250
|
+
return [2 /*return*/, makeNack({
|
|
251
|
+
code: (_g = row.nackCode) !== null && _g !== void 0 ? _g : "error",
|
|
252
|
+
message: row.error,
|
|
253
|
+
mutationId: row.mutationId,
|
|
254
|
+
})];
|
|
255
|
+
}
|
|
256
|
+
return [2 /*return*/, undefined];
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}); };
|
|
260
|
+
/**
|
|
261
|
+
* C5 (FIX 6) lease takeover: a `pending` row older than {@link SYNC_MUTATION_LEASE_MS}
|
|
262
|
+
* is assumed abandoned (the original claimant crashed between claiming the row and
|
|
263
|
+
* finalizing it) — atomically re-claim it via `findOneAndUpdate` so exactly one
|
|
264
|
+
* concurrent delivery wins the takeover. Returns the re-claimed row (with a bumped
|
|
265
|
+
* `claimedAt`) on success, or `undefined` if the row is not stale/pending (someone else
|
|
266
|
+
* is actively working it, or it already finalized).
|
|
267
|
+
*/
|
|
268
|
+
var takeoverStaleLease = function (mutationId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
269
|
+
var cutoff, claimed;
|
|
270
|
+
return __generator(this, function (_a) {
|
|
271
|
+
switch (_a.label) {
|
|
272
|
+
case 0:
|
|
273
|
+
cutoff = luxon_1.DateTime.now().minus({ milliseconds: models_1.SYNC_MUTATION_LEASE_MS }).toJSDate();
|
|
274
|
+
return [4 /*yield*/, models_1.SyncMutation.findOneAndUpdate({ claimedAt: { $lt: cutoff }, mutationId: mutationId, status: "pending" }, { $set: { claimedAt: new Date() } }, { new: true })];
|
|
275
|
+
case 1:
|
|
276
|
+
claimed = _a.sent();
|
|
277
|
+
return [2 /*return*/, claimed !== null && claimed !== void 0 ? claimed : undefined];
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}); };
|
|
281
|
+
/**
|
|
282
|
+
* Another delivery claimed this mutationId: poll the ledger row until its status leaves
|
|
283
|
+
* `pending` and return the recorded outcome. Before each poll, attempt a lease takeover
|
|
284
|
+
* (C5/FIX 6) — a `pending` row older than {@link SYNC_MUTATION_LEASE_MS} is assumed
|
|
285
|
+
* abandoned by a crashed claimant, so this delivery re-claims and re-applies it instead
|
|
286
|
+
* of waiting out the full poll budget (or worse, the 30-day TTL). If the row stays
|
|
287
|
+
* pending and un-stale past the timeout, nack `error` without modifying the row — the
|
|
288
|
+
* owning delivery will still finalize it.
|
|
289
|
+
*/
|
|
290
|
+
var waitForRecordedOutcome = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
291
|
+
var attempt, row, outcome, takenOver;
|
|
292
|
+
var entry = _b.entry, mutationId = _b.mutationId, request = _b.request, user = _b.user, onTakeover = _b.onTakeover;
|
|
293
|
+
return __generator(this, function (_c) {
|
|
294
|
+
switch (_c.label) {
|
|
295
|
+
case 0:
|
|
296
|
+
attempt = 0;
|
|
297
|
+
_c.label = 1;
|
|
298
|
+
case 1:
|
|
299
|
+
if (!(attempt < PENDING_POLL_ATTEMPTS)) return [3 /*break*/, 8];
|
|
300
|
+
return [4 /*yield*/, (0, plugins_1.findOneOrNoneFor)(models_1.SyncMutation, { mutationId: mutationId })];
|
|
301
|
+
case 2:
|
|
302
|
+
row = _c.sent();
|
|
303
|
+
if (!row) return [3 /*break*/, 5];
|
|
304
|
+
if (String(row.userId) !== String(user.id)) {
|
|
305
|
+
return [2 /*return*/, makeNack({
|
|
306
|
+
code: "unauthorized",
|
|
307
|
+
message: "Mutation ".concat(mutationId, " was submitted by another user"),
|
|
308
|
+
mutationId: mutationId,
|
|
309
|
+
})];
|
|
310
|
+
}
|
|
311
|
+
return [4 /*yield*/, outcomeFromLedgerRow({ entry: entry, request: request, row: row })];
|
|
312
|
+
case 3:
|
|
313
|
+
outcome = _c.sent();
|
|
314
|
+
if (outcome) {
|
|
315
|
+
return [2 /*return*/, outcome];
|
|
316
|
+
}
|
|
317
|
+
if (!(row.status === "pending")) return [3 /*break*/, 5];
|
|
318
|
+
return [4 /*yield*/, takeoverStaleLease(mutationId)];
|
|
319
|
+
case 4:
|
|
320
|
+
takenOver = _c.sent();
|
|
321
|
+
if (takenOver) {
|
|
322
|
+
logger_1.logger.warn("[sync] Took over a stale mutation lease", {
|
|
323
|
+
mutationId: mutationId,
|
|
324
|
+
staleForMs: Date.now() - new Date(row.claimedAt).getTime(),
|
|
325
|
+
});
|
|
326
|
+
return [2 /*return*/, onTakeover(takenOver)];
|
|
327
|
+
}
|
|
328
|
+
_c.label = 5;
|
|
329
|
+
case 5: return [4 /*yield*/, sleep(PENDING_POLL_INTERVAL_MS)];
|
|
330
|
+
case 6:
|
|
331
|
+
_c.sent();
|
|
332
|
+
_c.label = 7;
|
|
333
|
+
case 7:
|
|
334
|
+
attempt++;
|
|
335
|
+
return [3 /*break*/, 1];
|
|
336
|
+
case 8: return [2 /*return*/, makeNack({
|
|
337
|
+
code: "error",
|
|
338
|
+
message: "Mutation ".concat(mutationId, " is still in flight"),
|
|
339
|
+
mutationId: mutationId,
|
|
340
|
+
})];
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
}); };
|
|
344
|
+
/**
|
|
345
|
+
* C5 (FIX 6): true when `error` is (or wraps, via `APIError.error` — see
|
|
346
|
+
* `executeCreate`'s `model.create()` catch, which wraps the raw Mongo driver
|
|
347
|
+
* error in an APIError before it ever reaches the caller) a MongoDB E11000
|
|
348
|
+
* duplicate-key error.
|
|
349
|
+
*/
|
|
350
|
+
var isE11000Error = function (error) {
|
|
351
|
+
var candidate = error;
|
|
352
|
+
if ((candidate === null || candidate === void 0 ? void 0 : candidate.code) === 11000) {
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
var wrapped = candidate === null || candidate === void 0 ? void 0 : candidate.error;
|
|
356
|
+
return (wrapped === null || wrapped === void 0 ? void 0 : wrapped.code) === 11000;
|
|
357
|
+
};
|
|
358
|
+
/**
|
|
359
|
+
* C5 (FIX 6): true when every key/value in `data` (the mutation's own
|
|
360
|
+
* intended write) is already present on `doc` — used alongside the
|
|
361
|
+
* expected-post-write-seq check to distinguish "this mutation's own write
|
|
362
|
+
* already landed" (safe to report as already-applied) from "a genuinely
|
|
363
|
+
* different write landed and happens to have advanced the seq by exactly
|
|
364
|
+
* one" (a real conflict; a seq match alone cannot tell these apart).
|
|
365
|
+
*/
|
|
366
|
+
var docMatchesMutationData = function (doc, data) {
|
|
367
|
+
if (!data) {
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
var asDocument = doc;
|
|
371
|
+
var plain = typeof (asDocument === null || asDocument === void 0 ? void 0 : asDocument.toObject) === "function"
|
|
372
|
+
? asDocument.toObject()
|
|
373
|
+
: doc;
|
|
374
|
+
return Object.entries(data).every(function (_a) {
|
|
375
|
+
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
376
|
+
var current = plain === null || plain === void 0 ? void 0 : plain[key];
|
|
377
|
+
if (current instanceof Date && typeof value === "string") {
|
|
378
|
+
return current.toISOString() === new Date(value).toISOString();
|
|
379
|
+
}
|
|
380
|
+
return JSON.stringify(current) === JSON.stringify(value);
|
|
381
|
+
});
|
|
382
|
+
};
|
|
383
|
+
var nackCodeForError = function (error) {
|
|
384
|
+
if (!(0, errors_1.isAPIError)(error)) {
|
|
385
|
+
return "error";
|
|
386
|
+
}
|
|
387
|
+
if (error.status === 403 || error.status === 405) {
|
|
388
|
+
return "unauthorized";
|
|
389
|
+
}
|
|
390
|
+
if (error.status === 400 || error.status === 404 || error.status === 422) {
|
|
391
|
+
return "validation";
|
|
392
|
+
}
|
|
393
|
+
return "error";
|
|
394
|
+
};
|
|
395
|
+
/** Record a failed/conflicted outcome on the claimed ledger row and build the nack. */
|
|
396
|
+
var finalizeNack = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
397
|
+
var serverDoc, serializeError_1, resultId, code, message;
|
|
398
|
+
var _c;
|
|
399
|
+
var claimedId = _b.claimedId, entry = _b.entry, error = _b.error, mutation = _b.mutation, request = _b.request;
|
|
400
|
+
return __generator(this, function (_d) {
|
|
401
|
+
switch (_d.label) {
|
|
402
|
+
case 0:
|
|
403
|
+
if (!(0, executors_1.isExecutorConflictError)(error)) return [3 /*break*/, 6];
|
|
404
|
+
serverDoc = void 0;
|
|
405
|
+
_d.label = 1;
|
|
406
|
+
case 1:
|
|
407
|
+
_d.trys.push([1, 3, , 4]);
|
|
408
|
+
return [4 /*yield*/, (0, routes_1.serializeSyncDoc)({
|
|
409
|
+
doc: error.doc,
|
|
410
|
+
entry: entry,
|
|
411
|
+
req: request,
|
|
412
|
+
})];
|
|
413
|
+
case 2:
|
|
414
|
+
serverDoc = _d.sent();
|
|
415
|
+
return [3 /*break*/, 4];
|
|
416
|
+
case 3:
|
|
417
|
+
serializeError_1 = _d.sent();
|
|
418
|
+
logger_1.logger.warn("[sync] Failed to serialize conflict server doc", {
|
|
419
|
+
error: String(serializeError_1),
|
|
420
|
+
mutationId: mutation.mutationId,
|
|
421
|
+
});
|
|
422
|
+
return [3 /*break*/, 4];
|
|
423
|
+
case 4:
|
|
424
|
+
resultId = ((_c = error.doc) === null || _c === void 0 ? void 0 : _c._id) !== undefined
|
|
425
|
+
? String(error.doc._id)
|
|
426
|
+
: mutation.id;
|
|
427
|
+
return [4 /*yield*/, models_1.SyncMutation.updateOne({ _id: claimedId }, {
|
|
428
|
+
$set: {
|
|
429
|
+
error: error.title,
|
|
430
|
+
nackCode: "conflict",
|
|
431
|
+
resultId: resultId,
|
|
432
|
+
resultSeq: error.serverSeq,
|
|
433
|
+
status: "conflicted",
|
|
434
|
+
},
|
|
435
|
+
})];
|
|
436
|
+
case 5:
|
|
437
|
+
_d.sent();
|
|
438
|
+
return [2 /*return*/, makeNack({
|
|
439
|
+
code: "conflict",
|
|
440
|
+
message: error.title,
|
|
441
|
+
mutationId: mutation.mutationId,
|
|
442
|
+
serverDoc: serverDoc,
|
|
443
|
+
serverSeq: error.serverSeq,
|
|
444
|
+
})];
|
|
445
|
+
case 6:
|
|
446
|
+
code = nackCodeForError(error);
|
|
447
|
+
message = (0, errors_1.isAPIError)(error) ? error.title : String(error);
|
|
448
|
+
if (!(code === "error")) return [3 /*break*/, 8];
|
|
449
|
+
logger_1.logger.warn("[sync] Mutation failed unexpectedly", {
|
|
450
|
+
collection: mutation.collection,
|
|
451
|
+
error: message,
|
|
452
|
+
mutationId: mutation.mutationId,
|
|
453
|
+
});
|
|
454
|
+
// Task 9.18: an `error`-class failure is non-deterministic (DB hiccup, replica
|
|
455
|
+
// step-down) — it is not a verdict on the mutation. Recording it `failed` would make
|
|
456
|
+
// it sticky for the ledger's 30-day TTL, so every replay of the same mutationId reads
|
|
457
|
+
// the nack back and the client's durable outbox retries forever without the mutation
|
|
458
|
+
// ever re-executing. Release the claim instead: the next delivery re-claims the
|
|
459
|
+
// mutationId and genuinely re-runs the write. Deterministic outcomes
|
|
460
|
+
// (validation/unauthorized/conflict) stay recorded — replaying those must not re-run.
|
|
461
|
+
return [4 /*yield*/, models_1.SyncMutation.deleteOne({ _id: claimedId })];
|
|
462
|
+
case 7:
|
|
463
|
+
// Task 9.18: an `error`-class failure is non-deterministic (DB hiccup, replica
|
|
464
|
+
// step-down) — it is not a verdict on the mutation. Recording it `failed` would make
|
|
465
|
+
// it sticky for the ledger's 30-day TTL, so every replay of the same mutationId reads
|
|
466
|
+
// the nack back and the client's durable outbox retries forever without the mutation
|
|
467
|
+
// ever re-executing. Release the claim instead: the next delivery re-claims the
|
|
468
|
+
// mutationId and genuinely re-runs the write. Deterministic outcomes
|
|
469
|
+
// (validation/unauthorized/conflict) stay recorded — replaying those must not re-run.
|
|
470
|
+
_d.sent();
|
|
471
|
+
return [2 /*return*/, makeNack({ code: code, message: message, mutationId: mutation.mutationId })];
|
|
472
|
+
case 8: return [4 /*yield*/, models_1.SyncMutation.updateOne({ _id: claimedId }, { $set: { error: message, nackCode: code, status: "failed" } })];
|
|
473
|
+
case 9:
|
|
474
|
+
_d.sent();
|
|
475
|
+
return [2 /*return*/, makeNack({ code: code, message: message, mutationId: mutation.mutationId })];
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
}); };
|
|
479
|
+
/**
|
|
480
|
+
* C5 (FIX 6): read the doc back and report it as the ack for an ALREADY-APPLIED write —
|
|
481
|
+
* used both by the create-E11000 tolerance and the update/delete already-applied check.
|
|
482
|
+
* Finalizes the ledger `applied` from the doc's actual state (not assumed) before
|
|
483
|
+
* returning, exactly like the normal apply path, so a subsequent duplicate delivery
|
|
484
|
+
* reads back the same outcome.
|
|
485
|
+
*/
|
|
486
|
+
var finalizeAlreadyApplied = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
487
|
+
var resultId, resultSeq;
|
|
488
|
+
var _c;
|
|
489
|
+
var claimedId = _b.claimedId, doc = _b.doc, mutation = _b.mutation;
|
|
490
|
+
return __generator(this, function (_d) {
|
|
491
|
+
switch (_d.label) {
|
|
492
|
+
case 0:
|
|
493
|
+
resultId = String(doc._id);
|
|
494
|
+
resultSeq = (_c = doc._syncSeq) !== null && _c !== void 0 ? _c : 0;
|
|
495
|
+
return [4 /*yield*/, models_1.SyncMutation.updateOne({ _id: claimedId }, { $set: { resultId: resultId, resultSeq: resultSeq, status: "applied" } })];
|
|
496
|
+
case 1:
|
|
497
|
+
_d.sent();
|
|
498
|
+
logger_1.logger.info("[sync] Mutation already applied (lease-takeover tolerance)", {
|
|
499
|
+
collection: mutation.collection,
|
|
500
|
+
mutationId: mutation.mutationId,
|
|
501
|
+
seq: resultSeq,
|
|
502
|
+
});
|
|
503
|
+
return [2 /*return*/, { resultId: resultId, resultSeq: resultSeq }];
|
|
504
|
+
}
|
|
505
|
+
});
|
|
506
|
+
}); };
|
|
507
|
+
/**
|
|
508
|
+
* Apply a client mutation through the transport-agnostic executors (permissions,
|
|
509
|
+
* pre/post hooks, validation) against an ALREADY-CLAIMED ledger row (either a fresh claim
|
|
510
|
+
* or a lease takeover of an abandoned one — C5/FIX 6). Always finalizes the claimed row
|
|
511
|
+
* before returning so duplicate deliveries can read the outcome back.
|
|
512
|
+
*
|
|
513
|
+
* Reorder (M5): the document write finalizes the ledger `applied` BEFORE the model's
|
|
514
|
+
* post-hook runs. A post-hook throw is logged and reported as a `warning` on the ack —
|
|
515
|
+
* never converted into a nack, since the write already committed.
|
|
516
|
+
*
|
|
517
|
+
* Already-applied tolerance (M4, `isLeaseTakeover` only — a normal first attempt reports
|
|
518
|
+
* genuine conflicts/errors as usual): a lease takeover re-runs a mutation whose FIRST
|
|
519
|
+
* attempt may have already landed the write before crashing.
|
|
520
|
+
* - create: an E11000 on the same target `_id` is treated as success — the doc is read
|
|
521
|
+
* back and reported as the ack (a genuine `_id` collision from a DIFFERENT mutation is
|
|
522
|
+
* not expected for client-minted ids and is out of scope here).
|
|
523
|
+
* - update: re-checked reactively — if the executor's seq concurrency check throws a
|
|
524
|
+
* conflict whose `serverSeq` is EXACTLY `baseVersion + 1` (what this mutation's own
|
|
525
|
+
* write would have produced) while retrying via a lease takeover, that is treated as
|
|
526
|
+
* "this exact write already landed" rather than a genuine external conflict, and the
|
|
527
|
+
* current doc is reported as the ack instead of nacking.
|
|
528
|
+
* - delete: idempotent already (executeDelete on an already-deleted tombstone is a no-op
|
|
529
|
+
* success in this codebase's soft-delete model), so no special tolerance is needed.
|
|
530
|
+
*/
|
|
531
|
+
var applyClaimedMutation = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
532
|
+
var mutationId, model, doc, postHook, body, result_1, createError_1, existing, _c, resultId_1, resultSeq_1, result_2, cleanedBody_1, prevDoc_1, updateError_1, _d, resultId_2, resultSeq_2, result_3, resultId, resultSeq, warning, postHookError_1, error_2;
|
|
533
|
+
var _e, _f, _g, _h, _j;
|
|
534
|
+
var claimed = _b.claimed, entry = _b.entry, mutation = _b.mutation, request = _b.request, user = _b.user, scopeResolver = _b.scopeResolver, _k = _b.isLeaseTakeover, isLeaseTakeover = _k === void 0 ? false : _k;
|
|
535
|
+
return __generator(this, function (_l) {
|
|
536
|
+
switch (_l.label) {
|
|
537
|
+
case 0:
|
|
538
|
+
mutationId = mutation.mutationId;
|
|
539
|
+
_l.label = 1;
|
|
540
|
+
case 1:
|
|
541
|
+
_l.trys.push([1, 25, , 26]);
|
|
542
|
+
model = mongoose_1.default.model(entry.modelName);
|
|
543
|
+
// C6 (M6): sync-boundary write-scope backstop before any write pipeline runs.
|
|
544
|
+
return [4 /*yield*/, enforceWriteScope({
|
|
545
|
+
data: mutation.data,
|
|
546
|
+
entry: entry,
|
|
547
|
+
operation: mutation.operation,
|
|
548
|
+
scopeResolver: scopeResolver,
|
|
549
|
+
user: user,
|
|
550
|
+
})];
|
|
551
|
+
case 2:
|
|
552
|
+
// C6 (M6): sync-boundary write-scope backstop before any write pipeline runs.
|
|
553
|
+
_l.sent();
|
|
554
|
+
doc = void 0;
|
|
555
|
+
postHook = void 0;
|
|
556
|
+
if (!(mutation.operation === "create")) return [3 /*break*/, 10];
|
|
557
|
+
body = __assign({}, ((_e = mutation.data) !== null && _e !== void 0 ? _e : {}));
|
|
558
|
+
if (mutation.id) {
|
|
559
|
+
body._id = mutation.id;
|
|
560
|
+
}
|
|
561
|
+
_l.label = 3;
|
|
562
|
+
case 3:
|
|
563
|
+
_l.trys.push([3, 5, , 9]);
|
|
564
|
+
return [4 /*yield*/, (0, executors_1.executeCreate)({
|
|
565
|
+
body: body,
|
|
566
|
+
model: model,
|
|
567
|
+
options: entry.options,
|
|
568
|
+
req: request,
|
|
569
|
+
skipPostHooks: true,
|
|
570
|
+
user: user,
|
|
571
|
+
})];
|
|
572
|
+
case 4:
|
|
573
|
+
result_1 = _l.sent();
|
|
574
|
+
doc = result_1.doc;
|
|
575
|
+
postHook = function () {
|
|
576
|
+
return (0, executors_1.runPostCreate)({ doc: result_1.doc, options: entry.options, request: request });
|
|
577
|
+
};
|
|
578
|
+
return [3 /*break*/, 9];
|
|
579
|
+
case 5:
|
|
580
|
+
createError_1 = _l.sent();
|
|
581
|
+
if (!(isLeaseTakeover && isE11000Error(createError_1) && typeof mutation.id === "string")) return [3 /*break*/, 8];
|
|
582
|
+
return [4 /*yield*/, (0, plugins_1.findOneOrNoneFor)(model, { _id: mutation.id })];
|
|
583
|
+
case 6:
|
|
584
|
+
existing = _l.sent();
|
|
585
|
+
if (!existing) return [3 /*break*/, 8];
|
|
586
|
+
return [4 /*yield*/, finalizeAlreadyApplied({
|
|
587
|
+
claimedId: claimed._id,
|
|
588
|
+
doc: existing,
|
|
589
|
+
mutation: mutation,
|
|
590
|
+
})];
|
|
591
|
+
case 7:
|
|
592
|
+
_c = _l.sent(), resultId_1 = _c.resultId, resultSeq_1 = _c.resultSeq;
|
|
593
|
+
return [2 /*return*/, { ack: { id: resultId_1, mutationId: mutationId, seq: resultSeq_1 }, type: "ack" }];
|
|
594
|
+
case 8: throw createError_1;
|
|
595
|
+
case 9: return [3 /*break*/, 19];
|
|
596
|
+
case 10:
|
|
597
|
+
if (!(mutation.operation === "update")) return [3 /*break*/, 17];
|
|
598
|
+
_l.label = 11;
|
|
599
|
+
case 11:
|
|
600
|
+
_l.trys.push([11, 13, , 16]);
|
|
601
|
+
return [4 /*yield*/, (0, executors_1.executeUpdate)({
|
|
602
|
+
body: (_f = mutation.data) !== null && _f !== void 0 ? _f : {},
|
|
603
|
+
concurrencyCheck: { baseSeq: (_g = mutation.baseVersion) !== null && _g !== void 0 ? _g : 0, type: "seq" },
|
|
604
|
+
id: mutation.id,
|
|
605
|
+
model: model,
|
|
606
|
+
options: entry.options,
|
|
607
|
+
req: request,
|
|
608
|
+
skipPostHooks: true,
|
|
609
|
+
user: user,
|
|
610
|
+
})];
|
|
611
|
+
case 12:
|
|
612
|
+
result_2 = _l.sent();
|
|
613
|
+
doc = result_2.doc;
|
|
614
|
+
cleanedBody_1 = result_2.cleanedBody;
|
|
615
|
+
prevDoc_1 = result_2.prevDoc;
|
|
616
|
+
postHook = function () {
|
|
617
|
+
return (0, executors_1.runPostUpdate)({ cleanedBody: cleanedBody_1, doc: result_2.doc, options: entry.options, prevDoc: prevDoc_1, request: request });
|
|
618
|
+
};
|
|
619
|
+
return [3 /*break*/, 16];
|
|
620
|
+
case 13:
|
|
621
|
+
updateError_1 = _l.sent();
|
|
622
|
+
if (!(isLeaseTakeover &&
|
|
623
|
+
(0, executors_1.isExecutorConflictError)(updateError_1) &&
|
|
624
|
+
updateError_1.conflictType === "seq" &&
|
|
625
|
+
updateError_1.serverSeq === ((_h = mutation.baseVersion) !== null && _h !== void 0 ? _h : 0) + 1 &&
|
|
626
|
+
docMatchesMutationData(updateError_1.doc, mutation.data))) return [3 /*break*/, 15];
|
|
627
|
+
return [4 /*yield*/, finalizeAlreadyApplied({
|
|
628
|
+
claimedId: claimed._id,
|
|
629
|
+
doc: updateError_1.doc,
|
|
630
|
+
mutation: mutation,
|
|
631
|
+
})];
|
|
632
|
+
case 14:
|
|
633
|
+
_d = _l.sent(), resultId_2 = _d.resultId, resultSeq_2 = _d.resultSeq;
|
|
634
|
+
return [2 /*return*/, { ack: { id: resultId_2, mutationId: mutationId, seq: resultSeq_2 }, type: "ack" }];
|
|
635
|
+
case 15: throw updateError_1;
|
|
636
|
+
case 16: return [3 /*break*/, 19];
|
|
637
|
+
case 17: return [4 /*yield*/, (0, executors_1.executeDelete)({
|
|
638
|
+
id: mutation.id,
|
|
639
|
+
model: model,
|
|
640
|
+
options: entry.options,
|
|
641
|
+
req: request,
|
|
642
|
+
skipPostHooks: true,
|
|
643
|
+
user: user,
|
|
644
|
+
})];
|
|
645
|
+
case 18:
|
|
646
|
+
result_3 = _l.sent();
|
|
647
|
+
doc = result_3.doc;
|
|
648
|
+
postHook = function () {
|
|
649
|
+
return (0, executors_1.runPostDelete)({ doc: result_3.doc, options: entry.options, request: request });
|
|
650
|
+
};
|
|
651
|
+
_l.label = 19;
|
|
652
|
+
case 19:
|
|
653
|
+
resultId = String(doc._id);
|
|
654
|
+
resultSeq = (_j = doc._syncSeq) !== null && _j !== void 0 ? _j : 0;
|
|
655
|
+
// M5: finalize the ledger BEFORE the post-hook runs — a post-hook throw
|
|
656
|
+
// must never make a committed write look like a failure.
|
|
657
|
+
return [4 /*yield*/, models_1.SyncMutation.updateOne({ _id: claimed._id }, { $set: { resultId: resultId, resultSeq: resultSeq, status: "applied" } })];
|
|
658
|
+
case 20:
|
|
659
|
+
// M5: finalize the ledger BEFORE the post-hook runs — a post-hook throw
|
|
660
|
+
// must never make a committed write look like a failure.
|
|
661
|
+
_l.sent();
|
|
662
|
+
syncDebugLog.debug("[sync] Mutation applied", {
|
|
663
|
+
collection: mutation.collection,
|
|
664
|
+
mutationId: mutationId,
|
|
665
|
+
seq: resultSeq,
|
|
666
|
+
});
|
|
667
|
+
warning = void 0;
|
|
668
|
+
_l.label = 21;
|
|
669
|
+
case 21:
|
|
670
|
+
_l.trys.push([21, 23, , 24]);
|
|
671
|
+
return [4 /*yield*/, postHook()];
|
|
672
|
+
case 22:
|
|
673
|
+
_l.sent();
|
|
674
|
+
return [3 /*break*/, 24];
|
|
675
|
+
case 23:
|
|
676
|
+
postHookError_1 = _l.sent();
|
|
677
|
+
warning = errorMessageOf(postHookError_1);
|
|
678
|
+
logger_1.logger.error("[sync] Post-hook failed after a committed mutation", {
|
|
679
|
+
collection: mutation.collection,
|
|
680
|
+
error: warning,
|
|
681
|
+
mutationId: mutationId,
|
|
682
|
+
});
|
|
683
|
+
return [3 /*break*/, 24];
|
|
684
|
+
case 24: return [2 /*return*/, {
|
|
685
|
+
ack: __assign({ id: resultId, mutationId: mutationId, seq: resultSeq }, (warning ? { warning: warning } : {})),
|
|
686
|
+
type: "ack",
|
|
687
|
+
}];
|
|
688
|
+
case 25:
|
|
689
|
+
error_2 = _l.sent();
|
|
690
|
+
return [2 /*return*/, finalizeNack({ claimedId: claimed._id, entry: entry, error: error_2, mutation: mutation, request: request })];
|
|
691
|
+
case 26: return [2 /*return*/];
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
}); };
|
|
695
|
+
var errorMessageOf = function (error) {
|
|
696
|
+
return (0, errors_1.isAPIError)(error) ? error.title : error instanceof Error ? error.message : String(error);
|
|
697
|
+
};
|
|
698
|
+
/**
|
|
699
|
+
* Apply a client mutation through the transport-agnostic executors (permissions,
|
|
700
|
+
* pre/post hooks, validation) with an atomic idempotency claim. Always finalizes the
|
|
701
|
+
* claimed ledger row before returning so duplicate deliveries can read the outcome back.
|
|
702
|
+
*/
|
|
703
|
+
var applySyncMutation = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
704
|
+
var mutationId, validationNack, entry, request, claimed, error_3;
|
|
705
|
+
var user = _b.user, mutation = _b.mutation, req = _b.req, scopeResolver = _b.scopeResolver;
|
|
706
|
+
return __generator(this, function (_c) {
|
|
707
|
+
switch (_c.label) {
|
|
708
|
+
case 0:
|
|
709
|
+
mutationId = typeof (mutation === null || mutation === void 0 ? void 0 : mutation.mutationId) === "string" ? mutation.mutationId : "";
|
|
710
|
+
validationNack = function (message) {
|
|
711
|
+
return makeNack({ code: "validation", message: message, mutationId: mutationId });
|
|
712
|
+
};
|
|
713
|
+
if (!mutationId) {
|
|
714
|
+
return [2 /*return*/, validationNack("mutationId is required")];
|
|
715
|
+
}
|
|
716
|
+
if (!mutation.collection || typeof mutation.collection !== "string") {
|
|
717
|
+
return [2 /*return*/, validationNack("collection is required")];
|
|
718
|
+
}
|
|
719
|
+
if (!VALID_OPERATIONS.has(mutation.operation)) {
|
|
720
|
+
return [2 /*return*/, validationNack("Unknown operation: ".concat(String(mutation.operation)))];
|
|
721
|
+
}
|
|
722
|
+
if ((mutation.operation === "update" || mutation.operation === "delete") && !mutation.id) {
|
|
723
|
+
return [2 /*return*/, validationNack("id is required for ".concat(mutation.operation, " mutations"))];
|
|
724
|
+
}
|
|
725
|
+
// C8: an update MUST carry an explicit baseVersion — omitting it would silently become
|
|
726
|
+
// a `baseSeq: 0` check (an accidental conflict against every non-fresh doc, or an
|
|
727
|
+
// accidental success against a seq-0 doc). Reject it as a client bug up front.
|
|
728
|
+
if (mutation.operation === "update" && typeof mutation.baseVersion !== "number") {
|
|
729
|
+
return [2 /*return*/, validationNack("baseVersion is required for update mutations")];
|
|
730
|
+
}
|
|
731
|
+
entry = (0, registry_1.findSyncEntryByCollectionTag)(mutation.collection);
|
|
732
|
+
if (!entry) {
|
|
733
|
+
return [2 /*return*/, validationNack("Unknown sync collection: ".concat(mutation.collection))];
|
|
734
|
+
}
|
|
735
|
+
request = req !== null && req !== void 0 ? req : { user: user };
|
|
736
|
+
_c.label = 1;
|
|
737
|
+
case 1:
|
|
738
|
+
_c.trys.push([1, 3, , 4]);
|
|
739
|
+
return [4 /*yield*/, models_1.SyncMutation.create({
|
|
740
|
+
claimedAt: new Date(),
|
|
741
|
+
mutationId: mutationId,
|
|
742
|
+
status: "pending",
|
|
743
|
+
userId: String(user.id),
|
|
744
|
+
})];
|
|
745
|
+
case 2:
|
|
746
|
+
claimed = _c.sent();
|
|
747
|
+
return [3 /*break*/, 4];
|
|
748
|
+
case 3:
|
|
749
|
+
error_3 = _c.sent();
|
|
750
|
+
if (error_3.code !== 11000) {
|
|
751
|
+
throw error_3;
|
|
752
|
+
}
|
|
753
|
+
return [2 /*return*/, waitForRecordedOutcome({
|
|
754
|
+
entry: entry,
|
|
755
|
+
mutationId: mutationId,
|
|
756
|
+
onTakeover: function (takenOver) {
|
|
757
|
+
return applyClaimedMutation({
|
|
758
|
+
claimed: takenOver,
|
|
759
|
+
entry: entry,
|
|
760
|
+
isLeaseTakeover: true,
|
|
761
|
+
mutation: mutation,
|
|
762
|
+
request: request,
|
|
763
|
+
scopeResolver: scopeResolver,
|
|
764
|
+
user: user,
|
|
765
|
+
});
|
|
766
|
+
},
|
|
767
|
+
request: request,
|
|
768
|
+
user: user,
|
|
769
|
+
})];
|
|
770
|
+
case 4: return [2 /*return*/, applyClaimedMutation({ claimed: claimed, entry: entry, mutation: mutation, request: request, scopeResolver: scopeResolver, user: user })];
|
|
771
|
+
}
|
|
772
|
+
});
|
|
773
|
+
}); };
|
|
774
|
+
exports.applySyncMutation = applySyncMutation;
|
|
775
|
+
/**
|
|
776
|
+
* Up-front validation shared by both batch transports (HTTP and socket): reject an
|
|
777
|
+
* oversized batch or one with intra-batch duplicate mutationIds before anything is
|
|
778
|
+
* applied. On failure, returns a single-element `results` array carrying a
|
|
779
|
+
* `validation` nack for the offending mutation (or an empty-batch guard) — mirroring
|
|
780
|
+
* the shape callers expect from `applySyncMutationBatch`, but produced without
|
|
781
|
+
* touching the idempotency ledger since nothing was attempted.
|
|
782
|
+
*/
|
|
783
|
+
var validateSyncMutationBatch = function (mutations) {
|
|
784
|
+
var e_1, _a;
|
|
785
|
+
if (mutations.length > exports.MAX_SYNC_MUTATIONS_PER_BATCH) {
|
|
786
|
+
return {
|
|
787
|
+
ok: false,
|
|
788
|
+
response: {
|
|
789
|
+
results: [
|
|
790
|
+
makeNack({
|
|
791
|
+
code: "validation",
|
|
792
|
+
message: "Batch of ".concat(mutations.length, " exceeds the maximum of ").concat(exports.MAX_SYNC_MUTATIONS_PER_BATCH, " mutations"),
|
|
793
|
+
mutationId: "",
|
|
794
|
+
}),
|
|
795
|
+
],
|
|
796
|
+
},
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
var seen = new Set();
|
|
800
|
+
try {
|
|
801
|
+
for (var mutations_1 = __values(mutations), mutations_1_1 = mutations_1.next(); !mutations_1_1.done; mutations_1_1 = mutations_1.next()) {
|
|
802
|
+
var mutation = mutations_1_1.value;
|
|
803
|
+
var mutationId = typeof (mutation === null || mutation === void 0 ? void 0 : mutation.mutationId) === "string" ? mutation.mutationId : "";
|
|
804
|
+
if (mutationId && seen.has(mutationId)) {
|
|
805
|
+
return {
|
|
806
|
+
ok: false,
|
|
807
|
+
response: {
|
|
808
|
+
results: [
|
|
809
|
+
makeNack({
|
|
810
|
+
code: "validation",
|
|
811
|
+
message: "Duplicate mutationId within batch: ".concat(mutationId),
|
|
812
|
+
mutationId: mutationId,
|
|
813
|
+
}),
|
|
814
|
+
],
|
|
815
|
+
},
|
|
816
|
+
};
|
|
817
|
+
}
|
|
818
|
+
seen.add(mutationId);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
822
|
+
finally {
|
|
823
|
+
try {
|
|
824
|
+
if (mutations_1_1 && !mutations_1_1.done && (_a = mutations_1.return)) _a.call(mutations_1);
|
|
825
|
+
}
|
|
826
|
+
finally { if (e_1) throw e_1.error; }
|
|
827
|
+
}
|
|
828
|
+
return { ok: true };
|
|
829
|
+
};
|
|
830
|
+
exports.validateSyncMutationBatch = validateSyncMutationBatch;
|
|
831
|
+
/**
|
|
832
|
+
* Apply a batch of mutations strictly serially, in array order, reusing
|
|
833
|
+
* `applySyncMutation` per item — full reuse of the idempotency ledger, executors,
|
|
834
|
+
* permissions, and delta emission. Stops immediately at the first non-ack outcome
|
|
835
|
+
* (the user's hard requirement, INV-1): mutations after it are neither attempted nor
|
|
836
|
+
* ledgered, and are safe for the client to resend later (INV-3).
|
|
837
|
+
*
|
|
838
|
+
* Callers MUST run {@link validateSyncMutationBatch} first (oversized/duplicate
|
|
839
|
+
* rejection happens before any mutation is attempted); this function assumes the
|
|
840
|
+
* batch already passed that check.
|
|
841
|
+
*/
|
|
842
|
+
var applySyncMutationBatch = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
843
|
+
var results, mutations_2, mutations_2_1, mutation, outcome, e_2_1;
|
|
844
|
+
var e_2, _c;
|
|
845
|
+
var user = _b.user, mutations = _b.mutations, req = _b.req, scopeResolver = _b.scopeResolver;
|
|
846
|
+
return __generator(this, function (_d) {
|
|
847
|
+
switch (_d.label) {
|
|
848
|
+
case 0:
|
|
849
|
+
results = [];
|
|
850
|
+
_d.label = 1;
|
|
851
|
+
case 1:
|
|
852
|
+
_d.trys.push([1, 6, 7, 8]);
|
|
853
|
+
mutations_2 = __values(mutations), mutations_2_1 = mutations_2.next();
|
|
854
|
+
_d.label = 2;
|
|
855
|
+
case 2:
|
|
856
|
+
if (!!mutations_2_1.done) return [3 /*break*/, 5];
|
|
857
|
+
mutation = mutations_2_1.value;
|
|
858
|
+
return [4 /*yield*/, (0, exports.applySyncMutation)({ mutation: mutation, req: req, scopeResolver: scopeResolver, user: user })];
|
|
859
|
+
case 3:
|
|
860
|
+
outcome = _d.sent();
|
|
861
|
+
results.push(outcome);
|
|
862
|
+
if (outcome.type === "nack") {
|
|
863
|
+
// Stop-on-error: the client re-sends everything after this point, and
|
|
864
|
+
// INV-3's idempotency ledger makes that overlap safe.
|
|
865
|
+
return [3 /*break*/, 5];
|
|
866
|
+
}
|
|
867
|
+
_d.label = 4;
|
|
868
|
+
case 4:
|
|
869
|
+
mutations_2_1 = mutations_2.next();
|
|
870
|
+
return [3 /*break*/, 2];
|
|
871
|
+
case 5: return [3 /*break*/, 8];
|
|
872
|
+
case 6:
|
|
873
|
+
e_2_1 = _d.sent();
|
|
874
|
+
e_2 = { error: e_2_1 };
|
|
875
|
+
return [3 /*break*/, 8];
|
|
876
|
+
case 7:
|
|
877
|
+
try {
|
|
878
|
+
if (mutations_2_1 && !mutations_2_1.done && (_c = mutations_2.return)) _c.call(mutations_2);
|
|
879
|
+
}
|
|
880
|
+
finally { if (e_2) throw e_2.error; }
|
|
881
|
+
return [7 /*endfinally*/];
|
|
882
|
+
case 8: return [2 /*return*/, { results: results }];
|
|
883
|
+
}
|
|
884
|
+
});
|
|
885
|
+
}); };
|
|
886
|
+
exports.applySyncMutationBatch = applySyncMutationBatch;
|