Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make fs.read params optional #31402

Closed
Closed
Show file tree
Hide file tree
Changes from 16 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
34 changes: 34 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2760,6 +2760,29 @@ The callback is given the three arguments, `(err, bytesRead, buffer)`.
If this method is invoked as its [`util.promisify()`][]ed version, it returns
a `Promise` for an `Object` with `bytesRead` and `buffer` properties.

## `fs.read(fd, [options,] callback)`
<!-- YAML
added: REPLACEME
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/31402
description: Options object can be passed in
to make Buffer, offset, length and position optional
-->
* `fd` {integer}
* `options` {Object}
* `buffer` {Buffer|TypedArray|DataView} **Default:** `Buffer.alloc(16384)`
* `offset` {integer} **Default:** `0`
* `length` {integer} **Default:** `buffer.length`
* `position` {integer} **Default:** `null`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

* `callback` {Function}
* `err` {Error}
* `bytesRead` {integer}
* `buffer` {Buffer}

Similar to the above `fs.read` function, this version takes an optional `options` object.
If no `options` object is specified, it will default with the above values.

## `fs.readdir(path[, options], callback)`
<!-- YAML
added: v0.1.8
Expand Down Expand Up @@ -4342,6 +4365,17 @@ Following successful read, the `Promise` is resolved with an object with a
`bytesRead` property specifying the number of bytes read, and a `buffer`
property that is a reference to the passed in `buffer` argument.

#### `filehandle.read(options)`
<!-- YAML
added: REPLACEME
-->
* `options` {Object}
* `buffer` {Buffer|Uint8Array} **Default:** `Buffer.alloc(16384)`
* `offset` {integer} **Default:** `0`
* `length` {integer} **Default:** `buffer.length`
* `position` {integer} **Default:** `null`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the same as the actual. options.position is the undefined when default

* Returns: {Promise}
ronag marked this conversation as resolved.
Show resolved Hide resolved

#### `filehandle.readFile(options)`
<!-- YAML
added: v10.0.0
Expand Down
26 changes: 26 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,34 @@ function openSync(path, flags, mode) {
return result;
}

// usage:
// fs.read(fd, buffer, offset, length, position, callback);
// OR
// fs.read(fd, {}, callback)
function read(fd, buffer, offset, length, position, callback) {
validateInt32(fd, 'fd', 0);

if (arguments.length <= 3) {
// Assume fs.read(fd, options, callback)
let options = {};
if (arguments.length < 3) {
// This is fs.read(fd, callback)
// buffer will be the callback
callback = buffer;
} else {
// This is fs.read(fd, {}, callback)
// buffer will be the options object
// offset is the callback
options = buffer;
callback = offset;
}

buffer = options.buffer || Buffer.alloc(16384);
offset = options.offset || 0;
length = options.length || buffer.length;
position = options.position || null;
}

validateBuffer(buffer);
callback = maybeCallback(callback);

Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-fs-read-optional-options-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const common = require('../common');
const fixtures = require('../common/fixtures');
const fs = require('fs');
const assert = require('assert');
const filepath = fixtures.path('x.txt');
const fd = fs.openSync(filepath, 'r');

const expected = Buffer.from('xyz\n');
const defaultBufferAsync = Buffer.alloc(16384);

fs.read(fd, common.mustCall((err, bytesRead, buffer) => {
assert.strictEqual(bytesRead, expected.length);
assert.deepStrictEqual(defaultBufferAsync.length, buffer.length);
}));
16 changes: 16 additions & 0 deletions test/parallel/test-fs-read-optional-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const common = require('../common');
const fixtures = require('../common/fixtures');
const fs = require('fs');
const assert = require('assert');
const filepath = fixtures.path('x.txt');
const fd = fs.openSync(filepath, 'r');

const expected = Buffer.from('xyz\n');
const defaultBufferAsync = Buffer.alloc(16384);

fs.read(fd, {}, common.mustCall((err, bytesRead, buffer) => {
assert.strictEqual(bytesRead, expected.length);
assert.deepStrictEqual(defaultBufferAsync.length, buffer.length);
}));
ronag marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions test/parallel/test-fs-read-promises-optional-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const common = require('../common');
const fixtures = require('../common/fixtures');
const fs = require('fs');
const read = require('util').promisify(fs.read);
const assert = require('assert');
const filepath = fixtures.path('x.txt');
const fd = fs.openSync(filepath, 'r');

const expected = Buffer.from('xyz\n');
const defaultBufferAsync = Buffer.alloc(16384);

read(fd, {})
.then(function({ bytesRead, buffer }) {
assert.strictEqual(bytesRead, expected.length);
assert.deepStrictEqual(defaultBufferAsync.length, buffer.length);
})
.then(common.mustCall());