UNPKG

@juzi/wechaty

Version:

Wechaty is a RPA SDK for Chatbot Makers.

68 lines 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isJsonRpcResponse = exports.isJsonRpcRequest = exports.isJsonRpcNotification = exports.isJsonRpcError = exports.getPeer = void 0; /// <reference path="json-rpc-peer.d.ts"/> const json_rpc_peer_1 = require("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); exports.isJsonRpcRequest = isJsonRpcRequest; const isJsonRpcNotification = (payload) => isJsonRpcRequest(payload) && (!('id' in payload)); exports.isJsonRpcNotification = isJsonRpcNotification; const isJsonRpcResponse = (payload) => ('result' in payload); exports.isJsonRpcResponse = isJsonRpcResponse; const isJsonRpcError = (payload) => ('error' in payload); exports.isJsonRpcError = isJsonRpcError; 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 json_rpc_peer_1.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 json_rpc_peer_1.Peer(onMessage); return ioPeer; }; exports.getPeer = getPeer; //# sourceMappingURL=io-peer.js.map