description: "Atomic file replacement and cross-process writer locking for packages that must never leave partial, symlink-hijacked, or wider-permission content on disk."
English | 中文
dsh-atomic-write replaces a file's contents in one atomic step: readers of the target always observe either the complete old content or the complete new content, never a partial write. It also serializes read-modify-write cycles across processes with a writer lock, so concurrent writers of one file cannot resurrect each other's state. The caller states the permission bits for every replacement and the fresh inode carries them through the swap, so replacing a wider-permission file narrows it without a chmod race. It is a zero-dependency library shared by file-backed stores such as the user-settings document and the credentials store; a cordis.yml cannot load it, and crash durability is the caller's policy because there is no fsync.
Use writeFileAtomic when a file-backed store must replace one already-rendered string without ever exposing a partial, symlink-hijacked, or wider-permission state, and withFileLock when several processes read-modify-write the same file. The smallest path is one call with the final content and the replacement's permission bits.
import { writeFileAtomic } from '@deepseek-ai/dsh-atomic-write'
declare const text: string
await writeFileAtomic('/home/u/.dsh/settings.yaml', text, { mode: 0o600 })
Parent directories are created as needed, and readers observe either the old or the new complete content. On Windows, transient replacement interference reported as EACCES, EBUSY, or EPERM is retried for a bounded interval; any remaining failure removes the temporary file and leaves the target untouched.
For a read-render-commit cycle that a bare atomic commit cannot make safe on its own, hold the writer lock around the operation:
import { withFileLock, writeFileAtomic } from '@deepseek-ai/dsh-atomic-write'
declare const render: (previous: string) => string
declare const readCurrent: () => Promise<string>
await withFileLock('/home/u/.dsh/settings.yaml', async () => {
const previous = await readCurrent()
await writeFileAtomic('/home/u/.dsh/settings.yaml', render(previous), { mode: 0o600 })
})
Only writers contend — readers never take the lock — and a contender backs off exponentially and fails with a timed-out error rather than blocking forever. How long a contender waits is stated per call through waitMs: the default is sized for file work alone, so a holder whose cycle includes a network round trip — a credential mutation that refreshes an expired token — states a longer one, because leaving the default would fail every other writer of that file for the duration. The retry cadence stays fixed. A contender never removes an existing lock, because file age cannot prove that its owner stopped.
The lock's parent directory must already exist, so withFileLock rejects an invalid parent hierarchy before running the operation. A process that exits while holding the lock leaves the lock sibling behind; later writers time out, and an operator removes it only after verifying that no writer still owns it.
Read these pages when you need the consuming stores or the family this primitive belongs to.
None, as this is a pure filesystem write primitive that registers nothing model-facing.
Nothing here enters a request prefix, so provider cache reuse is unaffected.
These limits define where the package is not the right tool. They are current package constraints, not a task backlog.
fsync of the file or its directory, so after a crash the rename may be observed unwound. The file-backed stores here re-read and republish on boot, keeping durability the caller's policy.Buffer or stream form until a consumer needs one.