@wxn0brp/gloves-link-server 0.0.6 → 0.0.7

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,23 +1,26 @@
1
1
  import { WebSocketServer } from "ws";
2
2
  import { Server_Opts } from "./types.js";
3
3
  import { GLSocket } from "./socket.js";
4
- import FalconFrame from "@wxn0brp/falcon-frame";
4
+ import FalconFrame, { Router } from "@wxn0brp/falcon-frame";
5
5
  import { Room, Rooms } from "./room.js";
6
6
  import { Namespace } from "./namespace.js";
7
7
  export declare class GlovesLinkServer {
8
8
  wss: WebSocketServer;
9
9
  logs: boolean;
10
10
  opts: Server_Opts;
11
- initStatusTemp: {
12
- [key: string]: number;
13
- };
11
+ initStatusTemp: Record<string, {
12
+ status: number;
13
+ msg?: string;
14
+ }>;
14
15
  rooms: Rooms;
15
- private namespaces;
16
+ namespaces: Map<string, Namespace>;
16
17
  constructor(opts: Partial<Server_Opts>);
17
18
  private saveSocketStatus;
18
19
  of(path: string): Namespace;
19
20
  broadcastRoom(roomName: string, event: string, ...args: any[]): void;
20
21
  room(name: string): Room;
22
+ statusRouter(): Router;
23
+ clientRouter(clientDir?: string): Router;
21
24
  falconFrame(app: FalconFrame, clientDir?: string | false): void;
22
25
  }
23
26
  export { GLSocket, Namespace, Server_Opts };
package/dist/index.js CHANGED
@@ -37,8 +37,8 @@ export class GlovesLinkServer {
37
37
  return;
38
38
  }
39
39
  const authResult = await namespace.authFn({ headers, url, token, request, socket, head });
40
- if (!authResult) {
41
- this.saveSocketStatus(socketSelfId, pathname, 401);
40
+ if (!authResult || authResult.status !== 200) {
41
+ this.saveSocketStatus(socketSelfId, pathname, authResult?.status || 401, authResult?.msg || "Unauthorized");
42
42
  socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
43
43
  socket.destroy();
44
44
  return;
@@ -46,7 +46,7 @@ export class GlovesLinkServer {
46
46
  this.wss.handleUpgrade(request, socket, head, (ws) => {
47
47
  const glSocket = new GLSocket(ws, this);
48
48
  glSocket.logs = this.logs;
49
- if (typeof authResult === "object" && authResult !== null)
49
+ if (typeof authResult.user === "object" && authResult.user !== null)
50
50
  glSocket.user = authResult;
51
51
  glSocket.namespace = pathname;
52
52
  namespace.room.join(glSocket);
@@ -68,11 +68,14 @@ export class GlovesLinkServer {
68
68
  }
69
69
  });
70
70
  }
71
- saveSocketStatus(socketSelfId, namespace, status) {
71
+ saveSocketStatus(socketSelfId, namespace, status, msg) {
72
72
  if (!socketSelfId)
73
73
  return;
74
74
  const id = namespace + "-" + socketSelfId;
75
- this.initStatusTemp[id] = status;
75
+ this.initStatusTemp[id] = {
76
+ status,
77
+ msg
78
+ };
76
79
  setTimeout(() => {
77
80
  delete this.initStatusTemp[id];
78
81
  }, 10_000);
@@ -94,9 +97,8 @@ export class GlovesLinkServer {
94
97
  room(name) {
95
98
  return this.rooms.get(name) || this.rooms.set(name, new Room()).get(name);
96
99
  }
97
- falconFrame(app, clientDir) {
100
+ statusRouter() {
98
101
  const router = new Router();
99
- app.use("/gloves-link", router);
100
102
  router.get("/status", (req, res) => {
101
103
  const id = req.query.id;
102
104
  if (!id) {
@@ -116,14 +118,24 @@ export class GlovesLinkServer {
116
118
  res.json({ status });
117
119
  delete this.initStatusTemp[id];
118
120
  });
119
- if (clientDir === false)
120
- return;
121
+ return router;
122
+ }
123
+ clientRouter(clientDir) {
124
+ const router = new Router();
121
125
  clientDir = clientDir || "node_modules/@wxn0brp/gloves-link-client/dist/";
122
126
  router.static("/", clientDir);
123
127
  router.get("/*", (req, res) => {
124
128
  res.redirect("/gloves-link/GlovesLinkClient.js");
125
129
  res.end();
126
130
  });
131
+ return router;
132
+ }
133
+ falconFrame(app, clientDir) {
134
+ const router = new Router();
135
+ app.use("/gloves-link", router);
136
+ router.use(this.statusRouter());
137
+ if (clientDir !== false)
138
+ router.use(this.clientRouter(clientDir));
127
139
  }
128
140
  }
129
141
  export { GLSocket, Namespace };
package/dist/namespace.js CHANGED
@@ -2,7 +2,7 @@ export class Namespace {
2
2
  name;
3
3
  server;
4
4
  onConnectEvent = () => { };
5
- authFn = () => false;
5
+ authFn = async () => ({ status: 200 });
6
6
  room;
7
7
  constructor(name, server) {
8
8
  this.name = name;
package/dist/room.d.ts CHANGED
@@ -10,7 +10,9 @@ export declare class Room {
10
10
  onJoin(handler: (socket: GLSocket, room: Room) => void): this;
11
11
  onLeave(handler: (socket: GLSocket, room: Room) => void): this;
12
12
  get size(): number;
13
- get sockets(): GLSocket[];
13
+ get sockets(): GLSocket<{
14
+ _id?: string;
15
+ }>[];
14
16
  emit(evtName: string, ...data: any): void;
15
17
  emitWithoutSelf(socket: GLSocket, evtName: string, ...data: any): void;
16
18
  has(socket: GLSocket): boolean;
package/dist/socket.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  import { WebSocket } from "ws";
2
2
  import { GlovesLinkServer } from "./index.js";
3
- export declare class GLSocket {
3
+ export declare class GLSocket<T = {
4
+ _id?: string;
5
+ }> {
4
6
  ws: WebSocket;
5
7
  server: GlovesLinkServer;
6
8
  id: string;
7
- user: any;
9
+ user: T;
8
10
  namespace: string;
9
11
  ackIdCounter: number;
10
12
  ackCallbacks: Map<number, Function>;
@@ -22,4 +24,5 @@ export declare class GLSocket {
22
24
  joinRoom(roomName: string): void;
23
25
  leaveRoom(roomName: string): void;
24
26
  leaveAllRooms(): void;
27
+ getNamespace(): import("./namespace.js").Namespace;
25
28
  }
package/dist/socket.js CHANGED
@@ -3,7 +3,7 @@ export class GLSocket {
3
3
  ws;
4
4
  server;
5
5
  id;
6
- user = {};
6
+ user;
7
7
  namespace;
8
8
  ackIdCounter = 1;
9
9
  ackCallbacks = new Map();
@@ -14,6 +14,7 @@ export class GLSocket {
14
14
  this.ws = ws;
15
15
  this.server = server;
16
16
  this.id = id || Date.now().toString(36) + Math.random().toString(36).substring(2, 10);
17
+ this.user = { _id: this.id };
17
18
  this.handlers = {};
18
19
  this.ws.on("message", (raw) => this._handle(raw));
19
20
  }
@@ -92,4 +93,7 @@ export class GLSocket {
92
93
  }
93
94
  this.rooms.clear();
94
95
  }
96
+ getNamespace() {
97
+ return this.server.namespaces.get(this.namespace);
98
+ }
95
99
  }
package/dist/types.d.ts CHANGED
@@ -21,4 +21,10 @@ export interface Server_Auth_Opts {
21
21
  socket: Stream.Duplex;
22
22
  head: Buffer<ArrayBufferLike>;
23
23
  }
24
- export type AuthFn = (data: Server_Auth_Opts) => (Promise<object | boolean> | object | boolean);
24
+ export interface AuthFnResult {
25
+ status: number;
26
+ user?: Record<string, any>;
27
+ msg?: string;
28
+ toSet?: Record<string, any>;
29
+ }
30
+ export type AuthFn = (data: Server_Auth_Opts) => Promise<AuthFnResult>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/gloves-link-server",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",