34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
// UUID helpers for dev-deploy.
|
|
//
|
|
// Two flavors:
|
|
// - randomUuid(): a fresh v4 UUID, used for newly-created ops and entities
|
|
// - deterministicUuid(kind, name): a stable, repeatable UUID derived from
|
|
// (namespace, kind, name) using SHA-256. Used during first-run backfill so
|
|
// that two environments installed from the same base produce identical
|
|
// UUIDs for the same (kind, name) pair. RFC 4122 v5 shape with namespace.
|
|
|
|
const crypto = require("crypto");
|
|
|
|
const { ID_NAMESPACE } = require("./constants");
|
|
|
|
|
|
const deterministicUuid = (kind, name) => {
|
|
const input = `${ID_NAMESPACE}|${kind}|${name}`;
|
|
const hash = crypto.createHash("sha256").update(input).digest();
|
|
const bytes = Buffer.from(hash.subarray(0, 16));
|
|
bytes[6] = (bytes[6] & 0x0f) | 0x50;
|
|
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
const hex = bytes.toString("hex");
|
|
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
|
|
};
|
|
|
|
|
|
const randomUuid = () => {
|
|
return crypto.randomUUID();
|
|
};
|
|
|
|
|
|
module.exports = {
|
|
deterministicUuid,
|
|
randomUuid
|
|
};
|