@bymax-one/nest-realtime 1.2.8 → 1.2.10
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 +119 -1
- package/README.md +16 -16
- package/dist/internal/index.cjs +162 -49
- package/dist/internal/index.d.cts +75 -9
- package/dist/internal/index.d.ts +75 -9
- package/dist/internal/index.mjs +163 -51
- package/dist/websocket/index.cjs +55 -18
- package/dist/websocket/index.d.cts +19 -3
- package/dist/websocket/index.d.ts +19 -3
- package/dist/websocket/index.mjs +56 -19
- package/package.json +3 -2
|
@@ -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,16 @@ 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, so the gateway refuses a handshake from here on.
|
|
89
|
+
*
|
|
90
|
+
* Socket.IO closes every connection that is open when the server closes, so no connection is
|
|
91
|
+
* torn down here; a socket accepted after this point would only be closed a moment later, and
|
|
92
|
+
* its authenticator may already depend on a store the host has closed.
|
|
93
|
+
*/
|
|
94
|
+
beforeApplicationShutdown(): Promise<void>;
|
|
95
|
+
/** Whether shutdown has begun, after which a new handshake is refused. */
|
|
96
|
+
get shuttingDown(): boolean;
|
|
86
97
|
/**
|
|
87
98
|
* Wire the Socket.IO server. Called by `RealtimeGateway.afterInit`.
|
|
88
99
|
*
|
|
@@ -238,6 +249,8 @@ declare class RealtimeGateway implements OnGatewayInit, OnGatewayConnection, OnG
|
|
|
238
249
|
private readonly authenticated;
|
|
239
250
|
/** Authenticate before accept: a refusal is a `connect_error` with `data.code`; a failure is generic. */
|
|
240
251
|
private readonly authenticateHandshake;
|
|
252
|
+
/** The refusal for a handshake that arrives once shutdown has begun: a `connect_error` carrying `data.code`. */
|
|
253
|
+
private unavailable;
|
|
241
254
|
/** Wire the resolved Socket.IO server to the transport after gateway init. */
|
|
242
255
|
afterInit(server: Server): void;
|
|
243
256
|
/** Report a server that `RealtimeIoAdapter` did not create: its options are dropped. */
|
|
@@ -350,7 +363,8 @@ declare class RealtimeIoAdapter extends IoAdapter {
|
|
|
350
363
|
*
|
|
351
364
|
* Every `emitTo*` and `broadcast` invokes both SSE and WebSocket transports in
|
|
352
365
|
* parallel via `Promise.allSettled` so a failure in one never aborts the other.
|
|
353
|
-
*
|
|
366
|
+
* A failure of one is logged as a warning and the call resolves; a failure of both
|
|
367
|
+
* rejects.
|
|
354
368
|
*
|
|
355
369
|
* `joinRoom`, `leaveRoom`, and `disconnect` ask both transports and settle both: each
|
|
356
370
|
* transport acts only on a connection it holds, and a connection neither holds is handed
|
|
@@ -432,10 +446,12 @@ declare class CompositeTransport implements ITransport {
|
|
|
432
446
|
/**
|
|
433
447
|
* Run two transport tasks in parallel via `Promise.allSettled`.
|
|
434
448
|
*
|
|
435
|
-
*
|
|
436
|
-
*
|
|
449
|
+
* One task failing is logged as a warning and the call resolves, so one failing transport
|
|
450
|
+
* never blocks delivery on the other. Every task failing rejects.
|
|
437
451
|
*/
|
|
438
452
|
private fanOut;
|
|
453
|
+
/** The rejection for an operation that failed on every transport, with every reason in `cause`. */
|
|
454
|
+
private allFailed;
|
|
439
455
|
}
|
|
440
456
|
|
|
441
457
|
export { BymaxRealtimeWebSocketModule, CompositeTransport, RealtimeGateway, RealtimeIoAdapter, WebSocketTransport };
|
package/dist/websocket/index.mjs
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { createRequire } from 'node:module';
|
|
2
2
|
import { Injectable, Inject, Logger, Global, Module } from '@nestjs/common';
|
|
3
|
-
import { ConnectionRegistry, RoomRegistry, REALTIME_AUTHENTICATOR_TOKEN, REALTIME_HOOKS_TOKEN, REALTIME_OPTIONS_TOKEN, SseTransport, REALTIME_ERROR_CODES, ROOM_PREFIXES, unavailableError, composeForRoot, composeForRootAsync, REALTIME_TRANSPORT_TOKEN, SseSubscriptionHandler, RealtimePubSubSubscriber, createSseController, resolveAuthTenant, assertIdentityIsNotBlank, describeError, parseCookieHeader, RESERVED_EVENT_NAMES } from '@bymax-one/nest-realtime/internal';
|
|
3
|
+
import { ConnectionRegistry, RoomRegistry, REALTIME_AUTHENTICATOR_TOKEN, REALTIME_HOOKS_TOKEN, REALTIME_OPTIONS_TOKEN, SseTransport, REALTIME_ERROR_CODES, ROOM_PREFIXES, unavailableError, composeForRoot, composeForRootAsync, REALTIME_TRANSPORT_TOKEN, SseSubscriptionHandler, RealtimePubSubSubscriber, createSseController, resolveAuthTenant, assertIdentityIsNotBlank, describeError, parseCookieHeader, RESERVED_EVENT_NAMES, realtimeError } from '@bymax-one/nest-realtime/internal';
|
|
4
4
|
import { WebSocketServer, WebSocketGateway } from '@nestjs/websockets';
|
|
5
5
|
import { IoAdapter } from '@nestjs/platform-socket.io';
|
|
6
6
|
|
|
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,23 @@ 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
|
+
async beforeApplicationShutdown() {
|
|
55
|
+
__privateSet(this, _shuttingDown, true);
|
|
56
|
+
}
|
|
57
|
+
get shuttingDown() {
|
|
58
|
+
return __privateGet(this, _shuttingDown);
|
|
59
|
+
}
|
|
45
60
|
setServer(server) {
|
|
46
61
|
this.server = server;
|
|
47
62
|
this.logger.log("Socket.IO server wired to WebSocketTransport");
|
|
@@ -181,6 +196,7 @@ var WebSocketTransport = class {
|
|
|
181
196
|
}
|
|
182
197
|
}
|
|
183
198
|
};
|
|
199
|
+
_shuttingDown = new WeakMap();
|
|
184
200
|
WebSocketTransport = __decorateClass([
|
|
185
201
|
Injectable(),
|
|
186
202
|
__decorateParam(0, Inject(ConnectionRegistry)),
|
|
@@ -251,21 +267,24 @@ var CompositeTransport = class {
|
|
|
251
267
|
const reasons = results.flatMap((r) => r.status === "rejected" ? [r.reason] : []);
|
|
252
268
|
if (reasons.length === 0) return;
|
|
253
269
|
if (reasons.length === 1) throw reasons[0];
|
|
254
|
-
throw
|
|
255
|
-
REALTIME_ERROR_CODES.TRANSPORT_UNAVAILABLE,
|
|
256
|
-
CompositeTransport.name,
|
|
257
|
-
`${op} of ${connectionId} failed on both transports`,
|
|
258
|
-
new AggregateError(reasons, reasons.map(describeError).join("; "))
|
|
259
|
-
);
|
|
270
|
+
throw this.allFailed(`${op} of ${connectionId}`, reasons);
|
|
260
271
|
}
|
|
261
272
|
async fanOut(op, ...tasks) {
|
|
262
273
|
const results = await Promise.allSettled(tasks.map((t) => t()));
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
}
|
|
274
|
+
const reasons = results.flatMap((r) => r.status === "rejected" ? [r.reason] : []);
|
|
275
|
+
if (reasons.length === tasks.length) throw this.allFailed(op, reasons);
|
|
276
|
+
for (const reason of reasons) {
|
|
277
|
+
this.logger.warn(`Composite ${op} partially failed: ${describeError(reason)}`);
|
|
267
278
|
}
|
|
268
279
|
}
|
|
280
|
+
allFailed(what, reasons) {
|
|
281
|
+
return unavailableError(
|
|
282
|
+
REALTIME_ERROR_CODES.TRANSPORT_UNAVAILABLE,
|
|
283
|
+
CompositeTransport.name,
|
|
284
|
+
`${what} failed on both transports`,
|
|
285
|
+
new AggregateError(reasons, reasons.map(describeError).join("; "))
|
|
286
|
+
);
|
|
287
|
+
}
|
|
269
288
|
};
|
|
270
289
|
CompositeTransport = __decorateClass([
|
|
271
290
|
Injectable(),
|
|
@@ -322,9 +341,10 @@ var RealtimeIoAdapter = class _RealtimeIoAdapter extends IoAdapter {
|
|
|
322
341
|
server.adapter(createAdapter(pub, sub));
|
|
323
342
|
} catch (err) {
|
|
324
343
|
const reason = describeError(err);
|
|
325
|
-
throw
|
|
326
|
-
|
|
327
|
-
{
|
|
344
|
+
throw realtimeError(
|
|
345
|
+
REALTIME_ERROR_CODES.REDIS_ADAPTER_FAILED,
|
|
346
|
+
`websocket.redisAdapter is set but the Socket.IO Redis adapter could not be installed (${reason}). Install @socket.io/redis-adapter and pass an ioredis client as pubClient.`,
|
|
347
|
+
{ who: _RealtimeIoAdapter.name, cause: err }
|
|
328
348
|
);
|
|
329
349
|
}
|
|
330
350
|
this.adapterLogger.log(
|
|
@@ -349,7 +369,15 @@ var RealtimeGateway = class {
|
|
|
349
369
|
authenticated = /* @__PURE__ */ new WeakMap();
|
|
350
370
|
authenticateHandshake = async (socket, next) => {
|
|
351
371
|
try {
|
|
372
|
+
if (this.transport.shuttingDown) {
|
|
373
|
+
next(this.unavailable());
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
352
376
|
const auth = await this.transport.authenticator().authenticate(this.buildAuthContext(socket));
|
|
377
|
+
if (this.transport.shuttingDown) {
|
|
378
|
+
next(this.unavailable());
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
353
381
|
if (!auth) {
|
|
354
382
|
const refusal = new Error(REALTIME_ERROR_CODES.AUTH_FAILED);
|
|
355
383
|
refusal.data = { code: REALTIME_ERROR_CODES.AUTH_FAILED };
|
|
@@ -365,6 +393,11 @@ var RealtimeGateway = class {
|
|
|
365
393
|
next(new Error("Internal Server Error"));
|
|
366
394
|
}
|
|
367
395
|
};
|
|
396
|
+
unavailable() {
|
|
397
|
+
const refusal = new Error(REALTIME_ERROR_CODES.TRANSPORT_UNAVAILABLE);
|
|
398
|
+
refusal.data = { code: REALTIME_ERROR_CODES.TRANSPORT_UNAVAILABLE };
|
|
399
|
+
return refusal;
|
|
400
|
+
}
|
|
368
401
|
afterInit(server) {
|
|
369
402
|
server.use(this.authenticateHandshake);
|
|
370
403
|
this.transport.setServer(server);
|
|
@@ -401,6 +434,10 @@ var RealtimeGateway = class {
|
|
|
401
434
|
let resolvedAuth = this.authenticated.get(socket);
|
|
402
435
|
this.authenticated.delete(socket);
|
|
403
436
|
if (!resolvedAuth) {
|
|
437
|
+
if (this.transport.shuttingDown) {
|
|
438
|
+
socket.disconnect(true);
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
404
441
|
const auth = await this.transport.authenticator().authenticate(this.buildAuthContext(socket));
|
|
405
442
|
if (!auth) {
|
|
406
443
|
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.10",
|
|
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,6 +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-length": "node scripts/check-file-length.mjs",
|
|
99
100
|
"check:api": "node scripts/check-api-surface.mjs",
|
|
100
101
|
"check:published": "node scripts/check-published-surface.mjs",
|
|
101
102
|
"check:runtime": "node scripts/check-consumer-runtime.mjs",
|
|
@@ -107,7 +108,7 @@
|
|
|
107
108
|
"mutation": "stryker run",
|
|
108
109
|
"mutation:full": "node -e \"require('node:fs').rmSync('reports/stryker-incremental.json',{force:true,recursive:true})\" && stryker run",
|
|
109
110
|
"mutation:dry-run": "stryker run --dryRunOnly",
|
|
110
|
-
"prepublishOnly": "pnpm clean && pnpm typecheck && pnpm lint && pnpm check:mutants && pnpm test:cov:all && pnpm build && pnpm check:published && pnpm check:surface && pnpm check:runtime",
|
|
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",
|
|
111
112
|
"release": "npm publish --provenance --access public",
|
|
112
113
|
"size": "node scripts/check-size.mjs",
|
|
113
114
|
"test": "jest",
|