@@ -375,3 +375,90 @@ exports.realpath = function(path, callback) {
375
375
callback ( null , normalize ( path ) ) ;
376
376
} ) ;
377
377
}
378
+
379
+ exports . fileWriteStream = function ( path , options ) {
380
+ return new FileWriteStream ( path , options ) ;
381
+ } ;
382
+
383
+ var FileWriteStream = exports . FileWriteStream = function ( path , options ) {
384
+ this . path = path ;
385
+ this . fd = null ;
386
+ this . closed = false ;
387
+
388
+ this . flags = 'w' ;
389
+ this . encoding = 'binary' ;
390
+ this . mode = 0666 ;
391
+
392
+ process . mixin ( this , options || { } ) ;
393
+
394
+ var
395
+ self = this ,
396
+ queue = [ ] ,
397
+ busy = false ;
398
+
399
+ queue . push ( [ fs . open , this . path , this . flags , this . mode ] ) ;
400
+
401
+ function pump ( ) {
402
+ if ( busy ) {
403
+ return ;
404
+ }
405
+
406
+ var args = queue . shift ( ) ;
407
+ if ( ! args ) {
408
+ return self . emit ( 'drain' ) ;
409
+ }
410
+
411
+ busy = true ;
412
+
413
+ var method = args . shift ( ) ;
414
+
415
+ args . push ( function ( err ) {
416
+ busy = false ;
417
+
418
+ if ( err ) {
419
+ self . emit ( 'error' , err ) ;
420
+ return ;
421
+ }
422
+
423
+ // save reference for file pointer
424
+ if ( method === fs . open ) {
425
+ self . fd = arguments [ 1 ] ;
426
+ self . emit ( 'open' , self . fd ) ;
427
+ }
428
+
429
+ // stop pumping after close
430
+ if ( method === fs . close ) {
431
+ self . emit ( 'close' ) ;
432
+ return ;
433
+ }
434
+
435
+ pump ( ) ;
436
+ } ) ;
437
+
438
+ // Inject the file pointer
439
+ if ( method !== fs . open ) {
440
+ args . unshift ( self . fd ) ;
441
+ }
442
+
443
+ method . apply ( null , args ) ;
444
+ } ;
445
+
446
+ this . write = function ( data ) {
447
+ if ( this . closed ) {
448
+ throw new Error ( 'stream already closed' ) ;
449
+ }
450
+
451
+ queue . push ( [ fs . write , data , undefined , this . encoding ] ) ;
452
+ pump ( ) ;
453
+ return false ;
454
+ } ;
455
+
456
+ this . close = function ( ) {
457
+ this . closed = true ;
458
+ queue . push ( [ fs . close , ] ) ;
459
+ pump ( ) ;
460
+ } ;
461
+
462
+ pump ( ) ;
463
+ } ;
464
+ FileWriteStream . prototype . __proto__ = process . EventEmitter . prototype ;
0 commit comments