Skip to content

Commit

Permalink
feat(server): allow async bypass function for proxying (#2696)
Browse files Browse the repository at this point in the history
  • Loading branch information
gromo authored Aug 17, 2020
1 parent b825dd4 commit a2907a8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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') {
Expand Down
16 changes: 16 additions & 0 deletions test/server/proxy-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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);
});
});
});

Expand Down

0 comments on commit a2907a8

Please sign in to comment.