Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: refactor to use private fields #36443

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 64 additions & 71 deletions lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
FunctionPrototypeBind,
ObjectDefineProperty,
PromiseReject,
Symbol,
SymbolAsyncIterator,
} = primordials;

Expand Down Expand Up @@ -35,122 +34,116 @@ const {
validateUint32
} = require('internal/validators');

const kDirHandle = Symbol('kDirHandle');
const kDirPath = Symbol('kDirPath');
const kDirBufferedEntries = Symbol('kDirBufferedEntries');
const kDirClosed = Symbol('kDirClosed');
const kDirOptions = Symbol('kDirOptions');
const kDirReadImpl = Symbol('kDirReadImpl');
const kDirReadPromisified = Symbol('kDirReadPromisified');
const kDirClosePromisified = Symbol('kDirClosePromisified');
const kDirOperationQueue = Symbol('kDirOperationQueue');

class Dir {
#handle = undefined;
#path = undefined;
#options = undefined;
#bufferedEntries = [];
#dirClosed = false;
// Either `null` or an Array of pending operations (= functions to be called
// once the current operation is done).
#operationQueue = null;
#readPromisified = undefined;
#closePromisified = undefined;

constructor(handle, path, options) {
if (handle == null) throw new ERR_MISSING_ARGS('handle');
this[kDirHandle] = handle;
this[kDirBufferedEntries] = [];
this[kDirPath] = path;
this[kDirClosed] = false;

// Either `null` or an Array of pending operations (= functions to be called
// once the current operation is done).
this[kDirOperationQueue] = null;

this[kDirOptions] = {
this.#handle = handle;
this.#path = path;
this.#options = {
bufferSize: 32,
...getOptions(options, {
encoding: 'utf8'
})
};

validateUint32(this[kDirOptions].bufferSize, 'options.bufferSize', true);

this[kDirReadPromisified] = FunctionPrototypeBind(
internalUtil.promisify(this[kDirReadImpl]), this, false);
this[kDirClosePromisified] = FunctionPrototypeBind(
this.#readPromisified = FunctionPrototypeBind(
internalUtil.promisify(this.#readImpl), this, false);
this.#closePromisified = this.#closePromisified = FunctionPrototypeBind(
internalUtil.promisify(this.close), this);

validateUint32(this.#options.bufferSize, 'options.bufferSize', true);
}

get path() {
return this[kDirPath];
return this.#path;
}

read(callback) {
return this[kDirReadImpl](true, callback);
return this.#readImpl(true, callback);
}

[kDirReadImpl](maybeSync, callback) {
if (this[kDirClosed] === true) {
#readImpl(maybeSync, callback) {
if (this.#dirClosed === true) {
throw new ERR_DIR_CLOSED();
}

if (callback === undefined) {
return this[kDirReadPromisified]();
return this.#readPromisified();
} else if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}

if (this[kDirOperationQueue] !== null) {
ArrayPrototypePush(this[kDirOperationQueue], () => {
this[kDirReadImpl](maybeSync, callback);
if (this.#operationQueue !== null) {
ArrayPrototypePush(this.#operationQueue, () => {
this.#readImpl(maybeSync, callback);
});
return;
}

if (this[kDirBufferedEntries].length > 0) {
if (this.#bufferedEntries.length > 0) {
const [ name, type ] =
ArrayPrototypeSplice(this[kDirBufferedEntries], 0, 2);
ArrayPrototypeSplice(this.#bufferedEntries, 0, 2);
if (maybeSync)
process.nextTick(getDirent, this[kDirPath], name, type, callback);
process.nextTick(getDirent, this.#path, name, type, callback);
else
getDirent(this[kDirPath], name, type, callback);
getDirent(this.#path, name, type, callback);
return;
}

const req = new FSReqCallback();
req.oncomplete = (err, result) => {
process.nextTick(() => {
const queue = this[kDirOperationQueue];
this[kDirOperationQueue] = null;
const queue = this.#operationQueue;
this.#operationQueue = null;
for (const op of queue) op();
});

if (err || result === null) {
return callback(err, result);
}

this[kDirBufferedEntries] = ArrayPrototypeSlice(result, 2);
getDirent(this[kDirPath], result[0], result[1], callback);
this.#bufferedEntries = ArrayPrototypeSlice(result, 2);
getDirent(this.#path, result[0], result[1], callback);
};

this[kDirOperationQueue] = [];
this[kDirHandle].read(
this[kDirOptions].encoding,
this[kDirOptions].bufferSize,
this.#operationQueue = [];
this.#handle.read(
this.#options.encoding,
this.#options.bufferSize,
req
);
}

readSync() {
if (this[kDirClosed] === true) {
if (this.#dirClosed === true) {
throw new ERR_DIR_CLOSED();
}

if (this[kDirOperationQueue] !== null) {
if (this.#operationQueue !== null) {
throw new ERR_DIR_CONCURRENT_OPERATION();
}

if (this[kDirBufferedEntries].length > 0) {
if (this.#bufferedEntries.length > 0) {
const [ name, type ] =
ArrayPrototypeSplice(this[kDirBufferedEntries], 0, 2);
return getDirent(this[kDirPath], name, type);
ArrayPrototypeSplice(this.#bufferedEntries, 0, 2);
return getDirent(this.#path, name, type);
}

const ctx = { path: this[kDirPath] };
const result = this[kDirHandle].read(
this[kDirOptions].encoding,
this[kDirOptions].bufferSize,
const ctx = { path: this.#path };
const result = this.#handle.read(
this.#options.encoding,
this.#options.bufferSize,
undefined,
ctx
);
Expand All @@ -160,69 +153,69 @@ class Dir {
return result;
}

this[kDirBufferedEntries] = ArrayPrototypeSlice(result, 2);
return getDirent(this[kDirPath], result[0], result[1]);
this.#bufferedEntries = ArrayPrototypeSlice(result, 2);
return getDirent(this.#path, result[0], result[1]);
}

close(callback) {
// Promise
if (callback === undefined) {
if (this[kDirClosed] === true) {
if (this.#dirClosed === true) {
return PromiseReject(new ERR_DIR_CLOSED());
}
return this[kDirClosePromisified]();
return this.#closePromisified();
}

// callback
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}

if (this[kDirClosed] === true) {
if (this.#dirClosed === true) {
process.nextTick(callback, new ERR_DIR_CLOSED());
return;
}

if (this[kDirOperationQueue] !== null) {
this[kDirOperationQueue].push(() => {
if (this.#operationQueue !== null) {
this.#operationQueue.push(() => {
this.close(callback);
});
return;
}

this[kDirClosed] = true;
this.#dirClosed = true;
const req = new FSReqCallback();
req.oncomplete = callback;
this[kDirHandle].close(req);
this.#handle.close(req);
}

closeSync() {
if (this[kDirClosed] === true) {
if (this.#dirClosed === true) {
throw new ERR_DIR_CLOSED();
}

if (this[kDirOperationQueue] !== null) {
if (this.#operationQueue !== null) {
throw new ERR_DIR_CONCURRENT_OPERATION();
}

this[kDirClosed] = true;
const ctx = { path: this[kDirPath] };
const result = this[kDirHandle].close(undefined, ctx);
this.#dirClosed = true;
const ctx = { path: this.#path };
const result = this.#handle.close(undefined, ctx);
handleErrorFromBinding(ctx);
return result;
}

async* entries() {
try {
while (true) {
const result = await this[kDirReadPromisified]();
const result = await this.#readPromisified();
if (result === null) {
break;
}
yield result;
}
} finally {
await this[kDirClosePromisified]();
await this.#closePromisified();
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const {
validateUint32
} = require('internal/validators');
const pathModule = require('path');
const kType = Symbol('type');
const kStats = Symbol('stats');
const assert = require('internal/assert');

Expand Down Expand Up @@ -135,37 +134,38 @@ function assertEncoding(encoding) {
}

class Dirent {
#type = undefined;
constructor(name, type) {
this.name = name;
this[kType] = type;
this.#type = type;
}

isDirectory() {
return this[kType] === UV_DIRENT_DIR;
return this.#type === UV_DIRENT_DIR;
}

isFile() {
return this[kType] === UV_DIRENT_FILE;
return this.#type === UV_DIRENT_FILE;
}

isBlockDevice() {
return this[kType] === UV_DIRENT_BLOCK;
return this.#type === UV_DIRENT_BLOCK;
}

isCharacterDevice() {
return this[kType] === UV_DIRENT_CHAR;
return this.#type === UV_DIRENT_CHAR;
}

isSymbolicLink() {
return this[kType] === UV_DIRENT_LINK;
return this.#type === UV_DIRENT_LINK;
}

isFIFO() {
return this[kType] === UV_DIRENT_FIFO;
return this.#type === UV_DIRENT_FIFO;
}

isSocket() {
return this[kType] === UV_DIRENT_SOCKET;
return this.#type === UV_DIRENT_SOCKET;
}
}

Expand Down