@koa/router 13.1.1 → 14.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/HISTORY.md ADDED
@@ -0,0 +1,187 @@
1
+ # History
2
+
3
+ **[History has moved to the Releases tab of GitHub](https://github.com/koajs/router/releases).**
4
+
5
+
6
+ ## Log
7
+
8
+
9
+ ## 9.0.0 / 2020-04-09
10
+
11
+ * Update `path-to-regexp`. Migration path: change usage of `'*'` in routes to `(.*)` or `:splat*`.
12
+ * Example: `router.get('*', ....)` becomes `router.get('(.*)') ....)`
13
+
14
+
15
+ ## 8.0.0 / 2019-06-16
16
+
17
+ **others**
18
+
19
+ * [b5dd5e8](http://github.com/koajs/koa-router/commit/b5dd5e8f00e841b7061a62ab6228cbe96a999470)] - chore: rename to @koa/router (dead-horse)
20
+
21
+ ---
22
+
23
+ Changelogs inherit from koa-router.
24
+
25
+
26
+ ## 7.4.0
27
+
28
+ * Fix router.url() for multiple nested routers [#407](https://github.com/alexmingoia/koa-router/pull/407)
29
+ * `layer.name` added to `ctx` at `ctx.routerName` during routing [#412](https://github.com/alexmingoia/koa-router/pull/412)
30
+ * Router.use() was erroneously settings `(.*)` as a prefix to all routers nested with .use that did not pass an explicit prefix string as the first argument. This resulted in routes being matched that should not have been, included the running of multiple route handlers in error. [#369](https://github.com/alexmingoia/koa-router/issues/369) and [#410](https://github.com/alexmingoia/koa-router/issues/410) include information on this issue.
31
+
32
+
33
+ ## 7.3.0
34
+
35
+ * Router#url() now accepts query parameters to add to generated urls [#396](https://github.com/alexmingoia/koa-router/pull/396)
36
+
37
+
38
+ ## 7.2.1
39
+
40
+ * Respond to CORS preflights with 200, 0 length body [#359](https://github.com/alexmingoia/koa-router/issues/359)
41
+
42
+
43
+ ## 7.2.0
44
+
45
+ * Fix a bug in Router#url and append Router object to ctx. [#350](https://github.com/alexmingoia/koa-router/pull/350)
46
+ * Adds `_matchedRouteName` to context [#337](https://github.com/alexmingoia/koa-router/pull/337)
47
+ * Respond to CORS preflights with 200, 0 length body [#359](https://github.com/alexmingoia/koa-router/issues/359)
48
+
49
+
50
+ ## 7.1.1
51
+
52
+ * Fix bug where param handlers were run out of order [#282](https://github.com/alexmingoia/koa-router/pull/282)
53
+
54
+
55
+ ## 7.1.0
56
+
57
+ * Backports: merge 5.4 work into the 7.x upstream. See 5.4.0 updates for more details.
58
+
59
+
60
+ ## 7.0.1
61
+
62
+ * Fix: allowedMethods should be ctx.method not this.method [#215](https://github.com/alexmingoia/koa-router/pull/215)
63
+
64
+
65
+ ## 7.0.0
66
+
67
+ * The API has changed to match the new promise-based middleware
68
+ signature of koa 2. See the
69
+ [koa 2.x readme](https://github.com/koajs/koa/tree/2.0.0-alpha.3) for more
70
+ information.
71
+ * Middleware is now always run in the order declared by `.use()` (or `.get()`,
72
+ etc.), which matches Express 4 API.
73
+
74
+
75
+ ## 5.4.0
76
+
77
+ * Expose matched route at `ctx._matchedRoute`.
78
+
79
+
80
+ ## 5.3.0
81
+
82
+ * Register multiple routes with array of paths [#203](https://github.com/alexmingoia/koa-router/issue/143).
83
+ * Improved router.url() [#143](https://github.com/alexmingoia/koa-router/pull/143)
84
+ * Adds support for named routes and regular expressions
85
+ [#152](https://github.com/alexmingoia/koa-router/pull/152)
86
+ * Add support for custom throw functions for 405 and 501 responses [#206](https://github.com/alexmingoia/koa-router/pull/206)
87
+
88
+
89
+ ## 5.2.3
90
+
91
+ * Fix for middleware running twice when nesting routes [#184](https://github.com/alexmingoia/koa-router/issues/184)
92
+
93
+
94
+ ## 5.2.2
95
+
96
+ * Register routes without params before those with params [#183](https://github.com/alexmingoia/koa-router/pull/183)
97
+ * Fix for allowed methods [#182](https://github.com/alexmingoia/koa-router/issues/182)
98
+
99
+
100
+ ## 5.2.0
101
+
102
+ * Add support for async/await. Resolves [#130](https://github.com/alexmingoia/koa-router/issues/130).
103
+ * Add support for array of paths by Router#use(). Resolves [#175](https://github.com/alexmingoia/koa-router/issues/175).
104
+ * Inherit param middleware when nesting routers. Fixes [#170](https://github.com/alexmingoia/koa-router/issues/170).
105
+ * Default router middleware without path to root. Fixes [#161](https://github.com/alexmingoia/koa-router/issues/161), [#155](https://github.com/alexmingoia/koa-router/issues/155), [#156](https://github.com/alexmingoia/koa-router/issues/156).
106
+ * Run nested router middleware after parent's. Fixes [#156](https://github.com/alexmingoia/koa-router/issues/156).
107
+ * Remove dependency on koa-compose.
108
+
109
+
110
+ ## 5.1.1
111
+
112
+ * Match routes in order they were defined. Fixes #131.
113
+
114
+
115
+ ## 5.1.0
116
+
117
+ * Support mounting router middleware at a given path.
118
+
119
+
120
+ ## 5.0.1
121
+
122
+ * Fix bug with missing parameters when nesting routers.
123
+
124
+
125
+ ## 5.0.0
126
+
127
+ * Remove confusing API for extending koa app with router methods. Router#use()
128
+ does not have the same behavior as app#use().
129
+ * Add support for nesting routes.
130
+ * Remove support for regular expression routes to achieve nestable routers and
131
+ enable future trie-based routing optimizations.
132
+
133
+
134
+ ## 4.3.2
135
+
136
+ * Do not send 405 if route matched but status is 404. Fixes #112, closes #114.
137
+
138
+
139
+ ## 4.3.1
140
+
141
+ * Do not run middleware if not yielded to by previous middleware. Fixes #115.
142
+
143
+
144
+ ## 4.3.0
145
+
146
+ * Add support for router prefixes.
147
+ * Add MIT license.
148
+
149
+
150
+ ## 4.2.0
151
+
152
+ * Fixed issue with router middleware being applied even if no route was
153
+ matched.
154
+ * Router.url - new static method to generate url from url pattern and data
155
+
156
+
157
+ ## 4.1.0
158
+
159
+ Private API changed to separate context parameter decoration from route
160
+ matching. `Router#match` and `Route#match` are now pure functions that return
161
+ an array of routes that match the URL path.
162
+
163
+ For modules using this private API that need to determine if a method and path
164
+ match a route, `route.methods` must be checked against the routes returned from
165
+ `router.match()`:
166
+
167
+ ```javascript
168
+ var matchedRoute = router.match(path).filter(function (route) {
169
+ return ~route.methods.indexOf(method);
170
+ }).shift();
171
+ ```
172
+
173
+
174
+ ## 4.0.0
175
+
176
+ 405, 501, and OPTIONS response handling was moved into separate middleware
177
+ `router.allowedMethods()`. This resolves incorrect 501 or 405 responses when
178
+ using multiple routers.
179
+
180
+ ### Breaking changes
181
+
182
+ 4.x is mostly backwards compatible with 3.x, except for the following:
183
+
184
+ * Instantiating a router with `new` and `app` returns the router instance,
185
+ whereas 3.x returns the router middleware. When creating a router in 4.x, the
186
+ only time router middleware is returned is when creating using the
187
+ `Router(app)` signature (with `app` and without `new`).
package/README.md CHANGED
@@ -3,7 +3,9 @@
3
3
  > Router middleware for [Koa](https://github.com/koajs/koa). Maintained by [Forward Email][forward-email] and [Lad][].
4
4
 
5
5
  [![build status](https://github.com/koajs/router/actions/workflows/ci.yml/badge.svg)](https://github.com/koajs/router/actions/workflows/ci.yml)
6
- [![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
6
+
7
+ <!-- [![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) -->
8
+
7
9
  [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
8
10
  [![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)
9
11
  [![license](https://img.shields.io/github/license/koajs/router.svg)](LICENSE)
package/lib/layer.js CHANGED
@@ -13,6 +13,7 @@ module.exports = class Layer {
13
13
  * @param {String=} opts.name route name
14
14
  * @param {String=} opts.sensitive case sensitive (default: false)
15
15
  * @param {String=} opts.strict require the trailing slash (default: false)
16
+ * @param {Boolean=} opts.pathAsRegExp if true, treat `path` as a regular expression
16
17
  * @returns {Layer}
17
18
  * @private
18
19
  */
@@ -20,14 +21,12 @@ module.exports = class Layer {
20
21
  this.opts = opts;
21
22
  this.name = this.opts.name || null;
22
23
  this.methods = [];
23
- this.paramNames = [];
24
- this.stack = Array.isArray(middleware) ? middleware : [middleware];
25
-
26
24
  for (const method of methods) {
27
25
  const l = this.methods.push(method.toUpperCase());
28
26
  if (this.methods[l - 1] === 'GET') this.methods.unshift('HEAD');
29
27
  }
30
28
 
29
+ this.stack = Array.isArray(middleware) ? middleware : [middleware];
31
30
  // ensure middleware is a function
32
31
  for (let i = 0; i < this.stack.length; i++) {
33
32
  const fn = this.stack[i];
@@ -41,7 +40,20 @@ module.exports = class Layer {
41
40
  }
42
41
 
43
42
  this.path = path;
44
- this.regexp = pathToRegexp(path, this.paramNames, this.opts);
43
+ this.paramNames = [];
44
+
45
+ if (this.opts.pathAsRegExp === true) {
46
+ this.regexp = new RegExp(path);
47
+ } else if (this.path) {
48
+ if ('strict' in this.opts) {
49
+ // path-to-regexp renamed strict to trailing in v8.1.0
50
+ this.opts.trailing = this.opts.strict !== true;
51
+ }
52
+
53
+ const { regexp, keys } = pathToRegexp(this.path, this.opts);
54
+ this.regexp = regexp;
55
+ this.paramNames = keys;
56
+ }
45
57
  }
46
58
 
47
59
  /**
@@ -116,13 +128,14 @@ module.exports = class Layer {
116
128
 
117
129
  const toPath = compile(url, { encode: encodeURIComponent, ...options });
118
130
  let replaced;
119
-
120
- const tokens = parse(url);
131
+ const { tokens } = parse(url);
121
132
  let replace = {};
122
133
 
123
134
  if (Array.isArray(args)) {
124
135
  for (let len = tokens.length, i = 0, j = 0; i < len; i++) {
125
- if (tokens[i].name) replace[tokens[i].name] = args[j++];
136
+ if (tokens[i].name) {
137
+ replace[tokens[i].name] = args[j++];
138
+ }
126
139
  }
127
140
  } else if (tokens.some((token) => token.name)) {
128
141
  replace = params;
@@ -130,6 +143,10 @@ module.exports = class Layer {
130
143
  options = params;
131
144
  }
132
145
 
146
+ for (const [key, value] of Object.entries(replace)) {
147
+ replace[key] = String(value);
148
+ }
149
+
133
150
  replaced = toPath(replace);
134
151
 
135
152
  if (options && options.query) {
@@ -212,8 +229,13 @@ module.exports = class Layer {
212
229
  this.path !== '/' || this.opts.strict === true
213
230
  ? `${prefix}${this.path}`
214
231
  : prefix;
215
- this.paramNames = [];
216
- this.regexp = pathToRegexp(this.path, this.paramNames, this.opts);
232
+ if (this.opts.pathAsRegExp === true || prefix instanceof RegExp) {
233
+ this.regexp = new RegExp(this.path);
234
+ } else if (this.path) {
235
+ const { regexp, keys } = pathToRegexp(this.path, this.opts);
236
+ this.regexp = regexp;
237
+ this.paramNames = keys;
238
+ }
217
239
  }
218
240
 
219
241
  return this;
package/lib/router.js CHANGED
@@ -51,7 +51,7 @@ class Router {
51
51
  * @constructor
52
52
  */
53
53
  constructor(opts = {}) {
54
- if (!(this instanceof Router)) return new Router(opts); // eslint-disable-line no-constructor-return
54
+ if (!(this instanceof Router)) return new Router(opts);
55
55
 
56
56
  this.opts = opts;
57
57
  this.methods = this.opts.methods || [
@@ -164,14 +164,14 @@ class Router {
164
164
  }
165
165
  }
166
166
  } else {
167
- const keys = [];
168
- pathToRegexp(router.opts.prefix || '', keys);
167
+ const { keys } = pathToRegexp(router.opts.prefix || '', router.opts);
169
168
  const routerPrefixHasParam = Boolean(
170
169
  router.opts.prefix && keys.length > 0
171
170
  );
172
171
  router.register(path || '([^/]*)', [], m, {
173
172
  end: false,
174
- ignoreCaptures: !hasPath && !routerPrefixHasParam
173
+ ignoreCaptures: !hasPath && !routerPrefixHasParam,
174
+ pathAsRegExp: true
175
175
  });
176
176
  }
177
177
  }
@@ -379,7 +379,7 @@ class Router {
379
379
  * @returns {Router}
380
380
  */
381
381
  all(name, path, middleware) {
382
- if (typeof path === 'string') {
382
+ if (typeof path === 'string' || path instanceof RegExp) {
383
383
  middleware = Array.prototype.slice.call(arguments, 2);
384
384
  } else {
385
385
  middleware = Array.prototype.slice.call(arguments, 1);
@@ -395,7 +395,12 @@ class Router {
395
395
  )
396
396
  throw new Error('You have to provide a path when adding an all handler');
397
397
 
398
- this.register(path, methods, middleware, { name });
398
+ const opts = {
399
+ name,
400
+ pathAsRegExp: path instanceof RegExp
401
+ };
402
+
403
+ this.register(path, methods, middleware, { ...this.opts, ...opts });
399
404
 
400
405
  return this;
401
406
  }
@@ -454,10 +459,10 @@ class Router {
454
459
  * @returns {Layer}
455
460
  * @private
456
461
  */
457
- register(path, methods, middleware, opts = {}) {
462
+ register(path, methods, middleware, newOpts = {}) {
458
463
  const router = this;
459
464
  const { stack } = this;
460
-
465
+ const opts = { ...this.opts, ...newOpts };
461
466
  // support array of paths
462
467
  if (Array.isArray(path)) {
463
468
  for (const curPath of path) {
@@ -471,12 +476,14 @@ class Router {
471
476
  const route = new Layer(path, methods, middleware, {
472
477
  end: opts.end === false ? opts.end : true,
473
478
  name: opts.name,
474
- sensitive: opts.sensitive || this.opts.sensitive || false,
475
- strict: opts.strict || this.opts.strict || false,
476
- prefix: opts.prefix || this.opts.prefix || '',
477
- ignoreCaptures: opts.ignoreCaptures
479
+ sensitive: opts.sensitive || false,
480
+ strict: opts.strict || false,
481
+ prefix: opts.prefix || '',
482
+ ignoreCaptures: opts.ignoreCaptures,
483
+ pathAsRegExp: opts.pathAsRegExp
478
484
  });
479
485
 
486
+ // if parent prefix exists, add prefix to new route
480
487
  if (this.opts.prefix) {
481
488
  route.setPrefix(this.opts.prefix);
482
489
  }
@@ -574,7 +581,6 @@ class Router {
574
581
 
575
582
  debug('test %s %s', layer.path, layer.regexp);
576
583
 
577
- // eslint-disable-next-line unicorn/prefer-regexp-test
578
584
  if (layer.match(path)) {
579
585
  matched.path.push(layer);
580
586
 
@@ -811,14 +817,19 @@ for (const method of methods) {
811
817
  `You have to provide a path when adding a ${method} handler`
812
818
  );
813
819
 
814
- this.register(path, [method], middleware, { name });
820
+ const opts = {
821
+ name,
822
+ pathAsRegExp: path instanceof RegExp
823
+ };
815
824
 
825
+ // pass opts to register call on verb methods
826
+ this.register(path, [method], middleware, { ...this.opts, ...opts });
816
827
  return this;
817
828
  };
818
829
  }
819
830
 
820
831
  // Alias for `router.delete()` because delete is a reserved word
821
- // eslint-disable-next-line dot-notation
832
+
822
833
  Router.prototype.del = Router.prototype['delete'];
823
834
 
824
835
  module.exports = Router;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@koa/router",
3
3
  "description": "Router middleware for koa. Maintained by Forward Email and Lad.",
4
- "version": "13.1.1",
4
+ "version": "14.0.0",
5
5
  "author": "Alex Mingoia <talk@alexmingoia.com>",
6
6
  "bugs": {
7
7
  "url": "https://github.com/koajs/router/issues",
@@ -24,28 +24,28 @@
24
24
  "debug": "^4.4.1",
25
25
  "http-errors": "^2.0.0",
26
26
  "koa-compose": "^4.1.0",
27
- "path-to-regexp": "^6.3.0"
27
+ "path-to-regexp": "^8.2.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@commitlint/cli": "^17.7.2",
31
31
  "@commitlint/config-conventional": "^17.7.0",
32
32
  "@ladjs/env": "^4.0.0",
33
- "eslint": "^8.39.0",
34
- "eslint-config-xo-lass": "^2.0.1",
33
+ "chalk": "^5.4.1",
34
+ "eslint": "^9.32.0",
35
+ "eslint-plugin-unicorn": "^60.0.0",
35
36
  "fixpack": "^4.0.0",
36
- "husky": "^8.0.3",
37
+ "husky": "^9.1.7",
37
38
  "jsdoc-to-markdown": "^8.0.0",
38
- "koa": "^2.15.3",
39
+ "koa": "^3.0.1",
39
40
  "lint-staged": "^14.0.1",
40
- "mocha": "^10.7.3",
41
+ "mocha": "^11.7.1",
41
42
  "nyc": "^17.0.0",
42
43
  "remark-cli": "11",
43
44
  "remark-preset-github": "^4.0.4",
44
- "supertest": "^7.0.0",
45
- "xo": "0.53.1"
45
+ "supertest": "^7.0.0"
46
46
  },
47
47
  "engines": {
48
- "node": ">= 18"
48
+ "node": ">= 20"
49
49
  },
50
50
  "files": [
51
51
  "lib"
@@ -65,9 +65,10 @@
65
65
  },
66
66
  "scripts": {
67
67
  "bench": "make -C bench",
68
+ "benchmark": "node bench/run.js",
68
69
  "coverage": "nyc npm run test",
69
70
  "docs": "NODE_ENV=test jsdoc2md -t ./lib/API_tpl.hbs --src ./lib/*.js >| API.md",
70
- "lint": "xo --fix && remark . -qfo && fixpack",
71
+ "lint": "eslint . --fix && remark . -qfo && fixpack",
71
72
  "prepare": "husky install",
72
73
  "pretest": "npm run lint",
73
74
  "test": "mocha test/**/*.js",