Browse Source

hide info

master
peter 5 years ago
parent
commit
17dc58c5b3

+ 2
- 3
lib/src/backend/RPCSocketServer.d.ts View File

66
     uniqueName: string;
66
     uniqueName: string;
67
 };
67
 };
68
 export declare const rpcToRpcinfo: (rpc: socketioRPC, owner: string) => RpcInfo;
68
 export declare const rpcToRpcinfo: (rpc: socketioRPC, owner: string) => RpcInfo;
69
-export declare const rpcHooker: (socket: Socket, owner: string, RPCs: socketioRPC[], makeUnique?: boolean) => ExtendedRpcInfo[];
70
 declare type OnFunction = (type: 'error' | 'close', f: (e?: any) => void) => Socket;
69
 declare type OnFunction = (type: 'error' | 'close', f: (e?: any) => void) => Socket;
71
 export interface Socket {
70
 export interface Socket {
72
     port: number;
71
     port: number;
92
     private wsServer;
91
     private wsServer;
93
     constructor(port: number, rpcExporters?: RPCExporter[], visibility?: Visibility, conf?: RPCSocketConf);
92
     constructor(port: number, rpcExporters?: RPCExporter[], visibility?: Visibility, conf?: RPCSocketConf);
94
     private startWebsocket;
93
     private startWebsocket;
95
-    protected initApis(socket: Socket): void;
96
-    protected initPublicApis(socket: Socket): void;
94
+    protected initRPCs(socket: Socket): void;
95
+    protected initPublicRPCs(socket: Socket): void;
97
 }
96
 }
98
 export {};
97
 export {};

+ 12
- 24
lib/src/backend/RPCSocketServer.js View File

61
             };
61
             };
62
     }
62
     }
63
 };
63
 };
64
-exports.rpcHooker = (socket, owner, RPCs, makeUnique = true) => {
64
+function rpcHooker(socket, exporter, makeUnique = true) {
65
+    const owner = exporter.name;
66
+    const RPCs = [...exporter.exportPublicRPCs(), ...exporter.exportRPCs()];
65
     const suffix = makeUnique ? "-" + uuid().substr(0, 4) : "";
67
     const suffix = makeUnique ? "-" + uuid().substr(0, 4) : "";
66
     return RPCs.map(rpc => exports.rpcToRpcinfo(rpc, owner))
68
     return RPCs.map(rpc => exports.rpcToRpcinfo(rpc, owner))
67
         .map(info => {
69
         .map(info => {
77
         socket.on('close', () => socket.unhook(info.name));
79
         socket.on('close', () => socket.unhook(info.name));
78
         return ret;
80
         return ret;
79
     });
81
     });
80
-};
82
+}
81
 const hookGenerator = (rpc) => {
83
 const hookGenerator = (rpc) => {
82
     const argsArr = extractArgs(rpc.func);
84
     const argsArr = extractArgs(rpc.func);
83
     argsArr.pop();
85
     argsArr.pop();
124
                 socket.on('error', this.conf.errorHandler(socket));
126
                 socket.on('error', this.conf.errorHandler(socket));
125
                 socket.on('close', this.conf.closeHandler(socket));
127
                 socket.on('close', this.conf.closeHandler(socket));
126
                 if (this.visibility === "127.0.0.1")
128
                 if (this.visibility === "127.0.0.1")
127
-                    this.initApis(socket);
129
+                    this.initRPCs(socket);
128
                 else
130
                 else
129
-                    this.initPublicApis(socket);
131
+                    this.initPublicRPCs(socket);
130
             });
132
             });
131
             this.wsServer.listen(this.port, this.visibility);
133
             this.wsServer.listen(this.port, this.visibility);
132
         }
134
         }
135
             this.errorHandler(undefined)("Unable to connect to socket");
137
             this.errorHandler(undefined)("Unable to connect to socket");
136
         }
138
         }
137
     }
139
     }
138
-    initApis(socket) {
139
-        const adminRPCs = [
140
-            {
141
-                name: 'info',
142
-                type: 'call',
143
-                func: async () => rpcInfos
144
-            }
145
-        ];
140
+    initRPCs(socket) {
141
+        socket.hook('info', () => rpcInfos);
146
         const rpcInfos = [
142
         const rpcInfos = [
147
-            ...exports.rpcHooker(socket, "Admin", adminRPCs, false),
148
-            ...this.rpcExporters.flatMap(exporter => exports.rpcHooker(socket, exporter.name, [...exporter.exportPublicRPCs(), ...exporter.exportRPCs()]))
143
+            ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter))
149
         ];
144
         ];
150
     }
145
     }
151
-    initPublicApis(socket) {
152
-        const adminRPCs = [
153
-            {
154
-                name: 'info',
155
-                type: 'call',
156
-                func: async () => rpcInfos
157
-            }
158
-        ];
146
+    initPublicRPCs(socket) {
147
+        socket.hook('info', () => rpcInfos);
159
         const rpcInfos = [
148
         const rpcInfos = [
160
-            ...exports.rpcHooker(socket, "Admin", adminRPCs, false),
161
-            ...this.rpcExporters.flatMap(exporter => exports.rpcHooker(socket, exporter.name, exporter.exportPublicRPCs()))
149
+            ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter))
162
         ];
150
         ];
163
     }
151
     }
164
 }
152
 }

+ 1
- 1
lib/test/test.js View File

11
                 name: 'echo',
11
                 name: 'echo',
12
                 func: async (s) => s,
12
                 func: async (s) => s,
13
             }],
13
             }],
14
-    }], "0.0.0.0");
14
+    }]);
15
 const caller = new RPCSocket_1.RPCSocket(20000, 'localhost');
15
 const caller = new RPCSocket_1.RPCSocket(20000, 'localhost');
16
 caller.connect().then(_ => {
16
 caller.connect().then(_ => {
17
     caller.info().then(console.log);
17
     caller.info().then(console.log);

+ 8
- 19
src/backend/RPCSocketServer.ts View File

132
     }
132
     }
133
 }
133
 }
134
 
134
 
135
-export const rpcHooker = (socket: Socket, owner:string, RPCs: socketioRPC[], makeUnique = true):ExtendedRpcInfo[] => {
135
+
136
+function rpcHooker(socket: Socket, exporter:RPCExporter, makeUnique = true):ExtendedRpcInfo[]{
137
+    const owner = exporter.name
138
+    const RPCs = [...exporter.exportPublicRPCs(), ...exporter.exportRPCs()]
136
     const suffix = makeUnique?"-"+uuid().substr(0,4):""
139
     const suffix = makeUnique?"-"+uuid().substr(0,4):""
137
     return RPCs.map(rpc => rpcToRpcinfo(rpc, owner))
140
     return RPCs.map(rpc => rpcToRpcinfo(rpc, owner))
138
     .map(info => {
141
     .map(info => {
236
     }
239
     }
237
 
240
 
238
     protected initRPCs(socket:Socket){
241
     protected initRPCs(socket:Socket){
239
-        const infoRPC:socketioRPC[] = [
240
-            {
241
-                name: 'info',
242
-                type: 'call',
243
-                func: async () => rpcInfos
244
-            }
245
-        ]
242
+        socket.hook('info', () => rpcInfos)
246
 
243
 
247
         const rpcInfos:ExtendedRpcInfo[] = [
244
         const rpcInfos:ExtendedRpcInfo[] = [
248
-            ...rpcHooker(socket, "RPC", infoRPC, false),
249
-            ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter.name, [...exporter.exportPublicRPCs(), ...exporter.exportRPCs()]))
245
+            ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter))
250
         ]
246
         ]
251
     }
247
     }
252
 
248
 
253
     protected initPublicRPCs(socket:Socket){
249
     protected initPublicRPCs(socket:Socket){
254
-        const adminRPCs:socketioRPC[] = [
255
-            {
256
-                name: 'info',
257
-                type: 'call',
258
-                func: async () => rpcInfos
259
-            }
260
-        ]
250
+        socket.hook('info', () => rpcInfos)
261
 
251
 
262
         const rpcInfos:ExtendedRpcInfo[] = [
252
         const rpcInfos:ExtendedRpcInfo[] = [
263
-            ...rpcHooker(socket, "Admin", adminRPCs, false),
264
-            ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter.name, exporter.exportPublicRPCs()))
253
+            ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter))
265
         ]
254
         ]
266
     }
255
     }
267
 }
256
 }

Loading…
Cancel
Save