Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors under NextJS #352

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading