Skip to content

Commit

Permalink
fix: use path param with regex for npm package feed (#14626)
Browse files Browse the repository at this point in the history
* 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

---------
  • Loading branch information
ouuan authored Mar 3, 2024
1 parent 2c771aa commit cd6f2d9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
12 changes: 5 additions & 7 deletions lib/routes/npm/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { art } from '@/utils/render';
import * as path from 'node:path';

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

const packageVersion = packageVersionRes.time;
const packageVersionList = [];
for (const key in packageVersion) {
packageVersionList.push({
const packageVersionList = Object.keys(packageVersion)
.map((key) => ({
version: key,
time: packageVersion[key],
});
}
packageVersionList.reverse();
}))
.toReversed();

ctx.set('data', {
title: `${name} - npm`,
Expand Down
3 changes: 2 additions & 1 deletion lib/routes/npm/router.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export default (router) => {
router.get('package/*', './package');
// https://github.com/dword-design/package-name-regex/blob/master/src/index.js
router.get('package/:name{(@[a-z0-9-~][a-z0-9-._~]*\\/)?[a-z0-9-~][a-z0-9-._~]*}', './package');
};

0 comments on commit cd6f2d9

Please sign in to comment.