Two-Layer Config Editor for JSON, JSONC & YAML
Pure core functions for browser-safe patching. File layer with atomic writes and file locks for Node.js/Electron. RFC 6902 JSON Patch. OpenAPI spec validation built in. Dual ESM/CJS module.
Patch configs in seconds
import { patchContent, setContentValue } from "@powerduck/conf-patch/core"; import { detectFormat } from "@powerduck/conf-patch"; // Patch a config string (works in browser, no fs dependency) const original = `{"name": "app", "version": "1.0.0"}`; const patched = patchContent(original, [ { op: "replace", path: ["version"], value: "2.0.0" }, { op: "add", path: ["author"], value: "Powerduck" }, ], "json"); // Or use the convenience setter const updated = setContentValue(original, ["version"], "3.0.0", "json"); // Detect format from a file path (by extension) const format = detectFormat("./config.yaml"); // "yaml"
Production-grade configuration editing
Built for Electron apps, CLI tools, and web applications that need reliable config manipulation.
Two-Layer Architecture
Core layer: pure functions operating on strings, browser-safe, no fs dependency. File layer: atomic writes with file locks for Node.js/Electron. Use what you need.
Three Formats
JSON, JSONC (with comments), and YAML. Auto-detect format via detectFormat(). Preserve comments and formatting in JSONC and YAML.
RFC 6902 JSON Patch
Full RFC 6902 support: add, remove, replace, move, copy, test. Batch operations with strict and non-strict modes. Detailed error messages.
Atomic Writes & File Locks
Write to temp file then rename for crash safety. withFileLock() prevents concurrent writes. Cross-platform advisory locking.
OpenAPI Validation
validateOpenAPISpec() validates OpenAPI 2.0/3.0/3.1 documents. validateOpenAPIFile() validates from file path. Detailed error reporting.
Dual Module & Type Safe
ESM and CJS exports with full TypeScript types. ./core subpath for browser-only usage. Zero runtime dependencies.
Two layers, one API
Choose the layer that fits your runtime. Both share the same patch semantics.
Core Layer (Browser Safe)
Pure functions operating on config strings. No Node.js dependencies. Works in browsers, Edge Functions, Web Workers.
patchContent(content, ops, format?)setContentValue(content, path, value, format?)deleteContentValue(content, path, format?)- Import from
@powerduck/conf-patch/core
File Layer (Node.js / Electron)
File I/O with atomic writes and advisory locks. Wraps the core layer. Use in Electron main process, CLI tools, and Node.js servers.
readConfigFile(filePath)writeConfigFile(filePath, content)patchConfigFile(filePath, ops)setConfigValue(filePath, path, value)withFileLock(paths, callback)
Complete API
Core and file layer APIs.
Core Layer — import from @powerduck/conf-patch/core
| Parameter | Type | Description |
|---|---|---|
| contentrequired | string | Config content (JSON/JSONC/YAML) |
| opsrequired | JsonPatchOp[] | RFC 6902 operations |
| format | "json" | "jsonc" | "yaml" | Config format (auto-detected if omitted) |
| options.strict | boolean | Throw on first failed op default: true |
Convenience wrapper around patchContent with a single replace/add operation.
Convenience wrapper around patchContent with a single remove operation.
"json" | "jsonc" | "yaml" | null
File Layer — import from @powerduck/conf-patch
Returns parsed config object. Format auto-detected from file extension and content.
Writes to temp file then renames for crash safety. Creates parent directories if needed.
Combines read + patchContent + writeConfigFile. Use withFileLock for concurrent safety.
Convenience: read, set value, atomic write.
Convenience: read, delete value, atomic write.
Acquires locks for all paths, executes callback, releases locks. Prevents concurrent writes. Use releaseAllLocalLocks() for cleanup.
OpenAPI Validation
Supports Swagger 2.0, OpenAPI 3.0, and 3.1. Returns { valid, errors, warnings }.
Reads file and validates. Supports JSON and YAML.
Resolves relative paths, normalizes separators, prevents path traversal. Returns null for invalid paths.