|
6 | 6 |
|
7 | 7 | import{PackageVersion}from'./packageVersion'; |
8 | 8 | import*asutilfrom'./common'; |
9 | | -import*astmpfrom'tmp'; |
10 | 9 | import{PlatformInformation}from'./platform'; |
11 | 10 | import{OutgoingHttpHeaders}from'http'; |
12 | 11 |
|
@@ -177,47 +176,74 @@ function getTargetBuild(builds: Build[], updateChannel: string): Build { |
177 | 176 | returnneedsUpdate(userVersion,targetVersion) ?targetBuild :undefined; |
178 | 177 | } |
179 | 178 |
|
| 179 | +interfaceRate{ |
| 180 | +remaining:number; |
| 181 | +} |
| 182 | + |
| 183 | +interfaceRateLimit{ |
| 184 | +rate:Rate; |
| 185 | +} |
| 186 | + |
| 187 | +functionisRate(input:any):input isRate{ |
| 188 | +returninput&&input.remaining&&util.isNumber(input.remaining); |
| 189 | +} |
| 190 | + |
| 191 | +functionisRateLimit(input:any):input isRateLimit{ |
| 192 | +returninput&&isRate(input.rate); |
| 193 | +} |
| 194 | + |
| 195 | +asyncfunctiongetRateLimit():Promise<RateLimit>{ |
| 196 | +constheader:OutgoingHttpHeaders={'User-Agent':'vscode-cpptools'}; |
| 197 | +constdata:string=awaitutil.downloadFileToStr('https://api.github.com/rate_limit',header) |
| 198 | +.catch(()=>{thrownewError('Failed to download rate limit JSON');}); |
| 199 | + |
| 200 | +letrateLimit:any; |
| 201 | +try{ |
| 202 | +rateLimit=JSON.parse(data); |
| 203 | +}catch(error){ |
| 204 | +thrownewError('Failed to parse rate limit JSON'); |
| 205 | +} |
| 206 | + |
| 207 | +if(isRateLimit(rateLimit)){ |
| 208 | +returnPromise.resolve(rateLimit); |
| 209 | +}else{ |
| 210 | +thrownewError('Rate limit JSON is not of type RateLimit'); |
| 211 | +} |
| 212 | +} |
| 213 | + |
| 214 | +asyncfunctionrateLimitExceeded():Promise<boolean>{ |
| 215 | +constrateLimit:RateLimit=awaitgetRateLimit(); |
| 216 | +returnrateLimit.rate.remaining<=0; |
| 217 | +} |
| 218 | + |
180 | 219 | /** |
181 | 220 | * Download and parse the release list JSON from the GitHub API into a Build[]. |
182 | 221 | *@return Information about the released builds of the C/C++ extension. |
183 | 222 | */ |
184 | 223 | asyncfunctiongetReleaseJson():Promise<Build[]>{ |
185 | | -returnnewPromise<Build[]>((resolve,reject)=>{ |
186 | | -// Create temp file to hold JSON |
187 | | -tmp.file(async(err,releaseJsonPath,fd,cleanupCallback)=>{ |
188 | | -if(err){ |
189 | | -reject(newError('Failed to create release json file')); |
190 | | -return; |
191 | | -} |
| 224 | +if(awaitrateLimitExceeded()){ |
| 225 | +thrownewError('Failed to stay within GitHub API rate limit'); |
| 226 | +} |
192 | 227 |
|
193 | | -try{ |
194 | | -// Download release JSON |
195 | | -constreleaseUrl:string='https://api.github.com/repos/Microsoft/vscode-cpptools/releases'; |
196 | | -constheader:OutgoingHttpHeaders={'User-Agent':'vscode-cpptools'}; |
197 | | -awaitutil.downloadFileToDestination(releaseUrl,releaseJsonPath,header) |
198 | | -.catch(()=>{thrownewError('Failed to download release JSON');}); |
199 | | - |
200 | | -// Read the release JSON file |
201 | | -constfileContent:string=awaitutil.readFileText(releaseJsonPath) |
202 | | -.catch(()=>{thrownewError('Failed to read release JSON file');}); |
203 | | - |
204 | | -// Parse the file |
205 | | -letreleaseJson:any; |
206 | | -try{ |
207 | | -releaseJson=JSON.parse(fileContent); |
208 | | -}catch(error){ |
209 | | -thrownewError('Failed to parse release JSON'); |
210 | | -} |
| 228 | +// Download release JSON |
| 229 | +constreleaseUrl:string='https://api.github.com/repos/Microsoft/vscode-cpptools/releases'; |
| 230 | +constheader:OutgoingHttpHeaders={'User-Agent':'vscode-cpptools'}; |
211 | 231 |
|
212 | | -// Type check |
213 | | -if(isArrayOfBuilds(releaseJson)){ |
214 | | -resolve(releaseJson); |
215 | | -}else{ |
216 | | -reject(releaseJson); |
217 | | -} |
218 | | -}catch(error){ |
219 | | -reject(error); |
220 | | -} |
221 | | -}); |
222 | | -}); |
| 232 | +constdata:string=awaitutil.downloadFileToStr(releaseUrl,header) |
| 233 | +.catch(()=>{thrownewError('Failed to download release JSON');}); |
| 234 | + |
| 235 | +// Parse the file |
| 236 | +letreleaseJson:any; |
| 237 | +try{ |
| 238 | +releaseJson=JSON.parse(data); |
| 239 | +}catch(error){ |
| 240 | +thrownewError('Failed to parse release JSON'); |
| 241 | +} |
| 242 | + |
| 243 | +// Type check |
| 244 | +if(isArrayOfBuilds(releaseJson)){ |
| 245 | +returnreleaseJson; |
| 246 | +}else{ |
| 247 | +thrownewError('Release JSON is not of type Build[]'); |
| 248 | +} |
223 | 249 | } |