Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readline: eagerly load string_decoder #30807

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions lib/readline.js
Expand Up @@ -64,8 +64,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;
Expand Down Expand Up @@ -93,9 +92,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;
Expand Down Expand Up @@ -1131,8 +1127,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);
Expand All @@ -1147,8 +1141,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++) {
Expand All @@ -1160,10 +1157,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`
Expand Down