Effection Logo
@effectionx/stream-yamlv0.2.0thefrontside/effectionx
NPM Badge with published version
import { } from "@effectionx/stream-yaml"

Stream Yaml

A helper that parses a stream of strings as YAML documents. Composes well with @effectionx/stream-helpers.

YAML file to Documents

This example shows how to read the contents of a YAML file as a stream with Node and process each document.

import { main, createChannel, spawn } from "effection";
import { readTextFile } from "@effectionx/fs";
import { yamlDocuments } from "@effectionx/stream-yaml";

await main(function* () {
  const channel = createChannel<string, void>();

  // Read file and send contents to channel
  yield* spawn(function* () {
    const content = yield* readTextFile("./config.yaml");
    yield* channel.send(content);
    yield* channel.close();
  });

  // Parse YAML documents from the channel
  const docs = yield* yamlDocuments()(channel);

  for (let result = yield* docs.next(); !result.done; result = yield* docs.next()) {
    const doc = result.value;
    console.log("Document:", doc.toJS());
  }
});

Handling close value

If the stream is closed with a close value, it'll be returned as the result of the stream.

import { main, createChannel, spawn } from "effection";
import { yamlDocuments } from "@effectionx/stream-yaml";

await main(function* () {
  const channel = createChannel<string, string>();

  yield* spawn(function* () {
    yield* channel.send("---\nname: first\n---\nname: second\n");
    yield* channel.close("all-done");
  });

  const docs = yield* yamlDocuments()(channel);

  let result = yield* docs.next();
  while (!result.done) {
    console.log("Document:", result.value.toJS());
    result = yield* docs.next();
  }

  // The close value is returned when the stream is exhausted
  console.log("Stream closed with:", result.value); // "all-done"
});

Streaming from process output

This example shows how to parse YAML documents from a process's stdout, useful for tools that output multiple YAML documents.

import { main } from "effection";
import { exec } from "@effectionx/process";
import { yamlDocuments } from "@effectionx/stream-yaml";
import { map } from "@effectionx/stream-helpers";

await main(function* () {
  const process = yield* exec("kubectl get pods -o yaml");

  // Convert stdout bytes to strings, then parse as YAML
  const stringStream = yield* map((chunk: Uint8Array) => 
    new TextDecoder().decode(chunk)
  )(process.stdout);

  const docs = yield* yamlDocuments()(stringStream);

  for (let result = yield* docs.next(); !result.done; result = yield* docs.next()) {
    const pod = result.value.toJS();
    console.log("Pod:", pod.metadata?.name);
  }
});

API Reference

function yamlDocuments(): (stream: Stream<string, TClose>) => Stream<Document.Parsed, TClose>

Stream helper that parses YAML document streams from string chunks.

Transforms a stream of string chunks into a stream of YAML Document objects. Supports multi-document YAML streams (documents separated by ---).

Examples

Example 1
import { yamlDocuments } from "@effectionx/stream-yaml";
import { pipe } from "effection";

const docs = yield* pipe(stringStream, yamlDocuments());
for (const doc of yield* docs) {
  console.log(doc.toJS());
}

Return Type

<TClose>(stream: Stream<string, TClose>) => Stream<Document, TClose>