From fe54bc7b6926fa51a39c431ffe350bd02faf53e9 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 7 Aug 2017 17:11:06 -0700 Subject: [PATCH] lib: use Timer.now() in readline module Using Date.now() introduces problems when operating under load or otherwise with constrained resources. Use Timer.now() to mitigate. The problem was identified in `test-readline-interface` where under heavy load, `\r` and `\n` were received so far apart that they were treated as separate line endings rather than a single line ending. Switching to `Timer.now()` prevented this from happening. PR-URL: https://github.com/nodejs/node/pull/14681 Refs: https://github.com/nodejs/node/issues/14674 Reviewed-By: Colin Ihrig Reviewed-By: Refael Ackermann --- lib/readline.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/readline.js b/lib/readline.js index 57f9e7d6e8c849..5e96a04b1c5e98 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -48,6 +48,8 @@ const { kClearScreenDown } = CSI; +const now = process.binding('timer_wrap').Timer.now; + const kHistorySize = 30; const kMincrlfDelay = 100; // \r\n, \n, or \r followed by something other than \n @@ -409,7 +411,7 @@ Interface.prototype._normalWrite = function(b) { } var string = this._decoder.write(b); if (this._sawReturnAt && - Date.now() - this._sawReturnAt <= this.crlfDelay) { + now() - this._sawReturnAt <= this.crlfDelay) { string = string.replace(/^\n/, ''); this._sawReturnAt = 0; } @@ -422,7 +424,7 @@ Interface.prototype._normalWrite = function(b) { this._line_buffer = null; } if (newPartContainsEnding) { - this._sawReturnAt = string.endsWith('\r') ? Date.now() : 0; + this._sawReturnAt = string.endsWith('\r') ? now() : 0; // got one or more newlines; process into "line" events var lines = string.split(lineEnding); @@ -916,14 +918,14 @@ Interface.prototype._ttyWrite = function(s, key) { switch (key.name) { case 'return': // carriage return, i.e. \r - this._sawReturnAt = Date.now(); + this._sawReturnAt = now(); this._line(); break; case 'enter': // When key interval > crlfDelay if (this._sawReturnAt === 0 || - Date.now() - this._sawReturnAt > this.crlfDelay) { + now() - this._sawReturnAt > this.crlfDelay) { this._line(); } this._sawReturnAt = 0;