Skip to content
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

Add promise support for fs.readv #970

Merged
merged 1 commit into from Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions lib/fs/__tests__/multi-param.test.js
Expand Up @@ -149,6 +149,70 @@ describe('fs.write()', () => {
})
})

describe('fs.readv()', () => {
let TEST_FILE
let TEST_DATA
let TEST_FD

beforeEach(() => {
TEST_FILE = path.join(os.tmpdir(), 'fs-extra', 'readv-test-file')
TEST_DATA = crypto.randomBytes(SIZE)
fs.writeFileSync(TEST_FILE, TEST_DATA)
TEST_FD = fs.openSync(TEST_FILE, 'r')
})

afterEach(() => {
return fs.close(TEST_FD)
.then(() => fs.remove(TEST_FILE))
})

describe('with promises', () => {
it('returns an object', () => {
const bufferArray = [Buffer.alloc(SIZE / 2), Buffer.alloc(SIZE / 2)]
return fs.readv(TEST_FD, bufferArray, 0)
.then(({ bytesRead, buffers }) => {
assert.strictEqual(bytesRead, SIZE, 'bytesRead is correct')
assert.deepStrictEqual(buffers, bufferArray, 'returned data matches mutated input param')
assert.deepStrictEqual(Buffer.concat(buffers), TEST_DATA, 'data is correct')
})
})

it('returns an object when minimal arguments are passed', () => {
const bufferArray = [Buffer.alloc(SIZE / 2), Buffer.alloc(SIZE / 2)]
return fs.readv(TEST_FD, bufferArray)
.then(({ bytesRead, buffers }) => {
assert.strictEqual(bytesRead, SIZE, 'bytesRead is correct')
assert.deepStrictEqual(buffers, bufferArray, 'returned data matches mutated input param')
assert.deepStrictEqual(Buffer.concat(buffers), TEST_DATA, 'data is correct')
})
})
})

describe('with callbacks', () => {
it('works', done => {
const bufferArray = [Buffer.alloc(SIZE / 2), Buffer.alloc(SIZE / 2)]
fs.readv(TEST_FD, bufferArray, 0, (err, bytesRead, buffers) => {
assert.ifError(err)
assert.strictEqual(bytesRead, SIZE, 'bytesRead is correct')
assert.deepStrictEqual(buffers, bufferArray, 'returned data matches mutated input param')
assert.deepStrictEqual(Buffer.concat(buffers), TEST_DATA, 'data is correct')
done()
})
})

it('works when minimal arguments are passed', done => {
const bufferArray = [Buffer.alloc(SIZE / 2), Buffer.alloc(SIZE / 2)]
fs.readv(TEST_FD, bufferArray, (err, bytesRead, buffers) => {
assert.ifError(err)
assert.strictEqual(bytesRead, SIZE, 'bytesRead is correct')
assert.deepStrictEqual(buffers, bufferArray, 'returned data matches mutated input param')
assert.deepStrictEqual(Buffer.concat(buffers), TEST_DATA, 'data is correct')
done()
})
})
})
})

describe('fs.writev()', () => {
let TEST_FILE
let TEST_DATA
Expand Down
18 changes: 17 additions & 1 deletion lib/fs/index.js
Expand Up @@ -65,7 +65,7 @@ exports.exists = function (filename, callback) {
})
}

// fs.read(), fs.write(), & fs.writev() need special treatment due to multiple callback args
// fs.read(), fs.write(), fs.readv(), & fs.writev() need special treatment due to multiple callback args

exports.read = function (fd, buffer, offset, length, position, callback) {
if (typeof callback === 'function') {
Expand Down Expand Up @@ -97,6 +97,22 @@ exports.write = function (fd, buffer, ...args) {
})
}

// Function signature is
// s.readv(fd, buffers[, position], callback)
// We need to handle the optional arg, so we use ...args
exports.readv = function (fd, buffers, ...args) {
if (typeof args[args.length - 1] === 'function') {
return fs.readv(fd, buffers, ...args)
}

return new Promise((resolve, reject) => {
fs.readv(fd, buffers, ...args, (err, bytesRead, buffers) => {
if (err) return reject(err)
resolve({ bytesRead, buffers })
})
})
}

// Function signature is
// s.writev(fd, buffers[, position], callback)
// We need to handle the optional arg, so we use ...args
Expand Down