|
@@ -0,0 +1,256 @@
|
|
1
|
+import http = require('http');
|
|
2
|
+import bsock = require('bsock');
|
|
3
|
+
|
|
4
|
+import * as uuid from "uuid/v4"
|
|
5
|
+import { Socket } from "./RPCSocketServer"
|
|
6
|
+
|
|
7
|
+type rpcType = 'hook' | 'unhook' | 'call'
|
|
8
|
+export type Outcome = "Success" | "Error"
|
|
9
|
+export type Visibility = "127.0.0.1" | "0.0.0.0"
|
|
10
|
+
|
|
11
|
+/* Responses */
|
|
12
|
+export class Response{
|
|
13
|
+ constructor(
|
|
14
|
+ public message?:string
|
|
15
|
+ ){}
|
|
16
|
+}
|
|
17
|
+
|
|
18
|
+export class SuccessResponse extends Response{
|
|
19
|
+ result:Outcome = "Success"
|
|
20
|
+
|
|
21
|
+ constructor(
|
|
22
|
+ message?:string
|
|
23
|
+ ){
|
|
24
|
+ super(message)
|
|
25
|
+ }
|
|
26
|
+}
|
|
27
|
+
|
|
28
|
+export class ErrorResponse extends Response{
|
|
29
|
+ result:Outcome = "Error"
|
|
30
|
+
|
|
31
|
+ constructor(
|
|
32
|
+ message: string = "Unknown error"
|
|
33
|
+ ){
|
|
34
|
+ super(message)
|
|
35
|
+ }
|
|
36
|
+}
|
|
37
|
+
|
|
38
|
+export class SubscriptionResponse extends SuccessResponse{
|
|
39
|
+ constructor(
|
|
40
|
+ public uid: string,
|
|
41
|
+ message?:string
|
|
42
|
+ ){
|
|
43
|
+ super(message)
|
|
44
|
+ }
|
|
45
|
+}
|
|
46
|
+export type UnhookFunction = (uid:string) => Promise<SuccessResponse | ErrorResponse>
|
|
47
|
+export type callbackFunction = (...args) => Promise<SubscriptionResponse | ErrorResponse>
|
|
48
|
+export type AsyncFunction = (...args) => Promise<any>
|
|
49
|
+
|
|
50
|
+export interface RPCExporter{
|
|
51
|
+ name: string
|
|
52
|
+ exportRPCs() : socketioRPC[]
|
|
53
|
+ exportPublicRPCs() : socketioRPC[]
|
|
54
|
+}
|
|
55
|
+
|
|
56
|
+type baseRPC = {
|
|
57
|
+ type: rpcType
|
|
58
|
+ name: string
|
|
59
|
+}
|
|
60
|
+
|
|
61
|
+type hookRPC = baseRPC & {
|
|
62
|
+ type: 'hook'
|
|
63
|
+ func: callbackFunction
|
|
64
|
+ unhook: UnhookFunction
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+type unhookRPC = baseRPC & {
|
|
68
|
+ type: 'unhook'
|
|
69
|
+ func: UnhookFunction
|
|
70
|
+}
|
|
71
|
+
|
|
72
|
+type callRPC = baseRPC & {
|
|
73
|
+ type: 'call'
|
|
74
|
+ func: (...args) => Promise<any>
|
|
75
|
+}
|
|
76
|
+
|
|
77
|
+export type socketioRPC = callRPC | unhookRPC | hookRPC
|
|
78
|
+
|
|
79
|
+export type baseInfo = {
|
|
80
|
+ owner: string,
|
|
81
|
+ argNames: string[],
|
|
82
|
+}
|
|
83
|
+
|
|
84
|
+type HookInfo = baseRPC & baseInfo & {
|
|
85
|
+ type: 'hook',
|
|
86
|
+ generator: (socket) => callbackFunction
|
|
87
|
+ unhook: UnhookFunction
|
|
88
|
+}
|
|
89
|
+
|
|
90
|
+type UnhookInfo = baseRPC & baseInfo & {
|
|
91
|
+ type: 'unhook',
|
|
92
|
+ func: UnhookFunction
|
|
93
|
+}
|
|
94
|
+
|
|
95
|
+type CallInfo = baseRPC & baseInfo & {
|
|
96
|
+ type: 'call',
|
|
97
|
+ func: AsyncFunction
|
|
98
|
+}
|
|
99
|
+
|
|
100
|
+type RpcInfo = HookInfo | UnhookInfo | CallInfo
|
|
101
|
+
|
|
102
|
+export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
|
|
103
|
+
|
|
104
|
+export const rpcToRpcinfo = (rpc : socketioRPC, owner: string):RpcInfo => {
|
|
105
|
+ switch(rpc.type){
|
|
106
|
+ case "call" :
|
|
107
|
+ return {
|
|
108
|
+ owner: owner,
|
|
109
|
+ argNames: extractArgs(rpc.func),
|
|
110
|
+ type: rpc.type,
|
|
111
|
+ name: rpc.name,
|
|
112
|
+ func: rpc.func,
|
|
113
|
+ }
|
|
114
|
+ case "unhook" :
|
|
115
|
+ return {
|
|
116
|
+ owner: owner,
|
|
117
|
+ argNames: extractArgs(rpc.func),
|
|
118
|
+ type: rpc.type,
|
|
119
|
+ name: rpc.name,
|
|
120
|
+ func: rpc.func,
|
|
121
|
+ }
|
|
122
|
+ case "hook" :
|
|
123
|
+ const generator = hookGenerator(rpc)
|
|
124
|
+ return {
|
|
125
|
+ owner: owner,
|
|
126
|
+ argNames: extractArgs(generator(undefined)),
|
|
127
|
+ type: rpc.type,
|
|
128
|
+ name: rpc.name,
|
|
129
|
+ unhook: rpc.unhook,
|
|
130
|
+ generator: generator,
|
|
131
|
+ }
|
|
132
|
+ }
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+function rpcHooker(socket: Socket, exporter:RPCExporter, makeUnique = true):ExtendedRpcInfo[]{
|
|
137
|
+ const owner = exporter.name
|
|
138
|
+ const RPCs = [...exporter.exportPublicRPCs(), ...exporter.exportRPCs()]
|
|
139
|
+ const suffix = makeUnique?"-"+uuid().substr(0,4):""
|
|
140
|
+ return RPCs.map(rpc => rpcToRpcinfo(rpc, owner))
|
|
141
|
+ .map(info => {
|
|
142
|
+ const ret:any = info
|
|
143
|
+ ret.uniqueName = info.name+suffix
|
|
144
|
+
|
|
145
|
+ switch(info.type){
|
|
146
|
+ case "hook":
|
|
147
|
+ socket.hook(ret.uniqueName, info.generator(socket))
|
|
148
|
+ break;
|
|
149
|
+ default:
|
|
150
|
+ socket.hook(ret.uniqueName, info.func)
|
|
151
|
+ }
|
|
152
|
+ socket.on('close', () => socket.unhook(info.name))
|
|
153
|
+ return ret
|
|
154
|
+ })
|
|
155
|
+}
|
|
156
|
+
|
|
157
|
+const hookGenerator = (rpc:hookRPC): HookInfo['generator'] => {
|
|
158
|
+ const argsArr = extractArgs(rpc.func)
|
|
159
|
+ argsArr.pop()
|
|
160
|
+ const args = argsArr.join(',')
|
|
161
|
+
|
|
162
|
+ return eval(`(socket) => async (`+args+`) => {
|
|
163
|
+ const res = await rpc.func(`+args+(args.length!==0?',':'')+` (x) => {
|
|
164
|
+ socket.call(res.uid, x)
|
|
165
|
+ })
|
|
166
|
+ if(res.result == 'Success'){
|
|
167
|
+ socket.on('close', async () => {
|
|
168
|
+ const unhookRes = await rpc.unhook(res.uid)
|
|
169
|
+ console.log("Specific close handler for", rpc.name, res.uid, unhookRes)
|
|
170
|
+ })
|
|
171
|
+
|
|
172
|
+ }
|
|
173
|
+ return res
|
|
174
|
+ }`)
|
|
175
|
+}
|
|
176
|
+
|
|
177
|
+const extractArgs = (f:Function):string[] => {
|
|
178
|
+ let fn = String(f)
|
|
179
|
+ let args = fn.substr(0, fn.indexOf(")"))
|
|
180
|
+ args = args.substr(fn.indexOf("(")+1)
|
|
181
|
+ let ret = args.split(",")
|
|
182
|
+ return ret
|
|
183
|
+}
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => Socket
|
|
187
|
+
|
|
188
|
+export interface Socket {
|
|
189
|
+ port: number
|
|
190
|
+ hook: (rpcname: string, ...args: any[]) => Socket
|
|
191
|
+ unhook: (rpcname:string) => Socket
|
|
192
|
+ call: (rpcname:string, ...args: any[]) => Promise<any>
|
|
193
|
+ fire: (rpcname:string, ...args: any[]) => Promise<any>
|
|
194
|
+ on: OnFunction
|
|
195
|
+ destroy: ()=>void
|
|
196
|
+ close: ()=>void
|
|
197
|
+}
|
|
198
|
+
|
|
199
|
+export type RPCSocketConf = {
|
|
200
|
+ connectionHandler: (socket:Socket) => void
|
|
201
|
+ errorHandler: (socket:Socket) => (error:any) => void
|
|
202
|
+ closeHandler: (socket:Socket) => () => void
|
|
203
|
+}
|
|
204
|
+
|
|
205
|
+export class RPCSocketServer{
|
|
206
|
+
|
|
207
|
+ private io = bsock.createServer()
|
|
208
|
+ private wsServer = http.createServer()
|
|
209
|
+
|
|
210
|
+ constructor(
|
|
211
|
+ private port:number,
|
|
212
|
+ private rpcExporters: RPCExporter[] = [],
|
|
213
|
+ private visibility: Visibility = "127.0.0.1",
|
|
214
|
+ private conf: RPCSocketConf = {
|
|
215
|
+ errorHandler: (socket:Socket) => (error:any) => { socket.destroy(); console.error(error) },
|
|
216
|
+ closeHandler: (socket:Socket) => () => { console.log("Socket closing") },
|
|
217
|
+ connectionHandler: (socket:Socket) => { console.log("New websocket connection in port "+socket.port) }
|
|
218
|
+ }
|
|
219
|
+ ){
|
|
220
|
+ this.startWebsocket()
|
|
221
|
+ }
|
|
222
|
+
|
|
223
|
+ private startWebsocket(){
|
|
224
|
+ try{
|
|
225
|
+ this.io.attach(this.wsServer)
|
|
226
|
+ this.io.on('socket', (socket:Socket) => {
|
|
227
|
+ socket.on('error', this.conf.errorHandler(socket))
|
|
228
|
+ socket.on('close', this.conf.closeHandler(socket))
|
|
229
|
+ if(this.visibility === "127.0.0.1")
|
|
230
|
+ this.initRPCs(socket)
|
|
231
|
+ else
|
|
232
|
+ this.initPublicRPCs(socket)
|
|
233
|
+ })
|
|
234
|
+ this.wsServer.listen(this.port, this.visibility)
|
|
235
|
+ }catch(e){
|
|
236
|
+ //@ts-ignore
|
|
237
|
+ this.errorHandler(undefined)("Unable to connect to socket")
|
|
238
|
+ }
|
|
239
|
+ }
|
|
240
|
+
|
|
241
|
+ protected initRPCs(socket:Socket){
|
|
242
|
+ socket.hook('info', () => rpcInfos)
|
|
243
|
+
|
|
244
|
+ const rpcInfos:ExtendedRpcInfo[] = [
|
|
245
|
+ ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter))
|
|
246
|
+ ]
|
|
247
|
+ }
|
|
248
|
+
|
|
249
|
+ protected initPublicRPCs(socket:Socket){
|
|
250
|
+ socket.hook('info', () => rpcInfos)
|
|
251
|
+
|
|
252
|
+ const rpcInfos:ExtendedRpcInfo[] = [
|
|
253
|
+ ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter))
|
|
254
|
+ ]
|
|
255
|
+ }
|
|
256
|
+}
|