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).
npm install split-webstreamsBreak 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
split is same as String/split. But not support limit parameter.
split only accept string chunk, please pipeThrough TextDecoderStream first.
``javascript
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);
}
`
`bash`
$ npm i split-webstreams
#### ESM
`html`
#### UMD
`html`
`javascripthttps://rickandmortyapi.com/api/character/23
(async () => {
await fetch()``
.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);
}
// …
});
})();