8000 chore: clean-up + update path-to-regex v8.2 + rename pathIsRegex to p… · koajs/router@4518d30 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4518d30

Browse files
committed
chore: clean-up + update path-to-regex v8.2 + rename pathIsRegex to pathIsRegEx
1 parent a006b5d commit 4518d30

File tree

5 files changed

+102
-92
lines changed

5 files changed

+102
-92
lines changed

bench/run.js

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
'use strict'
1+
'use strict';
22

3-
const { now, print, operations } = require('./util')
4-
const KoaRouter = require('../lib/router')
3+
const KoaRouter = require('../lib/router');
4+
const { now, print, operations } = require('./util');
55

6-
const router = new KoaRouter()
6+
const router = new KoaRouter();
77

88
const routes = [
99
{ method: 'GET', url: '/user' },
@@ -18,66 +18,73 @@ const routes = [
1818
{ method: 'GET', url: '/status' },
1919
{ method: 'GET', url: '/very/deeply/nested/route/hello/there' },
2020
{ method: 'GET', url: '/static/(.*)' }
21-
]
21+
];
2222

23-
function noop () {}
23+
function noop() {}
2424

25-
var i = 0
26-
var time = 0
25+
let i = 0;
26+
let time = 0;
2727

28-
routes.forEach(route => {
28+
for (const route of routes) {
2929
if (route.method === 'GET') {
30-
router.get(route.url, noop)
30+
router.get(route.url, noop);
3131
} else {
32-
router.post(route.url, noop)
32+
router.post(route.url, noop);
3333
}
34-
})
34+
}
3535

36-
time = now()
36+
time = now();
3737
for (i = 0; i < operations; i++) {
38-
router.match('/user', 'GET')
38+
router.match('/user', 'GET');
3939
}
40-
print('short static:', time)
4140

42-
time = now()
41+
print('short static:', time);
42+
43+
time = now();
4344
for (i = 0; i < operations; i++) {
44-
router.match('/user/comments', 'GET')
45+
router.match('/user/comments', 'GET');
4546
}
46-
print('static with same radix:', time)
4747

48-
time = now()
48+
print('static with same radix:', time);
49+
50+
time = now();
4951
for (i = 0; i < operations; i++) {
50-
router.match('/user/lookup/username/john', 'GET')
52+
router.match('/user/lookup/username/john', 'GET');
5153
}
52-
print('dynamic route:', time)
5354

54-
time = now()
55+
print('dynamic route:', time);
56+
57+
time = now();
5558
for (i = 0; i < operations; i++) {
56-
router.match('/event/abcd1234/comments', 'GET')
59+
router.match('/event/abcd1234/comments', 'GET');
5760
}
58-
print('mixed static dynamic:', time)
5961

60-
time = now()
62+
print('mixed static dynamic:', time);
63+
64+
time = now();
6165
for (i = 0; i < operations; i++) {
62-
router.match('/very/deeply/nested/route/hello/there', 'GET')
66+
router.match('/very/deeply/nested/route/hello/there', 'GET');
6367
}
64-
print('long static:', time)
6568

66-
time = now()
69+
print('long static:', time);
70+
71+
time = now();
6772
for (i = 0; i < operations; i++) {
68-
router.match('/static/index.html', 'GET')
73+
router.match('/static/index.html', 'GET');
6974
}
70-
print('wildcard:', time)
7175

72-
time = now()
76+
print('wildcard:', time);
77+
78+
time = now();
7379
for (i = 0; i < operations; i++) {
74-
router.match('/user', 'GET')
75-
router.match('/user/comments', 'GET')
76-
router.match('/user/lookup/username/john', 'GET')
77-
router.match('/event/abcd1234/comments', 'GET')
78-
router.match('/very/deeply/nested/route/hello/there', 'GET')
79-
router.match('/static/index.html', 'GET')
80+
router.match('/user', 'GET');
81+
router.match('/user/comments', 'GET');
82+
router.match('/user/lookup/username/john', 'GET');
83+
router.match('/event/abcd1234/comments', 'GET');
84+
router.match('/very/deeply/nested/route/hello/there', 'GET');
85+
router.match('/static/index.html', 'GET');
8086
}
81-
const output = print('all together:', time)
8287

83-
require('fs').writeFileSync('bench-result.txt', String(output))
88+
const output = print('all together:', time);
89+
90+
require('fs').writeFileSync('bench-result.txt', String(output));

bench/util.js

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,55 @@
1-
'use strict'
1+
'use strict';
22

3-
const chalk = require('chalk').default
3+
const process = require('node:process');
44

5-
const operations = 1000000
5+
const chalk = require('chalk').default;
66

7-
function now () {
8-
var ts = process.hrtime()
9-
return (ts[0] * 1e3) + (ts[1] / 1e6)
7+
const operations = 1000000;
8+
9+
function now() {
10+
const ts = process.hrtime();
11+
return ts[0] * 1e3 + ts[1] / 1e6;
1012
}
1113

12-
function getOpsSec (ms) {
13-
return Number(((operations * 1000) / ms).toFixed())
14+
function getOpsSec(ms) {
15+
return Number(((operations * 1000) / ms).toFixed(0));
1416
}
1517

16-
function print (name, time) {
17-
const opsSec = getOpsSec(now() - time)
18-
console.log(chalk.yellow(name), opsSec.toLocaleString(), 'ops/sec')
19-
return Number(opsSec)
18+
function print(name, time) {
19+
const opsSec = getOpsSec(now() - time);
20+
console.log(chalk.yellow(name), opsSec.toLocaleString(), 'ops/sec');
21+
return Number(opsSec);
2022
}
2123

22-
function title (name) {
23-
console.log(chalk.green(`
24+
function title(name) {
25+
console.log(
26+
chalk.green(`
2427
${'='.repeat(name.length + 2)}
2528
${name}
26-
${'='.repeat(name.length + 2)}`))
29+
${'='.repeat(name.length + 2)}`)
30+
);
2731
}
2832

29-
function Queue () {
30-
this.q = []
31-
this.running = false
33+
function Queue() {
34+
this.q = [];
35+
this.running = false;
3236
}
3337

34-
Queue.prototype.add = function add (job) {
35-
this.q.push(job)
36-
if (!this.running) this.run()
37-
}
38+
Queue.prototype.add = function add(job) {
39+
this.q.push(job);
40+
if (!this.running) this.run();
41+
};
3842

39-
Queue.prototype.run = function run () {
40-
this.running = true
41-
const job = this.q.shift()
43+
Queue.prototype.run = function run() {
44+
this.running = true;
45+
const job = this.q.shift();
4246
job(() => {
43-
if (this.q.length) {
44-
this.run()
47+
if (this.q.length > 0) {
48+
this.run();
4549
} else {
46-
this.running = false
50+
this.running = false;
4751
}
48-
})
49-
}
52+
});
53+
};
5054

51-
module.exports = { now, getOpsSec, print, title, Queue, operations }
55+
module.exports = { now, getOpsSec, print, title, Queue, operations };

lib/layer.js

Lines changed: 12 additions & 13 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, stringify } = require('path-to-regexp');
3+
const { pathToRegexp, compile, parse } = require('path-to-regexp');
44

55
module.exports = class Layer {
66
/**
@@ -13,22 +13,20 @@ module.exports = class Layer {
1313
* @param {String=} opts.name route name
1414
* @param {String=} opts.sensitive case sensitive (default: false)
1515
* @param {String=} opts.strict require the trailing slash (default: false)
16-
* @param {Boolean=} opts.pathIsRegexp if true, treat `path` as a regular expression
16+
* @param {Boolean=} opts.pathAsRegExp if true, treat `path` as a regular expression
1717
* @returns {Layer}
1818
* @private
1919
*/
2020
constructor(path, methods, middleware, opts = {}) {
2121
this.opts = opts;
2222
this.name = this.opts.name || null;
2323
this.methods = [];
24-
this.paramNames = [];
25-
this.stack = Array.isArray(middleware) ? middleware : [middleware];
26-
2724
for (const method of methods) {
2825
const l = this.methods.push(method.toUpperCase());
2926
if (this.methods[l - 1] === 'GET') this.methods.unshift('HEAD');
3027
}
3128

29+
this.stack = Array.isArray(middleware) ? middleware : [middleware];
3230
// ensure middleware is a function
3331
for (let i = 0; i < this.stack.length; i++) {
3432
const fn = this.stack[i];
@@ -42,17 +40,18 @@ module.exports = class Layer {
4240
}
4341

4442
this.path = path;
43+
this.paramNames = [];
4544

46-
if (this.opts.pathIsRegexp === true) {
45+
if (this.opts.pathAsRegExp === true) {
4746
this.regexp = new RegExp(path);
4847
} else if (this.path) {
49-
if (this.opts.strict === true) {
48+
if ('strict' in this.opts) {
5049
// path-to-regexp renamed strict to trailing in v8.1.0
51-
this.opts.trailing = false;
50+
this.opts.trailing = this.opts.strict !== true;
5251
}
5352

54-
const { regexp: regex, keys } = pathToRegexp(this.path, this.opts);
55-
this.regexp = regex;
53+
const { regexp, keys } = pathToRegexp(this.path, this.opts);
54+
this.regexp = regexp;
5655
this.paramNames = keys;
5756
}
5857
}
@@ -230,11 +229,11 @@ module.exports = class Layer {
230229
this.path !== '/' || this.opts.strict === true
231230
? `${prefix}${this.path}`
232231
: prefix;
233-
if (this.opts.pathIsRegexp === true || prefix instanceof RegExp) {
232+
if (this.opts.pathAsRegExp === true || prefix instanceof RegExp) {
234233
this.regexp = new RegExp(this.path);
235234
} else if (this.path) {
236-
const { regexp: regex, keys } = pathToRegexp(this.path, this.opts);
237-
this.regexp = regex;
235+
const { regexp, keys } = pathToRegexp(this.path, this.opts);
236+
this.regexp = regexp;
238237
this.paramNames = keys;
239238
}
240239
}

lib/router.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class Router {
171171
router.register(path || '([^/]*)', [], m, {
172172
end: false,
173173
ignoreCaptures: !hasPath && !routerPrefixHasParam,
174-
pathIsRegexp: true
174+
pathAsRegExp: true
175175
});
176176
}
177177
}
@@ -397,7 +397,7 @@ class Router {
397397

398398
const opts = {
399399
name,
400-
pathIsRegexp: path instanceof RegExp
400+
pathAsRegExp: path instanceof RegExp
401401
};
402402

403403
this.register(path, methods, middleware, { ...this.opts, ...opts });
@@ -480,7 +480,7 @@ class Router {
480480
strict: opts.strict || false,
481481
prefix: opts.prefix || '',
482482
ignoreCaptures: opts.ignoreCaptures,
483-
pathIsRegexp: opts.pathIsRegexp
483+
pathAsRegExp: opts.pathAsRegExp
484484
});
485485

486486
// if parent prefix exists, add prefix to new route
@@ -820,7 +820,7 @@ for (const method of methods) {
820820

821821
const opts = {
822822
name,
823-
pathIsRegexp: path instanceof RegExp
823+
pathAsRegExp: path instanceof RegExp
824824
};
825825

826826
// pass opts to register call on verb methods

package.json

Lines changed: 5 additions & 5 deletions
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": "^8.1.0"
27+
"path-to-regexp": "^8.2.0"
2828
},
2929
"devDependencies": {
3030
"@commitlint/cli": "^17.7.2",
@@ -34,11 +34,11 @@
3434
"eslint": "^8.39.0",
3535
"eslint-config-xo-lass": "^2.0.1",
3636
"fixpack": "^4.0.0",
37-
"husky": "^8.0.3",
37+
"husky": "^9.1.7",
3838
"jsdoc-to-markdown": "^8.0.0",
39-
"koa": "^2.15.3",
39+
"koa": "^3.0.1",
4040
"lint-staged": "^14.0.1",
41-
"mocha": "^10.7.3",
41+
"mocha": "^11.7.1",
4242
"nyc": "^17.0.0",
4343
"remark-cli": "11",
4444
"remark-preset-github": "^4.0.4",
@@ -75,4 +75,4 @@
7575
"test": "mocha test/**/*.js",
7676
"test:watch": "mocha test/**/*.js --watch"
7777
}
78-
}
78+
}

0 commit comments

Comments
 (0)
0