@@ -92,40 +92,6 @@ process.createChildProcess = function (file, args, env) {
92
92
return child ;
93
93
} ;
94
94
95
- process . fs . cat = function ( path , encoding ) {
96
- var promise = new process . Promise ( ) ;
97
-
98
- encoding = encoding || "utf8" ; // default to utf8
99
-
100
- process . fs . open ( path , process . O_RDONLY , 0666 ) . addCallback ( function ( fd ) {
101
- var content = "" , pos = 0 ;
102
-
103
- function readChunk ( ) {
104
- process . fs . read ( fd , 16 * 1024 , pos , encoding ) . addCallback ( function ( chunk , bytes_read ) {
105
- if ( chunk ) {
106
- if ( chunk . constructor === String ) {
107
- content += chunk ;
108
- } else {
109
- content = content . concat ( chunk ) ;
110
- }
111
-
112
- pos += bytes_read ;
113
- readChunk ( ) ;
114
- } else {
115
- promise . emitSuccess ( content ) ;
116
- process . fs . close ( fd ) ;
117
- }
118
- } ) . addErrback ( function ( ) {
119
- promise . emitError ( ) ;
120
- } ) ;
121
- }
122
- readChunk ( ) ;
123
- } ) . addErrback ( function ( ) {
124
- promise . emitError ( new Error ( "Could not open " + path ) ) ;
125
- } ) ;
126
- return promise ;
127
- } ;
128
-
129
95
process . assert = function ( x , msg ) {
130
96
if ( ! ( x ) ) throw new Error ( msg || "assertion error" ) ;
131
97
} ;
@@ -374,6 +340,38 @@ process.unwatchFile = function (filename) {
374
340
}
375
341
} ;
376
342
343
+ process . Stats . prototype . _checkModeProperty = function ( property ) {
344
+ return ( ( this . mode & property ) === property ) ;
345
+ } ;
346
+
347
+ process . Stats . prototype . isDirectory = function ( ) {
348
+ return this . _checkModeProperty ( process . S_IFDIR ) ;
349
+ } ;
350
+
351
+ process . Stats . prototype . isFile = function ( ) {
352
+ return this . _checkModeProperty ( process . S_IFREG ) ;
353
+ } ;
354
+
355
+ process . Stats . prototype . isBlockDevice = function ( ) {
356
+ return this . _checkModeProperty ( process . S_IFBLK ) ;
357
+ } ;
358
+
359
+ process . Stats . prototype . isCharacterDevice = function ( ) {
360
+ return this . _checkModeProperty ( process . S_IFCHR ) ;
361
+ } ;
362
+
363
+ process . Stats . prototype . isSymbolicLink = function ( ) {
364
+ return this . _checkModeProperty ( process . S_IFLNK ) ;
365
+ } ;
366
+
367
+ process . Stats . prototype . isFIFO = function ( ) {
368
+ return this . _checkModeProperty ( process . S_IFIFO ) ;
369
+ } ;
370
+
371
+ process . Stats . prototype . isSocket = function ( ) {
372
+ return this . _checkModeProperty ( process . S_IFSOCK ) ;
373
+ } ;
374
+
377
375
378
376
379
377
// Timers
@@ -446,6 +444,171 @@ function createInternalModule (id, constructor) {
446
444
return m ;
447
445
} ;
448
446
447
+ var posixModule = createInternalModule ( "posix" , function ( exports ) {
448
+ exports . Stats = process . Stats ;
449
+
450
+ function callback ( promise ) {
451
+ return function ( ) {
452
+ if ( arguments [ 0 ] instanceof Error ) {
453
+ promise . emitError . apply ( promise , arguments ) ;
454
+ } else {
455
+ promise . emitSuccess . apply ( promise , arguments ) ;
456
+ }
457
+ }
458
+ }
459
+
460
+ // Yes, the follow could be easily DRYed up but I provide the explicit
461
+ // list to make the arguments clear.
462
+
463
+ exports . close = function ( fd ) {
464
+ var promise = new process . Promise ( )
465
+ process . fs . close ( fd , callback ( promise ) ) ;
466
+ return promise ;
467
+ } ;
468
+
469
+ exports . closeSync = function ( fd ) {
470
+ return process . fs . close ( fd ) ;
471
+ } ;
472
+
473
+ exports . open = function ( path , flags , mode ) {
474
+ var promise = new process . Promise ( )
475
+ process . fs . open ( path , flags , mode , callback ( promise ) ) ;
476
+ return promise ;
477
+ } ;
478
+
479
+ exports . openSync = function ( path , flags , mode ) {
480
+ return process . fs . open ( path , flags , mode ) ;
481
+ } ;
482
+
483
+ exports . read = function ( fd , length , position , encoding ) {
484
+ var promise = new process . Promise ( )
485
+ process . fs . read ( fd , length , position , encoding , callback ( promise ) ) ;
486
+ return promise ;
487
+ } ;
488
+
489
+ exports . readSync = function ( fd , length , position , encoding ) {
490
+ return process . fs . read ( fd , length , position , encoding ) ;
491
+ } ;
492
+
493
+ exports . write = function ( fd , data , position , encoding ) {
494
+ var promise = new process . Promise ( )
495
+ process . fs . write ( fd , data , position , encoding , callback ( promise ) ) ;
496
+ return promise ;
497
+ } ;
498
+
499
+ exports . writeSync = function ( fd , data , position , encoding ) {
500
+ return process . fs . write ( fd , data , position , encoding ) ;
501
+ } ;
502
+
503
+ exports . rename = function ( oldPath , newPath ) {
504
+ var promise = new process . Promise ( )
505
+ process . fs . rename ( oldPath , newPath , callback ( promise ) ) ;
506
+ return promise ;
507
+ } ;
508
+
509
+ exports . renameSync = function ( oldPath , newPath ) {
510
+ return process . fs . rename ( oldPath , newPath ) ;
511
+ } ;
512
+
513
+ exports . rmdir = function ( path ) {
514
+ var promise = new process . Promise ( )
515
+ process . fs . rmdir ( path , callback ( promise ) ) ;
516
+ return promise ;
517
+ } ;
518
+
519
+ exports . rmdirSync = function ( path ) {
520
+ return process . fs . rmdir ( path ) ;
521
+ } ;
522
+
523
+ exports . mkdir = function ( path , mode ) {
524
+ var promise = new process . Promise ( )
525
+ process . fs . mkdir ( path , mode , callback ( promise ) ) ;
526
+ return promise ;
527
+ } ;
528
+
529
+ exports . mkdirSync = function ( path , mode ) {
530
+ return process . fs . mkdir ( path , mode ) ;
531
+ } ;
532
+
533
+ exports . sendfile = function ( outFd , inFd , inOffset , length ) {
534
+ var promise = new process . Promise ( )
535
+ process . fs . sendfile ( outFd , inFd , inOffset , length , callback ( promise ) ) ;
536
+ return promise ;
537
+ } ;
538
+
539
+ exports . sendfileSync = function ( outFd , inFd , inOffset , length ) {
540
+ return process . fs . sendfile ( outFd , inFd , inOffset , length ) ;
541
+ } ;
542
+
543
+ exports . readdir = function ( path ) {
544
+ var promise = new process . Promise ( )
545
+ process . fs . readdir ( path , callback ( promise ) ) ;
546
+ return promise ;
547
+ } ;
548
+
549
+ exports . readdirSync = function ( path ) {
550
+ return process . fs . readdir ( path ) ;
551
+ } ;
552
+
553
+ exports . stat = function ( path ) {
554
+ var promise = new process . Promise ( )
555
+ process . fs . stat ( path , callback ( promise ) ) ;
556
+ return promise ;
557
+ } ;
558
+
559
+ exports . statSync = function ( path ) {
560
+ return process . fs . stat ( path ) ;
561
+ } ;
562
+
563
+ exports . unlink = function ( path ) {
564
+ var promise = new process . Promise ( )
565
+ process . fs . unlink ( path , callback ( promise ) ) ;
566
+ return promise ;
567
+ } ;
568
+
569
+ exports . unlinkSync = function ( path ) {
570
+ return process . fs . unlink ( path ) ;
571
+ } ;
572
+
573
+
574
+ exports . cat = function ( path , encoding ) {
575
+ var promise = new process . Promise ( ) ;
576
+
577
+ encoding = encoding || "utf8" ; // default to utf8
578
+
579
+ exports . open ( path , process . O_RDONLY , 0666 ) . addCallback ( function ( fd ) {
580
+ var content = "" , pos = 0 ;
581
+
582
+ function readChunk ( ) {
583
+ exports . read ( fd , 16 * 1024 , pos , encoding ) . addCallback ( function ( chunk , bytes_read ) {
584
+ if ( chunk ) {
585
+ if ( chunk . constructor === String ) {
586
+ content += chunk ;
587
+ } else {
588
+ content = content . concat ( chunk ) ;
589
+ }
590
+
591
+ pos += bytes_read ;
592
+ readChunk ( ) ;
593
+ } else {
594
+ promise . emitSuccess ( content ) ;
595
+ exports . close ( fd ) ;
596
+ }
597
+ } ) . addErrback ( function ( ) {
598
+ promise . emitError . call ( arguments ) ;
599
+ } ) ;
600
+ }
601
+ readChunk ( ) ;
602
+ } ) . addErrback ( function ( ) {
603
+ promise . emitError . apply ( promise , arguments ) ;
604
+ } ) ;
605
+ return promise ;
606
+ } ;
607
+ } ) ;
608
+
609
+ var posix = posixModule . exports ;
610
+
611
+
449
612
var pathModule = createInternalModule ( "path" , function ( exports ) {
450
613
exports . join = function ( ) {
451
614
var joined = "" ;
@@ -481,7 +644,7 @@ var pathModule = createInternalModule("path", function (exports) {
481
644
} ;
482
645
483
646
exports . exists = function ( path , callback ) {
484
- var p = process . fs . stat ( path ) ;
647
+ var p = posix . stat ( path ) ;
485
648
p . addCallback ( function ( ) { callback ( true ) ; } ) ;
486
649
p . addErrback ( function ( ) { callback ( false ) ; } ) ;
487
650
} ;
@@ -637,7 +800,7 @@ function cat (id, loadPromise) {
637
800
loadPromise . emitError ( new Error ( "could not load core module \"http\"" ) ) ;
638
801
} ) ;
639
802
} else {
640
- promise = process . fs . cat ( id ) ;
803
+ promise = posix . cat ( id ) ;
641
804
}
642
805
643
806
return promise ;
0 commit comments