-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
readline: add support for async iteration
Co-authored-by: Ivan Filenko <ivan.filenko@protonmail.com> Fixes: #18603 Refs: #18904 PR-URL: #23916 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
- Loading branch information
1 parent
74f4741
commit d3f15b0
Showing
6 changed files
with
315 additions
and
3 deletions.
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() {} }); | ||
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