— MIT Licensed — 280+ tests passing

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.

$npm install @powerduck/conf-patch
JSON + JSONC + YAMLRFC 6902 PatchAtomic WritesFile LocksBrowser SafeOpenAPI Validation
Quick Start

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"
Features

Production-grade configuration editing

Built for Electron apps, CLI tools, and web applications that need reliable config manipulation.

02

Three Formats

JSON, JSONC (with comments), and YAML. Auto-detect format via detectFormat(). Preserve comments and formatting in JSONC and YAML.

03

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.

04

Atomic Writes & File Locks

Write to temp file then rename for crash safety. withFileLock() prevents concurrent writes. Cross-platform advisory locking.

05

OpenAPI Validation

validateOpenAPISpec() validates OpenAPI 2.0/3.0/3.1 documents. validateOpenAPIFile() validates from file path. Detailed error reporting.

06

Dual Module & Type Safe

ESM and CJS exports with full TypeScript types. ./core subpath for browser-only usage. Zero runtime dependencies.

Architecture

Two layers, one API

Choose the layer that fits your runtime. Both share the same patch semantics.

Core

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

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)
API Reference

Complete API

Core and file layer APIs.

Core Layer — import from @powerduck/conf-patch/core

functionpatchContent(content, ops, format?, options?)Apply RFC 6902 patch operations to a config string
ParameterTypeDescription
contentrequiredstringConfig content (JSON/JSONC/YAML)
opsrequiredJsonPatchOp[]RFC 6902 operations
format"json" | "jsonc" | "yaml"Config format (auto-detected if omitted)
options.strictbooleanThrow on first failed op default: true
functionsetContentValue(content, path, value, format?, options?)Set a single value at a JSON path array (e.g. ["server", "port"])

Convenience wrapper around patchContent with a single replace/add operation.

functiondeleteContentValue(content, path, format?, options?)Delete a value at a JSON path array (e.g. ["server", "port"])

Convenience wrapper around patchContent with a single remove operation.

functiondetectFormat(filePath)Auto-detect config format from file extension
Returns
"json" | "jsonc" | "yaml" | null

File Layer — import from @powerduck/conf-patch

functionreadConfigFile(filePath)Read and parse a config file

Returns parsed config object. Format auto-detected from file extension and content.

functionwriteConfigFile(filePath, content, options?)Atomically write config content to file

Writes to temp file then renames for crash safety. Creates parent directories if needed.

functionpatchConfigFile(filePath, ops, options?)Read, patch, and atomically write a config file

Combines read + patchContent + writeConfigFile. Use withFileLock for concurrent safety.

functionsetConfigValue(filePath, path, value, options?)Set a single value in a config file

Convenience: read, set value, atomic write.

functiondeleteConfigValue(filePath, path, options?)Delete a value from a config file

Convenience: read, delete value, atomic write.

functionwithFileLock(paths, callback)Execute callback with advisory file locks

Acquires locks for all paths, executes callback, releases locks. Prevents concurrent writes. Use releaseAllLocalLocks() for cleanup.

OpenAPI Validation

functionvalidateOpenAPISpec(spec, filePath?)Validate an OpenAPI spec from string or object

Supports Swagger 2.0, OpenAPI 3.0, and 3.1. Returns { valid, errors, warnings }.

functionvalidateOpenAPIFile(filePath)Validate an OpenAPI spec from a file path

Reads file and validates. Supports JSON and YAML.

functionnormalizeFilePath(path)Normalize and validate a file path

Resolves relative paths, normalizes separators, prevents path traversal. Returns null for invalid paths.

3
Formats
2
Layers
0
Runtime Deps
MIT
License