• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const { Readable } = require('stream');
6const readline = require('readline');
7
8const CONTENT = 'content';
9const TOTAL_LINES = 18;
10
11(async () => {
12  const readable = new Readable({ read() {} });
13  readable.push(`${CONTENT}\n`.repeat(TOTAL_LINES));
14
15  const rli = readline.createInterface({
16    input: readable,
17    crlfDelay: Infinity
18  });
19
20  const it = rli[Symbol.asyncIterator]();
21  const highWaterMark = it.stream.readableHighWaterMark;
22
23  // For this test to work, we have to queue up more than the number of
24  // highWaterMark items in rli. Make sure that is the case.
25  assert(TOTAL_LINES > highWaterMark);
26
27  let iterations = 0;
28  let readableEnded = false;
29  for await (const line of it) {
30    assert.strictEqual(readableEnded, false);
31
32    assert.strictEqual(line, CONTENT);
33
34    const expectedPaused = TOTAL_LINES - iterations > highWaterMark;
35    assert.strictEqual(readable.isPaused(), expectedPaused);
36
37    iterations += 1;
38
39    // We have to end the input stream asynchronously for back pressure to work.
40    // Only end when we have reached the final line.
41    if (iterations === TOTAL_LINES) {
42      readable.push(null);
43      readableEnded = true;
44    }
45  }
46
47  assert.strictEqual(iterations, TOTAL_LINES);
48})().then(common.mustCall());
49