-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
lholmquist
wants to merge
17
commits into
nodejs:master
from
lholmquist:31237-fs-read-params-optional
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
95f03ef
feat: make fs.read params optional
lholmquist 478d2f0
squash: doc updates
lholmquist 10d3902
squash: adding in test
lholmquist dfdd3b4
squash: better use of destructoring
lholmquist cf320a0
squash: refactor.
lholmquist 418b6fd
squash: Update doc/api/fs.md
lholmquist e3835c2
squash: Update doc/api/fs.md
lholmquist 128aeab
squash: reverting some doc updates
lholmquist 2c5d3e7
squash: Update doc/api/fs.md
lholmquist 6433727
squash: Update doc/api/fs.md
lholmquist 3923975
squash: add test for promise based
lholmquist f6f01b6
squash: add defaults to filehandler.read docs
lholmquist 55a2356
squash: add the use case for an optional options object
lholmquist f86bdc1
squash: add test for optional options option
lholmquist a4c3763
squash: a little more doc update
lholmquist e321b88
squash: doc linting
lholmquist cb8c0ef
squash: combine tests and add another test that passes options
lholmquist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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` | ||
* `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 | ||
|
@@ -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` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the same as the actual. |
||
* Returns: {Promise} | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#### `filehandle.readFile(options)` | ||
<!-- YAML | ||
added: v10.0.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'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); | ||
const bufferAsOption = Buffer.allocUnsafe(expected.length); | ||
|
||
// Test passing in an empty options object | ||
fs.read(fd, { position: 0 }, 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
|
||
|
||
// Test not passing in any options object | ||
fs.read(fd, common.mustCall((err, bytesRead, buffer) => { | ||
assert.strictEqual(bytesRead, expected.length); | ||
assert.deepStrictEqual(defaultBufferAsync.length, buffer.length); | ||
})); | ||
|
||
// Test passing in options | ||
fs.read(fd, { | ||
buffer: bufferAsOption, | ||
offset: 0, | ||
lenght: bufferAsOption.length, | ||
position: 0 | ||
}, | ||
common.mustCall((err, bytesRead, buffer) => { | ||
assert.strictEqual(bytesRead, expected.length); | ||
assert.deepStrictEqual(bufferAsOption.length, buffer.length); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here