Skip to content

Commit

Permalink
fs: use kEmptyObject
Browse files Browse the repository at this point in the history
PR-URL: #43159
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
LiviaMedeiros authored and targos committed Jul 31, 2022
1 parent 835056c commit ca4157e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 44 deletions.
58 changes: 31 additions & 27 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ const {

const { FSReqCallback } = binding;
const { toPathIfFileURL } = require('internal/url');
const internalUtil = require('internal/util');
const {
customPromisifyArgs: kCustomPromisifyArgsSymbol,
kEmptyObject,
promisify: {
custom: kCustomPromisifiedSymbol,
},
} = require('internal/util');
const {
constants: {
kIoMaxLength,
Expand Down Expand Up @@ -272,7 +278,7 @@ function exists(path, callback) {
}
}

ObjectDefineProperty(exists, internalUtil.promisify.custom, {
ObjectDefineProperty(exists, kCustomPromisifiedSymbol, {
__proto__: null,
value: function exists(path) { // eslint-disable-line func-name-matching
return new Promise((resolve) => fs.exists(path, resolve));
Expand Down Expand Up @@ -619,7 +625,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
if (!isArrayBufferView(buffer)) {
// This is fs.read(fd, params, callback)
params = buffer;
({ buffer = Buffer.alloc(16384) } = params ?? ObjectCreate(null));
({ buffer = Buffer.alloc(16384) } = params ?? kEmptyObject);
}
callback = offsetOrOptions;
} else {
Expand All @@ -632,7 +638,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
offset = 0,
length = buffer.byteLength - offset,
position = null,
} = params ?? ObjectCreate(null));
} = params ?? kEmptyObject);
}

validateBuffer(buffer);
Expand Down Expand Up @@ -675,7 +681,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
binding.read(fd, buffer, offset, length, position, req);
}

ObjectDefineProperty(read, internalUtil.customPromisifyArgs,
ObjectDefineProperty(read, kCustomPromisifyArgsSymbol,
{ __proto__: null, value: ['bytesRead', 'buffer'], enumerable: false });

/**
Expand All @@ -697,7 +703,7 @@ function readSync(fd, buffer, offset, length, position) {

if (arguments.length <= 3) {
// Assume fs.readSync(fd, buffer, options)
const options = offset || ObjectCreate(null);
const options = offset || kEmptyObject;

({
offset = 0,
Expand Down Expand Up @@ -768,7 +774,7 @@ function readv(fd, buffers, position, callback) {
return binding.readBuffers(fd, buffers, position, req);
}

ObjectDefineProperty(readv, internalUtil.customPromisifyArgs,
ObjectDefineProperty(readv, kCustomPromisifyArgsSymbol,
{ __proto__: null, value: ['bytesRead', 'buffers'], enumerable: false });

/**
Expand Down Expand Up @@ -825,7 +831,7 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) {
offset = 0,
length = buffer.byteLength - offset,
position = null,
} = offsetOrOptions ?? ObjectCreate(null));
} = offsetOrOptions ?? kEmptyObject);
}

if (offset == null || typeof offset === 'function') {
Expand Down Expand Up @@ -865,7 +871,7 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) {
return binding.writeString(fd, str, offset, length, req);
}

ObjectDefineProperty(write, internalUtil.customPromisifyArgs,
ObjectDefineProperty(write, kCustomPromisifyArgsSymbol,
{ __proto__: null, value: ['bytesWritten', 'buffer'], enumerable: false });

/**
Expand All @@ -892,7 +898,7 @@ function writeSync(fd, buffer, offsetOrOptions, length, position) {
offset = 0,
length = buffer.byteLength - offset,
position = null,
} = offsetOrOptions ?? ObjectCreate(null));
} = offsetOrOptions ?? kEmptyObject);
}
if (position === undefined)
position = null;
Expand Down Expand Up @@ -955,7 +961,7 @@ function writev(fd, buffers, position, callback) {
return binding.writeBuffers(fd, buffers, position, req);
}

ObjectDefineProperty(writev, internalUtil.customPromisifyArgs, {
ObjectDefineProperty(writev, kCustomPromisifyArgsSymbol, {
__proto__: null,
value: ['bytesWritten', 'buffer'],
enumerable: false
Expand Down Expand Up @@ -1398,7 +1404,7 @@ function mkdirSync(path, options) {
*/
function readdir(path, options, callback) {
callback = makeCallback(typeof options === 'function' ? options : callback);
options = getOptions(options, {});
options = getOptions(options);
path = getValidatedPath(path);

const req = new FSReqCallback();
Expand Down Expand Up @@ -1427,7 +1433,7 @@ function readdir(path, options, callback) {
* @returns {string | Buffer[] | Dirent[]}
*/
function readdirSync(path, options) {
options = getOptions(options, {});
options = getOptions(options);
path = getValidatedPath(path);
const ctx = { path };
const result = binding.readdir(pathModule.toNamespacedPath(path),
Expand All @@ -1451,7 +1457,7 @@ function readdirSync(path, options) {
function fstat(fd, options = { bigint: false }, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
options = kEmptyObject;
}
fd = getValidatedFd(fd);
callback = makeStatsCallback(callback);
Expand All @@ -1475,7 +1481,7 @@ function fstat(fd, options = { bigint: false }, callback) {
function lstat(path, options = { bigint: false }, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
options = kEmptyObject;
}
callback = makeStatsCallback(callback);
path = getValidatedPath(path);
Expand All @@ -1498,7 +1504,7 @@ function lstat(path, options = { bigint: false }, callback) {
function stat(path, options = { bigint: false }, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
options = kEmptyObject;
}
callback = makeStatsCallback(callback);
path = getValidatedPath(path);
Expand Down Expand Up @@ -1596,7 +1602,7 @@ function statSync(path, options = { bigint: false, throwIfNoEntry: true }) {
*/
function readlink(path, options, callback) {
callback = makeCallback(typeof options === 'function' ? options : callback);
options = getOptions(options, {});
options = getOptions(options);
path = getValidatedPath(path, 'oldPath');
const req = new FSReqCallback();
req.oncomplete = callback;
Expand All @@ -1611,7 +1617,7 @@ function readlink(path, options, callback) {
* @returns {string | Buffer}
*/
function readlinkSync(path, options) {
options = getOptions(options, {});
options = getOptions(options);
path = getValidatedPath(path, 'oldPath');
const ctx = { path };
const result = binding.readlink(pathModule.toNamespacedPath(path),
Expand Down Expand Up @@ -2282,7 +2288,7 @@ function watch(filename, options, listener) {
if (typeof options === 'function') {
listener = options;
}
options = getOptions(options, {});
options = getOptions(options);

// Don't make changes directly on options object
options = copyObject(options);
Expand Down Expand Up @@ -2445,16 +2451,14 @@ if (isWindows) {
};
}

const emptyObj = ObjectCreate(null);

/**
* Returns the resolved pathname.
* @param {string | Buffer | URL} p
* @param {string | { encoding?: string | null; }} [options]
* @returns {string | Buffer}
*/
function realpathSync(p, options) {
options = getOptions(options, emptyObj);
options = getOptions(options);
p = toPathIfFileURL(p);
if (typeof p !== 'string') {
p += '';
Expand Down Expand Up @@ -2591,7 +2595,7 @@ function realpathSync(p, options) {
* @returns {string | Buffer}
*/
realpathSync.native = (path, options) => {
options = getOptions(options, {});
options = getOptions(options);
path = getValidatedPath(path);
const ctx = { path };
const result = binding.realpath(path, options.encoding, undefined, ctx);
Expand All @@ -2612,7 +2616,7 @@ realpathSync.native = (path, options) => {
*/
function realpath(p, options, callback) {
callback = typeof options === 'function' ? options : maybeCallback(callback);
options = getOptions(options, {});
options = getOptions(options);
p = toPathIfFileURL(p);

if (typeof p !== 'string') {
Expand Down Expand Up @@ -2750,7 +2754,7 @@ function realpath(p, options, callback) {
*/
realpath.native = (path, options, callback) => {
callback = makeCallback(callback || options);
options = getOptions(options, {});
options = getOptions(options);
path = getValidatedPath(path);
const req = new FSReqCallback();
req.oncomplete = callback;
Expand All @@ -2769,7 +2773,7 @@ realpath.native = (path, options, callback) => {
*/
function mkdtemp(prefix, options, callback) {
callback = makeCallback(typeof options === 'function' ? options : callback);
options = getOptions(options, {});
options = getOptions(options);

validateString(prefix, 'prefix');
nullCheck(prefix, 'prefix');
Expand All @@ -2786,7 +2790,7 @@ function mkdtemp(prefix, options, callback) {
* @returns {string}
*/
function mkdtempSync(prefix, options) {
options = getOptions(options, {});
options = getOptions(options);

validateString(prefix, 'prefix');
nullCheck(prefix, 'prefix');
Expand Down
21 changes: 12 additions & 9 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const {
Error,
MathMax,
MathMin,
ObjectCreate,
NumberIsSafeInteger,
Promise,
PromisePrototypeThen,
Expand Down Expand Up @@ -78,7 +77,11 @@ const {
validateString,
} = require('internal/validators');
const pathModule = require('path');
const { lazyDOMException, promisify } = require('internal/util');
const {
kEmptyObject,
lazyDOMException,
promisify,
} = require('internal/util');
const { EventEmitterMixin } = require('internal/event_target');
const { watch } = require('internal/fs/watchers');
const { isIterable } = require('internal/streams/utils');
Expand Down Expand Up @@ -467,7 +470,7 @@ async function read(handle, bufferOrParams, offset, length, position) {
offset = 0,
length = buffer.byteLength - offset,
position = null,
} = bufferOrParams ?? ObjectCreate(null));
} = bufferOrParams ?? kEmptyObject);

validateBuffer(buffer);
}
Expand Down Expand Up @@ -530,7 +533,7 @@ async function write(handle, buffer, offsetOrOptions, length, position) {
offset = 0,
length = buffer.byteLength - offset,
position = null,
} = offsetOrOptions ?? ObjectCreate(null));
} = offsetOrOptions ?? kEmptyObject);
}

if (offset == null) {
Expand Down Expand Up @@ -626,7 +629,7 @@ async function mkdir(path, options) {
const {
recursive = false,
mode = 0o777
} = options || {};
} = options || kEmptyObject;
path = getValidatedPath(path);
validateBoolean(recursive, 'options.recursive');

Expand All @@ -636,7 +639,7 @@ async function mkdir(path, options) {
}

async function readdir(path, options) {
options = getOptions(options, {});
options = getOptions(options);
path = getValidatedPath(path);
const result = await binding.readdir(pathModule.toNamespacedPath(path),
options.encoding,
Expand All @@ -648,7 +651,7 @@ async function readdir(path, options) {
}

async function readlink(path, options) {
options = getOptions(options, {});
options = getOptions(options);
path = getValidatedPath(path, 'oldPath');
return binding.readlink(pathModule.toNamespacedPath(path),
options.encoding, kUsePromises);
Expand Down Expand Up @@ -760,13 +763,13 @@ async function lutimes(path, atime, mtime) {
}

async function realpath(path, options) {
options = getOptions(options, {});
options = getOptions(options);
path = getValidatedPath(path);
return binding.realpath(path, options.encoding, kUsePromises);
}

async function mkdtemp(prefix, options) {
options = getOptions(options, {});
options = getOptions(options);

validateString(prefix, 'prefix');
nullCheck(prefix);
Expand Down
9 changes: 6 additions & 3 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const {
ERR_OUT_OF_RANGE,
ERR_METHOD_NOT_IMPLEMENTED,
} = require('internal/errors').codes;
const { deprecate } = require('internal/util');
const {
deprecate,
kEmptyObject,
} = require('internal/util');
const {
validateFunction,
validateInteger,
Expand Down Expand Up @@ -150,7 +153,7 @@ function ReadStream(path, options) {
return new ReadStream(path, options);

// A little bit bigger buffer and water marks by default
options = copyObject(getOptions(options, {}));
options = copyObject(getOptions(options, kEmptyObject));
if (options.highWaterMark === undefined)
options.highWaterMark = 64 * 1024;

Expand Down Expand Up @@ -309,7 +312,7 @@ function WriteStream(path, options) {
if (!(this instanceof WriteStream))
return new WriteStream(path, options);

options = copyObject(getOptions(options, {}));
options = copyObject(getOptions(options, kEmptyObject));

// Only buffers are supported.
options.decodeStrings = true;
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/fs/sync_write_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ const {
ObjectSetPrototypeOf,
ReflectApply,
} = primordials;
const { kEmptyObject } = require('internal/util');

const { Writable } = require('stream');
const { closeSync, writeSync } = require('fs');

function SyncWriteStream(fd, options) {
ReflectApply(Writable, this, [{ autoDestroy: true }]);

options = options || {};
options = options || kEmptyObject;

this.fd = fd;
this.readable = false;
Expand Down
10 changes: 6 additions & 4 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ const {
isDate,
isBigUint64Array
} = require('internal/util/types');
const { once } = require('internal/util');
const {
kEmptyObject,
once,
} = require('internal/util');
const { toPathIfFileURL } = require('internal/url');
const {
validateAbortSignal,
Expand Down Expand Up @@ -312,9 +315,8 @@ function getDirent(path, name, type, callback) {
}
}

function getOptions(options, defaultOptions) {
if (options === null || options === undefined ||
typeof options === 'function') {
function getOptions(options, defaultOptions = kEmptyObject) {
if (options == null || typeof options === 'function') {
return defaultOptions;
}

Expand Down

0 comments on commit ca4157e

Please sign in to comment.