are-streams-same
v1.0.2
Published
Check if the contents of two Node.js streams are the same.
Downloads
5
Maintainers
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 calledbuild/cjs.cjs
which is to transpileindex.js
into commonjs, to support commonjs.
- No dependencies. You will need
- Typescript Definitions
- Definitions are in
index.d.ts
.
- Definitions are in
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');