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

dependency-injection

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dependency-injection - npm Package Compare versions

Comparing version 1.6.7 to 1.7.0

Helpers.js

101

lib/DI.js
// Generated by CoffeeScript 1.6.3
(function() {
var DI, Service,
var DI, Helpers, Service,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

@@ -8,2 +8,4 @@

Helpers = require('./Helpers');
DI = (function() {

@@ -14,4 +16,13 @@ DI.prototype.services = null;

DI.prototype.creating = null;
function DI() {
this.services = {};
var di;
di = new Service(this, 'di', this);
di.instantiate = false;
di.injectMethods = false;
this.services = {
di: di
};
this.creating = [];
}

@@ -26,3 +37,3 @@

}
this.services[name] = new Service(this, service, args);
this.services[name] = new Service(this, name, service, args);
return this.services[name];

@@ -32,64 +43,10 @@ };

DI.prototype.autowireArguments = function(method, args) {
var arg, customArg, factory, i, methodArgs, num, result, self, _i, _ref,
_this = this;
if (args == null) {
args = [];
}
method = method.toString();
method = method.replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg, '');
methodArgs = method.slice(method.indexOf('(') + 1, method.indexOf(')')).match(/([^\s,]+)/g);
methodArgs = methodArgs === null ? [] : methodArgs;
num = methodArgs.length > args.length ? methodArgs.length : args.length;
result = [];
if (num === 0) {
return result;
}
for (i = _i = 0, _ref = num - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
arg = typeof methodArgs[i] === 'undefined' ? null : methodArgs[i];
if (arg === null) {
result.push(args[i]);
} else {
customArg = typeof args[i] !== 'undefined';
if (!customArg || (customArg && typeof args[i] === 'string' && (args[i] === '...' || args[i][0] === '@'))) {
if (customArg && args[i][0] === '@') {
arg = args[i].substr(1);
}
factory = false;
if (arg.match(/Factory$/) !== null) {
arg = arg.substring(0, arg.length - 7);
factory = true;
}
if (arg === 'di') {
self = factory === true ? function() {
return _this;
} : this;
result.push(self);
} else if (this.findDefinitionByName(arg).autowired === false) {
throw new Error("DI: Service '" + arg + "' can not be autowired.");
} else if (factory === true) {
result.push(this.getFactory(arg));
} else {
result.push(this.getByName(arg));
}
} else {
result.push(args[i]);
}
}
}
return result;
console.log('Method autowireArguments is deprecated, use the same method in Helpers class.');
return Helpers.autowireArguments(method, args, this);
};
DI._newInstanceWrapper = function(obj, args) {
var f;
if (args == null) {
args = [];
}
f = function() {
return obj.apply(this, args);
};
f.prototype = obj.prototype;
return f;
};
DI.prototype.createInstance = function(service, args, instantiate) {
DI.prototype.createInstance = function(service, args, instantiate, injectMethods) {
var method;

@@ -102,6 +59,11 @@ if (args == null) {

}
if (injectMethods == null) {
injectMethods = true;
}
if (instantiate === true) {
service = new (DI._newInstanceWrapper(service, this.autowireArguments(service, args)));
service = Helpers.createInstance(service, args, this);
} else if (Object.prototype.toString.call(service) === '[object Function]') {
service = this.inject(service, {}, args);
}
if (Object.prototype.toString.call(service) === '[object Object]') {
if (Object.prototype.toString.call(service) === '[object Object]' && injectMethods) {
for (method in service) {

@@ -116,14 +78,20 @@ if (method.match(/^inject/) !== null) {

DI.prototype.inject = function(fn, scope) {
var args;
DI.prototype.inject = function(fn, scope, args) {
if (scope == null) {
scope = {};
}
if (args == null) {
args = [];
}
if (!(fn instanceof Function)) {
throw new Error('DI: Inject method can be called only on functions.');
}
args = this.autowireArguments(fn, []);
args = Helpers.autowireArguments(fn, args, this);
return fn.apply(scope, args);
};
DI.prototype.hasDefinition = function(name) {
return typeof this.services[name] !== 'undefined';
};
DI.prototype.findDefinitionByName = function(name, need) {

@@ -133,3 +101,3 @@ if (need == null) {

}
if (typeof this.services[name] === 'undefined') {
if (!this.hasDefinition(name)) {
if (need === true) {

@@ -145,2 +113,3 @@ throw new Error("DI: Service '" + name + "' was not found.");

DI.prototype.getByName = function(name) {
console.log('DI: Method getByName is deprecated, use get method.');
return this.get(name);

@@ -147,0 +116,0 @@ };

@@ -78,3 +78,3 @@ // Generated by CoffeeScript 1.6.3

name = run[_i];
di.getByName(name);
di.get(name);
}

@@ -81,0 +81,0 @@ return di;

// Generated by CoffeeScript 1.6.3
(function() {
var Service;
var Helpers, Service;
Helpers = require('./Helpers');
Service = (function() {
Service.prototype.di = null;
Service.prototype.name = null;
Service.prototype.service = null;

@@ -16,2 +20,4 @@

Service.prototype.injectMethods = true;
Service.prototype.setup = null;

@@ -21,4 +27,5 @@

function Service(di, service, _arguments) {
function Service(di, name, service, _arguments) {
this.di = di;
this.name = name;
this.service = service;

@@ -37,3 +44,9 @@ this["arguments"] = _arguments != null ? _arguments : [];

Service.prototype.create = function() {
var args, method, service, _ref;
var args, e, method, names, s, service, _ref;
if (Helpers.arrayIndexOf(this.di.creating, this.name) !== -1) {
s = this.di.creating.length === 1 ? '' : 's';
names = this.di.creating.join(', ');
throw new Error("Circular reference detected for service" + s + ": " + names + ".");
}
this.di.creating.push(this.name);
service = this.service;

@@ -43,14 +56,21 @@ if (Object.prototype.toString.call(service) === '[object String]') {

}
service = this.di.createInstance(service, this["arguments"], this.instantiate);
_ref = this.setup;
for (method in _ref) {
args = _ref[method];
if (this.setup.hasOwnProperty(method)) {
if (typeof service[method] === 'function') {
service[method].apply(service, this.di.autowireArguments(service[method], args));
} else {
service[method] = args;
try {
service = this.di.createInstance(service, this["arguments"], this.instantiate, this.injectMethods);
_ref = this.setup;
for (method in _ref) {
args = _ref[method];
if (this.setup.hasOwnProperty(method)) {
if (typeof service[method] === 'function') {
service[method].apply(service, Helpers.autowireArguments(service[method], args, this.di));
} else {
service[method] = args;
}
}
}
} catch (_error) {
e = _error;
this.di.creating.splice(Helpers.arrayIndexOf(this.di.creating, this.name), 1);
throw e;
}
this.di.creating.splice(Helpers.arrayIndexOf(this.di.creating, this.name), 1);
return service;

@@ -57,0 +77,0 @@ };

{
"name": "dependency-injection",
"description": "Dependency injection with configuration and autowire for node js and browser",
"version": "1.6.7",
"version": "1.7.0",
"author": {

@@ -25,10 +25,11 @@ "name": "David Kudera",

"dependencies": {
"easy-configuration": "~1.5.6"
"easy-configuration": "~1.6.6"
},
"devDependencies": {
"chai": "~1.8.1"
"chai": "~1.8.1",
"mocha": "~1.14.0"
},
"scripts": {
"test": "cd ./test; mocha ./index.js --reporter spec;"
"test": "cd ./test; echo \"Testing in node:\"; mocha ./node/index.js --reporter spec; cd browser; echo \"Testing in browser:\"; mocha-phantomjs ./index.html"
}
}

@@ -163,2 +163,68 @@ # Dependency injection

## Examples
In your configuration, you can use three dots as replacement for services.
Services:
```
var serviceA = function(serviceB, serviceC) { ... };
var serviceB = function(serviceC, namespace, item) { ... };
var serviceC = function(namespace, item, serviceD) { ... };
var serviceD = function() { ... };
```
Configuration:
```
{
"services": {
"serviceA": {
"service": "path/to/service/A",
"instantiate": false
},
"serviceB": {
"service": "path/to/service/B",
"arguments": ["...", "some namespace", "some item"],
"instantiate": false
},
"serviceC": {
"service": "path/to/service/C",
"arguments": ["some namespace", "some item"],
"instantiate": false
},
"serviceD": {
"service": "path/to/service/D",
"instantiate": false
}
}
}
```
or more expanded:
```
{
"services": {
"serviceA": {
"service": "path/to/service/A",
"arguments": ["..."],
"instantiate": false
},
"serviceB": {
"service": "path/to/service/B",
"arguments": ["...", "some namespace", "some item"],
"instantiate": false
},
"serviceC": {
"service": "path/to/service/C",
"arguments": ["some namespace", "some item", "..."],
"instantiate": false
},
"serviceD": {
"service": "path/to/service/D",
"arguments": ["..."],
"instantiate": false
}
}
}
```
### Disable autowiring

@@ -219,2 +285,6 @@

```
di.get('di');
```
## Inject methods

@@ -282,2 +352,10 @@

* 1.7.0
+ Updated dependencies
+ Added `injectMethods` to services
+ Refactored autowiring
+ Some optimizations
+ `DI.autowireArguments` moved to `Helpers.autowireArguments`
+ Throwing an error if circular reference is found
* 1.6.6 - 1.6.7

@@ -284,0 +362,0 @@ + Bugs in Internet Explorer 8

@@ -51,3 +51,3 @@ // Generated by CoffeeScript 1.6.3

Application.prototype.withoutDefinition = function() {
Application.prototype.withoutDefinition = function(param) {
return this.other = arguments;

@@ -54,0 +54,0 @@ };

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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