-
-
Notifications
You must be signed in to change notification settings - Fork 669
Description
This would solve...
Some interceptors assume the request headers are necessarily objects:
undici/lib/interceptor/cache.js
Lines 238 to 242 in 6551919
headers: { | |
...opts.headers, | |
'if-modified-since': new Date(result.cachedAt).toUTCString(), | |
etag: result.etag | |
} |
Lines 360 to 363 in 6551919
headers: { | |
host: origin.hostname, | |
...origDispatchOpts.headers | |
} |
undici/lib/handler/redirect-handler.js
Lines 231 to 236 in 6551919
const entries = typeof headers[Symbol.iterator] === 'function' ? headers : Object.entries(headers) | |
for (const [key, value] of entries) { | |
if (!shouldRemoveHeader(key, removeContent, unknownOrigin)) { | |
ret.push(key, value) | |
} | |
} |
The implementation should look like...
IMHO, the interceptors should convert the request headers to an object before using them, considering that it makes them easy to manipulate and most users will provide them already as objects, so no conversion is needed.
I have also considered...
An alternative could be to fix the interceptors to handle the headers based on the possible types, but this would make very difficult to implement some logics, like checking the value of some headers (which is needed in the cache interceptor)
Additional context
Using redirect and dns interceptors:
createServer((req, res) => {
if (req.url?.endsWith("/redirect")) {
console.log(req.headers);
exit();
}
res.setHeader("location", "/redirect");
res.statusCode = 302;
res.end();
}).listen(8008);
request("http://localhost:8008", {
headers: { "user-agent": "something", hello: "world" },
dispatcher: new Agent().compose(
interceptors.dns(),
interceptors.redirect({ maxRedirections: 1 }),
),
});
// This logs
// {
// '0': 'user-agent',
// '1': 'something',
// '2': 'hello',
// '3': 'world',
// host: 'localhost',
// connection: 'keep-alive'
// }