-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
96 lines (84 loc) · 2.35 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const {exec} = require("child_process");
const os = require("os");
const fs = require("fs");
const path = require("path");
const UUID = require('uuid');
let uuid;
const uuidRegex = /\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/;
let defaultUuidFolder = os.homedir();
module.exports = function(cb) {
if (cb === undefined) {
return new Promise((resolve, reject)=>{
machineUuid(resolve);
})
}
else machineUuid(cb);
}
function machineUuid(cb) {
if (uuid) { return setImmediate(() => cb(uuid)); }
const platFormSpecific = {
'darwin': osxUuid,
'win32': winUuid,
'win64': winUuid,
'linux': linuxUuid
};
const platformGetUuid = platFormSpecific[os.platform()];
if (platformGetUuid) {
return platformGetUuid(function(err, id) {
if (err) {
return defaultUuid(cb);
} else {
return cb(uuid = id);
}
});
} else {
return defaultUuid(cb);
}
};
var linuxUuid = function(cb) {
try {
uuid = fs.readFile("/var/lib/dbus/machine-id", function(err, content) {
if (content) { // clean, add - and remove whitespace
uuid = content.toString().replace(/\s+/, '');
if ((!/-/.test(uuid)) && (uuid.length > 20)) {
uuid = uuid.slice(0, 8) + '-' + uuid.slice(8, 12) + '-' + uuid.slice(12, 16) + '-' + uuid.slice(16, 20) + '-' + uuid.slice(20);
}
}
return cb(err, content ? uuid : undefined);
});
} catch (e) {
return defaultUuid(cb);
}
};
var osxUuid = cb =>
exec("ioreg -rd1 -c IOPlatformExpertDevice", function(err, stdout, stderr) {
if (err) { return cb(err); }
for (let line of Array.from(stdout.split("\n"))) {
if (/IOPlatformUUID/.test(line) && uuidRegex.test(line)) {
return cb(null, uuidRegex.exec(line)[0]);
}
}
return cb(new Error("No match"));
})
;
var winUuid = cb =>
exec("wmic CsProduct Get UUID", function(err, stdout, stderr) {
if (err) { return cb(err); }
for (let line of Array.from(stdout.split("\n"))) {
if (uuidRegex.test(line)) {
return cb(null, uuidRegex.exec(line)[0]);
}
}
return cb(new Error("No match"));
})
;
var defaultUuid = function(cb) {
const f = path.resolve(defaultUuidFolder, '.nodemid');
if (fs.existsSync(f)) {
return cb(fs.readFileSync(f).toString());
} else {
const id = UUID.v1();
fs.writeFileSync(f, id);
return cb(id);
}
};