redirect-ssl
Advanced tools
Comparing version 1.3.0 to 1.4.0
55
index.js
@@ -5,28 +5,41 @@ const isHTTPS = require('is-https') | ||
const defaults = { | ||
xForwardedProto: true, | ||
redirectPort: 443, | ||
redirectHost: undefined, | ||
redirectUnknown: true, | ||
statusCode: 307, | ||
redirect: process.env.NODE_ENV === 'production' | ||
xForwardedProto: true, | ||
redirectPort: 443, | ||
redirectHost: undefined, | ||
redirectUnknown: true, | ||
statusCode: 307, | ||
redirect: process.env.NODE_ENV === 'production', | ||
exclude: [] | ||
} | ||
const isExcluded = function (url, patterns = []) { | ||
return patterns.some(pattern => url.match(pattern)) | ||
} | ||
// Creates new middleware using provided options | ||
function create(options) { | ||
const { xForwardedProto, redirectPort, redirectHost, statusCode, redirectUnknown, redirect } = Object.assign({}, defaults, options) | ||
const _port = redirectPort === 443 ? '' : (': ' + redirectPort) | ||
function create (options) { | ||
const { | ||
xForwardedProto, | ||
redirectPort, | ||
redirectHost, | ||
statusCode, | ||
redirectUnknown, | ||
redirect, | ||
exclude | ||
} = Object.assign({}, defaults, options) | ||
const _port = redirectPort === 443 ? '' : (': ' + redirectPort) | ||
return function redirectSSL(req, res, next) { | ||
if (redirect) { | ||
const _isHttps = isHTTPS(req, xForwardedProto) | ||
const shouldRedirect = _isHttps === false || (redirectUnknown && _isHttps === null) | ||
if (shouldRedirect) { | ||
const ـredirectURL = 'https://' + (redirectHost || req.headers.host) + _port + req.url | ||
res.writeHead(statusCode, { Location: ـredirectURL }) | ||
return res.end() | ||
} | ||
} | ||
return function redirectSSL (req, res, next) { | ||
if (redirect && !isExcluded(req.url, exclude)) { | ||
const _isHttps = isHTTPS(req, xForwardedProto) | ||
const shouldRedirect = _isHttps === false || (redirectUnknown && _isHttps === null) | ||
if (shouldRedirect) { | ||
const ـredirectURL = 'https://' + (redirectHost || req.headers.host) + _port + req.url | ||
res.writeHead(statusCode, { Location: ـredirectURL }) | ||
return res.end() | ||
} | ||
} | ||
return next() | ||
} | ||
return next() | ||
} | ||
} | ||
@@ -33,0 +46,0 @@ |
{ | ||
"name": "redirect-ssl", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Connect middleware to enforce https", | ||
@@ -9,5 +9,17 @@ "main": "index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"lint": "eslint . --ext js", | ||
"test": "yarn lint", | ||
"release": "standard-version && git push --follow-tags && npm publish" | ||
}, | ||
"devDependencies": { | ||
"connect": "^3.6.2", | ||
"pem": "^1.9.7" | ||
"connect": "^3.6.6", | ||
"eslint": "^5.6.1", | ||
"eslint-config-standard": "^12.0.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-node": "^7.0.1", | ||
"eslint-plugin-promise": "^4.0.1", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"pem": "^1.13.1", | ||
"standard-version": "^4.4.0" | ||
}, | ||
@@ -14,0 +26,0 @@ "dependencies": { |
@@ -8,3 +8,5 @@ # redirect-ssl | ||
## Usage | ||
Install package | ||
```bash | ||
@@ -15,2 +17,3 @@ yarn add redirect-ssl # or npm install redirect-ssl | ||
Require and use middleware (Make sure adding it as the first in the chain) | ||
```js | ||
@@ -63,3 +66,8 @@ const redirectSSL = require('redirect-ssl') | ||
### exclude | ||
- Default: `[]` | ||
An array of routes patterns for which redirection should be disabled. | ||
## License | ||
MIT - [Nuxt.js](https://nuxtjs.org) |
Sorry, the diff of this file is not supported yet
154445
8
47
71
9