From 68608736ef4371a98458ebeb41f6abb326786097 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Mon, 4 Apr 2022 19:07:50 +0800 Subject: [PATCH] fs: make params in writing methods optional This change allows passing objects as "named parameters" Fixes: https://github.com/nodejs/node/issues/41666 --- doc/api/fs.md | 36 ++++++++ lib/fs.js | 20 ++++- lib/internal/fs/promises.js | 15 +++- .../test-fs-promises-write-optional-params.js | 87 +++++++++++++++++++ .../test-fs-write-sync-optional-params.js | 82 +++++++++++++++++ 5 files changed, 234 insertions(+), 6 deletions(-) create mode 100644 test/parallel/test-fs-promises-write-optional-params.js create mode 100644 test/parallel/test-fs-write-sync-optional-params.js diff --git a/doc/api/fs.md b/doc/api/fs.md index fc27ea30d1f5f1..6591335bd7e50e 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -646,6 +646,25 @@ On Linux, positional writes do not work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file. +#### `filehandle.write(buffer, options)` + + + +* `buffer` {Buffer|TypedArray|DataView} +* `options` {Object} + * `offset` {integer} **Default:** `0` + * `length` {integer} **Default:** `buffer.byteLength - offset` + * `position` {integer} **Default:** `null` +* Returns: {Promise} + +Write `buffer` to the file. + +Similar to the above `filehandle.write` function, this version takes an +optional `options` object. If no `options` object is specified, it will +default with the above values. + #### `filehandle.write(string[, position[, encoding]])` + +* `fd` {integer} +* `buffer` {Buffer|TypedArray|DataView} +* `options` {Object} + * `offset` {integer} **Default:** `0` + * `length` {integer} **Default:** `buffer.byteLength - offset` + * `position` {integer} **Default:** `null` +* Returns: {number} The number of bytes written. + +For detailed information, see the documentation of the asynchronous version of +this API: [`fs.write(fd, buffer...)`][]. + ### `fs.writeSync(fd, string[, position[, encoding]])`