mirror of
https://github.com/overleaf/overleaf.git
synced 2025-02-17 04:51:33 +00:00
See the docs of OError.tag: https://github.com/overleaf/o-error#long-stack-traces-with-oerrortag (currently at 221dd902e7bfa0ee92de1ea5a3cbf3152c3ceeb4) I am tagging all errors at each async hop. Most of the controller code will only ever see already tagged errors -- or new errors created in our app code. They should have enough info that we do not need to tag them again.
35 lines
1 KiB
JavaScript
35 lines
1 KiB
JavaScript
const OError = require('@overleaf/o-error')
|
|
const { EventEmitter } = require('events')
|
|
|
|
module.exports = function (io, sessionStore, cookieParser, cookieName) {
|
|
const missingSessionError = new Error('could not look up session by key')
|
|
|
|
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
|
|
}
|