From 2f3f8c7fcc64b388f7c29342b541e87fdefd6fe1 Mon Sep 17 00:00:00 2001 From: Jan Leon Date: Tue, 30 Jun 2026 02:58:49 +0200 Subject: [PATCH] chore(duplication): share machine id fallback (#5498) --- src/shared/utils/machineId.ts | 77 +++++++++++++---------------------- 1 file changed, 29 insertions(+), 48 deletions(-) diff --git a/src/shared/utils/machineId.ts b/src/shared/utils/machineId.ts index 710d791a1f..db41fcce76 100644 --- a/src/shared/utils/machineId.ts +++ b/src/shared/utils/machineId.ts @@ -91,6 +91,33 @@ function getMachineIdRaw(): string { return "unknown-machine"; } +async function getRandomMachineFallbackId() { + try { + const cryptoFallback = await import("crypto"); + return cryptoFallback.randomUUID(); + } catch { + if (typeof globalThis !== "undefined" && globalThis.crypto && globalThis.crypto.randomUUID) { + return globalThis.crypto.randomUUID(); + } + return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) { + let r = 0; + if ( + typeof globalThis !== "undefined" && + globalThis.crypto && + globalThis.crypto.getRandomValues + ) { + const arr = new Uint8Array(1); + globalThis.crypto.getRandomValues(arr); + r = arr[0] % 16; + } else { + r = (Date.now() % 16) | 0; + } + const v = c === "x" ? r : (r & 0x3) | 0x8; + return v.toString(16); + }); + } +} + /** * Get consistent machine ID using native registry/OS query with salt * This ensures the same physical machine gets the same ID across runs @@ -113,30 +140,7 @@ export async function getConsistentMachineId(salt = null) { } catch (error) { console.log("Error getting machine ID:", error); // Fallback to random ID if node-machine-id fails - try { - const cryptoFallback = await import("crypto"); - return cryptoFallback.randomUUID(); - } catch { - if (typeof globalThis !== "undefined" && globalThis.crypto && globalThis.crypto.randomUUID) { - return globalThis.crypto.randomUUID(); - } - return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) { - let r = 0; - if ( - typeof globalThis !== "undefined" && - globalThis.crypto && - globalThis.crypto.getRandomValues - ) { - const arr = new Uint8Array(1); - globalThis.crypto.getRandomValues(arr); - r = arr[0] % 16; - } else { - r = (Date.now() % 16) | 0; - } - const v = c === "x" ? r : (r & 0x3) | 0x8; - return v.toString(16); - }); - } + return getRandomMachineFallbackId(); } } @@ -150,30 +154,7 @@ export async function getRawMachineId() { } catch (error) { console.log("Error getting raw machine ID:", error); // Fallback to random ID if node-machine-id fails - try { - const cryptoFallback = await import("crypto"); - return cryptoFallback.randomUUID(); - } catch { - if (typeof globalThis !== "undefined" && globalThis.crypto && globalThis.crypto.randomUUID) { - return globalThis.crypto.randomUUID(); - } - return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) { - let r = 0; - if ( - typeof globalThis !== "undefined" && - globalThis.crypto && - globalThis.crypto.getRandomValues - ) { - const arr = new Uint8Array(1); - globalThis.crypto.getRandomValues(arr); - r = arr[0] % 16; - } else { - r = (Date.now() % 16) | 0; - } - const v = c === "x" ? r : (r & 0x3) | 0x8; - return v.toString(16); - }); - } + return getRandomMachineFallbackId(); } }