@stonyx/sockets 0.1.1-beta.13 → 0.1.1-beta.15
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.d.ts +2 -2
- package/dist/client.js +12 -5
- package/dist/server.d.ts +2 -2
- package/dist/server.js +11 -4
- package/package.json +1 -1
package/dist/client.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ export default class SocketClient {
|
|
|
24
24
|
resolve: () => void;
|
|
25
25
|
reject: (reason?: unknown) => void;
|
|
26
26
|
} | null;
|
|
27
|
-
onDisconnect: (() => void) | null;
|
|
27
|
+
onDisconnect: ((code: number, reason: string) => void) | null;
|
|
28
28
|
onReconnecting: ((attempt: number, delay: number) => void) | null;
|
|
29
29
|
onReconnected: (() => void) | null;
|
|
30
30
|
onReconnectFailed: (() => void) | null;
|
|
@@ -36,7 +36,7 @@ export default class SocketClient {
|
|
|
36
36
|
send(payload: SocketMessage, useGlobalKey?: boolean): void;
|
|
37
37
|
heartBeat(): void;
|
|
38
38
|
nextHeartBeat(): void;
|
|
39
|
-
onClose(): void;
|
|
39
|
+
onClose(code?: number, reason?: string): void;
|
|
40
40
|
close(): void;
|
|
41
41
|
getReconnectDelay(): number;
|
|
42
42
|
reconnect(): Promise<void>;
|
package/dist/client.js
CHANGED
|
@@ -55,10 +55,10 @@ export default class SocketClient {
|
|
|
55
55
|
const socket = new WebSocket(address);
|
|
56
56
|
this.socket = socket;
|
|
57
57
|
socket.on('message', (data) => this.onMessage(data));
|
|
58
|
-
socket.on('close', () => this.onClose());
|
|
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,11 +124,12 @@ export default class SocketClient {
|
|
|
118
124
|
const { heartBeatInterval } = config.sockets;
|
|
119
125
|
this._heartBeatTimer = setTimeout(() => this.heartBeat(), heartBeatInterval);
|
|
120
126
|
}
|
|
121
|
-
|
|
122
|
-
|
|
127
|
+
// ws always provides code and reason; params are optional for direct calls and testing
|
|
128
|
+
onClose(code, reason) {
|
|
129
|
+
log.socket(`Disconnected from remote server (code: ${code ?? 'unknown'}, reason: ${reason || 'none'})`);
|
|
123
130
|
if (this._heartBeatTimer)
|
|
124
131
|
clearTimeout(this._heartBeatTimer);
|
|
125
|
-
this.onDisconnect?.();
|
|
132
|
+
this.onDisconnect?.(code ?? 1006, reason ?? '');
|
|
126
133
|
if (!this._intentionalClose) {
|
|
127
134
|
this.reconnect();
|
|
128
135
|
}
|
package/dist/server.d.ts
CHANGED
|
@@ -31,14 +31,14 @@ export default class SocketServer {
|
|
|
31
31
|
wss: WebSocketServer | null;
|
|
32
32
|
encryptionEnabled: boolean;
|
|
33
33
|
globalKey: Buffer | null;
|
|
34
|
-
onClientDisconnect: ((client: ConnectedClient) => void) | null;
|
|
34
|
+
onClientDisconnect: ((client: ConnectedClient, code: number, reason: string) => void) | null;
|
|
35
35
|
constructor();
|
|
36
36
|
init(): Promise<void>;
|
|
37
37
|
discoverHandlers(): Promise<void>;
|
|
38
38
|
validateAuthHandler(): void;
|
|
39
39
|
onMessage(payload: Buffer | string, client: ConnectedClient): Promise<void>;
|
|
40
40
|
prepareSend(client: ConnectedClient, ws: WebSocket): void;
|
|
41
|
-
handleDisconnect(client: ConnectedClient): void;
|
|
41
|
+
handleDisconnect(client: ConnectedClient, code?: number, reason?: string): void;
|
|
42
42
|
sendTo(clientId: number, request: string, response: unknown): void;
|
|
43
43
|
sendToByMeta(key: string, value: unknown, request: string, response: unknown): boolean;
|
|
44
44
|
broadcast(request: string, response: unknown): void;
|
package/dist/server.js
CHANGED
|
@@ -37,7 +37,7 @@ export default class SocketServer {
|
|
|
37
37
|
client.__authenticated = false;
|
|
38
38
|
this.prepareSend(client, ws);
|
|
39
39
|
ws.on('message', (payload) => this.onMessage(payload, client));
|
|
40
|
-
ws.on('close', () => this.handleDisconnect(client));
|
|
40
|
+
ws.on('close', (code, reason) => this.handleDisconnect(client, code, reason.toString()));
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
43
|
async discoverHandlers() {
|
|
@@ -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,11 +128,12 @@ export default class SocketServer {
|
|
|
122
128
|
}
|
|
123
129
|
};
|
|
124
130
|
}
|
|
125
|
-
|
|
131
|
+
// ws always provides code and reason; params are optional for direct calls and testing
|
|
132
|
+
handleDisconnect(client, code, reason) {
|
|
126
133
|
const { ip } = client;
|
|
127
|
-
log.socket(`[${ip}] Client disconnected`);
|
|
134
|
+
log.socket(`[${ip}] Client disconnected (code: ${code ?? 'unknown'}, reason: ${reason || 'none'})`);
|
|
128
135
|
this.clientMap.delete(client.id);
|
|
129
|
-
this.onClientDisconnect?.(client);
|
|
136
|
+
this.onClientDisconnect?.(client, code ?? 1006, reason ?? '');
|
|
130
137
|
}
|
|
131
138
|
sendTo(clientId, request, response) {
|
|
132
139
|
const client = this.clientMap.get(clientId);
|