import type * as d from '../declarations' ; import { fork } from 'child_process' ; import path from 'path' ; export function initServerProcessWorkerProxy ( sendToMain ) { const workerPath = require . resolve ( path . join ( __dirname , 'server-worker-thread.js' ) ) ; const filteredExecArgs = process . execArgv . filter ( ( v ) => ! /^--(debug|inspect)/ . test ( v ) ) ; const forkOpts = { execArgv : filteredExecArgs , env : process . env , cwd : process . cwd ( ) , stdio : [ 'pipe' , 'pipe' , 'pipe' , 'ipc' ] , } ; let serverProcess = fork ( workerPath , [ ] , forkOpts ) ; const receiveFromMain = ( msg ) => { if ( serverProcess ) { serverProcess . send ( msg ) ; } else if ( msg . closeServer ) { sendToMain ( { serverClosed : true } ) ; } } ; serverProcess . on ( 'message' , ( msg ) => { if ( msg . serverClosed && serverProcess ) { serverProcess . kill ( 'SIGINT' ) ; serverProcess = null ; } sendToMain ( msg ) ; } ) ; serverProcess . stdout . on ( 'data' , ( data ) => { console . log ( `dev server: ${ data }` ) ; } ) ; serverProcess . stderr . on ( 'data' , ( data ) => { sendToMain ( { error : { message : 'stderr: ' + data } } ) ; } ) ; return receiveFromMain ; }