atomic-lockfile 0.0.1-security → 1.4.2
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.
Potentially problematic release.
This version of atomic-lockfile might be problematic. Click here for more details.
- package/CHANGELOG.md +35 -0
- package/LICENSE +21 -0
- package/README.md +207 -3
- package/dist/esm/adapters/atomic.js +42 -0
- package/dist/esm/adapters/atomic.js.map +1 -0
- package/dist/esm/adapters/fs.js +29 -0
- package/dist/esm/adapters/fs.js.map +1 -0
- package/dist/esm/adapters/index.js +27 -0
- package/dist/esm/adapters/index.js.map +1 -0
- package/dist/esm/adapters/mkdir.js +31 -0
- package/dist/esm/adapters/mkdir.js.map +1 -0
- package/dist/esm/adapters/symlink.js +29 -0
- package/dist/esm/adapters/symlink.js.map +1 -0
- package/dist/esm/async/check.js +11 -0
- package/dist/esm/async/check.js.map +1 -0
- package/dist/esm/async/index.js +4 -0
- package/dist/esm/async/index.js.map +1 -0
- package/dist/esm/async/lock.js +13 -0
- package/dist/esm/async/lock.js.map +1 -0
- package/dist/esm/async/unlock.js +8 -0
- package/dist/esm/async/unlock.js.map +1 -0
- package/dist/esm/cli/commands/cleanup.js +28 -0
- package/dist/esm/cli/commands/cleanup.js.map +1 -0
- package/dist/esm/cli/commands/list.js +26 -0
- package/dist/esm/cli/commands/list.js.map +1 -0
- package/dist/esm/cli/commands/lock.js +26 -0
- package/dist/esm/cli/commands/lock.js.map +1 -0
- package/dist/esm/cli/commands/status.js +27 -0
- package/dist/esm/cli/commands/status.js.map +1 -0
- package/dist/esm/cli/commands/unlock.js +20 -0
- package/dist/esm/cli/commands/unlock.js.map +1 -0
- package/dist/esm/cli/formatters/index.js +14 -0
- package/dist/esm/cli/formatters/index.js.map +1 -0
- package/dist/esm/cli/formatters/json.js +15 -0
- package/dist/esm/cli/formatters/json.js.map +1 -0
- package/dist/esm/cli/formatters/table.js +18 -0
- package/dist/esm/cli/formatters/table.js.map +1 -0
- package/dist/esm/cli/formatters/text.js +20 -0
- package/dist/esm/cli/formatters/text.js.map +1 -0
- package/dist/esm/cli/index.js +56 -0
- package/dist/esm/cli/index.js.map +1 -0
- package/dist/esm/compat/cjs.js +11 -0
- package/dist/esm/compat/cjs.js.map +1 -0
- package/dist/esm/compat/esm.js +11 -0
- package/dist/esm/compat/esm.js.map +1 -0
- package/dist/esm/compat/index.js +4 -0
- package/dist/esm/compat/index.js.map +1 -0
- package/dist/esm/compat/v1.js +23 -0
- package/dist/esm/compat/v1.js.map +1 -0
- package/dist/esm/config/defaults.js +12 -0
- package/dist/esm/config/defaults.js.map +1 -0
- package/dist/esm/config/index.js +17 -0
- package/dist/esm/config/index.js.map +1 -0
- package/dist/esm/config/schema.js +55 -0
- package/dist/esm/config/schema.js.map +1 -0
- package/dist/esm/constants/defaults.js +15 -0
- package/dist/esm/constants/defaults.js.map +1 -0
- package/dist/esm/constants/errors.js +37 -0
- package/dist/esm/constants/errors.js.map +1 -0
- package/dist/esm/constants/index.js +14 -0
- package/dist/esm/constants/index.js.map +1 -0
- package/dist/esm/constants/signals.js +21 -0
- package/dist/esm/constants/signals.js.map +1 -0
- package/dist/esm/constants/timeouts.js +16 -0
- package/dist/esm/constants/timeouts.js.map +1 -0
- package/dist/esm/core/acquire.js +51 -0
- package/dist/esm/core/acquire.js.map +1 -0
- package/dist/esm/core/buildLockData.js +23 -0
- package/dist/esm/core/buildLockData.js.map +1 -0
- package/dist/esm/core/check.js +62 -0
- package/dist/esm/core/check.js.map +1 -0
- package/dist/esm/core/cleanup.js +45 -0
- package/dist/esm/core/cleanup.js.map +1 -0
- package/dist/esm/core/handle.js +43 -0
- package/dist/esm/core/handle.js.map +1 -0
- package/dist/esm/core/index.js +8 -0
- package/dist/esm/core/index.js.map +1 -0
- package/dist/esm/core/list.js +38 -0
- package/dist/esm/core/list.js.map +1 -0
- package/dist/esm/core/release.js +21 -0
- package/dist/esm/core/release.js.map +1 -0
- package/dist/esm/errors/AbortError.js +18 -0
- package/dist/esm/errors/AbortError.js.map +1 -0
- package/dist/esm/errors/ConflictError.js +20 -0
- package/dist/esm/errors/ConflictError.js.map +1 -0
- package/dist/esm/errors/IOError.js +24 -0
- package/dist/esm/errors/IOError.js.map +1 -0
- package/dist/esm/errors/LockError.js +30 -0
- package/dist/esm/errors/LockError.js.map +1 -0
- package/dist/esm/errors/PermissionError.js +21 -0
- package/dist/esm/errors/PermissionError.js.map +1 -0
- package/dist/esm/errors/StaleError.js +27 -0
- package/dist/esm/errors/StaleError.js.map +1 -0
- package/dist/esm/errors/TimeoutError.js +21 -0
- package/dist/esm/errors/TimeoutError.js.map +1 -0
- package/dist/esm/errors/ValidationError.js +21 -0
- package/dist/esm/errors/ValidationError.js.map +1 -0
- package/dist/esm/errors/index.js +24 -0
- package/dist/esm/errors/index.js.map +1 -0
- package/dist/esm/events/emitter.js +41 -0
- package/dist/esm/events/emitter.js.map +1 -0
- package/dist/esm/events/index.js +3 -0
- package/dist/esm/events/index.js.map +1 -0
- package/dist/esm/events/types.js +15 -0
- package/dist/esm/events/types.js.map +1 -0
- package/dist/esm/hooks/afterLock.js +18 -0
- package/dist/esm/hooks/afterLock.js.map +1 -0
- package/dist/esm/hooks/afterUnlock.js +18 -0
- package/dist/esm/hooks/afterUnlock.js.map +1 -0
- package/dist/esm/hooks/beforeLock.js +18 -0
- package/dist/esm/hooks/beforeLock.js.map +1 -0
- package/dist/esm/hooks/beforeUnlock.js +18 -0
- package/dist/esm/hooks/beforeUnlock.js.map +1 -0
- package/dist/esm/hooks/index.js +24 -0
- package/dist/esm/hooks/index.js.map +1 -0
- package/dist/esm/hooks/onError.js +18 -0
- package/dist/esm/hooks/onError.js.map +1 -0
- package/dist/esm/hooks/onStale.js +18 -0
- package/dist/esm/hooks/onStale.js.map +1 -0
- package/dist/esm/hooks/onTimeout.js +18 -0
- package/dist/esm/hooks/onTimeout.js.map +1 -0
- package/dist/esm/index.js +21 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/logger/console.js +38 -0
- package/dist/esm/logger/console.js.map +1 -0
- package/dist/esm/logger/debug.js +26 -0
- package/dist/esm/logger/debug.js.map +1 -0
- package/dist/esm/logger/index.js +15 -0
- package/dist/esm/logger/index.js.map +1 -0
- package/dist/esm/logger/noop.js +12 -0
- package/dist/esm/logger/noop.js.map +1 -0
- package/dist/esm/meta/index.js +3 -0
- package/dist/esm/meta/index.js.map +1 -0
- package/dist/esm/meta/package.js +27 -0
- package/dist/esm/meta/package.js.map +1 -0
- package/dist/esm/meta/version.js +27 -0
- package/dist/esm/meta/version.js.map +1 -0
- package/dist/esm/middleware/index.js +11 -0
- package/dist/esm/middleware/index.js.map +1 -0
- package/dist/esm/middleware/logging.js +20 -0
- package/dist/esm/middleware/logging.js.map +1 -0
- package/dist/esm/middleware/metrics.js +32 -0
- package/dist/esm/middleware/metrics.js.map +1 -0
- package/dist/esm/middleware/retry.js +22 -0
- package/dist/esm/middleware/retry.js.map +1 -0
- package/dist/esm/middleware/timeout.js +28 -0
- package/dist/esm/middleware/timeout.js.map +1 -0
- package/dist/esm/platform/darwin.js +17 -0
- package/dist/esm/platform/darwin.js.map +1 -0
- package/dist/esm/platform/index.js +6 -0
- package/dist/esm/platform/index.js.map +1 -0
- package/dist/esm/platform/linux.js +31 -0
- package/dist/esm/platform/linux.js.map +1 -0
- package/dist/esm/platform/unix.js +34 -0
- package/dist/esm/platform/unix.js.map +1 -0
- package/dist/esm/platform/windows.js +22 -0
- package/dist/esm/platform/windows.js.map +1 -0
- package/dist/esm/plugins/cleanup.js +39 -0
- package/dist/esm/plugins/cleanup.js.map +1 -0
- package/dist/esm/plugins/heartbeat.js +31 -0
- package/dist/esm/plugins/heartbeat.js.map +1 -0
- package/dist/esm/plugins/index.js +6 -0
- package/dist/esm/plugins/index.js.map +1 -0
- package/dist/esm/plugins/pid.js +25 -0
- package/dist/esm/plugins/pid.js.map +1 -0
- package/dist/esm/plugins/stale.js +21 -0
- package/dist/esm/plugins/stale.js.map +1 -0
- package/dist/esm/plugins/ttl.js +28 -0
- package/dist/esm/plugins/ttl.js.map +1 -0
- package/dist/esm/promise/index.js +3 -0
- package/dist/esm/promise/index.js.map +1 -0
- package/dist/esm/promise/lock.js +13 -0
- package/dist/esm/promise/lock.js.map +1 -0
- package/dist/esm/promise/withLock.js +22 -0
- package/dist/esm/promise/withLock.js.map +1 -0
- package/dist/esm/registry/global.js +29 -0
- package/dist/esm/registry/global.js.map +1 -0
- package/dist/esm/registry/index.js +3 -0
- package/dist/esm/registry/index.js.map +1 -0
- package/dist/esm/registry/local.js +28 -0
- package/dist/esm/registry/local.js.map +1 -0
- package/dist/esm/strategies/advisory.js +19 -0
- package/dist/esm/strategies/advisory.js.map +1 -0
- package/dist/esm/strategies/exclusive.js +19 -0
- package/dist/esm/strategies/exclusive.js.map +1 -0
- package/dist/esm/strategies/index.js +27 -0
- package/dist/esm/strategies/index.js.map +1 -0
- package/dist/esm/strategies/optimistic.js +22 -0
- package/dist/esm/strategies/optimistic.js.map +1 -0
- package/dist/esm/strategies/shared.js +19 -0
- package/dist/esm/strategies/shared.js.map +1 -0
- package/dist/esm/sync/check.js +39 -0
- package/dist/esm/sync/check.js.map +1 -0
- package/dist/esm/sync/index.js +4 -0
- package/dist/esm/sync/index.js.map +1 -0
- package/dist/esm/sync/lock.js +39 -0
- package/dist/esm/sync/lock.js.map +1 -0
- package/dist/esm/sync/unlock.js +21 -0
- package/dist/esm/sync/unlock.js.map +1 -0
- package/dist/esm/types/adapter.js +2 -0
- package/dist/esm/types/adapter.js.map +1 -0
- package/dist/esm/types/events.js +2 -0
- package/dist/esm/types/events.js.map +1 -0
- package/dist/esm/types/index.js +2 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/types/lock.js +2 -0
- package/dist/esm/types/lock.js.map +1 -0
- package/dist/esm/types/logger.js +2 -0
- package/dist/esm/types/logger.js.map +1 -0
- package/dist/esm/types/options.js +2 -0
- package/dist/esm/types/options.js.map +1 -0
- package/dist/esm/types/state.js +2 -0
- package/dist/esm/types/state.js.map +1 -0
- package/dist/esm/types/strategy.js +2 -0
- package/dist/esm/types/strategy.js.map +1 -0
- package/dist/esm/utils/backoff.js +40 -0
- package/dist/esm/utils/backoff.js.map +1 -0
- package/dist/esm/utils/fs.js +88 -0
- package/dist/esm/utils/fs.js.map +1 -0
- package/dist/esm/utils/hash.js +20 -0
- package/dist/esm/utils/hash.js.map +1 -0
- package/dist/esm/utils/hostname.js +28 -0
- package/dist/esm/utils/hostname.js.map +1 -0
- package/dist/esm/utils/index.js +14 -0
- package/dist/esm/utils/index.js.map +1 -0
- package/dist/esm/utils/parse.js +44 -0
- package/dist/esm/utils/parse.js.map +1 -0
- package/dist/esm/utils/path.js +35 -0
- package/dist/esm/utils/path.js.map +1 -0
- package/dist/esm/utils/pid.js +35 -0
- package/dist/esm/utils/pid.js.map +1 -0
- package/dist/esm/utils/platform.js +35 -0
- package/dist/esm/utils/platform.js.map +1 -0
- package/dist/esm/utils/random.js +26 -0
- package/dist/esm/utils/random.js.map +1 -0
- package/dist/esm/utils/retry.js +37 -0
- package/dist/esm/utils/retry.js.map +1 -0
- package/dist/esm/utils/serialize.js +27 -0
- package/dist/esm/utils/serialize.js.map +1 -0
- package/dist/esm/utils/time.js +44 -0
- package/dist/esm/utils/time.js.map +1 -0
- package/dist/esm/utils/validate.js +42 -0
- package/dist/esm/utils/validate.js.map +1 -0
- package/dist/types/adapters/atomic.d.ts +3 -0
- package/dist/types/adapters/atomic.d.ts.map +1 -0
- package/dist/types/adapters/fs.d.ts +3 -0
- package/dist/types/adapters/fs.d.ts.map +1 -0
- package/dist/types/adapters/index.d.ts +9 -0
- package/dist/types/adapters/index.d.ts.map +1 -0
- package/dist/types/adapters/mkdir.d.ts +3 -0
- package/dist/types/adapters/mkdir.d.ts.map +1 -0
- package/dist/types/adapters/symlink.d.ts +3 -0
- package/dist/types/adapters/symlink.d.ts.map +1 -0
- package/dist/types/async/check.d.ts +6 -0
- package/dist/types/async/check.d.ts.map +1 -0
- package/dist/types/async/index.d.ts +4 -0
- package/dist/types/async/index.d.ts.map +1 -0
- package/dist/types/async/lock.d.ts +5 -0
- package/dist/types/async/lock.d.ts.map +1 -0
- package/dist/types/async/unlock.d.ts +5 -0
- package/dist/types/async/unlock.d.ts.map +1 -0
- package/dist/types/cli/commands/cleanup.d.ts +2 -0
- package/dist/types/cli/commands/cleanup.d.ts.map +1 -0
- package/dist/types/cli/commands/list.d.ts +2 -0
- package/dist/types/cli/commands/list.d.ts.map +1 -0
- package/dist/types/cli/commands/lock.d.ts +2 -0
- package/dist/types/cli/commands/lock.d.ts.map +1 -0
- package/dist/types/cli/commands/status.d.ts +2 -0
- package/dist/types/cli/commands/status.d.ts.map +1 -0
- package/dist/types/cli/commands/unlock.d.ts +2 -0
- package/dist/types/cli/commands/unlock.d.ts.map +1 -0
- package/dist/types/cli/formatters/index.d.ts +7 -0
- package/dist/types/cli/formatters/index.d.ts.map +1 -0
- package/dist/types/cli/formatters/json.d.ts +5 -0
- package/dist/types/cli/formatters/json.d.ts.map +1 -0
- package/dist/types/cli/formatters/table.d.ts +3 -0
- package/dist/types/cli/formatters/table.d.ts.map +1 -0
- package/dist/types/cli/formatters/text.d.ts +6 -0
- package/dist/types/cli/formatters/text.d.ts.map +1 -0
- package/dist/types/cli/index.d.ts +3 -0
- package/dist/types/cli/index.d.ts.map +1 -0
- package/dist/types/compat/cjs.d.ts +11 -0
- package/dist/types/compat/cjs.d.ts.map +1 -0
- package/dist/types/compat/esm.d.ts +11 -0
- package/dist/types/compat/esm.d.ts.map +1 -0
- package/dist/types/compat/index.d.ts +4 -0
- package/dist/types/compat/index.d.ts.map +1 -0
- package/dist/types/compat/v1.d.ts +15 -0
- package/dist/types/compat/v1.d.ts.map +1 -0
- package/dist/types/config/defaults.d.ts +3 -0
- package/dist/types/config/defaults.d.ts.map +1 -0
- package/dist/types/config/index.d.ts +9 -0
- package/dist/types/config/index.d.ts.map +1 -0
- package/dist/types/config/schema.d.ts +12 -0
- package/dist/types/config/schema.d.ts.map +1 -0
- package/dist/types/constants/defaults.d.ts +15 -0
- package/dist/types/constants/defaults.d.ts.map +1 -0
- package/dist/types/constants/errors.d.ts +21 -0
- package/dist/types/constants/errors.d.ts.map +1 -0
- package/dist/types/constants/index.d.ts +14 -0
- package/dist/types/constants/index.d.ts.map +1 -0
- package/dist/types/constants/signals.d.ts +6 -0
- package/dist/types/constants/signals.d.ts.map +1 -0
- package/dist/types/constants/timeouts.d.ts +16 -0
- package/dist/types/constants/timeouts.d.ts.map +1 -0
- package/dist/types/core/acquire.d.ts +4 -0
- package/dist/types/core/acquire.d.ts.map +1 -0
- package/dist/types/core/buildLockData.d.ts +4 -0
- package/dist/types/core/buildLockData.d.ts.map +1 -0
- package/dist/types/core/check.d.ts +6 -0
- package/dist/types/core/check.d.ts.map +1 -0
- package/dist/types/core/cleanup.d.ts +14 -0
- package/dist/types/core/cleanup.d.ts.map +1 -0
- package/dist/types/core/handle.d.ts +4 -0
- package/dist/types/core/handle.d.ts.map +1 -0
- package/dist/types/core/index.d.ts +9 -0
- package/dist/types/core/index.d.ts.map +1 -0
- package/dist/types/core/list.d.ts +4 -0
- package/dist/types/core/list.d.ts.map +1 -0
- package/dist/types/core/release.d.ts +4 -0
- package/dist/types/core/release.d.ts.map +1 -0
- package/dist/types/errors/AbortError.d.ts +8 -0
- package/dist/types/errors/AbortError.d.ts.map +1 -0
- package/dist/types/errors/ConflictError.d.ts +9 -0
- package/dist/types/errors/ConflictError.d.ts.map +1 -0
- package/dist/types/errors/IOError.d.ts +10 -0
- package/dist/types/errors/IOError.d.ts.map +1 -0
- package/dist/types/errors/LockError.d.ts +12 -0
- package/dist/types/errors/LockError.d.ts.map +1 -0
- package/dist/types/errors/PermissionError.d.ts +9 -0
- package/dist/types/errors/PermissionError.d.ts.map +1 -0
- package/dist/types/errors/StaleError.d.ts +11 -0
- package/dist/types/errors/StaleError.d.ts.map +1 -0
- package/dist/types/errors/TimeoutError.d.ts +9 -0
- package/dist/types/errors/TimeoutError.d.ts.map +1 -0
- package/dist/types/errors/ValidationError.d.ts +9 -0
- package/dist/types/errors/ValidationError.d.ts.map +1 -0
- package/dist/types/errors/index.d.ts +14 -0
- package/dist/types/errors/index.d.ts.map +1 -0
- package/dist/types/events/emitter.d.ts +12 -0
- package/dist/types/events/emitter.d.ts.map +1 -0
- package/dist/types/events/index.d.ts +3 -0
- package/dist/types/events/index.d.ts.map +1 -0
- package/dist/types/events/types.d.ts +3 -0
- package/dist/types/events/types.d.ts.map +1 -0
- package/dist/types/hooks/afterLock.d.ts +7 -0
- package/dist/types/hooks/afterLock.d.ts.map +1 -0
- package/dist/types/hooks/afterUnlock.d.ts +7 -0
- package/dist/types/hooks/afterUnlock.d.ts.map +1 -0
- package/dist/types/hooks/beforeLock.d.ts +7 -0
- package/dist/types/hooks/beforeLock.d.ts.map +1 -0
- package/dist/types/hooks/beforeUnlock.d.ts +7 -0
- package/dist/types/hooks/beforeUnlock.d.ts.map +1 -0
- package/dist/types/hooks/index.d.ts +9 -0
- package/dist/types/hooks/index.d.ts.map +1 -0
- package/dist/types/hooks/onError.d.ts +6 -0
- package/dist/types/hooks/onError.d.ts.map +1 -0
- package/dist/types/hooks/onStale.d.ts +7 -0
- package/dist/types/hooks/onStale.d.ts.map +1 -0
- package/dist/types/hooks/onTimeout.d.ts +6 -0
- package/dist/types/hooks/onTimeout.d.ts.map +1 -0
- package/dist/types/index.d.ts +22 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/logger/console.d.ts +3 -0
- package/dist/types/logger/console.d.ts.map +1 -0
- package/dist/types/logger/debug.d.ts +3 -0
- package/dist/types/logger/debug.d.ts.map +1 -0
- package/dist/types/logger/index.d.ts +8 -0
- package/dist/types/logger/index.d.ts.map +1 -0
- package/dist/types/logger/noop.d.ts +3 -0
- package/dist/types/logger/noop.d.ts.map +1 -0
- package/dist/types/meta/index.d.ts +3 -0
- package/dist/types/meta/index.d.ts.map +1 -0
- package/dist/types/meta/package.d.ts +8 -0
- package/dist/types/meta/package.d.ts.map +1 -0
- package/dist/types/meta/version.d.ts +14 -0
- package/dist/types/meta/version.d.ts.map +1 -0
- package/dist/types/middleware/index.d.ts +10 -0
- package/dist/types/middleware/index.d.ts.map +1 -0
- package/dist/types/middleware/logging.d.ts +3 -0
- package/dist/types/middleware/logging.d.ts.map +1 -0
- package/dist/types/middleware/metrics.d.ts +13 -0
- package/dist/types/middleware/metrics.d.ts.map +1 -0
- package/dist/types/middleware/retry.d.ts +5 -0
- package/dist/types/middleware/retry.d.ts.map +1 -0
- package/dist/types/middleware/timeout.d.ts +7 -0
- package/dist/types/middleware/timeout.d.ts.map +1 -0
- package/dist/types/platform/darwin.d.ts +5 -0
- package/dist/types/platform/darwin.d.ts.map +1 -0
- package/dist/types/platform/index.d.ts +6 -0
- package/dist/types/platform/index.d.ts.map +1 -0
- package/dist/types/platform/linux.d.ts +6 -0
- package/dist/types/platform/linux.d.ts.map +1 -0
- package/dist/types/platform/unix.d.ts +8 -0
- package/dist/types/platform/unix.d.ts.map +1 -0
- package/dist/types/platform/windows.d.ts +7 -0
- package/dist/types/platform/windows.d.ts.map +1 -0
- package/dist/types/plugins/cleanup.d.ts +4 -0
- package/dist/types/plugins/cleanup.d.ts.map +1 -0
- package/dist/types/plugins/heartbeat.d.ts +7 -0
- package/dist/types/plugins/heartbeat.d.ts.map +1 -0
- package/dist/types/plugins/index.d.ts +8 -0
- package/dist/types/plugins/index.d.ts.map +1 -0
- package/dist/types/plugins/pid.d.ts +5 -0
- package/dist/types/plugins/pid.d.ts.map +1 -0
- package/dist/types/plugins/stale.d.ts +9 -0
- package/dist/types/plugins/stale.d.ts.map +1 -0
- package/dist/types/plugins/ttl.d.ts +7 -0
- package/dist/types/plugins/ttl.d.ts.map +1 -0
- package/dist/types/promise/index.d.ts +3 -0
- package/dist/types/promise/index.d.ts.map +1 -0
- package/dist/types/promise/lock.d.ts +6 -0
- package/dist/types/promise/lock.d.ts.map +1 -0
- package/dist/types/promise/withLock.d.ts +7 -0
- package/dist/types/promise/withLock.d.ts.map +1 -0
- package/dist/types/registry/global.d.ts +15 -0
- package/dist/types/registry/global.d.ts.map +1 -0
- package/dist/types/registry/index.d.ts +3 -0
- package/dist/types/registry/index.d.ts.map +1 -0
- package/dist/types/registry/local.d.ts +10 -0
- package/dist/types/registry/local.d.ts.map +1 -0
- package/dist/types/strategies/advisory.d.ts +4 -0
- package/dist/types/strategies/advisory.d.ts.map +1 -0
- package/dist/types/strategies/exclusive.d.ts +4 -0
- package/dist/types/strategies/exclusive.d.ts.map +1 -0
- package/dist/types/strategies/index.d.ts +9 -0
- package/dist/types/strategies/index.d.ts.map +1 -0
- package/dist/types/strategies/optimistic.d.ts +8 -0
- package/dist/types/strategies/optimistic.d.ts.map +1 -0
- package/dist/types/strategies/shared.d.ts +4 -0
- package/dist/types/strategies/shared.d.ts.map +1 -0
- package/dist/types/sync/check.d.ts +4 -0
- package/dist/types/sync/check.d.ts.map +1 -0
- package/dist/types/sync/index.d.ts +4 -0
- package/dist/types/sync/index.d.ts.map +1 -0
- package/dist/types/sync/lock.d.ts +7 -0
- package/dist/types/sync/lock.d.ts.map +1 -0
- package/dist/types/sync/unlock.d.ts +3 -0
- package/dist/types/sync/unlock.d.ts.map +1 -0
- package/dist/types/types/adapter.d.ts +21 -0
- package/dist/types/types/adapter.d.ts.map +1 -0
- package/dist/types/types/events.d.ts +37 -0
- package/dist/types/types/events.d.ts.map +1 -0
- package/dist/types/types/index.d.ts +8 -0
- package/dist/types/types/index.d.ts.map +1 -0
- package/dist/types/types/lock.d.ts +38 -0
- package/dist/types/types/lock.d.ts.map +1 -0
- package/dist/types/types/logger.d.ts +21 -0
- package/dist/types/types/logger.d.ts.map +1 -0
- package/dist/types/types/options.d.ts +52 -0
- package/dist/types/types/options.d.ts.map +1 -0
- package/dist/types/types/state.d.ts +34 -0
- package/dist/types/types/state.d.ts.map +1 -0
- package/dist/types/types/strategy.d.ts +24 -0
- package/dist/types/types/strategy.d.ts.map +1 -0
- package/dist/types/utils/backoff.d.ts +6 -0
- package/dist/types/utils/backoff.d.ts.map +1 -0
- package/dist/types/utils/fs.d.ts +12 -0
- package/dist/types/utils/fs.d.ts.map +1 -0
- package/dist/types/utils/hash.d.ts +7 -0
- package/dist/types/utils/hash.d.ts.map +1 -0
- package/dist/types/utils/hostname.d.ts +9 -0
- package/dist/types/utils/hostname.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +14 -0
- package/dist/types/utils/index.d.ts.map +1 -0
- package/dist/types/utils/parse.d.ts +7 -0
- package/dist/types/utils/parse.d.ts.map +1 -0
- package/dist/types/utils/path.d.ts +10 -0
- package/dist/types/utils/path.d.ts.map +1 -0
- package/dist/types/utils/pid.d.ts +11 -0
- package/dist/types/utils/pid.d.ts.map +1 -0
- package/dist/types/utils/platform.d.ts +12 -0
- package/dist/types/utils/platform.d.ts.map +1 -0
- package/dist/types/utils/random.d.ts +7 -0
- package/dist/types/utils/random.d.ts.map +1 -0
- package/dist/types/utils/retry.d.ts +10 -0
- package/dist/types/utils/retry.d.ts.map +1 -0
- package/dist/types/utils/serialize.d.ts +7 -0
- package/dist/types/utils/serialize.d.ts.map +1 -0
- package/dist/types/utils/time.d.ts +12 -0
- package/dist/types/utils/time.d.ts.map +1 -0
- package/dist/types/utils/validate.d.ts +5 -0
- package/dist/types/utils/validate.d.ts.map +1 -0
- package/package.json +132 -3
- package/src/adapters/atomic.ts +39 -0
- package/src/adapters/fs.ts +30 -0
- package/src/adapters/index.ts +31 -0
- package/src/adapters/mkdir.ts +32 -0
- package/src/adapters/symlink.ts +30 -0
- package/src/async/check.ts +15 -0
- package/src/async/index.ts +3 -0
- package/src/async/lock.ts +15 -0
- package/src/async/unlock.ts +11 -0
- package/src/cli/commands/cleanup.ts +28 -0
- package/src/cli/commands/list.ts +27 -0
- package/src/cli/commands/lock.ts +27 -0
- package/src/cli/commands/status.ts +26 -0
- package/src/cli/commands/unlock.ts +20 -0
- package/src/cli/formatters/index.ts +18 -0
- package/src/cli/formatters/json.ts +18 -0
- package/src/cli/formatters/table.ts +26 -0
- package/src/cli/formatters/text.ts +22 -0
- package/src/cli/index.ts +61 -0
- package/src/compat/cjs.ts +11 -0
- package/src/compat/esm.ts +11 -0
- package/src/compat/index.ts +3 -0
- package/src/compat/v1.ts +35 -0
- package/src/config/defaults.ts +20 -0
- package/src/config/index.ts +24 -0
- package/src/config/schema.ts +66 -0
- package/src/constants/defaults.ts +14 -0
- package/src/constants/errors.ts +39 -0
- package/src/constants/index.ts +14 -0
- package/src/constants/signals.ts +26 -0
- package/src/constants/timeouts.ts +18 -0
- package/src/core/acquire.ts +54 -0
- package/src/core/buildLockData.ts +25 -0
- package/src/core/check.ts +66 -0
- package/src/core/cleanup.ts +53 -0
- package/src/core/handle.ts +62 -0
- package/src/core/index.ts +8 -0
- package/src/core/list.ts +38 -0
- package/src/core/release.ts +35 -0
- package/src/errors/AbortError.ts +25 -0
- package/src/errors/ConflictError.ts +28 -0
- package/src/errors/IOError.ts +39 -0
- package/src/errors/LockError.ts +39 -0
- package/src/errors/PermissionError.ts +35 -0
- package/src/errors/StaleError.ts +41 -0
- package/src/errors/TimeoutError.ts +28 -0
- package/src/errors/ValidationError.ts +28 -0
- package/src/errors/index.ts +28 -0
- package/src/events/emitter.ts +50 -0
- package/src/events/index.ts +2 -0
- package/src/events/types.ts +16 -0
- package/src/hooks/afterLock.ts +24 -0
- package/src/hooks/afterUnlock.ts +24 -0
- package/src/hooks/beforeLock.ts +28 -0
- package/src/hooks/beforeUnlock.ts +24 -0
- package/src/hooks/deps +0 -0
- package/src/hooks/index.ts +25 -0
- package/src/hooks/onError.ts +22 -0
- package/src/hooks/onStale.ts +24 -0
- package/src/hooks/onTimeout.ts +27 -0
- package/src/index.ts +25 -0
- package/src/logger/console.ts +39 -0
- package/src/logger/debug.ts +29 -0
- package/src/logger/index.ts +20 -0
- package/src/logger/noop.ts +13 -0
- package/src/meta/index.ts +2 -0
- package/src/meta/package.ts +29 -0
- package/src/meta/version.ts +26 -0
- package/src/middleware/index.ts +18 -0
- package/src/middleware/logging.ts +21 -0
- package/src/middleware/metrics.ts +44 -0
- package/src/middleware/retry.ts +25 -0
- package/src/middleware/timeout.ts +34 -0
- package/src/platform/darwin.ts +19 -0
- package/src/platform/index.ts +6 -0
- package/src/platform/linux.ts +32 -0
- package/src/platform/unix.ts +36 -0
- package/src/platform/windows.ts +26 -0
- package/src/plugins/cleanup.ts +45 -0
- package/src/plugins/heartbeat.ts +42 -0
- package/src/plugins/index.ts +7 -0
- package/src/plugins/pid.ts +26 -0
- package/src/plugins/stale.ts +33 -0
- package/src/plugins/ttl.ts +27 -0
- package/src/promise/index.ts +2 -0
- package/src/promise/lock.ts +23 -0
- package/src/promise/withLock.ts +30 -0
- package/src/registry/global.ts +39 -0
- package/src/registry/index.ts +11 -0
- package/src/registry/local.ts +37 -0
- package/src/strategies/advisory.ts +21 -0
- package/src/strategies/exclusive.ts +21 -0
- package/src/strategies/index.ts +31 -0
- package/src/strategies/optimistic.ts +29 -0
- package/src/strategies/shared.ts +23 -0
- package/src/sync/check.ts +43 -0
- package/src/sync/index.ts +3 -0
- package/src/sync/lock.ts +38 -0
- package/src/sync/unlock.ts +19 -0
- package/src/types/adapter.ts +23 -0
- package/src/types/events.ts +63 -0
- package/src/types/index.ts +24 -0
- package/src/types/lock.ts +40 -0
- package/src/types/logger.ts +23 -0
- package/src/types/options.ts +57 -0
- package/src/types/state.ts +36 -0
- package/src/types/strategy.ts +31 -0
- package/src/utils/backoff.ts +52 -0
- package/src/utils/fs.ts +93 -0
- package/src/utils/hash.ts +25 -0
- package/src/utils/hostname.ts +31 -0
- package/src/utils/index.ts +13 -0
- package/src/utils/parse.ts +43 -0
- package/src/utils/path.ts +43 -0
- package/src/utils/pid.ts +36 -0
- package/src/utils/platform.ts +45 -0
- package/src/utils/random.ts +31 -0
- package/src/utils/retry.ts +59 -0
- package/src/utils/serialize.ts +31 -0
- package/src/utils/time.ts +51 -0
- package/src/utils/validate.ts +45 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Logger, LogLevel } from '../types/logger.js';
|
|
2
|
+
|
|
3
|
+
export const noopLogger: Logger = {
|
|
4
|
+
silent: () => undefined,
|
|
5
|
+
error: () => undefined,
|
|
6
|
+
warn: () => undefined,
|
|
7
|
+
info: () => undefined,
|
|
8
|
+
debug: () => undefined,
|
|
9
|
+
trace: () => undefined,
|
|
10
|
+
child: () => noopLogger,
|
|
11
|
+
setLevel: () => undefined,
|
|
12
|
+
getLevel: (): LogLevel => 'silent',
|
|
13
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export const PACKAGE_NAME = 'atomic-lockfile';
|
|
2
|
+
export const PACKAGE_DESCRIPTION = 'Atomic file-based locking with retry, TTL, heartbeat, PID tracking, stale detection, and multi-strategy support';
|
|
3
|
+
export const PACKAGE_HOMEPAGE = 'https://github.com/atomic-lockfile/atomic-lockfile';
|
|
4
|
+
export const PACKAGE_LICENSE = 'MIT';
|
|
5
|
+
export const PACKAGE_AUTHOR = 'atomic-lockfile contributors';
|
|
6
|
+
|
|
7
|
+
export const PACKAGE_CAPABILITIES = [
|
|
8
|
+
'atomic-write',
|
|
9
|
+
'ttl',
|
|
10
|
+
'heartbeat',
|
|
11
|
+
'pid-tracking',
|
|
12
|
+
'stale-detection',
|
|
13
|
+
'retry',
|
|
14
|
+
'backoff',
|
|
15
|
+
'multi-strategy',
|
|
16
|
+
'multi-adapter',
|
|
17
|
+
'events',
|
|
18
|
+
'hooks',
|
|
19
|
+
'middleware',
|
|
20
|
+
'plugins',
|
|
21
|
+
'cli',
|
|
22
|
+
'sync-api',
|
|
23
|
+
'async-api',
|
|
24
|
+
'esm',
|
|
25
|
+
'cjs',
|
|
26
|
+
'typescript',
|
|
27
|
+
] as const;
|
|
28
|
+
|
|
29
|
+
export type PackageCapability = (typeof PACKAGE_CAPABILITIES)[number];
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const VERSION = '1.0.0';
|
|
2
|
+
export const VERSION_MAJOR = 1;
|
|
3
|
+
export const VERSION_MINOR = 0;
|
|
4
|
+
export const VERSION_PATCH = 0;
|
|
5
|
+
export const VERSION_PRERELEASE: string | null = null;
|
|
6
|
+
export const VERSION_BUILD: string | null = null;
|
|
7
|
+
|
|
8
|
+
export function parseVersion(versionStr: string): { major: number; minor: number; patch: number } {
|
|
9
|
+
const [major = 0, minor = 0, patch = 0] = versionStr.split('.').map(Number);
|
|
10
|
+
return { major, minor, patch };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function compareVersions(a: string, b: string): -1 | 0 | 1 {
|
|
14
|
+
const pa = parseVersion(a);
|
|
15
|
+
const pb = parseVersion(b);
|
|
16
|
+
if (pa.major !== pb.major) return pa.major > pb.major ? 1 : -1;
|
|
17
|
+
if (pa.minor !== pb.minor) return pa.minor > pb.minor ? 1 : -1;
|
|
18
|
+
if (pa.patch !== pb.patch) return pa.patch > pb.patch ? 1 : -1;
|
|
19
|
+
return 0;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function isCompatibleVersion(required: string, current = VERSION): boolean {
|
|
23
|
+
const r = parseVersion(required);
|
|
24
|
+
const c = parseVersion(current);
|
|
25
|
+
return c.major === r.major && (c.minor > r.minor || (c.minor === r.minor && c.patch >= r.patch));
|
|
26
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { withLoggingMiddleware } from './logging.js';
|
|
2
|
+
export { createMetricsCollector } from './metrics.js';
|
|
3
|
+
export type { LockMetrics } from './metrics.js';
|
|
4
|
+
export { withTimeoutMiddleware, createTimeoutSignal } from './timeout.js';
|
|
5
|
+
export { withRetryMiddleware, withNoRetryMiddleware, withInfiniteRetryMiddleware } from './retry.js';
|
|
6
|
+
|
|
7
|
+
import type { AcquireOptions } from '../types/options.js';
|
|
8
|
+
|
|
9
|
+
export type Middleware = (options: AcquireOptions) => AcquireOptions;
|
|
10
|
+
|
|
11
|
+
export function composeMiddleware(...middlewares: Middleware[]): Middleware {
|
|
12
|
+
return (options: AcquireOptions) =>
|
|
13
|
+
middlewares.reduceRight((opts, mw) => mw(opts), options);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function applyMiddleware(options: AcquireOptions, ...middlewares: Middleware[]): AcquireOptions {
|
|
17
|
+
return composeMiddleware(...middlewares)(options);
|
|
18
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { AcquireOptions } from '../types/options.js';
|
|
2
|
+
import { getLogger } from '../logger/index.js';
|
|
3
|
+
|
|
4
|
+
export function withLoggingMiddleware(options: AcquireOptions): AcquireOptions {
|
|
5
|
+
const logger = getLogger();
|
|
6
|
+
return {
|
|
7
|
+
...options,
|
|
8
|
+
onAcquired: (lockPath) => {
|
|
9
|
+
logger.info('Lock acquired', { lockPath });
|
|
10
|
+
options.onAcquired?.(lockPath);
|
|
11
|
+
},
|
|
12
|
+
onRetry: (attempt, error) => {
|
|
13
|
+
logger.warn('Retrying lock acquisition', { attempt, error: error.message });
|
|
14
|
+
options.onRetry?.(attempt, error);
|
|
15
|
+
},
|
|
16
|
+
onStale: (lockPath) => {
|
|
17
|
+
logger.warn('Stale lock detected', { lockPath });
|
|
18
|
+
options.onStale?.(lockPath);
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { AcquireOptions } from '../types/options.js';
|
|
2
|
+
|
|
3
|
+
export interface LockMetrics {
|
|
4
|
+
acquisitions: number;
|
|
5
|
+
failures: number;
|
|
6
|
+
retries: number;
|
|
7
|
+
totalAcquisitionMs: number;
|
|
8
|
+
staleDetections: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function createMetricsCollector(): { metrics: LockMetrics; middleware: (opts: AcquireOptions) => AcquireOptions } {
|
|
12
|
+
const metrics: LockMetrics = {
|
|
13
|
+
acquisitions: 0,
|
|
14
|
+
failures: 0,
|
|
15
|
+
retries: 0,
|
|
16
|
+
totalAcquisitionMs: 0,
|
|
17
|
+
staleDetections: 0,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const startTimes = new Map<string, number>();
|
|
21
|
+
|
|
22
|
+
const middleware = (options: AcquireOptions): AcquireOptions => ({
|
|
23
|
+
...options,
|
|
24
|
+
onAcquired: (lockPath) => {
|
|
25
|
+
metrics.acquisitions++;
|
|
26
|
+
const start = startTimes.get(lockPath);
|
|
27
|
+
if (start !== undefined) {
|
|
28
|
+
metrics.totalAcquisitionMs += Date.now() - start;
|
|
29
|
+
startTimes.delete(lockPath);
|
|
30
|
+
}
|
|
31
|
+
options.onAcquired?.(lockPath);
|
|
32
|
+
},
|
|
33
|
+
onRetry: (attempt, error) => {
|
|
34
|
+
metrics.retries++;
|
|
35
|
+
options.onRetry?.(attempt, error);
|
|
36
|
+
},
|
|
37
|
+
onStale: (lockPath) => {
|
|
38
|
+
metrics.staleDetections++;
|
|
39
|
+
options.onStale?.(lockPath);
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return { metrics, middleware };
|
|
44
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { AcquireOptions } from '../types/options.js';
|
|
2
|
+
import { DEFAULT_RETRIES, DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MAX_DELAY } from '../constants/defaults.js';
|
|
3
|
+
|
|
4
|
+
export function withRetryMiddleware(options: AcquireOptions): AcquireOptions {
|
|
5
|
+
return {
|
|
6
|
+
retries: DEFAULT_RETRIES,
|
|
7
|
+
retryDelay: DEFAULT_RETRY_DELAY,
|
|
8
|
+
retryBackoff: 'exponential',
|
|
9
|
+
retryMaxDelay: DEFAULT_RETRY_MAX_DELAY,
|
|
10
|
+
...options,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function withNoRetryMiddleware(options: AcquireOptions): AcquireOptions {
|
|
15
|
+
return { ...options, retries: 0 };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function withInfiniteRetryMiddleware(options: AcquireOptions, maxDelayMs = DEFAULT_RETRY_MAX_DELAY): AcquireOptions {
|
|
19
|
+
return {
|
|
20
|
+
...options,
|
|
21
|
+
retries: Infinity,
|
|
22
|
+
retryBackoff: 'exponential',
|
|
23
|
+
retryMaxDelay: maxDelayMs,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AcquireOptions } from '../types/options.js';
|
|
2
|
+
import { DEFAULT_TIMEOUT } from '../constants/defaults.js';
|
|
3
|
+
|
|
4
|
+
export function withTimeoutMiddleware(
|
|
5
|
+
options: AcquireOptions,
|
|
6
|
+
defaultTimeoutMs = DEFAULT_TIMEOUT,
|
|
7
|
+
): AcquireOptions {
|
|
8
|
+
if (options.timeout !== undefined) return options;
|
|
9
|
+
const controller = new AbortController();
|
|
10
|
+
const timer = setTimeout(() => controller.abort(), defaultTimeoutMs);
|
|
11
|
+
|
|
12
|
+
const existingSignal = options.signal;
|
|
13
|
+
if (existingSignal) {
|
|
14
|
+
existingSignal.addEventListener('abort', () => {
|
|
15
|
+
clearTimeout(timer);
|
|
16
|
+
controller.abort();
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
...options,
|
|
22
|
+
timeout: defaultTimeoutMs,
|
|
23
|
+
signal: controller.signal,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function createTimeoutSignal(ms: number): { signal: AbortSignal; cancel: () => void } {
|
|
28
|
+
const controller = new AbortController();
|
|
29
|
+
const timer = setTimeout(() => controller.abort(), ms);
|
|
30
|
+
return {
|
|
31
|
+
signal: controller.signal,
|
|
32
|
+
cancel: () => clearTimeout(timer),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { isMacOS } from '../utils/platform.js';
|
|
2
|
+
|
|
3
|
+
export function assertDarwin(): void {
|
|
4
|
+
if (!isMacOS()) throw new Error('This operation is only supported on macOS');
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function getDarwinTempDir(): string {
|
|
8
|
+
return process.env['TMPDIR'] ?? '/tmp';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function isSandboxed(): boolean {
|
|
12
|
+
return !!process.env['APP_SANDBOX_CONTAINER_ID'];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function getContainerTempDir(): string | null {
|
|
16
|
+
return process.env['APP_SANDBOX_CONTAINER_ID']
|
|
17
|
+
? `${process.env['HOME']}/tmp`
|
|
18
|
+
: null;
|
|
19
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { execSync } from 'node:child_process';
|
|
3
|
+
import { isLinux } from '../utils/platform.js';
|
|
4
|
+
|
|
5
|
+
export function assertLinux(): void {
|
|
6
|
+
if (!isLinux()) throw new Error('This operation is only supported on Linux');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function getLinuxTempDir(): string {
|
|
10
|
+
return process.env['XDG_RUNTIME_DIR'] ?? '/tmp';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function isContainerEnvironment(): boolean {
|
|
14
|
+
try {
|
|
15
|
+
return readFileSync('/proc/1/cgroup', 'utf-8').includes('docker')
|
|
16
|
+
|| !!process.env['KUBERNETES_SERVICE_HOST'];
|
|
17
|
+
} catch {
|
|
18
|
+
return !!process.env['KUBERNETES_SERVICE_HOST'];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function getLinuxKernelVersion(): string | null {
|
|
23
|
+
try {
|
|
24
|
+
return execSync('uname -r', { encoding: 'utf-8' }).trim();
|
|
25
|
+
} catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function supportsInotify(): boolean {
|
|
31
|
+
return isLinux();
|
|
32
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { isUnix } from '../utils/platform.js';
|
|
2
|
+
|
|
3
|
+
export function assertUnix(): void {
|
|
4
|
+
if (!isUnix()) throw new Error('This operation is only supported on Unix-like systems');
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function getUnixFilePermissions(mode: number): string {
|
|
8
|
+
const octal = (mode & 0o777).toString(8).padStart(3, '0');
|
|
9
|
+
return octal;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function canUseFlock(): boolean {
|
|
13
|
+
return isUnix();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function canUseHardLinks(): boolean {
|
|
17
|
+
return isUnix();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function getUnixUid(): number {
|
|
21
|
+
if (typeof process.getuid !== 'function') throw new Error('getuid not available');
|
|
22
|
+
return process.getuid();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getUnixGid(): number {
|
|
26
|
+
if (typeof process.getgid !== 'function') throw new Error('getgid not available');
|
|
27
|
+
return process.getgid();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function isRootUser(): boolean {
|
|
31
|
+
try {
|
|
32
|
+
return getUnixUid() === 0;
|
|
33
|
+
} catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { isWindows } from '../utils/platform.js';
|
|
2
|
+
|
|
3
|
+
export function assertWindows(): void {
|
|
4
|
+
if (!isWindows()) throw new Error('This operation is only supported on Windows');
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function getWindowsTempDir(): string {
|
|
8
|
+
return process.env['TEMP'] ?? process.env['TMP'] ?? 'C:\\Temp';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function normalizeWindowsPath(filePath: string): string {
|
|
12
|
+
return filePath.replace(/\//g, '\\');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function isUNCPath(filePath: string): boolean {
|
|
16
|
+
return filePath.startsWith('\\\\');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getWindowsDrive(filePath: string): string | null {
|
|
20
|
+
const match = /^([A-Za-z]):/.exec(filePath);
|
|
21
|
+
return match ? (match[1] ?? null) : null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function canUseWindowsNamedMutex(): boolean {
|
|
25
|
+
return isWindows();
|
|
26
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { promises as fsp } from 'node:fs';
|
|
2
|
+
import { unlinkSafe } from '../utils/fs.js';
|
|
3
|
+
import { CLEANUP_SIGNALS } from '../constants/signals.js';
|
|
4
|
+
import type { CleanupSignal } from '../constants/signals.js';
|
|
5
|
+
|
|
6
|
+
const registeredCleanup = new Set<string>();
|
|
7
|
+
|
|
8
|
+
export function registerCleanupHandler(lockPath: string, onCleanup: (lockPath: string) => void): () => void {
|
|
9
|
+
registeredCleanup.add(lockPath);
|
|
10
|
+
|
|
11
|
+
const handler = (): void => {
|
|
12
|
+
if (registeredCleanup.has(lockPath)) {
|
|
13
|
+
onCleanup(lockPath);
|
|
14
|
+
registeredCleanup.delete(lockPath);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const signals: CleanupSignal[] = [...CLEANUP_SIGNALS];
|
|
19
|
+
signals.forEach((sig) => process.once(sig, handler));
|
|
20
|
+
process.once('exit', handler);
|
|
21
|
+
|
|
22
|
+
return () => {
|
|
23
|
+
signals.forEach((sig) => process.removeListener(sig, handler));
|
|
24
|
+
process.removeListener('exit', handler);
|
|
25
|
+
registeredCleanup.delete(lockPath);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function cleanupStaleLocks(directory: string, extension = '.lock'): Promise<string[]> {
|
|
30
|
+
const removed: string[] = [];
|
|
31
|
+
try {
|
|
32
|
+
const files = await fsp.readdir(directory);
|
|
33
|
+
for (const file of files) {
|
|
34
|
+
if (file.endsWith(extension)) {
|
|
35
|
+
await unlinkSafe(`${directory}/${file}`);
|
|
36
|
+
removed.push(file);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} catch { /* ignore */ }
|
|
40
|
+
return removed;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getRegisteredLocks(): string[] {
|
|
44
|
+
return [...registeredCleanup];
|
|
45
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { promises as fsp } from 'node:fs';
|
|
2
|
+
import type { LockData } from '../types/lock.js';
|
|
3
|
+
import { serializeLockData } from '../utils/serialize.js';
|
|
4
|
+
import { INTERVAL_HEARTBEAT } from '../constants/timeouts.js';
|
|
5
|
+
|
|
6
|
+
export interface HeartbeatHandle {
|
|
7
|
+
stop(): void;
|
|
8
|
+
readonly running: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function startHeartbeat(
|
|
12
|
+
lockPath: string,
|
|
13
|
+
getData: () => LockData,
|
|
14
|
+
intervalMs = INTERVAL_HEARTBEAT,
|
|
15
|
+
onError?: (err: Error) => void,
|
|
16
|
+
): HeartbeatHandle {
|
|
17
|
+
let running = true;
|
|
18
|
+
|
|
19
|
+
const beat = async (): Promise<void> => {
|
|
20
|
+
if (!running) return;
|
|
21
|
+
try {
|
|
22
|
+
const data = getData();
|
|
23
|
+
const updated: LockData = { ...data, updated: Date.now() };
|
|
24
|
+
await fsp.writeFile(lockPath, serializeLockData(updated), 'utf-8');
|
|
25
|
+
} catch (err) {
|
|
26
|
+
onError?.(err as Error);
|
|
27
|
+
}
|
|
28
|
+
if (running) setTimeout(beat, intervalMs);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const timer = setTimeout(beat, intervalMs);
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
stop() {
|
|
35
|
+
running = false;
|
|
36
|
+
clearTimeout(timer);
|
|
37
|
+
},
|
|
38
|
+
get running() {
|
|
39
|
+
return running;
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { checkStale, shouldRemoveStale } from './stale.js';
|
|
2
|
+
export type { StaleCheckResult } from './stale.js';
|
|
3
|
+
export { startHeartbeat } from './heartbeat.js';
|
|
4
|
+
export type { HeartbeatHandle } from './heartbeat.js';
|
|
5
|
+
export { writePidFile, readPidFile, removePidFile, buildPidFilePath } from './pid.js';
|
|
6
|
+
export { getTtlRemaining, isLockExpiredByTtl, buildTtlExpiry, ttlToHuman, validateTtl } from './ttl.js';
|
|
7
|
+
export { registerCleanupHandler, cleanupStaleLocks, getRegisteredLocks } from './cleanup.js';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { promises as fsp } from 'node:fs';
|
|
2
|
+
import { getCurrentPid } from '../utils/pid.js';
|
|
3
|
+
|
|
4
|
+
export async function writePidFile(pidFilePath: string): Promise<void> {
|
|
5
|
+
await fsp.writeFile(pidFilePath, String(getCurrentPid()), 'utf-8');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export async function readPidFile(pidFilePath: string): Promise<number | null> {
|
|
9
|
+
try {
|
|
10
|
+
const content = await fsp.readFile(pidFilePath, 'utf-8');
|
|
11
|
+
const pid = parseInt(content.trim(), 10);
|
|
12
|
+
return isNaN(pid) ? null : pid;
|
|
13
|
+
} catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function removePidFile(pidFilePath: string): Promise<void> {
|
|
19
|
+
try {
|
|
20
|
+
await fsp.unlink(pidFilePath);
|
|
21
|
+
} catch { /* ignore */ }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function buildPidFilePath(lockPath: string): string {
|
|
25
|
+
return `${lockPath}.pid`;
|
|
26
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { LockData } from '../types/lock.js';
|
|
2
|
+
import { isStale } from '../utils/time.js';
|
|
3
|
+
import { isOrphanedPid } from '../utils/pid.js';
|
|
4
|
+
import { isSameHost } from '../utils/hostname.js';
|
|
5
|
+
import { DEFAULT_STALE_AGE } from '../constants/defaults.js';
|
|
6
|
+
|
|
7
|
+
export interface StaleCheckResult {
|
|
8
|
+
isStale: boolean;
|
|
9
|
+
reason: 'age' | 'orphan' | 'expired' | null;
|
|
10
|
+
ageMs: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function checkStale(data: LockData, staleAgeMs = DEFAULT_STALE_AGE): StaleCheckResult {
|
|
14
|
+
const ageMs = Date.now() - data.updated;
|
|
15
|
+
|
|
16
|
+
if (data.expires !== null && Date.now() > data.expires) {
|
|
17
|
+
return { isStale: true, reason: 'expired', ageMs };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (isStale(data.updated, staleAgeMs)) {
|
|
21
|
+
return { isStale: true, reason: 'age', ageMs };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (isSameHost(data.hostname) && isOrphanedPid(data.pid)) {
|
|
25
|
+
return { isStale: true, reason: 'orphan', ageMs };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return { isStale: false, reason: null, ageMs };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function shouldRemoveStale(result: StaleCheckResult, removeStale: boolean): boolean {
|
|
32
|
+
return result.isStale && removeStale;
|
|
33
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { LockData } from '../types/lock.js';
|
|
2
|
+
import { remainingTtlMs, isExpired } from '../utils/time.js';
|
|
3
|
+
|
|
4
|
+
export function getTtlRemaining(data: LockData): number | null {
|
|
5
|
+
if (data.ttl === null) return null;
|
|
6
|
+
return remainingTtlMs(data.created, data.ttl);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function isLockExpiredByTtl(data: LockData): boolean {
|
|
10
|
+
if (data.ttl === null) return false;
|
|
11
|
+
return isExpired(data.created, data.ttl);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function buildTtlExpiry(ttlMs: number): number {
|
|
15
|
+
return Date.now() + ttlMs;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function ttlToHuman(ttlMs: number): string {
|
|
19
|
+
if (ttlMs < 1000) return `${ttlMs}ms`;
|
|
20
|
+
if (ttlMs < 60_000) return `${(ttlMs / 1000).toFixed(1)}s`;
|
|
21
|
+
return `${(ttlMs / 60_000).toFixed(1)}m`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function validateTtl(ttlMs: number): void {
|
|
25
|
+
if (ttlMs <= 0) throw new RangeError(`TTL must be > 0, got ${ttlMs}`);
|
|
26
|
+
if (!Number.isFinite(ttlMs)) throw new RangeError(`TTL must be finite, got ${ttlMs}`);
|
|
27
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AcquireOptions } from '../types/options.js';
|
|
2
|
+
import type { LockHandle } from '../types/lock.js';
|
|
3
|
+
import { acquireLock } from '../core/acquire.js';
|
|
4
|
+
|
|
5
|
+
export function lock(path: string, options?: AcquireOptions): Promise<LockHandle> {
|
|
6
|
+
return acquireLock(path, options);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function tryLock(path: string, options?: AcquireOptions): Promise<LockHandle | null> {
|
|
10
|
+
return acquireLock(path, { ...options, retries: 0 }).catch(() => null);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function lockWithTimeout(
|
|
14
|
+
path: string,
|
|
15
|
+
timeoutMs: number,
|
|
16
|
+
options?: AcquireOptions,
|
|
17
|
+
): Promise<LockHandle> {
|
|
18
|
+
const controller = new AbortController();
|
|
19
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
20
|
+
return acquireLock(path, { ...options, signal: controller.signal, timeout: timeoutMs }).finally(
|
|
21
|
+
() => clearTimeout(timer),
|
|
22
|
+
);
|
|
23
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { WithLockOptions } from '../types/options.js';
|
|
2
|
+
import { acquireLock } from '../core/acquire.js';
|
|
3
|
+
|
|
4
|
+
export async function withLock<T>(
|
|
5
|
+
path: string,
|
|
6
|
+
fn: () => T | Promise<T>,
|
|
7
|
+
options: WithLockOptions = {},
|
|
8
|
+
): Promise<T> {
|
|
9
|
+
const handle = await acquireLock(path, options);
|
|
10
|
+
try {
|
|
11
|
+
return await fn();
|
|
12
|
+
} finally {
|
|
13
|
+
await handle.release();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function withLockResult<T>(
|
|
18
|
+
path: string,
|
|
19
|
+
fn: () => T | Promise<T>,
|
|
20
|
+
options: WithLockOptions = {},
|
|
21
|
+
): Promise<{ result: T; heldMs: number }> {
|
|
22
|
+
const start = Date.now();
|
|
23
|
+
const handle = await acquireLock(path, options);
|
|
24
|
+
try {
|
|
25
|
+
const result = await fn();
|
|
26
|
+
return { result, heldMs: Date.now() - start };
|
|
27
|
+
} finally {
|
|
28
|
+
await handle.release();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { LockStateSnapshot } from '../types/state.js';
|
|
2
|
+
|
|
3
|
+
class GlobalLockRegistry {
|
|
4
|
+
private readonly locks = new Map<string, LockStateSnapshot>();
|
|
5
|
+
|
|
6
|
+
register(snapshot: LockStateSnapshot): void {
|
|
7
|
+
this.locks.set(snapshot.lockPath, snapshot);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
unregister(lockPath: string): void {
|
|
11
|
+
this.locks.delete(lockPath);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
get(lockPath: string): LockStateSnapshot | undefined {
|
|
15
|
+
return this.locks.get(lockPath);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
has(lockPath: string): boolean {
|
|
19
|
+
return this.locks.has(lockPath);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
getAll(): LockStateSnapshot[] {
|
|
23
|
+
return [...this.locks.values()];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
getActive(): LockStateSnapshot[] {
|
|
27
|
+
return this.getAll().filter((s) => s.state === 'locked');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
clear(): void {
|
|
31
|
+
this.locks.clear();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
size(): number {
|
|
35
|
+
return this.locks.size;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const globalRegistry = new GlobalLockRegistry();
|