Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

yield-stream

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

yield-stream

A small library for switching between streams, generators, and arrays.

  • 1.0.1
  • npm
  • Socket score

Version published
Weekly downloads
504
decreased by-5.44%
Maintainers
1
Weekly downloads
 
Created
Source

yield-stream

A small library for switching between streams, generators, and arrays.

import { GeneratorFn, StreamGenerator } from "./types";

/**
 * `compose(f, g, h, ...)` returns a generator function `G(data)` that yields
 * all `(f · g · h · ...)(data)`.
 */
export const compose = <T>(
  ...generators: GeneratorFn<T>[]
) => {
  return generators.reduce(
    (prev, next) => async function* (data) {
      for await (const chunk of prev(data)) {
        yield* next(chunk);
      }
    },
  );
};

/**
 * Runs each chunk through all of the given transforms.
 */
export const pipeline = <T>(
  stream: ReadableStream<T>,
  ...transforms: GeneratorFn<T>[]
) => {
  const composed = compose(...transforms);
  return generateStream(
    async function* () {
      for await (const chunk of yieldStream(stream)) {
        yield* composed(chunk);
      }
    }
  );
};

/**
 * Iterates over a stream, yielding each chunk.
 */
export const yieldStream = async function* <T>(
  stream: ReadableStream<T>,
  controller?: AbortController
) {
  const reader = stream.getReader();
  while (true) {
    if (controller?.signal.aborted) {
      break;
    }

    const { done, value } = await reader.read();
    if (done) {
      break;
    }

    yield value;
  }
};

/**
 * Creates a ReadableStream from a generator function.
 */
export const generateStream = <T, TReturn, D>(
  G: StreamGenerator<D, T, TReturn>,
  data?: D
) => {
  return new ReadableStream<T>({
    async start(controller) {
      for await (const chunk of G(data)) {
        controller.enqueue(chunk);
      }
      controller.close();
    },
  });
};

/**
 * Creates a ReadableStream that yields all values in an array.
 */
export const streamArray = <T>(array: T[]) => {
  return generateStream(function* () {
    for (const item of array) {
      yield item;
    }
  });
};

FAQs

Package last updated on 15 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc