2021-10-27 08:06:50 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
|
|
|
|
|
|
|
function errSerializer(err) {
|
|
|
|
if (!err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
message: err.message,
|
|
|
|
name: err.name,
|
2021-11-09 10:49:57 -05:00
|
|
|
stack: err.stack && OError.getFullStack(err),
|
2021-10-27 08:06:50 -04:00
|
|
|
info: OError.getFullInfo(err),
|
|
|
|
code: err.code,
|
|
|
|
signal: err.signal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function reqSerializer(req) {
|
|
|
|
if (!req) {
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
const headers = req.headers || {}
|
2021-11-09 10:49:57 -05:00
|
|
|
const entry = {
|
2021-10-27 08:06:50 -04:00
|
|
|
method: req.method,
|
|
|
|
url: req.originalUrl || req.url,
|
|
|
|
remoteAddress: getRemoteIp(req),
|
|
|
|
headers: {
|
|
|
|
referer: headers.referer || headers.referrer,
|
|
|
|
'user-agent': headers['user-agent'],
|
|
|
|
'content-length': headers['content-length']
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 10:49:57 -05:00
|
|
|
const projectId =
|
|
|
|
req.params.projectId || req.params.project_id || req.params.Project_id
|
|
|
|
const userId = req.params.userId || req.params.user_id
|
|
|
|
const docId = req.params.docId || req.params.doc_id
|
|
|
|
if (projectId) {
|
|
|
|
entry.projectId = projectId
|
|
|
|
}
|
|
|
|
if (userId) {
|
|
|
|
entry.userId = userId
|
|
|
|
}
|
|
|
|
if (docId) {
|
|
|
|
entry.docId = docId
|
|
|
|
}
|
|
|
|
return entry
|
2021-10-27 08:06:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function resSerializer(res) {
|
|
|
|
if (!res) {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
statusCode: res.statusCode,
|
|
|
|
headers: {
|
|
|
|
'content-length': res.getHeader && res.getHeader('content-length')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRemoteIp(req) {
|
|
|
|
if (req.ip) {
|
|
|
|
return req.ip
|
|
|
|
}
|
|
|
|
if (req.socket) {
|
|
|
|
if (req.socket.socket && req.socket.socket.remoteAddress) {
|
|
|
|
return req.socket.socket.remoteAddress
|
|
|
|
} else if (req.socket.remoteAddress) {
|
|
|
|
return req.socket.remoteAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { err: errSerializer, req: reqSerializer, res: resSerializer }
|