lite-sse-chunk

0.0.2 • Public • Published

Lite SSE(sever send event) Chunk

a lite tool to parse sse chunk.

stream profile

< chunk1 start
data: hello

data: world!

data: this is
< chunk1 end

==> messages : ['hello', 'world!']

< chunk2 start
last!

< chunk2 end

===> messages : ['this is last!']

Usage

basic

const { TextStream } = require('lite-sse-chunk');
const stream = new TextStream();
stream.addChunkListener((messages) => {
    console.log(messages); // the messages of newest chunk
    console.log(Array.isArray(messages)); // always true!
});

theSource.on('chunk', (chunk) => {
    const text = decodeText(chunk); // decode the chunk to text if the chunk is binary
    stream.receive(chunk);
});

custom chunk read

const { TextStream, createRegexExtractor } = require('lite-sse-chunk');
const reader = createParser(/xxxx: (.*)\n\n/g);
const stream = new TextStream(reader);
stream.addChunkListener((messages) => {
    console.log(messages); // the messages of newest chunk
    console.log(Array.isArray(messages)); // always true!
});

theSource.on('chunk', (chunk) => {
    const text = decodeText(chunk); // decode the chunk to text if the chunk is binary
    stream.receive(text);
});

the result:

< chunk1 start
xxxx: hello

xxxx: world!

< chunk1 end

==> messages : ['hello', 'world!']

ChangeLog

0.0.1

  • first release
  • support text parse

Package Sidebar

Install

npm i lite-sse-chunk

Weekly Downloads

0

Version

0.0.2

License

MIT

Unpacked Size

7.18 kB

Total Files

6

Last publish

Collaborators

  • xiaopingyuan