JSPM

t-readable

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 39
  • Score
    100M100P100Q68720F
  • License MIT

Split a readable-stream into 2 or more readable-streams

Package Exports

  • t-readable

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (t-readable) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status NPM version npm downloads

t-readable

Split one readable stream into multiple readable streams.

Installation

Using npm:

npm install t-readable

or yarn:

yan add t-readable

Usage

const { tee } = require('t-readable');
const got = require('got');

const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';

/**
 * Counts the number of bytes in the givent stream
 * @param readable {stream.Readable} - Readable stream
 * @return {Promise<number>} - Number of bytes until the end of stream is reached
 */
async function countBytes(readable) {
  let bytesRead = 0;

  return new Promise((resolve, reject) => {
    readable.on('data', chunk => {
      bytesRead += chunk.length;
    });
    readable.on('end', () => {
      resolve(bytesRead);
    });
    readable.on('error', error => {
      reject(error);
    });
  });
}

(async () => {
  const stream = got.stream(url); // stream is an instance of class stream.Readable

  const teedStreams = tee(stream);
  
  // Count the number of bytes received in each teed stream
  const counts = await Promise.all(teedStreams.map(readable => countBytes(readable)));
  console.log('Counts: ' + counts.join(', ')); // Counts: 27661, 27661
})();