From 4786374794b5f8f1f70d7a3c4eaaa05479814015 Mon Sep 17 00:00:00 2001 From: Alexey Kucherenko Date: Mon, 26 Feb 2018 18:01:58 +0300 Subject: [PATCH 1/2] add regexp support for custom pages --- gruntfile.js | 3 ++- tasks/libs/CustomPagesMiddleware.js | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/gruntfile.js b/gruntfile.js index 58e2eb5..3c6d8ba 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -14,7 +14,8 @@ module.exports = function(grunt) { //proxy : "http://someurl.com" openBrowser : true, customPages: { - "/custom": "README.md" + "/custom": "README.md", + "/^\/foo/(\\w+)$/": "README.md" } } } diff --git a/tasks/libs/CustomPagesMiddleware.js b/tasks/libs/CustomPagesMiddleware.js index d018949..a21e1b3 100644 --- a/tasks/libs/CustomPagesMiddleware.js +++ b/tasks/libs/CustomPagesMiddleware.js @@ -20,10 +20,30 @@ var CustomPagesMiddleware = function(basePath, rules){ basePath = Divhide.Safe.string(basePath); rules = Divhide.Safe.object(rules); + var reducer = function(acc, regExpStr){ + var re = Divhide.Safe.regexp(regExpStr); + var match = Divhide.Safe.string(rules[regExpStr]); + acc.push([re, match]); + return acc; + }; + var regexpRules = Object.keys(rules) + .filter(Divhide.Type.isRegExpStr) + .reduce(reducer, []); + return function(req, res){ var url = req.url; var relFilePath = Divhide.Safe.string(rules[url]); + // check for possible string regexp + if(!relFilePath){ + for(var i = 0, l = regexpRules.length; i < l; i++){ + if(regexpRules[i][0].test(url)){ + relFilePath = regexpRules[i][1]; + break; + } + } + } + // ignore if no rule is defined if(!relFilePath){ res.emit("next"); From e49f2e9d0e76fcfec8c2ae08e9e28a6a3da7b4f0 Mon Sep 17 00:00:00 2001 From: Alexey Kucherenko Date: Mon, 26 Feb 2018 22:09:38 +0300 Subject: [PATCH 2/2] fixed issue with string that can be considered as regexp --- gruntfile.js | 3 ++- tasks/libs/CustomPagesMiddleware.js | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/gruntfile.js b/gruntfile.js index 3c6d8ba..9bb829b 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -15,7 +15,8 @@ module.exports = function(grunt) { openBrowser : true, customPages: { "/custom": "README.md", - "/^\/foo/(\\w+)$/": "README.md" + "/^\/foo/(\\w+)$/": "README.md", + "/baz/quux": "LICENCE" } } } diff --git a/tasks/libs/CustomPagesMiddleware.js b/tasks/libs/CustomPagesMiddleware.js index a21e1b3..db94741 100644 --- a/tasks/libs/CustomPagesMiddleware.js +++ b/tasks/libs/CustomPagesMiddleware.js @@ -16,10 +16,19 @@ var _ = require("lodash"), * */ var CustomPagesMiddleware = function(basePath, rules){ - basePath = Divhide.Safe.string(basePath); rules = Divhide.Safe.object(rules); + // workaround for https://github.com/divhide/node-divhide/issues/39 + var RegExpFormat = /^\/.*\/([gimuy]*)$/; + var isRegExpStr = function (value){ + if(!Divhide.Type.isString(value)){ + return false; + } + + return !!RegExpFormat.exec(value); + }; + var reducer = function(acc, regExpStr){ var re = Divhide.Safe.regexp(regExpStr); var match = Divhide.Safe.string(rules[regExpStr]); @@ -27,7 +36,7 @@ var CustomPagesMiddleware = function(basePath, rules){ return acc; }; var regexpRules = Object.keys(rules) - .filter(Divhide.Type.isRegExpStr) + .filter(isRegExpStr) .reduce(reducer, []); return function(req, res){