get-url-hash.js 995 B

123456789101112131415161718192021222324252627282930
  1. const got = require('got');
  2. const url = require('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. console.error('Failed to get URL hash for', targetUrl, 'we will retry', err);
  24. return getUrlHash(targetUrl, algorithm, attempts - 1);
  25. }
  26. throw err;
  27. }
  28. };