@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
|
@@ -1,91 +1,32 @@
|
|
|
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
61
3
|
exports.installSyncSocketHandlers = exports.syncRoomForStream = exports.MAX_SYNC_SUBSCRIBE_ARRAY_LENGTH = exports.MAX_SYNC_COLLECTION_SUBSCRIPTIONS = void 0;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
var trimmed = organizationId === null || organizationId === void 0 ? void 0 : organizationId.trim();
|
|
4
|
+
const logger_1 = require("../logger");
|
|
5
|
+
const orgContext_1 = require("../orgs/orgContext");
|
|
6
|
+
const permissions_1 = require("../permissions");
|
|
7
|
+
const socketUser_1 = require("../realtime/socketUser");
|
|
8
|
+
const adminBroadcastScope_1 = require("./adminBroadcastScope");
|
|
9
|
+
const adminWindowAccess_1 = require("./adminWindowAccess");
|
|
10
|
+
const registry_1 = require("./registry");
|
|
11
|
+
const streams_1 = require("./streams");
|
|
12
|
+
const syncBatch_1 = require("./syncBatch");
|
|
13
|
+
const requestFromOrganizationId = ({ organizationId, user, }) => {
|
|
14
|
+
const trimmed = organizationId?.trim();
|
|
74
15
|
if (!trimmed) {
|
|
75
|
-
return { user
|
|
16
|
+
return { user };
|
|
76
17
|
}
|
|
77
18
|
return {
|
|
78
|
-
header:
|
|
19
|
+
header: (name) => {
|
|
79
20
|
return String(name).toLowerCase() === orgContext_1.ORGANIZATION_ID_HEADER ? trimmed : undefined;
|
|
80
21
|
},
|
|
81
|
-
user
|
|
22
|
+
user,
|
|
82
23
|
};
|
|
83
24
|
};
|
|
84
|
-
|
|
25
|
+
const organizationIdFromPayload = (payload) => {
|
|
85
26
|
if (typeof payload !== "object" || payload === null) {
|
|
86
27
|
return undefined;
|
|
87
28
|
}
|
|
88
|
-
|
|
29
|
+
const organizationId = payload.organizationId;
|
|
89
30
|
return typeof organizationId === "string" ? organizationId : undefined;
|
|
90
31
|
};
|
|
91
32
|
/**
|
|
@@ -123,44 +64,34 @@ exports.MAX_SYNC_COLLECTION_SUBSCRIPTIONS = 50;
|
|
|
123
64
|
*/
|
|
124
65
|
exports.MAX_SYNC_SUBSCRIBE_ARRAY_LENGTH = 100;
|
|
125
66
|
/** The Socket.io room a sync stream fans out through. */
|
|
126
|
-
|
|
67
|
+
const syncRoomForStream = (stream) => `sync:${stream}`;
|
|
127
68
|
exports.syncRoomForStream = syncRoomForStream;
|
|
128
69
|
/**
|
|
129
70
|
* Resolve the streams a user may subscribe to for a collection. Returns null (after
|
|
130
71
|
* emitting `sync:error`) when the scope cannot be resolved for this user/server config.
|
|
131
72
|
*/
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
user: user,
|
|
149
|
-
})];
|
|
150
|
-
case 2: return [2 /*return*/, _c.sent()];
|
|
151
|
-
case 3:
|
|
152
|
-
error_1 = _c.sent();
|
|
153
|
-
if (error_1 instanceof streams_1.MissingScopeResolverError) {
|
|
154
|
-
emitError(error_1.message);
|
|
155
|
-
return [2 /*return*/, null];
|
|
156
|
-
}
|
|
157
|
-
logger_1.logger.error("[sync] getUserScopes threw for ".concat(collection, ": ").concat(error_1));
|
|
158
|
-
emitError("Failed to resolve scopes for ".concat(collection));
|
|
159
|
-
return [2 /*return*/, null];
|
|
160
|
-
case 4: return [2 /*return*/];
|
|
73
|
+
const resolveUserStreams = async ({ entry, user, options, socket, }) => {
|
|
74
|
+
const collection = entry.collectionTag;
|
|
75
|
+
const emitError = (message) => {
|
|
76
|
+
socket.emit("sync:error", { collection, message });
|
|
77
|
+
};
|
|
78
|
+
try {
|
|
79
|
+
return await (0, streams_1.resolveUserStreamsForEntry)({
|
|
80
|
+
entry,
|
|
81
|
+
getUserScopes: options.getUserScopes,
|
|
82
|
+
user,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (error instanceof streams_1.MissingScopeResolverError) {
|
|
87
|
+
emitError(error.message);
|
|
88
|
+
return null;
|
|
161
89
|
}
|
|
162
|
-
|
|
163
|
-
});
|
|
90
|
+
logger_1.logger.error(`[sync] getUserScopes threw for ${collection}: ${error}`);
|
|
91
|
+
emitError(`Failed to resolve scopes for ${collection}`);
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
164
95
|
/**
|
|
165
96
|
* Install the sync socket handlers on a single socket. Wired into RealtimeApp's
|
|
166
97
|
* connection handler alongside `installRealtimeSocketHandlers`.
|
|
@@ -168,12 +99,9 @@ var resolveUserStreams = function (_a) { return __awaiter(void 0, [_a], void 0,
|
|
|
168
99
|
* The `_io` server parameter is accepted for signature symmetry with future fan-out
|
|
169
100
|
* needs; the current handlers only act on the connecting socket.
|
|
170
101
|
*/
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (handlerOptions === void 0) { handlerOptions = {}; }
|
|
175
|
-
var logInfo = (_a = handlerOptions.logInfo) !== null && _a !== void 0 ? _a : (function () { });
|
|
176
|
-
var userId = (_b = socket.decodedToken) === null || _b === void 0 ? void 0 : _b.id;
|
|
102
|
+
const installSyncSocketHandlers = (_io, socket, options = {}, handlerOptions = {}) => {
|
|
103
|
+
const logInfo = handlerOptions.logInfo ?? (() => { });
|
|
104
|
+
const userId = socket.decodedToken?.id;
|
|
177
105
|
// D2: re-derive the authorizing user on every event rather than capturing it once
|
|
178
106
|
// at install time — `socket.data.fullUser` is loaded at handshake and refreshed by
|
|
179
107
|
// D1's sweep, so a mutation sent after a refresh (e.g. after `organizationIds`
|
|
@@ -182,382 +110,229 @@ var installSyncSocketHandlers = function (_io, socket, options, handlerOptions)
|
|
|
182
110
|
// Task 9.21: awaits the handshake load when it is still in flight, closing the window
|
|
183
111
|
// where an event arriving first was authorized against the synthetic JWT-claim user
|
|
184
112
|
// (no membership fields — tenant scopes resolved to nothing).
|
|
185
|
-
|
|
113
|
+
const currentUser = () => (0, socketUser_1.awaitSocketFullUser)(socket);
|
|
186
114
|
// collection tag -> joined sync rooms, for unsubscribe/disconnect cleanup and D4's
|
|
187
115
|
// sweep-time re-resolution. Exposed on socket.data (when present — real sockets
|
|
188
116
|
// always have it; minimal test doubles may not) so the sweep, which runs outside
|
|
189
117
|
// this closure, can read and mutate it directly.
|
|
190
|
-
|
|
118
|
+
const subscriptions = socket.data?.syncSubscriptions ?? new Map();
|
|
191
119
|
if (socket.data) {
|
|
192
120
|
socket.data.syncSubscriptions = subscriptions;
|
|
193
121
|
}
|
|
194
|
-
socket.on("sync:subscribe",
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
return [2 /*return*/];
|
|
213
|
-
}
|
|
214
|
-
_f.label = 1;
|
|
215
|
-
case 1:
|
|
216
|
-
_f.trys.push([1, 23, 24, 25]);
|
|
217
|
-
collections_1 = __values(collections), collections_1_1 = collections_1.next();
|
|
218
|
-
_f.label = 2;
|
|
219
|
-
case 2:
|
|
220
|
-
if (!!collections_1_1.done) return [3 /*break*/, 22];
|
|
221
|
-
collection = collections_1_1.value;
|
|
222
|
-
if (typeof collection !== "string" || collection.length === 0) {
|
|
223
|
-
return [3 /*break*/, 21];
|
|
224
|
-
}
|
|
225
|
-
if (subscriptions.has(collection)) {
|
|
226
|
-
// Already subscribed — idempotent.
|
|
227
|
-
return [3 /*break*/, 21];
|
|
228
|
-
}
|
|
229
|
-
if (subscriptions.size >= exports.MAX_SYNC_COLLECTION_SUBSCRIPTIONS) {
|
|
230
|
-
logInfo("[sync] User ".concat(userId, " hit sync collection subscription limit"));
|
|
231
|
-
socket.emit("sync:error", {
|
|
232
|
-
collection: collection,
|
|
233
|
-
message: "Sync subscription limit of ".concat(exports.MAX_SYNC_COLLECTION_SUBSCRIPTIONS, " collections reached"),
|
|
234
|
-
});
|
|
235
|
-
return [3 /*break*/, 21];
|
|
236
|
-
}
|
|
237
|
-
entry = (0, registry_1.findSyncEntryByCollectionTag)(collection);
|
|
238
|
-
if (!entry) {
|
|
239
|
-
socket.emit("sync:error", {
|
|
240
|
-
collection: collection,
|
|
241
|
-
message: "Unknown sync collection: ".concat(collection),
|
|
242
|
-
});
|
|
243
|
-
return [3 /*break*/, 21];
|
|
244
|
-
}
|
|
245
|
-
return [4 /*yield*/, currentUser()];
|
|
246
|
-
case 3:
|
|
247
|
-
user = _f.sent();
|
|
248
|
-
if (!user) {
|
|
249
|
-
socket.emit("sync:error", { collection: collection, message: "Authentication required" });
|
|
250
|
-
return [3 /*break*/, 21];
|
|
251
|
-
}
|
|
252
|
-
return [4 /*yield*/, (0, permissions_1.checkPermissions)("list", entry.options.permissions.list, user)];
|
|
253
|
-
case 4:
|
|
254
|
-
if (!(_f.sent())) {
|
|
255
|
-
logInfo("[sync] User ".concat(userId, " denied sync subscription for ").concat(collection));
|
|
256
|
-
socket.emit("sync:error", {
|
|
257
|
-
collection: collection,
|
|
258
|
-
message: "Access to sync collection ".concat(collection, " denied"),
|
|
259
|
-
});
|
|
260
|
-
return [3 /*break*/, 21];
|
|
261
|
-
}
|
|
262
|
-
if (isWindow && entry.config.adminBroadcast !== true) {
|
|
263
|
-
socket.emit("sync:error", {
|
|
264
|
-
collection: collection,
|
|
265
|
-
message: "Window subscribe requires adminBroadcast on ".concat(collection),
|
|
266
|
-
});
|
|
267
|
-
return [3 /*break*/, 21];
|
|
268
|
-
}
|
|
269
|
-
_a = isWindow;
|
|
270
|
-
if (!_a) return [3 /*break*/, 6];
|
|
271
|
-
return [4 /*yield*/, (0, adminWindowAccess_1.canUseAdminBroadcastWindow)({
|
|
272
|
-
accessControl: options.accessControl,
|
|
273
|
-
canOpenAdminWindow: options.canOpenAdminWindow,
|
|
274
|
-
user: user,
|
|
275
|
-
})];
|
|
276
|
-
case 5:
|
|
277
|
-
_a = !(_f.sent());
|
|
278
|
-
_f.label = 6;
|
|
279
|
-
case 6:
|
|
280
|
-
if (_a) {
|
|
281
|
-
logInfo("[sync] User ".concat(userId, " denied window subscribe for ").concat(collection));
|
|
282
|
-
socket.emit("sync:error", {
|
|
283
|
-
collection: collection,
|
|
284
|
-
message: "Window subscribe requires admin panel access for ".concat(collection),
|
|
285
|
-
});
|
|
286
|
-
return [3 /*break*/, 21];
|
|
287
|
-
}
|
|
288
|
-
if (!isWindow) return [3 /*break*/, 9];
|
|
289
|
-
adminScope = (0, adminBroadcastScope_1.getAdminBroadcastScope)(entry.modelName);
|
|
290
|
-
_b = adminScope;
|
|
291
|
-
if (!_b) return [3 /*break*/, 8];
|
|
292
|
-
return [4 /*yield*/, (0, adminBroadcastScope_1.canListAdminBroadcastScope)({ scope: adminScope, user: user })];
|
|
293
|
-
case 7:
|
|
294
|
-
_b = !(_f.sent());
|
|
295
|
-
_f.label = 8;
|
|
296
|
-
case 8:
|
|
297
|
-
if (_b) {
|
|
298
|
-
logInfo("[sync] User ".concat(userId, " denied window subscribe list for ").concat(collection));
|
|
299
|
-
socket.emit("sync:error", {
|
|
300
|
-
collection: collection,
|
|
301
|
-
message: "Window subscribe requires admin collection access for ".concat(collection),
|
|
302
|
-
});
|
|
303
|
-
return [3 /*break*/, 21];
|
|
304
|
-
}
|
|
305
|
-
_f.label = 9;
|
|
306
|
-
case 9:
|
|
307
|
-
if (!isWindow) return [3 /*break*/, 10];
|
|
308
|
-
_c = [(0, streams_1.adminBroadcastStream)(collection)];
|
|
309
|
-
return [3 /*break*/, 12];
|
|
310
|
-
case 10: return [4 /*yield*/, resolveUserStreams({ entry: entry, options: options, socket: socket, user: user })];
|
|
311
|
-
case 11:
|
|
312
|
-
_c = _f.sent();
|
|
313
|
-
_f.label = 12;
|
|
314
|
-
case 12:
|
|
315
|
-
streams = _c;
|
|
316
|
-
if (!streams) {
|
|
317
|
-
return [3 /*break*/, 21];
|
|
318
|
-
}
|
|
319
|
-
rooms = new Set(streams.map(exports.syncRoomForStream));
|
|
320
|
-
_f.label = 13;
|
|
321
|
-
case 13:
|
|
322
|
-
_f.trys.push([13, 18, 19, 20]);
|
|
323
|
-
rooms_1 = (e_1 = void 0, __values(rooms)), rooms_1_1 = rooms_1.next();
|
|
324
|
-
_f.label = 14;
|
|
325
|
-
case 14:
|
|
326
|
-
if (!!rooms_1_1.done) return [3 /*break*/, 17];
|
|
327
|
-
room = rooms_1_1.value;
|
|
328
|
-
return [4 /*yield*/, socket.join(room)];
|
|
329
|
-
case 15:
|
|
330
|
-
_f.sent();
|
|
331
|
-
_f.label = 16;
|
|
332
|
-
case 16:
|
|
333
|
-
rooms_1_1 = rooms_1.next();
|
|
334
|
-
return [3 /*break*/, 14];
|
|
335
|
-
case 17: return [3 /*break*/, 20];
|
|
336
|
-
case 18:
|
|
337
|
-
e_1_1 = _f.sent();
|
|
338
|
-
e_1 = { error: e_1_1 };
|
|
339
|
-
return [3 /*break*/, 20];
|
|
340
|
-
case 19:
|
|
341
|
-
try {
|
|
342
|
-
if (rooms_1_1 && !rooms_1_1.done && (_e = rooms_1.return)) _e.call(rooms_1);
|
|
343
|
-
}
|
|
344
|
-
finally { if (e_1) throw e_1.error; }
|
|
345
|
-
return [7 /*endfinally*/];
|
|
346
|
-
case 20:
|
|
347
|
-
subscriptions.set(collection, rooms);
|
|
348
|
-
socket.emit("sync:subscribed", isWindow ? { collection: collection, mode: "window", streams: streams } : { collection: collection, streams: streams });
|
|
349
|
-
logInfo("[sync] User ".concat(userId, " subscribed to ").concat(collection, ": ").concat(streams.join(", ")));
|
|
350
|
-
_f.label = 21;
|
|
351
|
-
case 21:
|
|
352
|
-
collections_1_1 = collections_1.next();
|
|
353
|
-
return [3 /*break*/, 2];
|
|
354
|
-
case 22: return [3 /*break*/, 25];
|
|
355
|
-
case 23:
|
|
356
|
-
e_2_1 = _f.sent();
|
|
357
|
-
e_2 = { error: e_2_1 };
|
|
358
|
-
return [3 /*break*/, 25];
|
|
359
|
-
case 24:
|
|
360
|
-
try {
|
|
361
|
-
if (collections_1_1 && !collections_1_1.done && (_d = collections_1.return)) _d.call(collections_1);
|
|
362
|
-
}
|
|
363
|
-
finally { if (e_2) throw e_2.error; }
|
|
364
|
-
return [7 /*endfinally*/];
|
|
365
|
-
case 25: return [2 /*return*/];
|
|
122
|
+
socket.on("sync:subscribe", async (payload) => {
|
|
123
|
+
const collections = Array.isArray(payload?.collections) ? payload.collections : null;
|
|
124
|
+
const isWindow = payload?.mode === "window";
|
|
125
|
+
if (!collections) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
// C8: reject an oversized array up front (length check before iterating).
|
|
129
|
+
if (collections.length > exports.MAX_SYNC_SUBSCRIBE_ARRAY_LENGTH) {
|
|
130
|
+
logInfo(`[sync] User ${userId} sent an oversized sync:subscribe array`);
|
|
131
|
+
socket.emit("sync:error", {
|
|
132
|
+
collection: "",
|
|
133
|
+
message: `sync:subscribe accepts at most ${exports.MAX_SYNC_SUBSCRIBE_ARRAY_LENGTH} collections`,
|
|
134
|
+
});
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
for (const collection of collections) {
|
|
138
|
+
if (typeof collection !== "string" || collection.length === 0) {
|
|
139
|
+
continue;
|
|
366
140
|
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
var collections, collections_2, collections_2_1, collection, rooms, rooms_2, rooms_2_1, room, e_3_1, e_4_1;
|
|
371
|
-
var e_4, _a, e_3, _b;
|
|
372
|
-
return __generator(this, function (_c) {
|
|
373
|
-
switch (_c.label) {
|
|
374
|
-
case 0:
|
|
375
|
-
collections = Array.isArray(payload === null || payload === void 0 ? void 0 : payload.collections) ? payload.collections : null;
|
|
376
|
-
if (!collections) {
|
|
377
|
-
return [2 /*return*/];
|
|
378
|
-
}
|
|
379
|
-
_c.label = 1;
|
|
380
|
-
case 1:
|
|
381
|
-
_c.trys.push([1, 13, 14, 15]);
|
|
382
|
-
collections_2 = __values(collections), collections_2_1 = collections_2.next();
|
|
383
|
-
_c.label = 2;
|
|
384
|
-
case 2:
|
|
385
|
-
if (!!collections_2_1.done) return [3 /*break*/, 12];
|
|
386
|
-
collection = collections_2_1.value;
|
|
387
|
-
if (typeof collection !== "string") {
|
|
388
|
-
return [3 /*break*/, 11];
|
|
389
|
-
}
|
|
390
|
-
rooms = subscriptions.get(collection);
|
|
391
|
-
if (!rooms) {
|
|
392
|
-
return [3 /*break*/, 11];
|
|
393
|
-
}
|
|
394
|
-
_c.label = 3;
|
|
395
|
-
case 3:
|
|
396
|
-
_c.trys.push([3, 8, 9, 10]);
|
|
397
|
-
rooms_2 = (e_3 = void 0, __values(rooms)), rooms_2_1 = rooms_2.next();
|
|
398
|
-
_c.label = 4;
|
|
399
|
-
case 4:
|
|
400
|
-
if (!!rooms_2_1.done) return [3 /*break*/, 7];
|
|
401
|
-
room = rooms_2_1.value;
|
|
402
|
-
return [4 /*yield*/, socket.leave(room)];
|
|
403
|
-
case 5:
|
|
404
|
-
_c.sent();
|
|
405
|
-
_c.label = 6;
|
|
406
|
-
case 6:
|
|
407
|
-
rooms_2_1 = rooms_2.next();
|
|
408
|
-
return [3 /*break*/, 4];
|
|
409
|
-
case 7: return [3 /*break*/, 10];
|
|
410
|
-
case 8:
|
|
411
|
-
e_3_1 = _c.sent();
|
|
412
|
-
e_3 = { error: e_3_1 };
|
|
413
|
-
return [3 /*break*/, 10];
|
|
414
|
-
case 9:
|
|
415
|
-
try {
|
|
416
|
-
if (rooms_2_1 && !rooms_2_1.done && (_b = rooms_2.return)) _b.call(rooms_2);
|
|
417
|
-
}
|
|
418
|
-
finally { if (e_3) throw e_3.error; }
|
|
419
|
-
return [7 /*endfinally*/];
|
|
420
|
-
case 10:
|
|
421
|
-
subscriptions.delete(collection);
|
|
422
|
-
logInfo("[sync] User ".concat(userId, " unsubscribed from ").concat(collection));
|
|
423
|
-
_c.label = 11;
|
|
424
|
-
case 11:
|
|
425
|
-
collections_2_1 = collections_2.next();
|
|
426
|
-
return [3 /*break*/, 2];
|
|
427
|
-
case 12: return [3 /*break*/, 15];
|
|
428
|
-
case 13:
|
|
429
|
-
e_4_1 = _c.sent();
|
|
430
|
-
e_4 = { error: e_4_1 };
|
|
431
|
-
return [3 /*break*/, 15];
|
|
432
|
-
case 14:
|
|
433
|
-
try {
|
|
434
|
-
if (collections_2_1 && !collections_2_1.done && (_a = collections_2.return)) _a.call(collections_2);
|
|
435
|
-
}
|
|
436
|
-
finally { if (e_4) throw e_4.error; }
|
|
437
|
-
return [7 /*endfinally*/];
|
|
438
|
-
case 15: return [2 /*return*/];
|
|
141
|
+
if (subscriptions.has(collection)) {
|
|
142
|
+
// Already subscribed — idempotent.
|
|
143
|
+
continue;
|
|
439
144
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
respond = function (outcome) {
|
|
448
|
-
if (outcome.type === "ack") {
|
|
449
|
-
socket.emit("sync:ack", outcome.ack);
|
|
450
|
-
}
|
|
451
|
-
else {
|
|
452
|
-
socket.emit("sync:nack", outcome.nack);
|
|
453
|
-
}
|
|
454
|
-
if (typeof ack === "function") {
|
|
455
|
-
ack(outcome.type === "ack" ? { ack: outcome.ack } : { nack: outcome.nack });
|
|
456
|
-
}
|
|
457
|
-
};
|
|
458
|
-
nack = function (partial) {
|
|
459
|
-
respond({
|
|
460
|
-
nack: __assign({ mutationId: typeof (payload === null || payload === void 0 ? void 0 : payload.mutationId) === "string" ? payload.mutationId : "" }, partial),
|
|
461
|
-
type: "nack",
|
|
462
|
-
});
|
|
463
|
-
};
|
|
464
|
-
return [4 /*yield*/, currentUser()];
|
|
465
|
-
case 1:
|
|
466
|
-
user = _b.sent();
|
|
467
|
-
if (!user) {
|
|
468
|
-
nack({ code: "unauthorized", message: "Authentication required" });
|
|
469
|
-
return [2 /*return*/];
|
|
470
|
-
}
|
|
471
|
-
_b.label = 2;
|
|
472
|
-
case 2:
|
|
473
|
-
_b.trys.push([2, 4, , 5]);
|
|
474
|
-
return [4 /*yield*/, (0, syncBatch_1.runSyncMutation)({
|
|
475
|
-
mutation: payload,
|
|
476
|
-
req: requestFromOrganizationId({
|
|
477
|
-
organizationId: organizationIdFromPayload(payload),
|
|
478
|
-
user: user,
|
|
479
|
-
}),
|
|
480
|
-
scopeResolver: options.getUserScopes,
|
|
481
|
-
syncOptions: options,
|
|
482
|
-
user: user,
|
|
483
|
-
})];
|
|
484
|
-
case 3:
|
|
485
|
-
_a = _b.sent(), outcome = _a.outcome, stage = _a.stage;
|
|
486
|
-
if (stage === "rate_limited") {
|
|
487
|
-
logInfo("[sync] User ".concat(userId, " hit the sync:mutate rate limit"));
|
|
488
|
-
}
|
|
489
|
-
respond(outcome);
|
|
490
|
-
return [3 /*break*/, 5];
|
|
491
|
-
case 4:
|
|
492
|
-
error_2 = _b.sent();
|
|
493
|
-
logger_1.logger.error("[sync] sync:mutate failed for socket ".concat(socket.id, ": ").concat(error_2));
|
|
494
|
-
nack({ code: "error", message: "Internal error applying mutation" });
|
|
495
|
-
return [3 /*break*/, 5];
|
|
496
|
-
case 5: return [2 /*return*/];
|
|
145
|
+
if (subscriptions.size >= exports.MAX_SYNC_COLLECTION_SUBSCRIPTIONS) {
|
|
146
|
+
logInfo(`[sync] User ${userId} hit sync collection subscription limit`);
|
|
147
|
+
socket.emit("sync:error", {
|
|
148
|
+
collection,
|
|
149
|
+
message: `Sync subscription limit of ${exports.MAX_SYNC_COLLECTION_SUBSCRIPTIONS} collections reached`,
|
|
150
|
+
});
|
|
151
|
+
continue;
|
|
497
152
|
}
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
// FIX 5: emit the receipt IMMEDIATELY, before any processing (including
|
|
506
|
-
// rate-limit/auth checks) — this is what lets the client distinguish
|
|
507
|
-
// "no sync:mutateBatch handler at all" (silence past the grace period)
|
|
508
|
-
// from "the server is just slow finishing this batch" (a receipt
|
|
509
|
-
// arrived; the client then waits the full batch timeout instead of
|
|
510
|
-
// falling back to single sends and possibly double-processing).
|
|
511
|
-
if (typeof (payload === null || payload === void 0 ? void 0 : payload.batchId) === "string" && payload.batchId.length > 0) {
|
|
512
|
-
socket.emit("sync:batchReceived", { batchId: payload.batchId });
|
|
513
|
-
}
|
|
514
|
-
respondBatch = function (response) {
|
|
515
|
-
if (typeof ack === "function") {
|
|
516
|
-
ack(response);
|
|
517
|
-
}
|
|
518
|
-
};
|
|
519
|
-
singleNackBatch = function (partial, mutationId) {
|
|
520
|
-
if (mutationId === void 0) { mutationId = ""; }
|
|
521
|
-
respondBatch({ results: [{ nack: __assign({ mutationId: mutationId }, partial), type: "nack" }] });
|
|
522
|
-
};
|
|
523
|
-
mutations = Array.isArray(payload === null || payload === void 0 ? void 0 : payload.mutations) ? payload.mutations : [];
|
|
524
|
-
return [4 /*yield*/, currentUser()];
|
|
525
|
-
case 1:
|
|
526
|
-
user = _b.sent();
|
|
527
|
-
if (!user) {
|
|
528
|
-
singleNackBatch({ code: "unauthorized", message: "Authentication required" });
|
|
529
|
-
return [2 /*return*/];
|
|
530
|
-
}
|
|
531
|
-
_b.label = 2;
|
|
532
|
-
case 2:
|
|
533
|
-
_b.trys.push([2, 4, , 5]);
|
|
534
|
-
return [4 /*yield*/, (0, syncBatch_1.runSyncBatch)({
|
|
535
|
-
mutations: mutations,
|
|
536
|
-
req: requestFromOrganizationId({
|
|
537
|
-
organizationId: organizationIdFromPayload(payload),
|
|
538
|
-
user: user,
|
|
539
|
-
}),
|
|
540
|
-
scopeResolver: options.getUserScopes,
|
|
541
|
-
syncOptions: options,
|
|
542
|
-
user: user,
|
|
543
|
-
})];
|
|
544
|
-
case 3:
|
|
545
|
-
_a = _b.sent(), response = _a.response, stage = _a.stage;
|
|
546
|
-
if (stage === "rate_limited") {
|
|
547
|
-
logInfo("[sync] User ".concat(userId, " hit the sync:mutateBatch rate limit"));
|
|
548
|
-
}
|
|
549
|
-
respondBatch(response);
|
|
550
|
-
return [3 /*break*/, 5];
|
|
551
|
-
case 4:
|
|
552
|
-
error_3 = _b.sent();
|
|
553
|
-
logger_1.logger.error("[sync] sync:mutateBatch failed for socket ".concat(socket.id, ": ").concat(error_3));
|
|
554
|
-
singleNackBatch({ code: "error", message: "Internal error applying batch" });
|
|
555
|
-
return [3 /*break*/, 5];
|
|
556
|
-
case 5: return [2 /*return*/];
|
|
153
|
+
const entry = (0, registry_1.findSyncEntryByCollectionTag)(collection);
|
|
154
|
+
if (!entry) {
|
|
155
|
+
socket.emit("sync:error", {
|
|
156
|
+
collection,
|
|
157
|
+
message: `Unknown sync collection: ${collection}`,
|
|
158
|
+
});
|
|
159
|
+
continue;
|
|
557
160
|
}
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
161
|
+
const user = await currentUser();
|
|
162
|
+
if (!user) {
|
|
163
|
+
socket.emit("sync:error", { collection, message: "Authentication required" });
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (!(await (0, permissions_1.checkPermissions)("list", entry.options.permissions.list, user))) {
|
|
167
|
+
logInfo(`[sync] User ${userId} denied sync subscription for ${collection}`);
|
|
168
|
+
socket.emit("sync:error", {
|
|
169
|
+
collection,
|
|
170
|
+
message: `Access to sync collection ${collection} denied`,
|
|
171
|
+
});
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (isWindow && entry.config.adminBroadcast !== true) {
|
|
175
|
+
socket.emit("sync:error", {
|
|
176
|
+
collection,
|
|
177
|
+
message: `Window subscribe requires adminBroadcast on ${collection}`,
|
|
178
|
+
});
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (isWindow &&
|
|
182
|
+
!(await (0, adminWindowAccess_1.canUseAdminBroadcastWindow)({
|
|
183
|
+
accessControl: options.accessControl,
|
|
184
|
+
canOpenAdminWindow: options.canOpenAdminWindow,
|
|
185
|
+
user,
|
|
186
|
+
}))) {
|
|
187
|
+
logInfo(`[sync] User ${userId} denied window subscribe for ${collection}`);
|
|
188
|
+
socket.emit("sync:error", {
|
|
189
|
+
collection,
|
|
190
|
+
message: `Window subscribe requires admin panel access for ${collection}`,
|
|
191
|
+
});
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if (isWindow) {
|
|
195
|
+
const adminScope = (0, adminBroadcastScope_1.getAdminBroadcastScope)(entry.modelName);
|
|
196
|
+
if (adminScope && !(await (0, adminBroadcastScope_1.canListAdminBroadcastScope)({ scope: adminScope, user }))) {
|
|
197
|
+
logInfo(`[sync] User ${userId} denied window subscribe list for ${collection}`);
|
|
198
|
+
socket.emit("sync:error", {
|
|
199
|
+
collection,
|
|
200
|
+
message: `Window subscribe requires admin collection access for ${collection}`,
|
|
201
|
+
});
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const streams = isWindow
|
|
206
|
+
? [(0, streams_1.adminBroadcastStream)(collection)]
|
|
207
|
+
: await resolveUserStreams({ entry, options, socket, user });
|
|
208
|
+
if (!streams) {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const rooms = new Set(streams.map(exports.syncRoomForStream));
|
|
212
|
+
for (const room of rooms) {
|
|
213
|
+
await socket.join(room);
|
|
214
|
+
}
|
|
215
|
+
subscriptions.set(collection, rooms);
|
|
216
|
+
socket.emit("sync:subscribed", isWindow ? { collection, mode: "window", streams } : { collection, streams });
|
|
217
|
+
logInfo(`[sync] User ${userId} subscribed to ${collection}: ${streams.join(", ")}`);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
socket.on("sync:unsubscribe", async (payload) => {
|
|
221
|
+
const collections = Array.isArray(payload?.collections) ? payload.collections : null;
|
|
222
|
+
if (!collections) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
for (const collection of collections) {
|
|
226
|
+
if (typeof collection !== "string") {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const rooms = subscriptions.get(collection);
|
|
230
|
+
if (!rooms) {
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
for (const room of rooms) {
|
|
234
|
+
await socket.leave(room);
|
|
235
|
+
}
|
|
236
|
+
subscriptions.delete(collection);
|
|
237
|
+
logInfo(`[sync] User ${userId} unsubscribed from ${collection}`);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
socket.on("sync:mutate", async (payload, ack) => {
|
|
241
|
+
const respond = (outcome) => {
|
|
242
|
+
if (outcome.type === "ack") {
|
|
243
|
+
socket.emit("sync:ack", outcome.ack);
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
socket.emit("sync:nack", outcome.nack);
|
|
247
|
+
}
|
|
248
|
+
if (typeof ack === "function") {
|
|
249
|
+
ack(outcome.type === "ack" ? { ack: outcome.ack } : { nack: outcome.nack });
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
const nack = (partial) => {
|
|
253
|
+
respond({
|
|
254
|
+
nack: {
|
|
255
|
+
mutationId: typeof payload?.mutationId === "string" ? payload.mutationId : "",
|
|
256
|
+
...partial,
|
|
257
|
+
},
|
|
258
|
+
type: "nack",
|
|
259
|
+
});
|
|
260
|
+
};
|
|
261
|
+
const user = await currentUser();
|
|
262
|
+
if (!user) {
|
|
263
|
+
nack({ code: "unauthorized", message: "Authentication required" });
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
try {
|
|
267
|
+
// Task 9.20: the rate limit is charged inside the shared runner against this
|
|
268
|
+
// USER's window, so opening more sockets no longer multiplies the budget.
|
|
269
|
+
const { outcome, stage } = await (0, syncBatch_1.runSyncMutation)({
|
|
270
|
+
mutation: payload,
|
|
271
|
+
req: requestFromOrganizationId({
|
|
272
|
+
organizationId: organizationIdFromPayload(payload),
|
|
273
|
+
user,
|
|
274
|
+
}),
|
|
275
|
+
scopeResolver: options.getUserScopes,
|
|
276
|
+
syncOptions: options,
|
|
277
|
+
user,
|
|
278
|
+
});
|
|
279
|
+
if (stage === "rate_limited") {
|
|
280
|
+
logInfo(`[sync] User ${userId} hit the sync:mutate rate limit`);
|
|
281
|
+
}
|
|
282
|
+
respond(outcome);
|
|
283
|
+
}
|
|
284
|
+
catch (error) {
|
|
285
|
+
logger_1.logger.error(`[sync] sync:mutate failed for socket ${socket.id}: ${error}`);
|
|
286
|
+
nack({ code: "error", message: "Internal error applying mutation" });
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
socket.on("sync:mutateBatch", async (payload, ack) => {
|
|
290
|
+
// FIX 5: emit the receipt IMMEDIATELY, before any processing (including
|
|
291
|
+
// rate-limit/auth checks) — this is what lets the client distinguish
|
|
292
|
+
// "no sync:mutateBatch handler at all" (silence past the grace period)
|
|
293
|
+
// from "the server is just slow finishing this batch" (a receipt
|
|
294
|
+
// arrived; the client then waits the full batch timeout instead of
|
|
295
|
+
// falling back to single sends and possibly double-processing).
|
|
296
|
+
if (typeof payload?.batchId === "string" && payload.batchId.length > 0) {
|
|
297
|
+
socket.emit("sync:batchReceived", { batchId: payload.batchId });
|
|
298
|
+
}
|
|
299
|
+
const respondBatch = (response) => {
|
|
300
|
+
if (typeof ack === "function") {
|
|
301
|
+
ack(response);
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
const singleNackBatch = (partial, mutationId = "") => {
|
|
305
|
+
respondBatch({ results: [{ nack: { mutationId, ...partial }, type: "nack" }] });
|
|
306
|
+
};
|
|
307
|
+
const mutations = Array.isArray(payload?.mutations) ? payload.mutations : [];
|
|
308
|
+
const user = await currentUser();
|
|
309
|
+
if (!user) {
|
|
310
|
+
singleNackBatch({ code: "unauthorized", message: "Authentication required" });
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
try {
|
|
314
|
+
// Task 9.20: validate -> rate limit -> apply, shared with POST /sync/mutate/batch.
|
|
315
|
+
const { response, stage } = await (0, syncBatch_1.runSyncBatch)({
|
|
316
|
+
mutations,
|
|
317
|
+
req: requestFromOrganizationId({
|
|
318
|
+
organizationId: organizationIdFromPayload(payload),
|
|
319
|
+
user,
|
|
320
|
+
}),
|
|
321
|
+
scopeResolver: options.getUserScopes,
|
|
322
|
+
syncOptions: options,
|
|
323
|
+
user,
|
|
324
|
+
});
|
|
325
|
+
if (stage === "rate_limited") {
|
|
326
|
+
logInfo(`[sync] User ${userId} hit the sync:mutateBatch rate limit`);
|
|
327
|
+
}
|
|
328
|
+
respondBatch(response);
|
|
329
|
+
}
|
|
330
|
+
catch (error) {
|
|
331
|
+
logger_1.logger.error(`[sync] sync:mutateBatch failed for socket ${socket.id}: ${error}`);
|
|
332
|
+
singleNackBatch({ code: "error", message: "Internal error applying batch" });
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
socket.on("disconnect", () => {
|
|
561
336
|
subscriptions.clear();
|
|
562
337
|
});
|
|
563
338
|
};
|