get-url-hash.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. const got = require('got');
  2. const url = require('node:url');
  3. module.exports = async function getUrlHash (targetUrl, algorithm = 'sha256', attempts = 3) {
  4. const options = {
  5. code: process.env.ELECTRON_ARTIFACT_HASHER_FUNCTION_KEY,
  6. targetUrl,
  7. algorithm
  8. };
  9. const search = new url.URLSearchParams(options);
  10. const functionUrl = url.format({
  11. protocol: 'https:',
  12. hostname: 'electron-artifact-hasher.azurewebsites.net',
  13. pathname: '/api/HashArtifact',
  14. search: search.toString()
  15. });
  16. try {
  17. const resp = await got(functionUrl);
  18. if (resp.statusCode !== 200) throw new Error('non-200 status code received from hasher function');
  19. if (!resp.body) throw new Error('Successful lambda call but failed to get valid hash');
  20. return resp.body.trim();
  21. } catch (err) {
  22. if (attempts > 1) {
  23. if (err.response?.body) {
  24. console.error(`Failed to get URL hash for ${targetUrl} - we will retry`, {
  25. statusCode: err.response.statusCode,
  26. body: JSON.parse(err.response.body)
  27. });
  28. } else {
  29. console.error(`Failed to get URL hash for ${targetUrl} - we will retry`, err);
  30. }
  31. return getUrlHash(targetUrl, algorithm, attempts - 1);
  32. }
  33. throw err;
  34. }
  35. };