Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
feat: make fs.read params optional#31402
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
95f03ef478d2f010d3902dfdd3b4cf320a0418b6fde3835c2128aeab2c5d3e764337273923975f6f01b655a2356f86bdc1a4c3763e321b88cb8c0efFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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` | ||
Member 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| #### `filehandle.readFile(options)` | ||
| <!-- YAML | ||
| added: v10.0.0 | ||
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| // 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); | ||
| })); | ||
| 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()); |
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