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

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