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

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