UNPKG

@juzi/wechaty

Version:

Wechaty is a RPA SDK for Chatbot Makers.

61 lines 2.45 kB
/// <reference path="json-rpc-peer.d.ts"/> import { Peer, MethodNotFound } from 'json-rpc-peer'; // // https://stackoverflow.com/a/50375286/1123955 // type UnionToIntersection<U> = // (U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never // type UnknownJsonRpcPayload = Partial<UnionToIntersection<JsonRpcPayload>> const isJsonRpcRequest = (payload) => ('method' in payload); const isJsonRpcNotification = (payload) => isJsonRpcRequest(payload) && (!('id' in payload)); const isJsonRpcResponse = (payload) => ('result' in payload); const isJsonRpcError = (payload) => ('error' in payload); const getPeer = (options) => { const getServiceGrpcPort = () => options.serviceGrpcPort; const serviceImpl = { /** * Huan(202101) Need to be fixed by new IO Bus system. * See: https://github.com/wechaty/wechaty-puppet-service/issues/118 */ getHostieGrpcPort: getServiceGrpcPort, }; const onMessage = async (message) => { if (isJsonRpcRequest(message)) { const { // id, method, // params, } = message; if (!(method in serviceImpl)) { console.error('serviceImpl does not contain method: ' + method); return; } const serviceMethodName = method; switch (serviceMethodName) { /** * Huan(202101) Need to be fixed by new IO Bus system. * See: https://github.com/wechaty/wechaty-puppet-service/issues/118 */ case 'getHostieGrpcPort': return serviceImpl[serviceMethodName](); default: throw new MethodNotFound(serviceMethodName); } } else if (isJsonRpcResponse(message)) { // NOOP: we are server } else if (isJsonRpcNotification(message)) { // NOOP: we are server } else if (isJsonRpcError(message)) { // NOOP: we are server } else { throw new Error('unknown json-rpc message: ' + JSON.stringify(message)); } console.info(JSON.stringify(message)); }; const ioPeer = new Peer(onMessage); return ioPeer; }; export { getPeer, isJsonRpcError, isJsonRpcNotification, isJsonRpcRequest, isJsonRpcResponse, }; //# sourceMappingURL=io-peer.js.map