Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ function read(fd, buffer, offset, length, position, callback) {
({
buffer = Buffer.alloc(16384),
offset = 0,
length = buffer.length,
length = buffer.byteLength,
position
} = options);
}
Expand Down Expand Up @@ -586,15 +586,15 @@ ObjectDefineProperty(read, internalUtil.customPromisifyArgs,
function readSync(fd, buffer, offset, length, position) {
fd = getValidatedFd(fd);

validateBuffer(buffer);

if (arguments.length <= 3) {
// Assume fs.read(fd, buffer, options)
const options = offset || {};

({ offset = 0, length = buffer.length, position } = options);
({ offset = 0, length = buffer.byteLength, position } = options);
}

validateBuffer(buffer);

if (offset == null) {
offset = 0;
} else {
Expand Down Expand Up @@ -681,7 +681,7 @@ function write(fd, buffer, offset, length, position, callback) {
validateInteger(offset, 'offset');
}
if (typeof length !== 'number')
length = buffer.length - offset;
length = buffer.byteLength - offset;
if (typeof position !== 'number')
position = null;
validateOffsetLengthWrite(offset, length, buffer.byteLength);
Expand Down
16 changes: 8 additions & 8 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,19 @@ async function writeFileHandle(filehandle, data, signal, encoding) {
checkAborted(signal);
await write(
filehandle, buf, undefined,
isArrayBufferView(buf) ? buf.length : encoding);
isArrayBufferView(buf) ? buf.byteLength : encoding);
checkAborted(signal);
}
return;
}
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
let remaining = data.length;
let remaining = data.byteLength;
if (remaining === 0) return;
do {
checkAborted(signal);
const { bytesWritten } =
await write(filehandle, data, 0,
MathMin(kWriteFileMaxChunkSize, data.length));
MathMin(kWriteFileMaxChunkSize, data.byteLength));
remaining -= bytesWritten;
data = new Uint8Array(
data.buffer,
Expand Down Expand Up @@ -403,7 +403,7 @@ async function read(handle, bufferOrOptions, offset, length, position) {
buffer = Buffer.alloc(16384);
}
offset = bufferOrOptions.offset || 0;
length = buffer.length;
length = buffer.byteLength;
position = bufferOrOptions.position || null;
}

Expand All @@ -418,12 +418,12 @@ async function read(handle, bufferOrOptions, offset, length, position) {
if (length === 0)
return { bytesRead: length, buffer };

if (buffer.length === 0) {
if (buffer.byteLength === 0) {
throw new ERR_INVALID_ARG_VALUE('buffer', buffer,
'is empty and cannot be written');
}

validateOffsetLengthRead(offset, length, buffer.length);
validateOffsetLengthRead(offset, length, buffer.byteLength);

if (!NumberIsSafeInteger(position))
position = -1;
Expand All @@ -446,7 +446,7 @@ async function readv(handle, buffers, position) {
}

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

if (isArrayBufferView(buffer)) {
Expand All @@ -456,7 +456,7 @@ async function write(handle, buffer, offset, length, position) {
validateInteger(offset, 'offset');
}
if (typeof length !== 'number')
length = buffer.length - offset;
length = buffer.byteLength - offset;
if (typeof position !== 'number')
position = null;
validateOffsetLengthWrite(offset, length, buffer.byteLength);
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-fs-write-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,20 @@ tmpdir.refresh();
fs.closeSync(fd);
}));
}

// fs.write with a DataView, without the offset and length parameters:
{
const filename = path.join(tmpdir.path, 'write8.txt');
fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
const cb = common.mustSucceed((written) => {
assert.strictEqual(written, expected.length);
fs.closeSync(fd);

const found = fs.readFileSync(filename, 'utf8');
assert.strictEqual(found, expected.toString());
});

const uint8 = Uint8Array.from(expected);
fs.write(fd, new DataView(uint8.buffer), cb);
}));
}