From 12a1191727b583cab539f7e0b13e2398f102fc78 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 5 Dec 2019 07:51:47 +0100 Subject: [PATCH] readline: eagerly load string_decoder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There was no point in lazy loading the string_decoder, since it would be used in all cases anyway. PR-URL: https://github.com/nodejs/node/pull/30807 Reviewed-By: Anna Henningsen Reviewed-By: Michaƫl Zasso Reviewed-By: Yongsheng Zhang Reviewed-By: Rich Trott Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- lib/readline.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/readline.js b/lib/readline.js index e847ce961f53ad..95402020f07d71 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -55,8 +55,7 @@ const { kClearScreenDown } = CSI; -// Lazy load StringDecoder for startup performance. -let StringDecoder; +const { StringDecoder } = require('string_decoder'); // Lazy load Readable for startup performance. let Readable; @@ -84,9 +83,6 @@ function Interface(input, output, completer, terminal) { return new Interface(input, output, completer, terminal); } - if (StringDecoder === undefined) - StringDecoder = require('string_decoder').StringDecoder; - this._sawReturnAt = 0; this.isCompletionEnabled = true; this._sawKeyPress = false; @@ -1122,8 +1118,6 @@ Interface.prototype[Symbol.asyncIterator] = function() { function emitKeypressEvents(stream, iface) { if (stream[KEYPRESS_DECODER]) return; - if (StringDecoder === undefined) - StringDecoder = require('string_decoder').StringDecoder; stream[KEYPRESS_DECODER] = new StringDecoder('utf8'); stream[ESCAPE_DECODER] = emitKeys(stream); @@ -1138,8 +1132,11 @@ function emitKeypressEvents(stream, iface) { if (r) { clearTimeout(timeoutId); + let escapeTimeout = ESCAPE_CODE_TIMEOUT; + if (iface) { iface._sawKeyPress = r.length === 1; + escapeTimeout = iface.escapeCodeTimeout; } for (let i = 0; i < r.length; i++) { @@ -1151,10 +1148,7 @@ function emitKeypressEvents(stream, iface) { stream[ESCAPE_DECODER].next(r[i]); // Escape letter at the tail position if (r[i] === kEscape && i + 1 === r.length) { - timeoutId = setTimeout( - escapeCodeTimeout, - iface ? iface.escapeCodeTimeout : ESCAPE_CODE_TIMEOUT - ); + timeoutId = setTimeout(escapeCodeTimeout, escapeTimeout); } } catch (err) { // If the generator throws (it could happen in the `keypress`