chaosrouter
Advanced tools
Comparing version 0.2.14 to 0.2.15
@@ -37,2 +37,45 @@ | ||
var basepath = '.'; | ||
function replaceFileRefs( struct, parents, resp ) { | ||
var is_flat = false; | ||
if(typeof struct === "string") { | ||
struct = [struct]; | ||
is_flat = true; | ||
} | ||
parents = parents || []; | ||
for( var k in struct ) { | ||
var v = struct[k]; | ||
if ( typeof v === 'object' && v !== null || Array.isArray(v) ) | ||
replaceFileRefs( v ); | ||
if ( typeof v === 'string' && v.indexOf('file:') === 0 ) { | ||
var path = basepath +'/'+ v.substr(5); | ||
if ( parents.indexOf(path) !== -1 ) | ||
return resp({ | ||
"error": "Circular File Call", | ||
"message": "The file '"+path+"' is trying to load itself." | ||
}); | ||
if(! fs.existsSync(path) ) { | ||
return resp({ | ||
"error": "Invalid File", | ||
"message": "JSON file was not found: "+ path | ||
}); | ||
} | ||
var file = fs.readFileSync( path, 'utf8' ); | ||
try { | ||
var loaded = JSON.parse(file) | ||
parents.push(path); | ||
struct[k] = replaceFileRefs( loaded, parents, resp ); | ||
} catch(err) { | ||
return resp({ | ||
"error": "Invalid File", | ||
"message": "File was not valid JSON: "+ path | ||
}); | ||
} | ||
} | ||
} | ||
return is_flat ? struct[0] : struct; | ||
} | ||
function ChaosRouter(data, opts) { | ||
@@ -45,2 +88,3 @@ if (! (this instanceof ChaosRouter)) | ||
this.defaultExec = opts.defaultExec; | ||
this.baseArgs = {} | ||
@@ -75,4 +119,8 @@ if (opts.defaultExec === undefined) | ||
if (this.configfile !== null) | ||
this.config = JSON.parse( fs.readFileSync(this.configfile) ); | ||
if (this.configfile !== null) { | ||
var config = JSON.parse( fs.readFileSync(this.configfile) ); | ||
this.config = replaceFileRefs( config, null, function(err) { | ||
throw Error(err.error+': '+err.message); | ||
}); | ||
} | ||
@@ -178,2 +226,9 @@ var variables = {}; | ||
} | ||
ChaosRouter.prototype.set_arguments = function(args) { | ||
if (!is_iterable(args)) | ||
return false; | ||
for ( var name in args ) | ||
this.baseArgs[name] = args[name]; | ||
} | ||
@@ -190,5 +245,5 @@ function Endpoint(path, config, directives, path_vars, router) { | ||
this.__methods__ = router.__methods__; | ||
this.args = { | ||
this.args = extend(extend({}, router.baseArgs), { | ||
"path": path_vars | ||
}; | ||
}); | ||
this.directives = directives; | ||
@@ -232,5 +287,15 @@ | ||
} | ||
Endpoint.prototype.method = function() { | ||
var args = Array.prototype.slice.call(arguments); | ||
var cmd = eval("this.__methods__."+args.shift()); | ||
var self = this; | ||
return new Promise(function(f,r) { | ||
cmd.call(self, args, f, function (check) { | ||
check === true ? f() : r(check); | ||
}); | ||
}); | ||
} | ||
Endpoint.prototype.respondWith = function(path, cb) { | ||
var endpoint = this.router.route(path); | ||
endpoint.execute(this.args) | ||
endpoint.execute(extend({}, this.args)) | ||
.then(function(d) { | ||
@@ -237,0 +302,0 @@ cb(d); |
{ | ||
"name": "chaosrouter", | ||
"version": "0.2.14", | ||
"version": "0.2.15", | ||
"description": "ERROR: No README data found!", | ||
@@ -5,0 +5,0 @@ "main": "chaosrouter.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20698
683
2