|
1 |
| -const AWS = require('aws-sdk'); |
2 |
| - |
3 |
| -const lambda = new AWS.Lambda({ |
4 |
| - credentials: { |
5 |
| - accessKeyId: process.env.AWS_LAMBDA_EXECUTE_KEY, |
6 |
| - secretAccessKey: process.env.AWS_LAMBDA_EXECUTE_SECRET |
7 |
| - }, |
8 |
| - region: 'us-east-1' |
9 |
| -}); |
| 1 | +const got = require('got'); |
| 2 | +const url = require('url'); |
10 | 3 |
|
11 | 4 | module.exports = async function getUrlHash (targetUrl, algorithm = 'sha256', attempts = 3) {
|
| 5 | + const options = { |
| 6 | + code: process.env.ELECTRON_ARTIFACT_HASHER_FUNCTION_KEY, |
| 7 | + targetUrl, |
| 8 | + algorithm |
| 9 | + }; |
| 10 | + const search = new url.URLSearchParams(options); |
| 11 | + const functionUrl = url.format({ |
| 12 | + protocol: 'https:', |
| 13 | + hostname: 'electron-artifact-hasher.azurewebsites.net', |
| 14 | + pathname: '/api/HashArtifact', |
| 15 | + search: search.toString() |
| 16 | + }); |
12 | 17 | try {
|
13 |
| - return new Promise((resolve, reject) => { |
14 |
| - lambda.invoke({ |
15 |
| - FunctionName: 'hasher', |
16 |
| - Payload: JSON.stringify({ |
17 |
| - targetUrl, |
18 |
| - algorithm |
19 |
| - }) |
20 |
| - }, (err, data) => { |
21 |
| - if (err) return reject(err); |
22 |
| - try { |
23 |
| - const response = JSON.parse(data.Payload); |
24 |
| - if (response.statusCode !== 200) return reject(new Error('non-200 status code received from hasher function')); |
25 |
| - if (!response.hash) return reject(new Error('Successful lambda call but failed to get valid hash')); |
26 |
| - resolve(response.hash); |
27 |
| - } catch (err) { |
28 |
| - return reject(err); |
29 |
| - } |
30 |
| - }); |
31 |
| - }); |
| 18 | + const resp = await got(functionUrl); |
| 19 | + if (resp.statusCode !== 200) throw new Error('non-200 status code received from hasher function'); |
| 20 | + if (!resp.body) throw new Error('Successful lambda call but failed to get valid hash'); |
| 21 | + |
| 22 | + return resp.body.trim(); |
32 | 23 | } catch (err) {
|
33 | 24 | if (attempts > 1) {
|
34 | 25 | console.error('Failed to get URL hash for', targetUrl, 'we will retry', err);
|
|
0 commit comments