@tslock/in-memory
v2.0.1
Published
TSLock in-memory provider — test/local use only
Readme
@tslock/in-memory
In-memory lock provider for TSLock — testing and local development only.
⚠️ Not for production distributed locking. This provider locks only within a single Node.js process. Multiple instances of your application will each have their own
Mapand will not coordinate. Use this for unit tests, local development, and single-process demos. For any deployment with more than one instance, use a real distributed backend — see the provider matrix.
A TSLock provider backed by a plain Map<string, number>. Node.js is single-threaded, so no synchronization is needed. It implements ExtensibleLockProvider, so it supports extend().
Installation
pnpm add @tslock/core @tslock/in-memoryUsage
import { createLockConfig, DefaultLockingTaskExecutor } from '@tslock/core';
import { InMemoryLockProvider } from '@tslock/in-memory';
const provider = new InMemoryLockProvider();
const executor = new DefaultLockingTaskExecutor(provider);
await executor.executeWithLock(
() => myScheduledTask(),
createLockConfig({ name: 'my-task', lockAtMostFor: '5m', lockAtLeastFor: '1m' }),
);It's also handy for unit-testing code that depends on a LockProvider without standing up a real backend:
const provider = new InMemoryLockProvider();
provider.isLocked('my-task'); // false until a lock is heldIntegration tests
The in-memory provider runs the shared lock, extension, and fuzz contracts without external services:
pnpm --filter @tslock/in-memory test:integrationRequirements
- Node.js >= 22
License
Apache 2.0 — see LICENSE for details.
