@koa/router 8.0.3 → 8.0.8

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/README.md CHANGED
@@ -17,12 +17,6 @@ Router middleware for [koa](https://github.com/koajs/koa)
17
17
  * Multiple and nestable routers
18
18
  * `async/await` support
19
19
 
20
- ## Call for Maintainers
21
-
22
- This module is forked from the original [koa-router](https://github.com/ZijianHe/koa-router) due to its lack of activity. `koa-router` is the most widely used router module in the Koa community and we need maintainers. If you're interested in fixing bugs or implementing new features feel free to open a pull request. We'll be adding active contributors as collaborators.
23
-
24
- Thanks to the original authors @alexmingoia and the original team for their great work.
25
-
26
20
  ## Migrating to 7 / Koa 2
27
21
 
28
22
  - The API has changed to match the new promise-based middleware
@@ -53,3 +47,10 @@ Run tests using `npm test`.
53
47
  ## Support
54
48
 
55
49
  If you have any problem or suggestion please open an issue [here](https://github.com/koajs/router/issues).
50
+
51
+ ## Call for Maintainers
52
+
53
+ This module is forked from the original [koa-router](https://github.com/ZijianHe/koa-router) due to its lack of activity. `koa-router` is the most widely used router module in the Koa community and we need maintainers. If you're interested in fixing bugs or implementing new features feel free to open a pull request. We'll be adding active contributors as collaborators.
54
+
55
+ Thanks to the original authors @alexmingoia and the original team for their great work.
56
+
package/lib/layer.js CHANGED
@@ -25,15 +25,16 @@ function Layer(path, methods, middleware, opts) {
25
25
  this.paramNames = [];
26
26
  this.stack = Array.isArray(middleware) ? middleware : [middleware];
27
27
 
28
- methods.forEach(function(method) {
29
- var l = this.methods.push(method.toUpperCase());
28
+ for(var i = 0; i < methods.length; i++) {
29
+ var l = this.methods.push(methods[i].toUpperCase());
30
30
  if (this.methods[l-1] === 'GET') {
31
- this.methods.unshift('HEAD');
31
+ this.methods.unshift('HEAD');
32
32
  }
33
- }, this);
33
+ }
34
34
 
35
35
  // ensure middleware is a function
36
- this.stack.forEach(function(fn) {
36
+ for (var i = 0; i < this.stack.length; i++) {
37
+ var fn = this.stack[i];
37
38
  var type = (typeof fn);
38
39
  if (type !== 'function') {
39
40
  throw new Error(
@@ -41,7 +42,7 @@ function Layer(path, methods, middleware, opts) {
41
42
  + "must be a function, not `" + type + "`"
42
43
  );
43
44
  }
44
- }, this);
45
+ }
45
46
 
46
47
  this.path = path;
47
48
  this.regexp = pathToRegExp(path, this.paramNames, this.opts);
package/lib/router.js CHANGED
@@ -187,25 +187,28 @@ function Router(opts) {
187
187
  * @returns {Router}
188
188
  */
189
189
 
190
- methods.forEach(function (method) {
191
- Router.prototype[method] = function (name, path, middleware) {
192
- var middleware;
193
-
194
- if (typeof path === 'string' || path instanceof RegExp) {
195
- middleware = Array.prototype.slice.call(arguments, 2);
196
- } else {
197
- middleware = Array.prototype.slice.call(arguments, 1);
198
- path = name;
199
- name = null;
200
- }
190
+ for (var i = 0; i < methods.length; i++) {
191
+ function setMethodVerb(method) {
192
+ Router.prototype[method] = function(name, path, middleware) {
193
+ var middleware;
194
+
195
+ if (typeof path === "string" || path instanceof RegExp) {
196
+ middleware = Array.prototype.slice.call(arguments, 2);
197
+ } else {
198
+ middleware = Array.prototype.slice.call(arguments, 1);
199
+ path = name;
200
+ name = null;
201
+ }
201
202
 
202
- this.register(path, [method], middleware, {
203
- name: name
204
- });
203
+ this.register(path, [method], middleware, {
204
+ name: name
205
+ });
205
206
 
206
- return this;
207
- };
208
- });
207
+ return this;
208
+ };
209
+ }
210
+ setMethodVerb(methods[i]);
211
+ }
209
212
 
210
213
  // Alias for `router.delete()` because delete is a reserved word
211
214
  Router.prototype.del = Router.prototype['delete'];
@@ -247,10 +250,11 @@ Router.prototype.use = function () {
247
250
 
248
251
  // support array of paths
249
252
  if (Array.isArray(middleware[0]) && typeof middleware[0][0] === 'string') {
250
- middleware[0].forEach(function (p) {
253
+ var arrPaths = middleware[0];
254
+ for (var i = 0; i < arrPaths.length; i++) {
255
+ var p = arrPaths[i];
251
256
  router.use.apply(router, [p].concat(middleware.slice(1)));
252
- });
253
-
257
+ }
254
258
  return this;
255
259
  }
256
260
 
@@ -259,30 +263,40 @@ Router.prototype.use = function () {
259
263
  path = middleware.shift();
260
264
  }
261
265
 
262
- middleware.forEach(function (m) {
266
+ for (var i = 0; i < middleware.length; i++) {
267
+ var m = middleware[i];
263
268
  if (m.router) {
264
- const cloneRouter = Object.assign(Object.create(Router.prototype), m.router, {
269
+ var cloneRouter = Object.assign(Object.create(Router.prototype), m.router, {
265
270
  stack: m.router.stack.slice(0)
266
271
  });
267
272
 
268
- cloneRouter.stack.forEach(function (nestedLayer, index) {
269
- const cloneLayer = Object.assign(Object.create(Layer.prototype), nestedLayer);
273
+ for (var j = 0; j < cloneRouter.stack.length; j++) {
274
+ var nestedLayer = cloneRouter.stack[j];
275
+ var cloneLayer = Object.assign(
276
+ Object.create(Layer.prototype),
277
+ nestedLayer
278
+ );
270
279
 
271
280
  if (path) cloneLayer.setPrefix(path);
272
281
  if (router.opts.prefix) cloneLayer.setPrefix(router.opts.prefix);
273
282
  router.stack.push(cloneLayer);
274
- this[index] = cloneLayer;
275
- }, cloneRouter.stack);
283
+ cloneRouter.stack[j] = cloneLayer;
284
+ }
276
285
 
277
286
  if (router.params) {
278
- Object.keys(router.params).forEach(function (key) {
279
- cloneRouter.param(key, router.params[key]);
280
- });
287
+ function setRouterParams(paramArr) {
288
+ var routerParams = paramArr;
289
+ for (var j = 0; j < routerParams.length; j++) {
290
+ var key = routerParams[j];
291
+ cloneRouter.param(key, router.params[key]);
292
+ }
293
+ }
294
+ setRouterParams(Object.keys(router.params));
281
295
  }
282
296
  } else {
283
297
  router.register(path || '(.*)', [], m, { end: false, ignoreCaptures: !hasPath });
284
298
  }
285
- });
299
+ }
286
300
 
287
301
  return this;
288
302
  };
@@ -305,10 +319,11 @@ Router.prototype.prefix = function (prefix) {
305
319
 
306
320
  this.opts.prefix = prefix;
307
321
 
308
- this.stack.forEach(function (route) {
322
+ for (var i = 0; i < this.stack.length; i++) {
323
+ var route = this.stack[i];
309
324
  route.setPrefix(prefix);
310
- });
311
-
325
+ }
326
+
312
327
  return this;
313
328
  };
314
329
 
@@ -415,11 +430,13 @@ Router.prototype.allowedMethods = function (options) {
415
430
  var allowed = {};
416
431
 
417
432
  if (!ctx.status || ctx.status === 404) {
418
- ctx.matched.forEach(function (route) {
419
- route.methods.forEach(function (method) {
420
- allowed[method] = method;
421
- });
422
- });
433
+ for (var i = 0; i < ctx.matched.length; i++) {
434
+ var route = ctx.matched[i];
435
+ for (var j = 0; j < route.methods.length; j++) {
436
+ var method = route.methods[j];
437
+ allowed[method] = method
438
+ }
439
+ }
423
440
 
424
441
  var allowedArr = Object.keys(allowed);
425
442
 
@@ -549,9 +566,10 @@ Router.prototype.register = function (path, methods, middleware, opts) {
549
566
 
550
567
  // support array of paths
551
568
  if (Array.isArray(path)) {
552
- path.forEach(function (p) {
553
- router.register.call(router, p, methods, middleware, opts);
554
- });
569
+ for (var i = 0; i < path.length; i++) {
570
+ var curPath = path[i];
571
+ router.register.call(router, curPath, methods, middleware, opts);
572
+ }
555
573
 
556
574
  return this;
557
575
  }
@@ -571,9 +589,10 @@ Router.prototype.register = function (path, methods, middleware, opts) {
571
589
  }
572
590
 
573
591
  // add parameter middleware
574
- Object.keys(this.params).forEach(function (param) {
592
+ for (var i = 0; i < Object.keys(this.params).length; i++) {
593
+ var param = Object.keys(this.params)[i];
575
594
  route.param(param, this.params[param]);
576
- }, this);
595
+ }
577
596
 
578
597
  stack.push(route);
579
598
 
@@ -712,11 +731,13 @@ Router.prototype.match = function (path, method) {
712
731
  * @returns {Router}
713
732
  */
714
733
 
715
- Router.prototype.param = function (param, middleware) {
734
+ Router.prototype.param = function(param, middleware) {
716
735
  this.params[param] = middleware;
717
- this.stack.forEach(function (route) {
736
+ for (var i = 0; i < this.stack.length; i++) {
737
+ var route = this.stack[i];
718
738
  route.param(param, middleware);
719
- });
739
+ }
740
+
720
741
  return this;
721
742
  };
722
743
 
@@ -734,7 +755,7 @@ Router.prototype.param = function (param, middleware) {
734
755
  * @param {Object} params url parameters
735
756
  * @returns {String}
736
757
  */
737
- Router.url = function (path, params) {
758
+ Router.url = function (path) {
738
759
  var args = Array.prototype.slice.call(arguments, 1);
739
760
  return Layer.prototype.url.apply({ path: path }, args);
740
761
  };
package/package.json CHANGED
@@ -1,44 +1,49 @@
1
1
  {
2
2
  "name": "@koa/router",
3
3
  "description": "Router middleware for koa. Provides RESTful resource routing.",
4
- "repository": {
5
- "type": "git",
6
- "url": "https://github.com/koajs/koa-router.git"
7
- },
8
- "main": "lib/router.js",
9
- "files": [
10
- "lib"
11
- ],
4
+ "version": "8.0.8",
12
5
  "author": "Alex Mingoia <talk@alexmingoia.com>",
13
- "version": "8.0.3",
14
- "keywords": [
15
- "koa",
16
- "middleware",
17
- "router",
18
- "route"
19
- ],
6
+ "bugs": {
7
+ "url": "https://github.com/koajs/router/issues",
8
+ "email": "niftylettuce@gmail.com"
9
+ },
20
10
  "dependencies": {
21
- "debug": "^3.1.0",
22
- "http-errors": "^1.3.1",
23
- "koa-compose": "^3.0.0",
24
- "methods": "^1.0.1",
25
- "path-to-regexp": "^1.1.1",
26
- "urijs": "^1.19.0"
11
+ "debug": "^4.1.1",
12
+ "http-errors": "^1.7.3",
13
+ "koa-compose": "^4.1.0",
14
+ "methods": "^1.1.2",
15
+ "path-to-regexp": "1.x",
16
+ "urijs": "^1.19.2"
27
17
  },
28
18
  "devDependencies": {
29
19
  "expect.js": "^0.3.1",
30
- "jsdoc-to-markdown": "^5.0.1",
31
- "koa": "2.2.0",
32
- "mocha": "^6.2.0",
33
- "should": "^6.0.3",
20
+ "jsdoc-to-markdown": "^5.0.3",
21
+ "koa": "^2.11.0",
22
+ "mocha": "^6.2.2",
23
+ "should": "^13.2.3",
34
24
  "supertest": "^4.0.2"
35
25
  },
36
- "scripts": {
37
- "test": "NODE_ENV=test mocha test/**/*.js",
38
- "docs": "NODE_ENV=test jsdoc2md -t ./lib/API_tpl.hbs --src ./lib/*.js >| API.md"
39
- },
40
26
  "engines": {
41
27
  "node": ">= 8.0.0"
42
28
  },
43
- "license": "MIT"
29
+ "files": [
30
+ "lib"
31
+ ],
32
+ "homepage": "https://github.com/koajs/router",
33
+ "keywords": [
34
+ "koa",
35
+ "middleware",
36
+ "route",
37
+ "router"
38
+ ],
39
+ "license": "MIT",
40
+ "main": "lib/router.js",
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "https://github.com/koajs/koa-router.git"
44
+ },
45
+ "scripts": {
46
+ "docs": "NODE_ENV=test jsdoc2md -t ./lib/API_tpl.hbs --src ./lib/*.js >| API.md",
47
+ "test": "NODE_ENV=test mocha test/**/*.js"
48
+ }
44
49
  }