/** * Serves encrypted event-log replication. * * Encrypted `EventLogRemote` clients use this module when they need a remote * synchronization endpoint that never sees plaintext events. The server stores * encrypted entries and replication metadata keyed by client public key and * store id, then streams encrypted changes back to clients for local * decryption. This module defines the RPC handlers, server layer, storage * contract, and in-memory storage layer for that encrypted server path. * * @stability unstable * @since 4.0.0 */ import * as Arr from "../Array.ts"; import * as Cause from "../Cause.ts"; import * as Context from "../Context.ts"; import * as Duration from "../Duration.ts"; import * as Effect from "../Effect.ts"; import * as Layer from "../Layer.ts"; import type * as Rpc from "../rpc/Rpc.ts"; import type * as RpcGroup from "../rpc/RpcGroup.ts"; import * as RpcServer from "../rpc/RpcServer.ts"; import type * as Scope from "../Scope.ts"; import * as Stream from "../Stream.ts"; import { type EntryId, type RemoteId } from "./EventJournal.ts"; import type { EncryptedBatch, EncryptedRemoteBatch } from "./EventLogEncryption.ts"; import { type EventLogAuthentication, EventLogCompactionRpcs, EventLogRemoteRpcs, type StoreId, type WriteAck } from "./EventLogMessage.ts"; /** * Provides RPC handlers for the encrypted event-log server. * * **Details** * * Decodes and persists writes through `Storage`, acknowledging their stored * sequences. Change streams encode stored batches, excluding this connection's * writes. * * It also serves `EventLogCompactionRpcs`. If `Storage` provides `compaction`, * it sets `HelloResponse.compaction` and applies the compaction policy; * otherwise those handlers fail with `NotFound`. * * @stability unstable * @category layers * @since 4.0.0 */ export declare const layerRpcHandlers: Layer.Layer> | Rpc.ToHandler> | EventLogAuthentication, never, Storage>; /** * Provides an encrypted event-log RPC server using `EventLogRemoteRpcs` and the * encrypted server RPC handlers. * * **When to use** * * Use when you need an encrypted event-log RPC server for encrypted * `EventLogRemote` replication over an existing `RpcServer.Protocol`. * * **Details** * * This layer installs `EventLogRemoteRpcs` on the provided RPC server protocol * and wires those RPCs to `layerRpcHandlers`. Encrypted batches, session * authentication bindings, remote ids, and change streams are delegated to * `Storage`. It also installs `EventLogCompactionRpcs`, which clients call * only when `Storage` provides `compaction`. * * @see {@link layerRpcHandlers} for the encrypted handler layer without installing an RPC server protocol * @see {@link Storage} for the storage service required by this layer * @see {@link layerStorageMemory} for the process-local in-memory storage layer * * @stability unstable * @category layers * @since 4.0.0 */ export declare const layer: Layer.Layer; declare const Storage_base: Context.ServiceClass; /** * Lowest assigned sequence (default 0). Overestimating skips entries. */ readonly firstSequence?: number | undefined; /** * Returns the signing key bound to `publicKey`, binding `signingPublicKey` * when none is bound yet. The server only passes keys whose signature it * has verified, and rejects a key that differs from the returned binding. */ readonly getOrCreateSessionAuthBinding: (publicKey: string, signingPublicKey: Uint8Array) => Effect.Effect>; readonly write: (publicKey: string, storeId: StoreId, batches: ReadonlyArray) => Effect.Effect>; readonly entrySequences: (publicKey: string, storeId: StoreId, entryIds: ReadonlyArray) => Effect.Effect>; readonly changes: (publicKey: string, storeId: StoreId, startSequence: number) => Stream.Stream; readonly compaction?: StorageCompaction | undefined; }>; /** * Defines the backing store service used by the encrypted event-log server. * * **When to use** * * Use to provide durable encrypted event-log persistence for an encrypted * event-log server layer. * * **Details** * * It provides the server remote id, stores session authentication bindings, * persists encrypted batches, and streams encrypted changes for a public key and * store id. * * A batch owns one remote sequence per entry, starting at its `sequence`. * `write` stores a batch unless every one of its entry ids is already stored, * which makes retried writes idempotent, and returns the batches it stored. * `entrySequences` returns the sequence each entry was first stored at, so * skipped batches can still be acknowledged. `changes` streams every batch * whose last sequence is at or after `startSequence`, followed by newly stored * batches. * * Storages that support remote compaction provide `compaction`. With an active * snapshot, `changes` serves snapshot batches by their virtual sequences for a * start sequence at or below the snapshot base, then the originals above it. * * @stability unstable * @category services * @since 4.0.0 */ export declare class Storage extends Storage_base { } /** * Storage primitives for encrypted remote compaction. * * **Details** * * The server owns the policy: trigger threshold, lag, no-op backoff, commit * checks and virtual sequences. Each primitive is atomic per store. * * A store always has one active activation. Before the first commit it is the * originals, with base `firstSequence - 1`. `begin` opens a stage pinned to the * active activation and to `through`. The stage claim expires `stageTtl` after * begin or the latest stored Part, and `begin` takes over an expired claim. * Stage-scoped primitives fail with `CompactionNotFound` once the stage has * expired, been aborted or committed, or when it belongs to another store, so * a commit is answered only once. * * `stagePart` stores one segment of encrypted snapshot batches, or answers * `AlreadyStored` for a segment it already has. It doesn't check entry ids: * the server's commit checks reject repeats, and readers dedupe by id. * `commit` takes every staged batch's virtual * start sequence in position order, and activates the staged set only if the * pinned activation is still active. * * `rollback` reactivates the previous generation under a new activation id * and re-appends, at the head, the input of the stage that built the withdrawn * one: the previous batch set, then the originals between the two bases. * Clients that read the withdrawn snapshot get what it dropped through * `changes`. A sweep on `Clock` prunes a generation, and the originals it * replaced, `grace` after it was superseded or withdrawn. * * @stability unstable * @category models * @since 4.0.0 */ export interface StorageCompaction { readonly config: CompactionConfig; readonly status: (publicKey: string, storeId: StoreId) => Effect.Effect; readonly begin: (publicKey: string, storeId: StoreId, options: { readonly through: number; }) => Effect.Effect; /** * Batch headers in input order: the pinned batch set, then the originals in * `(base, through]`. */ readonly inputHeaders: (publicKey: string, storeId: StoreId, stageId: string) => Stream.Stream; /** * Input batches by their input sequence: snapshot batches at or below the * pinned base, originals above it. */ readonly fetch: (publicKey: string, storeId: StoreId, stageId: string, sequences: ReadonlyArray) => Stream.Stream; readonly stagePart: (publicKey: string, storeId: StoreId, stageId: string, segment: number, batches: Arr.NonEmptyReadonlyArray) => Effect.Effect<"Stored" | "AlreadyStored", CompactionInvalidRequest | CompactionNotFound>; /** * Staged batch headers in position order (segment, then batch index). */ readonly stagedHeaders: (publicKey: string, storeId: StoreId, stageId: string) => Stream.Stream<{ readonly position: number; readonly entryIds: ReadonlyArray; }, CompactionNotFound>; /** * Logs the outcome with this commit's own activation id, not the one active * when the log runs. */ readonly commit: (publicKey: string, storeId: StoreId, stageId: string, virtualSequences: ReadonlyArray) => Effect.Effect<"Committed" | "Superseded", CompactionNotFound>; /** * Records a no-op for the stage and logs it. */ readonly recordNoOp: (publicKey: string, storeId: StoreId, stageId: string) => Effect.Effect; /** * Releases an open stage and discards what it staged. Aborting a stage that * has left `open` is a no-op. */ readonly abort: (publicKey: string, storeId: StoreId, stageId: string) => Effect.Effect; readonly rollback: (publicKey: string, storeId: StoreId, expectedActivationId: string) => Effect.Effect; /** * Every activation of the store, oldest first. */ readonly history: (publicKey: string, storeId: StoreId) => Effect.Effect>; } /** * The compaction state of one store. * * **Details** * * `base` is `firstSequence - 1` while the originals are active. `lagHead` is * the highest last sequence in a storage-time hour that ended before * `now - lag`. * * @stability unstable * @category models * @since 4.0.0 */ export interface CompactionStoreStatus { readonly head: number; readonly base: number; readonly activeKeptCount: number; readonly lagHead: number; readonly noOp: { readonly at: number; readonly count: number; } | undefined; } /** * The result of opening a compaction stage. * * @stability unstable * @category models * @since 4.0.0 */ export type CompactionBeginResult = { readonly _tag: "Started"; readonly stageId: string; } | { readonly _tag: "Busy"; readonly expiresAt: number; }; /** * The header of one compaction input batch. * * @stability unstable * @category models * @since 4.0.0 */ export interface CompactionBatchHeader { readonly sequence: number; readonly entryIds: ReadonlyArray; } /** * One entry of a store's activation log. * * **Details** * * `keptCount` is `undefined` for the originals. `restorable` is `false` once * the generation's prune has started. * * @stability unstable * @category models * @since 4.0.0 */ export interface CompactionActivation { readonly activationId: string; readonly base: number; readonly keptCount: number | undefined; readonly active: boolean; readonly supersededAt: number | undefined; readonly restorable: boolean; } declare const CompactionNotFound_base: new = {}>(args: import("../Types.ts").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => Cause.YieldableError & { readonly _tag: "CompactionNotFound"; } & Readonly; /** * Error raised when a compaction stage has expired, was aborted or committed, * or is unknown to the store. * * @stability unstable * @category errors * @since 4.0.0 */ export declare class CompactionNotFound extends CompactionNotFound_base<{ readonly stageId: string; }> { } declare const CompactionInvalidRequest_base: new = {}>(args: import("../Types.ts").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => Cause.YieldableError & { readonly _tag: "CompactionInvalidRequest"; } & Readonly; /** * Error raised when a compaction Part fails validation. * * @stability unstable * @category errors * @since 4.0.0 */ export declare class CompactionInvalidRequest extends CompactionInvalidRequest_base<{ readonly stageId: string; readonly message: string; }> { } declare const CompactionRollbackError_base: new = {}>(args: import("../Types.ts").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => Cause.YieldableError & { readonly _tag: "CompactionRollbackError"; } & Readonly; /** * Error raised when a rollback is refused. * * **Details** * * `NotActive` means the expected activation is not the active one, * `NoPrevious` that there is no earlier generation, and `PruneStarted` that * the previous generation has started to be pruned. * * @stability unstable * @category errors * @since 4.0.0 */ export declare class CompactionRollbackError extends CompactionRollbackError_base<{ readonly reason: "NotActive" | "NoPrevious" | "PruneStarted"; }> { } /** * Tuning for encrypted remote compaction. * * **Details** * * `grace` is how long a superseded or withdrawn generation is kept before it * is pruned (default 30 days). `lag` is how old data must be, by storage time, * before it can be compacted (default 24 hours). `stageTtl` is how long a stage * claim lasts after its latest Part (default 10 minutes). `thresholdFloor` is * the smallest number of new entries that makes compaction wanted (default * 1,000). `sweepInterval` is how often pruning runs (default 1 hour). * * @stability unstable * @category models * @since 4.0.0 */ export interface CompactionOptions { readonly grace?: Duration.Input | undefined; readonly lag?: Duration.Input | undefined; readonly stageTtl?: Duration.Input | undefined; readonly thresholdFloor?: number | undefined; readonly sweepInterval?: Duration.Input | undefined; } /** * Resolved compaction tuning, shared by a storage and the server using it. * * @stability unstable * @category models * @since 4.0.0 */ export interface CompactionConfig { readonly grace: Duration.Duration; readonly lag: Duration.Duration; readonly stageTtl: Duration.Duration; readonly thresholdFloor: number; readonly sweepInterval: Duration.Duration; } /** * Resolves compaction options against their defaults. * * **Details** * * Dies with an `IllegalArgumentError` unless `grace` is longer than both `lag` * and `stageTtl`, `stageTtl` and `sweepInterval` are positive, and * `thresholdFloor` is a non-negative integer. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const makeCompactionConfig: (options?: CompactionOptions | undefined) => Effect.Effect; /** * Creates an in-memory encrypted server `Storage` with default compaction * tuning. * * **Details** * * Data, session authentication bindings, and streams are process-local and are * released with the surrounding scope. * * @see {@link makeStorageMemoryWith} to configure compaction * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const makeStorageMemory: Effect.Effect; /** * Creates an in-memory encrypted server `Storage`. * * **Details** * * Data, session authentication bindings, and streams are process-local and are * released with the surrounding scope. The storage always provides * `compaction`. Its sweep runs on `Clock` only while some store has a * generation to prune or a stage to expire. * * Dies when the compaction options are invalid (see {@link makeCompactionConfig}). * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const makeStorageMemoryWith: (options: { readonly compaction?: CompactionOptions | undefined; }) => Effect.Effect<{ readonly getId: Effect.Effect; /** * Lowest assigned sequence (default 0). Overestimating skips entries. */ readonly firstSequence?: number | undefined; /** * Returns the signing key bound to `publicKey`, binding `signingPublicKey` * when none is bound yet. The server only passes keys whose signature it * has verified, and rejects a key that differs from the returned binding. */ readonly getOrCreateSessionAuthBinding: (publicKey: string, signingPublicKey: Uint8Array) => Effect.Effect>; readonly write: (publicKey: string, storeId: StoreId, batches: ReadonlyArray) => Effect.Effect>; readonly entrySequences: (publicKey: string, storeId: StoreId, entryIds: ReadonlyArray) => Effect.Effect>; readonly changes: (publicKey: string, storeId: StoreId, startSequence: number) => Stream.Stream; readonly compaction?: StorageCompaction | undefined; }, never, Scope.Scope>; /** * Provides encrypted server `Storage` using the in-memory implementation with * default compaction tuning. * * @see {@link layerStorageMemoryWith} to configure compaction * * @stability unstable * @category layers * @since 4.0.0 */ export declare const layerStorageMemory: Layer.Layer; /** * Provides encrypted server `Storage` using the in-memory implementation. * * @stability unstable * @category layers * @since 4.0.0 */ export declare const layerStorageMemoryWith: (options: { readonly compaction?: CompactionOptions | undefined; }) => Layer.Layer; export {}; //# sourceMappingURL=EventLogServerEncrypted.d.ts.map