You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Backend.ts 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict'
  2. import http = require('http');
  3. import bsock = require('bsock');
  4. import * as T from './Types';
  5. import * as U from './Utils';
  6. import * as I from './Interfaces';
  7. /**
  8. * A Websocket-server-on-steroids with built-in RPC capabilities
  9. */
  10. export class RPCServer<
  11. SubResType = {},
  12. InterfaceT extends T.RPCInterface = T.RPCInterface,
  13. > implements I.Destroyable{
  14. private ws = http.createServer()
  15. private io = bsock.createServer()
  16. private visibility:T.Visibility
  17. private closeHandler:T.CloseHandler
  18. private errorHandler: T.ErrorHandler
  19. private connectionHandler: T.ConnectionHandler
  20. private sesame? : T.SesameFunction
  21. /**
  22. * @throws On RPC with no name
  23. * @param port The port to listen on
  24. * @param exporters A list of {@link RPCExporter} to publish
  25. * @param conf A {@link SocketConf} object with optional settings
  26. */
  27. constructor(
  28. private port:number,
  29. private exporters: I.RPCExporter<T.RPCInterface<InterfaceT>, keyof InterfaceT, SubResType>[] = [],
  30. conf: T.ServerConf = {}
  31. ){
  32. if(!conf.visibility) this.visibility = "127.0.0.1"
  33. if(conf.sesame){
  34. this.sesame = U.makeSesameFunction(conf.sesame)
  35. }
  36. this.errorHandler = (socket:I.Socket) => (error:any) => {
  37. if(conf.errorHandler) conf.errorHandler(socket, error)
  38. socket.destroy();
  39. console.error("Caught websocket error", String(error))
  40. }
  41. this.closeHandler = (socket:I.Socket) => {
  42. if(!conf.closeHandler) console.log("Connection on port "+socket.port+" closing")
  43. else conf.closeHandler(socket)
  44. }
  45. this.connectionHandler = (socket:I.Socket) => {
  46. if(!conf.connectionHandler) console.log("New connection on port "+socket.port)
  47. else conf.connectionHandler(socket)
  48. }
  49. let badRPC
  50. if(badRPC = exporters.flatMap(ex => ex.exportRPCs()).find(rpc => !rpc.name))
  51. throw new Error(`
  52. RPC did not provide a name.
  53. \nUse 'funtion name(..){ .. }' syntax instead.
  54. \n
  55. \n<------------OFFENDING RPC:
  56. \n`+badRPC.toString()+`
  57. \n>------------OFFENDING RPC`)
  58. this.startWebsocket()
  59. }
  60. private startWebsocket(){
  61. try{
  62. this.io.attach(this.ws)
  63. this.io.on('socket', (socket:I.Socket) => {
  64. socket.on('error', (err) => this.errorHandler(socket, err))
  65. socket.on('close', () => this.closeHandler(socket))
  66. this.connectionHandler(socket)
  67. this.initRPCs(socket)
  68. })
  69. this.ws.listen(this.port, this.visibility)
  70. }catch(e){
  71. this.errorHandler(this.io, e)
  72. }
  73. }
  74. protected initRPCs(socket:I.Socket){
  75. socket.hook('info', () => rpcInfos)
  76. const rpcInfos:T.ExtendedRpcInfo[] = [
  77. ...this.exporters.flatMap(exporter => U.rpcHooker(socket, exporter, this.sesame))
  78. ]
  79. }
  80. async destroy(): Promise<void> {
  81. this.io.close()
  82. this.ws.close()
  83. }
  84. }