Skip to content

Commit ff0775c

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

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

lib/fs.js

+23-12
Original file line numberDiff line numberDiff line change
@@ -789,26 +789,38 @@ 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]
793-
* @param {number} [length]
794-
* @param {number} [position]
792+
* @param {{
793+
* offset?: number;
794+
* length?: number;
795+
* position?: number;
796+
* }} [offsetOrOptions]
795797
* @param {(
796798
* err?: Error,
797799
* bytesWritten?: number;
798800
* buffer?: Buffer | TypedArray | DataView
799801
* ) => any} callback
800802
* @returns {void}
801803
*/
802-
function write(fd, buffer, offset, length, position, callback) {
804+
function write(fd, buffer, offsetOrOptions, length, position, callback) {
803805
function wrapper(err, written) {
804806
// Retain a reference to buffer so that it can't be GC'ed too soon.
805807
callback(err, written || 0, buffer);
806808
}
807809

808810
fd = getValidatedFd(fd);
809811

812+
let offset = offsetOrOptions;
810813
if (isArrayBufferView(buffer)) {
811814
callback = maybeCallback(callback || position || length || offset);
815+
816+
if (typeof offset === 'object') {
817+
({
818+
offset = 0,
819+
length = buffer.byteLength - offset,
820+
position = null
821+
} = offsetOrOptions ?? ObjectCreate(null));
822+
}
823+
812824
if (offset == null || typeof offset === 'function') {
813825
offset = 0;
814826
} else {
@@ -867,15 +879,14 @@ function writeSync(fd, buffer, offsetOrOptions, length, position) {
867879
let result;
868880

869881
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-
878882
if (isArrayBufferView(buffer)) {
883+
if (typeof offset === 'object') {
884+
({
885+
offset = 0,
886+
length = buffer.byteLength - offset,
887+
position = null
888+
} = offsetOrOptions ?? ObjectCreate(null));
889+
}
879890
if (position === undefined)
880891
position = null;
881892
if (offset == null) {

lib/internal/fs/promises.js

+11-13
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)