-
-
Notifications
You must be signed in to change notification settings - Fork 203
fix: getting tangled stats from our backend #981
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
Open
fatfingers23
wants to merge
3
commits into
npmx-dev:main
Choose a base branch
from
fatfingers23:bug/tangled-stats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+74
−30
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import type { CachedFetchFunction } from '#shared/utils/fetch-cache-config' | ||
|
|
||
| export default defineCachedEventHandler( | ||
| async event => { | ||
| let owner = getRouterParam(event, 'owner') | ||
| let repo = getRouterParam(event, 'repo') | ||
|
|
||
| let cachedFetch: CachedFetchFunction | ||
| if (event.context.cachedFetch) { | ||
| cachedFetch = event.context.cachedFetch | ||
| } else { | ||
| // Fallback: return a function that uses regular $fetch | ||
| // (shouldn't happen in normal operation) | ||
| cachedFetch = async <T = unknown>( | ||
| url: string, | ||
| options: Parameters<typeof $fetch>[1] = {}, | ||
| _ttl?: number, | ||
| ): Promise<CachedFetchResult<T>> => { | ||
| const data = (await $fetch<T>(url, options)) as T | ||
| return { data, isStale: false, cachedAt: null } | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| // Tangled doesn't have a public JSON API, but we can scrape the star count | ||
| // from the HTML page (it's in the hx-post URL as countHint=N) | ||
| const { data: html } = await cachedFetch<string>( | ||
| `https://tangled.org/${owner}/${repo}`, | ||
| { | ||
| headers: { 'User-Agent': 'npmx', 'Accept': 'text/html' }, | ||
| }, | ||
| CACHE_MAX_AGE_ONE_MINUTE * 10, | ||
| ) | ||
| // Extracts the at-uri used in atproto | ||
| const atUriMatch = html.match(/data-star-subject-at="([^"]+)"/) | ||
| // Extract star count from: hx-post="/star?subject=...&countHint=23" | ||
| const starMatch = html.match(/countHint=(\d+)/) | ||
| //We'll set the stars from tangled's repo page and may override it with constellation if successful | ||
| let stars = starMatch?.[1] ? parseInt(starMatch[1], 10) : 0 | ||
| let forks = 0 | ||
| const atUri = atUriMatch?.[1] | ||
|
|
||
| if (atUri) { | ||
| try { | ||
| const constellation = new Constellation(cachedFetch) | ||
| //Get counts of records that reference this repo in the atmosphere using constellation | ||
| const { data: allLinks } = await constellation.getAllLinks(atUri) | ||
| stars = allLinks.links['sh.tangled.feed.star']?.['.subject']?.distinct_dids ?? stars | ||
| forks = allLinks.links['sh.tangled.repo']?.['.source']?.distinct_dids ?? 0 | ||
| } catch { | ||
| //failing silently since this is just an enhancement to the information already showing | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| stars, | ||
| forks, | ||
| } | ||
| } catch { | ||
| return { | ||
| stars: 0, | ||
| forks: 0, | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| maxAge: CACHE_MAX_AGE_ONE_MINUTE * 10, | ||
| }, | ||
| ) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing imports will cause compilation and runtime errors.
The file uses
CachedFetchResult<T>(line 17) andConstellation(line 44) but neither is imported. This will cause TypeScript compilation errors and runtime failures.🐛 Proposed fix to add missing imports
📝 Committable suggestion