Skip to content

Commit

Permalink
Add promise support for fs.readv
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Oct 20, 2022
1 parent 6bb4b2a commit ccf9026
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
64 changes: 64 additions & 0 deletions lib/fs/__tests__/multi-param.test.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit ccf9026

Please sign in to comment.