tempalte strings & bugfixes
This commit is contained in:
+17
-24
@@ -4,7 +4,7 @@ import bsock = require('bsock');
|
||||
|
||||
import * as T from './Types';
|
||||
import * as I from './Interfaces';
|
||||
import { stripAfterEquals } from './Utils';
|
||||
import { stripAfterEquals, appendComma } from './Utils';
|
||||
|
||||
|
||||
|
||||
@@ -127,10 +127,9 @@ export class RPCSocket implements I.Socket{
|
||||
private callGenerator(fnName: string, fnArgs:string[], sesame?:string): T.AnyFunction{
|
||||
const headerArgs = fnArgs.join(",")
|
||||
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
||||
if(!sesame)
|
||||
return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", '+argParams+')} )()' )
|
||||
else
|
||||
return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", "'+sesame+'", '+argParams+')} )()' )
|
||||
sesame = appendComma(sesame)
|
||||
|
||||
return eval(`async (${headerArgs}) => { return await this.socket.call("${fnName}", ${sesame} ${argParams})}`)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,25 +139,19 @@ export class RPCSocket implements I.Socket{
|
||||
*/
|
||||
private frontEndHookGenerator(fnName: string, fnArgs:string[], sesame?:string): T.HookFunction{
|
||||
fnArgs.pop()
|
||||
const headerArgs = fnArgs.join(",")
|
||||
let headerArgs = fnArgs.join(",")
|
||||
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
||||
sesame = appendComma(sesame)
|
||||
headerArgs = appendComma(headerArgs)
|
||||
|
||||
if(!sesame){
|
||||
return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
|
||||
const r = await this.socket.call("`+fnName+`", `+argParams+`)
|
||||
if(r && r.result === 'Success'){
|
||||
this.socket.hook(r.uuid, callback)
|
||||
}
|
||||
return r
|
||||
} )()` )
|
||||
}else{
|
||||
return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
|
||||
const r = await this.socket.call("`+fnName+`", "`+sesame+`", `+argParams+`)
|
||||
if(r && r.result === 'Success'){
|
||||
this.socket.hook(r.uuid, callback)
|
||||
}
|
||||
return r
|
||||
} )()` )
|
||||
}
|
||||
return eval( `
|
||||
async (${headerArgs} callback) => {
|
||||
const r = await this.socket.call("${fnName}", ${sesame} ${argParams})
|
||||
if(r && r.result === 'Success'){
|
||||
this.socket.hook(r.uuid, callback)
|
||||
this.socket.on('error', e => this.socket.unhook(r.uuid))
|
||||
}
|
||||
return r
|
||||
}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+23
-26
@@ -10,7 +10,7 @@ import { SubscriptionResponse } from "./Types";
|
||||
* @param owner The owning RPC group's name
|
||||
* @throws Error on RPC without name property
|
||||
*/
|
||||
export const rpcToRpcinfo = <SubResT = {}>(rpc : T.RPC<any, any, SubResT>, owner: string, errorHandler: T.ErrorHandler, sesame?:T.SesameFunction):T.RpcInfo => {
|
||||
export const rpcToRpcinfo = <SubResT = {}>(socket: I.Socket, rpc : T.RPC<any, any, SubResT>, owner: string, errorHandler: T.ErrorHandler, sesame?:T.SesameFunction):T.RpcInfo => {
|
||||
switch (typeof rpc){
|
||||
case "object":
|
||||
if(rpc['call']){
|
||||
@@ -19,7 +19,7 @@ export const rpcToRpcinfo = <SubResT = {}>(rpc : T.RPC<any, any, SubResT>, owner
|
||||
argNames: extractArgs(rpc['call']),
|
||||
type: "Call",
|
||||
name: rpc.name,
|
||||
call: sesame?async (_sesame, ...args) => {if(sesame(_sesame)) return await rpc['call'].apply({}, args); throw makeError(rpc.name)}:rpc['call'], // check & remove sesame
|
||||
call: sesame?async (_sesame, ...args) => {if(sesame(_sesame)) return await rpc['call'].apply({}, args); socket.destroy()}:rpc['call'], // check & remove sesame
|
||||
}
|
||||
}else{
|
||||
const generator = hookGenerator(<T.HookRPC<any, any, any>>rpc, errorHandler, sesame)
|
||||
@@ -37,7 +37,7 @@ RPC did not provide a name.
|
||||
\nUse 'funtion name(..){ .. }' syntax instead.
|
||||
\n
|
||||
\n<------------OFFENDING RPC:
|
||||
\n`+rpc.toString()+`
|
||||
\n${rpc.toString()}
|
||||
\n>------------OFFENDING RPC`)
|
||||
return {
|
||||
owner : owner,
|
||||
@@ -61,7 +61,7 @@ export function rpcHooker<SubResT = {}>(socket: I.Socket, exporter:I.RPCExporter
|
||||
const RPCs = [...exporter.exportRPCs()]
|
||||
|
||||
|
||||
return RPCs.map(rpc => rpcToRpcinfo(rpc, owner, errorHandler, sesame))
|
||||
return RPCs.map(rpc => rpcToRpcinfo(socket, rpc, owner, errorHandler, sesame))
|
||||
.map(info => {
|
||||
const suffix = makeUnique?"-"+uuidv4().substr(0,4):""
|
||||
const ret:any = info
|
||||
@@ -111,41 +111,33 @@ export function stripAfterEquals(str:string):string{
|
||||
* @returns A {@link HookFunction}
|
||||
*/
|
||||
const hookGenerator = (rpc:T.HookRPC<any, any, any>, errorHandler: T.ErrorHandler, sesameFn?: T.SesameFunction): T.HookInfo['generator'] => {
|
||||
const argsArr = extractArgs(rpc.hook)
|
||||
let argsArr = extractArgs(rpc.hook)
|
||||
argsArr.pop() //remove 'callback' from the end
|
||||
const argsStr = argsArr.join(',')
|
||||
let callArgs = argsArr.join(',')
|
||||
|
||||
if(sesameFn){
|
||||
const args = ['sesame', ...argsArr].join(',')
|
||||
return eval(`(socket) => async (`+args+`) => {
|
||||
try{
|
||||
if(!sesameFn(sesame)) return
|
||||
const res = await rpc.hook(`+argsStr+(argsStr.length!==0?',':'')+` (...cbargs) => {
|
||||
if(rpc.onCallback) rpc.onCallback.apply({}, cbargs)
|
||||
socket.call.apply(socket, [res.uuid, ...cbargs])
|
||||
})
|
||||
return res
|
||||
}catch(e){
|
||||
errorHandler(socket)(e, rpc.name, [`+args+`])
|
||||
}
|
||||
}`)
|
||||
}
|
||||
const args = argsArr.join(',')
|
||||
return eval(`(socket) => async (`+args+`) => {
|
||||
const args = sesameFn?(['sesame', ...argsArr].join(','))
|
||||
:callArgs
|
||||
|
||||
callArgs = appendComma(callArgs)
|
||||
|
||||
//note rpc.hook is the associated RPC, not a socket.hook
|
||||
return eval(`
|
||||
(socket) => async (${args}) => {
|
||||
try{
|
||||
const res = await rpc.hook(`+args+(args.length!==0?',':'')+` (...cbargs) => {
|
||||
if(sesameFn && !sesameFn(sesame)) return
|
||||
const res = await rpc.hook(${callArgs} (...cbargs) => {
|
||||
if(rpc.onCallback) rpc.onCallback.apply({}, cbargs)
|
||||
socket.call.apply(socket, [res.uuid, ...cbargs])
|
||||
})
|
||||
return res
|
||||
}catch(e){
|
||||
errorHandler(socket)(e, rpc.name, [`+args+`])
|
||||
errorHandler(socket)(e, ${rpc.name}, [${args}])
|
||||
}
|
||||
}`)
|
||||
}
|
||||
|
||||
const makeError = (callName: string) => {
|
||||
return new Error("Unhandled Promise rejection: Call not found: "+callName+". ; Zone: <root> ; Task: Promise.then ; Value: Error: Call not found: "+callName)
|
||||
return new Error("Call not found: "+callName+". ; Zone: <root> ; Task: Promise.then ; Value: Error: Call not found: "+callName)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,3 +171,8 @@ export function makeSesameFunction (sesame : T.SesameFunction | string) : T.Sesa
|
||||
return testSesame === sesame
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function appendComma(s?:string):string{
|
||||
return s?`'${s}',`:""
|
||||
}
|
||||
Reference in New Issue
Block a user