@@ -108,8 +108,8 @@ function theEnd(val: number) {
108108
109109async function getNpmAndGitHubData ( pkg : string ) : Promise < PackageKey & PackageNpm & PackageGithub > {
110110 const [ npmInfo , npmDlInfo ] = await Promise . all ( [
111- fetch ( `https://registry.npmjs.org/${ pkg } ` ) . then ( ( r ) => r . json ( ) ) ,
112- fetch ( `https://api.npmjs.org/downloads/point/last-week/${ pkg } ` ) . then ( ( r ) => r . json ( ) )
111+ fetchJson ( `https://registry.npmjs.org/${ pkg } ` ) ,
112+ fetchJson ( `https://api.npmjs.org/downloads/point/last-week/${ pkg } ` )
113113 ] ) ;
114114 // delete npmInfo.readme;
115115 // delete npmInfo.versions;
@@ -148,12 +148,11 @@ async function getNpmAndGitHubData(pkg: string): Promise<PackageKey & PackageNpm
148148 if ( git_org && git_repo && ! skipGithubStars ) {
149149 const token = process . env . GITHUB_TOKEN ;
150150 const headers = token ? new Headers ( { authorization : 'Bearer ' + token } ) : { } ;
151- const res = await fetch ( `https://api.github.com/repos/${ git_org } /${ git_repo } ` , { headers } ) ;
152- const resJson = await res . json ( ) ;
153- if ( resJson . message && resJson . message . startsWith ( 'API rate limit exceeded' ) ) {
151+ const res = await fetchJson ( `https://api.github.com/repos/${ git_org } /${ git_repo } ` , { headers } ) ;
152+ if ( res . message && res . message . startsWith ( 'API rate limit exceeded' ) ) {
154153 skipGithubStars = true ;
155154 } else {
156- github_stars = resJson . stargazers_count ;
155+ github_stars = res . stargazers_count ;
157156 }
158157 }
159158
@@ -253,3 +252,18 @@ function writeJsonData(path: string, data: any) {
253252 fs . writeFileSync ( path , JSON . stringify ( sortedData , null , 2 ) ) ;
254253 execSync ( `prettier --write ${ path } ` ) ;
255254}
255+
256+ async function fetchJson ( url : string , options : RequestInit = { } ) : Promise < any > {
257+ const headers = new Headers ( { ...options . headers , 'User-Agent' : 'svelte.dev/packages_v0.0.1' } ) ;
258+
259+ for ( let i = 0 ; i < 5 ; i ++ ) {
260+ try {
261+ const res = await fetch ( url , { ...options , headers } ) ;
262+ return await res . json ( ) ;
263+ } catch ( e ) {
264+ console . error ( `Failed to fetch ${ url } after ${ i + 1 } retries` , e ) ;
265+ }
266+
267+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 * Math . pow ( 2 , i + 1 ) ) ) ;
268+ }
269+ }
0 commit comments