1
1
import * as fs from "fs" ;
2
2
import { callbackify } from "util" ;
3
- import { ClientProxy } from "../../common/proxy" ;
3
+ import { ClientProxy , Batch } from "../../common/proxy" ;
4
4
import { IEncodingOptions , IEncodingOptionsCallback } from "../../common/util" ;
5
5
import { FsModuleProxy , Stats as IStats , WatcherProxy , WriteStreamProxy } from "../../node/modules/fs" ;
6
6
import { Writable } from "./stream" ;
7
7
8
8
// tslint:disable no-any
9
9
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
+
10
40
class Watcher extends ClientProxy < WatcherProxy > implements fs . FSWatcher {
11
41
public close ( ) : void {
12
42
this . proxy . close ( ) ;
@@ -28,7 +58,15 @@ class WriteStream extends Writable<WriteStreamProxy> implements fs.WriteStream {
28
58
}
29
59
30
60
export 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
+ }
32
70
33
71
public access = ( path : fs . PathLike , mode : number | undefined | ( ( err : NodeJS . ErrnoException ) => void ) , callback ?: ( err : NodeJS . ErrnoException ) => void ) : void => {
34
72
if ( typeof mode === "function" ) {
@@ -72,9 +110,7 @@ export class FsModule {
72
110
}
73
111
74
112
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 ) ) ;
78
114
}
79
115
80
116
public fchmod = ( fd : number , mode : string | number , callback : ( err : NodeJS . ErrnoException ) => void ) : void => {
@@ -124,7 +160,7 @@ export class FsModule {
124
160
}
125
161
126
162
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 ) => {
128
164
callback ( error , stats && new Stats ( stats ) ) ;
129
165
} ) ;
130
166
}
@@ -175,7 +211,7 @@ export class FsModule {
175
211
callback = options ;
176
212
options = undefined ;
177
213
}
178
- callbackify ( this . proxy . readdir ) ( path , options , callback ! ) ;
214
+ callbackify ( this . readdirBatch . add ) ( { path, options } , callback ! ) ;
179
215
}
180
216
181
217
public readlink = ( path : fs . PathLike , options : IEncodingOptionsCallback , callback ?: ( err : NodeJS . ErrnoException , linkString : string | Buffer ) => void ) : void => {
@@ -203,7 +239,7 @@ export class FsModule {
203
239
}
204
240
205
241
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 ) => {
207
243
callback ( error , stats && new Stats ( stats ) ) ;
208
244
} ) ;
209
245
}
0 commit comments