File Lockdown is a library designed to handle file locking and asynchronous file operations.
It is built for use with fs-broker and allows communication with background processes via the net-fn module.
The library ensures thread safety and provides a robust API for managing file operations without blocking the event loop.
This manual is also available in HTML5 format.
Install the library using npm:
npm install file-lockdown
Here’s a simple example of how to use the library:
var file_lock = require("file-lockdown");
setTimeout(function () {
file_lock.lockAppendFile("./test.txt", "1 test\r\n", function (err) {
if (err) { console.error(err); }
});
}, 100);
The library uses a locking mechanism to ensure that file operations are serialized for the same file. This prevents race conditions and ensures thread safety.
lockFiles object tracks the state of locked files.next function.fnUnlock function is provided to release the lock after the operation is complete.net module.enableSyncWrites provide flexibility for synchronous or asynchronous writes.net-fn connection.net-fn connection.{
"production": {
"file_lockdown": {
"enableSyncWrites": false,
"ipc_port": 8021,
"ipc_host": "localhost"
}
}
}
Below is a list of the available functions and their usage:
lockFileLocks a file for exclusive access.
/**
* @param {string} filePath
* @param {(err:any,callback:(err:any,fnUnlock:()void)void)void} callback function(err,fnUnlock()=>void){...}
*/
function lockFile(filePath, callback)
lockReadFileReads a file while ensuring it is locked during the operation.
/**
* @param {string} filePath
* @param {(err:any,data:Buffer)void} callback function(err,data)
* @param {string} encoding Default: "utf8"
*/
function lockReadFile(filePath, callback, encoding = "utf8")
lockWriteFileWrites data to a file, optionally truncating it first.
/**
* @param {string} filePath
* @param {Buffer} bufferdata
* @param {(err:any)void} callback function(err)
* @param {string} encoding Default: "utf8"
*/
function lockWriteFile(filePath, buffer, callback, encoding = "utf8")
lockReadWriteFileCombines reading and writing operations on a file.
/**
* @param {string} filePath
* @param {(err:any,buf:Buffer,fnWriteClose:(buf:Buffer,isTruncated:boolean, callback:(err:any)void)void)void} fnRead function(err,data,fnWriteClose(buffer))'buffer'dataforwritingandclose|nullforclose
* @param {string} encoding Default: "utf8"
*/
function lockReadWriteFile(filePath, callback, encoding = "utf8",)
lockAppendFileAppends data to a file, creating it if it doesn't exist.
/**
* @param {string} filePath
* @param {Buffer} buffer
* @param {(err:any)void} callback function(err)
* @param {string} encoding Default: "utf8"
*/
function lockAppendFile(filePath, buffer, callback, encoding = "utf8")
lockDeleteFileDeletes a file.
/**
* @param {string} filePath
* @param {(err:any)void} callback function(err)
*/
function lockDeleteFile(filePath, callback)
lockRenameRenames a file.
/**
* @param {string} filePath
* @param {string} newPath
* @param {(err:any)void} callback
*/
function lockRename(filePath, newPath, callback)
lockCreateDirCreates a directory recursively.
/**
* @param {string} dirPath
* @param {(err:any)void} callback
*/
function lockCreateDir(dirPath, callback)
lockDeleteDirDeletes a directory recursively.
/**
* @param {string} dirPath
* @param {(err:any)void} callback
*/
function lockDeleteDir(dirPath, callback)
lockAccessChecks if a file or directory exists.
/**
* @param {string} path
* @param {(err:any)void} callback
* @param {number} timeout
*/
function lockAccess(path, callback, timeout)
This project is licensed under the MIT License.
Copyright © Manuel Lõhmus