New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

connect-redirecthost

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connect-redirecthost - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

58

lib/redirectHost.js

@@ -8,2 +8,3 @@ /*!

var _ = require('underscore');
var URIjs = require('URIjs');

@@ -53,3 +54,16 @@ /**

*
* Additionally, the protocol can be specified. If omitted, the current
* protocol is preserved.
*
* ```redirectHost({
* to: 'www.example.com',
* protocol: 'http'
* });```
*
* ```redirectHost({
* to: 'www.example.com',
* protocol: 'https'
* });```
*
*
* @param {String || Object} options

@@ -60,14 +74,17 @@ * @return {Function} middleware function(req, res, next)

exports.redirectHost = function(options){
if(!options){
exports.redirectHost = function(options) {
if (!options) {
throw new ReferenceError('options is required, specify at least a hostname');
}
// Preserve current protocol if not specified
var protocol = (options && options.protocol) || '';
// Determine if there is anything that must change the paths
var pathFunc = function(host, url){return url;};
if(options.changePath){
pathFunc = function(host, url){
var pathFunc = function(host, url) {return url;};
if (options.changePath) {
pathFunc = function(host, url) {
var override = options.changePath[host];
if(override){
if(_.isFunction(override)){
if (override) {
if (_.isFunction(override)) {
return override(host, url);

@@ -89,3 +106,3 @@ }

// Handle case with a single redirect target domain for all domains
if(typeof options === 'string'){
if (typeof options === 'string') {
except[options] = true;

@@ -100,8 +117,8 @@ return createHandler(options, except, pathFunc);

// Handle single exception specified as a string instead of an array
if(typeof options.except === "string"){
if (typeof options.except === "string") {
except[options.except] = true;
}else{
} else {
except = _.reduce(
options.except,
function(memo, host){
function(memo, host) {
memo[host] = true;

@@ -113,4 +130,4 @@ return memo;

return createHandler(to, except, pathFunc);
}
return createHandler(to, except, pathFunc, protocol);
};

@@ -127,13 +144,18 @@

function createHandler(to, except, pathFunc){
return function(req, res, next){
function createHandler(to, except, pathFunc, protocol) {
return function(req, res, next) {
var host = (req.header('host') || '').split(':')[0]; // strip port from host
var url = req.url;
if(host in except){
if (host in except) {
next();
}else{
res.redirect(301, 'http://' + to + pathFunc(host, url)); //<- change url based on host
} else {
var target = new URIjs(pathFunc(host, url))
.host(to)
.protocol(protocol || '')
.href();
res.redirect(301, target);
}
};
}

@@ -5,3 +5,3 @@ {

"description": "Connect middleware for the Express.js framework that allows redirecting multiple domains to a default one",
"version": "1.0.0",
"version": "2.0.0",
"repository": {

@@ -15,6 +15,7 @@ "url": "git@github.com:perropicante/connect-redirecthost.git"

"dependencies": {
"underscore" : "1.6.0"
"URIjs": "^1.14.1",
"underscore": "1.6.0"
},
"devDependencies": {
"vows": "latest"
"vows": "latest"
},

@@ -21,0 +22,0 @@ "scripts": {

@@ -65,3 +65,17 @@ # Connect Host Redirect

### Protocol overrides
By default, the current protocol is preserved when redirecting. Using the `protocol` option forces the redirect to a
particular protocol. Typical values are `http` and `https`.
```javascript
app.use(require('connect-redirecthost').redirectHost({
to: 'www.example.com',
protocol: 'https'
}));
```
IMPORTANT! Keep in mind this only sets the protocol when redirecting based on host matches. This will not normalize
the protocol on all requests. You'll need another module to accomplish that.
## License

@@ -68,0 +82,0 @@

@@ -88,12 +88,12 @@ /*!

'http://example.com/',
'http://www.example.com/'),
'//www.example.com/'),
'redirects from anything.example.com to www.example.com': verifyRedirect(
'http://anything.example.com/',
'http://www.example.com/'),
'//www.example.com/'),
'redirect preserves relative path': verifyRedirect(
'http://example.com/some/path.htm',
'http://www.example.com/some/path.htm'),
'//www.example.com/some/path.htm'),
'redirect preserves relative path and search': verifyRedirect(
'http://example.com/some/path.htm?key1=v1&key2=v2',
'http://www.example.com/some/path.htm?key1=v1&key2=v2'),
'//www.example.com/some/path.htm?key1=v1&key2=v2'),
'redirect avoided on www.example.com': verifyNext(

@@ -114,2 +114,70 @@ 'http://www.example.com'),

},
'Redirect by overriding protocol to http' : {
topic : function(){
return factory.redirectHost({to: 'www.example.com', protocol: 'http'});
},
'middleware exists': function(middleware){
assert.equal('function', typeof middleware);
},
'redirects from example.com to www.example.com': verifyRedirect(
'http://example.com/',
'http://www.example.com/'),
'redirects from anything.example.com to www.example.com': verifyRedirect(
'http://anything.example.com/',
'http://www.example.com/'),
'redirect preserves relative path': verifyRedirect(
'http://example.com/some/path.htm',
'http://www.example.com/some/path.htm'),
'redirect preserves relative path and search': verifyRedirect(
'http://example.com/some/path.htm?key1=v1&key2=v2',
'http://www.example.com/some/path.htm?key1=v1&key2=v2'),
'redirect avoided on www.example.com': verifyNext(
'http://www.example.com'),
'redirect avoided on www.example.com:80': verifyNext(
'http://www.example.com:80'),
'redirect avoided on www.example.com with path and query': verifyNext(
'http://www.example.com/some/path.htm?key1=v1&key2=v2'),
'redirect skipped for localhost': verifyNext(
'http://localhost/'),
'redirect skipped for localhost:3000': verifyNext(
'http://localhost:3000/'),
'redirect skipped for 127.0.0.1': verifyNext(
'http://127.0.0.1/'),
'redirect skipped for 127.0.0.1:3000': verifyNext(
'http://127.0.0.1:3000/')
},
'Redirect by overriding protocol to https' : {
topic : function(){
return factory.redirectHost({to: 'www.example.com', protocol: 'https'});
},
'middleware exists': function(middleware){
assert.equal('function', typeof middleware);
},
'redirects from example.com to www.example.com': verifyRedirect(
'http://example.com/',
'https://www.example.com/'),
'redirects from anything.example.com to www.example.com': verifyRedirect(
'http://anything.example.com/',
'https://www.example.com/'),
'redirect preserves relative path': verifyRedirect(
'http://example.com/some/path.htm',
'https://www.example.com/some/path.htm'),
'redirect preserves relative path and search': verifyRedirect(
'http://example.com/some/path.htm?key1=v1&key2=v2',
'https://www.example.com/some/path.htm?key1=v1&key2=v2'),
'redirect avoided on www.example.com': verifyNext(
'http://www.example.com'),
'redirect avoided on www.example.com:80': verifyNext(
'http://www.example.com:80'),
'redirect avoided on www.example.com with path and query': verifyNext(
'http://www.example.com/some/path.htm?key1=v1&key2=v2'),
'redirect skipped for localhost': verifyNext(
'http://localhost/'),
'redirect skipped for localhost:3000': verifyNext(
'http://localhost:3000/'),
'redirect skipped for 127.0.0.1': verifyNext(
'http://127.0.0.1/'),
'redirect skipped for 127.0.0.1:3000': verifyNext(
'http://127.0.0.1:3000/')
},
'Redirect from most subdomains to a single domain, except one' : {

@@ -127,9 +195,9 @@ topic : function(){

'http://example.com/',
'http://www.example.com/'),
'//www.example.com/'),
'redirects from anything.example.com to www.example.com': verifyRedirect(
'http://anything.example.com/',
'http://www.example.com/'),
'//www.example.com/'),
'redirects from origin.example.com to www.example.com': verifyRedirect(
'http://origin.example.com/',
'http://www.example.com/'),
'//www.example.com/'),
'redirect avoided on www.example.com': verifyNext(

@@ -162,6 +230,6 @@ 'http://www.example.com'),

'http://example.com/',
'http://www.example.com/'),
'//www.example.com/'),
'redirects from anything.example.com to www.example.com': verifyRedirect(
'http://anything.example.com/',
'http://www.example.com/'),
'//www.example.com/'),
'redirect avoided on www.example.com': verifyNext(

@@ -200,12 +268,12 @@ 'http://www.example.com'),

'http://example.com/',
'http://www.example.com/'),
'//www.example.com/'),
'redirects from anything.example.com to www.example.com': verifyRedirect(
'http://anything.example.com/',
'http://www.example.com/'),
'//www.example.com/'),
'redirect from www.example.co.uk to www.example.com/uk using static path': verifyRedirect(
'http://www.example.co.uk/',
'http://www.example.com/uk'),
'//www.example.com/uk'),
'redirect from www.example.ca/hello to www.example.com/ca/hello using path function': verifyRedirect(
'http://www.example.ca/hello',
'http://www.example.com/ca/hello'),
'//www.example.com/ca/hello'),
'redirect avoided on www.example.com': verifyNext(

@@ -212,0 +280,0 @@ 'http://www.example.com'),

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc