Skip to content

Commit

Permalink
rework: change fh.write signature, add fs.write
Browse files Browse the repository at this point in the history
  • Loading branch information
LiviaMedeiros committed Apr 19, 2022
1 parent e7531f0 commit 2fdc02d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
29 changes: 19 additions & 10 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,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} [position]
* @param {(
Expand All @@ -799,16 +799,26 @@ 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);
}

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 {
Expand Down Expand Up @@ -867,15 +877,14 @@ function writeSync(fd, buffer, offsetOrOptions, length, position) {
let result;

let offset = offsetOrOptions;
if (typeof offset === 'object' && offset !== null) {
({
offset = 0,
length = buffer.byteLength - offset,
position = null
} = 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) {
Expand Down
24 changes: 11 additions & 13 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,22 +560,20 @@ async function readv(handle, buffers, position) {
return { bytesRead, buffers };
}

async function write(handle, bufferOrOptions, offset, length, position) {
let buffer = bufferOrOptions;
if (!isArrayBufferView(buffer) && typeof buffer !== 'string') {
validateBuffer(bufferOrOptions?.buffer);
({
buffer,
offset = 0,
length = buffer.byteLength - offset,
position = null
} = bufferOrOptions);
}

if (buffer.byteLength === 0)
async function write(handle, buffer, offsetOrOptions, length, position) {
if (buffer?.byteLength === 0)
return { bytesWritten: 0, buffer };

let offset = offsetOrOptions;
if (isArrayBufferView(buffer)) {
if (typeof offset === 'object') {
({
offset = 0,
length = buffer.byteLength - offset,
position = null
} = offsetOrOptions ?? ObjectCreate(null));
}

if (offset == null) {
offset = 0;
} else {
Expand Down

0 comments on commit 2fdc02d

Please sign in to comment.