cpuabuse-system v0.6.0
Files & Data
- Default file extension is added, if missing
- For empty value in key/value pair, value equal to key is assumed
- In absent key/value child pairs, default values for missing keys are assumed
JSDoc - Member Declaration
Type | Selector | Declaration |
---|---|---|
static | . | Default |
inner | ~ | @inner @memberof module:myModule~myMember |
instance | # | @instance |
Structure
system
System is intended more than anything, for centralized managment.
- system
- static
- .System ⇐ Loader
- new System(options, [behaviors])
- instance
- static
- inner
- ~options : Object
- ~filterContext : Object
- ~behavior : Object
- .AtomicLock
- new AtomicLock()
- .locked : boolean ℗
- .lock() ⇒ Promise
- .release()
- .System ⇐ Loader
- inner
- ~Loader
- new Loader(rootDir, relativeInitDir, initFilename, callback)
- static
- .getFile(rootDir, relativeDir, file) ⇒ external.Promise
- .toRelative(dir, target) ⇒ string | Array.<string>
- .join(rootDir, target) ⇒ string | Array.<string>
- .isFile(rootDir, relativeDir, filename) ⇒ boolean
- .isDir(rootDir, relativeDir) ⇒ boolean
- .list(rootDir, relativeDir) ⇒ Promise
- .yamlToObject(string) ⇒ Object
- inner
- ~SystemError ⇐ Error
- new SystemError(code, message)
- instance
- .code : string ℗
- static
- .isSystemError(error) ⇒ boolean
- ~Behavior ⇐ EventEmitter
- new Behavior()
- .atomicLock : AtomicLock ℗
- .behaviorId : Object ℗
- .behaviorIndex : Array.<string> ℗
- .nextBehaviorCounter : number ℗
- .addBehavior(name, callback) ⇒ number
- .behave(name)
- ~test
- ~Loader
- static
system.System ⇐ Loader
Provides wide range of functionality for file loading and event exchange. Throws standard error if failed to perform basic initializations, or system failure that cannot be reported otherwise has occured.
Kind: static class of system
Extends: Loader
Emits: systemLoad
- .System ⇐ Loader
- new System(options, [behaviors])
- instance
- static
- inner
- ~options : Object
- ~filterContext : Object
- ~behavior : Object
new System(options, behaviors)
The constructor will perform necessary preparations, so that failures can be processed with system events. Up until these preparations are complete, the failure will result in thrown standard Error.
Throws:
- Error -
loader_failed
- Loader did not construct the mandatory properties
Param | Type | Description |
---|---|---|
options | options | System options. |
behaviors | Array.<behavior> | Optional Behaviors to add. |
system.events : Object
Events to be populated by the loader. System by itself does not deal with events, it only confirms that the events were initialized. Although, if the events are fired, and failure to fire event is set to throw, or undocumented events encountered, it would throw errors.
Kind: instance abstract property of System
- .events : Object
"errorExists"
Error could not be added, because error with same code already exists.
Kind: event emitted by events
Default: error_exists
"systemLoad"
System load event. Fires at the end of system load, so it is safe to execute code in the then() directive of behavior associated.
Kind: event emitted by events
Default: system_load
"behaviorAttach"
Behavior has been attached.
Kind: event emitted by events
Default: behavior_attach
"behaviorAttachFail"
Behavior could not be attached.
Kind: event emitted by events
Default: behavior_attach_fail
"behaviorAttachRequestFail"
Incorrect behavior attach request.
Kind: event emitted by events
Default: behavior_attach_request_fail
"typeError"
Type error has been encountered.
Kind: event emitted by events
Default: type_error
"eventFail"
Failed to fire an event.
Kind: event emitted by events
Default: event_fail
system.behaviors : Object
Behavior describtions initialized by loader.
Kind: instance abstract property of System
system.system : options
Contains system info.
Kind: instance property of System
Read only: true
Properties
Name | Type | Description |
---|---|---|
behavior | Behavior | Event emitter for the behaviors. Generally should use the public system instance methods instead. |
system.behavior : Behavior
Actual behaviors are located here.
Kind: static property of system
system.error : Object
Actual errors are located here.
Kind: static abstract property of system
system.file : Object
File system methods.
Kind: static property of system
- .file : Object
file.filter : Object
File level filters.
Kind: static property of file
filter.isFile(filterContext) ⇒ Promise
Check if argument is a file (relative to system root directory).
Kind: static method of filter
Returns: Promise - Promise, containing boolean result.
Param | Type | Description |
---|---|---|
filterContext | filterContext | Information on the item to be filtered. |
filter.isDir(filterContext) ⇒ Promise
Check if argument is a folder (relative to system root directory).
Kind: static method of filter
Returns: Promise - Promise, containing boolean result.
Param | Type | Description |
---|---|---|
filterContext | filterContext | Information on the item to be filtered |
file.toRelative(rootDir, target) ⇒ Promise
Converts absolute path to relative path.
Kind: static method of file
Returns: Promise - Promise, containing string relative path.
Param | Type | Description |
---|---|---|
rootDir | string | Relative directory. |
target | string | Absolute file/folder path. |
file.join(rootDir, target) ⇒ Promise
Joins two paths.
Kind: static method of file
Returns: Promise - Promise, containing string path.
Param | Type | Description |
---|---|---|
rootDir | string | Relative directory. |
target | string | File/folder path to rootDir. |
file.getFile(dir, file) ⇒ Promise
Get file contents relative to system root directory.
Kind: static method of file
Returns: Promise - Promise, containing string with file contents..
Param | Type | Description |
---|---|---|
dir | string | Directory, relative to system root. |
file | string | Filename. |
file.getYaml(dir, file) ⇒ Promise
Get contents of yaml file relative to system root directory.
Kind: static method of file
Returns: Promise - Promise, containing string with file contents..
Param | Type | Description |
---|---|---|
dir | string | Directory, relative to system root. |
file | string | Filename. |
file.list(dir, file) ⇒ Promise
List the contents of the folder, relative to system root directory.
Kind: static method of file
Returns: Promise - Promise, containing an array of filtered strings - files/folders relative to system root.
Param | Type | Description |
---|---|---|
dir | string | Folder relative to system root. |
file | string | Filename. |
Example (List folders)
systemInstance.system.file.list("css", systemInstance.system.file.filter.isDir);
system.addError(code, message)
Adds an error to the System dynamically
Kind: instance method of System
Emits: errorExists
Param | Type | Description |
---|---|---|
code | string | Error code |
message | string | Error description |
Example (Usage)
code = "no_beakers"
message = "Beakers out of stock."
var options = {
id: "lab_inventory",
rootDir: "labs",
relativeInitDir: "black_mesa",
initFilename: "inventory.yml",
notMute: true
};
var labInventory = new System(options);
labInventory.addError(code, message);
system.addBehaviors(behaviors)
Adds behaviors to the system, and fires post-addtion events. Firstly, this function attempts to add the behaviors. When the behavior addition has been processed, the function will attempt to fire post-addition events, depending on success/failure of behavior additions.
Kind: instance method of System
Emits: behaviorAttach, behaviorAttachFail, behaviorAttachRequestFail
Param | Type |
---|---|
behaviors | Array.<behavior> |
Example (Usage)
var options = {
id: "lab_inventory",
rootDir: "labs",
relativeInitDir: "black_mesa",
initFilename: "inventory.yml",
notMute: true
};
var behavior = {
"check_inventory": () => {
// Behavior functionality
// ...
}
}
var labInventory = new System(options);
labInventory.addBehaviors([behavior]).then(function(){
console.log("Behavior added.");
});
system.log(text)
Log message from the System context
Kind: instance method of System
Emits: module:system.System~event:type_error
Param | Type | Description |
---|---|---|
text | string | Message |
Example (Usage)
var options = {
id: "lab_inventory",
rootDir: "labs",
relativeInitDir: "black_mesa",
initFilename: "inventory.yml",
notMute: true
};
var text = "Lab Inventory working.";
var labInventory = new System(options);
labInventory.log(text);
system.error(text)
Log an error message from the System context
Kind: instance method of System
Emits: module:system.System~event:type_error
Param | Type | Description |
---|---|---|
text | string | Message |
Example (Usage)
var options = {
id: "lab_inventory",
rootDir: "labs",
relativeInitDir: "black_mesa",
initFilename: "inventory.yml",
notMute: true
};
var text = "Testing Lab Inventory error log.";
var labInventory = new System(options);
labInventory.error(text);
system.fire(name, message)
Fires a system event
Kind: instance method of System
Throws:
Emits: eventFail
Param | Type | Description |
---|---|---|
name | string | Event name, as specified in events. |
message | string | Optional Message is not strictly required, but preferred. If not specified, will assume value of the name |
Example (Usage)
var options = {
id: "lab_inventory",
rootDir: "labs",
relativeInitDir: "black_mesa",
initFilename: "inventory.yml",
notMute: true
};
var labInventory = new System(options);
labInventory.fire("system_load_aux", "Auxiliary system loaded.");
system.behave(event)
Emit an event as a behavior.
Kind: instance method of System
Param | Type | Description |
---|---|---|
event | string | Behavior name. |
Example (Usage)
// From the lab inventory system context
{
// ...
this.behave("system_load_aux");
// ...
}
system.on(event, callback)
Adds a behavior bound to "this".
Kind: instance method of System
Param | Type | Description |
---|---|---|
event | string | Behavior name. |
callback | function | Behavior. |
Example (Usage)
var options = {
id: "lab_inventory",
rootDir: "labs",
relativeInitDir: "black_mesa",
initFilename: "inventory.yml",
notMute: true
};
var labInventory = new System(options);
labInventory.on("system_load_aux", function(that){
console.log("Auxiliary system loaded - " + that.system.id);
});
System.checkOptionsFailure(options) ⇒ boolean
Checks options argument for missing incorrect property types
Kind: static method of System
Returns: boolean - Returns true if the arguments is corrupt; false if OK
Param | Type | Description |
---|---|---|
options | module:system~System~options | System options argument |
Example (Usage)
var options = {
id: "stars",
rootDir: "test",
relativeInitDir: "stars",
initFilename: "stars.yml",
notMute: true
};
if (System.checkOptionsFailure(options)){
throw new Error ("Options inconsistent.");
}
System.error(text)
Access stderr
Kind: static method of System
Param | Type |
---|---|
text | string |
Example (Usage)
system.System.error("Not enough resources.");
System.log(text)
Access stdout
Kind: static method of System
Param | Type |
---|---|
text | string |
Example (Usage)
system.System.log("Resources loaded.");
System~options : Object
System options
Kind: inner typedef of System
Properties
Name | Type | Description |
---|---|---|
id | string | System instace internal ID. |
rootDir | string | The root directory for the System instance. |
relativeInitDir | string | The relative directory to root of the location of the initialization file. |
initFilename | string | Initialization file filename. |
notMute | boolean | Whether the system logs or not. |
System~filterContext : Object
System options
Kind: inner typedef of System
Properties
Name | Type | Description |
---|---|---|
dir | string | Parent directory of the filtered item |
itemName | string | Name of the filtered item |
item | string | Path to the filtered item |
System~behavior : Object
System behavior - an object, with a property where key is the name of the behavior, and value is the function, taking a system context as an argument.
Kind: inner typedef of System
Properties
Type |
---|
function |
Example (Behavior - argument outline)
amazing_behavior: (that) => {
// Process system instance on "amazing_behavior"
amazingProcessor(that);
}
system.AtomicLock
Creates an instance of AtomicLock. It is not intended to be actually used for parallel processing, and mutual exlusion. It is intended for abstraction of atomic logic more than anything.
Note: To be eventually split into a primitive lock to objectify the functionality of a lock, and a lock with timout and a queue.
Kind: static class of system
- .AtomicLock
- new AtomicLock()
- .locked : boolean ℗
- .lock() ⇒ Promise
- .release()
new AtomicLock()
Creates an instance of AtomicLock. Does not take any arguments or return any values.
atomicLock.locked : boolean ℗
Indicates the locked/unlocked state.
Kind: instance property of AtomicLock
Access: private
atomicLock.lock() ⇒ Promise
Lock an atomic lock.
Kind: instance method of AtomicLock
Returns: Promise - Resolves when lock succeeds
Example (Usage)
// Lock
exampleAtomicLock.lock();
atomicLock.release()
Release atomic lock
Kind: instance method of AtomicLock
Example (Usage)
// Release
exampleAtomicLock.release();
system~Loader
Required by system to perform file initialization.
Kind: inner class of system
- ~Loader
- new Loader(rootDir, relativeInitDir, initFilename, callback)
- static
- .getFile(rootDir, relativeDir, file) ⇒ external.Promise
- .toRelative(dir, target) ⇒ string | Array.<string>
- .join(rootDir, target) ⇒ string | Array.<string>
- .isFile(rootDir, relativeDir, filename) ⇒ boolean
- .isDir(rootDir, relativeDir) ⇒ boolean
- .list(rootDir, relativeDir) ⇒ Promise
- .yamlToObject(string) ⇒ Object
- inner
new Loader(rootDir, relativeInitDir, initFilename, callback)
Param | Type | Description |
---|---|---|
rootDir | string | Absolute root directory. |
relativeInitDir | string | Relative path to root. |
initFilename | string | Filename. |
callback | function | Callback to call with Promise of completion. |
Loader.getFile(rootDir, relativeDir, file) ⇒ external.Promise
Gets file contents.
Kind: static method of Loader
Returns: external.Promise - File contents.
Param | Type | Description |
---|---|---|
rootDir | string | Absolute root directory. |
relativeDir | string | Directory relative to root. |
file | string | Full file name. |
Example (Usage)
// Load files
var grapefruitJuicer = Loader.getFile("c:\machines", "appliances", "grapefruitJuicer.txt");
// Output the result
grapefruitJuicer.then(function(result){ // grapefruitJuicer - on resolve
console.log(result);
}, function(error){ // grapefruitJuicer - on reject
console.error("Could not load a file.");
});
// Input - grapefruitJuicer.txt
// 1000W powerful juicer
// Output
// 1000W powerful juicer
Loader.toRelative(dir, target) ⇒ string | Array.<string>
Extracts relative path from rootDir to target.
Kind: static method of Loader
Returns: string | Array.<string> - Relative path|paths.
Param | Type | Description | |
---|---|---|---|
dir | string | Source folder. | |
target | string | Array.<string> | File/folder name | names. |
Example (Usage)
// Convert path and output the result
console.log(Loader.toRelative("c:\machines\refrigerators", "c:\machines\appliances"));
// Output
// ..\appliances
Loader.join(rootDir, target) ⇒ string | Array.<string>
Join a root directory with a file/folder or an array of files/folders to absolute path.
Kind: static method of Loader
Returns: string | Array.<string> - Absolute path|paths.
Param | Type | Description | |
---|---|---|---|
rootDir | string | Root folder. | |
target | string | Array.<string> | File/folder name | names. |
Example (Usage)
// Join and log result
console.log(Loader.join("c:\machines", "appliances"))
// Output
// c:\machines\appliances
Loader.isFile(rootDir, relativeDir, filename) ⇒ boolean
Checks if is a file
Kind: static method of Loader
Returns: boolean - Returns true
if a file, false
if not.
Param | Type | Description |
---|---|---|
rootDir | string | Absolute root directory. |
relativeDir | string | Relative directory to root. |
filename | string | Full filename. |
Example (Usage)
// Verify file
Loader.isFile("c:\machines","appliances","grapefruitJuicer.txt").then(function(result){
console.log(result);
});
// Input - grapefruitJuicer.txt
// 1000W powerful juicer
// Output
// true
Loader.isDir(rootDir, relativeDir) ⇒ boolean
Checks if is a directory.
Kind: static method of Loader
Returns: boolean - Returns true
if a directory, false
if not.
Param | Type | Description |
---|---|---|
rootDir | string | Absolute root directory. |
relativeDir | string | Relative directory to root. |
Example (Usage)
// Verify directory
Loader.isDir("c:\machines\appliances","grapefruitJuicer.txt").then(function(result){
console.log(result);
});
// Input - grapefruitJuicer.txt
// 1000W powerful juicer
// Output
// false
Loader.list(rootDir, relativeDir) ⇒ Promise
Returns an array of strings, representing the contents of a folder.
Kind: static method of Loader
Returns: Promise - Array with contents; Rejects with errors from fs.readdir.
Param | Type | Description |
---|---|---|
rootDir | string | Root directory. |
relativeDir | string | Relative directory. |
Example (Usage)
// List directory contents
Loader.list("c:","machines").then(function(result){
console.log(result);
}, function(error){
console.error("Folder not found.");
});
// Output
// ["machines", "appliances"]
Loader.yamlToObject(string) ⇒ Object
Converts YAML string to a JS object.
Kind: static method of Loader
Returns: Object - Javascript object.
Param | Type | Description |
---|---|---|
string | string | YAML string. |
Example (Usage)
// Ouput conversion of YAML to JSON
console.log(Loader.yamlToObject("Wine: Red"));
// Output
// {"Wine": "Red"}
Loader~initRecursion(rootDir, relativePath, initFilename, targetObject, extend) ⇒ Promise
System loader recursion.
Note:
- Default values are assumed for unspecified or empty values.
- Extension means recursive loading of data into variable, as if loading a new file into the current variable as new system.
- Relative path is relative to the directory location of current file.
Kind: inner method of Loader
Throws:
- Will throw an error if the directive is not an allowed one (folder, file, path, extend).
Param | Type | Description |
---|---|---|
rootDir | string | Root directory. |
relativePath | Object | Relative path. |
initFilename | string | Filename for settings. |
targetObject | Object | Object to be filled. |
extend | boolean | Extend the children objects or not. |
Example (Default filename - null)
# Variable settings to be populated with data from "system_root_dir/settings.yml"
settings: # Defaults to "settings"
Example (Default filename - empty string)
# Variable settings to be populated with data from "system_root_dir/settings.yml"
settings: "" # Defaults to "settings"
Example (Specified filename)
# Variable settings to be populated with data from "system_root_dir/xxx.yml"
settings: "xxx"
Example (Default values)
# Variable settings to be populated with data from "system_root_dir/settings.yml"
settings:
folder: # Defaults to "./"
file: # Defaults to "settings"
path: # Defaults to "absolute"
extend: # Defaults to "false"
Example (Specified values)
# Variable settings to be populated with data from "current_dir/hello/xxx.yml"
settings:
folder: "hello"
file: xxx
path: relative
extend: false
Example (Extension)
# Variable settings to be populated **recursively** with data from "current_dir/hello/xxx.yml"
settings:
folder: "hello"
file: xxx
path: relative
extend: true
Example (Usage)
// Input - ./settings/init.yml
// settings:
// path: relative
// Input - ./settings/settings.yml
// apples: red
// bananas: yellow
// Target object to fill
var targetObject = {};
initRecursion("./", "settings", "init.yml", targetObject, true));
Loader~initSettings(rootDir, initPath, filename) ⇒ Object
Init and populate globalspace with settings - specific global object member per file. Semantically this function has broader purpose than loadYaml.
Kind: inner method of Loader
Returns: Object - Javascript object with settings.
Param | Type | Description |
---|---|---|
rootDir | string | Root directory. |
initPath | string | Relative directory to root. |
filename | string | Filename. |
Example (Usage)
var settings = await initSettings("./", "settings", "settings");
Loader~loadYaml(rootDir, relativeDir, filename) ⇒ Promise
Parses YAML file, and returns and object; Adds extension if absent.
Kind: inner method of Loader
Returns: Promise - Javascript object.
Param | Type | Description |
---|---|---|
rootDir | string | Absolute directory path. |
relativeDir | string | Relative directory to root. |
filename | string | Filename, with or without extension. |
Example (Usage)
var settings = await loadYaml("./", "settings", "settings");
system~SystemError ⇐ Error
Extended system error class.
Kind: inner class of system
Extends: Error
- ~SystemError ⇐ Error
- new SystemError(code, message)
- instance
- .code : string ℗
- static
- .isSystemError(error) ⇒ boolean
new SystemError(code, message)
Creates an instance of SystemError.
Param | Type | Description |
---|---|---|
code | string | Error code |
message | string | Error message |
systemError.code : string ℗
Error code.
Kind: instance property of SystemError
Access: private
SystemError.isSystemError(error) ⇒ boolean
Check if an object is indeed a functional SystemError.
Note:
- Not checking for presence of code property, or for it being a string, as assuming that the object of SystemError type would have it initialized.
- Empty code errors will return false, due to the ambiguity.
Kind: static method of SystemError
Returns: boolean - Returns true
if is is a SystemError, false
if not.
Param | Type | Description |
---|---|---|
error | module:system.SystemError | Error to check |
Example (Usage)
// Try to load JSON
try{
loadJson();
} catch(error) {
if (SystemError.isSystemError(error)){
// If error is something that we have defined, throw a more generic error
throw new SystemError("json_load_fail", "Failed to load JSON file.");
} else {
// Rethrow the original error
throw error;
}
}
system~Behavior ⇐ EventEmitter
System behavior class
Kind: inner class of system
Extends: EventEmitter
- ~Behavior ⇐ EventEmitter
- new Behavior()
- .atomicLock : AtomicLock ℗
- .behaviorId : Object ℗
- .behaviorIndex : Array.<string> ℗
- .nextBehaviorCounter : number ℗
- .addBehavior(name, callback) ⇒ number
- .behave(name)
new Behavior()
Initializes system behavior
behavior.atomicLock : AtomicLock ℗
Atomic lock to perform counter increments
Kind: instance property of Behavior
Access: private
behavior.behaviorId : Object ℗
IDs to use as actual event identifiers
Kind: instance property of Behavior
Access: private
behavior.behaviorIndex : Array.<string> ℗
Index to link id's back to behavior names
Kind: instance property of Behavior
Access: private
behavior.nextBehaviorCounter : number ℗
Counter to use to generate IDs
Kind: instance property of Behavior
Access: private
behavior.addBehavior(name, callback) ⇒ number
Adds a behavior to the behavior class instance.
Note:
Does not check for inconsistencies within ID and index arrays, as if it is internally managed by this class, inconsistencies should not happen.
Kind: instance method of Behavior
Returns: number - ID of the behavior; -1
if creation failed
Param | Type | Description |
---|---|---|
name | string | Name of the bahavior |
callback | function | Behavior callback function |
Example (Usage)
// Create a new instance of Behavior
var behavior = new Behavior();
// Add a behavior
behavior.addBehavior("hello_behavior", () => console.log("Hello World"));
behavior.behave(name)
Triggers behaviors registered for name
Kind: instance method of Behavior
Param | Type | Description |
---|---|---|
name | string | Behavior name |
Example (Usage)
// Create a new instance of Behavior
var behavior = new Behavior();
// Add a behavior
behavior.addBehavior("hello_behavior", () => console.log("Hello World"));
// Call a behavior
behavior.behave("hello_behavior");
// Output:
// "Hello World"
system~test
Series of tests for the system.
Kind: inner property of system
test.Loader
Tests for the Loader class.
Kind: static property of test
Loader.toRelative
Tests the toRelative function.
Kind: static property of Loader
Loader.join
Tests the join function.
Kind: static property of Loader
Loader.isFile
Tests the isFile function.
Kind: static property of Loader
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago