2024-10-01 10:04:22 +00:00
|
|
|
import got from 'got';
|
2024-10-03 03:52:01 +00:00
|
|
|
|
2024-10-01 10:04:22 +00:00
|
|
|
import * as url from 'node:url';
|
2021-05-11 16:30:35 +00:00
|
|
|
|
2024-10-01 10:04:22 +00:00
|
|
|
const HASHER_FUNCTION_HOST = 'electron-artifact-hasher.azurewebsites.net';
|
|
|
|
const HASHER_FUNCTION_ROUTE = '/api/HashArtifact';
|
|
|
|
|
|
|
|
export async function getUrlHash (targetUrl: string, algorithm = 'sha256', attempts = 3) {
|
2022-05-06 09:42:34 +00:00
|
|
|
const options = {
|
2024-10-01 10:04:22 +00:00
|
|
|
code: process.env.ELECTRON_ARTIFACT_HASHER_FUNCTION_KEY!,
|
2022-05-06 09:42:34 +00:00
|
|
|
targetUrl,
|
|
|
|
algorithm
|
|
|
|
};
|
|
|
|
const search = new url.URLSearchParams(options);
|
|
|
|
const functionUrl = url.format({
|
|
|
|
protocol: 'https:',
|
2024-10-01 10:04:22 +00:00
|
|
|
hostname: HASHER_FUNCTION_HOST,
|
|
|
|
pathname: HASHER_FUNCTION_ROUTE,
|
2022-05-06 09:42:34 +00:00
|
|
|
search: search.toString()
|
|
|
|
});
|
2021-10-25 21:11:07 +00:00
|
|
|
try {
|
2024-09-17 22:27:27 +00:00
|
|
|
const resp = await got(functionUrl, {
|
|
|
|
throwHttpErrors: false
|
|
|
|
});
|
|
|
|
if (resp.statusCode !== 200) {
|
|
|
|
console.error('bad hasher function response:', resp.body.trim());
|
|
|
|
throw new Error('non-200 status code received from hasher function');
|
|
|
|
}
|
2022-05-06 09:42:34 +00:00
|
|
|
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-10-01 10:04:22 +00:00
|
|
|
const { response } = err as any;
|
|
|
|
if (response?.body) {
|
2024-01-12 13:37:39 +00:00
|
|
|
console.error(`Failed to get URL hash for ${targetUrl} - we will retry`, {
|
2024-10-01 10:04:22 +00:00
|
|
|
statusCode: response.statusCode,
|
|
|
|
body: JSON.parse(response.body)
|
2024-01-12 13:37:39 +00:00
|
|
|
});
|
|
|
|
} 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
|
|
|
};
|