Hot Method Patcher

2.0.1 · active · verified Sun Apr 19

Hot-Patcher is a JavaScript/TypeScript library designed to manage method overrides and patching across different environments, such as Node.js, web browsers, and React Native. It provides a consistent API for dynamically replacing or augmenting existing methods, which is particularly useful for adapting code behavior to specific runtime contexts or for plugin architectures. The library is currently at version 2.0.1 and focuses on offering a "single agreed-upon method" for handling these patches, addressing common challenges faced when managing conditional method implementations. While a specific release cadence is not stated, the project appears to be actively maintained. Key differentiators include its explicit support for chaining and sequencing functions via a plugin API, as well as clear mechanisms for restoring methods to their original patched state. It enforces an ESM-only architecture, which is a significant consideration for integration into existing projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to instantiate `HotPatcher`, wrap an existing function with `patchInline` for initial registration, apply a new patch, and then restore the original patched function.

import { HotPatcher } from "hot-patcher";

// Instantiate the patcher
const patcher = new HotPatcher();

// Define an original function (could be a method on a class)
function calculate(a: number, b: number): number {
    console.log("Original calculate called");
    return a + b;
}

// Wrap the original function with patchInline for initial execution and registration
function add(a: number, b: number): number {
    return patcher.patchInline("addNumbers", (numA, numB) => calculate(numA, numB), a, b);
}

console.log("Initial call (should use original logic):", add(5, 3));

// Now, patch the 'addNumbers' method to multiply instead
patcher.patch("addNumbers", (numA: number, numB: number) => {
    console.log("Patched calculate called, multiplying instead!");
    return numA * numB;
});

console.log("After patching (should use patched logic):", add(5, 3));

// Using execute directly
console.log("Direct execute after patching:", patcher.execute("addNumbers", 10, 2));

// Restore the method to its originally patched function
patcher.restore("addNumbers");
console.log("After restoring (should use original logic again):", add(5, 3));

view raw JSON →