@tsed/socketio 7.33.4 → 7.34.1

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.
Files changed (35) hide show
  1. package/jest.config.js +3 -3
  2. package/lib/cjs/class/SocketHandlersBuilder.js +6 -24
  3. package/lib/cjs/class/SocketHandlersBuilder.js.map +1 -1
  4. package/lib/cjs/class/SocketSessionData.js +73 -0
  5. package/lib/cjs/class/SocketSessionData.js.map +1 -0
  6. package/lib/cjs/decorators/socketSession.js +27 -3
  7. package/lib/cjs/decorators/socketSession.js.map +1 -1
  8. package/lib/cjs/index.js +1 -1
  9. package/lib/cjs/index.js.map +1 -1
  10. package/lib/cjs/interfaces/SocketFilters.js +1 -0
  11. package/lib/cjs/interfaces/SocketFilters.js.map +1 -1
  12. package/lib/esm/class/SocketHandlersBuilder.js +6 -24
  13. package/lib/esm/class/SocketHandlersBuilder.js.map +1 -1
  14. package/lib/esm/class/SocketSessionData.js +69 -0
  15. package/lib/esm/class/SocketSessionData.js.map +1 -0
  16. package/lib/esm/decorators/socketSession.js +25 -2
  17. package/lib/esm/decorators/socketSession.js.map +1 -1
  18. package/lib/esm/index.js +1 -1
  19. package/lib/esm/index.js.map +1 -1
  20. package/lib/esm/interfaces/SocketFilters.js +1 -0
  21. package/lib/esm/interfaces/SocketFilters.js.map +1 -1
  22. package/lib/tsconfig.esm.tsbuildinfo +1 -1
  23. package/lib/tsconfig.tsbuildinfo +1 -1
  24. package/lib/types/class/SocketHandlersBuilder.d.ts +1 -11
  25. package/lib/types/class/SocketSessionData.d.ts +23 -0
  26. package/lib/types/decorators/socketSession.d.ts +24 -3
  27. package/lib/types/index.d.ts +1 -1
  28. package/lib/types/interfaces/SocketFilters.d.ts +1 -0
  29. package/package.json +15 -15
  30. package/readme.md +29 -1
  31. package/lib/cjs/registries/NspSessionRegistry.js +0 -16
  32. package/lib/cjs/registries/NspSessionRegistry.js.map +0 -1
  33. package/lib/esm/registries/NspSessionRegistry.js +0 -12
  34. package/lib/esm/registries/NspSessionRegistry.js.map +0 -1
  35. package/lib/types/registries/NspSessionRegistry.d.ts +0 -1
@@ -6,7 +6,7 @@ import { Namespace, Socket } from "socket.io";
6
6
  export declare class SocketHandlersBuilder {
7
7
  private provider;
8
8
  private injector;
9
- private socketProviderMetadata;
9
+ private readonly socketProviderMetadata;
10
10
  constructor(provider: Provider, injector: InjectorService);
11
11
  /**
12
12
  *
@@ -27,16 +27,6 @@ export declare class SocketHandlersBuilder {
27
27
  */
28
28
  onConnection(socket: Socket, nsp: Namespace): Promise<void>;
29
29
  onDisconnect(socket: Socket, nsp: Namespace, reason?: string): Promise<void>;
30
- /**
31
- *
32
- * @param {Socket} socket
33
- */
34
- private createSession;
35
- /**
36
- *
37
- * @param {Socket} socket
38
- */
39
- private destroySession;
40
30
  private buildHandlers;
41
31
  private runQueue;
42
32
  private deserialize;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Represents session data associated with a socket connection, providing a compatibility layer with Ts.ED v7 by wrapping a `socket.data` which is used to archive recoverability and resiliency to a `Map` instance.
3
+ *
4
+ * This class implements the `Map<string, unknown>` interface and provides methods and properties to manage and manipulate session data.
5
+ *
6
+ * @internal This class is designed for internal use.
7
+ */
8
+ export declare class SocketSessionData extends Map<string, unknown> {
9
+ #private;
10
+ get size(): number;
11
+ static get [Symbol.species](): MapConstructor;
12
+ constructor(data: Record<string, unknown>);
13
+ [Symbol.iterator](): IterableIterator<[string, unknown]>;
14
+ clear(): void;
15
+ delete(key: string): boolean;
16
+ entries(): IterableIterator<[string, unknown]>;
17
+ forEach(callbackfn: (value: unknown, key: string, map: Map<string, unknown>) => void, thisArg?: any): void;
18
+ get(key: string): unknown | undefined;
19
+ has(key: string): boolean;
20
+ keys(): IterableIterator<string>;
21
+ set(key: string, value: unknown): this;
22
+ values(): IterableIterator<unknown>;
23
+ }
@@ -1,6 +1,6 @@
1
1
  export type SocketSession = Map<string, any>;
2
2
  /**
3
- * Inject the Socket instance in the decorated parameter.
3
+ * Inject the socket session data in the decorated parameter.
4
4
  *
5
5
  * ### Example
6
6
  *
@@ -9,7 +9,7 @@ export type SocketSession = Map<string, any>;
9
9
  * export class MyWS {
10
10
  *
11
11
  * @Input("event")
12
- * myMethod(@SocketSession session: Map) {
12
+ * myMethod(@SocketSession session: SocketSession) {
13
13
  * console.log(session);
14
14
  * }
15
15
  * }
@@ -20,4 +20,25 @@ export type SocketSession = Map<string, any>;
20
20
  * @param {number} index
21
21
  * @decorator
22
22
  */
23
- export declare function SocketSession(target: any, propertyKey: string, index: number): any;
23
+ export declare function SocketSession(target: any, propertyKey: string, index: number): void;
24
+ /**
25
+ * Inject the raw `socket.data` in the decorated parameter.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * @SocketService("/nsp")
30
+ * export class MyWS {
31
+ *
32
+ * @Input("event")
33
+ * myMethod(@RawSocketSession session: Record<string, unknown>) {
34
+ * console.log(session);
35
+ * }
36
+ * }
37
+ * ```
38
+ *
39
+ * @param target
40
+ * @param {string} propertyKey
41
+ * @param {number} index
42
+ * @constructor
43
+ */
44
+ export declare function RawSocketSession(target: any, propertyKey: string, index: number): void;
@@ -4,6 +4,7 @@
4
4
  export * from "./SocketIOModule";
5
5
  export * from "./class/SocketHandlersBuilder";
6
6
  export * from "./class/SocketProviderMetadata";
7
+ export * from "./class/SocketSessionData";
7
8
  export * from "./constants/constants";
8
9
  export * from "./decorators/args";
9
10
  export * from "./decorators/broadcast";
@@ -36,6 +37,5 @@ export * from "./interfaces/SocketParamMetadata";
36
37
  export * from "./interfaces/SocketProviderTypes";
37
38
  export * from "./interfaces/SocketReturnsTypes";
38
39
  export * from "./interfaces/interfaces";
39
- export * from "./registries/NspSessionRegistry";
40
40
  export * from "./services/SocketIOServer";
41
41
  export * from "./services/SocketIOService";
@@ -7,6 +7,7 @@ export declare enum SocketFilters {
7
7
  EVENT_NAME = "eventName",
8
8
  NSP = "nsp",
9
9
  SESSION = "session",
10
+ RAW_SESSION = "raw_session",
10
11
  ERR = "error",
11
12
  SOCKET_NSP = "socket_nsp",
12
13
  REASON = "reason"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/socketio",
3
- "version": "7.33.4",
3
+ "version": "7.34.1",
4
4
  "description": "Socket.io package for Ts.ED framework",
5
5
  "private": false,
6
6
  "keywords": [
@@ -33,27 +33,27 @@
33
33
  "tslib": "2.5.0"
34
34
  },
35
35
  "devDependencies": {
36
- "@tsed/common": "7.33.4",
37
- "@tsed/core": "7.33.4",
38
- "@tsed/di": "7.33.4",
39
- "@tsed/eslint": "7.33.4",
40
- "@tsed/json-mapper": "7.33.4",
41
- "@tsed/openspec": "7.33.4",
42
- "@tsed/platform-middlewares": "7.33.4",
43
- "@tsed/schema": "7.33.4",
44
- "@tsed/socketio-testing": "7.33.4",
45
- "@tsed/typescript": "7.33.4",
36
+ "@tsed/common": "7.34.1",
37
+ "@tsed/core": "7.34.1",
38
+ "@tsed/di": "7.34.1",
39
+ "@tsed/eslint": "7.34.1",
40
+ "@tsed/json-mapper": "7.34.1",
41
+ "@tsed/openspec": "7.34.1",
42
+ "@tsed/platform-middlewares": "7.34.1",
43
+ "@tsed/schema": "7.34.1",
44
+ "@tsed/socketio-testing": "7.34.1",
45
+ "@tsed/typescript": "7.34.1",
46
46
  "eslint": "^8.12.0",
47
47
  "express": "^4.17.1",
48
48
  "socket.io": "4.7.1",
49
49
  "socket.io-client": "^4.7.1"
50
50
  },
51
51
  "peerDependencies": {
52
- "@tsed/di": "^7.33.4",
53
- "@tsed/json-mapper": "^7.33.4",
52
+ "@tsed/di": "^7.34.1",
53
+ "@tsed/json-mapper": "^7.34.1",
54
54
  "@tsed/logger": ">=6.2.2",
55
- "@tsed/platform-middlewares": "^7.33.4",
56
- "@tsed/schema": "^7.33.4",
55
+ "@tsed/platform-middlewares": "^7.34.1",
56
+ "@tsed/schema": "^7.34.1",
57
57
  "socket.io": ">=4.0.0"
58
58
  },
59
59
  "peerDependenciesMeta": {
package/readme.md CHANGED
@@ -197,6 +197,33 @@ export class MySocketService {
197
197
  }
198
198
  ```
199
199
 
200
+ The session represents an arbitrary object that facilitates the storage of session data, allowing the sharing of information between Socket.IO servers.
201
+
202
+ In the event of an unexpected disconnection (i.e., when the socket is not manually disconnected using `socket.disconnect()`), the server will store the session of the socket. Upon reconnection, the server will make an attempt to restore the previous session.
203
+
204
+ To enable this behavior, you need to configure the [Connection state recovery](https://socket.io/docs/v4/connection-state-recovery) as follows:
205
+
206
+ ```ts
207
+ import {Configuration} from "@tsed/di";
208
+ import "@tsed/platform-express";
209
+ import "@tsed/socketio";
210
+
211
+ @Configuration({
212
+ socketIO: {
213
+ // ... see configuration
214
+ connectionStateRecovery: {
215
+ // the backup duration of the sessions and the packets
216
+ maxDisconnectionDuration: 2 * 60 * 1000,
217
+ // whether to skip middlewares upon successful recovery
218
+ skipMiddlewares: true
219
+ }
220
+ }
221
+ })
222
+ export class Server {}
223
+ ```
224
+
225
+ > By default, Ts.ED uses the built-in in-memory adapter for session management. However, for production environments, it is recommended to use [the persistent adapters](https://socket.io/docs/v4/connection-state-recovery#compatibility-with-existing-adapters) to enhance reliability.
226
+
200
227
  ### Middlewares
201
228
 
202
229
  A middleware can be also used on a `SocketService` either on a class or on a method.
@@ -258,6 +285,7 @@ Middlewares chain use the `Promise` to run it. If one of this middlewares/method
258
285
  import {SocketService, SocketUseAfter, SocketUseBefore, Emit, Input, Args, SocketSession} from "@tsed/socketio";
259
286
  import {UserConverterSocketMiddleware, ErrorHandlerSocketMiddleware} from "../middlewares";
260
287
  import {User} from "../models/User";
288
+ import {SocketSessionData} from "@tsed/socketio/lib/cjs";
261
289
 
262
290
  @SocketService("/my-namespace")
263
291
  @SocketUseBefore(UserConverterSocketMiddleware) // global version
@@ -267,7 +295,7 @@ export class MySocketService {
267
295
  @Emit("responseEventName") // or Broadcast or BroadcastOthers
268
296
  @SocketUseBefore(UserConverterSocketMiddleware)
269
297
  @SocketUseAfter(ErrorHandlerSocketMiddleware)
270
- async myMethod(@Args(0) userName: User) {
298
+ async myMethod(@Args(0) userName: User, @SocketSessionData session: SocketSessionData) {
271
299
  const user = session.get("user") || {};
272
300
  user.name = userName;
273
301
 
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getNspSession = void 0;
4
- /**
5
- *
6
- * @type {Map<any, any>}
7
- */
8
- const SESSIONS = new Map();
9
- function getNspSession(namespace = "/") {
10
- if (!SESSIONS.has(namespace)) {
11
- SESSIONS.set(namespace, new Map());
12
- }
13
- return SESSIONS.get(namespace);
14
- }
15
- exports.getNspSession = getNspSession;
16
- //# sourceMappingURL=NspSessionRegistry.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"NspSessionRegistry.js","sourceRoot":"","sources":["../../../src/registries/NspSessionRegistry.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACH,MAAM,QAAQ,GAA2C,IAAI,GAAG,EAAE,CAAC;AAEnE,SAAgB,aAAa,CAAC,YAA6B,GAAG;IAC5D,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE;QAC5B,QAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,IAAI,GAAG,EAAE,CAAC,CAAC;KACpC;IAED,OAAO,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;AACjC,CAAC;AAND,sCAMC"}
@@ -1,12 +0,0 @@
1
- /**
2
- *
3
- * @type {Map<any, any>}
4
- */
5
- const SESSIONS = new Map();
6
- export function getNspSession(namespace = "/") {
7
- if (!SESSIONS.has(namespace)) {
8
- SESSIONS.set(namespace, new Map());
9
- }
10
- return SESSIONS.get(namespace);
11
- }
12
- //# sourceMappingURL=NspSessionRegistry.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"NspSessionRegistry.js","sourceRoot":"","sources":["../../../src/registries/NspSessionRegistry.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,QAAQ,GAA2C,IAAI,GAAG,EAAE,CAAC;AAEnE,MAAM,UAAU,aAAa,CAAC,YAA6B,GAAG;IAC5D,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE;QAC5B,QAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,IAAI,GAAG,EAAE,CAAC,CAAC;KACpC;IAED,OAAO,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;AACjC,CAAC"}
@@ -1 +0,0 @@
1
- export declare function getNspSession(namespace?: string | RegExp): Map<string, any> | undefined;