|
@@ -17,8 +17,8 @@ function stripAfterEquals(str:string):string{
|
17
|
17
|
* A websocket-on-steroids with built-in RPC capabilities
|
18
|
18
|
*/
|
19
|
19
|
export class RPCSocket implements I.Socket{
|
20
|
|
- static async makeSocket<T extends T.RPCInterface= T.RPCInterface>(port:number, server: string, tls: boolean = false): Promise<RPCSocket & T.RPCInterface<T>> {
|
21
|
|
- const socket = <RPCSocket & T> new RPCSocket(port, server, tls)
|
|
20
|
+ static async makeSocket<T extends T.RPCInterface= T.RPCInterface>(port:number, server: string, conf?:T.SocketConf): Promise<RPCSocket & T.RPCInterface<T>> {
|
|
21
|
+ const socket = <RPCSocket & T> new RPCSocket(port, server, conf)
|
22
|
22
|
return await socket.connect<T>()
|
23
|
23
|
}
|
24
|
24
|
|
|
@@ -30,7 +30,7 @@ export class RPCSocket implements I.Socket{
|
30
|
30
|
* @param server Server address
|
31
|
31
|
* @param tls @default false use TLS
|
32
|
32
|
*/
|
33
|
|
- constructor(public port:number, private server: string, private tls: boolean = false){
|
|
33
|
+ constructor(public port:number, private server: string, private conf:T.SocketConf = { tls: false }){
|
34
|
34
|
Object.defineProperty(this, 'socket', {value: undefined, writable: true})
|
35
|
35
|
}
|
36
|
36
|
|
|
@@ -95,18 +95,18 @@ export class RPCSocket implements I.Socket{
|
95
|
95
|
/**
|
96
|
96
|
* Connects to the server and attaches available RPCs to this object
|
97
|
97
|
*/
|
98
|
|
- public async connect<T extends T.RPCInterface= T.RPCInterface>() : Promise<RPCSocket & T.RPCInterface<T>>{
|
99
|
|
- this.socket = await bsock.connect(this.port, this.server, this.tls)
|
|
98
|
+ public async connect<T extends T.RPCInterface= T.RPCInterface>( sesame?: string ) : Promise<RPCSocket & T.RPCInterface<T>>{
|
|
99
|
+ this.socket = await bsock.connect(this.port, this.server, this.conf.tls?this.conf.tls:false)
|
100
|
100
|
|
101
|
101
|
const info:T.ExtendedRpcInfo[] = await this.info()
|
102
|
102
|
info.forEach(i => {
|
103
|
103
|
let f: any
|
104
|
104
|
switch (i.type) {
|
105
|
105
|
case 'Call':
|
106
|
|
- f = this.callGenerator(i.uniqueName, i.argNames)
|
|
106
|
+ f = this.callGenerator(i.uniqueName, i.argNames, sesame)
|
107
|
107
|
break
|
108
|
108
|
case 'Hook':
|
109
|
|
- f = this.hookGenerator(i.uniqueName, i.argNames)
|
|
109
|
+ f = this.hookGenerator(i.uniqueName, i.argNames, sesame)
|
110
|
110
|
break
|
111
|
111
|
}
|
112
|
112
|
if(this[i.owner] == null)
|
|
@@ -129,10 +129,13 @@ export class RPCSocket implements I.Socket{
|
129
|
129
|
* @param fnName The function name
|
130
|
130
|
* @param fnArgs A string-list of parameters
|
131
|
131
|
*/
|
132
|
|
- private callGenerator(fnName: string, fnArgs:string[]): T.AnyFunction{
|
|
132
|
+ private callGenerator(fnName: string, fnArgs:string[], sesame?:string): T.AnyFunction{
|
133
|
133
|
const headerArgs = fnArgs.join(",")
|
134
|
134
|
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
135
|
|
- return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", '+argParams+')} )()' )
|
|
135
|
+ if(!sesame)
|
|
136
|
+ return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", '+argParams+')} )()' )
|
|
137
|
+ else
|
|
138
|
+ return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", "'+sesame+'", '+argParams+')} )()' )
|
136
|
139
|
}
|
137
|
140
|
|
138
|
141
|
/**
|
|
@@ -140,15 +143,25 @@ export class RPCSocket implements I.Socket{
|
140
|
143
|
* @param fnName The function name
|
141
|
144
|
* @param fnArgs A string-list of parameters
|
142
|
145
|
*/
|
143
|
|
- private hookGenerator(fnName: string, fnArgs:string[]): T.HookFunction{
|
|
146
|
+ private hookGenerator(fnName: string, fnArgs:string[], sesame?:string): T.HookFunction{
|
144
|
147
|
const headerArgs = fnArgs.join(",")
|
145
|
148
|
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
146
|
|
- return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
|
147
|
|
- const r = await this.socket.call("`+fnName+`", `+argParams+`)
|
148
|
|
- if(r.result === 'Success'){
|
149
|
|
- this.socket.hook(r.uuid, callback)
|
150
|
|
- }
|
151
|
|
- return r
|
152
|
|
- } )()` )
|
|
149
|
+ if(!sesame){
|
|
150
|
+ return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
|
|
151
|
+ const r = await this.socket.call("`+fnName+`", `+argParams+`)
|
|
152
|
+ if(r.result === 'Success'){
|
|
153
|
+ this.socket.hook(r.uuid, callback)
|
|
154
|
+ }
|
|
155
|
+ return r
|
|
156
|
+ } )()` )
|
|
157
|
+ }else{
|
|
158
|
+ return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
|
|
159
|
+ const r = await this.socket.call("`+fnName+`", "`+sesame+`", `+argParams+`)
|
|
160
|
+ if(r.result === 'Success'){
|
|
161
|
+ this.socket.hook(r.uuid, callback)
|
|
162
|
+ }
|
|
163
|
+ return r
|
|
164
|
+ } )()` )
|
|
165
|
+ }
|
153
|
166
|
}
|
154
|
167
|
}
|