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 🙏

© 2026 – Pkg Stats / Ryan Hefner

universal-lock

v1.0.0

Published

Lightweight, isomorphic universal locking library with pluggable backends

Downloads

29

Readme

universal-lock

Lightweight, isomorphic universal locking library with pluggable backends. Works in Node.js and browsers.

Part of the universal-lock monorepo.

Installation

npm install universal-lock

You also need a backend package:

npm install @universal-lock/memory      # single-process
npm install @universal-lock/redis        # distributed (cross-process/server)
npm install @universal-lock/web-locks    # browser (cross-tab, modern)
npm install @universal-lock/local-storage # browser (cross-tab, older)

Quick Start

ESM

import { lockFactory } from "universal-lock";
import { createBackend } from "@universal-lock/memory";

const lock = lockFactory(createBackend());

const release = await lock.acquire("my-resource");
try {
	// critical section
} finally {
	await release();
}

CommonJS

const { lockFactory } = require("universal-lock");
const { createBackend } = require("@universal-lock/memory");

const lock = lockFactory(createBackend());

Browser (IIFE)

<script src="https://unpkg.com/@universal-lock/memory/dist/index.global.js"></script>
<script src="https://unpkg.com/universal-lock/dist/index.global.js"></script>
<script>
	const lock = UniversalLock.lockFactory(UniversalLockMemory.createBackend());
</script>

API

lockFactory(backend, config?)

Creates a lock instance with the given backend and optional configuration. Returns a Lock object.

const lock = lockFactory(backend, {
	acquireInterval: 250, // retry interval in ms (default: 250)
	acquireFailTimeout: 5000, // max wait before failing acquisition (default: 5000)
	stale: 1000, // ignore locks older than this in ms (default: 1000)
	renewInterval: 250, // lock renewal interval in ms (default: 250)
	maxHoldTime: 2000, // auto-release after this duration in ms (default: 2000)
	onLockLost: (name, reason) => {}, // called when lock is lost (optional)
	onEvent: (event) => {}, // lifecycle events (optional)
});

lock.acquire(lockName)

Acquires a named lock. Returns a release function with a .signal: AbortSignal property. Rejects if the lock cannot be acquired within acquireFailTimeout.

lockDecoratorFactory(lock)

Creates a decorator that wraps async functions with automatic lock acquire/release.

The first argument is either a lock name string or an options object. When a string is passed, the wrapped function keeps its original signature. Pass { lockName, signal: true } to inject an AbortSignal as the first argument so the function can react to lock loss.

import { lockFactory, lockDecoratorFactory } from "universal-lock";
import { createBackend } from "@universal-lock/memory";

const lock = lockFactory(createBackend());
const withLock = lockDecoratorFactory(lock);

// Simple usage — no signal injection
const processOrder = withLock("orders", async (orderId: string) => {
	return await handleOrder(orderId);
});

await processOrder("order-123");

// With signal injection for lock loss detection
const processOrderSafe = withLock({ lockName: "orders", signal: true }, async (signal: AbortSignal, orderId: string) => {
	if (signal.aborted) return;
	return await handleOrder(orderId);
});

await processOrderSafe("order-123");

Lock Loss Detection

AbortSignal

Every release function has a .signal property that is aborted when the lock is lost:

const release = await lock.acquire("my-resource");

release.signal.addEventListener("abort", () => {
	console.log("Lock lost! Stop critical work.");
});

await release();

onLockLost callback

const lock = lockFactory(backend, {
	onLockLost: (lockName, reason) => {
		// reason: "renewFailed" | "timeout"
		console.error(`Lock "${lockName}" lost: ${reason}`);
	},
});

Lifecycle events

const lock = lockFactory(backend, {
	onEvent: (event) => {
		// event.type: "acquired" | "renewed" | "renewFailed" | "lockLost" | "released" | "acquireTimeout"
		console.log(event.type, event.lockName);
	},
});

Custom Backends

Implement the Backend interface to use any storage:

import type { Backend } from "universal-lock";

const myBackend: Backend = {
	setup: async () => {},
	acquire: async (lockName, stale, lockId) => {
		// set lock or throw if already held
	},
	renew: async (lockName, lockId) => {
		// extend lock TTL, verify ownership via lockId
	},
	release: async (lockName, lockId) => {
		// delete lock, verify ownership via lockId
	},
};

License

MIT