Skip to content

Commit 2f1ae4f

Browse files
BridgeARtargos
authored andcommitted
readline: eagerly load string_decoder
There was no point in lazy loading the string_decoder, since it would be used in all cases anyway. PR-URL: #30807 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 8ad53ab commit 2f1ae4f

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

lib/readline.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ const {
6464
kClearScreenDown
6565
} = CSI;
6666

67-
// Lazy load StringDecoder for startup performance.
68-
let StringDecoder;
67+
const { StringDecoder } = require('string_decoder');
6968

7069
// Lazy load Readable for startup performance.
7170
let Readable;
@@ -93,9 +92,6 @@ function Interface(input, output, completer, terminal) {
9392
return new Interface(input, output, completer, terminal);
9493
}
9594

96-
if (StringDecoder === undefined)
97-
StringDecoder = require('string_decoder').StringDecoder;
98-
9995
this._sawReturnAt = 0;
10096
this.isCompletionEnabled = true;
10197
this._sawKeyPress = false;
@@ -1131,8 +1127,6 @@ Interface.prototype[Symbol.asyncIterator] = function() {
11311127
function emitKeypressEvents(stream, iface) {
11321128
if (stream[KEYPRESS_DECODER]) return;
11331129

1134-
if (StringDecoder === undefined)
1135-
StringDecoder = require('string_decoder').StringDecoder;
11361130
stream[KEYPRESS_DECODER] = new StringDecoder('utf8');
11371131

11381132
stream[ESCAPE_DECODER] = emitKeys(stream);
@@ -1147,8 +1141,11 @@ function emitKeypressEvents(stream, iface) {
11471141
if (r) {
11481142
clearTimeout(timeoutId);
11491143

1144+
let escapeTimeout = ESCAPE_CODE_TIMEOUT;
1145+
11501146
if (iface) {
11511147
iface._sawKeyPress = r.length === 1;
1148+
escapeTimeout = iface.escapeCodeTimeout;
11521149
}
11531150

11541151
for (let i = 0; i < r.length; i++) {
@@ -1160,10 +1157,7 @@ function emitKeypressEvents(stream, iface) {
11601157
stream[ESCAPE_DECODER].next(r[i]);
11611158
// Escape letter at the tail position
11621159
if (r[i] === kEscape && i + 1 === r.length) {
1163-
timeoutId = setTimeout(
1164-
escapeCodeTimeout,
1165-
iface ? iface.escapeCodeTimeout : ESCAPE_CODE_TIMEOUT
1166-
);
1160+
timeoutId = setTimeout(escapeCodeTimeout, escapeTimeout);
11671161
}
11681162
} catch (err) {
11691163
// If the generator throws (it could happen in the `keypress`

0 commit comments

Comments
 (0)