Skip to content

Commit e8213a8

Browse files
committed
fix: fixed querystring fallback, upgraded deps and lass
1 parent e972fcb commit e8213a8

File tree

8 files changed

+4487
-1966
lines changed

8 files changed

+4487
-1966
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"presets": [
3-
["env", {
3+
["@babel/env", {
44
"targets": {
55
"node": "6.4.0"
66
}

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["eslint:recommended", "plugin:node/recommended"]
3+
}

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.nyc_output
2+
coverage

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
language: node_js
22
node_js:
33
- '6'
4-
- '7'
54
- '8'
5+
- '10'
6+
script:
7+
npm run test-coverage
68
after_success:
79
npm run coverage

package.json

Lines changed: 96 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,138 @@
11
{
22
"name": "express-redirect-loop",
3-
"description":
4-
"Prevent redirect loops with sessions since HTTP referrer header is unreliable",
3+
"description": "Prevent redirect loops with sessions since HTTP referrer header is unreliable",
54
"version": "0.0.4",
65
"author": "Nick Baugh <[email protected]> (http://niftylettuce.com/)",
6+
"ava": {
7+
"failFast": true,
8+
"verbose": true,
9+
"serial": true
10+
},
711
"bugs": {
812
"url": "https://github.com/niftylettuce/express-redirect-loop/issues",
913
"email": "[email protected]"
1014
},
15+
"commitlint": {
16+
"extends": [
17+
"@commitlint/config-conventional"
18+
]
19+
},
1120
"contributors": [
1221
"Nick Baugh <[email protected]> (http://niftylettuce.com/)"
1322
],
14-
"dependencies": {},
15-
"ava": {
16-
"failFast": true,
17-
"verbose": true,
18-
"serial": true
23+
"dependencies": {
24+
"depd": "^1.1.2"
1925
},
2026
"devDependencies": {
21-
"ava": "^0.22.0",
22-
"babel-cli": "^6.26.0",
23-
"babel-preset-env": "^1.6.1",
24-
"codecov": "^2.3.0",
25-
"cross-env": "^5.0.5",
26-
"depd": "^1.1.2",
27-
"eslint": "^4.5.0",
28-
"eslint-config-prettier": "^2.9.0",
29-
"eslint-plugin-prettier": "^2.5.0",
30-
"express": "^4.16.2",
27+
"@babel/cli": "^7.1.1",
28+
"@babel/core": "^7.1.1",
29+
"@babel/preset-env": "^7.1.0",
30+
"@commitlint/cli": "^7.1.2",
31+
"@commitlint/config-conventional": "^7.1.2",
32+
"ava": "^0.25.0",
33+
"codecov": "^3.1.0",
34+
"cross-env": "^5.2.0",
35+
"eslint": "5.6.1",
36+
"eslint-config-prettier": "^3.1.0",
37+
"eslint-config-xo-lass": "^1.0.2",
38+
"eslint-plugin-node": "^7.0.1",
39+
"eslint-plugin-prettier": "^2.7.0",
40+
"express": "^4.16.3",
3141
"express-session": "^1.15.6",
32-
"fetch-cookie": "^0.7.0",
33-
"husky": "^0.14.3",
34-
"lint-staged": "^4.0.4",
35-
"node-fetch": "^1.7.3",
36-
"nyc": "^11.1.0",
37-
"prettier": "^1.6.1",
38-
"remark-cli": "^4.0.0",
39-
"remark-preset-github": "^0.0.6",
40-
"xo": "latest"
42+
"fetch-cookie": "^0.7.2",
43+
"fixpack": "^2.3.1",
44+
"husky": "^1.1.1",
45+
"lint-staged": "^7.3.0",
46+
"node-fetch": "^2.2.0",
47+
"nyc": "^13.0.1",
48+
"prettier": "^1.14.3",
49+
"remark-cli": "^5.0.0",
50+
"remark-preset-github": "^0.0.8",
51+
"rimraf": "^2.6.2",
52+
"xo": "^0.23.0"
4153
},
4254
"engines": {
4355
"node": ">=8.3"
4456
},
57+
"files": "lib",
4558
"homepage": "https://github.com/niftylettuce/express-redirect-loop",
59+
"husky": {
60+
"hooks": {
61+
"pre-commit": "npm test",
62+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
63+
}
64+
},
4665
"keywords": [
47-
"express",
48-
"redirect",
49-
"loop",
50-
"redirection",
5166
"back",
52-
"referrer",
53-
"referer",
54-
"header",
55-
"http",
67+
"bug",
5668
"connect",
57-
"middleware",
58-
"route",
5969
"endless",
70+
"express",
6071
"fix",
61-
"prevention",
62-
"prevent",
6372
"fix",
73+
"header",
6474
"hotfix",
75+
"http",
76+
"loop",
77+
"middleware",
6578
"patch",
66-
"bug",
67-
"recursive"
79+
"prevent",
80+
"prevention",
81+
"recursive",
82+
"redirect",
83+
"redirection",
84+
"referer",
85+
"referrer",
86+
"route"
6887
],
6988
"license": "MIT",
7089
"lint-staged": {
71-
"*.{js,jsx,mjs,ts,tsx,css,less,scss,json,graphql}": [
72-
"prettier --write --single-quote --trailing-comma none",
73-
"git add"
74-
],
75-
"*.md": ["remark . -qfo", "git add"]
90+
"linters": {
91+
"*.js": [
92+
"xo --fix",
93+
"git add"
94+
],
95+
"*.md": [
96+
"remark . -qfo",
97+
"git add"
98+
],
99+
"package.json": [
100+
"fixpack",
101+
"git add"
102+
]
103+
}
76104
},
77105
"main": "lib/index.js",
106+
"prettier": {
107+
"singleQuote": true,
108+
"bracketSpacing": true,
109+
"trailingComma": "none"
110+
},
78111
"remarkConfig": {
79-
"plugins": ["preset-github"]
112+
"plugins": [
113+
"preset-github"
114+
]
80115
},
81116
"repository": {
82117
"type": "git",
83118
"url": "https://github.com/niftylettuce/express-redirect-loop"
84119
},
85-
"xo": {
86-
"extends": "prettier",
87-
"plugins": ["prettier"],
88-
"parserOptions": {
89-
"sourceType": "script"
90-
},
91-
"rules": {
92-
"prettier/prettier": [
93-
"error",
94-
{
95-
"singleQuote": true,
96-
"bracketSpacing": true,
97-
"trailingComma": "none"
98-
}
99-
],
100-
"max-len": [
101-
"error",
102-
{
103-
"code": 80,
104-
"ignoreUrls": true
105-
}
106-
],
107-
"capitalized-comments": "off",
108-
"camelcase": "off",
109-
"no-warning-comments": "off"
110-
},
111-
"space": true
112-
},
113120
"scripts": {
121+
"ava": "cross-env NODE_ENV=test ava",
122+
"build": "npm run build:clean && npm run build:lib",
123+
"build:clean": "rimraf lib",
124+
"build:lib": "babel src --out-dir lib",
114125
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
115-
"lint": "xo && remark . -qfo",
116-
"precommit": "lint-staged && npm test",
117-
"test": "npm run build && npm run lint && npm run test-coverage",
118-
"test-coverage": "cross-env NODE_ENV=test nyc ava",
119-
"build": "node_modules/.bin/babel src --out-dir lib",
120-
"watch": "node_modules/.bin/babel src --watch --out-dir lib"
126+
"lint": "xo && remark . -qfo && eslint lib",
127+
"nyc": "cross-env NODE_ENV=test nyc ava",
128+
"test": "npm run build && npm run lint && npm run ava",
129+
"test-coverage": "npm run build && npm run lint && npm run nyc"
121130
},
122-
"files": "lib"
131+
"xo": {
132+
"prettier": true,
133+
"space": true,
134+
"extends": [
135+
"xo-lass"
136+
]
137+
}
123138
}

src/index.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1+
const { parse } = require('url');
12
const deprecate = require('depd')('express');
23

34
module.exports = opts => {
4-
opts = Object.assign(
5-
{
6-
defaultPath: '/',
7-
maxRedirects: 5
8-
},
9-
opts
10-
);
5+
opts = {
6+
defaultPath: '/',
7+
maxRedirects: 5,
8+
...opts
9+
};
1110
const { defaultPath, maxRedirects } = opts;
1211
return (req, res, next) => {
1312
if (!req.session)
1413
return next(new Error('Sessions required for `express-redirect-loop`'));
1514

16-
const end = res.end;
15+
const { redirect, end } = res;
1716

1817
res.end = function(chunk, encoding) {
1918
if (!req.xhr) {
2019
req.session.prevPrevPath = req.session.prevPath;
21-
req.session.prevPath = req.path;
20+
req.session.prevPath = req.originalUrl;
2221
// if it was a redirect then store how many times
2322
// so that we can limit the max number of redirects
2423
if ([301, 302].includes(res.statusCode))
@@ -31,13 +30,12 @@ module.exports = opts => {
3130
end.call(res, chunk, encoding);
3231
};
3332

34-
const redirect = res.redirect;
35-
res.redirect = function(url) {
33+
res.redirect = function(url, ...args) {
3634
let address = url;
3735
let status = 302;
3836

3937
// allow status/url
40-
const args = [].slice.call(arguments);
38+
args = [url].concat(args);
4139
if (args.length === 2) {
4240
if (typeof args[0] === 'number') {
4341
status = args[0];
@@ -57,12 +55,19 @@ module.exports = opts => {
5755
req.maxRedirects = req.session.maxRedirects || 1;
5856

5957
if (req.prevPath && address === req.prevPath) {
60-
address =
58+
if (
6159
req.prevPrevPath &&
6260
address !== req.prevPrevPath &&
6361
req.maxRedirects <= maxRedirects
64-
? req.prevPrevPath
65-
: '/';
62+
) {
63+
address = req.prevPrevPath;
64+
} else {
65+
// if the prevPrevPath w/o querystring is !== prevPrevPath
66+
// then redirect then to prevPrevPath w/o querystring
67+
const { pathname } = parse(req.prevPrevPath);
68+
if (pathname === req.prevPrevPath) address = '/';
69+
else address = pathname;
70+
}
6671
}
6772

6873
redirect.call(res, status, address);

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const express = require('express');
33
const fetch = require('fetch-cookie/node-fetch')(require('node-fetch'));
44
const session = require('express-session');
55

6-
const redirectLoop = require('../');
6+
const redirectLoop = require('..');
77

88
test.beforeEach(t => {
99
const app = express();

0 commit comments

Comments
 (0)