Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wildcard path matching with globs
- Loading branch information
Showing
5 changed files
with
333 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,78 @@ | ||
var url = require('url'); | ||
var isGlob = require('is-glob'); | ||
var micromatch = require('micromatch'); | ||
|
||
module.exports = { | ||
match : matchContext, | ||
matchSinglePath : matchSinglePath, | ||
matchMultiPath : matchMultiPath | ||
match : matchContext | ||
} | ||
|
||
function matchContext (context, uri) { | ||
// single path | ||
if (typeof context === 'string') { | ||
return matchSinglePath(context, uri); | ||
if (isStringPath(context)) { | ||
return matchSingleStringPath(context, uri); | ||
} | ||
// single glob path | ||
if (isGlobPath(context)) { | ||
return matchSingleGlobPath(context, uri); | ||
} | ||
// multi path | ||
if (Array.isArray(context)) { | ||
return matchMultiPath(context, uri); | ||
if (context.every(isStringPath)) { | ||
return matchMultiPath(context, uri); | ||
} | ||
if (context.every(isGlobPath)) { | ||
return matchMultiGlobPath(context, uri); | ||
} | ||
|
||
throw new Error('[HPM] Invalid context. Expecting something like: ["/api", "/ajax"] or ["/api/**", "!**.html"]'); | ||
} | ||
|
||
throw new Error('[HPM] Invalid context. Expecting something like: "/api" or ["/api", "/ajax"]'); | ||
} | ||
|
||
function matchSinglePath (context, uri) { | ||
var urlPath = url.parse(uri).path; | ||
return urlPath.indexOf(context) === 0; | ||
/** | ||
* @param {String} context '/api' | ||
* @param {String} uri 'http://example.org/api/b/c/d.html' | ||
* @return {Boolean} | ||
*/ | ||
function matchSingleStringPath (context, uri) { | ||
var path = getUrlPath(uri); | ||
return path.indexOf(context) === 0; | ||
} | ||
|
||
function matchSingleGlobPath (pattern, uri) { | ||
var path = getUrlPath(uri); | ||
var matches = micromatch(path, pattern); | ||
return matches && (matches.length > 0); | ||
} | ||
|
||
function matchMultiGlobPath (patternList, uri) { | ||
return matchSingleGlobPath(patternList, uri); | ||
} | ||
|
||
/** | ||
* @param {String} context ['/api', '/ajax'] | ||
* @param {String} uri 'http://example.org/api/b/c/d.html' | ||
* @return {Boolean} | ||
*/ | ||
function matchMultiPath (contextList, uri) { | ||
for (var i = 0; i < contextList.length; i++) { | ||
var context = contextList[i]; | ||
if (matchSinglePath(context, uri)) { | ||
if (matchSingleStringPath(context, uri)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function getUrlPath (uri) { | ||
return uri && url.parse(uri).path; | ||
} | ||
|
||
function isStringPath (context) { | ||
return typeof context === 'string' && !isGlob(context); | ||
} | ||
|
||
function isGlobPath (context) { | ||
return isGlob(context); | ||
} |
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 |
---|---|---|
|
@@ -41,6 +41,8 @@ | |
}, | ||
"dependencies": { | ||
"http-proxy": "^1.11.1", | ||
"is-glob": "^2.0.0", | ||
"micromatch": "^2.1.6", | ||
"url": "^0.10.3" | ||
} | ||
} |
Oops, something went wrong.