Skip to content
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

Revert "readline: clean up event listener in onNewListener" #13560

Closed
wants to merge 2 commits into from
Closed
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: 0 additions & 3 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,9 +1039,6 @@ function emitKeypressEvents(stream, iface) {
} else {
stream.on('newListener', onNewListener);
}
if (iface) {
iface.once('close', () => { stream.removeListener('data', onData); });
}
}

/**
Expand Down
44 changes: 44 additions & 0 deletions test/parallel/test-readline-reopen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

// Regression test for https://github.com/nodejs/node/issues/13557
// Tests that multiple subsequent readline instances can re-use an input stream.

const common = require('../common');
const assert = require('assert');
const readline = require('readline');
const { PassThrough } = require('stream');

const input = new PassThrough();
const output = new PassThrough();

const rl1 = readline.createInterface({
input,
output,
terminal: true
});

rl1.on('line', common.mustCall(rl1OnLine));

// Write a line plus the first byte of a UTF-8 multibyte character to make sure
// that it doesn’t get lost when closing the readline instance.
input.write(Buffer.concat([
Buffer.from('foo\n'),
Buffer.from([ 0xe2 ]) // Exactly one third of a ☃ snowman.
]));

function rl1OnLine(line) {
assert.strictEqual(line, 'foo');
rl1.close();
const rl2 = readline.createInterface({
input,
output,
terminal: true
});

rl2.on('line', common.mustCall((line) => {
assert.strictEqual(line, '☃bar');
rl2.close();
}));
input.write(Buffer.from([0x98, 0x83])); // The rest of the ☃ snowman.
input.write('bar\n');
}
6 changes: 2 additions & 4 deletions test/parallel/test-readline-set-raw-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ assert(!rawModeCalled);
assert(resumeCalled);
assert(!pauseCalled);

// One data listener for the keypress events.
assert.strictEqual(stream.listeners('data').length, 1);

// close() should call setRawMode(false)
expectedRawMode = false;
Expand All @@ -88,5 +86,5 @@ assert(!resumeCalled);
assert(pauseCalled);

assert.deepStrictEqual(stream.listeners('keypress'), []);
// Data listener is removed once interface is closed.
assert.strictEqual(stream.listeners('data').length, 0);
// one data listener for the keypress events.
assert.strictEqual(stream.listeners('data').length, 1);