Initial commit

This commit is contained in:
斟酌 鵬兄 2016-05-31 17:54:50 +08:00
commit 3bfe995478
3 changed files with 175 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__*

122
api.js Normal file
View File

@ -0,0 +1,122 @@
var cl = global.botanLoader;
var Dragonfly = global.Dragonfly;
var util = require( "util" );
var events = require( "events" );
var zlib = require( "zlib" );
var http = require( "http" );
var conf = cl.load( "config.botanjs" );
var Session = cl.load( "botansx.modular.session" );
var hash = cl.load( "botansx.utils.hash" );
var BotanJS = function()
{
events.EventEmitter.call( this );
this.kv = {};
};
util.inherits( BotanJS, events.EventEmitter );
BotanJS.prototype.push = function()
{
for( var i in arguments )
{
this.kv[ arguments[i] ] = true;
}
};
BotanJS.prototype.remove = function( mod )
{
delete this.kv[ mod ];
};
BotanJS.prototype.compile = function( type )
{
var _self = this;
var j = conf.debug ? [ "Components.Console" ] : [];
for( var i in this.kv )
{
if( this.kv[ i ] ) j.push( i );
}
var rainet = conf.rinet || conf.serviceUri;
if( conf.type && "or".indexOf( conf.type ) != -1 )
{
this[ type ] = rainet + conf.type + type + "/" + j.join( "/" );
this.emit( "complete" );
return;
}
var gErr = function( mesg ) {
Dragonfly.Error( mesg );
_self.emit( "complete" );
};
var q = j.join( "," );
var lookupKey = hash.md5( q );
var fastCache = new Session( lookupKey, "BOTAN_JCACHE", true );
var deflateComplete = function( err, data )
{
if( err )
{
gErr( err.message );
return;
}
var suri = conf.serviceUri + type + "/" + data.toString( "base64" )
var req = http.get(
suri
, function( res )
{
res.setEncoding( "utf8" );
var ch = "";
res.on( "data", ( x ) => ch += x );
res.on( "end", function() {
_self[ type ] = rainet + ch;
Dragonfly.Debug( "Storing " + ch + " to fastcache[ " + fastCache.id + " ]" );
fastCache.spawn( 10 );
fastCache.set( type, ch );
if( res.statusCode != 200 )
{
_self.emit( "error", "Service calling failed: " + suri, ch );
return;
}
_self.emit( "complete" );
} );
}
).on( "error", gErr );
};
fastCache.addListener( "ready", function( fCache ) {
var k = fCache.get( type );
if( k )
{
Dragonfly.Debug( "FastCache: " + k );
fCache.expire( 30 );
_self[ type ] = rainet + k;
_self.emit( "complete" );
}
else
{
zlib.deflate( q, deflateComplete );
}
} );
};
module.exports = BotanJS;

52
config.js Normal file
View File

@ -0,0 +1,52 @@
var Conf = function()
{
this.kv = {};
};
var ResolvePath = function( path, _obj )
{
var p = path.split(".");
var l = p.length - 1;
if( !l ) return { obj: _obj, key: path };
if( !_obj[ p[0] ] ) _obj[ p[0] ] = {};
var obj = _obj[ p[0] ];
for( var i = 1; i < l; i ++ )
{
if( !obj[ p[i] ] ) obj[ p[i] ] = {};
obj = obj[ p[i] ];
}
return { obj: obj, key: p[l] };
};
/**
* Example:
* Conf.set( "a.b.c.d", 7 );
* gives { a: { b: { c: { d: 7 } } } }
**/
Conf.prototype.set = function( path, val )
{
var o = ResolvePath( path, this.kv );
o.obj[ o.key ] = val;
};
Conf.prototype.push = function( path, val )
{
var o = ResolvePath( path, this.kv );
o.obj[ o.key ]
? o.obj[ o.key ].push( val )
: o.obj[ o.key ] = [ val ]
;
};
Conf.prototype.toString = function()
{
return "var _AstConf_ = " + JSON.stringify( this.kv )
+ ( global.debug ? ", debugEnv = true" : "" )
+ ";";
};
module.exports = Conf;