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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import * as I from "./Interfaces";
  2. import { RPCSocket } from "./Frontend";
  3. export type PioBindListener = (...args: any) => void
  4. export type PioHookListener = GenericFunction
  5. export type GenericFunction<Parameters extends any[] = any[], Result = any> = {(...args: Parameters): Result}
  6. export type BackendHook<Func extends GenericFunction> =
  7. GenericFunction<
  8. [
  9. GenericFunction<
  10. Parameters<
  11. AsFunction<
  12. Parameters<Func>[0]
  13. >
  14. >,
  15. void
  16. >,
  17. ...Tail<Parameters<Func>>
  18. ],
  19. ReturnType<Func>
  20. >
  21. export type AccessFilter<InterfaceT extends RPCInterface = RPCInterface> = (sesame: string | undefined, exporter: I.RPCExporter<InterfaceT, keyof InterfaceT>) => Promise<boolean> | boolean
  22. export type Visibility = "127.0.0.1" | "0.0.0.0"
  23. export type ConnectionHandler = (socket: I.Socket) => void
  24. export type ErrorHandler = (socket: I.Socket, error: any, rpcName: string, args: any[]) => void
  25. export type CloseHandler = (socket: I.Socket) => void
  26. export type SesameFunction = (sesame: string) => boolean
  27. export type SesameConf = {
  28. sesame?: string | SesameFunction
  29. }
  30. export type FrontEndHandlerType = {
  31. 'error': (e: any) => void
  32. 'close': () => void
  33. }
  34. export type ClientConfig = SocketIOClient.ConnectOpts & {
  35. protocol?: 'http' | 'https',
  36. callTimeoutMs?: number
  37. }
  38. export type ExporterArray<InterfaceT extends RPCInterface = RPCInterface> = I.RPCExporter<RPCInterface<InterfaceT>, keyof InterfaceT>[]
  39. export type ConnectedSocket<T extends RPCInterface = RPCInterface> = RPCSocket & AsyncIfc<T>
  40. export type ServerConf<InterfaceT extends RPCInterface> = {
  41. accessFilter?: AccessFilter<InterfaceT>
  42. connectionHandler?: ConnectionHandler
  43. errorHandler?: ErrorHandler
  44. closeHandler?: CloseHandler
  45. throwOnUnknownRPC?: boolean
  46. } & SesameConf
  47. export type ResponseType = "Subscribe" | "Success" | "Error"
  48. export type Outcome = "Success" | "Error"
  49. export type Respose<T> = T & { result: Outcome }
  50. export type SuccessResponse<T = {}> = Respose<T> & { result: "Success" }
  51. export type ErrorResponse<T = {}> = Respose<T> & { result: "Error", message?: string }
  52. export type RPCType = 'Hook' | 'Unhook' | 'Call'
  53. export type CallRPC<Name, Func extends GenericFunction> = {
  54. name: Name
  55. call: Func
  56. }
  57. export type HookRPC<Name, Func extends GenericFunction> = {
  58. name: Name
  59. hook: BackendHook<Func>
  60. onCallback?: GenericFunction
  61. onDestroy?: HookCloseFunction<ReturnType<Func> extends Promise<infer T> ? T : ReturnType<Func>>
  62. }
  63. export type RPC<Name, Func extends GenericFunction> = HookRPC<Name, Func> | CallRPC<Name, Func> | Func
  64. export type RPCInterface<Impl extends RPCInterface = {}> = {
  65. [grp in string]: {
  66. [rpc in string]: GenericFunction
  67. }
  68. } & Impl
  69. export type exportT = {
  70. [group in string]: {}
  71. }
  72. export type RPCDefinitions<Ifc extends RPCInterface> = {
  73. [grp in keyof Ifc]: ({
  74. [rpc in keyof Ifc[grp]]: RPC<rpc, Ifc[grp][rpc]>
  75. }[keyof Ifc[grp]])[]
  76. }
  77. export type BaseInfo = {
  78. name: string,
  79. owner: string,
  80. argNames: string[],
  81. }
  82. export type HookInfo<SubresT = {}> = BaseInfo & {
  83. type: 'Hook',
  84. generator: (socket?: I.Socket) => (...args: any[]) => SubresT
  85. }
  86. export type CallInfo = BaseInfo & {
  87. type: 'Call',
  88. call: GenericFunction
  89. }
  90. export type RpcInfo = HookInfo | CallInfo
  91. export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
  92. export type OnFunction = <T extends "error" | "close">(type: T, f: FrontEndHandlerType[T]) => void
  93. export type HookCloseFunction<T> = (res: T, rpc: HookRPC<any, any>) => any
  94. export type AsyncIfc<Ifc extends RPCInterface> = { [grp in keyof Ifc]: { [rpcname in keyof Ifc[grp]]: AsyncGenericFunction<Ifc[grp][rpcname]> } }
  95. export type AsyncGenericFunction<F extends GenericFunction = GenericFunction> = F extends (...args: Parameters<F>) => infer R
  96. ? ((...args: Parameters<F>) => R extends Promise<any> ? R : Promise<R>)
  97. : Promise<any>
  98. type Destroyable = { destroy: () => void }
  99. export type Callback<Params extends any[] = []> =
  100. (this: Destroyable, ...args: Params) => void
  101. type AsFunction<F> = F extends GenericFunction ? F : GenericFunction
  102. type Last<Tuple extends any[]> = Tuple[ Subtract<Length<Tuple>, 1> ]
  103. type Tail<T extends any[]> = T extends [any, ...infer R] ? R : any[]
  104. type Length<T extends any[]> =
  105. T extends { length: infer L } ? L : never;
  106. type BuildTuple<L extends number, T extends any[] = []> =
  107. T extends { length: L } ? T : BuildTuple<L, [...T, any]>;
  108. type Subtract<A extends number, B extends number> =
  109. BuildTuple<A> extends [...(infer U), ...BuildTuple<B>]
  110. ? Length<U>
  111. : never;