npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hot-patcher

v2.0.1

Published

Hot method patching framework for handling environmental method differences

Downloads

95,084

Readme

Hot-Patcher

Hot method patching framework for handling environmental method differences

Build status npm version

About

Hot-Patcher provides a simple API to manage patched methods. I found while writing Buttercup that managing overwritten methods between environments (Node/Browser/React-Native) was becoming cumbersome, and having a single agreed-upon method of doing so was the best way to go.

Installation

Install Hot-Patcher from npm:

npm install hot-patcher --save

NB: This is an ESM library.

Usage

Hot-Patcher is a class and can simply be instantiated:

import { HotPatcher } from "hot-patcher";

const hp = new HotPatcher();

Hot-Patcher is designed to be used with patchable tools:

import { HotPatcher } from "hot-patcher";

export class MyHelper {
    public patcher: HotPatcher;

    constructor() {
        this.patcher = new HotPatcher();
    }

    increment(arg: number): number {
        return this.patcher.patchInline<number>("increment", someArg => {
            return someArg + 1;
        }, arg);
    }
}

You can then patch methods when required:

import { MyHelper } from "./MyHelper.js";

export function getHelper() {
    const helper = new MyHelper();
    helper.patch("increment", (val: number) => val + 2);
    return helper;
}

Patched methods can easily be fetched later:

import { getSharedPatcher } from "./patching.js";

const randomString = getSharedPatcher().get("randomString");
randomString(5); // Generates a random string

// Or, execute the method directly:
getSharedPatcher().execute("randomString", 5) // Generates a random string

You can check if a method is patched by using isPatched: patcher.isPatched("some method").

Inline patching and execution

Ideally you could wrap function implementation with a patch call, executing it on demand:

function add(a: number, b: number): number {
    return patcher.patchInline("add", (a, b) => a + b, a, b);
}

patcher.isPatched("add"); // false
add(1, 2); // 3
patcher.isPatched("add"); // true
// calling add() multiple times will call the patched method without "re-patching" it
// over and over again..

Plugins - Chaining/Sequencing functions

You can use Hot-Patcher to create sequences of functions:

patcher.plugin("increment", x => x * 2, x => x * 2);

patcher.execute("increment", 2); // 8

Which is basically syntactic sugar for a regular patch() call:

patcher
    .patch("increment", x => x * 2, { chain: true })
    .patch("increment", x => x * 2, { chain: true });

patcher.execute("increment", 2); // 8

Executing a regular patch() without chain: true will overwrite all chained methods with the new method.

Calling patch() with chain: true when a method already exists will simply add the new method after the existing:

patcher
    .patch("increment", x => x * 2, { chain: false }) // or simply without `chain` specified
    .patch("increment", x => x * 2, { chain: true });

patcher.execute("increment", 2); // still 8

Restoring methods

Methods can be restored to their originally patched function by calling the restore method:

const methodA = () => {};
const methodB = () => {};

patcher
    // methodA is now the current (and original)
    .patch("someMethod", methodA)
    // methodB is now the current
    .patch("someMethod", methodB);

// Restore "someMethod" to methodA (original)
patcher.restore("someMethod");

Use Sparingly

The intention of Hot-Patcher is not to push every method into a patching instance, but to provide a common API for specific methods which require patching in some specific environments or in situations where users/consumers are expected to provide their own custom implementations.