123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 'use strict'
-
- import http = require('http');
- import bsock = require('bsock');
-
- import * as T from './Types';
- import * as U from './Utils';
- 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
- */
- export class RPCServer<
- SubResType = {},
- InterfaceT extends T.RPCInterface = T.RPCInterface,
- > implements I.Destroyable{
-
- private ws = http.createServer()
- private io = bsock.createServer()
- private visibility:T.Visibility
- private closeHandler:T.CloseHandler
- private errorHandler: T.ErrorHandler
- private connectionHandler: T.ConnectionHandler
- private sesame? : T.SesameFunction
- private accessFilter: T.AccessFilter<InterfaceT, SubResType>
-
- /**
- * @throws On RPC with no name
- * @param port The port to listen on
- * @param exporters A list of {@link RPCExporter} to publish
- * @param conf A {@link SocketConf} object with optional settings
- */
- constructor(
- private port:number,
- private exporters: Exporters<InterfaceT, SubResType> = [],
- conf: T.ServerConf<InterfaceT, SubResType> = {}
- ){
- if(!conf.visibility) this.visibility = "0.0.0.0"
-
-
- if(conf.sesame){
- this.sesame = U.makeSesameFunction(conf.sesame)
- }
-
- this.accessFilter = conf.accessFilter || (async (sesame) => {
- if(!this.sesame) return true
- return this.sesame!(sesame!)
- })
-
-
- this.errorHandler = (socket:I.Socket) => (error:any, rpcName:string, args: any[]) => {
- if(conf.errorHandler) conf.errorHandler(socket, error, rpcName, args)
- else throw error
- }
-
- this.closeHandler = (socket:I.Socket) => {
- if(conf.closeHandler) conf.closeHandler(socket)
- }
-
- this.connectionHandler = (socket:I.Socket) => {
- if(conf.connectionHandler) conf.connectionHandler(socket)
- }
-
- exporters.forEach(U.fixNames) //TSC for some reason doesn't preserve name properties of methods
-
- let badRPC = exporters.flatMap(ex => ex.exportRPCs()).find(rpc => !rpc.name)
- if(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()
- }
-
- private startWebsocket(){
- try{
- this.io.attach(this.ws)
- this.io.on('socket', (socket:I.Socket) => {
- socket.on('error', (err) => this.errorHandler(socket, err, "system", []))
- socket.on('close', () => this.closeHandler(socket))
- this.connectionHandler(socket)
- this.initRPCs(socket)
- })
- this.ws.listen(this.port, this.visibility)
- }catch(e){
- this.errorHandler(this.io, e, 'system', [])
- }
- }
-
- protected initRPCs(socket:I.Socket){
- socket.hook('info', async (sesame? : string) => {
- const rpcs = await Promise.all(this.exporters.map(async exp => {
- const allowed = await this.accessFilter(sesame, exp)
- if(!allowed) return []
- return U.rpcHooker(socket, exp, this.errorHandler, this.sesame)
- }))
- return rpcs.flat()
- })
- }
-
- /**
- * 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 {
- this.io.close()
- this.ws.close()
- }
- }
|