UNPKG

tusk

Version:

Tusk lets you carve into JavaScript, TypeScript, and beyond with raw power.

2 lines (1 loc) 1.53 kB
"use strict";var a=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var d=(n,e)=>{for(var t in e)a(n,t,{get:e[t],enumerable:!0})},l=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of u(e))!g.call(n,o)&&o!==t&&a(n,o,{get:()=>e[o],enumerable:!(r=f(e,o))||r.enumerable});return n};var x=n=>l(a({},"__esModule",{value:!0}),n);var h={};d(h,{patch:()=>p,unpatch:()=>T});module.exports=x(h);function y(n,e,t){if(!n||typeof n!="object")throw new TypeError("Target must be a non-null object.");if(!e||typeof e!="string")throw new TypeError("Method must be a valid string.");if(typeof t!="function")throw new TypeError("Patch function must be a function.")}var c=new WeakMap,i=new WeakMap;function p(n,e,t){y(n,e,t);let r=n[e];if(typeof r!="function")throw new TypeError(`${String(e)} is not a function.`);if(i.has(n)&&i.get(n).has(e))throw new TypeError(`Method ${String(e)} is already patched.`);i.has(n)||i.set(n,new Map),i.get(n).set(e,{target:n,methodName:e,originalMethod:r,implementation:function(...o){return t.call(this,r,...o)}}),Object.defineProperty(n,e,{value:i.get(n).get(e).implementation,configurable:!0,writable:!0})}function T(n,e){let t=i.get(n)?.get(e),r=c.get(n)?.get(e);if(t)Object.defineProperty(n,e,{value:t.originalMethod,configurable:!0,writable:!0}),i.get(n).delete(e);else if(r)c.get(n).delete(e);else throw new TypeError(`Method ${String(e)} was not patched or extended.`)}0&&(module.exports={patch,unpatch});