Skip to content

native-reg changes #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion dist/index.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions dist/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { machineId, machineIdSync } = require(".");

const mid = machineIdSync();
console.log("machine sync id", mid);

machineId().then((mid) => {
console.log("machine id", mid);
});
80 changes: 0 additions & 80 deletions index.js

This file was deleted.

98 changes: 98 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { exec, execSync } from "child_process";
import { createHash } from "crypto";

const { platform } = process;
const reg = platform === "win32" ? require("native-reg") : null;
const 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(guid: string): string {
return createHash("sha256").update(guid).digest("hex");
}

function expose(result: string): string {
switch (platform) {
case "darwin":
return result
.split("IOPlatformUUID")[1]
.split("\n")[0]
.replace(/\=|\s+|\"/gi, "")
.toLowerCase();
case "win32":
return result
.toString()
.split("REG_SZ")[1]
.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:
throw new Error(`Unsupported platform: ${process.platform}`);
}
}

function windowsMachineId(): string {
return reg
.getValue(
reg.HKEY.LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Cryptography",
"MachineGuid"
)
.toString();
}

/**
* 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)
*/
export function machineIdSync(original: boolean = false): string {
const id =
platform === "win32"
? windowsMachineId()
: expose(execSync(guid[platform]).toString());

return original ? id : hash(id);
}

/**
* This function gets the OS native UUID/GUID asynchronously (recommended), hashed by default.
*
* Note: on windows this is still synchronous
* @param {boolean} [original=false] If true return original value of machine id, otherwise return hashed value (sha - 256)
*
*/
export function machineId(original: boolean = false): Promise<string> {
return new Promise((resolve: Function, reject: Function): Object => {
if (platform === "win32") {
try {
return resolve(windowsMachineId());
} catch (error) {
return reject(error);
}
}

return exec(guid[platform], {}, (err: any, stdout: any, stderr: any) => {
if (err) {
return reject(
new Error(`Error while obtaining machine id: ${err.stack}`)
);
}

const id = expose(stdout.toString());

return resolve(original ? id : hash(id));
});
});
}
Loading