-
-
Notifications
You must be signed in to change notification settings - Fork 2
refactor: migrate to fast-npm-meta
#18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e93c92f
refactor: migrate to `fast-npm-meta`
9romise 792754a
Merge branch 'main' into fast-npm-meta
9romise ad5129c
chore: reuse `PackageInfo`
9romise 2d74988
update
9romise 97fe2f9
Merge branch 'main' into fast-npm-meta
9romise efd2196
fix: filter jsr packages (404 in npm registry)
9romise 24e476b
Merge branch 'main' into fast-npm-meta
9romise fbe12f6
fix: handle error
9romise 4d493d8
Merge branch 'main' into fast-npm-meta
9romise 10725ee
upgrade `fast-npm-meta`
9romise 2efb8af
fix: use correct version
9romise e354207
chore: update
9romise be44a6d
fix: handle undefined
9romise File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,43 @@ | ||
| import type { Packument, PackumentVersion } from '@npm/types' | ||
| import { NPM_REGISTRY } from '#constants' | ||
| import type { PackageVersionsInfoWithMetadata } from 'fast-npm-meta' | ||
| import { logger } from '#state' | ||
| import { encodePackageName } from '#utils/package' | ||
| import { ofetch } from 'ofetch' | ||
| import { getVersions } from 'fast-npm-meta' | ||
| import { memoize } from '../memoize' | ||
|
|
||
| interface ResolvedPackumentVersion extends Pick<PackumentVersion, 'version'> { | ||
| tag?: string | ||
| hasProvenance: boolean | ||
| deprecated?: string | ||
| export interface PackageInfo extends PackageVersionsInfoWithMetadata { | ||
| versionToTag: Map<string, string> | ||
| } | ||
|
|
||
| export interface ResolvedPackument { | ||
| versions: Record<string, ResolvedPackumentVersion> | ||
| } | ||
|
|
||
| export const getPackageInfo = memoize<string, Promise<ResolvedPackument>>(async (name) => { | ||
| /** | ||
| * Fetch npm package versions and build a version-to-tag lookup map. | ||
| * | ||
| * @see https://github.com/antfu/fast-npm-meta | ||
| */ | ||
| export const getPackageInfo = memoize<string, Promise<PackageInfo | null>>(async (name) => { | ||
| logger.info(`Fetching package info for ${name}`) | ||
| const encodedName = encodePackageName(name) | ||
|
|
||
| const pkg = await ofetch<Packument>(`${NPM_REGISTRY}/${encodedName}`) | ||
| logger.info(`Fetched package info for ${name}`) | ||
|
|
||
| const resolvedVersions = Object.fromEntries( | ||
| Object.keys(pkg.versions) | ||
| .filter((v) => pkg.time[v]) | ||
| .map<[string, ResolvedPackumentVersion]>((v) => [ | ||
| v, | ||
| { | ||
| version: v, | ||
| // @ts-expect-error present if published with provenance | ||
| hasProvenance: !!pkg.versions[v].dist.attestations, | ||
| deprecated: pkg.versions[v].deprecated, | ||
| }, | ||
| ]), | ||
| ) | ||
|
|
||
| Object.entries(pkg['dist-tags']).forEach(([tag, version]) => { | ||
| resolvedVersions[version].tag = tag | ||
| const pkg = await getVersions(name, { | ||
| metadata: true, | ||
| throw: false, | ||
| }) | ||
|
|
||
| return { | ||
| versions: resolvedVersions, | ||
| if ('error' in pkg) { | ||
| logger.warn(`Fetching package info for ${name} error: ${JSON.stringify(pkg)}`) | ||
|
|
||
| // Return null to trigger a cache hit | ||
| if (pkg.status === 404) | ||
| return null | ||
|
|
||
| throw pkg | ||
| } | ||
9romise marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| logger.info(`Fetched package info for ${name}`) | ||
|
|
||
| const versionToTag = new Map<string, string>() | ||
| if (pkg.distTags) { | ||
| for (const [tag, ver] of Object.entries(pkg.distTags)) { | ||
| versionToTag.set(ver, tag) | ||
| } | ||
| } | ||
|
|
||
| return { ...pkg, versionToTag } | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.