UNPKG

3.64 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.isJsonRpcResponse = exports.isJsonRpcRequest = exports.isJsonRpcNotification = exports.isJsonRpcError = exports.getPeer = void 0;
23const json_rpc_peer_1 = __importStar(require("json-rpc-peer"));
24// // https://stackoverflow.com/a/50375286/1123955
25// type UnionToIntersection<U> =
26// (U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never
27// type UnknownJsonRpcPayload = Partial<UnionToIntersection<JsonRpcPayload>>
28const isJsonRpcRequest = (payload) => ('method' in payload);
29exports.isJsonRpcRequest = isJsonRpcRequest;
30const isJsonRpcNotification = (payload) => isJsonRpcRequest(payload) && (!('id' in payload));
31exports.isJsonRpcNotification = isJsonRpcNotification;
32const isJsonRpcResponse = (payload) => ('result' in payload);
33exports.isJsonRpcResponse = isJsonRpcResponse;
34const isJsonRpcError = (payload) => ('error' in payload);
35exports.isJsonRpcError = isJsonRpcError;
36const getPeer = (options) => {
37 const getServiceGrpcPort = () => options.serviceGrpcPort;
38 const serviceImpl = {
39 /**
40 * Huan(202101) Need to be fixed by new IO Bus system.
41 * See: https://github.com/wechaty/wechaty-puppet-service/issues/118
42 */
43 getHostieGrpcPort: getServiceGrpcPort,
44 };
45 const onMessage = async (message) => {
46 if (isJsonRpcRequest(message)) {
47 const {
48 // id,
49 method,
50 // params,
51 } = message;
52 if (!(method in serviceImpl)) {
53 console.error('serviceImpl does not contain method: ' + method);
54 return;
55 }
56 const serviceMethodName = method;
57 switch (serviceMethodName) {
58 /**
59 * Huan(202101) Need to be fixed by new IO Bus system.
60 * See: https://github.com/wechaty/wechaty-puppet-service/issues/118
61 */
62 case 'getHostieGrpcPort':
63 return serviceImpl[serviceMethodName]();
64 default:
65 throw new json_rpc_peer_1.MethodNotFound(serviceMethodName);
66 }
67 }
68 else if (isJsonRpcResponse(message)) {
69 // NOOP: we are server
70 }
71 else if (isJsonRpcNotification(message)) {
72 // NOOP: we are server
73 }
74 else if (isJsonRpcError(message)) {
75 // NOOP: we are server
76 }
77 else {
78 throw new Error('unknown json-rpc message: ' + JSON.stringify(message));
79 }
80 console.info(JSON.stringify(message));
81 };
82 const ioPeer = new json_rpc_peer_1.default(onMessage);
83 return ioPeer;
84};
85exports.getPeer = getPeer;
86//# sourceMappingURL=io-peer.js.map
\No newline at end of file