Grounds
    Preparing search index...

    Module @grounds/stream

    Streaming utilities for encoding and decoding Relish values incrementally.

    Provides both async generator and Web Streams API support for streaming scenarios. Includes schema-aware streaming for type-safe operations.

    Async generator streaming:

    import { encodeIterable, decodeIterable } from '@grounds/stream';
    import { U32 } from '@grounds/core';

    async function* generateValues() {
    yield U32(1);
    yield U32(2);
    }

    // Encode
    for await (const result of encodeIterable(generateValues())) {
    // Process encoded bytes
    }

    // Decode
    async function* readChunks() {
    // Read from network, file, etc.
    }

    for await (const result of decodeIterable(readChunks())) {
    // Process decoded values
    }

    Web Streams API:

    import { createEncoderStream, createDecoderStream } from '@grounds/stream';

    const encoder = createEncoderStream();
    const decoder = createDecoderStream();

    valuesStream.pipeThrough(encoder).pipeThrough(decoder);

    Schema-aware streaming with async generators:

    import { encodeIterable } from '@grounds/stream';
    import { createCodec, RStruct, field, RString, type Static } from '@grounds/schema';

    const schema = RStruct({ name: field(0, RString()) });
    type Data = Static<typeof schema>;

    async function* generate(): AsyncGenerator<Data> {
    yield { name: 'Alice' };
    }

    const codec = createCodec(schema);
    for await (const result of encodeIterable(generate())) {
    result.match(
    (bytes) => console.log('Encoded:', bytes),
    (error) => console.error('Failed:', error)
    );
    }

    Two API styles:

    • Async Generators: Full Result-based error handling, suitable for most use cases
    • Web Streams: Standard TransformStream API, compatible with browser/Node.js streams

    Schema-aware variants combine validation from @grounds/schema with streaming.

    Functions

    decodeIterable
    encodeIterable
    createSchemaEncoderStream
    createSchemaDecoderStream
    createEncoderStream
    createDecoderStream