UNPKG

helene

Version:
81 lines (80 loc) 3.19 kB
import { MethodParams, ServerMethods } from '../utils'; import { RequestListener } from 'http'; import { RedisClientOptions } from 'redis'; import WebSocket from 'ws'; import { z } from 'zod'; import { ClientNode } from './client-node'; import { Event } from './event'; import { Method, MethodFunction, MethodOptions } from './method'; import { ServerChannel } from './server-channel'; import { HttpTransport, RedisTransport, WebSocketTransport } from './transports'; declare global { var Helene: Server; } export type ChannelChecker = (node: ClientNode, channel: string) => Promise<boolean>; export type AuthFunction = (this: ClientNode, context: any) => any; export type RateLimit = boolean | { max: number; interval: number; }; export type ServerOptions = { host?: string; port?: number; auth?: AuthFunction; origins?: string[]; debug?: boolean; ws?: Partial<WebSocket.ServerOptions>; redis?: RedisClientOptions | boolean; requestListener?: RequestListener; globalInstance?: boolean; allowedContextKeys?: string[]; rateLimit?: RateLimit; shouldAllowChannelSubscribe?: ChannelChecker; }; export type ProxyMethodCreation = { [key: string]: ProxyMethodCreation; } & any; export declare class Server<Methods extends ServerMethods = ServerMethods> extends ServerChannel { uuid: string; httpTransport: HttpTransport; webSocketTransport: WebSocketTransport; redisTransport: RedisTransport; host: string; port: number; requestListener: RequestListener; allowedContextKeys: string[]; isAuthEnabled: boolean; auth: AuthFunction; debug: boolean; rateLimit: RateLimit; methods: Map<string, Method<any, any>>; allClients: Map<string, ClientNode>; channels: Map<string, ServerChannel>; events: Map<string, Event>; m: ProxyMethodCreation; acceptConnections: boolean; ready: boolean; shouldAllowChannelSubscribe: ChannelChecker; static ERROR_EVENT: string; handlers: Methods; constructor({ host, port, debug, origins, ws, redis, requestListener, globalInstance, allowedContextKeys, rateLimit, }?: ServerOptions); isReady(): Promise<unknown>; get express(): import("express").Express; setAuth({ auth, logIn }: { auth: AuthFunction; logIn: MethodFunction; }): void; setChannelAuthorization(checker: ChannelChecker): void; close(): Promise<boolean>; static(path: string, catchAll: boolean): void; debugger(...args: any[]): void; call<T = any>(method: string, params?: MethodParams<T>): Promise<any>; createDefaultMethods(): void; getMethod(method: string): Method<any, any>; addClient(node: ClientNode): void; deleteClient(node: ClientNode): void; addMethod<T = any, R = any, Schema extends z.ZodUndefined | z.ZodObject<any> = z.ZodUndefined>(method: string, fn: MethodFunction<Schema extends z.ZodUndefined ? T : z.input<Schema>, R>, opts?: MethodOptions<Schema>): void; channel(name?: string | object): ServerChannel; } export type InferServerMethods<T extends Server<any>> = T['handlers']; export declare function createServer(options?: ServerOptions): Server<ServerMethods>;