forked from JacksonTian/ping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.js
executable file
·49 lines (47 loc) · 1.51 KB
/
session.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
42
43
44
45
46
47
48
49
var Session = function (sessionId) {
this.sessionId = sessionId;
this._map = {};
};
Session.prototype.set = function (name, value) {
this._map[name] = value;
};
Session.prototype.get = function (name) {
return this._map[name];
};
Session.prototype.remove = function (key) {
delete this._map[key];
};
Session.prototype.removeAll = function () {
delete this._map;
this._map = {};
};
Session.prototype.updateTime = function () {
this._updateTime = new Date().getTime();
};
var SESSIONID_KEY = exports.SESSIONID_KEY = "session_id";
var SessionManager = function (timeout) {
this.timeout = timeout;
this._sessions = {};
};
SessionManager.prototype.renew = function (response) {
var that = this;
var sessionId = [new Date().getTime(), Math.round(Math.random() * 1000)].join("");
var session = new Session(sessionId);
session.updateTime();
this._sessions[sessionId] = session;
var clientTimeout = 30 * 24 * 60 * 60 * 1000;
var cookie = {key: SESSIONID_KEY, value: sessionId, path: "/", expires: new Date().getTime() + clientTimeout};
response.setCookie(cookie);
return session;
};
SessionManager.prototype.get = function (sessionId) {
return this._sessions[sessionId];
};
SessionManager.prototype.remove = function (sessionId) {
delete this._sessions[sessionId];
};
SessionManager.prototype.isTimeout = function (session) {
return (session._updateTime + this.timeout) < new Date().getTime();
};
exports.Session = Session;
exports.SessionManager = SessionManager;