fix subscribe+sesame
This commit is contained in:
@@ -37,7 +37,6 @@ export class RPCServer<
|
||||
if(!conf.visibility) this.visibility = "127.0.0.1"
|
||||
|
||||
if(conf.sesame){
|
||||
console.log("Setting Sesame")
|
||||
this.sesame = U.makeSesameFunction(conf.sesame)
|
||||
}
|
||||
|
||||
|
||||
+12
-5
@@ -106,7 +106,7 @@ export class RPCSocket implements I.Socket{
|
||||
f = this.callGenerator(i.uniqueName, i.argNames, sesame)
|
||||
break
|
||||
case 'Hook':
|
||||
f = this.hookGenerator(i.uniqueName, i.argNames, sesame)
|
||||
f = this.frontEndHookGenerator(i.uniqueName, i.argNames, sesame)
|
||||
break
|
||||
}
|
||||
if(this[i.owner] == null)
|
||||
@@ -143,21 +143,28 @@ export class RPCSocket implements I.Socket{
|
||||
* @param fnName The function name
|
||||
* @param fnArgs A string-list of parameters
|
||||
*/
|
||||
private hookGenerator(fnName: string, fnArgs:string[], sesame?:string): T.HookFunction{
|
||||
private frontEndHookGenerator(fnName: string, fnArgs:string[], sesame?:string): T.HookFunction{
|
||||
fnArgs.pop()
|
||||
const headerArgs = fnArgs.join(",")
|
||||
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
||||
|
||||
if(!sesame){
|
||||
return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
|
||||
const headerArgs = fnArgs.join(",")
|
||||
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
||||
|
||||
const f = eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
|
||||
const r = await this.socket.call("`+fnName+`", `+argParams+`)
|
||||
if(r.result === 'Success'){
|
||||
if(r && r.result === 'Success'){
|
||||
this.socket.hook(r.uuid, callback)
|
||||
}
|
||||
return r
|
||||
} )()` )
|
||||
return f
|
||||
}else{
|
||||
|
||||
return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
|
||||
const r = await this.socket.call("`+fnName+`", "`+sesame+`", `+argParams+`)
|
||||
if(r.result === 'Success'){
|
||||
if(r && r.result === 'Success'){
|
||||
this.socket.hook(r.uuid, callback)
|
||||
}
|
||||
return r
|
||||
|
||||
+20
-18
@@ -82,37 +82,38 @@ export function rpcHooker<SubResT = {}>(socket: I.Socket, exporter:I.RPCExporter
|
||||
})
|
||||
}
|
||||
/**
|
||||
* Utility function to generate {@link HookFunction} from a RPC
|
||||
* Utility function to generate {@link HookFunction} from a RPC for backend
|
||||
* @param rpc The RPC to transform
|
||||
* @returns A {@link HookFunction}
|
||||
*/
|
||||
const hookGenerator = (rpc:T.HookRPC<any, any, any>, sesame?:T.SesameFunction): T.HookInfo['generator'] => {
|
||||
const hookGenerator = (rpc:T.HookRPC<any, any, any>, sesameFn?: T.SesameFunction): T.HookInfo['generator'] => {
|
||||
const argsArr = extractArgs(rpc.hook)
|
||||
if(sesame){
|
||||
const _sesame = argsArr.shift()
|
||||
if(!sesame(_sesame!)){
|
||||
throw new Error('Bad sesame')
|
||||
}
|
||||
}
|
||||
argsArr.pop() //remove 'callback' from the end
|
||||
const args = argsArr.join(',')
|
||||
const argsStr = argsArr.join(',')
|
||||
|
||||
return eval(`(socket) => async (`+args+`) => {
|
||||
if(sesameFn){
|
||||
const args = ['sesame', ...argsArr].join(',')
|
||||
const f = eval(`(socket) => async (`+args+`) => {
|
||||
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
|
||||
}`)
|
||||
return f
|
||||
}
|
||||
const args = argsArr.join(',')
|
||||
return eval(`(socket) => async (`+args+`) => {
|
||||
const res = await rpc.hook(`+args+(args.length!==0?',':'')+` (...cbargs) => {
|
||||
if(rpc.onCallback) rpc.onCallback.apply({}, cbargs)
|
||||
socket.call.apply(socket, [res.uuid, ...cbargs])
|
||||
})
|
||||
if(res.result === 'Success'){
|
||||
if(rpc.onClose){
|
||||
socket.on('close', async () => {
|
||||
rpc.onClose(res, rpc)
|
||||
})
|
||||
}
|
||||
}
|
||||
return res
|
||||
}`)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract a string list of parameters from a function
|
||||
* @param f The source function
|
||||
@@ -134,6 +135,7 @@ export function makeSubResponse<T extends {} = {}>(extension:T):SubscriptionResp
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function makeSesameFunction (sesame : T.SesameFunction | string) : T.SesameFunction {
|
||||
if(typeof sesame === 'function'){
|
||||
return sesame
|
||||
@@ -142,4 +144,4 @@ export function makeSesameFunction (sesame : T.SesameFunction | string) : T.Sesa
|
||||
return (testSesame : string) => {
|
||||
return testSesame === sesame
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user