Security News
The Dark Side of Open Source
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
to-aop
Advanced tools
Changelog
Readme
The to-aop module help you with applying Aspect Oriented Programming to JavaScript. It use under the hood ES Proxy for object same as other similar modules. It allow you hook class without creating new instance as well. It use javascript prototype for that.
For example I am using it for adding hooks to production code where all debug code is missing. I don't have got access to instance but I have only class constructor. But usage is unlimited.
More articles about AOP:
Examples:
npm i to-aop --save
Easy example for native Date.now method.
import { aop, hookName, createHook } from 'to-aop';
const nowHook = createHook(hookName.aroundMethod, 'now', (args) => {
return 1;
});
aop(Date, nowHook);
Date.now() // returns 1
Other example with more hooks for your defined class.
// a.js
export default class A {
constructor(variable) {
this.variable = variable;
}
method() {
return this.variable;
}
notHookedMethod() {
return 'not hook';
}
}
You can use it for creating hook to your favorite framework or your own application without modification source code.
import { aop, hookName, createHook, unAop } from 'to-aop';
import A from './a';
const classHookBefore = createHook(
hookName.beforeMethod,
/^(method)$/,
({ target, object, property, context, args, meta }) => {
//call your own hook
meta.startTime = performance.now();
console.log(
`Instance of ${target.name} call "${property}"
with arguments ${args && args.length ? args : '[]'}.`
);
}
);
const classHookAfter = createHook(
hookName.afterMethod,
/^(method)$/,
({ target, object, property, context, args, payload, meta }) => {
//call your own hook
console.log(
`Instance of ${target.name} call "${property}"
with arguments ${args && args.length ? args : '[]'}
and return value is "${payload}" took ${performance.now() - meta.startTime}.`
);
}
);
const hooks = Object.assign({}, classHookBefore, classHookAfter);
aop(
A,
hooks
); // bind hook to class
const a = new A('my hook');
a.method(); // log: 'Instance of A call "method" with arguments [] and return value is "my hook".', returns: 'my hook'
a.notHookedClassMethod(); // returns: 'not hook'
unAop(A);
a.method(); // returns: 'my hook'
a.notHookedClassMethod(); // returns: 'not hook'
import { aop, hookName, createHook, unAop } from 'to-aop';
import A from './a';
const instanceHook = createHook(
hookName.afterMethod,
/^(method)$/,
({ target, object, property, context, args, payload, meta }) => {
console.log(
`Instance of ${object.constructor.name} call "${property}"
with arguments ${args && args.length ? args : '[]'}
and return value is "${payload}".`
);
}
);
const a = new A('my hook');
const hookedInstance = aop(a, instanceHook); // bind hook to instance
hookedInstance.method(); // log: 'Instance of A call "method" with arguments [] and return value is "my hook.', returns: 'my hook'
hookedInstance.notHookedClassMethod(); // returns: 'not hook'
unAop(a);
a.method(); // returns: 'my hook'
a.notHookedClassMethod(); // returns: 'not hook'
We implemented base set of hooks which you can use for AOP.
import { hookName } from 'to-aop';
// hookName.(beforeMethod|afterMethod|...)
FAQs
AOP(aspect-oriented programming) library or creating hooks for debug and monkey patch function using ES6 Proxy|Class Prototype.
The npm package to-aop receives a total of 3,196 weekly downloads. As such, to-aop popularity was classified as popular.
We found that to-aop demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.
Security News
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.