import { ProjectInfoCmds } from "./types" ; import { ProjectInfoServer } from "./server" ; import { exec } from "./utils" ; let _info = undefined ; export function get_ProjectInfoServer ( ) { if ( _info != null ) return _info ; _info = new ProjectInfoServer ( ) ; return _info ; } export async function project_info_ws ( primus , logger ) { const L = ( ... msg ) => logger . debug ( "project_info:" , ... msg ) ; const name = `project_info` ; const channel = primus . channel ( name ) ; function deregister ( spark ) { L ( `deregistering ${ spark . id }` ) ; } channel . on ( "connection" , function ( spark ) : void { L ( `channel: new connection from ${ spark . address . ip } -- ${ spark . id }` ) ; function close ( type ) { L ( `event ${ type }: deregistering` ) ; deregister ( spark ) ; } spark . on ( "close" , ( ) => close ( "close" ) ) ; spark . on ( "end" , ( ) => close ( "end" ) ) ; spark . on ( "data" , function ( data ) { if ( typeof data === "object" ) { switch ( data . cmd ) { case "signal" : L ( `Signal ${ data . signal } from ${ spark . id } for pids: ${ data . pids }` ) ; exec ( `kill -s ${ data . signal ?? 15 } ${ data . pids . join ( " " ) }` ) ; break ; default : throw Error ( "WARNING: unknown command -- " + data . cmd ) ; } } } ) ; } ) ; channel . on ( "disconnection" , function ( spark ) : void { L ( `channel: disconnection from ${ spark . address . ip } -- ${ spark . id }` ) ; deregister ( spark ) ; } ) ; return name ; }