Skip to content
Merged
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
140 changes: 7 additions & 133 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@types/jest": "^29.2.4",
"@types/node": "^25.6.2",
"eslint": "^10.3.0",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-prettier": "^5.5.5",
"jiti": "latest",
"globals": "^17.6.0",
Expand All @@ -71,8 +72,7 @@
},
"dependencies": {
"debug": "^4.3.4",
"ini": "^2.0.0",
"regedit": "^5.1.1"
"ini": "^2.0.0"
},
"engines": {
"node": ">=20.17.0",
Expand Down
53 changes: 43 additions & 10 deletions src/utils/registry.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
import regedit from 'regedit';
import { execFile } from 'child_process';
import * as Path from 'path';

// Matches a value line from `reg query`, e.g. " Name REG_SZ data".
const VALUE_LINE = /^ {4}(.*?) {4}(REG_[A-Z_]+)(?: {4}(.*))?$/;

/**
* Returns `undefined` if the key does not exist.
*/
export function readRegistryValue(key: string, subKey: string): Promise<string | undefined> {
if (process.platform !== 'win32') return Promise.resolve(undefined);

const regExe = Path.join(process.env.SystemRoot ?? 'C:\\Windows', 'System32', 'reg.exe');

return new Promise<string | undefined>((resolve, reject) => {
regedit.list([key], (err, result) => {
if (err) {
reject(new Error(`Failed to read registry value ${key} (${err})`));
} else {
const values = result[key]?.values ?? {};
if (subKey in values) {
resolve(values[subKey]!.value as string);
execFile(
regExe,
['query', key, '/v', subKey],
{ windowsHide: true },
(err, stdout, stderr) => {
if (err) {
// reg exits with 1 when the key or value is missing
if (err.code === 1) {
resolve(undefined);
} else {
reject(
new Error(
`Failed to read registry value ${key} (${stderr.trim() || err.message})`
)
);
}
} else {
resolve(undefined);
resolve(parseRegQueryOutput(stdout, subKey));
}
}
});
);
});
}

export function parseRegQueryOutput(stdout: string, valueName: string): string | undefined {
for (const line of stdout.split(/\r?\n/)) {
const match = VALUE_LINE.exec(line);
if (match && match[1]!.toLowerCase() === valueName.toLowerCase()) {
const type = match[2]!;
const data = match[3] ?? '';
// reg prints DWORD/QWORD as hex, callers expect decimal
if (type === 'REG_DWORD' || type === 'REG_QWORD') {
return BigInt(data).toString();
}
return data;
}
}
return undefined;
}
Loading
Loading