-
-
Notifications
You must be signed in to change notification settings - Fork 380
Improve getFilenameFromUrl
to work with proxy requests
#80
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4bdb28e
fix req.path is undefined issue
ijse 544b5be
fix when url.hostname is null
056fa01
fix when request hostname is null
ijse f33e53e
Merge branch 'ijse-patch-1' of https://github.com/ijse/webpack-dev-mi…
cf7e3fa
Merge pull request #1 from webpack/master
ijse 4d8c4af
merge with new updates
172b5d3
Merge branch 'master' of github.com:webpack/webpack-dev-middleware
ijse 8849bf7
restore tests
ijse 3ac5946
fix test
ijse 4bce806
fix unexpected error with RegExp
ijse 1ad584b
add test for url from proxy
ijse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,36 @@ | ||
var pathJoin = require("./PathJoin"); | ||
var urlParse = require("url").parse; | ||
|
||
function getFilenameFromUrl(publicPath, outputPath, url) { | ||
// publicPrefix is the folder our bundle should be in | ||
var localPrefix = 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(publicPath || "/"); | ||
var urlObject = urlParse(url); | ||
|
||
// publicPath has the hostname that is not the same as request url's, should fail | ||
if(localPrefix.hostname !== null && urlObject.hostname !== null && | ||
localPrefix.hostname !== urlObject.hostname) { | ||
return false; | ||
} | ||
|
||
// publicPath is not in url, so it should fail | ||
if(publicPath && localPrefix.hostname === urlObject.hostname && !new RegExp('^' + publicPath).test(url)) { | ||
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); | ||
} | ||
|
||
if(!urlObject.hostname && localPrefix.hostname && | ||
!new RegExp('^' + localPrefix.path).test(url)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with the |
||
return false; | ||
} | ||
// and if not match, use outputPath as filename | ||
return filename ? pathJoin(outputPath, filename) : outputPath; | ||
|
||
} | ||
|
||
module.exports = getFilenameFromUrl; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inserting
publicPath
intoRegExp
can lead to unexpected errors, since the string will be interpreted as regex.Maybe use
lastIndexOf
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lastIndexOf
orindexOf
?Is it expected to be
false
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
lastIndexOf
, see this question.Yes, the example you've given is expected to be
false
, becausepublicPath
is not in the url. Would be nice if you add your example to the tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, just saw your changes with
indexOf
, that's also ok.