@@ -108,8 +108,8 @@ function theEnd(val: number) {
108
108
109
109
async function getNpmAndGitHubData ( pkg : string ) : Promise < PackageKey & PackageNpm & PackageGithub > {
110
110
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 } ` )
113
113
] ) ;
114
114
// delete npmInfo.readme;
115
115
// delete npmInfo.versions;
@@ -148,12 +148,11 @@ async function getNpmAndGitHubData(pkg: string): Promise<PackageKey & PackageNpm
148
148
if ( git_org && git_repo && ! skipGithubStars ) {
149
149
const token = process . env . GITHUB_TOKEN ;
150
150
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' ) ) {
154
153
skipGithubStars = true ;
155
154
} else {
156
- github_stars = resJson . stargazers_count ;
155
+ github_stars = res . stargazers_count ;
157
156
}
158
157
}
159
158
@@ -253,3 +252,18 @@ function writeJsonData(path: string, data: any) {
253
252
fs . writeFileSync ( path , JSON . stringify ( sortedData , null , 2 ) ) ;
254
253
execSync ( `prettier --write ${ path } ` ) ;
255
254
}
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