@stonyx/sockets 0.1.1-alpha.39 → 0.1.1-alpha.40

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.
Files changed (2) hide show
  1. package/README.md +17 -5
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -15,11 +15,13 @@ WebSocket server and client module for the [Stonyx framework](https://github.com
15
15
  * **Encryption by default:** AES-256-GCM with per-session keys, zero external dependencies.
16
16
  * **Singleton pattern:** Matches the conventions of `@stonyx/rest-server` and `stonyx-orm`.
17
17
 
18
- ## Usage reference
18
+ ## Samples
19
19
 
20
- The handlers in [`test/sample/`](https://github.com/abofs/stonyx-sockets/tree/dev/test/sample) are the canonical usage reference for this module; the integration tests run against them. They are not included in the npm package.
20
+ The handlers in [`test/sample/`](https://github.com/abofs/stonyx-sockets/tree/dev/test/sample) are the canonical usage reference for this module; the integration tests run against them. They are not included in the npm package. They are TypeScript and type-check against the published `Handler` type, which declares `server` and `client` as function-valued properties, so the TypeScript samples assign the hooks as `function` properties rather than methods. The JavaScript examples below use methods, which work the same at runtime.
21
21
 
22
- Exports:
22
+ Before deploying, read the [security notes](https://github.com/abofs/stonyx-sockets/blob/dev/docs/encryption.md#security-notes): `authKey` falls back to the literal `'AUTH_KEY'` if unset, any `encryption` value other than `'true'`/`true` runs in plaintext, and the module provides no transport security unless you use `wss://`.
23
+
24
+ ## Exports
23
25
 
24
26
  * `@stonyx/sockets`: named `SocketServer`, `SocketClient` and `Handler`; the default export is the `Sockets` class Stonyx uses for auto-init
25
27
  * `@stonyx/sockets/server`: `SocketServer`
@@ -49,14 +51,24 @@ Each handler extends `Handler` and defines a `server()` method, a `client()` met
49
51
 
50
52
  ```javascript
51
53
  // socket-handlers/auth.js
54
+ import { timingSafeEqual } from 'node:crypto';
52
55
  import { Handler } from '@stonyx/sockets';
53
56
  import config from 'stonyx/config';
54
57
 
58
+ // Constant-time compare; timingSafeEqual throws on unequal lengths, so check length first.
59
+ function authKeyMatches(given) {
60
+ const { authKey } = config.sockets;
61
+ if (typeof authKey !== 'string' || authKey === '' || typeof given !== 'string') return false;
62
+
63
+ const a = Buffer.from(given), b = Buffer.from(authKey);
64
+ return a.length === b.length && timingSafeEqual(a, b);
65
+ }
66
+
55
67
  export default class AuthHandler extends Handler {
56
68
  static skipAuth = true; // auth handler must work before authentication
57
69
 
58
70
  server(data, client) {
59
- if (data.authKey !== config.sockets.authKey) return client.close();
71
+ if (!authKeyMatches(data?.authKey)) return client.close();
60
72
 
61
73
  this._serverRef.clientMap.set(client.id, client);
62
74
  return 'success';
@@ -217,7 +229,7 @@ Configuration is read from `stonyx/config` under `sockets`:
217
229
  |--------|---------|---------|-------------|
218
230
  | `port` | `SOCKET_PORT` | `2667` | WebSocket server port |
219
231
  | `address` | `SOCKET_ADDRESS` | `ws://localhost:2667` | Client connection address |
220
- | `authKey` | `SOCKET_AUTH_KEY` | `'AUTH_KEY'` | Shared authentication key |
232
+ | `authKey` | `SOCKET_AUTH_KEY` | `'AUTH_KEY'` | Shared authentication key; set it in production (see the [security notes](https://github.com/abofs/stonyx-sockets/blob/dev/docs/encryption.md#security-notes)) |
221
233
  | `heartBeatInterval` | `SOCKET_HEARTBEAT_INTERVAL` | `30000` | Heartbeat interval in ms |
222
234
  | `handlerDir` | `SOCKET_HANDLER_DIR` | `'./socket-handlers'` | Handler directory path |
223
235
  | `encryption` | `SOCKET_ENCRYPTION` | `'true'` | Enable AES-256-GCM encryption |
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.1.1-alpha.39",
7
+ "version": "0.1.1-alpha.40",
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",