Skip to content

Commit 2fdc02d

Browse files
committed
rework: change fh.write signature, add fs.write
1 parent e7531f0 commit 2fdc02d

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

lib/fs.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ function readvSync(fd, buffers, position) {
789789
* Writes `buffer` to the specified `fd` (file descriptor).
790790
* @param {number} fd
791791
* @param {Buffer | TypedArray | DataView | string | object} buffer
792-
* @param {number} [offset]
792+
* @param {number | object} [offsetOrOptions]
793793
* @param {number} [length]
794794
* @param {number} [position]
795795
* @param {(
@@ -799,16 +799,26 @@ function readvSync(fd, buffers, position) {
799799
* ) => any} callback
800800
* @returns {void}
801801
*/
802-
function write(fd, buffer, offset, length, position, callback) {
802+
function write(fd, buffer, offsetOrOptions, length, position, callback) {
803803
function wrapper(err, written) {
804804
// Retain a reference to buffer so that it can't be GC'ed too soon.
805805
callback(err, written || 0, buffer);
806806
}
807807

808808
fd = getValidatedFd(fd);
809809

810+
let offset = offsetOrOptions;
810811
if (isArrayBufferView(buffer)) {
811812
callback = maybeCallback(callback || position || length || offset);
813+
814+
if (typeof offset === 'object') {
815+
({
816+
offset = 0,
817+
length = buffer.byteLength - offset,
818+
position = null
819+
} = offsetOrOptions ?? ObjectCreate(null));
820+
}
821+
812822
if (offset == null || typeof offset === 'function') {
813823
offset = 0;
814824
} else {
@@ -867,15 +877,14 @@ function writeSync(fd, buffer, offsetOrOptions, length, position) {
867877
let result;
868878

869879
let offset = offsetOrOptions;
870-
if (typeof offset === 'object' && offset !== null) {
871-
({
872-
offset = 0,
873-
length = buffer.byteLength - offset,
874-
position = null
875-
} = offsetOrOptions);
876-
}
877-
878880
if (isArrayBufferView(buffer)) {
881+
if (typeof offset === 'object') {
882+
({
883+
offset = 0,
884+
length = buffer.byteLength - offset,
885+
position = null
886+
} = offsetOrOptions ?? ObjectCreate(null));
887+
}
879888
if (position === undefined)
880889
position = null;
881890
if (offset == null) {

lib/internal/fs/promises.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -560,22 +560,20 @@ async function readv(handle, buffers, position) {
560560
return { bytesRead, buffers };
561561
}
562562

563-
async function write(handle, bufferOrOptions, offset, length, position) {
564-
let buffer = bufferOrOptions;
565-
if (!isArrayBufferView(buffer) && typeof buffer !== 'string') {
566-
validateBuffer(bufferOrOptions?.buffer);
567-
({
568-
buffer,
569-
offset = 0,
570-
length = buffer.byteLength - offset,
571-
position = null
572-
} = bufferOrOptions);
573-
}
574-
575-
if (buffer.byteLength === 0)
563+
async function write(handle, buffer, offsetOrOptions, length, position) {
564+
if (buffer?.byteLength === 0)
576565
return { bytesWritten: 0, buffer };
577566

567+
let offset = offsetOrOptions;
578568
if (isArrayBufferView(buffer)) {
569+
if (typeof offset === 'object') {
570+
({
571+
offset = 0,
572+
length = buffer.byteLength - offset,
573+
position = null
574+
} = offsetOrOptions ?? ObjectCreate(null));
575+
}
576+
579577
if (offset == null) {
580578
offset = 0;
581579
} else {

0 commit comments

Comments
 (0)