-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionStore.js
41 lines (40 loc) · 1.03 KB
/
SessionStore.js
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
const os = require('os');
const path = require('path');
const fs = require('fs');
module.exports = class SessionStore{
constructor(dir=".koasess"){
this.sdir = path.resolve(os.tmpdir(), dir);
}
get(key){
const spath = path.resolve(this.sdir, key);
try{
if(fs.existsSync(spath)){
return JSON.parse(fs.readFileSync(spath, 'utf8'));
}
return null;
}catch(e){
console.error(e);
return null;
}
}
set(key, sess, maxAge){
try{
if(!fs.existsSync(this.sdir)){
fs.mkdirSync(this.sdir);
}
fs.writeFileSync(path.resolve(this.sdir, key), JSON.stringify(sess));
}catch(e){
console.error(e);
}
}
destroy(key){
try{
const spath = path.resolve(this.sdir, key);
if(fs.existsSync(spath)){
fs.unlinkSync(spath);
}
}catch(e){
console.err(e)
}
}
}