GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — readline: use Date.now() and move test to parallel · nodejs/node@1faae90

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • readline.js
  • test
    • parallel
      • test-readline-interface.js
    • sequential
      • test-readline-interface.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/readline.js‎

Copy file name to clipboardExpand all lines: lib/readline.js
+4-6Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ const {
4747
kClearScreenDown
4848
} = CSI;
4949

50-
const { now } = process.binding('timer_wrap').Timer;
51-
5250
const kHistorySize = 30;
5351
const kMincrlfDelay = 100;
5452
// \r\n, \n, or \r followed by something other than \n
@@ -400,7 +398,7 @@ Interface.prototype._normalWrite = function(b) {
400398
}
401399
var string = this._decoder.write(b);
402400
if (this._sawReturnAt &&
403-
now() - this._sawReturnAt <= this.crlfDelay) {
401+
Date.now() - this._sawReturnAt <= this.crlfDelay) {
404402
string = string.replace(/^\n/, '');
405403
this._sawReturnAt = 0;
406404
}
@@ -413,7 +411,7 @@ Interface.prototype._normalWrite = function(b) {
413411
this._line_buffer = null;
414412
}
415413
if (newPartContainsEnding) {
416-
this._sawReturnAt = string.endsWith('\r') ? now() : 0;
414+
this._sawReturnAt = string.endsWith('\r') ? Date.now() : 0;
417415

418416
// got one or more newlines; process into "line" events
419417
var lines = string.split(lineEnding);
@@ -908,14 +906,14 @@ Interface.prototype._ttyWrite = function(s, key) {
908906

909907
switch (key.name) {
910908
case 'return': // carriage return, i.e. \r
911-
this._sawReturnAt = now();
909+
this._sawReturnAt = Date.now();
912910
this._line();
913911
break;
914912

915913
case 'enter':
916914
// When key interval > crlfDelay
917915
if (this._sawReturnAt === 0 ||
918-
now() - this._sawReturnAt > this.crlfDelay) {
916+
Date.now() - this._sawReturnAt > this.crlfDelay) {
919917
this._line();
920918
}
921919
this._sawReturnAt = 0;
Collapse file

‎test/parallel/test-readline-interface.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-readline-interface.js
+77Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,3 +873,80 @@ function isWarned(emitter) {
873873
assert.strictEqual(rl._prompt, '$ ');
874874
}
875875
});
876+
877+
// For the purposes of the following tests, we do not care about the exact
878+
// value of crlfDelay, only that the behaviour conforms to what's expected.
879+
// Setting it to Infinity allows the test to succeed even under extreme
880+
// CPU stress.
881+
const crlfDelay = Infinity;
882+
883+
[ true, false ].forEach(function(terminal) {
884+
// sending multiple newlines at once that does not end with a new line
885+
// and a `end` event(last line is)
886+
887+
// \r\n should emit one line event, not two
888+
{
889+
const fi = new FakeInput();
890+
const rli = new readline.Interface(
891+
{
892+
input: fi,
893+
output: fi,
894+
terminal: terminal,
895+
crlfDelay
896+
}
897+
);
898+
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
899+
let callCount = 0;
900+
rli.on('line', function(line) {
901+
assert.strictEqual(line, expectedLines[callCount]);
902+
callCount++;
903+
});
904+
fi.emit('data', expectedLines.join('\r\n'));
905+
assert.strictEqual(callCount, expectedLines.length - 1);
906+
rli.close();
907+
}
908+
909+
// \r\n should emit one line event when split across multiple writes.
910+
{
911+
const fi = new FakeInput();
912+
const rli = new readline.Interface({
913+
input: fi,
914+
output: fi,
915+
terminal: terminal,
916+
crlfDelay
917+
});
918+
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
919+
let callCount = 0;
920+
rli.on('line', function(line) {
921+
assert.strictEqual(line, expectedLines[callCount]);
922+
callCount++;
923+
});
924+
expectedLines.forEach(function(line) {
925+
fi.emit('data', `${line}\r`);
926+
fi.emit('data', '\n');
927+
});
928+
assert.strictEqual(callCount, expectedLines.length);
929+
rli.close();
930+
}
931+
932+
// Emit one line event when the delay between \r and \n is
933+
// over the default crlfDelay but within the setting value.
934+
{
935+
const fi = new FakeInput();
936+
const delay = 125;
937+
const rli = new readline.Interface({
938+
input: fi,
939+
output: fi,
940+
terminal: terminal,
941+
crlfDelay
942+
});
943+
let callCount = 0;
944+
rli.on('line', () => callCount++);
945+
fi.emit('data', '\r');
946+
setTimeout(common.mustCall(() => {
947+
fi.emit('data', '\n');
948+
assert.strictEqual(callCount, 1);
949+
rli.close();
950+
}), delay);
951+
}
952+
});
Collapse file

‎test/sequential/test-readline-interface.js‎

Copy file name to clipboardExpand all lines: test/sequential/test-readline-interface.js
-108Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.