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

@sufalctl/rwlock

v2.0.6

Published

A lightweight asynchronous read-write lock that allows:-

Readme

🔐 RwLock – TypeScript Read-Write Lock (Usage Guide)

A lightweight asynchronous read-write lock that allows:-

  • ✅ Multiple concurrent handles
  • ✅ Exclusive single writer
  • ✅ FIFO queuing
  • ✅ Async/await support
  • ✅ Safe access and mutation of any shared value

📦 Import

// Import the RwLock class from your implementation file
import RwLock from "./RwLock"; // adjust the path as needed

// Create a new lock with an initial value
const lock = new RwLock<number>(0);
// Acquire a read lock
const [value, unlock] = await lock.read();

// Use the value
console.log("Read value:", value);

// Release the read lock
unlock();

// Acquire a write lock (exclusive access)
const [value, done] = await lock.write();

// Use the value (read-only access, no mutation)
console.log("Got write lock with value:", value);

// Release the write lock
done();

// Acquire a write lock with intention to modify the value
const [oldValue, set] = await lock.setWrite();

// Log the current value
console.log("Previous value:", oldValue);

// Update the value and release the lock
set(oldValue + 1);

🗃 Using with fs.promises (Node.js File System)

You can use RwLock to safely manage access to a file, ensuring no race conditions during concurrent reads or writes.

📦 Import

import fs from "node:fs/promises";
import RwLock from "./RwLock";

const filePath = "./example.txt";

// Open the file
const handle = await fs.open(filePath, "w+");

// Wrap the file handle with the RwLock
const fileLock = new RwLock(handle);

async function safeRead() {
  const [reader, unlock] = await fileLock.read();

  try {
    const { size } = await reader.stat();
    const buffer = Buffer.alloc(size);
    await reader.read(buffer, 0, size, 0);
    console.log("Read:", buffer.toString());
  } finally {
    unlock();
  }
}

async function safeWrite(content: string) {
  const [writer, done] = await fileLock.write();

  try {
    await writer.truncate(0); // Clear previous content
    await writer.writeFile(content);
    console.log("Wrote:", content);
  } finally {
    done();
  }
}

// Example
await safeWrite("Hello from RwLock!");
await safeRead();

await Promise.all([safeRead(), safeRead()]); // Optional concurrent reads

await safeWrite("New exclusive write");
await safeRead();

// Cleanup
await handle.close();

🗃 Recommended Smart way fs.promises (Node.js File System)

You can use RwLock to safely manage access to a file, ensuring no race conditions during concurrent reads or writes.

📦 Import

import fs from "node:fs/promises";
import RwLock from "./RwLock";

const filePath = "./example.txt";

// Give a unique value to identify the file you what to use
const fileLock = new RwLock(filePath); // Here i just take the file path name to identify the file

async function safeRead() {
  const [_, unlock] = await fileLock.read(); // no need to take value we use smart way

  try {
    const file = await fs.open(filePath, "w+");

    const { size } = await file.stat();
    const buffer = Buffer.alloc(size);
    await file.read(buffer, 0, size, 0);
    console.log("Read:", buffer.toString());
  } finally {
    unlock();
  }
}

async function safeWrite(content: string) {
  const [_, done] = await fileLock.write(); // no need to take value we use smart way

  try {
    const file = await fs.open(filePath, "w+");
    await file.truncate(0); // Clear previous content
    await file.writeFile(content);
    console.log("Wrote:", content);
  } finally {
    done();
  }
}

// Example
await safeWrite("Hello from RwLock!");
await safeRead();

await Promise.all([safeRead(), safeRead()]); // Optional concurrent reads

await safeWrite("New exclusive write");
await safeRead();

// Cleanup
await handle.close();