robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
0abc9ccc19
master
1// msgpack-rpc message shapes: 2// request: [0, msgid, method, params] 3// response: [1, msgid, error, result] 4// notification: [2, method, params] 5 6const Protocol = { 7encodeRequest ( msgid , method , params ) { 8return MsgpackCodec . encode ([ 0 , msgid , method , params ]); 9}, 10 11encodeNotification ( method , params ) { 12return MsgpackCodec . encode ([ 2 , method , params ]); 13}, 14 15// classifies a decoded msgpack-rpc array into a tagged object. 16// throws if msg doesn't look like a valid rpc message. 17parseMessage ( msg ) { 18if ( ! Array . isArray ( msg )) throw new Error ( "rpc message is not an array" ); 19const [ type , ...rest ] = msg ; 20if ( type === 0 ) { 21const [ msgid , method , params ] = rest ; 22return { kind :"request" , msgid, method, params}; 23} 24if ( type === 1 ) { 25const [ msgid , error , result ] = rest ; 26return { kind :"response" , msgid, error, result}; 27} 28if ( type === 2 ) { 29const [ method , params ] = rest ; 30return { kind :"notification" , method, params}; 31} 32throw new Error ( "unknown rpc message type: " + type ); 33}, 34};