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.

Frontend.ts 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict'
  2. var bsock = require('bsock')
  3. import * as T from './Types';
  4. import * as I from './Interfaces';
  5. //fix args with defaults like "force = true" -> "force"
  6. function stripAfterEquals(str:string){
  7. return str.split("=")[0]
  8. }
  9. export class RPCSocket implements I.Socket{
  10. private socket: I.Socket
  11. constructor(public port:number, private server: string, private tls: boolean = false){
  12. Object.defineProperty(this, 'socket', {value: undefined, writable: true})
  13. }
  14. public hook(name: T.Name, args: T.Arg){
  15. return this.socket.hook(name, args)
  16. }
  17. public unhook(name: T.Name){
  18. return this.socket.unhook(name)
  19. }
  20. public on(type: "error" | "close", f: (e?: any) => void){
  21. return this.socket.on(type, f)
  22. }
  23. public destroy(){
  24. return this.socket.destroy()
  25. }
  26. public close(){
  27. return this.socket.close()
  28. }
  29. public async call (rpcname: T.Name, ...args: T.Any[]) : Promise<T.Any>{
  30. return await this.socket.call.apply(this.socket, [rpcname, ...args])
  31. }
  32. public async fire(rpcname: T.Name, ...args: T.Any[]) : Promise<T.Any>{
  33. return await this.socket.fire.apply(this.socket, [rpcname, ...args])
  34. }
  35. public async connect(){
  36. this.socket = await bsock.connect(this.port, this.server, this.tls)
  37. const info:T.ExtendedRpcInfo[] = await this.info()
  38. info.forEach(i => {
  39. let f: any
  40. switch (i.type) {
  41. case 'Call':
  42. f = this.callGenerator(i.uniqueName, i.argNames)
  43. break
  44. case 'Hook':
  45. f = this.hookGenerator(i.uniqueName, i.argNames)
  46. break
  47. case 'Unhook':
  48. f = this.unhookGenerator(i.uniqueName, i.argNames)
  49. break
  50. }
  51. if(this[i.owner] == null)
  52. this[i.owner] = {}
  53. this[i.owner][i.name] = f
  54. this[i.owner][i.name].bind(this)
  55. })
  56. }
  57. public async info(){
  58. return await this.socket.call('info')
  59. }
  60. private callGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.AsyncFunction{
  61. const headerArgs = fnArgs.join(",")
  62. const argParams = fnArgs.map(stripAfterEquals).join(",")
  63. return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", '+argParams+')} )()' )
  64. }
  65. private hookGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.CallbackFunction{
  66. const headerArgs = fnArgs.join(",")
  67. const argParams = fnArgs.map(stripAfterEquals).join(",")
  68. return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
  69. const r = await this.socket.call("`+fnName+`", `+argParams+`)
  70. if(r.uid != null){
  71. this.socket.hook(res.uid, callback)
  72. }
  73. return res
  74. } )()` )
  75. }
  76. private unhookGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.UnhookFunction{
  77. const headerArgs = fnArgs.join(",")
  78. const argParams = fnArgs.map(stripAfterEquals).join(",")
  79. if(fnArgs.length != 1)
  80. console.error("UnhookFunction", fnName, "specified more than one argument: ("+headerArgs+")")
  81. return eval( `( () => async (`+headerArgs+`) => {
  82. const r = await this.socket.call("`+fnName+`", `+argParams+`)
  83. this.socket.unhook(`+argParams+`)
  84. return res
  85. } )()` )
  86. }
  87. }