浏览代码

fix bad rpc finding for class member for real this time

master
peter 4 年前
父节点
当前提交
a2268526af
共有 4 个文件被更改,包括 57 次插入4 次删除
  1. 2
    2
      package.json
  2. 8
    2
      src/Backend.ts
  3. 16
    0
      src/Utils.ts
  4. 31
    0
      test/Test.ts

+ 2
- 2
package.json 查看文件

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "rpclibrary",
3
-  "version": "1.6.3",
3
+  "version": "1.7.0",
4 4
   "description": "rpclibrary is a websocket on steroids!",
5 5
   "main": "./js/Index.js",
6 6
   "repository": {
@@ -30,7 +30,7 @@
30 30
     "frontend": "node js/test/TestFrontend.js",
31 31
     "build": "npm run clean && tsc && npm run webpack",
32 32
     "clean": "rm -rf js",
33
-    "test": "mocha js/test/Test.js",
33
+    "test": "npm run clean && npm run build && mocha js/test/Test.js",
34 34
     "docs": "typedoc --out docs ./src --readme ./README.md --plugin typedoc-plugin-markdown --mode file --hideBreadcrumbs --hideSources"
35 35
   },
36 36
   "license": "MIT",

+ 8
- 2
src/Backend.ts 查看文件

@@ -53,15 +53,21 @@ export class RPCServer<
53 53
             if(conf.connectionHandler) conf.connectionHandler(socket)
54 54
         }
55 55
 
56
+        exporters.forEach(U.fixNames)
57
+
56 58
         let badRPC = exporters.flatMap(ex => ex.exportRPCs()).find(rpc => !rpc.name)
57
-        if(badRPC)
58
-        throw new Error(`
59
+        if(badRPC){
60
+            console.log(badRPC);
61
+            
62
+
63
+            throw new Error(`
59 64
             RPC did not provide a name. 
60 65
             \nUse 'funtion name(..){ .. }' syntax instead.
61 66
             \n
62 67
             \n<------------OFFENDING RPC:
63 68
             \n`+badRPC.toString()+`
64 69
             \n>------------OFFENDING RPC`)
70
+        }
65 71
         this.startWebsocket()
66 72
     }
67 73
 

+ 16
- 0
src/Utils.ts 查看文件

@@ -172,4 +172,20 @@ export function makeSesameFunction (sesame : T.SesameFunction | string) : T.Sesa
172 172
 
173 173
 export function appendComma(s?:string):string{
174 174
     return s?`'${s}',`:"" 
175
+}
176
+
177
+
178
+/**
179
+ * Typescript incorrectly omits the function.name attribute for MethodDeclaration.
180
+ * This was supposedly fixed (https://github.com/microsoft/TypeScript/issues/5611) but it still is the case.
181
+ * This function sets the name value for all object members that are functions.
182
+*/
183
+export function fixNames(o:Object):void{
184
+    Object.keys(o).forEach(key => {
185
+        if(typeof o[key] === 'function' && !o[key].name){
186
+            Object.defineProperty(o[key], 'name', {
187
+                value: key
188
+            })
189
+        }
190
+    })
175 191
 }

+ 31
- 0
test/Test.ts 查看文件

@@ -4,6 +4,7 @@ import { RPCServer, RPCSocket, SubscriptionResponse, makeSubResponse } from '../
4 4
 import * as uuidv4 from "uuid/v4"
5 5
 import { doesNotReject } from "assert";
6 6
 import { Socket } from "dgram";
7
+import { RPCExporter } from "../src/Interfaces";
7 8
 
8 9
 const add = (...args:number[]) => {return args.reduce((a,b)=>a+b, 0)}
9 10
 function makeServer(){
@@ -499,4 +500,34 @@ describe("Errorhandler functionality", ()=>{
499 500
             })
500 501
         })
501 502
     })
503
+})
504
+
505
+
506
+describe("Class binding", ()=>{
507
+
508
+    class MyExporter implements RPCExporter{
509
+        name = "MyExporter";
510
+        exportRPCs = () => [
511
+            this.myRPC
512
+        ]
513
+
514
+        myRPC = () => "Hello World"
515
+
516
+    }
517
+
518
+    it("binds correctly", (done)=>{
519
+        const exporter = new MyExporter()
520
+        const serv = new RPCServer(21004, [exporter])
521
+        const sock = new RPCSocket(21004, 'localhost')
522
+        sock.connect().then(sock => {
523
+            if(sock.MyExporter && sock.MyExporter.myRPC){
524
+                done()
525
+            }
526
+        })
527
+        .catch(done)
528
+        .finally(() => {
529
+            sock.destroy()
530
+            serv.destroy()
531
+        })
532
+    })
502 533
 })

正在加载...
取消
保存