typedefs?????

master
Drake 2 years ago
parent f3c06ad1be
commit d77b0956e9

@ -0,0 +1,11 @@
[ignore]
[include]
[libs]
[lints]
[options]
[strict]

1
.gitignore vendored

@ -0,0 +1 @@
node_modules

1
dist/patcher.js vendored

@ -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};

@ -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
}

@ -2,9 +2,9 @@
"name": "demonpatcher", "name": "demonpatcher",
"version": "0.0.5", "version": "0.0.5",
"description": "Patch with the Power of Satan 😈😈😈", "description": "Patch with the Power of Satan 😈😈😈",
"main": "patcher.js", "main": "dist/patcher.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "build": "flow-copy-source src dist && rollup --config rollup.config.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -12,5 +12,21 @@
}, },
"author": "ruthenic", "author": "ruthenic",
"license": "BSD-3-Clause", "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"
}
} }

@ -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"
}
}

@ -1,4 +1,5 @@
function monkeyPatch(name, parentObj, patches) { // @flow
function monkeyPatch(name: string, parentObj: Object, patches: Object): ()=>void {
const injId = Symbol() const injId = Symbol()
const before = patches["before"]; const before = patches["before"];
const instead = patches["instead"]; const instead = patches["instead"];
@ -6,7 +7,7 @@ function monkeyPatch(name, parentObj, patches) {
const handler = { const handler = {
apply: (target, thisArg, [ctx, args]) => { apply: (target, thisArg, [ctx, args]) => {
if (before !== undefined) before.apply(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 if (after === undefined) return res
return after.apply(ctx, [res].concat(args)); return after.apply(ctx, [res].concat(args));
} }
@ -27,15 +28,15 @@ function monkeyPatch(name, parentObj, patches) {
return unpatch; return unpatch;
} }
function before(name, parentObj, func) { function before(name: string, parentObj: Object, func: (...args: mixed[])=>void): ()=>void {
return monkeyPatch(name, parentObj, {before: func}) 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}) 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}) return monkeyPatch(name, parentObj, {after: func})
} }
Loading…
Cancel
Save