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 🙏

© 2024 – Pkg Stats / Ryan Hefner

are-streams-same

v1.0.2

Published

Check if the contents of two Node.js streams are the same.

Downloads

5

Readme

are-streams-same

Check if the contents of two Node.js streams are the same.

Installing

This module does not need any dependencies. If you want to 'require' 'are-streams-same', then you will need rollup/

npm i are-streams-same

Using

There is a default function that takes in two readable streams and returns a promise. See the index.d.ts file for typescript definitions.

Compare two files

import { createReadStream } from 'fs';
import areStreamsSame from 'are-streams-same';

//a.txt: Hello
const stream1 = createReadStream('a.txt');

//b.txt: Hello World
const stream2 = createReadStream('b.txt');

areStreamsSame(stream1, stream2)
    .then(result => {
        console.log(result); 
        /* 
        { 
            same: false, 
            reason: 'The length of a stream exceeded the length of the other stream which was finished.' 
        };
        */
    })

Compare a Saved Hash to a file

import { createReadStream } from 'fs';
import { createHash } from 'crypto';
import areStreamsSame from 'are-streams-same';

//The old hash
const stream1 = createReadStream('hash.dat');

//The new hash
const stream2 = createReadStream('input.txt')
    .pipe(createHash('sha256'));

areStreamsSame(stream1, stream2)
    .then(({ same }) => {
        if(same){
            console.log("File hasn't changed");
        }
        else{
            console.log("File is different");
        }
    })

Features

  • Smart
    • If one stream ends and it is shorter than the other stream, then we know for sure that the streams are of different lengths. The promise is resolved right away and event handlers are removed.
  • Memory Efficient
    • This module is designed to hold as little memory as possible while chunks come from the two streams. When we get data, the two streams are compared right away. The only buffer that is saved is the new parts of the stream that are still waiting for the other stream in order to be compared. If one stream gets ahead of another, it is paused to prevent a big backlog.
  • Lightweight
    • No dependencies. You will need rollup for building if you are using commonjs.
    • Just one file. index.js is the only file with logic. index.d.ts is a small file for typescript definitions. There is also a file called build/cjs.cjs which is to transpile index.js into commonjs, to support commonjs.
  • Typescript Definitions
    • Definitions are in index.d.ts.

Using with CommonJs

This module is made with ESModules. ESModules are able to use other ESModules and CommonJs modules, but CommonJS modules aren't able to use ESModules. If you are using, CommonJS (require('are-streams-same')), or want to support CommonJS, then you can build a CommonJS file by using the build/cjs.cjs file. It is a file that exports an async function which uses rollup to generate the dist/cjs/index.cjs file. The function takes rollup as its argument. For example, you might have a setup like this:

build.js

const rollup = require('rollup');
const build = require('are-streams-same/build/cjs.cjs');

build(rollup)
    .then(() => {
        console.log("done building");
    });

package.json

{
    "scripts": {
        "build": "node build.js",
        "postinstall": "node build.js"
    }
}

Then you can do

npm run build

to generated dist/cjs/index.cjs. Then you can use this package normally by doing:

require('are-streams-same');