-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmemory.ts
More file actions
62 lines (48 loc) · 1.24 KB
/
memory.ts
File metadata and controls
62 lines (48 loc) · 1.24 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
export class Memory {
sessions: { [key: string]: any }
constructor(
sessions: { [key: string]: any } = Object.create(null)
) {
this.sessions = Object.create(null)
}
public getSession(sessionId: string) {
var sess = this.sessions[sessionId]
if (!sess) {
return
}
// parse
sess = JSON.parse(sess)
if (sess.cookie) {
var expires = typeof sess.cookie.expires === 'string'
? new Date(sess.cookie.expires)
: sess.cookie.expires
// destroy expired session
if (expires && expires <= Date.now()) {
delete this.sessions[sessionId]
return
}
}
return sess
}
public all() {
var sessionIds = Object.keys(this.sessions)
var sessions = Object.create(null)
for (var i = 0; i < sessionIds.length; i++) {
var sessionId = sessionIds[i]
var session = this.getSession(sessionId)
if (session) {
sessions[sessionId] = session;
}
}
return sessions
}
public set(sessionId: string, session: any) {
this.sessions[sessionId] = JSON.stringify(session)
}
public get(sessionId: string) {
return this.getSession(sessionId)
}
public destroy(sessionId: string) {
delete this.sessions[sessionId]
}
}