'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 = I.RPCExporter, 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 /** * @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 = [], conf: T.ServerConf = {} ){ if(!conf.visibility) this.visibility = "127.0.0.1" this.accessFilter = conf.accessFilter || (async () => true) if(conf.sesame){ this.sesame = U.makeSesameFunction(conf.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):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() } }