|
| 1 | +import { compare, prerelease, valid } from 'semver' |
| 2 | +import type { InstallSizeResult, SlimPackument } from '#shared/types' |
| 3 | + |
| 4 | +export interface InstallSizeDiff { |
| 5 | + comparisonVersion: string |
| 6 | + sizeRatio: number |
| 7 | + sizeIncrease: number |
| 8 | + currentSize: number |
| 9 | + previousSize: number |
| 10 | + depDiff: number |
| 11 | + currentDeps: number |
| 12 | + previousDeps: number |
| 13 | + sizeThresholdExceeded: boolean |
| 14 | + depThresholdExceeded: boolean |
| 15 | +} |
| 16 | + |
| 17 | +const SIZE_INCREASE_THRESHOLD = 0.25 |
| 18 | +const DEP_INCREASE_THRESHOLD = 5 |
| 19 | + |
| 20 | +function getComparisonVersion(pkg: SlimPackument, resolvedVersion: string): string | null { |
| 21 | + const isCurrentPrerelease = prerelease(resolvedVersion) !== null |
| 22 | + |
| 23 | + if (isCurrentPrerelease) { |
| 24 | + const latest = pkg['dist-tags']?.latest |
| 25 | + if (!latest || latest === resolvedVersion) return null |
| 26 | + return latest |
| 27 | + } |
| 28 | + |
| 29 | + // Find the previous version in time that was stable |
| 30 | + const stableVersions = Object.keys(pkg.time) |
| 31 | + .filter(v => v !== 'modified' && v !== 'created' && valid(v) !== null && prerelease(v) === null) |
| 32 | + .sort((a, b) => compare(a, b)) |
| 33 | + |
| 34 | + const currentIdx = stableVersions.indexOf(resolvedVersion) |
| 35 | + if (currentIdx <= 0) return null |
| 36 | + |
| 37 | + return stableVersions[currentIdx - 1]! |
| 38 | +} |
| 39 | + |
| 40 | +export function useInstallSizeDiff( |
| 41 | + packageName: MaybeRefOrGetter<string>, |
| 42 | + resolvedVersion: MaybeRefOrGetter<string | null | undefined>, |
| 43 | + pkg: MaybeRefOrGetter<SlimPackument | null | undefined>, |
| 44 | + currentInstallSize: MaybeRefOrGetter<InstallSizeResult | null | undefined>, |
| 45 | +) { |
| 46 | + const comparisonVersion = computed<string | null>(() => { |
| 47 | + const pkgVal = toValue(pkg) |
| 48 | + const version = toValue(resolvedVersion) |
| 49 | + if (!pkgVal || !version) return null |
| 50 | + return getComparisonVersion(pkgVal, version) |
| 51 | + }) |
| 52 | + |
| 53 | + const { |
| 54 | + data: comparisonInstallSize, |
| 55 | + status: comparisonStatus, |
| 56 | + execute: fetchComparisonSize, |
| 57 | + } = useLazyFetch<InstallSizeResult | null>( |
| 58 | + () => { |
| 59 | + const v = comparisonVersion.value |
| 60 | + if (!v) return '' |
| 61 | + return `/api/registry/install-size/${toValue(packageName)}/v/${v}` |
| 62 | + }, |
| 63 | + { |
| 64 | + server: false, |
| 65 | + immediate: false, |
| 66 | + default: () => null, |
| 67 | + }, |
| 68 | + ) |
| 69 | + |
| 70 | + if (import.meta.client) { |
| 71 | + watch( |
| 72 | + comparisonVersion, |
| 73 | + v => { |
| 74 | + if (v) fetchComparisonSize() |
| 75 | + }, |
| 76 | + { immediate: true }, |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + const diff = computed<InstallSizeDiff | null>(() => { |
| 81 | + const current = toValue(currentInstallSize) |
| 82 | + const previous = comparisonInstallSize.value |
| 83 | + const cv = comparisonVersion.value |
| 84 | + |
| 85 | + if (!current || !previous || !cv) return null |
| 86 | + |
| 87 | + const sizeRatio = |
| 88 | + previous.totalSize > 0 ? (current.totalSize - previous.totalSize) / previous.totalSize : 0 |
| 89 | + const depDiff = current.dependencyCount - previous.dependencyCount |
| 90 | + |
| 91 | + const sizeThresholdExceeded = sizeRatio > SIZE_INCREASE_THRESHOLD |
| 92 | + const depThresholdExceeded = depDiff > DEP_INCREASE_THRESHOLD |
| 93 | + |
| 94 | + if (!sizeThresholdExceeded && !depThresholdExceeded) return null |
| 95 | + |
| 96 | + return { |
| 97 | + comparisonVersion: cv, |
| 98 | + sizeRatio, |
| 99 | + sizeIncrease: current.totalSize - previous.totalSize, |
| 100 | + currentSize: current.totalSize, |
| 101 | + previousSize: previous.totalSize, |
| 102 | + depDiff, |
| 103 | + currentDeps: current.dependencyCount, |
| 104 | + previousDeps: previous.dependencyCount, |
| 105 | + sizeThresholdExceeded, |
| 106 | + depThresholdExceeded, |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + return { diff, comparisonVersion, comparisonStatus } |
| 111 | +} |
0 commit comments