umami/scripts/telemetry.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-03-17 05:50:24 +01:00
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
const retry = require('async-retry');
const isCI = require('is-ci');
const pkg = require('../package.json');
const dest = path.resolve(__dirname, '../.next/cache/umami.json');
const url = 'https://telemetry.umami.is/api/collect';
2022-03-17 06:05:43 +01:00
async function sendTelemetry() {
2022-03-17 05:50:24 +01:00
await fs.ensureFile(dest);
let json = {};
try {
json = await fs.readJSON(dest);
} catch {
// Ignore
}
if (json.version !== pkg.version) {
const { default: isDocker } = await import('is-docker');
const { default: fetch } = await import('node-fetch');
await fs.writeJSON(dest, { version: pkg.version });
const payload = {
2022-03-19 05:21:43 +01:00
version: pkg.version,
2022-03-17 05:50:24 +01:00
node: process.version,
platform: os.platform(),
arch: os.arch(),
2022-03-17 05:59:17 +01:00
os: `${os.type()} (${os.version()})`,
2022-03-19 05:21:43 +01:00
docker: isDocker(),
ci: isCI,
upgrade: json.version || false,
2022-03-17 05:50:24 +01:00
};
await retry(
async () => {
2022-03-17 05:59:17 +01:00
await fetch(url, {
2022-03-17 05:50:24 +01:00
method: 'post',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
},
{ minTimeout: 500, retries: 1, factor: 1 },
).catch(() => {});
}
}
2022-03-19 05:57:58 +01:00
module.exports = {
sendTelemetry,
};