mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
fs: use kEmptyObject
PR-URL: https://github.com/nodejs/node/pull/43159 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
1 parent
835056cbcb
commit
ca4157ea40
5 files changed
+57
-44
No files matched your search
@@ -83,7 +83,13 @@ const {
|
||||
|
||||
const { FSReqCallback } = binding;
|
||||
const { toPathIfFileURL } = require('internal/url');
|
||||
const internalUtil = require('internal/util');
|
||||
const {
|
||||
customPromisifyArgs: kCustomPromisifyArgsSymbol,
|
||||
kEmptyObject,
|
||||
promisify: {
|
||||
custom: kCustomPromisifiedSymbol,
|
||||
},
|
||||
} = require('internal/util');
|
||||
const {
|
||||
constants: {
|
||||
kIoMaxLength,
|
||||
@@ -272,7 +278,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));
|
||||
@@ -619,7 +625,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 {
|
||||
@@ -632,7 +638,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
|
||||
offset = 0,
|
||||
length = buffer.byteLength - offset,
|
||||
position = null,
|
||||
} = params ?? ObjectCreate(null));
|
||||
} = params ?? kEmptyObject);
|
||||
}
|
||||
|
||||
validateBuffer(buffer);
|
||||
@@ -675,7 +681,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 });
|
||||
|
||||
/**
|
||||
@@ -697,7 +703,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,
|
||||
@@ -768,7 +774,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 });
|
||||
|
||||
/**
|
||||
@@ -825,7 +831,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') {
|
||||
@@ -865,7 +871,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 });
|
||||
|
||||
/**
|
||||
@@ -892,7 +898,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;
|
||||
@@ -955,7 +961,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
|
||||
@@ -1398,7 +1404,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();
|
||||
@@ -1427,7 +1433,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),
|
||||
@@ -1451,7 +1457,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);
|
||||
@@ -1475,7 +1481,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);
|
||||
@@ -1498,7 +1504,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);
|
||||
@@ -1596,7 +1602,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;
|
||||
@@ -1611,7 +1617,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),
|
||||
@@ -2282,7 +2288,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);
|
||||
@@ -2445,8 +2451,6 @@ if (isWindows) {
|
||||
};
|
||||
}
|
||||
|
||||
const emptyObj = ObjectCreate(null);
|
||||
|
||||
/**
|
||||
* Returns the resolved pathname.
|
||||
* @param {string | Buffer | URL} p
|
||||
@@ -2454,7 +2458,7 @@ const emptyObj = ObjectCreate(null);
|
||||
* @returns {string | Buffer}
|
||||
*/
|
||||
function realpathSync(p, options) {
|
||||
options = getOptions(options, emptyObj);
|
||||
options = getOptions(options);
|
||||
p = toPathIfFileURL(p);
|
||||
if (typeof p !== 'string') {
|
||||
p += '';
|
||||
@@ -2591,7 +2595,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);
|
||||
@@ -2612,7 +2616,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') {
|
||||
@@ -2750,7 +2754,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;
|
||||
@@ -2769,7 +2773,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');
|
||||
@@ -2786,7 +2790,7 @@ function mkdtemp(prefix, options, callback) {
|
||||
* @returns {string}
|
||||
*/
|
||||
function mkdtempSync(prefix, options) {
|
||||
options = getOptions(options, {});
|
||||
options = getOptions(options);
|
||||
|
||||
validateString(prefix, 'prefix');
|
||||
nullCheck(prefix, 'prefix');
|
||||
|
||||
@@ -5,7 +5,6 @@ const {
|
||||
Error,
|
||||
MathMax,
|
||||
MathMin,
|
||||
ObjectCreate,
|
||||
NumberIsSafeInteger,
|
||||
Promise,
|
||||
PromisePrototypeThen,
|
||||
@@ -78,7 +77,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');
|
||||
@@ -467,7 +470,7 @@ async function read(handle, bufferOrParams, offset, length, position) {
|
||||
offset = 0,
|
||||
length = buffer.byteLength - offset,
|
||||
position = null,
|
||||
} = bufferOrParams ?? ObjectCreate(null));
|
||||
} = bufferOrParams ?? kEmptyObject);
|
||||
|
||||
validateBuffer(buffer);
|
||||
}
|
||||
@@ -530,7 +533,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) {
|
||||
@@ -626,7 +629,7 @@ async function mkdir(path, options) {
|
||||
const {
|
||||
recursive = false,
|
||||
mode = 0o777
|
||||
} = options || {};
|
||||
} = options || kEmptyObject;
|
||||
path = getValidatedPath(path);
|
||||
validateBoolean(recursive, 'options.recursive');
|
||||
|
||||
@@ -636,7 +639,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,
|
||||
@@ -648,7 +651,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);
|
||||
@@ -760,13 +763,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);
|
||||
|
||||
@@ -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,
|
||||
@@ -150,7 +153,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;
|
||||
|
||||
@@ -309,7 +312,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;
|
||||
|
||||
@@ -4,6 +4,7 @@ const {
|
||||
ObjectSetPrototypeOf,
|
||||
ReflectApply,
|
||||
} = primordials;
|
||||
const { kEmptyObject } = require('internal/util');
|
||||
|
||||
const { Writable } = require('stream');
|
||||
const { closeSync, writeSync } = require('fs');
|
||||
@@ -11,7 +12,7 @@ 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;
|
||||
|
||||
@@ -43,7 +43,10 @@ const {
|
||||
isDate,
|
||||
isBigUint64Array
|
||||
} = require('internal/util/types');
|
||||
const { once } = require('internal/util');
|
||||
const {
|
||||
kEmptyObject,
|
||||
once,
|
||||
} = require('internal/util');
|
||||
const { toPathIfFileURL } = require('internal/url');
|
||||
const {
|
||||
validateAbortSignal,
|
||||
@@ -312,9 +315,8 @@ function getDirent(path, name, type, callback) {
|
||||
}
|
||||
}
|
||||
|
||||
function getOptions(options, defaultOptions) {
|
||||
if (options === null || options === undefined ||
|
||||
typeof options === 'function') {
|
||||
function getOptions(options, defaultOptions = kEmptyObject) {
|
||||
if (options == null || typeof options === 'function') {
|
||||
return defaultOptions;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user