Skip to content

VaultMD / withFileTransform

Function: withFileTransform() ​

withFileTransform(fullPath, lockKey, relForCommit, transform, opts?): Promise<TransformResult>

Defined in: src/locked-file/transform.ts:36

Apply a transform to a vault file inside the per-file lock, with optimistic-concurrency retry when another writer changes the file, or creates it, between the read and the write. The transform callback receives the current file content (or null if the file is absent) and must return the desired new content, or null to leave the file untouched. A create never replaces a file that appeared after the read: the transform runs again on that file's text. When other writers still change or create the file after maxRetries retries, it throws MTIME_CONFLICT. The write is atomic and keeps the file's mode and, where the OS allows, its group and owner, as VaultIo.writeVaultFile says; it fails with UNSAFE_MODE when the OS gives the new file a mode that gives the group or the others more access than the file's mode did. It refuses a fullPath that is a symlink rather than replace the link; the read follows a link like any read does, so pass paths that VaultIo resolved — it refuses a link on every segment.

Parameters ​

fullPath ​

string

Absolute filesystem path to the target file.

lockKey ​

string

Canonical/case-folded serialization key — pass VaultIo.toKey(rel).

relForCommit ​

string

Display path written to CommitEvent.path — pass VaultIo.toVaultRelative(rel).

transform ​

(current) => string | null

Pure function from current content to desired content, or null for no-op.

opts? ​

TransformOpts = {}

Optional behaviour overrides — see TransformOpts.

Returns ​

Promise<TransformResult>

A TransformResult describing the outcome and final content.

Released under the MIT License.