diff --git a/.gitignore b/.gitignore index 3b5cec1..99e2399 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,5 @@ yarn-error.log* *.tsbuildinfo next-env.d.ts package-lock.json + +dist/ diff --git a/src/middleware.ts b/src/middleware.ts deleted file mode 100644 index 22223a1..0000000 --- a/src/middleware.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; - -// [@ant-design, icons] => @ant-design/icons -// [antd] => antd -function getPkgName(pathGroups: string[]) { - let [scope, name] = pathGroups; - if (!name) { - name = scope; - scope = ''; - } - - if (!name || name.startsWith('@')) { - return undefined; - } - - if (scope && !scope.startsWith('@')) { - return undefined; - } - - name = name.includes('@') ? name.split('@')[0] : name; - - return scope ? `${scope}/${name}` : name; -} - -// antd@^4 => ^4 -function getSpec(pathname: string, pkgName: string) { - const spec = pathname.replace(`/${pkgName}`, ''); - if (spec.startsWith('@')) { - return decodeURIComponent(spec.substring(1)); - } - return undefined; -} - -export default async function middleware(req: NextRequest) { - const { pathname } = req.nextUrl; - - const pathGroups = pathname.split("/").filter(Boolean); - - // logo - if (['/cnpm.png', '/favicon.ico', '/packages'].includes(pathname)) { - return NextResponse.next(); - } - - let pkgName = getPkgName(pathGroups); - if (pkgName) { - const spec = getSpec(pathname, pkgName); - const target = new URL(`/package/${pkgName}`, req.nextUrl); - if (spec) { - target.searchParams.set('version', spec); - } - return NextResponse.redirect(target); - } - - // /sync/antd/versions => package/antd/versions - const [action, ...rest] = pathGroups; - if (action === "sync") { - pkgName = getPkgName(rest); - if (pkgName) { - return NextResponse.redirect( - new URL(`/package/${pkgName}/versions`, req.nextUrl) - ); - } - } - - return NextResponse.next(); -} diff --git a/src/pages/api/info.ts b/src/pages/api/info.ts deleted file mode 100644 index caee9b9..0000000 --- a/src/pages/api/info.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { PackageManifest } from "@/hooks/useManifest"; -import { isEqual } from "lodash"; -import { NextApiRequest, NextApiResponse } from "next"; - -export default async function handler(req: NextApiRequest, res: NextApiResponse) { - - try { - - const { pkgName } = req.query; - - const [pkg, sourceRegistryInfo] = await Promise.all([ - fetch(`https://registry.npmmirror.com/${pkgName}`, { - cache: 'no-store', - keepalive: false, - }).then((res) => res.json()), - fetch(`https://registry.npmjs.org/${pkgName}`, { - cache: 'no-store', - keepalive: false, - headers: { - 'Accept': 'application/vnd.npm.install-v1+json', - }, - }).then( - (res) => res.json() - ), - ]); - - // dist-tag 一致,版本也一致,就认为不需要同步 - const alreadySync = - isEqual(pkg?.['dist-tags'], sourceRegistryInfo?.['dist-tags']) && - isEqual( - Object.keys(pkg?.versions || {}).sort(), - Object.keys(sourceRegistryInfo?.versions || {}).sort() - ); - - if (!pkg.name) { - res.status(404).json({}); - return; - } - - // 剪裁一下 pkg 的数据 - const { - versions, - maintainers = null, - repository = null, - } = pkg as PackageManifest; - const simpleVersions: Record> = {}; - - Object.entries(versions).forEach(([version, data]) => { - simpleVersions[version] = { - version: data.version, - dist: { - tarball: data?.dist?.tarball || '', - size: data?.dist?.size || '0', - }, - publish_time: data.publish_time || data._cnpmcore_publish_time || 0, - _npmUser: data._npmUser || null, - }; - }); - const data = { - name: pkg.name, - maintainers, - description: pkg.description, - repository, - 'dist-tags': pkg['dist-tags'], - versions: simpleVersions, - keywords: pkg.keywords, - homepage: pkg.homepage, - }; - - res.status(200).json({ data, needSync: !alreadySync }); - } catch (e) { - console.error(e); - res.status(500).json({ error: e }); - } -} diff --git a/src/pages/api/spec.ts b/src/pages/api/spec.ts deleted file mode 100644 index 8776ef1..0000000 --- a/src/pages/api/spec.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { NextApiRequest, NextApiResponse } from "next"; - -export default async function handler(req: NextApiRequest, res: NextApiResponse) { - - try { - - const { pkgName, spec } = req.query; - - const [pkg] = await Promise.all([ - fetch(`https://registry.npmmirror.com/${pkgName}/${spec}`, { - cache: 'no-store', - keepalive: false, - }).then((res) => res.json()), - ]); - - if (!pkg.name) { - res.status(404).json({}); - return; - } - - res.status(200).json({ version: pkg.version }); - } catch (e) { - console.error(e); - res.status(500).json({ error: e }); - } -} diff --git a/src/slugs/versions/index.tsx b/src/slugs/versions/index.tsx index 06f6ccd..ece8f00 100644 --- a/src/slugs/versions/index.tsx +++ b/src/slugs/versions/index.tsx @@ -96,7 +96,7 @@ function TagsList({ return (
  • - + {item} @@ -166,6 +166,7 @@ function VersionsList({