2016-11-16 14:55:59 +00:00
|
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
// Asynchronously submits install information to Realm.
|
|
|
|
|
//
|
|
|
|
|
// Why are we doing this? In short, because it helps us build a better product
|
|
|
|
|
// for you. None of the data personally identifies you, your employer or your
|
|
|
|
|
// app, but it *will* help us understand what language you use, what Node.js
|
|
|
|
|
// versions you target, etc. Having this info will help prioritizing our time,
|
|
|
|
|
// adding new features and deprecating old features. Collecting an anonymized
|
|
|
|
|
// application path & anonymized machine identifier is the only way for us to
|
|
|
|
|
// count actual usage of the other metrics accurately. If we don’t have a way to
|
|
|
|
|
// deduplicate the info reported, it will be useless, as a single developer
|
2018-11-14 12:33:55 +00:00
|
|
|
|
// `npm install`-ing the same app 10 times would report 10 times more than another
|
2016-11-16 14:55:59 +00:00
|
|
|
|
// developer that only installs once, making the data all but useless.
|
|
|
|
|
// No one likes sharing data unless it’s necessary, we get it, and we’ve
|
|
|
|
|
// debated adding this for a long long time. If you truly, absolutely
|
|
|
|
|
// feel compelled to not send this data back to Realm, then you can set an env
|
|
|
|
|
// variable named REALM_DISABLE_ANALYTICS.
|
|
|
|
|
//
|
|
|
|
|
// Currently the following information is reported:
|
|
|
|
|
// - What version of Realm is being installed.
|
|
|
|
|
// - The OS platform and version which is being used.
|
2018-11-14 12:33:55 +00:00
|
|
|
|
// - Node.js, v8, libuv, OpenSSL version numbers.
|
2016-11-16 14:55:59 +00:00
|
|
|
|
// - An anonymous machine identifier and hashed application path to aggregate the other information on.
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2018-11-14 12:33:55 +00:00
|
|
|
|
function isAnalyticsDisabled() {
|
|
|
|
|
return 'REALM_DISABLE_ANALYTICS' in process.env;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isAnalyticsDisabled()) {
|
2017-10-10 23:27:30 +00:00
|
|
|
|
module.exports = function(){};
|
2018-07-12 10:34:37 +00:00
|
|
|
|
} else {
|
|
|
|
|
const os = require('os');
|
|
|
|
|
const request = require('request');
|
2018-11-14 12:33:55 +00:00
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
const { machineId } = require("node-machine-id");
|
2016-11-16 14:55:59 +00:00
|
|
|
|
|
2018-07-12 10:34:37 +00:00
|
|
|
|
function sha256(data) {
|
|
|
|
|
let hash = crypto.createHash('sha256');
|
|
|
|
|
hash.update(data);
|
|
|
|
|
return hash.digest('hex');
|
|
|
|
|
}
|
2016-11-16 14:55:59 +00:00
|
|
|
|
|
2018-11-14 12:33:55 +00:00
|
|
|
|
module.exports = function(eventName, context) {
|
|
|
|
|
// Submit analytics after some time - we do this to give the application a chance to disable analytics.
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (isAnalyticsDisabled()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-11-16 14:55:59 +00:00
|
|
|
|
|
2018-11-14 12:33:55 +00:00
|
|
|
|
machineId()
|
|
|
|
|
.catch() // Ignore errors
|
|
|
|
|
.then((identifier) => {
|
|
|
|
|
if (!identifier) {
|
|
|
|
|
identifier = sha256('unknown');
|
|
|
|
|
}
|
2016-11-16 14:55:59 +00:00
|
|
|
|
|
2018-11-14 12:33:55 +00:00
|
|
|
|
const payload = {
|
|
|
|
|
'event': eventName,
|
|
|
|
|
'properties': {
|
|
|
|
|
'token': 'aab85907a13e1ff44a95be539d9942a9',
|
|
|
|
|
'distinct_id': identifier,
|
|
|
|
|
'Anonymized Machine Identifier': identifier,
|
|
|
|
|
'Anonymized Application ID': sha256(__dirname),
|
|
|
|
|
'Binding': context || 'node.js',
|
|
|
|
|
'Version': require('../package.json').version,
|
|
|
|
|
'Language': 'javascript',
|
|
|
|
|
'OS Type': os.platform(),
|
|
|
|
|
'OS Version': os.release(),
|
|
|
|
|
'Node.js versions': process.versions
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-11-16 14:55:59 +00:00
|
|
|
|
|
2018-11-14 12:33:55 +00:00
|
|
|
|
request(`https://api.mixpanel.com/track/?data=${Buffer.from(JSON.stringify(payload), 'utf8').toString('base64')}&ip=1`,
|
|
|
|
|
() => { /* Analytics failed. Do nothing. */ });
|
|
|
|
|
});
|
|
|
|
|
}, 100);
|
2018-07-12 10:34:37 +00:00
|
|
|
|
}
|
2016-11-16 14:55:59 +00:00
|
|
|
|
|
2018-07-12 10:34:37 +00:00
|
|
|
|
if (require.main === module) {
|
|
|
|
|
module.exports('Install');
|
|
|
|
|
}
|
2016-11-16 14:55:59 +00:00
|
|
|
|
}
|