-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmemory-store.ts
40 lines (34 loc) · 949 Bytes
/
memory-store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { SessionData, SessionStore } from "./types";
export default class MemoryStore implements SessionStore {
store: Map<string, string>;
constructor() {
this.store = new Map();
}
async get(sid: string): Promise<SessionData | null> {
const sess = this.store.get(sid);
if (sess) {
const session = JSON.parse(sess, (key, value) => {
if (key === "expires") return new Date(value);
return value;
}) as SessionData;
if (
session.cookie.expires &&
session.cookie.expires.getTime() <= Date.now()
) {
await this.destroy(sid);
return null;
}
return session;
}
return null;
}
async set(sid: string, sess: SessionData) {
this.store.set(sid, JSON.stringify(sess));
}
async destroy(sid: string) {
this.store.delete(sid);
}
async touch(sid: string, sess: SessionData) {
this.store.set(sid, JSON.stringify(sess));
}
}