@emeryld/rrroutes-server 2.5.4 → 2.6.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.
@@ -4,11 +4,14 @@ import type { SocketDebugOptions } from './socket.server.debug';
4
4
  import type { HeartbeatServerOptions, SysServerEventMap } from './socket.server.sys';
5
5
  import type { HandlerCtx } from './socket.server.types';
6
6
  export interface SocketConnection<T extends EventMap> {
7
+ /** Registers a handler for a specific event. The handler receives the parsed payload and a context object with information about the socket and helper methods. Returns an unregister function to remove the handler. */
7
8
  on<K extends keyof T & string>(args: {
8
9
  eventName: K;
9
10
  handler: (payload: Payload<T, K>, ctx: HandlerCtx) => void | Promise<void>;
10
11
  }): () => void;
12
+ /** Unregisters all handlers for a specific event. */
11
13
  off<K extends keyof T & string>(eventName: K): void;
14
+ /** Emits an event with a validated payload to specific rooms or sockets. The payload is validated against the schema defined for the event, and an envelope with metadata is sent. If the event is not registered, it falls back to a raw emit without validation or envelope. */
12
15
  emit<K extends keyof T & string>(args: {
13
16
  eventName: K;
14
17
  payload: Payload<T, K>;
@@ -16,6 +19,7 @@ export interface SocketConnection<T extends EventMap> {
16
19
  metadata?: Record<string, unknown>;
17
20
  onAck?: (ack: unknown) => void;
18
21
  }): void;
22
+ /** Emits an event with an unvalidated payload to specific rooms or sockets. This method does not perform schema validation or envelope wrapping, and is intended for emitting events that are not registered in the events map. */
19
23
  emitAny(args: {
20
24
  eventName: string;
21
25
  payload: unknown;
@@ -23,13 +27,31 @@ export interface SocketConnection<T extends EventMap> {
23
27
  metadata?: Record<string, unknown>;
24
28
  onAck?: (ack: unknown) => void;
25
29
  }): void;
30
+ /** Destroys the socket connection by removing all event handlers, cleaning up resources, and detaching from the Socket.IO server. After calling this method, the SocketConnection instance should not be used. */
26
31
  destroy(): void;
27
32
  }
28
- export declare function createSocketConnections<T extends EventMap, TConfig extends SocketConnectionConfigOutput = SocketConnectionConfigOutput>(io: Server, events: T, opts: {
33
+ export declare function createSocketConnections<T extends EventMap, TConfig extends SocketConnectionConfigOutput = SocketConnectionConfigOutput>(
34
+ /** The Socket.IO server instance to bind to */
35
+ io: Server,
36
+ /** The options for the socket connection, including debug settings, heartbeat configuration, system event handlers, and any custom config values */
37
+ events: T,
38
+ /** The options for the socket connection, including debug settings, heartbeat configuration, system event handlers, and any custom config values */
39
+ opts: {
40
+ /** Optional debug configuration for logging socket events and actions
41
+ * register: when handlers are registered/unregistered
42
+ * handler: when events are received and processed by handlers, including validation results and errors
43
+ * emit: when events are emitted, including payload validation results and errors
44
+ * rooms: when sockets join or leave rooms, including the rooms involved and any issues
45
+ * heartbeat: when heartbeat pings are received and pongs are sent, including any issues or errors
46
+ * config: when a socket connects and receives the initial config snapshot
47
+ * lifecycle: for general lifecycle events like server initialization and destruction
48
+ */
29
49
  debug?: SocketDebugOptions;
50
+ /** Optional heartbeat configuration to enable connection health checks and automatic disconnections */
30
51
  heartbeat?: HeartbeatServerOptions;
31
52
  /** System event customization (connect / disconnect / ping / pong / room join/leave) */
32
53
  sys: SysServerEventMap<TConfig, T>;
54
+ /** Custom config values that can be used in system event handlers via the HandlerCtx */
33
55
  config: TConfig;
34
56
  }): SocketConnection<T>;
35
57
  export type { SocketDebugOptions, SocketServerConfigSnapshot, SocketServerDebugEvent, SocketServerHeartbeatPhase, SocketServerLifecycleStage, } from './socket.server.debug';
@@ -12,6 +12,7 @@ export type HeartbeatServerOptions = {
12
12
  enabled?: boolean;
13
13
  };
14
14
  export type SysServerEventMap<Config extends SocketConnectionConfigOutput = SocketConnectionConfigOutput, Events extends EventMap = EventMap> = {
15
+ /** Handler for new socket connections. Must call `complete()` to proceed with default connection setup (attaching built-in handlers for heartbeat, room join/leave, disconnect, etc). */
15
16
  'sys:connect': (args: {
16
17
  socket: Socket;
17
18
  helper: SocketConnectionHelper<Events>;
@@ -21,6 +22,7 @@ export type SysServerEventMap<Config extends SocketConnectionConfigOutput = Sock
21
22
  */
22
23
  complete: () => void;
23
24
  }) => void | Promise<void>;
25
+ /** Handler for socket disconnections. Must call `cleanup()` to proceed with default disconnect cleanup (removing built-in handlers). */
24
26
  'sys:disconnect': (args: {
25
27
  socket: Socket;
26
28
  helper: SocketConnectionHelper<Events>;
@@ -28,12 +30,14 @@ export type SysServerEventMap<Config extends SocketConnectionConfigOutput = Sock
28
30
  cleanup: () => void;
29
31
  reason: string;
30
32
  }) => void | Promise<void>;
33
+ /** Handler for heartbeat pings. Must return the payload for the corresponding pong response. */
31
34
  'sys:ping': (args: {
32
35
  socket: Socket;
33
36
  helper: SocketConnectionHelper<Events>;
34
37
  ping: SocketSchemaOutput<Config['pingPayload']>;
35
38
  ctx: HandlerCtxNoAck;
36
39
  }) => SocketSchemaOutput<Config['pongPayload']> | Promise<SocketSchemaOutput<Config['pongPayload']>>;
40
+ /** Handler for joining rooms. Must call `join(room)` for each room to be joined. */
37
41
  'sys:room_join': (args: {
38
42
  socket: Socket;
39
43
  helper: SocketConnectionHelper<Events>;
@@ -41,6 +45,7 @@ export type SysServerEventMap<Config extends SocketConnectionConfigOutput = Sock
41
45
  meta: SocketSchemaOutput<Config['joinMetaMessage']>;
42
46
  join: (room: string) => Promise<void>;
43
47
  }) => void | Promise<void>;
48
+ /** Handler for leaving rooms. Must call `leave(room)` for each room to be left. */
44
49
  'sys:room_leave': (args: {
45
50
  socket: Socket;
46
51
  helper: SocketConnectionHelper<Events>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emeryld/rrroutes-server",
3
- "version": "2.5.4",
3
+ "version": "2.6.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -17,7 +17,7 @@
17
17
  "dist"
18
18
  ],
19
19
  "dependencies": {
20
- "@emeryld/rrroutes-contract": "^2.5.13",
20
+ "@emeryld/rrroutes-contract": "^2.6.0",
21
21
  "jose": "^6.1.3",
22
22
  "multer": "1.4.5-lts.2",
23
23
  "socket.io": "^4.8.3",