Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Infect.js is a simple way to add the magic of dependency injection to any web project, regardless of the framework on which you choose to write your application. It's infectiously simple!
Infect.js is a simple way to add the magic of dependency injection to any web project, regardless of the framework on which you choose to write your application. It's infectiously simple!
NPM: npm install infect
Bower: bower install infect
infect.set(String, Object|Function|Array|etc...)
A simple call to infect.set()
with the name you want to use, and the mutable object you'd like to register will do the trick. In the example below we are using a function, but you can register any type of mutable value (Functions, Arrays, Objects, etc).
infect.set('Logger', function (str) {
// prepend a time to every log line
console.log((new Date()).toLocaleTimeString() + ' ==> ' + str);
});
infect.func(Function, Object)
When you're writing a function that needs one or more dependency, simply pass it to infect.func()
and reference the dependencies as parameters that are prefixed with a dollar sign ($
). All dependencies should be added to the end of the parameter list, and they are not expected when you call the function. Optionally you may pass in an object that you want to be used as this
inside the function. If no object is provided, a blank object will be created for this purpose (the global scope will not be used).
var foo = infect.func(function (name, age, $Logger) {
$Logger(name + ' is ' + age);
}, this);
foo('Joe', 27);
// CONSOLE
// 11:50:37 PM ==> Joe is 27
infect.func(Function)
The infect.func()
method can also support javascript constructors (classes). Just like above, all dependencies should be added to the end of the parameter list, and they are not expected when you new
the constructor.
function Cat(name, $Logger) {
$Logger(name + ' is a Cat');
this.name = name;
}
Cat = infect.func(Cat);
var c = new Cat('Mr. Buttons');
infect.get('Logger')('is c a Cat? ' + (c instanceof Cat));
// CONSOLE
// 11:50:37 PM ==> Mr. Buttons is a Cat
// 11:50:37 PM ==> is c a Cat? true
infect.obj(Object, Array)
Sometimes function injection may not work for you, but you'd still like an easy way to pull multiple dependencies into a single place for reference within your code. Object injection suits this nicely and can be done by simply calling infect.obj()
with an object and an array of dependency names you'd like to have injected. Please node that any object can be injected except the global Window
object (for obvious reasons). If you try to infect the global scope, infect
will throw an error.
NOTE: You can reference the dependency with or without the dollar sign prefix ($
). Regardless of how you reference it, the object will be injected with the prefixed version for consistency.
var INFECTED = infect.obj({}, ['$Logger']);
var ALSO_INFECTED = {};
infect.obj(ALSO_INFECTED, ['Logger']);
INFECTED.$Logger('foo!');
ALSO_INFECTED.$Logger('bar!');
// CONSOLE
// 11:50:37 PM ==> foo!
// 11:50:37 PM ==> bar!
infect.get(String)
As a simple way to reference your dependencies, you can pull them out and assign individual dependencies to variables. This is not recommended as it breaks the consistency of your code, but it is possible so I wanted to show an example of usage.
var log = infect.get('Logger');
log('foo bar!');
// CONSOLE
// 11:50:37 PM ==> foo bar!
Function.$infect = Array
When running your code through minification or obfuscation, the function parameters get renamed in order to save space. Obviously this wreaks havoc on Infect's ability to detect what values should be injected into the function at execution. To solve this you can simply add an array of dependency names to the $infect
property of your function and infect will inject these values into the function.
var foo = infect.func(function (n, a, L) {
L(n + ' is ' + a);
}, this);
foo.$infect = ['Logger'];
foo('Joe', 27);
// CONSOLE
// 11:50:37 PM ==> Joe is 27
FAQs
Infect.js is a simple way to add the magic of dependency injection to any web project, regardless of the framework on which you choose to write your application. It's infectiously simple!
The npm package infect receives a total of 25 weekly downloads. As such, infect popularity was classified as not popular.
We found that infect 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.