@bymax-one/nest-realtime 1.2.9 → 1.2.11
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 +45 -1
- package/README.md +13 -13
- package/dist/internal/index.cjs +3 -0
- package/dist/internal/index.d.cts +7 -0
- package/dist/internal/index.d.ts +7 -0
- package/dist/internal/index.mjs +3 -0
- package/dist/websocket/index.cjs +41 -5
- package/dist/websocket/index.d.cts +23 -0
- package/dist/websocket/index.d.ts +23 -0
- package/dist/websocket/index.mjs +41 -5
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,48 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [1.2.11] - 2026-11-09
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- **Refusal during shutdown begins at `onModuleDestroy`, not at `beforeApplicationShutdown`.** Nest
|
|
15
|
+
runs the hooks of a module nearer the root before those of a module it imports, so a host's slow
|
|
16
|
+
`beforeApplicationShutdown` in the root module ran before the transport's own, and a WebSocket
|
|
17
|
+
handshake or an SSE stream arriving in that window was accepted. Nest runs every module's
|
|
18
|
+
`onModuleDestroy` before any `beforeApplicationShutdown`, so both transports now mark shutdown as
|
|
19
|
+
begun there: from then on a handshake gets `REALTIME_TRANSPORT_UNAVAILABLE` and a stream gets a 503. A slow `onModuleDestroy` in a module nearer the root than the realtime module still runs
|
|
20
|
+
first, and until it returns a connection is accepted: put slow shutdown work in
|
|
21
|
+
`beforeApplicationShutdown`, or make the host stop accepting traffic before it calls `close()`.
|
|
22
|
+
|
|
23
|
+
#### Apply to a derived backend
|
|
24
|
+
|
|
25
|
+
`SseTransport` and `WebSocketTransport` gain a public `onModuleDestroy()` that marks shutdown as
|
|
26
|
+
begun. A subclass that overrides it has to call `super.onModuleDestroy()`.
|
|
27
|
+
|
|
28
|
+
## [1.2.10] - 2026-11-09
|
|
29
|
+
|
|
30
|
+
### Fixed
|
|
31
|
+
|
|
32
|
+
- **A WebSocket handshake that arrives while the application is closing is refused.** The handshake
|
|
33
|
+
middleware authenticated every handshake and accepted it, however late it came, so a handshake
|
|
34
|
+
that arrived once shutdown had begun reached an authenticator whose store the host may already
|
|
35
|
+
have closed, and was accepted into a server that was about to close. From
|
|
36
|
+
`beforeApplicationShutdown` on, the middleware refuses it with a `connect_error` whose
|
|
37
|
+
`data.code` is `REALTIME_TRANSPORT_UNAVAILABLE`, before the authenticator runs and again if
|
|
38
|
+
shutdown began while the authenticator was running; this is the refusal SSE gives with a 503.
|
|
39
|
+
The code differs from `REALTIME_AUTH_FAILED`, which the same middleware uses for a refused
|
|
40
|
+
credential, so a client can tell the two apart.
|
|
41
|
+
|
|
42
|
+
#### Apply to a derived backend
|
|
43
|
+
|
|
44
|
+
`WebSocketTransport` gains a public `beforeApplicationShutdown()` hook and a `shuttingDown` getter,
|
|
45
|
+
and `RealtimeGateway` gains a private `unavailable` method. A class that extends either and already
|
|
46
|
+
declares a member with one of those names has to rename it or override it on purpose. A subclass
|
|
47
|
+
of `WebSocketTransport` that overrides `beforeApplicationShutdown` has to call
|
|
48
|
+
`super.beforeApplicationShutdown()`, or the gateway keeps accepting handshakes while the
|
|
49
|
+
application closes. A gateway that replaces `RealtimeGateway`'s handshake middleware gets no
|
|
50
|
+
refusal from this change; it can read `transport.shuttingDown` itself.
|
|
51
|
+
|
|
10
52
|
## [1.2.9] - 2026-11-06
|
|
11
53
|
|
|
12
54
|
### Changed
|
|
@@ -913,7 +955,9 @@ First published release.
|
|
|
913
955
|
rather than by the manual sweep that raised the NestJS floors — that sweep
|
|
914
956
|
asked about NestJS and never put the same question to the other ten peers.
|
|
915
957
|
|
|
916
|
-
[Unreleased]: https://github.com/bymaxone/nest-realtime/compare/v1.2.
|
|
958
|
+
[Unreleased]: https://github.com/bymaxone/nest-realtime/compare/v1.2.11...HEAD
|
|
959
|
+
[1.2.11]: https://github.com/bymaxone/nest-realtime/compare/v1.2.10...v1.2.11
|
|
960
|
+
[1.2.10]: https://github.com/bymaxone/nest-realtime/compare/v1.2.9...v1.2.10
|
|
917
961
|
[1.2.9]: https://github.com/bymaxone/nest-realtime/compare/v1.2.8...v1.2.9
|
|
918
962
|
[1.2.8]: https://github.com/bymaxone/nest-realtime/compare/v1.2.7...v1.2.8
|
|
919
963
|
[1.2.7]: https://github.com/bymaxone/nest-realtime/compare/v1.2.6...v1.2.7
|
package/README.md
CHANGED
|
@@ -871,19 +871,19 @@ These names belong to the library. Do not reuse them for application events. The
|
|
|
871
871
|
|
|
872
872
|
### Error Codes
|
|
873
873
|
|
|
874
|
-
| Code | Status
|
|
875
|
-
| ---------------------------------- |
|
|
876
|
-
| `REALTIME_INVALID_OPTIONS` | Raised at bootstrap for an invalid option
|
|
877
|
-
| `REALTIME_NO_AUTHENTICATOR` | Raised at bootstrap when the authenticator is missing or invalid
|
|
878
|
-
| `REALTIME_AUTH_FAILED` | Raised when authentication is refused: the SSE 401 body and the WebSocket handshake refusal
|
|
879
|
-
| `REALTIME_REAUTHENTICATION_FAILED` | Raised when a revalidation fails
|
|
880
|
-
| `REALTIME_TOO_MANY_CONNECTIONS` | Raised as the reason an evicted connection is closed
|
|
881
|
-
| `REALTIME_INVALID_TICKET` | Reserved, never raised today: a ticket store is yours to write
|
|
882
|
-
| `REALTIME_PUBSUB_UNAVAILABLE` | Raised as the rejection of a disconnect when the bus refuses the revocation; an emit's publish failure is logged and passed to the delivery-failure hook
|
|
883
|
-
| `REALTIME_TRANSPORT_UNAVAILABLE` | Raised as the rejection of a disconnect, a join or a leave when the transport that holds the connection fails or has no server attached, and of an emit to an offline user that has no delivery path left |
|
|
884
|
-
| `REALTIME_REDIS_ADAPTER_FAILED` | Raised at bootstrap when the Redis adapter is configured and cannot be installed
|
|
885
|
-
| `REALTIME_PAYLOAD_TOO_LARGE` | Reserved, never raised today
|
|
886
|
-
| `REALTIME_REPLAY_BUFFER_MISS` | Reserved, never raised today
|
|
874
|
+
| Code | Status |
|
|
875
|
+
| ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
876
|
+
| `REALTIME_INVALID_OPTIONS` | Raised at bootstrap for an invalid option |
|
|
877
|
+
| `REALTIME_NO_AUTHENTICATOR` | Raised at bootstrap when the authenticator is missing or invalid |
|
|
878
|
+
| `REALTIME_AUTH_FAILED` | Raised when authentication is refused: the SSE 401 body and the WebSocket handshake refusal |
|
|
879
|
+
| `REALTIME_REAUTHENTICATION_FAILED` | Raised when a revalidation fails |
|
|
880
|
+
| `REALTIME_TOO_MANY_CONNECTIONS` | Raised as the reason an evicted connection is closed |
|
|
881
|
+
| `REALTIME_INVALID_TICKET` | Reserved, never raised today: a ticket store is yours to write |
|
|
882
|
+
| `REALTIME_PUBSUB_UNAVAILABLE` | Raised as the rejection of a disconnect when the bus refuses the revocation; an emit's publish failure is logged and passed to the delivery-failure hook |
|
|
883
|
+
| `REALTIME_TRANSPORT_UNAVAILABLE` | Raised as the rejection of a disconnect, a join or a leave when the transport that holds the connection fails or has no server attached, and of an emit to an offline user that has no delivery path left. Also what a new SSE stream or WebSocket handshake is refused with once shutdown has begun: a 503 whose message is the code, and a connect_error whose data.code is the code. Shutdown begins, for this purpose, in the onModuleDestroy phase, ahead of every beforeApplicationShutdown hook, so a slow beforeApplicationShutdown in the host does not leave a window; a slow onModuleDestroy in a module nearer the root than the realtime module still runs first, and a connection is accepted until it returns. |
|
|
884
|
+
| `REALTIME_REDIS_ADAPTER_FAILED` | Raised at bootstrap when the Redis adapter is configured and cannot be installed |
|
|
885
|
+
| `REALTIME_PAYLOAD_TOO_LARGE` | Reserved, never raised today |
|
|
886
|
+
| `REALTIME_REPLAY_BUFFER_MISS` | Reserved, never raised today |
|
|
887
887
|
|
|
888
888
|
### DI Tokens
|
|
889
889
|
|
package/dist/internal/index.cjs
CHANGED
|
@@ -551,6 +551,9 @@ exports.SseTransport = class SseTransport {
|
|
|
551
551
|
options;
|
|
552
552
|
instanceId;
|
|
553
553
|
offlineQueue;
|
|
554
|
+
onModuleDestroy() {
|
|
555
|
+
__privateSet(this, _shuttingDown, true);
|
|
556
|
+
}
|
|
554
557
|
async beforeApplicationShutdown() {
|
|
555
558
|
__privateSet(this, _shuttingDown, true);
|
|
556
559
|
this.heartbeat.stopAll();
|
|
@@ -1215,6 +1215,13 @@ declare class SseTransport implements ITransport {
|
|
|
1215
1215
|
readonly kind: "sse";
|
|
1216
1216
|
private readonly logger;
|
|
1217
1217
|
constructor(connections: ConnectionRegistry, rooms: RoomRegistry, replayBuffer: EventReplayBuffer, idGen: EventIdGenerator, heartbeat: HeartbeatService, auth: IConnectionAuthenticator, pubsub: IRealtimePubSub, hooks: IConnectionLifecycleHooks, options: BymaxRealtimeModuleOptions, instanceId: string, offlineQueue?: IOfflineQueueStorage | undefined);
|
|
1218
|
+
/**
|
|
1219
|
+
* Mark shutdown as begun at the first hook Nest runs on close, so that a slow
|
|
1220
|
+
* `beforeApplicationShutdown` of the host's, which Nest runs before this transport's when it is in a
|
|
1221
|
+
* module nearer the root, cannot leave a window in which a new stream is accepted. A slow
|
|
1222
|
+
* `onModuleDestroy` in such a module still runs first; nothing the library owns runs earlier.
|
|
1223
|
+
*/
|
|
1224
|
+
onModuleDestroy(): void;
|
|
1218
1225
|
/**
|
|
1219
1226
|
* Tear down every SSE connection and stop heartbeat before the HTTP server closes.
|
|
1220
1227
|
*
|
package/dist/internal/index.d.ts
CHANGED
|
@@ -1215,6 +1215,13 @@ declare class SseTransport implements ITransport {
|
|
|
1215
1215
|
readonly kind: "sse";
|
|
1216
1216
|
private readonly logger;
|
|
1217
1217
|
constructor(connections: ConnectionRegistry, rooms: RoomRegistry, replayBuffer: EventReplayBuffer, idGen: EventIdGenerator, heartbeat: HeartbeatService, auth: IConnectionAuthenticator, pubsub: IRealtimePubSub, hooks: IConnectionLifecycleHooks, options: BymaxRealtimeModuleOptions, instanceId: string, offlineQueue?: IOfflineQueueStorage | undefined);
|
|
1218
|
+
/**
|
|
1219
|
+
* Mark shutdown as begun at the first hook Nest runs on close, so that a slow
|
|
1220
|
+
* `beforeApplicationShutdown` of the host's, which Nest runs before this transport's when it is in a
|
|
1221
|
+
* module nearer the root, cannot leave a window in which a new stream is accepted. A slow
|
|
1222
|
+
* `onModuleDestroy` in such a module still runs first; nothing the library owns runs earlier.
|
|
1223
|
+
*/
|
|
1224
|
+
onModuleDestroy(): void;
|
|
1218
1225
|
/**
|
|
1219
1226
|
* Tear down every SSE connection and stop heartbeat before the HTTP server closes.
|
|
1220
1227
|
*
|
package/dist/internal/index.mjs
CHANGED
|
@@ -549,6 +549,9 @@ var SseTransport = class {
|
|
|
549
549
|
options;
|
|
550
550
|
instanceId;
|
|
551
551
|
offlineQueue;
|
|
552
|
+
onModuleDestroy() {
|
|
553
|
+
__privateSet(this, _shuttingDown, true);
|
|
554
|
+
}
|
|
552
555
|
async beforeApplicationShutdown() {
|
|
553
556
|
__privateSet(this, _shuttingDown, true);
|
|
554
557
|
this.heartbeat.stopAll();
|
package/dist/websocket/index.cjs
CHANGED
|
@@ -7,6 +7,9 @@ var platformSocket_io = require('@nestjs/platform-socket.io');
|
|
|
7
7
|
|
|
8
8
|
var __defProp = Object.defineProperty;
|
|
9
9
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
10
|
+
var __typeError = (msg) => {
|
|
11
|
+
throw TypeError(msg);
|
|
12
|
+
};
|
|
10
13
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
14
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
12
15
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
@@ -23,8 +26,13 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
23
26
|
return result;
|
|
24
27
|
};
|
|
25
28
|
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
26
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);
|
|
29
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
30
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
31
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
32
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
33
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
27
34
|
var CONNECTION_ROOM_PREFIX = "connection";
|
|
35
|
+
var _shuttingDown;
|
|
28
36
|
exports.WebSocketTransport = class WebSocketTransport {
|
|
29
37
|
constructor(connections, rooms, auth, hooks, options) {
|
|
30
38
|
this.connections = connections;
|
|
@@ -32,16 +40,26 @@ exports.WebSocketTransport = class WebSocketTransport {
|
|
|
32
40
|
this.auth = auth;
|
|
33
41
|
this.hooks = hooks;
|
|
34
42
|
this.options = options;
|
|
43
|
+
__publicField(this, "kind", "websocket");
|
|
44
|
+
__publicField(this, "logger", new common.Logger(exports.WebSocketTransport.name));
|
|
45
|
+
__publicField(this, "server", null);
|
|
46
|
+
__publicField(this, "warnedUnattached", false);
|
|
47
|
+
__privateAdd(this, _shuttingDown, false);
|
|
35
48
|
}
|
|
36
49
|
connections;
|
|
37
50
|
rooms;
|
|
38
51
|
auth;
|
|
39
52
|
hooks;
|
|
40
53
|
options;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
54
|
+
onModuleDestroy() {
|
|
55
|
+
__privateSet(this, _shuttingDown, true);
|
|
56
|
+
}
|
|
57
|
+
async beforeApplicationShutdown() {
|
|
58
|
+
__privateSet(this, _shuttingDown, true);
|
|
59
|
+
}
|
|
60
|
+
get shuttingDown() {
|
|
61
|
+
return __privateGet(this, _shuttingDown);
|
|
62
|
+
}
|
|
45
63
|
setServer(server) {
|
|
46
64
|
this.server = server;
|
|
47
65
|
this.logger.log("Socket.IO server wired to WebSocketTransport");
|
|
@@ -181,6 +199,7 @@ exports.WebSocketTransport = class WebSocketTransport {
|
|
|
181
199
|
}
|
|
182
200
|
}
|
|
183
201
|
};
|
|
202
|
+
_shuttingDown = new WeakMap();
|
|
184
203
|
exports.WebSocketTransport = __decorateClass([
|
|
185
204
|
common.Injectable(),
|
|
186
205
|
__decorateParam(0, common.Inject(internal.ConnectionRegistry)),
|
|
@@ -353,7 +372,15 @@ exports.RealtimeGateway = class RealtimeGateway {
|
|
|
353
372
|
authenticated = /* @__PURE__ */ new WeakMap();
|
|
354
373
|
authenticateHandshake = async (socket, next) => {
|
|
355
374
|
try {
|
|
375
|
+
if (this.transport.shuttingDown) {
|
|
376
|
+
next(this.unavailable());
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
356
379
|
const auth = await this.transport.authenticator().authenticate(this.buildAuthContext(socket));
|
|
380
|
+
if (this.transport.shuttingDown) {
|
|
381
|
+
next(this.unavailable());
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
357
384
|
if (!auth) {
|
|
358
385
|
const refusal = new Error(internal.REALTIME_ERROR_CODES.AUTH_FAILED);
|
|
359
386
|
refusal.data = { code: internal.REALTIME_ERROR_CODES.AUTH_FAILED };
|
|
@@ -369,6 +396,11 @@ exports.RealtimeGateway = class RealtimeGateway {
|
|
|
369
396
|
next(new Error("Internal Server Error"));
|
|
370
397
|
}
|
|
371
398
|
};
|
|
399
|
+
unavailable() {
|
|
400
|
+
const refusal = new Error(internal.REALTIME_ERROR_CODES.TRANSPORT_UNAVAILABLE);
|
|
401
|
+
refusal.data = { code: internal.REALTIME_ERROR_CODES.TRANSPORT_UNAVAILABLE };
|
|
402
|
+
return refusal;
|
|
403
|
+
}
|
|
372
404
|
afterInit(server) {
|
|
373
405
|
server.use(this.authenticateHandshake);
|
|
374
406
|
this.transport.setServer(server);
|
|
@@ -405,6 +437,10 @@ exports.RealtimeGateway = class RealtimeGateway {
|
|
|
405
437
|
let resolvedAuth = this.authenticated.get(socket);
|
|
406
438
|
this.authenticated.delete(socket);
|
|
407
439
|
if (!resolvedAuth) {
|
|
440
|
+
if (this.transport.shuttingDown) {
|
|
441
|
+
socket.disconnect(true);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
408
444
|
const auth = await this.transport.authenticator().authenticate(this.buildAuthContext(socket));
|
|
409
445
|
if (!auth) {
|
|
410
446
|
socket.disconnect(true);
|
|
@@ -72,6 +72,7 @@ declare class BymaxRealtimeWebSocketModule {
|
|
|
72
72
|
* the gateway. All emit methods are safe no-ops while the server is unset.
|
|
73
73
|
*/
|
|
74
74
|
declare class WebSocketTransport implements ITransport {
|
|
75
|
+
#private;
|
|
75
76
|
private readonly connections;
|
|
76
77
|
private readonly rooms;
|
|
77
78
|
private readonly auth;
|
|
@@ -83,6 +84,26 @@ declare class WebSocketTransport implements ITransport {
|
|
|
83
84
|
private server;
|
|
84
85
|
private warnedUnattached;
|
|
85
86
|
constructor(connections: ConnectionRegistry, rooms: RoomRegistry, auth: IConnectionAuthenticator, hooks: IConnectionLifecycleHooks, options: BymaxRealtimeModuleOptions);
|
|
87
|
+
/**
|
|
88
|
+
* Mark shutdown as begun at the first hook Nest runs on close, so that no hook of the host's, however
|
|
89
|
+
* slow, can leave a window in which a handshake is accepted.
|
|
90
|
+
*
|
|
91
|
+
* Nest runs every module's `onModuleDestroy` before any `beforeApplicationShutdown`, and within a
|
|
92
|
+
* phase it runs a module nearer the root before one it imports, so a slow `beforeApplicationShutdown`
|
|
93
|
+
* in the host's root module runs before this transport's own. A slow `onModuleDestroy` in a module
|
|
94
|
+
* nearer the root than the realtime module still runs first; nothing the library owns runs earlier.
|
|
95
|
+
*/
|
|
96
|
+
onModuleDestroy(): void;
|
|
97
|
+
/**
|
|
98
|
+
* Mark shutdown as begun, for a caller that runs only this hook; `onModuleDestroy` already did.
|
|
99
|
+
*
|
|
100
|
+
* Socket.IO closes every connection that is open when the server closes, so no connection is
|
|
101
|
+
* torn down here; a socket accepted after this point would only be closed a moment later, and
|
|
102
|
+
* its authenticator may already depend on a store the host has closed.
|
|
103
|
+
*/
|
|
104
|
+
beforeApplicationShutdown(): Promise<void>;
|
|
105
|
+
/** Whether shutdown has begun, after which a new handshake is refused. */
|
|
106
|
+
get shuttingDown(): boolean;
|
|
86
107
|
/**
|
|
87
108
|
* Wire the Socket.IO server. Called by `RealtimeGateway.afterInit`.
|
|
88
109
|
*
|
|
@@ -238,6 +259,8 @@ declare class RealtimeGateway implements OnGatewayInit, OnGatewayConnection, OnG
|
|
|
238
259
|
private readonly authenticated;
|
|
239
260
|
/** Authenticate before accept: a refusal is a `connect_error` with `data.code`; a failure is generic. */
|
|
240
261
|
private readonly authenticateHandshake;
|
|
262
|
+
/** The refusal for a handshake that arrives once shutdown has begun: a `connect_error` carrying `data.code`. */
|
|
263
|
+
private unavailable;
|
|
241
264
|
/** Wire the resolved Socket.IO server to the transport after gateway init. */
|
|
242
265
|
afterInit(server: Server): void;
|
|
243
266
|
/** Report a server that `RealtimeIoAdapter` did not create: its options are dropped. */
|
|
@@ -72,6 +72,7 @@ declare class BymaxRealtimeWebSocketModule {
|
|
|
72
72
|
* the gateway. All emit methods are safe no-ops while the server is unset.
|
|
73
73
|
*/
|
|
74
74
|
declare class WebSocketTransport implements ITransport {
|
|
75
|
+
#private;
|
|
75
76
|
private readonly connections;
|
|
76
77
|
private readonly rooms;
|
|
77
78
|
private readonly auth;
|
|
@@ -83,6 +84,26 @@ declare class WebSocketTransport implements ITransport {
|
|
|
83
84
|
private server;
|
|
84
85
|
private warnedUnattached;
|
|
85
86
|
constructor(connections: ConnectionRegistry, rooms: RoomRegistry, auth: IConnectionAuthenticator, hooks: IConnectionLifecycleHooks, options: BymaxRealtimeModuleOptions);
|
|
87
|
+
/**
|
|
88
|
+
* Mark shutdown as begun at the first hook Nest runs on close, so that no hook of the host's, however
|
|
89
|
+
* slow, can leave a window in which a handshake is accepted.
|
|
90
|
+
*
|
|
91
|
+
* Nest runs every module's `onModuleDestroy` before any `beforeApplicationShutdown`, and within a
|
|
92
|
+
* phase it runs a module nearer the root before one it imports, so a slow `beforeApplicationShutdown`
|
|
93
|
+
* in the host's root module runs before this transport's own. A slow `onModuleDestroy` in a module
|
|
94
|
+
* nearer the root than the realtime module still runs first; nothing the library owns runs earlier.
|
|
95
|
+
*/
|
|
96
|
+
onModuleDestroy(): void;
|
|
97
|
+
/**
|
|
98
|
+
* Mark shutdown as begun, for a caller that runs only this hook; `onModuleDestroy` already did.
|
|
99
|
+
*
|
|
100
|
+
* Socket.IO closes every connection that is open when the server closes, so no connection is
|
|
101
|
+
* torn down here; a socket accepted after this point would only be closed a moment later, and
|
|
102
|
+
* its authenticator may already depend on a store the host has closed.
|
|
103
|
+
*/
|
|
104
|
+
beforeApplicationShutdown(): Promise<void>;
|
|
105
|
+
/** Whether shutdown has begun, after which a new handshake is refused. */
|
|
106
|
+
get shuttingDown(): boolean;
|
|
86
107
|
/**
|
|
87
108
|
* Wire the Socket.IO server. Called by `RealtimeGateway.afterInit`.
|
|
88
109
|
*
|
|
@@ -238,6 +259,8 @@ declare class RealtimeGateway implements OnGatewayInit, OnGatewayConnection, OnG
|
|
|
238
259
|
private readonly authenticated;
|
|
239
260
|
/** Authenticate before accept: a refusal is a `connect_error` with `data.code`; a failure is generic. */
|
|
240
261
|
private readonly authenticateHandshake;
|
|
262
|
+
/** The refusal for a handshake that arrives once shutdown has begun: a `connect_error` carrying `data.code`. */
|
|
263
|
+
private unavailable;
|
|
241
264
|
/** Wire the resolved Socket.IO server to the transport after gateway init. */
|
|
242
265
|
afterInit(server: Server): void;
|
|
243
266
|
/** Report a server that `RealtimeIoAdapter` did not create: its options are dropped. */
|
package/dist/websocket/index.mjs
CHANGED
|
@@ -7,6 +7,9 @@ import { IoAdapter } from '@nestjs/platform-socket.io';
|
|
|
7
7
|
const require$1 = createRequire(import.meta.url || __filename);
|
|
8
8
|
var __defProp = Object.defineProperty;
|
|
9
9
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
10
|
+
var __typeError = (msg) => {
|
|
11
|
+
throw TypeError(msg);
|
|
12
|
+
};
|
|
10
13
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
14
|
var __require = /* @__PURE__ */ ((x) => typeof require$1 !== "undefined" ? require$1 : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
12
15
|
get: (a, b) => (typeof require$1 !== "undefined" ? require$1 : a)[b]
|
|
@@ -23,8 +26,13 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
23
26
|
return result;
|
|
24
27
|
};
|
|
25
28
|
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
26
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);
|
|
29
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
30
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
31
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
32
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
33
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
27
34
|
var CONNECTION_ROOM_PREFIX = "connection";
|
|
35
|
+
var _shuttingDown;
|
|
28
36
|
var WebSocketTransport = class {
|
|
29
37
|
constructor(connections, rooms, auth, hooks, options) {
|
|
30
38
|
this.connections = connections;
|
|
@@ -32,16 +40,26 @@ var WebSocketTransport = class {
|
|
|
32
40
|
this.auth = auth;
|
|
33
41
|
this.hooks = hooks;
|
|
34
42
|
this.options = options;
|
|
43
|
+
__publicField(this, "kind", "websocket");
|
|
44
|
+
__publicField(this, "logger", new Logger(WebSocketTransport.name));
|
|
45
|
+
__publicField(this, "server", null);
|
|
46
|
+
__publicField(this, "warnedUnattached", false);
|
|
47
|
+
__privateAdd(this, _shuttingDown, false);
|
|
35
48
|
}
|
|
36
49
|
connections;
|
|
37
50
|
rooms;
|
|
38
51
|
auth;
|
|
39
52
|
hooks;
|
|
40
53
|
options;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
54
|
+
onModuleDestroy() {
|
|
55
|
+
__privateSet(this, _shuttingDown, true);
|
|
56
|
+
}
|
|
57
|
+
async beforeApplicationShutdown() {
|
|
58
|
+
__privateSet(this, _shuttingDown, true);
|
|
59
|
+
}
|
|
60
|
+
get shuttingDown() {
|
|
61
|
+
return __privateGet(this, _shuttingDown);
|
|
62
|
+
}
|
|
45
63
|
setServer(server) {
|
|
46
64
|
this.server = server;
|
|
47
65
|
this.logger.log("Socket.IO server wired to WebSocketTransport");
|
|
@@ -181,6 +199,7 @@ var WebSocketTransport = class {
|
|
|
181
199
|
}
|
|
182
200
|
}
|
|
183
201
|
};
|
|
202
|
+
_shuttingDown = new WeakMap();
|
|
184
203
|
WebSocketTransport = __decorateClass([
|
|
185
204
|
Injectable(),
|
|
186
205
|
__decorateParam(0, Inject(ConnectionRegistry)),
|
|
@@ -353,7 +372,15 @@ var RealtimeGateway = class {
|
|
|
353
372
|
authenticated = /* @__PURE__ */ new WeakMap();
|
|
354
373
|
authenticateHandshake = async (socket, next) => {
|
|
355
374
|
try {
|
|
375
|
+
if (this.transport.shuttingDown) {
|
|
376
|
+
next(this.unavailable());
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
356
379
|
const auth = await this.transport.authenticator().authenticate(this.buildAuthContext(socket));
|
|
380
|
+
if (this.transport.shuttingDown) {
|
|
381
|
+
next(this.unavailable());
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
357
384
|
if (!auth) {
|
|
358
385
|
const refusal = new Error(REALTIME_ERROR_CODES.AUTH_FAILED);
|
|
359
386
|
refusal.data = { code: REALTIME_ERROR_CODES.AUTH_FAILED };
|
|
@@ -369,6 +396,11 @@ var RealtimeGateway = class {
|
|
|
369
396
|
next(new Error("Internal Server Error"));
|
|
370
397
|
}
|
|
371
398
|
};
|
|
399
|
+
unavailable() {
|
|
400
|
+
const refusal = new Error(REALTIME_ERROR_CODES.TRANSPORT_UNAVAILABLE);
|
|
401
|
+
refusal.data = { code: REALTIME_ERROR_CODES.TRANSPORT_UNAVAILABLE };
|
|
402
|
+
return refusal;
|
|
403
|
+
}
|
|
372
404
|
afterInit(server) {
|
|
373
405
|
server.use(this.authenticateHandshake);
|
|
374
406
|
this.transport.setServer(server);
|
|
@@ -405,6 +437,10 @@ var RealtimeGateway = class {
|
|
|
405
437
|
let resolvedAuth = this.authenticated.get(socket);
|
|
406
438
|
this.authenticated.delete(socket);
|
|
407
439
|
if (!resolvedAuth) {
|
|
440
|
+
if (this.transport.shuttingDown) {
|
|
441
|
+
socket.disconnect(true);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
408
444
|
const auth = await this.transport.authenticator().authenticate(this.buildAuthContext(socket));
|
|
409
445
|
if (!auth) {
|
|
410
446
|
socket.disconnect(true);
|
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.11",
|
|
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",
|
|
@@ -96,7 +96,7 @@
|
|
|
96
96
|
"build": "pnpm clean && tsup && node scripts/strip-bundle-comments.mjs",
|
|
97
97
|
"check:exports": "attw --pack . --profile strict",
|
|
98
98
|
"check:mutants": "node scripts/check-mutation-directives.mjs",
|
|
99
|
-
"check:file-
|
|
99
|
+
"check:file-length": "node scripts/check-file-length.mjs",
|
|
100
100
|
"check:api": "node scripts/check-api-surface.mjs",
|
|
101
101
|
"check:published": "node scripts/check-published-surface.mjs",
|
|
102
102
|
"check:runtime": "node scripts/check-consumer-runtime.mjs",
|
|
@@ -108,7 +108,7 @@
|
|
|
108
108
|
"mutation": "stryker run",
|
|
109
109
|
"mutation:full": "node -e \"require('node:fs').rmSync('reports/stryker-incremental.json',{force:true,recursive:true})\" && stryker run",
|
|
110
110
|
"mutation:dry-run": "stryker run --dryRunOnly",
|
|
111
|
-
"prepublishOnly": "pnpm clean && pnpm typecheck && pnpm lint && pnpm check:mutants && pnpm check:file-
|
|
111
|
+
"prepublishOnly": "pnpm clean && pnpm typecheck && pnpm lint && pnpm check:mutants && pnpm check:file-length && pnpm test:cov:all && pnpm build && pnpm check:published && pnpm check:surface && pnpm check:runtime",
|
|
112
112
|
"release": "npm publish --provenance --access public",
|
|
113
113
|
"size": "node scripts/check-size.mjs",
|
|
114
114
|
"test": "jest",
|