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

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