A map function for Web Streams with concurrency support. Like node's ReadableStream.map(), but for Web Streams. Works in browsers and Node.js.
npm install web-stream-map
sMap creates a TransformStream
that can be pipeThrough()'d into a stream.
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]
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 100ms
-
fn
- A function that takes a chunk and returns a promise or value. -
options
- An optional object with aconcurrency
property. Defaults to 1.
Returns a TransformStream
that maps over the input stream and applies the function to each chunk.