123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import * as I from "./Interfaces";
- import { RPCSocket } from "./Frontend";
- import { ManagerOptions, SocketOptions } from 'socket.io-client'
-
- export type PioBindListener = (...args: any) => void
- export type PioHookListener = GenericFunction
-
- export type GenericFunction<Parameters extends any[] = any[], Result = any> = {(...args: Parameters): Result}
-
- export type BackendHook<Func extends GenericFunction> =
- GenericFunction<
- [
- GenericFunction<
- Parameters<
- AsFunction<
- Parameters<Func>[0]
- >
- >,
- void
- >,
- ...Tail<Parameters<Func>>
- ],
- ReturnType<Func>
- >
-
-
- export type AccessFilter<InterfaceT extends RPCInterface = RPCInterface> = (sesame: string | undefined, exporter: I.RPCExporter<InterfaceT, keyof InterfaceT>) => Promise<boolean> | boolean
- export type Visibility = "127.0.0.1" | "0.0.0.0"
- export type ConnectionHandler = (socket: I.Socket) => void
- export type ErrorHandler = (socket: I.Socket, error: any, rpcName: string, args: any[]) => void
- export type CloseHandler = (socket: I.Socket) => void
- export type SesameFunction = (sesame: string) => boolean
- export type SesameConf = {
- sesame?: string | SesameFunction
- }
- export type FrontEndHandlerType = {
- 'error': (e: any) => void
- 'close': () => void
- }
- export type ClientConfig = Partial<ManagerOptions & SocketOptions> & {
- protocol?: 'http' | 'https',
- callTimeoutMs?: number
- }
-
- export type SomeOf<T> = {
- [key in keyof T]?: T[key]
- }
-
- export type ExporterArray<InterfaceT extends RPCInterface = RPCInterface> = I.RPCExporter<RPCInterface<InterfaceT>, keyof InterfaceT>[]
-
- export type ConnectedSocket<T extends RPCInterface = RPCInterface> = RPCSocket & AsyncIfc<T>
-
- export type ServerConf<InterfaceT extends RPCInterface> = {
- accessFilter?: AccessFilter<InterfaceT>
- connectionHandler?: ConnectionHandler
- errorHandler?: ErrorHandler
- closeHandler?: CloseHandler
- throwOnUnknownRPC?: boolean
- } & SesameConf
-
- export type ResponseType = "Subscribe" | "Success" | "Error"
- export type Outcome = "Success" | "Error"
-
- export type Respose<T> = T & { result: Outcome }
- export type SuccessResponse<T = {}> = Respose<T> & { result: "Success" }
- export type ErrorResponse<T = {}> = Respose<T> & { result: "Error", message?: string }
-
- export type RPCType = 'Hook' | 'Unhook' | 'Call'
-
- export type CallRPC<Name, Func extends GenericFunction> = {
- name: Name
- call: Func
- }
-
- export type HookRPC<Name, Func extends GenericFunction> = {
- name: Name
- hook: BackendHook<Func>
- onCallback?: GenericFunction
- onDestroy?: HookCloseFunction<ReturnType<Func> extends Promise<infer T> ? T : ReturnType<Func>>
- }
-
- export type RPC<Name, Func extends GenericFunction> = HookRPC<Name, Func> | CallRPC<Name, Func> | Func
-
- export type RPCInterface<Impl extends RPCInterface = {}> = {
- [grp in string]: {
- [rpc in string]: GenericFunction
- }
- } & Impl
-
- export type exportT = {
- [group in string]: {}
- }
-
- export type RPCDefinitions<Ifc extends RPCInterface> = {
- [grp in keyof Ifc]: ({
- [rpc in keyof Ifc[grp]]: RPC<rpc, Ifc[grp][rpc]>
- }[keyof Ifc[grp]])[]
- }
-
- export type BaseInfo = {
- name: string,
- owner: string,
- argNames: string[],
- }
-
- export type HookInfo<SubresT = {}> = BaseInfo & {
- type: 'Hook',
- generator: (socket?: I.Socket) => (...args: any[]) => SubresT
- }
-
- export type CallInfo = BaseInfo & {
- type: 'Call',
- call: GenericFunction
- }
-
- export type RpcInfo = HookInfo | CallInfo
- export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
-
- export type OnFunction = <T extends "error" | "close">(type: T, f: FrontEndHandlerType[T]) => void
- export type HookCloseFunction<T> = (res: T, rpc: HookRPC<any, any>) => any
-
-
- export type AsyncIfc<Ifc extends RPCInterface> = { [grp in keyof Ifc]: { [rpcname in keyof Ifc[grp]]: AsyncGenericFunction<Ifc[grp][rpcname]> } }
-
- export type AsyncGenericFunction<F extends GenericFunction = GenericFunction> = F extends (...args: Parameters<F>) => infer R
- ? ((...args: Parameters<F>) => R extends Promise<any> ? R : Promise<R>)
- : Promise<any>
-
- type Destroyable = { destroy: () => void }
- export type Callback<Params extends any[] = []> =
- (this: Destroyable, ...args: Params) => void
-
- type AsFunction<F> = F extends GenericFunction ? F : GenericFunction
-
- type Last<Tuple extends any[]> = Tuple[ Subtract<Length<Tuple>, 1> ]
- type Tail<T extends any[]> = T extends [any, ...infer R] ? R : any[]
-
- type Length<T extends any[]> =
- T extends { length: infer L } ? L : never;
-
- type BuildTuple<L extends number, T extends any[] = []> =
- T extends { length: L } ? T : BuildTuple<L, [...T, any]>;
-
- type Subtract<A extends number, B extends number> =
- BuildTuple<A> extends [...(infer U), ...BuildTuple<B>]
- ? Length<U>
- : never;
|