RPC update
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import { parseSubResponse, parseResponse } from "frontblock-generic/Types";
|
||||
import { ExtendedRpcInfo, UnhookFunction, callbackFunction, AsyncFunction } from "frontblock-generic/RPC";
|
||||
var bsock = require('bsock')
|
||||
|
||||
//fix args with defaults like "force = true" -> "force"
|
||||
function stripAfterEquals(str:string){
|
||||
return str.split("=")[0]
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamic library to communicate with FrontblockService remotely
|
||||
*
|
||||
@@ -8,7 +14,7 @@ var bsock = require('bsock')
|
||||
* Will ask it's service for available RPCs and parse them into methods of this object
|
||||
* for convenient access.
|
||||
*/
|
||||
class FrontblockConfigLib{
|
||||
export class FrontblockConfigLib{
|
||||
private socket
|
||||
|
||||
constructor(){
|
||||
@@ -22,38 +28,42 @@ class FrontblockConfigLib{
|
||||
private parseResponse = parseResponse
|
||||
|
||||
private async init(){
|
||||
const info = await this.info()
|
||||
for (const i of info) {
|
||||
const info:ExtendedRpcInfo[] = await this.info()
|
||||
info.forEach(i => {
|
||||
let f: any
|
||||
switch (i.info.type) {
|
||||
switch (i.type) {
|
||||
case 'call':
|
||||
f = this.callGenerator(i.name, i.args)
|
||||
f = this.callGenerator(i.uniqueName, i.argNames)
|
||||
break
|
||||
case 'hook':
|
||||
f = this.hookGenerator(i.name, i.args)
|
||||
f = this.hookGenerator(i.uniqueName, i.argNames)
|
||||
break
|
||||
case 'unhook':
|
||||
f = this.unhookGenerator(i.name, i.args)
|
||||
f = this.unhookGenerator(i.uniqueName, i.argNames)
|
||||
break
|
||||
}
|
||||
if(this[i.owner] == null)
|
||||
this[i.owner] = {}
|
||||
this[i.owner][i.name] = f
|
||||
this[i.owner][i.name].bind(this)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async info(){
|
||||
return await this.socket.call('info')
|
||||
}
|
||||
|
||||
private callGenerator(fnName, fnArgs): Function{
|
||||
return eval( '( () => async ('+fnArgs+') => { return await this.socket.call("'+fnName+'", '+fnArgs+')} )()' )
|
||||
private callGenerator(fnName, fnArgs:string[]): AsyncFunction{
|
||||
const headerArgs = fnArgs.join(",")
|
||||
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
||||
return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", '+argParams+')} )()' )
|
||||
}
|
||||
|
||||
private hookGenerator(fnName, fnArgs): Function{
|
||||
return eval( `( () => async (`+fnArgs+(fnArgs.length!==0?",":"")+` callback) => {
|
||||
const r = await this.socket.call("`+fnName+`", `+fnArgs+`)
|
||||
private hookGenerator(fnName, fnArgs:string[]): callbackFunction{
|
||||
const headerArgs = fnArgs.join(",")
|
||||
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
||||
return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
|
||||
const r = await this.socket.call("`+fnName+`", `+argParams+`)
|
||||
const res = await this.parseSubResponse(r);
|
||||
if(res.uid != null){
|
||||
this.socket.hook(res.uid, callback)
|
||||
@@ -62,13 +72,16 @@ class FrontblockConfigLib{
|
||||
} )()` )
|
||||
}
|
||||
|
||||
private unhookGenerator(fnName, fnArgs): Function{
|
||||
return eval( `( () => async (`+fnArgs+`) => {
|
||||
const r = await this.socket.call("`+fnName+`", `+fnArgs+`)
|
||||
private unhookGenerator(fnName, fnArgs:string[]): UnhookFunction{
|
||||
const headerArgs = fnArgs.join(",")
|
||||
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
||||
if(fnArgs.length != 1)
|
||||
console.error("UnhookFunction", fnName, "specified more than one argument: ("+headerArgs+")")
|
||||
|
||||
return eval( `( () => async (`+headerArgs+`) => {
|
||||
const r = await this.socket.call("`+fnName+`", `+argParams+`)
|
||||
const res = await this.parseResponse(r)
|
||||
console.log(res)
|
||||
if(res.uid != null)
|
||||
this.socket.unhook(res.uid)
|
||||
this.socket.unhook(`+argParams+`)
|
||||
return res
|
||||
} )()` )
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user