npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

stream-compare

v3.0.0

Published

Compare the behavior of readable streams.

Readme

stream-compare

Build Status: Linux Build Status: Windows Coverage Dependency Status Supported Node Version Version on NPM

Compare the output of two Readable streams using a caller-provided comparison/assertion function.

Introductory Example

var assert = require('assert');
var fs = require('fs');
var streamCompare = require('stream-compare');

var stream1 = fs.createReadStream(file);
var stream2 = fs.createReadStream(file);
streamCompare(stream1, stream2, assert.deepStrictEqual).catch(function(err) {
  console.log(err); // AssertionError if streams differ
});

Features

This package is similar to the stream-equal package with several additional features:

  • Support for caller-defined comparisons, which can return errors or values not limited to equality.
  • Support for both incremental and one-shot comparisons.
  • Support for caller-defined data reduction to avoid storing the entire stream history in memory before comparison.
  • Makes no assumptions about the type of values read beyond whether they should be treated as objects (objectMode) or a stream of Buffers or strings.
  • Does not do any coercion of the values read.
  • Support for comparing (caller-configurable) events emitted by the streams.
  • Support reading in flowing or non-flowing mode.
  • Support for optionally aborting comparison on stream errors.
  • Support for catching multiple end/error events (within one tick by default, or an optional configurable delay).
  • Utility function for creating an incremental comparison and data-reduction function from a standard data comparison function (e.g. assert.deepEqual).

Installation

This package can be installed using npm, either globally or locally, by running:

npm install stream-compare

Recipes

Compare Incrementally

In order to avoid unnecessary memory use for streams with large amounts of data and avoid unnecessary delays for streams which produce data slowly, the output can be compared incrementally and inconclusive output can be removed. This is done by the incremental function. To make this easier, the utility function makeIncremental creates such a function from a data comparison function and/or an events comparison function:

var options = {
  incremental: streamCompare.makeIncremental(
    assert.deepStrictEqual,     // Compares data
    assert.deepStrictEqual      // Compares events
  )
};
streamCompare(stream1, stream2, options).catch(function(err) {
  console.log(err); // AssertionError if stream data values differ
});

Compare Data Values Separately

Sometimes it may be desirable to compare the values returned by .read() or 'data' events separately, rather than concatenated together. This can be done by setting objectMode: true (even if the values aren't Objects):

var options = {
  compare: assert.deepStrictEqual,
  objectMode: true
};
streamCompare(stream1, stream2, options).catch(function(err) {
  console.log(err); // AssertionError if stream data values differ
});

Compare Data and Event Interleaving

In order to compare the ordering of 'data' events with other events, add 'data' to the events option and set readPolicy to 'flowing' or 'none'. Any 'data' events and their arguments will appear with any other matching events in the events property of the state object.

var options = {
  compare: assert.deepStrictEqual,
  events: ['close', 'data', 'end', 'error'],
  readPolicy: 'none'
};
streamCompare(stream1, stream2, options).catch(function(err) {
  console.log(err); // AssertionError if stream events (including 'data') differ
});

Control comparison checkpoints

The returned Promise includes additional methods for controlling the comparison. A non-incremental compare can be run before both streams end using .checkpoint(). Additionally, the comparison can be concluded before both streams end using .end(). The full details are available in the API Documentation.

var PassThrough = require('stream').PassThrough;

var stream1 = new PassThrough();
var stream2 = new PassThrough();
var comparison = streamCompare(stream1, stream2, assert.deepStrictEqual);
comparison.then(
  function() { console.log('streams are equal'); },
  function(err) { console.log('streams differ: ' + err); }
);
stream1.write('Hello');
stream2.write('Hello');
process.nextTick(function() {
  comparison.checkpoint();

  stream1.write(' world!');
  stream2.write(' world!');

  process.nextTick(function() {
    comparison.end();
  });
});

More examples can be found in the test specifications.

API Docs

For the details of using this module as a library, see the API Documentation.

Contributing

Contributions are appreciated. Contributors agree to abide by the Contributor Covenant Code of Conduct. If this is your first time contributing to a Free and Open Source Software project, consider reading How to Contribute to Open Source in the Open Source Guides.

If the desired change is large, complex, backwards-incompatible, can have significantly differing implementations, or may not be in scope for this project, opening an issue before writing the code can avoid frustration and save a lot of time and effort.

License

This project is available under the terms of the MIT License. See the summary at TLDRLegal.

The template upon which this project is based is available under the terms of CC0 1.0 Universal.