diff --git a/lib/Server.js b/lib/Server.js index 5e64eb7d0c..f53c2312ed 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -254,7 +254,7 @@ class Server { this.websocketProxies.push(proxyMiddleware); } - const handle = (req, res, next) => { + const handle = async (req, res, next) => { if (typeof proxyConfigOrCallback === 'function') { const newProxyConfig = proxyConfigOrCallback(); @@ -269,7 +269,7 @@ class Server { // bypassUrl from it otherwise bypassUrl would be null const isByPassFuncDefined = typeof proxyConfig.bypass === 'function'; const bypassUrl = isByPassFuncDefined - ? proxyConfig.bypass(req, res, proxyConfig) + ? await proxyConfig.bypass(req, res, proxyConfig) : null; if (typeof bypassUrl === 'boolean') { diff --git a/test/server/proxy-option.test.js b/test/server/proxy-option.test.js index 000c5199ad..b2936aac28 100644 --- a/test/server/proxy-option.test.js +++ b/test/server/proxy-option.test.js @@ -36,6 +36,18 @@ const proxyOptionPathsAsProperties = { } }, }, + '/proxy/async': { + bypass(req, res) { + if (/\/proxy\/async$/.test(req.path)) { + return new Promise((resolve) => { + setTimeout(() => { + res.end('proxy async response'); + resolve(true); + }, 10); + }); + } + }, + }, }; const proxyOption = { @@ -135,6 +147,10 @@ describe('proxy option', () => { it('should not pass through a proxy when a bypass function returns false', (done) => { req.get('/proxyfalse').expect(404, done); }); + + it('should wait if bypass returns promise', (done) => { + req.get('/proxy/async').expect(200, 'proxy async response', done); + }); }); });