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(server): allow async bypass function for proxying #2696

Merged
merged 1 commit into from
Aug 17, 2020
Merged
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
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