node.js v6 can now extends String

This commit is contained in:
斟酌 鵬兄 2016-07-04 11:56:03 +08:00
parent 06c8b74221
commit c73aa30c39

View File

@ -1,19 +1,18 @@
"use strict"; "use strict";
var Dragonfly = global.Dragonfly;
var fs = require( "fs" ); const Dragonfly = global.Dragonfly;
var path = require( "path" );
var crypto = require( "crypto" );
var ReadStream = require( "stream" ).Readable; const fs = require( "fs" );
const path = require( "path" );
const crypto = require( "crypto" );
const ReadStream = require( "stream" ).Readable;
class ConditionalStream extends String class ConditionalStream extends String
{ {
constructor( tmpPath, triggerLimit ) constructor( tmpPath, triggerLimit )
{ {
super(); super();
// XXX: Dirty fix for incompat on node js v5
Object.setPrototypeOf( this, new.target.prototype );
if( !tmpPath ) if( !tmpPath )
{ {
@ -36,7 +35,6 @@ class ConditionalStream extends String
write( data ) write( data )
{ {
var _self = this;
this.size += data.length; this.size += data.length;
if( this.stream ) if( this.stream )
@ -61,36 +59,32 @@ class ConditionalStream extends String
end( handler ) end( handler )
{ {
var _self = this;
if( this.stream ) if( this.stream )
{ {
this.stream.addListener( "close", function() { this.stream.addListener( "close", () => {
_self.__finished = true; this.__finished = true;
handler( _self ); handler( this );
} ); } );
this.stream.end(); this.stream.end();
} }
else else
{ {
setTimeout( function() setTimeout( () => {
{ this.__finished = true;
_self.__finished = true; handler( this );
handler( _self )
} , 0 ); } , 0 );
} }
} }
discard() discard()
{ {
var _self = this;
this.__discard = true; this.__discard = true;
if( this.__finished && this.file ) if( this.__finished && this.file )
{ {
fs.unlink( this.file, function( e ) fs.unlink( this.file, ( e ) =>
{ {
Dragonfly.Debug( "Client Data Closed: " + _self.file ); Dragonfly.Debug( "Client Data Closed: " + this.file );
if( _self.__error ) throw new Error( _self.__error ); if( this.__error ) throw new Error( this.__error );
} ); } );
} }
} }
@ -108,22 +102,21 @@ class ConditionalStream extends String
resultStream() resultStream()
{ {
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" );
if( this.stream ) if( this.stream )
{ {
var rt = fs.createReadStream( this.file ); var rt = fs.createReadStream( this.file );
rt.addListener( "close", () => _self.discard() ); rt.addListener( "close", () => this.discard() );
return rt; return rt;
} }
var st = new ReadStream(); var st = new ReadStream();
st._read = function(){}; st._read = function(){};
setTimeout( function() { setTimeout( () => {
st.push( _self.hexData, "hex" ); st.push( this.hexData, "hex" );
st.push( null ); st.push( null );
}, 0 ); }, 0 );