is-file-equal-buffer
v1.0.4
Published
Compare file contents with buffer
Downloads
142
Maintainers
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
Objectfs
FileSystem 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.
Examples
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.
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:
- First, the function compares file and buffer sizes.
- If the file does not exist, the function will return
false
. - 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. - 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
. - 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).