string_decoder: lazy loaded

PR-URL: https://github.com/nodejs/node/pull/20567
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
This commit is contained in:
Ruben Bridgewater 2018-05-06 23:08:00 +02:00
parent 11892b0b64
commit f4f8b22f7d
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
4 changed files with 22 additions and 4 deletions

View File

@ -40,7 +40,9 @@ const {
} = require('internal/errors').codes;
const ReadableAsyncIterator = require('internal/streams/async_iterator');
const { emitExperimentalWarning } = require('internal/util');
var StringDecoder;
// Lazy loaded to improve the startup performance.
let StringDecoder;
util.inherits(Readable, Stream);

View File

@ -14,7 +14,6 @@ const {
ERR_MISSING_ARGS
}
} = require('internal/errors');
const { StringDecoder } = require('string_decoder');
const EventEmitter = require('events');
const net = require('net');
const dgram = require('dgram');
@ -47,6 +46,9 @@ const {
const { SocketListSend, SocketListReceive } = SocketList;
// Lazy loaded for startup performance.
let StringDecoder;
const MAX_HANDLE_RETRANSMISSIONS = 3;
// this object contain function to convert TCP objects to native handle objects
@ -476,6 +478,8 @@ function setupChannel(target, channel) {
const control = new Control(channel);
if (StringDecoder === undefined)
StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');
var jsonBuffer = '';
var pendingHandle = null;

View File

@ -28,11 +28,13 @@ const {
const assert = require('assert');
const LazyTransform = require('internal/streams/lazy_transform');
const { StringDecoder } = require('string_decoder');
const { inherits } = require('util');
const { normalizeEncoding } = require('internal/util');
// Lazy loaded for startup performance.
let StringDecoder;
function rsaFunctionFor(method, defaultPadding) {
return function(options, buffer) {
const key = options.key || options;
@ -49,6 +51,8 @@ const privateDecrypt = rsaFunctionFor(_privateDecrypt, RSA_PKCS1_OAEP_PADDING);
function getDecoder(decoder, encoding) {
encoding = normalizeEncoding(encoding);
if (StringDecoder === undefined)
StringDecoder = require('string_decoder').StringDecoder;
decoder = decoder || new StringDecoder(encoding);
assert(decoder.encoding === encoding, 'Cannot change encoding');
return decoder;

View File

@ -35,7 +35,6 @@ const {
const { debug, inherits } = require('util');
const { Buffer } = require('buffer');
const EventEmitter = require('events');
const { StringDecoder } = require('string_decoder');
const {
CSI,
emitKeys,
@ -52,6 +51,9 @@ const {
kClearScreenDown
} = CSI;
// Lazy load StringDecoder for startup performance.
let StringDecoder;
const kHistorySize = 30;
const kMincrlfDelay = 100;
// \r\n, \n, or \r followed by something other than \n
@ -73,6 +75,9 @@ 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;
@ -987,6 +992,9 @@ Interface.prototype._ttyWrite = function(s, key) {
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);