]> Piment Noir Git Repositories - poolifier.git/commitdiff
fix(examples): properly handle ws raw data
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 2 Nov 2025 16:13:54 +0000 (17:13 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 2 Nov 2025 16:13:54 +0000 (17:13 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
examples/typescript/websocket-server-pool/ws-cluster/src/utils.ts [new file with mode: 0644]
examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts
examples/typescript/websocket-server-pool/ws-hybrid/src/utils.ts [new file with mode: 0644]
examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts
examples/typescript/websocket-server-pool/ws-worker_threads/src/main.ts
examples/typescript/websocket-server-pool/ws-worker_threads/src/utils.ts [new file with mode: 0644]
src/circular-buffer.ts

diff --git a/examples/typescript/websocket-server-pool/ws-cluster/src/utils.ts b/examples/typescript/websocket-server-pool/ws-cluster/src/utils.ts
new file mode 100644 (file)
index 0000000..22f67e1
--- /dev/null
@@ -0,0 +1,16 @@
+import type { RawData } from 'ws'
+
+/**
+ * Converts WebSocket RawData to string safely
+ * @param message - The RawData from WebSocket
+ * @returns String representation of the message
+ */
+export function rawDataToString (message: RawData): string {
+  if (message instanceof Buffer) {
+    return message.toString()
+  }
+  if (Array.isArray(message)) {
+    return Buffer.concat(message).toString()
+  }
+  return new TextDecoder().decode(message as ArrayBuffer)
+}
index 4cc8088759c915313395e08a99a32e8da2391cc1..090ba60e76a917dd9b8576324fb2b34d8911fd42 100644 (file)
@@ -8,6 +8,7 @@ import {
   type WorkerData,
   type WorkerResponse,
 } from './types.js'
+import { rawDataToString } from './utils.js'
 
 class WebSocketServerWorker extends ClusterWorker<WorkerData, WorkerResponse> {
   private static wss: WebSocketServer
@@ -48,7 +49,7 @@ class WebSocketServerWorker extends ClusterWorker<WorkerData, WorkerResponse> {
       ws.on('error', console.error)
       ws.on('message', (message: RawData) => {
         const { data, type } = JSON.parse(
-          message.toString()
+          rawDataToString(message)
         ) as MessagePayload<DataPayload>
         switch (type) {
           case MessageType.echo:
diff --git a/examples/typescript/websocket-server-pool/ws-hybrid/src/utils.ts b/examples/typescript/websocket-server-pool/ws-hybrid/src/utils.ts
new file mode 100644 (file)
index 0000000..22f67e1
--- /dev/null
@@ -0,0 +1,16 @@
+import type { RawData } from 'ws'
+
+/**
+ * Converts WebSocket RawData to string safely
+ * @param message - The RawData from WebSocket
+ * @returns String representation of the message
+ */
+export function rawDataToString (message: RawData): string {
+  if (message instanceof Buffer) {
+    return message.toString()
+  }
+  if (Array.isArray(message)) {
+    return Buffer.concat(message).toString()
+  }
+  return new TextDecoder().decode(message as ArrayBuffer)
+}
index 86a0c8f8d149b06f83143ed65c3ab3c70d6bd76d..f055a2b370f07219988bc23bb4aa6a609adcd3dc 100644 (file)
@@ -14,6 +14,7 @@ import {
   type ThreadWorkerData,
   type ThreadWorkerResponse,
 } from './types.js'
+import { rawDataToString } from './utils.js'
 
 const emptyFunction = (): void => {
   /* Intentional */
@@ -66,7 +67,7 @@ class WebSocketServerWorker extends ClusterWorker<
       ws.on('error', console.error)
       ws.on('message', (message: RawData) => {
         const { data, type } = JSON.parse(
-          message.toString()
+          rawDataToString(message)
         ) as MessagePayload<DataPayload>
         switch (type) {
           case MessageType.echo:
index 38ea9d9563a176a1770ed7f2f5ec75e457fd6a9d..58bb65411511c92f4007522aede1a0d981017359 100644 (file)
@@ -2,6 +2,7 @@ import { type RawData, WebSocketServer } from 'ws'
 
 import { requestHandlerPool } from './pool.js'
 import { type DataPayload, type MessagePayload, MessageType } from './types.js'
+import { rawDataToString } from './utils.js'
 
 const port = 8080
 const wss = new WebSocketServer({ port }, () => {
@@ -18,7 +19,7 @@ wss.on('connection', ws => {
   ws.on('error', console.error)
   ws.on('message', (message: RawData) => {
     const { data, type } = JSON.parse(
-      message.toString()
+      rawDataToString(message)
     ) as MessagePayload<DataPayload>
     switch (type) {
       case MessageType.echo:
diff --git a/examples/typescript/websocket-server-pool/ws-worker_threads/src/utils.ts b/examples/typescript/websocket-server-pool/ws-worker_threads/src/utils.ts
new file mode 100644 (file)
index 0000000..22f67e1
--- /dev/null
@@ -0,0 +1,16 @@
+import type { RawData } from 'ws'
+
+/**
+ * Converts WebSocket RawData to string safely
+ * @param message - The RawData from WebSocket
+ * @returns String representation of the message
+ */
+export function rawDataToString (message: RawData): string {
+  if (message instanceof Buffer) {
+    return message.toString()
+  }
+  if (Array.isArray(message)) {
+    return Buffer.concat(message).toString()
+  }
+  return new TextDecoder().decode(message as ArrayBuffer)
+}
index 63fa44563f728c8559ad1bbcaa7e19f0ae63040d..7748cf56c9b6c1e23d28dc50df09d7a6a2b0677f 100644 (file)
@@ -15,6 +15,7 @@ export class CircularBuffer {
   private writeIdx: number
 
   /**
+   * CircularBuffer constructor.
    * @param size - Buffer size.
    * @defaultValue defaultBufferSize
    * @returns CircularBuffer.