|
@@ -10,7 +10,7 @@ import { SubscriptionResponse } from "./Types";
|
10
|
10
|
* @param owner The owning RPC group's name
|
11
|
11
|
* @throws Error on RPC without name property
|
12
|
12
|
*/
|
13
|
|
-export const rpcToRpcinfo = <SubResT = {}>(rpc : T.RPC<any, any, SubResT>, owner: string, errorHandler: T.ErrorHandler, sesame?:T.SesameFunction):T.RpcInfo => {
|
|
13
|
+export const rpcToRpcinfo = <SubResT = {}>(socket: I.Socket, rpc : T.RPC<any, any, SubResT>, owner: string, errorHandler: T.ErrorHandler, sesame?:T.SesameFunction):T.RpcInfo => {
|
14
|
14
|
switch (typeof rpc){
|
15
|
15
|
case "object":
|
16
|
16
|
if(rpc['call']){
|
|
@@ -19,7 +19,7 @@ export const rpcToRpcinfo = <SubResT = {}>(rpc : T.RPC<any, any, SubResT>, owner
|
19
|
19
|
argNames: extractArgs(rpc['call']),
|
20
|
20
|
type: "Call",
|
21
|
21
|
name: rpc.name,
|
22
|
|
- call: sesame?async (_sesame, ...args) => {if(sesame(_sesame)) return await rpc['call'].apply({}, args); throw makeError(rpc.name)}:rpc['call'], // check & remove sesame
|
|
22
|
+ call: sesame?async (_sesame, ...args) => {if(sesame(_sesame)) return await rpc['call'].apply({}, args); socket.destroy()}:rpc['call'], // check & remove sesame
|
23
|
23
|
}
|
24
|
24
|
}else{
|
25
|
25
|
const generator = hookGenerator(<T.HookRPC<any, any, any>>rpc, errorHandler, sesame)
|
|
@@ -37,7 +37,7 @@ RPC did not provide a name.
|
37
|
37
|
\nUse 'funtion name(..){ .. }' syntax instead.
|
38
|
38
|
\n
|
39
|
39
|
\n<------------OFFENDING RPC:
|
40
|
|
-\n`+rpc.toString()+`
|
|
40
|
+\n${rpc.toString()}
|
41
|
41
|
\n>------------OFFENDING RPC`)
|
42
|
42
|
return {
|
43
|
43
|
owner : owner,
|
|
@@ -61,7 +61,7 @@ export function rpcHooker<SubResT = {}>(socket: I.Socket, exporter:I.RPCExporter
|
61
|
61
|
const RPCs = [...exporter.exportRPCs()]
|
62
|
62
|
|
63
|
63
|
|
64
|
|
- return RPCs.map(rpc => rpcToRpcinfo(rpc, owner, errorHandler, sesame))
|
|
64
|
+ return RPCs.map(rpc => rpcToRpcinfo(socket, rpc, owner, errorHandler, sesame))
|
65
|
65
|
.map(info => {
|
66
|
66
|
const suffix = makeUnique?"-"+uuidv4().substr(0,4):""
|
67
|
67
|
const ret:any = info
|
|
@@ -111,41 +111,33 @@ export function stripAfterEquals(str:string):string{
|
111
|
111
|
* @returns A {@link HookFunction}
|
112
|
112
|
*/
|
113
|
113
|
const hookGenerator = (rpc:T.HookRPC<any, any, any>, errorHandler: T.ErrorHandler, sesameFn?: T.SesameFunction): T.HookInfo['generator'] => {
|
114
|
|
- const argsArr = extractArgs(rpc.hook)
|
|
114
|
+ let argsArr = extractArgs(rpc.hook)
|
115
|
115
|
argsArr.pop() //remove 'callback' from the end
|
116
|
|
- const argsStr = argsArr.join(',')
|
117
|
|
-
|
118
|
|
- if(sesameFn){
|
119
|
|
- const args = ['sesame', ...argsArr].join(',')
|
120
|
|
- return eval(`(socket) => async (`+args+`) => {
|
121
|
|
- try{
|
122
|
|
- if(!sesameFn(sesame)) return
|
123
|
|
- const res = await rpc.hook(`+argsStr+(argsStr.length!==0?',':'')+` (...cbargs) => {
|
124
|
|
- if(rpc.onCallback) rpc.onCallback.apply({}, cbargs)
|
125
|
|
- socket.call.apply(socket, [res.uuid, ...cbargs])
|
126
|
|
- })
|
127
|
|
- return res
|
128
|
|
- }catch(e){
|
129
|
|
- errorHandler(socket)(e, rpc.name, [`+args+`])
|
130
|
|
- }
|
131
|
|
- }`)
|
132
|
|
- }
|
133
|
|
- const args = argsArr.join(',')
|
134
|
|
- return eval(`(socket) => async (`+args+`) => {
|
|
116
|
+ let callArgs = argsArr.join(',')
|
|
117
|
+
|
|
118
|
+ const args = sesameFn?(['sesame', ...argsArr].join(','))
|
|
119
|
+ :callArgs
|
|
120
|
+
|
|
121
|
+ callArgs = appendComma(callArgs)
|
|
122
|
+
|
|
123
|
+ //note rpc.hook is the associated RPC, not a socket.hook
|
|
124
|
+ return eval(`
|
|
125
|
+ (socket) => async (${args}) => {
|
135
|
126
|
try{
|
136
|
|
- const res = await rpc.hook(`+args+(args.length!==0?',':'')+` (...cbargs) => {
|
|
127
|
+ if(sesameFn && !sesameFn(sesame)) return
|
|
128
|
+ const res = await rpc.hook(${callArgs} (...cbargs) => {
|
137
|
129
|
if(rpc.onCallback) rpc.onCallback.apply({}, cbargs)
|
138
|
130
|
socket.call.apply(socket, [res.uuid, ...cbargs])
|
139
|
131
|
})
|
140
|
132
|
return res
|
141
|
133
|
}catch(e){
|
142
|
|
- errorHandler(socket)(e, rpc.name, [`+args+`])
|
|
134
|
+ errorHandler(socket)(e, ${rpc.name}, [${args}])
|
143
|
135
|
}
|
144
|
136
|
}`)
|
145
|
137
|
}
|
146
|
138
|
|
147
|
139
|
const makeError = (callName: string) => {
|
148
|
|
- return new Error("Unhandled Promise rejection: Call not found: "+callName+". ; Zone: <root> ; Task: Promise.then ; Value: Error: Call not found: "+callName)
|
|
140
|
+ return new Error("Call not found: "+callName+". ; Zone: <root> ; Task: Promise.then ; Value: Error: Call not found: "+callName)
|
149
|
141
|
}
|
150
|
142
|
|
151
|
143
|
/**
|
|
@@ -179,3 +171,8 @@ export function makeSesameFunction (sesame : T.SesameFunction | string) : T.Sesa
|
179
|
171
|
return testSesame === sesame
|
180
|
172
|
}
|
181
|
173
|
}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+export function appendComma(s?:string):string{
|
|
177
|
+ return s?`'${s}',`:""
|
|
178
|
+}
|