express-naked-redirect
Advanced tools
Comparing version 0.1.3 to 0.1.4
92
index.js
@@ -7,28 +7,27 @@ /** | ||
var subdomainParser = (function () { | ||
const parseDomain = require('parse-domain') | ||
const parsedDomain = {} | ||
var subdomainParser = function() { | ||
const parseDomain = require('parse-domain'); | ||
const parsedDomain = {}; | ||
return { | ||
get: function(hostname) { | ||
get: function (hostname) { | ||
// Cache, Touch | ||
if (parsedDomain[hostname] == undefined) { | ||
if (parsedDomain[hostname] === undefined) { | ||
try { | ||
var ps = parseDomain(hostname); | ||
parsedDomain[hostname] = [ps.subdomain, ps.domain + '.' + ps.tld]; | ||
var ps = parseDomain(hostname) | ||
parsedDomain[hostname] = [ps.subdomain, ps.domain + '.' + ps.tld] | ||
} catch (e) { | ||
parsedDomain[hostname] = [null, null]; | ||
parsedDomain[hostname] = [null, null] | ||
} | ||
} | ||
return parsedDomain[hostname]; | ||
return parsedDomain[hostname] | ||
} | ||
} | ||
}(); | ||
}()) | ||
module.exports = function(reverse, subDomain, status) { | ||
var options; | ||
module.exports = function (reverse, subDomain, status) { | ||
var options | ||
if (arguments.length === 1 && typeof arguments[0] === 'object') { | ||
options = arguments[0]; | ||
options = arguments[0] | ||
} else { | ||
@@ -44,3 +43,3 @@ options = { | ||
if (typeof options.except === 'string') { | ||
options.except = [options.except]; | ||
options.except = [options.except] | ||
} | ||
@@ -50,23 +49,22 @@ | ||
if (typeof options.except.length !== 'number') { | ||
throw new Error('Except option must be set with an array or string.'); | ||
throw new Error('Except option must be set with an array or string.') | ||
} | ||
var UrlPattern; | ||
var UrlPattern | ||
try { | ||
UrlPattern = require('url-pattern'); | ||
} | ||
catch (e) { | ||
UrlPattern = require('url-pattern') | ||
} catch (e) { | ||
console.error(e) | ||
throw new Error('The third party module `url-pattern` is required.'); | ||
throw new Error('The third party module `url-pattern` is required.') | ||
} | ||
for (var i = 0; i < options.except.length; i++) { | ||
options.except[i] = new UrlPattern(options.except[i]); | ||
options.except[i] = new UrlPattern(options.except[i]) | ||
} | ||
} | ||
return function(req, res, next) { | ||
var domain = subdomainParser.get(req.hostname); | ||
options.subDomain = options.subDomain || 'www'; | ||
return function (req, res, next) { | ||
var domain = subdomainParser.get(req.hostname) | ||
options.subDomain = options.subDomain || 'www' | ||
@@ -76,3 +74,3 @@ if (options.except) { | ||
if (options.except[i].match(req.url)) { | ||
return next(); | ||
return next() | ||
} | ||
@@ -82,37 +80,35 @@ } | ||
if (typeof(options.reverse) === 'string' && options.subDomain === undefined) { | ||
options.subDomain = options.reverse; | ||
options.reverse = false; | ||
if (typeof (options.reverse) === 'string' && options.subDomain === undefined) { | ||
options.subDomain = options.reverse | ||
options.reverse = false | ||
} | ||
if (options.status === undefined) { | ||
options.status = 302; | ||
options.status = 302 | ||
} | ||
if (typeof(options.protocol) === 'string') { | ||
options.protocol = options.protocol; | ||
if (typeof (options.protocol) === 'string') { | ||
options.protocol = options.protocol // eslint-disable-line | ||
} else if (typeof (options.https) === 'boolean' && options.https) { | ||
options.protocol = 'https' | ||
} else { | ||
options.protocol = req.protocol | ||
} | ||
else if (typeof(options.https) === 'boolean' && options.https) { | ||
options.protocol = 'https'; | ||
} | ||
else { | ||
options.protocol = req.protocol; | ||
} | ||
var redirectTo = null; | ||
var redirectTo = null | ||
if (domain[0] == '' && !options.reverse) { | ||
redirectTo = `${options.protocol}://${options.subDomain}.${domain[1]}${req.url}`; | ||
} else if (domain[0] == options.subDomain && options.reverse) { | ||
redirectTo = `${options.protocol}://${domain[1]}${req.url}`; | ||
if (domain[0] === '' && !options.reverse) { | ||
redirectTo = `${options.protocol}://${options.subDomain}.${domain[1]}${req.url}` | ||
} else if (domain[0] === options.subDomain && options.reverse) { | ||
redirectTo = `${options.protocol}://${domain[1]}${req.url}` | ||
} else if (options.protocol !== req.protocol) { | ||
redirectTo = `${options.protocol}://${req.hostname}${req.url}`; | ||
redirectTo = `${options.protocol}://${req.hostname}${req.url}` | ||
} | ||
if (redirectTo !== null) { | ||
res.redirect(options.status, redirectTo); | ||
return; | ||
res.redirect(options.status, redirectTo) | ||
return | ||
} | ||
return next(); | ||
return next() | ||
} | ||
}; | ||
} |
{ | ||
"name": "express-naked-redirect", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "expressNakedRedirect is a middleware for Express that redirects naked(root domain) request to www or its reverse.", | ||
@@ -13,3 +13,6 @@ "contributors": [ | ||
"scripts": { | ||
"test": "mocha --reporter spec --bail --check-leaks test.js" | ||
"test": "standard && mocha --reporter spec --bail --check-leaks test.js", | ||
"lint": "standard", | ||
"lintfix": "standard --fix", | ||
"lintmon": "./node_modules/nodemon/bin/nodemon.js --exec './node_modules/standard/bin/cmd.js'" | ||
}, | ||
@@ -29,8 +32,9 @@ "repository": { | ||
"dependencies": { | ||
"parse-domain": "^2.0.0" | ||
"parse-domain": "^2.3.4" | ||
}, | ||
"devDependencies": { | ||
"express": "^4.15.2", | ||
"mocha": "^4.0.1", | ||
"supertest": "^3.0.0", | ||
"express": "^4.17.1", | ||
"mocha": "^6.2.2", | ||
"standard": "^14.3.1", | ||
"supertest": "^4.0.2", | ||
"url-pattern": "^1.0.3" | ||
@@ -43,2 +47,7 @@ }, | ||
], | ||
"standard": { | ||
"env": [ | ||
"mocha" | ||
] | ||
}, | ||
"engines": { | ||
@@ -45,0 +54,0 @@ "node": ">= 0.8.0" |
8411
5
92
- Removedelectron-to-chromium@1.4.774(transitive)
Updatedparse-domain@^2.3.4