@anhlp/alp-daemon 0.0.0-stage → 0.0.1-beta.10
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/LICENSE +202 -0
- package/NOTICE +7 -0
- package/README.md +14 -2
- package/dist/THIRD-PARTY-NOTICES.txt +456 -0
- package/dist/alp-templates.d.ts +5 -0
- package/dist/alpd.js +64050 -0
- package/dist/attachments.d.ts +187 -0
- package/dist/attachments.js +856 -0
- package/dist/build-manifest.json +150 -0
- package/dist/conversation.d.ts +119 -0
- package/dist/conversation.js +688 -0
- package/dist/disk.d.ts +67 -0
- package/dist/disk.js +152 -0
- package/dist/files.d.ts +262 -0
- package/dist/files.js +674 -0
- package/dist/git.d.ts +98 -0
- package/dist/git.js +1246 -0
- package/dist/gitignore.d.ts +62 -0
- package/dist/gitignore.js +254 -0
- package/dist/history.d.ts +24 -0
- package/dist/history.js +149 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +28 -0
- package/dist/lifecycle.d.ts +101 -0
- package/dist/lifecycle.js +239 -0
- package/dist/lock.d.ts +21 -0
- package/dist/lock.js +42 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +363 -0
- package/dist/model-catalog.d.ts +2 -0
- package/dist/model-catalog.js +44 -0
- package/dist/native-entry.d.ts +2 -0
- package/dist/native-entry.js +3 -0
- package/dist/native-register.d.ts +1 -0
- package/dist/native-register.js +13 -0
- package/dist/native-terminal.d.ts +45 -0
- package/dist/native-terminal.js +201 -0
- package/dist/operations.d.ts +40 -0
- package/dist/operations.js +313 -0
- package/dist/registry.d.ts +89 -0
- package/dist/registry.js +343 -0
- package/dist/rpc-error.d.ts +6 -0
- package/dist/rpc-error.js +11 -0
- package/dist/scheduler.d.ts +96 -0
- package/dist/scheduler.js +298 -0
- package/dist/schedules.d.ts +70 -0
- package/dist/schedules.js +246 -0
- package/dist/server.d.ts +60 -0
- package/dist/server.js +1958 -0
- package/dist/store.d.ts +178 -0
- package/dist/store.js +386 -0
- package/dist/terminal-native/README.md +201 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/dist/terminal-native/terminal-owner.c +389 -0
- package/dist/terminal-screen.d.ts +24 -0
- package/dist/terminal-screen.js +99 -0
- package/dist/terminals.d.ts +203 -0
- package/dist/terminals.js +570 -0
- package/dist/windows-terminal.d.ts +32 -0
- package/dist/windows-terminal.js +79 -0
- package/dist/worktrees.d.ts +65 -0
- package/dist/worktrees.js +318 -0
- package/native/README.md +201 -0
- package/native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/native/prebuilt/darwin-x64/manifest.json +11 -0
- package/native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/native/terminal-owner.c +389 -0
- package/package.json +163 -4
- package/src/alp-templates.d.ts +5 -0
- package/src/attachments.ts +908 -0
- package/src/conversation.ts +857 -0
- package/src/disk.ts +175 -0
- package/src/files.ts +884 -0
- package/src/git.ts +1220 -0
- package/src/gitignore.ts +277 -0
- package/src/history.ts +118 -0
- package/src/index.ts +12 -0
- package/src/lifecycle.ts +283 -0
- package/src/lock.ts +53 -0
- package/src/main.ts +381 -0
- package/src/model-catalog.ts +36 -0
- package/src/native-entry.ts +5 -0
- package/src/native-register.ts +21 -0
- package/src/native-terminal.ts +127 -0
- package/src/operations.ts +329 -0
- package/src/registry.ts +238 -0
- package/src/rpc-error.ts +4 -0
- package/src/scheduler.ts +438 -0
- package/src/schedules.ts +297 -0
- package/src/server.ts +2238 -0
- package/src/store.ts +449 -0
- package/src/terminal-screen.ts +74 -0
- package/src/terminals.ts +653 -0
- package/src/windows-terminal.ts +74 -0
- package/src/worktrees.ts +382 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// packages/daemon/src/lifecycle.ts
|
|
2
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
3
|
+
import { mkdir, readdir, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { replaceFile } from "./lock.js";
|
|
6
|
+
import { RpcError } from "./rpc-error.js";
|
|
7
|
+
var LIFECYCLE_RETENTION_MS = 7 * 24 * 60 * 60 * 1e3;
|
|
8
|
+
var keyOf = (resource, operation, requestId) => createHash("sha256").update(JSON.stringify(["lifecycle", resource, operation, requestId])).digest("hex");
|
|
9
|
+
function createLifecycleJournal(directory) {
|
|
10
|
+
const writes = /* @__PURE__ */ new Map();
|
|
11
|
+
let ready;
|
|
12
|
+
const prepare = () => ready ??= mkdir(directory, { recursive: true, mode: 448 }).then(() => {
|
|
13
|
+
}, (error) => {
|
|
14
|
+
ready = void 0;
|
|
15
|
+
throw error;
|
|
16
|
+
});
|
|
17
|
+
const file = (key) => path.join(directory, `${key}.json`);
|
|
18
|
+
async function read(name) {
|
|
19
|
+
let text;
|
|
20
|
+
try {
|
|
21
|
+
text = await readFile(path.join(directory, name), "utf8");
|
|
22
|
+
} catch (error) {
|
|
23
|
+
if (error?.code === "ENOENT") return void 0;
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
const entry = JSON.parse(text);
|
|
27
|
+
if (entry?.version !== 1 || typeof entry.key !== "string" || typeof entry.state !== "string") throw new Error(`unreadable lifecycle entry ${name}`);
|
|
28
|
+
return entry;
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
keyOf,
|
|
32
|
+
async get(resource, operation, requestId) {
|
|
33
|
+
const key = keyOf(resource, operation, requestId);
|
|
34
|
+
await writes.get(key);
|
|
35
|
+
await prepare();
|
|
36
|
+
return read(`${key}.json`);
|
|
37
|
+
},
|
|
38
|
+
/** Writes an entry atomically; writes of one key never land out of order. */
|
|
39
|
+
put(entry) {
|
|
40
|
+
const body = JSON.stringify({ ...entry, updatedAt: (/* @__PURE__ */ new Date()).toISOString() });
|
|
41
|
+
const next = (writes.get(entry.key) ?? Promise.resolve()).catch(() => {
|
|
42
|
+
}).then(prepare).then(async () => {
|
|
43
|
+
const temporary = `${file(entry.key)}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
|
|
44
|
+
try {
|
|
45
|
+
await writeFile(temporary, body, { mode: 384 });
|
|
46
|
+
await replaceFile(temporary, file(entry.key));
|
|
47
|
+
} catch (error) {
|
|
48
|
+
await rm(temporary, { force: true }).catch(() => {
|
|
49
|
+
});
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
writes.set(entry.key, next);
|
|
54
|
+
void next.finally(() => {
|
|
55
|
+
if (writes.get(entry.key) === next) writes.delete(entry.key);
|
|
56
|
+
}).catch(() => {
|
|
57
|
+
});
|
|
58
|
+
return next;
|
|
59
|
+
},
|
|
60
|
+
/** Every readable entry; an unreadable one is skipped (and named), never deleted. */
|
|
61
|
+
async list(onUnreadable) {
|
|
62
|
+
await prepare();
|
|
63
|
+
const entries = [];
|
|
64
|
+
for (const name of await readdir(directory)) {
|
|
65
|
+
if (!name.endsWith(".json")) continue;
|
|
66
|
+
try {
|
|
67
|
+
const entry = await read(name);
|
|
68
|
+
if (entry) entries.push(entry);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
onUnreadable?.(name, error);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return entries;
|
|
74
|
+
},
|
|
75
|
+
/** Removes terminal entries not written for `maxAgeMs`, and temporary files a crash left (only at start). */
|
|
76
|
+
async prune(maxAgeMs = LIFECYCLE_RETENTION_MS, { temporaries = false } = {}) {
|
|
77
|
+
await prepare();
|
|
78
|
+
const now = Date.now();
|
|
79
|
+
for (const name of await readdir(directory)) {
|
|
80
|
+
const target = path.join(directory, name);
|
|
81
|
+
if (name.endsWith(".tmp")) {
|
|
82
|
+
if (temporaries) await rm(target, { force: true });
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (!name.endsWith(".json")) continue;
|
|
86
|
+
const entry = await read(name).catch(() => void 0);
|
|
87
|
+
if (!entry || !["committed", "failed", "unknown"].includes(entry.state)) continue;
|
|
88
|
+
const info = await stat(target).catch(() => void 0);
|
|
89
|
+
if (info && now - info.mtimeMs > maxAgeMs) await rm(target, { force: true });
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
async flush() {
|
|
93
|
+
await Promise.all([...writes.values()].map((write) => write.catch(() => {
|
|
94
|
+
})));
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
var answerOf = (error) => error instanceof RpcError ? { code: error.code, message: error.message, ...error.data !== void 0 ? { data: error.data } : {} } : { code: 1e3, message: error instanceof Error ? error.message : String(error) };
|
|
99
|
+
var outcomeUnknown = (operation, requestId) => new RpcError(1004, `alpd cannot tell whether ${operation} ${requestId} reached the runtime; check the session, then try again with a new requestId`, { reason: "outcome_unknown" });
|
|
100
|
+
function createLifecycle(journal, {
|
|
101
|
+
crashPoint = (_name) => {
|
|
102
|
+
},
|
|
103
|
+
completed = async (_entry) => void 0
|
|
104
|
+
} = {}) {
|
|
105
|
+
const queues = /* @__PURE__ */ new Map();
|
|
106
|
+
const inflight = /* @__PURE__ */ new Map();
|
|
107
|
+
function serial(resource, work) {
|
|
108
|
+
const run2 = (queues.get(resource) ?? Promise.resolve()).catch(() => {
|
|
109
|
+
}).then(work);
|
|
110
|
+
queues.set(resource, run2);
|
|
111
|
+
void run2.finally(() => {
|
|
112
|
+
if (queues.get(resource) === run2) queues.delete(resource);
|
|
113
|
+
}).catch(() => {
|
|
114
|
+
});
|
|
115
|
+
return run2;
|
|
116
|
+
}
|
|
117
|
+
async function run(request, steps) {
|
|
118
|
+
const key = journal.keyOf(request.resource, request.operation, request.requestId);
|
|
119
|
+
const earlier = inflight.get(key);
|
|
120
|
+
if (earlier) await earlier.catch(() => {
|
|
121
|
+
});
|
|
122
|
+
const attempt = serial(request.resource, async () => {
|
|
123
|
+
let entry;
|
|
124
|
+
try {
|
|
125
|
+
entry = await journal.get(request.resource, request.operation, request.requestId);
|
|
126
|
+
} catch (error) {
|
|
127
|
+
throw new RpcError(1e3, `alpd cannot read the journal of ${request.operation} ${request.requestId}`, { reason: "persistence_failed" });
|
|
128
|
+
}
|
|
129
|
+
if (entry) {
|
|
130
|
+
if (entry.fingerprint !== request.fingerprint) throw new RpcError(1004, `${request.operation} ${request.requestId} was already requested with other parameters`, { reason: "key_conflict" });
|
|
131
|
+
if (entry.state === "committed") return { result: steps.repeat ? await steps.repeat(entry) : entry.result, duplicate: true, entry };
|
|
132
|
+
if (entry.state === "failed") throw new RpcError(entry.error?.code ?? 1e3, entry.error?.message ?? `${request.operation} failed`, entry.error?.data);
|
|
133
|
+
if (entry.state === "applied") {
|
|
134
|
+
const saved = await completed(entry);
|
|
135
|
+
const done2 = await commit(entry, steps, saved);
|
|
136
|
+
return { result: done2.result, duplicate: saved !== void 0, entry: done2 };
|
|
137
|
+
}
|
|
138
|
+
throw outcomeUnknown(request.operation, request.requestId);
|
|
139
|
+
}
|
|
140
|
+
for (const pending of await journal.list()) {
|
|
141
|
+
if (pending.resource === request.resource && ["configure", "rewind"].includes(pending.operation) && pending.state === "applied" && !await completed(pending)) {
|
|
142
|
+
throw new RpcError(1e3, `alpd must finish ${pending.operation} ${pending.requestId} before another conversation mutation`, { reason: "persistence_failed" });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const plan = await steps.prepare();
|
|
146
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
147
|
+
entry = { version: 1, key, ...request, attemptId: randomBytes(16).toString("hex"), state: "prepared", plan, createdAt: now, updatedAt: now };
|
|
148
|
+
try {
|
|
149
|
+
await journal.put(entry);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
throw new RpcError(1e3, `alpd could not record ${request.operation} ${request.requestId}; nothing was changed`, { reason: "persistence_failed" });
|
|
152
|
+
}
|
|
153
|
+
crashPoint(`${request.operation}.after-prepared`);
|
|
154
|
+
let native;
|
|
155
|
+
try {
|
|
156
|
+
native = await steps.apply(plan);
|
|
157
|
+
} catch (error) {
|
|
158
|
+
const unknown = error?.outcomeUnknown === true;
|
|
159
|
+
entry = { ...entry, state: unknown ? "unknown" : "failed", ...unknown ? {} : { error: answerOf(error) } };
|
|
160
|
+
await journal.put(entry).catch(() => {
|
|
161
|
+
});
|
|
162
|
+
if (unknown) throw outcomeUnknown(request.operation, request.requestId);
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
entry = { ...entry, state: "applied", native };
|
|
166
|
+
try {
|
|
167
|
+
await journal.put(entry);
|
|
168
|
+
} catch {
|
|
169
|
+
throw outcomeUnknown(request.operation, request.requestId);
|
|
170
|
+
}
|
|
171
|
+
crashPoint(`${request.operation}.after-applied`);
|
|
172
|
+
const done = await commit(entry, steps);
|
|
173
|
+
return { result: done.result, duplicate: false, entry: done };
|
|
174
|
+
});
|
|
175
|
+
inflight.set(key, attempt);
|
|
176
|
+
void attempt.finally(() => {
|
|
177
|
+
if (inflight.get(key) === attempt) inflight.delete(key);
|
|
178
|
+
}).catch(() => {
|
|
179
|
+
});
|
|
180
|
+
return attempt;
|
|
181
|
+
}
|
|
182
|
+
async function commit(entry, steps, saved) {
|
|
183
|
+
let result;
|
|
184
|
+
try {
|
|
185
|
+
if (!saved && !entry.attemptId && ["configure", "rewind"].includes(entry.operation)) {
|
|
186
|
+
const newer = (await journal.list()).some((other) => other.resource === entry.resource && other.key !== entry.key && other.createdAt > entry.createdAt && ["applied", "committed"].includes(other.state));
|
|
187
|
+
if (newer) throw new RpcError(1e3, `alpd cannot safely replay ${entry.operation} ${entry.requestId} over a later conversation mutation`, { reason: "persistence_failed" });
|
|
188
|
+
}
|
|
189
|
+
result = saved ? saved.result : await steps.commit(entry);
|
|
190
|
+
} catch (error) {
|
|
191
|
+
if (error instanceof RpcError) throw error;
|
|
192
|
+
throw new RpcError(1e3, `alpd could not finish ${entry.operation} ${entry.requestId}; it finishes when tried again or when alpd starts`, { reason: "persistence_failed" });
|
|
193
|
+
}
|
|
194
|
+
crashPoint(`${entry.operation}.before-committed`);
|
|
195
|
+
const done = { ...entry, state: "committed", result };
|
|
196
|
+
await journal.put(done).catch(() => {
|
|
197
|
+
});
|
|
198
|
+
crashPoint(`${entry.operation}.after-committed`);
|
|
199
|
+
return done;
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
run,
|
|
203
|
+
serial,
|
|
204
|
+
/**
|
|
205
|
+
* At start, before any request: `prepared` becomes `unknown` (the native step is never sent again),
|
|
206
|
+
* and unknown outcomes are durably observed once, without claiming success or replaying a commit.
|
|
207
|
+
* `applied` rolls forward through the operation's commit (left `applied` when that fails, to be
|
|
208
|
+
* finished by a repeat or the next start; such entries are answered).
|
|
209
|
+
*/
|
|
210
|
+
async recover(commits, log) {
|
|
211
|
+
const entries = await journal.list((name, error) => log?.(`unreadable lifecycle entry ${name}`, { error }));
|
|
212
|
+
const unfinished = [];
|
|
213
|
+
for (const entry of entries) {
|
|
214
|
+
if (entry.state === "prepared" || entry.state === "unknown" && !entry.recoverySeenAt) {
|
|
215
|
+
try {
|
|
216
|
+
await journal.put({ ...entry, state: "unknown", recoverySeenAt: (/* @__PURE__ */ new Date()).toISOString() });
|
|
217
|
+
log?.(`${entry.operation} ${entry.requestId} of ${entry.resource} may or may not have reached the runtime`, { code: "ALPD_LIFECYCLE_UNKNOWN", operation: entry.operation, resource: entry.resource, requestId: entry.requestId });
|
|
218
|
+
} catch (error) {
|
|
219
|
+
log?.(`could not record unknown ${entry.operation} ${entry.requestId} of ${entry.resource}`, { code: "ALPD_LIFECYCLE_OBSERVE_FAILED", error });
|
|
220
|
+
}
|
|
221
|
+
} else if (entry.state === "applied") {
|
|
222
|
+
const committer = commits[entry.operation];
|
|
223
|
+
if (!committer) continue;
|
|
224
|
+
await serial(entry.resource, async () => commit(entry, { commit: committer }, await completed(entry))).catch((error) => {
|
|
225
|
+
unfinished.push(entry);
|
|
226
|
+
log?.(`could not finish ${entry.operation} ${entry.requestId} of ${entry.resource}`, { code: "ALPD_LIFECYCLE_ROLLFORWARD_FAILED", error });
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return unfinished;
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
export {
|
|
235
|
+
LIFECYCLE_RETENTION_MS,
|
|
236
|
+
createLifecycle,
|
|
237
|
+
createLifecycleJournal,
|
|
238
|
+
outcomeUnknown
|
|
239
|
+
};
|
package/dist/lock.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type LockContents = {
|
|
2
|
+
pid: number;
|
|
3
|
+
startedAt: string;
|
|
4
|
+
bootTime: number;
|
|
5
|
+
uid: number;
|
|
6
|
+
version: string;
|
|
7
|
+
protocolVersion: number;
|
|
8
|
+
socket: string;
|
|
9
|
+
ready: boolean;
|
|
10
|
+
};
|
|
11
|
+
/** Single-instance lock (Paseo acquirePidLock, simplified): exclusive create, stale when its pid or boot is gone. */
|
|
12
|
+
export declare function acquireLock(home: string, contents: Omit<LockContents, 'pid' | 'startedAt' | 'bootTime' | 'uid' | 'ready'>): Promise<LockContents>;
|
|
13
|
+
/**
|
|
14
|
+
* Moves `temporary` over `file`. On Windows the move fails with EPERM, EACCES or EBUSY while
|
|
15
|
+
* another process has `file` open, such as a client reading the lock; it is tried again then.
|
|
16
|
+
*/
|
|
17
|
+
export declare function replaceFile(temporary: string, file: string): Promise<void>;
|
|
18
|
+
export declare function updateLock(home: string, lock: LockContents): Promise<void>;
|
|
19
|
+
/** True while this process still owns the lock; refreshes its mtime as a heartbeat. */
|
|
20
|
+
export declare function heartbeat(home: string): Promise<boolean>;
|
|
21
|
+
export declare function releaseLock(home: string): Promise<void>;
|
package/dist/lock.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// packages/daemon/src/lock.ts
|
|
2
|
+
import { unlink, utimes, writeFile, rename } from "node:fs/promises";
|
|
3
|
+
import { acquireDaemonLock, daemonPaths, readLock } from "@anhlp/alp-client/index";
|
|
4
|
+
async function acquireLock(home, contents) {
|
|
5
|
+
return acquireDaemonLock(home, contents);
|
|
6
|
+
}
|
|
7
|
+
async function replaceFile(temporary, file) {
|
|
8
|
+
for (let attempt = 0; ; attempt++) {
|
|
9
|
+
try {
|
|
10
|
+
return await rename(temporary, file);
|
|
11
|
+
} catch (error) {
|
|
12
|
+
if (process.platform !== "win32" || attempt >= 20 || !["EPERM", "EACCES", "EBUSY"].includes(error?.code)) throw error;
|
|
13
|
+
await new Promise((resolve) => setTimeout(resolve, 10 + attempt * 10));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async function updateLock(home, lock) {
|
|
18
|
+
const file = daemonPaths(home).lock;
|
|
19
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
20
|
+
await writeFile(temporary, JSON.stringify(lock), { mode: 384 });
|
|
21
|
+
await replaceFile(temporary, file);
|
|
22
|
+
}
|
|
23
|
+
async function heartbeat(home) {
|
|
24
|
+
const current = await readLock(home);
|
|
25
|
+
if (current?.pid !== process.pid) return false;
|
|
26
|
+
const now = /* @__PURE__ */ new Date();
|
|
27
|
+
await utimes(daemonPaths(home).lock, now, now).catch(() => {
|
|
28
|
+
});
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
async function releaseLock(home) {
|
|
32
|
+
const current = await readLock(home);
|
|
33
|
+
if (current?.pid === process.pid) await unlink(daemonPaths(home).lock).catch(() => {
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
acquireLock,
|
|
38
|
+
heartbeat,
|
|
39
|
+
releaseLock,
|
|
40
|
+
replaceFile,
|
|
41
|
+
updateLock
|
|
42
|
+
};
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|