@@ -164,6 +164,7 @@ console.log(translateNth(["", "a+"], 0, "d:\\snapshot\\countly\\plugins-ext\\123
164
164
// /////////////////////////////////////////////////////////////////
165
165
166
166
function isRootPath ( p ) {
167
+ if ( p === '.' ) p = require ( 'path' ) . resolve ( p ) ;
167
168
return require ( 'path' ) . dirname ( p ) === p ;
168
169
}
169
170
@@ -697,6 +698,7 @@ function payloadFileSync (pointer) {
697
698
698
699
var encoding = options . encoding ;
699
700
assertEncoding ( encoding ) ;
701
+
700
702
var buffer = readFileFromSnapshot ( path ) ;
701
703
if ( encoding ) buffer = buffer . toString ( encoding ) ;
702
704
return buffer ;
@@ -738,6 +740,49 @@ function payloadFileSync (pointer) {
738
740
// readdir ///////////////////////////////////////////////////////
739
741
// ///////////////////////////////////////////////////////////////
740
742
743
+ function readdirOptions ( options , hasCallback ) {
744
+ if ( ! options || ( hasCallback && typeof options === 'function' ) ) {
745
+ return { encoding : null } ;
746
+ } else if ( typeof options === 'string' ) {
747
+ return { encoding : options } ;
748
+ } else if ( typeof options === 'object' ) {
749
+ return options ;
750
+ } else {
751
+ return null ;
752
+ }
753
+ }
754
+
755
+ function Dirent ( name , type ) {
756
+ this . name = name ;
757
+ this . type = type ;
758
+ }
759
+
760
+ Dirent . prototype . isDirectory = function ( ) {
761
+ return this . type === 2 ;
762
+ } ;
763
+
764
+ Dirent . prototype . isFile = function ( ) {
765
+ return this . type === 1 ;
766
+ } ;
767
+
768
+ Dirent . prototype . isBlockDevice =
769
+ Dirent . prototype . isCharacterDevice =
770
+ Dirent . prototype . isSymbolicLink =
771
+ Dirent . prototype . isFIFO =
772
+ Dirent . prototype . isSocket = function ( ) {
773
+ return false ;
774
+ } ;
775
+
776
+ function getFileTypes ( path_ , entries ) {
777
+ return entries . map ( function ( entry ) {
778
+ var path = require ( 'path' ) . join ( path_ , entry ) ;
779
+ var entity = VIRTUAL_FILESYSTEM [ path ] ;
780
+ if ( entity [ STORE_BLOB ] || entity [ STORE_CONTENT ] ) return new Dirent ( entry , 1 ) ;
781
+ if ( entity [ STORE_LINKS ] ) return new Dirent ( entry , 2 ) ;
782
+ throw new Error ( 'UNEXPECTED-24' ) ;
783
+ } ) ;
784
+ }
785
+
741
786
function readdirRoot ( path , cb ) {
742
787
if ( cb ) {
743
788
ancestor . readdir ( path , function ( error , entries ) {
@@ -780,7 +825,7 @@ function payloadFileSync (pointer) {
780
825
return cb2 ( new Error ( 'UNEXPECTED-25' ) ) ;
781
826
}
782
827
783
- fs . readdirSync = function ( path ) {
828
+ fs . readdirSync = function ( path , options_ ) {
784
829
var isRoot = isRootPath ( path ) ;
785
830
786
831
if ( ! insideSnapshot ( path ) && ! isRoot ) {
@@ -790,10 +835,18 @@ function payloadFileSync (pointer) {
790
835
return ancestor . readdirSync . apply ( fs , translateNth ( arguments , 0 , path ) ) ;
791
836
}
792
837
793
- return readdirFromSnapshot ( path , isRoot ) ;
838
+ var options = readdirOptions ( options_ , false ) ;
839
+
840
+ if ( ! options ) {
841
+ return ancestor . readdirSync . apply ( fs , arguments ) ;
842
+ }
843
+
844
+ var entries = readdirFromSnapshot ( path , isRoot ) ;
845
+ if ( options . withFileTypes ) entries = getFileTypes ( path , entries ) ;
846
+ return entries ;
794
847
} ;
795
848
796
- fs . readdir = function ( path ) {
849
+ fs . readdir = function ( path , options_ ) {
797
850
var isRoot = isRootPath ( path ) ;
798
851
799
852
if ( ! insideSnapshot ( path ) && ! isRoot ) {
@@ -803,8 +856,18 @@ function payloadFileSync (pointer) {
803
856
return ancestor . readdir . apply ( fs , translateNth ( arguments , 0 , path ) ) ;
804
857
}
805
858
859
+ var options = readdirOptions ( options_ , true ) ;
860
+
861
+ if ( ! options ) {
862
+ return ancestor . readdir . apply ( fs , arguments ) ;
863
+ }
864
+
806
865
var callback = dezalgo ( maybeCallback ( arguments ) ) ;
807
- readdirFromSnapshot ( path , isRoot , callback ) ;
866
+ readdirFromSnapshot ( path , isRoot , function ( error , entries ) {
867
+ if ( error ) return callback ( error ) ;
868
+ if ( options . withFileTypes ) entries = getFileTypes ( path , entries ) ;
869
+ callback ( null , entries ) ;
870
+ } ) ;
808
871
} ;
809
872
810
873
// ///////////////////////////////////////////////////////////////
0 commit comments