CondStream to es6 class

This commit is contained in:
斟酌 鵬兄 2016-02-19 23:44:03 +08:00
parent f3ba7a4a2d
commit 041b3fa508

View File

@ -1,12 +1,20 @@
"use strict";
var Dragonfly = global.Dragonfly; var Dragonfly = global.Dragonfly;
var fs = require( "fs" ); var fs = require( "fs" );
var path = require( "path" ); var path = require( "path" );
var crypto = require( "crypto" ); var crypto = require( "crypto" );
var util = require( "util" );
var ReadStream = require( "stream" ).Readable; var ReadStream = require( "stream" ).Readable;
var ConditionalStream = function( tmpPath, triggerLimit ) class ConditionalStream extends String
{ {
constructor( tmpPath, triggerLimit )
{
super();
// XXX: Dirty fix for incompat on node js v5
Object.setPrototypeOf( this, new.target.prototype );
if( !tmpPath ) if( !tmpPath )
{ {
throw new Error( "Temp path is not defined" ); throw new Error( "Temp path is not defined" );
@ -24,13 +32,10 @@ var ConditionalStream = function( tmpPath, triggerLimit )
this.__ended = false; this.__ended = false;
this.__finished = false; this.__finished = false;
}; }
util.inherits( ConditionalStream, String ); write( data )
{
ConditionalStream.prototype.write = function( data )
{
var _self = this; var _self = this;
this.size += data.length; this.size += data.length;
@ -48,14 +53,14 @@ ConditionalStream.prototype.write = function( data )
{ {
this.file = path.join( this.tmpPath, "ss_" + crypto.randomBytes( 8 ).toString( "hex" ) ); this.file = path.join( this.tmpPath, "ss_" + crypto.randomBytes( 8 ).toString( "hex" ) );
this.stream = fs.createWriteStream( this.file, { mode: 0600 } ); this.stream = fs.createWriteStream( this.file, { mode: "0600" } );
this.stream.addListener( "finish", this.__end.bind( this ) ); this.stream.addListener( "finish", this.__end.bind( this ) );
this.stream.write( this.hexData, "hex" ); this.stream.write( this.hexData, "hex" );
} }
}; }
ConditionalStream.prototype.end = function( handler ) end( handler )
{ {
var _self = this; var _self = this;
if( this.stream ) if( this.stream )
{ {
@ -73,10 +78,10 @@ ConditionalStream.prototype.end = function( handler )
handler( _self ) handler( _self )
} , 0 ); } , 0 );
} }
}; }
ConditionalStream.prototype.discard = function() discard()
{ {
var _self = this; var _self = this;
this.__discard = true; this.__discard = true;
@ -88,16 +93,10 @@ ConditionalStream.prototype.discard = function()
if( _self.__error ) throw new Error( _self.__error ); if( _self.__error ) throw new Error( _self.__error );
} ); } );
} }
}; }
ConditionalStream.prototype.__end = function() toString( enc )
{ {
this.__finished = true;
if( this.__discard ) this.discard();
};
ConditionalStream.prototype.toString = function( enc )
{
if( this.stream ) if( this.stream )
{ {
this.discard(); this.discard();
@ -105,10 +104,10 @@ ConditionalStream.prototype.toString = function( enc )
} }
return new Buffer( this.hexData, "hex" ).toString( enc ); return new Buffer( this.hexData, "hex" ).toString( enc );
}; }
ConditionalStream.prototype.resultStream = function() resultStream()
{ {
var _self = this; var _self = this;
if( !this.__finished ) throw new Error( "Data is not finished yet" ); if( !this.__finished ) throw new Error( "Data is not finished yet" );
if( this.__discard ) throw new Error( "Data is discarded" ); if( this.__discard ) throw new Error( "Data is discarded" );
@ -129,6 +128,13 @@ ConditionalStream.prototype.resultStream = function()
}, 0 ); }, 0 );
return st; return st;
}; }
__end()
{
this.__finished = true;
if( this.__discard ) this.discard();
}
}
module.exports = ConditionalStream; module.exports = ConditionalStream;