split-webstreams
v0.1.5
Published
Break up a stream and reassemble it so that each line is a chunk. `split-webstreams` is inspired by [split2](https://github.com/mcollina/split2).
Downloads
7
Readme
split-webstreams
Break up a stream and reassemble it so that each line is a chunk. split-webstreams
is inspired by split2.
However this is for web streams which is support by Node(>18) and browser.
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
API
split
is same as String/split. But not support limit
parameter.
split
only accept string chunk, please pipeThrough TextDecoderStream first.
import { split } from 'split-webstreams';
const reader = Readable.toWeb(
createReadStream(path.join(__dirname, 'test-data.txt'))
)
.pipeThrough(new TextDecoderStream())
.pipeThrough(split());
for await (const chunk of reader) {
console.log(chunk);
}
Install
npm
$ npm i split-webstreams
jsdelivr
ESM
<script type="module">
import { split } from 'https://cdn.jsdelivr.net/npm/split-webstreams@latest';
console.log(split());
</script>
UMD
<script src="https://cdn.jsdelivr.net/npm/split-webstreams@latest/dist/split-webstreams.umd.js"></script>
<script>
console.log(window['split-webstreams'].split());
</script>
Usage
(async () => {
await fetch(`https://rickandmortyapi.com/api/character/23`)
.then((res) => res.body)
.then(async (body) => {
const reader = body
?.pipeThrough(new TextDecoderStream())
.pipeThrough(split())
.getReader();
for (
let result = await reader?.read();
!result?.done;
result = await reader?.read()
) {
console.log('[value]', result?.value);
}
// …
});
})();