modularity
Advanced tools
+41
| {join} = require 'path' | ||
| class Modularity | ||
| constructor: (@paths, @cache, exclude_builtin) -> | ||
| @paths or= [] | ||
| @cache or= {} | ||
| @paths.push '' unless exclude_builtin | ||
| load: (callback) -> | ||
| module = new Module '(root)', callback, @paths, @cache | ||
| module.load (err) -> | ||
| # TODO | ||
| class Module | ||
| constructor: (@name, module, @paths, @cache, @ancestors) -> | ||
| @ancestors or= [] | ||
| @dependencies @parameters_of module | ||
| load: (callback) -> | ||
| # TODO | ||
| parameters_of: (fn) -> fn.toString() | ||
| .match(/function [^\(]*\(([^\)]*)\)/)[1] | ||
| .split(/[\r\t\n ]*,[\r\t\n ]* /) | ||
| for_each: (array, iterator, callback) -> | ||
| remaining = array.length | ||
| return callback() unless remaining | ||
| position = 1 | ||
| do next = -> | ||
| iterator array[position++], (err) -> | ||
| return callback err if err or not --remaining | ||
| process.nextTick next | ||
| modules = new Modularity [ join __dirname + 'lib' ] | ||
| modules.load (foo, bar) -> | ||
| module.exports = function (foo_bar) { | ||
| return foo_bar; | ||
| }; | ||
| module.exports = function () { | ||
| return 'bar'; | ||
| }; | ||
+20
-5
@@ -1,2 +0,4 @@ | ||
| var join = require('path').join | ||
| var path = require('path') | ||
| , join = path.join | ||
| , sep = path.sep | ||
| , modularity = exports; | ||
@@ -7,4 +9,3 @@ | ||
| modularity.load = function (/* paths, */ callback) { | ||
| var paths = Array.prototype.slice.call(arguments) | ||
| , components = {}; | ||
| var paths = Array.prototype.slice.call(arguments); | ||
| callback = paths.pop(); | ||
@@ -52,3 +53,3 @@ var dependencies = modularity.args(callback) | ||
| return next(new Error('Circular dependency for "' + dependency + | ||
| '" found in module "' + parent +'"')); | ||
| '" found in module "' + parent + '"')); | ||
| } | ||
@@ -95,3 +96,3 @@ modularity.require(parent, dependency, paths, function (err, module, path) { | ||
| for (var path, module, i = 0, len = paths.length; i < len; i++) { | ||
| path = join(paths[i], dependency); | ||
| path = join(paths[i] || '', dependency); | ||
| try { | ||
@@ -108,2 +109,16 @@ module = require(path); | ||
| } | ||
| if (dependency.indexOf('_') !== -1) { | ||
| path = join(paths[i] || '', dependency.replace(/_/g, sep)); | ||
| try { | ||
| module = require(path); | ||
| return callback(null, module, path); | ||
| } catch (e) { | ||
| if (typeof e !== 'object' || !e.message || | ||
| e.code !== 'MODULE_NOT_FOUND' || | ||
| e.message.indexOf(path) === -1) { | ||
| return callback(e); | ||
| } | ||
| attempts.push(path); | ||
| } | ||
| } | ||
| } | ||
@@ -110,0 +125,0 @@ paths = attempts.map(function (path) { |
+1
-1
| { | ||
| "name": "modularity", | ||
| "version": "0.1.2", | ||
| "version": "0.2.0", | ||
| "description": "Module loader", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+8
-11
@@ -32,13 +32,2 @@ var modularity = require('../') | ||
| it('should throw an error if the callback isn\'t expecting err', function (done) { | ||
| var originalException = process.listeners('uncaughtException').pop(); | ||
| process.once("uncaughtException", function (error) { | ||
| process.listeners('uncaughtException').push(originalException); | ||
| done(); | ||
| }); | ||
| loadTest(2, function (foo) { | ||
| //Unreachable | ||
| }); | ||
| }); | ||
| it('should look in multiple directories to resolve deps', function (done) { | ||
@@ -116,3 +105,11 @@ modularity.load( | ||
| it('should load dependencies from subdirectories', function (done) { | ||
| loadTest(7, function (err, foo) { | ||
| assert(!err, err); | ||
| assert.equal(foo, 'bar'); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
14609
12.16%23
15%283
6.79%5
25%