@eryx/uuid Module

UUID generation, parsing, and formatting.

This module implements the RFC 9562 UUID families v1, v3, v4, v5, v6, v7, and v8, along with parsing, coercion, comparison, and formatting helpers.

UUIDs may be consumed as canonical strings, raw 16-byte buffer values, or metatable-backed UUID objects returned by this module. Parsed and generated UUID objects expose common metadata such as variant, version, and bytes, with additional version-specific fields available on timestamped layouts like v1, v6, and v7.

Version overview:

Textual forms:

local uuid = require("@eryx/uuid")

local id = uuid.v7()
print(id.kind) -- "v7"
print(uuid.format(id)) -- canonical UUID string

local parsed = uuid.parse("01890f3a-b5f7-7c0d-8f50-8f8c2c7adf4d")
print(parsed.kind, parsed.version) -- "v7", 7

Summary

Functions

<anonymous>(bytes: buffer)UUID
UUID:toString(style: UUIDFormatStyle?)string
UUID:toBuffer()buffer
UUID:equals(other: UUIDLike)boolean
UUID:compare(other: UUIDLike)UUIDOrdering
UUID:isNil()boolean
UUID:unixMilliseconds()integer?
uuid.parse(value: string, options: ParseOptions?)UUID
uuid.tryParse(value: string, options: ParseOptions?)UUID?
uuid.from(value: UUIDLike, options: ParseOptions?)UUID
uuid.toBuffer(value: UUIDLike)buffer

API Reference

Functions

<anonymous>

<anonymous>(bytes: buffer)UUID

UUID:toString

UUID:toString(style: UUIDFormatStyle?)string

UUID:toBuffer

Return this UUID formatted as a string.

@param style Optional output style. Defaults to "canonical".

UUID:toBuffer()buffer

Returns

buffer

The formatted UUID string.

UUID:equals

Return a copy of this UUID's raw 16-byte representation.

UUID:equals(other: UUIDLike)boolean

Returns

A new buffer containing the UUID bytes.

UUID:compare

Check whether this UUID equals another UUID-like value.

UUID:compare(other: UUIDLike)UUIDOrdering

Parameters

other: UUIDLike

A UUID object, UUID string, or 16-byte buffer.

Returns

true when both values encode the same bytes.

UUID:isNil

Compare this UUID to another UUID-like value using bytewise ordering.

@param other A UUID object, UUID string, or 16-byte buffer.

UUID:isNil()boolean

Returns

-1, 0, or 1 depending on ordering.

UUID:unixMilliseconds

Check whether this UUID is the all-zero nil UUID.

UUID:unixMilliseconds()integer?

Returns

true when every byte is zero.

UUID.__tostring

Return the recoverable Unix-millisecond timestamp for timestamped UUIDs.

For v1 and v6, this is derived from the embedded Gregorian 100ns timestamp. For v7, it is the native Unix-millisecond field. Other UUID versions return nil.

UUID.__tostring(self: UUID)string

Returns

Unix milliseconds, or nil when not applicable.

uuid.parse

Parse a UUID string into a UUID object.

Canonical hyphenated strings are always accepted. Hyphenless, braced, and URN forms may be enabled through options.

uuid.parse(value: string, options: ParseOptions?)UUID

Parameters

value: string

UUID string to parse.

options: ParseOptions?

Optional parse flags controlling accepted textual forms.

Returns

Parsed UUID object with recovered metadata where available.

uuid.tryParse

Parse a UUID string, returning nil instead of raising on invalid input.

uuid.tryParse(value: string, options: ParseOptions?)UUID?

Parameters

value: string

UUID string to parse.

options: ParseOptions?

Optional parse flags controlling accepted textual forms.

Returns

Parsed UUID object, or nil when parsing fails.

uuid.from

Coerce a UUID-like value into a UUID object.

Existing UUID objects are returned as-is. Strings are parsed, and 16-byte buffers are decoded directly.

uuid.from(value: UUIDLike, options: ParseOptions?)UUID

Parameters

value: UUIDLike

UUID object, UUID string, or 16-byte buffer.

options: ParseOptions?

Optional parse flags used when value is a string.

Returns

Normalized UUID object.

uuid.validate

Return whether a UUID-like value can be successfully coerced.

uuid.validate(value: UUIDLike, options: ParseOptions?)boolean

Parameters

value: UUIDLike

UUID object, UUID string, or 16-byte buffer.

options: ParseOptions?

Optional parse flags used when value is a string.

Returns

true when coercion succeeds.

uuid.format

Format a UUID-like value into a textual representation.

uuid.format(value: UUIDLike, style: UUIDFormatStyle?)string

Parameters

value: UUIDLike

UUID object, UUID string, or 16-byte buffer.

Optional output style. Defaults to "canonical".

Returns

Formatted UUID string.

uuid.toBuffer

Return a copy of a UUID-like value as a raw 16-byte buffer.

uuid.toBuffer(value: UUIDLike)buffer

Parameters

value: UUIDLike

UUID object, UUID string, or 16-byte buffer.

Returns

buffer

New buffer containing the UUID bytes.

uuid.equals

Check whether two UUID-like values are bytewise equal.

uuid.equals(a: UUIDLike, b: UUIDLike)boolean

Parameters

Left-hand UUID value.

Right-hand UUID value.

Returns

true when both values encode the same UUID.

uuid.compare

Compare two UUID-like values using raw byte ordering.

This is useful for sortable UUID families such as v6 and v7, or when storing UUIDs in ordered indexes.

uuid.compare(a: UUIDLike, b: UUIDLike)UUIDOrdering

Parameters

Left-hand UUID value.

Right-hand UUID value.

Returns

-1, 0, or 1 depending on ordering.

uuid.v1

Generate a version 1 UUID.

When not explicitly supplied, the node and clock sequence are derived from cryptographically secure random bytes. The nanoseconds field represents the sub-millisecond portion of the timestamp and must be a multiple of 100.

uuid.v1(options: UUIDv1Options?)UUIDv1

Parameters

options: UUIDv1Options?

Optional generation controls.

Returns

Generated version 1 UUID.

uuid.v3

Generate a version 3 namespace-name UUID using MD5.

uuid.v3(options: UUIDv3Options)UUIDv3

Parameters

options: UUIDv3Options

Namespace and name to hash.

Returns

Generated version 3 UUID.

uuid.v4

Generate a version 4 random UUID.

By default this uses @eryx/crypto/hazmat/random.bytes.

uuid.v4(options: UUIDv4Options?)UUIDv4

Parameters

options: UUIDv4Options?

Optional RNG override.

Returns

Generated version 4 UUID.

uuid.v5

Generate a version 5 namespace-name UUID using SHA-1.

uuid.v5(options: UUIDv5Options)UUIDv5

Parameters

options: UUIDv5Options

Namespace and name to hash.

Returns

Generated version 5 UUID.

uuid.v6

Generate a version 6 reordered time-based UUID.

This uses the same Gregorian 100ns timestamp source model as v1, but reorders the bytes to improve sort locality.

uuid.v6(options: UUIDv6Options?)UUIDv6

Parameters

options: UUIDv6Options?

Optional generation controls.

Returns

Generated version 6 UUID.

uuid.v7

Generate a version 7 Unix-time-ordered UUID.

When sequence is omitted, this module applies monotonic sequencing so that repeated calls remain bytewise ordered even within the same millisecond.

uuid.v7(options: UUIDv7Options?)UUIDv7

Parameters

options: UUIDv7Options?

Optional generation controls.

Returns

Generated version 7 UUID.

uuid.v8

Generate a version 8 UUID from caller-supplied bytes.

The provided payload must be exactly 16 bytes long. The version and variant bits are normalized to match a valid v8 UUID.

uuid.v8(options: UUIDv8Options)UUIDv8

Parameters

options: UUIDv8Options

Raw payload bytes to wrap as a version 8 UUID.

Returns

Generated version 8 UUID.

Types

UUIDVariant

type UUIDVariant = "ncs" | "rfc4122" | "microsoft" | "future"

UUIDVersionName

type UUIDVersionName = "v1" | "v3" | "v4" | "v5" | "v6" | "v7" | "v8"

UUIDFormatStyle

type UUIDFormatStyle = "canonical" | "simple" | "urn" | "braced"

UUIDOrdering

Implements: number

RandomSource

type RandomSource = ((byteCount: number) → buffer)

UUIDUnknown

type UUIDUnknown = UUIDBase & { kind: nil }
Implements: UUIDBase
kind: nil

UUIDv1

type UUIDv1 = UUIDBase & { kind: "v1", version: number, timestamp: integer, unixMillis: integer, clockSequence: number, node: buffer }
Implements: UUIDBase
kind: "v1"
version: number
timestamp: integer

Gregorian timestamp in 100ns intervals since 1582-10-15.

unixMillis: integer

Convenience timestamp in Unix milliseconds.

clockSequence: number

14-bit clock sequence.

node: buffer

48-bit node identifier as 6 bytes.

UUIDv3

type UUIDv3 = UUIDBase & { kind: "v3", version: number }
Implements: UUIDBase
kind: "v3"
version: number

UUIDv4

type UUIDv4 = UUIDBase & { kind: "v4", version: number }
Implements: UUIDBase
kind: "v4"
version: number

UUIDv5

type UUIDv5 = UUIDBase & { kind: "v5", version: number }
Implements: UUIDBase
kind: "v5"
version: number

UUIDv6

type UUIDv6 = UUIDBase & { kind: "v6", version: number, timestamp: integer, unixMillis: integer, clockSequence: number, node: buffer }
Implements: UUIDBase
kind: "v6"
version: number
timestamp: integer

Reordered Gregorian timestamp in 100ns intervals since 1582-10-15.

unixMillis: integer

Convenience timestamp in Unix milliseconds.

clockSequence: number

14-bit clock sequence.

node: buffer

48-bit node identifier as 6 bytes.

UUIDv7

type UUIDv7 = UUIDBase & { kind: "v7", version: number, unixMillis: integer, randA: number, randB: buffer }
Implements: UUIDBase
kind: "v7"
version: number
unixMillis: integer

Milliseconds since the Unix epoch.

randA: number

The 12-bit rand_a field.

randB: buffer

The trailing 64-bit payload as 8 bytes, including variant bits.

UUIDv8

type UUIDv8 = UUIDBase & { kind: "v8", version: number }
Implements: UUIDBase
kind: "v8"
version: number

UUID

UUIDLike

type UUIDLike = UUID | string | buffer

ParseOptions

type ParseOptions = { allowSimple: boolean?, allowBraced: boolean?, allowUrn: boolean? }
allowSimple: boolean?
allowBraced: boolean?
allowUrn: boolean?

UUIDv1Options

type UUIDv1Options = { unixMillis: integer?, nanoseconds: number?, clockSequence: number?, node: buffer | string?, random: RandomSource? }
unixMillis: integer?
nanoseconds: number?
clockSequence: number?
node: buffer | string?
random: RandomSource?

UUIDv3Options

type UUIDv3Options = { namespace: UUIDLike, name: string | buffer }
namespace: UUIDLike
name: string | buffer

UUIDv4Options

type UUIDv4Options = { random: RandomSource? }
random: RandomSource?

UUIDv5Options

type UUIDv5Options = { namespace: UUIDLike, name: string | buffer }
namespace: UUIDLike
name: string | buffer

UUIDv6Options

type UUIDv6Options = { unixMillis: integer?, nanoseconds: number?, clockSequence: number?, node: buffer | string?, random: RandomSource? }
unixMillis: integer?
nanoseconds: number?
clockSequence: number?
node: buffer | string?
random: RandomSource?

UUIDv7Options

type UUIDv7Options = { unixMillis: integer?, sequence: number?, random: RandomSource? }
unixMillis: integer?
sequence: number?
random: RandomSource?

UUIDv8Options

type UUIDv8Options = { bytes: buffer }
bytes: buffer