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