UNPKG

express-session-etcd3

Version:

An ETCD v3 store adapter for express-session using etcd3 client.

118 lines (117 loc) 4.39 kB
/// <reference types="express-session" /> import { Store } from 'express-session'; import { Etcd3, IOptions } from 'etcd3'; /** * One day in seconds. */ export declare const oneDay = 86400; /** * Max TTL time to leasing on ETCD3 Client in seconds. */ export declare const maxTTL = 6442450; /** * Configuration options for the etcd v3 */ export interface Etcd3StoreOptions extends IOptions { /** * Prefix used to record the keys of all the sessions. * * Defaults to `sess`. */ prefix?: string; /** * Option to skip touching process that express does every time it reads the session. * This is useful if you work with big TTL and wanna free your ETCD from this extra access. * * Defaults to `false`. */ skipTouch?: boolean; } /** * Default configuration values for the etcd v3 options */ export declare const defaultOptions: Etcd3StoreOptions; /** * An etcd v3 store adapter for Express session using [etcd3](https://github.com/mixer/etcd3) client. * * ``` * var session = require('express-session'); * var Etcd3Store = require('express-session-etcd3'); * * app.use(session({ * store: new Etcd3Store(options), * secret: 'keyboard cat', * resave: false * })); * ``` */ export default class Etcd3Store extends Store { private config; private client; private debug; constructor(config?: Etcd3StoreOptions, client?: Etcd3); /** * This method is used to get a session from the store given a session * ID (`sid`). The `callback` should be called as `callback(error, session)`. * * The `session` argument should be a session if found, otherwise `null` or * `undefined` if the session was not found (and there was no error). A special * case is made when `error.code === 'ENOENT'` to act like `callback(null, null)`. */ get: (sid: string, callback: (err: any, session: Express.SessionData) => void) => void; /** * This required method is used to upsert a session into the store given a * session ID (`sid`) and session (`session`) object. The callback should be * called as `callback(error)` once the session has been set in the store. */ set: (sid: string, session: Express.SessionData, callback: (err: any) => void) => void; /** * This method is used to "touch" a given session given a * session ID (`sid`) and session (`session`) object. The `callback` should be * called as `callback(error)` once the session has been touched. * * This is primarily used when the store will automatically delete idle sessions * and this method is used to signal to the store the given session is active, * potentially resetting the idle timer. */ touch: (sid: string, session: Express.SessionData, callback: (err: any) => void) => void; /** * This method is used to get all sessions in the store as an array. The * `callback` should be called as `callback(error, sessions)`. */ all: (callback: (err: any, obj: { [sid: string]: Express.SessionData; }) => void) => void; /** * This method is used to get the count of all sessions in the store. * The `callback` should be called as `callback(error, len)`. */ length: (callback: (err: any, length: number) => void) => void; /** * This method is used to destroy/delete a session from the store given * a session ID (`sid`). The `callback` should be called as `callback(error)` * once the session is destroyed. */ destroy: (sid: string, callback: (err: any) => void) => void; /** * This method is used to delete all sessions from the store. The `callback` * should be called as `callback(error)` once the store is cleared. */ clear: (callback: (err: any) => void) => void; /** * Build the etcd key with the right prefix and the givin session ID (`sid`) */ private key(sid?); /** * Get the Time to Live (`ttl`) of the session */ private getTTL(sess, sid); /** * Get the raw Time to Live (`ttl`) of the session from the data sources */ private getRawTTL(sess, sid); /** * Logging callback result */ private callbackWithLog(cb, err?, value?); }