UNPKG

web-dev-server

Version:

Node.js simple http server for common development or training purposes.

139 lines (138 loc) 5.52 kB
/// <reference types="node" /> import { Request } from "../Request"; import { Response } from "../Response"; import { INamespace } from "./Sessions/INamespace"; export declare class Session { static readonly LIFETIMES: { MINUTE: number; HOUR: number; DAY: number; WEEK: number; MONTH: number; YEAR: number; }; static GC_INTERVAL: number; static LOCK_CHECK_INTERVAL: number; protected static store: Map<string, Session>; protected static maxLockWaitTime: number; protected static cookieName: string; protected static maxLifeTimeMiliSeconds: number; protected static garbageCollecting: NodeJS.Timeout; protected static loadHandler: (id: string, store: Map<string, Session>, exists: boolean) => Promise<void>; protected static writeHandler: (id: string, store: Map<string, Session>) => Promise<void>; /** * @summary Set max waiting time in seconds to unlock session for another request. * @param maxLockWaitTime */ static SetMaxLockWaitTime(maxLockWaitTime: number): typeof Session; /** * @summary Get max waiting time in seconds to unlock session for another request. */ static GetMaxLockWaitTime(): number; /** * @summary Set used cookie name to identify user session. * @param cookieName */ static SetCookieName(cookieName: string): typeof Session; /** * @summary Get used cookie name to identify user session. */ static GetCookieName(): string; /** * @summary Set max. lifetime for all sessions and it's namespaces. * `0` means unlimited, 30 days by default. * @param maxLifeTimeSeconds */ static SetMaxLifeTime(maxLifeTimeSeconds: number): typeof Session; /** * @summary Get max. lifetime for all sessions and it's namespaces in seconds. */ static GetMaxLifeTime(): number; /** * Destroy all running sessions. */ static DestroyAll(): typeof Session; /** * @summary Set custom session load handler. * Implement any functionality to assign session instance under it's id into given store. * @param loadHandler */ static SetLoadHandler(loadHandler: (id: string, store: Map<string, Session>, exists: boolean) => Promise<void>): typeof Session; /** * @summary Set custom session write handler. * Implement any functionality to store session instance under it's id from given store anywhere else. * @param writeHandler */ static SetWriteHandler(writeHandler: (id: string, store: Map<string, Session>) => Promise<void>): typeof Session; /** * Start session based on cookies and data stored in current process. * @param request * @param response */ static Start(request: Request, response?: Response): Promise<Session>; /** * @summary Check if any session data exists for given request. * @param request */ static Exists(request: Request): Promise<boolean>; /** * @summary Get session object by session id or `null`. * Returned session could be already locked by another request. * @param sessionId */ static Get(sessionId: string): Promise<Session>; /** * @summary Set session object with session id and optional data or lock * into global store. If there is configured any write handler, then the * handler is invoked for this session id. * @param session */ static Set(session: Session): Promise<typeof Session>; protected static setUpResponse(session: Session, response: Response): void; protected static getRequestIdOrNew(request: Request): string; protected static runGarbageCollectingIfNecessary(): void; protected static waitToUnlock(id: string): Promise<Session>; /** * @summary Get session id string. */ GetId(): string; /** * @summary Get if session is locked. */ IsLocked(): boolean; /** * @summary Lock current session and prevent other request to using it. * Session is locked automaticly on session start. Use this method very carefully. */ Lock(): this; /** * @summary Unlock current session and allow other request to using it. * Session is unlocked automaticly on response send. Use this method very carefully. */ Unlock(): this; /** * @summary Wait until this session is unlocked by another request end. */ WaitToUnlock(): Promise<this>; /** * @summary Get new or existing session namespace instance. * @param name Session namespace unique name. */ GetNamespace(name?: string): INamespace; /** * @summary Destroy all namespaces and this session for current user. */ Destroy(): void; protected setLastAccessTime(lastAccessTime: number): Session; protected destroyNamespace(name: string): Session; protected setNamespaceExpirationHoops(name: string, hoopsCount: number): Session; protected setNamespaceExpirationTime(name: string, seconds: number): Session; protected init(): void; protected id: string; protected locked: boolean; protected lastAccessTime: number; protected namespacesHoops: Map<string, number>; protected namespacesExpirations: Map<string, number>; protected namespaces: Map<string, INamespace>; constructor(id: string, locked?: boolean); }