@@ -9,9 +9,10 @@ let log = debug('grabghr')
99const { chmod, unlink } = promises
1010const { arch, platform } = process
1111
12- /* c8 ignore next 5 */
13- function delay ( ) {
14- const delay = Math . floor ( ( 5 + 5 * Math . random ( ) ) * 1000 )
12+ /* c8 ignore next 6 */
13+ function delay ( seconds ) {
14+ if ( ! seconds ) seconds = Math . floor ( 5 + 5 * Math . random ( ) )
15+ const delay = seconds * 1000
1516 log ( 'wait %d ms before trying again' , delay )
1617 return new Promise ( resolve => setTimeout ( resolve , delay ) )
1718}
@@ -46,8 +47,32 @@ function fetchSafely(url, token, options = {}) {
4647 ...options
4748 }
4849 const res = await fetch ( url , options )
49- /* c8 ignore next 5 */
50+ /* c8 ignore next 29 */
5051 if ( ! res . ok ) {
52+ try {
53+ const response = await res . text ( )
54+ log ( '%s' , response )
55+ } catch { }
56+ if ( res . status === 403 || res . status === 429 ) {
57+ const {
58+ 'retry-after' : after ,
59+ 'x-ratelimit-limit' : limit ,
60+ 'x-ratelimit-remaining' : remaining ,
61+ 'x-ratelimit-used' : used ,
62+ 'x-ratelimit-reset' : reset ,
63+ 'x-ratelimit-resource' : resource
64+ } = res . headers
65+ log ( 'Retry first after: %s' , after )
66+ log ( 'The maximum number of requests that you can make per hour%s' , limit )
67+ log ( 'The number of requests remaining in the current rate limit window%s' , remaining )
68+ log ( 'The number of requests you have made in the current rate limit window%s' , used )
69+ log ( 'The time at which the current rate limit window resets, in UTC epoch seconds%s' , reset )
70+ log ( 'The rate limit resource that the request counted against%s' , resource )
71+ const wait = after || reset
72+ if ( wait ) {
73+ await delay ( wait )
74+ }
75+ }
5176 const err = new Error ( `GET "${ url } " failed: ${ res . status } ${ res . statusText } ` )
5277 err . response = res
5378 throw err
0 commit comments