Skip to content

Commit

Permalink
fix(websockets): issue with request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
awlayton committed May 31, 2022
1 parent 9ced175 commit 9739692
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions lib/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class WebSocketClient extends EventEmitter implements Connection {

/** Send a request to server */
async #doRequest(
{ headers: { authorization, ...headers }, ...request }: ConnectionRequest,
request: ConnectionRequest,
{ timeout, signal }: { timeout?: number; signal?: AbortSignal } = {}
): Promise<IConnectionResponse> {
const ws = await this.#ws;
Expand All @@ -196,20 +196,21 @@ export class WebSocketClient extends EventEmitter implements Connection {
request.requestId = requestId;
assertOADASocketRequest(request);

const { headers, watch, method } = request;

// Start listening for response before sending the request so we don't miss it
const responsePs = [once(this.#requests, `response:${requestId}`)];
const socketRequest: WebSocketRequest = {
...request,
headers: {
'user-agent': this.#userAgent,
'authorization': authorization!,
...headers,
},
method: request.watch
? request.method === 'head'
method: watch
? method === 'head'
? 'watch'
: `${request.method}-watch`
: request.method,
: `${method}-watch`
: method,
};
ws.send(JSON.stringify(socketRequest));
if (timeout) {
Expand All @@ -224,7 +225,7 @@ export class WebSocketClient extends EventEmitter implements Connection {
const [response] = await Promise.race(responsePs);

if (response.status >= 200 && response.status < 300) {
if (request.watch) {
if (watch) {
const changes = on(this.#requests, `change:${requestId}`, { signal });
return [response, changes];
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oada/client",
"version": "4.1.0",
"version": "4.1.1",
"description": "A lightweight client tool to interact with an OADA-compliant server",
"repository": "https://github.com/OADA/client",
"main": "dist/index.js",
Expand Down

0 comments on commit 9739692

Please sign in to comment.