66
77import type { ItemType , RegistryItem , RegistryManifestEntry } from "@hyperframes/core" ;
88import { fetchItemManifest , fetchRegistryManifest , DEFAULT_REGISTRY_URL } from "./remote.js" ;
9+ import { runWithConcurrencyLimit } from "../utils/concurrency.js" ;
910
1011export interface ResolveOptions {
1112 baseUrl ?: string ;
1213 /** Bypass the 24h manifest cache and fetch fresh data from the registry. */
1314 skipCache ?: boolean ;
1415 /**
15- * Called once per item that fails to load inside `loadAllItems`. Defaults
16- * to writing a diagnostic line to stderr. Pass a quieter implementation
17- * when rendering structured output (clack prompts, JSON, etc.).
16+ * Called once with a summary when items fail to load inside `loadAllItems`.
17+ * Defaults to writing a diagnostic line to stderr. Pass a quieter
18+ * implementation when rendering structured output (clack prompts, JSON, etc.).
1819 */
1920 onWarn ?: ( message : string ) => void ;
2021}
@@ -38,31 +39,39 @@ export async function listRegistryItems(
3839 return manifest . items . filter ( ( item ) => item . type === filter . type ) ;
3940}
4041
42+ // Hundreds of item manifests sharing one slow link all started their timeouts at once.
43+ const ITEM_FETCH_CONCURRENCY = 32 ;
44+
4145/**
42- * Load every item's full manifest in parallel. Used by the interactive init
43- * picker to populate titles/descriptions for all examples at once. Items that
44- * fail to load are skipped with a warning so one missing manifest doesn't
45- * break the picker.
46+ * Load every item's full manifest, a bounded number at a time. Items that fail
47+ * to load are skipped with one summary warning so one missing manifest doesn't
48+ * break the picker; after a timeout the rest come from cache only.
4649 */
4750export async function loadAllItems (
4851 entries : RegistryManifestEntry [ ] ,
4952 options : ResolveOptions = { } ,
5053) : Promise < RegistryItem [ ] > {
5154 const baseUrl = options . baseUrl ?? DEFAULT_REGISTRY_URL ;
5255 const warn = options . onWarn ?? defaultWarn ;
53- const results = await Promise . allSettled (
54- entries . map ( ( e ) => fetchItemManifest ( e . name , e . type , baseUrl ) ) ,
55- ) ;
56- const items : RegistryItem [ ] = [ ] ;
57- results . forEach ( ( r , i ) => {
58- if ( r . status === "fulfilled" ) {
59- items . push ( r . value ) ;
60- } else {
61- const name = entries [ i ] ?. name ?? "<unknown>" ;
62- warn ( `skipped item "${ name } ": ${ String ( r . reason ) } ` ) ;
56+ const skipped : string [ ] = [ ] ;
57+ let firstReason = "" ;
58+ let timedOut = false ;
59+ const loaded = await runWithConcurrencyLimit ( entries , ITEM_FETCH_CONCURRENCY , async ( e ) => {
60+ try {
61+ return await fetchItemManifest ( e . name , e . type , baseUrl , { cacheOnly : timedOut } ) ;
62+ } catch ( err ) {
63+ if ( err instanceof Error && err . name === "TimeoutError" ) timedOut = true ;
64+ skipped . push ( `"${ e . name } "` ) ;
65+ firstReason ||= String ( err ) ;
66+ return undefined ;
6367 }
6468 } ) ;
65- return items ;
69+ if ( skipped . length === 1 ) warn ( `skipped item ${ skipped [ 0 ] } : ${ firstReason } ` ) ;
70+ else if ( skipped . length > 1 )
71+ warn (
72+ `skipped ${ skipped . length } items (${ skipped . slice ( 0 , 3 ) . join ( ", " ) } ${ skipped . length > 3 ? ", ..." : "" } ): ${ firstReason } ` ,
73+ ) ;
74+ return loaded . filter ( ( item ) : item is RegistryItem => item !== undefined ) ;
6675}
6776
6877/**
0 commit comments