From d77b0956e9907634d6beb7123fd89e0a79409990 Mon Sep 17 00:00:00 2001 From: Ruthenic Date: Sun, 9 Jan 2022 22:16:07 -0500 Subject: [PATCH] typedefs????? --- .flowconfig | 11 ++++++++ .gitignore | 1 + dist/patcher.js | 1 + dist/patcher.js.flow | 49 ++++++++++++++++++++++++++++++++++++ package.json | 22 +++++++++++++--- rollup.config.js | 11 ++++++++ patcher.js => src/patcher.js | 11 ++++---- 7 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 .flowconfig create mode 100644 .gitignore create mode 100644 dist/patcher.js create mode 100644 dist/patcher.js.flow create mode 100644 rollup.config.js rename patcher.js => src/patcher.js (62%) diff --git a/.flowconfig b/.flowconfig new file mode 100644 index 0000000..1fed445 --- /dev/null +++ b/.flowconfig @@ -0,0 +1,11 @@ +[ignore] + +[include] + +[libs] + +[lints] + +[options] + +[strict] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/dist/patcher.js b/dist/patcher.js new file mode 100644 index 0000000..4eda8eb --- /dev/null +++ b/dist/patcher.js @@ -0,0 +1 @@ +"use strict";function monkeyPatch(e,t,o){var n=Symbol();const i=o.before,c=o.instead,f=o.after,r=new Proxy(t[e],{apply:(e,t,[n,r])=>{void 0!==i&&i.apply(n,r);const a=void 0!==o.instead?c.apply(n,[e.bind(n),...r]):e.apply(n,r);return void 0===f?a:f.apply(n,[a].concat(r))}}),a=t[e];t[e]=function(){return r(this,arguments)};var u=()=>{t[e]=a};return t[n]={name:e,orig:a,unpatch:u},u}function before(e,t,n){return monkeyPatch(e,t,{before:n})}function instead(e,t,n){return monkeyPatch(e,t,{instead:n})}function after(e,t,n){return monkeyPatch(e,t,{after:n})}module.exports={monkeyPatch:monkeyPatch,before:before,instead:instead,after:after}; diff --git a/dist/patcher.js.flow b/dist/patcher.js.flow new file mode 100644 index 0000000..1687c61 --- /dev/null +++ b/dist/patcher.js.flow @@ -0,0 +1,49 @@ +// @flow +function monkeyPatch(name: string, parentObj: Object, patches: Object): ()=>void { + const injId = Symbol() + const before = patches["before"]; + const instead = patches["instead"]; + const after = patches["after"]; + const handler = { + apply: (target, thisArg, [ctx, args]) => { + if (before !== undefined) before.apply(ctx, args); + const res = (patches["instead"] !== undefined) ? instead.apply(ctx, [target.bind(ctx), ...args]) : target.apply(ctx, args) + if (after === undefined) return res + return after.apply(ctx, [res].concat(args)); + } + }; + const prox = new Proxy(parentObj[name], handler); + const orig = parentObj[name]; + parentObj[name] = function() { + return prox(this, arguments); + }; + const unpatch = () => { + parentObj[name] = orig; + } + parentObj[injId] = { + name: name, + orig: orig, + unpatch: unpatch + }; + return unpatch; +} + +function before(name: string, parentObj: Object, func: (...args: mixed[])=>void): ()=>void { + return monkeyPatch(name, parentObj, {before: func}) +} + +function instead(name: string, parentObj: Object, func: (orig: (any)=>any, ...args: mixed[])=>any): ()=>void { + return monkeyPatch(name, parentObj, {instead: func}) +} + +function after(name: string, parentObj: Object, func: (res: any, ...args: mixed[])=>any): ()=>void { + return monkeyPatch(name, parentObj, {after: func}) +} + +module.exports = { + monkeyPatch, + before, + instead, + after +} + diff --git a/package.json b/package.json index b232478..0fd55ae 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,9 @@ "name": "demonpatcher", "version": "0.0.5", "description": "Patch with the Power of Satan 😈😈😈", - "main": "patcher.js", + "main": "dist/patcher.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "build": "flow-copy-source src dist && rollup --config rollup.config.js" }, "repository": { "type": "git", @@ -12,5 +12,21 @@ }, "author": "ruthenic", "license": "BSD-3-Clause", - "keywords": ["patcher", "patch", "demon", "devil", "satan", "serious", "hazbin", "hotel", "hell"] + "keywords": [ + "patcher", + "patch", + "demon", + "devil", + "satan", + "serious", + "hazbin", + "hotel", + "hell" + ], + "devDependencies": { + "flow-copy-source": "^2.0.9", + "rollup": "^2.63.0", + "rollup-plugin-flow": "^1.1.1", + "rollup-plugin-uglify": "^6.0.4" + } } diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..fed1db2 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,11 @@ +import flow from 'rollup-plugin-flow' +import { uglify } from 'rollup-plugin-uglify' + +export default { + input: 'src/patcher.js', + plugins: [flow(), uglify()], + output: { + file: 'dist/patcher.js', + format: "cjs" + } +} diff --git a/patcher.js b/src/patcher.js similarity index 62% rename from patcher.js rename to src/patcher.js index 52a4e95..1687c61 100644 --- a/patcher.js +++ b/src/patcher.js @@ -1,4 +1,5 @@ -function monkeyPatch(name, parentObj, patches) { +// @flow +function monkeyPatch(name: string, parentObj: Object, patches: Object): ()=>void { const injId = Symbol() const before = patches["before"]; const instead = patches["instead"]; @@ -6,7 +7,7 @@ function monkeyPatch(name, parentObj, patches) { const handler = { apply: (target, thisArg, [ctx, args]) => { if (before !== undefined) before.apply(ctx, args); - res = (patches["instead"] !== undefined) ? instead.apply(ctx, [target.bind(ctx), ...args]) : target.apply(ctx, args) + const res = (patches["instead"] !== undefined) ? instead.apply(ctx, [target.bind(ctx), ...args]) : target.apply(ctx, args) if (after === undefined) return res return after.apply(ctx, [res].concat(args)); } @@ -27,15 +28,15 @@ function monkeyPatch(name, parentObj, patches) { return unpatch; } -function before(name, parentObj, func) { +function before(name: string, parentObj: Object, func: (...args: mixed[])=>void): ()=>void { return monkeyPatch(name, parentObj, {before: func}) } -function instead(name, parentObj, func) { +function instead(name: string, parentObj: Object, func: (orig: (any)=>any, ...args: mixed[])=>any): ()=>void { return monkeyPatch(name, parentObj, {instead: func}) } -function after(name, parentObj, func) { +function after(name: string, parentObj: Object, func: (res: any, ...args: mixed[])=>any): ()=>void { return monkeyPatch(name, parentObj, {after: func}) }