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:
parent
08ff1c2cbf
commit
439e83de6c
9 changed files with 238 additions and 664 deletions
|
@ -86,8 +86,8 @@ available from next tick of the process.
|
||||||
const { session } = require('electron')
|
const { session } = require('electron')
|
||||||
session.defaultSession.on('will-download', (event, item, webContents) => {
|
session.defaultSession.on('will-download', (event, item, webContents) => {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
require('request')(item.getURL(), (data) => {
|
require('got')(item.getURL()).then((response) => {
|
||||||
require('fs').writeFileSync('/somewhere', data)
|
require('fs').writeFileSync('/somewhere', response.body)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
|
@ -35,7 +35,7 @@
|
||||||
"check-for-leaks": "^1.2.1",
|
"check-for-leaks": "^1.2.1",
|
||||||
"colors": "^1.4.0",
|
"colors": "^1.4.0",
|
||||||
"dotenv-safe": "^4.0.4",
|
"dotenv-safe": "^4.0.4",
|
||||||
"dugite": "^1.45.0",
|
"dugite": "^1.103.0",
|
||||||
"eslint": "^7.4.0",
|
"eslint": "^7.4.0",
|
||||||
"eslint-config-standard": "^14.1.1",
|
"eslint-config-standard": "^14.1.1",
|
||||||
"eslint-plugin-import": "^2.22.0",
|
"eslint-plugin-import": "^2.22.0",
|
||||||
|
@ -54,12 +54,10 @@
|
||||||
"markdownlint": "^0.21.1",
|
"markdownlint": "^0.21.1",
|
||||||
"markdownlint-cli": "^0.25.0",
|
"markdownlint-cli": "^0.25.0",
|
||||||
"minimist": "^1.2.5",
|
"minimist": "^1.2.5",
|
||||||
"nugget": "^2.0.1",
|
|
||||||
"null-loader": "^4.0.0",
|
"null-loader": "^4.0.0",
|
||||||
"pre-flight": "^1.1.0",
|
"pre-flight": "^1.1.0",
|
||||||
"remark-cli": "^10.0.0",
|
"remark-cli": "^10.0.0",
|
||||||
"remark-preset-lint-markdown-style-guide": "^4.0.0",
|
"remark-preset-lint-markdown-style-guide": "^4.0.0",
|
||||||
"request": "^2.88.2",
|
|
||||||
"semver": "^5.6.0",
|
"semver": "^5.6.0",
|
||||||
"shx": "^0.3.2",
|
"shx": "^0.3.2",
|
||||||
"standard-markdown": "^6.0.0",
|
"standard-markdown": "^6.0.0",
|
||||||
|
|
|
@ -1,52 +1,23 @@
|
||||||
const args = require('minimist')(process.argv.slice(2));
|
const args = require('minimist')(process.argv.slice(2));
|
||||||
const nugget = require('nugget');
|
const fs = require('fs');
|
||||||
const request = require('request');
|
const got = require('got');
|
||||||
|
const stream = require('stream');
|
||||||
|
const { promisify } = require('util');
|
||||||
|
|
||||||
async function makeRequest (requestOptions, parseResponse) {
|
const pipeline = promisify(stream.pipeline);
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function downloadArtifact (name, buildNum, dest) {
|
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 circleArtifactUrl = `https://circleci.com/api/v1.1/project/github/electron/electron/${args.buildNum}/artifacts?circle-token=${process.env.CIRCLE_TOKEN}`;
|
||||||
const artifacts = await makeRequest({
|
const responsePromise = got(circleArtifactUrl, {
|
||||||
method: 'GET',
|
|
||||||
url: circleArtifactUrl,
|
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Accept: '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 => {
|
const artifactToDownload = artifacts.find(artifact => {
|
||||||
return (artifact.path === name);
|
return (artifact.path === name);
|
||||||
});
|
});
|
||||||
|
@ -86,19 +57,10 @@ async function downloadWithRetry (url, directory) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function downloadFile (url, directory) {
|
function downloadFile (url, directory) {
|
||||||
return new Promise((resolve, reject) => {
|
return pipeline(
|
||||||
const nuggetOpts = {
|
got.stream(url),
|
||||||
dir: directory,
|
fs.createWriteStream(directory)
|
||||||
quiet: args.verbose
|
);
|
||||||
};
|
|
||||||
nugget(url, nuggetOpts, (err) => {
|
|
||||||
if (err) {
|
|
||||||
reject(err);
|
|
||||||
} else {
|
|
||||||
resolve();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!args.name || !args.buildNum || !args.dest) {
|
if (!args.name || !args.buildNum || !args.dest) {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
if (!process.env.CI) require('dotenv-safe').load();
|
if (!process.env.CI) require('dotenv-safe').load();
|
||||||
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const request = require('request');
|
const got = require('got');
|
||||||
|
|
||||||
const BUILD_APPVEYOR_URL = 'https://ci.appveyor.com/api/builds';
|
const BUILD_APPVEYOR_URL = 'https://ci.appveyor.com/api/builds';
|
||||||
const CIRCLECI_PIPELINE_URL = 'https://circleci.com/api/v2/project/gh/electron/electron/pipeline';
|
const CIRCLECI_PIPELINE_URL = 'https://circleci.com/api/v2/project/gh/electron/electron/pipeline';
|
||||||
|
@ -35,31 +35,18 @@ const vstsArmJobs = [
|
||||||
|
|
||||||
let jobRequestedCount = 0;
|
let jobRequestedCount = 0;
|
||||||
|
|
||||||
async function makeRequest (requestOptions, parseResponse) {
|
async function makeRequest ({ auth, url, headers, body, method }) {
|
||||||
return new Promise((resolve, reject) => {
|
const response = await got(url, {
|
||||||
request(requestOptions, (err, res, body) => {
|
headers,
|
||||||
if (!err && res.statusCode >= 200 && res.statusCode < 300) {
|
body,
|
||||||
if (parseResponse) {
|
method,
|
||||||
const build = JSON.parse(body);
|
auth: auth ? `${auth.username}:${auth.password}` : undefined
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
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) {
|
async function circleCIcall (targetBranch, workflowName, options) {
|
||||||
|
|
43
script/release/get-asset.js
Normal file
43
script/release/get-asset.js
Normal file
|
@ -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 fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const childProcess = require('child_process');
|
const childProcess = require('child_process');
|
||||||
const { getCurrentBranch, ELECTRON_DIR } = require('../lib/utils');
|
const got = require('got');
|
||||||
const request = require('request');
|
|
||||||
const semver = require('semver');
|
const semver = require('semver');
|
||||||
|
|
||||||
|
const { getCurrentBranch, ELECTRON_DIR } = require('../lib/utils');
|
||||||
const rootPackageJson = require('../../package.json');
|
const rootPackageJson = require('../../package.json');
|
||||||
|
|
||||||
const { Octokit } = require('@octokit/rest');
|
const { Octokit } = require('@octokit/rest');
|
||||||
|
const { getAssetContents } = require('./get-asset');
|
||||||
const octokit = new Octokit({
|
const octokit = new Octokit({
|
||||||
userAgent: 'electron-npm-publisher',
|
userAgent: 'electron-npm-publisher',
|
||||||
auth: process.env.ELECTRON_GITHUB_TOKEN
|
auth: process.env.ELECTRON_GITHUB_TOKEN
|
||||||
|
@ -91,22 +93,13 @@ new Promise((resolve, reject) => {
|
||||||
if (!tsdAsset) {
|
if (!tsdAsset) {
|
||||||
throw new Error(`cannot find electron.d.ts from v${rootPackageJson.version} release assets`);
|
throw new Error(`cannot find electron.d.ts from v${rootPackageJson.version} release assets`);
|
||||||
}
|
}
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
request.get({
|
const typingsContent = await getAssetContents(
|
||||||
url: tsdAsset.url,
|
rootPackageJson.version.indexOf('nightly') > 0 ? 'nightlies' : 'electron',
|
||||||
headers: {
|
tsdAsset.id
|
||||||
accept: 'application/octet-stream',
|
);
|
||||||
'user-agent': 'electron-npm-publisher'
|
|
||||||
}
|
fs.writeFileSync(path.join(tempDir, 'electron.d.ts'), typingsContent);
|
||||||
}, (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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
.then(async (release) => {
|
.then(async (release) => {
|
||||||
const currentBranch = await getCurrentBranch();
|
const currentBranch = await getCurrentBranch();
|
||||||
|
|
|
@ -384,7 +384,7 @@ async function verifyDraftGitHubReleaseAssets (release) {
|
||||||
console.log('Fetching authenticated GitHub artifact URLs to verify shasums');
|
console.log('Fetching authenticated GitHub artifact URLs to verify shasums');
|
||||||
|
|
||||||
const remoteFilesToHash = await Promise.all(release.assets.map(async asset => {
|
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',
|
owner: 'electron',
|
||||||
repo: targetRepo,
|
repo: targetRepo,
|
||||||
asset_id: asset.id,
|
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) => {
|
session.defaultSession.on('will-download', (event, item, webContents) => {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
require('request')(item.getURL(), (data: any) => {
|
require('got')(item.getURL()).then((data: any) => {
|
||||||
require('fs').writeFileSync('/somewhere', data)
|
require('fs').writeFileSync('/somewhere', data)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue