Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
W

web-locks

> 编程语言
Open source

Web Locks API

99 stars0 likes1 views
WebsiteGitHub

About

Web Locks API

Web Locks API

Web Locks API implementation for Node.js based on worker_threads, Atomics, SharedArrayBuffer, asynchronous functions, and queue.

See specification: wicg.github.io/web-locks/ and documentation: developer.mozilla.org/en-US/docs/Web/API/Lock

This implementation is a part of Metarhia technology stack, needed for the first pilot project of Node.js application server based on parallel programming and workload micro-isolation. Web Locks API is intended to be merged into Node.js in future.

Features

  • Simplest parallel programming primitive to solve a problem of data races and race conditions.
  • Node.js and worker_threads support.
  • Unified API for single-threaded asynchronous locks and multi-threaded locks.
  • Exclusive lock mode (default).
  • AbortSignal support to cancel a lock request while waiting.
  • TypeScript typings included.

Installation

bash
$ npm install web-locks

Usage

javascript
const { locks, AbortController } = require('web-locks');

// Exclusive lock (default)
await locks.request('Resource name', async (lock) => {
  // use named resource; it is released after the callback settles
});

// Cancel a waiting request with AbortSignal
const controller = new AbortController();
const pending = locks.request(
  'Resource name',
  { signal: controller.signal },
  async () => {
    // critical section
  },
);
controller.abort(); // rejects pending with AbortError

Attach workers so locks are coordinated across threads:

javascript
const { Worker } = require('worker_threads');
const { locks } = require('web-locks');

const worker = new Worker('./worker.js');
locks.attach(worker);

API

locks.request(name, handler): Promise

locks.request(name, options, handler): Promise

  • name: string — resource name
  • options.mode?: 'exclusive' — only exclusive is supported
  • options.signal?: AbortSignal — abort waiting for the lock
  • handler: (lock: Lock) => void | Promise — runs while holding the lock

Already-aborted signals reject immediately. Aborting while waiting cancels the queued request and rejects with AbortError (or signal.reason when set).

locks.query(): Promise

  • held: Array — currently held locks
  • pending: Array — queued lock requests

locks.attach(worker): void

Registers a worker_threads.Worker for cross-thread lock coordination.

Exports

  • locks: LockManager
  • AbortController, AbortSignal, AbortError — native when available, otherwise a small polyfill

License

This implementation of Web Locks API is MIT licensed.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptasyncatomicsawaitdata-race

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言