refactor: remove all usages of the legacy request module (#30492)

* Replaces request with got
* Replaces nugget with got streams
* Replaces request in docs with got
* Upgrades dugite to drop requests dependency
This commit is contained in:
Samuel Attard 2021-08-12 10:34:49 -07:00 коммит произвёл GitHub
Родитель 08ff1c2cbf
Коммит 439e83de6c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 238 добавлений и 664 удалений

Просмотреть файл

@ -86,8 +86,8 @@ available from next tick of the process.
const { session } = require('electron')
session.defaultSession.on('will-download', (event, item, webContents) => {
event.preventDefault()
require('request')(item.getURL(), (data) => {
require('fs').writeFileSync('/somewhere', data)
require('got')(item.getURL()).then((response) => {
require('fs').writeFileSync('/somewhere', response.body)
})
})
```

Просмотреть файл

@ -35,7 +35,7 @@
"check-for-leaks": "^1.2.1",
"colors": "^1.4.0",
"dotenv-safe": "^4.0.4",
"dugite": "^1.45.0",
"dugite": "^1.103.0",
"eslint": "^7.4.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.22.0",
@ -54,12 +54,10 @@
"markdownlint": "^0.21.1",
"markdownlint-cli": "^0.25.0",
"minimist": "^1.2.5",
"nugget": "^2.0.1",
"null-loader": "^4.0.0",
"pre-flight": "^1.1.0",
"remark-cli": "^10.0.0",
"remark-preset-lint-markdown-style-guide": "^4.0.0",
"request": "^2.88.2",
"semver": "^5.6.0",
"shx": "^0.3.2",
"standard-markdown": "^6.0.0",

Просмотреть файл

@ -1,52 +1,23 @@
const args = require('minimist')(process.argv.slice(2));
const nugget = require('nugget');
const request = require('request');
const fs = require('fs');
const got = require('got');
const stream = require('stream');
const { promisify } = require('util');
async function makeRequest (requestOptions, parseResponse) {
return new Promise((resolve, reject) => {
request(requestOptions, (err, res, body) => {
if (!err && res.statusCode >= 200 && res.statusCode < 300) {
if (parseResponse) {
const build = JSON.parse(body);
resolve(build);
} else {
resolve(body);
}
} else {
if (args.verbose) {
console.error('Error occurred while requesting:', requestOptions.url);
if (parseResponse) {
try {
console.log('Error: ', `(status ${res.statusCode})`, err || JSON.parse(res.body), requestOptions);
} catch (err) {
console.log('Error: ', `(status ${res.statusCode})`, err || res.body, requestOptions);
}
} else {
console.log('Error: ', `(status ${res.statusCode})`, err || res.body, requestOptions);
}
}
reject(err);
}
});
});
}
const pipeline = promisify(stream.pipeline);
async function downloadArtifact (name, buildNum, dest) {
const circleArtifactUrl = `https://circleci.com/api/v1.1/project/github/electron/electron/${args.buildNum}/artifacts?circle-token=${process.env.CIRCLE_TOKEN}`;
const artifacts = await makeRequest({
method: 'GET',
url: circleArtifactUrl,
const responsePromise = got(circleArtifactUrl, {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
}, true).catch(err => {
if (args.verbose) {
console.log('Error calling CircleCI:', err);
} else {
console.error('Error calling CircleCI to get artifact details');
}
});
const [response, artifacts] = await Promise.all([responsePromise, responsePromise.json()]);
if (response.statusCode !== 200) {
console.error('Could not fetch circleci artifact list, got status code:', response.statusCode);
}
const artifactToDownload = artifacts.find(artifact => {
return (artifact.path === name);
});
@ -86,19 +57,10 @@ async function downloadWithRetry (url, directory) {
}
function downloadFile (url, directory) {
return new Promise((resolve, reject) => {
const nuggetOpts = {
dir: directory,
quiet: args.verbose
};
nugget(url, nuggetOpts, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return pipeline(
got.stream(url),
fs.createWriteStream(directory)
);
}
if (!args.name || !args.buildNum || !args.dest) {

Просмотреть файл

@ -1,7 +1,7 @@
if (!process.env.CI) require('dotenv-safe').load();
const assert = require('assert');
const request = require('request');
const got = require('got');
const BUILD_APPVEYOR_URL = 'https://ci.appveyor.com/api/builds';
const CIRCLECI_PIPELINE_URL = 'https://circleci.com/api/v2/project/gh/electron/electron/pipeline';
@ -35,31 +35,18 @@ const vstsArmJobs = [
let jobRequestedCount = 0;
async function makeRequest (requestOptions, parseResponse) {
return new Promise((resolve, reject) => {
request(requestOptions, (err, res, body) => {
if (!err && res.statusCode >= 200 && res.statusCode < 300) {
if (parseResponse) {
const build = JSON.parse(body);
resolve(build);
} else {
resolve(body);
}
} else {
console.error('Error occurred while requesting:', requestOptions.url);
if (parseResponse) {
try {
console.log('Error: ', `(status ${res.statusCode})`, err || JSON.parse(res.body));
} catch (err) {
console.log('Error: ', `(status ${res.statusCode})`, res.body);
}
} else {
console.log('Error: ', `(status ${res.statusCode})`, err || res.body);
}
reject(err);
}
});
async function makeRequest ({ auth, url, headers, body, method }) {
const response = await got(url, {
headers,
body,
method,
auth: auth ? `${auth.username}:${auth.password}` : undefined
});
if (response.statusCode !== 200) {
console.error('Error: ', `(status ${response.statusCode})`, response.body);
throw new Error(`Unexpected status code ${response.statusCode} from ${url}`);
}
return JSON.parse(response.body);
}
async function circleCIcall (targetBranch, workflowName, options) {

Просмотреть файл

@ -0,0 +1,43 @@
const { Octokit } = require('@octokit/rest');
const got = require('got');
const octokit = new Octokit({
userAgent: 'electron-asset-fetcher',
auth: process.env.ELECTRON_GITHUB_TOKEN
});
async function getAssetContents (repo, assetId) {
const requestOptions = octokit.repos.getReleaseAsset.endpoint({
owner: 'electron',
repo,
asset_id: assetId,
headers: {
Accept: 'application/octet-stream'
}
});
const { url, headers } = requestOptions;
headers.authorization = `token ${process.env.ELECTRON_GITHUB_TOKEN}`;
const response = await got(url, {
followRedirect: false,
method: 'HEAD',
headers
});
if (!response.headers.location) {
console.error(response.headers, response.body);
throw new Error(`cannot find asset[${assetId}], asset download did not redirect`);
}
const fileResponse = await got(response.headers.location);
if (fileResponse.status !== 200) {
console.error(fileResponse.headers, fileResponse.body);
throw new Error(`cannot download asset[${assetId}] from ${response.headers.location}, got status: ${fileResponse.status}`);
}
return fileResponse.body;
}
module.exports = {
getAssetContents
};

Просмотреть файл

@ -2,12 +2,14 @@ const temp = require('temp');
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
const { getCurrentBranch, ELECTRON_DIR } = require('../lib/utils');
const request = require('request');
const got = require('got');
const semver = require('semver');
const { getCurrentBranch, ELECTRON_DIR } = require('../lib/utils');
const rootPackageJson = require('../../package.json');
const { Octokit } = require('@octokit/rest');
const { getAssetContents } = require('./get-asset');
const octokit = new Octokit({
userAgent: 'electron-npm-publisher',
auth: process.env.ELECTRON_GITHUB_TOKEN
@ -91,22 +93,13 @@ new Promise((resolve, reject) => {
if (!tsdAsset) {
throw new Error(`cannot find electron.d.ts from v${rootPackageJson.version} release assets`);
}
return new Promise((resolve, reject) => {
request.get({
url: tsdAsset.url,
headers: {
accept: 'application/octet-stream',
'user-agent': 'electron-npm-publisher'
}
}, (err, response, body) => {
if (err || response.statusCode !== 200) {
reject(err || new Error('Cannot download electron.d.ts'));
} else {
fs.writeFileSync(path.join(tempDir, 'electron.d.ts'), body);
resolve(release);
}
});
});
const typingsContent = await getAssetContents(
rootPackageJson.version.indexOf('nightly') > 0 ? 'nightlies' : 'electron',
tsdAsset.id
);
fs.writeFileSync(path.join(tempDir, 'electron.d.ts'), typingsContent);
})
.then(async (release) => {
const currentBranch = await getCurrentBranch();

Просмотреть файл

@ -384,7 +384,7 @@ async function verifyDraftGitHubReleaseAssets (release) {
console.log('Fetching authenticated GitHub artifact URLs to verify shasums');
const remoteFilesToHash = await Promise.all(release.assets.map(async asset => {
const requestOptions = await octokit.repos.getReleaseAsset.endpoint({
const requestOptions = octokit.repos.getReleaseAsset.endpoint({
owner: 'electron',
repo: targetRepo,
asset_id: asset.id,

Просмотреть файл

@ -1104,7 +1104,7 @@ shell.writeShortcutLink('/home/user/Desktop/shortcut.lnk', 'update', shell.readS
session.defaultSession.on('will-download', (event, item, webContents) => {
event.preventDefault()
require('request')(item.getURL(), (data: any) => {
require('got')(item.getURL()).then((data: any) => {
require('fs').writeFileSync('/somewhere', data)
})
})

715
yarn.lock

Разница между файлами не показана из-за своего большого размера Загрузить разницу