Skip to content

Commit

Permalink
fix: fixed circular dependency giving errors under webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolello-dev committed Jan 3, 2025
1 parent 18b4640 commit 5637d56
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
18 changes: 2 additions & 16 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import { convertProtocolToWs, isBrowser, isBun, isNode } from "./helpers";
import { convertProtocolToWs } from "./helpers";
import { isBrowser, isBun, isNode, NODE_VERSION, BUN_VERSION, BROWSER_AGENT } from "./runtime";
import { version } from "./version";
import type { DefaultNamespaceOptions, DefaultClientOptions } from "./types";

export const NODE_VERSION =
typeof process !== "undefined" && process.versions && process.versions.node
? 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}`;
Expand Down
7 changes: 0 additions & 7 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@ 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 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
20 changes: 20 additions & 0 deletions src/lib/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const NODE_VERSION =
typeof process !== "undefined" && process.versions && process.versions.node
? 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";

export const isBrowser = () => BROWSER_AGENT !== "unknown";

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

export const isBun = () => BUN_VERSION !== "unknown";
2 changes: 1 addition & 1 deletion src/packages/AbstractLiveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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";
import { isBun } from "../lib/runtime";

/**
* Represents a constructor for a WebSocket-like object that can be used in the application.
Expand Down
2 changes: 1 addition & 1 deletion src/packages/AbstractRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fetchWithAuth, resolveResponse } from "../lib/fetch";
import type { Fetch, FetchOptions, RequestMethodType } from "../lib/types/Fetch";
import { AbstractClient } from "./AbstractClient";
import { DeepgramClientOptions } from "../lib/types";
import { isBrowser } from "../lib/helpers";
import { isBrowser } from "../lib/runtime";
import merge from "deepmerge";

/**
Expand Down
4 changes: 2 additions & 2 deletions test/constants.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { applyDefaults, convertProtocolToWs, isBrowser } from "../src/lib/helpers";
import { applyDefaults, convertProtocolToWs } from "../src/lib/helpers";
import {
CONNECTION_STATE,
DEFAULT_GLOBAL_OPTIONS,
DEFAULT_HEADERS,
DEFAULT_OPTIONS,
DEFAULT_URL,
NODE_VERSION,
SOCKET_STATES,
} from "../src/lib/constants";
import { isBrowser, NODE_VERSION } from "../src/lib/runtime";
import { DeepgramClientOptions } from "../src/lib/types/DeepgramClientOptions";
import { expect } from "chai";
import { faker } from "@faker-js/faker";
Expand Down

0 comments on commit 5637d56

Please sign in to comment.