forked from mreinstein/alexa-verifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-cert.js
45 lines (35 loc) · 1.29 KB
/
fetch-cert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
const request = require('request-promise');
const globalCache = {} // default in-memory cache for downloaded certificates;
module.exports = async function fetchCert(options, callback) {
const url = options.url;
const cache = options.cache || globalCache;
const cachedResponse = cache[url.href];
let servedFromCache = false;
if (cachedResponse) {
servedFromCache = true;
process.nextTick(callback, undefined, cachedResponse, servedFromCache);
return;
}
try {
/**
* Use the request-promise library
* to fetch the certificate, since
* this library honors the HTTP(S)_PROXY
* env variables
*/
const response = await request(url, {
json: true,
resolveWithFullResponse: true
});
if (!response || 200 !== response.statusCode) {
const statusCode = response ? response.statusCode : 0;
return callback('Failed to download certificate at: ' + url.href + '. Response code: ' + statusCode);
}
const body = response.body;
cache[url.href] = body;
callback(undefined, body, servedFromCache);
} catch (err) {
callback('Failed to download certificate at: ' + url.href + '. Error: ' + err);
}
}