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 <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
LiviaMedeiros authored and danielleadams committed Jun 13, 2022
1 parent fda2105 commit 63bf49b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 45 deletions.
61 changes: 33 additions & 28 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ const {

const { FSReqCallback } = binding;
const { toPathIfFileURL } = require('internal/url');
const internalUtil = require('internal/util');
const {
customPromisifyArgs: kCustomPromisifyArgsSymbol,
deprecate,
kEmptyObject,
promisify: {
custom: kCustomPromisifiedSymbol,
},
} = require('internal/util');
const {
constants: {
kIoMaxLength,
Expand Down Expand Up @@ -164,7 +171,7 @@ const isWindows = process.platform === 'win32';
const isOSX = process.platform === 'darwin';


const showStringCoercionDeprecation = internalUtil.deprecate(
const showStringCoercionDeprecation = deprecate(
() => {},
'Implicit coercion of objects with own toString property is deprecated.',
'DEP0162'
Expand Down Expand Up @@ -276,7 +283,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 @@ -623,7 +630,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 @@ -636,7 +643,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 @@ -679,7 +686,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 @@ -701,7 +708,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 @@ -772,7 +779,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 @@ -829,7 +836,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 @@ -872,7 +879,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 @@ -899,7 +906,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 @@ -962,7 +969,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 @@ -1405,7 +1412,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 @@ -1434,7 +1441,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 @@ -1458,7 +1465,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 @@ -1482,7 +1489,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 @@ -1505,7 +1512,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 @@ -1603,7 +1610,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 @@ -1618,7 +1625,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 @@ -2295,7 +2302,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 @@ -2458,16 +2465,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 @@ -2604,7 +2609,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 @@ -2625,7 +2630,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 @@ -2763,7 +2768,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 @@ -2782,7 +2787,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 @@ -2799,7 +2804,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 @@ -81,7 +80,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 @@ -519,7 +522,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 @@ -582,7 +585,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 @@ -678,7 +681,7 @@ async function mkdir(path, options) {
const {
recursive = false,
mode = 0o777
} = options || {};
} = options || kEmptyObject;
path = getValidatedPath(path);
validateBoolean(recursive, 'options.recursive');

Expand All @@ -688,7 +691,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 @@ -700,7 +703,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 @@ -812,13 +815,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 @@ -147,7 +150,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 @@ -305,7 +308,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
Loading

0 comments on commit 63bf49b

Please sign in to comment.