From e7ceb836a2f61b9110eeaeba6a80e9689b224595 Mon Sep 17 00:00:00 2001 From: Lars Johansson Date: Mon, 20 Nov 2023 13:02:46 +0100 Subject: [PATCH 1/2] fix: Event server error when event data contains colons --- source/simple_socketio.ts | 3 ++- test/simple_socketio.test.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/source/simple_socketio.ts b/source/simple_socketio.ts index 9e288b2..989a24b 100644 --- a/source/simple_socketio.ts +++ b/source/simple_socketio.ts @@ -204,7 +204,8 @@ export default class SimpleSocketIOClient { * @private */ private handleMessage(event: MessageEvent): void { - const [packetType, data] = event.data.split(/:::?/); + const packetType = event.data[0]; // Get the first character of the message, the packet type + const data = event.data.replace(/^\d+:::?/, ""); // Remove the packet type and the : that split message parts. if (packetType === PACKET_TYPES.event) { const parsedData = JSON.parse(data) as Payload; const { name, args } = parsedData; diff --git a/test/simple_socketio.test.js b/test/simple_socketio.test.js index 96a6e17..66a0128 100644 --- a/test/simple_socketio.test.js +++ b/test/simple_socketio.test.js @@ -225,6 +225,18 @@ describe("Tests using SimpleSocketIOClient", () => { expect(client.handleEvent).toHaveBeenCalledWith(eventName, eventData); }); + test("handleMessage correctly handles data with '::' in it", () => { + const eventName = "testEvent"; + const eventData = { foo: ":::bar" }; + const packetData = JSON.stringify({ name: eventName, args: [eventData] }); + + vi.spyOn(client, "handleEvent"); + + client.handleMessage({ data: `${PACKET_TYPES.event}:::${packetData}` }); + + expect(client.handleEvent).toHaveBeenCalledWith(eventName, eventData); + }); + test("handleMessage correctly handles heartbeat packet type", () => { client.webSocket = createWebSocketMock(); From acce97a195e654db546a2b69b6b6663605bd9650 Mon Sep 17 00:00:00 2001 From: Lars Johansson Date: Mon, 20 Nov 2023 14:13:33 +0100 Subject: [PATCH 2/2] PR review changes --- source/simple_socketio.ts | 8 +++++++- test/simple_socketio.test.js | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/source/simple_socketio.ts b/source/simple_socketio.ts index 989a24b..c7d990a 100644 --- a/source/simple_socketio.ts +++ b/source/simple_socketio.ts @@ -201,11 +201,17 @@ export default class SimpleSocketIOClient { } /** * Handles WebSocket messages + * The messages are a version of the socket.io message protocol + * from before v1. The messages are formatted with the first character + * being an number, the packet type (see PACKET_TYPES const for available types). + * It is followed by two or three colons depending on if the packet type has data + * or not. For example heartbeat, "2::", does not contain data, while message, "3:::Hello", does. + * * @private */ private handleMessage(event: MessageEvent): void { const packetType = event.data[0]; // Get the first character of the message, the packet type - const data = event.data.replace(/^\d+:::?/, ""); // Remove the packet type and the : that split message parts. + const data = event.data.replace(/^\d:::?/, ""); // Remove the packet type and the : that split message parts. if (packetType === PACKET_TYPES.event) { const parsedData = JSON.parse(data) as Payload; const { name, args } = parsedData; diff --git a/test/simple_socketio.test.js b/test/simple_socketio.test.js index 66a0128..ed54dc1 100644 --- a/test/simple_socketio.test.js +++ b/test/simple_socketio.test.js @@ -227,7 +227,7 @@ describe("Tests using SimpleSocketIOClient", () => { test("handleMessage correctly handles data with '::' in it", () => { const eventName = "testEvent"; - const eventData = { foo: ":::bar" }; + const eventData = { foo: "::bar" }; const packetData = JSON.stringify({ name: eventName, args: [eventData] }); vi.spyOn(client, "handleEvent");