Grounds
    Preparing search index...

    Function decodeIterable

    • Decodes values from streaming binary data.

      Processes chunks incrementally, yielding decoded values as complete frames are available. Stops on first decode error.

      Parameters

      • chunks: AsyncIterable<Uint8Array<ArrayBufferLike>>

        Async iterable of byte chunks

      Returns AsyncGenerator<Result<DecodedValue, DecodeError>>

      Async generator yielding Result<DecodedValue, DecodeError>

      Streaming network decode:

      import { decodeIterable } from '@grounds/stream';

      async function* readNetworkStream(socket) {
      // Read chunks from network socket
      while (true) {
      const chunk = await socket.read();
      if (!chunk) break;
      yield chunk;
      }
      }

      for await (const result of decodeIterable(readNetworkStream(socket))) {
      result.match(
      (value) => console.log('Decoded:', value),
      (error) => console.error('Decode error:', error.code)
      );
      }

      File streaming:

      import { decodeIterable } from '@grounds/stream';
      import { createReadStream } from 'fs';

      async function* readFile(path: string) {
      const stream = createReadStream(path);
      for await (const chunk of stream) {
      yield new Uint8Array(chunk);
      }
      }

      for await (const result of decodeIterable(readFile('data.bin'))) {
      // Process decoded values
      }

      Maintains internal buffer for incomplete frames. When a complete value is decoded, it yields the Result and continues with remaining buffered data.

      Unlike encodeIterable, stops on first error (can't continue decoding after stream corruption).