-
Notifications
You must be signed in to change notification settings - Fork 33
/
report.js
73 lines (62 loc) · 1.81 KB
/
report.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const path = require('path')
const os = require('os')
const botiumAnalyticsHost = process.env.BOTIUM_ANALYTICS_HOST || 'v1.license.botium.cyaraportal.us'
const botiumAnalyticsPort = process.env.BOTIUM_ANALYTICS_PORT || 443
const https = botiumAnalyticsPort === 443 ? require('https') : require('http')
const execTimeout = 10000
function logIfVerbose (toLog, stream) {
if (process.env.BOTIUM_ANALYTICS_VERBOSE === 'true') {
(stream || console.log)(toLog)
}
}
async function reportPostInstall () {
if (process.env.BOTIUM_ANALYTICS === 'false') return
const packageJson = require(path.join(__dirname, 'package.json'))
const infoPayload = {
rawPlatform: os.platform(),
rawArch: os.arch(),
library: packageJson.name,
version: packageJson.version
}
const data = JSON.stringify(infoPayload)
logIfVerbose(`Botium analytics payload: ${data}`)
const reqOptions = {
host: botiumAnalyticsHost,
port: botiumAnalyticsPort,
method: 'POST',
path: '/metrics/installation/core',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
},
timeout: execTimeout
}
await new Promise((resolve, reject) => {
const req = https.request(reqOptions, (res) => {
logIfVerbose(`Response status: ${res.statusCode}`)
resolve()
})
req.on('error', error => {
logIfVerbose(error, console.error)
reject(error)
})
req.on('timeout', error => {
logIfVerbose(error, console.error)
reject(error)
})
req.write(data)
req.end()
})
}
if (require.main === module) {
try {
reportPostInstall().catch(e => {
logIfVerbose(`\n\n${e}`, console.error)
}).finally(() => {
process.exit(0)
})
} catch (e) {
logIfVerbose(`\n\nTop level error: ${e}`, console.error)
process.exit(0)
}
}