import ipc from 'node-ipc' ; import log , { captureException } from './log' ; const handlers = require ( './handlers' ) ; export default ( socketId ) => { ipc . config . id = socketId ; ipc . config . silent = true ; ipc . serve ( ( ) => { ipc . server . on ( 'message' , ( data , socket ) => { const msg = JSON . parse ( data ) ; const { id , name , payload } = msg ; if ( handlers [ name ] ) { handlers [ name ] ( payload ) . then ( ( result ) => { ipc . server . emit ( socket , 'message' , JSON . stringify ( { type : 'reply' , id , result } ) ) ; return null ; } ) . catch ( ( error ) => { ipc . server . emit ( socket , 'message' , JSON . stringify ( { type : 'error' , id } ) ) ; captureException ( error , { ipc : `handler ${ name }` } ) ; } ) ; } else { captureException ( new Error ( `handler unknown method: ${ name }` ) ) ; ipc . server . emit ( socket , 'message' , JSON . stringify ( { type : 'reply' , id , result : null } ) ) ; } } ) ; } ) ; ipc . server . start ( ) ; } ;