Skip to content
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

feat: allow sending a custom proxy timeout error #1650

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
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
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();
});
}

Expand Down
35 changes: 35 additions & 0 deletions test/lib-http-proxy-passes-web-incoming-test.js
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);
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