build: use azure function to hash assets instead of lambda (#34117)
This commit is contained in:
parent
0696320d28
commit
808efd89ed
1 changed files with 19 additions and 28 deletions
|
@ -1,34 +1,25 @@
|
||||||
const AWS = require('aws-sdk');
|
const got = require('got');
|
||||||
|
const url = require('url');
|
||||||
const lambda = new AWS.Lambda({
|
|
||||||
credentials: {
|
|
||||||
accessKeyId: process.env.AWS_LAMBDA_EXECUTE_KEY,
|
|
||||||
secretAccessKey: process.env.AWS_LAMBDA_EXECUTE_SECRET
|
|
||||||
},
|
|
||||||
region: 'us-east-1'
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = async function getUrlHash (targetUrl, algorithm = 'sha256', attempts = 3) {
|
module.exports = async function getUrlHash (targetUrl, algorithm = 'sha256', attempts = 3) {
|
||||||
|
const options = {
|
||||||
|
code: process.env.ELECTRON_ARTIFACT_HASHER_FUNCTION_KEY,
|
||||||
|
targetUrl,
|
||||||
|
algorithm
|
||||||
|
};
|
||||||
|
const search = new url.URLSearchParams(options);
|
||||||
|
const functionUrl = url.format({
|
||||||
|
protocol: 'https:',
|
||||||
|
hostname: 'electron-artifact-hasher.azurewebsites.net',
|
||||||
|
pathname: '/api/HashArtifact',
|
||||||
|
search: search.toString()
|
||||||
|
});
|
||||||
try {
|
try {
|
||||||
return new Promise((resolve, reject) => {
|
const resp = await got(functionUrl);
|
||||||
lambda.invoke({
|
if (resp.statusCode !== 200) throw new Error('non-200 status code received from hasher function');
|
||||||
FunctionName: 'hasher',
|
if (!resp.body) throw new Error('Successful lambda call but failed to get valid hash');
|
||||||
Payload: JSON.stringify({
|
|
||||||
targetUrl,
|
return resp.body.trim();
|
||||||
algorithm
|
|
||||||
})
|
|
||||||
}, (err, data) => {
|
|
||||||
if (err) return reject(err);
|
|
||||||
try {
|
|
||||||
const response = JSON.parse(data.Payload);
|
|
||||||
if (response.statusCode !== 200) return reject(new Error('non-200 status code received from hasher function'));
|
|
||||||
if (!response.hash) return reject(new Error('Successful lambda call but failed to get valid hash'));
|
|
||||||
resolve(response.hash);
|
|
||||||
} catch (err) {
|
|
||||||
return reject(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (attempts > 1) {
|
if (attempts > 1) {
|
||||||
console.error('Failed to get URL hash for', targetUrl, 'we will retry', err);
|
console.error('Failed to get URL hash for', targetUrl, 'we will retry', err);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue