2017-08-12 16:14:46 +00:00
|
|
|
require('isomorphic-fetch')
|
2017-09-13 15:35:25 +00:00
|
|
|
const invariant = require('invariant')
|
2017-08-17 18:24:40 +00:00
|
|
|
const gunzip = require('gunzip-maybe')
|
|
|
|
const ndjson = require('ndjson')
|
2017-08-12 16:14:46 +00:00
|
|
|
|
2017-08-17 05:47:24 +00:00
|
|
|
const CloudflareAPIURL = 'https://api.cloudflare.com'
|
2017-08-12 16:14:46 +00:00
|
|
|
const CloudflareEmail = process.env.CLOUDFLARE_EMAIL
|
|
|
|
const CloudflareKey = process.env.CLOUDFLARE_KEY
|
|
|
|
|
2017-09-13 15:35:25 +00:00
|
|
|
invariant(
|
2017-08-12 16:14:46 +00:00
|
|
|
CloudflareEmail,
|
|
|
|
'Missing the $CLOUDFLARE_EMAIL environment variable'
|
|
|
|
)
|
|
|
|
|
2017-09-13 15:35:25 +00:00
|
|
|
invariant(
|
2017-08-12 16:14:46 +00:00
|
|
|
CloudflareKey,
|
|
|
|
'Missing the $CLOUDFLARE_KEY environment variable'
|
|
|
|
)
|
|
|
|
|
|
|
|
function get(path, headers) {
|
2017-08-17 05:47:24 +00:00
|
|
|
return fetch(`${CloudflareAPIURL}/client/v4${path}`, {
|
2017-08-12 16:14:46 +00:00
|
|
|
headers: Object.assign({}, headers, {
|
|
|
|
'X-Auth-Email': CloudflareEmail,
|
|
|
|
'X-Auth-Key': CloudflareKey
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getJSON(path, headers) {
|
|
|
|
return get(path, headers).then(function (res) {
|
|
|
|
return res.json()
|
|
|
|
}).then(function (data) {
|
2017-08-22 15:31:33 +00:00
|
|
|
if (!data.success) {
|
|
|
|
console.error(`CloudflareAPI.getJSON failed at ${path}`)
|
|
|
|
console.error(data)
|
|
|
|
throw new Error('Failed to getJSON from Cloudflare')
|
|
|
|
}
|
|
|
|
|
2017-08-12 16:14:46 +00:00
|
|
|
return data.result
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-22 15:31:33 +00:00
|
|
|
function getZones(domains) {
|
|
|
|
return Promise.all(
|
|
|
|
(Array.isArray(domains) ? domains : [ domains ]).map(function (domain) {
|
|
|
|
return getJSON(`/zones?name=${domain}`)
|
|
|
|
})
|
|
|
|
).then(function (results) {
|
|
|
|
return results.reduce(function (memo, zones) {
|
|
|
|
return memo.concat(zones)
|
|
|
|
})
|
|
|
|
})
|
2017-08-17 18:24:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 15:31:33 +00:00
|
|
|
function reduceResults(target, values) {
|
|
|
|
Object.keys(values).forEach(key => {
|
|
|
|
const value = values[key]
|
|
|
|
|
|
|
|
if (typeof value === 'object' && value) {
|
|
|
|
target[key] = reduceResults(target[key] || {}, value)
|
|
|
|
} else if (typeof value === 'number') {
|
|
|
|
target[key] = (target[key] || 0) + values[key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
|
|
|
|
function getZoneAnalyticsDashboard(zones, since, until) {
|
|
|
|
return Promise.all(
|
|
|
|
(Array.isArray(zones) ? zones : [ zones ]).map(function (zone) {
|
|
|
|
return getJSON(`/zones/${zone.id}/analytics/dashboard?since=${since.toISOString()}&until=${until.toISOString()}`)
|
|
|
|
})
|
|
|
|
).then(function (results) {
|
|
|
|
return results.reduce(reduceResults)
|
|
|
|
})
|
2017-08-17 18:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getJSONStream(path, headers) {
|
|
|
|
const acceptGzipHeaders = Object.assign({}, headers, { 'Accept-Encoding': 'gzip' })
|
|
|
|
|
|
|
|
return get(path, acceptGzipHeaders).then(function (res) {
|
|
|
|
return res.body.pipe(gunzip())
|
|
|
|
}).then(function (stream) {
|
|
|
|
return stream.pipe(ndjson.parse())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLogs(zoneId, startTime, endTime) {
|
|
|
|
return getJSONStream(`/zones/${zoneId}/logs/requests?start=${startTime}&end=${endTime}`)
|
|
|
|
}
|
|
|
|
|
2017-08-12 16:14:46 +00:00
|
|
|
module.exports = {
|
|
|
|
get,
|
2017-08-17 18:24:40 +00:00
|
|
|
getJSON,
|
|
|
|
getZones,
|
|
|
|
getZoneAnalyticsDashboard,
|
|
|
|
getJSONStream,
|
|
|
|
getLogs
|
2017-08-12 16:14:46 +00:00
|
|
|
}
|