Skip to content

Commit 5e2ee11

Browse files
committed
perf: reduce payload size
1 parent 8821e86 commit 5e2ee11

4 files changed

Lines changed: 32 additions & 41 deletions

File tree

app/components/Package/Versions.vue

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import type { PackageVersionInfo, PackumentVersion } from '#shared/types'
2+
import type { PackageVersionInfo, SlimVersion } from '#shared/types'
33
import { compare } from 'semver'
44
import type { RouteLocationRaw } from 'vue-router'
55
import { fetchAllPackageVersions } from '~/composables/useNpmRegistry'
@@ -14,7 +14,7 @@ import {
1414
1515
const props = defineProps<{
1616
packageName: string
17-
versions: Record<string, PackumentVersion>
17+
versions: Record<string, SlimVersion>
1818
distTags: Record<string, string>
1919
time: Record<string, string>
2020
}>()
@@ -31,13 +31,6 @@ interface VersionDisplay {
3131
deprecated?: string
3232
}
3333
34-
// Check if a version has provenance/attestations
35-
function hasProvenance(version: PackumentVersion | undefined): boolean {
36-
if (!version?.dist) return false
37-
const dist = version.dist as { attestations?: unknown }
38-
return !!dist.attestations
39-
}
40-
4134
// Build route object for package version link
4235
function versionRoute(version: string): RouteLocationRaw {
4336
return {
@@ -53,10 +46,7 @@ const versionToTags = computed(() => buildVersionToTagsMap(props.distTags))
5346
// Deduplicates so each version appears only once, with all its tags
5447
const allTagRows = computed(() => {
5548
// Group tags by version with their metadata
56-
const versionMap = new Map<
57-
string,
58-
{ tags: string[]; versionData: PackumentVersion | undefined }
59-
>()
49+
const versionMap = new Map<string, { tags: string[]; versionData: SlimVersion | undefined }>()
6050
for (const [tag, version] of Object.entries(props.distTags)) {
6151
const existing = versionMap.get(version)
6252
if (existing) {
@@ -88,7 +78,7 @@ const allTagRows = computed(() => {
8878
version,
8979
time: props.time[version],
9080
tags,
91-
hasProvenance: hasProvenance(versionData),
81+
hasProvenance: versionData?.hasProvenance,
9282
deprecated: versionData?.deprecated,
9383
} as VersionDisplay,
9484
}))

app/composables/useNpmRegistry.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type {
22
Packument,
3-
PackumentVersion,
43
SlimPackument,
54
NpmSearchResponse,
65
NpmSearchResult,
@@ -128,20 +127,28 @@ function transformPackument(pkg: Packument, requestedVersion?: string | null): S
128127
}
129128

130129
// Build filtered versions object with install scripts info per version
131-
const filteredVersions: Record<string, PackumentVersion> = {}
130+
const filteredVersions: Record<string, SlimVersion> = {}
131+
let versionData: SlimPackumentVersion | null = null
132132
for (const v of includedVersions) {
133133
const version = pkg.versions[v]
134134
if (version) {
135-
// Strip readme from each version, extract install scripts info
136-
const { readme: _readme, scripts, ...slimVersion } = version
137-
138-
// Extract install scripts info (which scripts exist + npx deps)
139-
const installScripts = scripts ? extractInstallScriptsInfo(scripts) : null
140-
135+
if (version.version === requestedVersion) {
136+
// Strip readme from each version, extract install scripts info
137+
const { readme: _readme, scripts, ...slimVersion } = version
138+
139+
// Extract install scripts info (which scripts exist + npx deps)
140+
const installScripts = scripts ? extractInstallScriptsInfo(scripts) : null
141+
versionData = {
142+
...slimVersion,
143+
installScripts: installScripts ?? undefined,
144+
}
145+
}
141146
filteredVersions[v] = {
142-
...slimVersion,
143-
installScripts: installScripts ?? undefined,
144-
} satisfies PackumentVersion
147+
...((version?.dist as { attestations?: unknown }) ? { hasProvenance: true } : {}),
148+
version: version.version,
149+
deprecated: version.deprecated,
150+
tags: version.tags as string[],
151+
}
145152
}
146153
}
147154

@@ -167,6 +174,7 @@ function transformPackument(pkg: Packument, requestedVersion?: string | null): S
167174
'keywords': pkg.keywords,
168175
'repository': pkg.repository,
169176
'bugs': pkg.bugs,
177+
'requestedVersion': versionData,
170178
'versions': filteredVersions,
171179
}
172180
}

app/pages/[...package].vue

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,9 @@ const { data: packageAnalysis } = usePackageAnalysis(packageName, requestedVersi
105105
const { data: moduleReplacement } = useModuleReplacement(packageName)
106106
107107
const { data: resolvedVersion } = await useResolvedVersion(packageName, requestedVersion)
108-
const { data: pkg, status, error } = usePackage(packageName, requestedVersion)
109-
110-
// Get the version to display (resolved version or latest)
111-
const displayVersion = computed(() => {
112-
if (!pkg.value) return null
113-
114-
// Use resolved version if available
115-
if (resolvedVersion.value) {
116-
return pkg.value.versions[resolvedVersion.value] ?? null
117-
}
118108
119-
// Fallback to latest
120-
const latestTag = pkg.value['dist-tags']?.latest
121-
if (!latestTag) return null
122-
return pkg.value.versions[latestTag] ?? null
123-
})
109+
const { data: pkg, status, error } = usePackage(packageName, requestedVersion)
110+
const displayVersion = computed(() => pkg.value?.requestedVersion ?? null)
124111
125112
// Process package description
126113
const pkgDescription = useMarkdown(() => ({

shared/types/npm-registry.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export type SlimPackumentVersion = PackumentVersion & {
3030
installScripts?: InstallScriptsInfo
3131
}
3232

33+
export type SlimVersion = Pick<SlimPackumentVersion, 'version' | 'deprecated' | 'tags'> & {
34+
hasProvenance?: true
35+
}
36+
3337
/**
3438
* Slimmed down Packument for client-side use.
3539
* Strips unnecessary fields to reduce payload size.
@@ -52,8 +56,10 @@ export interface SlimPackument {
5256
'keywords'?: string[]
5357
'repository'?: { type?: string; url?: string; directory?: string }
5458
'bugs'?: { url?: string; email?: string }
59+
/** current version */
60+
'requestedVersion': SlimPackumentVersion | null
5561
/** Only includes dist-tag versions (with installScripts info added per version) */
56-
'versions': Record<string, SlimPackumentVersion>
62+
'versions': Record<string, SlimVersion>
5763
}
5864

5965
/**

0 commit comments

Comments
 (0)