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

Packet Validaton #1

Closed
wants to merge 5 commits into from
Closed

Packet Validaton #1

wants to merge 5 commits into from

Conversation

NouCake
Copy link
Member

@NouCake NouCake commented Dec 23, 2023

No description provided.

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_")) {
Copy link
Member Author

Choose a reason for hiding this comment

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

  1. packet cannot be undefined here.
    When there is no valid json object in message JSON.parse throws an error.

  2. 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

getData() : unknown {
return this.data
}

toString() : string {
Copy link
Member Author

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)

@@ -1,5 +1,4 @@
export class ServerPacket {

export class Packet {
private readonly type: string;
private readonly data: unknown;
Copy link
Member Author

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"

@NouCake NouCake closed this Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants