|
| 1 | +import * as v from 'valibot' |
| 2 | +import { PackageRouteParamsSchema } from '#shared/schemas/package' |
| 3 | +import { CACHE_MAX_AGE_ONE_HOUR } from '#shared/utils/constants' |
| 4 | + |
| 5 | +const E18E_LIVE_REGISTRY_URL = 'https://npm.devminer.xyz/live_registry' |
| 6 | +const E18E_REGISTRY_URL = 'https://npm.devminer.xyz/registry' |
| 7 | + |
| 8 | +interface DependentsViewResponse { |
| 9 | + total_rows: number |
| 10 | + offset: number |
| 11 | + rows: { |
| 12 | + id: string |
| 13 | + key: string |
| 14 | + value: { name: string; version: string } |
| 15 | + }[] |
| 16 | +} |
| 17 | + |
| 18 | +interface DownloadsViewResponse { |
| 19 | + total_rows: number |
| 20 | + offset: number |
| 21 | + rows: { |
| 22 | + id: string |
| 23 | + key: string |
| 24 | + value: number |
| 25 | + }[] |
| 26 | +} |
| 27 | + |
| 28 | +export interface DependentPackage { |
| 29 | + name: string |
| 30 | + downloads: number |
| 31 | + version?: string |
| 32 | +} |
| 33 | + |
| 34 | +export interface DependentsResponse { |
| 35 | + dependents: DependentPackage[] |
| 36 | + total: number |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * GET /api/registry/dependents/:name |
| 41 | + * |
| 42 | + * Fetch packages that depend on the given package using the e18e CouchDB mirror. |
| 43 | + * Uses CouchDB views for efficient lookups, then fetches download stats separately. |
| 44 | + * Results are sorted by download count (most downloaded first) for security triage. |
| 45 | + */ |
| 46 | +export default defineCachedEventHandler( |
| 47 | + async (event): Promise<DependentsResponse> => { |
| 48 | + const pkgParamSegments = getRouterParam(event, 'pkg')?.split('/') ?? [] |
| 49 | + const { rawPackageName } = parsePackageParams(pkgParamSegments) |
| 50 | + |
| 51 | + try { |
| 52 | + const { packageName } = v.parse(PackageRouteParamsSchema, { |
| 53 | + packageName: rawPackageName, |
| 54 | + }) |
| 55 | + |
| 56 | + const dependentsResponse = await $fetch<DependentsViewResponse>( |
| 57 | + `${E18E_LIVE_REGISTRY_URL}/_design/dependents/_view/dependents2?key=${encodeURIComponent(JSON.stringify(packageName))}&limit=250`, |
| 58 | + ) |
| 59 | + |
| 60 | + if (dependentsResponse.rows.length === 0) { |
| 61 | + return { dependents: [], total: 0 } |
| 62 | + } |
| 63 | + |
| 64 | + const packageNames = dependentsResponse.rows.map(row => row.value.name) |
| 65 | + |
| 66 | + const downloadsResponse = await $fetch<DownloadsViewResponse>( |
| 67 | + `${E18E_REGISTRY_URL}/_design/downloads/_view/downloads`, |
| 68 | + { |
| 69 | + method: 'POST', |
| 70 | + headers: { 'Content-Type': 'application/json' }, |
| 71 | + body: { keys: packageNames }, |
| 72 | + }, |
| 73 | + ) |
| 74 | + |
| 75 | + const downloadsMap = new Map<string, number>() |
| 76 | + for (const row of downloadsResponse.rows) { |
| 77 | + downloadsMap.set(row.key, row.value) |
| 78 | + } |
| 79 | + |
| 80 | + const versionMap = new Map<string, string>() |
| 81 | + for (const row of dependentsResponse.rows) { |
| 82 | + versionMap.set(row.value.name, row.value.version) |
| 83 | + } |
| 84 | + |
| 85 | + const dependents: DependentPackage[] = packageNames |
| 86 | + .map(name => ({ |
| 87 | + name, |
| 88 | + downloads: downloadsMap.get(name) ?? 0, |
| 89 | + version: versionMap.get(name), |
| 90 | + })) |
| 91 | + .sort((a, b) => b.downloads - a.downloads) |
| 92 | + |
| 93 | + return { |
| 94 | + dependents, |
| 95 | + total: dependents.length, |
| 96 | + } |
| 97 | + } catch { |
| 98 | + return { |
| 99 | + dependents: [], |
| 100 | + total: 0, |
| 101 | + } |
| 102 | + } |
| 103 | + }, |
| 104 | + { |
| 105 | + maxAge: CACHE_MAX_AGE_ONE_HOUR, |
| 106 | + swr: true, |
| 107 | + getKey: event => { |
| 108 | + const pkg = getRouterParam(event, 'pkg') ?? '' |
| 109 | + return `dependents:v2:${pkg.replace(/\/+$/, '').trim()}` |
| 110 | + }, |
| 111 | + }, |
| 112 | +) |
0 commit comments