GeezerVison is showing github.com — fs: make parameters optional for readSync · nodejs/node@991aca3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/fs.md +26Lines changed: 26 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -3080,6 +3080,32 @@ Returns the number of `bytesRead`. | 3080 | 3080 | For detailed information, see the documentation of the asynchronous version of | 3081 | 3081 | this API: [`fs.read()`][]. | 3082 | 3082 |
| | 3083 | +## `fs.readSync(fd, buffer, [options])` | | 3084 | +<!-- YAML | | 3085 | +added: REPLACEME | | 3086 | +changes: | | 3087 | + - version: REPLACEME | | 3088 | + pr-url: https://github.com/nodejs/node/pull/32460 | | 3089 | + description: Options object can be passed in | | 3090 | + to make offset, length and position optional | | 3091 | +--> | | 3092 | + | | 3093 | +* `fd` {integer} | | 3094 | +* `buffer` {Buffer|TypedArray|DataView} | | 3095 | +* `options` {Object} | | 3096 | + * `offset` {integer} **Default:** `0` | | 3097 | + * `length` {integer} **Default:** `buffer.length` | | 3098 | + * `position` {integer} **Default:** `null` | | 3099 | +* Returns: {number} | | 3100 | + | | 3101 | +Returns the number of `bytesRead`. | | 3102 | + | | 3103 | +Similar to the above `fs.readSync` function, this version takes an optional `options` object. | | 3104 | +If no `options` object is specified, it will default with the above values. | | 3105 | + | | 3106 | +For detailed information, see the documentation of the asynchronous version of | | 3107 | +this API: [`fs.read()`][]. | | 3108 | + | 3083 | 3109 | ## `fs.readv(fd, buffers[, position], callback)` | 3084 | 3110 | <!-- YAML | 3085 | 3111 | added: REPLACEME | |
Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -534,8 +534,20 @@ function read(fd, buffer, offset, length, position, callback) { | 534 | 534 | ObjectDefineProperty(read, internalUtil.customPromisifyArgs, | 535 | 535 | { value: ['bytesRead', 'buffer'], enumerable: false }); | 536 | 536 |
| | 537 | +// usage: | | 538 | +// fs.readSync(fd, buffer, offset, length, position); | | 539 | +// OR | | 540 | +// fs.readSync(fd, buffer, {}) or fs.readSync(fd, buffer) | 537 | 541 | function readSync(fd, buffer, offset, length, position) { | 538 | 542 | validateInt32(fd, 'fd', 0); | | 543 | + | | 544 | + if (arguments.length <= 3) { | | 545 | + // Assume fs.read(fd, buffer, options) | | 546 | + const options = offset || {}; | | 547 | + | | 548 | + ({ offset = 0, length = buffer.length, position } = options); | | 549 | + } | | 550 | + | 539 | 551 | validateBuffer(buffer); | 540 | 552 |
| 541 | 553 | if (offset == null) { | |
Collapse file Copy file name to clipboard +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +require('../common'); | | 4 | +const fixtures = require('../common/fixtures'); | | 5 | +const fs = require('fs'); | | 6 | +const assert = require('assert'); | | 7 | +const filepath = fixtures.path('x.txt'); | | 8 | +const fd = fs.openSync(filepath, 'r'); | | 9 | + | | 10 | +const expected = Buffer.from('xyz\n'); | | 11 | + | | 12 | +function runTest(defaultBuffer, options) { | | 13 | + const result = fs.readSync(fd, defaultBuffer, options); | | 14 | + assert.strictEqual(result, expected.length); | | 15 | + assert.deepStrictEqual(defaultBuffer, expected); | | 16 | +} | | 17 | + | | 18 | +// Test passing in an empty options object | | 19 | +runTest(Buffer.allocUnsafe(expected.length), { position: 0 }); | | 20 | + | | 21 | +// Test not passing in any options object | | 22 | +runTest(Buffer.allocUnsafe(expected.length)); | | 23 | + | | 24 | +// Test passing in options | | 25 | +runTest(Buffer.allocUnsafe(expected.length), { offset: 0, | | 26 | + length: expected.length, | | 27 | + position: 0 }); |
|