@@ -178,6 +178,7 @@ function initStream(self) {
178178 self . _writeQueue = [ ] ;
179179 self . _writeQueueEncoding = [ ] ;
180180 self . _writeQueueFD = [ ] ;
181+ self . _writeQueueCallbacks = [ ] ;
181182
182183 self . _writeWatcher = ioWatchers . alloc ( ) ;
183184 self . _writeWatcher . socket = self ;
@@ -296,6 +297,7 @@ Stream.prototype.write = function(data /* [encoding], [fd], [cb] */) {
296297 this . _writeQueue = [ ] ;
297298 this . _writeQueueEncoding = [ ] ;
298299 this . _writeQueueFD = [ ] ;
300+ this . _writeQueueCallbacks = [ ] ;
299301 }
300302
301303 // Slow. There is already a write queue, so let's append to it.
@@ -311,9 +313,22 @@ Stream.prototype.write = function(data /* [encoding], [fd], [cb] */) {
311313 this . _writeQueueEncoding [ last ] === encoding ) {
312314 // optimization - concat onto last
313315 this . _writeQueue [ last ] += data ;
316+
317+ if ( cb ) {
318+ if ( ! this . _writeQueueCallbacks [ last ] ) {
319+ this . _writeQueueCallbacks [ last ] = cb ;
320+ } else {
321+ // awful
322+ this . _writeQueueCallbacks [ last ] = function ( ) {
323+ this . _writeQueueCallbacks [ last ] ( ) ;
324+ cb ( ) ;
325+ } ;
326+ }
327+ }
314328 } else {
315329 this . _writeQueue . push ( data ) ;
316330 this . _writeQueueEncoding . push ( encoding ) ;
331+ this . _writeQueueCallbacks . push ( cb ) ;
317332 }
318333
319334 if ( fd != undefined ) {
@@ -325,7 +340,7 @@ Stream.prototype.write = function(data /* [encoding], [fd], [cb] */) {
325340 // Fast.
326341 // The most common case. There is no write queue. Just push the data
327342 // directly to the socket.
328- return this . _writeOut ( data , encoding , fd ) ;
343+ return this . _writeOut ( data , encoding , fd , cb ) ;
329344 }
330345} ;
331346
@@ -337,7 +352,7 @@ Stream.prototype.write = function(data /* [encoding], [fd], [cb] */) {
337352// 2. Write data to socket. Return true if flushed.
338353// 3. Slice out remaining
339354// 4. Unshift remaining onto _writeQueue. Return false.
340- Stream . prototype . _writeOut = function ( data , encoding , fd ) {
355+ Stream . prototype . _writeOut = function ( data , encoding , fd , cb ) {
341356 if ( ! this . writable ) {
342357 throw new Error ( 'Stream is not writable' ) ;
343358 }
@@ -388,6 +403,7 @@ Stream.prototype._writeOut = function(data, encoding, fd) {
388403 // Unshift whatever didn't fit onto the buffer
389404 this . _writeQueue . unshift ( data . slice ( charsWritten ) ) ;
390405 this . _writeQueueEncoding . unshift ( encoding ) ;
406+ this . _writeQueueCallbacks . unshift ( cb ) ;
391407 this . _writeWatcher . start ( ) ;
392408 queuedData = true ;
393409 }
@@ -416,6 +432,7 @@ Stream.prototype._writeOut = function(data, encoding, fd) {
416432 if ( queuedData ) {
417433 return false ;
418434 } else {
435+ if ( cb ) cb ( ) ;
419436 return true ;
420437 }
421438 }
@@ -434,6 +451,7 @@ Stream.prototype._writeOut = function(data, encoding, fd) {
434451 // data should be the next thing to write.
435452 this . _writeQueue . unshift ( leftOver ) ;
436453 this . _writeQueueEncoding . unshift ( null ) ;
454+ this . _writeQueueCallbacks . unshift ( cb ) ;
437455
438456 // If didn't successfully write any bytes, enqueue our fd and try again
439457 if ( ! bytesWritten ) {
@@ -450,14 +468,15 @@ Stream.prototype.flush = function() {
450468 while ( this . _writeQueue && this . _writeQueue . length ) {
451469 var data = this . _writeQueue . shift ( ) ;
452470 var encoding = this . _writeQueueEncoding . shift ( ) ;
471+ var cb = this . _writeQueueCallbacks . shift ( ) ;
453472 var fd = this . _writeQueueFD . shift ( ) ;
454473
455474 if ( data === END_OF_FILE ) {
456475 this . _shutdown ( ) ;
457476 return true ;
458477 }
459478
460- var flushed = this . _writeOut ( data , encoding , fd ) ;
479+ var flushed = this . _writeOut ( data , encoding , fd , cb ) ;
461480 if ( ! flushed ) return false ;
462481 }
463482 if ( this . _writeWatcher ) this . _writeWatcher . stop ( ) ;
0 commit comments