| 1 | import * as Context from "effect/Context"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | |
| 4 | /** A 16-byte (128-bit) random hex-encoded string representing an physical instance of a logical resource */ |
| 5 | export class InstanceId extends Context.Service<InstanceId, string>()( |
| 6 | "instance-id", |
| 7 | ) {} |
| 8 | |
| 9 | /** |
| 10 | * @returns Hex-encoded instance ID (16 random bytes) |
| 11 | */ |
| 12 | export const generateInstanceId = () => |
| 13 | Effect.sync(() => { |
| 14 | const bytes = new Uint8Array(16); |
| 15 | crypto.getRandomValues(bytes); |
| 16 | return Array.from(bytes) |
| 17 | .map((b) => b.toString(16).padStart(2, "0")) |
| 18 | .join(""); |
| 19 | }); |