Provides cross-process locking for shared settings files to prevent race conditions
// Source: https://github.com/juliantanx/aiusage | License: MIT | Copyright 2026 juliantanx
// Provided via justclaw.it.com; keep this notice with the code.
import { mkdirSync } from 'node:fs'
import { join, resolve } from 'node:path'
import Database from 'better-sqlite3'
/** How long a writer waits for another process to finish its update. */
export const SETTINGS_LOCK_TIMEOUT_MS = 10_000
/** The settings lock was not free within the timeout. Nothing was written. */
export class SettingsLockTimeoutError extends Error {
constructor(public readonly path: string, timeoutMs: number) {
super(`Another aiusage process kept ${path} locked for more than ${Math.round(timeoutMs / 1000)} seconds, so the settings were not saved. Try again.`)
this.name = 'SettingsLockTimeoutError'
}
}
//
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key