New class conditional stream

This commit is contained in:
斟酌 鵬兄 2015-07-23 17:15:24 +08:00
parent 07a26a2568
commit dce134676a
4 changed files with 210 additions and 52 deletions

View File

@ -1,5 +1,6 @@
var cl = global.botanLoader;
var Dragonfly = global.Dragonfly;
var CondStream = cl.load( "botanss.utils.CondStream" );
var FatalError = cl.load( "botanss.errors.FatalError" );
@ -63,14 +64,15 @@ Framework.prototype.run = function()
var method = "GET";
if( this.HTTP.request.isPost )
{
this.HTTP.request.raw.addListener( "data", function( data )
{
_self.requestStr += data.toString();
})
this.HTTP.request.raw.addListener( "end", function()
{
_self.parseResult();
});
_self.requestStr = new CondStream( "/tmp/", 2048 );
this.HTTP.request.raw.addListener(
"data" , ( x ) => _self.requestStr.write( x )
);
this.HTTP.request.raw.addListener(
"end", () => _self.requestStr.end( () => _self.parseResult() )
);
method = "POST";
}

View File

@ -1,48 +1,16 @@
var encodeCookie = function( cookie )
{
var cookieStr = "";
var p = "";
var e = "";
for( var i in cookie )
{
switch( i.toLowerCase() )
{
case "path":
p = cookie[i];
continue;
case "expires":
e = cookie[i];
continue;
}
cookieStr += i + "=" + cookie[i] + ";";
}
var cl = global.botanLoader;
// Path at tail
cookieStr += "Path=" + p + ";" + " Expires=" + e + ";";
return cookieStr;
};
var util = require( "util" );
var WebParam = cl.load( "botanss.utils.WebParam" );
var Cookie = function( cookieStr, HTTP )
{
var list = {};
cookieStr && cookieStr.split( ";" ).forEach( function( cookie )
{
var parts = cookie.split( "=" );
list[ parts.shift().trim() ] = decodeURI( parts.join( "=" ) );
} );
this.__cookie = list;
WebParam.call( this, cookieStr );
this.HTTP = HTTP;
};
Cookie.prototype.set = function( name, value )
{
this.__cookie[ name ] = value;
};
util.inherits( Cookie, WebParam );
Cookie.prototype.seth = function( name, value )
{
@ -50,15 +18,26 @@ Cookie.prototype.seth = function( name, value )
this.HTTP.response.headers[ "Set-Cookie" ] = this.toString();
};
Cookie.prototype.get = function( name )
{
return this.__cookie[ name ];
};
Cookie.prototype.toString = function()
{
return encodeCookie( this.__cookie );
var cookieStr = "";
var p = "";
var e = "";
for( var i in this.param )
{
switch( i.toLowerCase() )
{
case "path":
p = this.param[i];
continue;
case "expires":
e = this.param[i];
continue;
}
cookieStr += i + "=" + this.param[i] + ";";
}
cookieStr += "Path=" + p + ";" + " Expires=" + e + ";";
return cookieStr;
};
module.exports = Cookie;

130
utils/CondStream.js Normal file
View File

@ -0,0 +1,130 @@
var fs = require( "fs" );
var crypto = require( "crypto" );
var util = require( "util" );
var ReadStream = require( "stream" ).Readable;
var ConditionalStream = function( tmpPath, triggerLimit )
{
if( !tmpPath )
{
throw new Error( "Temp path is not defined" );
}
this.size = 0;
this.limit = triggerLimit * 1024;
this.stream = false;
this.hexData = "";
this.tmpPath = tmpPath;
this.file = false;
this.__discard = false;
this.__ended = false;
this.__finished = false;
};
util.inherits( ConditionalStream, String );
ConditionalStream.prototype.write = function( data )
{
var _self = this;
this.size += data.length;
if( this.stream )
{
this.hexData = false;
this.stream.write( data );
return;
}
this.hexData += data.toString( "hex" );
// Trigger
if( this.limit < this.size )
{
this.file = this.tmpPath + "ss_" + crypto.randomBytes( 8 ).toString( "hex" );
this.stream = fs.createWriteStream( this.file );
this.stream.addListener( "finish", this.__end.bind( this ) );
this.stream.write( this.hexData, "hex" );
}
};
ConditionalStream.prototype.end = function( handler )
{
var _self = this;
if( this.stream )
{
this.stream.addListener( "close", function() {
_self.__finished = true;
handler( _self );
} );
this.stream.end();
}
else
{
setTimeout( function()
{
_self.__finished = true;
handler( _self )
} , 0 );
}
};
ConditionalStream.prototype.discard = function()
{
var _self = this;
this.__discard = true;
if( this.__finished )
{
fs.unlink( this.file, function()
{
if( _self.__error ) throw new Error( _self.__error );
} );
}
};
ConditionalStream.prototype.__end = function()
{
this.__finished = true;
if( this.__discard ) this.discard();
};
ConditionalStream.prototype.toString = function( enc )
{
if( this.stream )
{
this.discard();
this.__error = "Received data is too large to process";
}
return new Buffer( this.hexData, "hex" ).toString( enc );
};
ConditionalStream.prototype.resultStream = function()
{
var _self = this;
if( !this.__finished ) throw new Error( "Data is not finished yet" );
if( this.__discard ) throw new Error( "Data is discarded" );
if( this.stream )
{
var rt = fs.createReadStream( this.file );
rt.addListener( "close", () => _self.discard() );
return rt;
}
var st = new ReadStream();
setTimeout( function() {
st.push( this.hexData, "hex" );
st.push( null );
}, 0 );
return st;
};
module.exports = ConditionalStream;

47
utils/WebParam.js Normal file
View File

@ -0,0 +1,47 @@
var WebParam = function( paramStr )
{
var list = {};
paramStr && paramStr.split( ";" ).forEach( function( param )
{
var parts = param.split( "=" );
list[ parts.shift().trim() ] = decodeURI( parts.join( "=" ) );
} );
this.param = list;
};
WebParam.prototype.set = function( name, value )
{
this.param[ name ] = value;
};
WebParam.prototype.get = function( name )
{
return this.param[ name ];
};
WebParam.prototype.toString = function()
{
var paramStr = "";
for( var i in param )
{
paramStr += i + "=" + param[i] + ";";
}
return paramStr;
};
WebParam.ExtractHeader = function( hstr )
{
var headers = {};
hstr.split( "\r\n" ).forEach( function( v )
{
if( !v ) return;
var colx = v.indexOf( ':' );
headers[ v.substring( 0, colx ) ] = new WebParam( v.substr( colx + 1 ) );
} );
return headers;
};
module.exports = WebParam;