69 lines
1.4 KiB
JavaScript
69 lines
1.4 KiB
JavaScript
"use strict";
|
|
|
|
// CommonJS shim for Pulsar/Atom (compile-cache) which cannot load ESM-only
|
|
// uuid versions. This package is used in the Jibo SDK as `require('uuid').v4()`.
|
|
|
|
var crypto;
|
|
try {
|
|
crypto = require("crypto");
|
|
} catch (e) {
|
|
crypto = null;
|
|
}
|
|
|
|
function randomBytes(size) {
|
|
if (crypto && typeof crypto.randomBytes === "function") {
|
|
return crypto.randomBytes(size);
|
|
}
|
|
|
|
// Extremely small fallback (should rarely be used).
|
|
var buf = Buffer.alloc(size);
|
|
for (var i = 0; i < size; i++) {
|
|
buf[i] = (Math.random() * 256) | 0;
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
function byteToHex(byte) {
|
|
return (byte + 0x100).toString(16).slice(1);
|
|
}
|
|
|
|
function unsafeV4() {
|
|
var bytes = randomBytes(16);
|
|
|
|
// Per RFC 4122 / RFC 9562: set version 4 and variant.
|
|
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
|
|
// Format 8-4-4-4-12
|
|
return (
|
|
byteToHex(bytes[0]) +
|
|
byteToHex(bytes[1]) +
|
|
byteToHex(bytes[2]) +
|
|
byteToHex(bytes[3]) +
|
|
"-" +
|
|
byteToHex(bytes[4]) +
|
|
byteToHex(bytes[5]) +
|
|
"-" +
|
|
byteToHex(bytes[6]) +
|
|
byteToHex(bytes[7]) +
|
|
"-" +
|
|
byteToHex(bytes[8]) +
|
|
byteToHex(bytes[9]) +
|
|
"-" +
|
|
byteToHex(bytes[10]) +
|
|
byteToHex(bytes[11]) +
|
|
byteToHex(bytes[12]) +
|
|
byteToHex(bytes[13]) +
|
|
byteToHex(bytes[14]) +
|
|
byteToHex(bytes[15])
|
|
);
|
|
}
|
|
|
|
function v4() {
|
|
return unsafeV4();
|
|
}
|
|
|
|
module.exports = {
|
|
v4: v4,
|
|
};
|