Skip to content

Switch windows to using native-reg to avoid permissions #57

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
86 changes: 85 additions & 1 deletion dist/index.js

Large diffs are not rendered by default.

80 changes: 0 additions & 80 deletions index.js

This file was deleted.

87 changes: 87 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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+|\"/ig, '')
.toLowerCase();
case 'win32':
return result
.toString()
.split('REG_SZ')[1]
.replace(/\r+|\n+|\s+/ig, '')
.toLowerCase();
case 'linux':
return result
.toString()
.replace(/\r+|\n+|\s+/ig, '')
.toLowerCase();
case 'freebsd':
return result
.toString()
.replace(/\r+|\n+|\s+/ig, '')
.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));
});
});
}
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
},
"homepage": "https://github.com/automation-stack/node-machine-id#readme",
"scripts": {
"build": "WEBPACK=node_modules/webpack/bin/webpack.js && $WEBPACK --config webpack.config.babel.js",
"build": "tsc",
"prepublish": "npm run test",
"lint": "node_modules/eslint/bin/eslint.js -c .eslintrc ./src",
"test": "node_modules/mocha/bin/mocha --compilers js:babel-core/register ./tests"
"lint": "npm bin && eslint -c .eslintrc ./src",
"test": "npm bin && mocha --compilers js:babel-core/register ./tests"
},
"devDependencies": {
"@types/node": "^16.0.1",
"babel-cli": "^6.18.0",
"babel-core": "^6.21.0",
"babel-eslint": "^7.1.1",
Expand Down Expand Up @@ -70,5 +71,8 @@
"raw-loader": "^0.5.1",
"source-map-support": "^0.4.0",
"webpack": "1.14.0"
},
"dependencies": {
"native-reg": "^0.3.5"
}
}
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"outDir": "./dist",
"moduleResolution": "node",
"target": "es5",
"module": "commonjs",
"sourceMap": false,
"declarationDir": "./types",
"declaration": true,
}
}
28 changes: 12 additions & 16 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/**
* Module based on OS native UUID/GUID which used for internal needs.
/**
* 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)
*/
declare module 'node-machine-id' {

/**
* 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?: boolean): string;

/**
* This function gets the OS native UUID/GUID asynchronously (recommended), hashed by default.
* @param {boolean} [original=false] - If true return original value of machine id, otherwise return hashed value (sha - 256)
*/
function machineId(original?: boolean): Promise<string>;
}
export declare function machineIdSync(original?: boolean): string;
/**
* 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 declare function machineId(original?: boolean): Promise<string>;