@wxn0brp/gloves-link-server 0.0.10 → 0.0.12
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.js +6 -3
- package/dist/namespace.d.ts +3 -3
- package/dist/socket.d.ts +3 -0
- package/dist/socket.js +2 -0
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -41,11 +41,12 @@ export class GlovesLinkServer {
|
|
|
41
41
|
return;
|
|
42
42
|
}
|
|
43
43
|
const data = url.searchParams.has("data") ? JSON.parse(url.searchParams.get("data")) : {};
|
|
44
|
-
const
|
|
44
|
+
const authData = {
|
|
45
45
|
token, data,
|
|
46
46
|
url, headers,
|
|
47
47
|
request, socket, head,
|
|
48
|
-
}
|
|
48
|
+
};
|
|
49
|
+
const authResult = await namespace.authFn(authData);
|
|
49
50
|
if (!authResult || authResult.status !== 200) {
|
|
50
51
|
this.saveSocketStatus(socketSelfId, pathname, authResult?.status || 401, authResult?.msg || "Unauthorized");
|
|
51
52
|
socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
|
|
@@ -55,11 +56,13 @@ export class GlovesLinkServer {
|
|
|
55
56
|
this.wss.handleUpgrade(request, socket, head, (ws) => {
|
|
56
57
|
const glSocket = new GLSocket(ws, this);
|
|
57
58
|
glSocket.logs = this.logs;
|
|
59
|
+
glSocket.authData = authData;
|
|
60
|
+
glSocket.authResult = authResult;
|
|
58
61
|
if (typeof authResult.user === "object" && authResult.user !== null)
|
|
59
62
|
glSocket.user = authResult.user;
|
|
60
63
|
glSocket.namespace = pathname;
|
|
61
64
|
namespace.room.join(glSocket);
|
|
62
|
-
namespace.onConnectHandler(glSocket);
|
|
65
|
+
namespace.onConnectHandler(glSocket, authData, authResult);
|
|
63
66
|
ws.on("close", () => {
|
|
64
67
|
glSocket.handlers?.disconnect?.();
|
|
65
68
|
namespace.room.leave(glSocket);
|
package/dist/namespace.d.ts
CHANGED
|
@@ -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:
|
|
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():
|
|
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/socket.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { WebSocket } from "ws";
|
|
2
2
|
import { GlovesLinkServer } from "./index.js";
|
|
3
3
|
import { Room, Rooms } from "./room.js";
|
|
4
|
+
import { AuthFnResult, Server_Auth_Opts } from "./types.js";
|
|
4
5
|
/**
|
|
5
6
|
* GLSocket class represents a WebSocket connection with additional functionality
|
|
6
7
|
* @template T - The type of user data associated with the socket
|
|
@@ -20,6 +21,8 @@ export declare class GLSocket<T = {
|
|
|
20
21
|
[key: string]: Function;
|
|
21
22
|
};
|
|
22
23
|
rooms: Set<string>;
|
|
24
|
+
authData: Server_Auth_Opts;
|
|
25
|
+
authResult: AuthFnResult;
|
|
23
26
|
/**
|
|
24
27
|
* Creates a new GLSocket instance
|
|
25
28
|
* @param ws - The underlying WebSocket connection
|
package/dist/socket.js
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
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
5
|
logs: boolean;
|
|
5
6
|
}
|
|
@@ -28,3 +29,4 @@ export interface AuthFnResult {
|
|
|
28
29
|
toSet?: Record<string, any>;
|
|
29
30
|
}
|
|
30
31
|
export type AuthFn = (data: Server_Auth_Opts) => Promise<AuthFnResult>;
|
|
32
|
+
export type OnConnect = (socket: GLSocket, auth: Server_Auth_Opts, result: AuthFnResult) => void;
|