|
@@ -10,7 +10,8 @@ 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, sesame?:string):T.RpcInfo => {
|
|
13
|
+export const rpcToRpcinfo = <SubResT = {}>(rpc : T.RPC<any, any, SubResT>, owner: string, sesame?:T.SesameFunction):T.RpcInfo => {
|
|
14
|
+
|
14
|
15
|
switch (typeof rpc){
|
15
|
16
|
case "object":
|
16
|
17
|
if(rpc['call']){
|
|
@@ -19,7 +20,7 @@ export const rpcToRpcinfo = <SubResT = {}>(rpc : T.RPC<any, any, SubResT>, owner
|
19
|
20
|
argNames: extractArgs(rpc['call']),
|
20
|
21
|
type: "Call",
|
21
|
22
|
name: rpc.name,
|
22
|
|
- call: sesame?async (_sesame, ...args) => {if(sesame === _sesame) return await rpc['call'].apply({}, args)}:rpc['call'],
|
|
23
|
+ call: sesame?async (_sesame, ...args) => {if(sesame(_sesame)) return await rpc['call'].apply({}, args)}:rpc['call'], // check & remove sesame
|
23
|
24
|
}
|
24
|
25
|
}else{
|
25
|
26
|
const generator = hookGenerator(<T.HookRPC<any, any, any>>rpc, sesame)
|
|
@@ -44,7 +45,7 @@ RPC did not provide a name.
|
44
|
45
|
argNames: extractArgs(rpc),
|
45
|
46
|
type: "Call",
|
46
|
47
|
name: rpc.name,
|
47
|
|
- call: async(...args) => (<Function>rpc).apply({}, args),
|
|
48
|
+ call: sesame?async (_sesame, ...args) => {if(sesame(_sesame)) return await rpc.apply({}, args)}:rpc, // check & remove sesame
|
48
|
49
|
}
|
49
|
50
|
}
|
50
|
51
|
throw new Error("Bad socketIORPC type "+ typeof rpc)
|
|
@@ -56,7 +57,7 @@ RPC did not provide a name.
|
56
|
57
|
* @param exporter The exporter
|
57
|
58
|
* @param makeUnique @default true Attach a suffix to RPC names
|
58
|
59
|
*/
|
59
|
|
-export function rpcHooker<SubResT = {}>(socket: I.Socket, exporter:I.RPCExporter<any, any, SubResT>, sesame?:string, makeUnique = true):T.ExtendedRpcInfo[]{
|
|
60
|
+export function rpcHooker<SubResT = {}>(socket: I.Socket, exporter:I.RPCExporter<any, any, SubResT>, sesame?:T.SesameFunction, makeUnique = true):T.ExtendedRpcInfo[]{
|
60
|
61
|
const owner = exporter.name
|
61
|
62
|
const RPCs = [...exporter.exportRPCs()]
|
62
|
63
|
|
|
@@ -85,15 +86,15 @@ export function rpcHooker<SubResT = {}>(socket: I.Socket, exporter:I.RPCExporter
|
85
|
86
|
* @param rpc The RPC to transform
|
86
|
87
|
* @returns A {@link HookFunction}
|
87
|
88
|
*/
|
88
|
|
-const hookGenerator = (rpc:T.HookRPC<any, any, any>, sesame?:string): T.HookInfo['generator'] => {
|
|
89
|
+const hookGenerator = (rpc:T.HookRPC<any, any, any>, sesame?:T.SesameFunction): T.HookInfo['generator'] => {
|
89
|
90
|
const argsArr = extractArgs(rpc.hook)
|
90
|
91
|
if(sesame){
|
91
|
92
|
const _sesame = argsArr.shift()
|
92
|
|
- if(sesame !== _sesame){
|
|
93
|
+ if(!sesame(_sesame!)){
|
93
|
94
|
throw new Error('Bad sesame')
|
94
|
95
|
}
|
95
|
96
|
}
|
96
|
|
- argsArr.pop()
|
|
97
|
+ argsArr.pop() //remove 'callback' from the end
|
97
|
98
|
const args = argsArr.join(',')
|
98
|
99
|
|
99
|
100
|
return eval(`(socket) => async (`+args+`) => {
|
|
@@ -131,4 +132,14 @@ export function makeSubResponse<T extends {} = {}>(extension:T):SubscriptionResp
|
131
|
132
|
uuid: uuidv4(),
|
132
|
133
|
...extension
|
133
|
134
|
}
|
|
135
|
+}
|
|
136
|
+
|
|
137
|
+export function makeSesameFunction (sesame : T.SesameFunction | string) : T.SesameFunction {
|
|
138
|
+ if(typeof sesame === 'function'){
|
|
139
|
+ return sesame
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ return (testSesame : string) => {
|
|
143
|
+ return testSesame === sesame
|
|
144
|
+ }
|
134
|
145
|
}
|