Skip to content

Commit

Permalink
Allow rewrites to have the destination of another
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed May 18, 2018
1 parent 22d0719 commit f331952
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
24 changes: 13 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ const path = require('path');
// Packages
const url = require('fast-url-parser');
const fs = require('fs-extra');
const minimatch = require('minimatch');
const slasher = require('glob-slasher');
const toRegExp = require('path-to-regexp');
const pathToRegExp = require('path-to-regexp');

const getHandlers = methods => {
const {createReadStream} = fs;
Expand All @@ -16,20 +15,23 @@ const getHandlers = methods => {
}, methods);
};

const applyRewrites = (requestURL, rewrites) => {
const {pathname} = url.parse(requestURL);
const toRegExp = (location, keys = null) => {
const normalized = slasher(location).replace('*', '(.*)');
return pathToRegExp(normalized, keys);
};

const applyRewrites = (requestPath, rewrites) => {
if (!Array.isArray(rewrites)) {
return pathname;
return requestPath;
}

const rewrite = rewrites.find(({source}) => minimatch(pathname, slasher(source)));
const rewrite = rewrites.find(({source}) => toRegExp(source).test(requestPath));

if (rewrite) {
return slasher(rewrite.destination);
return applyRewrites(slasher(rewrite.destination), rewrites);
}

return pathname;
return requestPath;
};

const applyRedirect = (rewrittenURL, redirects) => {
Expand All @@ -41,10 +43,9 @@ const applyRedirect = (rewrittenURL, redirects) => {
// iterate over an array
for (let index = 0; index < redirects.length; index++) {
const {destination, source, statusCode} = redirects[index];
const normalized = slasher(source).replace('*', '(.*)');

const keys = [];
const expression = toRegExp(normalized, keys);
const expression = toRegExp(source, keys);
const results = expression.exec(rewrittenURL);

if (results) {
Expand All @@ -71,7 +72,8 @@ module.exports = async (request, response, config = {}, methods) => {
const current = config.path ? path.join(cwd, config.path) : cwd;
const handlers = getHandlers(methods);

const rewrittenURL = applyRewrites(request.url, config.rewrites);
const {pathname} = url.parse(request.url);
const rewrittenURL = applyRewrites(pathname, config.rewrites);
const redirect = applyRedirect(rewrittenURL, config.redirects);

if (redirect) {
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
"fast-url-parser": "1.1.3",
"fs-extra": "6.0.1",
"glob-slasher": "1.0.1",
"mime": "2.3.1",
"minimatch": "3.0.4",
"path-to-regexp": "2.2.1"
}
}
6 changes: 1 addition & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -570,15 +570,11 @@ lru-cache@^4.0.1:
pseudomap "^1.0.2"
yallist "^2.1.2"

[email protected]:
version "2.3.1"
resolved "https://registry.yarnpkg.com/mime/-/mime-2.3.1.tgz#b1621c54d63b97c47d3cfe7f7215f7d64517c369"

mimic-fn@^1.0.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.2.0.tgz#820c86a39334640e99516928bd03fca88057d022"

minimatch@3.0.4, minimatch@^3.0.2, minimatch@^3.0.4:
minimatch@^3.0.2, minimatch@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
dependencies:
Expand Down

0 comments on commit f331952

Please sign in to comment.