2021-08-12 17:34:49 +00:00
|
|
|
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) {
|
2021-08-13 20:15:39 +00:00
|
|
|
console.error(response.headers, `${response.body}`.slice(0, 300));
|
2021-08-12 17:34:49 +00:00
|
|
|
throw new Error(`cannot find asset[${assetId}], asset download did not redirect`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileResponse = await got(response.headers.location);
|
2021-08-13 20:26:27 +00:00
|
|
|
if (fileResponse.statusCode !== 200) {
|
2021-08-13 20:15:39 +00:00
|
|
|
console.error(fileResponse.headers, `${fileResponse.body}`.slice(0, 300));
|
2021-08-12 17:34:49 +00:00
|
|
|
throw new Error(`cannot download asset[${assetId}] from ${response.headers.location}, got status: ${fileResponse.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileResponse.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getAssetContents
|
|
|
|
};
|