From 2f7319e3875138ff0b4509ffc804e9824ae3720c Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 24 Jan 2023 13:10:11 +0100 Subject: [PATCH] readline: fix detection of carriage return Fixes: https://github.com/nodejs/node/issues/45992 PR-URL: https://github.com/nodejs/node/pull/46306 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Luigi Pinca Reviewed-By: Yagiz Nizipli --- lib/internal/readline/interface.js | 1 + .../test-readline-big-file-carriage-return.js | 42 ------------------- ...readline-carriage-return-between-chunks.js | 23 ++++++++++ 3 files changed, 24 insertions(+), 42 deletions(-) delete mode 100644 test/known_issues/test-readline-big-file-carriage-return.js create mode 100644 test/parallel/test-readline-carriage-return-between-chunks.js diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js index 937f426615c1b9..2e9d43626be402 100644 --- a/lib/internal/readline/interface.js +++ b/lib/internal/readline/interface.js @@ -597,6 +597,7 @@ class Interface extends InterfaceConstructor { if (this[kLine_buffer]) { string = this[kLine_buffer] + string; this[kLine_buffer] = null; + lineEnding.lastIndex = 0; // Start the search from the beginning of the string. newPartContainsEnding = RegExpPrototypeExec(lineEnding, string); } this[kSawReturnAt] = StringPrototypeEndsWith(string, '\r') ? diff --git a/test/known_issues/test-readline-big-file-carriage-return.js b/test/known_issues/test-readline-big-file-carriage-return.js deleted file mode 100644 index 7dfc918927eb26..00000000000000 --- a/test/known_issues/test-readline-big-file-carriage-return.js +++ /dev/null @@ -1,42 +0,0 @@ -'use strict'; - -// Context: https://github.com/nodejs/node/issues/45992 - -require('../common'); - -const assert = require('assert'); -const fs = require('fs'); -const readline = require('readline'); - -const tmpdir = require('../common/tmpdir'); - -tmpdir.refresh(); -fs.mkdtempSync(`${tmpdir.path}/`); -const path = `${tmpdir.path}/foo`; -const writeStream = fs.createWriteStream(path); - -function write(iteration, callback) { - for (; iteration < 16384; iteration += 1) { - if (!writeStream.write('foo\r\n')) { - writeStream.once('drain', () => write(iteration + 1, callback)); - return; - } - } - - writeStream.end(); - callback(); -} - -write(0, () => { - const input = fs.createReadStream(path); - const rl = readline.createInterface({ input, crlfDelay: Infinity }); - let carriageReturns = 0; - - rl.on('line', (x) => { - if (x.includes('\r')) carriageReturns += 1; - }); - - rl.on('close', () => { - assert.strictEqual(carriageReturns, 0); - }); -}); diff --git a/test/parallel/test-readline-carriage-return-between-chunks.js b/test/parallel/test-readline-carriage-return-between-chunks.js new file mode 100644 index 00000000000000..aff012de5a9e5c --- /dev/null +++ b/test/parallel/test-readline-carriage-return-between-chunks.js @@ -0,0 +1,23 @@ +'use strict'; + +const common = require('../common'); + +const assert = require('node:assert'); +const readline = require('node:readline'); +const { Readable } = require('node:stream'); + + +const input = Readable.from((function*() { + yield 'a\nb'; + yield '\r\n'; +})()); +const rl = readline.createInterface({ input, crlfDelay: Infinity }); +let carriageReturns = 0; + +rl.on('line', (line) => { + if (line.includes('\r')) carriageReturns++; +}); + +rl.on('close', common.mustCall(() => { + assert.strictEqual(carriageReturns, 0); +}));