web-stream-map
v1.0.1
Published
A map function for Web Streams with concurrency support. Like node's ReadableStream.map(), but for Web Streams.
Downloads
1,120
Maintainers
Readme
web-stream-map
A map function for Web Streams with concurrency support. Like node's ReadableStream.map(), but for Web Streams. Works in browsers and Node.js.
Installation
npm install web-stream-mapUsage
sMap creates a TransformStream that can be pipeThrough()'d into a stream.
Basic
import sMap from "web-stream-map";
const stream = ReadableStream.from([1, 2, 3]);
const result = await Array.fromAsync(
stream.pipeThrough(sMap((chunk) => chunk * 2))
);
console.log(result); // [2, 4, 6]Concurrency
import sMap from "web-stream-map";
import { setTimeout } from "node:timers/promises";
const stream = ReadableStream.from([1, 2, 3]);
const result = await Array.fromAsync(
stream.pipeThrough(
sMap(
async (chunk) => {
await setTimeout(100);
return chunk * 2;
},
{ concurrency: 5 }
)
)
);
console.log(result); // [2, 4, 6], but in 100msAPI
sMap(fn, options)
fn- A function that takes a chunk and returns a promise or value.options- An optional object with aconcurrencyproperty. Defaults to 1.
Returns a TransformStream that maps over the input stream and applies the function to each chunk.
