-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
io-peer.ts
79 lines (65 loc) · 2.58 KB
/
io-peer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/// <reference path="json-rpc-peer.d.ts"/>
import jsonRpcPeer 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: jsonRpcPeer.JsonRpcPayload): payload is jsonRpcPeer.JsonRpcPayloadRequest => ('method' in payload)
const isJsonRpcNotification = (payload: jsonRpcPeer.JsonRpcPayload): payload is jsonRpcPeer.JsonRpcPayloadNotification => isJsonRpcRequest(payload) && (!('id' in payload))
const isJsonRpcResponse = (payload: jsonRpcPeer.JsonRpcPayload): payload is jsonRpcPeer.JsonRpcPayloadResponse => ('result' in payload)
const isJsonRpcError = (payload: jsonRpcPeer.JsonRpcPayload): payload is jsonRpcPeer.JsonRpcPayloadError => ('error' in payload)
interface IoPeerOptions {
serviceGrpcPort: number,
}
const getPeer = (options: IoPeerOptions) => {
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: jsonRpcPeer.JsonRpcPayload): Promise<any> => {
if (isJsonRpcRequest(message)) {
const {
// id,
method,
// params,
} = message
if (!(method in serviceImpl)) {
console.error('serviceImpl does not contain method: ' + method)
return
}
const serviceMethodName = method as keyof typeof serviceImpl
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 jsonRpcPeer.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 jsonRpcPeer.Peer(onMessage)
return ioPeer
}
export {
getPeer,
isJsonRpcError,
isJsonRpcNotification,
isJsonRpcRequest,
isJsonRpcResponse,
}