Skip to content

Commit de726ef

Browse files
lukeocodesnaomi-lgbt
authored andcommitted
fix: use type-only imports for Node.js stream module
- Changed 'import { Readable } from "stream"' to 'import type { Readable } from "stream"' in: - src/packages/AbstractRestClient.ts - src/lib/types/DeepgramSource.ts - src/lib/helpers.ts - Updated isReadStreamSource to use duck typing instead of instanceof check - This prevents bundler errors in browser environments (React, Vite, etc) - Maintains full type safety while removing runtime dependency on Node.js stream module
1 parent ff02a1f commit de726ef

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/lib/helpers.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import {
99
TranscriptionSchema,
1010
} from "./types";
1111
import { Headers as CrossFetchHeaders } from "cross-fetch";
12-
import { Readable } from "stream";
12+
import type { Readable } from "stream";
1313
import merge from "deepmerge";
14+
import { isBrowser } from "./runtime";
1415

1516
export function stripTrailingSlash(url: string): string {
1617
return url.replace(/\/$/, "");
@@ -71,7 +72,18 @@ const isBufferSource = (providedSource: PrerecordedSource): providedSource is Bu
7172
};
7273

7374
const isReadStreamSource = (providedSource: PrerecordedSource): providedSource is Readable => {
74-
return providedSource != null && providedSource instanceof Readable;
75+
if (providedSource == null) return false;
76+
77+
// In browser environments, there's no Readable stream from Node.js
78+
if (isBrowser()) return false;
79+
80+
// Check for stream-like properties without importing Readable
81+
return (
82+
typeof providedSource === "object" &&
83+
typeof (providedSource as any).pipe === "function" &&
84+
typeof (providedSource as any).read === "function" &&
85+
typeof (providedSource as any)._readableState === "object"
86+
);
7587
};
7688

7789
export class CallbackUrl extends URL {

src/lib/types/DeepgramSource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Readable } from "stream";
1+
import type { Readable } from "stream";
22

33
export type PrerecordedSource = UrlSource | Buffer | Readable;
44

src/packages/AbstractRestClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DeepgramApiError, DeepgramError, DeepgramUnknownError } from "../lib/errors";
2-
import { Readable } from "stream";
2+
import type { Readable } from "stream";
33
import { fetchWithAuth, resolveResponse } from "../lib/fetch";
44
import type { Fetch, FetchOptions, RequestMethodType } from "../lib/types/Fetch";
55
import { AbstractClient } from "./AbstractClient";

0 commit comments

Comments
 (0)