File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ import { SpeechCreateParams } from 'openai/resources/audio/speech';
1717import { Stream } from '../streaming' ;
1818import { AUDIO_FILE_DURATION_HEADER } from '../constants' ;
1919import getAudioFileDuration from '../getAudioDuration' ;
20- import { isRunningInBrowser } from '../core' ;
20+ import { isFsModuleAvailable } from '../core' ;
2121
2222export 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 = {
Original file line number Diff line number Diff 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+ } ;
Original file line number Diff line number Diff line change 1- import { isRunningInBrowser } from './core' ;
1+ import { isFsModuleAvailable } from './core' ;
22
3- const isBrowser = isRunningInBrowser ( ) ;
3+ const fsAvailable = isFsModuleAvailable ( ) ;
44
55let fs : any ;
66let open : any ;
77let read : any ;
88let stat : any ;
99let 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 */
2930async 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
You can’t perform that action at this time.
0 commit comments