UNPKG

extensor

Version:

Extra funcionalities to socket.io

116 lines (115 loc) 2.85 kB
/// <reference types="node" /> import { Socket } from "socket.io"; import { kSocketAuthStatus, kSocketAuthTimeout } from "./symbols"; export interface Storage { get(key: string): Promise<string | null>; set(key: string, value: any): Promise<any>; del(key: string): Promise<number>; deleteAll(keys: string[]): Promise<number | void | null>; } /** * Authorization options */ export declare type AuthOptions = { /** * Authorization timeout, in ms * @default false // Don't set timeout */ timeout?: number | boolean; /** * Allowed events before authentication * @default 'Extensor defaults' */ authorizedEvents?: string[]; }; /** * Force unique connection options */ export declare type UniqueOptions = { /** * Identifier for prevent multiple connection attemp * @default false Mix of ip and user-agent */ identifier?: string; /** * Set error handler for unique middleware * @default 'e => debug("%s: %s", e.local, e.message);' */ onError?: (local: string, error: Error, socket: Socket) => void; /** * Manager of connections state data * @default ExtensorSimpleStore */ storage?: Storage; }; export declare type AuthResultResponse = { error?: string; merge: { [prop: string]: any; }; }; export declare type AuthHandler = (ctx: { /** * Socket that requests authentication */ socket: SocketIO.Socket; /** * Sent data */ data: any; /** * Resolve authentication */ done: (response: AuthDoneResponse) => void; }) => AuthDoneResponse | void | Promise<AuthDoneResponse>; export declare type AuthDoneResponse = boolean | object | Error; export declare type Schema = string | string[] | { [prop: string]: Schema | Schema[]; }; /** * Parser schemas * @example * { * chat: { * id: 1, * schema: { * content: "string" * } * } * } */ export declare type ParserMapSchemas = { [eventName: string]: { id: number; schema: Schema; }; }; export declare type ParserIDMap = { [id: number]: string; }; export declare type ParserPacket = { type: number; data: any[]; nsp: string; id?: number; options?: { compress: boolean; }; }; export declare type Parser = { encode: (data: any) => Buffer; decode: <T>(buffer: Buffer) => T | any; }; export declare type ParsersList = { [event: string]: Parser; }; declare module "socket.io" { interface Socket { [kSocketAuthStatus]: boolean; [kSocketAuthTimeout]?: NodeJS.Timeout; /** * Handler socket authentication */ auth: Promise<unknown>; } }