diff options
author | LiviaMedeiros <livia@cirno.name> | 2022-05-21 17:52:23 +0800 |
---|---|---|
committer | Antoine du Hamel <duhamelantoine1995@gmail.com> | 2022-06-11 12:18:13 +0200 |
commit | c52454fdc33234af5b8e8942955e40aba5d76dc4 (patch) | |
tree | ffbed190272ff52107624410e4398842e72c5bf6 /lib/fs.js | |
parent | 9220aca5b92e06f92eacff891678d83b18afc174 (diff) | |
download | node-new-c52454fdc33234af5b8e8942955e40aba5d76dc4.tar.gz |
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>
Diffstat (limited to 'lib/fs.js')
-rw-r--r-- | lib/fs.js | 61 |
1 files changed, 33 insertions, 28 deletions
@@ -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, @@ -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' @@ -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)); @@ -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 { @@ -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); @@ -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 }); /** @@ -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, @@ -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 }); /** @@ -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') { @@ -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 }); /** @@ -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; @@ -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 @@ -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(); @@ -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), @@ -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); @@ -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); @@ -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); @@ -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; @@ -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), @@ -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); @@ -2458,8 +2465,6 @@ if (isWindows) { }; } -const emptyObj = ObjectCreate(null); - /** * Returns the resolved pathname. * @param {string | Buffer | URL} p @@ -2467,7 +2472,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 += ''; @@ -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); @@ -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') { @@ -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; @@ -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'); @@ -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'); |