setExporters

This commit is contained in:
2020-03-05 15:28:02 +01:00
parent ef5a83f1d4
commit 0057625800
3 changed files with 62 additions and 12 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "rpclibrary", "name": "rpclibrary",
"version": "1.8.3", "version": "1.9.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": {
+17 -2
View File
@@ -6,6 +6,9 @@ import bsock = require('bsock');
import * as T from './Types'; import * as T from './Types';
import * as U from './Utils'; import * as U from './Utils';
import * as I from './Interfaces'; import * as I from './Interfaces';
import { Socket } from 'dgram';
type Exporters<InterfaceT extends T.RPCInterface = T.RPCInterface, SubResType = {}> = I.RPCExporter<T.RPCInterface<InterfaceT>, keyof InterfaceT, SubResType>[]
/** /**
* A Websocket-server-on-steroids with built-in RPC capabilities * A Websocket-server-on-steroids with built-in RPC capabilities
@@ -31,10 +34,9 @@ export class RPCServer<
*/ */
constructor( constructor(
private port:number, private port:number,
private exporters: I.RPCExporter<T.RPCInterface<InterfaceT>, keyof InterfaceT, SubResType>[] = [], private exporters: Exporters<InterfaceT, SubResType> = [],
conf: T.ServerConf = {} conf: T.ServerConf = {}
){ ){
if(!conf.visibility) this.visibility = "127.0.0.1" if(!conf.visibility) this.visibility = "127.0.0.1"
if(conf.sesame){ if(conf.sesame){
@@ -91,6 +93,19 @@ export class RPCServer<
] ]
} }
/**
* Publishes a new list of Exporters. This destroys and restarts the socket
* @param exporters the exporters to publish
*/
public setExporters(exporters: Exporters<InterfaceT, SubResType>):any{
exporters.forEach(U.fixNames)
this.destroy()
this.ws = http.createServer()
this.io = bsock.createServer()
this.exporters = exporters
this.startWebsocket()
}
destroy(): void { destroy(): void {
this.io.close() this.io.close()
this.ws.close() this.ws.close()
+44 -9
View File
@@ -541,38 +541,73 @@ type myExporterIfc = {
describe("Class binding", ()=>{ describe("Class binding", ()=>{
let exporter1 : MyExporter
let serv : RPCServer
let sock: RPCSocket & myExporterIfc
class MyExporter implements RPCExporter<myExporterIfc>{ class MyExporter implements RPCExporter<myExporterIfc>{
name = "MyExporter" as "MyExporter"; name = "MyExporter" as "MyExporter";
exportRPCs = () => [ exportRPCs = () => [
this.myRPC this.myRPC
] ]
myRPC = async () => "Hello World" myRPC = async () => {
serv.setExporters([new MyOtherExporter])
return "Hello World"
}
}
class MyOtherExporter implements RPCExporter<myExporterIfc>{
name = "MyExporter" as "MyExporter";
exportRPCs = () => [
this.myRPC
]
myRPC = async () => "Hello Borld"
} }
let serv: RPCServer,
sock: RPCSocket & myExporterIfc,
exporter: MyExporter
before((done)=>{ before(done => {
exporter = new MyExporter() exporter1 = new MyExporter()
serv = new RPCServer(21004, [exporter]) serv = new RPCServer(21004, [exporter1])
done()
})
beforeEach((done)=>{
const s = new RPCSocket(21004, 'localhost') const s = new RPCSocket(21004, 'localhost')
s.connect<myExporterIfc>().then(conn => { s.connect<myExporterIfc>().then(conn => {
sock = conn sock = conn
done() done()
}) })
}) })
after(() => {
afterEach(done => {
sock.destroy() sock.destroy()
done()
})
after(() => {
serv.destroy() serv.destroy()
}) })
it("binds correctly", (done)=>{ it("binds correctly", (done)=>{
sock['MyExporter'].myRPC().then(() => { sock['MyExporter'].myRPC().then((res) => {
done(new Error(res))
}).catch(e => {
//job will time out because of setExporters
done() done()
}) })
}) })
it("changes exporters", (done) => {
sock['MyExporter'].myRPC().then((res) => {
if(res === "Hello Borld")
done()
else
done(new Error(res))
})
})
}) })