@glandjs/events
Version:
A fast, zero‑dependency event broker and message bus for building scalable, event‑driven applications.
28 lines (27 loc) • 1.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateUUID = generateUUID;
function generateUUID() {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID();
}
const getRandomValues = (buffer) => {
if (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function') {
return crypto.getRandomValues(buffer);
}
for (let i = 0; i < buffer.length; i++) {
buffer[i] = Math.floor(Math.random() * 256);
}
return buffer;
};
const rnds = new Uint8Array(16);
getRandomValues(rnds);
rnds[6] = (rnds[6] & 0x0f) | 0x40;
rnds[8] = (rnds[8] & 0x3f) | 0x80;
const hexBytes = [];
for (let i = 0; i < rnds.length; i++) {
const hex = rnds[i].toString(16).padStart(2, '0');
hexBytes.push(hex);
}
return [hexBytes.slice(0, 4).join(''), hexBytes.slice(4, 6).join(''), hexBytes.slice(6, 8).join(''), hexBytes.slice(8, 10).join(''), hexBytes.slice(10, 16).join('')].join('-');
}