Skip to content

Commit b5b08ba

Browse files
authored
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 76020dd commit b5b08ba

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
@@ -165,14 +165,14 @@ class Router {
165165
}
166166
}
167167
} else {
168-
const keys = [];
169-
pathToRegexp(router.opts.prefix || '', keys);
168+
const { keys } = pathToRegexp(router.opts.prefix || '', router.opts);
170169
const routerPrefixHasParam = Boolean(
171170
router.opts.prefix && keys.length > 0
172171
);
173172
router.register(path || '([^/]*)', [], m, {
174173
end: false,
175-
ignoreCaptures: !hasPath && !routerPrefixHasParam
174+
ignoreCaptures: !hasPath && !routerPrefixHasParam,
175+
pathIsRegexp: true
176176
});
177177
}
178178
}
@@ -380,7 +380,7 @@ class Router {
380380
* @returns {Router}
381381
*/
382382
all(name, path, middleware) {
383-
if (typeof path === 'string') {
383+
if (typeof path === 'string' || path instanceof RegExp) {
384384
middleware = Array.prototype.slice.call(arguments, 2);
385385
} else {
386386
middleware = Array.prototype.slice.call(arguments, 1);
@@ -396,7 +396,12 @@ class Router {
396396
)
397397
throw new Error('You have to provide a path when adding an all handler');
398398

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

401406
return this;
402407
}
@@ -455,10 +460,10 @@ class Router {
455460
* @returns {Layer}
456461
* @private
457462
*/
458-
register(path, methods, middleware, opts = {}) {
463+
register(path, methods, middleware, newOpts = {}) {
459464
const router = this;
460465
const { stack } = this;
461-
466+
const opts = { ...this.opts, ...newOpts };
462467
// support array of paths
463468
if (Array.isArray(path)) {
464469
for (const curPath of path) {
@@ -472,12 +477,15 @@ class Router {
472477
const route = new Layer(path, methods, middleware, {
473478
end: opts.end === false ? opts.end : true,
474479
name: opts.name,
475-
sensitive: opts.sensitive || this.opts.sensitive || false,
476-
strict: opts.strict || this.opts.strict || false,
477-
prefix: opts.prefix || this.opts.prefix || '',
478-
ignoreCaptures: opts.ignoreCaptures
480+
sensitive: opts.sensitive || false,
481+
strict: opts.strict || false,
482+
prefix: opts.prefix || '',
483+
ignoreCaptures: opts.ignoreCaptures,
484+
pathIsRegexp: opts.pathIsRegexp,
485+
trailing: opts.trailing
479486
});
480487

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

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

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"dependencies": {
2424
"http-errors": "^2.0.0",
2525
"koa-compose": "^4.1.0",
26-
"path-to-regexp": "^6.3.0"
26+
"path-to-regexp": "^8.1.0"
2727
},
2828
"devDependencies": {
2929
"@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)