fetch-stream

Easy fetch of HTTP/1.1 chunked content.

Usage no npm install needed!

<script type="module">
  import fetchStream from 'https://cdn.skypack.dev/fetch-stream';
</script>

README

npm version Build Status codecov.io Total downloads

fetch-stream

Easy fetch of HTTP/1.1 chunked content.

Basic usage

import fetchStream from 'fetch-stream';

const handler = (result) => {
    if (result.done) {
        console.log('completed');
        return;
    }
    console.log(result.value);
    return i < 100; // return false to cancel
};

fetchStream('/api/stream', handler);

Usage of stream API

import fetchStream from 'fetch-stream';

const handler = (result) => {
    if (result.done) {
        console.log('completed');
        return;
    }
    console.log(result.value);
    return i < 100; // return false to cancel
};

const stream = fetchStream('/api/stream');

const pump = () => {
    stream.read().then((result) => {
        if (result.done) {
            return;
        }
        if (handler(result) === false) {
            stream.cancel();
            return;
        }
        pump();
    });
};

// process all chunks
pump();