Skip to content

Commit 7d3a8cb

Browse files
joyeecheungdanielleadams
authored andcommitted
lib: remove unnecessary lazy loads
Now that more modules are included in the snapshot, it's not necessary to lazy load them anymore PR-URL: #38737 Refs: #35711 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 5d9442a commit 7d3a8cb

File tree

6 files changed

+13
-30
lines changed

6 files changed

+13
-30
lines changed

lib/internal/crypto/cipher.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ const LazyTransform = require('internal/streams/lazy_transform');
6060

6161
const { normalizeEncoding } = require('internal/util');
6262

63-
// Lazy loaded for startup performance.
64-
let StringDecoder;
63+
const { StringDecoder } = require('string_decoder');
6564

6665
function rsaFunctionFor(method, defaultPadding, keyType) {
6766
return (options, buffer) => {
@@ -93,8 +92,6 @@ const privateDecrypt = rsaFunctionFor(_privateDecrypt, RSA_PKCS1_OAEP_PADDING,
9392

9493
function getDecoder(decoder, encoding) {
9594
encoding = normalizeEncoding(encoding);
96-
if (StringDecoder === undefined)
97-
StringDecoder = require('string_decoder').StringDecoder;
9895
decoder = decoder || new StringDecoder(encoding);
9996
assert(decoder.encoding === encoding, 'Cannot change encoding');
10097
return decoder;

lib/internal/streams/pipeline.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {
88
SymbolAsyncIterator,
99
} = primordials;
1010

11-
let eos;
11+
const eos = require('internal/streams/end-of-stream');
1212

1313
const { once } = require('internal/util');
1414
const destroyImpl = require('internal/streams/destroy');
@@ -39,7 +39,6 @@ function destroyer(stream, reading, writing, callback) {
3939
finished = true;
4040
});
4141

42-
if (eos === undefined) eos = require('internal/streams/end-of-stream');
4342
eos(stream, { readable: reading, writable: writing }, (err) => {
4443
finished = !err;
4544

lib/internal/streams/readable.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ const { validateObject } = require('internal/validators');
6666

6767
const kPaused = Symbol('kPaused');
6868

69-
// Lazy loaded to improve the startup performance.
70-
let StringDecoder;
71-
let from;
69+
const { StringDecoder } = require('string_decoder');
70+
const from = require('internal/streams/from');
7271

7372
ObjectSetPrototypeOf(Readable.prototype, Stream.prototype);
7473
ObjectSetPrototypeOf(Readable, Stream);
@@ -171,8 +170,6 @@ function ReadableState(options, stream, isDuplex) {
171170
this.decoder = null;
172171
this.encoding = null;
173172
if (options && options.encoding) {
174-
if (!StringDecoder)
175-
StringDecoder = require('string_decoder').StringDecoder;
176173
this.decoder = new StringDecoder(options.encoding);
177174
this.encoding = options.encoding;
178175
}
@@ -334,8 +331,6 @@ Readable.prototype.isPaused = function() {
334331

335332
// Backwards compatibility.
336333
Readable.prototype.setEncoding = function(enc) {
337-
if (!StringDecoder)
338-
StringDecoder = require('string_decoder').StringDecoder;
339334
const decoder = new StringDecoder(enc);
340335
this._readableState.decoder = decoder;
341336
// If setEncoding(null), decoder.encoding equals utf8.
@@ -1362,8 +1357,5 @@ function endWritableNT(state, stream) {
13621357
}
13631358

13641359
Readable.from = function(iterable, opts) {
1365-
if (from === undefined) {
1366-
from = require('internal/streams/from');
1367-
}
13681360
return from(Readable, iterable, opts);
13691361
};

lib/stream.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ const pipeline = require('internal/streams/pipeline');
3333
const eos = require('internal/streams/end-of-stream');
3434
const internalBuffer = require('internal/buffer');
3535

36-
// Lazy loaded
37-
let promises = null;
36+
const promises = require('stream/promises');
3837

3938
const Stream = module.exports = require('internal/streams/legacy').Stream;
4039
Stream.Readable = require('internal/streams/readable');
@@ -47,30 +46,25 @@ const { addAbortSignal } = require('internal/streams/add-abort-signal');
4746
Stream.addAbortSignal = addAbortSignal;
4847
Stream.finished = eos;
4948

50-
function lazyLoadPromises() {
51-
if (promises === null) promises = require('stream/promises');
52-
return promises;
53-
}
54-
5549
ObjectDefineProperty(Stream, 'promises', {
5650
configurable: true,
5751
enumerable: true,
5852
get() {
59-
return lazyLoadPromises();
53+
return promises;
6054
}
6155
});
6256

6357
ObjectDefineProperty(pipeline, customPromisify, {
6458
enumerable: true,
6559
get() {
66-
return lazyLoadPromises().pipeline;
60+
return promises.pipeline;
6761
}
6862
});
6963

7064
ObjectDefineProperty(eos, customPromisify, {
7165
enumerable: true,
7266
get() {
73-
return lazyLoadPromises().finished;
67+
return promises.finished;
7468
}
7569
});
7670

lib/stream/promises.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ const {
1818
isStream,
1919
} = require('internal/streams/utils');
2020

21-
let pl;
22-
let eos;
21+
const pl = require('internal/streams/pipeline');
22+
const eos = require('internal/streams/end-of-stream');
2323

2424
function pipeline(...streams) {
25-
if (!pl) pl = require('internal/streams/pipeline');
2625
return new Promise((resolve, reject) => {
2726
let signal;
2827
const lastArg = streams[streams.length - 1];
@@ -47,7 +46,6 @@ function pipeline(...streams) {
4746
}
4847

4948
function finished(stream, opts) {
50-
if (!eos) eos = require('internal/streams/end-of-stream');
5149
return new Promise((resolve, reject) => {
5250
eos(stream, opts, (err) => {
5351
if (err) {

test/parallel/test-bootstrap-modules.js

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const expectedModules = new Set([
9393
'NativeModule internal/streams/destroy',
9494
'NativeModule internal/streams/duplex',
9595
'NativeModule internal/streams/end-of-stream',
96+
'NativeModule internal/streams/from',
9697
'NativeModule internal/streams/legacy',
9798
'NativeModule internal/streams/passthrough',
9899
'NativeModule internal/streams/pipeline',
@@ -116,6 +117,8 @@ const expectedModules = new Set([
116117
'NativeModule async_hooks',
117118
'NativeModule path',
118119
'NativeModule stream',
120+
'NativeModule stream/promises',
121+
'NativeModule string_decoder',
119122
'NativeModule timers',
120123
'NativeModule url',
121124
'NativeModule util',

0 commit comments

Comments
 (0)