fix bad rpc finding for class member for real this time
This commit is contained in:
+2
-2
@@ -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",
|
||||
|
||||
+8
-2
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -173,3 +173,19 @@ 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
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -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(){
|
||||
@@ -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()
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user