• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Regression test for https://github.com/nodejs/node/issues/13557
4// Tests that multiple subsequent readline instances can re-use an input stream.
5
6const common = require('../common');
7const assert = require('assert');
8const readline = require('readline');
9const { PassThrough } = require('stream');
10
11const input = new PassThrough();
12const output = new PassThrough();
13
14const rl1 = readline.createInterface({
15  input,
16  output,
17  terminal: true
18});
19
20rl1.on('line', common.mustCall(rl1OnLine));
21
22// Write a line plus the first byte of a UTF-8 multibyte character to make sure
23// that it doesn’t get lost when closing the readline instance.
24input.write(Buffer.concat([
25  Buffer.from('foo\n'),
26  Buffer.from([ 0xe2 ]),  // Exactly one third of a ☃ snowman.
27]));
28
29function rl1OnLine(line) {
30  assert.strictEqual(line, 'foo');
31  rl1.close();
32  const rl2 = readline.createInterface({
33    input,
34    output,
35    terminal: true
36  });
37
38  rl2.on('line', common.mustCall((line) => {
39    assert.strictEqual(line, '☃bar');
40    rl2.close();
41  }));
42  input.write(Buffer.from([0x98, 0x83])); // The rest of the ☃ snowman.
43  input.write('bar\n');
44}
45