@middy/http-header-normalizer 3.0.0-alpha.0 → 3.0.0-alpha.4
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.
- package/index.js +58 -0
- package/package.json +6 -4
package/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
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'];
|
|
2
|
+
const exceptions = exceptionsList.reduce((acc, curr) => {
|
|
3
|
+
acc[curr.toLowerCase()] = curr;
|
|
4
|
+
return acc;
|
|
5
|
+
}, {});
|
|
6
|
+
|
|
7
|
+
const normalizeHeaderKey = (key, canonical) => {
|
|
8
|
+
if (exceptions[key.toLowerCase()]) {
|
|
9
|
+
return exceptions[key.toLowerCase()];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (!canonical) {
|
|
13
|
+
return key.toLowerCase();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return key.split('-').map(text => text[0].toUpperCase() + text.substr(1).toLowerCase()).join('-');
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const defaults = {
|
|
20
|
+
canonical: false,
|
|
21
|
+
normalizeHeaderKey
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const httpHeaderNormalizerMiddleware = (opts = {}) => {
|
|
25
|
+
const options = { ...defaults,
|
|
26
|
+
...opts
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const httpHeaderNormalizerMiddlewareBefore = async request => {
|
|
30
|
+
if (request.event.headers) {
|
|
31
|
+
const rawHeaders = {};
|
|
32
|
+
const headers = {};
|
|
33
|
+
Object.keys(request.event.headers).forEach(key => {
|
|
34
|
+
rawHeaders[key] = request.event.headers[key];
|
|
35
|
+
headers[options.normalizeHeaderKey(key, options.canonical)] = request.event.headers[key];
|
|
36
|
+
});
|
|
37
|
+
request.event.headers = headers;
|
|
38
|
+
request.event.rawHeaders = rawHeaders;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (request.event.multiValueHeaders) {
|
|
42
|
+
const rawHeaders = {};
|
|
43
|
+
const headers = {};
|
|
44
|
+
Object.keys(request.event.multiValueHeaders).forEach(key => {
|
|
45
|
+
rawHeaders[key] = request.event.multiValueHeaders[key];
|
|
46
|
+
headers[options.normalizeHeaderKey(key, options.canonical)] = request.event.multiValueHeaders[key];
|
|
47
|
+
});
|
|
48
|
+
request.event.multiValueHeaders = headers;
|
|
49
|
+
request.event.rawMultiValueHeaders = rawHeaders;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
before: httpHeaderNormalizerMiddlewareBefore
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export default httpHeaderNormalizerMiddleware;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-header-normalizer",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.4",
|
|
4
4
|
"description": "Http header normalizer middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -13,11 +13,13 @@
|
|
|
13
13
|
"exports": "./index.js",
|
|
14
14
|
"types": "index.d.ts",
|
|
15
15
|
"files": [
|
|
16
|
+
"index.js",
|
|
16
17
|
"index.d.ts"
|
|
17
18
|
],
|
|
18
19
|
"scripts": {
|
|
19
20
|
"test": "npm run test:unit",
|
|
20
|
-
"test:unit": "ava"
|
|
21
|
+
"test:unit": "ava",
|
|
22
|
+
"test:benchmark": "node __benchmarks__/index.js"
|
|
21
23
|
},
|
|
22
24
|
"license": "MIT",
|
|
23
25
|
"keywords": [
|
|
@@ -47,8 +49,8 @@
|
|
|
47
49
|
"url": "https://github.com/middyjs/middy/issues"
|
|
48
50
|
},
|
|
49
51
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
50
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "d4bea7f4e21f6a9bbb1f6f6908361169598b9e53",
|
|
51
53
|
"devDependencies": {
|
|
52
|
-
"@middy/core": "^3.0.0-alpha.
|
|
54
|
+
"@middy/core": "^3.0.0-alpha.4"
|
|
53
55
|
}
|
|
54
56
|
}
|