Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
A powerful, extensible dependency injection micro container
BottleJS is a tiny yet powerful dependency injection container. It features lazy loading, middleware hooks, and a clean api inspired by the AngularJS Module API and the simple PHP library Pimple. You'll like BottleJS if you enjoy:
BottleJS supports IE9+ and other ECMAScript 5 compliant browsers.
BottleJS can be used in a browser or in a nodejs app. It can be installed via bower or npm:
$ bower install bottlejs
$ npm install bottlejs
The simplest recipe to get started with is Bottle#service
. Say you have a constructor for a service object:
var Beer = function() { /* A beer service, :yum: */ };
You can register the constructor with Bottle#service
:
var bottle = new Bottle();
bottle.service('Beer', Beer);
Later, when you need the constructed service, you just access the Beer
property like this:
bottle.container.Beer;
A lot happened behind the scenes:
bottle.container.Beer
property was accessed, Bottle looked up the provider and executed the factory to build and return the Beer service.bottle.container.Beer
property was set to be the Beer service instance. Accessing bottle.container.Beer
in the future becomes a simple property lookup.The above example is simple. But, what if the Beer service had dependencies? For eample:
var Barley = function() {};
var Hops = function() {};
var Water = function() {};
var Beer = function(barley, hops, water) { /* A beer service, :yum: */ };
You can register services with Bottle#service
and include dependencies like this:
var bottle = new Bottle();
bottle.service('Barley', Barley);
bottle.service('Hops', Hops);
bottle.service('Water', Water);
bottle.service('Beer', Beer, 'Barley', 'Hops', 'Water');
Now, when you access bottle.container.Beer
, Bottle will lazily load all of the dependencies and inject them into your Beer service before returning it.
If you need more complex logic when generating a service, you can register a factory instead. A factory function receives the container as an argument, and should return your constructed service:
var bottle = new Bottle();
bottle.service('Barley', Barley);
bottle.service('Hops', Hops);
bottle.service('Water', Water);
bottle.factory('Beer', function(container) {
var barley = container.Barley;
var hops = container.Hops;
var water = container.Water;
barley.halved();
hops.doubled();
water.spring();
return new Beer(barley, hops, water);
});
This is the meat of the Bottle library. The above methods Bottle#service
and Bottle#factory
are just shorthand for the provider function. You usually can get by with the simple functions above, but if you really need more granular control of your services in different environments, regiser them as a provider. To use it, pass a constructor for the provider that exposes a $get
function. The $get
function is used as a factory to build your service.
var bottle = new Bottle();
bottle.service('Barley', Barley);
bottle.service('Hops', Hops);
bottle.service('Water', Water);
bottle.provider('Beer', function() {
// This environment may not support water.
// We should polyfill it.
if (waterNotSupported) {
Beer.pollyfillWater();
}
// this is the service factory.
this.$get = function(container) {
var barley = container.Barley;
var hops = container.Hops;
var water = container.Water;
barley.halved();
hops.doubled();
water.spring();
return new Beer(barley, hops, water);
};
});
Bottle supports injecting middleware into the provider pipeline with the Bottle#middleware
method. Bottle middleware are just simple functions that intercept a service in the provider phase after it has been created, but before it is accessed for the first time. The function should return the service, or another object to be used as the service instead.
var bottle = new Bottle();
bottle.service('Beer', Beer);
bottle.service('Wine', Wine);
bottle.middleware(function(service) {
// this middleware will be run for both Beer and Wine services.
service.stayCold();
return service;
});
bottle.middleware('Wine', function(wine) {
// this middleware will only affect the Wine service.
wine.unCork();
return wine;
});
Used to add a read only value to the container.
Param | Type | Details |
---|---|---|
name | String | The name of the constant. Must be unique to each Bottle instance. |
value | Mixed | A value that will be defined as enumerable, but not writable. |
Used to register a service factory
Param | Type | Details |
---|---|---|
name | String | The name of the service. Must be unique to each Bottle instance. |
Factory | Function | A function that should return the service object. Will only be called once; the Service will be a singleton. Gets passed an instance of the container to allow dependency injection when creating the service. |
Used to register a middleware function that the provider will use to modify your services at creation time.
Param | Type | Details |
---|---|---|
name (optional) | String | The name of the service this middleware will affect. Will run for all services if not passed. |
func | Function | A function that will accept the service as the first parameter. Should return the service, or a new object to be used as the service. |
Used to register a service provider
Param | Type | Details |
---|---|---|
name | String | The name of the service. Must be unique to each Bottle instance. |
Provider | Function | A constructor function that will be instantiated as a singleton. Should expose a function called $get that will be used as a factory to instantiate the service. |
Used to register a service constructor
Param | Type | Details |
---|---|---|
name | String | The name of the service. Must be unique to each Bottle instance. |
Constructor | Function | A constructor function that will be instantiated as a singleton. |
dependency (optional) | String | An optional name for a dependency to be passed to the constructor. A dependency will be passed to the constructor for each name passed to Bottle#service in the order they are listed. |
Used to add an arbitrary value to the container.
Param | Type | Details |
---|---|---|
name | String | The name of the value. Must be unique to each Bottle instance. |
val | Mixed | A value that will be defined as enumerable, but not writable. |
FAQs
A powerful dependency injection micro container
The npm package bottlejs receives a total of 23,850 weekly downloads. As such, bottlejs popularity was classified as popular.
We found that bottlejs 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’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.