|
| 1 | +export type ScoreCategory = 'documentation' | 'maintenance' | 'types' | 'bestPractices' | 'security' |
| 2 | + |
| 3 | +export interface ScoreCheck { |
| 4 | + id: string |
| 5 | + category: ScoreCategory |
| 6 | + points: number |
| 7 | + maxPoints: number |
| 8 | +} |
| 9 | + |
| 10 | +export interface PackageScore { |
| 11 | + percentage: number |
| 12 | + totalPoints: number |
| 13 | + maxPoints: number |
| 14 | + checks: ScoreCheck[] |
| 15 | +} |
| 16 | + |
| 17 | +interface ScoreInput { |
| 18 | + pkg: SlimPackument | null | undefined |
| 19 | + resolvedVersion: string | null | undefined |
| 20 | + readmeHtml: string |
| 21 | + analysis: PackageAnalysisResponse | null | undefined |
| 22 | + vulnCounts: { total: number; critical: number; high: number } | null | undefined |
| 23 | + vulnStatus: string |
| 24 | + hasProvenance: boolean |
| 25 | +} |
| 26 | + |
| 27 | +function computeScore(input: ScoreInput): PackageScore { |
| 28 | + const { pkg, resolvedVersion, readmeHtml, analysis, vulnCounts, vulnStatus, hasProvenance } = |
| 29 | + input |
| 30 | + const checks: ScoreCheck[] = [] |
| 31 | + |
| 32 | + // --- Documentation (3 pts) --- |
| 33 | + // README: 0 = none, 1 = exists but short, 2 = substantial (> 500 chars of content) |
| 34 | + const hasReadme = !!readmeHtml |
| 35 | + const readmePoints = !hasReadme ? 0 : readmeHtml.length > 500 ? 2 : 1 |
| 36 | + checks.push({ id: 'has-readme', category: 'documentation', points: readmePoints, maxPoints: 2 }) |
| 37 | + |
| 38 | + const hasDescription = !!pkg?.description?.trim() |
| 39 | + checks.push({ |
| 40 | + id: 'has-description', |
| 41 | + category: 'documentation', |
| 42 | + points: hasDescription ? 1 : 0, |
| 43 | + maxPoints: 1, |
| 44 | + }) |
| 45 | + |
| 46 | + // --- Maintenance (2 pts) --- |
| 47 | + // 0 = older than 2 years, 1 = within 2 years, 2 = within 1 year |
| 48 | + const publishTime = resolvedVersion && pkg?.time?.[resolvedVersion] |
| 49 | + const msSincePublish = publishTime ? Date.now() - new Date(publishTime).getTime() : null |
| 50 | + const oneYear = 365 * 24 * 60 * 60 * 1000 |
| 51 | + const twoYears = 2 * oneYear |
| 52 | + |
| 53 | + const maintenancePoints = |
| 54 | + msSincePublish !== null && msSincePublish < oneYear |
| 55 | + ? 2 |
| 56 | + : msSincePublish !== null && msSincePublish < twoYears |
| 57 | + ? 1 |
| 58 | + : 0 |
| 59 | + checks.push({ |
| 60 | + id: 'update-frequency', |
| 61 | + category: 'maintenance', |
| 62 | + points: maintenancePoints, |
| 63 | + maxPoints: 2, |
| 64 | + }) |
| 65 | + |
| 66 | + // --- Types (2 pts) --- |
| 67 | + // 0 = none, 1 = @types available, 2 = bundled in package |
| 68 | + const typesKind = analysis?.types?.kind |
| 69 | + const typesPoints = typesKind === 'included' ? 2 : typesKind === '@types' ? 1 : 0 |
| 70 | + checks.push({ id: 'has-types', category: 'types', points: typesPoints, maxPoints: 2 }) |
| 71 | + |
| 72 | + // --- Best Practices (4 pts, each 1) --- |
| 73 | + checks.push({ |
| 74 | + id: 'has-license', |
| 75 | + category: 'bestPractices', |
| 76 | + points: pkg?.license ? 1 : 0, |
| 77 | + maxPoints: 1, |
| 78 | + }) |
| 79 | + checks.push({ |
| 80 | + id: 'has-repository', |
| 81 | + category: 'bestPractices', |
| 82 | + points: pkg?.repository ? 1 : 0, |
| 83 | + maxPoints: 1, |
| 84 | + }) |
| 85 | + checks.push({ |
| 86 | + id: 'has-provenance', |
| 87 | + category: 'bestPractices', |
| 88 | + points: hasProvenance ? 1 : 0, |
| 89 | + maxPoints: 1, |
| 90 | + }) |
| 91 | + |
| 92 | + const hasEsm = analysis?.moduleFormat === 'esm' || analysis?.moduleFormat === 'dual' |
| 93 | + checks.push({ id: 'has-esm', category: 'bestPractices', points: hasEsm ? 1 : 0, maxPoints: 1 }) |
| 94 | + |
| 95 | + // --- Security (3 pts) --- |
| 96 | + // Vulnerabilities are excluded from the score until loaded to avoid score jumps. |
| 97 | + // 0 = has critical/high, 1 = only moderate/low, 2 = none |
| 98 | + const vulnsLoaded = vulnStatus === 'success' |
| 99 | + if (vulnsLoaded) { |
| 100 | + const vulnPoints = |
| 101 | + !vulnCounts || vulnCounts.total === 0 |
| 102 | + ? 2 |
| 103 | + : vulnCounts.critical === 0 && vulnCounts.high === 0 |
| 104 | + ? 1 |
| 105 | + : 0 |
| 106 | + checks.push({ |
| 107 | + id: 'no-vulnerabilities', |
| 108 | + category: 'security', |
| 109 | + points: vulnPoints, |
| 110 | + maxPoints: 2, |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + const latestTag = pkg?.['dist-tags']?.latest |
| 115 | + const latestVersion = latestTag ? pkg?.versions[latestTag] : null |
| 116 | + const isNotDeprecated = !latestVersion?.deprecated |
| 117 | + checks.push({ |
| 118 | + id: 'not-deprecated', |
| 119 | + category: 'security', |
| 120 | + points: isNotDeprecated ? 1 : 0, |
| 121 | + maxPoints: 1, |
| 122 | + }) |
| 123 | + |
| 124 | + const totalPoints = checks.reduce((sum, c) => sum + c.points, 0) |
| 125 | + const maxPoints = checks.reduce((sum, c) => sum + c.maxPoints, 0) |
| 126 | + const percentage = maxPoints > 0 ? Math.round((totalPoints / maxPoints) * 100) : 0 |
| 127 | + |
| 128 | + return { percentage, totalPoints, maxPoints, checks } |
| 129 | +} |
| 130 | + |
| 131 | +type MaybeRefLike<T> = Ref<T> | ComputedRef<T> |
| 132 | + |
| 133 | +export function usePackageScore(input: { |
| 134 | + pkg: MaybeRefLike<SlimPackument | null | undefined> |
| 135 | + resolvedVersion: MaybeRefLike<string | null | undefined> |
| 136 | + readmeHtml: MaybeRefLike<string> |
| 137 | + analysis: MaybeRefLike<PackageAnalysisResponse | null | undefined> |
| 138 | + vulnCounts: MaybeRefLike<{ total: number; critical: number; high: number } | null | undefined> |
| 139 | + vulnStatus: MaybeRefLike<string> |
| 140 | + hasProvenance: MaybeRefLike<boolean> |
| 141 | +}) { |
| 142 | + return computed<PackageScore>(() => |
| 143 | + computeScore({ |
| 144 | + pkg: input.pkg.value, |
| 145 | + resolvedVersion: input.resolvedVersion.value, |
| 146 | + readmeHtml: input.readmeHtml.value, |
| 147 | + analysis: input.analysis.value, |
| 148 | + vulnCounts: input.vulnCounts.value, |
| 149 | + vulnStatus: input.vulnStatus.value, |
| 150 | + hasProvenance: input.hasProvenance.value, |
| 151 | + }), |
| 152 | + ) |
| 153 | +} |
0 commit comments