-
Notifications
You must be signed in to change notification settings - Fork 0
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
Packet Validaton #1
Conversation
src/index.ts
Outdated
webSocket.publish(webSocket.data.roomId, message) | ||
message: function (webSocket: ServerWebSocket<ClientData>, message: string): void | Promise<void> { | ||
let packet = JSON.parse(message) as Packet; | ||
if(packet !== undefined && packet.getType().startsWith("CLIENT_")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
packet cannot be undefined here.
When there is no valid json object in message JSON.parse throws an error. -
it is not possible for packet to have a function "getType"
JSON.parse always just returns a plain object, the parsed JSON. And since you cannot define a function in JSON you cannot have a function in a parsed JSON object.
Therefore this code is broken and will always throw an error
src/lib/packet.ts
Outdated
getData() : unknown { | ||
return this.data | ||
} | ||
|
||
toString() : string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not Java, a toString function is not needed.
You can just call JSON.stringify on the object and get the same result.
e.g. JSON.stringify(this)
src/lib/packet.ts
Outdated
@@ -1,5 +1,4 @@ | |||
export class ServerPacket { | |||
|
|||
export class Packet { | |||
private readonly type: string; | |||
private readonly data: unknown; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what this type "unknown" is/do.
Unless it has some functionaility I'm not aware of i would rather use "any"
No description provided.