mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 01:47:27 +00:00
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import fs from "node:fs";
|
|
import { readJsonFileWithFallback, writeJsonFileAtomically } from "openclaw/plugin-sdk";
|
|
import { withFileLock as withPathLock } from "./file-lock.js";
|
|
|
|
const STORE_LOCK_OPTIONS = {
|
|
retries: {
|
|
retries: 10,
|
|
factor: 2,
|
|
minTimeout: 100,
|
|
maxTimeout: 10_000,
|
|
randomize: true,
|
|
},
|
|
stale: 30_000,
|
|
} as const;
|
|
|
|
export async function readJsonFile<T>(
|
|
filePath: string,
|
|
fallback: T,
|
|
): Promise<{ value: T; exists: boolean }> {
|
|
return await readJsonFileWithFallback(filePath, fallback);
|
|
}
|
|
|
|
export async function writeJsonFile(filePath: string, value: unknown): Promise<void> {
|
|
await writeJsonFileAtomically(filePath, value);
|
|
}
|
|
|
|
async function ensureJsonFile(filePath: string, fallback: unknown) {
|
|
try {
|
|
await fs.promises.access(filePath);
|
|
} catch {
|
|
await writeJsonFile(filePath, fallback);
|
|
}
|
|
}
|
|
|
|
export async function withFileLock<T>(
|
|
filePath: string,
|
|
fallback: unknown,
|
|
fn: () => Promise<T>,
|
|
): Promise<T> {
|
|
await ensureJsonFile(filePath, fallback);
|
|
return await withPathLock(filePath, STORE_LOCK_OPTIONS, async () => {
|
|
return await fn();
|
|
});
|
|
}
|