@bymax-one/nest-realtime 1.2.15 → 1.2.16
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 +13 -1
- package/README.md +54 -3
- package/dist/internal/index.cjs +79 -42
- package/dist/internal/index.d.cts +22 -6
- package/dist/internal/index.d.ts +22 -6
- package/dist/internal/index.mjs +79 -42
- package/package.json +4 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [1.2.16] - 2026-11-14
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- **A publish to a Redis that had stopped answering never settled.** `RedisRealtimePubSub.publish` awaited the `PUBLISH` with no limit once its client reported `ready`, and a paused or partitioned Redis accepts the socket and never replies, so every `emitToRoom`, `emitToTenant` and `broadcast` that waited on the bus, and every `disconnect()` of a connection held on another instance, stayed pending. A publish now gives up after two seconds and rejects with `REALTIME_PUBSUB_UNAVAILABLE`, the error it already raised when the client was not ready, so no caller changes: an emit reports the failure through `onDeliveryFailure`, and `disconnect()` rejects. The rejection means the server did not confirm the publish in time, not that it was not delivered: the command had already been written to the socket, so Redis can still process it when it answers, and an emit can arrive after it was reported failed.
|
|
15
|
+
- **A pub/sub payload that was not an object was dropped without a trace, and the messages the subscriber could not act on flooded the log.** A payload that parsed to `null` threw inside the subscriber into a catch that discarded it, with no count and no line; a message with no known op, or a known op with no `args`, wrote one warning for each message, with no limit, and carried the op or the error's message, which whoever can publish to the channel controls. A payload that is not an object is now counted by the report of malformed payloads, and everything else the subscriber cannot act on is reported once for each ten seconds, with a count and a reason that is either `unknown op` or the class of the last failure, never the op, the payload or an error's message.
|
|
16
|
+
|
|
17
|
+
#### Apply to a derived backend
|
|
18
|
+
|
|
19
|
+
Set `commandTimeout` on the ioredis client you give `RedisRealtimePubSub` (for example `new Redis({ commandTimeout: 5000 })`) if you want the command itself rejected: the library's limit stops the caller waiting, but the command stays queued in the client until the server answers or the connection drops. If you alert on the log lines `Unknown pub/sub op: …` or `Pub/sub message handling failed: …`, they no longer exist: alert on `Pub/sub dropped N message(s) it could not act on since the last report` instead. Nothing else changes.
|
|
20
|
+
|
|
10
21
|
## [1.2.15] - 2026-11-14
|
|
11
22
|
|
|
12
23
|
### Fixed
|
|
@@ -1094,7 +1105,8 @@ First published release.
|
|
|
1094
1105
|
rather than by the manual sweep that raised the NestJS floors — that sweep
|
|
1095
1106
|
asked about NestJS and never put the same question to the other ten peers.
|
|
1096
1107
|
|
|
1097
|
-
[Unreleased]: https://github.com/bymaxone/nest-realtime/compare/v1.2.
|
|
1108
|
+
[Unreleased]: https://github.com/bymaxone/nest-realtime/compare/v1.2.16...HEAD
|
|
1109
|
+
[1.2.16]: https://github.com/bymaxone/nest-realtime/compare/v1.2.15...v1.2.16
|
|
1098
1110
|
[1.2.15]: https://github.com/bymaxone/nest-realtime/compare/v1.2.14...v1.2.15
|
|
1099
1111
|
[1.2.14]: https://github.com/bymaxone/nest-realtime/compare/v1.2.13...v1.2.14
|
|
1100
1112
|
[1.2.13]: https://github.com/bymaxone/nest-realtime/compare/v1.2.12...v1.2.13
|
package/README.md
CHANGED
|
@@ -385,7 +385,7 @@ What the client then does depends on how the attempt ended, and no `Retry-After`
|
|
|
385
385
|
| `503` with the generic body | the response is not committed yet | The connection fails and the browser does **not** reconnect by itself. The application has to open a new `EventSource`, which cannot send `Last-Event-ID`, so what was missed is not replayed unless it adds the id to the url as `lastEventId`. | Reconnects after its backoff with a **new** `EventSource` that carries the last id it saw as the `lastEventId` query parameter: the events are replayed from there. |
|
|
386
386
|
| `200`, then the stream ends with an error frame | an application interceptor committed the response first | Reconnects by itself and sends the last id it saw, so the events are read again. | The browser reconnects with the id. If the hook's own backoff opens a new source first, that one carries the id as `lastEventId`, so either way the events are read again. |
|
|
387
387
|
|
|
388
|
-
An application that cannot lose events has to keep its own cursor and open the stream with it. The id is read from the `Last-Event-ID` header or, when the request has none, from the `lastEventId` query parameter, which is what `useRealtimeSse` sends (replacing a `lastEventId` the url you gave it already carries, once it has seen an event; the other parameters are then written again, so a key with no value gains an equals sign (`?flag` becomes `?flag=`), an encoded space is written as `+`, an empty parameter is dropped,
|
|
388
|
+
An application that cannot lose events has to keep its own cursor and open the stream with it. The id is read from the `Last-Event-ID` header or, when the request has none, from the `lastEventId` query parameter, which is what `useRealtimeSse` sends (replacing a `lastEventId` the url you gave it already carries, once it has seen an event; the other parameters are then written again, so a key with no value gains an equals sign (`?flag` becomes `?flag=`), an encoded space is written as `+`, an empty parameter is dropped, an encoded `+` stays one, and a percent-encoded byte that is not valid UTF-8 is written as the replacement character, so `%FF` becomes `%EF%BF%BD` while a valid sequence such as `%E2%82%AC` is unchanged) and what an application adds to the url of a new `EventSource`; the header wins when both are present. An id longer than 256 characters, from either place, and a query value that is not a single string (a repeated parameter, say) are read as no id. The query parameter travels in the url, so it is written to access logs and caches with the rest of it; the ids this library generates are 20 characters (`{epoch ms}-{counter}`) and are not credentials.
|
|
389
389
|
|
|
390
390
|
#### How many connections one instance holds
|
|
391
391
|
|
|
@@ -429,6 +429,57 @@ The design specification, [docs/technical_specification.md](./docs/technical_spe
|
|
|
429
429
|
|
|
430
430
|
---
|
|
431
431
|
|
|
432
|
+
## 🏭 Running in Production
|
|
433
|
+
|
|
434
|
+
This section lists the bounds the library applies, what happens when each one is reached, and what an instance does when a dependency fails. A default is the value in the code. A behaviour stated here is one that a test or a measurement pins, and the `Evidence` column of the second table says which; where nothing pins a behaviour, the row says "not measured".
|
|
435
|
+
|
|
436
|
+
### Limits
|
|
437
|
+
|
|
438
|
+
| Bound | Default | When it is reached |
|
|
439
|
+
| ---------------------------------------------------------------------------------- | ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
440
|
+
| `maxConnections` | 10,000 | A handshake past it is refused before `authenticate` is called: SSE answers `503`, WebSocket answers the generic `connect_error`. Connections of both transports count together. See [How many connections one instance holds](#how-many-connections-one-instance-holds). |
|
|
441
|
+
| `maxPendingHandshakes` | 1,000 | A handshake that finds that many `authenticate` calls waiting is refused at once, and `authenticate` is not called. A call whose handshake was refused for taking too long keeps its place while it is still open. See [Authentication is bounded](#authentication-is-bounded). |
|
|
442
|
+
| `authenticateTimeoutMs` | 10,000 ms | A handshake whose `authenticate` has not settled by then is refused: `503` with the generic body on SSE, the generic `connect_error` on WebSocket. A read of the offline queue for a reconnect that carries `Last-Event-ID` is given up on after the same time. See [The offline-queue read on a reconnect](#the-offline-queue-read-on-a-reconnect). |
|
|
443
|
+
| `hookTimeoutMs` | 5,000 ms | A hook the library awaits that has not settled by then is given up on, and the failure is logged as `did not settle within N ms`. See [Hooks the library waits for](#hooks-the-library-waits-for). |
|
|
444
|
+
| `reauthenticationPolicy.revalidateTimeoutMs`, `reauthenticationPolicy.concurrency` | 10,000 ms; 8 | A `revalidate` call that has not settled by then is treated as one that threw, and its connection is revoked. At most `concurrency` calls run at once. |
|
|
445
|
+
| `sse.maxQueuedEvents`, `sse.maxQueuedBytes` | 1,000 events; 2,097,152 bytes | An event that would take the events queued for a connection past `sse.maxQueuedEvents` is not queued; that connection is closed with the reason `REALTIME_SLOW_CONSUMER` and the others are not affected. The queue's counter refuses an event that would take the bytes past `sse.maxQueuedBytes`, unless that event is the only one queued. With the defaults, a client that never reads is closed with the same reason. [A client that does not read](#a-client-that-does-not-read) states the cases this row does not cover. |
|
|
446
|
+
| `sse.stallHeartbeats` | 3 | A connection whose socket has taken nothing for that many heartbeats in a row, while the response holds bytes, is closed with the reason `REALTIME_SLOW_CONSUMER`. |
|
|
447
|
+
| `sse.replayBufferSize`, `sse.replayTtlMs`, `sse.replayMaxIdleUsers` | 100 events; 300,000 ms; 1,000 users | A user's ring keeps the newest `replayBufferSize` events and drops the older ones. An entry is dropped `replayTtlMs` after its user's last connection closed. At most `replayMaxIdleUsers` such entries are kept, and the one that closed longest ago goes first. See [Replay is per instance, and bounded](#replay-is-per-instance-and-bounded). |
|
|
448
|
+
| Length of a `Last-Event-ID` header or `lastEventId` query value | 256 characters | An id longer than that is treated as absent: no replay and no offline-queue read is made for it. An id of exactly 256 characters is used. A longer header with a valid query value falls back to the query value. |
|
|
449
|
+
|
|
450
|
+
### When a dependency fails
|
|
451
|
+
|
|
452
|
+
| Situation | What the instance does | Evidence |
|
|
453
|
+
| ------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
454
|
+
| SSE pub/sub: the Redis server refuses the SUBSCRIBE at start (wrong password, missing permission) | `subscribe` rejects with `REALTIME_PUBSUB_UNAVAILABLE`, and the boot fails. | tested |
|
|
455
|
+
| SSE pub/sub: Redis is unreachable at start | The boot completes without waiting for Redis, the instance is not subscribed, one warning line is logged for the outage, not one for each retry, and the instance subscribes when Redis comes up. | tested |
|
|
456
|
+
| Socket.IO Redis adapter: Redis never answers at start | Creating the Socket.IO server returns and the Redis adapter is registered, without waiting for Redis. | tested with a client whose commands never settle |
|
|
457
|
+
| SSE pub/sub: the publishing client's first connection is slow | `subscribe` waits for it, 2,000 ms at most; after that the boot completes and the instance is not subscribed. | tested |
|
|
458
|
+
| SSE pub/sub, Redis lost while running: a publish | It is refused at once with `REALTIME_PUBSUB_UNAVAILABLE` and is never sent when the connection returns. | tested |
|
|
459
|
+
| SSE pub/sub, Redis connected but not answering: a publish | It rejects with `REALTIME_PUBSUB_UNAVAILABLE` after 2,000 ms. The rejection means the server did not confirm the publish in time, not that it was not delivered: the command was already written to the socket, so Redis can still process it when it answers and an emit can arrive after it was reported failed. The command stays queued in the client until the server answers or the connection drops; set `commandTimeout` on your ioredis client (for example `new Redis({ commandTimeout: 5000 })`) to reject the command itself. | tested on a controlled clock; measured on 1.2.16, against a `redis:7-alpine` container paused with `docker pause`: rejected after 2,001, 2,003 and 2,005 ms |
|
|
460
|
+
| SSE pub/sub, Redis lost while running: `disconnect` of a connection this instance does not hold | `disconnect` rejects at once with `REALTIME_PUBSUB_UNAVAILABLE`. `emitToUser` for a user with no connection on this instance, with no offline queue configured, resolves at once. | tested |
|
|
461
|
+
| SSE pub/sub, Redis lost while running: the subscriber | One warning line is logged for an outage while its reconnects fail (several attempts over 400 ms in the test), and a `Subscribed` line is logged again when Redis returns. | tested |
|
|
462
|
+
| Redis is paused when an SSE application closes | The close waits at most 2,000 ms for the subscriber's QUIT, then disconnects it. `app.close()` settled in 2,003 ms. | measured on 1.2.15, against a `redis:7-alpine` container paused with `docker pause`; the bound is also pinned on a controlled clock |
|
|
463
|
+
| Redis is paused when an application with both transports closes | The adapter's subscriber and the pub/sub subscriber are each waited for at most 2,000 ms, one after the other. `app.close()` settled in 4,007 ms. | measured on 1.2.15, against a `redis:7-alpine` container paused with `docker pause`; the two bounds are also pinned on a controlled clock |
|
|
464
|
+
| The offline queue does not answer | A reconnect that carries `Last-Event-ID` gets a `503` with the generic body after `authenticateTimeoutMs`, and nothing is registered. A queue whose first read rejects gives an empty gap instead, and the client gets its live events. At most 1,000 reads wait at once by default; the next is refused without a call to the store. | tested |
|
|
465
|
+
| The authenticator does not answer | Handshakes are refused after `authenticateTimeoutMs`; once `maxPendingHandshakes` calls wait, further handshakes are refused at once. | tested |
|
|
466
|
+
| A consumer hook never settles | The library gives up on it after `hookTimeoutMs`. On WebSocket an `onConnect` that never settles delays the announcement of the connection until then, and an `onDisconnect` that never settles still removes the connection while the other client stays connected. When an application closes, five SSE streams whose `onDisconnect` never settles are released after one hook timeout of a test clock. | tested |
|
|
467
|
+
| Memory over a long soak; behaviour at `maxConnections` on a real host | not measured | not measured |
|
|
468
|
+
|
|
469
|
+
### Shutdown order
|
|
470
|
+
|
|
471
|
+
The Socket.IO server closes first, then the adapter's `dispose()` runs, and `dispose()` closes the adapter's own Redis subscriber connection. The pub/sub subscriber is unsubscribed in the application shutdown hook. When both transports run, the two Redis subscribers are closed in two successive phases, each waited for at most 2,000 ms. [Shutdown](#shutdown) describes how a shutdown starts.
|
|
472
|
+
|
|
473
|
+
### Before you deploy
|
|
474
|
+
|
|
475
|
+
- File-descriptor limit against `maxConnections`: [How many connections one instance holds](#how-many-connections-one-instance-holds).
|
|
476
|
+
- Proxy buffering for SSE: [docs/proxies-cheat-sheet.md](./docs/proxies-cheat-sheet.md).
|
|
477
|
+
- Sticky sessions with the polling fallback: [docs/architecture/scaling-cheatsheet.md](./docs/architecture/scaling-cheatsheet.md#sticky-sessions--mandatory-for-polling-fallback).
|
|
478
|
+
- Replay is per instance: [Replay is per instance, and bounded](#replay-is-per-instance-and-bounded).
|
|
479
|
+
- HTTPS and CORS: [Security Checklist](#security-checklist).
|
|
480
|
+
|
|
481
|
+
---
|
|
482
|
+
|
|
432
483
|
## 🏗️ Architecture
|
|
433
484
|
|
|
434
485
|
The package runs **inside** your NestJS application as a dynamic module — not as a separate realtime server:
|
|
@@ -595,7 +646,7 @@ The unified emit API. Identical on every transport.
|
|
|
595
646
|
|
|
596
647
|
All methods return `Promise<void>`.
|
|
597
648
|
|
|
598
|
-
**Revocation is never best-effort.** `disconnect(connectionId)` resolves once the connection was closed on this instance or the revocation was handed to the bus (`IRealtimePubSub`). It **rejects**, with `error.code` set to a `RealtimeErrorCode`, when it could not: `REALTIME_PUBSUB_UNAVAILABLE` when the bus refuses the publish (`RedisRealtimePubSub` refuses at once while its Redis client is not ready), `REALTIME_TRANSPORT_UNAVAILABLE` when the WebSocket server is not attached or its fan-out throws. Branch on the code, not the message; the error is Nest's `ServiceUnavailableException` (HTTP `503`) with `code` set, so a controller that lets it escape answers `503` with the generic body (see "What a client is told"). A rejection means the connection may still be connected, so retry.
|
|
649
|
+
**Revocation is never best-effort.** `disconnect(connectionId)` resolves once the connection was closed on this instance or the revocation was handed to the bus (`IRealtimePubSub`). It **rejects**, with `error.code` set to a `RealtimeErrorCode`, when it could not: `REALTIME_PUBSUB_UNAVAILABLE` when the bus refuses the publish (`RedisRealtimePubSub` refuses at once while its Redis client is not ready, and after 2,000 ms when the client is ready but the server does not answer), `REALTIME_TRANSPORT_UNAVAILABLE` when the WebSocket server is not attached or its fan-out throws. Branch on the code, not the message; the error is Nest's `ServiceUnavailableException` (HTTP `503`) with `code` set, so a controller that lets it escape answers `503` with the generic body (see "What a client is told"). A rejection means the connection may still be connected, so retry.
|
|
599
650
|
|
|
600
651
|
**Emits are at-most-once.** `emitToUser`, `emitToTenant`, `emitToRoom` and `broadcast` **resolve even when the publish to the other instances failed**; that failure is logged with `REALTIME_PUBSUB_UNAVAILABLE` and passed to the `onDeliveryFailure` hook, which is where to count it. An offline queue that could not keep an event for a user who is offline here is reported the same way, since another instance may have delivered it live; a failure on one transport of `'both'` is only logged. An emit rejects only when the event has no delivery path left: for `emitToUser`, the user is offline here, the queue failed, and the bus failed or cannot reach another instance (the default in-memory bus); with `'both'`, a failure on both transports rejects with `REALTIME_TRANSPORT_UNAVAILABLE`. A rejected emit is not proof that no client received it, so retry only where a duplicate is acceptable.
|
|
601
652
|
|
|
@@ -863,7 +914,7 @@ The code travels on the error you catch; what reaches a client is decided by the
|
|
|
863
914
|
|
|
864
915
|
The rejection of a call is Nest's own `ServiceUnavailableException`, so a controller that lets it escape answers `503` with the body `{"statusCode":503,"message":"Service Unavailable"}`. The library's message, which names the failed call, and the `cause`, which can hold a driver's text with a host and credentials, are on the instance for your logs and never in the response. Branch on `error.code`, not on the message.
|
|
865
916
|
|
|
866
|
-
Nest's default exception filter does not log an `HttpException`, so a `503` from one of these calls leaves no line in your log, where a plain `Error` was logged with its stack. To keep a record, catch `ServiceUnavailableException` in an exception filter of your own and log `exception.code`, `exception.message` and `exception.cause`, as you would for any other `HttpException` you want to see.
|
|
917
|
+
Nest's default exception filter does not log an `HttpException`, so a `503` from one of these calls leaves no line in your log, where a plain `Error` was logged with its stack. To keep a record, catch `ServiceUnavailableException` in an exception filter of your own and log `exception.code`, `exception.message` and `exception.cause`, as you would for any other `HttpException` you want to see. With `@bymax-one/nest-core` 1.6.10 and an `IUnexpectedErrorSink` bound, such a `503` reaches the sink with its `cause`, once for each status and code in each `envelope.serverErrorWindowMs` window (60 s by default), so the outage is recorded there without a filter of your own; the cause can carry a host and a port, so redact it in your logger.
|
|
867
918
|
|
|
868
919
|
**An authenticator that wants a `503` throws `ServiceUnavailableException`.** On SSE an `HttpException` thrown from `authenticate` is sent as it is, so `throw new ServiceUnavailableException()` answers `503`; any other error answers a generic `500`. On WebSocket the handshake carries no HTTP status: an authenticator's own exception is answered with the generic message and no `data.code`. Only the library's own unavailable refusals carry `REALTIME_TRANSPORT_UNAVAILABLE`, which a client can treat as retryable.
|
|
869
920
|
|
package/dist/internal/index.cjs
CHANGED
|
@@ -218,11 +218,26 @@ function reportDeliveryFailure(logger, hooks, failure, what) {
|
|
|
218
218
|
});
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
+
// src/server/utils/windowed-report.ts
|
|
222
|
+
var REPORT_WINDOW_MS = 1e4;
|
|
223
|
+
function windowedReport(fresh, emit) {
|
|
224
|
+
let startedAt = Number.NEGATIVE_INFINITY;
|
|
225
|
+
let pending = fresh();
|
|
226
|
+
return (add) => {
|
|
227
|
+
pending = add(pending);
|
|
228
|
+
const now = performance.now();
|
|
229
|
+
if (now - startedAt < REPORT_WINDOW_MS) return;
|
|
230
|
+
startedAt = now;
|
|
231
|
+
const accumulated = pending;
|
|
232
|
+
pending = fresh();
|
|
233
|
+
emit(accumulated);
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
221
237
|
// src/server/offline-queue/offline-queue-delivery.service.ts
|
|
222
238
|
var RETRIEVE_LIMIT = 200;
|
|
223
239
|
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
224
240
|
var DEFAULT_MAX_PENDING = 1e3;
|
|
225
|
-
var REPORT_WINDOW_MS = 1e4;
|
|
226
241
|
var OfflineQueueTimeoutError = class extends Error {
|
|
227
242
|
constructor() {
|
|
228
243
|
super("the offline queue did not answer in time");
|
|
@@ -241,8 +256,14 @@ exports.OfflineQueueDeliveryService = class OfflineQueueDeliveryService {
|
|
|
241
256
|
timeoutMs;
|
|
242
257
|
maxPending;
|
|
243
258
|
pending = 0;
|
|
244
|
-
|
|
245
|
-
|
|
259
|
+
timeouts = windowedReport(
|
|
260
|
+
() => 0,
|
|
261
|
+
(count) => {
|
|
262
|
+
this.logger.warn(
|
|
263
|
+
`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`
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
);
|
|
246
267
|
async retrieve(userId, lastEventId, ringBufferIds) {
|
|
247
268
|
const { events } = await this.retrieveGap(userId, lastEventId);
|
|
248
269
|
return events.filter((e) => !ringBufferIds.has(e.id));
|
|
@@ -318,15 +339,7 @@ exports.OfflineQueueDeliveryService = class OfflineQueueDeliveryService {
|
|
|
318
339
|
}
|
|
319
340
|
}
|
|
320
341
|
countTimeout() {
|
|
321
|
-
this.
|
|
322
|
-
const now = performance.now();
|
|
323
|
-
if (now - this.windowStartedAt < REPORT_WINDOW_MS) return;
|
|
324
|
-
this.windowStartedAt = now;
|
|
325
|
-
const count = this.timedOut;
|
|
326
|
-
this.timedOut = 0;
|
|
327
|
-
this.logger.warn(
|
|
328
|
-
`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`
|
|
329
|
-
);
|
|
342
|
+
this.timeouts((count) => count + 1);
|
|
330
343
|
}
|
|
331
344
|
};
|
|
332
345
|
exports.OfflineQueueDeliveryService = __decorateClass([
|
|
@@ -459,7 +472,6 @@ function resolveClientAddress(request) {
|
|
|
459
472
|
}
|
|
460
473
|
return peerAddress(request.socket?.remoteAddress);
|
|
461
474
|
}
|
|
462
|
-
var REPORT_WINDOW_MS2 = 1e4;
|
|
463
475
|
var AuthenticationUnavailableError = class extends Error {
|
|
464
476
|
constructor(reason) {
|
|
465
477
|
super(
|
|
@@ -480,8 +492,14 @@ exports.AuthenticationGate = class AuthenticationGate {
|
|
|
480
492
|
maxPending;
|
|
481
493
|
timeoutMs;
|
|
482
494
|
pending = 0;
|
|
483
|
-
|
|
484
|
-
|
|
495
|
+
refusals = windowedReport(
|
|
496
|
+
() => ({ saturated: 0, timeout: 0 }),
|
|
497
|
+
({ saturated, timeout }) => {
|
|
498
|
+
this.logger.warn(
|
|
499
|
+
`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`
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
);
|
|
485
503
|
async authenticate(context) {
|
|
486
504
|
if (this.pending >= this.maxPending) {
|
|
487
505
|
this.refuse("saturated");
|
|
@@ -510,15 +528,7 @@ exports.AuthenticationGate = class AuthenticationGate {
|
|
|
510
528
|
}
|
|
511
529
|
}
|
|
512
530
|
refuse(reason) {
|
|
513
|
-
this.refused[reason]
|
|
514
|
-
const now = performance.now();
|
|
515
|
-
if (now - this.windowStartedAt < REPORT_WINDOW_MS2) return;
|
|
516
|
-
this.windowStartedAt = now;
|
|
517
|
-
const { saturated, timeout } = this.refused;
|
|
518
|
-
this.refused = { saturated: 0, timeout: 0 };
|
|
519
|
-
this.logger.warn(
|
|
520
|
-
`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`
|
|
521
|
-
);
|
|
531
|
+
this.refusals((refused) => ({ ...refused, [reason]: refused[reason] + 1 }));
|
|
522
532
|
}
|
|
523
533
|
};
|
|
524
534
|
exports.AuthenticationGate = __decorateClass([
|
|
@@ -596,7 +606,6 @@ exports.ConnectionRegistry = __decorateClass([
|
|
|
596
606
|
var DEFAULT_AUTHENTICATE_TIMEOUT_MS = 1e4;
|
|
597
607
|
var DEFAULT_MAX_CONNECTIONS = 1e4;
|
|
598
608
|
var HOLD_MARGIN_MS = 5e3;
|
|
599
|
-
var REPORT_WINDOW_MS3 = 1e4;
|
|
600
609
|
var ConnectionLimitError = class extends Error {
|
|
601
610
|
constructor(limit) {
|
|
602
611
|
super(`the instance already holds ${limit} connections`);
|
|
@@ -619,8 +628,14 @@ exports.ConnectionAdmission = class ConnectionAdmission {
|
|
|
619
628
|
max;
|
|
620
629
|
holdMs;
|
|
621
630
|
held = /* @__PURE__ */ new Map();
|
|
622
|
-
|
|
623
|
-
|
|
631
|
+
refusals = windowedReport(
|
|
632
|
+
() => 0,
|
|
633
|
+
(count) => {
|
|
634
|
+
this.logger.warn(
|
|
635
|
+
`connections refused since the last report: ${count}, because the instance already holds ${this.max} (maxConnections)`
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
);
|
|
624
639
|
admit() {
|
|
625
640
|
const place = /* @__PURE__ */ Symbol("place");
|
|
626
641
|
this.take(place);
|
|
@@ -652,15 +667,7 @@ exports.ConnectionAdmission = class ConnectionAdmission {
|
|
|
652
667
|
}
|
|
653
668
|
}
|
|
654
669
|
refuse() {
|
|
655
|
-
this.
|
|
656
|
-
const now = performance.now();
|
|
657
|
-
if (now - this.windowStartedAt < REPORT_WINDOW_MS3) return;
|
|
658
|
-
this.windowStartedAt = now;
|
|
659
|
-
const count = this.refused;
|
|
660
|
-
this.refused = 0;
|
|
661
|
-
this.logger.warn(
|
|
662
|
-
`connections refused since the last report: ${count}, because the instance already holds ${this.max} (maxConnections)`
|
|
663
|
-
);
|
|
670
|
+
this.refusals((count) => count + 1);
|
|
664
671
|
}
|
|
665
672
|
};
|
|
666
673
|
exports.ConnectionAdmission = __decorateClass([
|
|
@@ -2135,9 +2142,17 @@ exports.RealtimePubSubSubscriber = class RealtimePubSubSubscriber {
|
|
|
2135
2142
|
this.unsubscribe = null;
|
|
2136
2143
|
}
|
|
2137
2144
|
}
|
|
2145
|
+
dropped = windowedReport(
|
|
2146
|
+
() => ({ count: 0, last: "none" }),
|
|
2147
|
+
({ count, last }) => {
|
|
2148
|
+
this.logger.warn(
|
|
2149
|
+
`Pub/sub dropped ${count} message(s) it could not act on since the last report (last: ${last})`
|
|
2150
|
+
);
|
|
2151
|
+
}
|
|
2152
|
+
);
|
|
2138
2153
|
handle(msg) {
|
|
2139
|
-
if (msg.origin === this.instanceId) return;
|
|
2140
2154
|
try {
|
|
2155
|
+
if (msg.origin === this.instanceId) return;
|
|
2141
2156
|
switch (msg.op) {
|
|
2142
2157
|
case "emitToUser": {
|
|
2143
2158
|
const a = msg.args;
|
|
@@ -2171,14 +2186,17 @@ exports.RealtimePubSubSubscriber = class RealtimePubSubSubscriber {
|
|
|
2171
2186
|
break;
|
|
2172
2187
|
}
|
|
2173
2188
|
default: {
|
|
2174
|
-
|
|
2175
|
-
this.
|
|
2189
|
+
msg.op;
|
|
2190
|
+
this.drop("unknown op");
|
|
2176
2191
|
}
|
|
2177
2192
|
}
|
|
2178
2193
|
} catch (err) {
|
|
2179
|
-
this.
|
|
2194
|
+
this.drop(String(Object(err).constructor?.name ?? "Error"));
|
|
2180
2195
|
}
|
|
2181
2196
|
}
|
|
2197
|
+
drop(last) {
|
|
2198
|
+
this.dropped(({ count }) => ({ count: count + 1, last }));
|
|
2199
|
+
}
|
|
2182
2200
|
};
|
|
2183
2201
|
exports.RealtimePubSubSubscriber = __decorateClass([
|
|
2184
2202
|
common.Injectable(),
|
|
@@ -2267,6 +2285,7 @@ var CANNOT_PUBLISH = /* @__PURE__ */ new Set([
|
|
|
2267
2285
|
"end"
|
|
2268
2286
|
]);
|
|
2269
2287
|
var PUBLISHER_WAIT_MS = 2e3;
|
|
2288
|
+
var PUBLISH_WAIT_MS = 2e3;
|
|
2270
2289
|
var CONNECTION_OVER = ["ready", "close", "reconnecting", "end"];
|
|
2271
2290
|
function connectionReason(err) {
|
|
2272
2291
|
const code = err?.code;
|
|
@@ -2298,7 +2317,20 @@ var RedisRealtimePubSub = class _RedisRealtimePubSub {
|
|
|
2298
2317
|
this.connectLazyClient();
|
|
2299
2318
|
throw this.cannotPublish(`the Redis client is ${status}`);
|
|
2300
2319
|
}
|
|
2301
|
-
|
|
2320
|
+
const sent = this.#pub.publish(this.channel, JSON.stringify(message));
|
|
2321
|
+
let timer;
|
|
2322
|
+
const expired = new Promise((_resolve, reject) => {
|
|
2323
|
+
timer = setTimeout(
|
|
2324
|
+
() => reject(this.cannotPublish(`the server did not answer within ${PUBLISH_WAIT_MS} ms`)),
|
|
2325
|
+
PUBLISH_WAIT_MS
|
|
2326
|
+
);
|
|
2327
|
+
timer.unref();
|
|
2328
|
+
});
|
|
2329
|
+
try {
|
|
2330
|
+
await Promise.race([sent, expired]);
|
|
2331
|
+
} finally {
|
|
2332
|
+
clearTimeout(timer);
|
|
2333
|
+
}
|
|
2302
2334
|
}
|
|
2303
2335
|
connectLazyClient() {
|
|
2304
2336
|
if (this.#pub.status === "wait") this.#pub.connect().catch(() => void 0);
|
|
@@ -2457,7 +2489,12 @@ var RedisRealtimePubSub = class _RedisRealtimePubSub {
|
|
|
2457
2489
|
dispatch(payload) {
|
|
2458
2490
|
let msg;
|
|
2459
2491
|
try {
|
|
2460
|
-
|
|
2492
|
+
const parsed = JSON.parse(payload);
|
|
2493
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
2494
|
+
this.reportMalformed();
|
|
2495
|
+
return;
|
|
2496
|
+
}
|
|
2497
|
+
msg = parsed;
|
|
2461
2498
|
} catch {
|
|
2462
2499
|
this.reportMalformed();
|
|
2463
2500
|
return;
|
|
@@ -1058,6 +1058,7 @@ declare class ReauthenticationService implements OnModuleInit, OnApplicationShut
|
|
|
1058
1058
|
private notify;
|
|
1059
1059
|
}
|
|
1060
1060
|
|
|
1061
|
+
/** How long a window of refusals is summarised in one log line. */
|
|
1061
1062
|
/**
|
|
1062
1063
|
* A handshake was refused because the authenticator could not be asked or did not answer in time:
|
|
1063
1064
|
* a fault of the system, not of the credentials, and carrying nothing about either.
|
|
@@ -1081,8 +1082,7 @@ declare class AuthenticationGate {
|
|
|
1081
1082
|
private readonly maxPending;
|
|
1082
1083
|
private readonly timeoutMs;
|
|
1083
1084
|
private pending;
|
|
1084
|
-
private
|
|
1085
|
-
private refused;
|
|
1085
|
+
private readonly refusals;
|
|
1086
1086
|
constructor(options: BymaxRealtimeModuleOptions, authenticator: IConnectionAuthenticator);
|
|
1087
1087
|
/**
|
|
1088
1088
|
* Ask the authenticator about one handshake.
|
|
@@ -1095,6 +1095,7 @@ declare class AuthenticationGate {
|
|
|
1095
1095
|
private refuse;
|
|
1096
1096
|
}
|
|
1097
1097
|
|
|
1098
|
+
/** How long a window of refusals is summarised in one log line. */
|
|
1098
1099
|
/** A handshake was refused because the instance already holds `maxConnections` connections. */
|
|
1099
1100
|
declare class ConnectionLimitError extends Error {
|
|
1100
1101
|
readonly limit: number;
|
|
@@ -1134,8 +1135,7 @@ declare class ConnectionAdmission {
|
|
|
1134
1135
|
private readonly holdMs;
|
|
1135
1136
|
/** The places held, oldest first, each with the time it is given back at whatever happens. */
|
|
1136
1137
|
private readonly held;
|
|
1137
|
-
private
|
|
1138
|
-
private refused;
|
|
1138
|
+
private readonly refusals;
|
|
1139
1139
|
constructor(options: BymaxRealtimeModuleOptions, registry: ConnectionRegistry);
|
|
1140
1140
|
/**
|
|
1141
1141
|
* Take a place for one connection.
|
|
@@ -1175,6 +1175,7 @@ declare class RoomRegistry {
|
|
|
1175
1175
|
countRooms(): number;
|
|
1176
1176
|
}
|
|
1177
1177
|
|
|
1178
|
+
/** How long a window of timeouts is summarised in one log line. */
|
|
1178
1179
|
/** What the first read of a reconnect's gap came to. */
|
|
1179
1180
|
interface OfflineQueueRead {
|
|
1180
1181
|
/** The gap events, in queue order. */
|
|
@@ -1204,8 +1205,7 @@ declare class OfflineQueueDeliveryService {
|
|
|
1204
1205
|
private readonly timeoutMs;
|
|
1205
1206
|
private readonly maxPending;
|
|
1206
1207
|
private pending;
|
|
1207
|
-
private
|
|
1208
|
-
private timedOut;
|
|
1208
|
+
private readonly timeouts;
|
|
1209
1209
|
constructor(storage?: IOfflineQueueStorage | undefined, hooks?: IConnectionLifecycleHooks | undefined, options?: BymaxRealtimeModuleOptions);
|
|
1210
1210
|
/**
|
|
1211
1211
|
* Return events for `userId` that arrived after `lastEventId` and are NOT
|
|
@@ -1870,7 +1870,15 @@ declare class RealtimePubSubSubscriber implements OnModuleInit, OnApplicationShu
|
|
|
1870
1870
|
constructor(pubsub: IRealtimePubSub, instanceId: string, sse: SseTransport);
|
|
1871
1871
|
onModuleInit(): Promise<void>;
|
|
1872
1872
|
onApplicationShutdown(): Promise<void>;
|
|
1873
|
+
/**
|
|
1874
|
+
* Messages the subscriber cannot act on, counted and reported once for each window. The line
|
|
1875
|
+
* carries the count and the reason in a fixed vocabulary: an unknown op, or the class of the last
|
|
1876
|
+
* failure. The op and the failure's message can echo what whoever publishes to the channel
|
|
1877
|
+
* wrote, so neither is ever written.
|
|
1878
|
+
*/
|
|
1879
|
+
private readonly dropped;
|
|
1873
1880
|
private handle;
|
|
1881
|
+
private drop;
|
|
1874
1882
|
}
|
|
1875
1883
|
|
|
1876
1884
|
/** Options for RedisRealtimePubSub. */
|
|
@@ -1927,6 +1935,14 @@ declare class RedisRealtimePubSub implements IRealtimePubSub {
|
|
|
1927
1935
|
* returns, and never gives up on one waiting behind a peer that does not answer, so a revocation
|
|
1928
1936
|
* would wait out the whole outage and then be sent after its caller was told it failed. It is
|
|
1929
1937
|
* refused at once instead, and never handed to the client.
|
|
1938
|
+
*
|
|
1939
|
+
* A client that can publish but whose server stops answering is given {@link PUBLISH_WAIT_MS}:
|
|
1940
|
+
* after it the publish rejects the same way. That rejection means the server did not confirm the
|
|
1941
|
+
* publish in time, not that it was not delivered: the command was already written to the socket
|
|
1942
|
+
* and Redis can still process it when it answers, so a message can arrive after its caller was
|
|
1943
|
+
* told it failed. The command also stays queued in the client until the server answers or the
|
|
1944
|
+
* connection drops; `commandTimeout` on the host's ioredis client is what rejects the command
|
|
1945
|
+
* itself.
|
|
1930
1946
|
*/
|
|
1931
1947
|
publish(message: RealtimePubSubMessage): Promise<void>;
|
|
1932
1948
|
/**
|
package/dist/internal/index.d.ts
CHANGED
|
@@ -1058,6 +1058,7 @@ declare class ReauthenticationService implements OnModuleInit, OnApplicationShut
|
|
|
1058
1058
|
private notify;
|
|
1059
1059
|
}
|
|
1060
1060
|
|
|
1061
|
+
/** How long a window of refusals is summarised in one log line. */
|
|
1061
1062
|
/**
|
|
1062
1063
|
* A handshake was refused because the authenticator could not be asked or did not answer in time:
|
|
1063
1064
|
* a fault of the system, not of the credentials, and carrying nothing about either.
|
|
@@ -1081,8 +1082,7 @@ declare class AuthenticationGate {
|
|
|
1081
1082
|
private readonly maxPending;
|
|
1082
1083
|
private readonly timeoutMs;
|
|
1083
1084
|
private pending;
|
|
1084
|
-
private
|
|
1085
|
-
private refused;
|
|
1085
|
+
private readonly refusals;
|
|
1086
1086
|
constructor(options: BymaxRealtimeModuleOptions, authenticator: IConnectionAuthenticator);
|
|
1087
1087
|
/**
|
|
1088
1088
|
* Ask the authenticator about one handshake.
|
|
@@ -1095,6 +1095,7 @@ declare class AuthenticationGate {
|
|
|
1095
1095
|
private refuse;
|
|
1096
1096
|
}
|
|
1097
1097
|
|
|
1098
|
+
/** How long a window of refusals is summarised in one log line. */
|
|
1098
1099
|
/** A handshake was refused because the instance already holds `maxConnections` connections. */
|
|
1099
1100
|
declare class ConnectionLimitError extends Error {
|
|
1100
1101
|
readonly limit: number;
|
|
@@ -1134,8 +1135,7 @@ declare class ConnectionAdmission {
|
|
|
1134
1135
|
private readonly holdMs;
|
|
1135
1136
|
/** The places held, oldest first, each with the time it is given back at whatever happens. */
|
|
1136
1137
|
private readonly held;
|
|
1137
|
-
private
|
|
1138
|
-
private refused;
|
|
1138
|
+
private readonly refusals;
|
|
1139
1139
|
constructor(options: BymaxRealtimeModuleOptions, registry: ConnectionRegistry);
|
|
1140
1140
|
/**
|
|
1141
1141
|
* Take a place for one connection.
|
|
@@ -1175,6 +1175,7 @@ declare class RoomRegistry {
|
|
|
1175
1175
|
countRooms(): number;
|
|
1176
1176
|
}
|
|
1177
1177
|
|
|
1178
|
+
/** How long a window of timeouts is summarised in one log line. */
|
|
1178
1179
|
/** What the first read of a reconnect's gap came to. */
|
|
1179
1180
|
interface OfflineQueueRead {
|
|
1180
1181
|
/** The gap events, in queue order. */
|
|
@@ -1204,8 +1205,7 @@ declare class OfflineQueueDeliveryService {
|
|
|
1204
1205
|
private readonly timeoutMs;
|
|
1205
1206
|
private readonly maxPending;
|
|
1206
1207
|
private pending;
|
|
1207
|
-
private
|
|
1208
|
-
private timedOut;
|
|
1208
|
+
private readonly timeouts;
|
|
1209
1209
|
constructor(storage?: IOfflineQueueStorage | undefined, hooks?: IConnectionLifecycleHooks | undefined, options?: BymaxRealtimeModuleOptions);
|
|
1210
1210
|
/**
|
|
1211
1211
|
* Return events for `userId` that arrived after `lastEventId` and are NOT
|
|
@@ -1870,7 +1870,15 @@ declare class RealtimePubSubSubscriber implements OnModuleInit, OnApplicationShu
|
|
|
1870
1870
|
constructor(pubsub: IRealtimePubSub, instanceId: string, sse: SseTransport);
|
|
1871
1871
|
onModuleInit(): Promise<void>;
|
|
1872
1872
|
onApplicationShutdown(): Promise<void>;
|
|
1873
|
+
/**
|
|
1874
|
+
* Messages the subscriber cannot act on, counted and reported once for each window. The line
|
|
1875
|
+
* carries the count and the reason in a fixed vocabulary: an unknown op, or the class of the last
|
|
1876
|
+
* failure. The op and the failure's message can echo what whoever publishes to the channel
|
|
1877
|
+
* wrote, so neither is ever written.
|
|
1878
|
+
*/
|
|
1879
|
+
private readonly dropped;
|
|
1873
1880
|
private handle;
|
|
1881
|
+
private drop;
|
|
1874
1882
|
}
|
|
1875
1883
|
|
|
1876
1884
|
/** Options for RedisRealtimePubSub. */
|
|
@@ -1927,6 +1935,14 @@ declare class RedisRealtimePubSub implements IRealtimePubSub {
|
|
|
1927
1935
|
* returns, and never gives up on one waiting behind a peer that does not answer, so a revocation
|
|
1928
1936
|
* would wait out the whole outage and then be sent after its caller was told it failed. It is
|
|
1929
1937
|
* refused at once instead, and never handed to the client.
|
|
1938
|
+
*
|
|
1939
|
+
* A client that can publish but whose server stops answering is given {@link PUBLISH_WAIT_MS}:
|
|
1940
|
+
* after it the publish rejects the same way. That rejection means the server did not confirm the
|
|
1941
|
+
* publish in time, not that it was not delivered: the command was already written to the socket
|
|
1942
|
+
* and Redis can still process it when it answers, so a message can arrive after its caller was
|
|
1943
|
+
* told it failed. The command also stays queued in the client until the server answers or the
|
|
1944
|
+
* connection drops; `commandTimeout` on the host's ioredis client is what rejects the command
|
|
1945
|
+
* itself.
|
|
1930
1946
|
*/
|
|
1931
1947
|
publish(message: RealtimePubSubMessage): Promise<void>;
|
|
1932
1948
|
/**
|
package/dist/internal/index.mjs
CHANGED
|
@@ -216,11 +216,26 @@ function reportDeliveryFailure(logger, hooks, failure, what) {
|
|
|
216
216
|
});
|
|
217
217
|
}
|
|
218
218
|
|
|
219
|
+
// src/server/utils/windowed-report.ts
|
|
220
|
+
var REPORT_WINDOW_MS = 1e4;
|
|
221
|
+
function windowedReport(fresh, emit) {
|
|
222
|
+
let startedAt = Number.NEGATIVE_INFINITY;
|
|
223
|
+
let pending = fresh();
|
|
224
|
+
return (add) => {
|
|
225
|
+
pending = add(pending);
|
|
226
|
+
const now = performance.now();
|
|
227
|
+
if (now - startedAt < REPORT_WINDOW_MS) return;
|
|
228
|
+
startedAt = now;
|
|
229
|
+
const accumulated = pending;
|
|
230
|
+
pending = fresh();
|
|
231
|
+
emit(accumulated);
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
219
235
|
// src/server/offline-queue/offline-queue-delivery.service.ts
|
|
220
236
|
var RETRIEVE_LIMIT = 200;
|
|
221
237
|
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
222
238
|
var DEFAULT_MAX_PENDING = 1e3;
|
|
223
|
-
var REPORT_WINDOW_MS = 1e4;
|
|
224
239
|
var OfflineQueueTimeoutError = class extends Error {
|
|
225
240
|
constructor() {
|
|
226
241
|
super("the offline queue did not answer in time");
|
|
@@ -239,8 +254,14 @@ var OfflineQueueDeliveryService = class {
|
|
|
239
254
|
timeoutMs;
|
|
240
255
|
maxPending;
|
|
241
256
|
pending = 0;
|
|
242
|
-
|
|
243
|
-
|
|
257
|
+
timeouts = windowedReport(
|
|
258
|
+
() => 0,
|
|
259
|
+
(count) => {
|
|
260
|
+
this.logger.warn(
|
|
261
|
+
`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`
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
);
|
|
244
265
|
async retrieve(userId, lastEventId, ringBufferIds) {
|
|
245
266
|
const { events } = await this.retrieveGap(userId, lastEventId);
|
|
246
267
|
return events.filter((e) => !ringBufferIds.has(e.id));
|
|
@@ -316,15 +337,7 @@ var OfflineQueueDeliveryService = class {
|
|
|
316
337
|
}
|
|
317
338
|
}
|
|
318
339
|
countTimeout() {
|
|
319
|
-
this.
|
|
320
|
-
const now = performance.now();
|
|
321
|
-
if (now - this.windowStartedAt < REPORT_WINDOW_MS) return;
|
|
322
|
-
this.windowStartedAt = now;
|
|
323
|
-
const count = this.timedOut;
|
|
324
|
-
this.timedOut = 0;
|
|
325
|
-
this.logger.warn(
|
|
326
|
-
`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`
|
|
327
|
-
);
|
|
340
|
+
this.timeouts((count) => count + 1);
|
|
328
341
|
}
|
|
329
342
|
};
|
|
330
343
|
OfflineQueueDeliveryService = __decorateClass([
|
|
@@ -457,7 +470,6 @@ function resolveClientAddress(request) {
|
|
|
457
470
|
}
|
|
458
471
|
return peerAddress(request.socket?.remoteAddress);
|
|
459
472
|
}
|
|
460
|
-
var REPORT_WINDOW_MS2 = 1e4;
|
|
461
473
|
var AuthenticationUnavailableError = class extends Error {
|
|
462
474
|
constructor(reason) {
|
|
463
475
|
super(
|
|
@@ -478,8 +490,14 @@ var AuthenticationGate = class {
|
|
|
478
490
|
maxPending;
|
|
479
491
|
timeoutMs;
|
|
480
492
|
pending = 0;
|
|
481
|
-
|
|
482
|
-
|
|
493
|
+
refusals = windowedReport(
|
|
494
|
+
() => ({ saturated: 0, timeout: 0 }),
|
|
495
|
+
({ saturated, timeout }) => {
|
|
496
|
+
this.logger.warn(
|
|
497
|
+
`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`
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
);
|
|
483
501
|
async authenticate(context) {
|
|
484
502
|
if (this.pending >= this.maxPending) {
|
|
485
503
|
this.refuse("saturated");
|
|
@@ -508,15 +526,7 @@ var AuthenticationGate = class {
|
|
|
508
526
|
}
|
|
509
527
|
}
|
|
510
528
|
refuse(reason) {
|
|
511
|
-
this.refused[reason]
|
|
512
|
-
const now = performance.now();
|
|
513
|
-
if (now - this.windowStartedAt < REPORT_WINDOW_MS2) return;
|
|
514
|
-
this.windowStartedAt = now;
|
|
515
|
-
const { saturated, timeout } = this.refused;
|
|
516
|
-
this.refused = { saturated: 0, timeout: 0 };
|
|
517
|
-
this.logger.warn(
|
|
518
|
-
`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`
|
|
519
|
-
);
|
|
529
|
+
this.refusals((refused) => ({ ...refused, [reason]: refused[reason] + 1 }));
|
|
520
530
|
}
|
|
521
531
|
};
|
|
522
532
|
AuthenticationGate = __decorateClass([
|
|
@@ -594,7 +604,6 @@ ConnectionRegistry = __decorateClass([
|
|
|
594
604
|
var DEFAULT_AUTHENTICATE_TIMEOUT_MS = 1e4;
|
|
595
605
|
var DEFAULT_MAX_CONNECTIONS = 1e4;
|
|
596
606
|
var HOLD_MARGIN_MS = 5e3;
|
|
597
|
-
var REPORT_WINDOW_MS3 = 1e4;
|
|
598
607
|
var ConnectionLimitError = class extends Error {
|
|
599
608
|
constructor(limit) {
|
|
600
609
|
super(`the instance already holds ${limit} connections`);
|
|
@@ -617,8 +626,14 @@ var ConnectionAdmission = class {
|
|
|
617
626
|
max;
|
|
618
627
|
holdMs;
|
|
619
628
|
held = /* @__PURE__ */ new Map();
|
|
620
|
-
|
|
621
|
-
|
|
629
|
+
refusals = windowedReport(
|
|
630
|
+
() => 0,
|
|
631
|
+
(count) => {
|
|
632
|
+
this.logger.warn(
|
|
633
|
+
`connections refused since the last report: ${count}, because the instance already holds ${this.max} (maxConnections)`
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
);
|
|
622
637
|
admit() {
|
|
623
638
|
const place = /* @__PURE__ */ Symbol("place");
|
|
624
639
|
this.take(place);
|
|
@@ -650,15 +665,7 @@ var ConnectionAdmission = class {
|
|
|
650
665
|
}
|
|
651
666
|
}
|
|
652
667
|
refuse() {
|
|
653
|
-
this.
|
|
654
|
-
const now = performance.now();
|
|
655
|
-
if (now - this.windowStartedAt < REPORT_WINDOW_MS3) return;
|
|
656
|
-
this.windowStartedAt = now;
|
|
657
|
-
const count = this.refused;
|
|
658
|
-
this.refused = 0;
|
|
659
|
-
this.logger.warn(
|
|
660
|
-
`connections refused since the last report: ${count}, because the instance already holds ${this.max} (maxConnections)`
|
|
661
|
-
);
|
|
668
|
+
this.refusals((count) => count + 1);
|
|
662
669
|
}
|
|
663
670
|
};
|
|
664
671
|
ConnectionAdmission = __decorateClass([
|
|
@@ -2133,9 +2140,17 @@ var RealtimePubSubSubscriber = class {
|
|
|
2133
2140
|
this.unsubscribe = null;
|
|
2134
2141
|
}
|
|
2135
2142
|
}
|
|
2143
|
+
dropped = windowedReport(
|
|
2144
|
+
() => ({ count: 0, last: "none" }),
|
|
2145
|
+
({ count, last }) => {
|
|
2146
|
+
this.logger.warn(
|
|
2147
|
+
`Pub/sub dropped ${count} message(s) it could not act on since the last report (last: ${last})`
|
|
2148
|
+
);
|
|
2149
|
+
}
|
|
2150
|
+
);
|
|
2136
2151
|
handle(msg) {
|
|
2137
|
-
if (msg.origin === this.instanceId) return;
|
|
2138
2152
|
try {
|
|
2153
|
+
if (msg.origin === this.instanceId) return;
|
|
2139
2154
|
switch (msg.op) {
|
|
2140
2155
|
case "emitToUser": {
|
|
2141
2156
|
const a = msg.args;
|
|
@@ -2169,14 +2184,17 @@ var RealtimePubSubSubscriber = class {
|
|
|
2169
2184
|
break;
|
|
2170
2185
|
}
|
|
2171
2186
|
default: {
|
|
2172
|
-
|
|
2173
|
-
this.
|
|
2187
|
+
msg.op;
|
|
2188
|
+
this.drop("unknown op");
|
|
2174
2189
|
}
|
|
2175
2190
|
}
|
|
2176
2191
|
} catch (err) {
|
|
2177
|
-
this.
|
|
2192
|
+
this.drop(String(Object(err).constructor?.name ?? "Error"));
|
|
2178
2193
|
}
|
|
2179
2194
|
}
|
|
2195
|
+
drop(last) {
|
|
2196
|
+
this.dropped(({ count }) => ({ count: count + 1, last }));
|
|
2197
|
+
}
|
|
2180
2198
|
};
|
|
2181
2199
|
RealtimePubSubSubscriber = __decorateClass([
|
|
2182
2200
|
Injectable(),
|
|
@@ -2265,6 +2283,7 @@ var CANNOT_PUBLISH = /* @__PURE__ */ new Set([
|
|
|
2265
2283
|
"end"
|
|
2266
2284
|
]);
|
|
2267
2285
|
var PUBLISHER_WAIT_MS = 2e3;
|
|
2286
|
+
var PUBLISH_WAIT_MS = 2e3;
|
|
2268
2287
|
var CONNECTION_OVER = ["ready", "close", "reconnecting", "end"];
|
|
2269
2288
|
function connectionReason(err) {
|
|
2270
2289
|
const code = err?.code;
|
|
@@ -2296,7 +2315,20 @@ var RedisRealtimePubSub = class _RedisRealtimePubSub {
|
|
|
2296
2315
|
this.connectLazyClient();
|
|
2297
2316
|
throw this.cannotPublish(`the Redis client is ${status}`);
|
|
2298
2317
|
}
|
|
2299
|
-
|
|
2318
|
+
const sent = this.#pub.publish(this.channel, JSON.stringify(message));
|
|
2319
|
+
let timer;
|
|
2320
|
+
const expired = new Promise((_resolve, reject) => {
|
|
2321
|
+
timer = setTimeout(
|
|
2322
|
+
() => reject(this.cannotPublish(`the server did not answer within ${PUBLISH_WAIT_MS} ms`)),
|
|
2323
|
+
PUBLISH_WAIT_MS
|
|
2324
|
+
);
|
|
2325
|
+
timer.unref();
|
|
2326
|
+
});
|
|
2327
|
+
try {
|
|
2328
|
+
await Promise.race([sent, expired]);
|
|
2329
|
+
} finally {
|
|
2330
|
+
clearTimeout(timer);
|
|
2331
|
+
}
|
|
2300
2332
|
}
|
|
2301
2333
|
connectLazyClient() {
|
|
2302
2334
|
if (this.#pub.status === "wait") this.#pub.connect().catch(() => void 0);
|
|
@@ -2455,7 +2487,12 @@ var RedisRealtimePubSub = class _RedisRealtimePubSub {
|
|
|
2455
2487
|
dispatch(payload) {
|
|
2456
2488
|
let msg;
|
|
2457
2489
|
try {
|
|
2458
|
-
|
|
2490
|
+
const parsed = JSON.parse(payload);
|
|
2491
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
2492
|
+
this.reportMalformed();
|
|
2493
|
+
return;
|
|
2494
|
+
}
|
|
2495
|
+
msg = parsed;
|
|
2459
2496
|
} catch {
|
|
2460
2497
|
this.reportMalformed();
|
|
2461
2498
|
return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bymax-one/nest-realtime",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.16",
|
|
4
4
|
"description": "Realtime backend → frontend communication for NestJS — dual-transport SSE (default) and WebSocket (opt-in) with a unified server-side API.",
|
|
5
5
|
"author": "Bymax One <support@bymax.one>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -97,13 +97,15 @@
|
|
|
97
97
|
"check:exports": "attw --pack . --profile strict",
|
|
98
98
|
"check:mutants": "node scripts/check-mutation-directives.mjs",
|
|
99
99
|
"check:file-length": "node scripts/check-file-length.mjs",
|
|
100
|
+
"check:spec-headers": "node scripts/check-spec-headers.mjs",
|
|
101
|
+
"check:suppressions": "node scripts/check-suppressions-baseline.mjs",
|
|
100
102
|
"check:api": "node scripts/check-api-surface.mjs",
|
|
101
103
|
"check:published": "node scripts/check-published-surface.mjs",
|
|
102
104
|
"check:runtime": "node scripts/check-consumer-runtime.mjs",
|
|
103
105
|
"benchmark": "node scripts/benchmark/run.mjs",
|
|
104
106
|
"check:surface": "node scripts/check-public-surface.mjs",
|
|
105
107
|
"clean": "rm -rf dist coverage",
|
|
106
|
-
"lint": "eslint src",
|
|
108
|
+
"lint": "eslint src && pnpm check:spec-headers && pnpm check:suppressions",
|
|
107
109
|
"lint:fix": "eslint src --fix",
|
|
108
110
|
"mutation": "stryker run",
|
|
109
111
|
"mutation:full": "node -e \"require('node:fs').rmSync('reports/stryker-incremental.json',{force:true,recursive:true})\" && stryker run",
|