This package has two functions for web streams.
-
webStreamsFrom(iterable)
is stream.Readable.from implementation for web streams. -
webStreams2AsyncIterator(readableStream)
is Async Iteration implementation for web streams in browser.web streams
spec has this, but no browserimplementation this yet.
Zero dependency! And package working in node.js (> 18) and browser.
streams sepc: https://streams.spec.whatwg.org/
MDN doc: https://developer.mozilla.org/en-US/docs/Web/API/Streams_API
Parameter iterable
is any value meet iterable
strings or TypedArray will only in one chunk.
import { webStreamsFrom, webStreams2AsyncIterator } from 'webstreams-converter';
function* testGen() {
yield 'a';
yield 'b';
yield 'c';
return 'ddd';
}
let result = '';
const read = webStreamsFrom(testGen());
Parameter readableStream is instance of ReadableStream
strings or TypedArray will only in one chunk.
import { webStreamsFrom, webStreams2AsyncIterator } from 'webstreams-converter';
const read = webStreamsFrom(['a', 'b', 'c']);
for await (const val of webStreams2AsyncIterator(read)) {
console.log(val);
}
$ npm i webstreams-converter
<script type="module">
import {
webStreamsFrom,
webStreams2AsyncIterator,
} from 'https://cdn.jsdelivr.net/npm/webstreams-converter@latest';
const read = webStreamsFrom(['a', 'b', 'c']);
for await (const val of webStreams2AsyncIterator(read)) {
console.log(val);
}
</script>
<script src="https://cdn.jsdelivr.net/npm/webstreams-converter@latest/dist/webstreams-converter.umd.js"></script>
<script>
const webStreamsFrom = window['webstreams-converter'].webStreamsFrom;
const webStreams2AsyncIterator =
window['webstreams-converter'].webStreams2AsyncIterator;
const read = webStreamsFrom(['a', 'b', 'c']);
(async () => {
for await (const val of webStreams2AsyncIterator(read)) {
console.log(val);
}
})();
</script>