Comparing version 1.1.2 to 1.1.3
@@ -23,3 +23,3 @@ var http = require('http'), | ||
router.get(/foo/, function () { | ||
this.res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
this.res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
this.res.end('hello world\n'); | ||
@@ -29,3 +29,3 @@ }); | ||
router.post(/foo/, function () { | ||
this.res.writeHead(200, { 'Content-Type': 'application/json' }) | ||
this.res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
this.res.end(JSON.stringify(this.req.body)); | ||
@@ -32,0 +32,0 @@ }); |
@@ -13,3 +13,3 @@ var events = require('events'), | ||
exports[name] = responses[name]; | ||
}) | ||
}); | ||
@@ -56,3 +56,3 @@ // | ||
return director.Router.prototype.configure.call(this, options); | ||
} | ||
}; | ||
@@ -70,6 +70,17 @@ // | ||
route = args.pop(), | ||
options = args.pop(); | ||
options = args.pop(), | ||
accept; | ||
if (options && options.stream) { | ||
route.stream = true; | ||
if (options) { | ||
if (options.stream) { | ||
route.stream = true; | ||
} | ||
if (options.accept) { | ||
this._hasAccepts = true; | ||
accept = options.accept; | ||
route.accept = (Array.isArray(accept) ? accept : [accept]).map(function (a) { | ||
return typeof a === 'string' ? RegExp(a) : a; | ||
}); | ||
} | ||
} | ||
@@ -103,10 +114,29 @@ | ||
var method = req.method === 'HEAD' ? 'get' : req.method.toLowerCase(), | ||
url = decodeURI(req.url.split('?', 1)[0]), | ||
fns = this.traverse(method, url, this.routes, ''), | ||
thisArg = { req: req, res: res }, | ||
self = this, | ||
contentType, | ||
runlist, | ||
stream, | ||
error; | ||
error, | ||
fns, | ||
url; | ||
// | ||
// Trap bad URLs from `decodeUri` | ||
// | ||
try { url = decodeURI(req.url.split('?', 1)[0]) } | ||
catch (ex) { url = null } | ||
if (url && this._hasAccepts) { | ||
contentType = req.headers['content-type']; | ||
fns = this.traverse(method, url, this.routes, '', function (route) { | ||
return !route.accept || route.accept.some(function (a) { | ||
return a.test(contentType); | ||
}); | ||
}); | ||
} | ||
else if (url) { | ||
fns = this.traverse(method, url, this.routes, ''); | ||
} | ||
if (this._attach) { | ||
@@ -119,3 +149,3 @@ for (var i in this._attach) { | ||
if (!fns || fns.length === 0) { | ||
error = new exports.NotFound('Could not find path: ' + req.url) | ||
error = new exports.NotFound('Could not find path: ' + req.url); | ||
if (typeof this.notfound === 'function') { | ||
@@ -135,3 +165,3 @@ this.notfound.call(thisArg, callback); | ||
runlist = this.runlist(fns); | ||
stream = this.stream || runlist.some(function (fn) { return fn.stream === true }); | ||
stream = this.stream || runlist.some(function (fn) { return fn.stream === true; }); | ||
@@ -161,3 +191,3 @@ function parseAndInvoke() { | ||
// | ||
req.once('end', parseAndInvoke) | ||
req.once('end', parseAndInvoke); | ||
} | ||
@@ -164,0 +194,0 @@ else { |
@@ -32,3 +32,3 @@ /* | ||
} | ||
}; | ||
} | ||
@@ -62,3 +62,3 @@ // | ||
})(); | ||
}; | ||
} | ||
@@ -82,3 +82,3 @@ // | ||
mod = params[param](str); | ||
if (mod !== str) { break } | ||
if (mod !== str) { break; } | ||
} | ||
@@ -375,3 +375,3 @@ } | ||
else if (typeof fn === 'string' && self.resource) { | ||
self.resource[fn].apply(thisArg, fns.captures || null) | ||
self.resource[fn].apply(thisArg, fns.captures || null); | ||
} | ||
@@ -388,2 +388,3 @@ }); | ||
// #### @regexp {string} Partial regexp representing the path to `routes`. | ||
// #### @filter {function} Filter function for filtering routes (expensive). | ||
// Core routing logic for `director.Router`: traverses the | ||
@@ -393,3 +394,3 @@ // specified `path` within `this.routes` looking for `method` | ||
// | ||
Router.prototype.traverse = function (method, path, routes, regexp) { | ||
Router.prototype.traverse = function (method, path, routes, regexp, filter) { | ||
var fns = [], | ||
@@ -402,2 +403,41 @@ current, | ||
function filterRoutes(routes) { | ||
if (!filter) { | ||
return routes; | ||
} | ||
function deepCopy(source) { | ||
var result = []; | ||
for (var i = 0; i < source.length; i++) { | ||
result[i] = Array.isArray(source[i]) ? deepCopy(source[i]) : source[i]; | ||
} | ||
return result; | ||
} | ||
function applyFilter(fns) { | ||
for (var i = fns.length - 1; i >= 0; i--) { | ||
if (Array.isArray(fns[i])) { | ||
applyFilter(fns[i]); | ||
if (fns[i].length === 0) { | ||
fns.splice(i, 1); | ||
} | ||
} | ||
else { | ||
if (!filter(fns[i])) { | ||
fns.splice(i, 1); | ||
} | ||
} | ||
} | ||
} | ||
var newRoutes = deepCopy(routes); | ||
newRoutes.matched = routes.matched; | ||
newRoutes.captures = routes.captures; | ||
newRoutes.after = routes.after.filter(filter); | ||
applyFilter(newRoutes); | ||
return newRoutes; | ||
} | ||
// | ||
@@ -413,3 +453,3 @@ // Base Case #1: | ||
next.captures = []; | ||
return next; | ||
return filterRoutes(next); | ||
} | ||
@@ -468,7 +508,7 @@ | ||
if (this.recurse && routes === this.routes) { | ||
next.push([routes['before'], routes['on']].filter(Boolean)); | ||
next.after = next.after.concat([routes['after']].filter(Boolean)) | ||
next.push([routes.before, routes.on].filter(Boolean)); | ||
next.after = next.after.concat([routes.after].filter(Boolean)); | ||
} | ||
return next; | ||
return filterRoutes(next); | ||
} | ||
@@ -503,3 +543,3 @@ | ||
fns.push([routes['before'], routes['on']].filter(Boolean)); | ||
next.after = next.after.concat([routes['after']].filter(Boolean)) | ||
next.after = next.after.concat([routes['after']].filter(Boolean)); | ||
} | ||
@@ -517,3 +557,3 @@ } | ||
// | ||
return fns; | ||
return filterRoutes(fns); | ||
} | ||
@@ -577,3 +617,3 @@ } | ||
case 'object': | ||
parent[method].push(route) | ||
parent[method].push(route); | ||
return; | ||
@@ -604,3 +644,3 @@ case 'undefined': | ||
case 'object': | ||
parent[part][method].push(route) | ||
parent[part][method].push(route); | ||
return; | ||
@@ -634,14 +674,16 @@ case 'undefined': | ||
for (i = 0; i < len; i++) { | ||
(function(method) { | ||
self._methods[method] = true; | ||
self[method] = function () { | ||
var extra = arguments.length === 1 | ||
? [method, ''] | ||
: [method]; | ||
function extend(method) { | ||
self._methods[method] = true; | ||
self[method] = function () { | ||
var extra = arguments.length === 1 | ||
? [method, ''] | ||
: [method]; | ||
self.on.apply(self, extra.concat(Array.prototype.slice.call(arguments))); | ||
}; | ||
})(methods[i]); | ||
self.on.apply(self, extra.concat(Array.prototype.slice.call(arguments))); | ||
}; | ||
} | ||
for (i = 0; i < len; i++) { | ||
extend(methods[i]); | ||
} | ||
}; | ||
@@ -648,0 +690,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"author": "Nodejitsu Inc. <info@nodejitsu.com>", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"maintainers": [ | ||
@@ -27,2 +27,3 @@ "hij1nx <paolo@nodejitsu.com>", | ||
"colors": "0.5.x", | ||
"api-easy": "0.3.x", | ||
"uglify-js": "1.0.6", | ||
@@ -29,0 +30,0 @@ "request": "2.9.x", |
@@ -10,3 +10,3 @@ <img src="https://github.com/flatiron/director/raw/master/img/director.png" /> | ||
# Status | ||
[![Build Status](https://secure.travis-ci.org/flatiron/director.png)](http://travis-ci.org/flatiron/director) | ||
[![Build Status](https://secure.travis-ci.org/flatiron/director.png?branch=master)](http://travis-ci.org/flatiron/director) | ||
@@ -13,0 +13,0 @@ # Features |
@@ -31,2 +31,8 @@ /* | ||
}, | ||
respondWithOk: function () { | ||
return function () { | ||
this.res.writeHead(200); | ||
this.res.end('ok'); | ||
}; | ||
}, | ||
streamBody: function () { | ||
@@ -47,2 +53,2 @@ var body = '', | ||
exports.macros = require('./macros'); | ||
exports.macros = require('./macros'); |
@@ -28,2 +28,14 @@ /* | ||
exports.assert404 = function (port, uri) { | ||
return { | ||
topic: function () { | ||
request({ uri: 'http://localhost:' + port + '/' + uri }, this.callback); | ||
}, | ||
"should respond with 404": function (err, res, body) { | ||
assert.isNull(err); | ||
assert.equal(res.statusCode, 404); | ||
} | ||
}; | ||
}; | ||
exports.assertPost = function(port, uri, expected) { | ||
@@ -30,0 +42,0 @@ return { |
@@ -52,3 +52,4 @@ /* | ||
"a request to bar/bazz/bark": assertBark('bar/bazz/bark'), | ||
"a request to foo/bar/bark?test=test": assertBark('foo/bar/bark?test=test') | ||
"a request to foo/bar/bark?test=test": assertBark('foo/bar/bark?test=test'), | ||
"a request to foo/%RT": macros.assert404(9090, 'foo/%RT') | ||
} | ||
@@ -55,0 +56,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
288942
43
4995
6