playsocketjs 5.2.0 → 5.3.0

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/README.md CHANGED
@@ -282,6 +282,7 @@ new PlaySocketServer(options?: PlaySocketServerOptions)
282
282
  | `path` | `string` | No | "/" | WebSocket endpoint path. |
283
283
  | `server` | `http.Server` | No | - | Existing http server. |
284
284
  | `rateLimit` | `number` | No | 20 | Messages/second rate limit. |
285
+ | `maxPayload` | `number` | No | 6291456 | Max. size of a single incoming message in bytes. |
285
286
  | `debug` | `boolean` | No | false | Enable debug logging. |
286
287
  | `verifyClient` | `function` | No | - | Callback to verify connections before WebSocket upgrade. |
287
288
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playsocketjs",
3
- "version": "5.2.0",
3
+ "version": "5.3.0",
4
4
  "description": "WebSocket wrapper for creating simple multiplayer systems with ease.",
5
5
  "type": "module",
6
6
  "module": "./src/client/client.js",
@@ -88,10 +88,11 @@ export default class PlaySocketServer {
88
88
  * @param {string} [options.path='/'] - WebSocket endpoint path
89
89
  * @param {boolean} [options.debug=false] - Enable debug logging
90
90
  * @param {number} [options.rateLimit=20] - Maximum number of operations per second per client
91
+ * @param {number} [options.maxPayload=6291456] - Maximum size of a single incoming message in bytes
91
92
  * @param {import("ws").ServerOptions["verifyClient"]} [options.verifyClient] - Optional callback to verify client connections before upgrade. Receives (info, callback) where info contains { req, origin } and callback is (verified, code?, message?) => void
92
93
  */
93
94
  constructor(options = {}) {
94
- const { server, port = 3000, path = "/", debug = false, rateLimit = 20, verifyClient } = options;
95
+ const { server, port = 3000, path = "/", debug = false, rateLimit = 20, maxPayload = 6 * 1024 * 1024, verifyClient } = options;
95
96
 
96
97
  if (debug) this.#debug = true; // Enable extra logging
97
98
 
@@ -112,6 +113,7 @@ export default class PlaySocketServer {
112
113
  this.#wss = new WebSocketServer({
113
114
  server: this.#server,
114
115
  path,
116
+ maxPayload,
115
117
  ...(verifyClient && { verifyClient })
116
118
  });
117
119
 
@@ -9,7 +9,7 @@ export const HEARTBEAT_INTERVAL = 5000;
9
9
  /**
10
10
  * Library version, must match package.json
11
11
  */
12
- export const VERSION = "5.2.0";
12
+ export const VERSION = "5.3.0";
13
13
 
14
14
  /**
15
15
  * Logging prefixes
@@ -110,7 +110,7 @@ export default class CRDTManager {
110
110
  importPropertyUpdate(update) {
111
111
  try {
112
112
  const { key, operation: rawOperation, vectorClock } = update;
113
- const operation = /** @type {Operation} */ (this.#sanitizeValue(rawOperation));
113
+ const operation = /** @type {Operation} */ (this.#normalizeAndCopy(rawOperation));
114
114
  if (this.#debug) console.log(CONSOLE_PREFIX + "Importing update:", update); // Debug
115
115
 
116
116
  // Check key limit to safeguard against too many keys
@@ -183,9 +183,9 @@ export default class CRDTManager {
183
183
  */
184
184
  updateProperty(key, type, value, secondValue) {
185
185
  try {
186
- // Sanitize inputs
187
- value = this.#sanitizeValue(value);
188
- secondValue = this.#sanitizeValue(secondValue);
186
+ // Normalize inputs
187
+ value = this.#normalizeAndCopy(value);
188
+ secondValue = this.#normalizeAndCopy(secondValue);
189
189
 
190
190
  // Debug log
191
191
  if (this.#debug) console.log(CONSOLE_PREFIX + `Updating property with key ${key}, type ${type}, value ${value} and secondValue ${secondValue}`);
@@ -431,20 +431,15 @@ export default class CRDTManager {
431
431
  }
432
432
 
433
433
  /**
434
- * Remove HTML to prevent XSS and enforce size limits
435
- * @param {*} value - Value to sanitize
436
- * @returns {*} Sanitized value
434
+ * Enforce size limit, normalize undefined to null, and return deep copy
435
+ * @param {*} value - Value to normalize
436
+ * @returns {*} Normalized value
437
437
  */
438
- #sanitizeValue(value) {
439
- if (value === undefined) return null; // Normalize undefined to null to avoid divergence in transport
440
-
441
- // Check total serialized size
442
- const jsonString = JSON.stringify(value);
443
- if (jsonString?.length > 50000) throw new Error("Value too large"); // 50KB limit
444
-
445
- if (typeof value === "string") return (value.includes("<") || value.includes(">")) ? value.replace(/[<>]/g, "") : value;
446
- if (Array.isArray(value)) return Array.from(value, item => this.#sanitizeValue(item));
447
- if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, this.#sanitizeValue(v)]));
438
+ #normalizeAndCopy(value) {
439
+ if (value === undefined) return null;
440
+ if (JSON.stringify(value)?.length > 50_000) throw new Error("Value too large"); // 50KB limit
441
+ if (Array.isArray(value)) return Array.from(value, item => this.#normalizeAndCopy(item));
442
+ if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, this.#normalizeAndCopy(v)]));
448
443
  return value;
449
444
  }
450
445
 
@@ -114,6 +114,7 @@ export default class PlaySocketServer {
114
114
  * @param {string} [options.path='/'] - WebSocket endpoint path
115
115
  * @param {boolean} [options.debug=false] - Enable debug logging
116
116
  * @param {number} [options.rateLimit=20] - Maximum number of operations per second per client
117
+ * @param {number} [options.maxPayload=6291456] - Maximum size of a single incoming message in bytes
117
118
  * @param {import("ws").ServerOptions["verifyClient"]} [options.verifyClient] - Optional callback to verify client connections before upgrade. Receives (info, callback) where info contains { req, origin } and callback is (verified, code?, message?) => void
118
119
  */
119
120
  constructor(options?: {
@@ -122,6 +123,7 @@ export default class PlaySocketServer {
122
123
  path?: string;
123
124
  debug?: boolean;
124
125
  rateLimit?: number;
126
+ maxPayload?: number;
125
127
  verifyClient?: import("ws").ServerOptions["verifyClient"];
126
128
  });
127
129
  /**
@@ -8,7 +8,7 @@ export declare const HEARTBEAT_INTERVAL = 5000;
8
8
  /**
9
9
  * Library version, must match package.json
10
10
  */
11
- export declare const VERSION = "5.2.0";
11
+ export declare const VERSION = "5.3.0";
12
12
  /**
13
13
  * Logging prefixes
14
14
  */