Hot Method Patcher
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
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ...hot-patcher/dist/index.js from ... not supported.
cause Attempting to `require()` the `hot-patcher` package in a CommonJS context, but the library is ESM-only.fixEnsure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) and use `import { HotPatcher } from 'hot-patcher';` instead of `const HotPatcher = require('hot-patcher');`. -
TypeError: hot_patcher_1.HotPatcher is not a constructor
cause Incorrect import statement (e.g., a default import instead of a named import) or attempting to instantiate a non-constructor.fixVerify that `HotPatcher` is imported as a named export: `import { HotPatcher } from 'hot-patcher';`. If using TypeScript, ensure `esModuleInterop` is enabled if you are trying to use `require` with a transpiler.
Warnings
- breaking Hot-Patcher is an ESM-only library starting from version 2.x. This means it can only be imported using `import` statements and will not work with CommonJS `require()` in Node.js environments unless transpiled or bundled.
- gotcha Calling `patch(name, fn)` without the `{ chain: true }` option will overwrite all existing chained methods for that `name` with the new function `fn`.
- gotcha The `patchInline` method will register and immediately execute a method, marking it as patched. In contrast, `patch` only registers the method without immediate execution.
Install
-
npm install hot-patcher -
yarn add hot-patcher -
pnpm add hot-patcher
Imports
- HotPatcher
const { HotPatcher } = require('hot-patcher');import { HotPatcher } from 'hot-patcher'; - HotPatcher (Type)
import type { HotPatcher } from 'hot-patcher'; - PatchMethod
import type { PatchMethod } from 'hot-patcher';
Quickstart
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));