Comparing version 0.4.0 to 0.5.0-beta.1
@@ -5,10 +5,12 @@ 'use strict'; | ||
var pkg = require('../package.json'); | ||
/** | ||
* When in beta | ||
* | ||
if (!process.env.R42_NOWARNING) { | ||
*/ | ||
if (pkg.version.match(/-beta\.[0-9]+/) && !process.env.R42_NOWARNING) { | ||
console.error(); | ||
console.error('---------'); | ||
console.error('r42 loaded... You installed a beta release of r42...'); | ||
console.error('There are breaking changes since 0.2.0 branch.'); | ||
console.error('There might be breaking changes since the previous branch.'); | ||
console.error('Please read the full documentation to update your code.'); | ||
@@ -23,3 +25,2 @@ console.error(); | ||
} | ||
//*/ | ||
@@ -26,0 +27,0 @@ var _ = require('lodash'); |
@@ -8,17 +8,28 @@ 'use strict'; | ||
function Module(parent, def, mm) { | ||
if (arguments.length < 3) { | ||
// Is module loaded? | ||
this.loaded = false; | ||
// Module's content | ||
this.content = {}; | ||
if (arguments.length === 1 && parent instanceof Module) { | ||
_.extend(this, _.omit(parent, ['loaded', 'content'])); | ||
return; | ||
} | ||
else if (arguments.length < 3) { | ||
mm = def; | ||
def = parent; | ||
parent = null; | ||
parent = { | ||
dirname: null, | ||
fullName: '<root>', | ||
}; | ||
} | ||
// Known parents | ||
this.parents = [ parent.fullName ]; | ||
// Known dependencies | ||
this.dependencies = []; | ||
// Module's content | ||
this.content = {}; | ||
// Module's directory | ||
this.dirname = null; | ||
// Module's coming rom NPM? | ||
// Module's coming from NPM? | ||
this.npm = false; | ||
// Default loading mode | ||
@@ -58,6 +69,23 @@ this.plugin = null; | ||
// Add dependency to the current module | ||
Module.prototype.addDependency = function(module) { | ||
Module.prototype.addDependency = function(module, mc) { | ||
if (this.dependencies.indexOf(module.fullName) < 0) { | ||
this.dependencies.push(module.fullName); | ||
} | ||
if (module.parents.indexOf(this.fullName) < 0) { | ||
module.parents.push(this.fullName); | ||
} | ||
}; | ||
// Get full dependency list | ||
Module.prototype.getAllDependencies = function(mc, alreadyBrowsed) { | ||
alreadyBrowsed = alreadyBrowsed ||[]; | ||
alreadyBrowsed.push(this.fullName); | ||
var notBrowsedDeps = _.difference(this.dependencies, alreadyBrowsed); | ||
_.each(notBrowsedDeps, function(dep) { | ||
mc.$modules[dep].getAllDependencies(mc, alreadyBrowsed); | ||
}); | ||
return alreadyBrowsed; | ||
} | ||
@@ -73,11 +101,17 @@ | ||
} | ||
} | ||
this.loaded = true; | ||
}; | ||
// Load current module | ||
Module.prototype.load = function(parent, mc) { | ||
if (this.loaded) { | ||
return; | ||
} | ||
this.loaded = true; | ||
if (!_.has(Module.pluginStore, this.plugin)) { | ||
throw new Error('[r42] Trying to use a ' + this.plugin + ' plugin that does not exists.'); | ||
} | ||
return Module.pluginStore[this.plugin](this, parent, mc); | ||
} | ||
Module.pluginStore[this.plugin](this, parent, mc); | ||
}; | ||
@@ -103,3 +137,3 @@ // fullName getter | ||
Module.pluginStore[name] = plugin; | ||
} | ||
}; | ||
@@ -154,3 +188,3 @@ // Default plugin to load a module | ||
} | ||
} | ||
}; | ||
@@ -167,3 +201,3 @@ // Require plugin to load a module using require | ||
} | ||
} | ||
}; | ||
@@ -180,3 +214,3 @@ // Require plugin to load a json module | ||
} | ||
} | ||
}; | ||
@@ -183,0 +217,0 @@ // Register default plugins |
@@ -31,2 +31,3 @@ 'use strict'; | ||
this.$config = config; | ||
this.$modules = {}; | ||
@@ -45,3 +46,3 @@ this.initModule(new Module()); | ||
if (content) { | ||
moduleDef.content = content; | ||
moduleDef.addContent(content); | ||
} | ||
@@ -63,8 +64,8 @@ }; | ||
ModuleContext.prototype.getModule = function(parent, moduleDef) { | ||
this.$modules[parent.fullName].addDependency(moduleDef); | ||
if (_.has(this.$modules, moduleDef.fullName)) { | ||
return this.$modules[moduleDef.fullName].content; | ||
moduleDef = this.$modules[moduleDef.fullName]; | ||
} | ||
this.$modules[parent.fullName].addDependency(moduleDef); | ||
moduleDef.load(parent, this); | ||
@@ -76,3 +77,3 @@ | ||
return moduleDef.content; | ||
return moduleDef; | ||
}; | ||
@@ -130,2 +131,3 @@ ModuleContext.prototype.removeModule = function(moduleDef) { | ||
dumpDeps: _.bind(this.dumpDeps, this), | ||
createSub: _.bind(this.createSub, this), | ||
} | ||
@@ -158,6 +160,20 @@ | ||
return _.map(deps, function(dep) { | ||
return this.getModule(parent, dep); | ||
return this.getModule(parent, dep).content; | ||
}, this); | ||
}; | ||
// Create a sub context for testing purposes | ||
ModuleContext.prototype.createSub = function(val, config) { | ||
if (arguments.length < 2) { | ||
config = val; | ||
val = undefined; | ||
} | ||
if (process.env.NODE_ENV !== 'test' && val !== "test") { | ||
throw new Error('[r42] You need to set NODE_ENV to "test" to use this API or to give "test" as a first argument to createSub') | ||
} | ||
var ModuleSubContext = require('./moduleSubContext'); | ||
return (new ModuleSubContext(config, this)).getWrapper(); | ||
}; | ||
module.exports = ModuleContext; | ||
@@ -207,5 +223,14 @@ | ||
var targetMC = mc; | ||
while (!_.has(mc.$modules, module) && targetMC.$parent) { | ||
targetMC = targetMC.$parent; | ||
} | ||
if (!_.has(targetMC.$modules, module)) { | ||
return '<not_found>'; | ||
} | ||
var activeMarkup = options.colors ? internals.coloredMarkup : internals.markup; | ||
var isNPM = mc.$modules[module].npm; | ||
var isNPM = targetMC.$modules[module].npm; | ||
@@ -222,3 +247,3 @@ if (isNPM) { | ||
var deps = mc.$modules[module].dependencies; | ||
var deps = targetMC.$modules[module].dependencies; | ||
_.each(deps, function(dep) { | ||
@@ -225,0 +250,0 @@ branch = activeMarkup.newBranch; |
{ | ||
"name": "r42", | ||
"version": "0.4.0", | ||
"version": "0.5.0-beta.1", | ||
"description": "Dependency injection done right.", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -225,2 +225,22 @@ ## r42 | ||
##### Testing API | ||
There is a test API that is intended to help writing tests & speed up tests by preventing a module to be reloaded more than necessary. This API is accessible when the `NODE_ENV` environment variable is set to `"test"` or by passing "test" as a first argument to the `createSub` method as demonstrated below: | ||
```js | ||
var context = /* create here as you want a r42 context with normal paths */ | ||
var subContext = context.createSub( | ||
"test", // this argument can be omitted if process.env.NODE_ENV="test" | ||
{ | ||
paths: { | ||
moduleToMockup: 'mockupModule', | ||
} | ||
} | ||
); | ||
// now subContext can be used just as context and inherits its configuration | ||
subContext.inject(/* inject as usual */); | ||
``` | ||
##### Writing your own loader plugin | ||
@@ -227,0 +247,0 @@ |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
28092
11
538
0
364
5