@nsshunt/stssocketioutils 1.1.30 → 1.1.32

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.
@@ -1,15 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __typeError = (msg) => {
3
- throw TypeError(msg);
4
- };
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
8
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
9
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
10
- var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
11
- var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
12
- var _options, _sockets, _SocketIoClientHelper_instances, LogDebugMessage_fn, LogErrorMessage_fn, EstablishSocketConnect_fn, _socketIoServerHelperOptions, _namespace, _SetupStandardEvents, _SetupConnectionMiddleware, _SetupMessageMiddleware;
13
1
  import { io } from "socket.io-client";
14
2
  var STSNamespace = /* @__PURE__ */ ((STSNamespace2) => {
15
3
  STSNamespace2["STSMonitor"] = "stsinstrumentmanager/stsmonitor";
@@ -43,14 +31,21 @@ var STSEvent = /* @__PURE__ */ ((STSEvent2) => {
43
31
  })(STSEvent || {});
44
32
  const isNode = Object.prototype.toString.call(typeof process !== "undefined" ? process : 0) === "[object process]";
45
33
  class SocketIoClientHelper {
34
+ #options;
35
+ #sockets = {};
46
36
  constructor(options) {
47
- __privateAdd(this, _SocketIoClientHelper_instances);
48
- __privateAdd(this, _options);
49
- __privateAdd(this, _sockets, {});
50
- __privateSet(this, _options, options);
37
+ this.#options = options;
38
+ }
39
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
+ #LogDebugMessage(message) {
41
+ if (this.#options.logger) this.#options.logger.debug(message);
42
+ }
43
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
44
+ #LogErrorMessage(message) {
45
+ if (this.#options.logger) this.#options.logger.error(message);
51
46
  }
52
47
  get sockets() {
53
- return __privateGet(this, _sockets);
48
+ return this.#sockets;
54
49
  }
55
50
  SetupClientSideSocket(name, address, socketIoCustomPath, onConnectCallBack, socketEventsCallBack, errorCallBack) {
56
51
  this.sockets[name] = {
@@ -59,7 +54,7 @@ class SocketIoClientHelper {
59
54
  socket: null,
60
55
  errorCallBack
61
56
  };
62
- __privateMethod(this, _SocketIoClientHelper_instances, EstablishSocketConnect_fn).call(this, name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack);
57
+ this.#EstablishSocketConnect(name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack);
63
58
  return this.sockets[name].socket;
64
59
  }
65
60
  GetSocket(name) {
@@ -68,252 +63,235 @@ class SocketIoClientHelper {
68
63
  GetSocketDetail(name) {
69
64
  return this.sockets[name];
70
65
  }
71
- }
72
- _options = new WeakMap();
73
- _sockets = new WeakMap();
74
- _SocketIoClientHelper_instances = new WeakSet();
75
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
76
- LogDebugMessage_fn = function(message) {
77
- if (__privateGet(this, _options).logger) __privateGet(this, _options).logger.debug(message);
78
- };
79
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
80
- LogErrorMessage_fn = function(message) {
81
- if (__privateGet(this, _options).logger) __privateGet(this, _options).logger.error(message);
82
- };
83
- EstablishSocketConnect_fn = function(name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack) {
84
- const socketDetail = this.sockets[name];
85
- if (socketDetail.socket !== null) {
86
- if (socketDetail.socket.connected === true) {
87
- socketDetail.socket.disconnect();
66
+ #EstablishSocketConnect(name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack) {
67
+ const socketDetail = this.sockets[name];
68
+ if (socketDetail.socket !== null) {
69
+ if (socketDetail.socket.connected === true) {
70
+ socketDetail.socket.disconnect();
71
+ }
72
+ socketDetail.socket = null;
73
+ if (isNode) {
74
+ setTimeout(() => this.#EstablishSocketConnect(name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack), 100).unref();
75
+ } else {
76
+ setTimeout(() => this.#EstablishSocketConnect(name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack), 100);
77
+ }
78
+ return;
88
79
  }
89
- socketDetail.socket = null;
80
+ let socketOptions;
90
81
  if (isNode) {
91
- setTimeout(() => __privateMethod(this, _SocketIoClientHelper_instances, EstablishSocketConnect_fn).call(this, name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack), 100).unref();
82
+ socketOptions = {
83
+ transports: ["websocket"]
84
+ };
85
+ if (this.#options.agentManager) {
86
+ socketOptions.agent = this.#options.agentManager.GetAgent(socketDetail.address);
87
+ }
92
88
  } else {
93
- setTimeout(() => __privateMethod(this, _SocketIoClientHelper_instances, EstablishSocketConnect_fn).call(this, name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack), 100);
89
+ socketOptions = {
90
+ transports: ["websocket"]
91
+ };
94
92
  }
95
- return;
96
- }
97
- let socketOptions;
98
- if (isNode) {
99
- socketOptions = {
100
- transports: ["websocket"]
101
- };
102
- if (__privateGet(this, _options).agentManager) {
103
- socketOptions.agent = __privateGet(this, _options).agentManager.GetAgent(socketDetail.address);
93
+ if (socketIoCustomPath && socketIoCustomPath.localeCompare("") !== 0) {
94
+ socketOptions.path = socketIoCustomPath;
104
95
  }
105
- } else {
106
- socketOptions = {
107
- transports: ["websocket"]
108
- };
109
- }
110
- if (socketIoCustomPath && socketIoCustomPath.localeCompare("") !== 0) {
111
- socketOptions.path = socketIoCustomPath;
112
- }
113
- socketDetail.socket = io(socketDetail.address, socketOptions);
114
- socketDetail.socket.io.on("error", (err) => {
115
- __privateMethod(this, _SocketIoClientHelper_instances, LogErrorMessage_fn).call(this, `SocketIoClientHelper(): socketDetail.socket.io.on('error'): [${err}]`);
116
- });
117
- socketDetail.socket.io.on("reconnect_error", (err) => {
118
- __privateMethod(this, _SocketIoClientHelper_instances, LogErrorMessage_fn).call(this, `SocketIoClientHelper(): socketDetail.socket.io.on('reconnect_error'): [${err}]`);
119
- });
120
- socketDetail.socket.io.on("reconnect", (attempt) => {
121
- __privateMethod(this, _SocketIoClientHelper_instances, LogErrorMessage_fn).call(this, `SocketIoClientHelper(): socketDetail.socket.io.on('reconnect'): Number: [${attempt}]`);
122
- });
123
- socketDetail.socket.on("connect_error", (err) => {
124
- __privateMethod(this, _SocketIoClientHelper_instances, LogErrorMessage_fn).call(this, `SocketIoClientHelper(): socketDetail.socket.on('connect_error'): [${err}]`);
125
- });
126
- socketDetail.socket.on("connect_timeout", () => {
127
- __privateMethod(this, _SocketIoClientHelper_instances, LogErrorMessage_fn).call(this, `SocketIoClientHelper(): socketDetail.socket.on('connect_timeout')`);
128
- });
129
- socketDetail.socket.on("connect", () => {
130
- if (socketDetail.socket) {
131
- __privateMethod(this, _SocketIoClientHelper_instances, LogDebugMessage_fn).call(this, `SocketIoClientHelper(): Socket: [${socketDetail.socket.id}]: connected`);
132
- if (onConnectCallBack) {
133
- setTimeout(() => {
134
- onConnectCallBack(socketDetail.socket);
135
- }, 0);
136
- }
137
- if (socketEventsCallBack) {
138
- socketEventsCallBack(socketDetail.socket);
96
+ socketDetail.socket = io(socketDetail.address, socketOptions);
97
+ socketDetail.socket.io.on("error", (err) => {
98
+ this.#LogErrorMessage(`SocketIoClientHelper(): socketDetail.socket.io.on('error'): [${err}]`);
99
+ });
100
+ socketDetail.socket.io.on("reconnect_error", (err) => {
101
+ this.#LogErrorMessage(`SocketIoClientHelper(): socketDetail.socket.io.on('reconnect_error'): [${err}]`);
102
+ });
103
+ socketDetail.socket.io.on("reconnect", (attempt) => {
104
+ this.#LogErrorMessage(`SocketIoClientHelper(): socketDetail.socket.io.on('reconnect'): Number: [${attempt}]`);
105
+ });
106
+ socketDetail.socket.on("connect_error", (err) => {
107
+ this.#LogErrorMessage(`SocketIoClientHelper(): socketDetail.socket.on('connect_error'): [${err}]`);
108
+ });
109
+ socketDetail.socket.on("connect_timeout", () => {
110
+ this.#LogErrorMessage(`SocketIoClientHelper(): socketDetail.socket.on('connect_timeout')`);
111
+ });
112
+ socketDetail.socket.on("connect", () => {
113
+ if (socketDetail.socket) {
114
+ this.#LogDebugMessage(`SocketIoClientHelper(): Socket: [${socketDetail.socket.id}]: connected`);
115
+ if (onConnectCallBack) {
116
+ setTimeout(() => {
117
+ onConnectCallBack(socketDetail.socket);
118
+ }, 0);
119
+ }
120
+ if (socketEventsCallBack) {
121
+ socketEventsCallBack(socketDetail.socket);
122
+ }
123
+ } else {
124
+ const errorMessage = "SocketIoClientHelper(): Could not get socket object from socket.io";
125
+ this.#LogErrorMessage(errorMessage);
126
+ socketDetail.errorCallBack(new Error(errorMessage));
139
127
  }
140
- } else {
141
- const errorMessage = "SocketIoClientHelper(): Could not get socket object from socket.io";
142
- __privateMethod(this, _SocketIoClientHelper_instances, LogErrorMessage_fn).call(this, errorMessage);
143
- socketDetail.errorCallBack(new Error(errorMessage));
144
- }
145
- });
146
- socketDetail.socket.on("disconnect", (reason) => {
147
- var _a;
148
- __privateMethod(this, _SocketIoClientHelper_instances, LogDebugMessage_fn).call(this, "SocketIoClientHelper(): socket disconnect: " + reason);
149
- switch (reason) {
150
- case "io server disconnect":
151
- {
152
- __privateMethod(this, _SocketIoClientHelper_instances, LogDebugMessage_fn).call(this, "SocketIoClientHelper(): The server disconnected using disconnectSockets, i.e. normal safe shutdown from explicit disconnection by the server.");
153
- __privateMethod(this, _SocketIoClientHelper_instances, LogDebugMessage_fn).call(this, "SocketIoClientHelper(): The connection will be re-established when the server becomes available.");
154
- const socketDetail2 = this.sockets[name];
155
- socketDetail2.socket = null;
156
- if (isNode) {
157
- if (__privateGet(this, _options).agentManager) {
158
- __privateGet(this, _options).agentManager.ResetAgent();
128
+ });
129
+ socketDetail.socket.on("disconnect", (reason) => {
130
+ this.#LogDebugMessage("SocketIoClientHelper(): socket disconnect: " + reason);
131
+ switch (reason) {
132
+ case "io server disconnect":
133
+ {
134
+ this.#LogDebugMessage("SocketIoClientHelper(): The server disconnected using disconnectSockets, i.e. normal safe shutdown from explicit disconnection by the server.");
135
+ this.#LogDebugMessage("SocketIoClientHelper(): The connection will be re-established when the server becomes available.");
136
+ const socketDetail2 = this.sockets[name];
137
+ socketDetail2.socket = null;
138
+ if (isNode) {
139
+ if (this.#options.agentManager) {
140
+ this.#options.agentManager.ResetAgent();
141
+ }
142
+ setTimeout(() => this.#EstablishSocketConnect(name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack), 100).unref();
143
+ } else {
144
+ setTimeout(() => this.#EstablishSocketConnect(name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack), 100);
159
145
  }
160
- setTimeout(() => __privateMethod(this, _SocketIoClientHelper_instances, EstablishSocketConnect_fn).call(this, name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack), 100).unref();
161
- } else {
162
- setTimeout(() => __privateMethod(this, _SocketIoClientHelper_instances, EstablishSocketConnect_fn).call(this, name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack), 100);
163
146
  }
164
- }
165
- break;
166
- case "io client disconnect":
167
- __privateMethod(this, _SocketIoClientHelper_instances, LogDebugMessage_fn).call(this, "SocketIoClientHelper(): The client disconnected using disconnectSockets, i.e. normal safe disconnection from explicit disconnection by the client.");
168
- __privateMethod(this, _SocketIoClientHelper_instances, LogDebugMessage_fn).call(this, "SocketIoClientHelper(): The connection will not be re-established automatically.");
169
- break;
170
- case "transport close":
171
- case "ping timeout":
172
- case "transport error":
173
- {
174
- __privateMethod(this, _SocketIoClientHelper_instances, LogDebugMessage_fn).call(this, `SocketIoClientHelper(): Server unexpectedly disconnected. Reason: [${reason}]`);
175
- __privateMethod(this, _SocketIoClientHelper_instances, LogDebugMessage_fn).call(this, "SocketIoClientHelper(): The connection will be re-established when the server becomes available.");
176
- const socketDetail2 = this.sockets[name];
177
- if (socketDetail2.socket) {
178
- socketDetail2.socket.disconnect();
179
- }
180
- socketDetail2.socket = null;
181
- if (isNode) {
182
- if (__privateGet(this, _options).agentManager) {
183
- (_a = __privateGet(this, _options).agentManager) == null ? void 0 : _a.ResetAgent();
147
+ break;
148
+ case "io client disconnect":
149
+ this.#LogDebugMessage("SocketIoClientHelper(): The client disconnected using disconnectSockets, i.e. normal safe disconnection from explicit disconnection by the client.");
150
+ this.#LogDebugMessage("SocketIoClientHelper(): The connection will not be re-established automatically.");
151
+ break;
152
+ case "transport close":
153
+ case "ping timeout":
154
+ case "transport error":
155
+ {
156
+ this.#LogDebugMessage(`SocketIoClientHelper(): Server unexpectedly disconnected. Reason: [${reason}]`);
157
+ this.#LogDebugMessage("SocketIoClientHelper(): The connection will be re-established when the server becomes available.");
158
+ const socketDetail2 = this.sockets[name];
159
+ if (socketDetail2.socket) {
160
+ socketDetail2.socket.disconnect();
161
+ }
162
+ socketDetail2.socket = null;
163
+ if (isNode) {
164
+ if (this.#options.agentManager) {
165
+ this.#options.agentManager?.ResetAgent();
166
+ }
167
+ setTimeout(() => this.#EstablishSocketConnect(name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack), 100).unref();
168
+ } else {
169
+ setTimeout(() => this.#EstablishSocketConnect(name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack), 100);
184
170
  }
185
- setTimeout(() => __privateMethod(this, _SocketIoClientHelper_instances, EstablishSocketConnect_fn).call(this, name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack), 100).unref();
186
- } else {
187
- setTimeout(() => __privateMethod(this, _SocketIoClientHelper_instances, EstablishSocketConnect_fn).call(this, name, socketIoCustomPath, onConnectCallBack, socketEventsCallBack), 100);
188
171
  }
189
- }
190
- break;
191
- }
192
- });
193
- };
172
+ break;
173
+ }
174
+ });
175
+ }
176
+ }
194
177
  class SocketIoServerHelper {
178
+ #socketIoServerHelperOptions;
179
+ #namespace = {};
195
180
  constructor(options) {
196
- __privateAdd(this, _socketIoServerHelperOptions);
197
- __privateAdd(this, _namespace, {});
198
- __publicField(this, "LeaveRoom", (namespace, socket, room) => {
199
- this.LogMessage(namespace, `${namespace.socketionamespace.name}: Leaving room [${room}]`);
200
- socket.leave(room);
181
+ this.#socketIoServerHelperOptions = options;
182
+ }
183
+ LogMessage(namespace, message) {
184
+ if (namespace) {
185
+ this.#socketIoServerHelperOptions.logger.debug(`${namespace.namespace}: ${message}`);
186
+ } else {
187
+ this.#socketIoServerHelperOptions.logger.debug(`${message}`);
188
+ }
189
+ }
190
+ LeaveRoom = (namespace, socket, room) => {
191
+ this.LogMessage(namespace, `${namespace.socketionamespace.name}: Leaving room [${room}]`);
192
+ socket.leave(room);
193
+ };
194
+ JoinRoom = (namespace, socket, room) => {
195
+ this.LogMessage(namespace, `${namespace.socketionamespace.name}: Socket joining room [${room}], ID: [${socket.id}]`);
196
+ socket.join(room);
197
+ };
198
+ #SetupStandardEvents = (namespace, socket) => {
199
+ socket.on("disconnect", (reason) => {
200
+ this.LogMessage(namespace, `${namespace.socketionamespace.name}: socket disconnect, ID: [${socket.id}] [${reason}]`);
201
201
  });
202
- __publicField(this, "JoinRoom", (namespace, socket, room) => {
203
- this.LogMessage(namespace, `${namespace.socketionamespace.name}: Socket joining room [${room}], ID: [${socket.id}]`);
204
- socket.join(room);
202
+ socket.on("disconnecting", (reason) => {
203
+ this.LogMessage(namespace, `${namespace.socketionamespace.name}: socket disconnecting, ID: [${socket.id}] [${reason}]`);
205
204
  });
206
- __privateAdd(this, _SetupStandardEvents, (namespace, socket) => {
207
- socket.on("disconnect", (reason) => {
208
- this.LogMessage(namespace, `${namespace.socketionamespace.name}: socket disconnect, ID: [${socket.id}] [${reason}]`);
209
- });
210
- socket.on("disconnecting", (reason) => {
211
- this.LogMessage(namespace, `${namespace.socketionamespace.name}: socket disconnecting, ID: [${socket.id}] [${reason}]`);
212
- });
213
- socket.on("error", (error) => {
214
- this.LogMessage(namespace, `${namespace.socketionamespace.name}: socket error, ID: [${socket.id}] [${error}]`);
215
- });
216
- socket.on("__STSdisconnect", (reason) => {
217
- this.LogMessage(namespace, `${namespace.socketionamespace.name}: __STSdisconnect: socket disconnect, ID: [${socket.id}] [${reason}]`);
218
- });
219
- socket.on("__STSdisconnecting", (reason, callBackResult) => {
220
- this.LogMessage(namespace, `${namespace.socketionamespace.name}: __STSdisconnecting: socket disconnecting, ID: [${socket.id}] [${reason}]`);
221
- callBackResult("__STSdisconnecting accepted by server.");
222
- });
223
- socket.on("__STSjoinRoom", (rooms) => {
224
- rooms.forEach((room) => {
225
- this.JoinRoom(namespace, socket, room);
226
- });
227
- });
228
- socket.on("__STSleaveRoom", (rooms) => {
229
- rooms.forEach((room) => {
230
- this.LeaveRoom(namespace, socket, room);
231
- });
232
- });
233
- socket.on("__STSsendToRoom", (rooms, payload) => {
234
- rooms.forEach((room) => {
235
- this.LogMessage(namespace, `${namespace.socketionamespace.name}:socket.on:sendToRoom: __STSsendToRoom: Sending to room [${room}], ID: [${socket.id}]`);
236
- namespace.socketionamespace.to(room).emit(payload.command, payload);
237
- });
238
- });
205
+ socket.on("error", (error) => {
206
+ this.LogMessage(namespace, `${namespace.socketionamespace.name}: socket error, ID: [${socket.id}] [${error}]`);
239
207
  });
240
- // Use this middleward to check every incomming connection
241
- __privateAdd(this, _SetupConnectionMiddleware, (nameSpace) => {
242
- nameSpace.socketionamespace.use((socket, next) => {
243
- {
244
- next();
245
- }
208
+ socket.on("__STSdisconnect", (reason) => {
209
+ this.LogMessage(namespace, `${namespace.socketionamespace.name}: __STSdisconnect: socket disconnect, ID: [${socket.id}] [${reason}]`);
210
+ });
211
+ socket.on("__STSdisconnecting", (reason, callBackResult) => {
212
+ this.LogMessage(namespace, `${namespace.socketionamespace.name}: __STSdisconnecting: socket disconnecting, ID: [${socket.id}] [${reason}]`);
213
+ callBackResult("__STSdisconnecting accepted by server.");
214
+ });
215
+ socket.on("__STSjoinRoom", (rooms) => {
216
+ rooms.forEach((room) => {
217
+ this.JoinRoom(namespace, socket, room);
246
218
  });
247
219
  });
248
- // Use this middleware to check very packet being received
249
- __privateAdd(this, _SetupMessageMiddleware, (socket) => {
250
- socket.use(([event, ...args], next) => {
251
- next();
220
+ socket.on("__STSleaveRoom", (rooms) => {
221
+ rooms.forEach((room) => {
222
+ this.LeaveRoom(namespace, socket, room);
252
223
  });
253
224
  });
254
- __publicField(this, "SetupNamespace", (io2, namespace, rooms, autoJoinRooms, socketConnectCallBack, socketEventsCallBack) => {
255
- __privateGet(this, _namespace)[namespace] = {
256
- namespace,
257
- pid: process.pid,
258
- socketionamespace: io2.of(`/${namespace}/`)
259
- };
260
- __privateGet(this, _SetupConnectionMiddleware).call(this, __privateGet(this, _namespace)[namespace]);
261
- __privateGet(this, _namespace)[namespace].socketionamespace.on("connection", (socket) => {
262
- this.LogMessage(__privateGet(this, _namespace)[namespace], `${namespace}: Socket connected, ID: [${socket.id}]`);
263
- this.LogMessage(__privateGet(this, _namespace)[namespace], `${namespace}: Authentication Handshake: [${JSON.stringify(socket.handshake.auth)}]`);
264
- __privateGet(this, _SetupMessageMiddleware).call(this, socket);
265
- if (autoJoinRooms) {
266
- rooms.map((room) => {
267
- this.JoinRoom(__privateGet(this, _namespace)[namespace], socket, room);
268
- });
269
- }
270
- __privateGet(this, _SetupStandardEvents).call(this, __privateGet(this, _namespace)[namespace], socket);
271
- if (socketConnectCallBack) {
272
- setTimeout(() => {
273
- socketConnectCallBack(socket);
274
- }, 0);
275
- }
276
- if (socketEventsCallBack) {
277
- socketEventsCallBack(socket);
278
- }
225
+ socket.on("__STSsendToRoom", (rooms, payload) => {
226
+ rooms.forEach((room) => {
227
+ this.LogMessage(namespace, `${namespace.socketionamespace.name}:socket.on:sendToRoom: __STSsendToRoom: Sending to room [${room}], ID: [${socket.id}]`);
228
+ namespace.socketionamespace.to(room).emit(payload.command, payload);
279
229
  });
280
- return __privateGet(this, _namespace)[namespace].socketionamespace;
281
230
  });
282
- __publicField(this, "GetSTSSocketIONamespace", (namespace) => {
283
- return __privateGet(this, _namespace)[namespace];
231
+ };
232
+ // Use this middleward to check every incomming connection
233
+ #SetupConnectionMiddleware = (nameSpace) => {
234
+ nameSpace.socketionamespace.use((socket, next) => {
235
+ {
236
+ next();
237
+ }
284
238
  });
285
- __publicField(this, "GetSTSSocketIONamespaces", () => {
286
- return __privateGet(this, _namespace);
239
+ };
240
+ // Use this middleware to check very packet being received
241
+ #SetupMessageMiddleware = (socket) => {
242
+ socket.use(([event, ...args], next) => {
243
+ next();
287
244
  });
288
- __publicField(this, "CloseAdaptors", async () => {
289
- for (const [, namespace] of Object.entries(__privateGet(this, _namespace))) {
290
- await namespace.socketionamespace.adapter.close();
245
+ };
246
+ SetupNamespace = (io2, namespace, rooms, autoJoinRooms, socketConnectCallBack, socketEventsCallBack) => {
247
+ this.#namespace[namespace] = {
248
+ namespace,
249
+ pid: process.pid,
250
+ socketionamespace: io2.of(`/${namespace}/`)
251
+ };
252
+ this.#SetupConnectionMiddleware(this.#namespace[namespace]);
253
+ this.#namespace[namespace].socketionamespace.on("connection", (socket) => {
254
+ this.LogMessage(this.#namespace[namespace], `${namespace}: Socket connected, ID: [${socket.id}]`);
255
+ this.LogMessage(this.#namespace[namespace], `${namespace}: Authentication Handshake: [${JSON.stringify(socket.handshake.auth)}]`);
256
+ this.#SetupMessageMiddleware(socket);
257
+ if (autoJoinRooms) {
258
+ rooms.map((room) => {
259
+ this.JoinRoom(this.#namespace[namespace], socket, room);
260
+ });
291
261
  }
292
- });
293
- __publicField(this, "DisconnectSockets", () => {
294
- for (const [, namespace] of Object.entries(__privateGet(this, _namespace))) {
295
- namespace.socketionamespace.disconnectSockets();
262
+ this.#SetupStandardEvents(this.#namespace[namespace], socket);
263
+ if (socketConnectCallBack) {
264
+ setTimeout(() => {
265
+ socketConnectCallBack(socket);
266
+ }, 0);
267
+ }
268
+ if (socketEventsCallBack) {
269
+ socketEventsCallBack(socket);
296
270
  }
297
- __privateSet(this, _namespace, {});
298
271
  });
299
- __privateSet(this, _socketIoServerHelperOptions, options);
300
- }
301
- LogMessage(namespace, message) {
302
- if (namespace) {
303
- __privateGet(this, _socketIoServerHelperOptions).logger.debug(`${namespace.namespace}: ${message}`);
304
- } else {
305
- __privateGet(this, _socketIoServerHelperOptions).logger.debug(`${message}`);
272
+ return this.#namespace[namespace].socketionamespace;
273
+ };
274
+ GetSTSSocketIONamespace = (namespace) => {
275
+ return this.#namespace[namespace];
276
+ };
277
+ GetSTSSocketIONamespaces = () => {
278
+ return this.#namespace;
279
+ };
280
+ CloseAdaptors = async () => {
281
+ for (const [, namespace] of Object.entries(this.#namespace)) {
282
+ await namespace.socketionamespace.adapter.close();
306
283
  }
307
- }
284
+ };
285
+ DisconnectSockets = () => {
286
+ for (const [, namespace] of Object.entries(this.#namespace)) {
287
+ namespace.socketionamespace.disconnectSockets();
288
+ }
289
+ this.#namespace = {};
290
+ };
308
291
  SetupEvent(event, eventCb) {
309
292
  return this;
310
293
  }
311
294
  }
312
- _socketIoServerHelperOptions = new WeakMap();
313
- _namespace = new WeakMap();
314
- _SetupStandardEvents = new WeakMap();
315
- _SetupConnectionMiddleware = new WeakMap();
316
- _SetupMessageMiddleware = new WeakMap();
317
295
  export {
318
296
  STSEvent,
319
297
  STSNamespace,