6
6
FunctionPrototypeBind,
7
7
ObjectDefineProperty,
8
8
PromiseReject,
9
- Symbol,
10
9
SymbolAsyncIterator,
11
10
} = primordials ;
12
11
@@ -34,74 +33,68 @@ const {
34
33
validateUint32,
35
34
} = require ( 'internal/validators' ) ;
36
35
37
- const kDirHandle = Symbol ( 'kDirHandle' ) ;
38
- const kDirPath = Symbol ( 'kDirPath' ) ;
39
- const kDirBufferedEntries = Symbol ( 'kDirBufferedEntries' ) ;
40
- const kDirClosed = Symbol ( 'kDirClosed' ) ;
41
- const kDirOptions = Symbol ( 'kDirOptions' ) ;
42
- const kDirReadImpl = Symbol ( 'kDirReadImpl' ) ;
43
- const kDirReadPromisified = Symbol ( 'kDirReadPromisified' ) ;
44
- const kDirClosePromisified = Symbol ( 'kDirClosePromisified' ) ;
45
- const kDirOperationQueue = Symbol ( 'kDirOperationQueue' ) ;
46
-
47
36
class Dir {
37
+ #handle;
38
+ #path;
39
+ #bufferedEntries = [ ] ;
40
+ #closed = false ;
41
+ #options;
42
+ #readPromisified;
43
+ #closePromisified;
44
+ // Either `null` or an Array of pending operations (= functions to be called
45
+ // once the current operation is done).
46
+ #operationQueue = null ;
47
+
48
48
constructor ( handle , path , options ) {
49
49
if ( handle == null ) throw new ERR_MISSING_ARGS ( 'handle' ) ;
50
- this [ kDirHandle ] = handle ;
51
- this [ kDirBufferedEntries ] = [ ] ;
52
- this [ kDirPath ] = path ;
53
- this [ kDirClosed ] = false ;
54
-
55
- // Either `null` or an Array of pending operations (= functions to be called
56
- // once the current operation is done).
57
- this [ kDirOperationQueue ] = null ;
58
-
59
- this [ kDirOptions ] = {
50
+ this . #handle = handle ;
51
+ this . #path = path ;
52
+ this . #options = {
60
53
bufferSize : 32 ,
61
54
...getOptions ( options , {
62
55
encoding : 'utf8' ,
63
56
} ) ,
64
57
} ;
65
58
66
- validateUint32 ( this [ kDirOptions ] . bufferSize , 'options.bufferSize' , true ) ;
59
+ validateUint32 ( this . #options . bufferSize , 'options.bufferSize' , true ) ;
67
60
68
- this [ kDirReadPromisified ] = FunctionPrototypeBind (
69
- internalUtil . promisify ( this [ kDirReadImpl ] ) , this , false ) ;
70
- this [ kDirClosePromisified ] = FunctionPrototypeBind (
61
+ this . #readPromisified = FunctionPrototypeBind (
62
+ internalUtil . promisify ( this . #readImpl ) , this , false ) ;
63
+ this . #closePromisified = FunctionPrototypeBind (
71
64
internalUtil . promisify ( this . close ) , this ) ;
72
65
}
73
66
74
67
get path ( ) {
75
- return this [ kDirPath ] ;
68
+ return this . #path ;
76
69
}
77
70
78
71
read ( callback ) {
79
- return this [ kDirReadImpl ] ( true , callback ) ;
72
+ return this . #readImpl ( true , callback ) ;
80
73
}
81
74
82
- [ kDirReadImpl ] ( maybeSync , callback ) {
83
- if ( this [ kDirClosed ] === true ) {
75
+ #readImpl ( maybeSync , callback ) {
76
+ if ( this . #closed === true ) {
84
77
throw new ERR_DIR_CLOSED ( ) ;
85
78
}
86
79
87
80
if ( callback === undefined ) {
88
- return this [ kDirReadPromisified ] ( ) ;
81
+ return this . #readPromisified ( ) ;
89
82
}
90
83
91
84
validateFunction ( callback , 'callback' ) ;
92
85
93
- if ( this [ kDirOperationQueue ] !== null ) {
94
- ArrayPrototypePush ( this [ kDirOperationQueue ] , ( ) => {
95
- this [ kDirReadImpl ] ( maybeSync , callback ) ;
86
+ if ( this . #operationQueue !== null ) {
87
+ ArrayPrototypePush ( this . #operationQueue , ( ) => {
88
+ this . #readImpl ( maybeSync , callback ) ;
96
89
} ) ;
97
90
return ;
98
91
}
99
92
100
- if ( this [ kDirBufferedEntries ] . length > 0 ) {
93
+ if ( this . #bufferedEntries . length > 0 ) {
101
94
try {
102
- const dirent = ArrayPrototypeShift ( this [ kDirBufferedEntries ] ) ;
95
+ const dirent = ArrayPrototypeShift ( this . #bufferedEntries ) ;
103
96
104
- if ( this [ kDirOptions ] . recursive && dirent . isDirectory ( ) ) {
97
+ if ( this . #options . recursive && dirent . isDirectory ( ) ) {
105
98
this . readSyncRecursive ( dirent ) ;
106
99
}
107
100
@@ -118,8 +111,8 @@ class Dir {
118
111
const req = new FSReqCallback ( ) ;
119
112
req . oncomplete = ( err , result ) => {
120
113
process . nextTick ( ( ) => {
121
- const queue = this [ kDirOperationQueue ] ;
122
- this [ kDirOperationQueue ] = null ;
114
+ const queue = this . #operationQueue ;
115
+ this . #operationQueue = null ;
123
116
for ( const op of queue ) op ( ) ;
124
117
} ) ;
125
118
@@ -128,9 +121,9 @@ class Dir {
128
121
}
129
122
130
123
try {
131
- this . processReadResult ( this [ kDirPath ] , result ) ;
132
- const dirent = ArrayPrototypeShift ( this [ kDirBufferedEntries ] ) ;
133
- if ( this [ kDirOptions ] . recursive && dirent . isDirectory ( ) ) {
124
+ this . processReadResult ( this . #path , result ) ;
125
+ const dirent = ArrayPrototypeShift ( this . #bufferedEntries ) ;
126
+ if ( this . #options . recursive && dirent . isDirectory ( ) ) {
134
127
this . readSyncRecursive ( dirent ) ;
135
128
}
136
129
callback ( null , dirent ) ;
@@ -139,18 +132,18 @@ class Dir {
139
132
}
140
133
} ;
141
134
142
- this [ kDirOperationQueue ] = [ ] ;
143
- this [ kDirHandle ] . read (
144
- this [ kDirOptions ] . encoding ,
145
- this [ kDirOptions ] . bufferSize ,
135
+ this . #operationQueue = [ ] ;
136
+ this . #handle . read (
137
+ this . #options . encoding ,
138
+ this . #options . bufferSize ,
146
139
req ,
147
140
) ;
148
141
}
149
142
150
143
processReadResult ( path , result ) {
151
144
for ( let i = 0 ; i < result . length ; i += 2 ) {
152
145
ArrayPrototypePush (
153
- this [ kDirBufferedEntries ] ,
146
+ this . #bufferedEntries ,
154
147
getDirent (
155
148
path ,
156
149
result [ i ] ,
@@ -165,14 +158,14 @@ class Dir {
165
158
const ctx = { path } ;
166
159
const handle = dirBinding . opendir (
167
160
pathModule . toNamespacedPath ( path ) ,
168
- this [ kDirOptions ] . encoding ,
161
+ this . #options . encoding ,
169
162
undefined ,
170
163
ctx ,
171
164
) ;
172
165
handleErrorFromBinding ( ctx ) ;
173
166
const result = handle . read (
174
- this [ kDirOptions ] . encoding ,
175
- this [ kDirOptions ] . bufferSize ,
167
+ this . #options . encoding ,
168
+ this . #options . bufferSize ,
176
169
undefined ,
177
170
ctx ,
178
171
) ;
@@ -186,26 +179,26 @@ class Dir {
186
179
}
187
180
188
181
readSync ( ) {
189
- if ( this [ kDirClosed ] === true ) {
182
+ if ( this . #closed === true ) {
190
183
throw new ERR_DIR_CLOSED ( ) ;
191
184
}
192
185
193
- if ( this [ kDirOperationQueue ] !== null ) {
186
+ if ( this . #operationQueue !== null ) {
194
187
throw new ERR_DIR_CONCURRENT_OPERATION ( ) ;
195
188
}
196
189
197
- if ( this [ kDirBufferedEntries ] . length > 0 ) {
198
- const dirent = ArrayPrototypeShift ( this [ kDirBufferedEntries ] ) ;
199
- if ( this [ kDirOptions ] . recursive && dirent . isDirectory ( ) ) {
190
+ if ( this . #bufferedEntries . length > 0 ) {
191
+ const dirent = ArrayPrototypeShift ( this . #bufferedEntries ) ;
192
+ if ( this . #options . recursive && dirent . isDirectory ( ) ) {
200
193
this . readSyncRecursive ( dirent ) ;
201
194
}
202
195
return dirent ;
203
196
}
204
197
205
- const ctx = { path : this [ kDirPath ] } ;
206
- const result = this [ kDirHandle ] . read (
207
- this [ kDirOptions ] . encoding ,
208
- this [ kDirOptions ] . bufferSize ,
198
+ const ctx = { path : this . #path } ;
199
+ const result = this . #handle . read (
200
+ this . #options . encoding ,
201
+ this . #options . bufferSize ,
209
202
undefined ,
210
203
ctx ,
211
204
) ;
@@ -215,10 +208,10 @@ class Dir {
215
208
return result ;
216
209
}
217
210
218
- this . processReadResult ( this [ kDirPath ] , result ) ;
211
+ this . processReadResult ( this . #path , result ) ;
219
212
220
- const dirent = ArrayPrototypeShift ( this [ kDirBufferedEntries ] ) ;
221
- if ( this [ kDirOptions ] . recursive && dirent . isDirectory ( ) ) {
213
+ const dirent = ArrayPrototypeShift ( this . #bufferedEntries ) ;
214
+ if ( this . #options . recursive && dirent . isDirectory ( ) ) {
222
215
this . readSyncRecursive ( dirent ) ;
223
216
}
224
217
return dirent ;
@@ -227,60 +220,60 @@ class Dir {
227
220
close ( callback ) {
228
221
// Promise
229
222
if ( callback === undefined ) {
230
- if ( this [ kDirClosed ] === true ) {
223
+ if ( this . #closed === true ) {
231
224
return PromiseReject ( new ERR_DIR_CLOSED ( ) ) ;
232
225
}
233
- return this [ kDirClosePromisified ] ( ) ;
226
+ return this . #closePromisified ( ) ;
234
227
}
235
228
236
229
// callback
237
230
validateFunction ( callback , 'callback' ) ;
238
231
239
- if ( this [ kDirClosed ] === true ) {
232
+ if ( this . #closed === true ) {
240
233
process . nextTick ( callback , new ERR_DIR_CLOSED ( ) ) ;
241
234
return ;
242
235
}
243
236
244
- if ( this [ kDirOperationQueue ] !== null ) {
245
- ArrayPrototypePush ( this [ kDirOperationQueue ] , ( ) => {
237
+ if ( this . #operationQueue !== null ) {
238
+ ArrayPrototypePush ( this . #operationQueue , ( ) => {
246
239
this . close ( callback ) ;
247
240
} ) ;
248
241
return ;
249
242
}
250
243
251
- this [ kDirClosed ] = true ;
244
+ this . #closed = true ;
252
245
const req = new FSReqCallback ( ) ;
253
246
req . oncomplete = callback ;
254
- this [ kDirHandle ] . close ( req ) ;
247
+ this . #handle . close ( req ) ;
255
248
}
256
249
257
250
closeSync ( ) {
258
- if ( this [ kDirClosed ] === true ) {
251
+ if ( this . #closed === true ) {
259
252
throw new ERR_DIR_CLOSED ( ) ;
260
253
}
261
254
262
- if ( this [ kDirOperationQueue ] !== null ) {
255
+ if ( this . #operationQueue !== null ) {
263
256
throw new ERR_DIR_CONCURRENT_OPERATION ( ) ;
264
257
}
265
258
266
- this [ kDirClosed ] = true ;
267
- const ctx = { path : this [ kDirPath ] } ;
268
- const result = this [ kDirHandle ] . close ( undefined , ctx ) ;
259
+ this . #closed = true ;
260
+ const ctx = { path : this . #path } ;
261
+ const result = this . #handle . close ( undefined , ctx ) ;
269
262
handleErrorFromBinding ( ctx ) ;
270
263
return result ;
271
264
}
272
265
273
266
async * entries ( ) {
274
267
try {
275
268
while ( true ) {
276
- const result = await this [ kDirReadPromisified ] ( ) ;
269
+ const result = await this . #readPromisified ( ) ;
277
270
if ( result === null ) {
278
271
break ;
279
272
}
280
273
yield result ;
281
274
}
282
275
} finally {
283
- await this [ kDirClosePromisified ] ( ) ;
276
+ await this . #closePromisified ( ) ;
284
277
}
285
278
}
286
279
}
0 commit comments