peter 5 gadus atpakaļ
vecāks
revīzija
741c253202

+ 1
- 1
lib/RPCaller.min.js
Failā izmaiņas netiks attēlotas, jo tās ir par lielu
Parādīt failu


+ 5
- 5
lib/src/backend/RPCSocketServer.d.ts Parādīt failu

@@ -1,6 +1,5 @@
1 1
 import { Socket } from "./RPCSocketServer";
2 2
 declare type rpcType = 'hook' | 'unhook' | 'call';
3
-declare type visibility = 'public' | 'private';
4 3
 export declare type Outcome = "Success" | "Error";
5 4
 export declare type Visibility = "127.0.0.1" | "0.0.0.0";
6 5
 export declare class Response {
@@ -25,11 +24,11 @@ export declare type AsyncFunction = (...args: any[]) => Promise<any>;
25 24
 export interface RPCExporter {
26 25
     name: string;
27 26
     exportRPCs(): socketioRPC[];
27
+    exportPublicRPCs(): socketioRPC[];
28 28
 }
29 29
 declare type baseRPC = {
30 30
     type: rpcType;
31 31
     name: string;
32
-    visibility: visibility;
33 32
 };
34 33
 declare type hookRPC = baseRPC & {
35 34
     type: 'hook';
@@ -69,7 +68,7 @@ export declare type ExtendedRpcInfo = RpcInfo & {
69 68
 export declare const rpcToRpcinfo: (rpc: socketioRPC, owner: string) => RpcInfo;
70 69
 export declare const rpcHooker: (socket: Socket, owner: string, RPCs: socketioRPC[], makeUnique?: boolean) => ExtendedRpcInfo[];
71 70
 declare type OnFunction = (type: 'error' | 'close', f: (e?: any) => void) => Socket;
72
-export declare type Socket = {
71
+export interface Socket {
73 72
     port: number;
74 73
     hook: (rpcname: string, ...args: any[]) => Socket;
75 74
     unhook: (rpcname: string) => Socket;
@@ -78,7 +77,7 @@ export declare type Socket = {
78 77
     on: OnFunction;
79 78
     destroy: () => void;
80 79
     close: () => void;
81
-};
80
+}
82 81
 export declare type RPCSocketConf = {
83 82
     connectionHandler: (socket: Socket) => void;
84 83
     errorHandler: (socket: Socket) => (error: any) => void;
@@ -93,6 +92,7 @@ export declare class RPCSocketServer {
93 92
     private wsServer;
94 93
     constructor(port: number, rpcExporters?: RPCExporter[], visibility?: Visibility, conf?: RPCSocketConf);
95 94
     private startWebsocket;
96
-    protected initApis(socket: any): void;
95
+    protected initApis(socket: Socket): void;
96
+    protected initPublicApis(socket: Socket): void;
97 97
 }
98 98
 export {};

+ 18
- 6
lib/src/backend/RPCSocketServer.js Parādīt failu

@@ -38,7 +38,6 @@ exports.rpcToRpcinfo = (rpc, owner) => {
38 38
                 owner: owner,
39 39
                 argNames: extractArgs(rpc.func),
40 40
                 type: rpc.type,
41
-                visibility: rpc.visibility,
42 41
                 name: rpc.name,
43 42
                 func: rpc.func,
44 43
             };
@@ -47,7 +46,6 @@ exports.rpcToRpcinfo = (rpc, owner) => {
47 46
                 owner: owner,
48 47
                 argNames: extractArgs(rpc.func),
49 48
                 type: rpc.type,
50
-                visibility: rpc.visibility,
51 49
                 name: rpc.name,
52 50
                 func: rpc.func,
53 51
             };
@@ -57,7 +55,6 @@ exports.rpcToRpcinfo = (rpc, owner) => {
57 55
                 owner: owner,
58 56
                 argNames: extractArgs(generator(undefined)),
59 57
                 type: rpc.type,
60
-                visibility: rpc.visibility,
61 58
                 name: rpc.name,
62 59
                 unhook: rpc.unhook,
63 60
                 generator: generator,
@@ -126,7 +123,10 @@ class RPCSocketServer {
126 123
             this.io.on('socket', (socket) => {
127 124
                 socket.on('error', this.conf.errorHandler(socket));
128 125
                 socket.on('close', this.conf.closeHandler(socket));
129
-                this.initApis(socket);
126
+                if (this.visibility === "127.0.0.1")
127
+                    this.initApis(socket);
128
+                else
129
+                    this.initPublicApis(socket);
130 130
             });
131 131
             this.wsServer.listen(this.port, this.visibility);
132 132
         }
@@ -140,13 +140,25 @@ class RPCSocketServer {
140 140
             {
141 141
                 name: 'info',
142 142
                 type: 'call',
143
-                visibility: 'private',
144 143
                 func: async () => rpcInfos
145 144
             }
146 145
         ];
147 146
         const rpcInfos = [
148 147
             ...exports.rpcHooker(socket, "Admin", adminRPCs, false),
149
-            ...this.rpcExporters.flatMap(exporter => exports.rpcHooker(socket, exporter.name, exporter.exportRPCs()))
148
+            ...this.rpcExporters.flatMap(exporter => exports.rpcHooker(socket, exporter.name, [...exporter.exportPublicRPCs(), ...exporter.exportRPCs()]))
149
+        ];
150
+    }
151
+    initPublicApis(socket) {
152
+        const adminRPCs = [
153
+            {
154
+                name: 'info',
155
+                type: 'call',
156
+                func: async () => rpcInfos
157
+            }
158
+        ];
159
+        const rpcInfos = [
160
+            ...exports.rpcHooker(socket, "Admin", adminRPCs, false),
161
+            ...this.rpcExporters.flatMap(exporter => exports.rpcHooker(socket, exporter.name, exporter.exportPublicRPCs()))
150 162
         ];
151 163
     }
152 164
 }

lib/src/frontend/RPCaller.d.ts → lib/src/frontend/RPCSocket.d.ts Parādīt failu

@@ -1,6 +1,4 @@
1
-declare type RPCReceiver = {
2
-    [RPCGroup in string]: any;
3
-};
1
+import { Socket } from "../backend/RPCSocketServer";
4 2
 /**
5 3
  * Dynamic library to communicate with FrontblockService remotely
6 4
  *
@@ -8,13 +6,22 @@ declare type RPCReceiver = {
8 6
  * Will ask it's service for available RPCs and parse them into methods of this object
9 7
  * for convenient access.
10 8
  */
11
-export declare class RPCaller implements RPCReceiver {
9
+export declare class RPCSocket implements Socket {
10
+    port: number;
11
+    private server;
12
+    private tls;
12 13
     private socket;
13 14
     constructor(port: number, server: string, tls?: boolean);
15
+    hook(name: any, args: any): Socket;
16
+    unhook(name: any): Socket;
17
+    on(type: "error" | "close", f: (e?: any) => void): Socket;
18
+    destroy(): void;
19
+    close(): void;
20
+    call(rpcname: string, ...args: any[]): Promise<any>;
21
+    fire(rpcname: string, ...args: any[]): Promise<any>;
14 22
     connect(): Promise<void>;
15 23
     info(): Promise<any>;
16 24
     private callGenerator;
17 25
     private hookGenerator;
18 26
     private unhookGenerator;
19 27
 }
20
-export {};

lib/src/frontend/RPCaller.js → lib/src/frontend/RPCSocket.js Parādīt failu

@@ -12,11 +12,35 @@ function stripAfterEquals(str) {
12 12
  * Will ask it's service for available RPCs and parse them into methods of this object
13 13
  * for convenient access.
14 14
  */
15
-class RPCaller {
15
+class RPCSocket {
16 16
     constructor(port, server, tls = false) {
17
-        this.socket = bsock.connect(port, server, tls);
17
+        this.port = port;
18
+        this.server = server;
19
+        this.tls = tls;
20
+    }
21
+    hook(name, args) {
22
+        return this.socket.hook(name, args);
23
+    }
24
+    unhook(name) {
25
+        return this.socket.unhook(name);
26
+    }
27
+    on(type, f) {
28
+        return this.socket.on(type, name);
29
+    }
30
+    destroy() {
31
+        return this.socket.destroy();
32
+    }
33
+    close() {
34
+        return this.socket.close();
35
+    }
36
+    async call(rpcname, ...args) {
37
+        return await this.socket.call.apply(this.socket, [rpcname, ...args]);
38
+    }
39
+    async fire(rpcname, ...args) {
40
+        return await this.socket.fire.apply(this.socket, [rpcname, ...args]);
18 41
     }
19 42
     async connect() {
43
+        this.socket = await bsock.connect(this.port, this.server, this.tls);
20 44
         const info = await this.info();
21 45
         info.forEach(i => {
22 46
             let f;
@@ -68,4 +92,4 @@ class RPCaller {
68 92
                         } )()`);
69 93
     }
70 94
 }
71
-exports.RPCaller = RPCaller;
95
+exports.RPCSocket = RPCSocket;

+ 5
- 5
lib/test/test.js Parādīt failu

@@ -2,17 +2,17 @@
2 2
 Object.defineProperty(exports, "__esModule", { value: true });
3 3
 const RPCSocketServer_1 = require("../src/backend/RPCSocketServer");
4 4
 //@ts-ignore
5
-const RPCaller_1 = require("../src/frontend/RPCaller");
5
+const RPCSocket_1 = require("../src/frontend/RPCSocket");
6 6
 new RPCSocketServer_1.RPCSocketServer(20000, [{
7 7
         name: "HelloWorldRPCGroup",
8
+        exportPublicRPCs: () => [],
8 9
         exportRPCs: () => [{
9 10
                 type: 'call',
10 11
                 name: 'echo',
11 12
                 func: async (s) => s,
12
-                visibility: 'private'
13
-            }]
14
-    }]);
15
-const caller = new RPCaller_1.RPCaller(20000, 'localhost');
13
+            }],
14
+    }], "0.0.0.0");
15
+const caller = new RPCSocket_1.RPCSocket(20000, 'localhost');
16 16
 caller.connect().then(_ => {
17 17
     caller.info().then(console.log);
18 18
     caller["HelloWorldRPCGroup"].echo("x").then(console.log);

+ 23
- 8
src/backend/RPCSocketServer.ts Parādīt failu

@@ -50,6 +50,7 @@ export type AsyncFunction = (...args) => Promise<any>
50 50
 export interface RPCExporter{
51 51
     name: string
52 52
     exportRPCs() : socketioRPC[]
53
+    exportPublicRPCs() : socketioRPC[]
53 54
 }
54 55
 
55 56
 type baseRPC = { 
@@ -107,7 +108,6 @@ export const rpcToRpcinfo = (rpc : socketioRPC, owner: string):RpcInfo => {
107 108
                 owner: owner,
108 109
                 argNames: extractArgs(rpc.func),
109 110
                 type: rpc.type,
110
-                visibility: rpc.visibility,
111 111
                 name: rpc.name,
112 112
                 func: rpc.func,
113 113
             }
@@ -116,7 +116,6 @@ export const rpcToRpcinfo = (rpc : socketioRPC, owner: string):RpcInfo => {
116 116
                 owner: owner,
117 117
                 argNames: extractArgs(rpc.func),
118 118
                 type: rpc.type,
119
-                visibility: rpc.visibility,
120 119
                 name: rpc.name,
121 120
                 func: rpc.func,
122 121
             }
@@ -126,7 +125,6 @@ export const rpcToRpcinfo = (rpc : socketioRPC, owner: string):RpcInfo => {
126 125
                 owner: owner,
127 126
                 argNames: extractArgs(generator(undefined)),
128 127
                 type: rpc.type,
129
-                visibility: rpc.visibility,
130 128
                 name: rpc.name,
131 129
                 unhook: rpc.unhook,
132 130
                 generator: generator,
@@ -184,7 +182,7 @@ const extractArgs = (f:Function):string[] => {
184 182
 
185 183
 type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => Socket
186 184
 
187
-export type Socket = {
185
+export interface Socket {
188 186
     port: number
189 187
     hook: (rpcname: string, ...args: any[]) => Socket
190 188
     unhook: (rpcname:string) => Socket
@@ -225,7 +223,10 @@ export class RPCSocketServer{
225 223
             this.io.on('socket', (socket:Socket) => {
226 224
                 socket.on('error', this.conf.errorHandler(socket))
227 225
                 socket.on('close', this.conf.closeHandler(socket))
228
-                this.initApis(socket)
226
+                if(this.visibility === "127.0.0.1")
227
+                    this.initApis(socket)
228
+                else
229
+                    this.initPublicApis(socket)
229 230
             })
230 231
             this.wsServer.listen(this.port, this.visibility)
231 232
         }catch(e){
@@ -234,19 +235,33 @@ export class RPCSocketServer{
234 235
         }
235 236
     }
236 237
 
237
-    protected initApis(socket){
238
+    protected initApis(socket:Socket){
238 239
         const adminRPCs:socketioRPC[] = [
239 240
             {
240 241
                 name: 'info',
241 242
                 type: 'call',
242
-                visibility: 'private',
243 243
                 func: async () => rpcInfos
244 244
             }
245 245
         ]
246 246
 
247 247
         const rpcInfos:ExtendedRpcInfo[] = [
248 248
             ...rpcHooker(socket, "Admin", adminRPCs, false),
249
-            ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter.name, exporter.exportRPCs()))
249
+            ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter.name, [...exporter.exportPublicRPCs(), ...exporter.exportRPCs()]))
250
+        ]
251
+    }
252
+
253
+    protected initPublicApis(socket:Socket){
254
+        const adminRPCs:socketioRPC[] = [
255
+            {
256
+                name: 'info',
257
+                type: 'call',
258
+                func: async () => rpcInfos
259
+            }
260
+        ]
261
+
262
+        const rpcInfos:ExtendedRpcInfo[] = [
263
+            ...rpcHooker(socket, "Admin", adminRPCs, false),
264
+            ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter.name, exporter.exportPublicRPCs()))
250 265
         ]
251 266
     }
252 267
 }

src/frontend/RPCaller.ts → src/frontend/RPCSocket.ts Parādīt failu

@@ -1,4 +1,4 @@
1
-import { ExtendedRpcInfo, UnhookFunction, callbackFunction, AsyncFunction } from "../backend/RPCSocketServer";
1
+import { ExtendedRpcInfo, UnhookFunction, callbackFunction, AsyncFunction, Socket } from "../backend/RPCSocketServer";
2 2
 var bsock = require('bsock')
3 3
 
4 4
 //fix args with defaults like "force = true" -> "force"
@@ -6,9 +6,6 @@ function stripAfterEquals(str:string){
6 6
     return str.split("=")[0]
7 7
 }
8 8
 
9
-type RPCReceiver = {
10
-    [RPCGroup in string]: any
11
-}
12 9
 
13 10
 /**
14 11
  * Dynamic library to communicate with FrontblockService remotely
@@ -17,14 +14,42 @@ type RPCReceiver = {
17 14
  * Will ask it's service for available RPCs and parse them into methods of this object
18 15
  * for convenient access.
19 16
  */
20
-export class RPCaller implements RPCReceiver{
21
-    private socket
22
-    
23
-    constructor(port:number, server: string, tls: boolean = false){
24
-        this.socket = bsock.connect(port, server, tls)
17
+export class RPCSocket implements Socket{
18
+    private socket: Socket
19
+    constructor(public port:number, private server: string, private tls: boolean = false){
25 20
     }
26 21
 
22
+    hook(name, args){
23
+        return this.socket.hook(name, args)
24
+    }
25
+
26
+    unhook(name){
27
+        return this.socket.unhook(name)
28
+    }
29
+
30
+    on(type: "error" | "close", f: (e?: any) => void){
31
+        return this.socket.on(type, name)
32
+    }
33
+
34
+    destroy(){
35
+        return this.socket.destroy()
36
+    }
37
+
38
+    close(){
39
+        return this.socket.close()
40
+    }
41
+
42
+    async call (rpcname: string, ...args: any[]) : Promise<any>{
43
+        return await this.socket.call.apply(this.socket, [rpcname, ...args])
44
+    }
45
+
46
+    async fire(rpcname: string, ...args: any[]) : Promise<any>{
47
+        return await this.socket.fire.apply(this.socket, [rpcname, ...args])
48
+    }
49
+    
27 50
     async connect(){
51
+        this.socket = await bsock.connect(this.port, this.server, this.tls)
52
+
28 53
         const info:ExtendedRpcInfo[] = await this.info()
29 54
         info.forEach(i => {
30 55
             let f: any

+ 1
- 1
src/frontend/webpack.prod.js Parādīt failu

@@ -4,7 +4,7 @@ const TerserPlugin = require('terser-webpack-plugin');
4 4
 module.exports = {
5 5
   mode: 'production',
6 6
   target: "web",
7
-  entry: path.resolve(__dirname, 'RPCaller.ts'),
7
+  entry: path.resolve(__dirname, 'RPCSocket.ts'),
8 8
   output: {
9 9
       path: path.resolve(__dirname, '../../lib'),
10 10
       filename: 'RPCaller.min.js',

+ 5
- 5
test/test.ts Parādīt failu

@@ -1,18 +1,18 @@
1 1
 import { RPCSocketServer } from '../src/backend/RPCSocketServer'
2 2
 //@ts-ignore
3
-import {RPCaller} from '../src/frontend/RPCaller'
3
+import {RPCSocket} from '../src/frontend/RPCSocket'
4 4
 
5 5
 new RPCSocketServer(20000, [{
6 6
     name: "HelloWorldRPCGroup",
7
+    exportPublicRPCs: () => [],
7 8
     exportRPCs: () => [{
8 9
         type: 'call',
9 10
         name: 'echo',
10 11
         func: async (s:string) => s,
11
-        visibility: 'private'
12
-    }]
13
-}] )
12
+    }],
13
+}])
14 14
 
15
-const caller = new RPCaller(20000, 'localhost')
15
+const caller = new RPCSocket(20000, 'localhost')
16 16
 caller.connect().then(_ => {
17 17
     caller.info().then(console.log)
18 18
     caller["HelloWorldRPCGroup"].echo("x").then(console.log)

Notiek ielāde…
Atcelt
Saglabāt