Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ function on(emitter, event, options = kEmptyObject) {
const value = unconsumedEvents.shift();
size--;
if (paused && size < lowWatermark) {
emitter.resume();
emitter.resume(); // Can not be finished yet
paused = false;
}
return PromiseResolve(createIterResult(value, false));
Expand Down Expand Up @@ -1188,6 +1188,7 @@ function on(emitter, event, options = kEmptyObject) {
abortListenerDisposable?.[SymbolDispose]();
removeAll();
finished = true;
paused = false;
const doneResult = createIterResult(undefined, true);
while (!unconsumedPromises.isEmpty()) {
unconsumedPromises.shift().resolve(doneResult);
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-readline-async-iterators.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const testContents = [
'line 1',
'line 1\nline 2 南越国是前203年至前111年存在于岭南地区的一个国家\nline 3\ntrailing',
'line 1\nline 2\nline 3 ends with newline\n',
Array(1e4).fill(0).map((_, i) => i).join('\n'), // More that 2 * highWaterMark
];

async function testSimple() {
Expand All @@ -43,6 +44,29 @@ async function testSimple() {
}
}

// Same as testSimple, but with Readable.from() instead of fs.createReadStream
async function testReadableFrom() {
for (const fileContent of testContents) {
const readable = Readable.from([fileContent]);
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/g, ''));
}
}

async function testMutual() {
for (const fileContent of testContents) {
fs.writeFileSync(filename, fileContent);
Expand Down Expand Up @@ -115,6 +139,7 @@ async function testSlowStreamForLeaks() {
}

testSimple()
.then(testReadableFrom)
.then(testMutual)
.then(testSlowStreamForLeaks)
.then(common.mustCall());
Loading