@terreno/api 0.30.0 → 56.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.d.ts +10 -0
- package/dist/api.js +110 -361
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +100 -1
- package/dist/betterAuth.d.ts +5 -0
- package/dist/betterAuthSetup.js +23 -4
- package/dist/docLoader.js +12 -9
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +29 -1
- package/dist/index.d.ts +17 -1
- package/dist/index.js +29 -1
- package/dist/middleware.d.ts +7 -0
- package/dist/middleware.js +16 -1
- package/dist/openApiCompat.d.ts +10 -1
- package/dist/openApiCompat.js +115 -47
- package/dist/permissions.d.ts +27 -0
- package/dist/permissions.js +42 -1
- package/dist/plugins.d.ts +13 -0
- package/dist/plugins.js +35 -1
- package/dist/realtime/changeStreamWatcher.d.ts +100 -3
- package/dist/realtime/changeStreamWatcher.js +542 -137
- package/dist/realtime/realtimeApp.d.ts +2 -0
- package/dist/realtime/realtimeApp.js +73 -21
- package/dist/realtime/sessionRevalidation.d.ts +90 -0
- package/dist/realtime/sessionRevalidation.js +438 -0
- package/dist/realtime/socketAuth.d.ts +97 -0
- package/dist/realtime/socketAuth.js +272 -0
- package/dist/realtime/socketUser.d.ts +54 -0
- package/dist/realtime/socketUser.js +75 -5
- package/dist/realtime/types.d.ts +40 -0
- package/dist/secretProviders.d.ts +23 -0
- package/dist/secretProviders.js +9 -3
- package/dist/sync/executors.d.ts +198 -0
- package/dist/sync/executors.js +670 -0
- package/dist/sync/loadHarness.js +807 -0
- package/dist/sync/models.d.ts +216 -0
- package/dist/sync/models.js +644 -0
- package/dist/sync/mutationHandler.d.ts +97 -0
- package/dist/sync/mutationHandler.js +886 -0
- package/dist/sync/registry.d.ts +73 -0
- package/dist/sync/registry.js +223 -0
- package/dist/sync/routes.d.ts +62 -0
- package/dist/sync/routes.js +936 -0
- package/dist/sync/scripts/compactTombstones.d.ts +47 -0
- package/dist/sync/scripts/compactTombstones.js +270 -0
- package/dist/sync/serialize.d.ts +20 -0
- package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
- package/dist/sync/socketHandlers.d.ts +54 -0
- package/dist/sync/socketHandlers.js +470 -0
- package/dist/sync/streams.d.ts +85 -0
- package/dist/sync/streams.js +221 -0
- package/dist/sync/syncApp.d.ts +29 -0
- package/dist/sync/syncApp.js +44 -0
- package/dist/sync/syncBatch.d.ts +71 -0
- package/dist/sync/syncBatch.js +252 -0
- package/dist/sync/syncSeqPlugin.d.ts +8 -0
- package/dist/sync/syncSeqPlugin.js +903 -0
- package/dist/sync/types.d.ts +254 -0
- package/dist/sync/types.js +9 -0
- package/dist/terrenoApp.js +90 -27
- package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
- package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
- package/package.json +7 -6
- package/src/__tests__/openApiCompat.test.ts +20 -0
- package/src/actions.openApi.test.ts +7 -10
- package/src/api.test.ts +12 -0
- package/src/api.ts +87 -295
- package/src/auth.test.ts +128 -176
- package/src/auth.ts +109 -0
- package/src/betterAuth.ts +6 -0
- package/src/betterAuthSetup.test.ts +34 -0
- package/src/betterAuthSetup.ts +20 -1
- package/src/docLoader.ts +3 -1
- package/src/errors.ts +33 -0
- package/src/expressServer.test.ts +7 -1
- package/src/index.ts +42 -0
- package/src/middleware.test.ts +40 -1
- package/src/middleware.ts +15 -0
- package/src/openApiCompat.ts +99 -45
- package/src/permissions.test.ts +75 -1
- package/src/permissions.ts +49 -0
- package/src/plugins.test.ts +54 -0
- package/src/plugins.ts +43 -0
- package/src/realtime/changeStreamWatcher.ts +552 -114
- package/src/realtime/realtime.test.ts +157 -0
- package/src/realtime/realtimeApp.ts +75 -11
- package/src/realtime/sessionRevalidation.test.ts +423 -0
- package/src/realtime/sessionRevalidation.ts +273 -0
- package/src/realtime/socketAuth.test.ts +355 -0
- package/src/realtime/socketAuth.ts +228 -0
- package/src/realtime/socketUser.ts +69 -0
- package/src/realtime/types.ts +40 -0
- package/src/secretProviders.test.ts +7 -1
- package/src/secretProviders.ts +15 -3
- package/src/sync/ensureSyncIndexes.test.ts +144 -0
- package/src/sync/executors.test.ts +882 -0
- package/src/sync/executors.ts +703 -0
- package/src/sync/integration.test.ts +1160 -0
- package/src/sync/loadHarness.ts +618 -0
- package/src/sync/models.ts +640 -0
- package/src/sync/mutationHandler.test.ts +1252 -0
- package/src/sync/mutationHandler.ts +850 -0
- package/src/sync/registry.ts +232 -0
- package/src/sync/routes.ts +812 -0
- package/src/sync/scripts/compactTombstones.test.ts +249 -0
- package/src/sync/scripts/compactTombstones.ts +145 -0
- package/src/sync/serialize.ts +48 -0
- package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
- package/src/sync/socketHandlers.ts +320 -0
- package/src/sync/streams.ts +188 -0
- package/src/sync/sync.test.ts +582 -0
- package/src/sync/syncApp.ts +44 -0
- package/src/sync/syncBatch.ts +219 -0
- package/src/sync/syncFrontier.test.ts +494 -0
- package/src/sync/syncPhaseC.test.ts +718 -0
- package/src/sync/syncRoutes.test.ts +865 -0
- package/src/sync/syncSeqPlugin.ts +738 -0
- package/src/sync/syncSocket.test.ts +1440 -0
- package/src/sync/types.ts +270 -0
- package/src/terrenoApp.ts +37 -17
- package/src/tests/bunSetup.ts +8 -0
- package/tsconfig.json +4 -4
- package/tsconfig.typedoc.json +7 -1
- package/dist/__tests__/openApiCompat.test.js +0 -317
- package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
- package/dist/__tests__/versionCheckPlugin.test.js +0 -597
- package/dist/actions.openApi.test.d.ts +0 -1
- package/dist/actions.openApi.test.js +0 -255
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -1089
- package/dist/api.arrayOperations.test.d.ts +0 -1
- package/dist/api.arrayOperations.test.js +0 -878
- package/dist/api.asyncHandler.test.d.ts +0 -1
- package/dist/api.asyncHandler.test.js +0 -236
- package/dist/api.coverage.test.d.ts +0 -1
- package/dist/api.coverage.test.js +0 -194
- package/dist/api.errorPaths.test.d.ts +0 -1
- package/dist/api.errorPaths.test.js +0 -801
- package/dist/api.errors.test.d.ts +0 -1
- package/dist/api.errors.test.js +0 -185
- package/dist/api.hooks.test.d.ts +0 -1
- package/dist/api.hooks.test.js +0 -950
- package/dist/api.query.test.d.ts +0 -1
- package/dist/api.query.test.js +0 -948
- package/dist/api.test.d.ts +0 -1
- package/dist/api.test.js +0 -2820
- package/dist/auth.compile.test.d.ts +0 -1
- package/dist/auth.compile.test.js +0 -201
- package/dist/auth.coverage.test.d.ts +0 -1
- package/dist/auth.coverage.test.js +0 -202
- package/dist/auth.test.d.ts +0 -1
- package/dist/auth.test.js +0 -2286
- package/dist/betterAuth.test.d.ts +0 -1
- package/dist/betterAuth.test.js +0 -181
- package/dist/betterAuthApp.test.d.ts +0 -1
- package/dist/betterAuthApp.test.js +0 -242
- package/dist/betterAuthSetup.test.d.ts +0 -1
- package/dist/betterAuthSetup.test.js +0 -762
- package/dist/config.test.d.ts +0 -1
- package/dist/config.test.js +0 -353
- package/dist/configuration.test.d.ts +0 -1
- package/dist/configuration.test.js +0 -980
- package/dist/configurationPlugin.test.d.ts +0 -1
- package/dist/configurationPlugin.test.js +0 -827
- package/dist/consentApp.test.d.ts +0 -1
- package/dist/consentApp.test.js +0 -1415
- package/dist/docLoader.test.d.ts +0 -1
- package/dist/docLoader.test.js +0 -137
- package/dist/envConfigurationPlugin.test.d.ts +0 -1
- package/dist/envConfigurationPlugin.test.js +0 -369
- package/dist/errors.test.d.ts +0 -1
- package/dist/errors.test.js +0 -607
- package/dist/expressServer.test.d.ts +0 -1
- package/dist/expressServer.test.js +0 -1249
- package/dist/githubAuth.test.d.ts +0 -1
- package/dist/githubAuth.test.js +0 -1142
- package/dist/httpClient.test.d.ts +0 -1
- package/dist/httpClient.test.js +0 -1471
- package/dist/logger.test.d.ts +0 -1
- package/dist/logger.test.js +0 -685
- package/dist/middleware.test.d.ts +0 -1
- package/dist/middleware.test.js +0 -241
- package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
- package/dist/notifiers/googleChatNotifier.test.js +0 -298
- package/dist/notifiers/slackNotifier.test.d.ts +0 -1
- package/dist/notifiers/slackNotifier.test.js +0 -297
- package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
- package/dist/notifiers/zoomNotifier.test.js +0 -373
- package/dist/openApi.test.d.ts +0 -1
- package/dist/openApi.test.js +0 -620
- package/dist/openApiBuilder.test.d.ts +0 -1
- package/dist/openApiBuilder.test.js +0 -601
- package/dist/openApiEtag.test.d.ts +0 -1
- package/dist/openApiEtag.test.js +0 -86
- package/dist/openApiValidator.test.d.ts +0 -1
- package/dist/openApiValidator.test.js +0 -761
- package/dist/permissions.middleware.test.d.ts +0 -1
- package/dist/permissions.middleware.test.js +0 -308
- package/dist/permissions.test.d.ts +0 -1
- package/dist/permissions.test.js +0 -439
- package/dist/plugins.test.d.ts +0 -1
- package/dist/plugins.test.js +0 -890
- package/dist/populate.test.d.ts +0 -1
- package/dist/populate.test.js +0 -373
- package/dist/realtime/realtime.test.d.ts +0 -10
- package/dist/realtime/realtime.test.js +0 -3982
- package/dist/requestContext.test.d.ts +0 -1
- package/dist/requestContext.test.js +0 -412
- package/dist/scriptRunner.test.d.ts +0 -1
- package/dist/scriptRunner.test.js +0 -386
- package/dist/secretProviders.test.d.ts +0 -1
- package/dist/secretProviders.test.js +0 -726
- package/dist/syncConsents.test.d.ts +0 -1
- package/dist/syncConsents.test.js +0 -522
- package/dist/terrenoApp.test.d.ts +0 -1
- package/dist/terrenoApp.test.js +0 -489
- package/dist/tests/bunSetup.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.js +0 -37
- package/dist/tests/testEnv.d.ts +0 -0
- package/dist/tests/testEnv.js +0 -6
- package/dist/transformers.test.d.ts +0 -1
- package/dist/transformers.test.js +0 -423
- package/dist/utils.test.d.ts +0 -1
- package/dist/utils.test.js +0 -242
- /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
|
@@ -69,8 +69,10 @@ export declare const installRealtimeSocketHandlers: (socket: RealtimeSocketLike,
|
|
|
69
69
|
* ```
|
|
70
70
|
*/
|
|
71
71
|
export declare class RealtimeApp implements TerrenoPlugin {
|
|
72
|
+
private app;
|
|
72
73
|
private io;
|
|
73
74
|
private config;
|
|
75
|
+
private sessionRevalidation;
|
|
74
76
|
constructor(config?: RealtimeAppOptions);
|
|
75
77
|
/**
|
|
76
78
|
* Register routes and middleware. Adds a /realtime/health endpoint.
|
|
@@ -82,13 +82,17 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
82
82
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
83
83
|
exports.RealtimeApp = exports.installRealtimeSocketHandlers = exports.redactCredentials = exports.MAX_QUERY_SUBSCRIPTIONS = exports.MAX_DOCUMENT_SUBSCRIPTIONS = exports.MAX_MODEL_SUBSCRIPTIONS = void 0;
|
|
84
84
|
var Sentry = __importStar(require("@sentry/bun"));
|
|
85
|
-
var socketio_jwt_1 = require("@thream/socketio-jwt");
|
|
86
85
|
var socket_io_1 = require("socket.io");
|
|
87
86
|
var logger_1 = require("../logger");
|
|
88
87
|
var permissions_1 = require("../permissions");
|
|
88
|
+
var registry_1 = require("../sync/registry");
|
|
89
|
+
var socketHandlers_1 = require("../sync/socketHandlers");
|
|
90
|
+
var syncApp_1 = require("../sync/syncApp");
|
|
89
91
|
var changeStreamWatcher_1 = require("./changeStreamWatcher");
|
|
90
92
|
var queryStore_1 = require("./queryStore");
|
|
91
|
-
var
|
|
93
|
+
var registry_2 = require("./registry");
|
|
94
|
+
var sessionRevalidation_1 = require("./sessionRevalidation");
|
|
95
|
+
var socketAuth_1 = require("./socketAuth");
|
|
92
96
|
var socketUser_1 = require("./socketUser");
|
|
93
97
|
/**
|
|
94
98
|
* Caps on per-socket subscriptions. Prevents a malicious or buggy client from
|
|
@@ -177,7 +181,9 @@ var installRealtimeSocketHandlers = function (socket, options) {
|
|
|
177
181
|
var logInfo = (_a = options.logInfo) !== null && _a !== void 0 ? _a : (function () { });
|
|
178
182
|
var userId = (_b = socket.decodedToken) === null || _b === void 0 ? void 0 : _b.id;
|
|
179
183
|
var isAdmin = ((_c = socket.decodedToken) === null || _c === void 0 ? void 0 : _c.admin) === true;
|
|
180
|
-
|
|
184
|
+
// D2: re-derive per event rather than capturing once at install time, so a
|
|
185
|
+
// handshake-time-loaded full user (or one refreshed by D1's sweep) is always used.
|
|
186
|
+
var currentUser = function () { return (0, socketUser_1.getSocketUser)(socket); };
|
|
181
187
|
var counts = { document: 0, model: 0, query: 0 };
|
|
182
188
|
var joinUserRooms = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
183
189
|
return __generator(this, function (_a) {
|
|
@@ -217,7 +223,7 @@ var installRealtimeSocketHandlers = function (socket, options) {
|
|
|
217
223
|
logInfo("[realtime] User ".concat(userId, " hit model subscription limit"));
|
|
218
224
|
return [2 /*return*/];
|
|
219
225
|
}
|
|
220
|
-
entry = (0,
|
|
226
|
+
entry = (0, registry_2.findRegistryEntryByRoutePath)(modelName);
|
|
221
227
|
if (!entry) {
|
|
222
228
|
logInfo("[realtime] User ".concat(userId, " denied model subscription: collection \"").concat(modelName, "\" not registered"));
|
|
223
229
|
return [2 /*return*/];
|
|
@@ -230,7 +236,7 @@ var installRealtimeSocketHandlers = function (socket, options) {
|
|
|
230
236
|
"owner strategy restricts model room to admins");
|
|
231
237
|
return [2 /*return*/];
|
|
232
238
|
}
|
|
233
|
-
return [4 /*yield*/, canSubscribe(entry, "list",
|
|
239
|
+
return [4 /*yield*/, canSubscribe(entry, "list", currentUser())];
|
|
234
240
|
case 1:
|
|
235
241
|
if (!(_a.sent())) {
|
|
236
242
|
logInfo("[realtime] User ".concat(userId, " denied model subscription for ").concat(modelName, ": list permission denied"));
|
|
@@ -275,13 +281,13 @@ var installRealtimeSocketHandlers = function (socket, options) {
|
|
|
275
281
|
logInfo("[realtime] User ".concat(userId, " hit document subscription limit"));
|
|
276
282
|
return [2 /*return*/];
|
|
277
283
|
}
|
|
278
|
-
entry = (0,
|
|
284
|
+
entry = (0, registry_2.findRegistryEntryByRoutePath)(payload.collection);
|
|
279
285
|
if (!entry) {
|
|
280
286
|
logInfo("[realtime] User ".concat(userId, " denied document subscription: ") +
|
|
281
287
|
"collection \"".concat(payload.collection, "\" not registered"));
|
|
282
288
|
return [2 /*return*/];
|
|
283
289
|
}
|
|
284
|
-
return [4 /*yield*/, canSubscribe(entry, "read",
|
|
290
|
+
return [4 /*yield*/, canSubscribe(entry, "read", currentUser())];
|
|
285
291
|
case 1:
|
|
286
292
|
if (!(_a.sent())) {
|
|
287
293
|
logInfo("[realtime] User ".concat(userId, " denied document subscription for ") +
|
|
@@ -336,13 +342,13 @@ var installRealtimeSocketHandlers = function (socket, options) {
|
|
|
336
342
|
logInfo("[realtime] User ".concat(userId, " hit query subscription limit"));
|
|
337
343
|
return [2 /*return*/];
|
|
338
344
|
}
|
|
339
|
-
entry = (0,
|
|
345
|
+
entry = (0, registry_2.findRegistryEntryByRoutePath)(payload.collection);
|
|
340
346
|
if (!entry) {
|
|
341
347
|
logInfo("[realtime] User ".concat(userId, " denied query subscription: ") +
|
|
342
348
|
"collection \"".concat(payload.collection, "\" not registered"));
|
|
343
349
|
return [2 /*return*/];
|
|
344
350
|
}
|
|
345
|
-
return [4 /*yield*/, getAuthorizedQuery(entry, __assign({}, payload.query),
|
|
351
|
+
return [4 /*yield*/, getAuthorizedQuery(entry, __assign({}, payload.query), currentUser())];
|
|
346
352
|
case 1:
|
|
347
353
|
query = _a.sent();
|
|
348
354
|
if (!query) {
|
|
@@ -421,7 +427,9 @@ exports.installRealtimeSocketHandlers = installRealtimeSocketHandlers;
|
|
|
421
427
|
var RealtimeApp = /** @class */ (function () {
|
|
422
428
|
function RealtimeApp(config) {
|
|
423
429
|
if (config === void 0) { config = {}; }
|
|
430
|
+
this.app = null;
|
|
424
431
|
this.io = null;
|
|
432
|
+
this.sessionRevalidation = null;
|
|
425
433
|
this.config = config;
|
|
426
434
|
}
|
|
427
435
|
/**
|
|
@@ -429,6 +437,7 @@ var RealtimeApp = /** @class */ (function () {
|
|
|
429
437
|
*/
|
|
430
438
|
RealtimeApp.prototype.register = function (app) {
|
|
431
439
|
var _this = this;
|
|
440
|
+
this.app = app;
|
|
432
441
|
app.get("/realtime/health", function (_req, res) {
|
|
433
442
|
var _a, _b, _c, _d;
|
|
434
443
|
var connected = (_c = (_b = (_a = _this.io) === null || _a === void 0 ? void 0 : _a.engine) === null || _b === void 0 ? void 0 : _b.clientsCount) !== null && _c !== void 0 ? _c : 0;
|
|
@@ -444,6 +453,7 @@ var RealtimeApp = /** @class */ (function () {
|
|
|
444
453
|
* and starts the change stream watcher.
|
|
445
454
|
*/
|
|
446
455
|
RealtimeApp.prototype.onServerCreated = function (server) {
|
|
456
|
+
var _this = this;
|
|
447
457
|
var _a, _b, _c;
|
|
448
458
|
var debug = (_a = this.config.debug) !== null && _a !== void 0 ? _a : false;
|
|
449
459
|
var logInfo = function (message) {
|
|
@@ -459,24 +469,48 @@ var RealtimeApp = /** @class */ (function () {
|
|
|
459
469
|
origin: "*",
|
|
460
470
|
},
|
|
461
471
|
});
|
|
462
|
-
// JWT
|
|
472
|
+
// Authentication middleware: legacy JWT first, optionally Better Auth sessions.
|
|
463
473
|
var tokenSecret = (_c = this.config.tokenSecret) !== null && _c !== void 0 ? _c : process.env.TOKEN_SECRET;
|
|
464
474
|
if (!tokenSecret) {
|
|
465
475
|
throw new Error("[realtime] TOKEN_SECRET is required for socket authentication. " +
|
|
466
476
|
"Set process.env.TOKEN_SECRET or pass tokenSecret in RealtimeAppOptions.");
|
|
467
477
|
}
|
|
468
|
-
|
|
469
|
-
|
|
478
|
+
// D1: pass the issuer through for parity with the HTTP JWT path
|
|
479
|
+
// (jwt.verify(token, secret, {issuer})) — without it a validly-signed token
|
|
480
|
+
// issued for a different TOKEN_ISSUER was silently accepted over sockets. A
|
|
481
|
+
// thunk (not a value captured once here) so a later change to
|
|
482
|
+
// process.env.TOKEN_ISSUER — or to an explicitly configured tokenIssuer — is
|
|
483
|
+
// honored on every handshake, matching the HTTP path's per-request env read.
|
|
484
|
+
var resolveTokenIssuer = function () { var _a; return (_a = _this.config.tokenIssuer) !== null && _a !== void 0 ? _a : process.env.TOKEN_ISSUER; };
|
|
485
|
+
this.io.use((0, socketAuth_1.createSocketAuthMiddleware)({
|
|
486
|
+
betterAuth: this.config.betterAuth,
|
|
487
|
+
issuer: resolveTokenIssuer,
|
|
488
|
+
tokenSecret: tokenSecret,
|
|
470
489
|
}));
|
|
471
|
-
logInfo("[realtime]
|
|
490
|
+
logInfo("[realtime] Socket auth middleware added (JWT".concat(this.config.betterAuth ? " + Better Auth" : "", ")"));
|
|
491
|
+
// Task 9.21: tenant/custom sync scopes can only be resolved from the full user
|
|
492
|
+
// document, so warn loudly here (once, at startup) rather than silently serving
|
|
493
|
+
// empty tenant streams for the life of the process.
|
|
494
|
+
(0, registry_1.warnOnSyncScopesWithoutUserModel)({ userModel: this.config.userModel });
|
|
472
495
|
// Configure adapter for multi-instance deployments
|
|
473
496
|
this.setupAdapter(logInfo);
|
|
474
497
|
// Connection handling
|
|
475
498
|
this.io.on("connection", function (socket) {
|
|
499
|
+
var _a, _b;
|
|
476
500
|
try {
|
|
477
|
-
//
|
|
501
|
+
// D2: load the full user document once at handshake (by the decoded
|
|
502
|
+
// token's id) and cache it on socket.data.fullUser — getSocketUser prefers
|
|
503
|
+
// it over the synthetic {_id, admin, id} shape for every authorization
|
|
504
|
+
// check below. Fire-and-forget: handlers install synchronously and fall
|
|
505
|
+
// back to the synthetic shape until this resolves.
|
|
506
|
+
void (0, sessionRevalidation_1.loadFullUserForSocket)(socket, _this.config.userModel);
|
|
507
|
+
// The auth middleware adds `decodedToken` at runtime; cast through
|
|
478
508
|
// RealtimeSocketLike (a structural subset) to keep socket handler logic testable.
|
|
479
509
|
(0, exports.installRealtimeSocketHandlers)(socket, { logInfo: logInfo });
|
|
510
|
+
// Sync protocol handlers (sync:subscribe / sync:mutate). Options come from the
|
|
511
|
+
// SyncApp registered on this Express app or an explicit `sync` override here;
|
|
512
|
+
// with neither, the handlers still install and reject unknown collections.
|
|
513
|
+
(0, socketHandlers_1.installSyncSocketHandlers)(_this.io, socket, (_b = (_a = _this.config.sync) !== null && _a !== void 0 ? _a : (_this.app ? (0, syncApp_1.getSyncAppOptions)(_this.app) : undefined)) !== null && _b !== void 0 ? _b : {}, { logInfo: logInfo });
|
|
480
514
|
}
|
|
481
515
|
catch (error) {
|
|
482
516
|
logger_1.logger.error("[realtime] Error handling connection: ".concat(error));
|
|
@@ -489,6 +523,21 @@ var RealtimeApp = /** @class */ (function () {
|
|
|
489
523
|
});
|
|
490
524
|
// Start the change stream watcher
|
|
491
525
|
(0, changeStreamWatcher_1.startChangeStreamWatcher)(this.io, this.config.changeStream, debug);
|
|
526
|
+
// D1: periodic session re-validation sweep — disconnects sockets whose JWT has
|
|
527
|
+
// expired, whose Better Auth session is no longer valid, or whose user has
|
|
528
|
+
// since been disabled; also refreshes socket.data.fullUser (D2) and
|
|
529
|
+
// re-resolves sync room membership (D4) for sockets that remain valid.
|
|
530
|
+
// A thunk (not a snapshot) reads this app's SyncApp options fresh on every sweep.
|
|
531
|
+
this.sessionRevalidation = (0, sessionRevalidation_1.startSessionRevalidationSweep)(this.io, function () {
|
|
532
|
+
var _a;
|
|
533
|
+
return ({
|
|
534
|
+
betterAuth: _this.config.betterAuth,
|
|
535
|
+
intervalMs: _this.config.sessionRevalidationIntervalMs,
|
|
536
|
+
logInfo: logInfo,
|
|
537
|
+
sync: (_a = _this.config.sync) !== null && _a !== void 0 ? _a : (_this.app ? (0, syncApp_1.getSyncAppOptions)(_this.app) : undefined),
|
|
538
|
+
userModel: _this.config.userModel,
|
|
539
|
+
});
|
|
540
|
+
});
|
|
492
541
|
logInfo("[realtime] Socket.io server setup complete");
|
|
493
542
|
}
|
|
494
543
|
catch (error) {
|
|
@@ -509,22 +558,25 @@ var RealtimeApp = /** @class */ (function () {
|
|
|
509
558
|
RealtimeApp.prototype.close = function () {
|
|
510
559
|
return __awaiter(this, void 0, void 0, function () {
|
|
511
560
|
var error_2;
|
|
512
|
-
|
|
513
|
-
|
|
561
|
+
var _a;
|
|
562
|
+
return __generator(this, function (_b) {
|
|
563
|
+
switch (_b.label) {
|
|
514
564
|
case 0:
|
|
515
|
-
|
|
565
|
+
_b.trys.push([0, 4, , 5]);
|
|
566
|
+
(_a = this.sessionRevalidation) === null || _a === void 0 ? void 0 : _a.stop();
|
|
567
|
+
this.sessionRevalidation = null;
|
|
516
568
|
return [4 /*yield*/, (0, changeStreamWatcher_1.stopChangeStreamWatcher)()];
|
|
517
569
|
case 1:
|
|
518
|
-
|
|
570
|
+
_b.sent();
|
|
519
571
|
if (!this.io) return [3 /*break*/, 3];
|
|
520
572
|
return [4 /*yield*/, this.io.close()];
|
|
521
573
|
case 2:
|
|
522
|
-
|
|
574
|
+
_b.sent();
|
|
523
575
|
this.io = null;
|
|
524
|
-
|
|
576
|
+
_b.label = 3;
|
|
525
577
|
case 3: return [3 /*break*/, 5];
|
|
526
578
|
case 4:
|
|
527
|
-
error_2 =
|
|
579
|
+
error_2 = _b.sent();
|
|
528
580
|
logger_1.logger.error("[realtime] Error closing: ".concat(error_2));
|
|
529
581
|
return [3 /*break*/, 5];
|
|
530
582
|
case 5: return [2 /*return*/];
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { Server, Socket } from "socket.io";
|
|
2
|
+
import type { UserModel } from "../auth";
|
|
3
|
+
import type { SyncAppOptions } from "../sync/routes";
|
|
4
|
+
import type { BetterAuthSocketOptions } from "./socketAuth";
|
|
5
|
+
import { type SocketDataBag, type SocketWithDecodedToken } from "./socketUser";
|
|
6
|
+
/**
|
|
7
|
+
* Default interval for the periodic socket session re-validation sweep (D1): 60
|
|
8
|
+
* seconds. Sockets authenticate once at handshake; without a sweep, a revoked
|
|
9
|
+
* session, an expired token, or a user subsequently disabled keeps streaming deltas
|
|
10
|
+
* (including PHI) indefinitely.
|
|
11
|
+
*/
|
|
12
|
+
export declare const DEFAULT_SESSION_REVALIDATION_INTERVAL_MS = 60000;
|
|
13
|
+
/** Minimal socket shape the sweep needs. Lets tests drive it with a mock. */
|
|
14
|
+
export interface RevalidatableSocket extends SocketWithDecodedToken {
|
|
15
|
+
id: string;
|
|
16
|
+
encodedToken?: string;
|
|
17
|
+
data?: SocketDataBag;
|
|
18
|
+
emit: (event: string, payload?: unknown) => void;
|
|
19
|
+
disconnect: (close?: boolean) => void;
|
|
20
|
+
join: (room: string) => Promise<void> | void;
|
|
21
|
+
leave: (room: string) => Promise<void> | void;
|
|
22
|
+
}
|
|
23
|
+
export interface SessionRevalidationOptions {
|
|
24
|
+
/** Application user model, for reloading the full user and its `disabled` flag. */
|
|
25
|
+
userModel?: UserModel;
|
|
26
|
+
/** Enables re-validating Better Auth-authenticated sockets. */
|
|
27
|
+
betterAuth?: BetterAuthSocketOptions;
|
|
28
|
+
/** Active SyncAppOptions (for `getUserScopes`), used by D4's room re-resolution. */
|
|
29
|
+
sync?: SyncAppOptions;
|
|
30
|
+
logInfo?: (message: string) => void;
|
|
31
|
+
}
|
|
32
|
+
/** Result of re-validating a single socket, for tests/observability. */
|
|
33
|
+
export type RevalidationOutcome = "valid" | "expired" | "disabled" | "invalid-session";
|
|
34
|
+
/**
|
|
35
|
+
* Re-run the cheap parts of the auth validator for an already-connected socket:
|
|
36
|
+
* - JWT sockets (`decodedToken.authKind === "jwt"`): verify `exp` locally (no
|
|
37
|
+
* signature re-check — a stolen-but-still-valid token is not this sweep's job;
|
|
38
|
+
* revocation-by-expiry and disablement are).
|
|
39
|
+
* - Better Auth sockets (`decodedToken.authKind === "better-auth"`): re-run
|
|
40
|
+
* `auth.api.getSession` for the retained session token.
|
|
41
|
+
* Also reloads the user's `disabled` flag (and the full user document, refreshing
|
|
42
|
+
* `socket.data.fullUser` for D2) when a `userModel` is configured.
|
|
43
|
+
*/
|
|
44
|
+
export declare const revalidateSocketSession: (socket: RevalidatableSocket, options: SessionRevalidationOptions) => Promise<RevalidationOutcome>;
|
|
45
|
+
/**
|
|
46
|
+
* D4: re-resolve the streams the socket's user currently belongs to for every
|
|
47
|
+
* subscribed collection, and `socket.leave()` any previously-joined sync room the
|
|
48
|
+
* user no longer holds (e.g. a revoked organization membership). Joins any newly
|
|
49
|
+
* granted rooms too, mirroring what a fresh `sync:subscribe` would do, so a
|
|
50
|
+
* membership grant also takes effect without a reconnect.
|
|
51
|
+
*/
|
|
52
|
+
export declare const reresolveSyncRoomsForSocket: (socket: RevalidatableSocket, options: SessionRevalidationOptions) => Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Run one sweep pass over every connected socket: re-validate the session (D1),
|
|
55
|
+
* disconnecting (`sync:auth-expired` then `disconnect(true)`) any socket that fails,
|
|
56
|
+
* and re-resolve sync room membership (D4) for sockets that remain valid.
|
|
57
|
+
*/
|
|
58
|
+
export declare const runSessionRevalidationSweep: (io: Server, options?: SessionRevalidationOptions) => Promise<void>;
|
|
59
|
+
/** Handle returned by {@link startSessionRevalidationSweep}; call to stop the timer. */
|
|
60
|
+
export type SessionRevalidationHandle = {
|
|
61
|
+
stop: () => void;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Start the periodic sweep (D1). Returns a handle to stop it (called from
|
|
65
|
+
* `RealtimeApp.close()`). A `intervalMs` of 0 disables the sweep (useful for tests
|
|
66
|
+
* that don't want a background timer).
|
|
67
|
+
*
|
|
68
|
+
* `options` may be a plain object or a thunk resolved fresh on EVERY tick — pass a
|
|
69
|
+
* thunk when any field (notably `sync`, whose `getUserScopes` resolver is published
|
|
70
|
+
* by the `SyncApp` plugin and may not be registered yet, or may change, at the time
|
|
71
|
+
* `RealtimeApp.onServerCreated()` runs) must never go stale for the lifetime of the
|
|
72
|
+
* sweep, mirroring the per-handshake freshness `createLegacyJwtValidator`'s issuer
|
|
73
|
+
* thunk already provides.
|
|
74
|
+
*/
|
|
75
|
+
export declare const startSessionRevalidationSweep: (io: Server, optionsOrThunk?: (SessionRevalidationOptions & {
|
|
76
|
+
intervalMs?: number;
|
|
77
|
+
}) | (() => SessionRevalidationOptions & {
|
|
78
|
+
intervalMs?: number;
|
|
79
|
+
})) => SessionRevalidationHandle;
|
|
80
|
+
/**
|
|
81
|
+
* Load the full user document for a just-authenticated socket and cache it on
|
|
82
|
+
* `socket.data.fullUser` (D2). Called once at handshake, right after the auth
|
|
83
|
+
* middleware succeeds; the periodic sweep (D1) refreshes it afterwards. A no-op when
|
|
84
|
+
* no `userModel` is configured (falls back to the synthetic decoded-token shape via
|
|
85
|
+
* `getSocketUser`) or the decoded token carries no id (should not happen for a
|
|
86
|
+
* socket that passed auth, but guarded defensively).
|
|
87
|
+
*/
|
|
88
|
+
export declare const loadFullUserForSocket: (socket: Socket & {
|
|
89
|
+
data: SocketDataBag;
|
|
90
|
+
}, userModel?: UserModel) => Promise<void>;
|