Skip to content

Commit

Permalink
Merge branch 'main' into lo/get-models-api
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeocodes authored Aug 29, 2024
2 parents 2a205d7 + 984591d commit 6ef8246
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
23 changes: 19 additions & 4 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertProtocolToWs, isBrowser } from "./helpers";
import { convertProtocolToWs, isBrowser, isBun, isNode } from "./helpers";
import { version } from "./version";
import type { DefaultNamespaceOptions, DefaultClientOptions } from "./types";

Expand All @@ -7,17 +7,32 @@ export const NODE_VERSION =
? process.versions.node
: "unknown";

export const BUN_VERSION =
typeof process !== "undefined" && process.versions && process.versions.bun
? process.versions.bun
: "unknown";

export const BROWSER_AGENT =
typeof window !== "undefined" && window.navigator && window.navigator.userAgent
? window.navigator.userAgent
: "unknown";

const getAgent = () => {
if (isNode()) {
return `node/${NODE_VERSION}`;
} else if (isBun()) {
return `bun/${BUN_VERSION}`;
} else if (isBrowser()) {
return `javascript ${BROWSER_AGENT}`;
} else {
return `unknown`;
}
};

export const DEFAULT_HEADERS = {
"Content-Type": `application/json`,
"X-Client-Info": `@deepgram/sdk; ${isBrowser() ? "browser" : "server"}; v${version}`,
"User-Agent": `@deepgram/sdk/${version} ${
isBrowser() ? `javascript ${BROWSER_AGENT}` : `node/${NODE_VERSION}`
}`,
"User-Agent": `@deepgram/sdk/${version} ${getAgent()}`,
};

export const DEFAULT_URL = "https://api.deepgram.com";
Expand Down
9 changes: 6 additions & 3 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ import {
import { Headers as CrossFetchHeaders } from "cross-fetch";
import { Readable } from "stream";
import merge from "deepmerge";
import { BROWSER_AGENT, BUN_VERSION, NODE_VERSION } from "./constants";

export function stripTrailingSlash(url: string): string {
return url.replace(/\/$/, "");
}

export function isBrowser() {
return typeof window !== "undefined" && typeof window.document !== "undefined";
}
export const isBrowser = () => BROWSER_AGENT !== "unknown";

export const isNode = () => NODE_VERSION !== "unknown";

export const isBun = () => BUN_VERSION !== "unknown";

export function applyDefaults<O, S>(options: Partial<O> = {}, subordinate: Partial<S> = {}): S {
return merge(subordinate, options);
Expand Down
5 changes: 3 additions & 2 deletions src/packages/AbstractLiveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AbstractClient, noop } from "./AbstractClient";
import { CONNECTION_STATE, SOCKET_STATES } from "../lib/constants";
import type { DeepgramClientOptions, LiveSchema } from "../lib/types";
import type { WebSocket as WSWebSocket } from "ws";
import { isBun } from "../lib/helpers";

/**
* Represents a constructor for a WebSocket-like object that can be used in the application.
Expand Down Expand Up @@ -125,7 +126,7 @@ export abstract class AbstractLiveClient extends AbstractClient {
* @summary you can track the issue here
* @link https://github.com/oven-sh/bun/issues/4529
*/
if (process?.versions?.bun) {
if (isBun()) {
import("ws").then(({ default: WS }) => {
this.conn = new WS(requestUrl, {
headers: this.headers,
Expand All @@ -135,7 +136,7 @@ export abstract class AbstractLiveClient extends AbstractClient {
});
return;
}

/**
* Native websocket transport (browser)
*/
Expand Down

0 comments on commit 6ef8246

Please sign in to comment.