Skip to content

sergeyt/fetch-stream

Repository files navigation

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

About

Easy fetch of HTTP/1.1 chunked content.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages