fetch-stream

0.6.5 • Public • Published

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();

Package Sidebar

Install

npm i fetch-stream

Weekly Downloads

14

Version

0.6.5

License

MIT

Last publish

Collaborators

  • sergeyt