From 41d4d186d83e818897ab425ed1b67f570e4185ce Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 27 Feb 2019 21:28:00 +0000 Subject: [PATCH 1/4] fix(na): proxy.bypass when return false. --- lib/Server.js | 11 +++++++++-- test/Proxy.test.js | 11 ++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index 07c19ff54c..818c10c1f8 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -381,14 +381,21 @@ class Server { } } - const bypass = typeof proxyConfig.bypass === 'function'; + // const bypass = typeof proxyConfig.bypass === 'function'; + // const bypassRawValue = bypass ? proxyConfig.bypass(req, res, proxyConfig) : null; + // const bypassUrl = typeof bypassRawValue === 'string' + // ? bypassRawValue + // : (typeof bypassRawValue === 'boolean' && bypassRawValue === false); + const bypass = typeof proxyConfig.bypass === 'function'; const bypassUrl = (bypass && proxyConfig.bypass(req, res, proxyConfig)) || false; if (bypassUrl) { + // In case bypassURL is a boolean with a true value + // (as result from setting a bypassRawValue boolean + // with false value) the request will be ignored req.url = bypassUrl; - next(); } else if (proxyMiddleware) { return proxyMiddleware(req, res, next); diff --git a/test/Proxy.test.js b/test/Proxy.test.js index 5791f868a3..945fa2bc26 100644 --- a/test/Proxy.test.js +++ b/test/Proxy.test.js @@ -21,10 +21,15 @@ const proxyOptionPathsAsProperties = { '/foo': { bypass(req) { if (/\.html$/.test(req.path)) { - return '/index.html'; + return 'index.html'; } }, }, + '/proxyfalse': { + bypass() { + return false; + }, + }, }; const proxyOption = { @@ -116,6 +121,10 @@ describe('Proxy', () => { it('should pass through a proxy when a bypass function returns null', (done) => { req.get('/foo.js').expect(200, /Hey/, done); }); + + it('should not pass through a proxy when a bypass function returns false', (done) => { + req.get('/proxyfalse').expect(404, done); + }); }); }); From 53f1380ad926559318c0fe83b97dd2460bc45d33 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 27 Feb 2019 22:09:02 +0000 Subject: [PATCH 2/4] fix(NA): logic for server bypass. test(NA): fix logic for the tests. --- lib/Server.js | 23 ++++++++++++----------- test/Proxy.test.js | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index 818c10c1f8..dc7a4cc767 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -381,21 +381,22 @@ class Server { } } - // const bypass = typeof proxyConfig.bypass === 'function'; - // const bypassRawValue = bypass ? proxyConfig.bypass(req, res, proxyConfig) : null; - // const bypassUrl = typeof bypassRawValue === 'string' - // ? bypassRawValue - // : (typeof bypassRawValue === 'boolean' && bypassRawValue === false); - const bypass = typeof proxyConfig.bypass === 'function'; + const bypassRawValue = bypass + ? proxyConfig.bypass(req, res, proxyConfig) + : null; const bypassUrl = - (bypass && proxyConfig.bypass(req, res, proxyConfig)) || false; + typeof bypassRawValue === 'string' + ? bypassRawValue + : typeof bypassRawValue === 'boolean' && + bypassRawValue === false; if (bypassUrl) { - // In case bypassURL is a boolean with a true value - // (as result from setting a bypassRawValue boolean - // with false value) the request will be ignored - req.url = bypassUrl; + // Only in case bypassURL is a string + // the request will be set + if (typeof bypassUrl === 'string') { + req.url = bypassUrl; + } next(); } else if (proxyMiddleware) { return proxyMiddleware(req, res, next); diff --git a/test/Proxy.test.js b/test/Proxy.test.js index 945fa2bc26..a6f752326b 100644 --- a/test/Proxy.test.js +++ b/test/Proxy.test.js @@ -21,7 +21,7 @@ const proxyOptionPathsAsProperties = { '/foo': { bypass(req) { if (/\.html$/.test(req.path)) { - return 'index.html'; + return '/index.html'; } }, }, From 53bf54e9c34ced26bc86c19c0394f90b6ae0969e Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Thu, 28 Feb 2019 01:56:05 +0000 Subject: [PATCH 3/4] test(NA): change test proxy configs. --- lib/Server.js | 7 +++++-- test/Proxy.test.js | 8 ++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index dc7a4cc767..033a01535f 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -392,11 +392,14 @@ class Server { bypassRawValue === false; if (bypassUrl) { - // Only in case bypassURL is a string - // the request will be set + if (typeof bypassUrl === 'boolean') { + req.url = null; + } + if (typeof bypassUrl === 'string') { req.url = bypassUrl; } + next(); } else if (proxyMiddleware) { return proxyMiddleware(req, res, next); diff --git a/test/Proxy.test.js b/test/Proxy.test.js index a6f752326b..45090458a6 100644 --- a/test/Proxy.test.js +++ b/test/Proxy.test.js @@ -23,11 +23,15 @@ const proxyOptionPathsAsProperties = { if (/\.html$/.test(req.path)) { return '/index.html'; } + + return null; }, }, '/proxyfalse': { - bypass() { - return false; + bypass(req) { + if (/\/proxyfalse$/.test(req.path)) { + return false; + } }, }, }; From 6311172a8f0c09ba49f078c5816cb118c39c7cb6 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Thu, 7 Mar 2019 17:47:48 +0000 Subject: [PATCH 4/4] refact(NA): make the bypass logic more clear. --- lib/Server.js | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index 033a01535f..cea529db1c 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -381,25 +381,22 @@ class Server { } } - const bypass = typeof proxyConfig.bypass === 'function'; - const bypassRawValue = bypass + // - Check if we have a bypass function defined + // - In case the bypass function is defined we'll retrieve the + // bypassUrl from it otherwise byPassUrl would be null + const isByPassFuncDefined = + typeof proxyConfig.bypass === 'function'; + const bypassUrl = isByPassFuncDefined ? proxyConfig.bypass(req, res, proxyConfig) : null; - const bypassUrl = - typeof bypassRawValue === 'string' - ? bypassRawValue - : typeof bypassRawValue === 'boolean' && - bypassRawValue === false; - - if (bypassUrl) { - if (typeof bypassUrl === 'boolean') { - req.url = null; - } - - if (typeof bypassUrl === 'string') { - req.url = bypassUrl; - } + if (typeof bypassUrl === 'boolean') { + // skip the proxy + req.url = null; + next(); + } else if (typeof bypassUrl === 'string') { + // byPass to that url + req.url = bypassUrl; next(); } else if (proxyMiddleware) { return proxyMiddleware(req, res, next);