express-api-stress-tester 2.0.1 → 2.0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-api-stress-tester",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "High-performance distributed API stress testing platform for Express.js APIs — simulate up to 10M concurrent virtual users",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -6,6 +6,9 @@
6
6
  */
7
7
  import { Pool } from 'undici';
8
8
 
9
+ const CONTROL_CHARS_REGEX = /[\0\r\n]/g;
10
+ const MAX_WARNED_HEADER_VALUES = 100;
11
+
9
12
  export class HttpEngine {
10
13
  /**
11
14
  * @param {object} options
@@ -31,6 +34,7 @@ export class HttpEngine {
31
34
  this.baseUrl = baseUrl;
32
35
  this.defaultHeaders = headers;
33
36
  this.timeout = timeout;
37
+ this.invalidHeaderWarningCache = { map: new Map(), queue: [] };
34
38
 
35
39
  this.pool = new Pool(baseUrl, {
36
40
  connections,
@@ -53,7 +57,10 @@ export class HttpEngine {
53
57
  * @returns {Promise<{ statusCode: number, headers: object, body: string, responseTime: number }>}
54
58
  */
55
59
  async request({ method = 'GET', path = '/', headers = {}, body = null } = {}) {
56
- const mergedHeaders = { ...this.defaultHeaders, ...headers };
60
+ const mergedHeaders = normalizeHeaders(
61
+ { ...this.defaultHeaders, ...headers },
62
+ this.invalidHeaderWarningCache,
63
+ );
57
64
 
58
65
  const start = process.hrtime.bigint();
59
66
 
@@ -88,3 +95,95 @@ export class HttpEngine {
88
95
  await this.pool.close();
89
96
  }
90
97
  }
98
+
99
+ function normalizeHeaders(headers, warningCache) {
100
+ const normalized = {};
101
+ for (const [key, value] of Object.entries(headers || {})) {
102
+ if (value === undefined || value === null) {
103
+ continue;
104
+ }
105
+ const normalizedKey = normalizeHeaderKey(key);
106
+ if (normalizedKey === null) {
107
+ continue;
108
+ }
109
+ if (Array.isArray(value)) {
110
+ const cleaned = [];
111
+ for (const entry of value) {
112
+ if (entry === undefined || entry === null) {
113
+ continue;
114
+ }
115
+ if (isValidHeaderValue(entry)) {
116
+ const cleanedEntry = normalizeHeaderValue(entry);
117
+ if (cleanedEntry !== null) {
118
+ cleaned.push(cleanedEntry);
119
+ }
120
+ } else {
121
+ warnInvalidHeaderValue(normalizedKey, entry, warningCache);
122
+ }
123
+ }
124
+ if (cleaned.length > 0) {
125
+ normalized[normalizedKey] = cleaned;
126
+ }
127
+ continue;
128
+ }
129
+
130
+ if (!isValidHeaderValue(value)) {
131
+ warnInvalidHeaderValue(normalizedKey, value, warningCache);
132
+ continue;
133
+ }
134
+
135
+ const cleanedValue = normalizeHeaderValue(value);
136
+ if (cleanedValue === null) {
137
+ continue;
138
+ }
139
+ normalized[normalizedKey] = cleanedValue;
140
+ }
141
+ return normalized;
142
+ }
143
+
144
+ function normalizeHeaderKey(value) {
145
+ if (typeof value !== 'string') {
146
+ return null;
147
+ }
148
+ const cleaned = value.replace(CONTROL_CHARS_REGEX, '');
149
+ return cleaned.length > 0 ? cleaned : null;
150
+ }
151
+
152
+ function normalizeHeaderValue(value) {
153
+ if (typeof value === 'string') {
154
+ const cleaned = value.replace(CONTROL_CHARS_REGEX, '');
155
+ return cleaned.length > 0 ? cleaned : null;
156
+ }
157
+ if (typeof value === 'number' || typeof value === 'boolean') {
158
+ return String(value);
159
+ }
160
+ return null;
161
+ }
162
+
163
+ function isValidHeaderValue(value) {
164
+ return (
165
+ typeof value === 'string' ||
166
+ typeof value === 'number' ||
167
+ typeof value === 'boolean'
168
+ );
169
+ }
170
+
171
+ function warnInvalidHeaderValue(key, value, warningCache) {
172
+ const type = typeof value;
173
+ const safeKey = key;
174
+ const signature = `${safeKey}::${type}`;
175
+ if (warningCache.map.has(signature)) {
176
+ return;
177
+ }
178
+ warningCache.map.set(signature, true);
179
+ warningCache.queue.push(signature);
180
+ if (warningCache.queue.length > MAX_WARNED_HEADER_VALUES) {
181
+ const oldest = warningCache.queue.shift();
182
+ if (oldest) {
183
+ warningCache.map.delete(oldest);
184
+ }
185
+ }
186
+ process.stderr.write(
187
+ `[HttpEngine] Dropping header "${safeKey}" with unsupported value type "${type}".\n`,
188
+ );
189
+ }