Skip to content

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

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

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion source/simple_socketio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no need for + since packetType is always a single digit.

would be nice with a small comment that showcased the message format, explaining that double colons are when data is not set.

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