replace 'got' with 'node-fetch' (#182629)

replace 'got' with 'node-fetch' (for #182624)
This commit is contained in:
Martin Aeschlimann
2023-05-17 06:20:31 +02:00
committed by GitHub
parent 011ae395be
commit f1258a5fbe
6 changed files with 56 additions and 174 deletions

View File

@@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import got from 'got';
import fetch from 'node-fetch';
import * as fs from 'fs';
import * as path from 'path';
import * as url from 'url';
@@ -25,23 +25,24 @@ async function downloadExtensionDetails(extension: IExtensionDefinition): Promis
const repository = url.parse(extension.repo).path!.substr(1);
const repositoryContentBaseUrl = `https://${token ? `${token}@` : ''}${contentBasePath}/${repository}/v${extension.version}`;
const promises = [];
for (const fileName of contentFileNames) {
promises.push(new Promise<{ fileName: string; body: Buffer | undefined | null }>(resolve => {
got(`${repositoryContentBaseUrl}/${fileName}`)
.then(response => {
resolve({ fileName, body: response.rawBody });
})
.catch(error => {
if (error.response.statusCode === 404) {
resolve({ fileName, body: undefined });
} else {
resolve({ fileName, body: null });
}
});
}));
async function getContent(fileName: string): Promise<{ fileName: string; body: Buffer | undefined | null }> {
try {
const response = await fetch(`${repositoryContentBaseUrl}/${fileName}`);
if (response.ok) {
return { fileName, body: await response.buffer() };
} else if (response.status === 404) {
return { fileName, body: undefined };
} else {
return { fileName, body: null };
}
} catch (e) {
return { fileName, body: null };
}
}
const promises = contentFileNames.map(getContent);
console.log(extensionLabel);
const results = await Promise.all(promises);
for (const result of results) {