UNPKG

3.04 kBTypeScriptView Raw
1import * as session from 'express-session';
2import { Collection, MongoClient, MongoClientOptions, WriteConcernSettings } from 'mongodb';
3export declare type CryptoOptions = {
4 secret: false | string;
5 algorithm?: string;
6 hashing?: string;
7 encodeas?: string;
8 key_size?: number;
9 iv_size?: number;
10 at_size?: number;
11};
12export declare type ConnectMongoOptions = {
13 mongoUrl?: string;
14 clientPromise?: Promise<MongoClient>;
15 client?: MongoClient;
16 collectionName?: string;
17 mongoOptions?: MongoClientOptions;
18 dbName?: string;
19 ttl?: number;
20 touchAfter?: number;
21 stringify?: boolean;
22 createAutoRemoveIdx?: boolean;
23 autoRemove?: 'native' | 'interval' | 'disabled';
24 autoRemoveInterval?: number;
25 serialize?: (a: any) => any;
26 unserialize?: (a: any) => any;
27 writeOperationOptions?: WriteConcernSettings;
28 transformId?: (a: any) => any;
29 crypto?: CryptoOptions;
30};
31export default class MongoStore extends session.Store {
32 private clientP;
33 private crypto;
34 private timer?;
35 collectionP: Promise<Collection>;
36 private options;
37 private transformFunctions;
38 constructor({ collectionName, ttl, mongoOptions, autoRemove, autoRemoveInterval, touchAfter, stringify, crypto, ...required }: ConnectMongoOptions);
39 static create(options: ConnectMongoOptions): MongoStore;
40 private setAutoRemove;
41 private computeStorageId;
42 /**
43 * promisify and bind the `this.crypto.get` function.
44 * Please check !!this.crypto === true before using this getter!
45 */
46 private get cryptoGet();
47 /**
48 * Decrypt given session data
49 * @param session session data to be decrypt. Mutate the input session.
50 */
51 private decryptSession;
52 /**
53 * Get a session from the store given a session ID (sid)
54 * @param sid session ID
55 */
56 get(sid: string, callback: (err: any, session?: session.SessionData | null) => void): void;
57 /**
58 * Upsert a session into the store given a session ID (sid) and session (session) object.
59 * @param sid session ID
60 * @param session session object
61 */
62 set(sid: string, session: session.SessionData, callback?: (err: any) => void): void;
63 touch(sid: string, session: session.SessionData & {
64 lastModified?: Date;
65 }, callback?: (err: any) => void): void;
66 /**
67 * Get all sessions in the store as an array
68 */
69 all(callback: (err: any, obj?: session.SessionData[] | {
70 [sid: string]: session.SessionData;
71 } | null) => void): void;
72 /**
73 * Destroy/delete a session from the store given a session ID (sid)
74 * @param sid session ID
75 */
76 destroy(sid: string, callback?: (err: any) => void): void;
77 /**
78 * Get the count of all sessions in the store
79 */
80 length(callback: (err: any, length: number) => void): void;
81 /**
82 * Delete all sessions from the store.
83 */
84 clear(callback?: (err: any) => void): void;
85 /**
86 * Close database connection
87 */
88 close(): Promise<void>;
89}