/
index.ts
115 lines (102 loc) · 3.09 KB
/
index.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const snyk = require('../../lib');
import { someTokenExists, getAuthHeader } from '../api-token';
const config = require('../config');
import { makeRequest } from '../request';
const debug = require('debug')('snyk');
const stripAnsi = require('strip-ansi');
import * as needle from 'needle';
import { getStandardData } from './getStandardData';
const metadata = {};
// analytics module is required at the beginning of the CLI run cycle
/**
*
* @param data the data to merge into that data which has been staged thus far (with the {@link add} function)
* and then sent to the backend.
*/
export function addDataAndSend(
data,
): Promise<void | { res: needle.NeedleResponse; body: any }> {
if (!data) {
data = {};
}
// merge any new data with data we picked up along the way
if (Array.isArray(data.args)) {
// this is an overhang from the cli/args.js and we don't want it
delete (data.args.slice(-1).pop() || {})._;
}
if (Object.keys(metadata).length) {
data.metadata = metadata;
}
return postAnalytics(data);
}
export function allowAnalytics(): boolean {
if (snyk.config.get('disable-analytics') || config.DISABLE_ANALYTICS) {
return false;
} else {
return true;
}
}
/**
* Actually send the analytics to the backend. This can be used standalone to send only the data
* given by the data parameter, or called from {@link addDataAndSend}.
* @param customData the analytics data to send to the backend.
*/
export async function postAnalytics(
customData,
): Promise<void | { res: needle.NeedleResponse; body: any }> {
// if the user opt'ed out of analytics, then let's bail out early
// ths applies to all sending to protect user's privacy
if (!allowAnalytics()) {
debug('analytics disabled');
return Promise.resolve();
}
try {
const standardData = await getStandardData(customData.args);
const analyticsData = {
...customData,
...standardData,
};
debug('analytics', JSON.stringify(analyticsData, null, ' '));
const headers = {};
if (someTokenExists()) {
headers['authorization'] = getAuthHeader();
}
const queryStringParams = {};
if (analyticsData.org) {
queryStringParams['org'] = analyticsData.org;
}
const queryString =
Object.keys(queryStringParams).length > 0 ? queryStringParams : undefined;
return makeRequest({
body: {
data: analyticsData,
},
qs: queryString,
url: config.API + '/analytics/cli',
json: true,
method: 'post',
headers: headers,
});
} catch (err) {
debug('analytics', err); // this swallows the analytics error
}
}
/**
* Adds a key-value pair to the analytics data `metadata` field. This doesn't send the analytis, just stages it for
* sending later (via the {@link addDataAndSend} function).
* @param key
* @param value
*/
export function add(key, value) {
if (typeof value === 'string') {
value = stripAnsi(value);
}
if (metadata[key]) {
if (!Array.isArray(metadata[key])) {
metadata[key] = [metadata[key]];
}
metadata[key].push(value);
} else {
metadata[key] = value;
}
}