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.

Types.ts 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import * as I from "./Interfaces";
  2. import { RPCSocket } from "./Frontend";
  3. export type AnyFunction = (...args:any) => any
  4. export type HookFunction = AnyFunction
  5. export type AccessFilter<InterfaceT extends RPCInterface = RPCInterface> = (sesame:string|undefined, exporter: I.RPCExporter<InterfaceT, keyof InterfaceT>) => Promise<boolean>
  6. export type Visibility = "127.0.0.1" | "0.0.0.0"
  7. export type ConnectionHandler = (socket:I.Socket) => void
  8. export type ErrorHandler = (socket:I.Socket, error:any, rpcName: string, args: any[]) => void
  9. export type CloseHandler = (socket:I.Socket) => void
  10. export type SesameFunction = (sesame : string) => boolean
  11. export type ExceptionHandling = 'local' | 'remote'
  12. export type SesameConf = {
  13. sesame?: string | SesameFunction
  14. }
  15. export type FrontEndHandlerType = {
  16. 'error' : (e: any) => void
  17. 'close' : () => void
  18. }
  19. export type ExporterArray<InterfaceT extends RPCInterface = RPCInterface> = I.RPCExporter<RPCInterface<InterfaceT>, keyof InterfaceT>[]
  20. export type ConnectedSocket<T extends RPCInterface = RPCInterface> = RPCSocket & T
  21. export type ServerConf<InterfaceT extends RPCInterface> = {
  22. accessFilter?: AccessFilter<InterfaceT>
  23. connectionHandler?: ConnectionHandler
  24. errorHandler?: ErrorHandler
  25. closeHandler?: CloseHandler
  26. visibility?: Visibility
  27. exceptionHandling?: ExceptionHandling
  28. } & SesameConf
  29. export type SocketConf = {
  30. tls:boolean
  31. }
  32. export type ResponseType = "Subscribe" | "Success" | "Error"
  33. export type Outcome = "Success" | "Error"
  34. export type Respose<T> = T & { result: Outcome }
  35. export type SuccessResponse<T = {}> = Respose<T> & { result: "Success" }
  36. export type ErrorResponse<T = {}> = Respose<T> & { result: "Error", message?:string }
  37. export type RPCType = 'Hook' | 'Unhook' | 'Call'
  38. export type CallRPC<Name, Func extends AnyFunction> = {
  39. name: Name
  40. call: Func
  41. }
  42. export type HookRPC<Name, Func extends AnyFunction> = {
  43. name: Name
  44. hook: Func
  45. onCallback?: AnyFunction
  46. onClose?: HookCloseFunction<ReturnType<Func> extends Promise<infer T> ? T : ReturnType<Func>>
  47. }
  48. export type RPC<Name, Func extends AnyFunction> = HookRPC<Name, Func> | CallRPC<Name,Func> | Func
  49. export type RPCInterface<Impl extends RPCInterface = {}> = {
  50. [grp in string] : {
  51. [rpc in string] : AnyFunction
  52. }
  53. } & Impl
  54. export type exportT = {
  55. [group in string]: {}
  56. }
  57. //This probably has lots of issues
  58. export type RPCDefinitions<Ifc extends RPCInterface> = {
  59. [grp in keyof Ifc]:( { [rpc in keyof Ifc[grp]]: RPC<rpc, Ifc[grp][rpc]> }[keyof Ifc[grp]] )[]
  60. }
  61. export type BaseInfo = {
  62. name: string,
  63. owner: string,
  64. argNames: string[],
  65. }
  66. export type HookInfo<SubresT = {}> = BaseInfo & {
  67. type: 'Hook',
  68. generator: (socket?:I.Socket) => (...args:any[]) => SubresT
  69. }
  70. export type CallInfo = BaseInfo & {
  71. type: 'Call',
  72. call: AnyFunction
  73. }
  74. export type RpcInfo = HookInfo | CallInfo
  75. export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
  76. export type OnFunction = <T extends "error" | "close">(type: T, f: FrontEndHandlerType[T]) => void
  77. export type HookCloseFunction<T> = (res: T, rpc:HookRPC<any, any>) => any