@bymax-one/nest-realtime 1.2.17 → 1.2.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +67 -1
- package/README.md +97 -8
- package/dist/internal/index.cjs +677 -110
- package/dist/internal/index.d.cts +130 -9
- package/dist/internal/index.d.ts +130 -9
- package/dist/internal/index.mjs +674 -112
- package/dist/observability/index.cjs +104 -0
- package/dist/observability/index.d.cts +47 -0
- package/dist/observability/index.d.ts +47 -0
- package/dist/observability/index.mjs +104 -0
- package/dist/websocket/index.cjs +117 -19
- package/dist/websocket/index.d.cts +14 -4
- package/dist/websocket/index.d.ts +14 -4
- package/dist/websocket/index.mjs +118 -20
- package/package.json +31 -3
package/dist/internal/index.cjs
CHANGED
|
@@ -43,6 +43,85 @@ var REALTIME_ERROR_CODES = {
|
|
|
43
43
|
REPLAY_BUFFER_MISS: "REALTIME_REPLAY_BUFFER_MISS"
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
+
// src/server/utils/is-plain-object.ts
|
|
47
|
+
function isPlainObject(value) {
|
|
48
|
+
if (typeof value !== "object") return false;
|
|
49
|
+
try {
|
|
50
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
51
|
+
} catch {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/server/config/drop-unset.ts
|
|
57
|
+
var SSE_KEYS = {
|
|
58
|
+
endpoint: true,
|
|
59
|
+
heartbeatMs: true,
|
|
60
|
+
replayBufferSize: true,
|
|
61
|
+
replayTtlMs: true,
|
|
62
|
+
replayMaxIdleUsers: true,
|
|
63
|
+
maxConnectionsPerUser: true,
|
|
64
|
+
emitConnectionEvent: true,
|
|
65
|
+
maxQueuedBytes: true,
|
|
66
|
+
maxQueuedEvents: true,
|
|
67
|
+
stallHeartbeats: true
|
|
68
|
+
};
|
|
69
|
+
var WEBSOCKET_KEYS = {
|
|
70
|
+
namespace: true,
|
|
71
|
+
cors: true,
|
|
72
|
+
maxHttpBufferSize: true,
|
|
73
|
+
pingIntervalMs: true,
|
|
74
|
+
pingTimeoutMs: true,
|
|
75
|
+
maxConnectionsPerUser: true,
|
|
76
|
+
emitConnectionEvent: true,
|
|
77
|
+
redisAdapter: true
|
|
78
|
+
};
|
|
79
|
+
var REAUTHENTICATION_KEYS = {
|
|
80
|
+
intervalSeconds: true,
|
|
81
|
+
onFailure: true,
|
|
82
|
+
cacheTtlMs: true,
|
|
83
|
+
revalidateTimeoutMs: true,
|
|
84
|
+
concurrency: true
|
|
85
|
+
};
|
|
86
|
+
var TOP_LEVEL_KEYS = {
|
|
87
|
+
transport: true,
|
|
88
|
+
authenticator: true,
|
|
89
|
+
tenantResolver: true,
|
|
90
|
+
hooks: true,
|
|
91
|
+
hookTimeoutMs: true,
|
|
92
|
+
onConnectFailure: true,
|
|
93
|
+
authenticateTimeoutMs: true,
|
|
94
|
+
maxPendingHandshakes: true,
|
|
95
|
+
maxConnections: true,
|
|
96
|
+
pubsub: true,
|
|
97
|
+
offlineQueue: true,
|
|
98
|
+
presence: true,
|
|
99
|
+
sse: true,
|
|
100
|
+
websocket: true,
|
|
101
|
+
reauthenticationPolicy: true
|
|
102
|
+
};
|
|
103
|
+
var GROUPS = {
|
|
104
|
+
sse: SSE_KEYS,
|
|
105
|
+
websocket: WEBSOCKET_KEYS,
|
|
106
|
+
reauthenticationPolicy: REAUTHENTICATION_KEYS
|
|
107
|
+
};
|
|
108
|
+
function readSet(source, known) {
|
|
109
|
+
const kept = {};
|
|
110
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(source), ...Object.keys(known)])) {
|
|
111
|
+
const value = source[key];
|
|
112
|
+
if (value !== void 0 && value !== null) kept[key] = value;
|
|
113
|
+
}
|
|
114
|
+
return kept;
|
|
115
|
+
}
|
|
116
|
+
function dropUnset(options) {
|
|
117
|
+
const kept = readSet(options, TOP_LEVEL_KEYS);
|
|
118
|
+
for (const [group, keys] of Object.entries(GROUPS)) {
|
|
119
|
+
const value = kept[group];
|
|
120
|
+
if (isPlainObject(value)) kept[group] = readSet(value, keys);
|
|
121
|
+
}
|
|
122
|
+
return kept;
|
|
123
|
+
}
|
|
124
|
+
|
|
46
125
|
// src/server/config/default-options.ts
|
|
47
126
|
var DEFAULT_SSE = {
|
|
48
127
|
endpoint: "/realtime/sse",
|
|
@@ -56,7 +135,8 @@ var DEFAULT_SSE = {
|
|
|
56
135
|
maxQueuedEvents: 1e3,
|
|
57
136
|
stallHeartbeats: 3
|
|
58
137
|
};
|
|
59
|
-
function applyDefaults(
|
|
138
|
+
function applyDefaults(raw) {
|
|
139
|
+
const options = dropUnset(raw);
|
|
60
140
|
return Object.freeze({
|
|
61
141
|
...options,
|
|
62
142
|
sse: { ...DEFAULT_SSE, ...options.sse ?? {} }
|
|
@@ -77,6 +157,25 @@ var DEFAULT_HOOK_TIMEOUT_MS = 5e3;
|
|
|
77
157
|
var HEARTBEAT_MIN_MS = 5e3;
|
|
78
158
|
var HEARTBEAT_MAX_MS = 9e4;
|
|
79
159
|
|
|
160
|
+
// src/server/utils/describe-received.ts
|
|
161
|
+
var KIND_OF_TYPE = {
|
|
162
|
+
string: "a string",
|
|
163
|
+
boolean: "a boolean",
|
|
164
|
+
bigint: "a bigint",
|
|
165
|
+
symbol: "a symbol",
|
|
166
|
+
function: "a function",
|
|
167
|
+
undefined: "undefined"
|
|
168
|
+
};
|
|
169
|
+
function describeReceived(value) {
|
|
170
|
+
if (typeof value === "number") {
|
|
171
|
+
return Number.isFinite(value) ? String(value) : "a number that is not finite";
|
|
172
|
+
}
|
|
173
|
+
if (value === null) return "null";
|
|
174
|
+
if (Array.isArray(value)) return "an array";
|
|
175
|
+
if (typeof value === "object" && !isPlainObject(value)) return "a built-in object";
|
|
176
|
+
return KIND_OF_TYPE[typeof value] ?? "an object";
|
|
177
|
+
}
|
|
178
|
+
|
|
80
179
|
// src/server/utils/realtime-error.ts
|
|
81
180
|
function realtimeError(code, detail, options = {}) {
|
|
82
181
|
const lead = options.who === void 0 ? code : `[${options.who}] ${code}`;
|
|
@@ -85,13 +184,93 @@ function realtimeError(code, detail, options = {}) {
|
|
|
85
184
|
return Object.assign(error, { code });
|
|
86
185
|
}
|
|
87
186
|
|
|
187
|
+
// src/server/utils/refuse-object.ts
|
|
188
|
+
function refuseObject(who, path, value) {
|
|
189
|
+
throw realtimeError(
|
|
190
|
+
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
191
|
+
`${path} must be an object, received ${describeReceived(value)}`,
|
|
192
|
+
{ who }
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// src/server/config/validate-shapes.ts
|
|
197
|
+
var REQUIRED_FUNCTIONS = {
|
|
198
|
+
pubsub: ["publish", "subscribe"],
|
|
199
|
+
offlineQueue: ["append", "retrieveSince", "acknowledge"],
|
|
200
|
+
presence: ["setOnline", "setOffline", "isOnline", "listOnlineByTenant", "countOnline"]
|
|
201
|
+
};
|
|
202
|
+
var HOOK_NAMES = [
|
|
203
|
+
"onConnect",
|
|
204
|
+
"onDisconnect",
|
|
205
|
+
"onError",
|
|
206
|
+
"onReauthenticationFailed",
|
|
207
|
+
"onDeliveryFailure"
|
|
208
|
+
];
|
|
209
|
+
var GROUP_NAMES = ["sse", "websocket", "reauthenticationPolicy"];
|
|
210
|
+
function refuse(path, expected, received) {
|
|
211
|
+
throw realtimeError(
|
|
212
|
+
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
213
|
+
`options.${path} must be ${expected}, received ${describeReceived(received)}`,
|
|
214
|
+
{ who: "BymaxRealtimeModule" }
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
function assertObject(path, value) {
|
|
218
|
+
if (value !== void 0 && !isPlainObject(value)) refuse(path, "an object", value);
|
|
219
|
+
}
|
|
220
|
+
function assertFunction(path, value) {
|
|
221
|
+
if (typeof value !== "function") refuse(path, "a function", value);
|
|
222
|
+
}
|
|
223
|
+
function assertOptionalFunction(path, value) {
|
|
224
|
+
if (value !== void 0 && value !== null) assertFunction(path, value);
|
|
225
|
+
}
|
|
226
|
+
function assertBoolean(path, value) {
|
|
227
|
+
if (value !== void 0 && typeof value !== "boolean") refuse(path, "a boolean", value);
|
|
228
|
+
}
|
|
229
|
+
function assertSseEndpoint(path, value) {
|
|
230
|
+
if (value === void 0 || value === null) return;
|
|
231
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
232
|
+
refuse(path, "a non-blank string", value);
|
|
233
|
+
}
|
|
234
|
+
if (/\s/.test(value)) refuse(path, "a string without whitespace", value);
|
|
235
|
+
}
|
|
236
|
+
function assertOptionsObject(value, who = "BymaxRealtimeModule") {
|
|
237
|
+
if (!isPlainObject(value)) refuseObject(who, "options", value);
|
|
238
|
+
}
|
|
239
|
+
function validatePieces(options) {
|
|
240
|
+
for (const [name, functions] of Object.entries(REQUIRED_FUNCTIONS)) {
|
|
241
|
+
const piece = options[name];
|
|
242
|
+
assertObject(name, piece);
|
|
243
|
+
if (piece === void 0) continue;
|
|
244
|
+
for (const fn of functions)
|
|
245
|
+
assertFunction(`${name}.${fn}`, piece[fn]);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function validateHooks(options) {
|
|
249
|
+
const hooks = options.hooks;
|
|
250
|
+
assertObject("hooks", hooks);
|
|
251
|
+
if (hooks === void 0) return;
|
|
252
|
+
for (const hook of HOOK_NAMES)
|
|
253
|
+
assertOptionalFunction(`hooks.${hook}`, hooks[hook]);
|
|
254
|
+
}
|
|
255
|
+
function validateShapes(options) {
|
|
256
|
+
for (const group of GROUP_NAMES) assertObject(group, options[group]);
|
|
257
|
+
validatePieces(options);
|
|
258
|
+
validateHooks(options);
|
|
259
|
+
assertOptionalFunction("tenantResolver", options.tenantResolver);
|
|
260
|
+
assertBoolean("sse.emitConnectionEvent", options.sse?.emitConnectionEvent);
|
|
261
|
+
assertBoolean("websocket.emitConnectionEvent", options.websocket?.emitConnectionEvent);
|
|
262
|
+
assertSseEndpoint("sse.endpoint", options.sse?.endpoint);
|
|
263
|
+
}
|
|
264
|
+
|
|
88
265
|
// src/server/config/validate-options.ts
|
|
89
266
|
var VALID_TRANSPORTS = /* @__PURE__ */ new Set(["sse", "websocket", "both"]);
|
|
90
|
-
function validateOptions(
|
|
267
|
+
function validateOptions(raw) {
|
|
268
|
+
assertOptionsObject(raw);
|
|
269
|
+
const options = dropUnset(raw);
|
|
91
270
|
if (!options.transport || !VALID_TRANSPORTS.has(options.transport)) {
|
|
92
271
|
throw realtimeError(
|
|
93
272
|
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
94
|
-
`options.transport must be one of 'sse' | 'websocket' | 'both'
|
|
273
|
+
`options.transport must be one of 'sse' | 'websocket' | 'both', received ${describeReceived(options.transport)}`,
|
|
95
274
|
{ who: "BymaxRealtimeModule" }
|
|
96
275
|
);
|
|
97
276
|
}
|
|
@@ -109,6 +288,7 @@ function validateOptions(options) {
|
|
|
109
288
|
{ who: "BymaxRealtimeModule" }
|
|
110
289
|
);
|
|
111
290
|
}
|
|
291
|
+
validateShapes(options);
|
|
112
292
|
validateHookTimeout(options);
|
|
113
293
|
validateOnConnectFailure(options);
|
|
114
294
|
validateSseOptions(options);
|
|
@@ -124,7 +304,7 @@ function assertInteger(path, value, least, most) {
|
|
|
124
304
|
if (valid) return;
|
|
125
305
|
throw realtimeError(
|
|
126
306
|
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
127
|
-
`options.${path} must be an integer >= ${least}${most === void 0 ? "" : ` and <= ${most}`},
|
|
307
|
+
`options.${path} must be an integer >= ${least}${most === void 0 ? "" : ` and <= ${most}`}, received ${describeReceived(value)}${most === void 0 && Number.isInteger(value) && !Number.isSafeInteger(value) ? ` (the most is ${Number.MAX_SAFE_INTEGER})` : ""}`,
|
|
128
308
|
{ who: "BymaxRealtimeModule" }
|
|
129
309
|
);
|
|
130
310
|
}
|
|
@@ -133,7 +313,7 @@ function validateOnConnectFailure(options) {
|
|
|
133
313
|
if (value === void 0 || value === "ignore" || value === "close") return;
|
|
134
314
|
throw realtimeError(
|
|
135
315
|
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
136
|
-
`options.onConnectFailure must be 'ignore' or 'close',
|
|
316
|
+
`options.onConnectFailure must be 'ignore' or 'close', received ${describeReceived(value)}`,
|
|
137
317
|
{ who: "BymaxRealtimeModule" }
|
|
138
318
|
);
|
|
139
319
|
}
|
|
@@ -172,16 +352,10 @@ function validateReauthPolicy(options) {
|
|
|
172
352
|
if (onFailure !== void 0 && onFailure !== "disconnect" && onFailure !== "event") {
|
|
173
353
|
throw realtimeError(
|
|
174
354
|
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
175
|
-
`options.reauthenticationPolicy.onFailure must be 'disconnect' or 'event',
|
|
355
|
+
`options.reauthenticationPolicy.onFailure must be 'disconnect' or 'event', received ${describeReceived(onFailure)}`,
|
|
176
356
|
{ who: "BymaxRealtimeModule" }
|
|
177
357
|
);
|
|
178
358
|
}
|
|
179
|
-
if (policy?.cacheTtlMs !== void 0 && policy.intervalSeconds !== void 0 && policy.cacheTtlMs > policy.intervalSeconds * 1e3) {
|
|
180
|
-
common.Logger.warn(
|
|
181
|
-
`[BymaxRealtimeModule] reauthenticationPolicy.cacheTtlMs (${policy.cacheTtlMs} ms) exceeds intervalSeconds*1000 (${policy.intervalSeconds * 1e3} ms): the positive reauth cache outlives the interval, so revalidate is skipped on ticks within the cache window \u2014 reauthentication is effectively weakened`,
|
|
182
|
-
"BymaxRealtimeModule"
|
|
183
|
-
);
|
|
184
|
-
}
|
|
185
359
|
}
|
|
186
360
|
function validateSseOptions(options) {
|
|
187
361
|
const sse = options.sse;
|
|
@@ -189,7 +363,7 @@ function validateSseOptions(options) {
|
|
|
189
363
|
if (heartbeatMs !== void 0 && !(typeof heartbeatMs === "number" && heartbeatMs >= HEARTBEAT_MIN_MS && heartbeatMs <= HEARTBEAT_MAX_MS)) {
|
|
190
364
|
throw realtimeError(
|
|
191
365
|
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
192
|
-
`options.sse.heartbeatMs must be between ${HEARTBEAT_MIN_MS} and ${HEARTBEAT_MAX_MS} ms
|
|
366
|
+
`options.sse.heartbeatMs must be between ${HEARTBEAT_MIN_MS} and ${HEARTBEAT_MAX_MS} ms, received ${describeReceived(heartbeatMs)}`,
|
|
193
367
|
{ who: "BymaxRealtimeModule" }
|
|
194
368
|
);
|
|
195
369
|
}
|
|
@@ -201,7 +375,7 @@ function validateSseOptions(options) {
|
|
|
201
375
|
if (value !== void 0 && !(typeof value === "number" && Number.isSafeInteger(value) && value >= 0)) {
|
|
202
376
|
throw realtimeError(
|
|
203
377
|
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
204
|
-
`options.sse.${name} must be an integer >= 0 (0 switches the bound off),
|
|
378
|
+
`options.sse.${name} must be an integer >= 0 (0 switches the bound off), received ${describeReceived(value)}`,
|
|
205
379
|
{ who: "BymaxRealtimeModule" }
|
|
206
380
|
);
|
|
207
381
|
}
|
|
@@ -213,6 +387,138 @@ function validateSseOptions(options) {
|
|
|
213
387
|
MAX_PER_USER_CONNECTIONS
|
|
214
388
|
);
|
|
215
389
|
}
|
|
390
|
+
var REFUSAL_REASONS = ["limit", "unauthorized", "unavailable", "error"];
|
|
391
|
+
var EVICTION_REASONS = ["per_user_cap", "slow_consumer", "revoked"];
|
|
392
|
+
var TALLY_TRANSPORTS = ["sse", "websocket"];
|
|
393
|
+
function zeroed(keys) {
|
|
394
|
+
return Object.fromEntries(keys.map((key) => [key, 0]));
|
|
395
|
+
}
|
|
396
|
+
var _refused, _evicted, _emitted, _slowConsumerDrops;
|
|
397
|
+
exports.RealtimeMetricsTally = class RealtimeMetricsTally {
|
|
398
|
+
constructor() {
|
|
399
|
+
__privateAdd(this, _refused, zeroed(REFUSAL_REASONS));
|
|
400
|
+
__privateAdd(this, _evicted, zeroed(EVICTION_REASONS));
|
|
401
|
+
__privateAdd(this, _emitted, zeroed(TALLY_TRANSPORTS));
|
|
402
|
+
__privateAdd(this, _slowConsumerDrops, 0);
|
|
403
|
+
}
|
|
404
|
+
refused(reason) {
|
|
405
|
+
__privateGet(this, _refused)[reason] += 1;
|
|
406
|
+
}
|
|
407
|
+
evicted(reason) {
|
|
408
|
+
__privateGet(this, _evicted)[reason] += 1;
|
|
409
|
+
}
|
|
410
|
+
emitted(transport) {
|
|
411
|
+
__privateGet(this, _emitted)[transport] += 1;
|
|
412
|
+
}
|
|
413
|
+
slowConsumerDropped() {
|
|
414
|
+
__privateSet(this, _slowConsumerDrops, __privateGet(this, _slowConsumerDrops) + 1);
|
|
415
|
+
}
|
|
416
|
+
snapshot() {
|
|
417
|
+
return {
|
|
418
|
+
refused: { ...__privateGet(this, _refused) },
|
|
419
|
+
evicted: { ...__privateGet(this, _evicted) },
|
|
420
|
+
emitted: { ...__privateGet(this, _emitted) },
|
|
421
|
+
slowConsumerDrops: __privateGet(this, _slowConsumerDrops)
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
_refused = new WeakMap();
|
|
426
|
+
_evicted = new WeakMap();
|
|
427
|
+
_emitted = new WeakMap();
|
|
428
|
+
_slowConsumerDrops = new WeakMap();
|
|
429
|
+
exports.RealtimeMetricsTally = __decorateClass([
|
|
430
|
+
common.Injectable()
|
|
431
|
+
], exports.RealtimeMetricsTally);
|
|
432
|
+
var STATUS_ERROR = 2;
|
|
433
|
+
var TRACER_NAME = "@bymax-one/nest-realtime";
|
|
434
|
+
var LIBRARY_CODE = /^REALTIME_[A-Z_]+$/;
|
|
435
|
+
function codeOf(error) {
|
|
436
|
+
if (typeof error !== "object" || error === null) return "REALTIME_ERROR";
|
|
437
|
+
const { code, message, data } = error;
|
|
438
|
+
for (const candidate of [code, data?.code, message]) {
|
|
439
|
+
if (typeof candidate === "string" && LIBRARY_CODE.test(candidate)) return candidate;
|
|
440
|
+
}
|
|
441
|
+
return "REALTIME_ERROR";
|
|
442
|
+
}
|
|
443
|
+
var _api;
|
|
444
|
+
exports.RealtimeTracing = class RealtimeTracing {
|
|
445
|
+
constructor() {
|
|
446
|
+
__privateAdd(this, _api);
|
|
447
|
+
}
|
|
448
|
+
async onModuleInit() {
|
|
449
|
+
try {
|
|
450
|
+
__privateSet(this, _api, await import('@opentelemetry/api'));
|
|
451
|
+
} catch {
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
async traced(span, run) {
|
|
455
|
+
let result;
|
|
456
|
+
try {
|
|
457
|
+
result = await run();
|
|
458
|
+
} catch (error) {
|
|
459
|
+
attempt(() => span.setStatus({ code: STATUS_ERROR, message: codeOf(error) }));
|
|
460
|
+
attempt(() => span.end());
|
|
461
|
+
throw error;
|
|
462
|
+
}
|
|
463
|
+
attempt(() => span.end());
|
|
464
|
+
return result;
|
|
465
|
+
}
|
|
466
|
+
span(name, attributes, run) {
|
|
467
|
+
const api = __privateGet(this, _api);
|
|
468
|
+
if (!api) return run();
|
|
469
|
+
const latch = { claimed: false };
|
|
470
|
+
attempt(
|
|
471
|
+
() => api.trace.getTracer(TRACER_NAME).startActiveSpan(name, { attributes }, (span) => {
|
|
472
|
+
if (!latch.claimed) {
|
|
473
|
+
latch.claimed = true;
|
|
474
|
+
latch.started = this.traced(span, run);
|
|
475
|
+
}
|
|
476
|
+
return latch.started;
|
|
477
|
+
})
|
|
478
|
+
);
|
|
479
|
+
if (latch.started) return latch.started;
|
|
480
|
+
latch.claimed = true;
|
|
481
|
+
return run();
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
_api = new WeakMap();
|
|
485
|
+
exports.RealtimeTracing = __decorateClass([
|
|
486
|
+
common.Injectable()
|
|
487
|
+
], exports.RealtimeTracing);
|
|
488
|
+
function attempt(call) {
|
|
489
|
+
try {
|
|
490
|
+
Promise.resolve(call()).catch(() => void 0);
|
|
491
|
+
} catch {
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// src/server/utils/log-event.ts
|
|
496
|
+
var REALTIME_LOG_EVENTS = [
|
|
497
|
+
"REALTIME_REAUTH_CACHE_WEAKENS_POLICY",
|
|
498
|
+
"REALTIME_HANDSHAKE_FAILED",
|
|
499
|
+
"REALTIME_HOOK_FAILED",
|
|
500
|
+
"REALTIME_DELIVERY_FAILED",
|
|
501
|
+
"REALTIME_SLOW_CONSUMER",
|
|
502
|
+
"REALTIME_DISCONNECT_FAILED",
|
|
503
|
+
"REALTIME_RECONNECTS_GIVEN_UP",
|
|
504
|
+
"REALTIME_OFFLINE_QUEUE_LIMIT",
|
|
505
|
+
"REALTIME_OFFLINE_QUEUE_ACK_FAILED",
|
|
506
|
+
"REALTIME_PUBSUB_NOT_PROVIDED",
|
|
507
|
+
"REALTIME_PUBSUB_UNSUBSCRIBE_FAILED",
|
|
508
|
+
"REALTIME_PUBSUB_DROPPED",
|
|
509
|
+
"REALTIME_PUBSUB_UNAVAILABLE",
|
|
510
|
+
"REALTIME_CONNECTIONS_REFUSED",
|
|
511
|
+
"REALTIME_HANDSHAKES_REFUSED",
|
|
512
|
+
"REALTIME_REAUTH_FAILED",
|
|
513
|
+
"REALTIME_REVOCATION_FAILED",
|
|
514
|
+
"REALTIME_ADAPTER_MISSING",
|
|
515
|
+
"REALTIME_ADAPTER_COMMAND_FAILED",
|
|
516
|
+
"REALTIME_COMPOSITE_PARTIAL_FAILURE",
|
|
517
|
+
"REALTIME_SERVER_NOT_ATTACHED"
|
|
518
|
+
];
|
|
519
|
+
function logEvent(logger, level, event, message, fields = {}, error) {
|
|
520
|
+
logger[level]({ event, message, ...fields, ...error === void 0 ? {} : { err: error } });
|
|
521
|
+
}
|
|
216
522
|
|
|
217
523
|
// src/server/utils/describe-error.ts
|
|
218
524
|
function describeError(reason) {
|
|
@@ -226,13 +532,32 @@ function describeError(reason) {
|
|
|
226
532
|
|
|
227
533
|
// src/server/utils/report-delivery-failure.ts
|
|
228
534
|
function reportDeliveryFailure(logger, hooks, failure, what) {
|
|
229
|
-
|
|
535
|
+
logEvent(
|
|
536
|
+
logger,
|
|
537
|
+
"warn",
|
|
538
|
+
"REALTIME_DELIVERY_FAILED",
|
|
539
|
+
`${what}`,
|
|
540
|
+
{
|
|
541
|
+
code: failure.code,
|
|
542
|
+
op: failure.op
|
|
543
|
+
},
|
|
544
|
+
failure.error
|
|
545
|
+
);
|
|
230
546
|
const reported = {
|
|
231
547
|
...failure,
|
|
232
548
|
error: failure.error instanceof Error ? failure.error : new Error(describeError(failure.error))
|
|
233
549
|
};
|
|
234
550
|
void Promise.resolve().then(() => hooks?.onDeliveryFailure?.(reported)).catch((err) => {
|
|
235
|
-
|
|
551
|
+
logEvent(
|
|
552
|
+
logger,
|
|
553
|
+
"warn",
|
|
554
|
+
"REALTIME_HOOK_FAILED",
|
|
555
|
+
`onDeliveryFailure hook failed`,
|
|
556
|
+
{
|
|
557
|
+
hook: "onDeliveryFailure"
|
|
558
|
+
},
|
|
559
|
+
err
|
|
560
|
+
);
|
|
236
561
|
});
|
|
237
562
|
}
|
|
238
563
|
|
|
@@ -277,8 +602,12 @@ exports.OfflineQueueDeliveryService = class OfflineQueueDeliveryService {
|
|
|
277
602
|
timeouts = windowedReport(
|
|
278
603
|
() => 0,
|
|
279
604
|
(count) => {
|
|
280
|
-
|
|
281
|
-
|
|
605
|
+
logEvent(
|
|
606
|
+
this.logger,
|
|
607
|
+
"warn",
|
|
608
|
+
"REALTIME_RECONNECTS_GIVEN_UP",
|
|
609
|
+
`reconnects given up on since the last report: ${count}, because the offline queue did not answer in ${this.timeoutMs} ms or already had ${this.maxPending} reads waiting`,
|
|
610
|
+
{ count, timeoutMs: this.timeoutMs, maxPending: this.maxPending }
|
|
282
611
|
);
|
|
283
612
|
}
|
|
284
613
|
);
|
|
@@ -307,8 +636,12 @@ exports.OfflineQueueDeliveryService = class OfflineQueueDeliveryService {
|
|
|
307
636
|
return { events: [], degraded: true };
|
|
308
637
|
}
|
|
309
638
|
if (events.length === RETRIEVE_LIMIT) {
|
|
310
|
-
|
|
311
|
-
|
|
639
|
+
logEvent(
|
|
640
|
+
this.logger,
|
|
641
|
+
"warn",
|
|
642
|
+
"REALTIME_OFFLINE_QUEUE_LIMIT",
|
|
643
|
+
`Offline queue retrieve hit the ${RETRIEVE_LIMIT}-event limit for user; older gap events may be dropped.`,
|
|
644
|
+
{ limit: RETRIEVE_LIMIT }
|
|
312
645
|
);
|
|
313
646
|
}
|
|
314
647
|
return { events, degraded: false };
|
|
@@ -319,7 +652,14 @@ exports.OfflineQueueDeliveryService = class OfflineQueueDeliveryService {
|
|
|
319
652
|
try {
|
|
320
653
|
await this.storage.acknowledge(userId, lastId);
|
|
321
654
|
} catch (err) {
|
|
322
|
-
|
|
655
|
+
logEvent(
|
|
656
|
+
this.logger,
|
|
657
|
+
"warn",
|
|
658
|
+
"REALTIME_OFFLINE_QUEUE_ACK_FAILED",
|
|
659
|
+
`Offline queue acknowledge failed`,
|
|
660
|
+
{},
|
|
661
|
+
err
|
|
662
|
+
);
|
|
323
663
|
}
|
|
324
664
|
}
|
|
325
665
|
async retrieveAppended(userId, lastEventId, read) {
|
|
@@ -426,7 +766,7 @@ function refuseReservedRoom(operation, roomId) {
|
|
|
426
766
|
}
|
|
427
767
|
|
|
428
768
|
// src/server/utils/admit-rooms.ts
|
|
429
|
-
function
|
|
769
|
+
function refuse2(detail) {
|
|
430
770
|
throw realtimeError(
|
|
431
771
|
REALTIME_ERROR_CODES.AUTH_FAILED,
|
|
432
772
|
`the authenticator returned rooms that are not valid: ${detail}. Fix the authenticator; the limits are bounds against abuse and apply to this field only.`
|
|
@@ -435,11 +775,11 @@ function refuse(detail) {
|
|
|
435
775
|
function admitRooms(auth) {
|
|
436
776
|
const { rooms } = auth;
|
|
437
777
|
if (rooms === void 0) return auth;
|
|
438
|
-
if (!Array.isArray(rooms))
|
|
778
|
+
if (!Array.isArray(rooms)) refuse2("`rooms` is not an array");
|
|
439
779
|
const admitted = [];
|
|
440
780
|
for (const room of rooms) {
|
|
441
781
|
if (admitted.length === ADMISSION_ROOM_LIMITS.MAX_ROOMS) {
|
|
442
|
-
|
|
782
|
+
refuse2(
|
|
443
783
|
`more than ${ADMISSION_ROOM_LIMITS.MAX_ROOMS} rooms: the limit is ${ADMISSION_ROOM_LIMITS.MAX_ROOMS}`
|
|
444
784
|
);
|
|
445
785
|
}
|
|
@@ -447,13 +787,13 @@ function admitRooms(auth) {
|
|
|
447
787
|
}
|
|
448
788
|
for (const room of admitted) {
|
|
449
789
|
if (typeof room !== "string" || room.trim().length === 0) {
|
|
450
|
-
|
|
790
|
+
refuse2("every room must be a non-blank string");
|
|
451
791
|
}
|
|
452
792
|
if (room.length > ADMISSION_ROOM_LIMITS.MAX_ROOM_NAME_LENGTH) {
|
|
453
|
-
|
|
793
|
+
refuse2(`a room name is longer than ${ADMISSION_ROOM_LIMITS.MAX_ROOM_NAME_LENGTH} characters`);
|
|
454
794
|
}
|
|
455
795
|
const reserved = reservedRoomPrefix(room);
|
|
456
|
-
if (reserved !== void 0)
|
|
796
|
+
if (reserved !== void 0) refuse2(`a room name starts with the reserved prefix "${reserved}"`);
|
|
457
797
|
}
|
|
458
798
|
return { ...auth, rooms: Object.freeze(admitted) };
|
|
459
799
|
}
|
|
@@ -525,6 +865,7 @@ var AuthenticationUnavailableError = class extends Error {
|
|
|
525
865
|
this.reason = reason;
|
|
526
866
|
}
|
|
527
867
|
reason;
|
|
868
|
+
code = REALTIME_ERROR_CODES.TRANSPORT_UNAVAILABLE;
|
|
528
869
|
};
|
|
529
870
|
exports.AuthenticationGate = class AuthenticationGate {
|
|
530
871
|
constructor(options, authenticator) {
|
|
@@ -540,8 +881,17 @@ exports.AuthenticationGate = class AuthenticationGate {
|
|
|
540
881
|
refusals = windowedReport(
|
|
541
882
|
() => ({ saturated: 0, timeout: 0 }),
|
|
542
883
|
({ saturated, timeout }) => {
|
|
543
|
-
|
|
544
|
-
|
|
884
|
+
logEvent(
|
|
885
|
+
this.logger,
|
|
886
|
+
"warn",
|
|
887
|
+
"REALTIME_HANDSHAKES_REFUSED",
|
|
888
|
+
`handshakes refused since the last report: ${saturated} because ${this.maxPending} were already waiting for the authenticator, ${timeout} because it did not answer in ${this.timeoutMs} ms`,
|
|
889
|
+
{
|
|
890
|
+
saturated,
|
|
891
|
+
timedOut: timeout,
|
|
892
|
+
maxPendingHandshakes: this.maxPending,
|
|
893
|
+
authenticateTimeoutMs: this.timeoutMs
|
|
894
|
+
}
|
|
545
895
|
);
|
|
546
896
|
}
|
|
547
897
|
);
|
|
@@ -657,6 +1007,7 @@ var ConnectionLimitError = class extends Error {
|
|
|
657
1007
|
this.limit = limit;
|
|
658
1008
|
}
|
|
659
1009
|
limit;
|
|
1010
|
+
code = REALTIME_ERROR_CODES.TRANSPORT_UNAVAILABLE;
|
|
660
1011
|
};
|
|
661
1012
|
var UNBOUNDED_TICKET = {
|
|
662
1013
|
release: () => void 0,
|
|
@@ -676,8 +1027,12 @@ exports.ConnectionAdmission = class ConnectionAdmission {
|
|
|
676
1027
|
refusals = windowedReport(
|
|
677
1028
|
() => 0,
|
|
678
1029
|
(count) => {
|
|
679
|
-
|
|
680
|
-
|
|
1030
|
+
logEvent(
|
|
1031
|
+
this.logger,
|
|
1032
|
+
"warn",
|
|
1033
|
+
"REALTIME_CONNECTIONS_REFUSED",
|
|
1034
|
+
`connections refused since the last report: ${count}, because the instance already holds ${this.max} (maxConnections)`,
|
|
1035
|
+
{ count, maxConnections: this.max }
|
|
681
1036
|
);
|
|
682
1037
|
}
|
|
683
1038
|
);
|
|
@@ -926,7 +1281,7 @@ exports.HeartbeatService = class HeartbeatService {
|
|
|
926
1281
|
if (intervalMs < HEARTBEAT_MIN_MS || intervalMs > HEARTBEAT_MAX_MS) {
|
|
927
1282
|
throw realtimeError(
|
|
928
1283
|
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
929
|
-
`heartbeatMs must be between ${HEARTBEAT_MIN_MS} and ${HEARTBEAT_MAX_MS} ms
|
|
1284
|
+
`heartbeatMs must be between ${HEARTBEAT_MIN_MS} and ${HEARTBEAT_MAX_MS} ms, received ${describeReceived(intervalMs)}`
|
|
930
1285
|
);
|
|
931
1286
|
}
|
|
932
1287
|
this.stop(connectionId);
|
|
@@ -1061,7 +1416,7 @@ var StallWatch = class {
|
|
|
1061
1416
|
// src/server/transports/sse/sse.transport.ts
|
|
1062
1417
|
var _shuttingDown;
|
|
1063
1418
|
exports.SseTransport = class SseTransport {
|
|
1064
|
-
constructor(connections, rooms, replayBuffer, idGen, heartbeat, gate, pubsub, hooks, options, instanceId, offlineQueue, admission) {
|
|
1419
|
+
constructor(connections, rooms, replayBuffer, idGen, heartbeat, gate, pubsub, hooks, options, instanceId, offlineQueue, admission, tally) {
|
|
1065
1420
|
this.connections = connections;
|
|
1066
1421
|
this.rooms = rooms;
|
|
1067
1422
|
this.replayBuffer = replayBuffer;
|
|
@@ -1074,6 +1429,7 @@ exports.SseTransport = class SseTransport {
|
|
|
1074
1429
|
this.instanceId = instanceId;
|
|
1075
1430
|
this.offlineQueue = offlineQueue;
|
|
1076
1431
|
this.admission = admission;
|
|
1432
|
+
this.tally = tally;
|
|
1077
1433
|
__publicField(this, "kind", "sse");
|
|
1078
1434
|
__publicField(this, "logger", new common.Logger(exports.SseTransport.name));
|
|
1079
1435
|
__privateAdd(this, _shuttingDown, false);
|
|
@@ -1091,6 +1447,7 @@ exports.SseTransport = class SseTransport {
|
|
|
1091
1447
|
instanceId;
|
|
1092
1448
|
offlineQueue;
|
|
1093
1449
|
admission;
|
|
1450
|
+
tally;
|
|
1094
1451
|
onModuleDestroy() {
|
|
1095
1452
|
__privateSet(this, _shuttingDown, true);
|
|
1096
1453
|
}
|
|
@@ -1177,7 +1534,18 @@ exports.SseTransport = class SseTransport {
|
|
|
1177
1534
|
}),
|
|
1178
1535
|
this.options.hookTimeoutMs ?? DEFAULT_HOOK_TIMEOUT_MS
|
|
1179
1536
|
);
|
|
1180
|
-
if (failed)
|
|
1537
|
+
if (failed) {
|
|
1538
|
+
logEvent(
|
|
1539
|
+
this.logger,
|
|
1540
|
+
"error",
|
|
1541
|
+
"REALTIME_HOOK_FAILED",
|
|
1542
|
+
`onDisconnect hook failed`,
|
|
1543
|
+
{
|
|
1544
|
+
hook: "onDisconnect"
|
|
1545
|
+
},
|
|
1546
|
+
failed.error
|
|
1547
|
+
);
|
|
1548
|
+
}
|
|
1181
1549
|
}
|
|
1182
1550
|
getReplayEvents(userId, lastEventId) {
|
|
1183
1551
|
return this.replayBuffer.since(userId, lastEventId);
|
|
@@ -1279,15 +1647,18 @@ exports.SseTransport = class SseTransport {
|
|
|
1279
1647
|
}
|
|
1280
1648
|
emitToUserLocal(userId, event, data, id) {
|
|
1281
1649
|
const message = this.buildMessage(id, event, data);
|
|
1650
|
+
this.tally?.emitted("sse");
|
|
1282
1651
|
this.replayBuffer.append(userId, message);
|
|
1283
1652
|
for (const conn of this.connections.byUser(userId, "sse")) this.deliver(conn, message);
|
|
1284
1653
|
}
|
|
1285
1654
|
emitToTenantLocal(tenantId, event, data, id) {
|
|
1286
1655
|
const message = this.buildMessage(id, event, data);
|
|
1656
|
+
this.tally?.emitted("sse");
|
|
1287
1657
|
for (const conn of this.connections.byTenant(tenantId, "sse")) this.deliver(conn, message);
|
|
1288
1658
|
}
|
|
1289
1659
|
emitToRoomLocal(roomId, event, data, id) {
|
|
1290
1660
|
const message = this.buildMessage(id, event, data);
|
|
1661
|
+
this.tally?.emitted("sse");
|
|
1291
1662
|
for (const connectionId of this.rooms.members(roomId)) {
|
|
1292
1663
|
const conn = this.connections.get(connectionId);
|
|
1293
1664
|
if (conn && conn.transport === "sse") this.deliver(conn, message);
|
|
@@ -1295,6 +1666,7 @@ exports.SseTransport = class SseTransport {
|
|
|
1295
1666
|
}
|
|
1296
1667
|
broadcastLocal(event, data, id) {
|
|
1297
1668
|
const message = this.buildMessage(id, event, data);
|
|
1669
|
+
this.tally?.emitted("sse");
|
|
1298
1670
|
for (const conn of this.connections.allByTransport("sse")) this.deliver(conn, message);
|
|
1299
1671
|
}
|
|
1300
1672
|
async disconnectLocal(connectionId, reason) {
|
|
@@ -1317,7 +1689,16 @@ exports.SseTransport = class SseTransport {
|
|
|
1317
1689
|
}
|
|
1318
1690
|
conn.subject?.next(message);
|
|
1319
1691
|
} catch (error) {
|
|
1320
|
-
|
|
1692
|
+
logEvent(
|
|
1693
|
+
this.logger,
|
|
1694
|
+
"warn",
|
|
1695
|
+
"REALTIME_DELIVERY_FAILED",
|
|
1696
|
+
`SSE delivery failed`,
|
|
1697
|
+
{
|
|
1698
|
+
connectionId: conn.connectionId
|
|
1699
|
+
},
|
|
1700
|
+
error
|
|
1701
|
+
);
|
|
1321
1702
|
}
|
|
1322
1703
|
}
|
|
1323
1704
|
track(params) {
|
|
@@ -1344,13 +1725,23 @@ exports.SseTransport = class SseTransport {
|
|
|
1344
1725
|
}
|
|
1345
1726
|
}
|
|
1346
1727
|
cutSlowConsumer(connectionId, why, tracked) {
|
|
1347
|
-
this.
|
|
1348
|
-
|
|
1349
|
-
|
|
1728
|
+
this.tally?.evicted("slow_consumer");
|
|
1729
|
+
this.tally?.slowConsumerDropped();
|
|
1730
|
+
logEvent(this.logger, "warn", "REALTIME_SLOW_CONSUMER", `closing an SSE connection, ${why}`, {
|
|
1731
|
+
connectionId,
|
|
1732
|
+
queuedEvents: tracked.backlog.queuedEvents,
|
|
1733
|
+
queuedBytes: tracked.backlog.queuedBytes,
|
|
1734
|
+
heldBytes: tracked.probe.buffered()
|
|
1735
|
+
});
|
|
1350
1736
|
this.disconnectLocal(connectionId, REALTIME_ERROR_CODES.SLOW_CONSUMER).catch(
|
|
1351
1737
|
(error) => {
|
|
1352
|
-
|
|
1353
|
-
|
|
1738
|
+
logEvent(
|
|
1739
|
+
this.logger,
|
|
1740
|
+
"warn",
|
|
1741
|
+
"REALTIME_DISCONNECT_FAILED",
|
|
1742
|
+
`Closing a slow SSE connection failed`,
|
|
1743
|
+
{ connectionId },
|
|
1744
|
+
error
|
|
1354
1745
|
);
|
|
1355
1746
|
}
|
|
1356
1747
|
);
|
|
@@ -1381,6 +1772,7 @@ exports.SseTransport = class SseTransport {
|
|
|
1381
1772
|
let userConnections = this.connections.byUser(userId, "sse");
|
|
1382
1773
|
while (userConnections.length > max) {
|
|
1383
1774
|
const oldest = userConnections.reduce((a, b) => a.connectedAt <= b.connectedAt ? a : b);
|
|
1775
|
+
this.tally?.evicted("per_user_cap");
|
|
1384
1776
|
await this.disconnectLocal(oldest.connectionId, REALTIME_ERROR_CODES.TOO_MANY_CONNECTIONS);
|
|
1385
1777
|
userConnections = this.connections.byUser(userId, "sse");
|
|
1386
1778
|
}
|
|
@@ -1402,7 +1794,9 @@ exports.SseTransport = __decorateClass([
|
|
|
1402
1794
|
__decorateParam(10, common.Optional()),
|
|
1403
1795
|
__decorateParam(10, common.Inject(REALTIME_OFFLINE_QUEUE_TOKEN)),
|
|
1404
1796
|
__decorateParam(11, common.Optional()),
|
|
1405
|
-
__decorateParam(11, common.Inject(exports.ConnectionAdmission))
|
|
1797
|
+
__decorateParam(11, common.Inject(exports.ConnectionAdmission)),
|
|
1798
|
+
__decorateParam(12, common.Optional()),
|
|
1799
|
+
__decorateParam(12, common.Inject(exports.RealtimeMetricsTally))
|
|
1406
1800
|
], exports.SseTransport);
|
|
1407
1801
|
|
|
1408
1802
|
// src/server/transports/sse/response-close.ts
|
|
@@ -1476,18 +1870,22 @@ function buildMeta(record) {
|
|
|
1476
1870
|
};
|
|
1477
1871
|
}
|
|
1478
1872
|
exports.SseSubscriptionHandler = class SseSubscriptionHandler {
|
|
1479
|
-
constructor(transport, heartbeat, options, hooks, offlineDelivery) {
|
|
1873
|
+
constructor(transport, heartbeat, options, hooks, offlineDelivery, tally, tracing) {
|
|
1480
1874
|
this.transport = transport;
|
|
1481
1875
|
this.heartbeat = heartbeat;
|
|
1482
1876
|
this.options = options;
|
|
1483
1877
|
this.hooks = hooks;
|
|
1484
1878
|
this.offlineDelivery = offlineDelivery;
|
|
1879
|
+
this.tally = tally;
|
|
1880
|
+
this.tracing = tracing;
|
|
1485
1881
|
}
|
|
1486
1882
|
transport;
|
|
1487
1883
|
heartbeat;
|
|
1488
1884
|
options;
|
|
1489
1885
|
hooks;
|
|
1490
1886
|
offlineDelivery;
|
|
1887
|
+
tally;
|
|
1888
|
+
tracing;
|
|
1491
1889
|
logger = new common.Logger(exports.SseSubscriptionHandler.name);
|
|
1492
1890
|
authenticated = /* @__PURE__ */ new WeakMap();
|
|
1493
1891
|
resolveAndValidateIdentity(auth) {
|
|
@@ -1496,7 +1894,15 @@ exports.SseSubscriptionHandler = class SseSubscriptionHandler {
|
|
|
1496
1894
|
assertIdentityIsNotBlank(resolvedAuth);
|
|
1497
1895
|
resolvedAuth = admitRooms(resolvedAuth);
|
|
1498
1896
|
} catch (err) {
|
|
1499
|
-
this.
|
|
1897
|
+
this.tally?.refused("error");
|
|
1898
|
+
logEvent(
|
|
1899
|
+
this.logger,
|
|
1900
|
+
"error",
|
|
1901
|
+
"REALTIME_HANDSHAKE_FAILED",
|
|
1902
|
+
`SSE connection refused \u2014 responding 500`,
|
|
1903
|
+
{},
|
|
1904
|
+
err
|
|
1905
|
+
);
|
|
1500
1906
|
throw new common.InternalServerErrorException();
|
|
1501
1907
|
}
|
|
1502
1908
|
return resolvedAuth;
|
|
@@ -1531,6 +1937,7 @@ exports.SseSubscriptionHandler = class SseSubscriptionHandler {
|
|
|
1531
1937
|
}
|
|
1532
1938
|
refuseDuringShutdown() {
|
|
1533
1939
|
if (this.transport.shuttingDown) {
|
|
1940
|
+
this.tally?.refused("unavailable");
|
|
1534
1941
|
throw new common.ServiceUnavailableException(REALTIME_ERROR_CODES.TRANSPORT_UNAVAILABLE);
|
|
1535
1942
|
}
|
|
1536
1943
|
}
|
|
@@ -1568,16 +1975,29 @@ exports.SseSubscriptionHandler = class SseSubscriptionHandler {
|
|
|
1568
1975
|
}
|
|
1569
1976
|
}
|
|
1570
1977
|
toClientError(err, what) {
|
|
1571
|
-
if (err instanceof
|
|
1978
|
+
if (err instanceof ConnectionLimitError) {
|
|
1979
|
+
this.tally?.refused("limit");
|
|
1572
1980
|
return new common.ServiceUnavailableException();
|
|
1573
1981
|
}
|
|
1982
|
+
if (err instanceof AuthenticationUnavailableError) {
|
|
1983
|
+
this.tally?.refused("unavailable");
|
|
1984
|
+
return new common.ServiceUnavailableException();
|
|
1985
|
+
}
|
|
1986
|
+
if (err instanceof common.HttpException && err.getStatus() === common.HttpStatus.UNAUTHORIZED) {
|
|
1987
|
+
this.tally?.refused("unauthorized");
|
|
1988
|
+
}
|
|
1574
1989
|
return err instanceof common.HttpException ? err : this.serverError(err, what);
|
|
1575
1990
|
}
|
|
1576
1991
|
serverError(err, what) {
|
|
1577
|
-
this.
|
|
1992
|
+
this.tally?.refused("error");
|
|
1993
|
+
logEvent(this.logger, "error", "REALTIME_HANDSHAKE_FAILED", `${what} \u2014 responding 500`, {}, err);
|
|
1578
1994
|
return new common.InternalServerErrorException();
|
|
1579
1995
|
}
|
|
1580
|
-
|
|
1996
|
+
authenticateRequest(req, res) {
|
|
1997
|
+
const run = () => this.admitAndAuthenticate(req, res);
|
|
1998
|
+
return this.tracing ? this.tracing.span("realtime.handshake", { "bymax.realtime.transport": "sse" }, run) : run();
|
|
1999
|
+
}
|
|
2000
|
+
async admitAndAuthenticate(req, res) {
|
|
1581
2001
|
const held = this.transport.admit();
|
|
1582
2002
|
const detach = onResponseClose(res, () => ticket.release());
|
|
1583
2003
|
const ticket = {
|
|
@@ -1711,7 +2131,14 @@ exports.SseSubscriptionHandler = class SseSubscriptionHandler {
|
|
|
1711
2131
|
fireHook(invoke, onFailure) {
|
|
1712
2132
|
void runHook(invoke).then((failed) => {
|
|
1713
2133
|
if (!failed) return;
|
|
1714
|
-
|
|
2134
|
+
logEvent(
|
|
2135
|
+
this.logger,
|
|
2136
|
+
"warn",
|
|
2137
|
+
"REALTIME_HOOK_FAILED",
|
|
2138
|
+
`Lifecycle hook failed`,
|
|
2139
|
+
{},
|
|
2140
|
+
failed.error
|
|
2141
|
+
);
|
|
1715
2142
|
onFailure?.();
|
|
1716
2143
|
});
|
|
1717
2144
|
}
|
|
@@ -1724,7 +2151,11 @@ exports.SseSubscriptionHandler = __decorateClass([
|
|
|
1724
2151
|
__decorateParam(3, common.Optional()),
|
|
1725
2152
|
__decorateParam(3, common.Inject(REALTIME_HOOKS_TOKEN)),
|
|
1726
2153
|
__decorateParam(4, common.Optional()),
|
|
1727
|
-
__decorateParam(4, common.Inject(exports.OfflineQueueDeliveryService))
|
|
2154
|
+
__decorateParam(4, common.Inject(exports.OfflineQueueDeliveryService)),
|
|
2155
|
+
__decorateParam(5, common.Optional()),
|
|
2156
|
+
__decorateParam(5, common.Inject(exports.RealtimeMetricsTally)),
|
|
2157
|
+
__decorateParam(6, common.Optional()),
|
|
2158
|
+
__decorateParam(6, common.Inject(exports.RealtimeTracing))
|
|
1728
2159
|
], exports.SseSubscriptionHandler);
|
|
1729
2160
|
|
|
1730
2161
|
// src/server/utils/normalize-endpoint-path.ts
|
|
@@ -1775,12 +2206,14 @@ function createSseController(endpoint) {
|
|
|
1775
2206
|
return DynamicSseController;
|
|
1776
2207
|
}
|
|
1777
2208
|
exports.RealtimeService = class RealtimeService {
|
|
1778
|
-
constructor(transport, pubsub) {
|
|
2209
|
+
constructor(transport, pubsub, tally) {
|
|
1779
2210
|
this.transport = transport;
|
|
1780
2211
|
this.pubsub = pubsub;
|
|
2212
|
+
this.tally = tally;
|
|
1781
2213
|
}
|
|
1782
2214
|
transport;
|
|
1783
2215
|
pubsub;
|
|
2216
|
+
tally;
|
|
1784
2217
|
isPubSubReady() {
|
|
1785
2218
|
if (this.transport.kind === "websocket") return true;
|
|
1786
2219
|
return this.pubsub?.isSubscribed?.() ?? true;
|
|
@@ -1806,15 +2239,18 @@ exports.RealtimeService = class RealtimeService {
|
|
|
1806
2239
|
refuseReservedRoom("leaveRoom", roomId);
|
|
1807
2240
|
return this.transport.leaveRoom(connectionId, roomId);
|
|
1808
2241
|
}
|
|
1809
|
-
disconnect(connectionId, reason) {
|
|
1810
|
-
|
|
2242
|
+
async disconnect(connectionId, reason) {
|
|
2243
|
+
await this.transport.disconnect(connectionId, reason);
|
|
2244
|
+
this.tally?.evicted("revoked");
|
|
1811
2245
|
}
|
|
1812
2246
|
};
|
|
1813
2247
|
exports.RealtimeService = __decorateClass([
|
|
1814
2248
|
common.Injectable(),
|
|
1815
2249
|
__decorateParam(0, common.Inject(REALTIME_TRANSPORT_TOKEN)),
|
|
1816
2250
|
__decorateParam(1, common.Optional()),
|
|
1817
|
-
__decorateParam(1, common.Inject(REALTIME_PUBSUB_TOKEN))
|
|
2251
|
+
__decorateParam(1, common.Inject(REALTIME_PUBSUB_TOKEN)),
|
|
2252
|
+
__decorateParam(2, common.Optional()),
|
|
2253
|
+
__decorateParam(2, common.Inject(exports.RealtimeMetricsTally))
|
|
1818
2254
|
], exports.RealtimeService);
|
|
1819
2255
|
|
|
1820
2256
|
// src/server/utils/semaphore.ts
|
|
@@ -1848,31 +2284,55 @@ function resolvePolicy(raw) {
|
|
|
1848
2284
|
};
|
|
1849
2285
|
}
|
|
1850
2286
|
exports.ReauthenticationService = class ReauthenticationService {
|
|
1851
|
-
constructor(connections, realtime, auth, options, hooks) {
|
|
2287
|
+
constructor(connections, realtime, auth, options, hooks, tracing) {
|
|
1852
2288
|
this.connections = connections;
|
|
1853
2289
|
this.realtime = realtime;
|
|
1854
2290
|
this.auth = auth;
|
|
1855
2291
|
this.hooks = hooks;
|
|
2292
|
+
this.tracing = tracing;
|
|
1856
2293
|
this.policy = resolvePolicy(options.reauthenticationPolicy);
|
|
2294
|
+
this.declared = options.reauthenticationPolicy;
|
|
1857
2295
|
this.slots = new Semaphore(this.policy.concurrency);
|
|
1858
2296
|
}
|
|
1859
2297
|
connections;
|
|
1860
2298
|
realtime;
|
|
1861
2299
|
auth;
|
|
1862
2300
|
hooks;
|
|
2301
|
+
tracing;
|
|
1863
2302
|
logger = new common.Logger(exports.ReauthenticationService.name);
|
|
1864
2303
|
timer = null;
|
|
1865
2304
|
policy;
|
|
2305
|
+
declared;
|
|
1866
2306
|
inFlight = /* @__PURE__ */ new Set();
|
|
1867
2307
|
slots;
|
|
2308
|
+
warnIfCacheOutlivesInterval() {
|
|
2309
|
+
const { cacheTtlMs, intervalSeconds } = this.declared ?? {};
|
|
2310
|
+
if (cacheTtlMs === void 0 || intervalSeconds === void 0) return;
|
|
2311
|
+
if (cacheTtlMs <= intervalSeconds * 1e3) return;
|
|
2312
|
+
logEvent(
|
|
2313
|
+
this.logger,
|
|
2314
|
+
"warn",
|
|
2315
|
+
"REALTIME_REAUTH_CACHE_WEAKENS_POLICY",
|
|
2316
|
+
`reauthenticationPolicy.cacheTtlMs (${cacheTtlMs} ms) exceeds intervalSeconds*1000 (${intervalSeconds * 1e3} ms): the positive reauth cache outlives the interval, so revalidate is skipped on ticks within the cache window \u2014 reauthentication is effectively weakened`,
|
|
2317
|
+
{ cacheTtlMs, intervalMs: intervalSeconds * 1e3 }
|
|
2318
|
+
);
|
|
2319
|
+
}
|
|
1868
2320
|
onModuleInit() {
|
|
1869
2321
|
if (!this.auth.revalidate) {
|
|
1870
2322
|
this.logger.log("Authenticator does not implement revalidate() \u2014 reauthentication disabled");
|
|
1871
2323
|
return;
|
|
1872
2324
|
}
|
|
2325
|
+
this.warnIfCacheOutlivesInterval();
|
|
1873
2326
|
this.timer = setInterval(() => {
|
|
1874
2327
|
this.runCycle().catch((err) => {
|
|
1875
|
-
|
|
2328
|
+
logEvent(
|
|
2329
|
+
this.logger,
|
|
2330
|
+
"warn",
|
|
2331
|
+
"REALTIME_REAUTH_FAILED",
|
|
2332
|
+
`Reauthentication cycle failed`,
|
|
2333
|
+
{},
|
|
2334
|
+
err
|
|
2335
|
+
);
|
|
1876
2336
|
});
|
|
1877
2337
|
}, this.policy.intervalSeconds * 1e3);
|
|
1878
2338
|
this.timer.unref();
|
|
@@ -1890,7 +2350,10 @@ exports.ReauthenticationService = class ReauthenticationService {
|
|
|
1890
2350
|
...this.connections.allByTransport("sse"),
|
|
1891
2351
|
...this.connections.allByTransport("websocket")
|
|
1892
2352
|
];
|
|
1893
|
-
|
|
2353
|
+
const run = async () => {
|
|
2354
|
+
await Promise.all(connections.map((conn) => this.check(conn, now)));
|
|
2355
|
+
};
|
|
2356
|
+
await (this.tracing ? this.tracing.span("realtime.reauthenticate", {}, run) : run());
|
|
1894
2357
|
}
|
|
1895
2358
|
async check(conn, now) {
|
|
1896
2359
|
if (this.inFlight.has(conn.connectionId)) return;
|
|
@@ -1927,7 +2390,14 @@ exports.ReauthenticationService = class ReauthenticationService {
|
|
|
1927
2390
|
}
|
|
1928
2391
|
await this.revoke(conn);
|
|
1929
2392
|
} catch (err) {
|
|
1930
|
-
|
|
2393
|
+
logEvent(
|
|
2394
|
+
this.logger,
|
|
2395
|
+
"warn",
|
|
2396
|
+
"REALTIME_REAUTH_FAILED",
|
|
2397
|
+
`Reauthentication errored`,
|
|
2398
|
+
{ connectionId: conn.connectionId },
|
|
2399
|
+
err
|
|
2400
|
+
);
|
|
1931
2401
|
await this.revoke(conn);
|
|
1932
2402
|
}
|
|
1933
2403
|
}
|
|
@@ -1951,8 +2421,13 @@ exports.ReauthenticationService = class ReauthenticationService {
|
|
|
1951
2421
|
try {
|
|
1952
2422
|
await this.bounded("revocation", () => this.handleFailure(conn));
|
|
1953
2423
|
} catch (err) {
|
|
1954
|
-
|
|
1955
|
-
|
|
2424
|
+
logEvent(
|
|
2425
|
+
this.logger,
|
|
2426
|
+
"warn",
|
|
2427
|
+
"REALTIME_REVOCATION_FAILED",
|
|
2428
|
+
`Revocation failed, retrying next cycle`,
|
|
2429
|
+
{ connectionId: conn.connectionId },
|
|
2430
|
+
err
|
|
1956
2431
|
);
|
|
1957
2432
|
}
|
|
1958
2433
|
}
|
|
@@ -1973,15 +2448,27 @@ exports.ReauthenticationService = class ReauthenticationService {
|
|
|
1973
2448
|
})
|
|
1974
2449
|
).then((failed) => {
|
|
1975
2450
|
if (failed) {
|
|
1976
|
-
|
|
2451
|
+
logEvent(
|
|
2452
|
+
this.logger,
|
|
2453
|
+
"warn",
|
|
2454
|
+
"REALTIME_HOOK_FAILED",
|
|
2455
|
+
`onReauthenticationFailed hook failed`,
|
|
2456
|
+
{ hook: "onReauthenticationFailed" },
|
|
2457
|
+
failed.error
|
|
2458
|
+
);
|
|
1977
2459
|
}
|
|
1978
2460
|
});
|
|
1979
2461
|
await this.realtime.disconnect(conn.connectionId, REALTIME_ERROR_CODES.REAUTHENTICATION_FAILED);
|
|
1980
2462
|
}
|
|
1981
2463
|
notify(conn) {
|
|
1982
2464
|
const failed = (err) => {
|
|
1983
|
-
|
|
1984
|
-
|
|
2465
|
+
logEvent(
|
|
2466
|
+
this.logger,
|
|
2467
|
+
"warn",
|
|
2468
|
+
"REALTIME_REAUTH_FAILED",
|
|
2469
|
+
`Reauthentication notification failed, disconnecting anyway`,
|
|
2470
|
+
{ connectionId: conn.connectionId },
|
|
2471
|
+
err
|
|
1985
2472
|
);
|
|
1986
2473
|
};
|
|
1987
2474
|
try {
|
|
@@ -2000,7 +2487,9 @@ exports.ReauthenticationService = __decorateClass([
|
|
|
2000
2487
|
__decorateParam(2, common.Inject(REALTIME_AUTHENTICATOR_TOKEN)),
|
|
2001
2488
|
__decorateParam(3, common.Inject(REALTIME_OPTIONS_TOKEN)),
|
|
2002
2489
|
__decorateParam(4, common.Optional()),
|
|
2003
|
-
__decorateParam(4, common.Inject(REALTIME_HOOKS_TOKEN))
|
|
2490
|
+
__decorateParam(4, common.Inject(REALTIME_HOOKS_TOKEN)),
|
|
2491
|
+
__decorateParam(5, common.Optional()),
|
|
2492
|
+
__decorateParam(5, common.Inject(exports.RealtimeTracing))
|
|
2004
2493
|
], exports.ReauthenticationService);
|
|
2005
2494
|
|
|
2006
2495
|
// src/server/composition/realtime-module.factory.ts
|
|
@@ -2008,6 +2497,7 @@ var DEFAULT_ASYNC_SSE_ENDPOINT = "/events";
|
|
|
2008
2497
|
var EXPORTED_TOKENS = [
|
|
2009
2498
|
exports.RealtimeService,
|
|
2010
2499
|
exports.ConnectionRegistry,
|
|
2500
|
+
exports.RealtimeMetricsTally,
|
|
2011
2501
|
REALTIME_OPTIONS_TOKEN,
|
|
2012
2502
|
REALTIME_INSTANCE_ID_TOKEN,
|
|
2013
2503
|
REALTIME_AUTHENTICATOR_TOKEN,
|
|
@@ -2027,7 +2517,9 @@ var SHARED_SERVICES = [
|
|
|
2027
2517
|
exports.ReauthenticationService,
|
|
2028
2518
|
exports.OfflineQueueDeliveryService,
|
|
2029
2519
|
exports.AuthenticationGate,
|
|
2030
|
-
exports.ConnectionAdmission
|
|
2520
|
+
exports.ConnectionAdmission,
|
|
2521
|
+
exports.RealtimeMetricsTally,
|
|
2522
|
+
exports.RealtimeTracing
|
|
2031
2523
|
];
|
|
2032
2524
|
function assertModeSupported(wiring, mode, moduleName) {
|
|
2033
2525
|
if (wiring.modes.includes(mode)) return;
|
|
@@ -2041,7 +2533,10 @@ function assertModeSupported(wiring, mode, moduleName) {
|
|
|
2041
2533
|
function warnIfSingleInstance(pubsub, transport, logger) {
|
|
2042
2534
|
if (transport === "websocket") return;
|
|
2043
2535
|
if (!pubsub && process.env["NODE_ENV"] === "production") {
|
|
2044
|
-
|
|
2536
|
+
logEvent(
|
|
2537
|
+
logger,
|
|
2538
|
+
"warn",
|
|
2539
|
+
"REALTIME_PUBSUB_NOT_PROVIDED",
|
|
2045
2540
|
"No IRealtimePubSub provided in production \u2014 single-instance only. Provide a Redis-backed IRealtimePubSub for horizontal scaling."
|
|
2046
2541
|
);
|
|
2047
2542
|
}
|
|
@@ -2058,7 +2553,9 @@ function buildCommonProviders(resolved, instanceId) {
|
|
|
2058
2553
|
...SHARED_SERVICES
|
|
2059
2554
|
];
|
|
2060
2555
|
}
|
|
2061
|
-
function composeForRoot(moduleClass, wiring, logger,
|
|
2556
|
+
function composeForRoot(moduleClass, wiring, logger, raw) {
|
|
2557
|
+
assertOptionsObject(raw, moduleClass.name);
|
|
2558
|
+
const options = dropUnset(raw);
|
|
2062
2559
|
validateOptions(options);
|
|
2063
2560
|
assertModeSupported(wiring, options.transport, moduleClass.name);
|
|
2064
2561
|
const resolved = applyDefaults(options);
|
|
@@ -2077,7 +2574,7 @@ function assertSseEndpointApplies(asyncOptions, moduleName) {
|
|
|
2077
2574
|
if (asyncOptions.sseEndpoint === void 0 || asyncOptions.transport !== "websocket") return;
|
|
2078
2575
|
throw realtimeError(
|
|
2079
2576
|
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
2080
|
-
`sseEndpoint
|
|
2577
|
+
`sseEndpoint was set but transport 'websocket' registers no SSE controller \u2014 use 'both' to serve SSE too`,
|
|
2081
2578
|
{ who: moduleName }
|
|
2082
2579
|
);
|
|
2083
2580
|
}
|
|
@@ -2085,34 +2582,39 @@ function bindSseEndpoint(resolved, declared, bound, moduleName) {
|
|
|
2085
2582
|
if (declared !== void 0 && normalizeEndpointPath(declared) !== normalizeEndpointPath(bound)) {
|
|
2086
2583
|
throw realtimeError(
|
|
2087
2584
|
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
2088
|
-
`the factory resolved sse.endpoint
|
|
2585
|
+
`the factory resolved an sse.endpoint that differs from the SSE route forRootAsync bound \u2014 controllers are registered before the factory runs, so declare the path as 'sseEndpoint' on the forRootAsync registration itself`,
|
|
2089
2586
|
{ who: moduleName }
|
|
2090
2587
|
);
|
|
2091
2588
|
}
|
|
2092
2589
|
return Object.freeze({ ...resolved, sse: { ...resolved.sse, endpoint: bound } });
|
|
2093
2590
|
}
|
|
2094
2591
|
function resolveAsyncOptions(raw, declaredMode, instanceId, source, logger, moduleName) {
|
|
2095
|
-
if (
|
|
2592
|
+
if (raw === void 0 || raw === null) {
|
|
2096
2593
|
throw realtimeError(REALTIME_ERROR_CODES.INVALID_OPTIONS, `${source} returned nothing`);
|
|
2097
2594
|
}
|
|
2098
|
-
|
|
2099
|
-
|
|
2595
|
+
assertOptionsObject(raw, moduleName);
|
|
2596
|
+
const options = dropUnset(raw);
|
|
2597
|
+
validateOptions(options);
|
|
2598
|
+
if (options.transport !== declaredMode) {
|
|
2100
2599
|
throw realtimeError(
|
|
2101
2600
|
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
2102
|
-
`forRootAsync was registered for transport '${declaredMode}' but the factory resolved '${
|
|
2601
|
+
`forRootAsync was registered for transport '${declaredMode}' but the factory resolved '${options.transport}'`,
|
|
2103
2602
|
{ who: moduleName }
|
|
2104
2603
|
);
|
|
2105
2604
|
}
|
|
2106
|
-
const resolved = applyDefaults(
|
|
2605
|
+
const resolved = applyDefaults(options);
|
|
2107
2606
|
logger.log(`Bootstrapped (transport=${resolved.transport}, instanceId=${instanceId})`);
|
|
2108
|
-
return resolved;
|
|
2607
|
+
return { resolved, declaredEndpoint: options.sse?.endpoint };
|
|
2608
|
+
}
|
|
2609
|
+
function isSet(pattern) {
|
|
2610
|
+
return pattern !== void 0 && pattern !== null;
|
|
2109
2611
|
}
|
|
2110
2612
|
function assertOneFactoryPattern(asyncOptions, moduleName) {
|
|
2111
2613
|
const patterns = [
|
|
2112
2614
|
asyncOptions.useFactory,
|
|
2113
2615
|
asyncOptions.useClass,
|
|
2114
2616
|
asyncOptions.useExisting
|
|
2115
|
-
].filter(
|
|
2617
|
+
].filter(isSet);
|
|
2116
2618
|
if (patterns.length === 1) return;
|
|
2117
2619
|
throw realtimeError(
|
|
2118
2620
|
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
@@ -2121,7 +2623,7 @@ function assertOneFactoryPattern(asyncOptions, moduleName) {
|
|
|
2121
2623
|
);
|
|
2122
2624
|
}
|
|
2123
2625
|
function buildOptionsProvider(asyncOptions, resolve, factoryToken) {
|
|
2124
|
-
if (asyncOptions.useFactory) {
|
|
2626
|
+
if (isSet(asyncOptions.useFactory)) {
|
|
2125
2627
|
return {
|
|
2126
2628
|
provide: REALTIME_OPTIONS_TOKEN,
|
|
2127
2629
|
useFactory: async (...args) => resolve(await asyncOptions.useFactory(...args), "useFactory"),
|
|
@@ -2130,10 +2632,18 @@ function buildOptionsProvider(asyncOptions, resolve, factoryToken) {
|
|
|
2130
2632
|
}
|
|
2131
2633
|
return {
|
|
2132
2634
|
provide: REALTIME_OPTIONS_TOKEN,
|
|
2133
|
-
useFactory: async (factory) =>
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2635
|
+
useFactory: async (factory) => {
|
|
2636
|
+
if (typeof factory?.createRealtimeOptions !== "function") {
|
|
2637
|
+
throw realtimeError(
|
|
2638
|
+
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
2639
|
+
"the provider named by useExisting or useClass must implement createRealtimeOptions"
|
|
2640
|
+
);
|
|
2641
|
+
}
|
|
2642
|
+
return resolve(
|
|
2643
|
+
await factory.createRealtimeOptions(),
|
|
2644
|
+
"createRealtimeOptions"
|
|
2645
|
+
);
|
|
2646
|
+
},
|
|
2137
2647
|
inject: [asyncOptions.useExisting ?? factoryToken]
|
|
2138
2648
|
};
|
|
2139
2649
|
}
|
|
@@ -2155,7 +2665,7 @@ function buildOptionDerivedProviders(logger, instanceId) {
|
|
|
2155
2665
|
}
|
|
2156
2666
|
function buildResolver(asyncOptions, instanceId, boundSseEndpoint, logger, moduleName) {
|
|
2157
2667
|
return (raw, source) => {
|
|
2158
|
-
const resolved = resolveAsyncOptions(
|
|
2668
|
+
const { resolved, declaredEndpoint } = resolveAsyncOptions(
|
|
2159
2669
|
raw,
|
|
2160
2670
|
asyncOptions.transport,
|
|
2161
2671
|
instanceId,
|
|
@@ -2164,19 +2674,21 @@ function buildResolver(asyncOptions, instanceId, boundSseEndpoint, logger, modul
|
|
|
2164
2674
|
moduleName
|
|
2165
2675
|
);
|
|
2166
2676
|
if (asyncOptions.transport === "websocket") return resolved;
|
|
2167
|
-
return bindSseEndpoint(resolved,
|
|
2677
|
+
return bindSseEndpoint(resolved, declaredEndpoint, boundSseEndpoint, moduleName);
|
|
2168
2678
|
};
|
|
2169
2679
|
}
|
|
2170
2680
|
function composeForRootAsync(moduleClass, wiring, logger, asyncOptions) {
|
|
2171
2681
|
const moduleName = moduleClass.name;
|
|
2682
|
+
assertOptionsObject(asyncOptions, moduleName);
|
|
2172
2683
|
assertOneFactoryPattern(asyncOptions, moduleName);
|
|
2173
2684
|
assertModeSupported(wiring, asyncOptions.transport, moduleName);
|
|
2685
|
+
assertSseEndpoint("sseEndpoint", asyncOptions.sseEndpoint);
|
|
2174
2686
|
assertSseEndpointApplies(asyncOptions, moduleName);
|
|
2175
2687
|
const FACTORY_TOKEN = /* @__PURE__ */ Symbol("REALTIME_OPTIONS_FACTORY");
|
|
2176
2688
|
const instanceId = crypto.randomUUID();
|
|
2177
2689
|
const boundSseEndpoint = asyncOptions.sseEndpoint ?? DEFAULT_ASYNC_SSE_ENDPOINT;
|
|
2178
2690
|
const resolve = buildResolver(asyncOptions, instanceId, boundSseEndpoint, logger, moduleName);
|
|
2179
|
-
const factoryClassProvider = asyncOptions.useClass ? [{ provide: FACTORY_TOKEN, useClass: asyncOptions.useClass }] : [];
|
|
2691
|
+
const factoryClassProvider = isSet(asyncOptions.useClass) ? [{ provide: FACTORY_TOKEN, useClass: asyncOptions.useClass }] : [];
|
|
2180
2692
|
const { providers: transportProviders, controllers } = wiring.buildAsync({
|
|
2181
2693
|
mode: asyncOptions.transport,
|
|
2182
2694
|
sseEndpoint: boundSseEndpoint
|
|
@@ -2215,7 +2727,14 @@ exports.RealtimePubSubSubscriber = class RealtimePubSubSubscriber {
|
|
|
2215
2727
|
try {
|
|
2216
2728
|
await this.unsubscribe();
|
|
2217
2729
|
} catch (err) {
|
|
2218
|
-
|
|
2730
|
+
logEvent(
|
|
2731
|
+
this.logger,
|
|
2732
|
+
"warn",
|
|
2733
|
+
"REALTIME_PUBSUB_UNSUBSCRIBE_FAILED",
|
|
2734
|
+
`Pub/sub unsubscribe failed`,
|
|
2735
|
+
{},
|
|
2736
|
+
err
|
|
2737
|
+
);
|
|
2219
2738
|
}
|
|
2220
2739
|
this.unsubscribe = null;
|
|
2221
2740
|
}
|
|
@@ -2223,8 +2742,12 @@ exports.RealtimePubSubSubscriber = class RealtimePubSubSubscriber {
|
|
|
2223
2742
|
dropped = windowedReport(
|
|
2224
2743
|
() => ({ count: 0, last: "none" }),
|
|
2225
2744
|
({ count, last }) => {
|
|
2226
|
-
|
|
2227
|
-
|
|
2745
|
+
logEvent(
|
|
2746
|
+
this.logger,
|
|
2747
|
+
"warn",
|
|
2748
|
+
"REALTIME_PUBSUB_DROPPED",
|
|
2749
|
+
`Pub/sub dropped ${count} message(s) it could not act on since the last report (last: ${last})`,
|
|
2750
|
+
{ count, last }
|
|
2228
2751
|
);
|
|
2229
2752
|
}
|
|
2230
2753
|
);
|
|
@@ -2252,17 +2775,9 @@ exports.RealtimePubSubSubscriber = class RealtimePubSubSubscriber {
|
|
|
2252
2775
|
this.sse.broadcastLocal(a.event, a.data, a.id);
|
|
2253
2776
|
break;
|
|
2254
2777
|
}
|
|
2255
|
-
case "disconnect":
|
|
2256
|
-
|
|
2257
|
-
Promise.resolve(this.sse.disconnectLocal(a.connectionId, a.reason)).catch(
|
|
2258
|
-
(err) => {
|
|
2259
|
-
this.logger.warn(
|
|
2260
|
-
`Remote disconnect of ${a.connectionId} failed: ${describeError(err)}`
|
|
2261
|
-
);
|
|
2262
|
-
}
|
|
2263
|
-
);
|
|
2778
|
+
case "disconnect":
|
|
2779
|
+
this.closeLocal(msg.args);
|
|
2264
2780
|
break;
|
|
2265
|
-
}
|
|
2266
2781
|
default: {
|
|
2267
2782
|
msg.op;
|
|
2268
2783
|
this.drop("unknown op");
|
|
@@ -2272,6 +2787,18 @@ exports.RealtimePubSubSubscriber = class RealtimePubSubSubscriber {
|
|
|
2272
2787
|
this.drop(String(Object(err).constructor?.name ?? "Error"));
|
|
2273
2788
|
}
|
|
2274
2789
|
}
|
|
2790
|
+
closeLocal(a) {
|
|
2791
|
+
Promise.resolve(this.sse.disconnectLocal(a.connectionId, a.reason)).catch((err) => {
|
|
2792
|
+
logEvent(
|
|
2793
|
+
this.logger,
|
|
2794
|
+
"warn",
|
|
2795
|
+
"REALTIME_DISCONNECT_FAILED",
|
|
2796
|
+
"Remote disconnect failed",
|
|
2797
|
+
{ connectionId: a.connectionId },
|
|
2798
|
+
err
|
|
2799
|
+
);
|
|
2800
|
+
});
|
|
2801
|
+
}
|
|
2275
2802
|
drop(last) {
|
|
2276
2803
|
this.dropped(({ count }) => ({ count: count + 1, last }));
|
|
2277
2804
|
}
|
|
@@ -2365,11 +2892,6 @@ var CANNOT_PUBLISH = /* @__PURE__ */ new Set([
|
|
|
2365
2892
|
var PUBLISHER_WAIT_MS = 2e3;
|
|
2366
2893
|
var PUBLISH_WAIT_MS = 2e3;
|
|
2367
2894
|
var CONNECTION_OVER = ["ready", "close", "reconnecting", "end"];
|
|
2368
|
-
function connectionReason(err) {
|
|
2369
|
-
const code = err?.code;
|
|
2370
|
-
const text = describeError(err);
|
|
2371
|
-
return [typeof code === "string" ? code : "", text].filter(Boolean).join(" ") || String(err);
|
|
2372
|
-
}
|
|
2373
2895
|
var RedisRealtimePubSub = class _RedisRealtimePubSub {
|
|
2374
2896
|
#pub;
|
|
2375
2897
|
channel;
|
|
@@ -2507,7 +3029,7 @@ var RedisRealtimePubSub = class _RedisRealtimePubSub {
|
|
|
2507
3029
|
if (outcome === "subscribed") {
|
|
2508
3030
|
this.markUp();
|
|
2509
3031
|
} else {
|
|
2510
|
-
this.markDown(
|
|
3032
|
+
this.markDown(void 0, outcome.unreachable);
|
|
2511
3033
|
first.catch((err) => this.refused(err));
|
|
2512
3034
|
}
|
|
2513
3035
|
} catch (err) {
|
|
@@ -2533,8 +3055,13 @@ var RedisRealtimePubSub = class _RedisRealtimePubSub {
|
|
|
2533
3055
|
const reason = describeError(err);
|
|
2534
3056
|
if (reason === this.#lastRefusal) return;
|
|
2535
3057
|
this.#lastRefusal = reason;
|
|
2536
|
-
|
|
2537
|
-
|
|
3058
|
+
logEvent(
|
|
3059
|
+
this.logger,
|
|
3060
|
+
"warn",
|
|
3061
|
+
"REALTIME_PUBSUB_UNAVAILABLE",
|
|
3062
|
+
"the server refused the subscription",
|
|
3063
|
+
{},
|
|
3064
|
+
err
|
|
2538
3065
|
);
|
|
2539
3066
|
}
|
|
2540
3067
|
markUp() {
|
|
@@ -2546,20 +3073,29 @@ var RedisRealtimePubSub = class _RedisRealtimePubSub {
|
|
|
2546
3073
|
`Subscribed to "${this.channel}"; remote events and revocations reach this instance`
|
|
2547
3074
|
);
|
|
2548
3075
|
}
|
|
2549
|
-
markDown(
|
|
3076
|
+
markDown(wording, error) {
|
|
2550
3077
|
this.#subscribed = false;
|
|
2551
3078
|
if (this.#outageLogged) return;
|
|
2552
3079
|
this.#outageLogged = true;
|
|
2553
|
-
|
|
2554
|
-
|
|
3080
|
+
logEvent(
|
|
3081
|
+
this.logger,
|
|
3082
|
+
"warn",
|
|
3083
|
+
"REALTIME_PUBSUB_UNAVAILABLE",
|
|
3084
|
+
`not subscribed${wording === void 0 ? "" : `: ${wording}`}. Remote events and revocations will not reach this instance until it subscribes again`,
|
|
3085
|
+
{},
|
|
3086
|
+
error
|
|
2555
3087
|
);
|
|
2556
3088
|
}
|
|
2557
3089
|
reportMalformed() {
|
|
2558
3090
|
this.malformedDropped++;
|
|
2559
3091
|
const now = Date.now();
|
|
2560
3092
|
if (now - this.malformedReportedAt < MALFORMED_REPORT_INTERVAL_MS) return;
|
|
2561
|
-
|
|
2562
|
-
|
|
3093
|
+
logEvent(
|
|
3094
|
+
this.logger,
|
|
3095
|
+
"warn",
|
|
3096
|
+
"REALTIME_PUBSUB_DROPPED",
|
|
3097
|
+
`dropped ${this.malformedDropped} malformed payload(s) since the last report`,
|
|
3098
|
+
{ count: this.malformedDropped, last: "malformed" }
|
|
2563
3099
|
);
|
|
2564
3100
|
this.malformedDropped = 0;
|
|
2565
3101
|
this.malformedReportedAt = now;
|
|
@@ -2587,6 +3123,18 @@ var RedisRealtimePubSub = class _RedisRealtimePubSub {
|
|
|
2587
3123
|
};
|
|
2588
3124
|
|
|
2589
3125
|
// src/server/offline-queue/redis-offline-queue.ts
|
|
3126
|
+
var MAX_PER_USER_CEILING = 1e5;
|
|
3127
|
+
var TTL_SECONDS_CEILING = 2147483647;
|
|
3128
|
+
function requireInteger(name, value, least, most) {
|
|
3129
|
+
if (typeof value === "number" && Number.isSafeInteger(value) && value >= least && value <= most) {
|
|
3130
|
+
return value;
|
|
3131
|
+
}
|
|
3132
|
+
throw realtimeError(
|
|
3133
|
+
REALTIME_ERROR_CODES.INVALID_OPTIONS,
|
|
3134
|
+
`options.${name} must be an integer >= ${least} and <= ${most}, received ${describeReceived(value)}`,
|
|
3135
|
+
{ who: "RedisOfflineQueue" }
|
|
3136
|
+
);
|
|
3137
|
+
}
|
|
2590
3138
|
var RedisOfflineQueue = class _RedisOfflineQueue {
|
|
2591
3139
|
#client;
|
|
2592
3140
|
maxPerUser;
|
|
@@ -2594,9 +3142,23 @@ var RedisOfflineQueue = class _RedisOfflineQueue {
|
|
|
2594
3142
|
static MS_PAD = 16;
|
|
2595
3143
|
static COUNTER_PAD = 6;
|
|
2596
3144
|
constructor(options) {
|
|
3145
|
+
if (!isPlainObject(options)) refuseObject("RedisOfflineQueue", "options", options);
|
|
3146
|
+
if (typeof options.client !== "object" || options.client === null) {
|
|
3147
|
+
refuseObject("RedisOfflineQueue", "options.client", options.client);
|
|
3148
|
+
}
|
|
2597
3149
|
this.#client = options.client;
|
|
2598
|
-
this.maxPerUser =
|
|
2599
|
-
|
|
3150
|
+
this.maxPerUser = requireInteger(
|
|
3151
|
+
"maxPerUser",
|
|
3152
|
+
options.maxPerUser ?? 500,
|
|
3153
|
+
1,
|
|
3154
|
+
MAX_PER_USER_CEILING
|
|
3155
|
+
);
|
|
3156
|
+
this.ttlSeconds = requireInteger(
|
|
3157
|
+
"ttlSeconds",
|
|
3158
|
+
options.ttlSeconds ?? 3600,
|
|
3159
|
+
1,
|
|
3160
|
+
TTL_SECONDS_CEILING
|
|
3161
|
+
);
|
|
2600
3162
|
}
|
|
2601
3163
|
key(userId) {
|
|
2602
3164
|
return `bymax:oq:${userId}`;
|
|
@@ -2653,19 +3215,23 @@ exports.ADMISSION_ROOM_LIMITS = ADMISSION_ROOM_LIMITS;
|
|
|
2653
3215
|
exports.AuthenticationUnavailableError = AuthenticationUnavailableError;
|
|
2654
3216
|
exports.ConnectionLimitError = ConnectionLimitError;
|
|
2655
3217
|
exports.DEFAULT_HOOK_TIMEOUT_MS = DEFAULT_HOOK_TIMEOUT_MS;
|
|
3218
|
+
exports.EVICTION_REASONS = EVICTION_REASONS;
|
|
2656
3219
|
exports.REALTIME_AUTHENTICATOR_TOKEN = REALTIME_AUTHENTICATOR_TOKEN;
|
|
2657
3220
|
exports.REALTIME_ERROR_CODES = REALTIME_ERROR_CODES;
|
|
2658
3221
|
exports.REALTIME_HOOKS_TOKEN = REALTIME_HOOKS_TOKEN;
|
|
2659
3222
|
exports.REALTIME_INSTANCE_ID_TOKEN = REALTIME_INSTANCE_ID_TOKEN;
|
|
3223
|
+
exports.REALTIME_LOG_EVENTS = REALTIME_LOG_EVENTS;
|
|
2660
3224
|
exports.REALTIME_OFFLINE_QUEUE_TOKEN = REALTIME_OFFLINE_QUEUE_TOKEN;
|
|
2661
3225
|
exports.REALTIME_OPTIONS_TOKEN = REALTIME_OPTIONS_TOKEN;
|
|
2662
3226
|
exports.REALTIME_PRESENCE_TOKEN = REALTIME_PRESENCE_TOKEN;
|
|
2663
3227
|
exports.REALTIME_PUBSUB_TOKEN = REALTIME_PUBSUB_TOKEN;
|
|
2664
3228
|
exports.REALTIME_TRANSPORT_TOKEN = REALTIME_TRANSPORT_TOKEN;
|
|
3229
|
+
exports.REFUSAL_REASONS = REFUSAL_REASONS;
|
|
2665
3230
|
exports.RESERVED_EVENT_NAMES = RESERVED_EVENT_NAMES;
|
|
2666
3231
|
exports.ROOM_PREFIXES = ROOM_PREFIXES;
|
|
2667
3232
|
exports.RedisOfflineQueue = RedisOfflineQueue;
|
|
2668
3233
|
exports.RedisRealtimePubSub = RedisRealtimePubSub;
|
|
3234
|
+
exports.TALLY_TRANSPORTS = TALLY_TRANSPORTS;
|
|
2669
3235
|
exports.UNBOUNDED_TICKET = UNBOUNDED_TICKET;
|
|
2670
3236
|
exports.admitRooms = admitRooms;
|
|
2671
3237
|
exports.assertIdentityIsNotBlank = assertIdentityIsNotBlank;
|
|
@@ -2674,6 +3240,7 @@ exports.composeForRootAsync = composeForRootAsync;
|
|
|
2674
3240
|
exports.composeRoomId = composeRoomId;
|
|
2675
3241
|
exports.createSseController = createSseController;
|
|
2676
3242
|
exports.describeError = describeError;
|
|
3243
|
+
exports.logEvent = logEvent;
|
|
2677
3244
|
exports.parseCookieHeader = parseCookieHeader;
|
|
2678
3245
|
exports.quitWithin = quitWithin;
|
|
2679
3246
|
exports.realtimeError = realtimeError;
|