@@ -92,86 +92,107 @@ const nodeToSupporter = (node) => ({
9292} ) ;
9393
9494const getAllNodes = async ( graphqlQuery , getNodes ) => {
95- const body = {
96- query : graphqlQuery ,
97- variables : {
98- limit : graphqlPageSize ,
99- offset : 0 ,
100- dateFrom : new Date (
101- new Date ( ) . setFullYear ( new Date ( ) . getFullYear ( ) - 1 )
102- ) . toISOString ( ) , // data from last year
103- } ,
104- } ;
105-
106- let allNodes = [ ] ;
107-
108- let limit = 10 ,
109- remaining = 10 ,
110- reset ;
111- if ( process . env . OPENCOLLECTIVE_API_KEY ) {
112- limit = 100 ;
113- remaining = 100 ;
114- }
115- // Handling pagination if necessary
116-
117- while ( true ) {
118- if ( remaining === 0 ) {
119- console . log ( `Rate limit exceeded. Sleeping until ${ new Date ( reset ) } .` ) ;
120- await new Promise ( ( resolve ) =>
121- setTimeout ( resolve , reset - Date . now ( ) + 100 )
122- ) ;
95+ // Store original value
96+ const originalTlsRejectUnauthorized =
97+ process . env . NODE_TLS_REJECT_UNAUTHORIZED ;
98+
99+ try {
100+ // Only disable SSL verification in local development
101+ if ( process . env . CI !== 'true' ) {
102+ process . env . NODE_TLS_REJECT_UNAUTHORIZED = '0' ;
103+ console . log ( 'Running locally - SSL verification disabled' ) ;
123104 }
124- const result = await fetch ( graphqlEndpoint , {
125- method : 'POST' ,
126- body : JSON . stringify ( body ) ,
127- headers : {
128- 'Content-Type' : 'application/json' ,
105+
106+ const body = {
107+ query : graphqlQuery ,
108+ variables : {
109+ limit : graphqlPageSize ,
110+ offset : 0 ,
111+ dateFrom : new Date (
112+ new Date ( ) . setFullYear ( new Date ( ) . getFullYear ( ) - 1 )
113+ ) . toISOString ( ) , // data from last year
129114 } ,
130- } ) . then ( async ( response ) => {
131- if ( response . headers . get ( 'content-type' ) . includes ( 'json' ) ) {
132- const json = await response . json ( ) ;
133- console . log ( 'json' , json ) ;
134- if ( json . error ) {
135- // when rate limit exceeded, api won't return headers data like x-ratelimit-limit, etc.
136- remaining = 0 ;
137- reset = Date . now ( ) + 1000 * 60 ; // 1 minute
138- } else {
139- limit = response . headers . get ( 'x-ratelimit-limit' ) * 1 ;
140- remaining = response . headers . get ( 'x-ratelimit-remaining' ) * 1 ;
141- reset = response . headers . get ( 'x-ratelimit-reset' ) * 1000 ;
142- console . log (
143- `Rate limit: ${ remaining } /${ limit } remaining. Reset in ${ new Date (
144- reset
145- ) } `
146- ) ;
147- }
148- return json ;
149- } else {
150- // utilities/fetch-supporters: SyntaxError: Unexpected token < in JSON at position 0
151- console . log ( 'something wrong when fetching supporters' ) ;
152- return {
153- error : {
154- message : await response . text ( ) ,
155- } ,
156- } ;
115+ } ;
116+
117+ let allNodes = [ ] ;
118+
119+ let limit = 10 ,
120+ remaining = 10 ,
121+ reset ;
122+ if ( process . env . OPENCOLLECTIVE_API_KEY ) {
123+ limit = 100 ;
124+ remaining = 100 ;
125+ }
126+ // Handling pagination if necessary
127+
128+ while ( true ) {
129+ if ( remaining === 0 ) {
130+ console . log ( `Rate limit exceeded. Sleeping until ${ new Date ( reset ) } .` ) ;
131+ await new Promise ( ( resolve ) =>
132+ setTimeout ( resolve , reset - Date . now ( ) + 100 )
133+ ) ;
157134 }
158- } ) ;
159- // when rate limit exceeded, api will return {error: {message: ''}}
160- // but we could hopefully avoid rate limit by sleeping in the beginning of the loop
161- // however, when there're multiple task running simultaneously, it's still possible to hit the rate limit
162- if ( result . error ) {
163- console . log ( 'error' , result . error ) ;
164- // let the loop continue
165- } else {
166- const nodes = getNodes ( result . data ) ;
167- allNodes = [ ...allNodes , ...nodes ] ;
168- body . variables . offset += graphqlPageSize ;
169- if ( nodes . length < graphqlPageSize ) {
170- return allNodes ;
135+ const fetchOptions = {
136+ method : 'POST' ,
137+ body : JSON . stringify ( body ) ,
138+ headers : {
139+ 'Content-Type' : 'application/json' ,
140+ } ,
141+ } ;
142+
143+ const result = await fetch ( graphqlEndpoint , fetchOptions ) . then (
144+ async ( response ) => {
145+ if ( response . headers . get ( 'content-type' ) . includes ( 'json' ) ) {
146+ const json = await response . json ( ) ;
147+ console . log ( 'json' , json ) ;
148+ if ( json . error ) {
149+ // when rate limit exceeded, api won't return headers data like x-ratelimit-limit, etc.
150+ remaining = 0 ;
151+ reset = Date . now ( ) + 1000 * 60 ; // 1 minute
152+ } else {
153+ limit = response . headers . get ( 'x-ratelimit-limit' ) * 1 ;
154+ remaining = response . headers . get ( 'x-ratelimit-remaining' ) * 1 ;
155+ reset = response . headers . get ( 'x-ratelimit-reset' ) * 1000 ;
156+ console . log (
157+ `Rate limit: ${ remaining } /${ limit } remaining. Reset in ${ new Date (
158+ reset
159+ ) } `
160+ ) ;
161+ }
162+ return json ;
163+ } else {
164+ // utilities/fetch-supporters: SyntaxError: Unexpected token < in JSON at position 0
165+ console . log ( 'something wrong when fetching supporters' ) ;
166+ return {
167+ error : {
168+ message : await response . text ( ) ,
169+ } ,
170+ } ;
171+ }
172+ }
173+ ) ;
174+ // when rate limit exceeded, api will return {error: {message: ''}}
175+ // but we could hopefully avoid rate limit by sleeping in the beginning of the loop
176+ // however, when there're multiple task running simultaneously, it's still possible to hit the rate limit
177+ if ( result . error ) {
178+ console . log ( 'error' , result . error ) ;
179+ // let the loop continue
171180 } else {
172- // more nodes to fetch
181+ const nodes = getNodes ( result . data ) ;
182+ allNodes = [ ...allNodes , ...nodes ] ;
183+ body . variables . offset += graphqlPageSize ;
184+ if ( nodes . length < graphqlPageSize ) {
185+ return allNodes ;
186+ } else {
187+ // more nodes to fetch
188+ }
173189 }
174190 }
191+ } finally {
192+ // Only restore if we modified it
193+ if ( process . env . CI !== 'true' ) {
194+ process . env . NODE_TLS_REJECT_UNAUTHORIZED = originalTlsRejectUnauthorized ;
195+ }
175196 }
176197} ;
177198
0 commit comments