UNPKG

3.17 kBTypeScriptView Raw
1import * as session from 'express-session';
2import { Collection, MongoClient, MongoClientOptions, WriteConcernSettings } from 'mongodb';
3export 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 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};
31type InternalSessionType = {
32 _id: string;
33 session: any;
34 expires?: Date;
35 lastModified?: Date;
36};
37export default class MongoStore extends session.Store {
38 private clientP;
39 private crypto;
40 private timer?;
41 collectionP: Promise<Collection<InternalSessionType>>;
42 private options;
43 private transformFunctions;
44 constructor({ collectionName, ttl, mongoOptions, autoRemove, autoRemoveInterval, touchAfter, stringify, crypto, ...required }: ConnectMongoOptions);
45 static create(options: ConnectMongoOptions): MongoStore;
46 private setAutoRemove;
47 private computeStorageId;
48 /**
49 * promisify and bind the `this.crypto.get` function.
50 * Please check !!this.crypto === true before using this getter!
51 */
52 private get cryptoGet();
53 /**
54 * Decrypt given session data
55 * @param session session data to be decrypt. Mutate the input session.
56 */
57 private decryptSession;
58 /**
59 * Get a session from the store given a session ID (sid)
60 * @param sid session ID
61 */
62 get(sid: string, callback: (err: any, session?: session.SessionData | null) => void): void;
63 /**
64 * Upsert a session into the store given a session ID (sid) and session (session) object.
65 * @param sid session ID
66 * @param session session object
67 */
68 set(sid: string, session: session.SessionData, callback?: (err: any) => void): void;
69 touch(sid: string, session: session.SessionData & {
70 lastModified?: Date;
71 }, callback?: (err: any) => void): void;
72 /**
73 * Get all sessions in the store as an array
74 */
75 all(callback: (err: any, obj?: session.SessionData[] | {
76 [sid: string]: session.SessionData;
77 } | null) => void): void;
78 /**
79 * Destroy/delete a session from the store given a session ID (sid)
80 * @param sid session ID
81 */
82 destroy(sid: string, callback?: (err: any) => void): void;
83 /**
84 * Get the count of all sessions in the store
85 */
86 length(callback: (err: any, length: number) => void): void;
87 /**
88 * Delete all sessions from the store.
89 */
90 clear(callback?: (err: any) => void): void;
91 /**
92 * Close database connection
93 */
94 close(): Promise<void>;
95}
96export {};