@middy/http-header-normalizer 5.0.0-alpha.1 → 5.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +77 -92
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,96 +1,81 @@
1
1
  const exceptionsList = [
2
- 'ALPN',
3
- 'C-PEP',
4
- 'C-PEP-Info',
5
- 'CalDAV-Timezones',
6
- 'Content-ID',
7
- 'Content-MD5',
8
- 'DASL',
9
- 'DAV',
10
- 'DNT',
11
- 'ETag',
12
- 'GetProfile',
13
- 'HTTP2-Settings',
14
- 'Last-Event-ID',
15
- 'MIME-Version',
16
- 'Optional-WWW-Authenticate',
17
- 'Sec-WebSocket-Accept',
18
- 'Sec-WebSocket-Extensions',
19
- 'Sec-WebSocket-Key',
20
- 'Sec-WebSocket-Protocol',
21
- 'Sec-WebSocket-Version',
22
- 'SLUG',
23
- 'TCN',
24
- 'TE',
25
- 'TTL',
26
- 'WWW-Authenticate',
27
- 'X-ATT-DeviceId',
28
- 'X-DNSPrefetch-Control',
29
- 'X-UIDH'
30
- ]
31
-
32
- const exceptions = exceptionsList.reduce((acc, curr) => {
33
- acc[curr.toLowerCase()] = curr
34
- return acc
35
- }, {})
36
-
37
- const normalizeHeaderKey = (key, canonical) => {
38
- const lowerCaseKey = key.toLowerCase()
39
- if (!canonical) {
40
- return lowerCaseKey
41
- }
42
-
43
- if (exceptions[lowerCaseKey]) {
44
- return exceptions[lowerCaseKey]
45
- }
46
-
47
- return lowerCaseKey
48
- .split('-')
49
- .map((text) => text[0].toUpperCase() + text.substr(1))
50
- .join('-')
51
- }
52
-
53
- const defaults = {
54
- canonical: false,
55
- normalizeHeaderKey
56
- }
57
-
58
- const httpHeaderNormalizerMiddleware = (opts = {}) => {
59
- const options = { ...defaults, ...opts }
60
-
61
- const httpHeaderNormalizerMiddlewareBefore = async (request) => {
62
- if (request.event.headers) {
63
- const rawHeaders = {}
64
- const headers = {}
65
-
66
- for (const key of Object.keys(request.event.headers)) {
67
- rawHeaders[key] = request.event.headers[key]
68
- headers[options.normalizeHeaderKey(key, options.canonical)] =
69
- request.event.headers[key]
70
- }
71
-
72
- request.event.headers = headers
73
- request.event.rawHeaders = rawHeaders
2
+ 'ALPN',
3
+ 'C-PEP',
4
+ 'C-PEP-Info',
5
+ 'CalDAV-Timezones',
6
+ 'Content-ID',
7
+ 'Content-MD5',
8
+ 'DASL',
9
+ 'DAV',
10
+ 'DNT',
11
+ 'ETag',
12
+ 'GetProfile',
13
+ 'HTTP2-Settings',
14
+ 'Last-Event-ID',
15
+ 'MIME-Version',
16
+ 'Optional-WWW-Authenticate',
17
+ 'Sec-WebSocket-Accept',
18
+ 'Sec-WebSocket-Extensions',
19
+ 'Sec-WebSocket-Key',
20
+ 'Sec-WebSocket-Protocol',
21
+ 'Sec-WebSocket-Version',
22
+ 'SLUG',
23
+ 'TCN',
24
+ 'TE',
25
+ 'TTL',
26
+ 'WWW-Authenticate',
27
+ 'X-ATT-DeviceId',
28
+ 'X-DNSPrefetch-Control',
29
+ 'X-UIDH'
30
+ ];
31
+ const exceptions = exceptionsList.reduce((acc, curr)=>{
32
+ acc[curr.toLowerCase()] = curr;
33
+ return acc;
34
+ }, {});
35
+ const normalizeHeaderKey = (key, canonical)=>{
36
+ const lowerCaseKey = key.toLowerCase();
37
+ if (!canonical) {
38
+ return lowerCaseKey;
74
39
  }
75
-
76
- if (request.event.multiValueHeaders) {
77
- const rawHeaders = {}
78
- const headers = {}
79
-
80
- for (const key of Object.keys(request.event.multiValueHeaders)) {
81
- rawHeaders[key] = request.event.multiValueHeaders[key]
82
- headers[options.normalizeHeaderKey(key, options.canonical)] =
83
- request.event.multiValueHeaders[key]
84
- }
85
-
86
- request.event.multiValueHeaders = headers
87
- request.event.rawMultiValueHeaders = rawHeaders
40
+ if (exceptions[lowerCaseKey]) {
41
+ return exceptions[lowerCaseKey];
88
42
  }
89
- }
90
-
91
- return {
92
- before: httpHeaderNormalizerMiddlewareBefore
93
- }
94
- }
43
+ return lowerCaseKey.split('-').map((text)=>text[0].toUpperCase() + text.substr(1)).join('-');
44
+ };
45
+ const defaults = {
46
+ canonical: false,
47
+ normalizeHeaderKey
48
+ };
49
+ const httpHeaderNormalizerMiddleware = (opts = {})=>{
50
+ const options = {
51
+ ...defaults,
52
+ ...opts
53
+ };
54
+ const httpHeaderNormalizerMiddlewareBefore = async (request)=>{
55
+ if (request.event.headers) {
56
+ const rawHeaders = {};
57
+ const headers = {};
58
+ for (const key of Object.keys(request.event.headers)){
59
+ rawHeaders[key] = request.event.headers[key];
60
+ headers[options.normalizeHeaderKey(key, options.canonical)] = request.event.headers[key];
61
+ }
62
+ request.event.headers = headers;
63
+ request.event.rawHeaders = rawHeaders;
64
+ }
65
+ if (request.event.multiValueHeaders) {
66
+ const rawHeaders = {};
67
+ const headers = {};
68
+ for (const key of Object.keys(request.event.multiValueHeaders)){
69
+ rawHeaders[key] = request.event.multiValueHeaders[key];
70
+ headers[options.normalizeHeaderKey(key, options.canonical)] = request.event.multiValueHeaders[key];
71
+ }
72
+ request.event.multiValueHeaders = headers;
73
+ request.event.rawMultiValueHeaders = rawHeaders;
74
+ }
75
+ };
76
+ return {
77
+ before: httpHeaderNormalizerMiddlewareBefore
78
+ };
79
+ };
80
+ export default httpHeaderNormalizerMiddleware;
95
81
 
96
- export default httpHeaderNormalizerMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-header-normalizer",
3
- "version": "5.0.0-alpha.1",
3
+ "version": "5.0.0-alpha.2",
4
4
  "description": "Http header normalizer middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -63,6 +63,6 @@
63
63
  },
64
64
  "gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1",
65
65
  "devDependencies": {
66
- "@middy/core": "5.0.0-alpha.1"
66
+ "@middy/core": "5.0.0-alpha.2"
67
67
  }
68
68
  }