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

writable-async-iterable-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

writable-async-iterable-stream

An async stream which can be iterated over using a for-await-of loop.

  • 2.1.0
  • unpublished
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

writable-async-iterable-stream

An async stream which can be iterated over using a for-await-of loop and which can be written to.

The WritableAsyncIterableStream class extends the AsyncIterableStream class.
See https://github.com/SocketCluster/async-iterable-stream

Installation

npm install writable-async-iterable-stream

Usage

Consume a stream and write to it asynchronously:

let iterableStream = new WritableAsyncIterableStream();

async function consumeAsyncIterable(asyncIterable) {
  // Consume iterable data asynchronously.
  for await (let packet of asyncIterable) {
    console.log('Packet:', packet);
  }
}
consumeAsyncIterable(iterableStream);

setInterval(() => {
  // Write data to the stream asynchronously,
  iterableStream.write(`Timestamp: ${Date.now()}`);
}, 100);

Consume a filtered stream using an async generator:

let iterableStream = new WritableAsyncIterableStream();

// Creates an async generator which only produces packets which are allowed by the
// specified filterFunction.
async function* createFilteredStreamGenerator(fullStream, filterFunction) {
  for await (let packet of fullStream) {
    if (filterFunction(packet)) {
      yield packet;
    }
  }
};

async function consumeAsyncIterable(asyncIterable) {
  // Consume iterable data asynchronously.
  for await (let packet of asyncIterable) {
    console.log('Packet:', packet);
  }
}

// The filter function will only include strings which end with the number 5.
function filterFn(data) {
  return /5$/.test(data);
}
let filteredStreamGenerator = createFilteredStreamGenerator(iterableStream, filterFn);

consumeAsyncIterable(filteredStreamGenerator);

setInterval(() => {
  // Write data to the stream asynchronously,
  iterableStream.write(`Timestamp: ${Date.now()}`);
}, 100);

Consume only the next data object which will be written to the stream:

let iterableStream = new WritableAsyncIterableStream();

(async () => {
  let data = await iterableStream.once();
  console.log(data);
})();

setInterval(() => {
  // Write data to the stream asynchronously,
  iterableStream.write(`Timestamp: ${Date.now()}`);
}, 100);

See test/ directory for additional examples.

Keywords

FAQs

Package last updated on 04 Dec 2018

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