@terreno/api 0.30.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/dist/api.d.ts +10 -0
- 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.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 +35 -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 +73 -21
- 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/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/actions.openApi.test.ts +7 -10
- package/src/api.test.ts +12 -0
- package/src/api.ts +87 -295
- 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 +20 -1
- 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 +40 -1
- package/src/middleware.ts +15 -0
- package/src/openApiCompat.ts +99 -45
- package/src/permissions.test.ts +75 -1
- package/src/permissions.ts +49 -0
- package/src/plugins.test.ts +54 -0
- package/src/plugins.ts +43 -0
- package/src/realtime/changeStreamWatcher.ts +552 -114
- package/src/realtime/realtime.test.ts +157 -0
- package/src/realtime/realtimeApp.ts +75 -11
- 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/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 -255
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -1089
- 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 -194
- package/dist/api.errorPaths.test.d.ts +0 -1
- package/dist/api.errorPaths.test.js +0 -801
- 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 -1415
- 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 -601
- 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 -761
- 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 -890
- package/dist/populate.test.d.ts +0 -1
- package/dist/populate.test.js +0 -373
- package/dist/realtime/realtime.test.d.ts +0 -10
- package/dist/realtime/realtime.test.js +0 -3982
- 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,903 @@
|
|
|
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 __values = (this && this.__values) || function(o) {
|
|
50
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
51
|
+
if (m) return m.call(o);
|
|
52
|
+
if (o && typeof o.length === "number") return {
|
|
53
|
+
next: function () {
|
|
54
|
+
if (o && i >= o.length) o = void 0;
|
|
55
|
+
return { value: o && o[i++], done: !o };
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
59
|
+
};
|
|
60
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
61
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
62
|
+
if (!m) return o;
|
|
63
|
+
var i = m.call(o), r, ar = [], e;
|
|
64
|
+
try {
|
|
65
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
66
|
+
}
|
|
67
|
+
catch (error) { e = { error: error }; }
|
|
68
|
+
finally {
|
|
69
|
+
try {
|
|
70
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
71
|
+
}
|
|
72
|
+
finally { if (e) throw e.error; }
|
|
73
|
+
}
|
|
74
|
+
return ar;
|
|
75
|
+
};
|
|
76
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
77
|
+
exports.syncPlugin = exports.SCOPE_MOVE_MARKER_ATTEMPTS = void 0;
|
|
78
|
+
var logger_1 = require("../logger");
|
|
79
|
+
var executors_1 = require("./executors");
|
|
80
|
+
var models_1 = require("./models");
|
|
81
|
+
var registry_1 = require("./registry");
|
|
82
|
+
var streams_1 = require("./streams");
|
|
83
|
+
/**
|
|
84
|
+
* Schema plugin for sync-enabled models. Stamps a monotonic per-stream `_syncSeq` on
|
|
85
|
+
* every single-document write and records `_syncPrevStream` when a write moves the
|
|
86
|
+
* document between scopes (owner/tenant change).
|
|
87
|
+
*
|
|
88
|
+
* Apply to the schema alongside `isDeletedPlugin`; activation is keyed off the sync
|
|
89
|
+
* registry, so models that are not registered via modelRouter's `sync` option no-op.
|
|
90
|
+
*
|
|
91
|
+
* Sequencing guarantees (C1 — stable frontier):
|
|
92
|
+
* - Validation failures never consume a seq: Mongoose runs validation before user
|
|
93
|
+
* pre('save') hooks, so the claim happens post-validation.
|
|
94
|
+
* - Each claim registers the claimed seq on the counter's in-flight `pending` registry;
|
|
95
|
+
* the matching post-write hook (`confirmSyncSeqs`) clears it. Until confirmed, the
|
|
96
|
+
* stream's stable frontier holds below the claimed seq, so a snapshot/delta cursor
|
|
97
|
+
* never advances past a seq whose owning write has not yet committed. A crash between
|
|
98
|
+
* claim and confirm leaves a stale pending entry that ages out via the lease.
|
|
99
|
+
* - The claim joins the caller's session when one is present; that path gets true
|
|
100
|
+
* counter+write atomicity and skips the pending registry entirely (no confirm due).
|
|
101
|
+
* - Every write path claims in its pre hook and confirms in its POST hook — single saves,
|
|
102
|
+
* `insertMany` batches, and query-writes alike. Confirming before the documents commit
|
|
103
|
+
* would let a cursor advance past seqs that have not landed, permanently stranding those
|
|
104
|
+
* documents below every catch-up cursor.
|
|
105
|
+
*
|
|
106
|
+
* Scope moves (C4): when a write moves the document between streams, a durable
|
|
107
|
+
* `SyncScopeMove` marker is written AFTER the move commits, carrying a seq claimed from
|
|
108
|
+
* the OLD stream's counter. The change-stream watcher and old-stream snapshot catch-up
|
|
109
|
+
* tombstone the document from the marker (not the racy `_syncPrevStream` post-image),
|
|
110
|
+
* so a racing second write can no longer erase the tombstone. `_syncPrevStream` is still
|
|
111
|
+
* stamped (harmless), but the marker is the source of truth. Writing the marker
|
|
112
|
+
* post-commit keeps a failed write from leaving a phantom tombstone; the insert is
|
|
113
|
+
* retried ({@link SCOPE_MOVE_MARKER_ATTEMPTS}) because a committed move whose marker is
|
|
114
|
+
* lost can never be tombstoned on the old stream.
|
|
115
|
+
*
|
|
116
|
+
* Lost-update protection (Task 9.13): the plugin turns on Mongoose
|
|
117
|
+
* `optimisticConcurrency`, so `doc.save()` on an existing document filters by the loaded
|
|
118
|
+
* `__v` and throws a `VersionError` when another write landed in between. This closes the
|
|
119
|
+
* read-compare-save TOCTOU window in `executeUpdate`'s `baseVersion` check — two clients
|
|
120
|
+
* sending the same `baseVersion` concurrently now produce exactly one ack and one conflict
|
|
121
|
+
* instead of two acks with one edit silently lost. The loser's already-claimed seq is
|
|
122
|
+
* released (never stamped on anything), so the stream's frontier does not stall for the
|
|
123
|
+
* pending-claim lease.
|
|
124
|
+
*
|
|
125
|
+
* Write restrictions:
|
|
126
|
+
* - `updateMany`, `deleteMany`, hard deletes, and `bulkWrite` are unsupported on synced
|
|
127
|
+
* models and all throw. The first three are guarded here; `bulkWrite` bypasses
|
|
128
|
+
* middleware entirely, so `registerSync` replaces that static on the model instead.
|
|
129
|
+
* - Query-writes MUST target a single document by `_id` (m9): a non-`_id` filter could
|
|
130
|
+
* match a different document than intended and stamp the wrong stream's seq.
|
|
131
|
+
* - `upsert: true` is rejected on query-writes (m8/C6): an upsert can create a document
|
|
132
|
+
* the pre-write lookup never saw, escaping seq stamping.
|
|
133
|
+
*/
|
|
134
|
+
var INITIAL_STREAM_KEY = "_syncInitialStream";
|
|
135
|
+
/**
|
|
136
|
+
* Resolve the stream for a plain object under an entry's scope. Total by design: this also
|
|
137
|
+
* runs on documents already in the database (hydration, post-commit seq confirmation, and
|
|
138
|
+
* the previous-stream lookup for a scope move), where a legacy row with no scope value
|
|
139
|
+
* must still be nameable rather than unreadable.
|
|
140
|
+
*/
|
|
141
|
+
var streamForObject = function (entry, obj) {
|
|
142
|
+
return (0, streams_1.resolveStreamForDoc)({ collectionTag: entry.collectionTag, doc: obj, scope: entry.config.scope });
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Same, for the document a write is about to persist: refuses a tenant-scoped write with
|
|
146
|
+
* no scope value rather than filing it under an unsubscribable `tenant:undefined` stream
|
|
147
|
+
* (Task 9.21). Only the seq-stamping write paths use this — reads stay total.
|
|
148
|
+
*/
|
|
149
|
+
var writableStreamForObject = function (entry, obj) {
|
|
150
|
+
(0, streams_1.assertWritableStream)({ collectionTag: entry.collectionTag, doc: obj, scope: entry.config.scope });
|
|
151
|
+
return streamForObject(entry, obj);
|
|
152
|
+
};
|
|
153
|
+
var unsupportedWrite = function (modelName, operation) {
|
|
154
|
+
return new Error("".concat(operation, " is not supported on sync-enabled model ").concat(modelName, ": per-document seq ") +
|
|
155
|
+
"stamping requires single-document writes (and deletes must be soft). " +
|
|
156
|
+
"Loop per document instead.");
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* m9: a query-write on a synced model must target exactly one document by `_id`, so the
|
|
160
|
+
* pre-write lookup resolves the stream of the SAME document the update mutates. A non-`_id`
|
|
161
|
+
* filter can match a different document and stamp the wrong stream's seq (duplicate seqs
|
|
162
|
+
* within a stream). Accept `{_id: value}` and `{_id: {$eq: value}}`.
|
|
163
|
+
*/
|
|
164
|
+
var filterTargetsSingleId = function (filter) {
|
|
165
|
+
var idClause = filter._id;
|
|
166
|
+
if (idClause === undefined || idClause === null) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
// A plain string/number id, or an ObjectId (or any BSON value — has a `_bsontype`),
|
|
170
|
+
// is a direct single-document match.
|
|
171
|
+
if (typeof idClause !== "object" || idClause._bsontype) {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
// A query-operator object is only single-document when it is exactly `{$eq: value}`.
|
|
175
|
+
var keys = Object.keys(idClause);
|
|
176
|
+
return keys.length === 1 && keys[0] === "$eq";
|
|
177
|
+
};
|
|
178
|
+
var nonIdFilterError = function (modelName, operation) {
|
|
179
|
+
return new Error("".concat(operation, " on sync-enabled model ").concat(modelName, " must target a single document by _id ") +
|
|
180
|
+
"(use findByIdAndUpdate or loop per document): a non-_id filter can stamp the wrong " +
|
|
181
|
+
"stream's seq.");
|
|
182
|
+
};
|
|
183
|
+
var upsertError = function (modelName, operation) {
|
|
184
|
+
return new Error("".concat(operation, " with upsert:true is not supported on sync-enabled model ").concat(modelName, ": an ") +
|
|
185
|
+
"upsert can create a document the pre-write lookup never saw, escaping seq stamping.");
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* C4: how many times a scope-move marker insert is attempted before giving up. Losing the
|
|
189
|
+
* marker loses the old stream's tombstone forever (the exact race C4 exists to eliminate),
|
|
190
|
+
* so a transient failure (replica step-down, transient network error) is retried.
|
|
191
|
+
*/
|
|
192
|
+
exports.SCOPE_MOVE_MARKER_ATTEMPTS = 3;
|
|
193
|
+
/** Backoff between marker-write attempts. */
|
|
194
|
+
var SCOPE_MOVE_MARKER_RETRY_MS = 25;
|
|
195
|
+
var delay = function (ms) {
|
|
196
|
+
return new Promise(function (resolve) {
|
|
197
|
+
setTimeout(resolve, ms);
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* C4: write the scope-move marker (claim a seq from the OLD stream so it orders in that
|
|
202
|
+
* stream's frontier). Runs AFTER the move commits, so it never leaves a phantom tombstone
|
|
203
|
+
* for a write that failed; the insert is retried up to {@link SCOPE_MOVE_MARKER_ATTEMPTS}
|
|
204
|
+
* times because the already-committed move cannot be undone if the marker is lost.
|
|
205
|
+
*
|
|
206
|
+
* Never throws: the owning write has already committed, so a marker failure is logged
|
|
207
|
+
* rather than surfaced to the caller.
|
|
208
|
+
*/
|
|
209
|
+
var writeScopeMoveMarker = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
210
|
+
var claim, error_1, attempt, error_2, exhausted;
|
|
211
|
+
var entry = _b.entry, entityId = _b.entityId, fromStream = _b.fromStream, toStream = _b.toStream, session = _b.session;
|
|
212
|
+
return __generator(this, function (_c) {
|
|
213
|
+
switch (_c.label) {
|
|
214
|
+
case 0:
|
|
215
|
+
_c.trys.push([0, 2, , 3]);
|
|
216
|
+
return [4 /*yield*/, (0, models_1.claimSyncSeqs)({ session: session, stream: fromStream })];
|
|
217
|
+
case 1:
|
|
218
|
+
claim = _c.sent();
|
|
219
|
+
return [3 /*break*/, 3];
|
|
220
|
+
case 2:
|
|
221
|
+
error_1 = _c.sent();
|
|
222
|
+
logger_1.logger.error("[sync] Failed to claim scope-move marker seq", {
|
|
223
|
+
entityId: entityId,
|
|
224
|
+
error: String(error_1),
|
|
225
|
+
fromStream: fromStream,
|
|
226
|
+
});
|
|
227
|
+
return [2 /*return*/];
|
|
228
|
+
case 3:
|
|
229
|
+
attempt = 1;
|
|
230
|
+
_c.label = 4;
|
|
231
|
+
case 4:
|
|
232
|
+
if (!(attempt <= exports.SCOPE_MOVE_MARKER_ATTEMPTS)) return [3 /*break*/, 12];
|
|
233
|
+
_c.label = 5;
|
|
234
|
+
case 5:
|
|
235
|
+
_c.trys.push([5, 9, , 11]);
|
|
236
|
+
return [4 /*yield*/, models_1.SyncScopeMove.create([
|
|
237
|
+
{
|
|
238
|
+
collectionTag: entry.collectionTag,
|
|
239
|
+
entityId: entityId,
|
|
240
|
+
fromStream: fromStream,
|
|
241
|
+
seq: claim.lastSeq,
|
|
242
|
+
toStream: toStream,
|
|
243
|
+
},
|
|
244
|
+
], session ? { session: session } : {})];
|
|
245
|
+
case 6:
|
|
246
|
+
_c.sent();
|
|
247
|
+
if (!claim.registered) return [3 /*break*/, 8];
|
|
248
|
+
return [4 /*yield*/, (0, models_1.confirmSyncSeqs)({ seqs: claim.seqs, stream: fromStream }).catch(function (error) {
|
|
249
|
+
logger_1.logger.error("[sync] Failed to confirm scope-move marker seq", {
|
|
250
|
+
error: String(error),
|
|
251
|
+
stream: fromStream,
|
|
252
|
+
});
|
|
253
|
+
})];
|
|
254
|
+
case 7:
|
|
255
|
+
_c.sent();
|
|
256
|
+
_c.label = 8;
|
|
257
|
+
case 8: return [2 /*return*/];
|
|
258
|
+
case 9:
|
|
259
|
+
error_2 = _c.sent();
|
|
260
|
+
exhausted = attempt === exports.SCOPE_MOVE_MARKER_ATTEMPTS;
|
|
261
|
+
logger_1.logger.error("[sync] Failed to write scope-move marker", {
|
|
262
|
+
attempt: attempt,
|
|
263
|
+
entityId: entityId,
|
|
264
|
+
error: String(error_2),
|
|
265
|
+
exhausted: exhausted,
|
|
266
|
+
fromStream: fromStream,
|
|
267
|
+
});
|
|
268
|
+
if (exhausted) {
|
|
269
|
+
// The claim is left pending: the old stream's frontier holds below the missing
|
|
270
|
+
// tombstone until the lease ages it out, rather than skipping it silently.
|
|
271
|
+
return [2 /*return*/];
|
|
272
|
+
}
|
|
273
|
+
return [4 /*yield*/, delay(SCOPE_MOVE_MARKER_RETRY_MS)];
|
|
274
|
+
case 10:
|
|
275
|
+
_c.sent();
|
|
276
|
+
return [3 /*break*/, 11];
|
|
277
|
+
case 11:
|
|
278
|
+
attempt++;
|
|
279
|
+
return [3 /*break*/, 4];
|
|
280
|
+
case 12: return [2 /*return*/];
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
}); };
|
|
284
|
+
/**
|
|
285
|
+
* Task 9.17: the confirm found no pending entry, meaning this writer's claim was reaped as
|
|
286
|
+
* abandoned (it stalled past {@link PENDING_CLAIM_LEASE_MS} without crashing) and the
|
|
287
|
+
* frontier has already advanced past `reapedSeq`. The document just committed BELOW every
|
|
288
|
+
* catch-up cursor, so re-stamp it with a freshly claimed seq: the raw-collection write
|
|
289
|
+
* bypasses this plugin's hooks (no recursion, no second claim) while still producing a
|
|
290
|
+
* change event, so online clients get a delta and offline ones see it on catch-up.
|
|
291
|
+
*
|
|
292
|
+
* Returns the new seq, or undefined when re-stamping failed (logged; the document stays
|
|
293
|
+
* below the frontier and only a full re-bootstrap recovers it).
|
|
294
|
+
*/
|
|
295
|
+
var restampReapedSeq = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
296
|
+
var claim, result, confirmed, error_3;
|
|
297
|
+
var _c;
|
|
298
|
+
var model = _b.model, id = _b.id, stream = _b.stream, reapedSeq = _b.reapedSeq;
|
|
299
|
+
return __generator(this, function (_d) {
|
|
300
|
+
switch (_d.label) {
|
|
301
|
+
case 0:
|
|
302
|
+
logger_1.logger.error("[sync] Seq claim was reaped before its write committed; re-stamping the document above " +
|
|
303
|
+
"the frontier", { entityId: String(id), model: model.modelName, reapedSeq: reapedSeq, stream: stream });
|
|
304
|
+
_d.label = 1;
|
|
305
|
+
case 1:
|
|
306
|
+
_d.trys.push([1, 7, , 8]);
|
|
307
|
+
return [4 /*yield*/, (0, models_1.claimSyncSeqs)({ stream: stream })];
|
|
308
|
+
case 2:
|
|
309
|
+
claim = _d.sent();
|
|
310
|
+
return [4 /*yield*/, model.collection.updateOne({ _id: id }, { $set: { _syncSeq: claim.lastSeq } })];
|
|
311
|
+
case 3:
|
|
312
|
+
result = _d.sent();
|
|
313
|
+
if (!(((_c = result.matchedCount) !== null && _c !== void 0 ? _c : 0) === 0)) return [3 /*break*/, 5];
|
|
314
|
+
logger_1.logger.error("[sync] Re-stamp matched no document; releasing the re-stamp claim", {
|
|
315
|
+
entityId: String(id),
|
|
316
|
+
seq: claim.lastSeq,
|
|
317
|
+
stream: stream,
|
|
318
|
+
});
|
|
319
|
+
return [4 /*yield*/, (0, models_1.releaseSyncSeqs)({ seqs: claim.seqs, stream: stream })];
|
|
320
|
+
case 4:
|
|
321
|
+
_d.sent();
|
|
322
|
+
return [2 /*return*/, undefined];
|
|
323
|
+
case 5: return [4 /*yield*/, (0, models_1.confirmSyncSeqs)({ seqs: claim.seqs, stream: stream })];
|
|
324
|
+
case 6:
|
|
325
|
+
confirmed = _d.sent();
|
|
326
|
+
if (!confirmed.cleared) {
|
|
327
|
+
// Two reaps in a row means the process is pathologically slow; stop recursing.
|
|
328
|
+
logger_1.logger.error("[sync] Re-stamped seq was itself reaped; document remains below the frontier", {
|
|
329
|
+
entityId: String(id),
|
|
330
|
+
seq: claim.lastSeq,
|
|
331
|
+
stream: stream,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
return [2 /*return*/, claim.lastSeq];
|
|
335
|
+
case 7:
|
|
336
|
+
error_3 = _d.sent();
|
|
337
|
+
logger_1.logger.error("[sync] Failed to re-stamp a reaped seq", {
|
|
338
|
+
entityId: String(id),
|
|
339
|
+
error: String(error_3),
|
|
340
|
+
reapedSeq: reapedSeq,
|
|
341
|
+
stream: stream,
|
|
342
|
+
});
|
|
343
|
+
return [2 /*return*/, undefined];
|
|
344
|
+
case 8: return [2 /*return*/];
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
}); };
|
|
348
|
+
var syncPlugin = function (schema) {
|
|
349
|
+
// Task 9.13: `doc.save()` must be conditional on the version the document was loaded at,
|
|
350
|
+
// so the `baseVersion` comparison in executeUpdate cannot be defeated by a concurrent
|
|
351
|
+
// save landing between the read and the write.
|
|
352
|
+
if (!schema.options.versionKey) {
|
|
353
|
+
throw new Error("syncPlugin requires a versionKey on the schema: sync relies on Mongoose " +
|
|
354
|
+
"optimisticConcurrency to make the baseVersion conflict check atomic. " +
|
|
355
|
+
"Remove `versionKey: false` from the schema options.");
|
|
356
|
+
}
|
|
357
|
+
schema.set("optimisticConcurrency", true);
|
|
358
|
+
schema.add({
|
|
359
|
+
_syncPrevStream: {
|
|
360
|
+
default: null,
|
|
361
|
+
description: "The document's previous sync stream, set when a write moved it between scopes; " +
|
|
362
|
+
"null when the last write did not move it",
|
|
363
|
+
type: String,
|
|
364
|
+
},
|
|
365
|
+
_syncSeq: {
|
|
366
|
+
description: "Monotonic per-stream sequence stamped on every synced write",
|
|
367
|
+
index: true,
|
|
368
|
+
type: Number,
|
|
369
|
+
},
|
|
370
|
+
});
|
|
371
|
+
// Capture the stream the document belonged to when it was loaded, so scope moves can
|
|
372
|
+
// be detected at save time without re-querying.
|
|
373
|
+
schema.post("init", function () {
|
|
374
|
+
var entry = (0, registry_1.findSyncEntryByModelName)(this.constructor.modelName);
|
|
375
|
+
if (!entry) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
this.$locals[INITIAL_STREAM_KEY] = streamForObject(entry, this.toObject());
|
|
379
|
+
});
|
|
380
|
+
var PENDING_COMMIT_KEY = "_syncPendingCommit";
|
|
381
|
+
schema.pre("save", function () {
|
|
382
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
383
|
+
var entry, ignored_1, meaningful, session, currentStream, prevStream, initialStream, claim, pending;
|
|
384
|
+
var _a;
|
|
385
|
+
return __generator(this, function (_b) {
|
|
386
|
+
switch (_b.label) {
|
|
387
|
+
case 0:
|
|
388
|
+
entry = (0, registry_1.findSyncEntryByModelName)(this.constructor.modelName);
|
|
389
|
+
if (!entry) {
|
|
390
|
+
return [2 /*return*/];
|
|
391
|
+
}
|
|
392
|
+
// A previous save on this instance that failed after this hook leaves its pending work
|
|
393
|
+
// behind; drop it so this save's post hook never acts on the earlier attempt's claim.
|
|
394
|
+
this.$locals[PENDING_COMMIT_KEY] = undefined;
|
|
395
|
+
// m10: a save that changes nothing meaningful must not burn a seq or emit a delta.
|
|
396
|
+
// Excluded from the "meaningful" set: the sync stamps this hook writes (`_syncSeq`,
|
|
397
|
+
// `_syncPrevStream`) and auto-managed timestamp metadata (`updated`, which
|
|
398
|
+
// createdUpdatedPlugin bumps on EVERY save — otherwise no save would ever be a
|
|
399
|
+
// no-op). A save whose only modified paths are these is a no-op: skip the claim.
|
|
400
|
+
if (!this.isNew) {
|
|
401
|
+
ignored_1 = new Set(["_syncSeq", "_syncPrevStream", "updated"]);
|
|
402
|
+
meaningful = this.modifiedPaths().filter(function (p) { return !ignored_1.has(p); });
|
|
403
|
+
if (meaningful.length === 0) {
|
|
404
|
+
return [2 /*return*/];
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
session = (_a = this.$session()) !== null && _a !== void 0 ? _a : null;
|
|
408
|
+
currentStream = writableStreamForObject(entry, this.toObject());
|
|
409
|
+
prevStream = null;
|
|
410
|
+
if (!this.isNew) {
|
|
411
|
+
initialStream = this.$locals[INITIAL_STREAM_KEY];
|
|
412
|
+
if (initialStream && initialStream !== currentStream) {
|
|
413
|
+
prevStream = initialStream;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return [4 /*yield*/, (0, models_1.claimSyncSeqs)({ session: session, stream: currentStream })];
|
|
417
|
+
case 1:
|
|
418
|
+
claim = _b.sent();
|
|
419
|
+
this.set({ _syncPrevStream: prevStream, _syncSeq: claim.lastSeq });
|
|
420
|
+
pending = {
|
|
421
|
+
claim: claim,
|
|
422
|
+
currentStream: currentStream,
|
|
423
|
+
entityId: String(this._id),
|
|
424
|
+
entry: entry,
|
|
425
|
+
prevStream: prevStream,
|
|
426
|
+
session: session,
|
|
427
|
+
};
|
|
428
|
+
this.$locals[PENDING_COMMIT_KEY] = pending;
|
|
429
|
+
return [2 /*return*/];
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
// C1/C4: once the document write commits, confirm the claimed seq (so the stable
|
|
435
|
+
// frontier can advance past it) and record the durable scope-move marker. Document
|
|
436
|
+
// post('save') `this` is the saved document.
|
|
437
|
+
schema.post("save", function () {
|
|
438
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
439
|
+
var pending, confirmed, restamped;
|
|
440
|
+
return __generator(this, function (_a) {
|
|
441
|
+
switch (_a.label) {
|
|
442
|
+
case 0:
|
|
443
|
+
pending = this.$locals[PENDING_COMMIT_KEY];
|
|
444
|
+
if (!pending) {
|
|
445
|
+
return [2 /*return*/];
|
|
446
|
+
}
|
|
447
|
+
this.$locals[PENDING_COMMIT_KEY] = undefined;
|
|
448
|
+
if (!pending.claim.registered) return [3 /*break*/, 3];
|
|
449
|
+
return [4 /*yield*/, (0, models_1.confirmSyncSeqs)({
|
|
450
|
+
seqs: pending.claim.seqs,
|
|
451
|
+
stream: pending.currentStream,
|
|
452
|
+
}).catch(function (error) {
|
|
453
|
+
// Never fail the user write for a confirm error: the entry ages out via the lease.
|
|
454
|
+
logger_1.logger.error("[sync] Failed to confirm seq after save", {
|
|
455
|
+
error: String(error),
|
|
456
|
+
stream: pending.currentStream,
|
|
457
|
+
});
|
|
458
|
+
return { cleared: true };
|
|
459
|
+
})];
|
|
460
|
+
case 1:
|
|
461
|
+
confirmed = _a.sent();
|
|
462
|
+
if (!!confirmed.cleared) return [3 /*break*/, 3];
|
|
463
|
+
return [4 /*yield*/, restampReapedSeq({
|
|
464
|
+
id: this._id,
|
|
465
|
+
model: this.constructor,
|
|
466
|
+
reapedSeq: pending.claim.seqs[pending.claim.seqs.length - 1],
|
|
467
|
+
stream: pending.currentStream,
|
|
468
|
+
})];
|
|
469
|
+
case 2:
|
|
470
|
+
restamped = _a.sent();
|
|
471
|
+
if (restamped !== undefined) {
|
|
472
|
+
// Reflect the durable seq on the in-memory document so the caller's ack (and
|
|
473
|
+
// any serialization of this instance) reports the seq clients will actually see.
|
|
474
|
+
this.$set("_syncSeq", restamped);
|
|
475
|
+
this.unmarkModified("_syncSeq");
|
|
476
|
+
}
|
|
477
|
+
_a.label = 3;
|
|
478
|
+
case 3:
|
|
479
|
+
if (!pending.prevStream) return [3 /*break*/, 5];
|
|
480
|
+
return [4 /*yield*/, writeScopeMoveMarker({
|
|
481
|
+
entityId: pending.entityId,
|
|
482
|
+
entry: pending.entry,
|
|
483
|
+
fromStream: pending.prevStream,
|
|
484
|
+
session: pending.session,
|
|
485
|
+
toStream: pending.currentStream,
|
|
486
|
+
})];
|
|
487
|
+
case 4:
|
|
488
|
+
_a.sent();
|
|
489
|
+
_a.label = 5;
|
|
490
|
+
case 5:
|
|
491
|
+
// The committed stream becomes the baseline for the next save on this instance; a
|
|
492
|
+
// FAILED save keeps the old baseline so a retry still detects the move.
|
|
493
|
+
this.$locals[INITIAL_STREAM_KEY] = pending.currentStream;
|
|
494
|
+
return [2 /*return*/];
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
// Task 9.13: an optimistic-concurrency loss (`VersionError`) matched no document, so the
|
|
500
|
+
// seq claimed in pre('save') will never be stamped on anything. Cancel it instead of
|
|
501
|
+
// letting it hold the stable frontier down for the full pending-claim lease — a lost
|
|
502
|
+
// update race is now an EXPECTED outcome, not a crash, and must not stall catch-up for
|
|
503
|
+
// every other client on the stream.
|
|
504
|
+
schema.post("save", function (error, doc, next) {
|
|
505
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
506
|
+
var locals, pending;
|
|
507
|
+
var _a;
|
|
508
|
+
return __generator(this, function (_b) {
|
|
509
|
+
switch (_b.label) {
|
|
510
|
+
case 0:
|
|
511
|
+
locals = ((_a = this === null || this === void 0 ? void 0 : this.$locals) !== null && _a !== void 0 ? _a : doc === null || doc === void 0 ? void 0 : doc.$locals);
|
|
512
|
+
pending = locals === null || locals === void 0 ? void 0 : locals[PENDING_COMMIT_KEY];
|
|
513
|
+
if (locals) {
|
|
514
|
+
locals[PENDING_COMMIT_KEY] = undefined;
|
|
515
|
+
}
|
|
516
|
+
if (!((pending === null || pending === void 0 ? void 0 : pending.claim.registered) && (0, executors_1.isVersionError)(error))) return [3 /*break*/, 2];
|
|
517
|
+
return [4 /*yield*/, (0, models_1.releaseSyncSeqs)({ seqs: pending.claim.seqs, stream: pending.currentStream }).catch(function (releaseError) {
|
|
518
|
+
logger_1.logger.error("[sync] Failed to release the seq claim of a version-conflicted save", {
|
|
519
|
+
error: String(releaseError),
|
|
520
|
+
stream: pending.currentStream,
|
|
521
|
+
});
|
|
522
|
+
})];
|
|
523
|
+
case 1:
|
|
524
|
+
_b.sent();
|
|
525
|
+
_b.label = 2;
|
|
526
|
+
case 2:
|
|
527
|
+
next(error);
|
|
528
|
+
return [2 /*return*/];
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
});
|
|
532
|
+
});
|
|
533
|
+
schema.pre("insertMany", function (next, docs, options) {
|
|
534
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
535
|
+
var model, entry, session, byStream, docs_1, docs_1_1, doc, stream, group, _loop_1, byStream_1, byStream_1_1, _a, stream, group, e_1_1, error_4;
|
|
536
|
+
var e_2, _b, e_1, _c;
|
|
537
|
+
var _d, _e;
|
|
538
|
+
return __generator(this, function (_f) {
|
|
539
|
+
switch (_f.label) {
|
|
540
|
+
case 0:
|
|
541
|
+
_f.trys.push([0, 9, , 10]);
|
|
542
|
+
model = this;
|
|
543
|
+
entry = (0, registry_1.findSyncEntryByModelName)(model.modelName);
|
|
544
|
+
if (!entry || !Array.isArray(docs) || docs.length === 0) {
|
|
545
|
+
return [2 /*return*/, next()];
|
|
546
|
+
}
|
|
547
|
+
session = (_d = options === null || options === void 0 ? void 0 : options.session) !== null && _d !== void 0 ? _d : null;
|
|
548
|
+
byStream = new Map();
|
|
549
|
+
try {
|
|
550
|
+
for (docs_1 = __values(docs), docs_1_1 = docs_1.next(); !docs_1_1.done; docs_1_1 = docs_1.next()) {
|
|
551
|
+
doc = docs_1_1.value;
|
|
552
|
+
stream = writableStreamForObject(entry, doc);
|
|
553
|
+
group = (_e = byStream.get(stream)) !== null && _e !== void 0 ? _e : [];
|
|
554
|
+
group.push(doc);
|
|
555
|
+
byStream.set(stream, group);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
559
|
+
finally {
|
|
560
|
+
try {
|
|
561
|
+
if (docs_1_1 && !docs_1_1.done && (_b = docs_1.return)) _b.call(docs_1);
|
|
562
|
+
}
|
|
563
|
+
finally { if (e_2) throw e_2.error; }
|
|
564
|
+
}
|
|
565
|
+
_loop_1 = function (stream, group) {
|
|
566
|
+
var claim;
|
|
567
|
+
return __generator(this, function (_g) {
|
|
568
|
+
switch (_g.label) {
|
|
569
|
+
case 0: return [4 /*yield*/, (0, models_1.claimSyncSeqs)({ count: group.length, session: session, stream: stream })];
|
|
570
|
+
case 1:
|
|
571
|
+
claim = _g.sent();
|
|
572
|
+
group.forEach(function (doc, index) {
|
|
573
|
+
doc._syncPrevStream = null;
|
|
574
|
+
doc._syncSeq = claim.seqs[index];
|
|
575
|
+
});
|
|
576
|
+
return [2 /*return*/];
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
};
|
|
580
|
+
_f.label = 1;
|
|
581
|
+
case 1:
|
|
582
|
+
_f.trys.push([1, 6, 7, 8]);
|
|
583
|
+
byStream_1 = __values(byStream), byStream_1_1 = byStream_1.next();
|
|
584
|
+
_f.label = 2;
|
|
585
|
+
case 2:
|
|
586
|
+
if (!!byStream_1_1.done) return [3 /*break*/, 5];
|
|
587
|
+
_a = __read(byStream_1_1.value, 2), stream = _a[0], group = _a[1];
|
|
588
|
+
return [5 /*yield**/, _loop_1(stream, group)];
|
|
589
|
+
case 3:
|
|
590
|
+
_f.sent();
|
|
591
|
+
_f.label = 4;
|
|
592
|
+
case 4:
|
|
593
|
+
byStream_1_1 = byStream_1.next();
|
|
594
|
+
return [3 /*break*/, 2];
|
|
595
|
+
case 5: return [3 /*break*/, 8];
|
|
596
|
+
case 6:
|
|
597
|
+
e_1_1 = _f.sent();
|
|
598
|
+
e_1 = { error: e_1_1 };
|
|
599
|
+
return [3 /*break*/, 8];
|
|
600
|
+
case 7:
|
|
601
|
+
try {
|
|
602
|
+
if (byStream_1_1 && !byStream_1_1.done && (_c = byStream_1.return)) _c.call(byStream_1);
|
|
603
|
+
}
|
|
604
|
+
finally { if (e_1) throw e_1.error; }
|
|
605
|
+
return [7 /*endfinally*/];
|
|
606
|
+
case 8:
|
|
607
|
+
// C1: the confirm belongs in post('insertMany') — confirming here would let the
|
|
608
|
+
// stable frontier (and therefore a snapshot cursor) advance past seqs whose
|
|
609
|
+
// documents have not committed yet, stranding them below every catch-up cursor.
|
|
610
|
+
return [2 /*return*/, next()];
|
|
611
|
+
case 9:
|
|
612
|
+
error_4 = _f.sent();
|
|
613
|
+
return [2 /*return*/, next(error_4)];
|
|
614
|
+
case 10: return [2 /*return*/];
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
});
|
|
618
|
+
});
|
|
619
|
+
// C1: the batch has committed — confirm the claimed seqs so the frontier advances past
|
|
620
|
+
// them. Post insertMany middleware receives the inserted (hydrated) documents, which
|
|
621
|
+
// carry the seqs stamped in the pre hook.
|
|
622
|
+
schema.post("insertMany", function (docs) {
|
|
623
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
624
|
+
var modelName, entry, seqsByStream, _a, _b, doc, obj, seq, stream, seqs, _loop_2, seqsByStream_1, seqsByStream_1_1, _c, stream, seqs, e_3_1;
|
|
625
|
+
var e_4, _d, e_3, _e;
|
|
626
|
+
var _f, _g, _h, _j;
|
|
627
|
+
return __generator(this, function (_k) {
|
|
628
|
+
switch (_k.label) {
|
|
629
|
+
case 0:
|
|
630
|
+
if (!Array.isArray(docs) || docs.length === 0) {
|
|
631
|
+
return [2 /*return*/];
|
|
632
|
+
}
|
|
633
|
+
modelName = ((_h = (_g = (_f = docs[0]) === null || _f === void 0 ? void 0 : _f.constructor) === null || _g === void 0 ? void 0 : _g.modelName) !== null && _h !== void 0 ? _h : this.modelName);
|
|
634
|
+
entry = modelName ? (0, registry_1.findSyncEntryByModelName)(modelName) : undefined;
|
|
635
|
+
if (!entry) {
|
|
636
|
+
return [2 /*return*/];
|
|
637
|
+
}
|
|
638
|
+
seqsByStream = new Map();
|
|
639
|
+
try {
|
|
640
|
+
for (_a = __values(docs), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
641
|
+
doc = _b.value;
|
|
642
|
+
// A session-backed claim registered nothing to confirm, and a `$pull` issued outside
|
|
643
|
+
// the caller's still-open transaction would conflict with its counter write.
|
|
644
|
+
if (typeof (doc === null || doc === void 0 ? void 0 : doc.$session) === "function" && doc.$session()) {
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
647
|
+
obj = (typeof (doc === null || doc === void 0 ? void 0 : doc.toObject) === "function" ? doc.toObject() : doc);
|
|
648
|
+
seq = obj._syncSeq;
|
|
649
|
+
if (typeof seq !== "number") {
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
stream = streamForObject(entry, obj);
|
|
653
|
+
seqs = (_j = seqsByStream.get(stream)) !== null && _j !== void 0 ? _j : [];
|
|
654
|
+
seqs.push(seq);
|
|
655
|
+
seqsByStream.set(stream, seqs);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
659
|
+
finally {
|
|
660
|
+
try {
|
|
661
|
+
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
662
|
+
}
|
|
663
|
+
finally { if (e_4) throw e_4.error; }
|
|
664
|
+
}
|
|
665
|
+
_loop_2 = function (stream, seqs) {
|
|
666
|
+
return __generator(this, function (_l) {
|
|
667
|
+
switch (_l.label) {
|
|
668
|
+
case 0: return [4 /*yield*/, (0, models_1.confirmSyncSeqs)({ seqs: seqs, stream: stream }).catch(function (error) {
|
|
669
|
+
// Never fail the user write for a confirm error: the entries age out via the lease.
|
|
670
|
+
logger_1.logger.error("[sync] Failed to confirm insertMany seqs", {
|
|
671
|
+
error: String(error),
|
|
672
|
+
stream: stream,
|
|
673
|
+
});
|
|
674
|
+
})];
|
|
675
|
+
case 1:
|
|
676
|
+
_l.sent();
|
|
677
|
+
return [2 /*return*/];
|
|
678
|
+
}
|
|
679
|
+
});
|
|
680
|
+
};
|
|
681
|
+
_k.label = 1;
|
|
682
|
+
case 1:
|
|
683
|
+
_k.trys.push([1, 6, 7, 8]);
|
|
684
|
+
seqsByStream_1 = __values(seqsByStream), seqsByStream_1_1 = seqsByStream_1.next();
|
|
685
|
+
_k.label = 2;
|
|
686
|
+
case 2:
|
|
687
|
+
if (!!seqsByStream_1_1.done) return [3 /*break*/, 5];
|
|
688
|
+
_c = __read(seqsByStream_1_1.value, 2), stream = _c[0], seqs = _c[1];
|
|
689
|
+
return [5 /*yield**/, _loop_2(stream, seqs)];
|
|
690
|
+
case 3:
|
|
691
|
+
_k.sent();
|
|
692
|
+
_k.label = 4;
|
|
693
|
+
case 4:
|
|
694
|
+
seqsByStream_1_1 = seqsByStream_1.next();
|
|
695
|
+
return [3 /*break*/, 2];
|
|
696
|
+
case 5: return [3 /*break*/, 8];
|
|
697
|
+
case 6:
|
|
698
|
+
e_3_1 = _k.sent();
|
|
699
|
+
e_3 = { error: e_3_1 };
|
|
700
|
+
return [3 /*break*/, 8];
|
|
701
|
+
case 7:
|
|
702
|
+
try {
|
|
703
|
+
if (seqsByStream_1_1 && !seqsByStream_1_1.done && (_e = seqsByStream_1.return)) _e.call(seqsByStream_1);
|
|
704
|
+
}
|
|
705
|
+
finally { if (e_3) throw e_3.error; }
|
|
706
|
+
return [7 /*endfinally*/];
|
|
707
|
+
case 8: return [2 /*return*/];
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
});
|
|
711
|
+
});
|
|
712
|
+
// Single-document query writes: fetch the target to resolve its stream (and detect
|
|
713
|
+
// scope moves), claim a seq, and merge the stamp into the update.
|
|
714
|
+
var preQueryWrite = function () {
|
|
715
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
716
|
+
var model, entry, operation, rawFilter, session, filter, matches, target, targetObj, previousStream, rawUpdate, op, isTrueReplacement, hasOperators, setFields, scopeField, currentStream, effectiveDoc, unsetFields, newScopeValue, prevStream, claim;
|
|
717
|
+
var _a;
|
|
718
|
+
var _b, _c, _d, _e, _f, _g, _h;
|
|
719
|
+
return __generator(this, function (_j) {
|
|
720
|
+
switch (_j.label) {
|
|
721
|
+
case 0:
|
|
722
|
+
model = this.model;
|
|
723
|
+
entry = (0, registry_1.findSyncEntryByModelName)(model.modelName);
|
|
724
|
+
if (!entry) {
|
|
725
|
+
return [2 /*return*/];
|
|
726
|
+
}
|
|
727
|
+
operation = (_b = this.op) !== null && _b !== void 0 ? _b : "query-write";
|
|
728
|
+
// m8/C6: reject upserts — an upsert can create a doc the lookup never saw.
|
|
729
|
+
if (this.getOptions().upsert) {
|
|
730
|
+
throw upsertError(model.modelName, operation);
|
|
731
|
+
}
|
|
732
|
+
rawFilter = this.getFilter();
|
|
733
|
+
// m9: refuse a non-single-_id filter — it could match a different doc than intended.
|
|
734
|
+
if (!filterTargetsSingleId(rawFilter)) {
|
|
735
|
+
throw nonIdFilterError(model.modelName, operation);
|
|
736
|
+
}
|
|
737
|
+
session = (_c = this.getOptions().session) !== null && _c !== void 0 ? _c : null;
|
|
738
|
+
filter = __assign({}, rawFilter);
|
|
739
|
+
if (filter.deleted === undefined) {
|
|
740
|
+
filter.deleted = { $in: [true, false] };
|
|
741
|
+
}
|
|
742
|
+
return [4 /*yield*/, model
|
|
743
|
+
.find(filter)
|
|
744
|
+
.setOptions(session ? { session: session } : {})
|
|
745
|
+
.limit(1)];
|
|
746
|
+
case 1:
|
|
747
|
+
matches = _j.sent();
|
|
748
|
+
target = matches[0];
|
|
749
|
+
if (!target) {
|
|
750
|
+
// The update matches nothing; let it proceed as a no-op.
|
|
751
|
+
return [2 /*return*/];
|
|
752
|
+
}
|
|
753
|
+
targetObj = target.toObject();
|
|
754
|
+
previousStream = streamForObject(entry, targetObj);
|
|
755
|
+
rawUpdate = ((_d = this.getUpdate()) !== null && _d !== void 0 ? _d : {});
|
|
756
|
+
op = (_e = this.op) !== null && _e !== void 0 ? _e : "";
|
|
757
|
+
isTrueReplacement = op === "replaceOne" || op === "findOneAndReplace";
|
|
758
|
+
hasOperators = Object.keys(rawUpdate).some(function (key) { return key.startsWith("$"); });
|
|
759
|
+
setFields = isTrueReplacement
|
|
760
|
+
? {}
|
|
761
|
+
: hasOperators
|
|
762
|
+
? ((_f = rawUpdate.$set) !== null && _f !== void 0 ? _f : {})
|
|
763
|
+
: rawUpdate;
|
|
764
|
+
scopeField = (0, streams_1.getScopeField)(entry.config.scope);
|
|
765
|
+
if (typeof entry.config.scope === "function") {
|
|
766
|
+
effectiveDoc = isTrueReplacement ? rawUpdate : __assign(__assign({}, targetObj), setFields);
|
|
767
|
+
currentStream = streamForObject(entry, effectiveDoc);
|
|
768
|
+
}
|
|
769
|
+
else if (scopeField) {
|
|
770
|
+
unsetFields = hasOperators
|
|
771
|
+
? ((_g = rawUpdate.$unset) !== null && _g !== void 0 ? _g : {})
|
|
772
|
+
: {};
|
|
773
|
+
newScopeValue = void 0;
|
|
774
|
+
if (isTrueReplacement) {
|
|
775
|
+
newScopeValue = rawUpdate[scopeField];
|
|
776
|
+
}
|
|
777
|
+
else if (scopeField in unsetFields) {
|
|
778
|
+
newScopeValue = undefined;
|
|
779
|
+
}
|
|
780
|
+
else if (scopeField in setFields) {
|
|
781
|
+
newScopeValue = setFields[scopeField];
|
|
782
|
+
}
|
|
783
|
+
else {
|
|
784
|
+
newScopeValue = targetObj[scopeField];
|
|
785
|
+
}
|
|
786
|
+
// A replacement, a `$set` to null, or an `$unset` can strip the tenant field off an
|
|
787
|
+
// existing document, which would move it into an unsubscribable stream just as
|
|
788
|
+
// surely as creating it without one.
|
|
789
|
+
(0, streams_1.assertWritableStream)({
|
|
790
|
+
collectionTag: entry.collectionTag,
|
|
791
|
+
doc: (_a = {}, _a[scopeField] = newScopeValue, _a),
|
|
792
|
+
scope: entry.config.scope,
|
|
793
|
+
});
|
|
794
|
+
currentStream = (0, streams_1.streamForScopeValue)({
|
|
795
|
+
collectionTag: entry.collectionTag,
|
|
796
|
+
scope: entry.config.scope,
|
|
797
|
+
scopeValue: newScopeValue,
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
else {
|
|
801
|
+
currentStream = previousStream;
|
|
802
|
+
}
|
|
803
|
+
prevStream = previousStream !== currentStream ? previousStream : null;
|
|
804
|
+
return [4 /*yield*/, (0, models_1.claimSyncSeqs)({ session: session, stream: currentStream })];
|
|
805
|
+
case 2:
|
|
806
|
+
claim = _j.sent();
|
|
807
|
+
if (isTrueReplacement || !hasOperators) {
|
|
808
|
+
// True replacements and implicit-$set plain objects both take plain keys.
|
|
809
|
+
rawUpdate._syncPrevStream = prevStream;
|
|
810
|
+
rawUpdate._syncSeq = claim.lastSeq;
|
|
811
|
+
}
|
|
812
|
+
else {
|
|
813
|
+
rawUpdate.$set = __assign(__assign({}, ((_h = rawUpdate.$set) !== null && _h !== void 0 ? _h : {})), { _syncPrevStream: prevStream, _syncSeq: claim.lastSeq });
|
|
814
|
+
}
|
|
815
|
+
this.setUpdate(rawUpdate);
|
|
816
|
+
// Stash the claim + move info so the query post hook confirms/records after commit.
|
|
817
|
+
this._syncPendingConfirm = {
|
|
818
|
+
claim: claim,
|
|
819
|
+
currentStream: currentStream,
|
|
820
|
+
entityId: String(target._id),
|
|
821
|
+
entry: entry,
|
|
822
|
+
prevStream: prevStream,
|
|
823
|
+
session: session,
|
|
824
|
+
};
|
|
825
|
+
return [2 /*return*/];
|
|
826
|
+
}
|
|
827
|
+
});
|
|
828
|
+
});
|
|
829
|
+
};
|
|
830
|
+
// C1/C4: after the query write commits, confirm the claimed seq and record the
|
|
831
|
+
// scope-move marker. Query post middleware `this` is the Query (not a document).
|
|
832
|
+
var postQueryWrite = function () {
|
|
833
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
834
|
+
var pending, _a, claim, currentStream, entityId, entry, prevStream, session;
|
|
835
|
+
return __generator(this, function (_b) {
|
|
836
|
+
switch (_b.label) {
|
|
837
|
+
case 0:
|
|
838
|
+
pending = this._syncPendingConfirm;
|
|
839
|
+
if (!pending) {
|
|
840
|
+
return [2 /*return*/];
|
|
841
|
+
}
|
|
842
|
+
this._syncPendingConfirm = undefined;
|
|
843
|
+
_a = pending, claim = _a.claim, currentStream = _a.currentStream, entityId = _a.entityId, entry = _a.entry, prevStream = _a.prevStream, session = _a.session;
|
|
844
|
+
if (!claim.registered) return [3 /*break*/, 2];
|
|
845
|
+
return [4 /*yield*/, (0, models_1.confirmSyncSeqs)({ seqs: claim.seqs, stream: currentStream }).catch(function (error) {
|
|
846
|
+
logger_1.logger.error("[sync] Failed to confirm seq after query write", {
|
|
847
|
+
error: String(error),
|
|
848
|
+
stream: currentStream,
|
|
849
|
+
});
|
|
850
|
+
})];
|
|
851
|
+
case 1:
|
|
852
|
+
_b.sent();
|
|
853
|
+
_b.label = 2;
|
|
854
|
+
case 2:
|
|
855
|
+
if (!prevStream) return [3 /*break*/, 4];
|
|
856
|
+
// Retries internally and never throws: the move already committed, so the marker
|
|
857
|
+
// must not be dropped on the first transient failure nor fail the user's write.
|
|
858
|
+
return [4 /*yield*/, writeScopeMoveMarker({
|
|
859
|
+
entityId: entityId,
|
|
860
|
+
entry: entry,
|
|
861
|
+
fromStream: prevStream,
|
|
862
|
+
session: session,
|
|
863
|
+
toStream: currentStream,
|
|
864
|
+
})];
|
|
865
|
+
case 3:
|
|
866
|
+
// Retries internally and never throws: the move already committed, so the marker
|
|
867
|
+
// must not be dropped on the first transient failure nor fail the user's write.
|
|
868
|
+
_b.sent();
|
|
869
|
+
_b.label = 4;
|
|
870
|
+
case 4: return [2 /*return*/];
|
|
871
|
+
}
|
|
872
|
+
});
|
|
873
|
+
});
|
|
874
|
+
};
|
|
875
|
+
schema.pre("updateOne", { document: false, query: true }, preQueryWrite);
|
|
876
|
+
schema.pre("findOneAndUpdate", preQueryWrite);
|
|
877
|
+
schema.pre("replaceOne", preQueryWrite);
|
|
878
|
+
schema.pre("findOneAndReplace", preQueryWrite);
|
|
879
|
+
schema.post("updateOne", { document: false, query: true }, postQueryWrite);
|
|
880
|
+
schema.post("findOneAndUpdate", postQueryWrite);
|
|
881
|
+
schema.post("replaceOne", postQueryWrite);
|
|
882
|
+
schema.post("findOneAndReplace", postQueryWrite);
|
|
883
|
+
// Unsupported multi-document / hard-delete paths throw for registered models.
|
|
884
|
+
var guardQuery = function (operation) {
|
|
885
|
+
return function () {
|
|
886
|
+
var model = this.model;
|
|
887
|
+
if ((0, registry_1.findSyncEntryByModelName)(model.modelName)) {
|
|
888
|
+
throw unsupportedWrite(model.modelName, operation);
|
|
889
|
+
}
|
|
890
|
+
};
|
|
891
|
+
};
|
|
892
|
+
schema.pre("updateMany", guardQuery("updateMany"));
|
|
893
|
+
schema.pre("deleteMany", guardQuery("deleteMany"));
|
|
894
|
+
schema.pre("deleteOne", { document: false, query: true }, guardQuery("deleteOne"));
|
|
895
|
+
schema.pre("findOneAndDelete", guardQuery("findOneAndDelete"));
|
|
896
|
+
schema.pre("deleteOne", { document: true, query: false }, function () {
|
|
897
|
+
var entry = (0, registry_1.findSyncEntryByModelName)(this.constructor.modelName);
|
|
898
|
+
if (entry) {
|
|
899
|
+
throw unsupportedWrite(entry.modelName, "document deleteOne (hard delete)");
|
|
900
|
+
}
|
|
901
|
+
});
|
|
902
|
+
};
|
|
903
|
+
exports.syncPlugin = syncPlugin;
|