diff options
author | Livia Medeiros <74449973+LiviaMedeiros@users.noreply.github.com> | 2022-05-03 02:01:27 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-02 20:01:27 +0200 |
commit | 57678e55817366c39a3a241f89949c8fbe8418bc (patch) | |
tree | 12f8024cd2716feaeb6ed7cfdf0681c2a372ac1d /lib/fs.js | |
parent | 6be94c9443c890cd5706cfa6508c4c00e06dfe9e (diff) | |
download | node-new-57678e55817366c39a3a241f89949c8fbe8418bc.tar.gz |
fs: add `read(buffer[, options])` versions
This adds the following:
- `fs.read(fd, buffer[, options], callback)`.
- `filehandle.read(buffer[, options])`.
PR-URL: https://github.com/nodejs/node/pull/42768
Refs: https://github.com/nodejs/node/pull/42601
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Diffstat (limited to 'lib/fs.js')
-rw-r--r-- | lib/fs.js | 37 |
1 files changed, 22 insertions, 15 deletions
@@ -136,6 +136,7 @@ const { validateEncoding, validateFunction, validateInteger, + validateObject, validateString, } = require('internal/validators'); @@ -595,7 +596,7 @@ function openSync(path, flags, mode) { * Reads file from the specified `fd` (file descriptor). * @param {number} fd * @param {Buffer | TypedArray | DataView} buffer - * @param {number} offset + * @param {number} offsetOrOptions * @param {number} length * @param {number | bigint | null} position * @param {( @@ -605,30 +606,36 @@ function openSync(path, flags, mode) { * ) => any} callback * @returns {void} */ -function read(fd, buffer, offset, length, position, callback) { +function read(fd, buffer, offsetOrOptions, length, position, callback) { fd = getValidatedFd(fd); - if (arguments.length <= 3) { - // Assume fs.read(fd, options, callback) - let options = ObjectCreate(null); - if (arguments.length < 3) { + let offset = offsetOrOptions; + let params = null; + if (arguments.length <= 4) { + if (arguments.length === 4) { + // This is fs.read(fd, buffer, options, callback) + validateObject(offsetOrOptions, 'options', { nullable: true }); + callback = length; + params = offsetOrOptions; + } else if (arguments.length === 3) { + // This is fs.read(fd, bufferOrParams, callback) + if (!isArrayBufferView(buffer)) { + // This is fs.read(fd, params, callback) + params = buffer; + ({ buffer = Buffer.alloc(16384) } = params ?? ObjectCreate(null)); + } + callback = offsetOrOptions; + } else { // 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 = Buffer.alloc(16384); } ({ - buffer = Buffer.alloc(16384), offset = 0, length = buffer.byteLength - offset, position = null - } = options); + } = params ?? ObjectCreate(null)); } validateBuffer(buffer); |