I have a Node server that uses Connect to insert some middleware that is trying to convert the response stream from node-http-proxy. Sometimes this conversion can be quite slow, and it would be preferable in such cases to simply return a response that does not include the transformations or, alternatively, includes their partial application.
In my application, I tried to use setTimeoutto call nextafter some milliseconds in the context of the conversion middleware. This usually works, but provides a race condition if the middleware has already called next, and then it setTimeoutfires and does the same with an error that looks like this:Error: Can't set headers after they are sent.
In the end, I developed setTimeoutto call nextwith an instance Erroras my first argument, and then I would catch this error in my middleware chain and assuming that I res.headersSentwas false, it would start sending the response through res.end.call(res). It worked and it was amazing that I could set a timeout for almost nothing, and the answer would be much faster and complete.
I feel that this last method is a bit hacked and not immune to the same race condition, but perhaps it seems to be a little more stable. So I would like to know which idiomatic Node and Connect approaches deal with such things.
How can I find the time of slow middleware and just return a response stream?
Currently, it seems to be doing what I want, more or less, but again it feels a little rude.
let resTimedout = false;
const timeout = setTimeout(() => {
if (!resTimedout) {
resTimedout = true;
next();
}
}, 100);
getSelectors(headers, uri, (selectors) => {
const resSelectors = Object.keys(selectors).map((selector) => {
...
};
const rewrite = resRewrite(resSelectors);
rewrite(req, res, () => {
if (!resTimedout) {
resTimedout = true;
clearTimeout(timeout);
next();
}
});
});