-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepareForLogging.js
40 lines (35 loc) · 1017 Bytes
/
prepareForLogging.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
var obfuscateUrlPassword = require('../obfuscateUrlPassword')
module.exports = function prepareForLogging (r) {
return removeUndefined({
method: r.method,
url: r.url && obfuscateUrlPassword(r.url),
headers: r.headers && obfuscateHeaders(r.headers),
body: r.stringBody !== undefined ? r.stringBody : r.body ? '[Stream]' : undefined,
statusCode: r.statusCode,
statusText: r.statusText
})
}
function obfuscateHeaders (headers) {
var result = {}
Object.keys(headers).forEach(function (key) {
if (key.toLowerCase() === 'authorization') {
var auth = headers[key].split(/\s*/g)
if (auth.length > 1) {
result[key] = String(headers[key]).split(' ')[0] + ' ********'
} else {
result[key] = '********'
}
} else {
result[key] = headers[key]
}
})
return result
}
function removeUndefined (obj) {
Object.keys(obj).forEach(function (key) {
if (typeof obj[key] === 'undefined') {
delete obj[key]
}
})
return obj
}