@wxn0brp/gloves-link-server 0.0.7 → 0.0.9

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/dist/index.d.ts CHANGED
@@ -4,6 +4,9 @@ import { GLSocket } from "./socket.js";
4
4
  import FalconFrame, { Router } from "@wxn0brp/falcon-frame";
5
5
  import { Room, Rooms } from "./room.js";
6
6
  import { Namespace } from "./namespace.js";
7
+ /**
8
+ * GlovesLinkServer class provides a WebSocket server with namespace and room functionality
9
+ */
7
10
  export declare class GlovesLinkServer {
8
11
  wss: WebSocketServer;
9
12
  logs: boolean;
@@ -14,13 +17,55 @@ export declare class GlovesLinkServer {
14
17
  }>;
15
18
  rooms: Rooms;
16
19
  namespaces: Map<string, Namespace>;
20
+ /**
21
+ * Creates a new GlovesLinkServer instance
22
+ * @param opts - Server options including the HTTP server instance
23
+ */
17
24
  constructor(opts: Partial<Server_Opts>);
25
+ /**
26
+ * Saves the status of a socket connection for temporary tracking
27
+ * @param socketSelfId - The ID of the socket
28
+ * @param namespace - The namespace of the socket
29
+ * @param status - The status code to save
30
+ * @param msg - Optional message to save with the status
31
+ * @private
32
+ */
18
33
  private saveSocketStatus;
34
+ /**
35
+ * Gets or creates a namespace by path
36
+ * @param path - The path for the namespace
37
+ * @returns The namespace instance
38
+ */
19
39
  of(path: string): Namespace;
40
+ /**
41
+ * Broadcasts an event to all sockets in a room
42
+ * @param roomName - The name of the room to broadcast to
43
+ * @param event - The event name to broadcast
44
+ * @param args - Arguments to send with the event
45
+ */
20
46
  broadcastRoom(roomName: string, event: string, ...args: any[]): void;
47
+ /**
48
+ * Gets or creates a room by name
49
+ * @param name - The name of the room
50
+ * @returns The room instance
51
+ */
21
52
  room(name: string): Room;
53
+ /**
54
+ * Creates a router for handling status requests
55
+ * @returns A router instance for status endpoints
56
+ */
22
57
  statusRouter(): Router;
58
+ /**
59
+ * Creates a router for serving client files
60
+ * @param clientDir - Optional directory path for client files, defaults to node_modules/@wxn0brp/gloves-link-client/dist/
61
+ * @returns A router instance for client file serving
62
+ */
23
63
  clientRouter(clientDir?: string): Router;
64
+ /**
65
+ * Integrates the GlovesLink server with a FalconFrame application
66
+ * @param app - The FalconFrame application instance
67
+ * @param clientDir - Optional directory path for client files, or false to disable client serving
68
+ */
24
69
  falconFrame(app: FalconFrame, clientDir?: string | false): void;
25
70
  }
26
71
  export { GLSocket, Namespace, Server_Opts };
package/dist/index.js CHANGED
@@ -3,6 +3,9 @@ import { GLSocket } from "./socket.js";
3
3
  import { Router } from "@wxn0brp/falcon-frame";
4
4
  import { Room } from "./room.js";
5
5
  import { Namespace } from "./namespace.js";
6
+ /**
7
+ * GlovesLinkServer class provides a WebSocket server with namespace and room functionality
8
+ */
6
9
  export class GlovesLinkServer {
7
10
  wss;
8
11
  logs = false;
@@ -10,6 +13,10 @@ export class GlovesLinkServer {
10
13
  initStatusTemp = {};
11
14
  rooms = new Map();
12
15
  namespaces = new Map();
16
+ /**
17
+ * Creates a new GlovesLinkServer instance
18
+ * @param opts - Server options including the HTTP server instance
19
+ */
13
20
  constructor(opts) {
14
21
  this.opts = {
15
22
  server: null,
@@ -36,7 +43,12 @@ export class GlovesLinkServer {
36
43
  socket.destroy();
37
44
  return;
38
45
  }
39
- const authResult = await namespace.authFn({ headers, url, token, request, socket, head });
46
+ const data = url.searchParams.has("data") ? JSON.parse(url.searchParams.get("data")) : {};
47
+ const authResult = await namespace.authFn({
48
+ token, data,
49
+ url, headers,
50
+ request, socket, head,
51
+ });
40
52
  if (!authResult || authResult.status !== 200) {
41
53
  this.saveSocketStatus(socketSelfId, pathname, authResult?.status || 401, authResult?.msg || "Unauthorized");
42
54
  socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
@@ -47,7 +59,7 @@ export class GlovesLinkServer {
47
59
  const glSocket = new GLSocket(ws, this);
48
60
  glSocket.logs = this.logs;
49
61
  if (typeof authResult.user === "object" && authResult.user !== null)
50
- glSocket.user = authResult;
62
+ glSocket.user = authResult.user;
51
63
  glSocket.namespace = pathname;
52
64
  namespace.room.join(glSocket);
53
65
  namespace.onConnectHandler(glSocket);
@@ -68,6 +80,14 @@ export class GlovesLinkServer {
68
80
  }
69
81
  });
70
82
  }
83
+ /**
84
+ * Saves the status of a socket connection for temporary tracking
85
+ * @param socketSelfId - The ID of the socket
86
+ * @param namespace - The namespace of the socket
87
+ * @param status - The status code to save
88
+ * @param msg - Optional message to save with the status
89
+ * @private
90
+ */
71
91
  saveSocketStatus(socketSelfId, namespace, status, msg) {
72
92
  if (!socketSelfId)
73
93
  return;
@@ -80,6 +100,11 @@ export class GlovesLinkServer {
80
100
  delete this.initStatusTemp[id];
81
101
  }, 10_000);
82
102
  }
103
+ /**
104
+ * Gets or creates a namespace by path
105
+ * @param path - The path for the namespace
106
+ * @returns The namespace instance
107
+ */
83
108
  of(path) {
84
109
  let namespace = this.namespaces.get(path);
85
110
  if (!namespace) {
@@ -88,15 +113,30 @@ export class GlovesLinkServer {
88
113
  }
89
114
  return namespace;
90
115
  }
116
+ /**
117
+ * Broadcasts an event to all sockets in a room
118
+ * @param roomName - The name of the room to broadcast to
119
+ * @param event - The event name to broadcast
120
+ * @param args - Arguments to send with the event
121
+ */
91
122
  broadcastRoom(roomName, event, ...args) {
92
123
  const room = this.room(roomName);
93
124
  if (!room)
94
125
  return;
95
126
  room.emit(event, ...args);
96
127
  }
128
+ /**
129
+ * Gets or creates a room by name
130
+ * @param name - The name of the room
131
+ * @returns The room instance
132
+ */
97
133
  room(name) {
98
134
  return this.rooms.get(name) || this.rooms.set(name, new Room()).get(name);
99
135
  }
136
+ /**
137
+ * Creates a router for handling status requests
138
+ * @returns A router instance for status endpoints
139
+ */
100
140
  statusRouter() {
101
141
  const router = new Router();
102
142
  router.get("/status", (req, res) => {
@@ -120,6 +160,11 @@ export class GlovesLinkServer {
120
160
  });
121
161
  return router;
122
162
  }
163
+ /**
164
+ * Creates a router for serving client files
165
+ * @param clientDir - Optional directory path for client files, defaults to node_modules/@wxn0brp/gloves-link-client/dist/
166
+ * @returns A router instance for client file serving
167
+ */
123
168
  clientRouter(clientDir) {
124
169
  const router = new Router();
125
170
  clientDir = clientDir || "node_modules/@wxn0brp/gloves-link-client/dist/";
@@ -130,6 +175,11 @@ export class GlovesLinkServer {
130
175
  });
131
176
  return router;
132
177
  }
178
+ /**
179
+ * Integrates the GlovesLink server with a FalconFrame application
180
+ * @param app - The FalconFrame application instance
181
+ * @param clientDir - Optional directory path for client files, or false to disable client serving
182
+ */
133
183
  falconFrame(app, clientDir) {
134
184
  const router = new Router();
135
185
  app.use("/gloves-link", router);
@@ -2,16 +2,49 @@ import { AuthFn } from "./types.js";
2
2
  import { GLSocket } from "./socket.js";
3
3
  import { Room } from "./room.js";
4
4
  import { GlovesLinkServer } from "./index.js";
5
+ /**
6
+ * Namespace class represents a logical grouping of sockets that can communicate with each other
7
+ */
5
8
  export declare class Namespace {
6
9
  name: string;
7
10
  private server;
8
11
  private onConnectEvent;
9
12
  authFn: AuthFn;
10
13
  room: Room;
14
+ /**
15
+ * Creates a new Namespace instance
16
+ * @param name - The name of the namespace
17
+ * @param server - The GlovesLinkServer instance
18
+ */
11
19
  constructor(name: string, server: GlovesLinkServer);
20
+ /**
21
+ * Sets the connection event handler for this namespace
22
+ * @param handler - The function to be called when a socket connects to this namespace
23
+ * @returns The current Namespace instance for chaining
24
+ */
12
25
  onConnect(handler: (ws: GLSocket) => void): this;
26
+ /**
27
+ * Sets the authentication function for this namespace
28
+ * @param authFn - The authentication function to be used for this namespace
29
+ * @returns The current Namespace instance for chaining
30
+ */
13
31
  auth(authFn: AuthFn): this;
32
+ /**
33
+ * Gets the connection event handler for this namespace
34
+ * @returns The connection event handler function
35
+ */
14
36
  get onConnectHandler(): (ws: GLSocket) => void;
37
+ /**
38
+ * Emits an event to all sockets in the namespace's room
39
+ * @param event - The event name to emit
40
+ * @param args - The arguments to pass with the event
41
+ */
15
42
  emit(event: string, ...args: any[]): void;
43
+ /**
44
+ * Emits an event to all sockets in the namespace's room except the specified socket
45
+ * @param socket - The socket to exclude from the emission
46
+ * @param event - The event name to emit
47
+ * @param args - The arguments to pass with the event
48
+ */
16
49
  emitWithoutSelf(socket: GLSocket, event: string, ...args: any[]): void;
17
50
  }
package/dist/namespace.js CHANGED
@@ -1,29 +1,62 @@
1
+ /**
2
+ * Namespace class represents a logical grouping of sockets that can communicate with each other
3
+ */
1
4
  export class Namespace {
2
5
  name;
3
6
  server;
4
7
  onConnectEvent = () => { };
5
8
  authFn = async () => ({ status: 200 });
6
9
  room;
10
+ /**
11
+ * Creates a new Namespace instance
12
+ * @param name - The name of the namespace
13
+ * @param server - The GlovesLinkServer instance
14
+ */
7
15
  constructor(name, server) {
8
16
  this.name = name;
9
17
  this.server = server;
10
18
  const roomName = `gls-namespace-${name}`;
11
19
  this.room = this.server.room(roomName);
12
20
  }
21
+ /**
22
+ * Sets the connection event handler for this namespace
23
+ * @param handler - The function to be called when a socket connects to this namespace
24
+ * @returns The current Namespace instance for chaining
25
+ */
13
26
  onConnect(handler) {
14
27
  this.onConnectEvent = handler;
15
28
  return this;
16
29
  }
30
+ /**
31
+ * Sets the authentication function for this namespace
32
+ * @param authFn - The authentication function to be used for this namespace
33
+ * @returns The current Namespace instance for chaining
34
+ */
17
35
  auth(authFn) {
18
36
  this.authFn = authFn;
19
37
  return this;
20
38
  }
39
+ /**
40
+ * Gets the connection event handler for this namespace
41
+ * @returns The connection event handler function
42
+ */
21
43
  get onConnectHandler() {
22
44
  return this.onConnectEvent;
23
45
  }
46
+ /**
47
+ * Emits an event to all sockets in the namespace's room
48
+ * @param event - The event name to emit
49
+ * @param args - The arguments to pass with the event
50
+ */
24
51
  emit(event, ...args) {
25
52
  this.room.emit(event, ...args);
26
53
  }
54
+ /**
55
+ * Emits an event to all sockets in the namespace's room except the specified socket
56
+ * @param socket - The socket to exclude from the emission
57
+ * @param event - The event name to emit
58
+ * @param args - The arguments to pass with the event
59
+ */
27
60
  emitWithoutSelf(socket, event, ...args) {
28
61
  this.room.emitWithoutSelf(socket, event, ...args);
29
62
  }
package/dist/room.d.ts CHANGED
@@ -1,22 +1,84 @@
1
1
  import EventEmitter from "events";
2
2
  import { GLSocket } from "./socket.js";
3
3
  export type Rooms = Map<string, Room>;
4
+ /**
5
+ * Room class represents a collection of sockets that can communicate with each other
6
+ */
4
7
  export declare class Room {
5
8
  clients: Set<GLSocket>;
6
9
  eventEmitter: EventEmitter<any>;
10
+ /**
11
+ * Adds a socket to the room
12
+ * @param socket - The socket to add to the room
13
+ */
7
14
  join(socket: GLSocket): void;
15
+ /**
16
+ * Removes a socket from the room
17
+ * @param socket - The socket to remove from the room
18
+ */
8
19
  leave(socket: GLSocket): void;
20
+ /**
21
+ * Removes all sockets from the room
22
+ */
9
23
  leaveAll(): void;
24
+ /**
25
+ * Registers a handler for when a socket joins the room
26
+ * @param handler - The function to be called when a socket joins the room
27
+ * @returns The current Room instance for chaining
28
+ */
10
29
  onJoin(handler: (socket: GLSocket, room: Room) => void): this;
30
+ /**
31
+ * Registers a handler for when a socket leaves the room
32
+ * @param handler - The function to be called when a socket leaves the room
33
+ * @returns The current Room instance for chaining
34
+ */
11
35
  onLeave(handler: (socket: GLSocket, room: Room) => void): this;
36
+ /**
37
+ * Gets the number of clients in the room
38
+ * @returns The number of clients in the room
39
+ */
12
40
  get size(): number;
41
+ /**
42
+ * Gets an array of all clients in the room
43
+ * @returns An array containing all the sockets in the room
44
+ */
13
45
  get sockets(): GLSocket<{
14
46
  _id?: string;
15
47
  }>[];
48
+ /**
49
+ * Emits an event to all clients in the room
50
+ * @param evtName - The name of the event to emit
51
+ * @param data - The data to send with the event
52
+ */
16
53
  emit(evtName: string, ...data: any): void;
54
+ /**
55
+ * Emits an event to all clients in the room except the specified socket
56
+ * @param socket - The socket to exclude from the emission
57
+ * @param evtName - The name of the event to emit
58
+ * @param data - The data to send with the event
59
+ */
17
60
  emitWithoutSelf(socket: GLSocket, evtName: string, ...data: any): void;
61
+ /**
62
+ * Checks if a socket is in the room
63
+ * @param socket - The socket to check
64
+ * @returns True if the socket is in the room, false otherwise
65
+ */
18
66
  has(socket: GLSocket): boolean;
19
67
  }
68
+ /**
69
+ * Adds a socket to a room by name, creating the room if it doesn't exist
70
+ * @param socket - The socket to add to the room
71
+ * @param name - The name of the room to join
72
+ */
20
73
  export declare function joinSocketToRoom(socket: GLSocket, name: string): void;
74
+ /**
75
+ * Removes a socket from a room by name
76
+ * @param socket - The socket to remove from the room
77
+ * @param roomName - The name of the room to leave
78
+ */
21
79
  export declare function leaveSocketFromRoom(socket: GLSocket, roomName: string): void;
80
+ /**
81
+ * Removes a socket from all rooms it has joined
82
+ * @param socket - The socket to remove from all rooms
83
+ */
22
84
  export declare function leaveAllSocketFromRoom(socket: GLSocket): void;
package/dist/room.js CHANGED
@@ -1,38 +1,81 @@
1
1
  import EventEmitter from "events";
2
+ /**
3
+ * Room class represents a collection of sockets that can communicate with each other
4
+ */
2
5
  export class Room {
3
6
  clients = new Set();
4
7
  eventEmitter = new EventEmitter();
8
+ /**
9
+ * Adds a socket to the room
10
+ * @param socket - The socket to add to the room
11
+ */
5
12
  join(socket) {
6
13
  this.clients.add(socket);
7
14
  this.eventEmitter.emit("join", socket, this);
8
15
  }
16
+ /**
17
+ * Removes a socket from the room
18
+ * @param socket - The socket to remove from the room
19
+ */
9
20
  leave(socket) {
10
21
  this.clients.delete(socket);
11
22
  this.eventEmitter.emit("leave", socket, this);
12
23
  }
24
+ /**
25
+ * Removes all sockets from the room
26
+ */
13
27
  leaveAll() {
14
28
  this.clients.clear();
15
29
  this.eventEmitter.emit("leaveAll", this);
16
30
  }
31
+ /**
32
+ * Registers a handler for when a socket joins the room
33
+ * @param handler - The function to be called when a socket joins the room
34
+ * @returns The current Room instance for chaining
35
+ */
17
36
  onJoin(handler) {
18
37
  this.eventEmitter.on("join", handler);
19
38
  return this;
20
39
  }
40
+ /**
41
+ * Registers a handler for when a socket leaves the room
42
+ * @param handler - The function to be called when a socket leaves the room
43
+ * @returns The current Room instance for chaining
44
+ */
21
45
  onLeave(handler) {
22
46
  this.eventEmitter.on("leave", handler);
23
47
  return this;
24
48
  }
49
+ /**
50
+ * Gets the number of clients in the room
51
+ * @returns The number of clients in the room
52
+ */
25
53
  get size() {
26
54
  return this.clients.size;
27
55
  }
56
+ /**
57
+ * Gets an array of all clients in the room
58
+ * @returns An array containing all the sockets in the room
59
+ */
28
60
  get sockets() {
29
61
  return Array.from(this.clients);
30
62
  }
63
+ /**
64
+ * Emits an event to all clients in the room
65
+ * @param evtName - The name of the event to emit
66
+ * @param data - The data to send with the event
67
+ */
31
68
  emit(evtName, ...data) {
32
69
  for (const socket of this.clients) {
33
70
  socket.emit(evtName, ...data);
34
71
  }
35
72
  }
73
+ /**
74
+ * Emits an event to all clients in the room except the specified socket
75
+ * @param socket - The socket to exclude from the emission
76
+ * @param evtName - The name of the event to emit
77
+ * @param data - The data to send with the event
78
+ */
36
79
  emitWithoutSelf(socket, evtName, ...data) {
37
80
  for (const client of this.clients) {
38
81
  if (client === socket)
@@ -40,15 +83,30 @@ export class Room {
40
83
  client.emit(evtName, ...data);
41
84
  }
42
85
  }
86
+ /**
87
+ * Checks if a socket is in the room
88
+ * @param socket - The socket to check
89
+ * @returns True if the socket is in the room, false otherwise
90
+ */
43
91
  has(socket) {
44
92
  return this.clients.has(socket);
45
93
  }
46
94
  }
95
+ /**
96
+ * Adds a socket to a room by name, creating the room if it doesn't exist
97
+ * @param socket - The socket to add to the room
98
+ * @param name - The name of the room to join
99
+ */
47
100
  export function joinSocketToRoom(socket, name) {
48
101
  const rooms = socket.server.rooms;
49
102
  const room = rooms.get(name) || rooms.set(name, new Room()).get(name);
50
103
  room.join(socket);
51
104
  }
105
+ /**
106
+ * Removes a socket from a room by name
107
+ * @param socket - The socket to remove from the room
108
+ * @param roomName - The name of the room to leave
109
+ */
52
110
  export function leaveSocketFromRoom(socket, roomName) {
53
111
  const rooms = socket.server.rooms;
54
112
  const room = rooms.get(roomName);
@@ -59,6 +117,10 @@ export function leaveSocketFromRoom(socket, roomName) {
59
117
  return;
60
118
  rooms.delete(roomName);
61
119
  }
120
+ /**
121
+ * Removes a socket from all rooms it has joined
122
+ * @param socket - The socket to remove from all rooms
123
+ */
62
124
  export function leaveAllSocketFromRoom(socket) {
63
125
  const rooms = socket.server.rooms;
64
126
  for (const room of rooms.values())
package/dist/socket.d.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  import { WebSocket } from "ws";
2
2
  import { GlovesLinkServer } from "./index.js";
3
+ import { Room, Rooms } from "./room.js";
4
+ /**
5
+ * GLSocket class represents a WebSocket connection with additional functionality
6
+ * @template T - The type of user data associated with the socket
7
+ */
3
8
  export declare class GLSocket<T = {
4
9
  _id?: string;
5
10
  }> {
@@ -15,14 +20,74 @@ export declare class GLSocket<T = {
15
20
  [key: string]: Function;
16
21
  };
17
22
  rooms: Set<string>;
23
+ /**
24
+ * Creates a new GLSocket instance
25
+ * @param ws - The underlying WebSocket connection
26
+ * @param server - The GlovesLinkServer instance
27
+ * @param id - Optional ID for the socket, will be generated if not provided
28
+ */
18
29
  constructor(ws: WebSocket, server: GlovesLinkServer, id?: string);
30
+ /**
31
+ * Internal method to handle incoming messages from the WebSocket
32
+ * @param raw - The raw message string received from the WebSocket
33
+ */
19
34
  _handle(raw: string): void;
35
+ /**
36
+ * Registers an event handler for the specified event
37
+ * @param evt - The event name to listen for
38
+ * @param handler - The function to be called when the event is received
39
+ */
20
40
  on(evt: string, handler: (...args: any[]) => void | any): void;
41
+ /**
42
+ * Sends an event to the connected WebSocket client
43
+ * @param evt - The event name to send
44
+ * @param args - The arguments to pass with the event
45
+ */
21
46
  emit(evt: string, ...args: any[]): void;
47
+ /**
48
+ * Sends an event to the connected WebSocket client (alias for emit)
49
+ * @param evt - The event name to send
50
+ * @param args - The arguments to pass with the event
51
+ * @returns The result of the emit method
52
+ */
22
53
  send(evt: string, ...args: any[]): void;
54
+ /**
55
+ * Closes the WebSocket connection
56
+ */
23
57
  close(): void;
58
+ /**
59
+ * Joins the socket to a room
60
+ * @param roomName - The name of the room to join
61
+ */
24
62
  joinRoom(roomName: string): void;
63
+ /**
64
+ * Removes the socket from a room
65
+ * @param roomName - The name of the room to leave
66
+ */
25
67
  leaveRoom(roomName: string): void;
68
+ /**
69
+ * Removes the socket from all rooms it has joined
70
+ */
26
71
  leaveAllRooms(): void;
72
+ /**
73
+ * Gets the namespace associated with this socket
74
+ * @returns The namespace object or undefined if not found
75
+ */
27
76
  getNamespace(): import("./namespace.js").Namespace;
77
+ /**
78
+ * Gets the room associated with this socket's namespace
79
+ * @returns The room object or undefined if namespace is not found
80
+ */
81
+ namespaceRoom(): Room;
82
+ /**
83
+ * Gets a room from the server by name
84
+ * @param roomName - The name of the room to retrieve
85
+ * @returns The room object
86
+ */
87
+ serverRoom(roomName: string): Room;
88
+ /**
89
+ * Gets all rooms from the server
90
+ * @returns A map of all rooms on the server
91
+ */
92
+ serverRooms(): Rooms;
28
93
  }
package/dist/socket.js CHANGED
@@ -1,4 +1,8 @@
1
1
  import { joinSocketToRoom, leaveSocketFromRoom } from "./room.js";
2
+ /**
3
+ * GLSocket class represents a WebSocket connection with additional functionality
4
+ * @template T - The type of user data associated with the socket
5
+ */
2
6
  export class GLSocket {
3
7
  ws;
4
8
  server;
@@ -10,6 +14,12 @@ export class GLSocket {
10
14
  logs = false;
11
15
  handlers;
12
16
  rooms = new Set();
17
+ /**
18
+ * Creates a new GLSocket instance
19
+ * @param ws - The underlying WebSocket connection
20
+ * @param server - The GlovesLinkServer instance
21
+ * @param id - Optional ID for the socket, will be generated if not provided
22
+ */
13
23
  constructor(ws, server, id) {
14
24
  this.ws = ws;
15
25
  this.server = server;
@@ -18,6 +28,10 @@ export class GLSocket {
18
28
  this.handlers = {};
19
29
  this.ws.on("message", (raw) => this._handle(raw));
20
30
  }
31
+ /**
32
+ * Internal method to handle incoming messages from the WebSocket
33
+ * @param raw - The raw message string received from the WebSocket
34
+ */
21
35
  _handle(raw) {
22
36
  let msg;
23
37
  try {
@@ -53,9 +67,19 @@ export class GLSocket {
53
67
  }
54
68
  this.handlers[evt]?.(...data);
55
69
  }
70
+ /**
71
+ * Registers an event handler for the specified event
72
+ * @param evt - The event name to listen for
73
+ * @param handler - The function to be called when the event is received
74
+ */
56
75
  on(evt, handler) {
57
76
  this.handlers[evt] = handler;
58
77
  }
78
+ /**
79
+ * Sends an event to the connected WebSocket client
80
+ * @param evt - The event name to send
81
+ * @param args - The arguments to pass with the event
82
+ */
59
83
  emit(evt, ...args) {
60
84
  const ackI = args.map((data, i) => {
61
85
  if (typeof data === "function")
@@ -73,27 +97,73 @@ export class GLSocket {
73
97
  ackI: ackI.length ? ackI : undefined
74
98
  }));
75
99
  }
100
+ /**
101
+ * Sends an event to the connected WebSocket client (alias for emit)
102
+ * @param evt - The event name to send
103
+ * @param args - The arguments to pass with the event
104
+ * @returns The result of the emit method
105
+ */
76
106
  send(evt, ...args) {
77
107
  return this.emit(evt, ...args);
78
108
  }
109
+ /**
110
+ * Closes the WebSocket connection
111
+ */
79
112
  close() {
80
113
  this.ws.close();
81
114
  }
115
+ /**
116
+ * Joins the socket to a room
117
+ * @param roomName - The name of the room to join
118
+ */
82
119
  joinRoom(roomName) {
83
120
  joinSocketToRoom(this, roomName);
84
121
  this.rooms.add(roomName);
85
122
  }
123
+ /**
124
+ * Removes the socket from a room
125
+ * @param roomName - The name of the room to leave
126
+ */
86
127
  leaveRoom(roomName) {
87
128
  leaveSocketFromRoom(this, roomName);
88
129
  this.rooms.delete(roomName);
89
130
  }
131
+ /**
132
+ * Removes the socket from all rooms it has joined
133
+ */
90
134
  leaveAllRooms() {
91
135
  for (const roomName of this.rooms) {
92
136
  leaveSocketFromRoom(this, roomName);
93
137
  }
94
138
  this.rooms.clear();
95
139
  }
140
+ /**
141
+ * Gets the namespace associated with this socket
142
+ * @returns The namespace object or undefined if not found
143
+ */
96
144
  getNamespace() {
97
145
  return this.server.namespaces.get(this.namespace);
98
146
  }
147
+ /**
148
+ * Gets the room associated with this socket's namespace
149
+ * @returns The room object or undefined if namespace is not found
150
+ */
151
+ namespaceRoom() {
152
+ return this.getNamespace()?.room;
153
+ }
154
+ /**
155
+ * Gets a room from the server by name
156
+ * @param roomName - The name of the room to retrieve
157
+ * @returns The room object
158
+ */
159
+ serverRoom(roomName) {
160
+ return this.server.room(roomName);
161
+ }
162
+ /**
163
+ * Gets all rooms from the server
164
+ * @returns A map of all rooms on the server
165
+ */
166
+ serverRooms() {
167
+ return this.server.rooms;
168
+ }
99
169
  }
package/dist/types.d.ts CHANGED
@@ -20,6 +20,7 @@ export interface Server_Auth_Opts {
20
20
  request: http.IncomingMessage;
21
21
  socket: Stream.Duplex;
22
22
  head: Buffer<ArrayBufferLike>;
23
+ data?: Record<string, any>;
23
24
  }
24
25
  export interface AuthFnResult {
25
26
  status: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/gloves-link-server",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",