Browse Source

some cleanup

master
peter 4 years ago
parent
commit
cbefba6625
3 changed files with 63 additions and 20 deletions
  1. 1
    6
      src/Frontend.ts
  2. 1
    1
      src/Utils.ts
  3. 61
    13
      test/Test.ts

+ 1
- 6
src/Frontend.ts View File

149
         const argParams = fnArgs.map(stripAfterEquals).join(",")
149
         const argParams = fnArgs.map(stripAfterEquals).join(",")
150
 
150
 
151
         if(!sesame){
151
         if(!sesame){
152
-            const headerArgs = fnArgs.join(",")
153
-            const argParams = fnArgs.map(stripAfterEquals).join(",")
154
-
155
-            const f = eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
152
+            return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
156
                 const r = await this.socket.call("`+fnName+`", `+argParams+`)
153
                 const r = await this.socket.call("`+fnName+`", `+argParams+`)
157
                 if(r && r.result === 'Success'){
154
                 if(r && r.result === 'Success'){
158
                     this.socket.hook(r.uuid, callback)
155
                     this.socket.hook(r.uuid, callback)
159
                 }
156
                 }
160
                 return r
157
                 return r
161
             } )()` )
158
             } )()` )
162
-            return f
163
         }else{
159
         }else{
164
-
165
             return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
160
             return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
166
                 const r = await this.socket.call("`+fnName+`", "`+sesame+`", `+argParams+`)
161
                 const r = await this.socket.call("`+fnName+`", "`+sesame+`", `+argParams+`)
167
                 if(r && r.result === 'Success'){
162
                 if(r && r.result === 'Success'){

+ 1
- 1
src/Utils.ts View File

20
                     argNames: extractArgs(rpc['call']),
20
                     argNames: extractArgs(rpc['call']),
21
                     type: "Call",
21
                     type: "Call",
22
                     name: rpc.name,
22
                     name: rpc.name,
23
-                    call: sesame?async (_sesame, ...args) => {if(sesame(_sesame)) return await rpc['call'].apply({}, args)}:rpc['call'], // check & remove sesame 
23
+                    call: sesame?async (_sesame, ...args) => {if(sesame(_sesame)) return await rpc['call'].apply({}, args); throw new Error('Bad sesame')}:rpc['call'], // check & remove sesame 
24
                 }
24
                 }
25
             }else{
25
             }else{
26
                 const generator = hookGenerator(<T.HookRPC<any, any, any>>rpc, sesame)
26
                 const generator = hookGenerator(<T.HookRPC<any, any, any>>rpc, sesame)

+ 61
- 13
test/Test.ts View File

6
 const add = (...args:number[]) => {return args.reduce((a,b)=>a+b, 0)}
6
 const add = (...args:number[]) => {return args.reduce((a,b)=>a+b, 0)}
7
 function makeServer(){
7
 function makeServer(){
8
     let subcallback
8
     let subcallback
9
-    return new RPCServer<{ topic: string }>(20000, [{
9
+    return new RPCServer<{ topic: string }>(21000, [{
10
         name: "test",
10
         name: "test",
11
         exportRPCs: () => [
11
         exportRPCs: () => [
12
             {
12
             {
59
         
59
         
60
         const echo = (x) => x
60
         const echo = (x) => x
61
         
61
         
62
-        const server = new RPCServer(20003, [{
62
+        const server = new RPCServer(21003, [{
63
             name: 'HelloWorldRPCGroup',
63
             name: 'HelloWorldRPCGroup',
64
             exportRPCs: ()  => [
64
             exportRPCs: ()  => [
65
                 echo, //named function variable
65
                 echo, //named function variable
66
                 function echof(x){ return x }, //named function
66
                 function echof(x){ return x }, //named function
67
                 {
67
                 {
68
                     name: 'echoExplicit', //describing object
68
                     name: 'echoExplicit', //describing object
69
-                    call: async (x) => x
69
+                    call: async (x,y,z) => [x,y,z]
70
                 }
70
                 }
71
             ]
71
             ]
72
         }])
72
         }])
73
 
73
 
74
-        const client = new RPCSocket(20003, 'localhost')
74
+        const client = new RPCSocket(21003, 'localhost')
75
 
75
 
76
         client.connect().then(async () => {
76
         client.connect().then(async () => {
77
             const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
77
             const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
78
             const r1 = await client['HelloWorldRPCGroup'].echof('World')
78
             const r1 = await client['HelloWorldRPCGroup'].echof('World')
79
-            const r2 = await client['HelloWorldRPCGroup'].echoExplicit('RPC!')
79
+            const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R','P','C!')
80
 
80
 
81
-            if(r0 === 'Hello' && r1 === 'World' && r2 ==='RPC!'){
81
+            if(r0 === 'Hello' && r1 === 'World' && r2.join('') ==='RPC!'){
82
                 client.destroy()
82
                 client.destroy()
83
                 server.destroy()
83
                 server.destroy()
84
                 done()
84
                 done()
108
 
108
 
109
     before(async() => {
109
     before(async() => {
110
         server = makeServer()
110
         server = makeServer()
111
-        client = new RPCSocket(20000, "localhost")
111
+        client = new RPCSocket(21000, "localhost")
112
         return await client.connect()
112
         return await client.connect()
113
     })
113
     })
114
 
114
 
177
     let server: RPCServer<{topic: string}> 
177
     let server: RPCServer<{topic: string}> 
178
 
178
 
179
     before(async() => {
179
     before(async() => {
180
-        server = new RPCServer<{ topic: string }>(20000, [{
180
+        server = new RPCServer<{ topic: string }>(21000, [{
181
             name: "test",
181
             name: "test",
182
             exportRPCs: () => [{
182
             exportRPCs: () => [{
183
                 name: 'subscribe',
183
                 name: 'subscribe',
198
             closeHandler: (socket) => { console.log("closeHandler OK") },
198
             closeHandler: (socket) => { console.log("closeHandler OK") },
199
             errorHandler: (socket, err) => { console.error("errorHandler OK SO YOU SHOULDN'T SEE THIS"); throw err }
199
             errorHandler: (socket, err) => { console.error("errorHandler OK SO YOU SHOULDN'T SEE THIS"); throw err }
200
         })
200
         })
201
-        client = new RPCSocket(20000, "localhost")
201
+        client = new RPCSocket(21000, "localhost")
202
         return await client.connect()
202
         return await client.connect()
203
     })
203
     })
204
 
204
 
240
     let cb = (...args) => {}
240
     let cb = (...args) => {}
241
 
241
 
242
     before(async() => {
242
     before(async() => {
243
-        server = new RPCServer(20004, [{
243
+        server = new RPCServer(21004, [{
244
             name: "test",
244
             name: "test",
245
             exportRPCs: () => [
245
             exportRPCs: () => [
246
                 {
246
                 {
255
                     }
255
                     }
256
                 },
256
                 },
257
                 async function checkCandy():Promise<string> { cb(candy); cb=()=>{}; return candy },
257
                 async function checkCandy():Promise<string> { cb(candy); cb=()=>{}; return candy },
258
+                async function manyParams(a,b,c,d) {return [a,b,c,d]}
258
             ]}
259
             ]}
259
         ],{
260
         ],{
260
             sesame: (_sesame) => _sesame === 'sesame!' 
261
             sesame: (_sesame) => _sesame === 'sesame!' 
261
         })
262
         })
262
-        const sock = new RPCSocket(20004, "localhost")
263
+        const sock = new RPCSocket(21004, "localhost")
263
         client = await sock.connect<SesameTestIfc>('sesame!')
264
         client = await sock.connect<SesameTestIfc>('sesame!')
264
     })
265
     })
265
 
266
 
272
         client.test.checkCandy().then(c => done())
273
         client.test.checkCandy().then(c => done())
273
     })
274
     })
274
 
275
 
276
+    it('should work with multiple params', (done) => {
277
+        client.test['manyParams']('a','b','c','d').then(c => {
278
+            if(c[0] == 'a' && c[1] === 'b' && c[2] === 'c'  && c[3] === 'd')
279
+                done()
280
+        })
281
+    })
282
+
275
     it('should not work without sesame', (done) => {
283
     it('should not work without sesame', (done) => {
276
-        const sock = new RPCSocket(20004, "localhost")
284
+        const sock = new RPCSocket(21004, "localhost")
277
         sock.connect<SesameTestIfc>( /* no sesame */).then(async (c) => {
285
         sock.connect<SesameTestIfc>( /* no sesame */).then(async (c) => {
278
             c.test.checkCandy().then(d => {
286
             c.test.checkCandy().then(d => {
279
                 if(d === candy)
287
                 if(d === candy)
299
     })
307
     })
300
 
308
 
301
     it('callback should not work without sesame', (done) => {
309
     it('callback should not work without sesame', (done) => {
302
-        const sock = new RPCSocket(20004, "localhost")
310
+        const sock = new RPCSocket(21004, "localhost")
303
         sock.connect<SesameTestIfc>( /* no sesame */).then(async (c) => {
311
         sock.connect<SesameTestIfc>( /* no sesame */).then(async (c) => {
304
             c.test.subscribe((c) => {
312
             c.test.subscribe((c) => {
305
                 console.log("CALLBACK TRIGGERED UNEXPECTED");
313
                 console.log("CALLBACK TRIGGERED UNEXPECTED");
317
         })
325
         })
318
     })
326
     })
319
 })
327
 })
328
+
329
+/*
330
+class myServer{
331
+    server = new RPCServer(21004, [ {
332
+        name: 'createUser' as 'createUser',
333
+        exportRPCs: () => [{
334
+            name: 'createUser' as 'createUser',
335
+            call: this.createUser
336
+        }]
337
+    }
338
+    ])
339
+
340
+    createUser = async( user: {a:any,b:any}) => {
341
+        console.log(user)
342
+        return user
343
+    }
344
+}
345
+
346
+describe('Should pass the createUser edge case', ()=>{
347
+    let server
348
+
349
+    before(()=>{
350
+        server = new myServer()
351
+    })
352
+
353
+    after(()=>{
354
+        server.server.destroy()
355
+    })
356
+
357
+    it("should work", async ()=>{
358
+        let sock = new RPCSocket(21004, 'localhost')
359
+        let client = await sock.connect()
360
+        client["createUser"]["createUser"]({
361
+            a:'a',
362
+            b:'b'
363
+        }).then(console.log)
364
+    })
365
+    
366
+})
367
+*/

Loading…
Cancel
Save