-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathroute.js
243 lines (191 loc) · 4.49 KB
/
route.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
/*!
* router
* Copyright(c) 2013 Roman Shtylman
* Copyright(c) 2014-2022 Douglas Christopher Wilson
* MIT Licensed
*/
'use strict'
/**
* Module dependencies.
* @private
*/
const Layer = require('./layer')
const { METHODS } = require('node:http')
/**
* Module variables.
* @private
*/
const slice = Array.prototype.slice
const flatten = Array.prototype.flat
const methods = METHODS.map((method) => method.toLowerCase())
/* istanbul ignore next */
const defer = typeof setImmediate === 'function'
? setImmediate
: function (fn) { process.nextTick(fn.bind.apply(fn, arguments)) }
/**
* Expose `Route`.
*/
module.exports = Route
/**
* Initialize `Route` with the given `path`,
*
* @param {String} path
* @api private
*/
function Route (path) {
this.path = path
this.stack = []
// route handlers for various http methods
this.methods = Object.create(null)
}
/**
* @private
*/
Route.prototype._handlesMethod = function _handlesMethod (method) {
if (this.methods._all) {
return true
}
// normalize name
let name = typeof method === 'string'
? method.toLowerCase()
: method
if (name === 'head' && !this.methods.head) {
name = 'get'
}
return Boolean(this.methods[name])
}
/**
* @return {array} supported HTTP methods
* @private
*/
Route.prototype._methods = function _methods () {
const methods = Object.keys(this.methods)
// append automatic head
if (this.methods.get && !this.methods.head) {
methods.push('head')
}
for (let i = 0; i < methods.length; i++) {
// make upper case
methods[i] = methods[i].toUpperCase()
}
return methods
}
/**
* dispatch req, res into this route
*
* @private
*/
Route.prototype.dispatch = function dispatch (req, res, done) {
let idx = 0
const stack = this.stack
let sync = 0
if (stack.length === 0) {
return done()
}
let method = typeof req.method === 'string'
? req.method.toLowerCase()
: req.method
if (method === 'head' && !this.methods.head) {
method = 'get'
}
req.route = this
next()
function next (err) {
// signal to exit route
if (err && err === 'route') {
return done()
}
// signal to exit router
if (err && err === 'router') {
return done(err)
}
// no more matching layers
if (idx >= stack.length) {
return done(err)
}
// max sync stack
if (++sync > 100) {
return defer(next, err)
}
let layer
let match
// find next matching layer
while (match !== true && idx < stack.length) {
layer = stack[idx++]
match = !layer.method || layer.method === method
}
// no match
if (match !== true) {
return done(err)
}
if (err) {
layer.handleError(err, req, res, next)
} else {
layer.handleRequest(req, res, next)
}
sync = 0
}
}
/**
* Add a handler for all HTTP verbs to this route.
*
* Behaves just like middleware and can respond or call `next`
* to continue processing.
*
* You can use multiple `.all` call to add multiple handlers.
*
* function check_something(req, res, next){
* next()
* }
*
* function validate_user(req, res, next){
* next()
* }
*
* route
* .all(validate_user)
* .all(check_something)
* .get(function(req, res, next){
* res.send('hello world')
* })
*
* @param {array|function} handler
* @return {Route} for chaining
* @api public
*/
Route.prototype.all = function all (handler) {
const callbacks = flatten.call(slice.call(arguments), Infinity)
if (callbacks.length === 0) {
throw new TypeError('argument handler is required')
}
for (let i = 0; i < callbacks.length; i++) {
const fn = callbacks[i]
if (typeof fn !== 'function') {
throw new TypeError('argument handler must be a function')
}
const layer = Layer('/', {}, fn)
layer.method = undefined
this.methods._all = true
this.stack.push(layer)
}
return this
}
methods.forEach(function (method) {
Route.prototype[method] = function (handler) {
const callbacks = flatten.call(slice.call(arguments), Infinity)
if (callbacks.length === 0) {
throw new TypeError('argument handler is required')
}
for (let i = 0; i < callbacks.length; i++) {
const fn = callbacks[i]
if (typeof fn !== 'function') {
throw new TypeError('argument handler must be a function')
}
const layer = Layer('/', {}, fn)
layer.method = method
this.methods[method] = true
this.stack.push(layer)
}
return this
}
})