-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (93 loc) · 3.47 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* koa-redirects-path index.js
*/
const querystring = require('querystring');
const { pathToRegexp, parse, compile } = require('path-to-regexp');
const MAX_CACHE_SIZE = 1024;
module.exports = function ({ redirects, maxUrlLength = 1024, trailingSlash = false, onRedirect = async () => {} }) {
const redirectList = redirects.map((redirect) => {
const parsedSourceReg = parse(redirect.source);
const isRegPath = parsedSourceReg.length > 1;
return {
...redirect,
parsedSourceReg,
sourceReg: pathToRegexp(redirect.source),
parsedDestinationReg: isRegPath ? parse(redirect.destination) : [redirect.destination],
destinationToPath: isRegPath
? compile(redirect.destination, {
encode: encodeURIComponent,
})
: () => redirect.destination,
};
});
async function doRedirect(ctx, redirect) {
const { query } = ctx;
const { redirectPath, permanent = false } = redirect;
let redirectUrl = redirectPath;
if (Object.keys(query).length > 0) {
redirectUrl = `${redirectPath}?${querystring.stringify(query)}`;
}
redirectUrl = redirectUrl.substr(0, maxUrlLength);
ctx.status = permanent ? 301 : 302;
ctx.redirect(redirectUrl);
await onRedirect(ctx, {
...redirect,
redirectUrl,
});
}
return async function redirectsPath(ctx, next) {
const { path } = ctx;
if (!trailingSlash && path !== '/' && path.endsWith('/')) {
return await doRedirect(ctx, { redirectPath: path.replace(/\/+$/, '') })
}
if (redirectList.length === 0) {
return await next();
}
let pathExec = null;
let redirect = null
if (!ctx.app.redirectsPathMap) {
ctx.app.redirectsPathMap = new Map();
} else if (ctx.app.redirectsPathMap.size > MAX_CACHE_SIZE) {
ctx.app.redirectsPathMap.delete(ctx.app.redirectsPathMap.keys()[0])
}
if (ctx.app.redirectsPathMap.has(path)) {
redirect = ctx.app.redirectsPathMap.get(path);
if (redirect === null) {
return await next();
}
return await doRedirect(ctx, redirect);
}
for (const item of redirectList) {
pathExec = item.sourceReg.exec(path);
if (pathExec) {
redirect = item;
break;
}
}
if (!redirect) {
ctx.app.redirectsPathMap.set(path, null);
return await next();
}
if (redirect.parsedDestinationReg.length === 1) {
redirect = {
...redirect,
redirectPath: redirect.destination,
};
ctx.app.redirectsPathMap.set(path, redirect);
return await doRedirect(ctx, redirect);
}
const params = redirect.parsedSourceReg.reduce((result, reg, index) => {
if (Object.keys(reg).includes('name')) {
return {
...result,
[reg.name]: pathExec[index],
};
}
return result;
}, {});
const redirectPath = decodeURIComponent(redirect.destinationToPath(params));
redirect = { ...redirect, redirectPath };
ctx.app.redirectsPathMap.set(path, redirect)
return await doRedirect(ctx, redirect);
};
};