From 1296167385b38ca226f17c3a87ac2135d53c769b Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Wed, 19 Jul 2023 11:27:29 -0700 Subject: [PATCH 1/1] support escaping param indicator in route paths --- lib/router/index.js | 22 +++++++++++++++++++++- test/lib/router.js | 7 +++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/router/index.js b/lib/router/index.js index a13ac47..f0d2d94 100644 --- a/lib/router/index.js +++ b/lib/router/index.js @@ -61,17 +61,37 @@ class Router { _pathToRoutePath(rawPath) { const routePath = rawPath .split('/') - .map((p) => p.startsWith(this.paramPrefix) ? new PathParameter(p.slice(this.paramPrefix.length)) : p); + .map((p) => this._pathPartMunge(p)); + if (this.ignoreTrailingSlash && routePath[routePath.length - 1] === '') { routePath.pop(); } + routePath[kPathMethods] = {}; Object.freeze(routePath); return routePath; } + /* + * Convert a path part string to parameter if needed. + * Remove escape from an escaped parameter. + * @param {String} part + * @returns {PathParameter|String} + * @private + */ + _pathPartMunge(part) { + if (part.startsWith(this.paramPrefix)) { + return new PathParameter(part.slice(this.paramPrefix.length)); + } + if (part.startsWith('\\' + this.paramPrefix)) { + return part.slice(1); + } + return part; + } + + /** * Compare checkPath to fixedPath, no param substitution, params must match. * @param {Router~RoutePath} routePath diff --git a/test/lib/router.js b/test/lib/router.js index d639170..f9a2401 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -46,6 +46,13 @@ describe('Router', function () { const r = router._pathToRoutePath(p); assert.deepStrictEqual(r, expected); }); + it('defines a path with escaped parameter', function () { + const p = '/a/\\:b/c'; + const expected = ['', 'a', ':b', 'c']; + expected[Router.kPathMethods] = {}; + const r = router._pathToRoutePath(p); + assert.deepStrictEqual(r, expected); + }); }); // _pathToRoutePath describe('_pathCompareExact', function () { -- 2.45.2