Skip to content

Commit c141335

Browse files
author
Jungku Lee
authored
fs: use private fields instead of symbols for Dir
PR-URL: #51037 Refs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_properties Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 5e64483 commit c141335

File tree

1 file changed

+68
-75
lines changed

1 file changed

+68
-75
lines changed

lib/internal/fs/dir.js

+68-75
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const {
66
FunctionPrototypeBind,
77
ObjectDefineProperty,
88
PromiseReject,
9-
Symbol,
109
SymbolAsyncIterator,
1110
} = primordials;
1211

@@ -34,74 +33,68 @@ const {
3433
validateUint32,
3534
} = require('internal/validators');
3635

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-
4736
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+
4848
constructor(handle, path, options) {
4949
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 = {
6053
bufferSize: 32,
6154
...getOptions(options, {
6255
encoding: 'utf8',
6356
}),
6457
};
6558

66-
validateUint32(this[kDirOptions].bufferSize, 'options.bufferSize', true);
59+
validateUint32(this.#options.bufferSize, 'options.bufferSize', true);
6760

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(
7164
internalUtil.promisify(this.close), this);
7265
}
7366

7467
get path() {
75-
return this[kDirPath];
68+
return this.#path;
7669
}
7770

7871
read(callback) {
79-
return this[kDirReadImpl](true, callback);
72+
return this.#readImpl(true, callback);
8073
}
8174

82-
[kDirReadImpl](maybeSync, callback) {
83-
if (this[kDirClosed] === true) {
75+
#readImpl(maybeSync, callback) {
76+
if (this.#closed === true) {
8477
throw new ERR_DIR_CLOSED();
8578
}
8679

8780
if (callback === undefined) {
88-
return this[kDirReadPromisified]();
81+
return this.#readPromisified();
8982
}
9083

9184
validateFunction(callback, 'callback');
9285

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);
9689
});
9790
return;
9891
}
9992

100-
if (this[kDirBufferedEntries].length > 0) {
93+
if (this.#bufferedEntries.length > 0) {
10194
try {
102-
const dirent = ArrayPrototypeShift(this[kDirBufferedEntries]);
95+
const dirent = ArrayPrototypeShift(this.#bufferedEntries);
10396

104-
if (this[kDirOptions].recursive && dirent.isDirectory()) {
97+
if (this.#options.recursive && dirent.isDirectory()) {
10598
this.readSyncRecursive(dirent);
10699
}
107100

@@ -118,8 +111,8 @@ class Dir {
118111
const req = new FSReqCallback();
119112
req.oncomplete = (err, result) => {
120113
process.nextTick(() => {
121-
const queue = this[kDirOperationQueue];
122-
this[kDirOperationQueue] = null;
114+
const queue = this.#operationQueue;
115+
this.#operationQueue = null;
123116
for (const op of queue) op();
124117
});
125118

@@ -128,9 +121,9 @@ class Dir {
128121
}
129122

130123
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()) {
134127
this.readSyncRecursive(dirent);
135128
}
136129
callback(null, dirent);
@@ -139,18 +132,18 @@ class Dir {
139132
}
140133
};
141134

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,
146139
req,
147140
);
148141
}
149142

150143
processReadResult(path, result) {
151144
for (let i = 0; i < result.length; i += 2) {
152145
ArrayPrototypePush(
153-
this[kDirBufferedEntries],
146+
this.#bufferedEntries,
154147
getDirent(
155148
path,
156149
result[i],
@@ -165,14 +158,14 @@ class Dir {
165158
const ctx = { path };
166159
const handle = dirBinding.opendir(
167160
pathModule.toNamespacedPath(path),
168-
this[kDirOptions].encoding,
161+
this.#options.encoding,
169162
undefined,
170163
ctx,
171164
);
172165
handleErrorFromBinding(ctx);
173166
const result = handle.read(
174-
this[kDirOptions].encoding,
175-
this[kDirOptions].bufferSize,
167+
this.#options.encoding,
168+
this.#options.bufferSize,
176169
undefined,
177170
ctx,
178171
);
@@ -186,26 +179,26 @@ class Dir {
186179
}
187180

188181
readSync() {
189-
if (this[kDirClosed] === true) {
182+
if (this.#closed === true) {
190183
throw new ERR_DIR_CLOSED();
191184
}
192185

193-
if (this[kDirOperationQueue] !== null) {
186+
if (this.#operationQueue !== null) {
194187
throw new ERR_DIR_CONCURRENT_OPERATION();
195188
}
196189

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()) {
200193
this.readSyncRecursive(dirent);
201194
}
202195
return dirent;
203196
}
204197

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,
209202
undefined,
210203
ctx,
211204
);
@@ -215,10 +208,10 @@ class Dir {
215208
return result;
216209
}
217210

218-
this.processReadResult(this[kDirPath], result);
211+
this.processReadResult(this.#path, result);
219212

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()) {
222215
this.readSyncRecursive(dirent);
223216
}
224217
return dirent;
@@ -227,60 +220,60 @@ class Dir {
227220
close(callback) {
228221
// Promise
229222
if (callback === undefined) {
230-
if (this[kDirClosed] === true) {
223+
if (this.#closed === true) {
231224
return PromiseReject(new ERR_DIR_CLOSED());
232225
}
233-
return this[kDirClosePromisified]();
226+
return this.#closePromisified();
234227
}
235228

236229
// callback
237230
validateFunction(callback, 'callback');
238231

239-
if (this[kDirClosed] === true) {
232+
if (this.#closed === true) {
240233
process.nextTick(callback, new ERR_DIR_CLOSED());
241234
return;
242235
}
243236

244-
if (this[kDirOperationQueue] !== null) {
245-
ArrayPrototypePush(this[kDirOperationQueue], () => {
237+
if (this.#operationQueue !== null) {
238+
ArrayPrototypePush(this.#operationQueue, () => {
246239
this.close(callback);
247240
});
248241
return;
249242
}
250243

251-
this[kDirClosed] = true;
244+
this.#closed = true;
252245
const req = new FSReqCallback();
253246
req.oncomplete = callback;
254-
this[kDirHandle].close(req);
247+
this.#handle.close(req);
255248
}
256249

257250
closeSync() {
258-
if (this[kDirClosed] === true) {
251+
if (this.#closed === true) {
259252
throw new ERR_DIR_CLOSED();
260253
}
261254

262-
if (this[kDirOperationQueue] !== null) {
255+
if (this.#operationQueue !== null) {
263256
throw new ERR_DIR_CONCURRENT_OPERATION();
264257
}
265258

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);
269262
handleErrorFromBinding(ctx);
270263
return result;
271264
}
272265

273266
async* entries() {
274267
try {
275268
while (true) {
276-
const result = await this[kDirReadPromisified]();
269+
const result = await this.#readPromisified();
277270
if (result === null) {
278271
break;
279272
}
280273
yield result;
281274
}
282275
} finally {
283-
await this[kDirClosePromisified]();
276+
await this.#closePromisified();
284277
}
285278
}
286279
}

0 commit comments

Comments
 (0)