Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Types.ts 3.5KB

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