/** * A compact binary codec derived from the encoded side of a Schema. * * The default wire format supports compatible schema evolution. An array of * structs is written as a row run: rows declare their shape once and * back-reference repeated strings, so both stay off the wire on later rows. * Fingerprint mode uses positional layouts and rejects mismatches for smaller * frames; its row shapes are presence masks instead of field id lists. Encoded * results may be views into a shared buffer; see {@link toCodec} for ownership * details. * * @stability unstable * @since 4.0.0 */ import * as Arr from "../Array.ts"; import * as Channel from "../Channel.ts"; import * as Effect from "../Effect.ts"; import * as Schema from "../Schema.ts"; import * as SchemaAST from "../SchemaAST.ts"; /** * Selects the wire mode. * * **Details** * * The default mode supports compatible schema evolution. `fingerprint: true` * uses positional layouts and an 8-byte layout hash for smaller frames, but * requires peers to use an identical encoded layout. * * @stability unstable * @category models * @since 4.0.0 */ export interface Options { /** * Uses a compact positional layout with a schema fingerprint. * * @stability unstable * @since 4.0.0 */ readonly fingerprint?: boolean | undefined; } /** * The codec type returned by {@link toCodec}. * * @stability unstable * @category models * @since 4.0.0 */ export interface toCodec extends Schema.Codec, S["DecodingServices"], S["EncodingServices"]> { } /** * Derives a compact binary codec from a schema. * * **Details** * * The wire layout is compiled from the encoded side of the schema. Each * encode/decode handles exactly one frame; use {@link parser} for streams. * Derived codecs are memoized by schema identity and wire mode. * * Encoded results may be views into a larger shared buffer. Use * `bytes.slice()` when independent ownership is required. * * **Gotchas** * * Bytes carry the layout of the schema that wrote them. In the default mode, * decode them with that schema or a compatible evolution of it; fingerprint * mode requires an identical encoded layout. Most union codecs prefix values * with a member selector, so they cannot read bytes written by a member's own * codec. * * `Uint8Array` values of 16 KiB or more are copied when the encode call * returns, not when they are reached, so they must not change until then. If * such a value's length changes before then, for example because its buffer * is detached, the encode fails with a `SchemaError`. * * **Example** (Deriving a binary codec) * * ```ts * import { Schema } from "effect" * import { SchemaBinary } from "effect/encoding" * * const Person = Schema.Struct({ name: Schema.String, age: Schema.Number }) * const codec = SchemaBinary.toCodec(Person) * * const bytes = Schema.encodeUnknownSync(codec)({ name: "Ada", age: 36 }) * const person = Schema.decodeUnknownSync(codec)(bytes) * ``` * * @stability unstable * @category constructors * @since 4.0.0 */ export declare function toCodec(schema: S, options?: Options): toCodec; /** * A stateful frame parser for concatenated {@link toCodec} outputs. * * @stability unstable * @category models * @since 4.0.0 */ export interface Parser { /** * Feeds another chunk into the parser and returns any completed values. * * @stability unstable * @since 4.0.0 */ feed(chunk: Uint8Array): Effect.Effect, Schema.SchemaError>; /** * Feeds another chunk into the parser synchronously. * * @stability unstable * @since 4.0.0 */ feedSync(chunk: Uint8Array): ReadonlyArray; /** * Finishes parsing and fails if an incomplete frame remains. * * @stability unstable * @since 4.0.0 */ end: Effect.Effect; /** * Finishes parsing synchronously and throws for an incomplete frame. * * @stability unstable * @since 4.0.0 */ endSync(): void; } /** * Creates a stateful parser for a stream of concatenated frames. * * **Details** * * Values completed before a failure remain observable. After a failure, the * parser rejects further calls. Use `maxFrameSize` to limit buffered frames. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare function parser(schema: S, options?: SchemaAST.ParseOptions & Options & StreamOptions & { readonly maxFrameSize?: number | undefined; }): Parser; /** * Options for the connection-scoped pair, {@link encoder} and {@link parser}. * * @stability unstable * @category models * @since 4.0.0 */ export interface StreamOptions { /** * Share one string dictionary across every frame on the connection, so a * string that repeats costs a reference after the first frame that carries * it. Frames stop standing alone: they only decode through the parser that * saw the frames before them, in order. * * **Gotchas** * * Both ends have to set it. A schema the binary layer does not fully * validate on its own cannot use it, and asking for it throws. * * @stability unstable * @since 4.0.0 */ readonly dictionary?: boolean | undefined; } /** * A writer for one connection. Every frame it produces shares the string * dictionary that the matching {@link parser} rebuilds as it reads them, so the * two have to be created from the same schema and the same options. * * **Gotchas** * * As with {@link toCodec}, `Uint8Array` values of 16 KiB or more are copied * when the call returns. For `encodeMany`, that is after the whole batch is * written, so no value in the batch may change until the call returns. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare function encoder(schema: S, options?: SchemaAST.ParseOptions & Options & StreamOptions): Encoder; /** * The writer returned by {@link encoder}. * * @stability unstable * @category models * @since 4.0.0 */ export interface Encoder { encode(value: unknown): Uint8Array; encodeMany(values: ReadonlyArray): Uint8Array; } /** * Creates a channel that encodes chunks of schema values into binary frames. * * **Details** * * Each input chunk is emitted as one byte element holding concatenated frames * written in one writer pass, so a batch costs no more allocations than a * single frame. Schemas with transformations first run one schema pass per * chunk to the binary-adjusted encoded side — supporting async transformations * and encoding services — before that writer pass. Failures are * `Schema.SchemaError`, matching {@link toCodec}. `maxFrameSize` only applies * to {@link decode} and is ignored here. * * @stability unstable * @category channels * @since 4.0.0 */ export declare const encode: (schema: S, options?: SchemaAST.ParseOptions & Options & { readonly maxFrameSize?: number | undefined; }) => () => Channel.Channel>, Schema.SchemaError | IE, Done, Arr.NonEmptyReadonlyArray, IE, Done, S["EncodingServices"]>; /** * Creates a channel that decodes chunks of binary frames into schema values. * * **Details** * * The channel keeps one frame parser for its lifetime: frames may be * fragmented across chunks or concatenated within one, values completed before * a failure remain observable, and a leftover incomplete frame fails when the * upstream is done. Schemas with transformations run the schema pass per * framed value, which supports async transformations and decoding services. * Failures are `Schema.SchemaError`, matching {@link toCodec}. * * @stability unstable * @category channels * @since 4.0.0 */ export declare const decode: (schema: S, options?: SchemaAST.ParseOptions & Options & { readonly maxFrameSize?: number | undefined; }) => () => Channel.Channel, Schema.SchemaError | IE, Done, Arr.NonEmptyReadonlyArray>, IE, Done, S["DecodingServices"]>; /** * Wraps a bidirectional byte channel with schema-driven binary encoding and * decoding. * * **Details** * * Values sent to the wrapped channel are encoded with `inputSchema` as binary * frames; bytes received from it are decoded with `outputSchema`. * * @stability unstable * @category channels * @since 4.0.0 */ export declare const duplex: { /** * Wraps a bidirectional byte channel with schema-driven binary encoding and * decoding. * * **Details** * * Values sent to the wrapped channel are encoded with `inputSchema` as binary * frames; bytes received from it are decoded with `outputSchema`. * * @stability unstable * @category channels * @since 4.0.0 */ (options: SchemaAST.ParseOptions & Options & { readonly inputSchema: In; readonly outputSchema: Out; readonly maxFrameSize?: number | undefined; }): (self: Channel.Channel>, OutErr, OutDone, Arr.NonEmptyReadonlyArray>, Schema.SchemaError | InErr, InDone, R>) => Channel.Channel, Schema.SchemaError | OutErr, OutDone, Arr.NonEmptyReadonlyArray, InErr, InDone, R | In["EncodingServices"] | Out["DecodingServices"]>; /** * Wraps a bidirectional byte channel with schema-driven binary encoding and * decoding. * * **Details** * * Values sent to the wrapped channel are encoded with `inputSchema` as binary * frames; bytes received from it are decoded with `outputSchema`. * * @stability unstable * @category channels * @since 4.0.0 */ (self: Channel.Channel>, OutErr, OutDone, Arr.NonEmptyReadonlyArray>, Schema.SchemaError | InErr, InDone, R>, options: SchemaAST.ParseOptions & Options & { readonly inputSchema: In; readonly outputSchema: Out; readonly maxFrameSize?: number | undefined; }): Channel.Channel, Schema.SchemaError | OutErr, OutDone, Arr.NonEmptyReadonlyArray, InErr, InDone, R | In["EncodingServices"] | Out["DecodingServices"]>; }; /** * Assigns an explicit wire field id to a struct property. * * **When to use** * * Use to preserve the wire id across a rename or resolve a hash collision. * * **Details** * * Valid ids are integers from 1 through 4294967295. * * **Example** (Assigning a wire field id) * * ```ts * import { Schema } from "effect" * import { SchemaBinary } from "effect/encoding" * * const Person = Schema.Struct({ * id: Schema.String.pipe(SchemaBinary.fieldId(1)) * }) * ``` * * @stability unstable * @category annotations * @since 4.0.0 */ export declare function fieldId(id: number): (self: S) => S["Rebuild"]; /** * A 32-bit signed integer written as four little-endian bytes. * * **When to use** * * Use when values regularly need more than four bytes as a varint, such as * hashes or full-range ids. Small values are cheaper as `Schema.Int`. * * **Details** * * Rejects non-integers and values outside -2147483648 through 2147483647 * instead of truncating them. `-0` is written as `0`. * * **Gotchas** * * Switching a field between this, {@link uint32}, `Schema.Number` or * `Schema.Int` changes its encoding, so it is not a compatible evolution. In * default mode the other reader can decode a different value without an error, * for example after a signedness swap or inside an array of structs. * Fingerprint mode rejects the layout mismatch. * * **Example** (Declaring fixed-width integer fields) * * ```ts * import { Schema } from "effect" * import { SchemaBinary } from "effect/encoding" * * const Packet = Schema.Struct({ * version: SchemaBinary.uint32, * offset: SchemaBinary.int32, * sequence: SchemaBinary.uint64, * timestamp: SchemaBinary.int64 * }) * ``` * * @stability unstable * @category schemas * @since 4.0.0 */ export declare const int32: Schema.Number; /** * A 32-bit unsigned integer written as four little-endian bytes. * * **Details** * * Rejects non-integers and values outside 0 through 4294967295 instead of * wrapping them. See {@link int32} for when a fixed width pays off. * * @stability unstable * @category schemas * @since 4.0.0 */ export declare const uint32: Schema.Number; /** * A 64-bit signed integer, as a `bigint`, written as eight little-endian bytes. * * **Details** * * Rejects values outside -2^63 through 2^63 - 1 instead of wrapping them. As a * struct field it costs one length byte more than the payload, because the * eight-byte field kind belongs to floating-point numbers. * * **Gotchas** * * Switching a field between this, {@link uint64} or `Schema.BigInt` is not a * compatible evolution. All three are length-prefixed struct fields, and eight * bytes can be valid for each, so a default-mode reader may decode the other * form as a different value without an error. Fingerprint mode rejects the * layout mismatch. * * @stability unstable * @category schemas * @since 4.0.0 */ export declare const int64: Schema.BigInt; /** * A 64-bit unsigned integer, as a `bigint`, written as eight little-endian * bytes. * * **Details** * * Rejects values outside 0 through 2^64 - 1 instead of wrapping them. See * {@link int64} for the struct field cost. * * **Gotchas** * * Switching a field between this, {@link int64} or `Schema.BigInt` can decode * as a different value without an error in default mode; see {@link int64}. * * @stability unstable * @category schemas * @since 4.0.0 */ export declare const uint64: Schema.BigInt; //# sourceMappingURL=SchemaBinary.d.ts.map