@lickle/lock 0.0.1-alpha.0 → 0.0.1-alpha.2
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.
- package/README.md +182 -76
- package/cjs.js +583 -0
- package/dist/cjs/core/guard.d.ts +50 -0
- package/dist/cjs/core/guard.d.ts.map +1 -0
- package/dist/cjs/core/guard.js +91 -0
- package/dist/cjs/core/guard.js.map +1 -0
- package/dist/cjs/core/index.d.ts +8 -0
- package/dist/cjs/core/index.d.ts.map +1 -0
- package/dist/cjs/core/index.js +41 -0
- package/dist/cjs/core/index.js.map +1 -0
- package/dist/cjs/core/locking.d.ts +23 -0
- package/dist/cjs/core/locking.d.ts.map +1 -0
- package/dist/cjs/core/locking.js +93 -0
- package/dist/cjs/core/locking.js.map +1 -0
- package/dist/cjs/core/node.d.ts +19 -0
- package/dist/cjs/core/node.d.ts.map +1 -0
- package/dist/cjs/core/node.js +69 -0
- package/dist/cjs/core/node.js.map +1 -0
- package/dist/cjs/core/types.d.ts +49 -0
- package/dist/cjs/core/types.d.ts.map +1 -0
- package/dist/cjs/core/types.js +13 -0
- package/dist/cjs/core/types.js.map +1 -0
- package/dist/cjs/index.d.ts +63 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +110 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/core/guard.d.ts +50 -0
- package/dist/esm/core/guard.d.ts.map +1 -0
- package/dist/esm/core/guard.js +86 -0
- package/dist/esm/core/guard.js.map +1 -0
- package/dist/esm/core/index.d.ts +8 -0
- package/dist/esm/core/index.d.ts.map +1 -0
- package/dist/esm/core/index.js +21 -0
- package/dist/esm/core/index.js.map +1 -0
- package/dist/esm/core/locking.d.ts +23 -0
- package/dist/esm/core/locking.d.ts.map +1 -0
- package/dist/esm/core/locking.js +56 -0
- package/dist/esm/core/locking.js.map +1 -0
- package/dist/esm/core/node.d.ts +19 -0
- package/dist/esm/core/node.d.ts.map +1 -0
- package/dist/esm/core/node.js +61 -0
- package/dist/esm/core/node.js.map +1 -0
- package/dist/esm/core/types.d.ts +49 -0
- package/dist/esm/core/types.d.ts.map +1 -0
- package/dist/esm/core/types.js +9 -0
- package/dist/esm/core/types.js.map +1 -0
- package/dist/esm/index.d.ts +63 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +88 -0
- package/dist/esm/index.js.map +1 -0
- package/esm.js +587 -0
- package/index.d.ts +40 -0
- package/package.json +6 -6
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type FileDescriptor, type FileHandle, type Locker, type LockRange } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Guard that holds a lock without owning the file handle.
|
|
4
|
+
* Releasing the guard unlocks the file but does not close it.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const guard = await lock(handle, Lock.Exclusive)
|
|
8
|
+
* try { /* work *\/ } finally { await guard.drop() }
|
|
9
|
+
*/
|
|
10
|
+
export declare class LockGuard<H extends FileDescriptor> {
|
|
11
|
+
private locking;
|
|
12
|
+
private range?;
|
|
13
|
+
private _dropped;
|
|
14
|
+
/** The file descriptor number. */
|
|
15
|
+
readonly fd: number;
|
|
16
|
+
constructor(locking: Locker, handle: H, range?: LockRange | undefined);
|
|
17
|
+
/** True if the lock has been released. */
|
|
18
|
+
get dropped(): boolean;
|
|
19
|
+
/** Release the lock. Safe to call multiple times. */
|
|
20
|
+
drop(): Promise<void>;
|
|
21
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* RAII guard that holds a file lock. Unlocks and closes the file on drop.
|
|
25
|
+
* Supports `await using` via `Symbol.asyncDispose`.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* await using guard = await exclusive('/tmp/my.lock')
|
|
29
|
+
* // lock is released when guard goes out of scope
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* const guard = await exclusive('/tmp/my.lock')
|
|
33
|
+
* try { /* work *\/ } finally { await guard.drop() }
|
|
34
|
+
*/
|
|
35
|
+
export declare class FileLockGuard<H extends FileHandle> {
|
|
36
|
+
private guard;
|
|
37
|
+
private hndl;
|
|
38
|
+
private _dropped;
|
|
39
|
+
/** The file descriptor number. */
|
|
40
|
+
readonly fd: number;
|
|
41
|
+
constructor(guard: LockGuard<H>, hndl: H);
|
|
42
|
+
/** The underlying file handle. Throws if the guard has been dropped. */
|
|
43
|
+
get handle(): H;
|
|
44
|
+
/** True if the lock has been released. */
|
|
45
|
+
get dropped(): boolean;
|
|
46
|
+
/** Release the lock and close the file. Safe to call multiple times. */
|
|
47
|
+
drop(): Promise<void>;
|
|
48
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guard.d.ts","sourceRoot":"","sources":["../../../lib/core/guard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,UAAU,EAAE,KAAK,MAAM,EAAE,KAAK,SAAS,EAAa,MAAM,YAAY,CAAA;AAEzG;;;;;;;GAOG;AACH,qBAAa,SAAS,CAAC,CAAC,SAAS,cAAc;IAM3C,OAAO,CAAC,OAAO;IAEf,OAAO,CAAC,KAAK,CAAC;IAPhB,OAAO,CAAC,QAAQ,CAA2B;IAC3C,kCAAkC;IAClC,SAAgB,EAAE,EAAE,MAAM,CAAA;gBAGhB,OAAO,EAAE,MAAM,EACvB,MAAM,EAAE,CAAC,EACD,KAAK,CAAC,EAAE,SAAS,YAAA;IAK3B,0CAA0C;IAC1C,IAAI,OAAO,YAEV;IAED,qDAAqD;IACrD,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAKrB,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;CAGvC;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,aAAa,CAAC,CAAC,SAAS,UAAU;IAM3C,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,IAAI;IANd,OAAO,CAAC,QAAQ,CAA2B;IAC3C,kCAAkC;IAClC,SAAgB,EAAE,EAAE,MAAM,CAAA;gBAGhB,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,EACnB,IAAI,EAAE,CAAC;IAKjB,wEAAwE;IACxE,IAAI,MAAM,IAAI,CAAC,CAGd;IAED,0CAA0C;IAC1C,IAAI,OAAO,YAEV;IAED,wEAAwE;IACxE,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAarB,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;CAGvC"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { resolveFd } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Guard that holds a lock without owning the file handle.
|
|
4
|
+
* Releasing the guard unlocks the file but does not close it.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const guard = await lock(handle, Lock.Exclusive)
|
|
8
|
+
* try { /* work *\/ } finally { await guard.drop() }
|
|
9
|
+
*/
|
|
10
|
+
export class LockGuard {
|
|
11
|
+
locking;
|
|
12
|
+
range;
|
|
13
|
+
_dropped;
|
|
14
|
+
/** The file descriptor number. */
|
|
15
|
+
fd;
|
|
16
|
+
constructor(locking, handle, range) {
|
|
17
|
+
this.locking = locking;
|
|
18
|
+
this.range = range;
|
|
19
|
+
this.fd = resolveFd(handle);
|
|
20
|
+
}
|
|
21
|
+
/** True if the lock has been released. */
|
|
22
|
+
get dropped() {
|
|
23
|
+
return !!this._dropped;
|
|
24
|
+
}
|
|
25
|
+
/** Release the lock. Safe to call multiple times. */
|
|
26
|
+
drop() {
|
|
27
|
+
if (!this._dropped)
|
|
28
|
+
this._dropped = this.locking.unlock(this.fd, this.range);
|
|
29
|
+
return this._dropped;
|
|
30
|
+
}
|
|
31
|
+
[Symbol.asyncDispose]() {
|
|
32
|
+
return this.drop();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* RAII guard that holds a file lock. Unlocks and closes the file on drop.
|
|
37
|
+
* Supports `await using` via `Symbol.asyncDispose`.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* await using guard = await exclusive('/tmp/my.lock')
|
|
41
|
+
* // lock is released when guard goes out of scope
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* const guard = await exclusive('/tmp/my.lock')
|
|
45
|
+
* try { /* work *\/ } finally { await guard.drop() }
|
|
46
|
+
*/
|
|
47
|
+
export class FileLockGuard {
|
|
48
|
+
guard;
|
|
49
|
+
hndl;
|
|
50
|
+
_dropped;
|
|
51
|
+
/** The file descriptor number. */
|
|
52
|
+
fd;
|
|
53
|
+
constructor(guard, hndl) {
|
|
54
|
+
this.guard = guard;
|
|
55
|
+
this.hndl = hndl;
|
|
56
|
+
this.fd = hndl.fd;
|
|
57
|
+
}
|
|
58
|
+
/** The underlying file handle. Throws if the guard has been dropped. */
|
|
59
|
+
get handle() {
|
|
60
|
+
if (this._dropped)
|
|
61
|
+
throw new Error('FileGuard has been dropped');
|
|
62
|
+
return this.hndl;
|
|
63
|
+
}
|
|
64
|
+
/** True if the lock has been released. */
|
|
65
|
+
get dropped() {
|
|
66
|
+
return !!this._dropped;
|
|
67
|
+
}
|
|
68
|
+
/** Release the lock and close the file. Safe to call multiple times. */
|
|
69
|
+
drop() {
|
|
70
|
+
if (!this._dropped) {
|
|
71
|
+
this._dropped = (async () => {
|
|
72
|
+
try {
|
|
73
|
+
await this.guard.drop();
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
await this.hndl.close();
|
|
77
|
+
}
|
|
78
|
+
})();
|
|
79
|
+
}
|
|
80
|
+
return this._dropped;
|
|
81
|
+
}
|
|
82
|
+
[Symbol.asyncDispose]() {
|
|
83
|
+
return this.drop();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//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K,CAAC,4BAA4B,CAAC,CAAA;QAChE,OAAO,IAAI,CAAC,IAAI,CAAA;IAClB,CAAC;IAED,0CAA0C;IAC1C,IAAI,OAAO;QACT,OAAO,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAA;IACxB,CAAC;IAED,wEAAwE;IACxE,IAAI;QACF,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnB,IAAI,CAAC,QAAQ,GAAG,CAAC,KAAK,IAAI,EAAE;gBAC1B,IAAI,CAAC;oBACH,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAA;gBACzB,CAAC;wBAAS,CAAC;oBACT,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAA;gBACzB,CAAC;YACH,CAAC,CAAC,EAAE,CAAA;QACN,CAAC;QACD,OAAO,IAAI,CAAC,QAAQ,CAAA;IACtB,CAAC;IAED,CAAC,MAAM,CAAC,YAAY,CAAC;QACnB,OAAO,IAAI,CAAC,IAAI,EAAE,CAAA;IACpB,CAAC;CACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Locker, type Fs } from './types.js';
|
|
2
|
+
export { type Fs as Backend, type LockRange, type PollOptions, Lock, type FileDescriptor, type FileHandle, type Locker, type Fs, } from './types.js';
|
|
3
|
+
export * from './locking.js';
|
|
4
|
+
export * from './guard.js';
|
|
5
|
+
export * from './node.js';
|
|
6
|
+
export declare const defaultFs: () => Fs<import("node:fs/promises").FileHandle>;
|
|
7
|
+
export declare const defaultLocking: () => Locker;
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../lib/core/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,MAAM,YAAY,CAAA;AAG5C,OAAO,EACL,KAAK,EAAE,IAAI,OAAO,EAClB,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,IAAI,EACJ,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,MAAM,EACX,KAAK,EAAE,GACR,MAAM,YAAY,CAAA;AACnB,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AAIzB,eAAO,MAAM,SAAS,iDAGrB,CAAA;AAID,eAAO,MAAM,cAAc,cAG1B,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createNodeFs, NodeLockHooks } from './node.js';
|
|
2
|
+
import { createLocker } from './locking.js';
|
|
3
|
+
export { Lock, } from './types.js';
|
|
4
|
+
export * from './locking.js';
|
|
5
|
+
export * from './guard.js';
|
|
6
|
+
export * from './node.js';
|
|
7
|
+
/** Lazily create and return the shared default Node.js filesystem singleton. */
|
|
8
|
+
let _defaultFs;
|
|
9
|
+
export const defaultFs = () => {
|
|
10
|
+
if (!_defaultFs)
|
|
11
|
+
_defaultFs = createNodeFs();
|
|
12
|
+
return _defaultFs;
|
|
13
|
+
};
|
|
14
|
+
/** Lazily create and return the shared default native locker singleton. */
|
|
15
|
+
let _defaultLocking;
|
|
16
|
+
export const defaultLocking = () => {
|
|
17
|
+
if (!_defaultLocking)
|
|
18
|
+
_defaultLocking = createLocker(new NodeLockHooks());
|
|
19
|
+
return _defaultLocking;
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../lib/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAuB,MAAM,WAAW,CAAA;AAE5E,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAE3C,OAAO,EAIL,IAAI,GAKL,MAAM,YAAY,CAAA;AACnB,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AAEzB,gFAAgF;AAChF,IAAI,UAA0C,CAAA;AAC9C,MAAM,CAAC,MAAM,SAAS,GAAG,GAAG,EAAE;IAC5B,IAAI,CAAC,UAAU;QAAE,UAAU,GAAG,YAAY,EAAE,CAAA;IAC5C,OAAO,UAAU,CAAA;AACnB,CAAC,CAAA;AAED,2EAA2E;AAC3E,IAAI,eAAmC,CAAA;AACvC,MAAM,CAAC,MAAM,cAAc,GAAG,GAAG,EAAE;IACjC,IAAI,CAAC,eAAe;QAAE,eAAe,GAAG,YAAY,CAAC,IAAI,aAAa,EAAE,CAAC,CAAA;IACzE,OAAO,eAAe,CAAA;AACxB,CAAC,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type FileDescriptor, type Locker } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Lifecycle hooks called when locks are acquired and released.
|
|
4
|
+
* Used by backends to track active locks for cleanup on exit or signals.
|
|
5
|
+
*/
|
|
6
|
+
export interface LockHooks {
|
|
7
|
+
/** Called after a lock is successfully acquired. */
|
|
8
|
+
register: (fd: FileDescriptor) => Promise<void>;
|
|
9
|
+
/** Called when a lock is released (even if unlock itself fails). */
|
|
10
|
+
unregister: (fd: FileDescriptor) => Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Create a {@link Locker} backed by native OS locks.
|
|
14
|
+
*
|
|
15
|
+
* Whole-file locks use `flock(2)` on Unix and `LockFileEx` on Windows.
|
|
16
|
+
* Range locks use `fcntl(2)` OFD locks on Linux, POSIX record locks on
|
|
17
|
+
* macOS/BSD, and ranged `LockFileEx` on Windows.
|
|
18
|
+
*
|
|
19
|
+
* If `hooks` are provided they are called after lock acquisition and on
|
|
20
|
+
* release, allowing backends to track active locks for cleanup.
|
|
21
|
+
*/
|
|
22
|
+
export declare const createLocker: (hooks?: LockHooks) => Locker;
|
|
23
|
+
//# sourceMappingURL=locking.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"locking.d.ts","sourceRoot":"","sources":["../../../lib/core/locking.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoC,KAAK,cAAc,EAAE,KAAK,MAAM,EAAmB,MAAM,YAAY,CAAA;AAGhH;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,oDAAoD;IACpD,QAAQ,EAAE,CAAC,EAAE,EAAE,cAAc,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAC/C,oEAAoE;IACpE,UAAU,EAAE,CAAC,EAAE,EAAE,cAAc,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAClD;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,YAAY,GAAI,QAAQ,SAAS,KAAG,MAqC/C,CAAA"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Lock, resolveFd } from './types.js';
|
|
2
|
+
import * as native from '#native';
|
|
3
|
+
/**
|
|
4
|
+
* Create a {@link Locker} backed by native OS locks.
|
|
5
|
+
*
|
|
6
|
+
* Whole-file locks use `flock(2)` on Unix and `LockFileEx` on Windows.
|
|
7
|
+
* Range locks use `fcntl(2)` OFD locks on Linux, POSIX record locks on
|
|
8
|
+
* macOS/BSD, and ranged `LockFileEx` on Windows.
|
|
9
|
+
*
|
|
10
|
+
* If `hooks` are provided they are called after lock acquisition and on
|
|
11
|
+
* release, allowing backends to track active locks for cleanup.
|
|
12
|
+
*/
|
|
13
|
+
export const createLocker = (hooks) => ({
|
|
14
|
+
async lock(fd, type, range, options) {
|
|
15
|
+
const d = resolveFd(fd);
|
|
16
|
+
await native.lock(d, toNativeLock(type), range, options);
|
|
17
|
+
try {
|
|
18
|
+
await hooks?.register(d);
|
|
19
|
+
}
|
|
20
|
+
catch (err) {
|
|
21
|
+
try {
|
|
22
|
+
native.lockSync(d, native.Lock.Unlock, range);
|
|
23
|
+
}
|
|
24
|
+
catch { }
|
|
25
|
+
throw err;
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
async tryLock(fd, type, range) {
|
|
29
|
+
const d = resolveFd(fd);
|
|
30
|
+
const locked = await native.tryLock(d, toNativeLock(type), range);
|
|
31
|
+
if (!locked)
|
|
32
|
+
return false;
|
|
33
|
+
try {
|
|
34
|
+
await hooks?.register(d);
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
try {
|
|
39
|
+
native.lockSync(d, native.Lock.Unlock, range);
|
|
40
|
+
}
|
|
41
|
+
catch { }
|
|
42
|
+
throw err;
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
async unlock(fd, range) {
|
|
46
|
+
const d = resolveFd(fd);
|
|
47
|
+
try {
|
|
48
|
+
native.lockSync(d, native.Lock.Unlock, range);
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
await hooks?.unregister(d);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
const toNativeLock = (type) => (type === Lock.Exclusive ? native.Lock.Exclusive : native.Lock.Shared);
|
|
56
|
+
//# sourceMappingURL=locking.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"locking.js","sourceRoot":"","sources":["../../../lib/core/locking.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsE,IAAI,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAChH,OAAO,KAAK,MAAM,MAAM,SAAS,CAAA;AAajC;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAiB,EAAU,EAAE,CAAC,CAAC;IAC1D,KAAK,CAAC,IAAI,CAAC,EAAkB,EAAE,IAAU,EAAE,KAAiB,EAAE,OAAqB;QACjF,MAAM,CAAC,GAAG,SAAS,CAAC,EAAE,CAAC,CAAA;QACvB,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,YAAY,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAA;QACxD,IAAI,CAAC;YACH,MAAM,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,CA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AC,IAAU,EAAe,EAAE,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { type Fs, FileDescriptor } from './types.js';
|
|
3
|
+
import { type LockHooks } from './locking.js';
|
|
4
|
+
/** Node.js file handle type alias. */
|
|
5
|
+
export type NodeFileHandle = fs.promises.FileHandle;
|
|
6
|
+
/** Create a Node.js filesystem that opens files with appropriate flags for the lock type. */
|
|
7
|
+
export declare const createNodeFs: () => Fs<NodeFileHandle>;
|
|
8
|
+
/**
|
|
9
|
+
* Track active locks by file descriptor and ensure they are released on process exit.
|
|
10
|
+
*/
|
|
11
|
+
export declare class NodeLockHooks implements LockHooks {
|
|
12
|
+
private _active;
|
|
13
|
+
register(fd: FileDescriptor): Promise<void>;
|
|
14
|
+
unregister(fd: FileDescriptor): Promise<void>;
|
|
15
|
+
private _listen;
|
|
16
|
+
private _unlisten;
|
|
17
|
+
private _onExit;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=node.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../../lib/core/node.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,SAAS,CAAA;AAExB,OAAO,EAAE,KAAK,EAAE,EAAQ,cAAc,EAAa,MAAM,YAAY,CAAA;AACrE,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,cAAc,CAAA;AAE7C,sCAAsC;AACtC,MAAM,MAAM,cAAc,GAAG,EAAE,CAAC,QAAQ,CAAC,UAAU,CAAA;AAEnD,6FAA6F;AAC7F,eAAO,MAAM,YAAY,QAAO,EAAE,CAAC,cAAc,CAQhD,CAAA;AAED;;GAEG;AACH,qBAAa,aAAc,YAAW,SAAS;IAC7C,OAAO,CAAC,OAAO,CAA4B;IAErC,QAAQ,CAAC,EAAE,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAM3C,UAAU,CAAC,EAAE,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAWnD,OAAO,CAAC,OAAO;IAKf,OAAO,CAAC,SAAS;IAKjB,OAAO,CAAC,OAAO,CASd;CACF"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { isMainThread, parentPort } from 'node:worker_threads';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { Lock, resolveFd } from './types.js';
|
|
4
|
+
/** Create a Node.js filesystem that opens files with appropriate flags for the lock type. */
|
|
5
|
+
export const createNodeFs = () => {
|
|
6
|
+
return {
|
|
7
|
+
async open(file, type) {
|
|
8
|
+
const flags = type === Lock.Exclusive ? fs.constants.O_CREAT | fs.constants.O_RDWR : fs.constants.O_RDONLY;
|
|
9
|
+
const handle = await fs.promises.open(file, flags);
|
|
10
|
+
return handle;
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Track active locks by file descriptor and ensure they are released on process exit.
|
|
16
|
+
*/
|
|
17
|
+
export class NodeLockHooks {
|
|
18
|
+
_active = new Map();
|
|
19
|
+
async register(fd) {
|
|
20
|
+
if (this._active.size === 0)
|
|
21
|
+
this._listen();
|
|
22
|
+
const d = resolveFd(fd);
|
|
23
|
+
this._active.set(d, (this._active.get(d) ?? 0) + 1);
|
|
24
|
+
}
|
|
25
|
+
async unregister(fd) {
|
|
26
|
+
const d = resolveFd(fd);
|
|
27
|
+
const count = this._active.get(d);
|
|
28
|
+
if (count !== undefined) {
|
|
29
|
+
if (count <= 1)
|
|
30
|
+
this._active.delete(d);
|
|
31
|
+
else
|
|
32
|
+
this._active.set(d, count - 1);
|
|
33
|
+
}
|
|
34
|
+
if (this._active.size === 0)
|
|
35
|
+
this._unlisten();
|
|
36
|
+
}
|
|
37
|
+
_listen() {
|
|
38
|
+
if (isMainThread)
|
|
39
|
+
process.on('exit', this._onExit);
|
|
40
|
+
else
|
|
41
|
+
parentPort?.on('close', this._onExit);
|
|
42
|
+
}
|
|
43
|
+
_unlisten() {
|
|
44
|
+
if (isMainThread)
|
|
45
|
+
process.off('exit', this._onExit);
|
|
46
|
+
else
|
|
47
|
+
parentPort?.off('close', this._onExit);
|
|
48
|
+
}
|
|
49
|
+
_onExit = () => {
|
|
50
|
+
for (const fd of Array.from(this._active.keys()).reverse()) {
|
|
51
|
+
try {
|
|
52
|
+
fs.closeSync(fd);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
// EBADF expected if already closed
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
this._active.clear();
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.js","sourceRoot":"","sources":["../../../lib/core/node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAC9D,OAAO,EAAE,MAAM,SAAS,CAAA;AAExB,OAAO,EAAW,IAAI,EAAkB,SAAS,EAAE,MAAM,YAAY,CAAA;AAMrE,6FAA6F;AAC7F,MAAM,CAAC,MAAM,YAAY,GAAG,GAAuB,EAAE;IACnD,OAAO;QACL,KAAK,CAAC,IAAI,CAAC,IAAY,EAAE,IAAU;YACjC,MAAM,KAAK,GAAG,IAAI,KAAK,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,OAAO,GAAG,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,QAAQ,CAAA;YAC1G,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;YA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CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAA;;YAC9C,UAAU,EAAE,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,CAAA;IAC7C,CAAC;IAEO,OAAO,GAAG,GAAS,EAAE;QAC3B,KAAK,MAAM,EAAE,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC;YAC3D,IAAI,CAAC;gBACH,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;YAClB,CAAC;YAAC,MAAM,CAAC;gBACP,mCAAmC;YACrC,CAAC;QACH,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAA;IACtB,CAAC,CAAA;CACF"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Filesystem abstraction for opening lock files. */
|
|
2
|
+
export interface Fs<H extends FileHandle> {
|
|
3
|
+
/** Open (or create) the lock file and return a handle. */
|
|
4
|
+
open(file: string, type: Lock): Promise<H>;
|
|
5
|
+
}
|
|
6
|
+
/** Lock/unlock operations, separated from file opening for composability. */
|
|
7
|
+
export interface Locker {
|
|
8
|
+
/** Acquire a lock, polling until available or timeout is reached. */
|
|
9
|
+
lock: (fd: FileDescriptor, type: Lock, range?: LockRange, options?: PollOptions) => Promise<void>;
|
|
10
|
+
/** Try to acquire a lock without waiting. Return true if acquired. */
|
|
11
|
+
tryLock: (fd: FileDescriptor, type: Lock, range?: LockRange) => Promise<boolean>;
|
|
12
|
+
/** Release a previously acquired lock. */
|
|
13
|
+
unlock: (fd: FileDescriptor, range?: LockRange) => Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
/** Lock mode: exclusive (write) or shared (read). */
|
|
16
|
+
export declare enum Lock {
|
|
17
|
+
Exclusive = 0,
|
|
18
|
+
Shared = 1
|
|
19
|
+
}
|
|
20
|
+
/** Options for polling lock acquisition. */
|
|
21
|
+
export interface PollOptions {
|
|
22
|
+
/** Polling interval in milliseconds. */
|
|
23
|
+
pollMs?: number;
|
|
24
|
+
/** Maximum wait time in milliseconds. */
|
|
25
|
+
timeout?: number;
|
|
26
|
+
/** Multiplier applied to pollMs after each failed attempt (e.g. 2 = exponential backoff). */
|
|
27
|
+
backoff?: number;
|
|
28
|
+
}
|
|
29
|
+
/** Byte range within a file to lock. Used for range-level locking. */
|
|
30
|
+
export interface LockRange {
|
|
31
|
+
/** Starting byte offset. */
|
|
32
|
+
offset: number;
|
|
33
|
+
/** Number of bytes to lock. */
|
|
34
|
+
length: number;
|
|
35
|
+
}
|
|
36
|
+
/** An open file handle with a descriptor and a close method. */
|
|
37
|
+
export interface FileHandle {
|
|
38
|
+
/** The numeric file descriptor. */
|
|
39
|
+
fd: number;
|
|
40
|
+
/** Close the file handle. */
|
|
41
|
+
close: () => Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
/** File descriptor or file descriptor object. */
|
|
44
|
+
export type FileDescriptor = {
|
|
45
|
+
fd: number;
|
|
46
|
+
} | number;
|
|
47
|
+
/** @internal */
|
|
48
|
+
export declare const resolveFd: (fd: FileDescriptor) => number;
|
|
49
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../lib/core/types.ts"],"names":[],"mappings":"AAAA,qDAAqD;AACrD,MAAM,WAAW,EAAE,CAAC,CAAC,SAAS,UAAU;IACtC,0DAA0D;IAC1D,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;CAC3C;AAED,6EAA6E;AAC7E,MAAM,WAAW,MAAM;IACrB,qEAAqE;IACrE,IAAI,EAAE,CAAC,EAAE,EAAE,cAAc,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACjG,sEAAsE;IACtE,OAAO,EAAE,CAAC,EAAE,EAAE,cAAc,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,EAAE,SAAS,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAChF,0CAA0C;IAC1C,MAAM,EAAE,CAAC,EAAE,EAAE,cAAc,EAAE,KAAK,CAAC,EAAE,SAAS,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CACjE;AAED,qDAAqD;AACrD,oBAAY,IAAI;IACd,SAAS,IAAA;IACT,MAAM,IAAA;CACP;AAED,4CAA4C;AAC5C,MAAM,WAAW,WAAW;IAC1B,wCAAwC;IACxC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,yCAAyC;IACzC,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,6FAA6F;IAC7F,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,sEAAsE;AACtE,MAAM,WAAW,SAAS;IACxB,4BAA4B;IAC5B,MAAM,EAAE,MAAM,CAAA;IACd,+BAA+B;IAC/B,MAAM,EAAE,MAAM,CAAA;CACf;AAED,gEAAgE;AAChE,MAAM,WAAW,UAAU;IACzB,mCAAmC;IACnC,EAAE,EAAE,MAAM,CAAA;IACV,6BAA6B;IAC7B,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC3B;AAED,iDAAiD;AACjD,MAAM,MAAM,cAAc,GAAG;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAA;AAEpD,gBAAgB;AAChB,eAAO,MAAM,SAAS,GAAI,IAAI,cAAc,KAAG,MAA+C,CAAA"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Lock mode: exclusive (write) or shared (read). */
|
|
2
|
+
export var Lock;
|
|
3
|
+
(function (Lock) {
|
|
4
|
+
Lock[Lock["Exclusive"] = 0] = "Exclusive";
|
|
5
|
+
Lock[Lock["Shared"] = 1] = "Shared";
|
|
6
|
+
})(Lock || (Lock = {}));
|
|
7
|
+
/** @internal */
|
|
8
|
+
export const resolveFd = (fd) => (typeof fd === 'number' ? fd : fd.fd);
|
|
9
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../lib/core/types.ts"],"names":[],"mappings":"AAgBA,qDAAqD;AACrD,MAAM,CAAN,IAAY,IAGX;AAHD,WAAY,IAAI;IACd,yCAAS,CAAA;IACT,mCAAM,CAAA;AACR,CAAC,EAHW,IAAI,KAAJ,IAAI,QAGf;AA+BD,gBAAgB;AAChB,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,EAAkB,EAAU,EAAE,CAAC,CAAC,OAAO,EAAE,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAA"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type NodeFileHandle, type LockRange, type PollOptions, type Lock, type FileHandle, type FileDescriptor, type Fs, type Locker, FileLockGuard, LockGuard } from './core/index.js';
|
|
2
|
+
export * from './core/index.js';
|
|
3
|
+
/** Options for specifying a custom locker and/or byte range. */
|
|
4
|
+
export interface LockingOptions {
|
|
5
|
+
/** Custom locker implementation. Uses the default native locker if omitted. */
|
|
6
|
+
locking?: Locker;
|
|
7
|
+
/** Byte range to lock. Locks the entire file if omitted. */
|
|
8
|
+
range?: LockRange;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Acquire a lock on an already-open file handle.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* import fs from 'node:fs/promises'
|
|
15
|
+
* const handle = await fs.open('/tmp/my.lock', 'r+')
|
|
16
|
+
* await using guard = await lock(handle, Lock.Exclusive)
|
|
17
|
+
*/
|
|
18
|
+
export declare const lock: <H extends FileHandle = NodeFileHandle>(handle: H, type: Lock, options?: LockingOptions & PollOptions) => Promise<LockGuard<H>>;
|
|
19
|
+
/**
|
|
20
|
+
* Try to acquire a lock on an already-open file handle without waiting.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* import fs from 'node:fs/promises'
|
|
24
|
+
* const handle = await fs.open('/tmp/my.lock', 'r+')
|
|
25
|
+
* const guard = await tryLock(handle, Lock.Exclusive)
|
|
26
|
+
* if (guard) { /* acquired *\/ }
|
|
27
|
+
*/
|
|
28
|
+
export declare const tryLock: <H extends FileDescriptor>(handle: H, type: Lock, options?: LockingOptions & PollOptions) => Promise<LockGuard<H> | undefined>;
|
|
29
|
+
/**
|
|
30
|
+
* Release a lock on a file descriptor or file handle.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* import fs from 'node:fs/promises'
|
|
34
|
+
* const handle = await fs.open('/tmp/my.lock', 'r+')
|
|
35
|
+
* await lock(handle, Lock.Exclusive)
|
|
36
|
+
* // ... critical section ...
|
|
37
|
+
* await unlock(handle)
|
|
38
|
+
*/
|
|
39
|
+
export declare const unlock: <H extends FileDescriptor>(handle: H, options?: LockingOptions) => Promise<void>;
|
|
40
|
+
/** Options for open-and-lock functions that manage the file lifecycle. */
|
|
41
|
+
export interface OpenLockOptions<H extends FileHandle> extends LockingOptions {
|
|
42
|
+
/** Custom filesystem for opening files. Uses the default Node.js fs if omitted. */
|
|
43
|
+
fs?: Fs<H>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Open a file and acquire a lock, polling until available. Closes the file on failure.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* await using guard = await openLock('/tmp/my.lock', Lock.Exclusive)
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* await using guard = await openLock('/tmp/my.lock', Lock.Shared, { timeout: 5000 })
|
|
53
|
+
*/
|
|
54
|
+
export declare const openLock: <H extends FileHandle = NodeFileHandle>(file: string, type: Lock, options?: OpenLockOptions<H> & PollOptions) => Promise<FileLockGuard<H>>;
|
|
55
|
+
/**
|
|
56
|
+
* Open a file and try to acquire a lock without waiting. Closes the file if not acquired.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* const guard = await tryOpenLock('/tmp/my.lock', Lock.Exclusive)
|
|
60
|
+
* if (guard) { /* acquired *\/ }
|
|
61
|
+
*/
|
|
62
|
+
export declare const tryOpenLock: <H extends FileHandle = NodeFileHandle>(file: string, type: Lock, options?: OpenLockOptions<H>) => Promise<FileLockGuard<H> | undefined>;
|
|
63
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,IAAI,EACT,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,EAAE,EACP,KAAK,MAAM,EACX,aAAa,EACb,SAAS,EAGV,MAAM,iBAAiB,CAAA;AACxB,cAAc,iBAAiB,CAAA;AAE/B,gEAAgE;AAChE,MAAM,WAAW,cAAc;IAC7B,+EAA+E;IAC/E,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,4DAA4D;IAC5D,KAAK,CAAC,EAAE,SAAS,CAAA;CAClB;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,IAAI,GAAU,CAAC,SAAS,UAAU,GAAG,cAAc,EAC9D,QAAQ,CAAC,EACT,MAAM,IAAI,EACV,UAAU,cAAc,GAAG,WAAW,KACrC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAItB,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,OAAO,GAAU,CAAC,SAAS,cAAc,EACpD,QAAQ,CAAC,EACT,MAAM,IAAI,EACV,UAAU,cAAc,GAAG,WAAW,KACrC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,CAKlC,CAAA;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,MAAM,GAAU,CAAC,SAAS,cAAc,EAAE,QAAQ,CAAC,EAAE,UAAU,cAAc,KAAG,OAAO,CAAC,IAAI,CAGxG,CAAA;AAED,0EAA0E;AAC1E,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,UAAU,CAAE,SAAQ,cAAc;IAC3E,mFAAmF;IACnF,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAA;CACX;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,QAAQ,GAAU,CAAC,SAAS,UAAU,GAAG,cAAc,EAClE,MAAM,MAAM,EACZ,MAAM,IAAI,EACV,UAAU,eAAe,CAAC,CAAC,CAAC,GAAG,WAAW,KACzC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAS1B,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,GAAU,CAAC,SAAS,UAAU,GAAG,cAAc,EACrE,MAAM,MAAM,EACZ,MAAM,IAAI,EACV,UAAU,eAAe,CAAC,CAAC,CAAC,KAC3B,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,GAAG,SAAS,CAYtC,CAAA"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { FileLockGuard, LockGuard, defaultFs, defaultLocking, } from './core/index.js';
|
|
2
|
+
export * from './core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Acquire a lock on an already-open file handle.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* import fs from 'node:fs/promises'
|
|
8
|
+
* const handle = await fs.open('/tmp/my.lock', 'r+')
|
|
9
|
+
* await using guard = await lock(handle, Lock.Exclusive)
|
|
10
|
+
*/
|
|
11
|
+
export const lock = async (handle, type, options) => {
|
|
12
|
+
const { locking = defaultLocking() } = options ?? {};
|
|
13
|
+
await locking.lock(handle, type, options?.range, options);
|
|
14
|
+
return new LockGuard(locking, handle, options?.range);
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Try to acquire a lock on an already-open file handle without waiting.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* import fs from 'node:fs/promises'
|
|
21
|
+
* const handle = await fs.open('/tmp/my.lock', 'r+')
|
|
22
|
+
* const guard = await tryLock(handle, Lock.Exclusive)
|
|
23
|
+
* if (guard) { /* acquired *\/ }
|
|
24
|
+
*/
|
|
25
|
+
export const tryLock = async (handle, type, options) => {
|
|
26
|
+
const { locking = defaultLocking() } = options ?? {};
|
|
27
|
+
const result = await locking.tryLock(handle, type, options?.range);
|
|
28
|
+
if (result)
|
|
29
|
+
return new LockGuard(locking, handle, options?.range);
|
|
30
|
+
return undefined;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Release a lock on a file descriptor or file handle.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* import fs from 'node:fs/promises'
|
|
37
|
+
* const handle = await fs.open('/tmp/my.lock', 'r+')
|
|
38
|
+
* await lock(handle, Lock.Exclusive)
|
|
39
|
+
* // ... critical section ...
|
|
40
|
+
* await unlock(handle)
|
|
41
|
+
*/
|
|
42
|
+
export const unlock = async (handle, options) => {
|
|
43
|
+
const { locking = defaultLocking() } = options ?? {};
|
|
44
|
+
await locking.unlock(handle, options?.range);
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Open a file and acquire a lock, polling until available. Closes the file on failure.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* await using guard = await openLock('/tmp/my.lock', Lock.Exclusive)
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* await using guard = await openLock('/tmp/my.lock', Lock.Shared, { timeout: 5000 })
|
|
54
|
+
*/
|
|
55
|
+
export const openLock = async (file, type, options) => {
|
|
56
|
+
const { fs = defaultFs() } = options ?? {};
|
|
57
|
+
const handle = await fs.open(file, type);
|
|
58
|
+
try {
|
|
59
|
+
return new FileLockGuard(await lock(handle, type, options), handle);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
await handle.close();
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Open a file and try to acquire a lock without waiting. Closes the file if not acquired.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* const guard = await tryOpenLock('/tmp/my.lock', Lock.Exclusive)
|
|
71
|
+
* if (guard) { /* acquired *\/ }
|
|
72
|
+
*/
|
|
73
|
+
export const tryOpenLock = async (file, type, options) => {
|
|
74
|
+
const { fs = defaultFs() } = options ?? {};
|
|
75
|
+
const handle = await fs.open(file, type);
|
|
76
|
+
try {
|
|
77
|
+
const result = await tryLock(handle, type, options);
|
|
78
|
+
if (result)
|
|
79
|
+
return new FileLockGuard(result, handle);
|
|
80
|
+
await handle.close();
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
await handle.close();
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EASL,aAAa,EACb,SAAS,EACT,SAAS,EACT,cAAc,GACf,MAAM,iBAAiB,CAAA;AACxB,cAAc,iBAAiB,CAAA;AAU/B;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,KAAK,EACvB,MAAS,EACT,IAAU,EACV,OAAsC,EACf,EAAE;IACzB,MAAM,EAAE,OAAO,GAAG,cAAc,EAAE,EAAE,GAAG,OAAO,IAAI,EAAE,CAAA;IACpD,MAAM,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,CAAA;IACzD,OAAO,IAAI,SAAS,CAAI,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,CAAA;AAC1D,CAAC,CAAA;AAED;;;;;;;;G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