Skip to content

Commit

Permalink
fs: rebased & updated
Browse files Browse the repository at this point in the history
  • Loading branch information
branisha committed Nov 14, 2020
1 parent c7d2a45 commit ea1cc73
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
11 changes: 10 additions & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,16 @@ async function open(path, flags, mode) {
flagsNumber, mode, kUsePromises));
}

async function read(handle, buffer, offset, length, position) {
async function read(handle, bufferOrOptions, offset, length, position) {
let buffer = bufferOrOptions;

if (!isArrayBufferView(buffer)) {
buffer = bufferOrOptions.buffer || Buffer.alloc(16384);
offset = bufferOrOptions.offset || 0;
length = buffer.length;
position = bufferOrOptions.position || null;
}

validateBuffer(buffer);

if (offset == null) {
Expand Down
29 changes: 21 additions & 8 deletions test/parallel/test-fs-promises-file-handle-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

tmpdir.refresh();
async function read(fileHandle, buffer, offset, length, position) {
return useConf ?
fileHandle.read({ buffer, offset, length, position }) :
fileHandle.read(buffer, offset, length, position);
}

async function validateRead() {
const filePath = path.resolve(tmpDir, 'tmp-read-file.txt');
Expand All @@ -23,7 +27,7 @@ async function validateRead() {
const fd = fs.openSync(filePath, 'w+');
fs.writeSync(fd, buffer, 0, buffer.length);
fs.closeSync(fd);
const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
const readAsyncHandle = await read(fileHandle, Buffer.alloc(11), 0, 11, 0);
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);
assert.deepStrictEqual(buffer, readAsyncHandle.buffer);

Expand All @@ -38,7 +42,7 @@ async function validateEmptyRead() {
const fd = fs.openSync(filePath, 'w+');
fs.writeSync(fd, buffer, 0, buffer.length);
fs.closeSync(fd);
const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
const readAsyncHandle = await read(fileHandle, Buffer.alloc(11), 0, 11, 0);
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);

await fileHandle.close();
Expand All @@ -51,12 +55,21 @@ async function validateLargeRead() {
const filePath = fixtures.path('x.txt');
const fileHandle = await open(filePath, 'r');
const pos = 0xffffffff + 1; // max-uint32 + 1
const readHandle = await fileHandle.read(Buffer.alloc(1), 0, 1, pos);
const readHandle = await read(fileHandle, Buffer.alloc(1), 0, 1, pos);

assert.strictEqual(readHandle.bytesRead, 0);
}

validateRead()
.then(validateEmptyRead)
.then(validateLargeRead)
.then(common.mustCall());
let useConf = false;

(async function() {
for (const value of [false, true]) {
tmpdir.refresh();
useConf = value;

await validateRead()
.then(validateEmptyRead)
.then(validateLargeRead)
.then(common.mustCall());
}
});

0 comments on commit ea1cc73

Please sign in to comment.