Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Event server error when event data contains colons #177

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion source/simple_socketio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +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, 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;
Expand Down
12 changes: 12 additions & 0 deletions test/simple_socketio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down