fix
This commit is contained in:
Vendored
+19
File diff suppressed because one or more lines are too long
Vendored
+96
@@ -0,0 +1,96 @@
|
||||
import { Socket } from "./RPCSocketServer";
|
||||
declare type rpcType = 'hook' | 'unhook' | 'call';
|
||||
declare type visibility = 'public' | 'private';
|
||||
export declare type Outcome = "Success" | "Error";
|
||||
export declare class Response {
|
||||
message?: string | undefined;
|
||||
constructor(message?: string | undefined);
|
||||
}
|
||||
export declare class SuccessResponse extends Response {
|
||||
result: Outcome;
|
||||
constructor(message?: string);
|
||||
}
|
||||
export declare class ErrorResponse extends Response {
|
||||
result: Outcome;
|
||||
constructor(message?: string);
|
||||
}
|
||||
export declare class SubscriptionResponse extends SuccessResponse {
|
||||
uid: string;
|
||||
constructor(uid: string, message?: string);
|
||||
}
|
||||
export declare type UnhookFunction = (uid: string) => Promise<SuccessResponse | ErrorResponse>;
|
||||
export declare type callbackFunction = (...args: any[]) => Promise<SubscriptionResponse | ErrorResponse>;
|
||||
export declare type AsyncFunction = (...args: any[]) => Promise<any>;
|
||||
export interface RPCExporter {
|
||||
name: string;
|
||||
exportRPCs(): socketioRPC[];
|
||||
}
|
||||
declare type baseRPC = {
|
||||
type: rpcType;
|
||||
name: string;
|
||||
visibility: visibility;
|
||||
};
|
||||
declare type hookRPC = baseRPC & {
|
||||
type: 'hook';
|
||||
func: callbackFunction;
|
||||
unhook: UnhookFunction;
|
||||
};
|
||||
declare type unhookRPC = baseRPC & {
|
||||
type: 'unhook';
|
||||
func: UnhookFunction;
|
||||
};
|
||||
declare type callRPC = baseRPC & {
|
||||
type: 'call';
|
||||
func: (...args: any[]) => Promise<any>;
|
||||
};
|
||||
export declare type socketioRPC = callRPC | unhookRPC | hookRPC;
|
||||
export declare type baseInfo = {
|
||||
owner: string;
|
||||
argNames: string[];
|
||||
};
|
||||
declare type HookInfo = baseRPC & baseInfo & {
|
||||
type: 'hook';
|
||||
generator: (socket: any) => callbackFunction;
|
||||
unhook: UnhookFunction;
|
||||
};
|
||||
declare type UnhookInfo = baseRPC & baseInfo & {
|
||||
type: 'unhook';
|
||||
func: UnhookFunction;
|
||||
};
|
||||
declare type CallInfo = baseRPC & baseInfo & {
|
||||
type: 'call';
|
||||
func: AsyncFunction;
|
||||
};
|
||||
declare type RpcInfo = HookInfo | UnhookInfo | CallInfo;
|
||||
export declare type ExtendedRpcInfo = RpcInfo & {
|
||||
uniqueName: string;
|
||||
};
|
||||
export declare const rpcToRpcinfo: (rpc: socketioRPC, owner: string) => RpcInfo;
|
||||
export declare const rpcHooker: (socket: Socket, owner: string, RPCs: socketioRPC[], makeUnique?: boolean) => ExtendedRpcInfo[];
|
||||
declare type OnFunction = (type: 'error' | 'close', f: (e?: any) => void) => Socket;
|
||||
export declare type Socket = {
|
||||
port: number;
|
||||
hook: (rpcname: string, ...args: any[]) => Socket;
|
||||
unhook: (rpcname: string) => Socket;
|
||||
call: (rpcname: string, ...args: any[]) => Promise<any>;
|
||||
fire: (rpcname: string, ...args: any[]) => Promise<any>;
|
||||
on: OnFunction;
|
||||
destroy: () => void;
|
||||
close: () => void;
|
||||
};
|
||||
export declare type RPCSocketConf = {
|
||||
connectionHandler: (socket: Socket) => void;
|
||||
errorHandler: (socket: Socket) => (error: any) => void;
|
||||
closeHandler: (socket: Socket) => () => void;
|
||||
};
|
||||
export declare class RPCSocketServer {
|
||||
private port;
|
||||
private rpcExporters;
|
||||
private conf;
|
||||
private io;
|
||||
private wsServer;
|
||||
constructor(port: number, rpcExporters?: RPCExporter[], conf?: RPCSocketConf);
|
||||
private startWebsocket;
|
||||
protected initApis(socket: any): void;
|
||||
}
|
||||
export {};
|
||||
@@ -0,0 +1,152 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const http = require("http");
|
||||
const bsock = require("bsock");
|
||||
const uuid = require("uuid/v4");
|
||||
/* Responses */
|
||||
class Response {
|
||||
constructor(message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
exports.Response = Response;
|
||||
class SuccessResponse extends Response {
|
||||
constructor(message) {
|
||||
super(message);
|
||||
this.result = "Success";
|
||||
}
|
||||
}
|
||||
exports.SuccessResponse = SuccessResponse;
|
||||
class ErrorResponse extends Response {
|
||||
constructor(message = "Unknown error") {
|
||||
super(message);
|
||||
this.result = "Error";
|
||||
}
|
||||
}
|
||||
exports.ErrorResponse = ErrorResponse;
|
||||
class SubscriptionResponse extends SuccessResponse {
|
||||
constructor(uid, message) {
|
||||
super(message);
|
||||
this.uid = uid;
|
||||
}
|
||||
}
|
||||
exports.SubscriptionResponse = SubscriptionResponse;
|
||||
exports.rpcToRpcinfo = (rpc, owner) => {
|
||||
switch (rpc.type) {
|
||||
case "call":
|
||||
return {
|
||||
owner: owner,
|
||||
argNames: extractArgs(rpc.func),
|
||||
type: rpc.type,
|
||||
visibility: rpc.visibility,
|
||||
name: rpc.name,
|
||||
func: rpc.func,
|
||||
};
|
||||
case "unhook":
|
||||
return {
|
||||
owner: owner,
|
||||
argNames: extractArgs(rpc.func),
|
||||
type: rpc.type,
|
||||
visibility: rpc.visibility,
|
||||
name: rpc.name,
|
||||
func: rpc.func,
|
||||
};
|
||||
case "hook":
|
||||
const generator = hookGenerator(rpc);
|
||||
return {
|
||||
owner: owner,
|
||||
argNames: extractArgs(generator(undefined)),
|
||||
type: rpc.type,
|
||||
visibility: rpc.visibility,
|
||||
name: rpc.name,
|
||||
unhook: rpc.unhook,
|
||||
generator: generator,
|
||||
};
|
||||
}
|
||||
};
|
||||
exports.rpcHooker = (socket, owner, RPCs, makeUnique = true) => {
|
||||
const suffix = makeUnique ? "-" + uuid().substr(0, 4) : "";
|
||||
return RPCs.map(rpc => exports.rpcToRpcinfo(rpc, owner))
|
||||
.map(info => {
|
||||
const ret = info;
|
||||
ret.uniqueName = info.name + suffix;
|
||||
switch (info.type) {
|
||||
case "hook":
|
||||
socket.hook(ret.uniqueName, info.generator(socket));
|
||||
break;
|
||||
default:
|
||||
socket.hook(ret.uniqueName, info.func);
|
||||
}
|
||||
socket.on('close', () => socket.unhook(info.name));
|
||||
return ret;
|
||||
});
|
||||
};
|
||||
const hookGenerator = (rpc) => {
|
||||
const argsArr = extractArgs(rpc.func);
|
||||
argsArr.pop();
|
||||
const args = argsArr.join(',');
|
||||
return eval(`(socket) => async (` + args + `) => {
|
||||
const res = await rpc.func(` + args + (args.length !== 0 ? ',' : '') + ` (x) => {
|
||||
socket.call(res.uid, x)
|
||||
})
|
||||
if(res.result == 'Success'){
|
||||
socket.on('close', async () => {
|
||||
const unhookRes = await rpc.unhook(res.uid)
|
||||
console.log("Specific close handler for", rpc.name, res.uid, unhookRes)
|
||||
})
|
||||
|
||||
}
|
||||
return res
|
||||
}`);
|
||||
};
|
||||
const extractArgs = (f) => {
|
||||
let fn = String(f);
|
||||
let args = fn.substr(0, fn.indexOf(")"));
|
||||
args = args.substr(fn.indexOf("(") + 1);
|
||||
let ret = args.split(",");
|
||||
return ret;
|
||||
};
|
||||
class RPCSocketServer {
|
||||
constructor(port, rpcExporters = [], conf = {
|
||||
errorHandler: (socket) => (error) => { socket.destroy(); console.error(error); },
|
||||
closeHandler: (socket) => () => { console.log("Socket closing"); },
|
||||
connectionHandler: (socket) => { console.log("New websocket connection in port " + socket.port); }
|
||||
}) {
|
||||
this.port = port;
|
||||
this.rpcExporters = rpcExporters;
|
||||
this.conf = conf;
|
||||
this.io = bsock.createServer();
|
||||
this.wsServer = http.createServer();
|
||||
this.startWebsocket();
|
||||
}
|
||||
startWebsocket() {
|
||||
try {
|
||||
this.io.attach(this.wsServer);
|
||||
this.io.on('socket', (socket) => {
|
||||
socket.on('error', this.conf.errorHandler(socket));
|
||||
socket.on('close', this.conf.closeHandler(socket));
|
||||
this.initApis(socket);
|
||||
});
|
||||
this.wsServer.listen(this.port);
|
||||
}
|
||||
catch (e) {
|
||||
//@ts-ignore
|
||||
this.errorHandler(undefined)("Unable to connect to socket");
|
||||
}
|
||||
}
|
||||
initApis(socket) {
|
||||
const adminRPCs = [
|
||||
{
|
||||
name: 'info',
|
||||
type: 'call',
|
||||
visibility: 'private',
|
||||
func: async () => rpcInfos
|
||||
}
|
||||
];
|
||||
const rpcInfos = [
|
||||
...exports.rpcHooker(socket, "Admin", adminRPCs, false),
|
||||
...this.rpcExporters.flatMap(exporter => exports.rpcHooker(socket, exporter.name, exporter.exportRPCs()))
|
||||
];
|
||||
}
|
||||
}
|
||||
exports.RPCSocketServer = RPCSocketServer;
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
declare type RPCReceiver = {
|
||||
[RPCGroup in string]: any;
|
||||
};
|
||||
/**
|
||||
* 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 declare class RPCaller implements RPCReceiver {
|
||||
private socket;
|
||||
constructor(port: number, server: string, tls?: boolean);
|
||||
connect(): Promise<void>;
|
||||
info(): Promise<any>;
|
||||
private callGenerator;
|
||||
private hookGenerator;
|
||||
private unhookGenerator;
|
||||
}
|
||||
export {};
|
||||
@@ -0,0 +1,71 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var bsock = require('bsock');
|
||||
//fix args with defaults like "force = true" -> "force"
|
||||
function stripAfterEquals(str) {
|
||||
return str.split("=")[0];
|
||||
}
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
class RPCaller {
|
||||
constructor(port, server, tls = false) {
|
||||
this.socket = bsock.connect(port, server, tls);
|
||||
}
|
||||
async connect() {
|
||||
const info = await this.info();
|
||||
info.forEach(i => {
|
||||
let f;
|
||||
switch (i.type) {
|
||||
case 'call':
|
||||
f = this.callGenerator(i.uniqueName, i.argNames);
|
||||
break;
|
||||
case 'hook':
|
||||
f = this.hookGenerator(i.uniqueName, i.argNames);
|
||||
break;
|
||||
case 'unhook':
|
||||
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');
|
||||
}
|
||||
callGenerator(fnName, fnArgs) {
|
||||
const headerArgs = fnArgs.join(",");
|
||||
const argParams = fnArgs.map(stripAfterEquals).join(",");
|
||||
return eval('( () => async (' + headerArgs + ') => { return await this.socket.call("' + fnName + '", ' + argParams + ')} )()');
|
||||
}
|
||||
hookGenerator(fnName, fnArgs) {
|
||||
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 + `)
|
||||
if(r.uid != null){
|
||||
this.socket.hook(res.uid, callback)
|
||||
}
|
||||
return res
|
||||
} )()`);
|
||||
}
|
||||
unhookGenerator(fnName, fnArgs) {
|
||||
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 + `)
|
||||
this.socket.unhook(` + argParams + `)
|
||||
return res
|
||||
} )()`);
|
||||
}
|
||||
}
|
||||
exports.RPCaller = RPCaller;
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
@@ -0,0 +1,19 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const RPCSocketServer_1 = require("../src/backend/RPCSocketServer");
|
||||
//@ts-ignore
|
||||
const RPCaller_1 = require("../src/frontend/RPCaller");
|
||||
new RPCSocketServer_1.RPCSocketServer(20000, [{
|
||||
name: "HelloWorldRPCGroup",
|
||||
exportRPCs: () => [{
|
||||
type: 'call',
|
||||
name: 'echo',
|
||||
func: async (s) => s,
|
||||
visibility: 'private'
|
||||
}]
|
||||
}]);
|
||||
const caller = new RPCaller_1.RPCaller(20000, 'localhost');
|
||||
caller.connect().then(_ => {
|
||||
caller.info().then(console.log);
|
||||
caller["HelloWorldRPCGroup"].echo("x").then(console.log);
|
||||
});
|
||||
Reference in New Issue
Block a user