@@ -56,17 +56,18 @@ var ioWatchers = new FreeList("iowatcher", 100, function () {
5656
5757
5858IOWatcher . prototype . ondrain = function ( ) {
59- assert ( this . socket ) ;
60- var socket = this . socket ;
59+ if ( this . socket ) {
60+ var socket = this . socket ;
6161
62- if ( socket . writable || socket . readable ) {
63- require ( 'timers' ) . active ( socket ) ;
64- }
62+ if ( socket . writable || socket . readable ) {
63+ require ( 'timers' ) . active ( socket ) ;
64+ }
6565
66- socket . emit ( 'drain' ) ;
67- if ( socket . ondrain ) socket . ondrain ( ) ;
66+ socket . emit ( 'drain' ) ;
67+ if ( socket . ondrain ) socket . ondrain ( ) ;
6868
69- if ( socket . _eof ) socket . _shutdown ( ) ;
69+ if ( socket . _eof ) socket . _shutdown ( ) ;
70+ }
7071} ;
7172
7273
@@ -252,12 +253,13 @@ Object.defineProperty(Stream.prototype, 'readyState', {
252253} ) ;
253254
254255
255- Stream . prototype . _appendBucket = function ( data , encoding , fd ) {
256+ Stream . prototype . _appendBucket = function ( data , encoding , fd , callback ) {
256257 if ( data . length != 0 ) {
257258 // TODO reject empty data.
258259 var newBucket = { data : data } ;
259260 if ( encoding ) newBucket . encoding = encoding ;
260261 if ( fd ) newBucket . fd = fd ;
262+ if ( callback ) newBucket . callback = callback ;
261263
262264 // TODO properly calculate queueSize
263265
@@ -280,7 +282,7 @@ Stream.prototype._appendBucket = function (data, encoding, fd) {
280282} ;
281283
282284
283- Stream . prototype . write = function ( data , encoding , fd ) {
285+ Stream . prototype . write = function ( data /* encoding, fd, callback */ ) {
284286 if ( this . _eof ) {
285287 throw new Error ( 'Stream.end() called already; cannot write.' ) ;
286288 }
@@ -289,7 +291,29 @@ Stream.prototype.write = function (data, encoding, fd) {
289291 throw new Error ( 'Stream is not writable' ) ;
290292 }
291293
292- var queueSize = this . _appendBucket ( data , encoding , fd ) ;
294+ // parse the arguments. ugly.
295+
296+ var encoding , fd , callback ;
297+
298+ if ( arguments [ 1 ] === undefined || typeof arguments [ 1 ] == 'string' ) {
299+ encoding = arguments [ 1 ] ;
300+ if ( typeof arguments [ 2 ] == 'number' ) {
301+ fd = arguments [ 2 ] ;
302+ callback = arguments [ 3 ] ;
303+ } else {
304+ callback = arguments [ 2 ] ;
305+ }
306+ } else if ( typeof arguments [ 1 ] == 'number' ) {
307+ fd = arguments [ 1 ] ;
308+ callback = arguments [ 2 ] ;
309+ } else if ( typeof arguments [ 1 ] == 'function' ) {
310+ callback = arguments [ 1 ] ;
311+ } else {
312+ throw new Error ( "Bad type for second argument" ) ;
313+ }
314+
315+
316+ var queueSize = this . _appendBucket ( data , encoding , fd , callback ) ;
293317
294318 if ( this . _connecting ) return false ;
295319
0 commit comments