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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. }
  13. public hook(name: T.Name, args: T.Arg){
  14. return this.socket.hook(name, args)
  15. }
  16. public unhook(name: T.Name){
  17. return this.socket.unhook(name)
  18. }
  19. public on(type: "error" | "close", f: (e?: any) => void){
  20. return this.socket.on(type, f)
  21. }
  22. public destroy(){
  23. return this.socket.destroy()
  24. }
  25. public close(){
  26. return this.socket.close()
  27. }
  28. public async call (rpcname: T.Name, ...args: T.Any[]) : Promise<T.Any>{
  29. return await this.socket.call.apply(this.socket, [rpcname, ...args])
  30. }
  31. public async fire(rpcname: T.Name, ...args: T.Any[]) : Promise<T.Any>{
  32. return await this.socket.fire.apply(this.socket, [rpcname, ...args])
  33. }
  34. public async connect(){
  35. this.socket = await bsock.connect(this.port, this.server, this.tls)
  36. const info:T.ExtendedRpcInfo[] = await this.info()
  37. info.forEach(i => {
  38. let f: any
  39. switch (i.type) {
  40. case 'Call':
  41. f = this.callGenerator(i.uniqueName, i.argNames)
  42. break
  43. case 'Hook':
  44. f = this.hookGenerator(i.uniqueName, i.argNames)
  45. break
  46. case 'Unhook':
  47. f = this.unhookGenerator(i.uniqueName, i.argNames)
  48. break
  49. }
  50. if(this[i.owner] == null)
  51. this[i.owner] = {}
  52. this[i.owner][i.name] = f
  53. this[i.owner][i.name].bind(this)
  54. })
  55. }
  56. public async info(){
  57. return await this.socket.call('info')
  58. }
  59. private callGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.AsyncFunction{
  60. const headerArgs = fnArgs.join(",")
  61. const argParams = fnArgs.map(stripAfterEquals).join(",")
  62. return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", '+argParams+')} )()' )
  63. }
  64. private hookGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.CallbackFunction{
  65. const headerArgs = fnArgs.join(",")
  66. const argParams = fnArgs.map(stripAfterEquals).join(",")
  67. return eval( `( () => async (`+headerArgs+(headerArgs.length!==0?",":"")+` callback) => {
  68. const r = await this.socket.call("`+fnName+`", `+argParams+`)
  69. if(r.uid != null){
  70. this.socket.hook(res.uid, callback)
  71. }
  72. return res
  73. } )()` )
  74. }
  75. private unhookGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.UnhookFunction{
  76. const headerArgs = fnArgs.join(",")
  77. const argParams = fnArgs.map(stripAfterEquals).join(",")
  78. if(fnArgs.length != 1)
  79. console.error("UnhookFunction", fnName, "specified more than one argument: ("+headerArgs+")")
  80. return eval( `( () => async (`+headerArgs+`) => {
  81. const r = await this.socket.call("`+fnName+`", `+argParams+`)
  82. this.socket.unhook(`+argParams+`)
  83. return res
  84. } )()` )
  85. }
  86. }