Skip to content

Commit cd6f2d9

Browse files
authored
fix: use path param with regex for npm package feed (#14626)
* fix: use path param with regex for npm package feed 1. Don't include query params like `?code` in the package name. 2. Handle special cases when the org name of the package ends with `package`, i.e. the package name is something like `@*package/*`. * refactor: use map ---------
1 parent 2c771aa commit cd6f2d9

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

lib/routes/npm/package.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { art } from '@/utils/render';
33
import * as path from 'node:path';
44

55
export default async (ctx) => {
6-
const name = ctx.req.url.split('package/')[1];
6+
const name = ctx.req.param('name');
77
const packageDownloadLastMonthAPI = `https://api.npmjs.org/downloads/point/last-month/${name}`; // 按月统计
88
const packageDownloadLastWeekAPI = `https://api.npmjs.org/downloads/point/last-week/${name}`; // 按周统计
99
const packageDownloadLastDayAPI = `https://api.npmjs.org/downloads/point/last-day/${name}`; // 按天统计
@@ -15,14 +15,12 @@ export default async (ctx) => {
1515
const packageVersionRes = await got(packageVersionAPI).json();
1616

1717
const packageVersion = packageVersionRes.time;
18-
const packageVersionList = [];
19-
for (const key in packageVersion) {
20-
packageVersionList.push({
18+
const packageVersionList = Object.keys(packageVersion)
19+
.map((key) => ({
2120
version: key,
2221
time: packageVersion[key],
23-
});
24-
}
25-
packageVersionList.reverse();
22+
}))
23+
.toReversed();
2624

2725
ctx.set('data', {
2826
title: `${name} - npm`,

lib/routes/npm/router.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export default (router) => {
2-
router.get('package/*', './package');
2+
// https://github.com/dword-design/package-name-regex/blob/master/src/index.js
3+
router.get('package/:name{(@[a-z0-9-~][a-z0-9-._~]*\\/)?[a-z0-9-~][a-z0-9-._~]*}', './package');
34
};

0 commit comments

Comments
 (0)