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

lib: add internalUtil.kEmptyObject #43159

Merged
merged 20 commits into from
Jun 11, 2022
Merged
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
7 changes: 5 additions & 2 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ const {
ERR_OUT_OF_RANGE,
},
} = require('internal/errors');
const { once } = require('internal/util');
const {
kEmptyObject,
once,
} = require('internal/util');
const {
validateNumber,
validateOneOf,
Expand Down Expand Up @@ -220,7 +223,7 @@ Agent.defaultMaxSockets = Infinity;
Agent.prototype.createConnection = net.createConnection;

// Get the key for a given set of request options
Agent.prototype.getName = function getName(options = {}) {
Agent.prototype.getName = function getName(options = kEmptyObject) {
let name = options.host || 'localhost';

name += ':';
Expand Down
7 changes: 5 additions & 2 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ const {

const net = require('net');
const assert = require('internal/assert');
const { once } = require('internal/util');
const {
kEmptyObject,
once,
} = require('internal/util');
const {
_checkIsHttpToken: checkIsHttpToken,
freeParser,
Expand Down Expand Up @@ -133,7 +136,7 @@ function ClientRequest(input, options, cb) {

if (typeof options === 'function') {
cb = options;
options = input || {};
options = input || kEmptyObject;
} else {
options = ObjectAssign(input || {}, options);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const {
},
} = internalBinding('constants');

const {
kEmptyObject,
} = require('internal/util');

const {
validateInteger,
} = require('internal/validators');
Expand Down Expand Up @@ -93,7 +97,7 @@ function SecureContext(secureProtocol, secureOptions, minVersion, maxVersion) {
}

function createSecureContext(options) {
if (!options) options = {};
if (!options) options = kEmptyObject;

const {
honorCipherOrder,
Expand Down
7 changes: 4 additions & 3 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const {

const {
assertCrypto,
deprecate
deprecate,
kEmptyObject,
} = require('internal/util');

assertCrypto();
Expand Down Expand Up @@ -1182,9 +1183,9 @@ function Server(options, listener) {

if (typeof options === 'function') {
listener = options;
options = {};
options = kEmptyObject;
} else if (options == null || typeof options === 'object') {
options = options || {};
options = options ?? kEmptyObject;
} else {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
ERR_ASYNC_TYPE,
ERR_INVALID_ASYNC_ID
} = require('internal/errors').codes;
const { kEmptyObject } = require('internal/util');
const {
validateFunction,
validateString,
Expand Down Expand Up @@ -156,7 +157,7 @@ function createHook(fns) {
const destroyedSymbol = Symbol('destroyed');

class AsyncResource {
constructor(type, opts = {}) {
constructor(type, opts = kEmptyObject) {
validateString(type, 'type');

let triggerAsyncId = opts;
Expand Down
7 changes: 4 additions & 3 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ const {
} = primordials;

const {
promisify,
convertToValidSignal,
createDeferredPromise,
getSystemErrorName
getSystemErrorName,
kEmptyObject,
promisify,
} = require('internal/util');
const { isArrayBufferView } = require('internal/util/types');
let debug = require('internal/util/debuglog').debuglog(
Expand Down Expand Up @@ -510,7 +511,7 @@ function normalizeSpawnArguments(file, args, options) {
}

if (options === undefined)
options = {};
options = kEmptyObject;
else
validateObject(options, 'options');

Expand Down
5 changes: 4 additions & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const {
SymbolAsyncIterator,
} = primordials;
const kRejection = SymbolFor('nodejs.rejection');

const { kEmptyObject } = require('internal/util');

const { inspect } = require('internal/util/inspect');

let spliceOne;
Expand Down Expand Up @@ -945,7 +948,7 @@ function getEventListeners(emitterOrTarget, type) {
* @param {{ signal: AbortSignal; }} [options]
* @returns {Promise}
*/
async function once(emitter, name, options = {}) {
async function once(emitter, name, options = kEmptyObject) {
const signal = options?.signal;
validateAbortSignal(signal, 'options.signal');
if (signal?.aborted)
Expand Down
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
Loading