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

@markdrei/ironguard-typescript-locks

v0.2.3

Published

IronGuard: Unbreakable TypeScript lock system with compile-time deadlock prevention and runtime mutual exclusion

Downloads

492

Readme

🛡️ IronGuard

Unbreakable TypeScript compile-time lock order violation detection system with runtime mutual exclusion. Prevents both deadlocks (through compile-time validation) and race conditions (through async mutual exclusion).

Installation

npm install @markdrei/ironguard-typescript-locks

Quick Start

import { 
  createLockContext, 
  LOCK_1, 
  LOCK_3,
  type LockContext
} from '@markdrei/ironguard-typescript-locks';

// Basic lock acquisition with automatic cleanup
async function example(): Promise<void> {
  const ctx0 = createLockContext();
  
  await ctx0.useLockWithAcquire(LOCK_1, async (ctx1) => {
    console.log(`Holding: [${ctx1.getHeldLocks()}]`); // [1]
    
    await ctx1.useLockWithAcquire(LOCK_3, async (ctx13) => {
      console.log(`Holding: [${ctx13.getHeldLocks()}]`); // [1, 3]
      // Use locks here
    }); // LOCK_3 auto-released
  }); // LOCK_1 auto-released
}

What It Delivers

  • Compile-time deadlock prevention: TypeScript type system enforces lock ordering
  • Runtime thread safety: Async mutual exclusion prevents race conditions
  • Read/write lock semantics: Concurrent readers with writer preference
  • Context transfer validation: Type-safe function parameters with lock requirements
  • Flexible lock patterns: Sequential acquisition, lock skipping, temporary elevation
  • 15 lock levels supported: LOCK_1 through LOCK_15 available
  • Production-ready: Clean API, comprehensive testing, proper resource management

Core Features

Manual Lock Management

For fine-grained control, acquire and release locks explicitly:

const ctx1 = await createLockContext().acquireWrite(LOCK_1);
try {
  const ctx13 = await ctx1.acquireWrite(LOCK_3);
  try {
    // Use locks here
  } finally {
    ctx13.releaseLock(LOCK_3); // Release only LOCK_3
  }
} finally {
  ctx1.releaseLock(LOCK_1); // Release only LOCK_1
}

Read/Write Lock Semantics

Full read/write lock support with concurrent readers and writer preference:

// Multiple readers can hold the same lock simultaneously
const reader1 = await createLockContext().acquireRead(LOCK_3);
const reader2 = await createLockContext().acquireRead(LOCK_3);  // ✅ Concurrent

// Writers get priority over new readers
const writer = await createLockContext().acquireWrite(LOCK_3);  // ⏳ Waits for readers

Context Transfer with Type Safety

Pass lock contexts between functions with compile-time validation:

import type { LocksAtMost5, HasLock3Context } from '@markdrei/ironguard-typescript-locks';

// Accepts any ordered combination of locks 1-5
async function middleProcessor(context: LockContext<LocksAtMost5>): Promise<void> {
  // Can acquire locks > 5
  const withLock6 = await context.acquireWrite(LOCK_6);
  try {
    // Use locks
  } finally {
    withLock6.releaseLock(LOCK_6);
  }
}

// Requires LOCK_3 to be held
function processData<THeld extends IronLocks>(
  ctx: HasLock3Context<THeld>
): void {
  ctx.useLock(LOCK_3, () => {
    // TypeScript guarantees LOCK_3 is present
  });
}

Flexible Lock Types

// Pre-defined types for locks 1-9
import type { LocksAtMost1, LocksAtMost5, LocksAtMost9 } from '@markdrei/ironguard-typescript-locks';

// Nullable types for locks 10-15 (performance optimization)
import type { NullableLocksAtMost10, NullableLocksAtMost15 } from '@markdrei/ironguard-typescript-locks';

// Ensure specific locks are held
import type { HasLock3Context, HasLock11Context } from '@markdrei/ironguard-typescript-locks';

What's Missing

  • Performance benchmarks for high-contention scenarios
  • Lock timeout/cancellation mechanisms
  • Lock priority/scheduling policies

Commands

# See IronGuard in action
npm run examples

# Run all tests
npm test

# Run compile-time validation tests
npm run test:compile

# Build the project
npm run build

Documentation

License

MIT