forked from mreinstein/alexa-verifier
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch-cert.js
More file actions
28 lines (24 loc) · 849 Bytes
/
fetch-cert.js
File metadata and controls
28 lines (24 loc) · 849 Bytes
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
var request = require('request')
// default in-memory cache for downloaded certificates
var globalCache = {}
module.exports = function fetchCert(options, callback) {
var url = options.url
var cache = options.cache || globalCache
var cachedResponse = cache[url.href]
var servedFromCache = false
if (cachedResponse) {
servedFromCache = true
process.nextTick(callback, undefined, cachedResponse, servedFromCache)
return
}
request.get(url.href, function(er, response, body) {
var statusCode
if (response && 200 === response.statusCode) {
cache[url.href] = body
callback(undefined, body, servedFromCache)
} else {
statusCode = response ? response.statusCode : 0
callback('Failed to download certificate at: ' + url.href + '. Response code: ' + statusCode + ', error: ' + er)
}
})
}