89 lines
3.1 KiB
TypeScript
89 lines
3.1 KiB
TypeScript
import { parseSubResponse, parseResponse } from "frontblock-generic/Types";
|
|
var bsock = require('bsock')
|
|
|
|
/**
|
|
* Dynamic library to communicate with FrontblockService remotely
|
|
*
|
|
* This will be automatically injected into the webpages served by FrontblockService
|
|
* Will ask it's service for available RPCs and parse them into methods of this object
|
|
* for convenient access.
|
|
*/
|
|
export class FrontblockConfigLib{
|
|
private socket
|
|
|
|
constructor(){
|
|
this.socket = bsock.connect(20000, 'localhost', false/*tls*/)
|
|
this.init()
|
|
}
|
|
|
|
// need this-context for eval-magic below
|
|
// DO NOT REMOVE. They're not really unused
|
|
private parseSubResponse = parseSubResponse
|
|
private parseResponse = parseResponse
|
|
|
|
private async init(){
|
|
const info = await this.info()
|
|
for (const i of info) {
|
|
let f: any
|
|
switch (i.info.type) {
|
|
case 'call':
|
|
f = this.callGenerator(i.name, i.args)
|
|
break
|
|
case 'hook':
|
|
f = this.hookGenerator(i.name, i.args)
|
|
break
|
|
case 'unhook':
|
|
f = this.unhookGenerator(i.name, i.args)
|
|
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 hookGenerator(fnName, fnArgs): Function{
|
|
return eval( `( () => async (`+fnArgs+(fnArgs.length!==0?",":"")+` callback) => {
|
|
const r = await this.socket.call("`+fnName+`", `+fnArgs+`)
|
|
const res = await this.parseSubResponse(r);
|
|
if(res.uid != null){
|
|
this.socket.hook(res.uid, callback)
|
|
}
|
|
return res
|
|
} )()` )
|
|
}
|
|
|
|
private unhookGenerator(fnName, fnArgs): Function{
|
|
return eval( `( () => async (`+fnArgs+`) => {
|
|
const r = await this.socket.call("`+fnName+`", `+fnArgs+`)
|
|
const res = await this.parseResponse(r)
|
|
console.log(res)
|
|
if(res.uid != null)
|
|
this.socket.unhook(res.uid)
|
|
return res
|
|
} )()` )
|
|
}
|
|
|
|
}
|
|
|
|
window['fb'] = new FrontblockConfigLib()
|
|
declare const fb:{PluginManager:any}
|
|
window['setup'] = async() => {
|
|
await Promise.all([
|
|
fb.PluginManager.installPlugin("ApiClient"),
|
|
fb.PluginManager.installPlugin("HtmlSupplier"),
|
|
fb.PluginManager.installPlugin("PaymentManager"),
|
|
fb.PluginManager.installPlugin("BTCWallet"),
|
|
])
|
|
await fb.PluginManager.installPlugin("PluginManager"),
|
|
location.reload();
|
|
} |