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