Skip to content
Merged
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
16 changes: 4 additions & 12 deletions app/composables/useCachedFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@ export function useCachedFetch(): CachedFetchFunction {
if (import.meta.client) {
return async <T = unknown>(
url: string,
options: {
method?: string
body?: unknown
headers?: Record<string, string>
} = {},
options: Parameters<typeof $fetch>[1] = {},
_ttl?: number,
): Promise<CachedFetchResult<T>> => {
const data = (await $fetch(url, options as Parameters<typeof $fetch>[1])) as T
const data = (await $fetch<T>(url, options)) as T
return { data, isStale: false, cachedAt: null }
}
}
Expand All @@ -59,14 +55,10 @@ export function useCachedFetch(): CachedFetchFunction {
// (shouldn't happen in normal operation)
return async <T = unknown>(
url: string,
options: {
method?: string
body?: unknown
headers?: Record<string, string>
} = {},
options: Parameters<typeof $fetch>[1] = {},
_ttl?: number,
): Promise<CachedFetchResult<T>> => {
const data = (await $fetch(url, options as Parameters<typeof $fetch>[1])) as T
const data = (await $fetch<T>(url, options)) as T
return { data, isStale: false, cachedAt: null }
}
}
17 changes: 11 additions & 6 deletions app/composables/useNpmRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ export function usePackage(

const asyncData = useLazyAsyncData(
() => `package:${toValue(name)}:${toValue(requestedVersion) ?? ''}`,
async () => {
async (_nuxtApp, { signal }) => {
const encodedName = encodePackageName(toValue(name))
const { data: r, isStale } = await cachedFetch<Packument>(`${NPM_REGISTRY}/${encodedName}`)
const { data: r, isStale } = await cachedFetch<Packument>(`${NPM_REGISTRY}/${encodedName}`, {
signal,
})
const reqVer = toValue(requestedVersion)
const pkg = transformPackument(r, reqVer)
const resolvedVersion = getResolvedVersion(pkg, reqVer)
Expand Down Expand Up @@ -233,10 +235,11 @@ export function usePackageDownloads(

const asyncData = useLazyAsyncData(
() => `downloads:${toValue(name)}:${toValue(period)}`,
async () => {
async (_nuxtApp, { signal }) => {
const encodedName = encodePackageName(toValue(name))
const { data, isStale } = await cachedFetch<NpmDownloadCount>(
`${NPM_API}/downloads/point/${toValue(period)}/${encodedName}`,
{ signal },
)
return { ...data, isStale }
},
Expand Down Expand Up @@ -306,7 +309,7 @@ export function useNpmSearch(

const asyncData = useLazyAsyncData(
() => `search:incremental:${toValue(query)}`,
async () => {
async (_nuxtApp, { signal }) => {
const q = toValue(query)
if (!q.trim()) {
return emptySearchResponse
Expand All @@ -325,7 +328,7 @@ export function useNpmSearch(

const { data: response, isStale } = await cachedFetch<NpmSearchResponse>(
`${NPM_REGISTRY}/-/v1/search?${params.toString()}`,
{},
{ signal },
60,
)

Expand Down Expand Up @@ -509,7 +512,7 @@ export function useOrgPackages(orgName: MaybeRefOrGetter<string>) {

const asyncData = useLazyAsyncData(
() => `org-packages:${toValue(orgName)}`,
async () => {
async (_nuxtApp, { signal }) => {
const org = toValue(orgName)
if (!org) {
return emptySearchResponse
Expand All @@ -520,6 +523,7 @@ export function useOrgPackages(orgName: MaybeRefOrGetter<string>) {
try {
const { data } = await cachedFetch<Record<string, string>>(
`${NPM_REGISTRY}/-/org/${encodeURIComponent(org)}/package`,
{ signal },
)
packageNames = Object.keys(data)
} catch (err) {
Expand Down Expand Up @@ -553,6 +557,7 @@ export function useOrgPackages(orgName: MaybeRefOrGetter<string>) {
const encoded = encodePackageName(name)
const { data: pkg } = await cachedFetch<MinimalPackument>(
`${NPM_REGISTRY}/${encoded}`,
{ signal },
)
return pkg
} catch {
Expand Down
44 changes: 24 additions & 20 deletions app/composables/useRepoMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type ProviderAdapter = {
cachedFetch: CachedFetchFunction,
ref: RepoRef,
links: RepoMetaLinks,
options?: Parameters<typeof $fetch>[1],
): Promise<RepoMeta | null>
}

Expand Down Expand Up @@ -126,13 +127,13 @@ const githubAdapter: ProviderAdapter = {
}
},

async fetchMeta(cachedFetch, ref, links) {
async fetchMeta(cachedFetch, ref, links, options = {}) {
// Using UNGH to avoid API limitations of the Github API
let res: UnghRepoResponse | null = null
try {
const { data } = await cachedFetch<UnghRepoResponse>(
`https://ungh.cc/repos/${ref.owner}/${ref.repo}`,
{ headers: { 'User-Agent': 'npmx' } },
{ headers: { 'User-Agent': 'npmx', ...options.headers }, ...options },
REPO_META_TTL,
)
res = data
Expand Down Expand Up @@ -191,14 +192,14 @@ const gitlabAdapter: ProviderAdapter = {
}
},

async fetchMeta(cachedFetch, ref, links) {
async fetchMeta(cachedFetch, ref, links, options = {}) {
const baseHost = ref.host ?? 'gitlab.com'
const projectPath = encodeURIComponent(`${ref.owner}/${ref.repo}`)
let res: GitLabProjectResponse | null = null
try {
const { data } = await cachedFetch<GitLabProjectResponse>(
`https://${baseHost}/api/v4/projects/${projectPath}`,
{ headers: { 'User-Agent': 'npmx' } },
{ headers: { 'User-Agent': 'npmx', ...options.headers }, ...options },
REPO_META_TTL,
)
res = data
Expand Down Expand Up @@ -249,12 +250,12 @@ const bitbucketAdapter: ProviderAdapter = {
}
},

async fetchMeta(cachedFetch, ref, links) {
async fetchMeta(cachedFetch, ref, links, options = {}) {
let res: BitbucketRepoResponse | null = null
try {
const { data } = await cachedFetch<BitbucketRepoResponse>(
`https://api.bitbucket.org/2.0/repositories/${ref.owner}/${ref.repo}`,
{ headers: { 'User-Agent': 'npmx' } },
{ headers: { 'User-Agent': 'npmx', ...options.headers }, ...options },
REPO_META_TTL,
)
res = data
Expand Down Expand Up @@ -307,12 +308,12 @@ const codebergAdapter: ProviderAdapter = {
}
},

async fetchMeta(cachedFetch, ref, links) {
async fetchMeta(cachedFetch, ref, links, options = {}) {
let res: GiteaRepoResponse | null = null
try {
const { data } = await cachedFetch<GiteaRepoResponse>(
`https://codeberg.org/api/v1/repos/${ref.owner}/${ref.repo}`,
{ headers: { 'User-Agent': 'npmx' } },
{ headers: { 'User-Agent': 'npmx', ...options.headers }, ...options },
REPO_META_TTL,
)
res = data
Expand Down Expand Up @@ -365,12 +366,12 @@ const giteeAdapter: ProviderAdapter = {
}
},

async fetchMeta(cachedFetch, ref, links) {
async fetchMeta(cachedFetch, ref, links, options = {}) {
let res: GiteeRepoResponse | null = null
try {
const { data } = await cachedFetch<GiteeRepoResponse>(
`https://gitee.com/api/v5/repos/${ref.owner}/${ref.repo}`,
{ headers: { 'User-Agent': 'npmx' } },
{ headers: { 'User-Agent': 'npmx', ...options.headers }, ...options },
REPO_META_TTL,
)
res = data
Expand Down Expand Up @@ -452,7 +453,7 @@ const giteaAdapter: ProviderAdapter = {
}
},

async fetchMeta(cachedFetch, ref, links) {
async fetchMeta(cachedFetch, ref, links, options = {}) {
if (!ref.host) return null

// Note: Generic Gitea instances may not be in the allowlist,
Expand All @@ -461,7 +462,7 @@ const giteaAdapter: ProviderAdapter = {
try {
const { data } = await cachedFetch<GiteaRepoResponse>(
`https://${ref.host}/api/v1/repos/${ref.owner}/${ref.repo}`,
{ headers: { 'User-Agent': 'npmx' } },
{ headers: { 'User-Agent': 'npmx', ...options.headers }, ...options },
REPO_META_TTL,
)
res = data
Expand Down Expand Up @@ -564,13 +565,16 @@ const tangledAdapter: ProviderAdapter = {
}
},

async fetchMeta(cachedFetch, ref, links) {
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' } },
{
headers: { 'User-Agent': 'npmx', 'Accept': 'text/html', ...options.headers },
...options,
},
REPO_META_TTL,
)
// Extracts the at-uri used in atproto
Expand Down Expand Up @@ -640,12 +644,12 @@ const radicleAdapter: ProviderAdapter = {
}
},

async fetchMeta(cachedFetch, ref, links) {
async fetchMeta(cachedFetch, ref, links, options = {}) {
let res: RadicleProjectResponse | null = null
try {
const { data } = await cachedFetch<RadicleProjectResponse>(
`https://seed.radicle.at/api/v1/projects/${ref.repo}`,
{ headers: { 'User-Agent': 'npmx' } },
{ headers: { 'User-Agent': 'npmx', ...options.headers }, ...options },
REPO_META_TTL,
)
res = data
Expand Down Expand Up @@ -704,14 +708,14 @@ const forgejoAdapter: ProviderAdapter = {
}
},

async fetchMeta(cachedFetch, ref, links) {
async fetchMeta(cachedFetch, ref, links, options = {}) {
if (!ref.host) return null

let res: GiteaRepoResponse | null = null
try {
const { data } = await cachedFetch<GiteaRepoResponse>(
`https://${ref.host}/api/v1/repos/${ref.owner}/${ref.repo}`,
{ headers: { 'User-Agent': 'npmx' } },
{ headers: { 'User-Agent': 'npmx', ...options.headers }, ...options },
REPO_META_TTL,
)
res = data
Expand Down Expand Up @@ -766,15 +770,15 @@ export function useRepoMeta(repositoryUrl: MaybeRefOrGetter<string | null | unde
repoRef.value
? `repo-meta:${repoRef.value.provider}:${repoRef.value.owner}/${repoRef.value.repo}`
: 'repo-meta:none',
async () => {
async (_nuxtApp, { signal }) => {
const ref = repoRef.value
if (!ref) return null

const adapter = providers.find(provider => provider.id === ref.provider)
if (!adapter) return null

const links = adapter.links(ref)
return await adapter.fetchMeta(cachedFetch, ref, links)
return await adapter.fetchMeta(cachedFetch, ref, links, { signal })
},
)

Expand Down
8 changes: 2 additions & 6 deletions server/plugins/fetch-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,12 @@ export default defineNitroPlugin(nitroApp => {
function createCachedFetch(event: H3Event): CachedFetchFunction {
return async <T = unknown>(
url: string,
options: {
method?: string
body?: unknown
headers?: Record<string, string>
} = {},
options: Parameters<typeof $fetch>[1] = {},
ttl: number = FETCH_CACHE_DEFAULT_TTL,
): Promise<CachedFetchResult<T>> => {
// Check if this URL should be cached
if (!isAllowedDomain(url)) {
const data = (await $fetch(url, options as Parameters<typeof $fetch>[1])) as T
const data = (await $fetch(url, options)) as T
return { data, isStale: false, cachedAt: null }
}

Expand Down
6 changes: 1 addition & 5 deletions shared/utils/fetch-cache-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ export interface CachedFetchResult<T> {
*/
export type CachedFetchFunction = <T = unknown>(
url: string,
options?: {
method?: string
body?: unknown
headers?: Record<string, string>
},
options?: Parameters<typeof $fetch>[1],
ttl?: number,
) => Promise<CachedFetchResult<T>>
Loading