npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

file-lockdown

v1.2.1

Published

A robust library for file locking and asynchronous file operations, designed for thread safety and inter-process communication using the 'net' module.

Readme

Table of contents

File Lockdown

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.


Installation

Install the library using npm:

npm install file-lockdown


Basic Usage

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);

How It Works

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.

  1. A lockFiles object tracks the state of locked files.
  2. If a file is already locked, the callback is queued.
  3. If the file is not locked, it is locked, and queued callbacks are processed sequentially using the next function.
  4. A fnUnlock function is provided to release the lock after the operation is complete.

Key Features

  • Thread Safety: Ensures that file operations are serialized for the same file.
  • Asynchronous Operations: All operations are non-blocking and use callbacks.
  • IPC Support: Allows communication with background processes via the net module.
  • Configurable Behavior: Options like enableSyncWrites provide flexibility for synchronous or asynchronous writes.

Config Sets

  • enableSyncWrites: Enable synchronous writing. This is safer than asynchronous writing. (added with version 1.2)
  • ipc_port: Port number. See more about net-fn connection.
  • ipc_host: Hostname.See more about net-fn connection.
{
  "production": {
    "file_lockdown": {
      "enableSyncWrites": false,
      "ipc_port": 8021,
      "ipc_host": "localhost"
    }
  }
}

Reference

Below is a list of the available functions and their usage:

lockFile

Locks 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)

lockReadFile

Reads 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")

lockWriteFile

Writes 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")

lockReadWriteFile

Combines 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",)

lockAppendFile

Appends 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")

lockDeleteFile

Deletes a file.

/**
 * @param {string} filePath
 * @param {(err:any)void} callback function(err)
 */
function lockDeleteFile(filePath, callback)

lockRename

Renames a file.

/**
 * @param {string} filePath
 * @param {string} newPath
 * @param {(err:any)void} callback
 */
function lockRename(filePath, newPath, callback)

lockCreateDir

Creates a directory recursively.

/**
 * @param {string} dirPath
 * @param {(err:any)void} callback
 */
function lockCreateDir(dirPath, callback)

lockDeleteDir

Deletes a directory recursively.

/**
 * @param {string} dirPath
 * @param {(err:any)void} callback
 */
function lockDeleteDir(dirPath, callback)

lockAccess

Checks if a file or directory exists.

/**
 * @param {string} path
 * @param {(err:any)void} callback
 * @param {number} timeout
 */
function lockAccess(path, callback, timeout)

License

This project is licensed under the MIT License.

Copyright © Manuel Lõhmus