-
-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathuseOrgPackages.ts
More file actions
281 lines (243 loc) · 8.25 KB
/
useOrgPackages.ts
File metadata and controls
281 lines (243 loc) · 8.25 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import type { NpmSearchResponse, NpmSearchResult, PackageMetaResponse } from '#shared/types'
import { emptySearchResponse, metaToSearchResult } from './search-utils'
import { mapWithConcurrency } from '#shared/utils/async'
/** Number of packages to fetch metadata for in the initial load */
const INITIAL_BATCH_SIZE = 250
/** Max names per Algolia getObjects request */
const ALGOLIA_BATCH_SIZE = 1000
export interface OrgPackagesResponse extends NpmSearchResponse {
/** Total number of packages in the org (may exceed objects.length if not all loaded yet) */
totalPackages: number
/** Whether there are more packages that haven't been loaded yet */
isTruncated: boolean
}
function emptyOrgResponse(): OrgPackagesResponse {
return {
...emptySearchResponse(),
totalPackages: 0,
isTruncated: false,
}
}
/**
* Fetch packages for an npm organization with progressive loading.
*
* 1. Gets the authoritative package list from the npm registry (single request)
* 2. Fetches metadata for the first batch immediately
* 3. Remaining packages are loaded on-demand via `loadAll()`
*/
export function useOrgPackages(orgName: MaybeRefOrGetter<string>) {
const route = useRoute()
const { searchProvider } = useSearchProvider()
const searchProviderValue = computed(() => {
const p = normalizeSearchParam(route.query.p)
if (p === 'npm' || searchProvider.value === 'npm') return 'npm'
return 'algolia'
})
const { getPackagesByNameSlice } = useAlgoliaSearch()
// --- Progressive loading state ---
const cache = shallowRef<{
org: string
allNames: string[]
objects: NpmSearchResult[]
totalPackages: number
} | null>(null)
const isLoadingMore = shallowRef(false)
const hasMore = computed(() => {
if (!cache.value) return false
return cache.value.objects.length < cache.value.allNames.length
})
// Promise lock to prevent duplicate loadAll calls
let loadAllPromise: Promise<void> | null = null
const asyncData = useLazyAsyncData(
() => `org-packages:${searchProviderValue.value}:${toValue(orgName)}`,
async ({ ssrContext }, { signal }) => {
const org = toValue(orgName)
if (!org) {
return emptyOrgResponse()
}
// Get the authoritative package list from the npm registry
let packageNames: string[]
try {
const { packages } = await $fetch<{ packages: string[]; count: number }>(
`/api/registry/org/${encodeURIComponent(org)}/packages`,
{ signal },
)
packageNames = packages
} catch (err) {
if (err && typeof err === 'object' && 'statusCode' in err && err.statusCode === 404) {
const error = createError({
statusCode: 404,
statusMessage: 'Organization not found',
message: `The organization "@${org}" does not exist on npm`,
})
if (import.meta.server) {
ssrContext!.payload.error = error
}
throw error
}
packageNames = []
}
if (packageNames.length === 0) {
cache.value = { org, allNames: [], objects: [], totalPackages: 0 }
return emptyOrgResponse()
}
const totalPackages = packageNames.length
const initialNames = packageNames.slice(0, INITIAL_BATCH_SIZE)
// Fetch metadata for first batch
let initialObjects: NpmSearchResult[] = []
if (searchProviderValue.value === 'algolia') {
try {
initialObjects = await getPackagesByNameSlice(initialNames)
} catch {
// Fall through to npm fallback
}
}
// Staleness guard
if (toValue(orgName) !== org) return emptyOrgResponse()
// npm fallback for initial batch
if (initialObjects.length === 0) {
const metaResults = await mapWithConcurrency(
initialNames,
async name => {
try {
return await $fetch<PackageMetaResponse>(
`/api/registry/package-meta/${encodePackageName(name)}`,
{ signal },
)
} catch {
return null
}
},
10,
)
if (toValue(orgName) !== org) return emptyOrgResponse()
initialObjects = metaResults
.filter((meta): meta is PackageMetaResponse => meta !== null)
.map(metaToSearchResult)
}
cache.value = {
org,
allNames: packageNames,
objects: initialObjects,
totalPackages,
}
return {
isStale: false,
objects: initialObjects,
total: initialObjects.length,
totalPackages,
isTruncated: packageNames.length > initialObjects.length,
time: new Date().toISOString(),
} satisfies OrgPackagesResponse
},
{ default: emptyOrgResponse },
)
/** Load all remaining packages that weren't fetched in the initial batch */
async function loadAll(): Promise<void> {
if (!hasMore.value) return
// Reuse existing promise if already running
if (loadAllPromise) {
await loadAllPromise
return
}
loadAllPromise = _doLoadAll()
try {
await loadAllPromise
} finally {
loadAllPromise = null
}
}
async function _doLoadAll(): Promise<void> {
const currentCache = cache.value
if (!currentCache || currentCache.objects.length >= currentCache.allNames.length) return
const org = currentCache.org
isLoadingMore.value = true
try {
const remainingNames = currentCache.allNames.slice(currentCache.objects.length)
if (searchProviderValue.value === 'algolia') {
// Split remaining into batches and fetch in parallel
const batches: string[][] = []
for (let i = 0; i < remainingNames.length; i += ALGOLIA_BATCH_SIZE) {
batches.push(remainingNames.slice(i, i + ALGOLIA_BATCH_SIZE))
}
const results = await Promise.allSettled(
batches.map(batch => getPackagesByNameSlice(batch)),
)
if (toValue(orgName) !== org) return
const newObjects: NpmSearchResult[] = []
for (const result of results) {
if (result.status === 'fulfilled') {
newObjects.push(...result.value)
}
}
if (newObjects.length > 0) {
const existingNames = new Set(currentCache.objects.map(o => o.package.name))
const deduped = newObjects.filter(o => !existingNames.has(o.package.name))
cache.value = {
...currentCache,
objects: [...currentCache.objects, ...deduped],
}
}
} else {
// npm fallback: fetch with concurrency
const metaResults = await mapWithConcurrency(
remainingNames,
async name => {
try {
return await $fetch<PackageMetaResponse>(
`/api/registry/package-meta/${encodePackageName(name)}`,
)
} catch {
return null
}
},
10,
)
if (toValue(orgName) !== org) return
const newObjects = metaResults
.filter((meta): meta is PackageMetaResponse => meta !== null)
.map(metaToSearchResult)
if (newObjects.length > 0) {
const existingNames = new Set(currentCache.objects.map(o => o.package.name))
const deduped = newObjects.filter(o => !existingNames.has(o.package.name))
cache.value = {
...currentCache,
objects: [...currentCache.objects, ...deduped],
}
}
}
} finally {
isLoadingMore.value = false
}
}
// Reset cache when provider changes
watch(
() => searchProviderValue.value,
() => {
cache.value = null
loadAllPromise = null
},
)
// Computed data that prefers cache
const data = computed<OrgPackagesResponse | null>(() => {
const org = toValue(orgName)
if (cache.value && cache.value.org === org) {
return {
isStale: false,
objects: cache.value.objects,
total: cache.value.objects.length,
totalPackages: cache.value.totalPackages,
isTruncated: cache.value.objects.length < cache.value.allNames.length,
time: new Date().toISOString(),
}
}
return asyncData.data.value
})
return {
...asyncData,
data,
isLoadingMore,
hasMore,
loadAll,
}
}