-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This makes all the parameters of the `fs.read` function, except for `fd` and the callback(when not using as a promise) optional. They will default to sensible defaults. Fixes: #31237 PR-URL: #31402 Reviewed-By: Robert Nagy <[email protected]>
- Loading branch information
1 parent
35d0569
commit 94f3eed
Showing
4 changed files
with
115 additions
and
0 deletions.
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
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); | ||
})); | ||
|
||
// 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()); |