Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

electrolyte

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

electrolyte - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

6

lib/component.js

@@ -17,2 +17,8 @@ var debug = require('debug')('electrolyte');

var inst = container.create(deps[i], this);
var source = container._sources[this._sid];
if (source) {
if (typeof source.fn.scope == 'function') {
inst = source.fn.scope(deps[i], inst, { prefix: source.prefix, options: source.options });
}
}
args.push(inst);

@@ -19,0 +25,0 @@ }

98

lib/container.js

@@ -0,1 +1,4 @@

/**
* Module dependencies.
*/
var path = require('path')

@@ -9,7 +12,43 @@ , Factory = require('./patterns/factory')

/**
* `Container` constructor.
*
* A container contains a set of named object instances, known as components.
* These components are automatically created when needed and injected into
* other components that require them.
*
* A default `Container` singleton is exported via the module. Applications
* should not need to construct additional instances, and are advised against
* doing so.
*
* @api public
*/
function Container() {
this._o = {};
this._loaders = [];
this._sources = {};
this._order = [];
}
/**
* Create a component.
*
* When the component being created requires other components (using `@require`
* annotations), those components will automatically be created and injected
* into the component. In this way, complex graphs of objects can be
* constructed with a single line of code, eliminating extraneous boilerplate.
*
* A component can be annotated as being a singletion (using `@singleton`). If
* so, only one instance of the named component will be created. Subsequent
* calls to create the component will return the singleton instance.
*
* Examples:
*
* var foo = IoC.create('foo');
*
* var boop = IoC.create('beep/boop');
*
* @param {String} id
* @return {mixed}
* @api public
*/
Container.prototype.create = function(id, parent) {

@@ -23,3 +62,4 @@ if (parent && id[0] == '.') {

// special modules
if (id == '$container') {
switch (id) {
case '$container':
return this;

@@ -34,2 +74,3 @@ }

}
comp = this._o[id];

@@ -45,3 +86,3 @@ if (!comp) {

Container.prototype.factory = function(id, dependencies, fn) {
Container.prototype.factory = function(id, dependencies, fn, sid) {
if (typeof dependencies == 'function') {

@@ -52,6 +93,6 @@ fn = dependencies;

debug('register factory %s %s', id, dependencies);
this.register(new Factory(id, dependencies, fn));
this.register(new Factory(id, dependencies, fn), sid);
}
Container.prototype.singleton = function(id, dependencies, fn) {
Container.prototype.singleton = function(id, dependencies, fn, sid) {
if (typeof dependencies == 'function') {

@@ -62,6 +103,6 @@ fn = dependencies;

debug('register singleton %s %s', id, dependencies);
this.register(new Singleton(id, dependencies, fn));
this.register(new Singleton(id, dependencies, fn), sid);
}
Container.prototype.constructor = function(id, dependencies, ctor) {
Container.prototype.constructor = function(id, dependencies, ctor, sid) {
if (typeof dependencies == 'function') {

@@ -72,15 +113,17 @@ ctor = dependencies;

debug('register constructor %s %s', id, dependencies);
this.register(new Constructor(id, dependencies, ctor));
this.register(new Constructor(id, dependencies, ctor), sid);
}
Container.prototype.literal = function(id, obj) {
Container.prototype.literal = function(id, obj, sid) {
debug('register literal %s', id);
this.register(new Literal(id, [], obj));
this.register(new Literal(id, [], obj), sid);
}
Container.prototype.register = function(comp) {
Container.prototype.register = function(comp, sid) {
// TODO: Pass sid to constructor (??)
comp._sid = sid;
this._o[comp.id] = comp;
}
Container.prototype.loader = function(prefix, fn) {
Container.prototype.loader = function(prefix, fn, options) {
if (typeof prefix == 'function') {

@@ -91,3 +134,5 @@ fn = prefix;

if (prefix.length && prefix[prefix.length - 1] != '/') { prefix += '/'; }
this._loaders.push({ prefix: prefix, fn: fn });
var id = this._order.length;
this._sources[id] = { prefix: prefix, fn: fn, options: options };
this._order.push({ id: id, prefix: prefix, fn: fn });
}

@@ -97,12 +142,13 @@

debug('autoload %s', id);
var loaders = this._loaders
, loader, prefix, lid, mod;
for (var i = 0, len = loaders.length; i < len; ++i) {
loader = loaders[i];
prefix = loader.prefix;
var sources = this._order
, source, prefix, rid, mod;
for (var i = 0, len = sources.length; i < len; ++i) {
source = sources[i];
prefix = source.prefix;
if (id.indexOf(prefix) !== 0) { continue; }
lid = id.slice(prefix.length);
mod = loader.fn(lid);
rid = id.slice(prefix.length);
mod = source.fn(rid);
if (mod) {
this._registerModule(id, mod);
this._registerModule(id, mod, source.id);
break;

@@ -113,3 +159,3 @@ }

Container.prototype._registerModule = function(id, mod) {
Container.prototype._registerModule = function(id, mod, sid) {
var dependencies = mod['@require'] || []

@@ -138,12 +184,12 @@ , pattern = 'literal';

case 'factory':
this.factory(id, dependencies, mod);
this.factory(id, dependencies, mod, sid);
break;
case 'singleton':
this.singleton(id, dependencies, mod);
this.singleton(id, dependencies, mod, sid);
break;
case 'constructor':
this.constructor(id, dependencies, mod);
this.constructor(id, dependencies, mod, sid);
break;
case 'literal':
this.literal(id, mod);
this.literal(id, mod, sid);
break;

@@ -150,0 +196,0 @@ }

@@ -17,2 +17,3 @@ var Component = require('../component')

debug('instantiate %s', this.id);
this.inst = this.fn.apply(undefined, arguments);

@@ -19,0 +20,0 @@ this.loaded = true;

{
"name": "electrolyte",
"version": "0.0.4",
"version": "0.0.5",
"description": "Elegant dependency injection for Node.js.",

@@ -5,0 +5,0 @@ "keywords": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc