-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (127 loc) · 4.06 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
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
'use strict';
var util = require('util');
var error = require('./lib/error');
var connect = require('./lib/connect');
/**
* @param option Object
* - simulation: boolean whether support put and delete simulation
* - size: number to save path cache size
* - base: string base path
* - fixSlash: boolean if true the request 127.0.0.1/hello/ will be ok
* @constructor
*/
function Router(option) {
this._option = option;
this._cache = option && option.size ? new util.LruCache(option.size) : new util.LruCache(100);
this._base = option && option.base ? option.base : '';
this._pathParams = {};
this._map = {
get: {},
post: {},
delete: {},
put: {},
all: {}
}
}
Router.prototype = {
getAllRoute: function() {
var that = this;
return function(next) {
var method = this.method.toLowerCase();
//var methodSim = this.query._method || this.form._method;
var methodSim = this.form._method;
var path = this.path, regexPath;
var l = path.length - 1;
if (that._option
&& that._option.fixSlash
&& path.lastIndexOf('/') == l) {
path = path.substr(0, l);
}
method = filterMethod.call(that, method, methodSim);
regexPath = findPath.call(that, method, path);
extractParams.call(this, that, regexPath, path);
exec.call(this, that, method, regexPath);
next && next();
}
}
};
var methodList = ['get', 'post', 'put', 'delete'];
methodList.concat(['all']).forEach(function(m) {
Router.prototype[m] = function() {
var argv = Array.prototype.slice.call(arguments);
var regex = routeRegex.call(this, argv.shift());
this._map[m][regex] = argv;
}
});
module.exports = function(option) {
return new Router(option);
};
function extractParams(that, regexPath, requestPath) {
var _params = that._pathParams[regexPath];
if (!_params || _params.length === 0) {
return;
}
this.params = this.params || {};
var regex = new RegExp('^' + that._base + regexPath + '$'),
params = regex.exec(requestPath),
_this = this;
params.shift();
params.forEach(function(p, idx) {
_this.params[_params[idx]] = p;
});
}
function exec(that, method, regexPath) {
var executive = that._map[method][regexPath];
if (that._map['all'][regexPath]) {
executive = that._map['all'][regexPath].concat(executive);
}
connect.call(this, executive).call(this);
}
function filterMethod(method, methodSim) {
if (!!!method) {
throw error.P('not found method');
}
if (methodList.indexOf(method) != -1 && !!!methodSim) {
return method;
} else if (method === 'post' && (this._option && this._option.simulation)
&& (methodSim === 'put' || methodSim === 'delete')) {
return methodSim;
} else {
throw error.P('not found method');
}
}
function findPath(method, path) {
var cacheResult = this._cache.get(path);
if (cacheResult && cacheResult.m === method) {
return cacheResult.p;
} else {
for (var _regex in this._map[method]) {
var regex = new RegExp('^' + this._base + _regex + '$'); //TODO: better
if (regex.test(path)) {
this._cache.set(path, {
m: method,
p: _regex
});
return _regex;
}
}
}
throw error.P('not found path');
}
function routeRegex(regex) {
if (regex.indexOf('^') === 0) {
regex = regex.substr(1);
}
if (regex.indexOf('$') === regex.length - 1) {
regex = regex.substr(0, regex.length - 1);
}
var paramsRegex = /:[0-9a-zA-Z]+/g;
var p;
var params = [];
while (p = paramsRegex.exec(regex)) {
params.push(p[0].substr(1));
}
regex = regex.replace(/:[0-9a-zA-Z]+/g, '([^\/]+)');
this._pathParams[regex] = params;
return regex;
}