11import * as fs from "fs" ;
22import { callbackify } from "util" ;
3- import { ClientProxy } from "../../common/proxy" ;
3+ import { ClientProxy , Batch } from "../../common/proxy" ;
44import { IEncodingOptions , IEncodingOptionsCallback } from "../../common/util" ;
55import { FsModuleProxy , Stats as IStats , WatcherProxy , WriteStreamProxy } from "../../node/modules/fs" ;
66import { Writable } from "./stream" ;
77
88// tslint:disable no-any
99
10+ class StatBatch extends Batch < IStats , { path : fs . PathLike } > {
11+ public constructor ( private readonly proxy : FsModuleProxy ) {
12+ super ( ) ;
13+ }
14+
15+ protected remoteCall ( batch : { path : fs . PathLike } [ ] ) : Promise < ( IStats | Error ) [ ] > {
16+ return this . proxy . statBatch ( batch ) ;
17+ }
18+ }
19+
20+ class LstatBatch extends Batch < IStats , { path : fs . PathLike } > {
21+ public constructor ( private readonly proxy : FsModuleProxy ) {
22+ super ( ) ;
23+ }
24+
25+ protected remoteCall ( batch : { path : fs . PathLike } [ ] ) : Promise < ( IStats | Error ) [ ] > {
26+ return this . proxy . lstatBatch ( batch ) ;
27+ }
28+ }
29+
30+ class ReaddirBatch extends Batch < Buffer [ ] | fs . Dirent [ ] | string [ ] , { path : fs . PathLike , options : IEncodingOptions } > {
31+ public constructor ( private readonly proxy : FsModuleProxy ) {
32+ super ( ) ;
33+ }
34+
35+ protected remoteCall ( queue : { path : fs . PathLike , options : IEncodingOptions } [ ] ) : Promise < ( Buffer [ ] | fs . Dirent [ ] | string [ ] | Error ) [ ] > {
36+ return this . proxy . readdirBatch ( queue ) ;
37+ }
38+ }
39+
1040class Watcher extends ClientProxy < WatcherProxy > implements fs . FSWatcher {
1141 public close ( ) : void {
1242 this . proxy . close ( ) ;
@@ -28,7 +58,15 @@ class WriteStream extends Writable<WriteStreamProxy> implements fs.WriteStream {
2858}
2959
3060export class FsModule {
31- public constructor ( private readonly proxy : FsModuleProxy ) { }
61+ private readonly statBatch : StatBatch ;
62+ private readonly lstatBatch : LstatBatch ;
63+ private readonly readdirBatch : ReaddirBatch ;
64+
65+ public constructor ( private readonly proxy : FsModuleProxy ) {
66+ this . statBatch = new StatBatch ( this . proxy ) ;
67+ this . lstatBatch = new LstatBatch ( this . proxy ) ;
68+ this . readdirBatch = new ReaddirBatch ( this . proxy ) ;
69+ }
3270
3371 public access = ( path : fs . PathLike , mode : number | undefined | ( ( err : NodeJS . ErrnoException ) => void ) , callback ?: ( err : NodeJS . ErrnoException ) => void ) : void => {
3472 if ( typeof mode === "function" ) {
@@ -72,9 +110,7 @@ export class FsModule {
72110 }
73111
74112 public exists = ( path : fs . PathLike , callback : ( exists : boolean ) => void ) : void => {
75- callbackify ( this . proxy . exists ) ( path , ( exists ) => {
76- callback ! ( exists as any ) ;
77- } ) ;
113+ this . proxy . exists ( path ) . then ( ( exists ) => callback ( exists ) ) . catch ( ( ) => callback ( false ) ) ;
78114 }
79115
80116 public fchmod = ( fd : number , mode : string | number , callback : ( err : NodeJS . ErrnoException ) => void ) : void => {
@@ -124,7 +160,7 @@ export class FsModule {
124160 }
125161
126162 public lstat = ( path : fs . PathLike , callback : ( err : NodeJS . ErrnoException , stats : fs . Stats ) => void ) : void => {
127- callbackify ( this . proxy . lstat ) ( path , ( error , stats ) => {
163+ callbackify ( this . lstatBatch . add ) ( { path } , ( error , stats ) => {
128164 callback ( error , stats && new Stats ( stats ) ) ;
129165 } ) ;
130166 }
@@ -175,7 +211,7 @@ export class FsModule {
175211 callback = options ;
176212 options = undefined ;
177213 }
178- callbackify ( this . proxy . readdir ) ( path , options , callback ! ) ;
214+ callbackify ( this . readdirBatch . add ) ( { path, options } , callback ! ) ;
179215 }
180216
181217 public readlink = ( path : fs . PathLike , options : IEncodingOptionsCallback , callback ?: ( err : NodeJS . ErrnoException , linkString : string | Buffer ) => void ) : void => {
@@ -203,7 +239,7 @@ export class FsModule {
203239 }
204240
205241 public stat = ( path : fs . PathLike , callback : ( err : NodeJS . ErrnoException , stats : fs . Stats ) => void ) : void => {
206- callbackify ( this . proxy . stat ) ( path , ( error , stats ) => {
242+ callbackify ( this . statBatch . add ) ( { path } , ( error , stats ) => {
207243 callback ( error , stats && new Stats ( stats ) ) ;
208244 } ) ;
209245 }
0 commit comments