@lickle/lock 0.0.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +208 -0
  3. package/package.json +100 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Dan Beaven
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,208 @@
1
+ # @lickle/lock
2
+
3
+ [![Build Status](https://img.shields.io/github/actions/workflow/status/Pingid/lickle-lock/CI.yml?branch=main&style=flat&colorA=000000&colorB=000000)](https://github.com/Pingid/lickle-lock/actions?query=workflow:CI)
4
+ [![Build Size](https://img.shields.io/bundlephobia/minzip/@lickle/lock?label=bundle%20size&style=flat&colorA=000000&colorB=000000)](https://bundlephobia.com/result?p=@lickle/lock)
5
+ [![Version](https://img.shields.io/npm/v/@lickle/lock?style=flat&colorA=000000&colorB=000000)](https://www.npmjs.com/package/@lickle/lock)
6
+ [![Downloads](https://img.shields.io/npm/dt/@lickle/lock.svg?style=flat&colorA=000000&colorB=000000)](https://www.npmjs.com/package/@lickle/lock)
7
+
8
+ File-based locking for Node.js using **native OS locks** (`flock` on Unix, `LockFileEx` on Windows).
9
+
10
+ Supports:
11
+
12
+ - **Exclusive (write) locks**
13
+ - **Shared (read) locks**
14
+ - **Cross-process / cross-thread coordination**
15
+ - **Automatic cleanup** on exit, signals, and garbage collection
16
+
17
+ ---
18
+
19
+ ## Install
20
+
21
+ ```bash
22
+ npm install @lickle/lock
23
+ ```
24
+
25
+ ---
26
+
27
+ # Quick Start
28
+
29
+ ```ts
30
+ import { exclusive } from '@lickle/lock'
31
+
32
+ await using guard = await exclusive('/tmp/my.lock')
33
+
34
+ // access the files handle through the guard
35
+ await guard.handle.readFile({ encoding: 'utf-8' })
36
+
37
+ // lock automatically released when guard goes out of scope
38
+ ```
39
+
40
+ Locks work **across processes and worker threads**, making them suitable for coordinating filesystem access.
41
+
42
+ ---
43
+
44
+ # API
45
+
46
+ ## `exclusive(file, options?)`
47
+
48
+ Acquire an **exclusive (write) lock**.
49
+
50
+ Blocks until the lock becomes available.
51
+
52
+ ```ts
53
+ import { exclusive } from '@lickle/lock'
54
+
55
+ const guard = await exclusive('/tmp/my.lock')
56
+
57
+ try {
58
+ // critical section
59
+ } finally {
60
+ await guard.drop()
61
+ }
62
+ ```
63
+
64
+ ---
65
+
66
+ ## `shared(file, options?)`
67
+
68
+ Acquire a **shared (read) lock**.
69
+
70
+ Multiple shared locks can coexist, but they block exclusive locks.
71
+
72
+ ```ts
73
+ import { shared } from '@lickle/lock'
74
+
75
+ const reader1 = await shared('/tmp/data.lock')
76
+ const reader2 = await shared('/tmp/data.lock')
77
+
78
+ await reader1.drop()
79
+ await reader2.drop()
80
+ ```
81
+
82
+ ---
83
+
84
+ ## `tryExclusive(file)`
85
+
86
+ Attempt to acquire an exclusive lock **without waiting**.
87
+
88
+ Returns `undefined` if the lock is already held.
89
+
90
+ ```ts
91
+ import { tryExclusive } from '@lickle/lock'
92
+
93
+ const guard = await tryExclusive('/tmp/my.lock')
94
+
95
+ if (guard) {
96
+ // acquired lock
97
+ await guard.drop()
98
+ }
99
+ ```
100
+
101
+ ---
102
+
103
+ ## `tryShared(file)`
104
+
105
+ Attempt to acquire a shared lock **without waiting**.
106
+
107
+ Returns `undefined` if an exclusive lock is currently held.
108
+
109
+ ```ts
110
+ import { tryShared } from '@lickle/lock'
111
+
112
+ const guard = await tryShared('/tmp/data.lock')
113
+ ```
114
+
115
+ ---
116
+
117
+ # Options
118
+
119
+ Both `exclusive` and `shared` accept:
120
+
121
+ ```ts
122
+ {
123
+ pollMs?: number // polling interval (default: 10ms)
124
+ timeout?: number // max wait time before throwing
125
+ backend?: Backend // custom backend
126
+ }
127
+ ```
128
+
129
+ Example:
130
+
131
+ ```ts
132
+ await exclusive('/tmp/my.lock', {
133
+ pollMs: 20,
134
+ timeout: 1000,
135
+ })
136
+ ```
137
+
138
+ If the lock cannot be acquired within the timeout:
139
+
140
+ ```
141
+ Error: Timed out acquiring lock
142
+ ```
143
+
144
+ ---
145
+
146
+ # FileGuard
147
+
148
+ Lock functions return a **`FileGuard`**.
149
+
150
+ The guard manages the lifetime of the lock.
151
+
152
+ ```ts
153
+ const guard = await exclusive('/tmp/my.lock')
154
+
155
+ guard.handle // fs.promises.FileHandle
156
+ guard.fd // file descriptor
157
+ guard.path // lock file path
158
+ guard.dropped // boolean
159
+
160
+ await guard.drop()
161
+ ```
162
+
163
+ ---
164
+
165
+ ## Automatic cleanup with `await using`
166
+
167
+ `FileGuard` implements the **Explicit Resource Management** proposal.
168
+
169
+ ```ts
170
+ await using guard = await exclusive('/tmp/my.lock')
171
+
172
+ const text = await guard.handle.readFile('utf8')
173
+ ```
174
+
175
+ The lock is automatically released when the scope exits.
176
+
177
+ Spec:
178
+ [https://github.com/tc39/proposal-explicit-resource-management](https://github.com/tc39/proposal-explicit-resource-management)
179
+
180
+ ---
181
+
182
+ # Cleanup Guarantees
183
+
184
+ Locks are released automatically when:
185
+
186
+ - the process exits
187
+ - `SIGINT` or `SIGTERM` is received
188
+ - a worker thread shuts down
189
+ - the guard is garbage collected
190
+
191
+ This prevents stale locks if your program crashes.
192
+
193
+ ---
194
+
195
+ # Backends
196
+
197
+ The default backend uses native OS locks:
198
+
199
+ - **Unix:** `flock`
200
+ - **Windows:** `LockFileEx`
201
+
202
+ You can implement custom backends for alternative file systems or environments.
203
+
204
+ ---
205
+
206
+ # License
207
+
208
+ MIT © Dan Beaven
package/package.json ADDED
@@ -0,0 +1,100 @@
1
+ {
2
+ "name": "@lickle/lock",
3
+ "version": "0.0.1-alpha.0",
4
+ "description": "File-based locking for Node.js using native OS locks (flock on Unix, LockFileEx on Windows). Exclusive and shared locks with cross-process coordination.",
5
+ "author": "Dan Beaven <dm.beaven@gmail.com>",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Pingid/lickle-lock.git"
9
+ },
10
+ "homepage": "https://github.com/Pingid/lickle-lock#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/Pingid/lickle-lock/issues"
13
+ },
14
+ "license": "MIT",
15
+ "type": "module",
16
+ "main": "dist/cjs/index.js",
17
+ "types": "dist/esm/index.d.ts",
18
+ "module": "dist/esm/index.js",
19
+ "sideEffects": false,
20
+ "exports": {
21
+ "./package.json": "./package.json",
22
+ ".": {
23
+ "types": "./dist/esm/index.d.ts",
24
+ "import": "./dist/esm/index.js",
25
+ "require": "./dist/cjs/index.js"
26
+ }
27
+ },
28
+ "imports": {
29
+ "#native": {
30
+ "types": "./index.d.ts",
31
+ "import": "./esm.js",
32
+ "require": "./cjs.js"
33
+ }
34
+ },
35
+ "keywords": [
36
+ "lock",
37
+ "file-lock",
38
+ "flock",
39
+ "mutex",
40
+ "exclusive-lock",
41
+ "shared-lock",
42
+ "read-write-lock",
43
+ "cross-process",
44
+ "napi",
45
+ "native"
46
+ ],
47
+ "files": [
48
+ "dist",
49
+ "index.d.ts",
50
+ "esm.js",
51
+ "cjs.js",
52
+ "README.md",
53
+ "LICENSE"
54
+ ],
55
+ "napi": {
56
+ "binaryName": "lickle-lock",
57
+ "targets": [
58
+ "x86_64-pc-windows-msvc",
59
+ "x86_64-apple-darwin",
60
+ "x86_64-unknown-linux-gnu",
61
+ "aarch64-apple-darwin"
62
+ ]
63
+ },
64
+ "engines": {
65
+ "node": ">= 6.14.2 < 7 || >= 8.11.2 < 9 || >= 9.11.0 < 10 || >= 10.0.0"
66
+ },
67
+ "publishConfig": {
68
+ "registry": "https://registry.npmjs.org/",
69
+ "access": "public"
70
+ },
71
+ "scripts": {
72
+ "artifacts": "napi artifacts",
73
+ "build": "node scripts/build.js",
74
+ "prepublishOnly": "napi prepublish -t npm",
75
+ "typecheck": "tsc --noEmit",
76
+ "test": "vitest",
77
+ "preversion": "napi build --platform && git add .",
78
+ "version": "napi version"
79
+ },
80
+ "devDependencies": {
81
+ "@napi-rs/cli": "^3.2.0",
82
+ "@types/node": "^25.5.0",
83
+ "prettier": "^3.6.2",
84
+ "typescript": "^5.9.2",
85
+ "vitest": "^4.1.0"
86
+ },
87
+ "prettier": {
88
+ "printWidth": 120,
89
+ "semi": false,
90
+ "trailingComma": "all",
91
+ "singleQuote": true,
92
+ "arrowParens": "always"
93
+ },
94
+ "optionalDependencies": {
95
+ "@lickle/lock-win32-x64-msvc": "0.0.1-alpha.0",
96
+ "@lickle/lock-darwin-x64": "0.0.1-alpha.0",
97
+ "@lickle/lock-linux-x64-gnu": "0.0.1-alpha.0",
98
+ "@lickle/lock-darwin-arm64": "0.0.1-alpha.0"
99
+ }
100
+ }