Skip to content

Commit

Permalink
fix: fixes issues 197 and 198 (#202)
Browse files Browse the repository at this point in the history
* fix: closing the socket too early (#201)

* fix: a wild fix for this has appeared (#200)
  • Loading branch information
lukeocodes authored Nov 17, 2023
1 parent 0dce6e5 commit f64f76c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 4 additions & 13 deletions src/packages/LiveClient.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit f64f76c

Please sign in to comment.