Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Move test for undefined socket closer to usage
Browse files Browse the repository at this point in the history
We found that if sendMessage() was called but not awaited on, the
code execution would get to the awaited signRequest() call and then
immediately return execution to caller of sendMessage() before
signRequest() returns. If the caller then calls disconnect(), when
signRequest() returns, the socket would be undefined, even though
our test for undefined above would find it existing.
  • Loading branch information
hjoelr committed Jan 25, 2022
1 parent 49556c2 commit 2195b2f
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/client/WebSocketClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,6 @@ export class WebSocketClient extends EventEmitter {
}

async sendMessage(message: WebSocketRequest): Promise<void> {
if (!this.socket) {
throw new Error(`Failed to send message of type "${message.type}": You need to connect to the WebSocket first.`);
}

/**
* Authentication will result in a couple of benefits:
* 1. Messages where you're one of the parties are expanded and have more useful fields
Expand All @@ -498,6 +494,10 @@ export class WebSocketClient extends EventEmitter {
});
Object.assign(message, signature);

if (!this.socket) {
throw new Error(`Failed to send message of type "${message.type}": You need to connect to the WebSocket first.`);
}

this.socket.send(JSON.stringify(message));
}

Expand Down

0 comments on commit 2195b2f

Please sign in to comment.