Skip to content

Commit

Permalink
string_decoder: lazy loaded
Browse files Browse the repository at this point in the history
PR-URL: #20567
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
  • Loading branch information
BridgeAR authored and MylesBorins committed May 22, 2018
1 parent 526163c commit 0ace8f9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 5 additions & 1 deletion lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/crypto/cipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 { deprecate, normalizeEncoding } = require('internal/util');

// Lazy loaded for startup performance.
let StringDecoder;

function rsaFunctionFor(method, defaultPadding) {
return function(options, buffer) {
const key = options.key || options;
Expand All @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0ace8f9

Please sign in to comment.