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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. export class RPCServer<
  8. InterfaceT extends T.RPCInterface = T.RPCInterface,
  9. > implements I.Destroyable {
  10. private ws = http.createServer()
  11. private io = bsock.createServer()
  12. private visibility: T.Visibility
  13. private closeHandler: T.CloseHandler
  14. private errorHandler: T.ErrorHandler
  15. private connectionHandler: T.ConnectionHandler
  16. private sesame?: T.SesameFunction
  17. private accessFilter: T.AccessFilter<InterfaceT>
  18. /**
  19. * @throws On RPC with no name
  20. * @param port The port to listen on
  21. * @param exporters A list of {@link RPCExporter} to publish
  22. * @param conf A {@link SocketConf} object with optional settings
  23. */
  24. constructor(
  25. private port: number,
  26. private exporters: T.ExporterArray<InterfaceT> = [],
  27. conf: T.ServerConf<InterfaceT> = {}
  28. ) {
  29. if (!conf.visibility) this.visibility = "0.0.0.0"
  30. if (conf.sesame) {
  31. this.sesame = U.makeSesameFunction(conf.sesame)
  32. }
  33. this.accessFilter = conf.accessFilter || (async (sesame) => {
  34. if (!this.sesame) return true
  35. return this.sesame!(sesame!)
  36. })
  37. this.errorHandler = (socket: I.Socket) => (error: any, rpcName: string, args: any[]) => {
  38. if (conf.errorHandler) conf.errorHandler(socket, error, rpcName, args)
  39. else throw error
  40. }
  41. this.closeHandler = (socket: I.Socket) => {
  42. if (conf.closeHandler) conf.closeHandler(socket)
  43. }
  44. this.connectionHandler = (socket: I.Socket) => {
  45. if (conf.connectionHandler) conf.connectionHandler(socket)
  46. }
  47. exporters.forEach(U.fixNames) //TSC for some reason doesn't preserve name properties of methods
  48. let badRPC = exporters.flatMap(ex => ex.exportRPCs()).find(rpc => !rpc.name)
  49. if (badRPC) {
  50. throw new Error(`
  51. RPC did not provide a name.
  52. \nUse 'funtion name(..){ .. }' syntax instead.
  53. \n
  54. \n<------------OFFENDING RPC:
  55. \n`+ badRPC.toString() + `
  56. \n>------------OFFENDING RPC`)
  57. }
  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, "system", []))
  65. socket.on('close', () => this.closeHandler(socket))
  66. this.connectionHandler(socket)
  67. this.initRPCs(socket)
  68. })
  69. this.ws = this.ws.listen(this.port, this.visibility)
  70. } catch (e) {
  71. this.errorHandler(this.io, e, 'system', [])
  72. }
  73. }
  74. protected initRPCs(socket: I.Socket) {
  75. socket.hook('info', async (sesame?: string) => {
  76. const rpcs = await Promise.all(this.exporters.map(async exp => {
  77. const allowed = await this.accessFilter(sesame, exp)
  78. if (!allowed) return []
  79. return U.rpcHooker(socket, exp, this.errorHandler, this.sesame)
  80. }))
  81. return rpcs.flat()
  82. })
  83. }
  84. /**
  85. * Publishes a new list of Exporters. This destroys and restarts the socket
  86. * @param exporters the exporters to publish
  87. public setExporters(exporters: T.ExporterArray<InterfaceT>): any {
  88. exporters.forEach(U.fixNames)
  89. this.destroy()
  90. this.ws = http.createServer()
  91. this.io = bsock.createServer()
  92. this.exporters = exporters
  93. this.startWebsocket()
  94. }
  95. */
  96. destroy(): void {
  97. this.io.close()
  98. this.ws.close()
  99. }
  100. }