1.0.1 • Published 4 years ago

agenstream v1.0.1

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

agenstream

Node.js Streams using Async Generators

Examples

Using pipe()

import { pipe } from 'agenstream'

// Stream data from somewhere
async function* source() {
	yield 'a'
	yield 'b'
	yield 'c'
}

// Do something with the data
async function* transform(input: AsyncIterable<string>) {
	for await (const x of input) yield x.charCodeAt(0)
}

// Use the transformed data for something
async function* sink(input: AsyncIterable<number>) {
	let result = 0
	for await (const x of input) result += x
	console.log(result)
}

pipe(source, transform, sink)

Transforming an async generator into a stream

import { stream } from 'agenstream'

// Stream data from somewhere
async function* source() {
	yield 'a'
	yield 'b'
	yield 'c'
}
stream.readable(source) // creates a Readable stream


// Do something with the data
async function* transform(input: AsyncIterable<string>) {
	for await (const x of input) yield x.charCodeAt(0)
}
stream.duplex(transform) // creates a Duplex stream

// Use the transformed data for something
async function* sink(input: AsyncIterable<number>) {
	let result = 0
	for await (const x of input) result += x
	console.log(result)
}
stream.writable(sink) // creates a Writable stream
1.0.1

4 years ago

1.0.0

4 years ago