@bymax-one/nest-realtime 1.2.16 → 1.2.17
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 +38 -3
- package/README.md +23 -22
- package/dist/internal/index.cjs +143 -63
- package/dist/internal/index.d.cts +50 -8
- package/dist/internal/index.d.ts +50 -8
- package/dist/internal/index.mjs +142 -64
- package/dist/react/index.cjs +163 -101
- package/dist/react/index.mjs +164 -102
- package/dist/websocket/index.cjs +18 -3
- package/dist/websocket/index.d.cts +4 -0
- package/dist/websocket/index.d.ts +4 -0
- package/dist/websocket/index.mjs +19 -4
- package/package.json +4 -3
package/dist/websocket/index.cjs
CHANGED
|
@@ -105,6 +105,15 @@ exports.WebSocketTransport = class WebSocketTransport {
|
|
|
105
105
|
metadata: auth.metadata
|
|
106
106
|
}
|
|
107
107
|
});
|
|
108
|
+
await this.joinRooms(socket, auth);
|
|
109
|
+
await this.evictBeyondLimit(auth.userId);
|
|
110
|
+
const failed = await this.runOnConnect(socket, auth, connectedAt);
|
|
111
|
+
if (!failed) return;
|
|
112
|
+
const message = `onConnect hook failed: ${internal.describeError(failed.error)}`;
|
|
113
|
+
if (this.options.onConnectFailure === "close") throw new Error(message);
|
|
114
|
+
this.logger.error(message);
|
|
115
|
+
}
|
|
116
|
+
async joinRooms(socket, auth) {
|
|
108
117
|
await socket.join(`${internal.ROOM_PREFIXES.USER}:${auth.userId}`);
|
|
109
118
|
if (auth.tenantId) await socket.join(`${internal.ROOM_PREFIXES.TENANT}:${auth.tenantId}`);
|
|
110
119
|
await socket.join(`${CONNECTION_ROOM_PREFIX}:${socket.id}`);
|
|
@@ -112,8 +121,9 @@ exports.WebSocketTransport = class WebSocketTransport {
|
|
|
112
121
|
this.rooms.join(socket.id, `${internal.ROOM_PREFIXES.USER}:${auth.userId}`);
|
|
113
122
|
if (auth.tenantId) this.rooms.join(socket.id, `${internal.ROOM_PREFIXES.TENANT}:${auth.tenantId}`);
|
|
114
123
|
for (const room of auth.rooms ?? []) this.rooms.join(socket.id, room);
|
|
115
|
-
|
|
116
|
-
|
|
124
|
+
}
|
|
125
|
+
runOnConnect(socket, auth, connectedAt) {
|
|
126
|
+
return internal.runHook(
|
|
117
127
|
() => this.hooks.onConnect?.({
|
|
118
128
|
connectionId: socket.id,
|
|
119
129
|
userId: auth.userId,
|
|
@@ -127,7 +137,6 @@ exports.WebSocketTransport = class WebSocketTransport {
|
|
|
127
137
|
}),
|
|
128
138
|
this.options.hookTimeoutMs ?? internal.DEFAULT_HOOK_TIMEOUT_MS
|
|
129
139
|
);
|
|
130
|
-
if (failed) this.logger.error(`onConnect hook failed: ${internal.describeError(failed.error)}`);
|
|
131
140
|
}
|
|
132
141
|
async unregisterSocket(connectionId, reason) {
|
|
133
142
|
const record = this.connections.unregister(connectionId);
|
|
@@ -159,12 +168,14 @@ exports.WebSocketTransport = class WebSocketTransport {
|
|
|
159
168
|
this.attached("emitToTenant")?.to(`${internal.ROOM_PREFIXES.TENANT}:${tenantId}`).emit(event, data);
|
|
160
169
|
}
|
|
161
170
|
async emitToRoom(roomId, event, data) {
|
|
171
|
+
internal.requireRoomName("emitToRoom", roomId);
|
|
162
172
|
this.attached("emitToRoom")?.to(roomId).emit(event, data);
|
|
163
173
|
}
|
|
164
174
|
async broadcast(event, data) {
|
|
165
175
|
this.attached("broadcast")?.emit(event, data);
|
|
166
176
|
}
|
|
167
177
|
async joinRoom(connectionId, roomId) {
|
|
178
|
+
internal.refuseReservedRoom("joinRoom", roomId);
|
|
168
179
|
const socket = this.localSockets()?.get(connectionId);
|
|
169
180
|
if (socket) {
|
|
170
181
|
await socket.join(roomId);
|
|
@@ -172,6 +183,7 @@ exports.WebSocketTransport = class WebSocketTransport {
|
|
|
172
183
|
}
|
|
173
184
|
}
|
|
174
185
|
async leaveRoom(connectionId, roomId) {
|
|
186
|
+
internal.refuseReservedRoom("leaveRoom", roomId);
|
|
175
187
|
const socket = this.localSockets()?.get(connectionId);
|
|
176
188
|
if (socket) {
|
|
177
189
|
await socket.leave(roomId);
|
|
@@ -253,6 +265,7 @@ exports.CompositeTransport = class CompositeTransport {
|
|
|
253
265
|
);
|
|
254
266
|
}
|
|
255
267
|
async emitToRoom(roomId, event, data) {
|
|
268
|
+
internal.requireRoomName("emitToRoom", roomId);
|
|
256
269
|
await this.fanOut(
|
|
257
270
|
"emitToRoom",
|
|
258
271
|
() => this.sse.emitToRoom(roomId, event, data),
|
|
@@ -267,12 +280,14 @@ exports.CompositeTransport = class CompositeTransport {
|
|
|
267
280
|
);
|
|
268
281
|
}
|
|
269
282
|
async joinRoom(connectionId, roomId) {
|
|
283
|
+
internal.refuseReservedRoom("joinRoom", roomId);
|
|
270
284
|
await this.settleBoth("joinRoom", connectionId, [
|
|
271
285
|
() => this.sse.joinRoom(connectionId, roomId),
|
|
272
286
|
() => this.ws.joinRoom(connectionId, roomId)
|
|
273
287
|
]);
|
|
274
288
|
}
|
|
275
289
|
async leaveRoom(connectionId, roomId) {
|
|
290
|
+
internal.refuseReservedRoom("leaveRoom", roomId);
|
|
276
291
|
await this.settleBoth("leaveRoom", connectionId, [
|
|
277
292
|
() => this.sse.leaveRoom(connectionId, roomId),
|
|
278
293
|
() => this.ws.leaveRoom(connectionId, roomId)
|
|
@@ -152,6 +152,10 @@ declare class WebSocketTransport implements ITransport {
|
|
|
152
152
|
* @param auth - The resolved authentication result containing userId, tenantId, and roles.
|
|
153
153
|
*/
|
|
154
154
|
registerSocket(socket: Socket, auth: AuthenticationResult): Promise<void>;
|
|
155
|
+
/** Join the socket to its canonical rooms and the ones the authenticator named, here and in the registry. */
|
|
156
|
+
private joinRooms;
|
|
157
|
+
/** Run `onConnect` for a registered socket, bounded by `hookTimeoutMs`; resolves with its failure, if any. */
|
|
158
|
+
private runOnConnect;
|
|
155
159
|
/**
|
|
156
160
|
* Unregister a disconnected WebSocket connection.
|
|
157
161
|
*
|
|
@@ -152,6 +152,10 @@ declare class WebSocketTransport implements ITransport {
|
|
|
152
152
|
* @param auth - The resolved authentication result containing userId, tenantId, and roles.
|
|
153
153
|
*/
|
|
154
154
|
registerSocket(socket: Socket, auth: AuthenticationResult): Promise<void>;
|
|
155
|
+
/** Join the socket to its canonical rooms and the ones the authenticator named, here and in the registry. */
|
|
156
|
+
private joinRooms;
|
|
157
|
+
/** Run `onConnect` for a registered socket, bounded by `hookTimeoutMs`; resolves with its failure, if any. */
|
|
158
|
+
private runOnConnect;
|
|
155
159
|
/**
|
|
156
160
|
* Unregister a disconnected WebSocket connection.
|
|
157
161
|
*
|
package/dist/websocket/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createRequire } from 'node:module';
|
|
2
2
|
import { Injectable, Inject, Optional, Logger, Global, Module } from '@nestjs/common';
|
|
3
|
-
import { ConnectionRegistry, RoomRegistry, AuthenticationGate, REALTIME_HOOKS_TOKEN, REALTIME_OPTIONS_TOKEN, ConnectionAdmission, SseTransport, REALTIME_ERROR_CODES, ROOM_PREFIXES, runHook, DEFAULT_HOOK_TIMEOUT_MS,
|
|
3
|
+
import { ConnectionRegistry, RoomRegistry, AuthenticationGate, REALTIME_HOOKS_TOKEN, REALTIME_OPTIONS_TOKEN, ConnectionAdmission, SseTransport, REALTIME_ERROR_CODES, describeError, ROOM_PREFIXES, runHook, DEFAULT_HOOK_TIMEOUT_MS, requireRoomName, refuseReservedRoom, unavailableError, UNBOUNDED_TICKET, composeForRoot, composeForRootAsync, REALTIME_TRANSPORT_TOKEN, SseSubscriptionHandler, RealtimePubSubSubscriber, createSseController, resolveAuthTenant, assertIdentityIsNotBlank, admitRooms, AuthenticationUnavailableError, ConnectionLimitError, parseCookieHeader, RESERVED_EVENT_NAMES, quitWithin, 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
|
|
|
@@ -105,6 +105,15 @@ var WebSocketTransport = class {
|
|
|
105
105
|
metadata: auth.metadata
|
|
106
106
|
}
|
|
107
107
|
});
|
|
108
|
+
await this.joinRooms(socket, auth);
|
|
109
|
+
await this.evictBeyondLimit(auth.userId);
|
|
110
|
+
const failed = await this.runOnConnect(socket, auth, connectedAt);
|
|
111
|
+
if (!failed) return;
|
|
112
|
+
const message = `onConnect hook failed: ${describeError(failed.error)}`;
|
|
113
|
+
if (this.options.onConnectFailure === "close") throw new Error(message);
|
|
114
|
+
this.logger.error(message);
|
|
115
|
+
}
|
|
116
|
+
async joinRooms(socket, auth) {
|
|
108
117
|
await socket.join(`${ROOM_PREFIXES.USER}:${auth.userId}`);
|
|
109
118
|
if (auth.tenantId) await socket.join(`${ROOM_PREFIXES.TENANT}:${auth.tenantId}`);
|
|
110
119
|
await socket.join(`${CONNECTION_ROOM_PREFIX}:${socket.id}`);
|
|
@@ -112,8 +121,9 @@ var WebSocketTransport = class {
|
|
|
112
121
|
this.rooms.join(socket.id, `${ROOM_PREFIXES.USER}:${auth.userId}`);
|
|
113
122
|
if (auth.tenantId) this.rooms.join(socket.id, `${ROOM_PREFIXES.TENANT}:${auth.tenantId}`);
|
|
114
123
|
for (const room of auth.rooms ?? []) this.rooms.join(socket.id, room);
|
|
115
|
-
|
|
116
|
-
|
|
124
|
+
}
|
|
125
|
+
runOnConnect(socket, auth, connectedAt) {
|
|
126
|
+
return runHook(
|
|
117
127
|
() => this.hooks.onConnect?.({
|
|
118
128
|
connectionId: socket.id,
|
|
119
129
|
userId: auth.userId,
|
|
@@ -127,7 +137,6 @@ var WebSocketTransport = class {
|
|
|
127
137
|
}),
|
|
128
138
|
this.options.hookTimeoutMs ?? DEFAULT_HOOK_TIMEOUT_MS
|
|
129
139
|
);
|
|
130
|
-
if (failed) this.logger.error(`onConnect hook failed: ${describeError(failed.error)}`);
|
|
131
140
|
}
|
|
132
141
|
async unregisterSocket(connectionId, reason) {
|
|
133
142
|
const record = this.connections.unregister(connectionId);
|
|
@@ -159,12 +168,14 @@ var WebSocketTransport = class {
|
|
|
159
168
|
this.attached("emitToTenant")?.to(`${ROOM_PREFIXES.TENANT}:${tenantId}`).emit(event, data);
|
|
160
169
|
}
|
|
161
170
|
async emitToRoom(roomId, event, data) {
|
|
171
|
+
requireRoomName("emitToRoom", roomId);
|
|
162
172
|
this.attached("emitToRoom")?.to(roomId).emit(event, data);
|
|
163
173
|
}
|
|
164
174
|
async broadcast(event, data) {
|
|
165
175
|
this.attached("broadcast")?.emit(event, data);
|
|
166
176
|
}
|
|
167
177
|
async joinRoom(connectionId, roomId) {
|
|
178
|
+
refuseReservedRoom("joinRoom", roomId);
|
|
168
179
|
const socket = this.localSockets()?.get(connectionId);
|
|
169
180
|
if (socket) {
|
|
170
181
|
await socket.join(roomId);
|
|
@@ -172,6 +183,7 @@ var WebSocketTransport = class {
|
|
|
172
183
|
}
|
|
173
184
|
}
|
|
174
185
|
async leaveRoom(connectionId, roomId) {
|
|
186
|
+
refuseReservedRoom("leaveRoom", roomId);
|
|
175
187
|
const socket = this.localSockets()?.get(connectionId);
|
|
176
188
|
if (socket) {
|
|
177
189
|
await socket.leave(roomId);
|
|
@@ -253,6 +265,7 @@ var CompositeTransport = class {
|
|
|
253
265
|
);
|
|
254
266
|
}
|
|
255
267
|
async emitToRoom(roomId, event, data) {
|
|
268
|
+
requireRoomName("emitToRoom", roomId);
|
|
256
269
|
await this.fanOut(
|
|
257
270
|
"emitToRoom",
|
|
258
271
|
() => this.sse.emitToRoom(roomId, event, data),
|
|
@@ -267,12 +280,14 @@ var CompositeTransport = class {
|
|
|
267
280
|
);
|
|
268
281
|
}
|
|
269
282
|
async joinRoom(connectionId, roomId) {
|
|
283
|
+
refuseReservedRoom("joinRoom", roomId);
|
|
270
284
|
await this.settleBoth("joinRoom", connectionId, [
|
|
271
285
|
() => this.sse.joinRoom(connectionId, roomId),
|
|
272
286
|
() => this.ws.joinRoom(connectionId, roomId)
|
|
273
287
|
]);
|
|
274
288
|
}
|
|
275
289
|
async leaveRoom(connectionId, roomId) {
|
|
290
|
+
refuseReservedRoom("leaveRoom", roomId);
|
|
276
291
|
await this.settleBoth("leaveRoom", connectionId, [
|
|
277
292
|
() => this.sse.leaveRoom(connectionId, roomId),
|
|
278
293
|
() => this.ws.leaveRoom(connectionId, roomId)
|
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.17",
|
|
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,6 +97,7 @@
|
|
|
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:function-length": "node scripts/check-function-length.mjs",
|
|
100
101
|
"check:spec-headers": "node scripts/check-spec-headers.mjs",
|
|
101
102
|
"check:suppressions": "node scripts/check-suppressions-baseline.mjs",
|
|
102
103
|
"check:api": "node scripts/check-api-surface.mjs",
|
|
@@ -105,7 +106,7 @@
|
|
|
105
106
|
"benchmark": "node scripts/benchmark/run.mjs",
|
|
106
107
|
"check:surface": "node scripts/check-public-surface.mjs",
|
|
107
108
|
"clean": "rm -rf dist coverage",
|
|
108
|
-
"lint": "eslint src && pnpm check:spec-headers && pnpm check:suppressions",
|
|
109
|
+
"lint": "eslint src && pnpm check:spec-headers && pnpm check:suppressions && node --test scripts/check-suppressions-baseline.test.mjs",
|
|
109
110
|
"lint:fix": "eslint src --fix",
|
|
110
111
|
"mutation": "stryker run",
|
|
111
112
|
"mutation:full": "node -e \"require('node:fs').rmSync('reports/stryker-incremental.json',{force:true,recursive:true})\" && stryker run",
|
|
@@ -120,7 +121,7 @@
|
|
|
120
121
|
"test:e2e": "jest --config jest.e2e.config.ts",
|
|
121
122
|
"test:e2e:cross-instance": "jest --config jest.e2e.cross-instance.config.ts --runInBand",
|
|
122
123
|
"test:watch": "jest --watch",
|
|
123
|
-
"verify": "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 size && pnpm check:api",
|
|
124
|
+
"verify": "pnpm clean && pnpm typecheck && pnpm lint && pnpm check:mutants && pnpm check:file-length && pnpm check:function-length && pnpm test:cov:all && pnpm build && pnpm check:published && pnpm check:surface && pnpm size && pnpm check:api",
|
|
124
125
|
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.server.json"
|
|
125
126
|
},
|
|
126
127
|
"peerDependencies": {
|