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,19 +149,14 @@ export class RPCSocket implements I.Socket{
149 149
         const argParams = fnArgs.map(stripAfterEquals).join(",")
150 150
 
151 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 153
                 const r = await this.socket.call("`+fnName+`", `+argParams+`)
157 154
                 if(r && r.result === 'Success'){
158 155
                     this.socket.hook(r.uuid, callback)
159 156
                 }
160 157
                 return r
161 158
             } )()` )
162
-            return f
163 159
         }else{
164
-
165 160
             return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
166 161
                 const r = await this.socket.call("`+fnName+`", "`+sesame+`", `+argParams+`)
167 162
                 if(r && r.result === 'Success'){

+ 1
- 1
src/Utils.ts View File

@@ -20,7 +20,7 @@ export const rpcToRpcinfo = <SubResT = {}>(rpc : T.RPC<any, any, SubResT>, owner
20 20
                     argNames: extractArgs(rpc['call']),
21 21
                     type: "Call",
22 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 25
             }else{
26 26
                 const generator = hookGenerator(<T.HookRPC<any, any, any>>rpc, sesame)

+ 61
- 13
test/Test.ts View File

@@ -6,7 +6,7 @@ import * as uuidv4 from "uuid/v4"
6 6
 const add = (...args:number[]) => {return args.reduce((a,b)=>a+b, 0)}
7 7
 function makeServer(){
8 8
     let subcallback
9
-    return new RPCServer<{ topic: string }>(20000, [{
9
+    return new RPCServer<{ topic: string }>(21000, [{
10 10
         name: "test",
11 11
         exportRPCs: () => [
12 12
             {
@@ -59,26 +59,26 @@ describe('RPCServer', () => {
59 59
         
60 60
         const echo = (x) => x
61 61
         
62
-        const server = new RPCServer(20003, [{
62
+        const server = new RPCServer(21003, [{
63 63
             name: 'HelloWorldRPCGroup',
64 64
             exportRPCs: ()  => [
65 65
                 echo, //named function variable
66 66
                 function echof(x){ return x }, //named function
67 67
                 {
68 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 76
         client.connect().then(async () => {
77 77
             const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
78 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 82
                 client.destroy()
83 83
                 server.destroy()
84 84
                 done()
@@ -108,7 +108,7 @@ describe('RPCSocket', () => {
108 108
 
109 109
     before(async() => {
110 110
         server = makeServer()
111
-        client = new RPCSocket(20000, "localhost")
111
+        client = new RPCSocket(21000, "localhost")
112 112
         return await client.connect()
113 113
     })
114 114
 
@@ -177,7 +177,7 @@ describe('It should do unhook', () => {
177 177
     let server: RPCServer<{topic: string}> 
178 178
 
179 179
     before(async() => {
180
-        server = new RPCServer<{ topic: string }>(20000, [{
180
+        server = new RPCServer<{ topic: string }>(21000, [{
181 181
             name: "test",
182 182
             exportRPCs: () => [{
183 183
                 name: 'subscribe',
@@ -198,7 +198,7 @@ describe('It should do unhook', () => {
198 198
             closeHandler: (socket) => { console.log("closeHandler OK") },
199 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 202
         return await client.connect()
203 203
     })
204 204
 
@@ -240,7 +240,7 @@ describe('Sesame should unlock the socket', () => {
240 240
     let cb = (...args) => {}
241 241
 
242 242
     before(async() => {
243
-        server = new RPCServer(20004, [{
243
+        server = new RPCServer(21004, [{
244 244
             name: "test",
245 245
             exportRPCs: () => [
246 246
                 {
@@ -255,11 +255,12 @@ describe('Sesame should unlock the socket', () => {
255 255
                     }
256 256
                 },
257 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 261
             sesame: (_sesame) => _sesame === 'sesame!' 
261 262
         })
262
-        const sock = new RPCSocket(20004, "localhost")
263
+        const sock = new RPCSocket(21004, "localhost")
263 264
         client = await sock.connect<SesameTestIfc>('sesame!')
264 265
     })
265 266
 
@@ -272,8 +273,15 @@ describe('Sesame should unlock the socket', () => {
272 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 283
     it('should not work without sesame', (done) => {
276
-        const sock = new RPCSocket(20004, "localhost")
284
+        const sock = new RPCSocket(21004, "localhost")
277 285
         sock.connect<SesameTestIfc>( /* no sesame */).then(async (c) => {
278 286
             c.test.checkCandy().then(d => {
279 287
                 if(d === candy)
@@ -299,7 +307,7 @@ describe('Sesame should unlock the socket', () => {
299 307
     })
300 308
 
301 309
     it('callback should not work without sesame', (done) => {
302
-        const sock = new RPCSocket(20004, "localhost")
310
+        const sock = new RPCSocket(21004, "localhost")
303 311
         sock.connect<SesameTestIfc>( /* no sesame */).then(async (c) => {
304 312
             c.test.subscribe((c) => {
305 313
                 console.log("CALLBACK TRIGGERED UNEXPECTED");
@@ -317,3 +325,43 @@ describe('Sesame should unlock the socket', () => {
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