UNPKG

nodedb-json

Version:

A lightweight JSON-based database for Node.js with TypeScript support, indexing, and complex query capabilities

159 lines 5.22 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.FileLock = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const file_lock_error_1 = require("../errors/file-lock-error"); const atomic_writer_1 = require("./atomic-writer"); const activeLocks = new Map(); let exitHookInstalled = false; class FileLock { constructor(options) { this.acquired = false; this.lockPath = options.lockPath; this.enabled = options.enabled; this.staleLockMs = options.staleLockMs; this.lockId = `${process.pid}:${Date.now()}:${Math.random().toString(16).slice(2)}`; } acquire() { if (!this.enabled || this.acquired) { return; } (0, atomic_writer_1.ensureParentDirectory)(this.lockPath); this.removeStaleLock(); try { fs.writeFileSync(this.lockPath, this.createLockPayload(), { encoding: 'utf-8', flag: 'wx' }); this.acquired = true; activeLocks.set(this.lockPath, this.lockId); installExitHook(); } catch (error) { if ((error === null || error === void 0 ? void 0 : error.code) === 'EEXIST') { throw new file_lock_error_1.FileLockError(this.lockPath); } throw error; } } release() { if (!this.enabled || !this.acquired) { return; } this.acquired = false; activeLocks.delete(this.lockPath); unlinkLockFile(this.lockPath, this.lockId); } get path() { return this.lockPath; } removeStaleLock() { if (!this.staleLockMs || this.staleLockMs <= 0 || !fs.existsSync(this.lockPath)) { return; } const stat = fs.statSync(this.lockPath); if (Date.now() - stat.mtimeMs < this.staleLockMs) { return; } const lockOwnerPid = readLockOwnerPid(this.lockPath); if (lockOwnerPid === undefined || !isProcessAlive(lockOwnerPid)) { fs.unlinkSync(this.lockPath); } } createLockPayload() { return `${JSON.stringify({ pid: process.pid, id: this.lockId, cwd: process.cwd(), file: path.resolve(this.lockPath), createdAt: new Date().toISOString() }, null, 2)}\n`; } } exports.FileLock = FileLock; function readLockOwnerPid(lockPath) { try { const content = fs.readFileSync(lockPath, 'utf-8'); const parsed = JSON.parse(content); return Number.isInteger(parsed.pid) && parsed.pid > 0 ? parsed.pid : undefined; } catch (_a) { return undefined; } } function isProcessAlive(pid) { try { process.kill(pid, 0); return true; } catch (error) { return (error === null || error === void 0 ? void 0 : error.code) === 'EPERM'; } } function installExitHook() { if (exitHookInstalled) { return; } exitHookInstalled = true; process.once('exit', () => { for (const [lockPath, lockId] of activeLocks) { try { unlinkLockFile(lockPath, lockId); } catch (_a) { // Process exit cleanup is best-effort. } } activeLocks.clear(); }); } function unlinkLockFile(lockPath, expectedId) { try { const content = fs.readFileSync(lockPath, 'utf-8'); const parsed = JSON.parse(content); if (parsed.id !== expectedId) { return; } fs.unlinkSync(lockPath); } catch (error) { if ((error === null || error === void 0 ? void 0 : error.code) !== 'ENOENT') { throw error; } } } //# sourceMappingURL=file-lock.js.map