remove unhook
This commit is contained in:
+2
-2
@@ -27,7 +27,7 @@ export class RPCServer{
|
|||||||
if(!conf.errorHandler) this.errorHandler =
|
if(!conf.errorHandler) this.errorHandler =
|
||||||
(socket:I.Socket) => (error:any) => {
|
(socket:I.Socket) => (error:any) => {
|
||||||
socket.destroy();
|
socket.destroy();
|
||||||
console.error(error)
|
console.error("Caught websocket error", String(error))
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!conf.closeHandler) this.closeHandler =
|
if(!conf.closeHandler) this.closeHandler =
|
||||||
@@ -37,7 +37,7 @@ export class RPCServer{
|
|||||||
|
|
||||||
if(!conf.connectionHandler) this.connectionHandler =
|
if(!conf.connectionHandler) this.connectionHandler =
|
||||||
(socket:I.Socket) => {
|
(socket:I.Socket) => {
|
||||||
console.log("New websocket connection in port "+socket.port)
|
console.log("New websocket connection on port "+socket.port)
|
||||||
}
|
}
|
||||||
|
|
||||||
let badRPC
|
let badRPC
|
||||||
|
|||||||
+1
-18
@@ -58,9 +58,6 @@ export class RPCSocket implements I.Socket{
|
|||||||
case 'Hook':
|
case 'Hook':
|
||||||
f = this.hookGenerator(i.uniqueName, i.argNames)
|
f = this.hookGenerator(i.uniqueName, i.argNames)
|
||||||
break
|
break
|
||||||
case 'Unhook':
|
|
||||||
f = this.unhookGenerator(i.uniqueName, i.argNames)
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
if(this[i.owner] == null)
|
if(this[i.owner] == null)
|
||||||
this[i.owner] = {}
|
this[i.owner] = {}
|
||||||
@@ -79,7 +76,7 @@ export class RPCSocket implements I.Socket{
|
|||||||
return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", '+argParams+')} )()' )
|
return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", '+argParams+')} )()' )
|
||||||
}
|
}
|
||||||
|
|
||||||
private hookGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.CallbackFunction{
|
private hookGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.HookFunction{
|
||||||
const headerArgs = fnArgs.join(",")
|
const headerArgs = fnArgs.join(",")
|
||||||
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
||||||
return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
|
return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
|
||||||
@@ -90,18 +87,4 @@ export class RPCSocket implements I.Socket{
|
|||||||
return r
|
return r
|
||||||
} )()` )
|
} )()` )
|
||||||
}
|
}
|
||||||
|
|
||||||
private unhookGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.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+`)
|
|
||||||
this.socket.unhook(`+argParams+`)
|
|
||||||
return r
|
|
||||||
} )()` )
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+10
-19
@@ -25,13 +25,9 @@ export type BaseRPC = {
|
|||||||
|
|
||||||
export type HookRPC = BaseRPC & {
|
export type HookRPC = BaseRPC & {
|
||||||
type: 'Hook'
|
type: 'Hook'
|
||||||
hook: CallbackFunction
|
hook: HookFunction
|
||||||
unhook: UnhookFunction
|
onCallback?: CallbackFunction,
|
||||||
}
|
onClose?: HookCloseFunction
|
||||||
|
|
||||||
export type UnhookRPC = BaseRPC & {
|
|
||||||
type: 'Unhook'
|
|
||||||
unhook: UnhookFunction
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CallRPC = (BaseRPC & {
|
export type CallRPC = (BaseRPC & {
|
||||||
@@ -39,7 +35,7 @@ export type CallRPC = (BaseRPC & {
|
|||||||
call: AsyncFunction
|
call: AsyncFunction
|
||||||
} ) | Function
|
} ) | Function
|
||||||
|
|
||||||
export type RPC = CallRPC | UnhookRPC | HookRPC
|
export type RPC = CallRPC | HookRPC
|
||||||
|
|
||||||
export type BaseInfo = {
|
export type BaseInfo = {
|
||||||
owner: Name,
|
owner: Name,
|
||||||
@@ -48,13 +44,7 @@ export type BaseInfo = {
|
|||||||
|
|
||||||
export type HookInfo = BaseRPC & BaseInfo & {
|
export type HookInfo = BaseRPC & BaseInfo & {
|
||||||
type: 'Hook',
|
type: 'Hook',
|
||||||
generator: (socket) => CallbackFunction
|
generator: (socket) => HookFunction
|
||||||
unhook: UnhookFunction
|
|
||||||
}
|
|
||||||
|
|
||||||
export type UnhookInfo = BaseRPC & BaseInfo & {
|
|
||||||
type: 'Unhook',
|
|
||||||
unhook: UnhookFunction
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CallInfo = BaseRPC & BaseInfo & {
|
export type CallInfo = BaseRPC & BaseInfo & {
|
||||||
@@ -62,10 +52,11 @@ export type CallInfo = BaseRPC & BaseInfo & {
|
|||||||
call: AsyncFunction
|
call: AsyncFunction
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RpcInfo = HookInfo | UnhookInfo | CallInfo
|
export type RpcInfo = HookInfo | CallInfo
|
||||||
export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
|
export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
|
||||||
|
|
||||||
export type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => I.Socket
|
export type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => I.Socket
|
||||||
export type UnhookFunction = (uid:string) => Promise<R.SuccessResponse | R.ErrorResponse>
|
export type HookCloseFunction = (res:R.SubscriptionResponse) => any
|
||||||
export type CallbackFunction = (...args) => Promise<R.SubscriptionResponse | R.ErrorResponse>
|
export type HookFunction = (...args:any[]) => Promise<R.SubscriptionResponse | R.ErrorResponse>
|
||||||
export type AsyncFunction = (...args) => Promise<any>
|
export type AsyncFunction = (...args:any[]) => Promise<any>
|
||||||
|
export type CallbackFunction = (arg: any) => void
|
||||||
+8
-21
@@ -15,14 +15,6 @@ export const rpcToRpcinfo = (rpc : T.RPC, owner: T.Owner):T.RpcInfo => {
|
|||||||
name: rpc.name,
|
name: rpc.name,
|
||||||
call: rpc.call,
|
call: rpc.call,
|
||||||
}
|
}
|
||||||
case "Unhook" :
|
|
||||||
return {
|
|
||||||
owner: owner,
|
|
||||||
argNames: extractArgs(rpc.unhook),
|
|
||||||
type: rpc.type,
|
|
||||||
name: rpc.name,
|
|
||||||
unhook: rpc.unhook,
|
|
||||||
}
|
|
||||||
case "Hook" :
|
case "Hook" :
|
||||||
const generator = hookGenerator(rpc)
|
const generator = hookGenerator(rpc)
|
||||||
return {
|
return {
|
||||||
@@ -30,8 +22,8 @@ export const rpcToRpcinfo = (rpc : T.RPC, owner: T.Owner):T.RpcInfo => {
|
|||||||
argNames: extractArgs(generator(undefined)),
|
argNames: extractArgs(generator(undefined)),
|
||||||
type: rpc.type,
|
type: rpc.type,
|
||||||
name: rpc.name,
|
name: rpc.name,
|
||||||
unhook: rpc.unhook,
|
|
||||||
generator: generator,
|
generator: generator,
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -67,9 +59,6 @@ export function rpcHooker(socket: I.Socket, exporter:I.Exporter, makeUnique = tr
|
|||||||
case "Hook":
|
case "Hook":
|
||||||
socket.hook(ret.uniqueName, info.generator(socket))
|
socket.hook(ret.uniqueName, info.generator(socket))
|
||||||
break;
|
break;
|
||||||
case "Unhook":
|
|
||||||
socket.hook(ret.uniqueName, info.unhook)
|
|
||||||
break;
|
|
||||||
case "Call":
|
case "Call":
|
||||||
socket.hook(ret.uniqueName, info.call)
|
socket.hook(ret.uniqueName, info.call)
|
||||||
break;
|
break;
|
||||||
@@ -85,13 +74,14 @@ const hookGenerator = (rpc:T.HookRPC): T.HookInfo['generator'] => {
|
|||||||
const args = argsArr.join(',')
|
const args = argsArr.join(',')
|
||||||
|
|
||||||
return eval(`(socket) => async (`+args+`) => {
|
return eval(`(socket) => async (`+args+`) => {
|
||||||
const res = await rpc.hook(`+args+(args.length!==0?',':'')+` (x) => {
|
const res = await rpc.hook(`+args+(args.length!==0?',':'')+` (...cbargs) => {
|
||||||
socket.call(res.uid, x)
|
if(rpc.onCallback) rpc.onCallback.apply({}, cbargs)
|
||||||
|
socket.call.apply(socket, [res.uid, ...cbargs])
|
||||||
})
|
})
|
||||||
if(res.result == 'Success'){
|
if(res.result == 'Success'){
|
||||||
|
if(rpc.onClose)
|
||||||
socket.on('close', async () => {
|
socket.on('close', async () => {
|
||||||
const unhookRes = await rpc.unhook(res.uid)
|
rpc.onClose(res)
|
||||||
console.log("Specific close handler for", rpc.name, res.uid, unhookRes)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
@@ -99,9 +89,6 @@ const hookGenerator = (rpc:T.HookRPC): T.HookInfo['generator'] => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const extractArgs = (f:Function):T.Arg[] => {
|
const extractArgs = (f:Function):T.Arg[] => {
|
||||||
let fn = String(f)
|
let fn
|
||||||
let args = fn.substr(0, fn.indexOf(")"))
|
return (fn = String(f)).substr(0, fn.indexOf(")")).substr(fn.indexOf("(")+1).split(",")
|
||||||
args = args.substr(fn.indexOf("(")+1)
|
|
||||||
let ret = args.split(",")
|
|
||||||
return ret
|
|
||||||
}
|
}
|
||||||
+8
-4
@@ -1,5 +1,6 @@
|
|||||||
import { RPCServer } from '../src/Backend'
|
import { RPCServer } from '../src/Backend'
|
||||||
import { SubscriptionResponse } from '../src/Responses'
|
import { SubscriptionResponse, ErrorResponse, SuccessResponse } from '../src/Responses'
|
||||||
|
import { HookRPC } from '../src/Types'
|
||||||
|
|
||||||
let subcallback
|
let subcallback
|
||||||
|
|
||||||
@@ -17,10 +18,13 @@ new RPCServer(20000, [{
|
|||||||
subcallback = callback
|
subcallback = callback
|
||||||
return new SubscriptionResponse(""+Math.random())
|
return new SubscriptionResponse(""+Math.random())
|
||||||
},
|
},
|
||||||
unhook: async (uid):Promise<any> => { subcallback = null }
|
onClose: (res:SubscriptionResponse) => {
|
||||||
|
console.log("Specific close handler for", res)
|
||||||
|
subcallback = null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
|
function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
|
||||||
function triggerCallback(message):number {return subcallback(message)},
|
function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
|
||||||
]
|
]
|
||||||
}])
|
}])
|
||||||
|
|
||||||
@@ -32,5 +36,5 @@ try{
|
|||||||
]
|
]
|
||||||
}])
|
}])
|
||||||
}catch(badRPCError){
|
}catch(badRPCError){
|
||||||
console.log("expected bad-RPC error happened: "+ !!badRPCError)
|
console.log("expected bad-RPC error happened: "+ !!badRPCError, String(badRPCError))
|
||||||
}
|
}
|
||||||
@@ -17,7 +17,7 @@ client.connect().then(async _ => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await client["HelloWorldRPCGroup"].subscribe(handler)
|
await client["HelloWorldRPCGroup"].subscribe(handler)
|
||||||
client["HelloWorldRPCGroup"].triggerCallback("test1")
|
client["HelloWorldRPCGroup"].triggerCallback("test1", "test1", "test1", )
|
||||||
client["HelloWorldRPCGroup"].triggerCallback("test2")
|
client["HelloWorldRPCGroup"].triggerCallback("test2", "test2", "test2", )
|
||||||
client["HelloWorldRPCGroup"].triggerCallback("test3")
|
client["HelloWorldRPCGroup"].triggerCallback("test3", "test3", "test3", )
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user