diff options
author | LiviaMedeiros <livia@cirno.name> | 2022-04-04 14:07:50 +0300 |
---|---|---|
committer | Bryan English <bryan@bryanenglish.com> | 2022-05-30 19:26:50 +0300 |
commit | 3e89b7336dbdfb7bec601e52b49d63fa3a0105f4 (patch) | |
tree | f0bb0730071dd579374fa5427f88eadcbfa1eeca /lib | |
parent | 72a767b7cab21c9e5c57994767e318c6f2041a9c (diff) |
fs: make params in writing methods optional
This change allows passing objects as "named parameters":
- `fs.write(fd, buffer[, options], callback)`
- `fs.writeSync(fd, buffer[, options])`
- `filehandle.write(buffer[, options])`
Fixes: https://github.com/nodejs/node/issues/41666
PR-URL: https://github.com/nodejs/node/pull/42601
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/fs.js | 37 | ||||
-rw-r--r-- | lib/internal/fs/promises.js | 11 |
2 files changed, 39 insertions, 9 deletions
diff --git a/lib/fs.js b/lib/fs.js index 3b29f985e51..69198c7811d 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -634,7 +634,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) { ({ offset = 0, length = buffer.byteLength - offset, - position = null + position = null, } = params ?? ObjectCreate(null)); } @@ -705,7 +705,7 @@ function readSync(fd, buffer, offset, length, position) { ({ offset = 0, length = buffer.byteLength - offset, - position = null + position = null, } = options); } @@ -801,7 +801,7 @@ function readvSync(fd, buffers, position) { * Writes `buffer` to the specified `fd` (file descriptor). * @param {number} fd * @param {Buffer | TypedArray | DataView | string | object} buffer - * @param {number} [offset] + * @param {number | object} [offsetOrOptions] * @param {number} [length] * @param {number | null} [position] * @param {( @@ -811,7 +811,7 @@ function readvSync(fd, buffers, position) { * ) => any} callback * @returns {void} */ -function write(fd, buffer, offset, length, position, callback) { +function write(fd, buffer, offsetOrOptions, length, position, callback) { function wrapper(err, written) { // Retain a reference to buffer so that it can't be GC'ed too soon. callback(err, written || 0, buffer); @@ -819,8 +819,18 @@ function write(fd, buffer, offset, length, position, callback) { fd = getValidatedFd(fd); + let offset = offsetOrOptions; if (isArrayBufferView(buffer)) { callback = maybeCallback(callback || position || length || offset); + + if (typeof offset === 'object') { + ({ + offset = 0, + length = buffer.byteLength - offset, + position = null, + } = offsetOrOptions ?? ObjectCreate(null)); + } + if (offset == null || typeof offset === 'function') { offset = 0; } else { @@ -869,16 +879,27 @@ ObjectDefineProperty(write, internalUtil.customPromisifyArgs, * specified `fd` (file descriptor). * @param {number} fd * @param {Buffer | TypedArray | DataView | string} buffer - * @param {number} [offset] - * @param {number} [length] - * @param {number | null} [position] + * @param {{ + * offset?: number; + * length?: number; + * position?: number | null; + * }} [offsetOrOptions] * @returns {number} */ -function writeSync(fd, buffer, offset, length, position) { +function writeSync(fd, buffer, offsetOrOptions, length, position) { fd = getValidatedFd(fd); const ctx = {}; let result; + + let offset = offsetOrOptions; if (isArrayBufferView(buffer)) { + if (typeof offset === 'object') { + ({ + offset = 0, + length = buffer.byteLength - offset, + position = null + } = offsetOrOptions ?? ObjectCreate(null)); + } if (position === undefined) position = null; if (offset == null) { diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 9ae66a8e073..dfd16667f73 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -569,11 +569,20 @@ async function readv(handle, buffers, position) { return { bytesRead, buffers }; } -async function write(handle, buffer, offset, length, position) { +async function write(handle, buffer, offsetOrOptions, length, position) { if (buffer?.byteLength === 0) return { bytesWritten: 0, buffer }; + let offset = offsetOrOptions; if (isArrayBufferView(buffer)) { + if (typeof offset === 'object') { + ({ + offset = 0, + length = buffer.byteLength - offset, + position = null + } = offsetOrOptions ?? ObjectCreate(null)); + } + if (offset == null) { offset = 0; } else { |