Skip to content

Commit 0ace8f9

Browse files
BridgeARMylesBorins
authored andcommitted
string_decoder: lazy loaded
PR-URL: #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>
1 parent 526163c commit 0ace8f9

4 files changed

Lines changed: 22 additions & 4 deletions

File tree

‎lib/_stream_readable.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ const {
4040
}=require('internal/errors').codes;
4141
constReadableAsyncIterator=require('internal/streams/async_iterator');
4242
const{ emitExperimentalWarning }=require('internal/util');
43-
varStringDecoder;
43+
44+
// Lazy loaded to improve the startup performance.
45+
letStringDecoder;
4446

4547
util.inherits(Readable,Stream);
4648

‎lib/internal/child_process.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const {
1414
ERR_MISSING_ARGS
1515
}
1616
}=require('internal/errors');
17-
const{ StringDecoder }=require('string_decoder');
1817
constEventEmitter=require('events');
1918
constnet=require('net');
2019
constdgram=require('dgram');
@@ -47,6 +46,9 @@ const {
4746

4847
const{ SocketListSend, SocketListReceive }=SocketList;
4948

49+
// Lazy loaded for startup performance.
50+
letStringDecoder;
51+
5052
constMAX_HANDLE_RETRANSMISSIONS=3;
5153

5254
// this object contain function to convert TCP objects to native handle objects
@@ -476,6 +478,8 @@ function setupChannel(target, channel) {
476478

477479
constcontrol=newControl(channel);
478480

481+
if(StringDecoder===undefined)
482+
StringDecoder=require('string_decoder').StringDecoder;
479483
vardecoder=newStringDecoder('utf8');
480484
varjsonBuffer='';
481485
varpendingHandle=null;

‎lib/internal/crypto/cipher.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ const {
2828

2929
constassert=require('assert');
3030
constLazyTransform=require('internal/streams/lazy_transform');
31-
const{ StringDecoder }=require('string_decoder');
3231

3332
const{ inherits }=require('util');
3433
const{ deprecate, normalizeEncoding }=require('internal/util');
3534

35+
// Lazy loaded for startup performance.
36+
letStringDecoder;
37+
3638
functionrsaFunctionFor(method,defaultPadding){
3739
returnfunction(options,buffer){
3840
constkey=options.key||options;
@@ -49,6 +51,8 @@ const privateDecrypt = rsaFunctionFor(_privateDecrypt, RSA_PKCS1_OAEP_PADDING);
4951

5052
functiongetDecoder(decoder,encoding){
5153
encoding=normalizeEncoding(encoding);
54+
if(StringDecoder===undefined)
55+
StringDecoder=require('string_decoder').StringDecoder;
5256
decoder=decoder||newStringDecoder(encoding);
5357
assert(decoder.encoding===encoding,'Cannot change encoding');
5458
returndecoder;

‎lib/readline.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const {
3535
const{ debug, inherits }=require('util');
3636
const{ Buffer }=require('buffer');
3737
constEventEmitter=require('events');
38-
const{ StringDecoder }=require('string_decoder');
3938
const{
4039
CSI,
4140
emitKeys,
@@ -52,6 +51,9 @@ const {
5251
kClearScreenDown
5352
}=CSI;
5453

54+
// Lazy load StringDecoder for startup performance.
55+
letStringDecoder;
56+
5557
constkHistorySize=30;
5658
constkMincrlfDelay=100;
5759
// \r\n, \n, or \r followed by something other than \n
@@ -73,6 +75,9 @@ function Interface(input, output, completer, terminal) {
7375
returnnewInterface(input,output,completer,terminal);
7476
}
7577

78+
if(StringDecoder===undefined)
79+
StringDecoder=require('string_decoder').StringDecoder;
80+
7681
this._sawReturnAt=0;
7782
this.isCompletionEnabled=true;
7883
this._sawKeyPress=false;
@@ -987,6 +992,9 @@ Interface.prototype._ttyWrite = function(s, key) {
987992

988993
functionemitKeypressEvents(stream,iface){
989994
if(stream[KEYPRESS_DECODER])return;
995+
996+
if(StringDecoder===undefined)
997+
StringDecoder=require('string_decoder').StringDecoder;
990998
stream[KEYPRESS_DECODER]=newStringDecoder('utf8');
991999

9921000
stream[ESCAPE_DECODER]=emitKeys(stream);

0 commit comments

Comments
 (0)