@wxn0brp/gloves-link-server 0.0.9 → 0.0.11

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
@@ -1,9 +1,10 @@
1
- import { WebSocketServer } from "ws";
2
- import { Server_Opts } from "./types.js";
3
- import { GLSocket } from "./socket.js";
4
1
  import FalconFrame, { Router } from "@wxn0brp/falcon-frame";
5
- import { Room, Rooms } from "./room.js";
2
+ import http from "http";
3
+ import { WebSocketServer } from "ws";
6
4
  import { Namespace } from "./namespace.js";
5
+ import { Room, Rooms } from "./room.js";
6
+ import { GLSocket } from "./socket.js";
7
+ import { Server_Opts } from "./types.js";
7
8
  /**
8
9
  * GlovesLinkServer class provides a WebSocket server with namespace and room functionality
9
10
  */
@@ -22,6 +23,7 @@ export declare class GlovesLinkServer {
22
23
  * @param opts - Server options including the HTTP server instance
23
24
  */
24
25
  constructor(opts: Partial<Server_Opts>);
26
+ createServer(server: http.Server): void;
25
27
  /**
26
28
  * Saves the status of a socket connection for temporary tracking
27
29
  * @param socketSelfId - The ID of the socket
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
- import { WebSocketServer } from "ws";
2
- import { GLSocket } from "./socket.js";
3
1
  import { Router } from "@wxn0brp/falcon-frame";
4
- import { Room } from "./room.js";
2
+ import { WebSocketServer } from "ws";
5
3
  import { Namespace } from "./namespace.js";
4
+ import { Room } from "./room.js";
5
+ import { GLSocket } from "./socket.js";
6
6
  /**
7
7
  * GlovesLinkServer class provides a WebSocket server with namespace and room functionality
8
8
  */
@@ -19,15 +19,12 @@ export class GlovesLinkServer {
19
19
  */
20
20
  constructor(opts) {
21
21
  this.opts = {
22
- server: null,
23
22
  logs: false,
24
23
  ...opts
25
24
  };
26
- if (!this.opts?.server) {
27
- throw new Error("Server is not provided");
28
- }
29
- const { server } = opts;
30
25
  this.wss = new WebSocketServer({ noServer: true });
26
+ }
27
+ createServer(server) {
31
28
  server.on("upgrade", async (request, socket, head) => {
32
29
  const headers = request.headers;
33
30
  let socketSelfId;
@@ -44,11 +41,12 @@ export class GlovesLinkServer {
44
41
  return;
45
42
  }
46
43
  const data = url.searchParams.has("data") ? JSON.parse(url.searchParams.get("data")) : {};
47
- const authResult = await namespace.authFn({
44
+ const authData = {
48
45
  token, data,
49
46
  url, headers,
50
47
  request, socket, head,
51
- });
48
+ };
49
+ const authResult = await namespace.authFn(authData);
52
50
  if (!authResult || authResult.status !== 200) {
53
51
  this.saveSocketStatus(socketSelfId, pathname, authResult?.status || 401, authResult?.msg || "Unauthorized");
54
52
  socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
@@ -62,7 +60,7 @@ export class GlovesLinkServer {
62
60
  glSocket.user = authResult.user;
63
61
  glSocket.namespace = pathname;
64
62
  namespace.room.join(glSocket);
65
- namespace.onConnectHandler(glSocket);
63
+ namespace.onConnectHandler(glSocket, authData, authResult);
66
64
  ws.on("close", () => {
67
65
  glSocket.handlers?.disconnect?.();
68
66
  namespace.room.leave(glSocket);
@@ -1,4 +1,4 @@
1
- import { AuthFn } from "./types.js";
1
+ import { AuthFn, OnConnect } from "./types.js";
2
2
  import { GLSocket } from "./socket.js";
3
3
  import { Room } from "./room.js";
4
4
  import { GlovesLinkServer } from "./index.js";
@@ -22,7 +22,7 @@ export declare class Namespace {
22
22
  * @param handler - The function to be called when a socket connects to this namespace
23
23
  * @returns The current Namespace instance for chaining
24
24
  */
25
- onConnect(handler: (ws: GLSocket) => void): this;
25
+ onConnect(handler: OnConnect): this;
26
26
  /**
27
27
  * Sets the authentication function for this namespace
28
28
  * @param authFn - The authentication function to be used for this namespace
@@ -33,7 +33,7 @@ export declare class Namespace {
33
33
  * Gets the connection event handler for this namespace
34
34
  * @returns The connection event handler function
35
35
  */
36
- get onConnectHandler(): (ws: GLSocket) => void;
36
+ get onConnectHandler(): OnConnect;
37
37
  /**
38
38
  * Emits an event to all sockets in the namespace's room
39
39
  * @param event - The event name to emit
package/dist/types.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import http from "http";
2
2
  import Stream from "stream";
3
+ import { GLSocket } from "./socket.js";
3
4
  export interface Server_Opts {
4
- server: http.Server;
5
5
  logs: boolean;
6
6
  }
7
7
  export interface Server_DataEvent {
@@ -29,3 +29,4 @@ export interface AuthFnResult {
29
29
  toSet?: Record<string, any>;
30
30
  }
31
31
  export type AuthFn = (data: Server_Auth_Opts) => Promise<AuthFnResult>;
32
+ export type OnConnect = (socket: GLSocket, auth: Server_Auth_Opts, result: AuthFnResult) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/gloves-link-server",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",