@middy/http-header-normalizer 7.1.2 → 7.1.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/README.md CHANGED
@@ -30,10 +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
- ## License
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).
34
43
 
35
- 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).
36
44
 
37
- <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
38
- <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
39
- </a>
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
+
50
+ ## License
51
+
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<Event>;
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 = options.normalizeHeaderKey(key, options.canonical);
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 httpHeaderNormalizerMiddlewareBefore = async (request) => {
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 of Object.keys(request.event.headers)) {
83
- headers[options.normalizeHeaderKey(key, options.canonical)] =
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 = { ...defaultMultiValueHeaders };
105
+ const headers = hasDefaultMultiValueHeaders
106
+ ? { ...defaultMultiValueHeaders }
107
+ : {};
92
108
 
93
- for (const key of Object.keys(request.event.multiValueHeaders)) {
94
- headers[options.normalizeHeaderKey(key, options.canonical)] =
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.2",
3
+ "version": "7.1.4",
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.2"
70
+ "@middy/core": "7.1.4",
71
+ "@types/node": "^22.0.0"
72
72
  }
73
73
  }