Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@koa/router

Package Overview
Dependencies
Maintainers
8
Versions
26
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 8.0.8 to 9.0.1

6

history.md

@@ -0,2 +1,8 @@

9.0.0 / 2020-04-09
==================
- Update `path-to-regexp`. Migration path: change usage of `'*'` in routes to `(.*)` or `:splat*`.
- Example: `router.get('*', ....)` becomes `router.get('(.*)') ....)`
8.0.0 / 2019-06-16

@@ -3,0 +9,0 @@ ==================

79

lib/layer.js

@@ -1,4 +0,4 @@

var debug = require('debug')('koa-router');
var pathToRegExp = require('path-to-regexp');
var uri = require('urijs');
const debug = require('debug')('koa-router');
const { pathToRegexp, compile, parse } = require('path-to-regexp');
const { parse: parseUrl, format: formatUrl } = require('url');

@@ -28,25 +28,21 @@ module.exports = Layer;

for(var i = 0; i < methods.length; i++) {
var l = this.methods.push(methods[i].toUpperCase());
if (this.methods[l-1] === 'GET') {
this.methods.unshift('HEAD');
}
for(let i = 0; i < methods.length; i++) {
const l = this.methods.push(methods[i].toUpperCase());
if (this.methods[l-1] === 'GET') this.methods.unshift('HEAD');
}
// ensure middleware is a function
for (var i = 0; i < this.stack.length; i++) {
var fn = this.stack[i];
var type = (typeof fn);
if (type !== 'function') {
for (let i = 0; i < this.stack.length; i++) {
const fn = this.stack[i];
const type = (typeof fn);
if (type !== 'function')
throw new Error(
methods.toString() + " `" + (this.opts.name || path) +"`: `middleware` "
+ "must be a function, not `" + type + "`"
`${methods.toString()} \`${this.opts.name || path}\`: \`middleware\` must be a function, not \`${type}\``
);
}
}
this.path = path;
this.regexp = pathToRegExp(path, this.paramNames, this.opts);
this.regexp = pathToRegexp(path, this.paramNames, this.opts);
debug('defined route %s %s', this.methods, this.opts.prefix + this.path);
debug('defined route %s %s', this.methods, `${this.opts.prefix}${this.path}`);
};

@@ -77,7 +73,7 @@

Layer.prototype.params = function (path, captures, existingParams) {
var params = existingParams || {};
const params = existingParams || {};
for (var len = captures.length, i=0; i<len; i++) {
for (let len = captures.length, i=0; i<len; i++) {
if (this.paramNames[i]) {
var c = captures[i];
const c = captures[i];
params[this.paramNames[i].name] = c ? safeDecodeURIComponent(c) : c;

@@ -99,4 +95,3 @@ }

Layer.prototype.captures = function (path) {
if (this.opts.ignoreCaptures) return [];
return path.match(this.regexp).slice(1);
return this.opts.ignoreCaptures ? [] : path.match(this.regexp).slice(1);
};

@@ -121,6 +116,4 @@

Layer.prototype.url = function (params, options) {
var args = params;
var url = this.path.replace(/\(\.\*\)/g, '');
var toPath = pathToRegExp.compile(url);
var replaced;
let args = params;
const url = this.path.replace(/\(\.\*\)/g, '');

@@ -135,7 +128,10 @@ if (typeof params != 'object') {

var tokens = pathToRegExp.parse(url);
var replace = {};
const toPath = compile(url, options);
let replaced;
const tokens = parse(url);
let replace = {};
if (args instanceof Array) {
for (var len = tokens.length, i=0, j=0; i<len; i++) {
for (let len = tokens.length, i=0, j=0; i<len; i++) {
if (tokens[i].name) replace[tokens[i].name] = args[j++];

@@ -152,5 +148,10 @@ }

if (options && options.query) {
var replaced = new uri(replaced)
replaced.search(options.query);
return replaced.toString();
replaced = parseUrl(replaced);
if (typeof options.query === 'string') {
replaced.search = options.query;
} else {
replaced.search = undefined;
replaced.query = options.query;
}
return formatUrl(replaced);
}

@@ -185,5 +186,5 @@

Layer.prototype.param = function (param, fn) {
var stack = this.stack;
var params = this.paramNames;
var middleware = function (ctx, next) {
const stack = this.stack;
const params = this.paramNames;
const middleware = function (ctx, next) {
return fn.call(this, ctx.params[param], ctx, next);

@@ -193,7 +194,7 @@ };

var names = params.map(function (p) {
const names = params.map(function (p) {
return p.name;
});
var x = names.indexOf(param);
const x = names.indexOf(param);
if (x > -1) {

@@ -225,5 +226,5 @@ // iterate through the stack, to figure out where to place the handler fn

if (this.path) {
this.path = prefix + this.path;
this.path = (this.path !== '/' || this.opts.strict === true) ? `${prefix}${this.path}` : prefix
this.paramNames = [];
this.regexp = pathToRegExp(this.path, this.paramNames, this.opts);
this.regexp = pathToRegexp(this.path, this.paramNames, this.opts);
}

@@ -230,0 +231,0 @@

@@ -8,7 +8,7 @@ /**

var debug = require('debug')('koa-router');
var compose = require('koa-compose');
var HttpError = require('http-errors');
var methods = require('methods');
var Layer = require('./layer');
const debug = require('debug')('koa-router');
const compose = require('koa-compose');
const HttpError = require('http-errors');
const methods = require('methods');
const Layer = require('./layer');

@@ -51,5 +51,3 @@ /**

function Router(opts) {
if (!(this instanceof Router)) {
return new Router(opts);
}
if (!(this instanceof Router)) return new Router(opts);

@@ -192,7 +190,5 @@ this.opts = opts || {};

for (var i = 0; i < methods.length; i++) {
for (let i = 0; i < methods.length; i++) {
function setMethodVerb(method) {
Router.prototype[method] = function(name, path, middleware) {
var middleware;
if (typeof path === "string" || path instanceof RegExp) {

@@ -250,31 +246,30 @@ middleware = Array.prototype.slice.call(arguments, 2);

Router.prototype.use = function () {
var router = this;
var middleware = Array.prototype.slice.call(arguments);
var path;
const router = this;
const middleware = Array.prototype.slice.call(arguments);
let path;
// support array of paths
if (Array.isArray(middleware[0]) && typeof middleware[0][0] === 'string') {
var arrPaths = middleware[0];
for (var i = 0; i < arrPaths.length; i++) {
var p = arrPaths[i];
let arrPaths = middleware[0];
for (let i = 0; i < arrPaths.length; i++) {
const p = arrPaths[i];
router.use.apply(router, [p].concat(middleware.slice(1)));
}
return this;
}
var hasPath = typeof middleware[0] === 'string';
if (hasPath) {
path = middleware.shift();
}
const hasPath = typeof middleware[0] === 'string';
if (hasPath) path = middleware.shift();
for (var i = 0; i < middleware.length; i++) {
var m = middleware[i];
for (let i = 0; i < middleware.length; i++) {
const m = middleware[i];
if (m.router) {
var cloneRouter = Object.assign(Object.create(Router.prototype), m.router, {
const cloneRouter = Object.assign(Object.create(Router.prototype), m.router, {
stack: m.router.stack.slice(0)
});
for (var j = 0; j < cloneRouter.stack.length; j++) {
var nestedLayer = cloneRouter.stack[j];
var cloneLayer = Object.assign(
for (let j = 0; j < cloneRouter.stack.length; j++) {
const nestedLayer = cloneRouter.stack[j];
const cloneLayer = Object.assign(
Object.create(Layer.prototype),

@@ -292,5 +287,5 @@ nestedLayer

function setRouterParams(paramArr) {
var routerParams = paramArr;
for (var j = 0; j < routerParams.length; j++) {
var key = routerParams[j];
const routerParams = paramArr;
for (let j = 0; j < routerParams.length; j++) {
const key = routerParams[j];
cloneRouter.param(key, router.params[key]);

@@ -327,7 +322,7 @@ }

for (var i = 0; i < this.stack.length; i++) {
var route = this.stack[i];
for (let i = 0; i < this.stack.length; i++) {
const route = this.stack[i];
route.setPrefix(prefix);
}
return this;

@@ -343,10 +338,10 @@ };

Router.prototype.routes = Router.prototype.middleware = function () {
var router = this;
const router = this;
var dispatch = function dispatch(ctx, next) {
let dispatch = function dispatch(ctx, next) {
debug('%s %s', ctx.method, ctx.path);
var path = router.opts.routerPath || ctx.routerPath || ctx.path;
var matched = router.match(path, ctx.method);
var layerChain, layer, i;
const path = router.opts.routerPath || ctx.routerPath || ctx.path;
const matched = router.match(path, ctx.method);
let layerChain;

@@ -363,4 +358,4 @@ if (ctx.matched) {

var matchedLayers = matched.pathAndMethod
var mostSpecificLayer = matchedLayers[matchedLayers.length - 1]
const matchedLayers = matched.pathAndMethod
const mostSpecificLayer = matchedLayers[matchedLayers.length - 1]
ctx._matchedRoute = mostSpecificLayer.path;

@@ -434,27 +429,25 @@ if (mostSpecificLayer.name) {

options = options || {};
var implemented = this.methods;
const implemented = this.methods;
return function allowedMethods(ctx, next) {
return next().then(function() {
var allowed = {};
const allowed = {};
if (!ctx.status || ctx.status === 404) {
for (var i = 0; i < ctx.matched.length; i++) {
var route = ctx.matched[i];
for (var j = 0; j < route.methods.length; j++) {
var method = route.methods[j];
allowed[method] = method
for (let i = 0; i < ctx.matched.length; i++) {
const route = ctx.matched[i];
for (let j = 0; j < route.methods.length; j++) {
const method = route.methods[j];
allowed[method] = method;
}
}
var allowedArr = Object.keys(allowed);
const allowedArr = Object.keys(allowed);
if (!~implemented.indexOf(ctx.method)) {
if (options.throw) {
var notImplementedThrowable;
if (typeof options.notImplemented === 'function') {
notImplementedThrowable = options.notImplemented(); // set whatever the user returns from their function
} else {
notImplementedThrowable = new HttpError.NotImplemented();
}
let notImplementedThrowable = (typeof options.notImplemented === 'function')
? options.notImplemented() // set whatever the user returns from their function
: new HttpError.NotImplemented();
throw notImplementedThrowable;

@@ -472,8 +465,6 @@ } else {

if (options.throw) {
var notAllowedThrowable;
if (typeof options.methodNotAllowed === 'function') {
notAllowedThrowable = options.methodNotAllowed(); // set whatever the user returns from their function
} else {
notAllowedThrowable = new HttpError.MethodNotAllowed();
}
let notAllowedThrowable = (typeof options.methodNotAllowed === 'function')
? options.methodNotAllowed() // set whatever the user returns from their function
: new HttpError.MethodNotAllowed();
throw notAllowedThrowable;

@@ -503,4 +494,2 @@ } else {

Router.prototype.all = function (name, path, middleware) {
var middleware;
if (typeof path === 'string') {

@@ -514,5 +503,3 @@ middleware = Array.prototype.slice.call(arguments, 2);

this.register(path, methods, middleware, {
name: name
});
this.register(path, methods, middleware, { name });

@@ -548,10 +535,6 @@ return this;

// lookup source route by name
if (source[0] !== '/') {
source = this.url(source);
}
if (source[0] !== '/') source = this.url(source);
// lookup destination route by name
if (destination[0] !== '/') {
destination = this.url(destination);
}
if (destination[0] !== '/') destination = this.url(destination);

@@ -577,9 +560,9 @@ return this.all(source, ctx => {

var router = this;
var stack = this.stack;
const router = this;
const stack = this.stack;
// support array of paths
if (Array.isArray(path)) {
for (var i = 0; i < path.length; i++) {
var curPath = path[i];
for (let i = 0; i < path.length; i++) {
const curPath = path[i];
router.register.call(router, curPath, methods, middleware, opts);

@@ -592,3 +575,3 @@ }

// create route
var route = new Layer(path, methods, middleware, {
const route = new Layer(path, methods, middleware, {
end: opts.end === false ? opts.end : true,

@@ -607,4 +590,4 @@ name: opts.name,

// add parameter middleware
for (var i = 0; i < Object.keys(this.params).length; i++) {
var param = Object.keys(this.params)[i];
for (let i = 0; i < Object.keys(this.params).length; i++) {
const param = Object.keys(this.params)[i];
route.param(param, this.params[param]);

@@ -626,8 +609,6 @@ }

Router.prototype.route = function (name) {
var routes = this.stack;
const routes = this.stack;
for (var len = routes.length, i=0; i<len; i++) {
if (routes[i].name && routes[i].name === name) {
return routes[i];
}
for (let len = routes.length, i=0; i<len; i++) {
if (routes[i].name && routes[i].name === name) return routes[i];
}

@@ -674,10 +655,10 @@

Router.prototype.url = function (name, params) {
var route = this.route(name);
const route = this.route(name);
if (route) {
var args = Array.prototype.slice.call(arguments, 1);
const args = Array.prototype.slice.call(arguments, 1);
return route.url.apply(route, args);
}
return new Error("No route found for name: " + name);
return new Error(`No route found for name: ${name}`);
};

@@ -696,5 +677,5 @@

Router.prototype.match = function (path, method) {
var layers = this.stack;
var layer;
var matched = {
const layers = this.stack;
let layer;
const matched = {
path: [],

@@ -705,3 +686,3 @@ pathAndMethod: [],

for (var len = layers.length, i = 0; i < len; i++) {
for (let len = layers.length, i = 0; i < len; i++) {
layer = layers[i];

@@ -756,4 +737,4 @@

this.params[param] = middleware;
for (var i = 0; i < this.stack.length; i++) {
var route = this.stack[i];
for (let i = 0; i < this.stack.length; i++) {
const route = this.stack[i];
route.param(param, middleware);

@@ -780,4 +761,4 @@ }

Router.url = function (path) {
var args = Array.prototype.slice.call(arguments, 1);
return Layer.prototype.url.apply({ path: path }, args);
const args = Array.prototype.slice.call(arguments, 1);
return Layer.prototype.url.apply({ path }, args);
};
{
"name": "@koa/router",
"description": "Router middleware for koa. Provides RESTful resource routing.",
"version": "8.0.8",
"version": "9.0.1",
"author": "Alex Mingoia <talk@alexmingoia.com>",

@@ -15,12 +15,14 @@ "bugs": {

"methods": "^1.1.2",
"path-to-regexp": "1.x",
"urijs": "^1.19.2"
"path-to-regexp": "^6.1.0"
},
"devDependencies": {
"@ladjs/env": "^1.0.0",
"expect.js": "^0.3.1",
"jsdoc-to-markdown": "^5.0.3",
"koa": "^2.11.0",
"mocha": "^6.2.2",
"mocha": "^7.0.1",
"nyc": "^15.0.0",
"should": "^13.2.3",
"supertest": "^4.0.2"
"supertest": "^4.0.2",
"wrk": "^1.2.0"
},

@@ -44,8 +46,10 @@ "engines": {

"type": "git",
"url": "https://github.com/koajs/koa-router.git"
"url": "https://github.com/koajs/router.git"
},
"scripts": {
"docs": "NODE_ENV=test jsdoc2md -t ./lib/API_tpl.hbs --src ./lib/*.js >| API.md",
"test": "NODE_ENV=test mocha test/**/*.js"
"test": "mocha test/**/*.js",
"coverage": "nyc npm run test",
"bench": "make -C bench"
}
}

@@ -1,4 +0,4 @@

# @koa/router
# [@koa/router](https://github.com/koajs/router)
Router middleware for [koa](https://github.com/koajs/koa)
> Router middleware for [Koa](https://github.com/koajs/koa).

@@ -23,4 +23,3 @@ [![NPM version](https://img.shields.io/npm/v/@koa/router.svg?style=flat)](https://npmjs.org/package/@koa/router)

- The API has changed to match the new promise-based middleware
signature of koa 2. See the
[koa 2.x readme](https://github.com/koajs/koa/tree/2.0.0-alpha.3) for more
signature of koa 2. See the [koa 2.x readme](https://github.com/koajs/koa/tree/2.0.0-alpha.3) for more
information.

@@ -32,6 +31,7 @@ - Middleware is now always run in the order declared by `.use()` (or `.get()`,

Install using [npm](https://www.npmjs.org/):
```sh
npm install @koa/router
```bash
# npm ..
npm i @koa/router
# yarn ..
yarn add @koa/router
```

@@ -45,6 +45,2 @@

## Tests
Run tests using `npm test`.
## Support

@@ -60,1 +56,4 @@

### License
[MIT](LICENSE)

Sorry, the diff of this file is not supported yet

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