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 <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
Antoine du Hamel 2023-01-24 13:10:11 +01:00 committed by GitHub
parent 7dd4583825
commit e8d40154d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 42 deletions

View File

@ -590,6 +590,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') ?

View File

@ -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);
});
});

View File

@ -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);
}));