From ca7c74932fbf7ecc89ffa57452b481406584a18c Mon Sep 17 00:00:00 2001 From: gromo <13real008@gmail.com> Date: Thu, 6 Aug 2020 08:53:15 +0200 Subject: [PATCH] feat(server): allow async bypass function for proxying --- lib/Server.js | 4 ++-- test/server/proxy-option.test.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) 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); + }); }); });