New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

irrelon-appcore

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

irrelon-appcore - npm Package Compare versions

Comparing version 1.0.16 to 1.0.17

LICENSE

607

index.js

@@ -20,2 +20,110 @@ /**

/**
* A class that encapsulates a module's lifecycle.
* @param {AppCore} appCore The AppCore instance that this module
* belongs to.
* @param {String} moduleName The module name.
* @constructor
*/
var AppCoreModule = function (appCore, moduleName) {
this._appCore = appCore;
this._moduleName = moduleName;
this._config = [];
this._run = [];
if (this._appCore._logLevel >= 4) { console.log('AppCore: ' + this._moduleName + ': Init...'); }
};
/**
* Config functions are checked for dependencies and run as
* soon as they are declared.
* @param definition
*/
AppCoreModule.prototype.config = function (definition, callback) {
var i;
if (definition) {
this._config.push(definition);
return this;
}
// Execute all config blocks
this._appCore._executeQueue(this._config, function (err, valueArr) {
if (callback) { callback(err, valueArr); }
});
return this;
};
/**
* Run functions are executed once the AppCore is bootstrapped.
* @param definition
*/
AppCoreModule.prototype.run = function (definition, callback) {
var i;
if (definition) {
this._run.push(definition);
return this;
}
// Execute all run blocks
this._appCore._executeQueue(this._run, function (err, valueArr) {
if (callback) { callback(err, valueArr); }
});
return this;
};
/**
* Controller functions are executed as they are requested from
* a dependency injection. If a controller has already been
* injected previously then the existing return value is returned
* unless the controller has been destroyed using module.destroy()
* in which case it is executed again and its return value stored
* against it again.
* @param definition
*/
AppCoreModule.prototype.controller = function (definition, callback) {
var self = this;
// Check if we were passed a controller function
if (definition) {
this._controller = definition;
if (this._appCore._logLevel >= 4) { console.log('AppCore: ' + this._moduleName + ': Controller defined'); }
return this;
}
// Check if we have a pre-cached controller return value
if (this._value) {
if (this._appCore._logLevel >= 4) { console.log('AppCore: ' + this._moduleName + ': Returning cached value'); }
if (callback) { callback(false, self._value); }
return this._value;
}
// Resolve dependencies, execute the controller and store the
// return value
self._appCore._getDependencies(self._moduleName, self._controller, function (err, argsArr) {
if (self._appCore._logLevel >= 4) { console.log('AppCore: ' + self._moduleName + ': All dependencies found, executing controller...'); }
self._value = self._controller.apply(self, argsArr);
if (self._appCore._logLevel >= 4) { console.log('AppCore: ' + self._moduleName + ': Controller executed'); }
if (callback) { callback(false, self._value); }
});
};
/**
* Destroys a module's cached controller return data which
* means next time the module is requested the controller will
* be re-executed.
*/
AppCoreModule.prototype.destroy = function () {
// Fire destroy event
//this.emit('destroy');
if (this._appCore._logLevel >= 4) { console.log('AppCore: ' + this._moduleName + ': Destroying controller instance'); }
delete this._value;
if (this._appCore._logLevel >= 4) { console.log('AppCore: ' + this._moduleName + ': Controller instance destroyed'); }
//this.emit('destroyed');
};
/**
* The main application class that ties all the application

@@ -32,2 +140,4 @@ * modules together and exposes the appCore to the global scope

this._moduleDefs = {};
this._config = [];
this._run = [];

@@ -38,135 +148,80 @@ // The object that holds a reference to callbacks that

// Set a log level so we only show warnings (2) and errors (1)
// level 3 and 4 are info - lots of console spamming
this._logLevel = 2;
console.log('----------------------------------------------');
console.log('| Powered By Irrelon AppCore |');
console.log('| https://github.com/irrelon/irrelon-appcore |');
console.log('----------------------------------------------');
};
/**
* Executes the passed function once all it's required dependencies
* have loaded.
* @param {Function} functionDefinition The function to execute once
* all its dependencies have been met.
* @returns {AppCore} Returns "this" to allow chaining.
* Gets / sets the logging level that AppCore will use to explain
* what it is doing. Lower levels (1 and 2) are error and warnings,
* higher levels (up to 4) are info. Setting to 4 is asking for all
* levels 4 and below.
* @param {Number=} newLevel If provided, sets the new logging level.
* @returns {*}
*/
AppCore.prototype.depends = function (functionDefinition) {
var moduleDeps,
moduleDepsArr,
depArgumentArr = [],
dependenciesSatisfied = 0,
gotDependency,
depIndex,
depTimeout = [];
if (!functionDefinition) {
throw('You must provide a function as the first argument to appCore.depends()!');
AppCore.prototype.logLevel = function (newLevel) {
if (newLevel !== undefined) {
this._logLevel = newLevel;
return this;
}
// Convert dependency list to an array
moduleDeps = this._dependencyList(functionDefinition);
moduleDepsArr = moduleDeps.arr;
return this._logLevel;
};
/**
* Gets / registers a module with the application.
* @param {String} moduleName The name of the module to define.
* @param {Function=} controllerDefinition Optional. The function to
* assign as the module's controller. If omitted we will return the module
* specified by the "name" argument if it exists.
* @returns {Function|AppCore} If "moduleDefinition" is provided, returns
* "this" to allow chaining. If "moduleDefinition" is omitted,
* returns the module specified by the "name" argument.
*/
AppCore.prototype.module = function (moduleName, controllerDefinition) {
var module;
// Check if the module has dependencies
if (!moduleDepsArr.length) {
// No dependencies were found
return this;
}
// Grab the dependencies we need - this is a really simple way
// to check we got our dependencies by how many times this function
// gets called.
gotDependency = function (dependencyName, dependency) {
var depArgumentIndex;
if (!controllerDefinition) {
module = this._modules[moduleName];
dependenciesSatisfied++;
// Check which index this dependency should be in
depArgumentIndex = moduleDepsArr.indexOf(dependencyName);
// Clear the timeout for the dependency
clearTimeout(depTimeout[depArgumentIndex]);
depTimeout[depArgumentIndex] = 0;
// Assign the dependency to the correct argument index
depArgumentArr[depArgumentIndex] = dependency;
// Check if we have all the dependencies we need
if (dependenciesSatisfied === moduleDepsArr.length) {
// We have our dependencies, load the module! YAY!
return functionDefinition.apply(functionDefinition, depArgumentArr);
if (!module) {
throw('Module with name "' + moduleName + '" not defined!');
}
};
// Register our dependency handler for each dependency
for (depIndex = 0; depIndex < moduleDepsArr.length; depIndex++) {
// Create a timeout that will cause a browser error if we are
// waiting too long for a dependency to arrive
depTimeout[depIndex] = setTimeout(this.generateDependencyTimeout(moduleDeps.func, moduleDepsArr[depIndex]), 3000);
// Now ask to wait for the module
this._waitForModule(moduleDepsArr[depIndex], gotDependency);
return this._modules[moduleName];
}
return this;
};
AppCore.prototype.sanityCheck = function () {
var i,
moduleDef,
moduleDefString,
moduleNameRegExp,
moduleDeps,
moduleNamesArr,
nameIndex,
moduleName;
this._modules[moduleName] = new AppCoreModule(this, moduleName);
// Grab all module names
moduleNamesArr = Object.keys(this._moduleDefs);
if (controllerDefinition) {
this._modules[moduleName].controller(controllerDefinition);
}
// Loop the modules
for (i in this._moduleDefs) {
if (this._moduleDefs.hasOwnProperty(i)) {
moduleDef = this._moduleDefs[i];
moduleDefString = moduleDef.toString();
// Clean definition
moduleDefString = moduleDefString
.replace(/(\/\*\*[.\s\S]*?\*\/)/g, '')
.replace(/\/\/[.\s\S]*?$/gm, '');
moduleDeps = this._dependencyList(moduleDef);
// Loop the module names array
for (nameIndex = 0; nameIndex < moduleNamesArr.length; nameIndex++) {
moduleName = moduleNamesArr[nameIndex];
moduleNameRegExp = new RegExp('\\b' + moduleName + '\\b');
if (moduleName.toLowerCase() !== i.toLowerCase() && moduleDeps.arr.indexOf(moduleName) === -1) {
// Check for module usage without dependency injection
if (moduleNameRegExp.test(moduleDefString)) {
console.warn('AppCore: Module "' + i + '" might require un-injected module "' + moduleName + '"');
}
}
}
}
}
// Now inform any waiting dependants that this module is here
this._moduleLoaded(moduleName);
// Allow chaining
return this._modules[moduleName];
};
AppCore.prototype.getModule = function (moduleName, callback) {
this._waitForModule(moduleName, callback);
return this;
};
/**
* Gets / registers a module with the application and executes the
* module's function once all it's required dependencies
* have loaded.
* @param {String} moduleName The name of the module to define.
* @param {Function=} moduleDefinition Optional. The function that
* returns the module. If omitted we will return the module
* specified by the "name" argument if it exists.
* @returns {Function|AppCore} If "moduleDefinition" is provided, returns
* "this" to allow chaining. If "moduleDefinition" is omitted,
* returns the module specified by the "name" argument.
* Scans a function definition for dependencies and waits for those
* dependencies to become available, then calls back to the waiting
* code with the controller return data for each dependency.
* @param {String} dependantName The dependant module name (the one
* waiting for the dependencies to become available).
* @param {Function} definition The function with optional arguments
* that represent dependencies to inject.
* @param {Function} callback The callback function to call once all
* the dependencies are available.
* @returns {AppCore}
* @private
*/
AppCore.prototype.module = function (moduleName, moduleDefinition) {
var self = this,
moduleDeps,
AppCore.prototype._getDependencies = function (dependantName, definition, callback) {
var moduleDeps,
moduleDepsArr,

@@ -179,18 +234,8 @@ depArgumentArr = [],

if (!moduleName) {
throw('You must name your module!');
if (!definition) {
throw('No function provided to AppCoreModule._getDependencies()!');
}
if (!moduleDefinition) {
return this._modules[moduleName];
}
if (this._modules[moduleName] !== undefined) {
throw('Cannot redefine module "' + moduleName + '" - it has already been defined!');
}
if (this._logLevel >= 4) { console.log('AppCore: ' + moduleName + ': Init...'); }
// Convert dependency list to an array
moduleDeps = this._dependencyList(moduleDefinition);
moduleDeps = this._dependencyList(definition);
moduleDepsArr = moduleDeps.arr;

@@ -200,13 +245,13 @@

if (!moduleDepsArr.length) {
// No dependencies were found, just register the module
if (this._logLevel >= 4) { console.log('AppCore: ' + moduleName + ': Has no dependencies'); }
return this._registerModule(moduleName, moduleDefinition, []);
// No dependencies were found
if (this._logLevel >= 4) { console.log('AppCore: ' + dependantName + ': Has no dependencies'); }
// We have our dependencies, send them back
callback(false, depArgumentArr);
return this;
}
if (this._logLevel >= 4) { console.log('AppCore: ' + moduleName + ': Has ' + moduleDepsArr.length + ' dependenc' + (moduleDepsArr.length > 1 ? 'ies' : 'y') + ' (' + moduleDepsArr.join(', ') + ')'); }
// Grab the dependencies we need - this is a really simple way
// to check we got our dependencies by how many times this function
// gets called. Quick and dirty - I'm writing a game of life sim
// here rather than a dependency injection lib after all.
// gets called.
gotDependency = function (dependencyName, dependency) {

@@ -217,4 +262,2 @@ var depArgumentIndex;

if (self._logLevel >= 4) { console.log('AppCore: ' + moduleName + ': Found dependency "' + dependencyName + '"'); }
// Check which index this dependency should be in

@@ -232,5 +275,4 @@ depArgumentIndex = moduleDepsArr.indexOf(dependencyName);

if (dependenciesSatisfied === moduleDepsArr.length) {
// We have our dependencies, load the module! YAY!
if (self._logLevel >= 4) { console.log('AppCore: ' + moduleName + ': Has all required dependencies, loading...'); }
return self._registerModule(moduleName, moduleDefinition, depArgumentArr);
// We have our dependencies, send them back
return callback(false, depArgumentArr);
}

@@ -240,9 +282,10 @@ };

// Register our dependency handler for each dependency
if (this._logLevel >= 4) { console.log('AppCore: ' + dependantName + ': Getting dependencies', moduleDepsArr); }
for (depIndex = 0; depIndex < moduleDepsArr.length; depIndex++) {
// Create a timeout that will cause a browser error if we are
// waiting too long for a dependency to arrive
depTimeout[depIndex] = setTimeout(this.generateDependencyTimeout(moduleName, moduleDepsArr[depIndex]), 3000);
depTimeout[depIndex] = setTimeout(this.generateDependencyTimeout(dependantName, moduleDepsArr[depIndex]), 3000);
// Now ask to wait for the module
this._waitForModule(moduleDepsArr[depIndex], gotDependency);
this._waitForModule(dependantName, moduleDepsArr[depIndex], gotDependency);
}

@@ -254,30 +297,16 @@

/**
* Generates a function that will be called by a timeout when a
* dependency does not load in the given time.
* @param {String} moduleName The name of the module that is waiting
* for a module to load.
* @param {String} dependencyName The name of the dependency module
* that we are waiting for.
* @returns {Function}
*/
AppCore.prototype.generateDependencyTimeout = function (moduleName, dependencyName) {
return function () {
if (this._logLevel >= 1) { console.error('AppCore: ' + moduleName + ': Dependency failed to load in time: ' + dependencyName); }
};
};
/**
* Reads a function's definition and finds argument dependencies.
* @param moduleDefinition
* @returns {Array} An array of dependency names.
* Gets an array of dependency names.
* @param {Function} definition The function to get dependency
* names for.
* @returns {{arr: Array, name: *}}
* @private
*/
AppCore.prototype._dependencyList = function (moduleDefinition) {
AppCore.prototype._dependencyList = function (definition) {
var moduleString,
moduleDeps,
moduleDepsArr,
moduleDepsArr = [],
moduleRegExp = /^function(.*?)\((.*?)\)/gi;
// Stringify the module function
moduleString = moduleDefinition.toString();
moduleString = definition.toString();
moduleString = moduleString

@@ -294,19 +323,16 @@ .replace(/\n/g, '')

// Check if the module has dependencies
if (!moduleDeps || !moduleDeps.length || moduleDeps[2] === "") {
// No dependencies were found
return {
arr: []
};
if (moduleDeps && moduleDeps.length) {
// Clean the function name and dependency list by removing whitespace
moduleDeps[1] = moduleDeps[1].replace(/ /gi, '');
moduleDeps[2] = moduleDeps[2].replace(/ /gi, '');
if (moduleDeps[2] !== "") {
// Convert dependency list to an array
moduleDepsArr = moduleDeps[2].split(',');
}
}
// Clean the dependency list by removing whitespace
moduleDeps[2] = moduleDeps[2].replace(/ /gi, '');
// Convert dependency list to an array
moduleDepsArr = moduleDeps[2].split(',');
return {
arr: moduleDepsArr,
func: moduleDeps[0]
name: moduleDeps[1] || moduleDeps[0]
};

@@ -316,4 +342,23 @@ };

/**
* Generates a function that will be called by a timeout when a
* dependency does not load in the given time.
* @param {String} moduleName The name of the module that is waiting
* for a module to load.
* @param {String} dependencyName The name of the dependency module
* that we are waiting for.
* @returns {Function}
*/
AppCore.prototype.generateDependencyTimeout = function (moduleName, dependencyName) {
var self = this;
return function () {
if (self._logLevel >= 1) { console.error('AppCore: ' + moduleName + ': Dependency failed to load in time: ' + dependencyName); }
};
};
/**
* Adds the passed callback function to an array that will be
* processed once the named module has loaded.
* @param {String} dependantName The name of the module waiting
* for the dependency.
* @param {String} moduleName The name of the module to wait for.

@@ -325,7 +370,15 @@ * @param {Function} callback The function to call once the

*/
AppCore.prototype._waitForModule = function (moduleName, callback) {
AppCore.prototype._waitForModule = function (dependantName, moduleName, callback) {
var self = this;
// Check if the module we are waiting for already exists
if (this._modules[moduleName] !== undefined) {
// The module is already loaded, callback now
callback(moduleName, this._modules[moduleName]);
if (this._logLevel >= 4) { console.log('AppCore: ' + dependantName + ': Dependency "' + moduleName + '" exists'); }
// The module is already loaded, ask for it
this._modules[moduleName].config();
this._modules[moduleName].controller(undefined, function (err, value) {
self._modules[moduleName].run();
if (self._logLevel >= 4) { console.log('AppCore: ' + dependantName + ': Dependency "' + moduleName + '" loaded'); }
callback(moduleName, value);
});
return this;

@@ -335,4 +388,8 @@ }

// Add the callback to the waiting list for this module
if (this._logLevel >= 4) { console.log('AppCore: ' + dependantName + ': Dependency "' + moduleName + '" does not yet exist'); }
this._waiting[moduleName] = this._waiting[moduleName] || [];
this._waiting[moduleName].push(callback);
this._waiting[moduleName].push(function (moduleName, value) {
if (self._logLevel >= 4) { console.log('AppCore: ' + dependantName + ': Dependency "' + moduleName + '" now exists'); }
callback(moduleName, value);
});

@@ -351,3 +408,4 @@ return this;

AppCore.prototype._moduleLoaded = function (moduleName) {
var waitingArr,
var self = this,
waitingArr,
waitingIndex;

@@ -364,29 +422,180 @@

// Loop the waiting array and tell the receiver that
// this module has loaded
for (waitingIndex = 0; waitingIndex < waitingArr.length; waitingIndex++) {
waitingArr[waitingIndex](moduleName, this._modules[moduleName]);
// Now get the module's controller result by executing it
if (self._logLevel >= 4) { console.log('AppCore: ' + moduleName + ': ' + waitingArr.length + ' Dependants are waiting for "' + moduleName + '" and it now exists, executing...'); }
this._modules[moduleName].config();
this._modules[moduleName].controller(undefined, function (err, value) {
self._modules[moduleName].run();
// Loop the waiting array and tell the receiver that
// this module has loaded
for (waitingIndex = 0; waitingIndex < waitingArr.length; waitingIndex++) {
waitingArr[waitingIndex](moduleName, value);
}
// Clear the waiting array for this module
delete self._waiting[moduleName];
});
};
/**
* Takes an array of functions and waits for each function's
* dependencies to be resolved and then executes the function.
* This is done in order, one at a time.
* @param {Array} arr An array of functions.
* @param {Function} callback Callback to call when complete.
* @private
*/
AppCore.prototype._executeQueue = function (arr, callback) {
var self = this,
definition,
nextItem,
valueArr,
i;
i = -1;
valueArr = [];
nextItem = function () {
var deps;
i++;
definition = arr[i];
if (!definition) {
return callback(false, valueArr);
}
deps = self._dependencyList(definition);
self._getDependencies(deps.name, definition, function (err, argsArr) {
// Execute the item function passing the dependencies
// and store the return value in the valueArr
valueArr[i] = definition.apply(self, argsArr);
// Check for more items in the array
if (i < arr.length) {
// Process the next item
return nextItem();
}
// All processing finished, callback now
return callback(false, valueArr);
});
};
// Now start the processing
nextItem();
};
/**
* Config functions are checked for dependencies and run as
* soon as they are declared.
* @param definition
*/
AppCore.prototype.config = function (definition, callback) {
var i;
if (definition) {
this._config.push(definition);
return this;
}
// Clear the waiting array for this module
delete this._waiting[moduleName];
// Execute all config blocks
this._executeQueue(this._config, function (err, valueArr) {
if (callback) { callback(err, valueArr); }
});
return this;
};
/**
* Registers a module by executing the module function and
* storing the result under the _modules object by name.
* @param {String} moduleName The name of the module to store.
* @param {Function} func The module function to execute and
* store the return value of.
* @param {Array} args The array of modules that this module
* asked for as dependencies.
* @private
* Run functions are executed once the AppCore is bootstrapped.
* @param definition
*/
AppCore.prototype._registerModule = function (moduleName, func, args) {
if (this._logLevel >= 4) { console.log('AppCore: ' + moduleName + ': Loaded'); }
this._modules[moduleName] = func.apply(func, args) || null;
this._moduleDefs[moduleName] = func;
this._moduleLoaded(moduleName);
AppCore.prototype.run = function (definition, callback) {
var i;
if (definition) {
this._run.push(definition);
return this;
}
// Execute all run blocks
this._executeQueue(this._run, function (err, valueArr) {
if (callback) { callback(err, valueArr); }
});
return this;
};
/**
* Starts the app core - this defines the entry point into
* your application by the passed function.
* @param {Function} definition The function to call to start
* the application. Will wait for all the function's dependencies
* to become available before calling it.
*/
AppCore.prototype.bootstrap = function (definition) {
var self = this,
deps;
if (self._logLevel >= 4) { console.log('AppCore: Bootstrapping...'); }
// Execute any config blocks
self.config(undefined, function () {
// Get the dependencies for the bootstrap function
deps = self._dependencyList(definition);
self._getDependencies(deps.name, definition, function (err, depArr) {
// Execute any run blocks
self.run(undefined, function () {
// Now execute the bootstrap function
if (self._logLevel >= 4) { console.log('AppCore: Bootstrap complete, executing bootstrap callback...'); }
definition.apply(definition, depArr);
});
});
});
};
AppCore.prototype.sanityCheck = function () {
var i,
moduleDef,
moduleDefString,
moduleNameRegExp,
moduleDeps,
moduleNamesArr,
nameIndex,
moduleName;
// Grab all module names
moduleNamesArr = Object.keys(this._moduleDefs);
// Loop the modules
for (i in this._moduleDefs) {
if (this._moduleDefs.hasOwnProperty(i)) {
moduleDef = this._moduleDefs[i];
moduleDefString = moduleDef.toString();
// Clean definition
moduleDefString = moduleDefString
.replace(/(\/\*\*[.\s\S]*?\*\/)/g, '')
.replace(/\/\/[.\s\S]*?$/gm, '');
moduleDeps = this._dependencyList(moduleDef);
// Loop the module names array
for (nameIndex = 0; nameIndex < moduleNamesArr.length; nameIndex++) {
moduleName = moduleNamesArr[nameIndex];
moduleNameRegExp = new RegExp('\\b' + moduleName + '\\b');
if (moduleName.toLowerCase() !== i.toLowerCase() && moduleDeps.arr.indexOf(moduleName) === -1) {
// Check for module usage without dependency injection
if (moduleNameRegExp.test(moduleDefString)) {
console.warn('AppCore: Module "' + i + '" might require un-injected module "' + moduleName + '"');
}
}
}
}
}
};
singelton = new AppCore();

@@ -393,0 +602,0 @@

{
"name": "irrelon-appcore",
"version": "1.0.16",
"version": "1.0.17",
"description": "A very lightweight application dependency manager for maintaining clean modularised code without polluting the global namespace.",

@@ -13,3 +13,3 @@ "main": "index.js",

},
"author": "Rob Evans",
"author": "Rob Evans (Irrelon Software Limited)",
"license": "MIT",

@@ -16,0 +16,0 @@ "bugs": {

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