@terreno/api 0.31.0 → 56.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +122 -2
- package/LICENSE +202 -21
- package/dist/api.d.ts +12 -3
- package/dist/api.js +110 -361
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +100 -1
- package/dist/betterAuth.d.ts +5 -0
- package/dist/betterAuthSetup.d.ts +1 -15
- package/dist/betterAuthSetup.js +29 -8
- package/dist/configurationPlugin.js +2 -2
- package/dist/docLoader.js +12 -9
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +29 -1
- package/dist/expressServer.js +5 -11
- package/dist/githubAuth.js +4 -1
- package/dist/index.d.ts +17 -1
- package/dist/index.js +29 -1
- package/dist/middleware.d.ts +7 -0
- package/dist/middleware.js +16 -1
- package/dist/openApiCompat.d.ts +10 -1
- package/dist/openApiCompat.js +115 -47
- package/dist/permissions.d.ts +27 -0
- package/dist/permissions.js +42 -1
- package/dist/plugins.d.ts +15 -3
- package/dist/plugins.js +36 -6
- package/dist/realtime/changeStreamWatcher.d.ts +100 -3
- package/dist/realtime/changeStreamWatcher.js +542 -137
- package/dist/realtime/realtimeApp.d.ts +3 -1
- package/dist/realtime/realtimeApp.js +75 -27
- package/dist/realtime/sessionRevalidation.d.ts +90 -0
- package/dist/realtime/sessionRevalidation.js +438 -0
- package/dist/realtime/socketAuth.d.ts +97 -0
- package/dist/realtime/socketAuth.js +272 -0
- package/dist/realtime/socketUser.d.ts +54 -0
- package/dist/realtime/socketUser.js +75 -5
- package/dist/realtime/types.d.ts +40 -0
- package/dist/secretProviders.d.ts +23 -0
- package/dist/secretProviders.js +9 -3
- package/dist/sync/executors.d.ts +198 -0
- package/dist/sync/executors.js +670 -0
- package/dist/sync/loadHarness.js +807 -0
- package/dist/sync/models.d.ts +216 -0
- package/dist/sync/models.js +644 -0
- package/dist/sync/mutationHandler.d.ts +97 -0
- package/dist/sync/mutationHandler.js +886 -0
- package/dist/sync/registry.d.ts +73 -0
- package/dist/sync/registry.js +223 -0
- package/dist/sync/routes.d.ts +62 -0
- package/dist/sync/routes.js +936 -0
- package/dist/sync/scripts/compactTombstones.d.ts +47 -0
- package/dist/sync/scripts/compactTombstones.js +270 -0
- package/dist/sync/serialize.d.ts +20 -0
- package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
- package/dist/sync/socketHandlers.d.ts +54 -0
- package/dist/sync/socketHandlers.js +470 -0
- package/dist/sync/streams.d.ts +85 -0
- package/dist/sync/streams.js +221 -0
- package/dist/sync/syncApp.d.ts +29 -0
- package/dist/sync/syncApp.js +44 -0
- package/dist/sync/syncBatch.d.ts +71 -0
- package/dist/sync/syncBatch.js +252 -0
- package/dist/sync/syncSeqPlugin.d.ts +8 -0
- package/dist/sync/syncSeqPlugin.js +903 -0
- package/dist/sync/types.d.ts +254 -0
- package/dist/sync/types.js +9 -0
- package/dist/terrenoApp.js +90 -27
- package/dist/tests/models.d.ts +11 -27
- package/dist/tests/models.js +3 -5
- package/dist/tests/testHelper.d.ts +3 -9
- package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
- package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
- package/package.json +10 -8
- package/src/__tests__/openApiCompat.test.ts +20 -0
- package/src/actions.test.ts +12 -19
- package/src/actions.ts +2 -2
- package/src/api.coverage.test.ts +9 -7
- package/src/api.errorPaths.test.ts +1 -47
- package/src/api.test.ts +12 -0
- package/src/api.ts +98 -317
- package/src/auth.test.ts +128 -176
- package/src/auth.ts +109 -0
- package/src/betterAuth.ts +6 -0
- package/src/betterAuthSetup.test.ts +34 -0
- package/src/betterAuthSetup.ts +27 -8
- package/src/configurationPlugin.ts +2 -2
- package/src/consentApp.test.ts +12 -12
- package/src/docLoader.ts +3 -1
- package/src/errors.ts +33 -0
- package/src/expressServer.test.ts +7 -1
- package/src/expressServer.ts +5 -11
- package/src/githubAuth.ts +3 -1
- package/src/index.ts +42 -0
- package/src/middleware.test.ts +40 -1
- package/src/middleware.ts +15 -0
- package/src/models/versionConfig.ts +1 -1
- package/src/openApiBuilder.test.ts +9 -24
- package/src/openApiCompat.ts +99 -45
- package/src/openApiValidator.test.ts +0 -41
- package/src/permissions.test.ts +75 -1
- package/src/permissions.ts +49 -0
- package/src/plugins.test.ts +54 -14
- package/src/plugins.ts +47 -9
- package/src/populate.test.ts +16 -25
- package/src/realtime/changeStreamWatcher.ts +552 -114
- package/src/realtime/realtime.test.ts +157 -239
- package/src/realtime/realtimeApp.ts +80 -19
- package/src/realtime/sessionRevalidation.test.ts +423 -0
- package/src/realtime/sessionRevalidation.ts +273 -0
- package/src/realtime/socketAuth.test.ts +355 -0
- package/src/realtime/socketAuth.ts +228 -0
- package/src/realtime/socketUser.ts +69 -0
- package/src/realtime/types.ts +40 -0
- package/src/secretProviders.test.ts +7 -1
- package/src/secretProviders.ts +15 -3
- package/src/sync/ensureSyncIndexes.test.ts +144 -0
- package/src/sync/executors.test.ts +882 -0
- package/src/sync/executors.ts +703 -0
- package/src/sync/integration.test.ts +1160 -0
- package/src/sync/loadHarness.ts +618 -0
- package/src/sync/models.ts +640 -0
- package/src/sync/mutationHandler.test.ts +1252 -0
- package/src/sync/mutationHandler.ts +850 -0
- package/src/sync/registry.ts +232 -0
- package/src/sync/routes.ts +812 -0
- package/src/sync/scripts/compactTombstones.test.ts +249 -0
- package/src/sync/scripts/compactTombstones.ts +145 -0
- package/src/sync/serialize.ts +48 -0
- package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
- package/src/sync/socketHandlers.ts +320 -0
- package/src/sync/streams.ts +188 -0
- package/src/sync/sync.test.ts +582 -0
- package/src/sync/syncApp.ts +44 -0
- package/src/sync/syncBatch.ts +219 -0
- package/src/sync/syncFrontier.test.ts +494 -0
- package/src/sync/syncPhaseC.test.ts +718 -0
- package/src/sync/syncRoutes.test.ts +865 -0
- package/src/sync/syncSeqPlugin.ts +738 -0
- package/src/sync/syncSocket.test.ts +1440 -0
- package/src/sync/types.ts +270 -0
- package/src/terrenoApp.ts +37 -17
- package/src/tests/bunSetup.ts +8 -0
- package/src/tests/models.ts +6 -8
- package/tsconfig.json +4 -4
- package/tsconfig.typedoc.json +7 -1
- package/dist/__tests__/openApiCompat.test.js +0 -317
- package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
- package/dist/__tests__/versionCheckPlugin.test.js +0 -597
- package/dist/actions.openApi.test.d.ts +0 -1
- package/dist/actions.openApi.test.js +0 -253
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -1087
- package/dist/api.arrayOperations.test.d.ts +0 -1
- package/dist/api.arrayOperations.test.js +0 -878
- package/dist/api.asyncHandler.test.d.ts +0 -1
- package/dist/api.asyncHandler.test.js +0 -236
- package/dist/api.coverage.test.d.ts +0 -1
- package/dist/api.coverage.test.js +0 -192
- package/dist/api.errorPaths.test.d.ts +0 -1
- package/dist/api.errorPaths.test.js +0 -887
- package/dist/api.errors.test.d.ts +0 -1
- package/dist/api.errors.test.js +0 -185
- package/dist/api.hooks.test.d.ts +0 -1
- package/dist/api.hooks.test.js +0 -950
- package/dist/api.query.test.d.ts +0 -1
- package/dist/api.query.test.js +0 -948
- package/dist/api.test.d.ts +0 -1
- package/dist/api.test.js +0 -2820
- package/dist/auth.compile.test.d.ts +0 -1
- package/dist/auth.compile.test.js +0 -201
- package/dist/auth.coverage.test.d.ts +0 -1
- package/dist/auth.coverage.test.js +0 -202
- package/dist/auth.test.d.ts +0 -1
- package/dist/auth.test.js +0 -2286
- package/dist/betterAuth.test.d.ts +0 -1
- package/dist/betterAuth.test.js +0 -181
- package/dist/betterAuthApp.test.d.ts +0 -1
- package/dist/betterAuthApp.test.js +0 -242
- package/dist/betterAuthSetup.test.d.ts +0 -1
- package/dist/betterAuthSetup.test.js +0 -762
- package/dist/config.test.d.ts +0 -1
- package/dist/config.test.js +0 -353
- package/dist/configuration.test.d.ts +0 -1
- package/dist/configuration.test.js +0 -980
- package/dist/configurationPlugin.test.d.ts +0 -1
- package/dist/configurationPlugin.test.js +0 -827
- package/dist/consentApp.test.d.ts +0 -1
- package/dist/consentApp.test.js +0 -1411
- package/dist/docLoader.test.d.ts +0 -1
- package/dist/docLoader.test.js +0 -137
- package/dist/envConfigurationPlugin.test.d.ts +0 -1
- package/dist/envConfigurationPlugin.test.js +0 -369
- package/dist/errors.test.d.ts +0 -1
- package/dist/errors.test.js +0 -607
- package/dist/expressServer.test.d.ts +0 -1
- package/dist/expressServer.test.js +0 -1249
- package/dist/githubAuth.test.d.ts +0 -1
- package/dist/githubAuth.test.js +0 -1142
- package/dist/httpClient.test.d.ts +0 -1
- package/dist/httpClient.test.js +0 -1471
- package/dist/logger.test.d.ts +0 -1
- package/dist/logger.test.js +0 -685
- package/dist/middleware.test.d.ts +0 -1
- package/dist/middleware.test.js +0 -241
- package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
- package/dist/notifiers/googleChatNotifier.test.js +0 -298
- package/dist/notifiers/slackNotifier.test.d.ts +0 -1
- package/dist/notifiers/slackNotifier.test.js +0 -297
- package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
- package/dist/notifiers/zoomNotifier.test.js +0 -373
- package/dist/openApi.test.d.ts +0 -1
- package/dist/openApi.test.js +0 -620
- package/dist/openApiBuilder.test.d.ts +0 -1
- package/dist/openApiBuilder.test.js +0 -599
- package/dist/openApiEtag.test.d.ts +0 -1
- package/dist/openApiEtag.test.js +0 -86
- package/dist/openApiValidator.test.d.ts +0 -1
- package/dist/openApiValidator.test.js +0 -795
- package/dist/permissions.middleware.test.d.ts +0 -1
- package/dist/permissions.middleware.test.js +0 -308
- package/dist/permissions.test.d.ts +0 -1
- package/dist/permissions.test.js +0 -439
- package/dist/plugins.test.d.ts +0 -1
- package/dist/plugins.test.js +0 -902
- package/dist/populate.test.d.ts +0 -1
- package/dist/populate.test.js +0 -370
- package/dist/realtime/realtime.test.d.ts +0 -10
- package/dist/realtime/realtime.test.js +0 -4213
- package/dist/realtime/realtimeAppSetup.test.d.ts +0 -1
- package/dist/realtime/realtimeAppSetup.test.js +0 -213
- package/dist/requestContext.test.d.ts +0 -1
- package/dist/requestContext.test.js +0 -412
- package/dist/scriptRunner.test.d.ts +0 -1
- package/dist/scriptRunner.test.js +0 -386
- package/dist/secretProviders.test.d.ts +0 -1
- package/dist/secretProviders.test.js +0 -726
- package/dist/syncConsents.test.d.ts +0 -1
- package/dist/syncConsents.test.js +0 -522
- package/dist/terrenoApp.test.d.ts +0 -1
- package/dist/terrenoApp.test.js +0 -489
- package/dist/tests/bunSetup.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.js +0 -37
- package/dist/tests/testEnv.d.ts +0 -0
- package/dist/tests/testEnv.js +0 -6
- package/dist/transformers.test.d.ts +0 -1
- package/dist/transformers.test.js +0 -423
- package/dist/utils.test.d.ts +0 -1
- package/dist/utils.test.js +0 -242
- package/src/realtime/realtimeAppSetup.test.ts +0 -161
- /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
+
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);
|
|
28
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
+
function step(op) {
|
|
31
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
33
|
+
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;
|
|
34
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
+
switch (op[0]) {
|
|
36
|
+
case 0: case 1: t = op; break;
|
|
37
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
+
default:
|
|
41
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
+
if (t[2]) _.ops.pop();
|
|
46
|
+
_.trys.pop(); continue;
|
|
47
|
+
}
|
|
48
|
+
op = body.call(thisArg, _);
|
|
49
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
|
+
exports.resolveUserStreamsForEntry = exports.MissingScopeResolverError = exports.assertWritableStream = exports.resolveStreamForDoc = exports.parseStreamKey = exports.streamForScopeValue = exports.getScopeField = void 0;
|
|
55
|
+
var errors_1 = require("../errors");
|
|
56
|
+
/**
|
|
57
|
+
* Stream key resolution for SyncDB.
|
|
58
|
+
*
|
|
59
|
+
* A stream is the unit of ordered delivery and cursor resumption:
|
|
60
|
+
* `{collectionTag}|owner:{ownerId}`, `{collectionTag}|tenant:{tenantId}`,
|
|
61
|
+
* `{collectionTag}|all` (broadcast), or `{collectionTag}|custom:{value}`.
|
|
62
|
+
*/
|
|
63
|
+
/** The document field a scope reads, or null for broadcast/custom scopes. */
|
|
64
|
+
var getScopeField = function (scope) {
|
|
65
|
+
var _a;
|
|
66
|
+
if (typeof scope === "function") {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
if (scope.type === "owner") {
|
|
70
|
+
return (_a = scope.field) !== null && _a !== void 0 ? _a : "ownerId";
|
|
71
|
+
}
|
|
72
|
+
if (scope.type === "tenant") {
|
|
73
|
+
return scope.field;
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
};
|
|
77
|
+
exports.getScopeField = getScopeField;
|
|
78
|
+
/** Build a stream key from a collection tag and a raw scope value. */
|
|
79
|
+
var streamForScopeValue = function (_a) {
|
|
80
|
+
var collectionTag = _a.collectionTag, scope = _a.scope, scopeValue = _a.scopeValue;
|
|
81
|
+
if (typeof scope === "function") {
|
|
82
|
+
return "".concat(collectionTag, "|custom:").concat(String(scopeValue));
|
|
83
|
+
}
|
|
84
|
+
if (scope.type === "owner") {
|
|
85
|
+
return "".concat(collectionTag, "|owner:").concat(String(scopeValue));
|
|
86
|
+
}
|
|
87
|
+
if (scope.type === "tenant") {
|
|
88
|
+
return "".concat(collectionTag, "|tenant:").concat(String(scopeValue));
|
|
89
|
+
}
|
|
90
|
+
return "".concat(collectionTag, "|all");
|
|
91
|
+
};
|
|
92
|
+
exports.streamForScopeValue = streamForScopeValue;
|
|
93
|
+
/**
|
|
94
|
+
* Parse a stream key into its collection tag and scope value. The value is everything
|
|
95
|
+
* after the first `:` following the `|`-delimited collection tag, so custom values
|
|
96
|
+
* containing `:` survive intact. Broadcast streams (`{tag}|all`) yield `scopeValue: null`.
|
|
97
|
+
* Returns null when the key is not a valid stream key.
|
|
98
|
+
*/
|
|
99
|
+
var parseStreamKey = function (stream) {
|
|
100
|
+
var pipe = stream.indexOf("|");
|
|
101
|
+
if (pipe <= 0) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
var collectionTag = stream.slice(0, pipe);
|
|
105
|
+
var rest = stream.slice(pipe + 1);
|
|
106
|
+
if (rest === "all") {
|
|
107
|
+
return { collectionTag: collectionTag, scopeKind: "all", scopeValue: null };
|
|
108
|
+
}
|
|
109
|
+
var colon = rest.indexOf(":");
|
|
110
|
+
if (colon < 0) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
collectionTag: collectionTag,
|
|
115
|
+
scopeKind: rest.slice(0, colon),
|
|
116
|
+
scopeValue: rest.slice(colon + 1),
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
exports.parseStreamKey = parseStreamKey;
|
|
120
|
+
/**
|
|
121
|
+
* Resolve the stream a document belongs to under the given scope.
|
|
122
|
+
*
|
|
123
|
+
* Deliberately total: readers (the change-stream watcher, the tombstone compactor) must
|
|
124
|
+
* be able to name a stream for any document already in the database, including legacy
|
|
125
|
+
* rows written before a scope field existed. Rejecting an unroutable document is the
|
|
126
|
+
* write path's job — see {@link assertWritableStream}.
|
|
127
|
+
*/
|
|
128
|
+
var resolveStreamForDoc = function (_a) {
|
|
129
|
+
var collectionTag = _a.collectionTag, scope = _a.scope, doc = _a.doc;
|
|
130
|
+
if (typeof scope === "function") {
|
|
131
|
+
return (0, exports.streamForScopeValue)({ collectionTag: collectionTag, scope: scope, scopeValue: scope(doc) });
|
|
132
|
+
}
|
|
133
|
+
if (scope.type === "broadcast") {
|
|
134
|
+
return (0, exports.streamForScopeValue)({ collectionTag: collectionTag, scope: scope, scopeValue: null });
|
|
135
|
+
}
|
|
136
|
+
var field = (0, exports.getScopeField)(scope);
|
|
137
|
+
return (0, exports.streamForScopeValue)({ collectionTag: collectionTag, scope: scope, scopeValue: doc[field] });
|
|
138
|
+
};
|
|
139
|
+
exports.resolveStreamForDoc = resolveStreamForDoc;
|
|
140
|
+
/**
|
|
141
|
+
* Write-path guard: refuse to file a tenant-scoped document under `tenant:undefined`, a
|
|
142
|
+
* stream no client can ever subscribe to, which would leave the document written,
|
|
143
|
+
* invisible, and never synced (Task 9.21).
|
|
144
|
+
*
|
|
145
|
+
* Called from the `_syncSeq` plugin's write hooks, so it sees the EFFECTIVE scope value —
|
|
146
|
+
* after any `preCreate` has injected it and before the write commits — on every write
|
|
147
|
+
* path, sync mutations and plain REST/model writes alike. That is why the sync mutation
|
|
148
|
+
* handler does not try to infer the same thing from the raw request body: a create may
|
|
149
|
+
* legitimately omit the field for `preCreate` to supply.
|
|
150
|
+
*
|
|
151
|
+
* Owner scopes are intentionally not guarded: `ownerId` is conventionally stamped by
|
|
152
|
+
* `preCreate`/`IsOwner` plumbing, and an unowned document is still admin-reachable, so
|
|
153
|
+
* tightening that is a separate behavioral change.
|
|
154
|
+
*/
|
|
155
|
+
var assertWritableStream = function (_a) {
|
|
156
|
+
var collectionTag = _a.collectionTag, scope = _a.scope, doc = _a.doc;
|
|
157
|
+
if (typeof scope === "function" || scope.type !== "tenant") {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
var field = (0, exports.getScopeField)(scope);
|
|
161
|
+
var scopeValue = doc[field];
|
|
162
|
+
if (scopeValue === undefined || scopeValue === null) {
|
|
163
|
+
throw new errors_1.APIError({
|
|
164
|
+
status: 400,
|
|
165
|
+
title: "Sync write on ".concat(collectionTag, " is missing tenant scope field \"").concat(field, "\""),
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
exports.assertWritableStream = assertWritableStream;
|
|
170
|
+
/**
|
|
171
|
+
* Resolve the streams a user currently belongs to for one registered entry — the
|
|
172
|
+
* authoritative membership set, shared by the socket `sync:subscribe` handler,
|
|
173
|
+
* `GET /sync/streams`, and the snapshot stream-membership check.
|
|
174
|
+
*
|
|
175
|
+
* - broadcast → the single `{collection}|all` stream.
|
|
176
|
+
* - owner → the single stream keyed by the authenticated user's own id (a client-supplied
|
|
177
|
+
* id must never select the stream).
|
|
178
|
+
* - tenant / custom → one stream per value from `getUserScopes`. Requires the resolver;
|
|
179
|
+
* throws {@link MissingScopeResolverError} when it is absent.
|
|
180
|
+
*
|
|
181
|
+
* Runs against the FULL user (D2) so tenant memberships resolve from current
|
|
182
|
+
* `organizationIds`.
|
|
183
|
+
*/
|
|
184
|
+
var MissingScopeResolverError = /** @class */ (function (_super) {
|
|
185
|
+
__extends(MissingScopeResolverError, _super);
|
|
186
|
+
function MissingScopeResolverError(collection) {
|
|
187
|
+
var _this = _super.call(this, "Sync collection ".concat(collection, " requires a getUserScopes resolver on SyncApp")) || this;
|
|
188
|
+
_this.collection = collection;
|
|
189
|
+
_this.name = "MissingScopeResolverError";
|
|
190
|
+
return _this;
|
|
191
|
+
}
|
|
192
|
+
return MissingScopeResolverError;
|
|
193
|
+
}(Error));
|
|
194
|
+
exports.MissingScopeResolverError = MissingScopeResolverError;
|
|
195
|
+
var resolveUserStreamsForEntry = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
196
|
+
var scope, collection, scopeValues;
|
|
197
|
+
var entry = _b.entry, user = _b.user, getUserScopes = _b.getUserScopes;
|
|
198
|
+
return __generator(this, function (_c) {
|
|
199
|
+
switch (_c.label) {
|
|
200
|
+
case 0:
|
|
201
|
+
scope = entry.config.scope;
|
|
202
|
+
collection = entry.collectionTag;
|
|
203
|
+
if (typeof scope !== "function" && scope.type === "broadcast") {
|
|
204
|
+
return [2 /*return*/, [(0, exports.streamForScopeValue)({ collectionTag: collection, scope: scope, scopeValue: null })]];
|
|
205
|
+
}
|
|
206
|
+
if (typeof scope !== "function" && scope.type === "owner") {
|
|
207
|
+
return [2 /*return*/, [(0, exports.streamForScopeValue)({ collectionTag: collection, scope: scope, scopeValue: user.id })]];
|
|
208
|
+
}
|
|
209
|
+
if (!getUserScopes) {
|
|
210
|
+
throw new MissingScopeResolverError(collection);
|
|
211
|
+
}
|
|
212
|
+
return [4 /*yield*/, getUserScopes(user, entry)];
|
|
213
|
+
case 1:
|
|
214
|
+
scopeValues = _c.sent();
|
|
215
|
+
return [2 /*return*/, scopeValues.map(function (scopeValue) {
|
|
216
|
+
return (0, exports.streamForScopeValue)({ collectionTag: collection, scope: scope, scopeValue: scopeValue });
|
|
217
|
+
})];
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}); };
|
|
221
|
+
exports.resolveUserStreamsForEntry = resolveUserStreamsForEntry;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type express from "express";
|
|
2
|
+
import type { TerrenoPlugin } from "../terrenoPlugin";
|
|
3
|
+
import { type SyncAppOptions } from "./routes";
|
|
4
|
+
/** Resolve the SyncApp configuration attached to one Express application. */
|
|
5
|
+
export declare const getSyncAppOptions: (app: express.Application) => SyncAppOptions | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* TerrenoPlugin mounting the SyncDB local-first sync HTTP routes
|
|
8
|
+
* (`GET /sync/snapshot`, `POST /sync/mutate`, `GET /sync/key`). Models opt in via
|
|
9
|
+
* modelRouter's `sync` option; this plugin serves the registered collections.
|
|
10
|
+
*
|
|
11
|
+
* Registration attaches the plugin's options (notably `getUserScopes`) to this Express
|
|
12
|
+
* application so RealtimeApp's connection handler can install the socket
|
|
13
|
+
* mutation/subscription channel (`sync:subscribe`, `sync:mutate`) with the same
|
|
14
|
+
* configuration — the socket layer requires both plugins: SyncApp for config/routes and
|
|
15
|
+
* RealtimeApp for the Socket.io server and `sync:delta` emission.
|
|
16
|
+
*
|
|
17
|
+
* Registration also kicks off the bookkeeping-model index builds (`SyncCounter`,
|
|
18
|
+
* `SyncMutation`, `SyncScopeMove`, `SyncKey`) and enqueues them for `ensureSyncIndexes()`,
|
|
19
|
+
* which `TerrenoApp.start()` awaits before listening. Those indexes are correctness
|
|
20
|
+
* requirements — the unique `mutationId` index is what makes duplicate mutation deliveries
|
|
21
|
+
* idempotent, and the unique `stream` index is what keeps the counter upsert race from
|
|
22
|
+
* minting duplicate seqs — so an index-build failure fails startup loudly. Apps that build
|
|
23
|
+
* the Express app without `TerrenoApp.start()` should await `ensureSyncIndexes()` themselves.
|
|
24
|
+
*/
|
|
25
|
+
export declare class SyncApp implements TerrenoPlugin {
|
|
26
|
+
private readonly options;
|
|
27
|
+
constructor(options?: SyncAppOptions);
|
|
28
|
+
register(app: express.Application): void;
|
|
29
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SyncApp = exports.getSyncAppOptions = void 0;
|
|
4
|
+
var models_1 = require("./models");
|
|
5
|
+
var registry_1 = require("./registry");
|
|
6
|
+
var routes_1 = require("./routes");
|
|
7
|
+
var SYNC_APP_OPTIONS_LOCAL_KEY = "terrenoSyncAppOptions";
|
|
8
|
+
/** Resolve the SyncApp configuration attached to one Express application. */
|
|
9
|
+
var getSyncAppOptions = function (app) {
|
|
10
|
+
return app.locals[SYNC_APP_OPTIONS_LOCAL_KEY];
|
|
11
|
+
};
|
|
12
|
+
exports.getSyncAppOptions = getSyncAppOptions;
|
|
13
|
+
/**
|
|
14
|
+
* TerrenoPlugin mounting the SyncDB local-first sync HTTP routes
|
|
15
|
+
* (`GET /sync/snapshot`, `POST /sync/mutate`, `GET /sync/key`). Models opt in via
|
|
16
|
+
* modelRouter's `sync` option; this plugin serves the registered collections.
|
|
17
|
+
*
|
|
18
|
+
* Registration attaches the plugin's options (notably `getUserScopes`) to this Express
|
|
19
|
+
* application so RealtimeApp's connection handler can install the socket
|
|
20
|
+
* mutation/subscription channel (`sync:subscribe`, `sync:mutate`) with the same
|
|
21
|
+
* configuration — the socket layer requires both plugins: SyncApp for config/routes and
|
|
22
|
+
* RealtimeApp for the Socket.io server and `sync:delta` emission.
|
|
23
|
+
*
|
|
24
|
+
* Registration also kicks off the bookkeeping-model index builds (`SyncCounter`,
|
|
25
|
+
* `SyncMutation`, `SyncScopeMove`, `SyncKey`) and enqueues them for `ensureSyncIndexes()`,
|
|
26
|
+
* which `TerrenoApp.start()` awaits before listening. Those indexes are correctness
|
|
27
|
+
* requirements — the unique `mutationId` index is what makes duplicate mutation deliveries
|
|
28
|
+
* idempotent, and the unique `stream` index is what keeps the counter upsert race from
|
|
29
|
+
* minting duplicate seqs — so an index-build failure fails startup loudly. Apps that build
|
|
30
|
+
* the Express app without `TerrenoApp.start()` should await `ensureSyncIndexes()` themselves.
|
|
31
|
+
*/
|
|
32
|
+
var SyncApp = /** @class */ (function () {
|
|
33
|
+
function SyncApp(options) {
|
|
34
|
+
if (options === void 0) { options = {}; }
|
|
35
|
+
this.options = options;
|
|
36
|
+
}
|
|
37
|
+
SyncApp.prototype.register = function (app) {
|
|
38
|
+
app.locals[SYNC_APP_OPTIONS_LOCAL_KEY] = this.options;
|
|
39
|
+
(0, registry_1.trackSyncIndexCreation)((0, models_1.ensureSyncModelIndexes)());
|
|
40
|
+
(0, routes_1.addSyncRoutes)(app, this.options);
|
|
41
|
+
};
|
|
42
|
+
return SyncApp;
|
|
43
|
+
}());
|
|
44
|
+
exports.SyncApp = SyncApp;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type express from "express";
|
|
2
|
+
import type { User } from "../auth";
|
|
3
|
+
import { type SyncMutationOutcome, type SyncMutationScopeResolver } from "./mutationHandler";
|
|
4
|
+
import type { SyncMutateBatchResponse, SyncMutateRequest } from "./types";
|
|
5
|
+
/**
|
|
6
|
+
* Task 9.20: the single orchestration both mutation transports run.
|
|
7
|
+
*
|
|
8
|
+
* `POST /sync/mutate{,/batch}` and `sync:mutate{,Batch}` used to each carry their own copy
|
|
9
|
+
* of the size-cap -> rate-limit -> validate -> apply sequence, and the socket budget lived
|
|
10
|
+
* in a per-connection closure — so one user on N sockets got N times the budget, while the
|
|
11
|
+
* HTTP window map was per-user but never evicted. Everything now funnels through
|
|
12
|
+
* {@link runSyncMutation} / {@link runSyncBatch} against ONE per-user window map, so a
|
|
13
|
+
* user's budget is theirs no matter how many sockets or HTTP requests they spread it over.
|
|
14
|
+
*
|
|
15
|
+
* Multi-instance semantics: the window map is per PROCESS, so a deployment running N API
|
|
16
|
+
* instances behind a load balancer grants up to N times this budget per user in the worst
|
|
17
|
+
* case. That is intentional — this limiter exists to stop a runaway client from saturating
|
|
18
|
+
* one process, not to meter fair use across a cluster. A cluster-wide limit needs shared
|
|
19
|
+
* state (Redis) and belongs in front of the app, not here.
|
|
20
|
+
*/
|
|
21
|
+
export declare const MAX_SYNC_MUTATIONS_PER_SECOND = 100;
|
|
22
|
+
/**
|
|
23
|
+
* Drop windows that have expired, so the map does not grow with every user ever seen.
|
|
24
|
+
* Runs automatically (throttled to {@link WINDOW_EVICTION_INTERVAL_MS}) whenever budget is
|
|
25
|
+
* charged; `now` is injectable so the sweep can be exercised without waiting a minute.
|
|
26
|
+
* Returns the number of windows still retained.
|
|
27
|
+
*/
|
|
28
|
+
export declare const evictExpiredSyncMutationWindows: ({ now, }?: {
|
|
29
|
+
now?: number;
|
|
30
|
+
}) => number;
|
|
31
|
+
/** Clear every per-user window. Test-only: windows are module state shared across suites. */
|
|
32
|
+
export declare const resetSyncMutationRateLimits: () => void;
|
|
33
|
+
/** Number of retained per-user windows — lets tests assert eviction actually happens. */
|
|
34
|
+
export declare const syncMutationRateLimitWindowCount: () => number;
|
|
35
|
+
/**
|
|
36
|
+
* Which stage produced the result. The HTTP routes map this to a status code
|
|
37
|
+
* (`rate_limited` -> 429, `validation` -> 422, `applied` -> 200); the socket handlers
|
|
38
|
+
* ignore it and just return the payload.
|
|
39
|
+
*/
|
|
40
|
+
export type SyncRunStage = "applied" | "rate_limited" | "validation";
|
|
41
|
+
export interface SyncMutationRunResult {
|
|
42
|
+
stage: Exclude<SyncRunStage, "validation">;
|
|
43
|
+
outcome: SyncMutationOutcome;
|
|
44
|
+
}
|
|
45
|
+
export interface SyncBatchRunResult {
|
|
46
|
+
stage: SyncRunStage;
|
|
47
|
+
response: SyncMutateBatchResponse;
|
|
48
|
+
}
|
|
49
|
+
/** Shared orchestration for a SINGLE mutation: rate limit, then apply. */
|
|
50
|
+
export declare const runSyncMutation: ({ mutation, req, scopeResolver, user, }: {
|
|
51
|
+
mutation: SyncMutateRequest;
|
|
52
|
+
/** The real Express request when called over HTTP; hooks receive a `{user}` stub otherwise. */
|
|
53
|
+
req?: express.Request;
|
|
54
|
+
scopeResolver?: SyncMutationScopeResolver;
|
|
55
|
+
user: User;
|
|
56
|
+
}) => Promise<SyncMutationRunResult>;
|
|
57
|
+
/**
|
|
58
|
+
* Shared orchestration for a BATCH: validate (size cap + intra-batch duplicate ids),
|
|
59
|
+
* then rate limit, then apply.
|
|
60
|
+
*
|
|
61
|
+
* Validation runs FIRST so a malformed batch consumes no budget — it was never going to
|
|
62
|
+
* be applied, and charging for it let a buggy client rate-limit itself out of the
|
|
63
|
+
* mutations that would have succeeded. The budget is then charged per MUTATION (not per
|
|
64
|
+
* batch), so batching cannot buy extra throughput over single sends.
|
|
65
|
+
*/
|
|
66
|
+
export declare const runSyncBatch: ({ mutations, req, scopeResolver, user, }: {
|
|
67
|
+
mutations: SyncMutateRequest[];
|
|
68
|
+
req?: express.Request;
|
|
69
|
+
scopeResolver?: SyncMutationScopeResolver;
|
|
70
|
+
user: User;
|
|
71
|
+
}) => Promise<SyncBatchRunResult>;
|
|
@@ -0,0 +1,252 @@
|
|
|
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.runSyncBatch = exports.runSyncMutation = exports.syncMutationRateLimitWindowCount = exports.resetSyncMutationRateLimits = exports.evictExpiredSyncMutationWindows = exports.MAX_SYNC_MUTATIONS_PER_SECOND = void 0;
|
|
78
|
+
var logger_1 = require("../logger");
|
|
79
|
+
var mutationHandler_1 = require("./mutationHandler");
|
|
80
|
+
/**
|
|
81
|
+
* Task 9.20: the single orchestration both mutation transports run.
|
|
82
|
+
*
|
|
83
|
+
* `POST /sync/mutate{,/batch}` and `sync:mutate{,Batch}` used to each carry their own copy
|
|
84
|
+
* of the size-cap -> rate-limit -> validate -> apply sequence, and the socket budget lived
|
|
85
|
+
* in a per-connection closure — so one user on N sockets got N times the budget, while the
|
|
86
|
+
* HTTP window map was per-user but never evicted. Everything now funnels through
|
|
87
|
+
* {@link runSyncMutation} / {@link runSyncBatch} against ONE per-user window map, so a
|
|
88
|
+
* user's budget is theirs no matter how many sockets or HTTP requests they spread it over.
|
|
89
|
+
*
|
|
90
|
+
* Multi-instance semantics: the window map is per PROCESS, so a deployment running N API
|
|
91
|
+
* instances behind a load balancer grants up to N times this budget per user in the worst
|
|
92
|
+
* case. That is intentional — this limiter exists to stop a runaway client from saturating
|
|
93
|
+
* one process, not to meter fair use across a cluster. A cluster-wide limit needs shared
|
|
94
|
+
* state (Redis) and belongs in front of the app, not here.
|
|
95
|
+
*/
|
|
96
|
+
exports.MAX_SYNC_MUTATIONS_PER_SECOND = 100;
|
|
97
|
+
/** Width of the rolling per-user mutation budget window. */
|
|
98
|
+
var RATE_LIMIT_WINDOW_MS = 1000;
|
|
99
|
+
/**
|
|
100
|
+
* How often expired windows are swept out of the map. Windows are only 1s wide, so an
|
|
101
|
+
* unswept map would otherwise retain one entry per user seen since process start.
|
|
102
|
+
*/
|
|
103
|
+
var WINDOW_EVICTION_INTERVAL_MS = 60000;
|
|
104
|
+
/** userId -> rolling one-second mutation window, shared by every transport. */
|
|
105
|
+
var mutationWindows = new Map();
|
|
106
|
+
var lastEvictionAt = 0;
|
|
107
|
+
/**
|
|
108
|
+
* Drop windows that have expired, so the map does not grow with every user ever seen.
|
|
109
|
+
* Runs automatically (throttled to {@link WINDOW_EVICTION_INTERVAL_MS}) whenever budget is
|
|
110
|
+
* charged; `now` is injectable so the sweep can be exercised without waiting a minute.
|
|
111
|
+
* Returns the number of windows still retained.
|
|
112
|
+
*/
|
|
113
|
+
var evictExpiredSyncMutationWindows = function (_a) {
|
|
114
|
+
var e_1, _b;
|
|
115
|
+
var _c = _a === void 0 ? {} : _a, _d = _c.now, now = _d === void 0 ? Date.now() : _d;
|
|
116
|
+
lastEvictionAt = now;
|
|
117
|
+
try {
|
|
118
|
+
for (var mutationWindows_1 = __values(mutationWindows), mutationWindows_1_1 = mutationWindows_1.next(); !mutationWindows_1_1.done; mutationWindows_1_1 = mutationWindows_1.next()) {
|
|
119
|
+
var _e = __read(mutationWindows_1_1.value, 2), userId = _e[0], window_1 = _e[1];
|
|
120
|
+
if (now - window_1.windowStart >= RATE_LIMIT_WINDOW_MS) {
|
|
121
|
+
mutationWindows.delete(userId);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
126
|
+
finally {
|
|
127
|
+
try {
|
|
128
|
+
if (mutationWindows_1_1 && !mutationWindows_1_1.done && (_b = mutationWindows_1.return)) _b.call(mutationWindows_1);
|
|
129
|
+
}
|
|
130
|
+
finally { if (e_1) throw e_1.error; }
|
|
131
|
+
}
|
|
132
|
+
return mutationWindows.size;
|
|
133
|
+
};
|
|
134
|
+
exports.evictExpiredSyncMutationWindows = evictExpiredSyncMutationWindows;
|
|
135
|
+
/** Sweep expired windows at most once per {@link WINDOW_EVICTION_INTERVAL_MS}. */
|
|
136
|
+
var maybeEvictExpiredWindows = function (now) {
|
|
137
|
+
if (now - lastEvictionAt < WINDOW_EVICTION_INTERVAL_MS) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
(0, exports.evictExpiredSyncMutationWindows)({ now: now });
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Charge `weight` mutations to `userId`'s rolling window. Returns `undefined` when the
|
|
144
|
+
* budget allowed it (and the weight was consumed), or the ms remaining in the window when
|
|
145
|
+
* it did not — the caller nacks `rate_limited` with that as `retryAfterMs` rather than
|
|
146
|
+
* burning any retry budget (rate limiting must never look like a durable-data error).
|
|
147
|
+
*
|
|
148
|
+
* A rejected request consumes nothing: a client held over budget would otherwise keep its
|
|
149
|
+
* own window pinned full and never recover within the second.
|
|
150
|
+
*/
|
|
151
|
+
var consumeMutationBudget = function (_a) {
|
|
152
|
+
var userId = _a.userId, weight = _a.weight;
|
|
153
|
+
var now = Date.now();
|
|
154
|
+
maybeEvictExpiredWindows(now);
|
|
155
|
+
var existing = mutationWindows.get(userId);
|
|
156
|
+
var window = !existing || now - existing.windowStart >= RATE_LIMIT_WINDOW_MS
|
|
157
|
+
? { count: 0, windowStart: now }
|
|
158
|
+
: existing;
|
|
159
|
+
if (window.count + weight > exports.MAX_SYNC_MUTATIONS_PER_SECOND) {
|
|
160
|
+
// Keep the window as-is (a fresh one is not stored) so the caller's retryAfterMs
|
|
161
|
+
// reflects the window that is actually full.
|
|
162
|
+
return Math.max(0, RATE_LIMIT_WINDOW_MS - (now - window.windowStart));
|
|
163
|
+
}
|
|
164
|
+
window.count += weight;
|
|
165
|
+
mutationWindows.set(userId, window);
|
|
166
|
+
return undefined;
|
|
167
|
+
};
|
|
168
|
+
/** Clear every per-user window. Test-only: windows are module state shared across suites. */
|
|
169
|
+
var resetSyncMutationRateLimits = function () {
|
|
170
|
+
mutationWindows.clear();
|
|
171
|
+
lastEvictionAt = 0;
|
|
172
|
+
};
|
|
173
|
+
exports.resetSyncMutationRateLimits = resetSyncMutationRateLimits;
|
|
174
|
+
/** Number of retained per-user windows — lets tests assert eviction actually happens. */
|
|
175
|
+
var syncMutationRateLimitWindowCount = function () { return mutationWindows.size; };
|
|
176
|
+
exports.syncMutationRateLimitWindowCount = syncMutationRateLimitWindowCount;
|
|
177
|
+
/** The `rate_limited` nack both transports return, minus the mutationId the caller knows. */
|
|
178
|
+
var rateLimitNack = function (retryAfterMs) { return ({
|
|
179
|
+
code: "rate_limited",
|
|
180
|
+
message: "Rate limit of ".concat(exports.MAX_SYNC_MUTATIONS_PER_SECOND, " mutations per second exceeded"),
|
|
181
|
+
retryAfterMs: retryAfterMs,
|
|
182
|
+
}); };
|
|
183
|
+
/** Shared orchestration for a SINGLE mutation: rate limit, then apply. */
|
|
184
|
+
var runSyncMutation = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
185
|
+
var userId, mutationId, retryAfterMs;
|
|
186
|
+
var _c;
|
|
187
|
+
var mutation = _b.mutation, req = _b.req, scopeResolver = _b.scopeResolver, user = _b.user;
|
|
188
|
+
return __generator(this, function (_d) {
|
|
189
|
+
switch (_d.label) {
|
|
190
|
+
case 0:
|
|
191
|
+
userId = String(user.id);
|
|
192
|
+
mutationId = typeof (mutation === null || mutation === void 0 ? void 0 : mutation.mutationId) === "string" ? mutation.mutationId : "";
|
|
193
|
+
retryAfterMs = consumeMutationBudget({ userId: userId, weight: 1 });
|
|
194
|
+
if (retryAfterMs !== undefined) {
|
|
195
|
+
logger_1.logger.info("[sync] User hit the sync mutation rate limit", { mutationId: mutationId, userId: userId });
|
|
196
|
+
return [2 /*return*/, {
|
|
197
|
+
outcome: { nack: __assign({ mutationId: mutationId }, rateLimitNack(retryAfterMs)), type: "nack" },
|
|
198
|
+
stage: "rate_limited",
|
|
199
|
+
}];
|
|
200
|
+
}
|
|
201
|
+
_c = {};
|
|
202
|
+
return [4 /*yield*/, (0, mutationHandler_1.applySyncMutation)({ mutation: mutation, req: req, scopeResolver: scopeResolver, user: user })];
|
|
203
|
+
case 1: return [2 /*return*/, (_c.outcome = _d.sent(),
|
|
204
|
+
_c.stage = "applied",
|
|
205
|
+
_c)];
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}); };
|
|
209
|
+
exports.runSyncMutation = runSyncMutation;
|
|
210
|
+
/**
|
|
211
|
+
* Shared orchestration for a BATCH: validate (size cap + intra-batch duplicate ids),
|
|
212
|
+
* then rate limit, then apply.
|
|
213
|
+
*
|
|
214
|
+
* Validation runs FIRST so a malformed batch consumes no budget — it was never going to
|
|
215
|
+
* be applied, and charging for it let a buggy client rate-limit itself out of the
|
|
216
|
+
* mutations that would have succeeded. The budget is then charged per MUTATION (not per
|
|
217
|
+
* batch), so batching cannot buy extra throughput over single sends.
|
|
218
|
+
*/
|
|
219
|
+
var runSyncBatch = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
220
|
+
var validation, userId, retryAfterMs;
|
|
221
|
+
var _c;
|
|
222
|
+
var mutations = _b.mutations, req = _b.req, scopeResolver = _b.scopeResolver, user = _b.user;
|
|
223
|
+
return __generator(this, function (_d) {
|
|
224
|
+
switch (_d.label) {
|
|
225
|
+
case 0:
|
|
226
|
+
validation = (0, mutationHandler_1.validateSyncMutationBatch)(mutations);
|
|
227
|
+
if (!validation.ok) {
|
|
228
|
+
return [2 /*return*/, { response: validation.response, stage: "validation" }];
|
|
229
|
+
}
|
|
230
|
+
userId = String(user.id);
|
|
231
|
+
retryAfterMs = consumeMutationBudget({ userId: userId, weight: mutations.length });
|
|
232
|
+
if (retryAfterMs !== undefined) {
|
|
233
|
+
logger_1.logger.info("[sync] User hit the sync batch mutation rate limit", {
|
|
234
|
+
batchSize: mutations.length,
|
|
235
|
+
userId: userId,
|
|
236
|
+
});
|
|
237
|
+
return [2 /*return*/, {
|
|
238
|
+
response: {
|
|
239
|
+
results: [{ nack: __assign({ mutationId: "" }, rateLimitNack(retryAfterMs)), type: "nack" }],
|
|
240
|
+
},
|
|
241
|
+
stage: "rate_limited",
|
|
242
|
+
}];
|
|
243
|
+
}
|
|
244
|
+
_c = {};
|
|
245
|
+
return [4 /*yield*/, (0, mutationHandler_1.applySyncMutationBatch)({ mutations: mutations, req: req, scopeResolver: scopeResolver, user: user })];
|
|
246
|
+
case 1: return [2 /*return*/, (_c.response = _d.sent(),
|
|
247
|
+
_c.stage = "applied",
|
|
248
|
+
_c)];
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
}); };
|
|
252
|
+
exports.runSyncBatch = runSyncBatch;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Schema } from "mongoose";
|
|
2
|
+
/**
|
|
3
|
+
* C4: how many times a scope-move marker insert is attempted before giving up. Losing the
|
|
4
|
+
* marker loses the old stream's tombstone forever (the exact race C4 exists to eliminate),
|
|
5
|
+
* so a transient failure (replica step-down, transient network error) is retried.
|
|
6
|
+
*/
|
|
7
|
+
export declare const SCOPE_MOVE_MARKER_ATTEMPTS = 3;
|
|
8
|
+
export declare const syncPlugin: (schema: Schema<any, any, any, any>) => void;
|