peter 5 роки тому
джерело
коміт
17dc58c5b3

+ 2
- 3
lib/src/backend/RPCSocketServer.d.ts Переглянути файл

@@ -66,7 +66,6 @@ export declare type ExtendedRpcInfo = RpcInfo & {
66 66
     uniqueName: string;
67 67
 };
68 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 69
 declare type OnFunction = (type: 'error' | 'close', f: (e?: any) => void) => Socket;
71 70
 export interface Socket {
72 71
     port: number;
@@ -92,7 +91,7 @@ export declare class RPCSocketServer {
92 91
     private wsServer;
93 92
     constructor(port: number, rpcExporters?: RPCExporter[], visibility?: Visibility, conf?: RPCSocketConf);
94 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 97
 export {};

+ 12
- 24
lib/src/backend/RPCSocketServer.js Переглянути файл

@@ -61,7 +61,9 @@ exports.rpcToRpcinfo = (rpc, owner) => {
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 67
     const suffix = makeUnique ? "-" + uuid().substr(0, 4) : "";
66 68
     return RPCs.map(rpc => exports.rpcToRpcinfo(rpc, owner))
67 69
         .map(info => {
@@ -77,7 +79,7 @@ exports.rpcHooker = (socket, owner, RPCs, makeUnique = true) => {
77 79
         socket.on('close', () => socket.unhook(info.name));
78 80
         return ret;
79 81
     });
80
-};
82
+}
81 83
 const hookGenerator = (rpc) => {
82 84
     const argsArr = extractArgs(rpc.func);
83 85
     argsArr.pop();
@@ -124,9 +126,9 @@ class RPCSocketServer {
124 126
                 socket.on('error', this.conf.errorHandler(socket));
125 127
                 socket.on('close', this.conf.closeHandler(socket));
126 128
                 if (this.visibility === "127.0.0.1")
127
-                    this.initApis(socket);
129
+                    this.initRPCs(socket);
128 130
                 else
129
-                    this.initPublicApis(socket);
131
+                    this.initPublicRPCs(socket);
130 132
             });
131 133
             this.wsServer.listen(this.port, this.visibility);
132 134
         }
@@ -135,30 +137,16 @@ class RPCSocketServer {
135 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 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 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 Переглянути файл

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

+ 8
- 19
src/backend/RPCSocketServer.ts Переглянути файл

@@ -132,7 +132,10 @@ export const rpcToRpcinfo = (rpc : socketioRPC, owner: string):RpcInfo => {
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 139
     const suffix = makeUnique?"-"+uuid().substr(0,4):""
137 140
     return RPCs.map(rpc => rpcToRpcinfo(rpc, owner))
138 141
     .map(info => {
@@ -236,32 +239,18 @@ export class RPCSocketServer{
236 239
     }
237 240
 
238 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 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 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 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
 }

Завантаження…
Відмінити
Зберегти