@bearly/flock

中文文档
0.2.3发布于 2026/10/30MITTypeScriptES Module构建来源已验证近 30 天下载 9169

Crash-safe fd-held flock ownership for Bun on local macOS and Linux filesystems

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. Descriptor inheritance
  4. Diagnostic bytes
  5. Development

@bearly/flock

Crash-safe, fd-held advisory file locks for Bun and Node 24 on local macOS and Linux filesystems.

@bearly/flock is a small flock(2) binding with three guarantees:

  • the kernel releases ownership after the final descriptor closes, including when its process receives SIGKILL;
  • only EAGAIN / EWOULDBLOCK is reported as contention; other syscall failures throw with the path and errno;
  • optional diagnostic bytes are completely written and fsynced before an acquired handle is returned.

The durable pathname and its bytes are diagnostics, not ownership. The file is intentionally retained after release and may describe the previous owner. Use isFlockHeld() to observe current ownership.

Install

Install from npm. Source installs require Bun or Node 24 with TypeScript stripping.

bun add @bearly/flock

Bun uses its own bun:ffi adapter; Node uses the prebuilt Koffi 3.3.2 adapter. Neither runtime loads the other binding. Koffi install scripts are not trusted; no compiler is required on the supported prebuilt platforms.

Runtime and platformEvidence
Bun, Linux and macOSExisting native contract suite
Node 24, Linux x64Measured inherited-FD and crash-release contract suite
Node 24, Linux arm64 and macOS x64/arm64Upstream prebuilds; unmeasured by us
WindowsUnsupported

Node on macOS requires the same contract suite to pass before a consumer is declared supported. The contract is limited to local filesystems. Network filesystems can implement flock with different semantics and are outside this package's guarantees.

Usage

import { tryAcquireFlock } from "@bearly/flock"

using lock = tryAcquireFlock("/tmp/my-app/writer.lock", {
  body: `${JSON.stringify({ pid: process.pid })}\n`,
})

if (lock === null) throw new Error("another writer owns the resource")
// The lock remains held until this scope closes its fd.

For a synchronous critical section that must wait indefinitely:

import { acquireFlockBlocking } from "@bearly/flock"

using lock = acquireFlockBlocking("/tmp/my-app/migration.lock")
runMigration()

For a finite synchronous wait, acquireFlockWithTimeout(path, timeoutMs, options?) returns a handle or null when the bound expires. It uses one descriptor and monotonic time, parking the whole calling thread between attempts. The caller chooses the bound; cancellation, path policy and error wording stay with it. Existing tryAcquireFlock() still tries immediately without waiting.

Descriptor inheritance

lock.fd is public so a supervisor can pass the acquired descriptor to a child. release() closes only this handle's descriptor; it never calls LOCK_UN, because explicit unlock would also release a lock still owned through an inherited duplicate. The kernel drops ownership after the last descriptor for that open-file description closes.

lock.held reports only this local handle. It becomes false after lock.release() even when an inherited child still owns a duplicate.

Diagnostic bytes

lock.replaceBody(body) truncates, writes every byte, and fsyncs before returning. If truncate, write, or fsync fails, the local claim is closed and the error is thrown. Readers that ignore the flock can still observe an in-progress truncate/write sequence; the guarantee is completion-or-error for the writer, not atomic old-or-new visibility for unlocked readers.

Development

bun run test
# With Node 24 available:
bun run test:node
bun run typecheck
bun run build

版本与发布记录

@bearly/flock 共发布 5 个版本,首次发布于 2026/08/10;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.3latest2026/10/308 天前
0.2.22026/10/021 个月前
0.2.12026/09/291 个月前
0.1.12026/09/181 个月前
0.1.02026/08/102 个月前

关于 @bearly/flock 的常见问题

@bearly/flock 是什么?

Crash-safe fd-held flock ownership for Bun on local macOS and Linux filesystems

如何安装 @bearly/flock?

在项目目录运行 npm install @bearly/flock,或使用 pnpm add / yarn add / bun add。

@bearly/flock 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bearly/flock 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@bearly/flock 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bearly/flock 可以商用吗?

@bearly/flock 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bearly/flock 的下载量有多少?

根据 npm 官方下载统计,@bearly/flock 在 2026/10/08 – 2026/11/06 这 30 天内被下载 9,169 次,日均约 306 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bearly/flock 还在维护吗?

最新版本 0.2.3 发布于 2026/10/30(8 天前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2026/08/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。