Skip to content

Commit 63bf49b

Browse files
LiviaMedeirosdanielleadams
authored andcommitted
fs: use kEmptyObject
PR-URL: #43159 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent fda2105 commit 63bf49b

File tree

5 files changed

+59
-45
lines changed

5 files changed

+59
-45
lines changed

lib/fs.js

+33-28
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ const {
8383

8484
const { FSReqCallback } = binding;
8585
const { toPathIfFileURL } = require('internal/url');
86-
const internalUtil = require('internal/util');
86+
const {
87+
customPromisifyArgs: kCustomPromisifyArgsSymbol,
88+
deprecate,
89+
kEmptyObject,
90+
promisify: {
91+
custom: kCustomPromisifiedSymbol,
92+
},
93+
} = require('internal/util');
8794
const {
8895
constants: {
8996
kIoMaxLength,
@@ -164,7 +171,7 @@ const isWindows = process.platform === 'win32';
164171
const isOSX = process.platform === 'darwin';
165172

166173

167-
const showStringCoercionDeprecation = internalUtil.deprecate(
174+
const showStringCoercionDeprecation = deprecate(
168175
() => {},
169176
'Implicit coercion of objects with own toString property is deprecated.',
170177
'DEP0162'
@@ -276,7 +283,7 @@ function exists(path, callback) {
276283
}
277284
}
278285

279-
ObjectDefineProperty(exists, internalUtil.promisify.custom, {
286+
ObjectDefineProperty(exists, kCustomPromisifiedSymbol, {
280287
__proto__: null,
281288
value: function exists(path) { // eslint-disable-line func-name-matching
282289
return new Promise((resolve) => fs.exists(path, resolve));
@@ -623,7 +630,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
623630
if (!isArrayBufferView(buffer)) {
624631
// This is fs.read(fd, params, callback)
625632
params = buffer;
626-
({ buffer = Buffer.alloc(16384) } = params ?? ObjectCreate(null));
633+
({ buffer = Buffer.alloc(16384) } = params ?? kEmptyObject);
627634
}
628635
callback = offsetOrOptions;
629636
} else {
@@ -636,7 +643,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
636643
offset = 0,
637644
length = buffer.byteLength - offset,
638645
position = null,
639-
} = params ?? ObjectCreate(null));
646+
} = params ?? kEmptyObject);
640647
}
641648

642649
validateBuffer(buffer);
@@ -679,7 +686,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
679686
binding.read(fd, buffer, offset, length, position, req);
680687
}
681688

682-
ObjectDefineProperty(read, internalUtil.customPromisifyArgs,
689+
ObjectDefineProperty(read, kCustomPromisifyArgsSymbol,
683690
{ __proto__: null, value: ['bytesRead', 'buffer'], enumerable: false });
684691

685692
/**
@@ -701,7 +708,7 @@ function readSync(fd, buffer, offset, length, position) {
701708

702709
if (arguments.length <= 3) {
703710
// Assume fs.readSync(fd, buffer, options)
704-
const options = offset || ObjectCreate(null);
711+
const options = offset || kEmptyObject;
705712

706713
({
707714
offset = 0,
@@ -772,7 +779,7 @@ function readv(fd, buffers, position, callback) {
772779
return binding.readBuffers(fd, buffers, position, req);
773780
}
774781

775-
ObjectDefineProperty(readv, internalUtil.customPromisifyArgs,
782+
ObjectDefineProperty(readv, kCustomPromisifyArgsSymbol,
776783
{ __proto__: null, value: ['bytesRead', 'buffers'], enumerable: false });
777784

778785
/**
@@ -829,7 +836,7 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) {
829836
offset = 0,
830837
length = buffer.byteLength - offset,
831838
position = null,
832-
} = offsetOrOptions ?? ObjectCreate(null));
839+
} = offsetOrOptions ?? kEmptyObject);
833840
}
834841

835842
if (offset == null || typeof offset === 'function') {
@@ -872,7 +879,7 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) {
872879
return binding.writeString(fd, str, offset, length, req);
873880
}
874881

875-
ObjectDefineProperty(write, internalUtil.customPromisifyArgs,
882+
ObjectDefineProperty(write, kCustomPromisifyArgsSymbol,
876883
{ __proto__: null, value: ['bytesWritten', 'buffer'], enumerable: false });
877884

878885
/**
@@ -899,7 +906,7 @@ function writeSync(fd, buffer, offsetOrOptions, length, position) {
899906
offset = 0,
900907
length = buffer.byteLength - offset,
901908
position = null,
902-
} = offsetOrOptions ?? ObjectCreate(null));
909+
} = offsetOrOptions ?? kEmptyObject);
903910
}
904911
if (position === undefined)
905912
position = null;
@@ -962,7 +969,7 @@ function writev(fd, buffers, position, callback) {
962969
return binding.writeBuffers(fd, buffers, position, req);
963970
}
964971

965-
ObjectDefineProperty(writev, internalUtil.customPromisifyArgs, {
972+
ObjectDefineProperty(writev, kCustomPromisifyArgsSymbol, {
966973
__proto__: null,
967974
value: ['bytesWritten', 'buffer'],
968975
enumerable: false
@@ -1405,7 +1412,7 @@ function mkdirSync(path, options) {
14051412
*/
14061413
function readdir(path, options, callback) {
14071414
callback = makeCallback(typeof options === 'function' ? options : callback);
1408-
options = getOptions(options, {});
1415+
options = getOptions(options);
14091416
path = getValidatedPath(path);
14101417

14111418
const req = new FSReqCallback();
@@ -1434,7 +1441,7 @@ function readdir(path, options, callback) {
14341441
* @returns {string | Buffer[] | Dirent[]}
14351442
*/
14361443
function readdirSync(path, options) {
1437-
options = getOptions(options, {});
1444+
options = getOptions(options);
14381445
path = getValidatedPath(path);
14391446
const ctx = { path };
14401447
const result = binding.readdir(pathModule.toNamespacedPath(path),
@@ -1458,7 +1465,7 @@ function readdirSync(path, options) {
14581465
function fstat(fd, options = { bigint: false }, callback) {
14591466
if (typeof options === 'function') {
14601467
callback = options;
1461-
options = {};
1468+
options = kEmptyObject;
14621469
}
14631470
fd = getValidatedFd(fd);
14641471
callback = makeStatsCallback(callback);
@@ -1482,7 +1489,7 @@ function fstat(fd, options = { bigint: false }, callback) {
14821489
function lstat(path, options = { bigint: false }, callback) {
14831490
if (typeof options === 'function') {
14841491
callback = options;
1485-
options = {};
1492+
options = kEmptyObject;
14861493
}
14871494
callback = makeStatsCallback(callback);
14881495
path = getValidatedPath(path);
@@ -1505,7 +1512,7 @@ function lstat(path, options = { bigint: false }, callback) {
15051512
function stat(path, options = { bigint: false }, callback) {
15061513
if (typeof options === 'function') {
15071514
callback = options;
1508-
options = {};
1515+
options = kEmptyObject;
15091516
}
15101517
callback = makeStatsCallback(callback);
15111518
path = getValidatedPath(path);
@@ -1603,7 +1610,7 @@ function statSync(path, options = { bigint: false, throwIfNoEntry: true }) {
16031610
*/
16041611
function readlink(path, options, callback) {
16051612
callback = makeCallback(typeof options === 'function' ? options : callback);
1606-
options = getOptions(options, {});
1613+
options = getOptions(options);
16071614
path = getValidatedPath(path, 'oldPath');
16081615
const req = new FSReqCallback();
16091616
req.oncomplete = callback;
@@ -1618,7 +1625,7 @@ function readlink(path, options, callback) {
16181625
* @returns {string | Buffer}
16191626
*/
16201627
function readlinkSync(path, options) {
1621-
options = getOptions(options, {});
1628+
options = getOptions(options);
16221629
path = getValidatedPath(path, 'oldPath');
16231630
const ctx = { path };
16241631
const result = binding.readlink(pathModule.toNamespacedPath(path),
@@ -2295,7 +2302,7 @@ function watch(filename, options, listener) {
22952302
if (typeof options === 'function') {
22962303
listener = options;
22972304
}
2298-
options = getOptions(options, {});
2305+
options = getOptions(options);
22992306

23002307
// Don't make changes directly on options object
23012308
options = copyObject(options);
@@ -2458,16 +2465,14 @@ if (isWindows) {
24582465
};
24592466
}
24602467

2461-
const emptyObj = ObjectCreate(null);
2462-
24632468
/**
24642469
* Returns the resolved pathname.
24652470
* @param {string | Buffer | URL} p
24662471
* @param {string | { encoding?: string | null; }} [options]
24672472
* @returns {string | Buffer}
24682473
*/
24692474
function realpathSync(p, options) {
2470-
options = getOptions(options, emptyObj);
2475+
options = getOptions(options);
24712476
p = toPathIfFileURL(p);
24722477
if (typeof p !== 'string') {
24732478
p += '';
@@ -2604,7 +2609,7 @@ function realpathSync(p, options) {
26042609
* @returns {string | Buffer}
26052610
*/
26062611
realpathSync.native = (path, options) => {
2607-
options = getOptions(options, {});
2612+
options = getOptions(options);
26082613
path = getValidatedPath(path);
26092614
const ctx = { path };
26102615
const result = binding.realpath(path, options.encoding, undefined, ctx);
@@ -2625,7 +2630,7 @@ realpathSync.native = (path, options) => {
26252630
*/
26262631
function realpath(p, options, callback) {
26272632
callback = typeof options === 'function' ? options : maybeCallback(callback);
2628-
options = getOptions(options, {});
2633+
options = getOptions(options);
26292634
p = toPathIfFileURL(p);
26302635

26312636
if (typeof p !== 'string') {
@@ -2763,7 +2768,7 @@ function realpath(p, options, callback) {
27632768
*/
27642769
realpath.native = (path, options, callback) => {
27652770
callback = makeCallback(callback || options);
2766-
options = getOptions(options, {});
2771+
options = getOptions(options);
27672772
path = getValidatedPath(path);
27682773
const req = new FSReqCallback();
27692774
req.oncomplete = callback;
@@ -2782,7 +2787,7 @@ realpath.native = (path, options, callback) => {
27822787
*/
27832788
function mkdtemp(prefix, options, callback) {
27842789
callback = makeCallback(typeof options === 'function' ? options : callback);
2785-
options = getOptions(options, {});
2790+
options = getOptions(options);
27862791

27872792
validateString(prefix, 'prefix');
27882793
nullCheck(prefix, 'prefix');
@@ -2799,7 +2804,7 @@ function mkdtemp(prefix, options, callback) {
27992804
* @returns {string}
28002805
*/
28012806
function mkdtempSync(prefix, options) {
2802-
options = getOptions(options, {});
2807+
options = getOptions(options);
28032808

28042809
validateString(prefix, 'prefix');
28052810
nullCheck(prefix, 'prefix');

lib/internal/fs/promises.js

+12-9
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,
@@ -81,7 +80,11 @@ const {
8180
validateString,
8281
} = require('internal/validators');
8382
const pathModule = require('path');
84-
const { lazyDOMException, promisify } = require('internal/util');
83+
const {
84+
kEmptyObject,
85+
lazyDOMException,
86+
promisify,
87+
} = require('internal/util');
8588
const { EventEmitterMixin } = require('internal/event_target');
8689
const { watch } = require('internal/fs/watchers');
8790
const { isIterable } = require('internal/streams/utils');
@@ -519,7 +522,7 @@ async function read(handle, bufferOrParams, offset, length, position) {
519522
offset = 0,
520523
length = buffer.byteLength - offset,
521524
position = null,
522-
} = bufferOrParams ?? ObjectCreate(null));
525+
} = bufferOrParams ?? kEmptyObject);
523526

524527
validateBuffer(buffer);
525528
}
@@ -582,7 +585,7 @@ async function write(handle, buffer, offsetOrOptions, length, position) {
582585
offset = 0,
583586
length = buffer.byteLength - offset,
584587
position = null,
585-
} = offsetOrOptions ?? ObjectCreate(null));
588+
} = offsetOrOptions ?? kEmptyObject);
586589
}
587590

588591
if (offset == null) {
@@ -678,7 +681,7 @@ async function mkdir(path, options) {
678681
const {
679682
recursive = false,
680683
mode = 0o777
681-
} = options || {};
684+
} = options || kEmptyObject;
682685
path = getValidatedPath(path);
683686
validateBoolean(recursive, 'options.recursive');
684687

@@ -688,7 +691,7 @@ async function mkdir(path, options) {
688691
}
689692

690693
async function readdir(path, options) {
691-
options = getOptions(options, {});
694+
options = getOptions(options);
692695
path = getValidatedPath(path);
693696
const result = await binding.readdir(pathModule.toNamespacedPath(path),
694697
options.encoding,
@@ -700,7 +703,7 @@ async function readdir(path, options) {
700703
}
701704

702705
async function readlink(path, options) {
703-
options = getOptions(options, {});
706+
options = getOptions(options);
704707
path = getValidatedPath(path, 'oldPath');
705708
return binding.readlink(pathModule.toNamespacedPath(path),
706709
options.encoding, kUsePromises);
@@ -812,13 +815,13 @@ async function lutimes(path, atime, mtime) {
812815
}
813816

814817
async function realpath(path, options) {
815-
options = getOptions(options, {});
818+
options = getOptions(options);
816819
path = getValidatedPath(path);
817820
return binding.realpath(path, options.encoding, kUsePromises);
818821
}
819822

820823
async function mkdtemp(prefix, options) {
821-
options = getOptions(options, {});
824+
options = getOptions(options);
822825

823826
validateString(prefix, 'prefix');
824827
nullCheck(prefix);

lib/internal/fs/streams.js

+6-3
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,
@@ -147,7 +150,7 @@ function ReadStream(path, options) {
147150
return new ReadStream(path, options);
148151

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

@@ -305,7 +308,7 @@ function WriteStream(path, options) {
305308
if (!(this instanceof WriteStream))
306309
return new WriteStream(path, options);
307310

308-
options = copyObject(getOptions(options, {}));
311+
options = copyObject(getOptions(options, kEmptyObject));
309312

310313
// Only buffers are supported.
311314
options.decodeStrings = true;

lib/internal/fs/sync_write_stream.js

+2-1
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;

0 commit comments

Comments
 (0)