fs: make params in writing methods optional · nodejs/node@3e89b73 · GitHub
Skip to content

Commit 3e89b73

Browse files
LiviaMedeirosbengl
authored andcommitted
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: #41666 PR-URL: #42601 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 72a767b commit 3e89b73

6 files changed

Lines changed: 388 additions & 13 deletions

File tree

doc/api/fs.md

Lines changed: 63 additions & 4 deletions

lib/fs.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
634634
({
635635
offset = 0,
636636
length = buffer.byteLength - offset,
637-
position = null
637+
position = null,
638638
} = params ?? ObjectCreate(null));
639639
}
640640

@@ -705,7 +705,7 @@ function readSync(fd, buffer, offset, length, position) {
705705
({
706706
offset = 0,
707707
length = buffer.byteLength - offset,
708-
position = null
708+
position = null,
709709
} = options);
710710
}
711711

@@ -801,7 +801,7 @@ function readvSync(fd, buffers, position) {
801801
* Writes `buffer` to the specified `fd` (file descriptor).
802802
* @param {number} fd
803803
* @param {Buffer | TypedArray | DataView | string | object} buffer
804-
* @param {number} [offset]
804+
* @param {number | object} [offsetOrOptions]
805805
* @param {number} [length]
806806
* @param {number | null} [position]
807807
* @param {(
@@ -811,16 +811,26 @@ function readvSync(fd, buffers, position) {
811811
* ) => any} callback
812812
* @returns {void}
813813
*/
814-
function write(fd, buffer, offset, length, position, callback) {
814+
function write(fd, buffer, offsetOrOptions, length, position, callback) {
815815
function wrapper(err, written) {
816816
// Retain a reference to buffer so that it can't be GC'ed too soon.
817817
callback(err, written || 0, buffer);
818818
}
819819

820820
fd = getValidatedFd(fd);
821821

822+
let offset = offsetOrOptions;
822823
if (isArrayBufferView(buffer)) {
823824
callback = maybeCallback(callback || position || length || offset);
825+
826+
if (typeof offset === 'object') {
827+
({
828+
offset = 0,
829+
length = buffer.byteLength - offset,
830+
position = null,
831+
} = offsetOrOptions ?? ObjectCreate(null));
832+
}
833+
824834
if (offset == null || typeof offset === 'function') {
825835
offset = 0;
826836
} else {
@@ -869,16 +879,27 @@ ObjectDefineProperty(write, internalUtil.customPromisifyArgs,
869879
* specified `fd` (file descriptor).
870880
* @param {number} fd
871881
* @param {Buffer | TypedArray | DataView | string} buffer
872-
* @param {number} [offset]
873-
* @param {number} [length]
874-
* @param {number | null} [position]
882+
* @param {{
883+
* offset?: number;
884+
* length?: number;
885+
* position?: number | null;
886+
* }} [offsetOrOptions]
875887
* @returns {number}
876888
*/
877-
function writeSync(fd, buffer, offset, length, position) {
889+
function writeSync(fd, buffer, offsetOrOptions, length, position) {
878890
fd = getValidatedFd(fd);
879891
const ctx = {};
880892
let result;
893+
894+
let offset = offsetOrOptions;
881895
if (isArrayBufferView(buffer)) {
896+
if (typeof offset === 'object') {
897+
({
898+
offset = 0,
899+
length = buffer.byteLength - offset,
900+
position = null
901+
} = offsetOrOptions ?? ObjectCreate(null));
902+
}
882903
if (position === undefined)
883904
position = null;
884905
if (offset == null) {

lib/internal/fs/promises.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,20 @@ async function readv(handle, buffers, position) {
569569
return { bytesRead, buffers };
570570
}
571571

572-
async function write(handle, buffer, offset, length, position) {
572+
async function write(handle, buffer, offsetOrOptions, length, position) {
573573
if (buffer?.byteLength === 0)
574574
return { bytesWritten: 0, buffer };
575575

576+
let offset = offsetOrOptions;
576577
if (isArrayBufferView(buffer)) {
578+
if (typeof offset === 'object') {
579+
({
580+
offset = 0,
581+
length = buffer.byteLength - offset,
582+
position = null
583+
} = offsetOrOptions ?? ObjectCreate(null));
584+
}
585+
577586
if (offset == null) {
578587
offset = 0;
579588
} else {
Lines changed: 95 additions & 0 deletions

0 commit comments

Comments
 (0)