From f64f76ca84f0815afab569b445d74cf4db8385e4 Mon Sep 17 00:00:00 2001 From: Luke Oliff Date: Fri, 17 Nov 2023 10:36:57 +0000 Subject: [PATCH] fix: fixes issues 197 and 198 (#202) * fix: closing the socket too early (#201) * fix: a wild fix for this has appeared (#200) --- src/lib/helpers.ts | 1 + src/packages/LiveClient.ts | 17 ++++------------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index 7e8e65ee..de979566 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -8,6 +8,7 @@ export function stripTrailingSlash(url: string): string { } export const isBrowser = () => typeof window !== "undefined"; +export const isServer = () => typeof process !== "undefined"; export function applySettingDefaults( options: DeepgramClientOptions, diff --git a/src/packages/LiveClient.ts b/src/packages/LiveClient.ts index deaf5b3c..cda1d241 100644 --- a/src/packages/LiveClient.ts +++ b/src/packages/LiveClient.ts @@ -1,5 +1,5 @@ import { AbstractWsClient } from "./AbstractWsClient"; -import { appendSearchParams, isBrowser } from "../lib/helpers"; +import { appendSearchParams, isServer } from "../lib/helpers"; import { DeepgramError } from "../lib/errors"; import { DEFAULT_OPTIONS } from "../lib/constants"; import { LiveConnectionState, LiveTranscriptionEvents } from "../lib/enums"; @@ -28,19 +28,15 @@ export class LiveClient extends AbstractWsClient { url.protocol = url.protocol.toLowerCase().replace(/(http)(s)?/gi, "ws$2"); appendSearchParams(url.searchParams, this.transcriptionOptions); - try { + if (isServer()) { this._socket = new WebSocket(url.toString(), { headers: { Authorization: `token ${this.key}`, ...this.options?.global?.headers, }, }); - } catch (e) { - if (e instanceof SyntaxError) { - this._socket = new WebSocket(url.toString(), ["token", this.key]); - } else { - throw e; // let others bubble up - } + } else { + this._socket = new WebSocket(url.toString(), ["token", this.key]); } this._socket.onopen = () => { @@ -150,10 +146,5 @@ export class LiveClient extends AbstractWsClient { type: "CloseStream", }) ); - - // close the socket from the client end - if (this._socket.readyState === LiveConnectionState.OPEN) { - this._socket.close(); - } } }