Skip to content

Commit

Permalink
lib: use kEmptyObject as default value for options
Browse files Browse the repository at this point in the history
`kEmptyObject` is more suitable than {} if options don't
need mutation.

PR-URL: #46011
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
deokjinkim authored and RafaelGSS committed Jan 4, 2023
1 parent ba8bdd4 commit 9b40bd9
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
5 changes: 3 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,9 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
* @param {{ signal: AbortSignal; }} [options]
* @returns {AsyncIterator}
*/
function on(emitter, event, options) {
const signal = options?.signal;
function on(emitter, event, options = kEmptyObject) {
// Parameters validation
const signal = options.signal;
validateAbortSignal(signal, 'options.signal');
if (signal?.aborted)
throw new AbortError(undefined, { cause: signal?.reason });
Expand Down
7 changes: 5 additions & 2 deletions lib/internal/fs/watchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const {
ERR_INVALID_ARG_VALUE,
},
} = require('internal/errors');
const { createDeferredPromise } = require('internal/util');
const {
createDeferredPromise,
kEmptyObject,
} = require('internal/util');

const {
kFsStatsFieldsNumber,
Expand Down Expand Up @@ -296,7 +299,7 @@ ObjectDefineProperty(FSEvent.prototype, 'owner', {
set(v) { return this[owner_symbol] = v; }
});

async function* watch(filename, options = {}) {
async function* watch(filename, options = kEmptyObject) {
const path = toNamespacedPath(getValidatedPath(filename));
validateObject(options, 'options');

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -3326,7 +3326,7 @@ function createSecureServer(options, handler) {
function createServer(options, handler) {
if (typeof options === 'function') {
handler = options;
options = {};
options = kEmptyObject;
}
return new Http2Server(options, handler);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/socketaddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {

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

const { inspect } = require('internal/util/inspect');
Expand All @@ -44,7 +45,7 @@ class SocketAddress extends JSTransferable {
return value?.[kHandle] !== undefined;
}

constructor(options = {}) {
constructor(options = kEmptyObject) {
super();
validateObject(options, 'options');
let { family = 'ipv4' } = options;
Expand Down
3 changes: 2 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const {
} = require('internal/errors');
const { isUint8Array } = require('internal/util/types');
const { queueMicrotask } = require('internal/process/task_queues');
const { kEmptyObject } = require('internal/util');
const {
validateAbortSignal,
validateBoolean,
Expand Down Expand Up @@ -1584,7 +1585,7 @@ function Server(options, connectionListener) {

if (typeof options === 'function') {
connectionListener = options;
options = {};
options = kEmptyObject;
this.on('connection', connectionListener);
} else if (options == null || typeof options === 'object') {
options = { ...options };
Expand Down

0 comments on commit 9b40bd9

Please sign in to comment.