2020-06-23 13:29:38 -04:00
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Sanity-check the conversion and remove this comment.
|
2020-06-23 13:29:34 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-06-23 13:29:44 -04:00
|
|
|
const { EventEmitter } = require('events')
|
2020-06-06 08:37:40 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
module.exports = function (io, sessionStore, cookieParser, cookieName) {
|
|
|
|
const missingSessionError = new Error('could not look up session by key')
|
2020-06-06 08:37:40 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
const sessionSockets = new EventEmitter()
|
|
|
|
const next = (error, socket, session) =>
|
|
|
|
sessionSockets.emit('connection', error, socket, session)
|
2020-06-06 08:37:40 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
io.on('connection', function (socket) {
|
|
|
|
const req = socket.handshake
|
|
|
|
return cookieParser(req, {}, function () {
|
|
|
|
const sessionId = req.signedCookies && req.signedCookies[cookieName]
|
|
|
|
if (!sessionId) {
|
|
|
|
return next(missingSessionError, socket)
|
|
|
|
}
|
|
|
|
return sessionStore.get(sessionId, function (error, session) {
|
|
|
|
if (error) {
|
|
|
|
return next(error, socket)
|
|
|
|
}
|
|
|
|
if (!session) {
|
|
|
|
return next(missingSessionError, socket)
|
|
|
|
}
|
|
|
|
return next(null, socket, session)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-06-06 08:37:40 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
return sessionSockets
|
|
|
|
}
|