-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
readline: add support for async iteration #23916
Closed
Closed
Changes from all commits
Commits
Show all changes
2 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
There are no files selected for viewing
This file contains 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 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
48 changes: 48 additions & 0 deletions
48
test/parallel/test-readline-async-iterators-backpressure.js
This file contains 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,48 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { Readable } = require('stream'); | ||
const readline = require('readline'); | ||
|
||
const CONTENT = 'content'; | ||
const TOTAL_LINES = 18; | ||
|
||
(async () => { | ||
const readable = new Readable({ read() {} }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at this now (to solve another bug) I think this backpressure behaviour is confusing since other consumers can listen to |
||
readable.push(`${CONTENT}\n`.repeat(TOTAL_LINES)); | ||
|
||
const rli = readline.createInterface({ | ||
input: readable, | ||
crlfDelay: Infinity | ||
}); | ||
|
||
const it = rli[Symbol.asyncIterator](); | ||
const highWaterMark = it.stream.readableHighWaterMark; | ||
|
||
// For this test to work, we have to queue up more than the number of | ||
// highWaterMark items in rli. Make sure that is the case. | ||
assert(TOTAL_LINES > highWaterMark); | ||
|
||
let iterations = 0; | ||
let readableEnded = false; | ||
for await (const line of it) { | ||
assert.strictEqual(readableEnded, false); | ||
|
||
assert.strictEqual(line, CONTENT); | ||
|
||
const expectedPaused = TOTAL_LINES - iterations > highWaterMark; | ||
assert.strictEqual(readable.isPaused(), expectedPaused); | ||
|
||
iterations += 1; | ||
|
||
// We have to end the input stream asynchronously for back pressure to work. | ||
// Only end when we have reached the final line. | ||
if (iterations === TOTAL_LINES) { | ||
readable.push(null); | ||
readableEnded = true; | ||
} | ||
} | ||
|
||
assert.strictEqual(iterations, TOTAL_LINES); | ||
})().then(common.mustCall()); |
This file contains 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,78 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fs = require('fs'); | ||
const { join } = require('path'); | ||
const readline = require('readline'); | ||
const assert = require('assert'); | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const filename = join(tmpdir.path, 'test.txt'); | ||
|
||
const testContents = [ | ||
'', | ||
'\n', | ||
'line 1', | ||
'line 1\nline 2 南越国是前203年至前111年存在于岭南地区的一个国家\nline 3\ntrailing', | ||
'line 1\nline 2\nline 3 ends with newline\n' | ||
]; | ||
|
||
async function testSimpleDestroy() { | ||
for (const fileContent of testContents) { | ||
fs.writeFileSync(filename, fileContent); | ||
|
||
const readable = fs.createReadStream(filename); | ||
const rli = readline.createInterface({ | ||
input: readable, | ||
crlfDelay: Infinity | ||
}); | ||
|
||
const iteratedLines = []; | ||
for await (const k of rli) { | ||
iteratedLines.push(k); | ||
break; | ||
} | ||
|
||
const expectedLines = fileContent.split('\n'); | ||
if (expectedLines[expectedLines.length - 1] === '') { | ||
expectedLines.pop(); | ||
} | ||
expectedLines.splice(1); | ||
|
||
assert.deepStrictEqual(iteratedLines, expectedLines); | ||
} | ||
} | ||
|
||
async function testMutualDestroy() { | ||
for (const fileContent of testContents) { | ||
fs.writeFileSync(filename, fileContent); | ||
|
||
const readable = fs.createReadStream(filename); | ||
const rli = readline.createInterface({ | ||
input: readable, | ||
crlfDelay: Infinity | ||
}); | ||
|
||
const expectedLines = fileContent.split('\n'); | ||
if (expectedLines[expectedLines.length - 1] === '') { | ||
expectedLines.pop(); | ||
} | ||
expectedLines.splice(2); | ||
|
||
const iteratedLines = []; | ||
for await (const k of rli) { | ||
iteratedLines.push(k); | ||
for await (const l of rli) { | ||
iteratedLines.push(l); | ||
break; | ||
} | ||
assert.deepStrictEqual(iteratedLines, expectedLines); | ||
} | ||
|
||
assert.deepStrictEqual(iteratedLines, expectedLines); | ||
} | ||
} | ||
|
||
testSimpleDestroy().then(testMutualDestroy).then(common.mustCall()); |
This file contains 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,77 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fs = require('fs'); | ||
const { join } = require('path'); | ||
const readline = require('readline'); | ||
const assert = require('assert'); | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const filename = join(tmpdir.path, 'test.txt'); | ||
|
||
const testContents = [ | ||
'', | ||
'\n', | ||
'line 1', | ||
'line 1\nline 2 南越国是前203年至前111年存在于岭南地区的一个国家\nline 3\ntrailing', | ||
'line 1\nline 2\nline 3 ends with newline\n' | ||
]; | ||
|
||
async function testSimple() { | ||
for (const fileContent of testContents) { | ||
fs.writeFileSync(filename, fileContent); | ||
|
||
const readable = fs.createReadStream(filename); | ||
const rli = readline.createInterface({ | ||
input: readable, | ||
crlfDelay: Infinity | ||
}); | ||
|
||
const iteratedLines = []; | ||
for await (const k of rli) { | ||
iteratedLines.push(k); | ||
} | ||
|
||
const expectedLines = fileContent.split('\n'); | ||
if (expectedLines[expectedLines.length - 1] === '') { | ||
expectedLines.pop(); | ||
} | ||
assert.deepStrictEqual(iteratedLines, expectedLines); | ||
assert.strictEqual(iteratedLines.join(''), fileContent.replace(/\n/gm, '')); | ||
} | ||
} | ||
|
||
async function testMutual() { | ||
for (const fileContent of testContents) { | ||
fs.writeFileSync(filename, fileContent); | ||
|
||
const readable = fs.createReadStream(filename); | ||
const rli = readline.createInterface({ | ||
input: readable, | ||
crlfDelay: Infinity | ||
}); | ||
|
||
const expectedLines = fileContent.split('\n'); | ||
if (expectedLines[expectedLines.length - 1] === '') { | ||
expectedLines.pop(); | ||
} | ||
const iteratedLines = []; | ||
let iterated = false; | ||
for await (const k of rli) { | ||
// This outer loop should only iterate once. | ||
assert.strictEqual(iterated, false); | ||
iterated = true; | ||
|
||
iteratedLines.push(k); | ||
for await (const l of rli) { | ||
iteratedLines.push(l); | ||
} | ||
assert.deepStrictEqual(iteratedLines, expectedLines); | ||
} | ||
assert.deepStrictEqual(iteratedLines, expectedLines); | ||
} | ||
} | ||
|
||
testSimple().then(testMutual).then(common.mustCall()); |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense expose this as a separate method? converting to a stream might be an issue for multiple people.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I consider this an implementation detail of
@@asyncIterator
method. A major reason of why the performance of this method isn't up to par to'line'
event, as you have noted in #23916 (comment), is because of the double buffering necessitated by the intermediate stream, so I'd rather not expose the stream at the moment.