forked from deployd/deployd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
141 lines (119 loc) · 3.68 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
var db = require('./db')
, Context = require('./context')
, escapeRegExp = /[\-\[\]{}()+?.,\\\^$|#\s]/g
, debug = require('debug')('router')
, doh = require('doh')
, error404 = doh.createResponder()
, async = require('async');
/**
* A `Router` routes incoming requests to the correct resource. It also initializes and
* executes the correct methods on a resource.
*
* @param {Resource Array} resources
* @api private
*/
function Router(resources, server) {
this.resources = resources || [];
this.server = server;
}
/**
* Route requests to resources with matching root paths.
* Generate a `ctx` object and hand it to the resource, along with the `res` by calling its `resource.handle(ctx, next)` method.
* If a resource calls `next()`, move on to the next resource.
*
* If all matching resources call next(), or if the router does not find a resource, respond with `404`.
*
* @param {ServerRequest} req
* @param {ServerResponse} res
* @api public
*/
Router.prototype.route = function (req, res) {
var router = this
, server = this.server
, url = req.url
, resources = this.matchResources(url)
, i = 0;
if (req._routed) {
return;
}
req._routed = true;
async.series([function(fn) {
async.forEach(router.resources, function(resource, fn) {
if(resource.handleSession) {
var ctx = new Context(resource, req, res, server);
resource.handleSession(ctx, fn);
} else {
fn();
}
}, fn);
}], function(err) {
if (err) throw err;
nextResource();
});
//TODO: Handle edge case where ctx.next() is called more than once
function nextResource() {
var resource = resources[i++]
, ctx;
var handler = doh.createHandler({req: req, res: res, server: server});
handler.run(function () {
process.nextTick(function () {
if (resource) {
debug('routing %s to %s', req.url, resource.path);
ctx = new Context(resource, req, res, server);
ctx.router = router;
// default root to false
if(ctx.session) ctx.session.isRoot = req.isRoot || false;
// external functions
var furl = ctx.url.replace('/', '');
if(resource.external && resource.external[furl]) {
resource.external[furl](ctx.body, ctx, ctx.done);
} else {
resource.handle(ctx, nextResource);
}
} else {
debug('404 %s', req.url);
res.statusCode = 404;
error404({message: 'resource not found'}, req, res);
}
});
});
}
};
/**
* Get resources whose base path matches the incoming URL, and order by specificness.
* (So that /foo/bar will handle a request before /foo)
*
* @param {String} url
* @param {Resource Array} matching resources
* @api private
*/
Router.prototype.matchResources = function(url) {
var router = this
, result;
debug('resources %j', this.resources.map(function(r) { return r.path; }));
if (!this.resources || !this.resources.length) return [];
result = this.resources.filter(function(d) {
return url.match(router.generateRegex(d.path));
}).sort(function(a, b) {
return specificness(b) - specificness(a);
});
return result;
};
/**
* Generates a regular expression from a base path.
*
* @param {String} path
* @return {RegExp} regular expression
* @api private
*/
Router.prototype.generateRegex = function(path) {
if (!path || path === '/') path = '';
path = path.replace(escapeRegExp, '\\$&');
return new RegExp('^' + path + '(?:[/?].*)?$');
};
function specificness(resource) {
var path = resource.path;
if (!path || path === '/') path = '';
return path.split('/').length;
}
module.exports = Router;