@maximai/maxim-js
Version:
Maxim AI JS SDK. Visit https://getmaxim.ai for more info.
39 lines • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAllKeysByValue = void 0;
exports.generateUniqueId = generateUniqueId;
exports.generateCuid = generateCuid;
const platform_1 = require("../platform");
const secureRandom_1 = require("./secureRandom");
function generateUniqueId() {
const timestamp = Date.now().toString(36); // Convert timestamp to base 36 string
const hostname = platform_1.platform.crypto.hostname(); // Get the hostname
const randomPart = (0, secureRandom_1.getRandomHex)(8); // Generate 8 hex chars (4 bytes)
return `${timestamp}-${hostname}-${randomPart}`;
}
const getAllKeysByValue = (obj, value) => {
return Object.keys(obj).filter((key) => obj[key] === value);
};
exports.getAllKeysByValue = getAllKeysByValue;
/**
* Generates a collision-resistant unique identifier (CUID)
* Format: c<timestamp><counter><fingerprint><random>
* @returns A unique identifier string
*/
function generateCuid() {
// Current timestamp
const timestamp = Date.now().toString(36);
// Process-specific counter to improve uniqueness
const counter = Math.floor(Math.random() * 1000)
.toString(36)
.padStart(2, "0");
// Generate cryptographically secure random bytes
const randomPart = (0, secureRandom_1.getRandomHex)(8); // 4 bytes as hex
// Get hostname or machine fingerprint (reduced to avoid full hostname)
const fingerprint = platform_1.platform.crypto.createHash("md5").update(platform_1.platform.crypto.hostname()).digest("hex").slice(0, 2);
// Combine parts to create CUID
const prefix = "c";
const cuid = [prefix, timestamp, counter, fingerprint, randomPart].join("");
return cuid;
}
//# sourceMappingURL=utils.js.map