Skip to content

Commit c50e013

Browse files
authored
fix: avoid generating error log when getting machine id (#977)
1 parent fd9ba9c commit c50e013

File tree

4 files changed

+68
-13
lines changed

4 files changed

+68
-13
lines changed

packages/schema/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
"langium": "1.2.0",
9191
"lower-case-first": "^2.0.2",
9292
"mixpanel": "^0.17.0",
93-
"node-machine-id": "^1.1.12",
9493
"ora": "^5.4.1",
9594
"pluralize": "^8.0.0",
9695
"pretty-repl": "^4.0.0",

packages/schema/src/telemetry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import sleep from 'sleep-promise';
88
import { CliError } from './cli/cli-error';
99
import { TELEMETRY_TRACKING_TOKEN } from './constants';
1010
import isDocker from './utils/is-docker';
11-
import { getVersion } from './utils/version-utils';
1211
import { getMachineId } from './utils/machine-id-utils';
12+
import { getVersion } from './utils/version-utils';
1313

1414
/**
1515
* Telemetry events

packages/schema/src/utils/machine-id-utils.ts

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,73 @@
1-
import { machineIdSync } from "node-machine-id";
1+
// modified from https://github.com/automation-stack/node-machine-id
2+
3+
import { execSync } from 'child_process';
4+
import { createHash } from 'crypto';
25
import { v4 as uuid } from 'uuid';
36

7+
const { platform } = process;
8+
const win32RegBinPath = {
9+
native: '%windir%\\System32',
10+
mixed: '%windir%\\sysnative\\cmd.exe /c %windir%\\System32',
11+
};
12+
const guid = {
13+
darwin: 'ioreg -rd1 -c IOPlatformExpertDevice',
14+
win32:
15+
`${win32RegBinPath[isWindowsProcessMixedOrNativeArchitecture()]}\\REG.exe ` +
16+
'QUERY HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography ' +
17+
'/v MachineGuid',
18+
linux: '( cat /var/lib/dbus/machine-id /etc/machine-id 2> /dev/null || hostname 2> /dev/null) | head -n 1 || :',
19+
freebsd: 'kenv -q smbios.system.uuid || sysctl -n kern.hostuuid',
20+
};
21+
22+
function isWindowsProcessMixedOrNativeArchitecture() {
23+
// eslint-disable-next-line no-prototype-builtins
24+
if (process.arch === 'ia32' && process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432')) {
25+
return 'mixed';
26+
}
27+
return 'native';
28+
}
29+
30+
function hash(guid: string): string {
31+
return createHash('sha256').update(guid).digest('hex');
32+
}
33+
34+
function expose(result: string): string {
35+
switch (platform) {
36+
case 'darwin':
37+
return result
38+
.split('IOPlatformUUID')[1]
39+
.split('\n')[0]
40+
.replace(/=|\s+|"/gi, '')
41+
.toLowerCase();
42+
case 'win32':
43+
return result
44+
.toString()
45+
.split('REG_SZ')[1]
46+
.replace(/\r+|\n+|\s+/gi, '')
47+
.toLowerCase();
48+
case 'linux':
49+
return result
50+
.toString()
51+
.replace(/\r+|\n+|\s+/gi, '')
52+
.toLowerCase();
53+
case 'freebsd':
54+
return result
55+
.toString()
56+
.replace(/\r+|\n+|\s+/gi, '')
57+
.toLowerCase();
58+
default:
59+
throw new Error(`Unsupported platform: ${process.platform}`);
60+
}
61+
}
62+
463
export function getMachineId() {
5-
// machineIdSync() is not compatible with non-shell hosts such as Vercel
64+
if (!(platform in guid)) {
65+
return uuid();
66+
}
667
try {
7-
return machineIdSync();
68+
const value = execSync(guid[platform as keyof typeof guid]);
69+
const id = expose(value.toString());
70+
return hash(id);
871
} catch {
972
return uuid();
1073
}

pnpm-lock.yaml

Lines changed: 1 addition & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)