-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
fs: add fs.readv() #32356
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
Closed
Closed
fs: add fs.readv() #32356
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
fs: Add fs.readv()
- Loading branch information
commit 9048caee577393edb1a596b46bc1ad097269cfc4
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
| 'use strict'; | ||
|
|
||
| require('../common'); | ||
| const assert = require('assert'); | ||
| const path = require('path'); | ||
| const fs = require('fs').promises; | ||
| const tmpdir = require('../common/tmpdir'); | ||
|
|
||
| tmpdir.refresh(); | ||
|
|
||
| const expected = 'ümlaut. Лорем 運務ホソモ指及 आपको करने विकास 紙読決多密所 أضف'; | ||
| const exptectedBuff = Buffer.from(expected); | ||
|
|
||
| let cnt = 0; | ||
| function getFileName() { | ||
| return path.join(tmpdir.path, `readv_promises_${++cnt}.txt`); | ||
| } | ||
|
|
||
| const allocateEmptyBuffers = (combinedLength) => { | ||
| const bufferArr = []; | ||
| // Allocate two buffers, each half the size of exptectedBuff | ||
| bufferArr[0] = Buffer.alloc(Math.floor(combinedLength / 2)), | ||
| bufferArr[1] = Buffer.alloc(combinedLength - bufferArr[0].length); | ||
|
|
||
| return bufferArr; | ||
| }; | ||
|
|
||
| (async () => { | ||
| { | ||
| const filename = getFileName(); | ||
| await fs.writeFile(filename, exptectedBuff); | ||
| const handle = await fs.open(filename, 'r'); | ||
| // const buffer = Buffer.from(expected); | ||
| const bufferArr = allocateEmptyBuffers(exptectedBuff.length); | ||
| const expectedLength = exptectedBuff.length; | ||
|
|
||
| let { bytesRead, buffers } = await handle.readv([Buffer.from('')], | ||
| null); | ||
| assert.deepStrictEqual(bytesRead, 0); | ||
| assert.deepStrictEqual(buffers, [Buffer.from('')]); | ||
|
|
||
| ({ bytesRead, buffers } = await handle.readv(bufferArr, null)); | ||
| assert.deepStrictEqual(bytesRead, expectedLength); | ||
| assert.deepStrictEqual(buffers, bufferArr); | ||
| assert(Buffer.concat(bufferArr).equals(await fs.readFile(filename))); | ||
| handle.close(); | ||
| } | ||
|
|
||
| { | ||
| const filename = getFileName(); | ||
| await fs.writeFile(filename, exptectedBuff); | ||
| const handle = await fs.open(filename, 'r'); | ||
| // const buffer = Buffer.from(expected); | ||
| const bufferArr = allocateEmptyBuffers(exptectedBuff.length); | ||
| const expectedLength = exptectedBuff.length; | ||
|
|
||
| let { bytesRead, buffers } = await handle.readv([Buffer.from('')]); | ||
| assert.deepStrictEqual(bytesRead, 0); | ||
| assert.deepStrictEqual(buffers, [Buffer.from('')]); | ||
|
|
||
| ({ bytesRead, buffers } = await handle.readv(bufferArr)); | ||
| assert.deepStrictEqual(bytesRead, expectedLength); | ||
| assert.deepStrictEqual(buffers, bufferArr); | ||
| assert(Buffer.concat(bufferArr).equals(await fs.readFile(filename))); | ||
| handle.close(); | ||
| } | ||
| })(); |
This file contains hidden or 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,97 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const path = require('path'); | ||
| const fs = require('fs'); | ||
| const tmpdir = require('../common/tmpdir'); | ||
|
|
||
| tmpdir.refresh(); | ||
|
|
||
| const expected = 'ümlaut. Лорем 運務ホソモ指及 आपको करने विकास 紙読決多密所 أضف'; | ||
|
|
||
| let cnt = 0; | ||
| const getFileName = () => path.join(tmpdir.path, `readv_${++cnt}.txt`); | ||
| const exptectedBuff = Buffer.from(expected); | ||
|
|
||
| const allocateEmptyBuffers = (combinedLength) => { | ||
| const bufferArr = []; | ||
| // Allocate two buffers, each half the size of exptectedBuff | ||
| bufferArr[0] = Buffer.alloc(Math.floor(combinedLength / 2)), | ||
| bufferArr[1] = Buffer.alloc(combinedLength - bufferArr[0].length); | ||
|
|
||
| return bufferArr; | ||
| }; | ||
|
|
||
| const getCallback = (fd, bufferArr) => { | ||
| return common.mustCall((err, bytesRead, buffers) => { | ||
| assert.ifError(err); | ||
|
|
||
| assert.deepStrictEqual(bufferArr, buffers); | ||
| const expectedLength = exptectedBuff.length; | ||
| assert.deepStrictEqual(bytesRead, expectedLength); | ||
| fs.closeSync(fd); | ||
|
|
||
| assert(Buffer.concat(bufferArr).equals(exptectedBuff)); | ||
| }); | ||
| }; | ||
|
|
||
| // fs.readv with array of buffers with all parameters | ||
| { | ||
| const filename = getFileName(); | ||
| const fd = fs.openSync(filename, 'w+'); | ||
| fs.writeSync(fd, exptectedBuff); | ||
|
|
||
| const bufferArr = allocateEmptyBuffers(exptectedBuff.length); | ||
| const callback = getCallback(fd, bufferArr); | ||
|
|
||
| fs.readv(fd, bufferArr, 0, callback); | ||
| } | ||
|
|
||
| // fs.readv with array of buffers without position | ||
| { | ||
| const filename = getFileName(); | ||
| fs.writeFileSync(filename, exptectedBuff); | ||
| const fd = fs.openSync(filename, 'r'); | ||
|
|
||
| const bufferArr = allocateEmptyBuffers(exptectedBuff.length); | ||
| const callback = getCallback(fd, bufferArr); | ||
|
|
||
| fs.readv(fd, bufferArr, callback); | ||
| } | ||
|
|
||
| /** | ||
| * Testing with incorrect arguments | ||
| */ | ||
| const wrongInputs = [false, 'test', {}, [{}], ['sdf'], null, undefined]; | ||
|
|
||
| { | ||
| const filename = getFileName(2); | ||
| fs.writeFileSync(filename, exptectedBuff); | ||
| const fd = fs.openSync(filename, 'r'); | ||
|
|
||
|
|
||
| wrongInputs.forEach((wrongInput) => { | ||
| assert.throws( | ||
| () => fs.readv(fd, wrongInput, null, common.mustNotCall()), { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| name: 'TypeError' | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| fs.closeSync(fd); | ||
| } | ||
|
|
||
| { | ||
| // fs.readv with wrong fd argument | ||
| wrongInputs.forEach((wrongInput) => { | ||
| assert.throws( | ||
| () => fs.readv(wrongInput, common.mustNotCall()), | ||
| { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| name: 'TypeError' | ||
| } | ||
| ); | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.