https://github.com/awwit/node-is-file-equal-buffer
Compare file contents with buffer (check for content equality)
https://github.com/awwit/node-is-file-equal-buffer
buffer compare comparison equal file nodejs
Last synced: about 2 months ago
JSON representation
Compare file contents with buffer (check for content equality)
- Host: GitHub
- URL: https://github.com/awwit/node-is-file-equal-buffer
- Owner: awwit
- License: mit
- Created: 2020-04-20T14:06:31.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T06:01:53.000Z (over 3 years ago)
- Last Synced: 2025-10-20T21:32:23.326Z (6 months ago)
- Topics: buffer, compare, comparison, equal, file, nodejs
- Language: TypeScript
- Homepage:
- Size: 1.08 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Is File Equal Buffer
This module has the function `isFileEqualBuffer` to compare file contents and buffer.
The function runs asynchronously and returns a `Promise` with resolved `boolean`.
`isFileEqualBuffer(filePath, buffer[, options])`
- `filePath` [string] | [Buffer] | [URL] path to the file you want to compare.
- `buffer` [Buffer] buffer with which to compare file contents.
- `options` [Object]
- `fs` [FileSystem](https://nodejs.org/api/fs.html) file system module (`fs`). __Default:__ `require('fs')`.
- Returns: [Promise]<[boolean]> `true` if the contents of the file and buffer are equal, `false` — if not equal.
[URL]: https://nodejs.org/api/url.html#url_the_whatwg_url_api
[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type
[Buffer]: https://nodejs.org/api/buffer.html#buffer_class_buffer
[Object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
[boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
## Examples
```js
const { isFileEqualBuffer } = require('is-file-equal-buffer')
const filePath = 'path/to/file'
const buffer = Buffer.from('buffer content')
isFileEqualBuffer(filePath, buffer).then(function checkIsEqual(isEqual) {
if (isEqual) {
// Do something if their contents are equal
} else {
// Do something if their contents are NOT equal
}
})
```
You can also provide your own modified `fs` module.
```js
const { isFileEqualBuffer } = require('is-file-equal-buffer')
const fs = require('graceful-fs') // <-- modified `fs` module
const filePath = 'path/to/file'
const buffer = Buffer.from('buffer content')
isFileEqualBuffer(filePath, buffer, { fs }).then(function checkIsEqual(isEqual) { /* ... */ })
```
## How it works (inside)
The comparison function is made in the most optimal way:
1. First, the function compares file and buffer sizes.
2. If the file does not exist, the function will return `false`.
3. If the file and buffer sizes are the same: a stream of reading data from a file is created (`fs.createReadStream`), and as new data arrives from the stream, they are compared with the buffer.
4. As soon as an inequality of the contents of the file and the buffer is detected, the read stream closes and the function returns `false`.
5. The function will return `true` only if the contents of the file and buffer completely match.
## Motivation
This can be useful not to overwrite files that do not change after processing ([and save a erase cycles for SSD as example](https://en.wikipedia.org/wiki/Wear_leveling#Rationale)).