Skip to content

Commit 8bba7c1

Browse files
authored
Merge pull request #270 from Portkey-AI/feat/envCheck
FS Module Check
2 parents 483bc0a + 6dbc50c commit 8bba7c1

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

src/apis/audio.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { SpeechCreateParams } from 'openai/resources/audio/speech';
1717
import { Stream } from '../streaming';
1818
import { AUDIO_FILE_DURATION_HEADER } from '../constants';
1919
import getAudioFileDuration from '../getAudioDuration';
20-
import { isRunningInBrowser } from '../core';
20+
import { isFsModuleAvailable } from '../core';
2121

2222
export class Audio extends ApiResource {
2323
transcriptions: transcriptions;
@@ -78,7 +78,8 @@ export class transcriptions extends ApiResource {
7878
> {
7979
// @ts-ignore
8080
const path = body.file?.path;
81-
if (path && this.client.calculateAudioDuration && !isRunningInBrowser()) {
81+
82+
if (path && this.client.calculateAudioDuration && isFsModuleAvailable()) {
8283
const duration = await getAudioFileDuration(path);
8384
if (duration) {
8485
params = {
@@ -110,7 +111,8 @@ export class translations extends ApiResource {
110111
): Promise<any> {
111112
const body: any = _body;
112113
const path = body.file?.path;
113-
if (path && this.client.calculateAudioDuration && !isRunningInBrowser()) {
114+
115+
if (path && this.client.calculateAudioDuration && isFsModuleAvailable()) {
114116
const duration = await getAudioFileDuration(path);
115117
if (duration) {
116118
params = {

src/core.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,12 @@ export const isRunningInBrowser = () => {
5959
typeof navigator !== 'undefined'
6060
);
6161
};
62+
63+
export const isFsModuleAvailable = () => {
64+
try {
65+
require('fs');
66+
return true;
67+
} catch (e) {
68+
return false;
69+
}
70+
};

src/getAudioDuration.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { isRunningInBrowser } from './core';
1+
import { isFsModuleAvailable } from './core';
22

3-
const isBrowser = isRunningInBrowser();
3+
const fsAvailable = isFsModuleAvailable();
44

55
let fs: any;
66
let open: any;
77
let read: any;
88
let stat: any;
99
let close: any;
1010

11-
if (!isBrowser) {
11+
// Check if we're in Node.js AND fs module is available
12+
if (fsAvailable) {
1213
try {
1314
fs = require('fs');
1415
const { promisify } = require('util');
@@ -27,7 +28,8 @@ if (!isBrowser) {
2728
* Uses optimized file reading to avoid loading entire files into memory
2829
*/
2930
async function getAudioFileDuration(filePath: string): Promise<string | null> {
30-
if (isBrowser || !fs) {
31+
// Only proceed if we're in Node.js, fs is available, and fs module loaded successfully
32+
if (!fsAvailable || !fs) {
3133
return null;
3234
}
3335

0 commit comments

Comments
 (0)