From 4bdb28e58f56909c3aaadc1f0d9be55f14fe3fd2 Mon Sep 17 00:00:00 2001 From: Li Yi Date: Fri, 25 Mar 2016 07:26:50 +0800 Subject: [PATCH 1/7] fix req.path is undefined issue fix https://github.com/webpack/webpack-dev-middleware/issues/79 --- middleware.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/middleware.js b/middleware.js index 3092d49f5..e67df7c15 100644 --- a/middleware.js +++ b/middleware.js @@ -4,6 +4,7 @@ */ var MemoryFileSystem = require("memory-fs"); var mime = require("mime"); +var urlParse = require("url").parse; var HASH_REGEXP = /[0-9a-f]{10,}/; @@ -124,26 +125,25 @@ module.exports = function(compiler, options) { } function getFilenameFromUrl(url) { - // publicPrefix is the folder our bundle should be in - var localPrefix = options.publicPath || "/"; - if(url.indexOf(localPrefix) !== 0) { - if(/^(https?:)?\/\//.test(localPrefix)) { - localPrefix = "/" + localPrefix.replace(/^(https?:)?\/\/[^\/]+\//, ""); - // fast exit if another directory requested - if(url.indexOf(localPrefix) !== 0) return false; - } else return false; + var filename; + // localPrefix is the folder our bundle should be in + var localPrefix = urlParse(options.publicPath || "/"); + var urlObject = urlParse(url); + if(localPrefix.hostname !== null && localPrefix.hostname !== urlObject.hostname) { + // publicPath has hostname and is not the same as request url's + return false; } - // get filename from request - var filename = url.substr(localPrefix.length); - if(filename.indexOf("?") >= 0) { - filename = filename.substr(0, filename.indexOf("?")); + // strip localPrefix from the start of url + if(urlObject.pathname.indexOf(localPrefix.pathname) === 0) { + filename = urlObject.pathname.substr(localPrefix.pathname.length); } + // and if not match, use outputPath as filename return filename ? pathJoin(compiler.outputPath, filename) : compiler.outputPath; } - + // The middleware function function webpackDevMiddleware(req, res, next) { - var filename = getFilenameFromUrl(req.path); + var filename = getFilenameFromUrl(req.url); if (filename === false) return next(); // in lazy mode, rebuild on bundle request From 544b5bef733e72652533857d5574c1706008ac0a Mon Sep 17 00:00:00 2001 From: liyi16 Date: Fri, 25 Mar 2016 07:57:18 +0800 Subject: [PATCH 2/7] fix when url.hostname is null --- middleware.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/middleware.js b/middleware.js index e67df7c15..1a0112c4a 100644 --- a/middleware.js +++ b/middleware.js @@ -129,18 +129,20 @@ module.exports = function(compiler, options) { // localPrefix is the folder our bundle should be in var localPrefix = urlParse(options.publicPath || "/"); var urlObject = urlParse(url); - if(localPrefix.hostname !== null && localPrefix.hostname !== urlObject.hostname) { - // publicPath has hostname and is not the same as request url's - return false; - } - // strip localPrefix from the start of url + if(localPrefix.hostname !== null && + urlObject.hostname !== null && + localPrefix.hostname !== urlObject.hostname) { + // publicPath has hostname and is not the same as request url's + return false; + } + // strip localPrefix from the start of url if(urlObject.pathname.indexOf(localPrefix.pathname) === 0) { filename = urlObject.pathname.substr(localPrefix.pathname.length); } // and if not match, use outputPath as filename return filename ? pathJoin(compiler.outputPath, filename) : compiler.outputPath; } - + // The middleware function function webpackDevMiddleware(req, res, next) { var filename = getFilenameFromUrl(req.url); From 056fa016ea23fd9b1a2111bd48479fc40f88ac18 Mon Sep 17 00:00:00 2001 From: Li Yi Date: Fri, 25 Mar 2016 07:59:18 +0800 Subject: [PATCH 3/7] fix when request hostname is null --- middleware.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/middleware.js b/middleware.js index e67df7c15..1a0112c4a 100644 --- a/middleware.js +++ b/middleware.js @@ -129,18 +129,20 @@ module.exports = function(compiler, options) { // localPrefix is the folder our bundle should be in var localPrefix = urlParse(options.publicPath || "/"); var urlObject = urlParse(url); - if(localPrefix.hostname !== null && localPrefix.hostname !== urlObject.hostname) { - // publicPath has hostname and is not the same as request url's - return false; - } - // strip localPrefix from the start of url + if(localPrefix.hostname !== null && + urlObject.hostname !== null && + localPrefix.hostname !== urlObject.hostname) { + // publicPath has hostname and is not the same as request url's + return false; + } + // strip localPrefix from the start of url if(urlObject.pathname.indexOf(localPrefix.pathname) === 0) { filename = urlObject.pathname.substr(localPrefix.pathname.length); } // and if not match, use outputPath as filename return filename ? pathJoin(compiler.outputPath, filename) : compiler.outputPath; } - + // The middleware function function webpackDevMiddleware(req, res, next) { var filename = getFilenameFromUrl(req.url); From 8849bf75bb42886723d5acc877b3757ad94095a2 Mon Sep 17 00:00:00 2001 From: ijse Date: Fri, 16 Sep 2016 11:56:07 +0800 Subject: [PATCH 4/7] restore tests --- test/GetFilenameFromUrl.test.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/test/GetFilenameFromUrl.test.js b/test/GetFilenameFromUrl.test.js index 868dd5e80..9900b1e86 100644 --- a/test/GetFilenameFromUrl.test.js +++ b/test/GetFilenameFromUrl.test.js @@ -66,14 +66,6 @@ describe("GetFilenameFromUrl", function() { expected: false, } ]; - // results.forEach(testUrl); - results.pop(); - - testUrl({ - url: "c.js", - outputPath: "/dist", - publicPath: "/", - expected: false, // publicPath is not in url, so it should fail - }); + results.forEach(testUrl); }); }); From 3ac59467a530806eec23ff9a51932aeb36c3d921 Mon Sep 17 00:00:00 2001 From: ijse Date: Fri, 16 Sep 2016 12:27:39 +0800 Subject: [PATCH 5/7] fix test --- lib/GetFilenameFromUrl.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/GetFilenameFromUrl.js b/lib/GetFilenameFromUrl.js index 8cf250874..1ceb2d8a7 100644 --- a/lib/GetFilenameFromUrl.js +++ b/lib/GetFilenameFromUrl.js @@ -15,7 +15,7 @@ function getFilenameFromUrl(publicPath, outputPath, url) { } // publicPath is not in url, so it should fail - if(publicPath && !new RegExp('^' + publicPath).test(url)) { + if(publicPath && localPrefix.hostname === urlObject.hostname && !new RegExp('^' + publicPath).test(url)) { return false; } @@ -24,6 +24,10 @@ function getFilenameFromUrl(publicPath, outputPath, url) { filename = urlObject.pathname.substr(localPrefix.pathname.length); } + if(!urlObject.hostname && localPrefix.hostname && + !new RegExp('^' + localPrefix.path).test(url)) { + return false; + } // and if not match, use outputPath as filename return filename ? pathJoin(outputPath, filename) : outputPath; From 4bce806426b1a0fb1771692be6e0e09d7e16db95 Mon Sep 17 00:00:00 2001 From: ijse Date: Fri, 16 Sep 2016 16:57:18 +0800 Subject: [PATCH 6/7] fix unexpected error with RegExp --- lib/GetFilenameFromUrl.js | 4 ++-- test/GetFilenameFromUrl.test.js | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/GetFilenameFromUrl.js b/lib/GetFilenameFromUrl.js index 1ceb2d8a7..0d92bfb3c 100644 --- a/lib/GetFilenameFromUrl.js +++ b/lib/GetFilenameFromUrl.js @@ -15,7 +15,7 @@ function getFilenameFromUrl(publicPath, outputPath, url) { } // publicPath is not in url, so it should fail - if(publicPath && localPrefix.hostname === urlObject.hostname && !new RegExp('^' + publicPath).test(url)) { + if(publicPath && localPrefix.hostname === urlObject.hostname && url.indexOf(publicPath) !== 0) { return false; } @@ -25,7 +25,7 @@ function getFilenameFromUrl(publicPath, outputPath, url) { } if(!urlObject.hostname && localPrefix.hostname && - !new RegExp('^' + localPrefix.path).test(url)) { + url.indexOf(localPrefix.path) !== 0) { return false; } // and if not match, use outputPath as filename diff --git a/test/GetFilenameFromUrl.test.js b/test/GetFilenameFromUrl.test.js index 9900b1e86..b5d6a033c 100644 --- a/test/GetFilenameFromUrl.test.js +++ b/test/GetFilenameFromUrl.test.js @@ -49,6 +49,11 @@ describe("GetFilenameFromUrl", function() { outputPath: "/a", publicPath: "/", expected: "/a/more/complex/path.js", + }, { + url: "/more/complex/path.js", + outputPath: "/a", + publicPath: "/complex", + expected: false, }, { url: "c.js", outputPath: "/dist", From 1ad584b0cb50f303d0c032b17c8a2f60c250669f Mon Sep 17 00:00:00 2001 From: ijse Date: Fri, 16 Sep 2016 17:34:23 +0800 Subject: [PATCH 7/7] add test for url from proxy --- test/GetFilenameFromUrl.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/GetFilenameFromUrl.test.js b/test/GetFilenameFromUrl.test.js index b5d6a033c..3d5e7ead5 100644 --- a/test/GetFilenameFromUrl.test.js +++ b/test/GetFilenameFromUrl.test.js @@ -69,6 +69,11 @@ describe("GetFilenameFromUrl", function() { outputPath: "/", publicPath: "http://localhost/foo/", expected: false, + }, { + url: "http://test.domain/test/sample.js", + outputPath: "/", + publicPath: "/test/", + expected: "/sample.js" } ]; results.forEach(testUrl);