From f2479945110658c12db30f2eb82ab8c71d5f1dc5 Mon Sep 17 00:00:00 2001 From: matannahmani <58164660+matannahmani@users.noreply.github.com> Date: Wed, 17 Jul 2024 23:08:48 +0900 Subject: [PATCH] Update AbstractLiveClient.ts to close #317 Mismatch client protocol when using Bun native Websockets. If bun env is detected we replace native websocket with "ws" --- src/packages/AbstractLiveClient.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/packages/AbstractLiveClient.ts b/src/packages/AbstractLiveClient.ts index b0cf78c..d972cfe 100644 --- a/src/packages/AbstractLiveClient.ts +++ b/src/packages/AbstractLiveClient.ts @@ -119,6 +119,23 @@ export abstract class AbstractLiveClient extends AbstractClient { return; } + /** + * @summary Bun websocket transport has a bug where it's native WebSocket implementation messes up the headers + * @summary This is a workaround to use the WS package for the websocket connection instead of the native Bun WebSocket + * @summary you can track the issue here + * @link https://github.com/oven-sh/bun/issues/4529 + */ + if (process?.versions?.bun) { + import("ws").then(({ default: WS }) => { + this.conn = new WS(requestUrl, { + headers: this.headers, + }); + console.log(`Using WS package`); + this.setupConnection(); + }); + return; + } + /** * Native websocket transport (browser) */