Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
84 changes: 84 additions & 0 deletions app/components/PackageDependents.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<script setup lang="ts">
import { usePackageDependents } from '~/composables/useNpmRegistry'
import { formatCompactNumber } from '~/utils/formatters'

const props = defineProps<{
packageName: string
}>()

const { data, status } = usePackageDependents(() => props.packageName)

const dependents = computed(() => data.value?.objects ?? [])
const total = computed(() => data.value?.total ?? 0)

// Expanded state for showing all dependents
const expanded = ref(false)

// Show first 10 by default, all when expanded
const visibleDependents = computed(() => {
if (expanded.value) {
return dependents.value
}
return dependents.value.slice(0, 10)
})

// Show section only when we have dependents or are loading
const showSection = computed(() => {
return status.value === 'pending' || dependents.value.length > 0
})
</script>

<template>
<section v-if="showSection" aria-labelledby="dependents-heading">
<h2 id="dependents-heading" class="text-xs text-fg-subtle uppercase tracking-wider mb-3">
Dependents
<span v-if="status === 'success' && total > 0">({{ total.toLocaleString() }})</span>
</h2>

<!-- Loading state -->
<div v-if="status === 'pending'" class="space-y-2">
<div v-for="i in 5" :key="i" class="flex items-center justify-between py-1">
<div class="skeleton h-4 rounded" :style="{ width: `${60 + (i % 3) * 20}px` }" />
<div class="skeleton h-3 w-12 rounded" />
</div>
</div>

<!-- Dependents list -->
<ul
v-else-if="dependents.length > 0"
class="space-y-1 list-none m-0 p-0"
aria-label="Packages that depend on this package"
>
<li
v-for="dependent in visibleDependents"
:key="dependent.package.name"
class="flex items-center justify-between py-1 text-sm gap-2"
>
<NuxtLink
:to="{ name: 'package', params: { package: dependent.package.name.split('/') } }"
class="font-mono text-fg-muted hover:text-fg transition-colors duration-200 truncate min-w-0"
>
{{ dependent.package.name }}
</NuxtLink>
<span
v-if="dependent.downloads?.weekly"
class="font-mono text-xs text-fg-subtle shrink-0 flex items-center gap-1"
:title="`${dependent.downloads.weekly.toLocaleString()} weekly downloads`"
>
<span class="i-carbon-download w-3 h-3" aria-hidden="true" />
{{ formatCompactNumber(dependent.downloads.weekly, { decimals: 1 }) }}
</span>
</li>
</ul>

<!-- Expand button -->
<button
v-if="dependents.length > 10 && !expanded"
type="button"
class="mt-2 font-mono text-xs text-fg-muted hover:text-fg transition-colors duration-200 rounded focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50"
@click="expanded = true"
>
show top {{ dependents.length }}
</button>
</section>
</template>
44 changes: 44 additions & 0 deletions app/composables/useNpmRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,50 @@ export function getOutdatedTooltip(info: OutdatedDependencyInfo): string {
return `Patch update available (latest: ${info.latest})`
}

// ============================================================================
// Package Dependents
// ============================================================================

/**
* Fetch packages that depend on a given package (dependents).
* Results are sorted by weekly download count (most downloaded first)
* to help with security triage when vulnerabilities are discovered.
*/
export function usePackageDependents(
packageName: MaybeRefOrGetter<string>,
options: MaybeRefOrGetter<{ size?: number }> = {},
) {
return useLazyAsyncData(
() => `dependents:${toValue(packageName)}:${JSON.stringify(toValue(options))}`,
async () => {
const name = toValue(packageName)
if (!name) return emptySearchResponse

const { size = 50 } = toValue(options)

// Use the existing searchNpmPackages with depends-on: query
// This finds packages that have `name` as a dependency
const response = await searchNpmPackages(`depends-on:${name}`, { size })
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure this pulls in correct data:

https://www.npmjs.com/search?q=depends-on%3Anuxt

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

in e18e we have a whole other database to do this query because the npm one wasn't capable. so when i saw this, i was very surprised and hoped it was true so we could switch 😂

but im pretty sure the npm api doesn't do what you think here, or it does, but very poorly

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Darn, back to the drawing board! Thought the data was correct 🥲

Copy link
Copy Markdown

Choose a reason for hiding this comment

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


// Sort by weekly downloads (descending) for security triage
const sortedObjects = [...response.objects].sort((a, b) => {
const aDownloads = a.downloads?.weekly ?? 0
const bDownloads = b.downloads?.weekly ?? 0
return bDownloads - aDownloads
})

return {
...response,
objects: sortedObjects,
}
},
{
server: false,
default: () => emptySearchResponse,
},
)
}

/**
* Get CSS class for a dependency version based on outdated status
*/
Expand Down
3 changes: 3 additions & 0 deletions app/pages/[...package].vue
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,9 @@ defineOgImageComponent('Package', {
:peer-dependencies-meta="displayVersion?.peerDependenciesMeta"
:optional-dependencies="displayVersion?.optionalDependencies"
/>

<!-- Dependents (packages that depend on this one) -->
<PackageDependents :package-name="pkg.name" />
</div>
</div>
</article>
Expand Down