This commit is contained in:
2019-09-18 01:43:58 +02:00
parent 34ec2d0244
commit 741c253202
10 changed files with 131 additions and 48 deletions
+23 -8
View File
@@ -50,6 +50,7 @@ export type AsyncFunction = (...args) => Promise<any>
export interface RPCExporter{
name: string
exportRPCs() : socketioRPC[]
exportPublicRPCs() : socketioRPC[]
}
type baseRPC = {
@@ -107,7 +108,6 @@ export const rpcToRpcinfo = (rpc : socketioRPC, owner: string):RpcInfo => {
owner: owner,
argNames: extractArgs(rpc.func),
type: rpc.type,
visibility: rpc.visibility,
name: rpc.name,
func: rpc.func,
}
@@ -116,7 +116,6 @@ export const rpcToRpcinfo = (rpc : socketioRPC, owner: string):RpcInfo => {
owner: owner,
argNames: extractArgs(rpc.func),
type: rpc.type,
visibility: rpc.visibility,
name: rpc.name,
func: rpc.func,
}
@@ -126,7 +125,6 @@ export const rpcToRpcinfo = (rpc : socketioRPC, owner: string):RpcInfo => {
owner: owner,
argNames: extractArgs(generator(undefined)),
type: rpc.type,
visibility: rpc.visibility,
name: rpc.name,
unhook: rpc.unhook,
generator: generator,
@@ -184,7 +182,7 @@ const extractArgs = (f:Function):string[] => {
type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => Socket
export type Socket = {
export interface Socket {
port: number
hook: (rpcname: string, ...args: any[]) => Socket
unhook: (rpcname:string) => Socket
@@ -225,7 +223,10 @@ export class RPCSocketServer{
this.io.on('socket', (socket:Socket) => {
socket.on('error', this.conf.errorHandler(socket))
socket.on('close', this.conf.closeHandler(socket))
this.initApis(socket)
if(this.visibility === "127.0.0.1")
this.initApis(socket)
else
this.initPublicApis(socket)
})
this.wsServer.listen(this.port, this.visibility)
}catch(e){
@@ -234,19 +235,33 @@ export class RPCSocketServer{
}
}
protected initApis(socket){
protected initApis(socket:Socket){
const adminRPCs:socketioRPC[] = [
{
name: 'info',
type: 'call',
visibility: 'private',
func: async () => rpcInfos
}
]
const rpcInfos:ExtendedRpcInfo[] = [
...rpcHooker(socket, "Admin", adminRPCs, false),
...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter.name, exporter.exportRPCs()))
...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter.name, [...exporter.exportPublicRPCs(), ...exporter.exportRPCs()]))
]
}
protected initPublicApis(socket:Socket){
const adminRPCs:socketioRPC[] = [
{
name: 'info',
type: 'call',
func: async () => rpcInfos
}
]
const rpcInfos:ExtendedRpcInfo[] = [
...rpcHooker(socket, "Admin", adminRPCs, false),
...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter.name, exporter.exportPublicRPCs()))
]
}
}
@@ -1,4 +1,4 @@
import { ExtendedRpcInfo, UnhookFunction, callbackFunction, AsyncFunction } from "../backend/RPCSocketServer";
import { ExtendedRpcInfo, UnhookFunction, callbackFunction, AsyncFunction, Socket } from "../backend/RPCSocketServer";
var bsock = require('bsock')
//fix args with defaults like "force = true" -> "force"
@@ -6,9 +6,6 @@ function stripAfterEquals(str:string){
return str.split("=")[0]
}
type RPCReceiver = {
[RPCGroup in string]: any
}
/**
* Dynamic library to communicate with FrontblockService remotely
@@ -17,14 +14,42 @@ type RPCReceiver = {
* Will ask it's service for available RPCs and parse them into methods of this object
* for convenient access.
*/
export class RPCaller implements RPCReceiver{
private socket
constructor(port:number, server: string, tls: boolean = false){
this.socket = bsock.connect(port, server, tls)
export class RPCSocket implements Socket{
private socket: Socket
constructor(public port:number, private server: string, private tls: boolean = false){
}
hook(name, args){
return this.socket.hook(name, args)
}
unhook(name){
return this.socket.unhook(name)
}
on(type: "error" | "close", f: (e?: any) => void){
return this.socket.on(type, name)
}
destroy(){
return this.socket.destroy()
}
close(){
return this.socket.close()
}
async call (rpcname: string, ...args: any[]) : Promise<any>{
return await this.socket.call.apply(this.socket, [rpcname, ...args])
}
async fire(rpcname: string, ...args: any[]) : Promise<any>{
return await this.socket.fire.apply(this.socket, [rpcname, ...args])
}
async connect(){
this.socket = await bsock.connect(this.port, this.server, this.tls)
const info:ExtendedRpcInfo[] = await this.info()
info.forEach(i => {
let f: any
+1 -1
View File
@@ -4,7 +4,7 @@ const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
mode: 'production',
target: "web",
entry: path.resolve(__dirname, 'RPCaller.ts'),
entry: path.resolve(__dirname, 'RPCSocket.ts'),
output: {
path: path.resolve(__dirname, '../../lib'),
filename: 'RPCaller.min.js',