@@ -85,28 +85,67 @@ process.on("SIGINT", () => cleanExit());
8585process . on ( "SIGTERM" , ( ) => cleanExit ( ) ) ;
8686
8787const handleSpawn = ( debug : boolean , fileName : string , port : number , filePath ?: string ) => {
88- const execPath = filePath ? `"${ filePath } "` : `"${ path . join ( __dirname , fileName ) } "` ;
89- const shell = process . platform === "win32" ? false : true ;
90- child = spawn ( execPath , {
88+ try {
89+ // Determine the executable path
90+ let execPath : string ;
91+
92+ if ( filePath ) {
93+ // If filePath is provided, use it directly
94+ execPath = filePath ;
95+ } else {
96+ // Otherwise, construct path relative to __dirname
97+ execPath = path . join ( __dirname , fileName ) ;
98+ }
99+
100+ // Remove quotes as they're not needed and can cause issues on Windows
101+ execPath = execPath . replace ( / " / g, '' ) ;
102+
103+ // Verify file exists before attempting to spawn
104+ if ( ! require ( 'fs' ) . existsSync ( execPath ) ) {
105+ throw new Error ( `Executable not found at path: ${ execPath } ` ) ;
106+ }
107+
108+ const spawnOptions = {
91109 env : { WS_PORT : port . toString ( ) } ,
92- shell : shell ,
110+ shell : process . platform !== "win32" , // false for Windows, true for others
93111 windowsHide : true ,
94- detached : process . platform !== "win32"
95- } ) ;
112+ detached : process . platform !== "win32" ,
113+ // Add cwd option to ensure proper working directory
114+ cwd : path . dirname ( execPath )
115+ } ;
116+ child = spawn ( execPath , [ ] , spawnOptions ) ;
96117 child . stderr . on ( "data" , ( stderr ) => {
97- if ( stderr . toString ( ) . includes ( "Request_Id_On_The_Left" ) ) {
98- const splitRequestIdAndError = stderr . toString ( ) . split ( "Request_Id_On_The_Left" ) ;
99- const [ requestId , error ] = splitRequestIdAndError ;
100- //TODO Correctly parse logging messages
101- // this.emit(requestId, { error: new Error(error) });
118+ const errorMessage = stderr . toString ( ) ;
119+ if ( errorMessage . includes ( "Request_Id_On_The_Left" ) ) {
120+ const [ requestId , error ] = errorMessage . split ( "Request_Id_On_The_Left" ) ;
121+ // Handle request-specific error
102122 } else {
103- debug
104- ? cleanExit ( new Error ( stderr ) )
105- //TODO add Correct error logging url request/ response/
106- : cleanExit ( `Error Processing Request (please open an issue https://github.com/Danny-Dasilva/CycleTLS/issues/new/choose) -> ${ stderr } ` , false ) . then ( ( ) => handleSpawn ( debug , fileName , port ) ) ;
123+ if ( debug ) {
124+ cleanExit ( new Error ( errorMessage ) ) ;
125+ } else {
126+ cleanExit (
127+ `Error Processing Request (please open an issue https://github.com/Danny-Dasilva/CycleTLS/issues/new/choose) -> ${ errorMessage } ` ,
128+ false
129+ ) . then ( ( ) => handleSpawn ( debug , fileName , port ) ) ;
130+ }
107131 }
108132 } ) ;
133+
134+ // Add error handler for spawn errors
135+ child . on ( 'error' , ( error : any ) => {
136+ console . error ( `Failed to start subprocess: ${ error . message } ` ) ;
137+ if ( error . code === 'ENOENT' ) {
138+ console . error ( `Executable not found at: ${ execPath } ` ) ;
139+ console . error ( 'Please ensure the executable exists and has correct permissions' ) ;
140+ }
141+ throw error ;
142+ } ) ;
143+
144+ } catch ( error ) {
145+ console . error ( `Error in handleSpawn: ${ error . message } ` ) ;
146+ throw error ;
109147}
148+ } ;
110149
111150// Function to convert a stream into a string
112151async function streamToString ( stream : Readable ) : Promise < string > {
@@ -335,7 +374,7 @@ const initCycleTLS = async (
335374
336375 if ( ! port ) port = 9119 ;
337376 if ( ! debug ) debug = false ;
338- if ( ! timeout ) timeout = 4000 ;
377+ if ( ! timeout ) timeout = 20000 ;
339378
340379 const instance = new Golang ( port , debug , timeout , executablePath ) ;
341380 instance . on ( "ready" , ( ) => {
0 commit comments