0.1.5 • Published 2 years ago

split-webstreams v0.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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>

esm-example-html

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>

umd-example-html

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);
      }
      // …
    });
})();
0.0.12

2 years ago

0.0.13

2 years ago

0.1.0

2 years ago

0.1.4-next.64

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.5-next.67

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.5

2 years ago

0.0.0-next.37

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago