-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
73 lines (57 loc) · 1.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
const URL = require('url').URL;
module.exports = removeTrailingSlashes;
function removeTrailingSlashes(opts) {
opts = opts || {};
if (opts.defer !== false) {
opts.defer = opts.defer || true;
}
if (opts.chained !== false) {
opts.chained = opts.chained || true;
}
return async function(ctx, next) {
if (opts.defer) {
await next();
}
let path;
let querystring = '';
// We have already done a redirect and we will continue if we are in chained mode
if (opts.chained && ctx.status === 301) {
const location = ctx.response.get('Location') || '';
// We can't use ctx.querystring because it may not be up to date
const parsedLocation = location.match(/\?(.*)$/);
if (parsedLocation && parsedLocation[1]) {
querystring = parsedLocation[1];
}
path = getPath(location, querystring);
} else if (ctx.status !== 301) {
querystring = ctx.querystring;
path = getPath(ctx.originalUrl, ctx.querystring);
}
if (path && haveSlash(path)) {
path = path.slice(0, -1);
const query = querystring.length ? '?' + querystring : '';
try {
const newURL = new URL(path + query, ctx.request.URL);
if (ctx.origin === newURL.origin) {
ctx.status = 301;
ctx.redirect(path + query);
}
} catch (err) {
// Intentionally left blank. Can't parse the requested path as URL
}
}
if (!opts.defer) {
await next();
}
};
}
function haveSlash(path) {
return path !== '/' && path.slice(-1) === '/';
}
function getPath(original, querystring) {
if (querystring.length) {
return original.slice(0, -querystring.length - 1);
}
return original;
}