-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
174 lines (141 loc) · 4.77 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
module.exports = exports;
/**
*
* @param {Object} io socket.io reference after call listen
* @param {Object} sessionStore Connect session store instance
* @param {Function} cookieParser Connect cookie parser instance
* @param {Object} [options] to setup the socket. The optional object's properties are:
* {
* key: cookie name; by default 'connect.sid'
* autoErrManager: boolean that if it is 'true', then any error to retrieve the session
* is managed internally; otherwise provide an error parameter to the listen
* callbacks; by default 'false'.
* noSessIo: A boolean which if it is true, then the 'authentication' method that deals
* with with session and the sessOn method won't be applied to the io.sockets, so
* it will an original socket.io but with the possibility to get namespaced sockets
* with the session features.
* }
*
* @returns {Object} socket.io object (it has been prototyped with new behaviours in some original
* methods and provided a new ones)
*/
function expressionSocket(io, sessionStore, cookieParser, options) {
var key = 'connect.sid';
var autoErrManager = false;
var noSessIo = false;
if (options) {
if ('string' === typeof options.key) {
key = options.key;
}
if (options.autoErrManager === true) {
autoErrManager = true;
}
if (options.noSessIo === true) {
noSessIo = true;
}
}
var findCookie = function (handshake) {
return (handshake.secureCookies && handshake.secureCookies[key])
|| (handshake.signedCookies && handshake.signedCookies[key])
|| (handshake.cookies && handshake.cookies[key]) || false;
};
var authorization = function (callback) {
this.__proto__.authorization(function (handshake, fn) {
cookieParser(handshake, {}, function (parseErr) {
var cookieId = findCookie(handshake);
if ((parseErr) || (!cookieId)) {
if (autoErrManager) {
fn(parseErr, false);
return;
} else {
callback(parseErr, null, handshake, fn);
}
}
sessionStore.load(cookieId, function (storeErr, session) {
if (autoErrManager) {
if ((storeErr) || (!session)) {
fn(storeErr, false);
} else {
callback(session, handshake, fn);
}
} else {
callback(storeErr, session, handshake, fn);
}
});
});
});
};
var serverSessOn = function (event, callback) {
this.on(event, function (socket) {
cookieParser(socket.handshake, {}, function (parseErr) {
socket.expressionCookieId = findCookie(socket.handshake);
if ((parseErr) || (!socket.expressionCookieId)) {
if (autoErrManager) {
socket.disconnect();
return;
} else {
socket.clientSessOn = clientSessOn;
callback(parseErr, null, socket);
}
}
sessionStore.load(socket.expressionCookieId, function (storeErr, session) {
if (autoErrManager) {
if ((storeErr) || (!session)) {
socket.disconnect();
} else {
socket.clientSessOn = clientSessOn;
callback(session, socket);
}
} else {
socket.clientSessOn = clientSessOn;
callback(storeErr, session, socket);
}
});
});
});
};
var clientSessOn = function (event, callback) {
var self = this;
this.on(event, function (data, ackFn) {
sessionStore.load(self.expressionCookieId, function (storeErr, session) {
if (autoErrManager) {
if ((storeErr) || (!session)) {
self.disconnect();
} else {
if (!data) {
callback(session);
} else if (!ackFn) {
callback(session, data);
} else {
callback(session, data, ackFn);
}
}
} else {
if (!data) {
callback(storeErr, session);
} else if (!ackFn) {
callback(storeErr, session, data);
} else {
callback(storeErr, session, data, ackFn);
}
}
});
});
};
if (noSessIo === false) {
io.sockets.sessOn = serverSessOn;
io.sockets.authorization = authorization;
}
io.of = function (namespace, noSession) {
var nsSockets = this.__proto__.of.call(this, namespace);
if (noSession === true) {
return nsSockets;
}
nsSockets.sessOn = serverSessOn;
nsSockets.authorization = authorization;
return nsSockets;
};
return io;
}
module.exports = expressionSocket;