Socket
Socket
Sign inDemoInstall

koa-router

Package Overview
Dependencies
Maintainers
1
Versions
91
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-router - npm Package Compare versions

Comparing version 5.1.1 to 5.1.2

16

lib/layer.js

@@ -53,18 +53,2 @@ var compose = require('koa-compose');

// nested router
if (middleware[0].name === 'dispatch' || this.opts.end === false) {
var splitPath = path.split('/');
if (splitPath[splitPath.length - 1] === '') {
splitPath.pop();
}
this.opts.end = false;
middleware.unshift(function *(next) {
this.routerPath = '/' + this.path.split('/').slice(splitPath.length).join('/');
yield next;
})
}
this.fns.middleware = middleware;

@@ -71,0 +55,0 @@

200

lib/router.js

@@ -61,6 +61,3 @@ /**

this.params = {};
this.stack = {
middleware: [],
routes: []
};
this.stack = [];
};

@@ -202,9 +199,16 @@

Router.prototype[method] = function (name, path, middleware) {
var args = Array.prototype.slice.call(arguments);
var middleware;
if (typeof path === 'string') {
args.splice(2, 0, [method]);
middleware = Array.prototype.slice.call(arguments, 2);
} else {
args.splice(1, 0, [method]);
middleware = Array.prototype.slice.call(arguments, 1);
path = name;
name = null;
}
this.register.apply(this, args);
this.register(path, [method], middleware, {
name: name
});
return this;

@@ -228,7 +232,6 @@ };

*
* // runs session and authorize middleware before routing
* // use middleware only with given path
* router.use('/users', userAuth());
*
* app.use(router.routes());
*
* // use middleware only with given path
* app.use('/users', userAuth());
* ```

@@ -243,21 +246,37 @@ *

Router.prototype.use = function (path, middleware) {
var middleware;
var router = this;
middleware = Array.prototype.slice.call(arguments);
if (typeof path === 'string') {
middleware = Array.prototype.slice.call(arguments, 1);
middleware = middleware.slice(1).filter(function (fn) {
if (fn.router) {
// nested routers
fn.router.stack.forEach(function (layer) {
layer.setPrefix(path);
if (router.opts.prefix) {
layer.setPrefix(router.opts.prefix);
}
router.stack.push(layer);
});
if (!this.stack.routes.some(function (route) {
if (route.path === path) {
route.fns.middleware = middleware.concat(route.fns.middleware);
route.middleware = compose(route.fns.middleware);
return false;
}
return route.path === path;
})) {
this.stack.routes.push(new Layer(path, [], middleware, {
return true;
});
if (middleware.length) {
// middleware at path
router.register(path, [], middleware, {
end: false
}));
});
}
} else {
this.stack.middleware.push.apply(this.stack.middleware, arguments);
// middleware at root
router.register('/(.*)', [], middleware, {
end: false
});
}
return this;

@@ -282,3 +301,3 @@ };

this.stack.routes.forEach(function (route) {
this.stack.forEach(function (route) {
route.setPrefix(prefix);

@@ -299,47 +318,34 @@ });

return function *dispatch(next) {
var dispatch = function *dispatch(next) {
debug('%s %s', this.method, this.path);
var method = this.method;
var path = router.opts.routerPath || this.routerPath || this.path;
var matched = router.match(path);
var middleware = router.stack.middleware.slice(0);
var route;
var matched = router.match(path, this.method);
for (var i = matched.length - 1; i >= 0; --i) {
if (matched[i].methods.length === 0) {
middleware.push(matched[i].middleware);
} else if (~matched[i].methods.indexOf(method)) {
route = matched[i];
}
}
if (this.matched) {
this.matched.push.apply(this.matched, matched);
this.matched.push.apply(this.matched, matched.layers);
} else {
this.matched = matched;
this.matched = matched.layers;
}
// Find route matching requested path and method
if (route) {
this.route = route;
this.captures = route.captures(path, this.captures);
this.params = route.params(path, this.captures, this.params);
if (matched.route) {
this.route = matched.route;
this.captures = this.route.captures(path, this.captures);
this.params = this.route.params(path, this.captures, this.params);
debug('dispatch %s %s', this.route.path, this.route.regexp);
next = this.route.middleware.call(this, next);
next = matched.route.middleware.call(this, next);
for (var i = middleware.length - 1; i >= 0; --i) {
next = middleware[i].call(this, next);
for (var i = 0, l = matched.middleware.length; i < l; i++) {
next = matched.middleware[i].call(this, next);
}
}
yield *next;
}
else {
// Could not find any route matching the requested path
// simply yield to downstream koa middleware
return yield *next;
}
yield *next;
};
dispatch.router = this;
return dispatch;
};

@@ -352,6 +358,2 @@

*
* `router.allowedMethods()` is automatically mounted if the router is created
* with `app.use(router(app))`. Create the router separately if you do not want
* to use `.allowedMethods()`, or if you are using multiple routers.
*
* @example

@@ -425,6 +427,16 @@ *

Router.prototype.all = function (name, path, middleware) {
var args = Array.prototype.slice.call(arguments);
args.splice(typeof path == 'function' ? 1 : 2, 0, methods);
var middleware;
this.register.apply(this, args);
if (typeof path === 'string') {
middleware = Array.prototype.slice.call(arguments, 2);
} else {
middleware = Array.prototype.slice.call(arguments, 1);
path = name;
name = null;
}
this.register(path, methods, middleware, {
name: name
});
return this;

@@ -477,27 +489,18 @@ };

*
* @param {String} name Optional.
* @param {String} path Path string or regular expression.
* @param {Array.<String>} methods Array of HTTP verbs.
* @param {Function} middleware Multiple middleware also accepted.
* @returns {Route}
* @returns {Layer}
* @private
*/
Router.prototype.register = function (name, path, methods, middleware) {
if (path instanceof Array) {
middleware = Array.prototype.slice.call(arguments, 2);
methods = path;
path = name;
name = null;
}
else {
middleware = Array.prototype.slice.call(arguments, 3);
}
Router.prototype.register = function (path, methods, middleware, opts) {
opts = opts || {};
// create route
var route = new Layer(path, methods, middleware, {
end: true,
name: name,
sensitive: this.opts.sensitive || false,
strict: this.opts.strict || false
end: opts.end === false ? opts.end : true,
name: opts.name,
sensitive: opts.sensitive || this.opts.sensitive || false,
strict: opts.strict || this.opts.strict || false
});

@@ -515,3 +518,3 @@

// register route with router
this.stack.routes.push(route);
this.stack.unshift(route);

@@ -525,7 +528,7 @@ return route;

* @param {String} name
* @returns {Route|false}
* @returns {Layer|false}
*/
Router.prototype.route = function (name) {
var routes = this.stack.routes;
var routes = this.stack;

@@ -577,19 +580,38 @@ for (var len = routes.length, i=0; i<len; i++) {

* @param {String} path
* @returns {Array.<Route>} Returns matched routes.
* @param {String} method
* @returns {Object.<layer, middleware, matched>} Returns matched route layer,
* middleware, and all layers that matched the path.
* @private
*/
Router.prototype.match = function (path) {
var routes = this.stack.routes;
Router.prototype.match = function (path, method) {
var layers = this.stack;
var matched = [];
var middleware = [];
var route;
var methods;
for (var len = routes.length, i = 0; i < len; i++) {
debug('test %s %s', routes[i].path, routes[i].regexp);
for (var len = layers.length, i = 0; i < len; i++) {
debug('test %s %s', layers[i].path, layers[i].regexp);
if (routes[i].match(path)) {
matched.push(routes[i]);
if (layers[i].match(path)) {
methods = layers[i].methods;
if (method && methods.length && ~methods.indexOf(method)) {
route = layers[i];
}
if (methods.length === 0) {
middleware.push(layers[i].middleware);
}
matched.push(layers[i]);
}
}
return matched;
return {
route: route,
layers: matched,
middleware: middleware
};
};

@@ -627,3 +649,3 @@

this.params[param] = middleware;
this.stack.routes.forEach(function (route) {
this.stack.forEach(function (route) {
route.param(param, middleware);

@@ -630,0 +652,0 @@ });

@@ -13,3 +13,3 @@ {

"author": "Alex Mingoia <talk@alexmingoia.com>",
"version": "5.1.1",
"version": "5.1.2",
"keywords": [

@@ -16,0 +16,0 @@ "koa",

@@ -32,7 +32,7 @@ # koa-router

* [.routes](#module_koa-router--Router+routes) ⇒ <code>function</code>
* [.use(\[path\], middleware, \[...\])](#module_koa-router--Router+use) ⇒ <code>Router</code>
* [.use([path], middleware, [...])](#module_koa-router--Router+use) ⇒ <code>Router</code>
* [.prefix(prefix)](#module_koa-router--Router+prefix) ⇒ <code>Router</code>
* [.allowedMethods([options])](#module_koa-router--Router+allowedMethods) ⇒ <code>function</code>
* [.redirect(source, destination, code)](#module_koa-router--Router+redirect) ⇒ <code>Router</code>
* [.route(name)](#module_koa-router--Router+route) ⇒ <code>Route</code> &#124; <code>false</code>
* [.route(name)](#module_koa-router--Router+route) ⇒ <code>Layer</code> &#124; <code>false</code>
* [.url(name, params)](#module_koa-router--Router+url) ⇒ <code>String</code> &#124; <code>Error</code>

@@ -226,7 +226,6 @@ * [.param(param, middleware)](#module_koa-router--Router+param) ⇒ <code>Router</code>

// runs session and authorize middleware before routing
// use middleware only with given path
router.use('/users', userAuth());
app.use(router.routes());
// use middleware only with given path
app.use('/users', userAuth());
```

@@ -253,6 +252,2 @@ <a name="module_koa-router--Router+prefix"></a>

`router.allowedMethods()` is automatically mounted if the router is created
with `app.use(router(app))`. Create the router separately if you do not want
to use `.allowedMethods()`, or if you are using multiple routers.
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>

@@ -301,3 +296,3 @@

<a name="module_koa-router--Router+route"></a>
#### router.route(name) ⇒ <code>Route</code> &#124; <code>false</code>
#### router.route(name) ⇒ <code>Layer</code> &#124; <code>false</code>
Lookup route with given `name`.

@@ -304,0 +299,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc