summaryrefslogtreecommitdiff
path: root/lib/fs.js
diff options
context:
space:
mode:
authorLiviaMedeiros <livia@cirno.name>2022-04-04 19:07:50 +0800
committerLiviaMedeiros <livia@cirno.name>2022-05-18 00:49:23 +0800
commitc100f9ad295b3d930cc47b9b4d91abd34ce93973 (patch)
treecb165a8cb62548f0b87b806a582d5f02011e4a20 /lib/fs.js
parentace89d9a89922257dcfe343f86725181f35b65e1 (diff)
downloadnode-new-c100f9ad295b3d930cc47b9b4d91abd34ce93973.tar.gz
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/fs.js')
-rw-r--r--lib/fs.js37
1 files changed, 29 insertions, 8 deletions
diff --git a/lib/fs.js b/lib/fs.js
index 3b29f985e5..69198c7811 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) {