Skip to content

Commit b0e6400

Browse files
fix: properly detect plain objects
The typeof check was not sufficient, as it also matches arrays and nulls.
1 parent d9db473 commit b0e6400

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ export class Encoder {
131131
}
132132
}
133133

134+
// see https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript
135+
function isObject(value: any): boolean {
136+
return Object.prototype.toString.call(value) === "[object Object]";
137+
}
138+
134139
interface DecoderReservedEvents {
135140
decoded: (packet: Packet) => void;
136141
}
@@ -280,11 +285,11 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
280285
private static isPayloadValid(type: PacketType, payload: any): boolean {
281286
switch (type) {
282287
case PacketType.CONNECT:
283-
return typeof payload === "object";
288+
return isObject(payload);
284289
case PacketType.DISCONNECT:
285290
return payload === undefined;
286291
case PacketType.CONNECT_ERROR:
287-
return typeof payload === "string" || typeof payload === "object";
292+
return typeof payload === "string" || isObject(payload);
288293
case PacketType.EVENT:
289294
case PacketType.BINARY_EVENT:
290295
return (

test/parser.js

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ describe("socket.io-parser", () => {
115115

116116
isInvalidPayload('442["some","data"');
117117
isInvalidPayload('0/admin,"invalid"');
118+
isInvalidPayload("0[]");
118119
isInvalidPayload("1/admin,{}");
119120
isInvalidPayload('2/admin,"invalid');
120121
isInvalidPayload("2/admin,{}");

0 commit comments

Comments
 (0)