backtrace-node
Version:
Backtrace error reporting tool
135 lines • 4.82 kB
JavaScript
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Aleksandr Komlev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
/* Based on source: https://github.com/Nokel81/node-machine-id/commit/ee2d03efca9e9ccb363e850093a2e6654137275c */
var child_process_1 = require("child_process");
var crypto_1 = require("crypto");
var reg = tslib_1.__importStar(require("native-reg"));
var os_1 = require("os");
var supportedPlatforms = ['darwin', 'linux', 'freebsd', 'win32'];
function getPlatform() {
var platform = process.platform;
if (supportedPlatforms.indexOf(platform) === -1) {
return null;
}
return platform;
}
exports.getPlatform = getPlatform;
var platform = getPlatform();
var guid = {
darwin: 'ioreg -rd1 -c IOPlatformExpertDevice',
linux: '( cat /var/lib/dbus/machine-id /etc/machine-id 2> /dev/null || hostname ) | head -n 1 || :',
freebsd: 'kenv -q smbios.system.uuid || sysctl -n kern.hostuuid',
};
function hash(str) {
return crypto_1.createHash('sha256').update(str).digest('hex');
}
function expose(result) {
switch (platform) {
case 'darwin':
return result
.split('IOPlatformUUID')[1]
.split('\n')[0]
.replace(/\=|\s+|\"/gi, '')
.toLowerCase();
case 'win32':
return result
.toString()
.replace(/\r+|\n+|\s+/gi, '')
.toLowerCase();
case 'linux':
return result
.toString()
.replace(/\r+|\n+|\s+/gi, '')
.toLowerCase();
case 'freebsd':
return result
.toString()
.replace(/\r+|\n+|\s+/gi, '')
.toLowerCase();
default:
return null;
}
}
function windowsMachineId() {
var regVal = reg.getValue(reg.HKEY.LOCAL_MACHINE, 'SOFTWARE\\Microsoft\\Cryptography', 'MachineGuid');
if (regVal) {
return expose(regVal.toString());
}
else {
return null;
}
}
function nonWindowsMachineId() {
try {
if (platform !== null && guid[platform]) {
return expose(child_process_1.execSync(guid[platform]).toString());
}
else {
return null;
}
}
catch (_e) {
return null;
}
}
function generateUuid(name) {
if (name === void 0) { name = os_1.hostname(); }
var uuidSize = 16;
var bytes = name
? Buffer.concat([Buffer.from(name, 'utf8'), Buffer.alloc(uuidSize)], uuidSize)
: crypto_1.pseudoRandomBytes(uuidSize);
return (bytes.slice(0, 4).toString('hex') +
'-' +
bytes.slice(4, 6).toString('hex') +
'-' +
bytes.slice(6, 8).toString('hex') +
'-' +
bytes.slice(8, 10).toString('hex') +
'-' +
bytes.slice(10, 16).toString('hex'));
}
exports.generateUuid = generateUuid;
/**
* This function gets the OS native UUID/GUID synchronously, hashed by default.
* @param {boolean} [original=false] If true return original value of machine id, otherwise return hashed value (sha - 256)
*/
function machineIdSync(original) {
if (original === void 0) { original = false; }
var id = null;
if (platform === 'win32') {
id = windowsMachineId();
}
else if (platform !== null) {
id = nonWindowsMachineId();
}
if (id === null) {
id = generateUuid();
}
return original ? id : hash(id);
}
exports.machineIdSync = machineIdSync;
//# sourceMappingURL=machineId.js.map
;