Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from chimurai/path-rewrite
feat: added rewritePath support
- Loading branch information
Showing
5 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,10 +1,57 @@ | ||
var url = require('url'); | ||
|
||
module.exports = { | ||
hasContext : hasContext | ||
hasContext : hasContext, | ||
createPathRewriter : createPathRewriter | ||
} | ||
|
||
function hasContext (context, uri) { | ||
var urlPath = url.parse(uri).path; | ||
return urlPath.indexOf(context) === 0; | ||
}; | ||
} | ||
|
||
/** | ||
* Create rewrite function, to cache parsed rewrite rules. | ||
*/ | ||
function createPathRewriter (config) { | ||
var rules = parsePathRewriteRules(config); | ||
|
||
return rewritePath; | ||
|
||
function rewritePath (path) { | ||
var result = path; | ||
|
||
rules.forEach(function (rule) { | ||
if (rule.regex.test(path)) { | ||
result = result.replace(rule.regex, rule.value); | ||
} | ||
}); | ||
|
||
return result; | ||
} | ||
} | ||
|
||
function parsePathRewriteRules (config) { | ||
var rules = []; | ||
|
||
if (isObject(config) === false) { | ||
throw new Error('[HPM] Invalid pathRewrite config. Expecting an object literal with pathRewrite configuration'); | ||
} | ||
|
||
for (var key in config) { | ||
if (config.hasOwnProperty(key)) { | ||
rules.push({ | ||
regex : new RegExp(key), | ||
value : config[key] | ||
}); | ||
|
||
console.log('[HPM] Proxy rewrite rule created: "' + key + '" -> "' + config[key] + '"'); | ||
} | ||
} | ||
|
||
return rules; | ||
} | ||
|
||
function isObject (val) { | ||
return Object.prototype.toString.call(val) === '[object Object]' && typeof val === 'object'; | ||
} |
This file contains 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
15784f0
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.
Although documented in the README.md as rewritePath, the code is actually using pathRewrite.
15784f0
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.
@gwbarrett Nice catch!
Will update the docs.