-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (79 loc) · 2.39 KB
/
index.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'use strict';
var Cookie = require('express-session').Cookie;
var Session = require('express-session').Session;
var PrimusError = require('primus').PrimusError;
//
// Expose the configuration function.
//
module.exports = function configure(options) {
var key = options.key || 'connect.sid'
, store = options.store
, primus = this;
if (!store) {
//
// Throw an error when the session store is not passed.
//
var message = 'Session middleware configuration failed due to missing '
+ '`store` option';
throw new PrimusError(message, this);
}
// Source: https://github.com/expressjs/session/blob/master/session/store.js#L76
var createSession = function(req, sess){
var expires = sess.cookie.expires
, orig = sess.cookie.originalMaxAge;
sess.cookie = new Cookie(sess.cookie);
if ('string' == typeof expires) sess.cookie.expires = new Date(expires);
sess.cookie.originalMaxAge = orig;
req.session = new Session(req, sess);
return req.session;
};
//
// The actual session middleware. This middleware is async so we need 3
// arguments.
//
function session(req, res, next) {
//
// The session id is stored in the cookies.
// `req.signedCookies` is assigned by the `cookie-parser` middleware.
//
var sid = req.signedCookies[key];
//
// Default to an empty session.
//
req.session = {};
// We need this for createSession() to work
req.sessionID = sid;
req.sessionStore = store;
//
// If we don't have a session id we are done.
//
if (!sid) return next();
//
// Pause the request before retrieving the session. This ensures that no
// `data` event is lost while we perform our async call.
//
req.pause();
//
// Grab the session from the store.
//
store.get(sid, function (err, session) {
//
// At this point the request stream can resume emitting `data` events.
//
req.resume();
//
// We don't want to kill the connection when we get an error from the
// session store so we just log the error.
//
if (err) {
primus.emit('log', 'error', err);
return next();
}
// if (session) req.session = session;
if (session) req.session = createSession(req, session);
// if (session) req.session = Store.createSession(req, session);
next();
});
}
return session;
};