Skip to content

Commit

Permalink
fix: resolve connect Promise on close before open (AnWeber/httpbook#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Sep 10, 2023
1 parent 868cd9e commit 2b42690
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [unreleased]

- resolve connect Promise in Websocket Requests on close before open Connection ()

## [6.6.5] (2023-09-06)

### Fixes
Expand Down
10 changes: 7 additions & 3 deletions src/plugins/websocket/websocketRequestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ export class WebsocketRequestClient extends models.AbstractRequestClient<WebSock
if (this.closeOnFinish) {
this.registerEvents(this._nativeClient);
await new Promise<void>(resolve => {
this._nativeClient?.on('open', () => {
const resolveListener = () => {
resolve();
});
this.nativeClient?.off('open', resolveListener);
this.nativeClient?.off('close', resolveListener);
};
this._nativeClient?.on('open', resolveListener);
this._nativeClient?.on('close', resolveListener);
});
}
}
Expand Down Expand Up @@ -134,7 +138,7 @@ export class WebsocketRequestClient extends models.AbstractRequestClient<WebSock
date: new Date(),
},
request: this.request,
body: utils.toString(reason),
body: utils.toString(reason) || 'close',
rawBody: Buffer.isBuffer(reason) ? reason : undefined,
});
this.removeWebsocketSession();
Expand Down

0 comments on commit 2b42690

Please sign in to comment.