Skip to content
Merged
Changes from 1 commit
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
51 changes: 39 additions & 12 deletions src/shared/utils/machineId.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { execFileSync, execSync } from "child_process";
import { existsSync, readFileSync } from "fs";
import os from "os";

/**
* Module-level cache: on Windows, execSync("hostname") spawns
* cmd.exe which is expensive. Cache the result after first call
* since the machine ID never changes at runtime.
*/
let cachedRawId: string | null = null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The introduction of the module-level cachedRawId variable makes it difficult to unit test different strategies or mock the environment across multiple test cases, as the cache cannot be cleared. To improve testability, consider exporting a helper function to reset the cache.

let cachedRawId: string | null = null;

/**
 * Resets the cached machine ID. Used primarily for testing.
 */
export function resetMachineIdCache(): void {
  cachedRawId = null;
}


/**
* Get raw machine ID using OS-specific methods.
Expand All @@ -11,6 +19,9 @@ import { existsSync, readFileSync } from "fs";
* On Linux: skips Windows (REG.exe) and macOS (ioreg) strategies entirely.
*/
function getMachineIdRaw(): string {
// Return cached result immediately (machine identity is stable at runtime)
if (cachedRawId !== null) return cachedRawId;
Comment on lines 29 to +31

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to the Repository Style Guide (Rule 9 under Hard Rules), you must always include tests when changing production code under src/. Since src/shared/utils/machineId.ts has been modified, please add or update the corresponding unit tests in the tests/ directory to cover the new caching behavior and strategy fallback order.

References
  1. Always include tests when changing production code (src/, open-sse/, electron/, bin/). (link)


// Strategy 1: Windows — REG.exe query for MachineGuid
try {
if (process.platform !== "win32") {
Expand All @@ -28,7 +39,10 @@ function getMachineIdRaw(): string {
.split("REG_SZ")[1]
?.replace(/\r+|\n+|\s+/gi, "")
?.toLowerCase();
if (id && id.length > 8) return id;
if (id && id.length > 8) {
cachedRawId = id;
return id;
}
}
} catch {
// Not Windows or REG.exe failed — continue
Expand All @@ -49,7 +63,10 @@ function getMachineIdRaw(): string {
?.split("\n")[0]
?.replace(/=|\s+|"/gi, "")
?.toLowerCase();
if (id && id.length > 8) return id;
if (id && id.length > 8) {
cachedRawId = id;
return id;
}
}
} catch {
// Not macOS or ioreg not available — continue
Expand All @@ -62,7 +79,10 @@ function getMachineIdRaw(): string {
const content = readFileSync(/* turbopackIgnore: true */ filePath, "utf8")
.trim()
.toLowerCase();
if (content.length > 8) return content;
if (content.length > 8) {
cachedRawId = content;
return content;
}
} catch {
// Try the next candidate file
}
Expand All @@ -71,23 +91,30 @@ function getMachineIdRaw(): string {
// Files not readable — continue
}

// Strategy 4: Hostname fallback (works on all platforms)
// Strategy 4: Node.js os.hostname() — no child process, works everywhere
try {
const hostname = execSync("hostname", { encoding: "utf8", timeout: 5000 });
const id = hostname.trim().toLowerCase();
if (id) return id;
const hostname = os.hostname().toLowerCase();
if (hostname) {
cachedRawId = hostname;
return hostname;
}
} catch {
// hostname failed — continue
// os.hostname() not available — continue
}

// Strategy 5: Node.js os.hostname() (no exec needed)
// Strategy 5: execSync("hostname") shell fallback (for constrained environments)
try {
const os = require("os");
return os.hostname().toLowerCase();
const hostname = execSync("hostname", { encoding: "utf8", timeout: 5000 });
const id = hostname.trim().toLowerCase();
if (id) {
cachedRawId = id;
return id;
}
} catch {
// Final fallback
// hostname failed — continue
}

cachedRawId = "unknown-machine";
return "unknown-machine";
}

Expand Down