Skip to content

feat: allow sending a custom proxy timeout error #1650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ proxyServer.listen(8015);
```
* **headers**: object with extra headers to be added to target requests.
* **proxyTimeout**: timeout (in millis) for outgoing proxy requests
* **proxyTimeoutCustomError**: true/false, default: false - specify whether you want to throw a custom `ETIMEDOUT` error when the `proxyTimeout` is reached. If false then the default `ECONNRESET` error will be thrown.
* **timeout**: timeout (in millis) for incoming requests
* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects
* **selfHandleResponse** true/false, if set to true, none of the webOutgoing passes are called and it's your responsibility to appropriately return the response by listening and acting on the `proxyRes` event
Expand Down
7 changes: 6 additions & 1 deletion lib/http-proxy/passes/web-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ module.exports = {
// show an error page at the initial request
if(options.proxyTimeout) {
proxyReq.setTimeout(options.proxyTimeout, function() {
proxyReq.abort();
if (options.proxyTimeoutCustomError) {
var timeoutError = new Error('The proxy request timed out');
timeoutError.code = 'ETIMEDOUT';
return proxyReq.destroy(timeoutError);
}
proxyReq.destroy();
Copy link

@ravin00 ravin00 Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the change here?

});
}

Expand Down
35 changes: 35 additions & 0 deletions test/lib-http-proxy-passes-web-incoming-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,41 @@ describe('#createProxyServer.web() using own http server', function () {
}, function() {}).end();
});

it('should proxy the request with custom timeout errors (proxyTimeoutCustomError)', function(done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:45002',
proxyTimeout: 100,
proxyTimeoutCustomError: true
});

require('net').createServer().listen(45002);

var proxyServer = http.createServer(requestHandler);

var started = new Date().getTime();
function requestHandler(req, res) {
proxy.once('error', function (err, errReq, errRes) {
proxyServer.close();
expect(err).to.be.an(Error);
expect(errReq).to.be.equal(req);
expect(errRes).to.be.equal(res);
expect(new Date().getTime() - started).to.be.greaterThan(99);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about adding the expect(new Date().getTime() - started).to.be.above(99); . Will it make a difference to this line or to the whole code

expect(err.code).to.be('ETIMEDOUT');
done();
});

proxy.web(req, res);
}

proxyServer.listen('8087');

http.request({
hostname: '127.0.0.1',
port: '8087',
method: 'GET',
}, function() {}).end();
});

it('should proxy the request and handle timeout error', function(done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:45001',
Expand Down