Skip to content

Commit a3e6b51

Browse files
LiviaMedeirosguangwong
authored andcommitted
fs: use kEmptyObject
PR-URL: nodejs/node#43159 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 48de7fb commit a3e6b51

File tree

5 files changed

+57
-44
lines changed

5 files changed

+57
-44
lines changed

lib/fs.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,13 @@ const {
8383

8484
const { FSReqCallback } = binding;
8585
const { toPathIfFileURL } = require('internal/url');
86-
const internalUtil = require('internal/util');
86+
const {
87+
customPromisifyArgs: kCustomPromisifyArgsSymbol,
88+
kEmptyObject,
89+
promisify: {
90+
custom: kCustomPromisifiedSymbol,
91+
},
92+
} = require('internal/util');
8793
const {
8894
constants: {
8995
kIoMaxLength,
@@ -272,7 +278,7 @@ function exists(path, callback) {
272278
}
273279
}
274280

275-
ObjectDefineProperty(exists, internalUtil.promisify.custom, {
281+
ObjectDefineProperty(exists, kCustomPromisifiedSymbol, {
276282
__proto__: null,
277283
value: function exists(path) { // eslint-disable-line func-name-matching
278284
return new Promise((resolve) => fs.exists(path, resolve));
@@ -619,7 +625,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
619625
if (!isArrayBufferView(buffer)) {
620626
// This is fs.read(fd, params, callback)
621627
params = buffer;
622-
({ buffer = Buffer.alloc(16384) } = params ?? ObjectCreate(null));
628+
({ buffer = Buffer.alloc(16384) } = params ?? kEmptyObject);
623629
}
624630
callback = offsetOrOptions;
625631
} else {
@@ -632,7 +638,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
632638
offset = 0,
633639
length = buffer.byteLength - offset,
634640
position = null,
635-
} = params ?? ObjectCreate(null));
641+
} = params ?? kEmptyObject);
636642
}
637643

638644
validateBuffer(buffer);
@@ -675,7 +681,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
675681
binding.read(fd, buffer, offset, length, position, req);
676682
}
677683

678-
ObjectDefineProperty(read, internalUtil.customPromisifyArgs,
684+
ObjectDefineProperty(read, kCustomPromisifyArgsSymbol,
679685
{ __proto__: null, value: ['bytesRead', 'buffer'], enumerable: false });
680686

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

698704
if (arguments.length <= 3) {
699705
// Assume fs.readSync(fd, buffer, options)
700-
const options = offset || ObjectCreate(null);
706+
const options = offset || kEmptyObject;
701707

702708
({
703709
offset = 0,
@@ -768,7 +774,7 @@ function readv(fd, buffers, position, callback) {
768774
return binding.readBuffers(fd, buffers, position, req);
769775
}
770776

771-
ObjectDefineProperty(readv, internalUtil.customPromisifyArgs,
777+
ObjectDefineProperty(readv, kCustomPromisifyArgsSymbol,
772778
{ __proto__: null, value: ['bytesRead', 'buffers'], enumerable: false });
773779

774780
/**
@@ -825,7 +831,7 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) {
825831
offset = 0,
826832
length = buffer.byteLength - offset,
827833
position = null,
828-
} = offsetOrOptions ?? ObjectCreate(null));
834+
} = offsetOrOptions ?? kEmptyObject);
829835
}
830836

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

868-
ObjectDefineProperty(write, internalUtil.customPromisifyArgs,
874+
ObjectDefineProperty(write, kCustomPromisifyArgsSymbol,
869875
{ __proto__: null, value: ['bytesWritten', 'buffer'], enumerable: false });
870876

871877
/**
@@ -892,7 +898,7 @@ function writeSync(fd, buffer, offsetOrOptions, length, position) {
892898
offset = 0,
893899
length = buffer.byteLength - offset,
894900
position = null,
895-
} = offsetOrOptions ?? ObjectCreate(null));
901+
} = offsetOrOptions ?? kEmptyObject);
896902
}
897903
if (position === undefined)
898904
position = null;
@@ -955,7 +961,7 @@ function writev(fd, buffers, position, callback) {
955961
return binding.writeBuffers(fd, buffers, position, req);
956962
}
957963

958-
ObjectDefineProperty(writev, internalUtil.customPromisifyArgs, {
964+
ObjectDefineProperty(writev, kCustomPromisifyArgsSymbol, {
959965
__proto__: null,
960966
value: ['bytesWritten', 'buffer'],
961967
enumerable: false
@@ -1398,7 +1404,7 @@ function mkdirSync(path, options) {
13981404
*/
13991405
function readdir(path, options, callback) {
14001406
callback = makeCallback(typeof options === 'function' ? options : callback);
1401-
options = getOptions(options, {});
1407+
options = getOptions(options);
14021408
path = getValidatedPath(path);
14031409

14041410
const req = new FSReqCallback();
@@ -1427,7 +1433,7 @@ function readdir(path, options, callback) {
14271433
* @returns {string | Buffer[] | Dirent[]}
14281434
*/
14291435
function readdirSync(path, options) {
1430-
options = getOptions(options, {});
1436+
options = getOptions(options);
14311437
path = getValidatedPath(path);
14321438
const ctx = { path };
14331439
const result = binding.readdir(pathModule.toNamespacedPath(path),
@@ -1451,7 +1457,7 @@ function readdirSync(path, options) {
14511457
function fstat(fd, options = { bigint: false }, callback) {
14521458
if (typeof options === 'function') {
14531459
callback = options;
1454-
options = {};
1460+
options = kEmptyObject;
14551461
}
14561462
fd = getValidatedFd(fd);
14571463
callback = makeStatsCallback(callback);
@@ -1475,7 +1481,7 @@ function fstat(fd, options = { bigint: false }, callback) {
14751481
function lstat(path, options = { bigint: false }, callback) {
14761482
if (typeof options === 'function') {
14771483
callback = options;
1478-
options = {};
1484+
options = kEmptyObject;
14791485
}
14801486
callback = makeStatsCallback(callback);
14811487
path = getValidatedPath(path);
@@ -1498,7 +1504,7 @@ function lstat(path, options = { bigint: false }, callback) {
14981504
function stat(path, options = { bigint: false }, callback) {
14991505
if (typeof options === 'function') {
15001506
callback = options;
1501-
options = {};
1507+
options = kEmptyObject;
15021508
}
15031509
callback = makeStatsCallback(callback);
15041510
path = getValidatedPath(path);
@@ -1596,7 +1602,7 @@ function statSync(path, options = { bigint: false, throwIfNoEntry: true }) {
15961602
*/
15971603
function readlink(path, options, callback) {
15981604
callback = makeCallback(typeof options === 'function' ? options : callback);
1599-
options = getOptions(options, {});
1605+
options = getOptions(options);
16001606
path = getValidatedPath(path, 'oldPath');
16011607
const req = new FSReqCallback();
16021608
req.oncomplete = callback;
@@ -1611,7 +1617,7 @@ function readlink(path, options, callback) {
16111617
* @returns {string | Buffer}
16121618
*/
16131619
function readlinkSync(path, options) {
1614-
options = getOptions(options, {});
1620+
options = getOptions(options);
16151621
path = getValidatedPath(path, 'oldPath');
16161622
const ctx = { path };
16171623
const result = binding.readlink(pathModule.toNamespacedPath(path),
@@ -2282,7 +2288,7 @@ function watch(filename, options, listener) {
22822288
if (typeof options === 'function') {
22832289
listener = options;
22842290
}
2285-
options = getOptions(options, {});
2291+
options = getOptions(options);
22862292

22872293
// Don't make changes directly on options object
22882294
options = copyObject(options);
@@ -2445,16 +2451,14 @@ if (isWindows) {
24452451
};
24462452
}
24472453

2448-
const emptyObj = ObjectCreate(null);
2449-
24502454
/**
24512455
* Returns the resolved pathname.
24522456
* @param {string | Buffer | URL} p
24532457
* @param {string | { encoding?: string | null; }} [options]
24542458
* @returns {string | Buffer}
24552459
*/
24562460
function realpathSync(p, options) {
2457-
options = getOptions(options, emptyObj);
2461+
options = getOptions(options);
24582462
p = toPathIfFileURL(p);
24592463
if (typeof p !== 'string') {
24602464
p += '';
@@ -2591,7 +2595,7 @@ function realpathSync(p, options) {
25912595
* @returns {string | Buffer}
25922596
*/
25932597
realpathSync.native = (path, options) => {
2594-
options = getOptions(options, {});
2598+
options = getOptions(options);
25952599
path = getValidatedPath(path);
25962600
const ctx = { path };
25972601
const result = binding.realpath(path, options.encoding, undefined, ctx);
@@ -2612,7 +2616,7 @@ realpathSync.native = (path, options) => {
26122616
*/
26132617
function realpath(p, options, callback) {
26142618
callback = typeof options === 'function' ? options : maybeCallback(callback);
2615-
options = getOptions(options, {});
2619+
options = getOptions(options);
26162620
p = toPathIfFileURL(p);
26172621

26182622
if (typeof p !== 'string') {
@@ -2750,7 +2754,7 @@ function realpath(p, options, callback) {
27502754
*/
27512755
realpath.native = (path, options, callback) => {
27522756
callback = makeCallback(callback || options);
2753-
options = getOptions(options, {});
2757+
options = getOptions(options);
27542758
path = getValidatedPath(path);
27552759
const req = new FSReqCallback();
27562760
req.oncomplete = callback;
@@ -2769,7 +2773,7 @@ realpath.native = (path, options, callback) => {
27692773
*/
27702774
function mkdtemp(prefix, options, callback) {
27712775
callback = makeCallback(typeof options === 'function' ? options : callback);
2772-
options = getOptions(options, {});
2776+
options = getOptions(options);
27732777

27742778
validateString(prefix, 'prefix');
27752779
nullCheck(prefix, 'prefix');
@@ -2786,7 +2790,7 @@ function mkdtemp(prefix, options, callback) {
27862790
* @returns {string}
27872791
*/
27882792
function mkdtempSync(prefix, options) {
2789-
options = getOptions(options, {});
2793+
options = getOptions(options);
27902794

27912795
validateString(prefix, 'prefix');
27922796
nullCheck(prefix, 'prefix');

lib/internal/fs/promises.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const {
55
Error,
66
MathMax,
77
MathMin,
8-
ObjectCreate,
98
NumberIsSafeInteger,
109
Promise,
1110
PromisePrototypeThen,
@@ -78,7 +77,11 @@ const {
7877
validateString,
7978
} = require('internal/validators');
8079
const pathModule = require('path');
81-
const { lazyDOMException, promisify } = require('internal/util');
80+
const {
81+
kEmptyObject,
82+
lazyDOMException,
83+
promisify,
84+
} = require('internal/util');
8285
const { EventEmitterMixin } = require('internal/event_target');
8386
const { watch } = require('internal/fs/watchers');
8487
const { isIterable } = require('internal/streams/utils');
@@ -467,7 +470,7 @@ async function read(handle, bufferOrParams, offset, length, position) {
467470
offset = 0,
468471
length = buffer.byteLength - offset,
469472
position = null,
470-
} = bufferOrParams ?? ObjectCreate(null));
473+
} = bufferOrParams ?? kEmptyObject);
471474

472475
validateBuffer(buffer);
473476
}
@@ -530,7 +533,7 @@ async function write(handle, buffer, offsetOrOptions, length, position) {
530533
offset = 0,
531534
length = buffer.byteLength - offset,
532535
position = null,
533-
} = offsetOrOptions ?? ObjectCreate(null));
536+
} = offsetOrOptions ?? kEmptyObject);
534537
}
535538

536539
if (offset == null) {
@@ -626,7 +629,7 @@ async function mkdir(path, options) {
626629
const {
627630
recursive = false,
628631
mode = 0o777
629-
} = options || {};
632+
} = options || kEmptyObject;
630633
path = getValidatedPath(path);
631634
validateBoolean(recursive, 'options.recursive');
632635

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

638641
async function readdir(path, options) {
639-
options = getOptions(options, {});
642+
options = getOptions(options);
640643
path = getValidatedPath(path);
641644
const result = await binding.readdir(pathModule.toNamespacedPath(path),
642645
options.encoding,
@@ -648,7 +651,7 @@ async function readdir(path, options) {
648651
}
649652

650653
async function readlink(path, options) {
651-
options = getOptions(options, {});
654+
options = getOptions(options);
652655
path = getValidatedPath(path, 'oldPath');
653656
return binding.readlink(pathModule.toNamespacedPath(path),
654657
options.encoding, kUsePromises);
@@ -760,13 +763,13 @@ async function lutimes(path, atime, mtime) {
760763
}
761764

762765
async function realpath(path, options) {
763-
options = getOptions(options, {});
766+
options = getOptions(options);
764767
path = getValidatedPath(path);
765768
return binding.realpath(path, options.encoding, kUsePromises);
766769
}
767770

768771
async function mkdtemp(prefix, options) {
769-
options = getOptions(options, {});
772+
options = getOptions(options);
770773

771774
validateString(prefix, 'prefix');
772775
nullCheck(prefix);

lib/internal/fs/streams.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ const {
1616
ERR_OUT_OF_RANGE,
1717
ERR_METHOD_NOT_IMPLEMENTED,
1818
} = require('internal/errors').codes;
19-
const { deprecate } = require('internal/util');
19+
const {
20+
deprecate,
21+
kEmptyObject,
22+
} = require('internal/util');
2023
const {
2124
validateFunction,
2225
validateInteger,
@@ -150,7 +153,7 @@ function ReadStream(path, options) {
150153
return new ReadStream(path, options);
151154

152155
// A little bit bigger buffer and water marks by default
153-
options = copyObject(getOptions(options, {}));
156+
options = copyObject(getOptions(options, kEmptyObject));
154157
if (options.highWaterMark === undefined)
155158
options.highWaterMark = 64 * 1024;
156159

@@ -309,7 +312,7 @@ function WriteStream(path, options) {
309312
if (!(this instanceof WriteStream))
310313
return new WriteStream(path, options);
311314

312-
options = copyObject(getOptions(options, {}));
315+
options = copyObject(getOptions(options, kEmptyObject));
313316

314317
// Only buffers are supported.
315318
options.decodeStrings = true;

lib/internal/fs/sync_write_stream.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ const {
44
ObjectSetPrototypeOf,
55
ReflectApply,
66
} = primordials;
7+
const { kEmptyObject } = require('internal/util');
78

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

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

14-
options = options || {};
15+
options = options || kEmptyObject;
1516

1617
this.fd = fd;
1718
this.readable = false;

lib/internal/fs/utils.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ const {
4343
isDate,
4444
isBigUint64Array
4545
} = require('internal/util/types');
46-
const { once } = require('internal/util');
46+
const {
47+
kEmptyObject,
48+
once,
49+
} = require('internal/util');
4750
const { toPathIfFileURL } = require('internal/url');
4851
const {
4952
validateAbortSignal,
@@ -312,9 +315,8 @@ function getDirent(path, name, type, callback) {
312315
}
313316
}
314317

315-
function getOptions(options, defaultOptions) {
316-
if (options === null || options === undefined ||
317-
typeof options === 'function') {
318+
function getOptions(options, defaultOptions = kEmptyObject) {
319+
if (options == null || typeof options === 'function') {
318320
return defaultOptions;
319321
}
320322

0 commit comments

Comments
 (0)