Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb0e6400

Browse files
fix: properly detect plain objects
The typeof check was not sufficient, as it also matches arrays andnulls.
1 parentd9db473 commitb0e6400

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

‎lib/index.ts‎

Lines changed: 7 additions & 2 deletions
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+
functionisObject(value:any):boolean{
136+
returnObject.prototype.toString.call(value)==="[object Object]";
137+
}
138+
134139
interfaceDecoderReservedEvents{
135140
decoded:(packet:Packet)=>void;
136141
}
@@ -280,11 +285,11 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
280285
privatestaticisPayloadValid(type:PacketType,payload:any):boolean{
281286
switch(type){
282287
casePacketType.CONNECT:
283-
returntypeofpayload==="object";
288+
returnisObject(payload);
284289
casePacketType.DISCONNECT:
285290
returnpayload===undefined;
286291
casePacketType.CONNECT_ERROR:
287-
returntypeofpayload==="string"||typeofpayload==="object";
292+
returntypeofpayload==="string"||isObject(payload);
288293
casePacketType.EVENT:
289294
casePacketType.BINARY_EVENT:
290295
return(

‎test/parser.js‎

Lines changed: 1 addition & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp