Skip to content

Commit d5da6a3

Browse files
harryby11493imed-jaberi
authored andcommitted
Chore: Update path-to-regex to v8.1.0 (#189)
* chore: upgrade path-to-regex to v8.1.0 * chore: upgrade path-to-regex to v8.1.0 * chore: clean up test syntax * chore: clean up whitespace changes * chore: use String constructor instead of .toString for type conversion
1 parent 82d832b commit d5da6a3

File tree

4 files changed

+68
-34
lines changed

4 files changed

+68
-34
lines changed

lib/layer.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { parse: parseUrl, format: formatUrl } = require('node:url');
22

3-
const { pathToRegexp, compile, parse } = require('path-to-regexp');
3+
const { pathToRegexp, compile, parse, stringify } = require('path-to-regexp');
44

55
module.exports = class Layer {
66
/**
@@ -41,7 +41,14 @@ module.exports = class Layer {
4141
}
4242

4343
this.path = path;
44-
this.regexp = pathToRegexp(path, this.paramNames, this.opts);
44+
45+
if (this.opts.pathIsRegexp === true) {
46+
this.regexp = new RegExp(path);
47+
} else if (this.path) {
48+
const { regexp: regex, keys } = pathToRegexp(this.path, this.opts);
49+
this.regexp = regex;
50+
this.paramNames = keys;
51+
}
4552
}
4653

4754
/**
@@ -116,20 +123,25 @@ module.exports = class Layer {
116123

117124
const toPath = compile(url, { encode: encodeURIComponent, ...options });
118125
let replaced;
119-
120-
const tokens = parse(url);
126+
const { tokens } = parse(url);
121127
let replace = {};
122128

123129
if (Array.isArray(args)) {
124130
for (let len = tokens.length, i = 0, j = 0; i < len; i++) {
125-
if (tokens[i].name) replace[tokens[i].name] = args[j++];
131+
if (tokens[i].name) {
132+
replace[tokens[i].name] = args[j++];
133+
}
126134
}
127135
} else if (tokens.some((token) => token.name)) {
128136
replace = params;
129137
} else if (!options) {
130138
options = params;
131139
}
132140

141+
for (const [key, value] of Object.entries(replace)) {
142+
replace[key] = String(value);
143+
}
144+
133145
replaced = toPath(replace);
134146

135147
if (options && options.query) {
@@ -212,8 +224,13 @@ module.exports = class Layer {
212224
this.path !== '/' || this.opts.strict === true
213225
? `${prefix}${this.path}`
214226
: prefix;
215-
this.paramNames = [];
216-
this.regexp = pathToRegexp(this.path, this.paramNames, this.opts);
227+
if (this.opts.pathIsRegexp === true || prefix instanceof RegExp) {
228+
this.regexp = new RegExp(this.path);
229+
} else if (this.path) {
230+
const { regexp: regex, keys } = pathToRegexp(this.path, this.opts);
231+
this.regexp = regex;
232+
this.paramNames = keys;
233+
}
217234
}
218235

219236
return this;

lib/router.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,14 @@ class Router {
164164
}
165165
}
166166
} else {
167-
const keys = [];
168-
pathToRegexp(router.opts.prefix || '', keys);
167+
const { keys } = pathToRegexp(router.opts.prefix || '', router.opts);
169168
const routerPrefixHasParam = Boolean(
170169
router.opts.prefix && keys.length > 0
171170
);
172171
router.register(path || '([^/]*)', [], m, {
173172
end: false,
174-
ignoreCaptures: !hasPath && !routerPrefixHasParam
173+
ignoreCaptures: !hasPath && !routerPrefixHasParam,
174+
pathIsRegexp: true
175175
});
176176
}
177177
}
@@ -379,7 +379,7 @@ class Router {
379379
* @returns {Router}
380380
*/
381381
all(name, path, middleware) {
382-
if (typeof path === 'string') {
382+
if (typeof path === 'string' || path instanceof RegExp) {
383383
middleware = Array.prototype.slice.call(arguments, 2);
384384
} else {
385385
middleware = Array.prototype.slice.call(arguments, 1);
@@ -395,7 +395,12 @@ class Router {
395395
)
396396
throw new Error('You have to provide a path when adding an all handler');
397397

398-
this.register(path, methods, middleware, { name });
398+
const opts = {
399+
name,
400+
pathIsRegexp: path instanceof RegExp
401+
};
402+
403+
this.register(path, methods, middleware, { ...this.opts, ...opts });
399404

400405
return this;
401406
}
@@ -454,10 +459,10 @@ class Router {
454459
* @returns {Layer}
455460
* @private
456461
*/
457-
register(path, methods, middleware, opts = {}) {
462+
register(path, methods, middleware, newOpts = {}) {
458463
const router = this;
459464
const { stack } = this;
460-
465+
const opts = { ...this.opts, ...newOpts };
461466
// support array of paths
462467
if (Array.isArray(path)) {
463468
for (const curPath of path) {
@@ -471,12 +476,15 @@ class Router {
471476
const route = new Layer(path, methods, middleware, {
472477
end: opts.end === false ? opts.end : true,
473478
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+
pathIsRegexp: opts.pathIsRegexp,
484+
trailing: opts.trailing
478485
});
479486

487+
// if parent prefix exists, add prefix to new route
480488
if (this.opts.prefix) {
481489
route.setPrefix(this.opts.prefix);
482490
}
@@ -811,8 +819,13 @@ for (const method of methods) {
811819
`You have to provide a path when adding a ${method} handler`
812820
);
813821

814-
this.register(path, [method], middleware, { name });
822+
const opts = {
823+
name,
824+
pathIsRegexp: path instanceof RegExp
825+
};
815826

827+
// pass opts to register call on verb methods
828+
this.register(path, [method], middleware, { ...this.opts, ...opts });
816829
return this;
817830
};
818831
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"debug": "^4.4.1",
2525
"http-errors": "^2.0.0",
2626
"koa-compose": "^4.1.0",
27-
"path-to-regexp": "^6.3.0"
27+
"path-to-regexp": "^8.1.0"
2828
},
2929
"devDependencies": {
3030
"@commitlint/cli": "^17.7.2",

test/lib/router.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,13 @@ describe('Router', () => {
220220
const router = new Router();
221221

222222
router
223-
.get('user_page', '/user/(.*).jsx', (ctx) => {
223+
.get('user_page', '/user/{*any}.jsx', (ctx) => {
224224
ctx.body = { order: 1 };
225225
})
226-
.all('app', '/app/(.*).jsx', (ctx) => {
226+
.all('app', '/app/{*any}.jsx', (ctx) => {
227227
ctx.body = { order: 2 };
228228
})
229-
.all('view', '(.*).jsx', (ctx) => {
229+
.all('view', '{*any}.jsx', (ctx) => {
230230
ctx.body = { order: 3 };
231231
});
232232

@@ -244,7 +244,7 @@ describe('Router', () => {
244244
const router = new Router();
245245

246246
router
247-
.get('users_single', '/users/:id(.*)', (ctx, next) => {
247+
.get('users_single', '/users/:id{/*path}', (ctx, next) => {
248248
ctx.body = { single: true };
249249
next();
250250
})
@@ -268,10 +268,14 @@ describe('Router', () => {
268268
const router = new Router({ exclusive: true });
269269

270270
router
271-
.get('users_single', '/users/:id(.*)', (ctx, next) => {
272-
ctx.body = { single: true };
273-
next();
274-
})
271+
.get(
272+
'users_single',
273+
new RegExp('/users/:id(.*)'), // eslint-disable-line prefer-regex-literals
274+
(ctx, next) => {
275+
ctx.body = { single: true };
276+
next();
277+
}
278+
)
275279
.get('users_all', '/users/all', (ctx, next) => {
276280
ctx.body = { ...ctx.body, all: true };
277281
next();
@@ -293,7 +297,7 @@ describe('Router', () => {
293297

294298
router.get(
295299
'user_page',
296-
'/user/(.*).jsx',
300+
'/user/{*any}.jsx',
297301
() => {
298302
// no next()
299303
},
@@ -458,7 +462,7 @@ it('matches corresponding requests with optional route parameter', async () => {
458462
});
459463
const id = '10';
460464
const ext = '.json';
461-
router.get('/resources/:id{.:ext}?', (ctx) => {
465+
router.get('/resources/:id{.:ext}', (ctx) => {
462466
assert.strictEqual('params' in ctx, true);
463467
assert.strictEqual(ctx.params.id, id);
464468
if (ctx.params.ext) assert.strictEqual(ctx.params.ext, ext.slice(1));
@@ -1653,7 +1657,7 @@ describe('Router#opts', () => {
16531657
it('responds with 200', async () => {
16541658
const app = new Koa();
16551659
const router = new Router({
1656-
strict: true
1660+
trailing: false
16571661
});
16581662
router.get('/info', (ctx) => {
16591663
ctx.body = 'hello';
@@ -1685,7 +1689,7 @@ describe('Router#opts', () => {
16851689
it('responds with 404 when has a trailing slash', async () => {
16861690
const app = new Koa();
16871691
const router = new Router({
1688-
strict: true
1692+
trailing: false
16891693
});
16901694
router.get('/info', (ctx) => {
16911695
ctx.body = 'hello';
@@ -1700,7 +1704,7 @@ describe('use middleware with opts', () => {
17001704
it('responds with 200', async () => {
17011705
const app = new Koa();
17021706
const router = new Router({
1703-
strict: true
1707+
trailing: false
17041708
});
17051709
router.get('/info', (ctx) => {
17061710
ctx.body = 'hello';
@@ -1716,7 +1720,7 @@ describe('use middleware with opts', () => {
17161720
it('responds with 404 when has a trailing slash', async () => {
17171721
const app = new Koa();
17181722
const router = new Router({
1719-
strict: true
1723+
trailing: false
17201724
});
17211725
router.get('/info', (ctx) => {
17221726
ctx.body = 'hello';

0 commit comments

Comments
 (0)