-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
269 lines (212 loc) · 6.17 KB
/
router.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
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
/*!
* grapnel-plus
* @author Nick Gavrilov
*/
var Grapnel = require('grapnel')
function getRouter (_Grapnel) {
'use strict'
function Router (opts) {
var self = this
_Grapnel.call(self, opts)
/**
* List of all routes registered via .page()
*/
self.routes = []
/**
* Common middlewares for all pages, that attached via `router.use()`
*/
self.middlewares = []
/**
* Version
*/
self.version = '0.0.3 (Grapnel v' + self.version + ')'
}
var proto = Router.prototype = Object.create(_Grapnel.prototype)
proto.constructor = Router
proto.getRoute = function (name) {
if (typeof name === 'object' && name.name) {
return name
}
return this.getRouteBy('name', name)
}
proto.getRouteBy = function (by, value) {
var self = this
var routes = self.routes
if (by === 'path') {
var path = value.replace(self.options.root, '')
for (var i = 0; i < routes.length; i++) {
/* eslint-disable new-cap */
if (new Grapnel.regexRoute(routes[i][by], []).test(path)) {
/* eslint-enable new-cap */
return routes[i]
}
}
}
return self.routes.find(function (r) {
return r[by] === value
}) || null
}
/**
* Register a new group
*
* @param {String} root
* @param {Function} fn
* @returns {Router}
*/
proto.group = function (root, fn) {
var self = this
var group = new Group(self, root)
fn.call(group)
return self
}
/**
* Register a new page (GET requests only)
*
* @param {string} path - path after '<APPNAME>/'
* @param {string|function} page - name of the page which should point to a file
* @param {number|function} routeName
* @param {function} ...middlewares
*/
proto.page = function (path, page, routeName) {
var group = this instanceof Group && this
var self = group ? group.router : this
page || (page = null)
routeName = uniqueRouteName(self, path, page, routeName)
// strip trailing slash from "path"
if (path[path.length - 1] === '/') {
path = path.slice(0, -1)
}
// if it's not a full path
if (path[0] !== '/') {
path = (self.options.root || '') + '/' + path
}
path = path.replace(/\/+/g, '/')
// Hook middlewares in a proper order
var middlewares = []
for (var i = 0; i < self.middlewares.length; i++) {
var mv = self.middlewares[i]
// if the middleware was registered via .use() from within a group
if (mv[0] instanceof Group) {
// only attach middlewares from the current group, otherways skip it
if (mv[0] === group) {
middlewares = middlewares.concat(mv[1])
}
} else {
middlewares.push(mv)
}
}
for (var j = 1, args = arguments; j < args.length; j++) {
if (typeof args[j] === 'function') middlewares.push(args[j])
}
self.hasPages = true
self.routes.push({
name: routeName,
page: page,
path: path
})
// Register Grapnel route
self.get.apply(self, [path].concat(middlewares))
}
/**
* Register middleware(s)
*
* @param {Function|Array} middleware, middleware2, [middleware3, ...], ...
* @returns {Router} instance
*/
proto.use = function () {
var group = this instanceof Group && this
var self = group ? group.router : this
if (!group && self.hasPages || group && group.hasPages) {
throw Error('`use` method can be only used before `page`')
}
for (var i = 0, args = arguments; i < args.length; i++) {
var middleware = args[i]
if (typeof middleware === 'function') {
self.middlewares.push(middleware)
// If it's an array of middlewares attached by `group.use()
} else if (middleware && middleware[0] instanceof Group &&
(middleware[1].every(function (m) {
return typeof m === 'function' || m instanceof Array &&
m.every(function (m) { return typeof m === 'function' })
}))) {
// Flatten the array (2 levels only, should be enough)
middleware[1] = [].concat.apply([], middleware[1])
// Find a set of group middlewares and append new middlewares to it,
// or create if it's not already there
var mv = self.middlewares.find(function (m) {
return m[0] === middleware[0]
})
if (mv) {
mv[1] = mv[1].concat(middleware[1])
} else {
self.middlewares.push(middleware)
}
// Arrays of middlewares
} else if (middleware instanceof Array &&
(middleware.every(function (m) { return typeof m === 'function' }))) {
self.middlewares = self.middlewares.concat(middleware)
}
}
return self
}
return Router
}
/**
* Group of routes
*/
function Group (router, root) {
var self = this
/**
* Router instance
* @type {Router}
*/
self.router = router
self.root = root
self.middlewares = []
}
Group.prototype = {
page: function () {
var self = this
var args = arguments
// prepend root to path
args[0] = self.root + '/' + args[0]
self.router.page.apply(self, args)
self.hasPages = true
return self
},
use: function () {
var self = this
var middlewares = Array.prototype.slice.call(arguments)
if (self.hasPages) {
throw Error('`use` method can be only used before `page`')
}
self.middlewares = self.middlewares.concat(middlewares)
self.router.use.apply(self, [ [self, middlewares] ])
return self
}
}
/**
* Generate a unique name for the route
*/
function uniqueRouteName (router, path, page, routeName) {
if (typeof routeName !== 'string') {
routeName = page
if (typeof page !== 'string') {
routeName = path.toLowerCase()
.trim()
// strip query string
.replace(/\?.*$/, '')
// replace all special chars with "_"
.replace(/\W+/g, '_')
// trim "_" chars
.replace(/(?:^_+|_+$)/g, '')
}
}
var _routeName = routeName
for (var increment = ''; router.getRoute(routeName); increment++) {
routeName = _routeName + increment
}
return routeName
}
module.exports = getRouter
module.exports.Group = Group