@terreno/api 57.4.0 → 57.6.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/README.md +5 -5
- package/bunfig.toml +1 -1
- package/dist/actions.js +197 -368
- package/dist/adminModelRouter.js +88 -173
- package/dist/api.js +598 -852
- package/dist/audit/auditApp.js +48 -89
- package/dist/audit/auditEventModel.js +15 -16
- package/dist/audit/cloudTasksEnqueue.js +56 -149
- package/dist/audit/diff.js +70 -160
- package/dist/audit/processQueue.js +25 -70
- package/dist/audit/rbacSink.js +25 -66
- package/dist/audit/record.js +129 -251
- package/dist/auth.js +432 -681
- package/dist/authRecovery.js +155 -269
- package/dist/authTokens.js +43 -118
- package/dist/betterAuthApp.js +25 -26
- package/dist/betterAuthSetup.js +258 -413
- package/dist/browserLogsRoute.d.ts +6 -0
- package/dist/browserLogsRoute.js +97 -0
- package/dist/collectionRegistry.js +32 -47
- package/dist/config.js +53 -122
- package/dist/configurationApp.js +180 -361
- package/dist/configurationPlugin.js +130 -302
- package/dist/consentApp.js +276 -421
- package/dist/docLoader.js +78 -126
- package/dist/envConfigurationPlugin.js +25 -147
- package/dist/errors.js +151 -247
- package/dist/example.js +28 -34
- package/dist/expressServer.d.ts +10 -0
- package/dist/expressServer.js +109 -255
- package/dist/frameworkModelNames.d.ts +24 -0
- package/dist/frameworkModelNames.js +31 -0
- package/dist/githubAuth.js +152 -216
- package/dist/httpClient.js +176 -264
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -2
- package/dist/logger.js +145 -278
- package/dist/mcp/auth.js +76 -128
- package/dist/mcp/createMCPRequest.js +4 -5
- package/dist/mcp/handlers.js +268 -480
- package/dist/mcp/query.js +56 -150
- package/dist/mcp/registry.js +17 -30
- package/dist/mcp/schemaGenerator.js +95 -181
- package/dist/mcp/server.js +28 -100
- package/dist/mcp/serviceTokens.js +102 -191
- package/dist/mcp/toolGenerator.js +78 -191
- package/dist/middleware.js +15 -27
- package/dist/migrations/fixtures/all-down/20260910120000-one.js +2 -42
- package/dist/migrations/fixtures/all-down/20260910120001-two.js +2 -42
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +1 -39
- package/dist/migrations/fixtures/bad-name/not-a-migration.js +1 -39
- package/dist/migrations/fixtures/valid/20260910120000-alpha.js +1 -39
- package/dist/migrations/fixtures/valid/20260910120001-beta.js +2 -42
- package/dist/migrations/generate.js +107 -229
- package/dist/migrations/load.js +75 -201
- package/dist/migrations/lock.js +54 -126
- package/dist/migrations/runner.js +192 -446
- package/dist/migrations/schemaCatalog.js +122 -263
- package/dist/models/consentForm.js +6 -6
- package/dist/models/consentResponse.js +5 -5
- package/dist/models/mcpServiceToken.js +66 -156
- package/dist/models/notification.js +6 -6
- package/dist/models/notificationPreference.js +6 -6
- package/dist/models/versionConfig.js +5 -5
- package/dist/notifications/notificationService.js +133 -273
- package/dist/notifications/notificationsApp.js +85 -179
- package/dist/notifications/notificationsBeforeSend.js +19 -64
- package/dist/notifiers/googleChatNotifier.js +36 -117
- package/dist/notifiers/slackNotifier.d.ts +31 -2
- package/dist/notifiers/slackNotifier.js +170 -119
- package/dist/notifiers/zoomNotifier.js +60 -111
- package/dist/openApi.js +107 -122
- package/dist/openApiBuilder.js +117 -159
- package/dist/openApiCompat.js +81 -148
- package/dist/openApiEtag.js +7 -7
- package/dist/openApiValidator.js +177 -286
- package/dist/orgs/orgContext.js +87 -157
- package/dist/orgs/orgPermissions.js +29 -68
- package/dist/orgs/orgPlugin.js +9 -27
- package/dist/orgs/organizationModel.js +65 -166
- package/dist/orgs/organizationSettings.js +29 -36
- package/dist/orgs/orgsApp.js +346 -608
- package/dist/permissions.js +88 -181
- package/dist/plugins.js +125 -218
- package/dist/populate.js +130 -240
- package/dist/rateLimit/applyTrustProxy.js +1 -1
- package/dist/rateLimit/createStore.js +7 -8
- package/dist/rateLimit/memoryStore.js +22 -100
- package/dist/rateLimit/middleware.js +48 -99
- package/dist/rateLimit/mongoStore.js +54 -112
- package/dist/rateLimit/policies.js +41 -75
- package/dist/rateLimit/redisStore.js +39 -133
- package/dist/rbac/access.js +119 -267
- package/dist/rbac/assertAllowed.js +95 -171
- package/dist/rbac/auditModel.js +14 -58
- package/dist/rbac/backfillAdmins.js +24 -130
- package/dist/rbac/fieldViews.js +46 -141
- package/dist/rbac/membershipOrgAdminSource.js +28 -99
- package/dist/rbac/middleware.js +30 -96
- package/dist/rbac/modelRouterAccess.js +130 -307
- package/dist/rbac/permissionUtils.js +40 -179
- package/dist/rbac/realtimeAccess.js +81 -147
- package/dist/rbac/resolve.d.ts +1 -1
- package/dist/rbac/resolve.js +148 -342
- package/dist/rbac/roleManager.js +318 -700
- package/dist/rbac/roleModel.js +44 -171
- package/dist/rbac/routes.js +93 -196
- package/dist/rbac/scopes.js +39 -133
- package/dist/rbac/statements.js +23 -91
- package/dist/rbac/userPlugin.js +3 -29
- package/dist/realtime/changeStreamWatcher.js +489 -800
- package/dist/realtime/deprecation.js +8 -10
- package/dist/realtime/queryMatcher.js +110 -191
- package/dist/realtime/queryStore.js +35 -118
- package/dist/realtime/realtimeApp.js +247 -401
- package/dist/realtime/registry.js +19 -30
- package/dist/realtime/sessionRevalidation.js +132 -358
- package/dist/realtime/socketAuth.js +89 -208
- package/dist/realtime/socketUser.js +10 -56
- package/dist/requestContext.js +93 -139
- package/dist/schemaMetadata.js +179 -274
- package/dist/scriptRunner.js +96 -266
- package/dist/scrubAdminFields.js +36 -108
- package/dist/secretProviders.js +130 -299
- package/dist/seedRunner.js +221 -514
- package/dist/sync/adminBroadcastScope.js +58 -170
- package/dist/sync/adminWindowAccess.js +15 -86
- package/dist/sync/adminWindowMutation.js +110 -181
- package/dist/sync/executors.js +425 -629
- package/dist/sync/loadHarness.js +392 -689
- package/dist/sync/models.js +181 -341
- package/dist/sync/mutationHandler.js +533 -801
- package/dist/sync/registrationSideEffects.js +41 -95
- package/dist/sync/registry.js +29 -49
- package/dist/sync/routes.js +484 -817
- package/dist/sync/scripts/compactTombstones.js +83 -228
- package/dist/sync/serialize.js +18 -58
- package/dist/sync/socketHandlers.js +253 -478
- package/dist/sync/streams.js +49 -117
- package/dist/sync/syncApp.js +12 -15
- package/dist/sync/syncBatch.js +63 -168
- package/dist/sync/syncSeqPlugin.js +430 -702
- package/dist/syncConsents.js +113 -253
- package/dist/terrenoApp.js +166 -276
- package/dist/tests/createTestData.js +146 -256
- package/dist/tests/models.js +14 -54
- package/dist/tests/mongoTestSetup.js +42 -103
- package/dist/tests/testHelper.js +31 -93
- package/dist/tests.js +12 -51
- package/dist/transformers.js +61 -164
- package/dist/utils.js +31 -93
- package/dist/vendor/wesleytodd-openapi/index.js +37 -51
- package/dist/vendor/wesleytodd-openapi/lib/generate-doc.js +32 -40
- package/dist/vendor/wesleytodd-openapi/lib/layer-schema.js +3 -3
- package/dist/vendor/wesleytodd-openapi/lib/ui.js +50 -19
- package/dist/vendor/wesleytodd-openapi/lib/validate.js +19 -47
- package/dist/versionCheckPlugin.js +52 -100
- package/dist/webhooks/headerValue.js +2 -2
- package/dist/webhooks/idempotency/memoryStore.js +16 -59
- package/dist/webhooks/idempotency/mongoStore.js +55 -130
- package/dist/webhooks/idempotency/webhookReceipt.js +9 -9
- package/dist/webhooks/rawBody.js +1 -1
- package/dist/webhooks/timingSafeEqual.js +4 -5
- package/dist/webhooks/verifiers/hmac.js +17 -18
- package/dist/webhooks/verifiers/sendgrid.js +20 -22
- package/dist/webhooks/verifiers/stripe.js +31 -65
- package/dist/webhooks/verifiers/twilio.js +23 -73
- package/dist/webhooks/webhooksApp.js +78 -177
- package/dist/zodOpenApi.js +2 -2
- package/package.json +6 -6
- package/src/__tests__/browserLogsRoute.test.ts +165 -0
- package/src/audit/auditApp.test.ts +5 -5
- package/src/audit/auditEventModel.ts +7 -3
- package/src/audit/auditRouter.test.ts +5 -5
- package/src/audit/rbacSink.test.ts +2 -2
- package/src/audit/record.test.ts +2 -2
- package/src/audit/record.ts +3 -2
- package/src/auth.ts +1 -1
- package/src/authRecovery.test.ts +43 -0
- package/src/authRecovery.ts +16 -10
- package/src/authTokens.ts +6 -2
- package/src/authVerification.test.ts +22 -0
- package/src/browserLogsRoute.ts +110 -0
- package/src/configurationPlugin.test.ts +1 -11
- package/src/expressServer.ts +10 -0
- package/src/frameworkModelNames.test.ts +62 -0
- package/src/frameworkModelNames.ts +33 -0
- package/src/index.ts +7 -0
- package/src/isolated/migrateCliConnect.isolated.ts +51 -0
- package/src/mcp/auth.test.ts +34 -1
- package/src/migrations/cli.test.ts +0 -23
- package/src/models/mcpServiceToken.ts +4 -3
- package/src/models/notification.ts +3 -2
- package/src/models/notificationPreference.ts +1 -1
- package/src/notifications/notificationsApp.test.ts +24 -1
- package/src/notifiers/slackNotifier.test.ts +186 -1
- package/src/notifiers/slackNotifier.ts +167 -8
- package/src/orgs/orgPlugin.ts +1 -1
- package/src/orgs/organizationModel.ts +11 -6
- package/src/plugins.test.ts +5 -0
- package/src/plugins.ts +3 -0
- package/src/realtime/realtimeApp.ts +1 -1
- package/src/sync/integration.test.ts +2 -2
- package/src/terrenoApp.ts +3 -0
- package/src/types.d.ts +1 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +4 -3
- package/tsconfig.json +13 -54
- package/tsconfig.typedoc.json +2 -0
package/dist/sync/streams.js
CHANGED
|
@@ -1,58 +1,7 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
3
|
exports.resolveUserStreamsForEntry = exports.MissingScopeResolverError = exports.assertWritableStream = exports.resolveStreamForDoc = exports.parseStreamKey = exports.streamForScopeValue = exports.adminBroadcastStream = exports.getScopeField = void 0;
|
|
55
|
-
|
|
4
|
+
const errors_1 = require("../errors");
|
|
56
5
|
/**
|
|
57
6
|
* Stream key resolution for SyncDB.
|
|
58
7
|
*
|
|
@@ -61,13 +10,12 @@ var errors_1 = require("../errors");
|
|
|
61
10
|
* `{collectionTag}|all` (broadcast), or `{collectionTag}|custom:{value}`.
|
|
62
11
|
*/
|
|
63
12
|
/** The document field a scope reads, or null for broadcast/custom scopes. */
|
|
64
|
-
|
|
65
|
-
var _a;
|
|
13
|
+
const getScopeField = (scope) => {
|
|
66
14
|
if (typeof scope === "function") {
|
|
67
15
|
return null;
|
|
68
16
|
}
|
|
69
17
|
if (scope.type === "owner") {
|
|
70
|
-
return
|
|
18
|
+
return scope.field ?? "ownerId";
|
|
71
19
|
}
|
|
72
20
|
if (scope.type === "tenant") {
|
|
73
21
|
return scope.field;
|
|
@@ -76,21 +24,20 @@ var getScopeField = function (scope) {
|
|
|
76
24
|
};
|
|
77
25
|
exports.getScopeField = getScopeField;
|
|
78
26
|
/** Fan-in stream for admin window subscribers when `SyncConfig.adminBroadcast` is true. */
|
|
79
|
-
|
|
27
|
+
const adminBroadcastStream = (collectionTag) => `${collectionTag}|admin`;
|
|
80
28
|
exports.adminBroadcastStream = adminBroadcastStream;
|
|
81
29
|
/** Build a stream key from a collection tag and a raw scope value. */
|
|
82
|
-
|
|
83
|
-
var collectionTag = _a.collectionTag, scope = _a.scope, scopeValue = _a.scopeValue;
|
|
30
|
+
const streamForScopeValue = ({ collectionTag, scope, scopeValue, }) => {
|
|
84
31
|
if (typeof scope === "function") {
|
|
85
|
-
return
|
|
32
|
+
return `${collectionTag}|custom:${String(scopeValue)}`;
|
|
86
33
|
}
|
|
87
34
|
if (scope.type === "owner") {
|
|
88
|
-
return
|
|
35
|
+
return `${collectionTag}|owner:${String(scopeValue)}`;
|
|
89
36
|
}
|
|
90
37
|
if (scope.type === "tenant") {
|
|
91
|
-
return
|
|
38
|
+
return `${collectionTag}|tenant:${String(scopeValue)}`;
|
|
92
39
|
}
|
|
93
|
-
return
|
|
40
|
+
return `${collectionTag}|all`;
|
|
94
41
|
};
|
|
95
42
|
exports.streamForScopeValue = streamForScopeValue;
|
|
96
43
|
/**
|
|
@@ -99,25 +46,25 @@ exports.streamForScopeValue = streamForScopeValue;
|
|
|
99
46
|
* containing `:` survive intact. Broadcast streams (`{tag}|all`) yield `scopeValue: null`.
|
|
100
47
|
* Returns null when the key is not a valid stream key.
|
|
101
48
|
*/
|
|
102
|
-
|
|
103
|
-
|
|
49
|
+
const parseStreamKey = (stream) => {
|
|
50
|
+
const pipe = stream.indexOf("|");
|
|
104
51
|
if (pipe <= 0) {
|
|
105
52
|
return null;
|
|
106
53
|
}
|
|
107
|
-
|
|
108
|
-
|
|
54
|
+
const collectionTag = stream.slice(0, pipe);
|
|
55
|
+
const rest = stream.slice(pipe + 1);
|
|
109
56
|
if (rest === "all") {
|
|
110
|
-
return { collectionTag
|
|
57
|
+
return { collectionTag, scopeKind: "all", scopeValue: null };
|
|
111
58
|
}
|
|
112
59
|
if (rest === "admin") {
|
|
113
|
-
return { collectionTag
|
|
60
|
+
return { collectionTag, scopeKind: "admin", scopeValue: null };
|
|
114
61
|
}
|
|
115
|
-
|
|
62
|
+
const colon = rest.indexOf(":");
|
|
116
63
|
if (colon < 0) {
|
|
117
64
|
return null;
|
|
118
65
|
}
|
|
119
66
|
return {
|
|
120
|
-
collectionTag
|
|
67
|
+
collectionTag,
|
|
121
68
|
scopeKind: rest.slice(0, colon),
|
|
122
69
|
scopeValue: rest.slice(colon + 1),
|
|
123
70
|
};
|
|
@@ -131,16 +78,15 @@ exports.parseStreamKey = parseStreamKey;
|
|
|
131
78
|
* rows written before a scope field existed. Rejecting an unroutable document is the
|
|
132
79
|
* write path's job — see {@link assertWritableStream}.
|
|
133
80
|
*/
|
|
134
|
-
|
|
135
|
-
var collectionTag = _a.collectionTag, scope = _a.scope, doc = _a.doc;
|
|
81
|
+
const resolveStreamForDoc = ({ collectionTag, scope, doc, }) => {
|
|
136
82
|
if (typeof scope === "function") {
|
|
137
|
-
return (0, exports.streamForScopeValue)({ collectionTag
|
|
83
|
+
return (0, exports.streamForScopeValue)({ collectionTag, scope, scopeValue: scope(doc) });
|
|
138
84
|
}
|
|
139
85
|
if (scope.type === "broadcast") {
|
|
140
|
-
return (0, exports.streamForScopeValue)({ collectionTag
|
|
86
|
+
return (0, exports.streamForScopeValue)({ collectionTag, scope, scopeValue: null });
|
|
141
87
|
}
|
|
142
|
-
|
|
143
|
-
return (0, exports.streamForScopeValue)({ collectionTag
|
|
88
|
+
const field = (0, exports.getScopeField)(scope);
|
|
89
|
+
return (0, exports.streamForScopeValue)({ collectionTag, scope, scopeValue: doc[field] });
|
|
144
90
|
};
|
|
145
91
|
exports.resolveStreamForDoc = resolveStreamForDoc;
|
|
146
92
|
/**
|
|
@@ -158,17 +104,16 @@ exports.resolveStreamForDoc = resolveStreamForDoc;
|
|
|
158
104
|
* `preCreate`/`IsOwner` plumbing, and an unowned document is still admin-reachable, so
|
|
159
105
|
* tightening that is a separate behavioral change.
|
|
160
106
|
*/
|
|
161
|
-
|
|
162
|
-
var collectionTag = _a.collectionTag, scope = _a.scope, doc = _a.doc;
|
|
107
|
+
const assertWritableStream = ({ collectionTag, scope, doc, }) => {
|
|
163
108
|
if (typeof scope === "function" || scope.type !== "tenant") {
|
|
164
109
|
return;
|
|
165
110
|
}
|
|
166
|
-
|
|
167
|
-
|
|
111
|
+
const field = (0, exports.getScopeField)(scope);
|
|
112
|
+
const scopeValue = doc[field];
|
|
168
113
|
if (scopeValue === undefined || scopeValue === null) {
|
|
169
114
|
throw new errors_1.APIError({
|
|
170
115
|
status: 400,
|
|
171
|
-
title:
|
|
116
|
+
title: `Sync write on ${collectionTag} is missing tenant scope field "${field}"`,
|
|
172
117
|
});
|
|
173
118
|
}
|
|
174
119
|
};
|
|
@@ -187,41 +132,28 @@ exports.assertWritableStream = assertWritableStream;
|
|
|
187
132
|
* Runs against the FULL user (D2) so tenant memberships resolve from current
|
|
188
133
|
* `organizationIds`.
|
|
189
134
|
*/
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
return MissingScopeResolverError;
|
|
199
|
-
}(Error));
|
|
135
|
+
class MissingScopeResolverError extends Error {
|
|
136
|
+
collection;
|
|
137
|
+
constructor(collection) {
|
|
138
|
+
super(`Sync collection ${collection} requires a getUserScopes resolver on SyncApp`);
|
|
139
|
+
this.collection = collection;
|
|
140
|
+
this.name = "MissingScopeResolverError";
|
|
141
|
+
}
|
|
142
|
+
}
|
|
200
143
|
exports.MissingScopeResolverError = MissingScopeResolverError;
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
throw new MissingScopeResolverError(collection);
|
|
217
|
-
}
|
|
218
|
-
return [4 /*yield*/, getUserScopes(user, entry)];
|
|
219
|
-
case 1:
|
|
220
|
-
scopeValues = _c.sent();
|
|
221
|
-
return [2 /*return*/, scopeValues.map(function (scopeValue) {
|
|
222
|
-
return (0, exports.streamForScopeValue)({ collectionTag: collection, scope: scope, scopeValue: scopeValue });
|
|
223
|
-
})];
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
}); };
|
|
144
|
+
const resolveUserStreamsForEntry = async ({ entry, user, getUserScopes, }) => {
|
|
145
|
+
const { scope } = entry.config;
|
|
146
|
+
const collection = entry.collectionTag;
|
|
147
|
+
if (typeof scope !== "function" && scope.type === "broadcast") {
|
|
148
|
+
return [(0, exports.streamForScopeValue)({ collectionTag: collection, scope, scopeValue: null })];
|
|
149
|
+
}
|
|
150
|
+
if (typeof scope !== "function" && scope.type === "owner") {
|
|
151
|
+
return [(0, exports.streamForScopeValue)({ collectionTag: collection, scope, scopeValue: user.id })];
|
|
152
|
+
}
|
|
153
|
+
if (!getUserScopes) {
|
|
154
|
+
throw new MissingScopeResolverError(collection);
|
|
155
|
+
}
|
|
156
|
+
const scopeValues = await getUserScopes(user, entry);
|
|
157
|
+
return scopeValues.map((scopeValue) => (0, exports.streamForScopeValue)({ collectionTag: collection, scope, scopeValue }));
|
|
158
|
+
};
|
|
227
159
|
exports.resolveUserStreamsForEntry = resolveUserStreamsForEntry;
|
package/dist/sync/syncApp.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SyncApp = exports.getSyncAppOptions = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const models_1 = require("./models");
|
|
5
|
+
const registry_1 = require("./registry");
|
|
6
|
+
const routes_1 = require("./routes");
|
|
7
|
+
const SYNC_APP_OPTIONS_LOCAL_KEY = "terrenoSyncAppOptions";
|
|
8
8
|
/** Resolve the SyncApp configuration attached to one Express application. */
|
|
9
|
-
|
|
10
|
-
return app.locals[SYNC_APP_OPTIONS_LOCAL_KEY];
|
|
11
|
-
};
|
|
9
|
+
const getSyncAppOptions = (app) => app.locals[SYNC_APP_OPTIONS_LOCAL_KEY];
|
|
12
10
|
exports.getSyncAppOptions = getSyncAppOptions;
|
|
13
11
|
/**
|
|
14
12
|
* TerrenoPlugin mounting the SyncDB local-first sync HTTP routes
|
|
@@ -30,16 +28,15 @@ exports.getSyncAppOptions = getSyncAppOptions;
|
|
|
30
28
|
* the Express app without `TerrenoApp.start()` should await `ensureSyncIndexes()` themselves.
|
|
31
29
|
* Pass `httpServer` when the process already bound `PORT` (Cloud Run startup probe).
|
|
32
30
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
class SyncApp {
|
|
32
|
+
options;
|
|
33
|
+
constructor(options = {}) {
|
|
36
34
|
this.options = options;
|
|
37
35
|
}
|
|
38
|
-
|
|
36
|
+
register(app) {
|
|
39
37
|
app.locals[SYNC_APP_OPTIONS_LOCAL_KEY] = this.options;
|
|
40
|
-
(0, registry_1.trackSyncIndexCreation)(
|
|
38
|
+
(0, registry_1.trackSyncIndexCreation)(() => (0, models_1.ensureSyncModelIndexes)());
|
|
41
39
|
(0, routes_1.addSyncRoutes)(app, this.options);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
}());
|
|
40
|
+
}
|
|
41
|
+
}
|
|
45
42
|
exports.SyncApp = SyncApp;
|
package/dist/sync/syncBatch.js
CHANGED
|
@@ -1,83 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
24
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var __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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
77
3
|
exports.runSyncBatch = exports.runSyncMutation = exports.syncMutationRateLimitWindowCount = exports.resetSyncMutationRateLimits = exports.evictExpiredSyncMutationWindows = exports.MAX_SYNC_MUTATIONS_PER_SECOND = void 0;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
4
|
+
const luxon_1 = require("luxon");
|
|
5
|
+
const logger_1 = require("../logger");
|
|
6
|
+
const mutationHandler_1 = require("./mutationHandler");
|
|
81
7
|
/**
|
|
82
8
|
* Task 9.20: the single orchestration both mutation transports run.
|
|
83
9
|
*
|
|
@@ -96,49 +22,37 @@ var mutationHandler_1 = require("./mutationHandler");
|
|
|
96
22
|
*/
|
|
97
23
|
exports.MAX_SYNC_MUTATIONS_PER_SECOND = 100;
|
|
98
24
|
/** Width of the rolling per-user mutation budget window. */
|
|
99
|
-
|
|
25
|
+
const RATE_LIMIT_WINDOW_MS = 1000;
|
|
100
26
|
/**
|
|
101
27
|
* How often expired windows are swept out of the map. Windows are only 1s wide, so an
|
|
102
28
|
* unswept map would otherwise retain one entry per user seen since process start.
|
|
103
29
|
*/
|
|
104
|
-
|
|
30
|
+
const WINDOW_EVICTION_INTERVAL_MS = 60_000;
|
|
105
31
|
/** userId -> rolling one-second mutation window, shared by every transport. */
|
|
106
|
-
|
|
107
|
-
|
|
32
|
+
const mutationWindows = new Map();
|
|
33
|
+
let lastEvictionAt = 0;
|
|
108
34
|
/**
|
|
109
35
|
* Drop windows that have expired, so the map does not grow with every user ever seen.
|
|
110
36
|
* Runs automatically (throttled to {@link WINDOW_EVICTION_INTERVAL_MS}) whenever budget is
|
|
111
37
|
* charged; `now` is injectable so the sweep can be exercised without waiting a minute.
|
|
112
38
|
* Returns the number of windows still retained.
|
|
113
39
|
*/
|
|
114
|
-
|
|
115
|
-
var e_1, _b;
|
|
116
|
-
var _c = _a === void 0 ? {} : _a, _d = _c.now, now = _d === void 0 ? luxon_1.DateTime.now().toMillis() : _d;
|
|
40
|
+
const evictExpiredSyncMutationWindows = ({ now = luxon_1.DateTime.now().toMillis(), } = {}) => {
|
|
117
41
|
lastEvictionAt = now;
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (now - window_1.windowStart >= RATE_LIMIT_WINDOW_MS) {
|
|
122
|
-
mutationWindows.delete(userId);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
127
|
-
finally {
|
|
128
|
-
try {
|
|
129
|
-
if (mutationWindows_1_1 && !mutationWindows_1_1.done && (_b = mutationWindows_1.return)) _b.call(mutationWindows_1);
|
|
42
|
+
for (const [userId, window] of mutationWindows) {
|
|
43
|
+
if (now - window.windowStart >= RATE_LIMIT_WINDOW_MS) {
|
|
44
|
+
mutationWindows.delete(userId);
|
|
130
45
|
}
|
|
131
|
-
finally { if (e_1) throw e_1.error; }
|
|
132
46
|
}
|
|
133
47
|
return mutationWindows.size;
|
|
134
48
|
};
|
|
135
49
|
exports.evictExpiredSyncMutationWindows = evictExpiredSyncMutationWindows;
|
|
136
50
|
/** Sweep expired windows at most once per {@link WINDOW_EVICTION_INTERVAL_MS}. */
|
|
137
|
-
|
|
51
|
+
const maybeEvictExpiredWindows = (now) => {
|
|
138
52
|
if (now - lastEvictionAt < WINDOW_EVICTION_INTERVAL_MS) {
|
|
139
53
|
return;
|
|
140
54
|
}
|
|
141
|
-
(0, exports.evictExpiredSyncMutationWindows)({ now
|
|
55
|
+
(0, exports.evictExpiredSyncMutationWindows)({ now });
|
|
142
56
|
};
|
|
143
57
|
/**
|
|
144
58
|
* Charge `weight` mutations to `userId`'s rolling window. Returns `undefined` when the
|
|
@@ -149,12 +63,11 @@ var maybeEvictExpiredWindows = function (now) {
|
|
|
149
63
|
* A rejected request consumes nothing: a client held over budget would otherwise keep its
|
|
150
64
|
* own window pinned full and never recover within the second.
|
|
151
65
|
*/
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
var now = luxon_1.DateTime.now().toMillis();
|
|
66
|
+
const consumeMutationBudget = ({ userId, weight, }) => {
|
|
67
|
+
const now = luxon_1.DateTime.now().toMillis();
|
|
155
68
|
maybeEvictExpiredWindows(now);
|
|
156
|
-
|
|
157
|
-
|
|
69
|
+
const existing = mutationWindows.get(userId);
|
|
70
|
+
const window = !existing || now - existing.windowStart >= RATE_LIMIT_WINDOW_MS
|
|
158
71
|
? { count: 0, windowStart: now }
|
|
159
72
|
: existing;
|
|
160
73
|
if (window.count + weight > exports.MAX_SYNC_MUTATIONS_PER_SECOND) {
|
|
@@ -167,46 +80,37 @@ var consumeMutationBudget = function (_a) {
|
|
|
167
80
|
return undefined;
|
|
168
81
|
};
|
|
169
82
|
/** Clear every per-user window. Test-only: windows are module state shared across suites. */
|
|
170
|
-
|
|
83
|
+
const resetSyncMutationRateLimits = () => {
|
|
171
84
|
mutationWindows.clear();
|
|
172
85
|
lastEvictionAt = 0;
|
|
173
86
|
};
|
|
174
87
|
exports.resetSyncMutationRateLimits = resetSyncMutationRateLimits;
|
|
175
88
|
/** Number of retained per-user windows — lets tests assert eviction actually happens. */
|
|
176
|
-
|
|
89
|
+
const syncMutationRateLimitWindowCount = () => mutationWindows.size;
|
|
177
90
|
exports.syncMutationRateLimitWindowCount = syncMutationRateLimitWindowCount;
|
|
178
91
|
/** The `rate_limited` nack both transports return, minus the mutationId the caller knows. */
|
|
179
|
-
|
|
92
|
+
const rateLimitNack = (retryAfterMs) => ({
|
|
180
93
|
code: "rate_limited",
|
|
181
|
-
message:
|
|
182
|
-
retryAfterMs
|
|
183
|
-
});
|
|
94
|
+
message: `Rate limit of ${exports.MAX_SYNC_MUTATIONS_PER_SECOND} mutations per second exceeded`,
|
|
95
|
+
retryAfterMs,
|
|
96
|
+
});
|
|
184
97
|
/** Shared orchestration for a SINGLE mutation: rate limit, then apply. */
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
_c = {};
|
|
203
|
-
return [4 /*yield*/, (0, mutationHandler_1.applySyncMutation)({ mutation: mutation, req: req, scopeResolver: scopeResolver, syncOptions: syncOptions, user: user })];
|
|
204
|
-
case 1: return [2 /*return*/, (_c.outcome = _d.sent(),
|
|
205
|
-
_c.stage = "applied",
|
|
206
|
-
_c)];
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
}); };
|
|
98
|
+
const runSyncMutation = async ({ mutation, req, scopeResolver, syncOptions, user, }) => {
|
|
99
|
+
const userId = String(user.id);
|
|
100
|
+
const mutationId = typeof mutation?.mutationId === "string" ? mutation.mutationId : "";
|
|
101
|
+
const retryAfterMs = consumeMutationBudget({ userId, weight: 1 });
|
|
102
|
+
if (retryAfterMs !== undefined) {
|
|
103
|
+
logger_1.logger.info("[sync] User hit the sync mutation rate limit", { mutationId, userId });
|
|
104
|
+
return {
|
|
105
|
+
outcome: { nack: { mutationId, ...rateLimitNack(retryAfterMs) }, type: "nack" },
|
|
106
|
+
stage: "rate_limited",
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
outcome: await (0, mutationHandler_1.applySyncMutation)({ mutation, req, scopeResolver, syncOptions, user }),
|
|
111
|
+
stage: "applied",
|
|
112
|
+
};
|
|
113
|
+
};
|
|
210
114
|
exports.runSyncMutation = runSyncMutation;
|
|
211
115
|
/**
|
|
212
116
|
* Shared orchestration for a BATCH: validate (size cap + intra-batch duplicate ids),
|
|
@@ -217,37 +121,28 @@ exports.runSyncMutation = runSyncMutation;
|
|
|
217
121
|
* mutations that would have succeeded. The budget is then charged per MUTATION (not per
|
|
218
122
|
* batch), so batching cannot buy extra throughput over single sends.
|
|
219
123
|
*/
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
_c = {};
|
|
246
|
-
return [4 /*yield*/, (0, mutationHandler_1.applySyncMutationBatch)({ mutations: mutations, req: req, scopeResolver: scopeResolver, syncOptions: syncOptions, user: user })];
|
|
247
|
-
case 1: return [2 /*return*/, (_c.response = _d.sent(),
|
|
248
|
-
_c.stage = "applied",
|
|
249
|
-
_c)];
|
|
250
|
-
}
|
|
251
|
-
});
|
|
252
|
-
}); };
|
|
124
|
+
const runSyncBatch = async ({ mutations, req, scopeResolver, syncOptions, user, }) => {
|
|
125
|
+
const validation = (0, mutationHandler_1.validateSyncMutationBatch)(mutations);
|
|
126
|
+
if (!validation.ok) {
|
|
127
|
+
return { response: validation.response, stage: "validation" };
|
|
128
|
+
}
|
|
129
|
+
const userId = String(user.id);
|
|
130
|
+
const retryAfterMs = consumeMutationBudget({ userId, weight: mutations.length });
|
|
131
|
+
if (retryAfterMs !== undefined) {
|
|
132
|
+
logger_1.logger.info("[sync] User hit the sync batch mutation rate limit", {
|
|
133
|
+
batchSize: mutations.length,
|
|
134
|
+
userId,
|
|
135
|
+
});
|
|
136
|
+
return {
|
|
137
|
+
response: {
|
|
138
|
+
results: [{ nack: { mutationId: "", ...rateLimitNack(retryAfterMs) }, type: "nack" }],
|
|
139
|
+
},
|
|
140
|
+
stage: "rate_limited",
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
response: await (0, mutationHandler_1.applySyncMutationBatch)({ mutations, req, scopeResolver, syncOptions, user }),
|
|
145
|
+
stage: "applied",
|
|
146
|
+
};
|
|
147
|
+
};
|
|
253
148
|
exports.runSyncBatch = runSyncBatch;
|