@stonyx/sockets 0.1.1-alpha.10 → 0.1.1-alpha.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/client.js CHANGED
@@ -58,7 +58,7 @@ export default class SocketClient {
58
58
  socket.on('close', (code, reason) => this.onClose(code, reason.toString()));
59
59
  socket.on('error', () => {
60
60
  log.socket(`Error connecting to socket server`);
61
- reject('Error connecting to socket server');
61
+ reject(new Error('Error connecting to socket server'));
62
62
  });
63
63
  socket.on('open', () => {
64
64
  this._intentionalClose = false;
@@ -72,6 +72,8 @@ export default class SocketClient {
72
72
  let parsed;
73
73
  if (this.encryptionEnabled) {
74
74
  const key = this.sessionKey || this.globalKey;
75
+ if (!key)
76
+ throw new Error('Encryption enabled but no key available');
75
77
  const raw = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
76
78
  parsed = JSON.parse(decrypt(raw, key));
77
79
  }
@@ -102,8 +104,12 @@ export default class SocketClient {
102
104
  }
103
105
  }
104
106
  send(payload, useGlobalKey = false) {
107
+ if (!this.socket)
108
+ throw new Error('Socket is not connected');
105
109
  if (this.encryptionEnabled) {
106
110
  const key = useGlobalKey ? this.globalKey : this.sessionKey;
111
+ if (!key)
112
+ throw new Error('Encryption enabled but no key available');
107
113
  const data = encrypt(JSON.stringify(payload), key);
108
114
  this.socket.send(data);
109
115
  }
@@ -118,6 +124,7 @@ export default class SocketClient {
118
124
  const { heartBeatInterval } = config.sockets;
119
125
  this._heartBeatTimer = setTimeout(() => this.heartBeat(), heartBeatInterval);
120
126
  }
127
+ // ws always provides code and reason; params are optional for direct calls and testing
121
128
  onClose(code, reason) {
122
129
  log.socket(`Disconnected from remote server (code: ${code ?? 'unknown'}, reason: ${reason || 'none'})`);
123
130
  if (this._heartBeatTimer)
package/dist/server.js CHANGED
@@ -61,6 +61,8 @@ export default class SocketServer {
61
61
  let parsed;
62
62
  if (this.encryptionEnabled) {
63
63
  const key = client.__authenticated ? client.__sessionKey : this.globalKey;
64
+ if (!key)
65
+ throw new Error('Encryption enabled but no key available');
64
66
  const raw = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
65
67
  parsed = JSON.parse(decrypt(raw, key));
66
68
  }
@@ -92,6 +94,8 @@ export default class SocketServer {
92
94
  if (this.encryptionEnabled) {
93
95
  const sessionKey = generateSessionKey();
94
96
  client.__sessionKey = sessionKey;
97
+ if (!this.globalKey)
98
+ throw new Error('Encryption enabled but globalKey not initialized');
95
99
  client.send({ request, response, sessionKey: sessionKey.toString('base64') }, this.globalKey);
96
100
  }
97
101
  else {
@@ -114,6 +118,8 @@ export default class SocketServer {
114
118
  client.send = (payload, keyOverride) => {
115
119
  if (server.encryptionEnabled) {
116
120
  const key = keyOverride || client.__sessionKey;
121
+ if (!key)
122
+ throw new Error('Encryption enabled but no key available');
117
123
  const data = encrypt(JSON.stringify(payload), key);
118
124
  socketSend(data);
119
125
  }
@@ -122,6 +128,7 @@ export default class SocketServer {
122
128
  }
123
129
  };
124
130
  }
131
+ // ws always provides code and reason; params are optional for direct calls and testing
125
132
  handleDisconnect(client, code, reason) {
126
133
  const { ip } = client;
127
134
  log.socket(`[${ip}] Client disconnected (code: ${code ?? 'unknown'}, reason: ${reason || 'none'})`);
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.1.1-alpha.10",
7
+ "version": "0.1.1-alpha.11",
8
8
  "description": "WebSocket server and client module for the Stonyx framework",
9
9
  "main": "dist/main.js",
10
10
  "types": "dist/main.d.ts",