-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrouter.js
More file actions
297 lines (286 loc) · 9.1 KB
/
router.js
File metadata and controls
297 lines (286 loc) · 9.1 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
const helper = require('think-helper');
const debug = require('debug')('think-router');
const querystring = require('querystring');
const assert = require('assert');
/**
*
* rules = [
* ['/index', 'test', 'get']
* ]
*
* rules = [
* {
* match: /match/,
* rules: [match, path, method, options]
* }
* ]
*
* rules = [
* {
* match: /match/,
* rules: [match, path, method, options]
* },
* ['/index', 'test', 'get']
* ]
*
*
* rules = {
* admin: {
* match: '',
* rules: [
* ['/index', 'test', 'get']
* ]
* }
* }
*/
class Router {
/**
* constructor
* @param {Object} ctx koa ctx
* @param {Function} next koa next
* @param {Object} options middleware options
*/
constructor(ctx, next, options) {
this.ctx = ctx;
this.next = next;
this.options = options;
this.modules = this.ctx.app.modules;
this.controllers = this.ctx.app.controllers;
this.pathname = this.getPathname();
this.rules = this.ctx.app.routers;
this.ctxMethod = ctx.method.toUpperCase();
}
/**
* get pathname, remove prefix & suffix
*/
getPathname() {
let pathname = this.ctx.path || '';
const prefix = this.options.prefix;
// remove prefix in pathname
if (prefix && prefix.length) {
prefix.some(item => {
if (helper.isString(item) && pathname.indexOf(item) === 0) {
pathname = pathname.slice(item.length);
return true;
}
if (helper.isRegExp(item) && item.test(pathname)) {
pathname = pathname.replace(item, '');
return true;
}
});
}
// remove suffix in pathname
const suffix = this.options.suffix;
if (suffix && suffix.length) {
suffix.some(item => {
if (helper.isString(item) && pathname.endsWith(item)) {
pathname = pathname.slice(0, pathname.length - item.length);
return true;
}
if (helper.isRegExp(item) && item.test(pathname)) {
pathname = pathname.replace(item, '');
return true;
}
});
}
// deal subdomain
const subdomain = this.options.subdomain;
if (!helper.isEmpty(subdomain)) {
const subdomainStr = this.ctx.subdomains.join(',');
if (subdomainStr && subdomain[subdomainStr]) {
if (pathname[0] === '/') {
pathname = '/' + subdomain[subdomainStr] + pathname;
} else {
pathname = subdomain[subdomainStr] + '/' + pathname;
}
}
}
return pathname;
}
/**
* get router rules
*/
getRules() {
const rules = this.rules;
if (this.modules.length === 0) {
return this.rules;
} else if (this.modules.length && helper.isObject(rules)) {
for (const m in rules) {
const match = rules[m].match;
if (match) {
assert(helper.isRegExp(match), 'router.match must be a RegExp');
if (match.test(this.pathname)) {
this.ctx.module = m;
return rules[m].rules || [];
}
}
}
return [];
}
return rules;
}
/**
* detect rule match
*/
getMatchedRule(rules) {
let rule;
const specialMethods = ['REDIRECT', 'REST'];
rules.some(item => {
if (!item.rules) {
// check rule'method matched
const itemMethod = item.method;
if (itemMethod && specialMethods.indexOf(itemMethod) === -1) {
if (itemMethod.indexOf(this.ctxMethod) === -1) return;
}
// check rule'match matched
assert(helper.isRegExp(item.match), 'router.match must be a RegExp');
const match = item.match.exec(this.pathname);
if (!match) return;
// parse query
assert(helper.isArray(item.query), 'router.query must be an array');
const query = {};
let pathname = item.path || this.pathname;
item.query.forEach((queryItem, index) => {
if (/^\d+$/.test(queryItem.name)) {
const index = parseInt(queryItem.name) + 1;
pathname = pathname.replace(new RegExp(`:${index}`, 'g'), match[index] || '');
} else {
query[queryItem.name] = match[index + 1];
pathname = pathname.replace(new RegExp(`:${queryItem.name}`, 'g'), query[queryItem.name]);
}
});
rule = Object.assign({}, item, {query, path: pathname});
return true;
} else {
const multiMatch = item.match.exec(this.pathname);
if (!multiMatch) return;
rule = this.getMatchedRule(item.rules);
}
});
return rule;
}
/**
parse module
*/
parseModule({ pathname, controllers }) {
let m = '';
if (this.modules.length) {
const pos = pathname.indexOf('/');
m = pos === -1 ? pathname : pathname.slice(0, pos);
if (this.modules.indexOf(m) > -1 && m !== 'common' && this.options.denyModules.indexOf(m) === -1) {
pathname = pos === -1 ? '' : pathname.slice(pos + 1);
} else {
m = this.options.defaultModule;
}
controllers = controllers[m] || {};
}
return { m, controllers, pathname };
}
/**
parse controller
*/
parseController({ pathname, controllers }) {
let controller = '';
// only check multiple layer controller, because single layer can get controller from pathname
for (const name in controllers) {
if (name.indexOf('/') === -1) break; // if single layer, break the loop
if (name === pathname || pathname.indexOf(`${name}/`) === 0) {
controller = name;
pathname = pathname.slice(name.length + 1);
break; // if already have matched, break the loop
}
}
if (controller === '') {
const pos = pathname.indexOf('/');
controller = pos === -1 ? pathname : pathname.slice(0, pos);
pathname = pos === -1 ? '' : pathname.slice(pos + 1);
}
controller = controller || this.options.defaultController;
return { controller, pathname };
}
/**
* parse action
*/
parseAction({ pathname, ruleMethod }) {
let action = '';
pathname = pathname.split('/');
action = ruleMethod === 'REST' ? this.ctxMethod.toLowerCase() : pathname[0];
action = action || this.options.defaultAction;
return { action };
}
/**
* parser item rule
*/
parseRule(rule) {
const ruleMethod = rule.method;
// redirect url
if (ruleMethod === 'REDIRECT') {
if (rule.options && rule.options.statusCode) {
this.ctx.status = rule.options.statusCode;
}
return this.ctx.redirect(rule.path);
}
// remove needless `/` in pathname
let pathname = rule.path.replace(/^\/|\/$/g, '').replace(/\/{2,}/g, '/');
let query = rule.query || {};
const queryPos = pathname.indexOf('?');
// parse query in path
if (queryPos > -1) {
query = Object.assign(query, querystring.parse(pathname.slice(queryPos + 1)));
pathname = pathname.slice(0, queryPos);
}
// remove when query value is undefined or empty string
for (const name in query) {
const isUndefind = query[name] === undefined;
const isEmptyString = helper.isString(query[name]) && query[name].trim() === '';
const isEmptyArray = helper.isArray(query[name]) && query[name].every(val => !val);
if (isUndefind || isEmptyString || isEmptyArray) {
delete query[name];
}
}
// parse module
const parseModuleResult = this.parseModule({ pathname, controllers: this.controllers });
const { m, controllers } = parseModuleResult;
pathname = parseModuleResult.pathname;
// parse controller
const parseControllerResult = this.parseController({ pathname, controllers });
const { controller } = parseControllerResult;
pathname = parseControllerResult.pathname;
// parse action
const { action } = this.parseAction({ pathname, ruleMethod });
// wirte back to ctx
this.ctx.module = m;
this.ctx.controller = controller;
this.ctx.action = action;
this.ctx.param(query);
debug(`RouterParser: path=${this.ctx.path}, module=${this.ctx.module}, controller=${this.ctx.controller}, action=${this.ctx.action}, query=${JSON.stringify(query)}`);
return this.next();
}
/**
* parse router
*/
run() {
const pathname = this.pathname;
// ignore user defined rules for home page request, optimize request performance
// default home page pathname is `/`, when user define prefix, it may be an empty string
if (this.options.optimizeHomepageRouter && (pathname === '' || pathname === '/')) {
this.ctx.module = this.modules.length ? this.options.defaultModule : '';
this.ctx.controller = this.options.defaultController;
this.ctx.action = this.options.defaultAction;
debug(`RouterParser: path=${this.ctx.path}, module=${this.ctx.module}, controller=${this.ctx.controller}, action=${this.ctx.action}`);
return this.next();
}
// parse rules
const rules = this.getRules();
const matchedRule = this.getMatchedRule(rules);
if (matchedRule) {
debug(`matchedRule: ${JSON.stringify(matchedRule)}`);
return this.parseRule(matchedRule);
}
if (this.options.enableDefaultRouter) {
return this.parseRule({path: this.pathname});
}
return this.next();
}
}
module.exports = Router;