2022-05-06 09:42:34 +00:00
|
|
|
const got = require('got');
|
2023-06-22 14:21:42 +00:00
|
|
|
const url = require('node:url');
|
2021-05-11 16:30:35 +00:00
|
|
|
|
2021-10-25 21:11:07 +00:00
|
|
|
module.exports = async function getUrlHash (targetUrl, algorithm = 'sha256', attempts = 3) {
|
2022-05-06 09:42:34 +00:00
|
|
|
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()
|
|
|
|
});
|
2021-10-25 21:11:07 +00:00
|
|
|
try {
|
2022-05-06 09:42:34 +00:00
|
|
|
const resp = await got(functionUrl);
|
|
|
|
if (resp.statusCode !== 200) throw new Error('non-200 status code received from hasher function');
|
|
|
|
if (!resp.body) throw new Error('Successful lambda call but failed to get valid hash');
|
|
|
|
|
|
|
|
return resp.body.trim();
|
2021-10-25 21:11:07 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (attempts > 1) {
|
2024-01-12 13:37:39 +00:00
|
|
|
if (err.response?.body) {
|
|
|
|
console.error(`Failed to get URL hash for ${targetUrl} - we will retry`, {
|
|
|
|
statusCode: err.response.statusCode,
|
|
|
|
body: JSON.parse(err.response.body)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.error(`Failed to get URL hash for ${targetUrl} - we will retry`, err);
|
|
|
|
}
|
2021-10-25 21:11:07 +00:00
|
|
|
return getUrlHash(targetUrl, algorithm, attempts - 1);
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
2021-05-11 16:30:35 +00:00
|
|
|
};
|