Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Backend.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. import { Socket } from 'dgram';
  8. type Exporters<InterfaceT extends T.RPCInterface = T.RPCInterface, SubResType = {}> = I.RPCExporter<T.RPCInterface<InterfaceT>, keyof InterfaceT, SubResType>[]
  9. /**
  10. * A Websocket-server-on-steroids with built-in RPC capabilities
  11. */
  12. export class RPCServer<
  13. SubResType = {},
  14. InterfaceT extends T.RPCInterface = T.RPCInterface,
  15. > implements I.Destroyable{
  16. private ws = http.createServer()
  17. private io = bsock.createServer()
  18. private visibility:T.Visibility
  19. private closeHandler:T.CloseHandler
  20. private errorHandler: T.ErrorHandler
  21. private connectionHandler: T.ConnectionHandler
  22. private sesame? : T.SesameFunction
  23. /**
  24. * @throws On RPC with no name
  25. * @param port The port to listen on
  26. * @param exporters A list of {@link RPCExporter} to publish
  27. * @param conf A {@link SocketConf} object with optional settings
  28. */
  29. constructor(
  30. private port:number,
  31. private exporters: Exporters<InterfaceT, SubResType> = [],
  32. conf: T.ServerConf = {}
  33. ){
  34. if(!conf.visibility) this.visibility = "127.0.0.1"
  35. if(conf.sesame){
  36. this.sesame = U.makeSesameFunction(conf.sesame)
  37. }
  38. this.errorHandler = (socket:I.Socket) => (error:any, rpcName:string, args: any[]) => {
  39. if(conf.errorHandler) conf.errorHandler(socket, error, rpcName, args)
  40. else throw error
  41. }
  42. this.closeHandler = (socket:I.Socket) => {
  43. if(conf.closeHandler) conf.closeHandler(socket)
  44. }
  45. this.connectionHandler = (socket:I.Socket) => {
  46. if(conf.connectionHandler) conf.connectionHandler(socket)
  47. }
  48. exporters.forEach(U.fixNames) //TSC for some reason doesn't preserve name properties of methods
  49. let badRPC = exporters.flatMap(ex => ex.exportRPCs()).find(rpc => !rpc.name)
  50. if(badRPC){
  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. }
  59. this.startWebsocket()
  60. }
  61. private startWebsocket(){
  62. try{
  63. this.io.attach(this.ws)
  64. this.io.on('socket', (socket:I.Socket) => {
  65. socket.on('error', (err) => this.errorHandler(socket, err, "system", []))
  66. socket.on('close', () => this.closeHandler(socket))
  67. this.connectionHandler(socket)
  68. this.initRPCs(socket)
  69. })
  70. this.ws.listen(this.port, this.visibility)
  71. }catch(e){
  72. this.errorHandler(this.io, e, 'system', [])
  73. }
  74. }
  75. protected initRPCs(socket:I.Socket){
  76. socket.hook('info', () => rpcInfos)
  77. const rpcInfos:T.ExtendedRpcInfo[] = [
  78. ...this.exporters.flatMap(exporter => U.rpcHooker(socket, exporter, this.errorHandler, this.sesame))
  79. ]
  80. }
  81. /**
  82. * Publishes a new list of Exporters. This destroys and restarts the socket
  83. * @param exporters the exporters to publish
  84. */
  85. public setExporters(exporters: Exporters<InterfaceT, SubResType>):any{
  86. exporters.forEach(U.fixNames)
  87. this.destroy()
  88. this.ws = http.createServer()
  89. this.io = bsock.createServer()
  90. this.exporters = exporters
  91. this.startWebsocket()
  92. }
  93. destroy(): void {
  94. this.io.close()
  95. this.ws.close()
  96. }
  97. }