{"id":15979248,"url":"https://github.com/perry-mitchell/hot-patcher","last_synced_at":"2025-05-07T03:21:11.963Z","repository":{"id":57266791,"uuid":"141818749","full_name":"perry-mitchell/hot-patcher","owner":"perry-mitchell","description":"Hot method patching framework for handling environmental method differences","archived":false,"fork":false,"pushed_at":"2023-07-17T11:32:44.000Z","size":526,"stargazers_count":3,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-19T17:57:59.696Z","etag":null,"topics":["hot-replacement","override","patch","patching"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/perry-mitchell.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["perry-mitchell"]}},"created_at":"2018-07-21T14:09:00.000Z","updated_at":"2023-01-27T17:42:51.000Z","dependencies_parsed_at":"2024-06-18T18:35:35.937Z","dependency_job_id":"d7a6b6a2-c533-45b7-9c6b-b17b25a15167","html_url":"https://github.com/perry-mitchell/hot-patcher","commit_stats":{"total_commits":71,"total_committers":2,"mean_commits":35.5,"dds":"0.014084507042253502","last_synced_commit":"4edf9b18a427be5873a399813b78a5777c863800"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perry-mitchell%2Fhot-patcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perry-mitchell%2Fhot-patcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perry-mitchell%2Fhot-patcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perry-mitchell%2Fhot-patcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/perry-mitchell","download_url":"https://codeload.github.com/perry-mitchell/hot-patcher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252804770,"owners_count":21806872,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hot-replacement","override","patch","patching"],"created_at":"2024-10-07T23:41:45.659Z","updated_at":"2025-05-07T03:21:11.946Z","avatar_url":"https://github.com/perry-mitchell.png","language":"JavaScript","funding_links":["https://github.com/sponsors/perry-mitchell"],"categories":[],"sub_categories":[],"readme":"# Hot-Patcher\n\u003e Hot method patching framework for handling environmental method differences\n\n![Build status](https://github.com/perry-mitchell/hot-patcher/actions/workflows/test.yml/badge.svg) [![npm version](https://badge.fury.io/js/hot-patcher.svg)](https://www.npmjs.com/package/hot-patcher)\n\n## About\n\nHot-Patcher provides a simple API to manage patched methods. I found while writing [Buttercup](https://buttercup.pw) 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.\n\n## Installation\n\nInstall Hot-Patcher from [npm](https://www.npmjs.com/package/hot-patcher):\n\n```shell\nnpm install hot-patcher --save\n```\n\n**NB:** This is an ESM library.\n\n## Usage\n\nHot-Patcher is a class and can simply be instantiated:\n\n```typescript\nimport { HotPatcher } from \"hot-patcher\";\n\nconst hp = new HotPatcher();\n```\n\nHot-Patcher is designed to be used with patchable tools:\n\n```typescript\nimport { HotPatcher } from \"hot-patcher\";\n\nexport class MyHelper {\n    public patcher: HotPatcher;\n\n    constructor() {\n        this.patcher = new HotPatcher();\n    }\n\n    increment(arg: number): number {\n        return this.patcher.patchInline\u003cnumber\u003e(\"increment\", someArg =\u003e {\n            return someArg + 1;\n        }, arg);\n    }\n}\n```\n\nYou can then patch methods when required:\n\n```typescript\nimport { MyHelper } from \"./MyHelper.js\";\n\nexport function getHelper() {\n    const helper = new MyHelper();\n    helper.patch(\"increment\", (val: number) =\u003e val + 2);\n    return helper;\n}\n```\n\nPatched methods can easily be fetched later:\n\n```typescript\nimport { getSharedPatcher } from \"./patching.js\";\n\nconst randomString = getSharedPatcher().get(\"randomString\");\nrandomString(5); // Generates a random string\n\n// Or, execute the method directly:\ngetSharedPatcher().execute(\"randomString\", 5) // Generates a random string\n```\n\nYou can check if a method is patched by using `isPatched`: `patcher.isPatched(\"some method\")`.\n\n### Inline patching and execution\n\nIdeally you could wrap function implementation with a patch call, executing it on demand:\n\n```typescript\nfunction add(a: number, b: number): number {\n    return patcher.patchInline(\"add\", (a, b) =\u003e a + b, a, b);\n}\n\npatcher.isPatched(\"add\"); // false\nadd(1, 2); // 3\npatcher.isPatched(\"add\"); // true\n// calling add() multiple times will call the patched method without \"re-patching\" it\n// over and over again..\n```\n\n### Plugins - Chaining/Sequencing functions\n\nYou can use Hot-Patcher to create sequences of functions:\n\n```typescript\npatcher.plugin(\"increment\", x =\u003e x * 2, x =\u003e x * 2);\n\npatcher.execute(\"increment\", 2); // 8\n```\n\nWhich is basically syntactic sugar for a regular `patch()` call: \n\n```typescript\npatcher\n    .patch(\"increment\", x =\u003e x * 2, { chain: true })\n    .patch(\"increment\", x =\u003e x * 2, { chain: true });\n\npatcher.execute(\"increment\", 2); // 8\n```\n\nExecuting a regular `patch()` without `chain: true` will overwrite all chained methods with the new method. \n\nCalling `patch()` with `chain: true` when a method already exists will simply add the new method after the existing:\n\n```typescript\npatcher\n    .patch(\"increment\", x =\u003e x * 2, { chain: false }) // or simply without `chain` specified\n    .patch(\"increment\", x =\u003e x * 2, { chain: true });\n\npatcher.execute(\"increment\", 2); // still 8\n```\n\n\n### Restoring methods\nMethods can be restored to their _originally patched function_ by calling the `restore` method:\n\n```typescript\nconst methodA = () =\u003e {};\nconst methodB = () =\u003e {};\n\npatcher\n    // methodA is now the current (and original)\n    .patch(\"someMethod\", methodA)\n    // methodB is now the current\n    .patch(\"someMethod\", methodB);\n\n// Restore \"someMethod\" to methodA (original)\npatcher.restore(\"someMethod\");\n```\n\n## Use Sparingly\n\nThe 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperry-mitchell%2Fhot-patcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fperry-mitchell%2Fhot-patcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperry-mitchell%2Fhot-patcher/lists"}