選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Types.ts 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import * as I from "./Interfaces";
  2. export type AnyFunction = (...args:any) => any
  3. export type HookFunction<F extends AnyFunction = AnyFunction, SubresT = {}> = (...args: Parameters<F>) => Promise<SubscriptionResponse<SubresT> | ErrorResponse>
  4. export type AccessFilter<InterfaceT extends RPCInterface, SubresT> = (sesame:string|undefined, exporter: I.RPCExporter<RPCInterface<InterfaceT>, keyof InterfaceT, SubresT>) => Promise<boolean>
  5. export type Visibility = "127.0.0.1" | "0.0.0.0"
  6. export type ConnectionHandler = (socket:I.Socket) => void
  7. export type ErrorHandler = (socket:I.Socket, error:any, rpcName: string, args: any[]) => void
  8. export type CloseHandler = (socket:I.Socket) => void
  9. export type SesameFunction = (sesame : string) => boolean
  10. export type ExceptionHandling = 'local' | 'remote'
  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 ServerConf<InterfaceT extends RPCInterface, SubresT> = {
  19. accessFilter?: AccessFilter<InterfaceT, SubresT>
  20. connectionHandler?: ConnectionHandler
  21. errorHandler?: ErrorHandler
  22. closeHandler?: CloseHandler
  23. visibility?: Visibility
  24. exceptionHandling?: ExceptionHandling
  25. } & SesameConf
  26. export type SocketConf = {
  27. tls:boolean
  28. }
  29. export type ResponseType = "Subscribe" | "Success" | "Error"
  30. export type Outcome = "Success" | "Error"
  31. export type Respose<T> = T & { result: Outcome }
  32. export type SuccessResponse<T = {}> = Respose<T> & { result: "Success" }
  33. export type ErrorResponse<T = {}> = Respose<T> & { result: "Error", message?:string }
  34. export type SubscriptionResponse<T = {}> = Respose<T> & { result: "Success"; uuid: string }
  35. export type RPCType = 'Hook' | 'Unhook' | 'Call'
  36. export type CallRPC<N, F> = {
  37. name: N
  38. call: F
  39. }
  40. export type HookRPC<N, F extends AnyFunction, SubresT = {}> = {
  41. name: N
  42. hook: HookFunction<F, SubresT>
  43. onCallback?: AnyFunction
  44. onClose?: HookCloseFunction<SubresT>
  45. }
  46. export type RPC<N, F extends AnyFunction, SubresT = {}> = HookRPC<N, F, SubresT> | CallRPC<N,F> | F
  47. export type RPCInterface<Impl extends RPCInterface = {}> = {
  48. [grp in string] : {
  49. [rpc in string] : AnyFunction
  50. }
  51. } & Impl
  52. export type RPCInterfaceArray<Itfc extends RPCInterface, SubresT = {}> = {
  53. [grp in keyof Itfc]: Array<
  54. { [rpc in keyof Itfc[grp]]: RPC<rpc, Itfc[grp][rpc], SubresT> }[keyof Itfc[grp]]
  55. >
  56. }
  57. export type BaseInfo = {
  58. name: string,
  59. owner: string,
  60. argNames: string[],
  61. }
  62. export type HookInfo<SubresT = {}> = BaseInfo & {
  63. type: 'Hook',
  64. generator: (socket?:I.Socket) => (...args:any[]) => SubresT
  65. }
  66. export type CallInfo = BaseInfo & {
  67. type: 'Call',
  68. call: AnyFunction
  69. }
  70. export type RpcInfo = HookInfo | CallInfo
  71. export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
  72. export type OnFunction = <T extends "error" | "close">(type: T, f: FrontEndHandlerType[T]) => void
  73. export type HookCloseFunction<T = {}> = (res:SubscriptionResponse<T>, rpc:HookRPC<any, any, T>) => any