From 048d367fe998100775b38cea97e1981a1d40a871 Mon Sep 17 00:00:00 2001 From: sinewyk Date: Wed, 8 Apr 2020 21:41:02 +0200 Subject: [PATCH 1/5] upgrade path-to-regexp --- lib/layer.js | 10 +++++----- package.json | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/layer.js b/lib/layer.js index 56fc8ee..46c6954 100644 --- a/lib/layer.js +++ b/lib/layer.js @@ -1,5 +1,5 @@ var debug = require('debug')('koa-router'); -var pathToRegExp = require('path-to-regexp'); +var { pathToRegexp, compile, parse } = require('path-to-regexp'); var uri = require('urijs'); module.exports = Layer; @@ -45,7 +45,7 @@ function Layer(path, methods, middleware, opts) { } 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); }; @@ -117,7 +117,7 @@ Layer.prototype.captures = function (path) { Layer.prototype.url = function (params, options) { var args = params; var url = this.path.replace(/\(\.\*\)/g, ''); - var toPath = pathToRegExp.compile(url); + var toPath = compile(url); var replaced; if (typeof params != 'object') { @@ -128,7 +128,7 @@ Layer.prototype.url = function (params, options) { } } - var tokens = pathToRegExp.parse(url); + var tokens = parse(url); var replace = {}; if (args instanceof Array) { @@ -216,7 +216,7 @@ Layer.prototype.setPrefix = function (prefix) { if (this.path) { this.path = prefix + this.path; this.paramNames = []; - this.regexp = pathToRegExp(this.path, this.paramNames, this.opts); + this.regexp = pathToRegexp(this.path, this.paramNames, this.opts); } return this; diff --git a/package.json b/package.json index 26ba147..97d9073 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "http-errors": "^1.7.3", "koa-compose": "^4.1.0", "methods": "^1.1.2", - "path-to-regexp": "1.x", + "path-to-regexp": "^6.1.0", "urijs": "^1.19.2" }, "devDependencies": { From ff31e4b355bbb732ffd87605542eb3d76986aa70 Mon Sep 17 00:00:00 2001 From: sinewyk Date: Wed, 8 Apr 2020 21:49:51 +0200 Subject: [PATCH 2/5] fix correct usage of Layer constructor --- test/lib/layer.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/lib/layer.js b/test/lib/layer.js index 49ec31f..fcf6afc 100644 --- a/test/lib/layer.js +++ b/test/lib/layer.js @@ -218,7 +218,7 @@ describe('Layer', function() { }); it('param with paramNames positive check', function () { - var route = new Layer('/:category/:title', ['get'], [function () {}], 'books'); + var route = new Layer('/:category/:title', ['get'], [function () {}], {name: 'books'}); route.paramNames = [{ name: 'category', }] @@ -230,7 +230,7 @@ describe('Layer', function() { describe('Layer#url()', function() { it('generates route URL', function() { - var route = new Layer('/:category/:title', ['get'], [function () {}], 'books'); + var route = new Layer('/:category/:title', ['get'], [function () {}], {name: 'books'}); var url = route.url({ category: 'programming', title: 'how-to-node' }); url.should.equal('/programming/how-to-node'); url = route.url('programming', 'how-to-node'); @@ -238,12 +238,12 @@ describe('Layer', function() { }); it('escapes using encodeURIComponent()', function() { - var route = new Layer('/:category/:title', ['get'], [function () {}], 'books'); + var route = new Layer('/:category/:title', ['get'], [function () {}], {name: 'books'}); var url = route.url({ category: 'programming', title: 'how to node' }); url.should.equal('/programming/how%20to%20node'); }); it('setPrefix method checks Layer for path', function () { - const route = new Layer('/category', ['get'], [function () {}], 'books'); + const route = new Layer('/category', ['get'], [function () {}], {name: 'books'}); route.path = '/hunter2' const prefix = route.setPrefix('TEST') prefix.path.should.equal('TEST/hunter2') From 78a292925594f6acb6def50723094467e7c2c413 Mon Sep 17 00:00:00 2001 From: sinewyk Date: Wed, 8 Apr 2020 22:04:44 +0200 Subject: [PATCH 3/5] Fix Layer#url options to handle encode --- lib/layer.js | 5 +++-- test/lib/layer.js | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/layer.js b/lib/layer.js index 46c6954..5311267 100644 --- a/lib/layer.js +++ b/lib/layer.js @@ -117,8 +117,6 @@ Layer.prototype.captures = function (path) { Layer.prototype.url = function (params, options) { var args = params; var url = this.path.replace(/\(\.\*\)/g, ''); - var toPath = compile(url); - var replaced; if (typeof params != 'object') { args = Array.prototype.slice.call(arguments); @@ -128,6 +126,9 @@ Layer.prototype.url = function (params, options) { } } + var toPath = compile(url, options); + var replaced; + var tokens = parse(url); var replace = {}; diff --git a/test/lib/layer.js b/test/lib/layer.js index fcf6afc..3508e44 100644 --- a/test/lib/layer.js +++ b/test/lib/layer.js @@ -239,7 +239,10 @@ describe('Layer', function() { it('escapes using encodeURIComponent()', function() { var route = new Layer('/:category/:title', ['get'], [function () {}], {name: 'books'}); - var url = route.url({ category: 'programming', title: 'how to node' }); + var url = route.url( + { category: 'programming', title: 'how to node' }, + { encode: encodeURIComponent } + ); url.should.equal('/programming/how%20to%20node'); }); it('setPrefix method checks Layer for path', function () { From 5fcc2ed01a377fe935e0b540226ddc8ab11b53d3 Mon Sep 17 00:00:00 2001 From: sinewyk Date: Wed, 8 Apr 2020 22:25:24 +0200 Subject: [PATCH 4/5] Fix router#url tests --- test/lib/router.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/test/lib/router.js b/test/lib/router.js index 397335b..0969e12 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -1223,9 +1223,15 @@ describe('Router', function () { router.get('books', '/:category/:title', function (ctx) { ctx.status = 204; }); - var url = router.url('books', { category: 'programming', title: 'how to node' }); + var url = router.url( + 'books', + { category: 'programming', title: 'how to node' }, + { encode: encodeURIComponent } + ); url.should.equal('/programming/how%20to%20node'); - url = router.url('books', 'programming', 'how to node'); + url = router.url('books', 'programming', 'how to node', { + encode: encodeURIComponent, + }); url.should.equal('/programming/how%20to%20node'); done(); @@ -1245,9 +1251,15 @@ describe('Router', function () { }); router.use(embeddedRouter.routes()); app.use(router.routes()); - var url = router.url('chapters', { chapterName: 'Learning ECMA6', pageNumber: 123 }); + var url = router.url( + 'chapters', + { chapterName: 'Learning ECMA6', pageNumber: 123 }, + { encode: encodeURIComponent } + ); url.should.equal('/books/chapters/Learning%20ECMA6/123'); - url = router.url('chapters', 'Learning ECMA6', 123); + url = router.url('chapters', 'Learning ECMA6', 123, { + encode: encodeURIComponent, + }); url.should.equal('/books/chapters/Learning%20ECMA6/123'); done(); }); @@ -1269,7 +1281,11 @@ describe('Router', function () { embeddedRouter.use(embeddedRouter2.routes()); router.use(embeddedRouter.routes()); app.use(router.routes()); - var url = router.url('chapters', { chapterName: 'Learning ECMA6', pageNumber: 123 }); + var url = router.url( + 'chapters', + { chapterName: 'Learning ECMA6', pageNumber: 123 }, + { encode: encodeURIComponent } + ); url.should.equal('/books/chapters/Learning%20ECMA6/pages/123'); done(); }); @@ -1845,7 +1861,11 @@ describe('Router', function () { }); it('escapes using encodeURIComponent()', function () { - var url = Router.url('/:category/:title', { category: 'programming', title: 'how to node' }); + var url = Router.url( + '/:category/:title', + { category: 'programming', title: 'how to node' }, + { encode: encodeURIComponent } + ); url.should.equal('/programming/how%20to%20node'); }); From e047222f10a63dc9cf57815f9a2697772a44748b Mon Sep 17 00:00:00 2001 From: sinewyk Date: Wed, 8 Apr 2020 23:18:08 +0200 Subject: [PATCH 5/5] Fix router prefix and index '/' route https://github.com/pillarjs/path-to-regexp/blob/master/History.md#200--2017-08-23 > Explicitly handle trailing delimiters instead of trimming them (e.g. /test/ is now treated as /test/ instead of /test when matching) --- lib/layer.js | 6 ++++- test/lib/router.js | 59 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/lib/layer.js b/lib/layer.js index 5311267..d83b866 100644 --- a/lib/layer.js +++ b/lib/layer.js @@ -215,7 +215,11 @@ Layer.prototype.param = function (param, fn) { Layer.prototype.setPrefix = function (prefix) { if (this.path) { - this.path = prefix + this.path; + if (this.path !== '/' || this.opts.strict === true) { + this.path = prefix + this.path; + } else { + this.path = prefix; + } this.paramNames = []; this.regexp = pathToRegexp(this.path, this.paramNames, this.opts); } diff --git a/test/lib/router.js b/test/lib/router.js index 0969e12..41e7a15 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -1107,7 +1107,7 @@ describe('Router', function () { done(); }, error => done(error)); }); - + it('uses a same router middleware at given paths continuously - ZijianHe/koa-router#gh-244 gh-18', function (done) { const app = new Koa(); const base = new Router({ prefix: '/api' }); @@ -1234,7 +1234,7 @@ describe('Router', function () { }); url.should.equal('/programming/how%20to%20node'); done(); - + }); it('generates URL for given route name within embedded routers', function (done) { @@ -1852,6 +1852,61 @@ describe('Router', function () { }); } } + + it(`prefix and '/' route behavior`, function(done) { + var app = new Koa(); + var router = new Router({ + strict: false, + prefix: '/foo' + }); + + var strictRouter = new Router({ + strict: true, + prefix: '/bar' + }) + + router.get('/', function(ctx) { + ctx.body = ''; + }); + + strictRouter.get('/', function(ctx) { + ctx.body = ''; + }); + + app.use(router.routes()); + app.use(strictRouter.routes()); + + var server = http.createServer(app.callback()); + + request(server) + .get('/foo') + .expect(200) + .end(function (err) { + if (err) return done(err); + + request(server) + .get('/foo/') + .expect(200) + .end(function (err) { + if (err) return done(err); + + request(server) + .get('/bar') + .expect(404) + .end(function (err) { + if (err) return done(err); + + request(server) + .get('/bar/') + .expect(200) + .end(function (err) { + if (err) return done(err); + done(); + }); + }); + }); + }); + }) }); describe('Static Router#url()', function () {