Skip to content

Commit

Permalink
replace 'got' with 'node-fetch' (#182629)
Browse files Browse the repository at this point in the history
replace 'got' with 'node-fetch' (for #182624)
  • Loading branch information
aeschli committed May 17, 2023
1 parent 011ae39 commit f1258a5
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 174 deletions.
37 changes: 19 additions & 18 deletions build/lib/builtInExtensionsCG.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 17 additions & 16 deletions build/lib/builtInExtensionsCG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand Down
17 changes: 11 additions & 6 deletions build/lib/github.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions build/lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { Stream } from 'stream';
import got from 'got';
import fetch from 'node-fetch';
import { remote } from './gulpRemoteSource';
import * as through2 from 'through2';

Expand All @@ -30,14 +30,18 @@ export function assetFromGithub(repo: string, version: string, assetFilter: (nam
return remote(`/repos/${repo.replace(/^\/|\/$/g, '')}/releases/tags/v${version}`, {
base: 'https://api.github.com',
fetchOptions: { headers: ghApiHeaders }
}).pipe(through2.obj(function (file, _enc, callback) {
}).pipe(through2.obj(async function (file, _enc, callback) {
const asset = JSON.parse(file.contents.toString()).assets.find((a: { name: string }) => assetFilter(a.name));
if (!asset) {
return callback(new Error(`Could not find asset in release of ${repo} @ ${version}`));
}
const response = await fetch(asset.url, { headers: ghDownloadHeaders });
if (response.ok) {
file.contents = response.body.pipe(through2());
callback(null, file);
} else {
return callback(new Error(`Request ${response.url} failed with status code: ${response.status}`));
}

const res = got.stream(asset.url, { headers: ghDownloadHeaders, followRedirect: true });
file.contents = res.pipe(through2());
callback(null, file);
}));
}
1 change: 0 additions & 1 deletion build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"electron-osx-sign": "^0.4.16",
"esbuild": "0.17.14",
"extract-zip": "^2.0.1",
"got": "11.8.5",
"gulp-merge-json": "^2.1.1",
"gulp-shell": "^0.8.0",
"jsonc-parser": "^2.3.0",
Expand Down
Loading

0 comments on commit f1258a5

Please sign in to comment.