Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 5 additions & 30 deletions app/composables/useRepoMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,43 +566,18 @@ const tangledAdapter: ProviderAdapter = {
},

async fetchMeta(cachedFetch, ref, links, options = {}) {
// 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)
try {
const { data: html } = await cachedFetch<string>(
`https://tangled.org/${ref.owner}/${ref.repo}`,
{
headers: { 'User-Agent': 'npmx', 'Accept': 'text/html', ...options.headers },
...options,
},
const { data } = await cachedFetch<{ stars: number; forks: number }>(
`/api/atproto/tangled-stats/${ref.owner}/${ref.repo}`,
options,
REPO_META_TTL,
)
// 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 ?? stars
} catch {
//failing silently since this is just an enhancement to the information already showing
}
}

return {
provider: 'tangled',
url: links.repo,
stars,
forks,
stars: data.stars,
forks: data.forks,
links,
}
} catch {
Expand Down
69 changes: 69 additions & 0 deletions server/api/atproto/tangled-stats/[owner]/[...repo].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type { CachedFetchFunction } from '#shared/utils/fetch-cache-config'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Missing imports will cause compilation and runtime errors.

The file uses CachedFetchResult<T> (line 17) and Constellation (line 44) but neither is imported. This will cause TypeScript compilation errors and runtime failures.

🐛 Proposed fix to add missing imports
-import type { CachedFetchFunction } from '#shared/utils/fetch-cache-config'
+import type { CachedFetchFunction, CachedFetchResult } from '#shared/utils/fetch-cache-config'
+import { Constellation } from '#shared/utils/constellation'
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import type { CachedFetchFunction } from '#shared/utils/fetch-cache-config'
import type { CachedFetchFunction, CachedFetchResult } from '#shared/utils/fetch-cache-config'
import { Constellation } from '#shared/utils/constellation'


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,
},
)
Loading