@middy/http-header-normalizer 7.1.1 → 7.1.3
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/README.md +17 -0
- package/index.d.ts +3 -5
- package/index.js +25 -10
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -30,6 +30,23 @@
|
|
|
30
30
|
<p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/http-header-normalizer">https://middy.js.org/docs/middlewares/http-header-normalizer</a></p>
|
|
31
31
|
</div>
|
|
32
32
|
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm install --save @middy/http-header-normalizer
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
## Documentation and examples
|
|
41
|
+
|
|
42
|
+
For documentation and examples, refer to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org/docs/middlewares/http-header-normalizer).
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
## Contributing
|
|
46
|
+
|
|
47
|
+
Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls).
|
|
48
|
+
|
|
49
|
+
|
|
33
50
|
## License
|
|
34
51
|
|
|
35
52
|
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2026 [will Farrell](https://github.com/willfarrell), [Luciano Mammino](https://github.com/lmammino), and [Middy contributors](https://github.com/middyjs/middy/graphs/contributors).
|
package/index.d.ts
CHANGED
|
@@ -4,14 +4,12 @@ import type middy from "@middy/core";
|
|
|
4
4
|
|
|
5
5
|
export interface Options {
|
|
6
6
|
canonical?: boolean;
|
|
7
|
-
defaultHeaders?: Record<string, string>;
|
|
8
|
-
normalizeHeaderKey?: (key: string) => string;
|
|
7
|
+
defaultHeaders?: Record<string, string | string[]>;
|
|
8
|
+
normalizeHeaderKey?: (key: string, canonical: boolean) => string;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export type Event = {};
|
|
12
|
-
|
|
13
11
|
declare function httpHeaderNormalizer(
|
|
14
12
|
options?: Options,
|
|
15
|
-
): middy.MiddlewareObj<
|
|
13
|
+
): middy.MiddlewareObj<unknown, unknown, Error>;
|
|
16
14
|
|
|
17
15
|
export default httpHeaderNormalizer;
|
package/index.js
CHANGED
|
@@ -62,10 +62,21 @@ const defaults = {
|
|
|
62
62
|
const httpHeaderNormalizerMiddleware = (opts = {}) => {
|
|
63
63
|
const options = { ...defaults, ...opts };
|
|
64
64
|
|
|
65
|
+
// Cache for normalized header keys to avoid repeated split/map/join
|
|
66
|
+
const normalizedKeyCache = new Map();
|
|
67
|
+
const cachedNormalizeKey = (key) => {
|
|
68
|
+
let normalized = normalizedKeyCache.get(key);
|
|
69
|
+
if (normalized === undefined) {
|
|
70
|
+
normalized = options.normalizeHeaderKey(key, options.canonical);
|
|
71
|
+
normalizedKeyCache.set(key, normalized);
|
|
72
|
+
}
|
|
73
|
+
return normalized;
|
|
74
|
+
};
|
|
75
|
+
|
|
65
76
|
const defaultHeaders = {};
|
|
66
77
|
const defaultMultiValueHeaders = {};
|
|
67
78
|
for (const key of Object.keys(options.defaultHeaders)) {
|
|
68
|
-
const newKey =
|
|
79
|
+
const newKey = cachedNormalizeKey(key);
|
|
69
80
|
const isArray = Array.isArray(options.defaultHeaders[key]);
|
|
70
81
|
defaultHeaders[newKey] = isArray
|
|
71
82
|
? options.defaultHeaders[key].join(",")
|
|
@@ -75,24 +86,28 @@ const httpHeaderNormalizerMiddleware = (opts = {}) => {
|
|
|
75
86
|
: options.defaultHeaders[key].split(",");
|
|
76
87
|
}
|
|
77
88
|
|
|
78
|
-
const
|
|
89
|
+
const hasDefaultHeaders = Object.keys(defaultHeaders).length > 0;
|
|
90
|
+
const hasDefaultMultiValueHeaders =
|
|
91
|
+
Object.keys(defaultMultiValueHeaders).length > 0;
|
|
92
|
+
|
|
93
|
+
const httpHeaderNormalizerMiddlewareBefore = (request) => {
|
|
79
94
|
if (request.event.headers) {
|
|
80
|
-
const headers = { ...defaultHeaders };
|
|
95
|
+
const headers = hasDefaultHeaders ? { ...defaultHeaders } : {};
|
|
81
96
|
|
|
82
|
-
for (const key
|
|
83
|
-
headers[
|
|
84
|
-
request.event.headers[key];
|
|
97
|
+
for (const key in request.event.headers) {
|
|
98
|
+
headers[cachedNormalizeKey(key)] = request.event.headers[key];
|
|
85
99
|
}
|
|
86
100
|
|
|
87
101
|
request.event.headers = headers;
|
|
88
102
|
}
|
|
89
103
|
|
|
90
104
|
if (request.event.multiValueHeaders) {
|
|
91
|
-
const headers =
|
|
105
|
+
const headers = hasDefaultMultiValueHeaders
|
|
106
|
+
? { ...defaultMultiValueHeaders }
|
|
107
|
+
: {};
|
|
92
108
|
|
|
93
|
-
for (const key
|
|
94
|
-
headers[
|
|
95
|
-
request.event.multiValueHeaders[key];
|
|
109
|
+
for (const key in request.event.multiValueHeaders) {
|
|
110
|
+
headers[cachedNormalizeKey(key)] = request.event.multiValueHeaders[key];
|
|
96
111
|
}
|
|
97
112
|
|
|
98
113
|
request.event.multiValueHeaders = headers;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-header-normalizer",
|
|
3
|
-
"version": "7.1.
|
|
3
|
+
"version": "7.1.3",
|
|
4
4
|
"description": "HTTP header normalizer middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -66,8 +66,8 @@
|
|
|
66
66
|
"type": "github",
|
|
67
67
|
"url": "https://github.com/sponsors/willfarrell"
|
|
68
68
|
},
|
|
69
|
-
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
|
|
70
69
|
"devDependencies": {
|
|
71
|
-
"@middy/core": "7.1.
|
|
70
|
+
"@middy/core": "7.1.3",
|
|
71
|
+
"@types/node": "^22.0.0"
|
|
72
72
|
}
|
|
73
73
|
}
|