Read and write to binary files in Node JS.
IMPORTANT: This is an ESM NodeJS only package.
Node JS has the means to read and write buffers to and from a file. You can also read and write different types of binary data to and from a buffer, like unsigned 16 bit integers.
This library combines both these into a single class that allows you to read and write binary data to and from a file in a simple way.
It comes in two classes, BinaryFilePromise for when you want to use async/await and BinaryFileSync if you prefer not to use promises.
It also uses a data cache to improve performance.
This example uses the synchronous class version. It opens the file and writes an UINT8 (unsigned 8 bit integer), then an INT32 (signed 32 bit integer), and then writes a double (64 bit floating point number), and then finishes things off by closing the file.
This next example uses the asynchronous promise class version. It does the same things as before but it uses promises.
Creates a new instance of the BinaryFileSync or BinaryFilePromise object.
Opens a file for reading.
If the file does not exist then an exception is thrown.
The BinaryFilePromise version will return a Promise.
Opens a file for writing additional data to.
If the file does not exist then the file is created.
The file is not truncated if it already exists.
The BinaryFilePromise version will return a Promise.
Opens a file for writing data to.
If the file does not exist then the file is created.
If the file does already exist then it is truncated (emptied).
The BinaryFilePromise version will return a Promise.
Closes a file that is currently open.
The BinaryFilePromise version will return a Promise.
After the file has been opened, you can get the full path of the file that was used to open it.
After the file has been opened, you can get file descriptor value, which you can use to perform any additional tasks with the node FS functions.
After a file has been opened, you can get the file handle class object, which you can use to call a number of additional functions.
This is only used with the BinaryFilePromise class version.
This property is used to get the current read or write position within the file.
After the file has been opened, you can get the length of the file. If you are writing to the file then this value will be updated to give the current size of the file.
Writes binary data to the file.
The BinaryFilePromise version will return a Promise.
Number type except for writeBuffer (which is a Buffer object), and the
functions writeInt64 and writeUint64 (which require a BigInt).
Writes binary data to the file.
The BinaryFilePromise version will return a Promise.
Moves the current position to read or write from to a different location within the file.
This allows you to move to different positions within the file whenever you want.
However, please note, doing this will force the cache to be re-read or flushed, so do not use it too much as it will impact performance.
The BinaryFilePromise version will return a Promise.
0 = From Start of File, 1 = From Current Position or 2 = From End of File.