forked from middyjs/middy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpHeaderNormalizer.js
88 lines (76 loc) · 1.98 KB
/
httpHeaderNormalizer.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
module.exports = (opts) => {
const exceptionsList = [
'ALPN',
'C-PEP',
'C-PEP-Info',
'CalDAV-Timezones',
'Content-ID',
'Content-MD5',
'DASL',
'DAV',
'DNT',
'ETag',
'GetProfile',
'HTTP2-Settings',
'Last-Event-ID',
'MIME-Version',
'Optional-WWW-Authenticate',
'Sec-WebSocket-Accept',
'Sec-WebSocket-Extensions',
'Sec-WebSocket-Key',
'Sec-WebSocket-Protocol',
'Sec-WebSocket-Version',
'SLUG',
'TCN',
'TE',
'TTL',
'WWW-Authenticate',
'X-ATT-DeviceId',
'X-DNSPrefetch-Control',
'X-UIDH'
]
const exceptions = exceptionsList.reduce((acc, curr) => {
acc[curr.toLowerCase()] = curr
return acc
}, {})
const normalizeHeaderKey = (key) => {
if (exceptions[key.toLowerCase()]) {
return exceptions[key.toLowerCase()]
}
return key
.split('-')
.map(text =>
text.charAt(0).toUpperCase() + text.substr(1).toLowerCase()
)
.join('-')
}
const defaults = {
normalizeHeaderKey
}
const options = Object.assign({}, defaults, opts)
return ({
before: (handler, next) => {
if (handler.event.headers) {
const rawHeaders = {}
const headers = {}
Object.keys(handler.event.headers).forEach((key) => {
rawHeaders[key] = handler.event.headers[key]
headers[options.normalizeHeaderKey(key)] = handler.event.headers[key]
})
handler.event.headers = headers
handler.event.rawHeaders = rawHeaders
}
if (handler.event.multiValueHeaders) {
const rawHeaders = {}
const headers = {}
Object.keys(handler.event.multiValueHeaders).forEach((key) => {
rawHeaders[key] = handler.event.multiValueHeaders[key]
headers[options.normalizeHeaderKey(key)] = handler.event.multiValueHeaders[key]
})
handler.event.multiValueHeaders = headers
handler.event.rawMultiValueHeaders = rawHeaders
}
next()
}
})
}