Generate a UUID v4 string or write the bytes into a buffer.UUID v4 is a purely random UUID with 122 bits of entropy. It’s the most widely compatible UUID format, supported by virtually all databases and systems. Use when you need maximum compatibility and don’t require time-ordering.
Efficiently write multiple UUIDs to a pre-allocated buffer:
const buffer = new Uint8Array(64) // Space for 4 UUIDsuuidv4(undefined, buffer, 0) // First UUID at offset 0uuidv4(undefined, buffer, 16) // Second UUID at offset 16uuidv4(undefined, buffer, 32) // Third UUID at offset 32uuidv4(undefined, buffer, 48) // Fourth UUID at offset 48
export type UuidV4Options = { /** * 16 bytes of random data to use for UUID generation. * Note: Bytes at index 6 and 8 will be modified in-place to set version/variant bits. */ random?: Uint8Array}export type UuidV4 = { (): string <TBuf extends Uint8Array = Uint8Array>(options: UuidV4Options | undefined, buf: TBuf, offset?: number): TBuf (options?: UuidV4Options, buf?: undefined, offset?: number): string toBytes(id: string): Uint8Array fromBytes(bytes: Uint8Array): string isValid(id: unknown): id is string /** The nil UUID (all zeros) */ NIL: string /** The max UUID (all ones) */ MAX: string}