fix bad rpc finding for class member for real this time

This commit is contained in:
2020-02-01 11:45:35 +01:00
parent fff118d9d2
commit a2268526af
4 changed files with 57 additions and 4 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
{ {
"name": "rpclibrary", "name": "rpclibrary",
"version": "1.6.3", "version": "1.7.0",
"description": "rpclibrary is a websocket on steroids!", "description": "rpclibrary is a websocket on steroids!",
"main": "./js/Index.js", "main": "./js/Index.js",
"repository": { "repository": {
@@ -30,7 +30,7 @@
"frontend": "node js/test/TestFrontend.js", "frontend": "node js/test/TestFrontend.js",
"build": "npm run clean && tsc && npm run webpack", "build": "npm run clean && tsc && npm run webpack",
"clean": "rm -rf js", "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" "docs": "typedoc --out docs ./src --readme ./README.md --plugin typedoc-plugin-markdown --mode file --hideBreadcrumbs --hideSources"
}, },
"license": "MIT", "license": "MIT",
+8 -2
View File
@@ -53,15 +53,21 @@ export class RPCServer<
if(conf.connectionHandler) conf.connectionHandler(socket) if(conf.connectionHandler) conf.connectionHandler(socket)
} }
exporters.forEach(U.fixNames)
let badRPC = exporters.flatMap(ex => ex.exportRPCs()).find(rpc => !rpc.name) let badRPC = exporters.flatMap(ex => ex.exportRPCs()).find(rpc => !rpc.name)
if(badRPC) if(badRPC){
throw new Error(` console.log(badRPC);
throw new Error(`
RPC did not provide a name. RPC did not provide a name.
\nUse 'funtion name(..){ .. }' syntax instead. \nUse 'funtion name(..){ .. }' syntax instead.
\n \n
\n<------------OFFENDING RPC: \n<------------OFFENDING RPC:
\n`+badRPC.toString()+` \n`+badRPC.toString()+`
\n>------------OFFENDING RPC`) \n>------------OFFENDING RPC`)
}
this.startWebsocket() this.startWebsocket()
} }
+16
View File
@@ -173,3 +173,19 @@ export function makeSesameFunction (sesame : T.SesameFunction | string) : T.Sesa
export function appendComma(s?:string):string{ export function appendComma(s?:string):string{
return s?`'${s}',`:"" 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
})
}
})
}
+31
View File
@@ -4,6 +4,7 @@ import { RPCServer, RPCSocket, SubscriptionResponse, makeSubResponse } from '../
import * as uuidv4 from "uuid/v4" import * as uuidv4 from "uuid/v4"
import { doesNotReject } from "assert"; import { doesNotReject } from "assert";
import { Socket } from "dgram"; import { Socket } from "dgram";
import { RPCExporter } from "../src/Interfaces";
const add = (...args:number[]) => {return args.reduce((a,b)=>a+b, 0)} const add = (...args:number[]) => {return args.reduce((a,b)=>a+b, 0)}
function makeServer(){ function makeServer(){
@@ -500,3 +501,33 @@ 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()
})
})
})