Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
async-plugin
Advanced tools
Readme
Another way to implement AOP in JavaScript.
By using this module, one can easily write pluggable and extendable components
to manipulate objects and be benefit from async/await
functionality.
PS: this module internally uses function-intercepter to bind and call functions.
npm i async-plugin
import AsyncPlugin from "async-plugin";
var plugin = new AsyncPlugin();
// Bind a handler function to add a name on the output object.
plugin.bind((input, output) => {
output.name = input.name || "Ayon Lee";
});
// Bind an async handler to fetch data from the internet and assign them to the
// output object if the input one doesn't have cache data.
plugin.bind(async (input, output) => {
if (input.cache) {
Object.assign(output, input.cache);
} else {
let res = await fetch(`https://somewhere.com/data?name=${output.name}`);
let data = await res.json();
input.cache = data;
Object.assign(output, data);
}
});
(async () => {
var obj = {};
// Apply the plugin and called bound handlers.
var output = await plugin.apply(obj);
console.log(output); // => { name: "Ayon Lee", ... }
})();
To create an AsyncPlugin instance, there are several signatures you can use:
new AsyncPlugin<I = any, O = any>()
new AsyncPlugin<I, O = any>(inputType?: I | (new (...arg) => I))
new AsyncPlugin<I, O>(inputType?: I | (new (...args) => I), outputType?: O | (new (...args) => O))
inputType
The prototype of the input object, it is very common to set this
argument a class constructor or null
to indicate any types.
outputType
The prototype of output object, when apply the plugin, a new object created via
Object.create(outputType)
will be passed to the first handler.
Note that both inputType
and outputType
can be omitted, passing them is
mainly for type hints in the IDE, and when omitted, they will be assigned
Object
.
To bind a handler on the plugin, just call the plugin.bind()
method, which
accepts the signature in this way:
plugin.bind((input: I, output?: O) => void | false | [I, O] | Promise<void | false | [I, O]>): this
Normally this method will not return anything, but when it does, the false
value indicates to stop calling remaining handlers and return the output
object immediately. Other than false
, the only acceptable returning value is
an array contains both the input
and output
objects, and when they're
returned,they will be passed to the next handler as arguments. That means you're
allowed to return a different value in the previous handler so that to change
the context in the next handler (however it's not recommended).
To apply the plugin in a context, just call plugin.apply()
method and pass the
input
object and optional output
object as default value.
plugin.apply(input: I, output?: O): Promise<O>
If output
is omitted, it will be generated via Object.create(outputType)
as
talked above. After all the handlers are called, this method returns the output
object.
FAQs
Another way to implement AOP in JavaScript.
The npm package async-plugin receives a total of 1 weekly downloads. As such, async-plugin popularity was classified as not popular.
We found that async-plugin 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.