From a2268526af972e226b9e32d0222ab9e254faff96 Mon Sep 17 00:00:00 2001 From: peter Date: Sat, 1 Feb 2020 11:45:35 +0100 Subject: [PATCH] fix bad rpc finding for class member for real this time --- package.json | 4 ++-- src/Backend.ts | 10 ++++++++-- src/Utils.ts | 16 ++++++++++++++++ test/Test.ts | 31 +++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 1d751d3..a1e1186 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rpclibrary", - "version": "1.6.3", + "version": "1.7.0", "description": "rpclibrary is a websocket on steroids!", "main": "./js/Index.js", "repository": { @@ -30,7 +30,7 @@ "frontend": "node js/test/TestFrontend.js", "build": "npm run clean && tsc && npm run webpack", "clean": "rm -rf js", - "test": "mocha js/test/Test.js", + "test": "npm run clean && npm run build && mocha js/test/Test.js", "docs": "typedoc --out docs ./src --readme ./README.md --plugin typedoc-plugin-markdown --mode file --hideBreadcrumbs --hideSources" }, "license": "MIT", diff --git a/src/Backend.ts b/src/Backend.ts index 51667aa..00e80a1 100644 --- a/src/Backend.ts +++ b/src/Backend.ts @@ -53,15 +53,21 @@ export class RPCServer< if(conf.connectionHandler) conf.connectionHandler(socket) } + exporters.forEach(U.fixNames) + let badRPC = exporters.flatMap(ex => ex.exportRPCs()).find(rpc => !rpc.name) - if(badRPC) - throw new Error(` + if(badRPC){ + console.log(badRPC); + + + throw new Error(` RPC did not provide a name. \nUse 'funtion name(..){ .. }' syntax instead. \n \n<------------OFFENDING RPC: \n`+badRPC.toString()+` \n>------------OFFENDING RPC`) + } this.startWebsocket() } diff --git a/src/Utils.ts b/src/Utils.ts index 950c70a..9535b36 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -172,4 +172,20 @@ export function makeSesameFunction (sesame : T.SesameFunction | string) : T.Sesa export function appendComma(s?:string):string{ return s?`'${s}',`:"" +} + + +/** + * Typescript incorrectly omits the function.name attribute for MethodDeclaration. + * This was supposedly fixed (https://github.com/microsoft/TypeScript/issues/5611) but it still is the case. + * This function sets the name value for all object members that are functions. +*/ +export function fixNames(o:Object):void{ + Object.keys(o).forEach(key => { + if(typeof o[key] === 'function' && !o[key].name){ + Object.defineProperty(o[key], 'name', { + value: key + }) + } + }) } \ No newline at end of file diff --git a/test/Test.ts b/test/Test.ts index 3ff99b8..f976206 100644 --- a/test/Test.ts +++ b/test/Test.ts @@ -4,6 +4,7 @@ import { RPCServer, RPCSocket, SubscriptionResponse, makeSubResponse } from '../ import * as uuidv4 from "uuid/v4" import { doesNotReject } from "assert"; import { Socket } from "dgram"; +import { RPCExporter } from "../src/Interfaces"; const add = (...args:number[]) => {return args.reduce((a,b)=>a+b, 0)} function makeServer(){ @@ -499,4 +500,34 @@ describe("Errorhandler functionality", ()=>{ }) }) }) +}) + + +describe("Class binding", ()=>{ + + class MyExporter implements RPCExporter{ + name = "MyExporter"; + exportRPCs = () => [ + this.myRPC + ] + + myRPC = () => "Hello World" + + } + + it("binds correctly", (done)=>{ + const exporter = new MyExporter() + const serv = new RPCServer(21004, [exporter]) + const sock = new RPCSocket(21004, 'localhost') + sock.connect().then(sock => { + if(sock.MyExporter && sock.MyExporter.myRPC){ + done() + } + }) + .catch(done) + .finally(() => { + sock.destroy() + serv.destroy() + }) + }) }) \ No newline at end of file