UNPKG

2.4 kBTypeScriptView Raw
1import { LockfileOptions } from './lockfile';
2export declare type ReadStatus = {
3 status: 'open';
4 file: string;
5} | {
6 status: 'write_lock';
7 job: Job;
8 file: string;
9};
10export declare type WriteStatus = ReadStatus | {
11 status: 'read_lock';
12 file: string;
13 jobs: Job[];
14};
15export declare type Status = WriteStatus;
16export interface RWLockOptions {
17 reason?: string;
18 ifLocked?: IfLockedFn;
19 timeout?: number;
20 retryInterval?: number;
21}
22export interface RWLockfileOptions extends LockfileOptions {
23 timeout?: number;
24 retryInterval?: number;
25 ifLocked?: IfLockedFn;
26}
27export declare type RWLockType = 'read' | 'write';
28export interface Job {
29 uuid: string;
30 pid: number;
31 reason?: string;
32 created: Date;
33}
34export interface IfLockedFn {
35 ({status}: {
36 status: Status;
37 }): Promise<void> | void;
38}
39export declare class RWLockfile {
40 base: string;
41 ifLocked: IfLockedFn;
42 timeout: number;
43 retryInterval: number;
44 private _debug;
45 private uuid;
46 private fs;
47 private internal;
48 private _count;
49 /**
50 * creates a new read/write lockfile
51 * @param base {string} - base filepath to create lock from
52 */
53 constructor(base: string, options?: RWLockfileOptions);
54 readonly count: {
55 readonly read: number;
56 readonly write: number;
57 };
58 readonly file: string;
59 add(type: RWLockType, opts?: RWLockOptions): Promise<void>;
60 addSync(type: RWLockType, opts?: {
61 reason?: string;
62 }): void;
63 remove(type: RWLockType): Promise<void>;
64 removeSync(type: RWLockType): void;
65 unlock(type?: RWLockType): Promise<void>;
66 unlockSync(type?: RWLockType): void;
67 check(type: RWLockType): Promise<Status>;
68 checkSync(type: RWLockType): Status;
69 private _statusFromFile(type, f);
70 private _parseFile(input);
71 private _stringifyFile(input);
72 private _fetchFile();
73 private _fetchFileSync();
74 private addJob(type, reason, f);
75 private _removeJob(type);
76 private _removeJobSync(type);
77 private _removeJobFromFile(type, f);
78 private _lock(type, opts);
79 tryLock(type: RWLockType, reason?: string, inc?: boolean): Promise<void>;
80 private _lockSync(type, reason?);
81 private writeFile(f);
82 private writeFileSync(f);
83 private readonly debug;
84 private _debugReport(action, type, {reason}?);
85}
86export default RWLockfile;