class / @typed/id/RandomValues

RandomValues

Effect service that produces fresh byte arrays of an exact requested length.

Package version
1.0.0-beta.13
Category
Entropy services
Since
1.0.0

Import

import { RandomValues } from "@typed/id/RandomValues";

Signatures

export declare class RandomValues extends RandomValues_base {
    static readonly call: <const N extends number>(length: N) => Effect.Effect<Uint8Array & {
        readonly length: N;
    }, never, RandomValues>;
    static readonly Default: Layer.Layer<RandomValues, never, never>;
    static readonly Random: Layer.Layer<RandomValues, never, never>;
}

Why

Entropy is an explicit dependency so secure production generation and reproducible tests use the same typed generator APIs. RandomValues.Default requires globalThis.crypto.getRandomValues; an unavailable implementation or thrown platform error is an Effect defect because the service’s typed error channel is never.

Ownership and lifetime

A Layer owns the entropy source; every call allocates and transfers ownership of a fresh mutable byte array to the caller.

Property: Default

Provides cryptographic bytes from globalThis.crypto.getRandomValues.

Property: Default: Why

Production IDs need platform entropy. Missing globalThis.crypto.getRandomValues and platform exceptions are Effect defects, not typed errors, rather than silently weakening randomness.

Property: Default: Ownership and lifetime

The Layer owns no mutable generator state; each request returns a fresh buffer. The runtime must provide Web Crypto or the request defects.

Property: Random

Provides reproducible bytes from Effect Random.

Property: Random: Why

Deterministic tests and simulations need replaceable entropy; this Layer is not cryptographically secure.

Property: Random: Ownership and lifetime

The provided Effect Random service owns sequence state for the Layer lifetime; each request returns a fresh buffer.

Property: call

Requests a fresh byte array from the current RandomValues service.

Property: call: Why

The static call preserves literal length in the type while keeping the entropy source in Effect’s service channel.

Property: call: Ownership and lifetime

Each invocation allocates a new buffer owned by the caller and acquires no persistent resource.

Examples

import { RandomValues } from "@typed/id/RandomValues"
import { Effect } from "effect"
const bytes = Effect.provide(RandomValues.call(16), RandomValues.Default)
import { RandomValues } from "@typed/id/RandomValues"
import { Effect } from "effect"
const bytes = RandomValues.call(32).pipe(Effect.provide(RandomValues.Default))

Other public imports

These import paths expose the same declaration. Each page retains its own public name and signature.

Source