redirect-ssl
Advanced tools
| /// <reference types="node" /> | ||
| import { IncomingMessage, ServerResponse } from 'http'; | ||
| declare const defaults: { | ||
| trustProxy: boolean; | ||
| redirectPort: number; | ||
| redirectHost: string; | ||
| redirectUnknown: boolean; | ||
| enabled: boolean; | ||
| statusCode: number; | ||
| exclude: never[]; | ||
| }; | ||
| declare type Options = Partial<typeof defaults>; | ||
| interface Middleware { | ||
| (req: IncomingMessage, res: ServerResponse, next?: Function): void; | ||
| create: (options: Options) => Middleware; | ||
| } | ||
| declare const _default: Middleware; | ||
| export default _default; |
| 'use strict'; | ||
| function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
| var tslib = require('tslib'); | ||
| var isHTTPS = _interopDefault(require('is-https')); | ||
| var defaults = { | ||
| trustProxy: true, | ||
| redirectPort: 443, | ||
| redirectHost: '', | ||
| redirectUnknown: true, | ||
| enabled: true, | ||
| statusCode: 307, | ||
| exclude: [] | ||
| }; | ||
| var isExcluded = function (url, patterns) { | ||
| if (patterns === void 0) { | ||
| patterns = []; | ||
| } | ||
| return patterns.some(function (pattern) { | ||
| return url.match(pattern); | ||
| }); | ||
| }; // Creates new middleware using provided options | ||
| function create(_options) { | ||
| var options = tslib.__assign(tslib.__assign({}, defaults), _options); | ||
| var _port = options.redirectPort === 443 ? '' : ':' + options.redirectPort; | ||
| function redirectSSL(req, res, next) { | ||
| var url = req.url || ''; | ||
| if (!options.enabled || isExcluded(url, options.exclude)) { | ||
| return next && next(); | ||
| } | ||
| var _isHttps = isHTTPS(req, options.trustProxy); | ||
| var shouldRedirect = options.redirectUnknown ? !_isHttps : _isHttps === false; | ||
| if (shouldRedirect) { | ||
| var _redirectHost = (options.redirectHost || req.headers.host || '').split(':')[0]; | ||
| var ـredirectURL = 'https://' + _redirectHost + _port + url; | ||
| res.writeHead(options.statusCode, { | ||
| Location: ـredirectURL | ||
| }); | ||
| return res.end(ـredirectURL); | ||
| } | ||
| return next && next(); | ||
| } | ||
| redirectSSL.create = create; | ||
| return redirectSSL; | ||
| } // Export a new instance using defaults | ||
| var index = create({}); | ||
| module.exports = index; |
+28
-1
@@ -1,5 +0,32 @@ | ||
| # Change Log | ||
| # Changelog | ||
| All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. | ||
| ## [2.0.0](https://github.com/nuxt-contrib/redirect-ssl/compare/v1.4.1...v2.0.0) (2020-05-31) | ||
| ### ⚠ BREAKING CHANGES | ||
| * xForwardedProto renamed to trustProxy | ||
| * redirect changed to enabled and default value is always true (see example in docs) | ||
| * there might be behaviour changes for express-like frameworks | ||
| ### Features | ||
| * `enabled` option ([f06e398](https://github.com/nuxt-contrib/redirect-ssl/commit/f06e3982a61bd3e6a98fdc09ab4aab85575b0e70)) | ||
| * improve redirectHost type ([88cafc0](https://github.com/nuxt-contrib/redirect-ssl/commit/88cafc0fb54c6507286d5dfa9c30a5368003513b)) | ||
| * rewrite to typescript ([d8460a7](https://github.com/nuxt-contrib/redirect-ssl/commit/d8460a73cd29328e055203181900bf2fa4d011b1)) | ||
| * trustProxy option ([1c8cb3d](https://github.com/nuxt-contrib/redirect-ssl/commit/1c8cb3d8c769a961f96f442e922242030c0d6645)) | ||
| * write redirectURL to response too ([763165b](https://github.com/nuxt-contrib/redirect-ssl/commit/763165b221a7b37957618bfe7f7f7b533790fa96)) | ||
| ### Bug Fixes | ||
| * fix types and redirectURL with non standard port ([e728797](https://github.com/nuxt-contrib/redirect-ssl/commit/e72879708dc4a664edd3366997f87965d6d99dfc)) | ||
| ### fat | ||
| * update is-https to 2.x ([d4267e7](https://github.com/nuxt-contrib/redirect-ssl/commit/d4267e76a2e176ed792a03f80b3ffebf7e39ea7e)) | ||
| <a name="1.4.1"></a> | ||
@@ -6,0 +33,0 @@ ## [1.4.1](https://github.com/nuxt-community/redirect-ssl/compare/v1.4.0...v1.4.1) (2019-08-08) |
+29
-20
| { | ||
| "name": "redirect-ssl", | ||
| "version": "1.4.1", | ||
| "description": "Connect middleware to enforce https", | ||
| "main": "index.js", | ||
| "repository": "git@github.com:nuxt-community/redirect-ssl.git", | ||
| "author": "Pooya Parsa <pooya@pi0.ir>", | ||
| "version": "2.0.0", | ||
| "description": "Connect/Express middleware to enforce https", | ||
| "repository": "nuxt-contrib/redirect-ssl", | ||
| "license": "MIT", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "scripts": { | ||
| "lint": "eslint . --ext js", | ||
| "test": "yarn lint", | ||
| "release": "standard-version && git push --follow-tags && npm publish" | ||
| "build": "bili src/index.ts --minimal", | ||
| "dev": "ts-node --dir test server.ts", | ||
| "lint": "eslint --ext ts .", | ||
| "typecheck": "tsc --noEmit -p . && tsc --noEmit -p ./test", | ||
| "release": "yarn build && standard-version && git push --follow-tags && npm publish", | ||
| "test": "yarn lint && yarn typecheck" | ||
| }, | ||
| "dependencies": { | ||
| "is-https": "^2.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "connect": "^3.7.0", | ||
| "eslint": "^5.16.0", | ||
| "eslint-config-standard": "^12.0.0", | ||
| "eslint-plugin-import": "^2.18.2", | ||
| "eslint-plugin-node": "^7.0.1", | ||
| "eslint-plugin-promise": "^4.2.1", | ||
| "eslint-plugin-standard": "^4.0.0", | ||
| "pem": "^1.13.1", | ||
| "standard-version": "^4.4.0" | ||
| }, | ||
| "dependencies": { | ||
| "is-https": "^1.0.0" | ||
| "@nuxtjs/eslint-config-typescript": "latest", | ||
| "@types/connect": "latest", | ||
| "@types/node": "latest", | ||
| "@types/pem": "latest", | ||
| "bili": "latest", | ||
| "connect": "latest", | ||
| "eslint": "latest", | ||
| "pem": "latest", | ||
| "rollup-plugin-typescript2": "latest", | ||
| "standard-version": "latest", | ||
| "ts-node": "latest", | ||
| "typescript": "latest" | ||
| } | ||
| } |
+63
-25
| # redirect-ssl | ||
| > Connect/Express middleware to enforce https using [is-https](https://www.npmjs.com/package/is-https). | ||
| [](https://npmjs.com/package/redirect-ssl) | ||
| [](https://npmjs.com/package/redirect-ssl) | ||
| [![version][npm-v-src]][npm-v-href] | ||
| [![downloads][npm-d-src]][npm-d-href] | ||
| [![ci][ci-src]][ci-href] | ||
| ## Usage | ||
| Install package | ||
| Install package: | ||
| ```bash | ||
| yarn add redirect-ssl # or npm install redirect-ssl | ||
| yarn add redirect-ssl | ||
| # or | ||
| npm install redirect-ssl | ||
| ``` | ||
| Require and use `redirect-ss`. Make sure to use this middlware as the first in your middleware chain (if using express see [middleware chain](http://expressjs.com/en/guide/using-middleware.html): | ||
| Require and use `redirect-ssl`. Make sure to use this middlware as the first in your middleware chain (if using express see [middleware chain](http://expressjs.com/en/guide/using-middleware.html): | ||
| ```js | ||
| import redirectSSL from 'redirect-ssl' | ||
| // or | ||
| const redirectSSL = require('redirect-ssl') | ||
@@ -22,29 +27,29 @@ | ||
| app.use(redirectSSL) | ||
| app.use(nuxt.render) // if using nuxt | ||
| // Using custom options | ||
| app.use(redirectSSL.create({ redirectPort: 8443 })) | ||
| ``` | ||
| The `redirect-ssl` middleware also takes an array of options upon invocation: | ||
| ### Disable for non-production or localhost | ||
| If you want to disable on `localhost`, use the exclude option: | ||
| ```js | ||
| app.use(redirectSSL.create({ redirectPort: 8443 })) | ||
| app.use(redirectSSL.create({ | ||
| exclude: ['localhost'] | ||
| })) | ||
| ``` | ||
| ## Usage with Nuxt | ||
| Only enable in production environments: | ||
| Add the `redirect-ssl` to the [`serverMiddleware`](https://nuxtjs.org/api/configuration-servermiddleware#usage) array within in the [nuxt.config.js](https://nuxtjs.org/api/configuration-server) file is the preferred usage: | ||
| ```js | ||
| export default { | ||
| serverMiddleware: [ | ||
| // Will register redirect-ssl npm package | ||
| 'redirect-ssl' | ||
| ] | ||
| } | ||
| app.use(redirectSSL.create({ | ||
| enabled: process.env.NODE_ENV === 'production' | ||
| })) | ||
| ``` | ||
| You will still need to install this package within your project for it work. | ||
| ## Options | ||
| ### xForwardedProto | ||
| ### trustProxy | ||
| - Default: `true` | ||
@@ -54,8 +59,8 @@ | ||
| ### redirect | ||
| - Default: `process.env.NODE_ENV === 'production'` | ||
| ### enabled | ||
| Only enabled in production environment. Force redirecting locally by setting this option to `true`. | ||
| - Default: `true` | ||
| ### redirectPort | ||
| - Default: `443` | ||
@@ -66,4 +71,5 @@ | ||
| ### redirectHost | ||
| - Default: `undefined` | ||
| - Default: `req.headers.host` | ||
| Redirects using this value as host, if omitted will use request host for redirects. | ||
@@ -74,2 +80,3 @@ | ||
| ### redirectUnknown | ||
| - Default: `true` | ||
@@ -80,2 +87,3 @@ | ||
| ### statusCode | ||
| - Default: `307` *Temporary Redirect* | ||
@@ -89,2 +97,3 @@ | ||
| ### exclude | ||
| - Default: `[]` | ||
@@ -94,3 +103,32 @@ | ||
| ## Using with [Nuxt.js](https://github.com/nuxt/nuxt.js) | ||
| Add the `redirect-ssl` to the [`serverMiddleware`](https://nuxtjs.org/api/configuration-servermiddleware#usage) array within in the [nuxt.config.js](https://nuxtjs.org/api/configuration-server) file is the preferred usage: | ||
| ```js | ||
| import redirectSSL from 'redirectSSL' | ||
| export default { | ||
| serverMiddleware: [ | ||
| redirectSSL.create({ | ||
| enabled: process.env.NODE_ENV === 'production' | ||
| }), | ||
| ] | ||
| } | ||
| ``` | ||
| You will still need to install this package within your project for it work. | ||
| ## License | ||
| MIT - [Nuxt.js](https://nuxtjs.org) | ||
| MIT. Made with 💖 | ||
| <!-- Refs --> | ||
| [npm-v-src]: https://img.shields.io/npm/v/redirect-ssl?style=flat-square | ||
| [npm-v-href]: https://npmjs.com/package/redirect-ssl | ||
| [npm-d-src]: https://img.shields.io/npm/dm/redirect-ssl?style=flat-square | ||
| [npm-d-href]: https://npmjs.com/package/redirect-ssl | ||
| [ci-src]: https://img.shields.io/github/workflow/status/nuxt-contrib/redirect-ssl/ci/master?style=flat-square | ||
| [ci-href]: https://github.com/nuxt-contrib/redirect-ssl/actions?query=workflow%3Aci |
| module.exports = { | ||
| "extends": "standard" | ||
| }; |
-53
| const isHTTPS = require('is-https') | ||
| // Default options | ||
| const defaults = { | ||
| 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, | ||
| exclude | ||
| } = Object.assign({}, defaults, options) | ||
| const _port = redirectPort === 443 ? '' : (':' + redirectPort) | ||
| 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() | ||
| } | ||
| } | ||
| // Create a new instance using defaults | ||
| const instance = create({}) | ||
| // Assign create to instance | ||
| instance.create = create | ||
| // Export default instance | ||
| module.exports = instance |
| { | ||
| "extends": [ | ||
| "@nuxtjs" | ||
| ] | ||
| } |
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
9957
38.66%64
23.08%128
42.22%1
-50%12
33.33%6
-14.29%2
100%2
Infinity%+ Added
- Removed
Updated