mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-27 21:41:27 +00:00
425052ff91
[misc] migrate to OError tagging/wrapping
36 lines
1.1 KiB
JavaScript
36 lines
1.1 KiB
JavaScript
const OError = require('@overleaf/o-error')
|
|
const { EventEmitter } = require('events')
|
|
const { MissingSessionError } = require('./Errors')
|
|
|
|
module.exports = function (io, sessionStore, cookieParser, cookieName) {
|
|
const missingSessionError = new MissingSessionError()
|
|
|
|
const sessionSockets = new EventEmitter()
|
|
function next(error, socket, session) {
|
|
sessionSockets.emit('connection', error, socket, session)
|
|
}
|
|
|
|
io.on('connection', function (socket) {
|
|
const req = socket.handshake
|
|
cookieParser(req, {}, function () {
|
|
const sessionId = req.signedCookies && req.signedCookies[cookieName]
|
|
if (!sessionId) {
|
|
return next(missingSessionError, socket)
|
|
}
|
|
sessionStore.get(sessionId, function (error, session) {
|
|
if (error) {
|
|
OError.tag(error, 'error getting session from sessionStore', {
|
|
sessionId
|
|
})
|
|
return next(error, socket)
|
|
}
|
|
if (!session) {
|
|
return next(missingSessionError, socket)
|
|
}
|
|
next(null, socket, session)
|
|
})
|
|
})
|
|
})
|
|
|
|
return sessionSockets
|
|
}
|