Go to file
kingecg d3637a21c5 feat(init): 初始化项目结构和基础代码
添加了异步锁库的基础实现,包括内存锁和文件锁功能。
- 新增 `AsyncLock` 类用于进程内异步加锁
- 新增 `FileLock` 类用于跨进程的文件锁机制
- 添加单元测试覆盖核心逻辑
- 配置 Jest 测试环境并启用覆盖率收集
- 创建 README 文档说明安装、使用方法与 API 详情
- 添加 .gitignore 忽略构建产物及敏感文件
- 添加 MIT 许可证声明

该提交涵盖了项目的初始设置以及基本功能的完整实现。
2025-11-13 22:32:05 +08:00
__tests__ feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
.gitignore feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
LICENSE feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
README.md feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
async-lock.js feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
file-lock.js feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
index.js feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
jest.config.js feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
package.json feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00

README.md

nlocks

A Node.js library providing asynchronous locking mechanisms including in-memory locks and file-based locks for coordinating access to resources across concurrent processes.

Features

  • AsyncLock: In-memory asynchronous lock for coordinating access to resources within a single process
  • FileLock: File-based lock for coordinating access to resources across multiple processes
  • Lightweight and easy to use
  • Promise-based API
  • Comprehensive test coverage

Installation

npm install nlocks

Usage

const { AsyncLock, FileLock } = require('nlocks');

const asyncLock = new AsyncLock();
const fileLock = new FileLock();

AsyncLock

An in-memory lock for synchronizing async operations within a single Node.js process:

const AsyncLock = require('nlocks/async-lock');

const lock = new AsyncLock();

async function protectedOperation() {
  await lock.acquire();
  try {
    // Your critical section code here
    console.log('Performing protected operation');
    await someAsyncWork();
  } finally {
    lock.release();
  }
}

FileLock

A file-based lock for synchronizing operations across multiple Node.js processes:

const FileLock = require('nlocks/file-lock');

const lock = new FileLock();

async function fileProtectedOperation() {
  await lock.acquire('/path/to/your/file.txt');
  try {
    // Your file operation code here
    console.log('Performing file operation');
    await someFileAsyncWork();
  } finally {
    lock.release('/path/to/your/file.txt');
  }
}

API

AsyncLock

  • new AsyncLock() - Creates a new AsyncLock instance
  • acquire(): Promise<void> - Acquires the lock
  • release(): void - Releases the lock

FileLock

  • new FileLock() - Creates a new FileLock instance
  • acquire(filePath): Promise<void> - Acquires a lock for the specified file path
  • release(filePath): void - Releases the lock for the specified file path

Testing

Run the test suite with:

npm test

License

MIT