Skip to content

Commit 484140e

Browse files
committed
fs: stop lazy loading stream constructors
Fixes: #21489 PR-URL: #21776 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent ec0ff70 commit 484140e

2 files changed

Lines changed: 19 additions & 59 deletions

File tree

‎lib/fs.js‎

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const {
5252
}=errors.codes;
5353

5454
const{ FSReqWrap, statValues }=binding;
55+
const{ ReadStream, WriteStream }=require('internal/fs/streams');
5556
constinternalFS=require('internal/fs/utils');
5657
const{ getPathFromURL }=require('internal/url');
5758
constinternalUtil=require('internal/util');
@@ -91,13 +92,6 @@ let fs;
9192
letpromises;
9293
letwatchers;
9394
letReadFileContext;
94-
letReadStream;
95-
letWriteStream;
96-
97-
// These have to be separate because of how graceful-fs happens to do it's
98-
// monkeypatching.
99-
letFileReadStream;
100-
letFileWriteStream;
10195

10296
constisWindows=process.platform==='win32';
10397

@@ -1697,20 +1691,11 @@ function copyFileSync(src, dest, flags) {
16971691
handleErrorFromBinding(ctx);
16981692
}
16991693

1700-
functionlazyLoadStreams(){
1701-
if(!ReadStream){
1702-
({ ReadStream, WriteStream }=require('internal/fs/streams'));
1703-
[FileReadStream,FileWriteStream]=[ReadStream,WriteStream];
1704-
}
1705-
}
1706-
17071694
functioncreateReadStream(path,options){
1708-
lazyLoadStreams();
17091695
returnnewReadStream(path,options);
17101696
}
17111697

17121698
functioncreateWriteStream(path,options){
1713-
lazyLoadStreams();
17141699
returnnewWriteStream(path,options);
17151700
}
17161701

@@ -1793,43 +1778,12 @@ module.exports = fs = {
17931778
writeSync,
17941779
Stats,
17951780

1796-
getReadStream(){
1797-
lazyLoadStreams();
1798-
returnReadStream;
1799-
},
1800-
1801-
setReadStream(val){
1802-
ReadStream=val;
1803-
},
1804-
1805-
getWriteStream(){
1806-
lazyLoadStreams();
1807-
returnWriteStream;
1808-
},
1809-
1810-
setWriteStream(val){
1811-
WriteStream=val;
1812-
},
1813-
1814-
// Legacy names... these have to be separate because of how graceful-fs
1815-
// (and possibly other) modules monkey patch the values.
1816-
getFileReadStream(){
1817-
lazyLoadStreams();
1818-
returnFileReadStream;
1819-
},
1820-
1821-
setFileReadStream(val){
1822-
FileReadStream=val;
1823-
},
1824-
1825-
getFileWriteStream(){
1826-
lazyLoadStreams();
1827-
returnFileWriteStream;
1828-
},
1829-
1830-
setFileWriteStream(val){
1831-
FileWriteStream=val;
1832-
},
1781+
// Stream constructors
1782+
ReadStream,
1783+
WriteStream,
1784+
// Legacy names...
1785+
FileReadStream: ReadStream,
1786+
FileWriteStream: WriteStream,
18331787

18341788
// For tests
18351789
_toUnixTimestamp: toUnixTimestamp

‎lib/internal/fs/streams.js‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const {
88
ERR_INVALID_ARG_TYPE,
99
ERR_OUT_OF_RANGE
1010
}=require('internal/errors').codes;
11-
constfs=require('fs');
1211
const{ Buffer }=require('buffer');
1312
const{
1413
copyObject,
@@ -18,6 +17,13 @@ const { Readable, Writable } = require('stream');
1817
const{ getPathFromURL }=require('internal/url');
1918
constutil=require('util');
2019

20+
letfs;
21+
functionlazyFs(){
22+
if(fs===undefined)
23+
fs=require('fs');
24+
returnfs;
25+
}
26+
2127
constkMinPoolSpace=128;
2228

2329
letpool;
@@ -92,7 +98,7 @@ function ReadStream(path, options) {
9298
util.inherits(ReadStream,Readable);
9399

94100
ReadStream.prototype.open=function(){
95-
fs.open(this.path,this.flags,this.mode,(er,fd)=>{
101+
lazyFs().open(this.path,this.flags,this.mode,(er,fd)=>{
96102
if(er){
97103
if(this.autoClose){
98104
this.destroy();
@@ -142,7 +148,7 @@ ReadStream.prototype._read = function(n) {
142148
returnthis.push(null);
143149

144150
// the actual read.
145-
fs.read(this.fd,pool,pool.used,toRead,this.pos,(er,bytesRead)=>{
151+
lazyFs().read(this.fd,pool,pool.used,toRead,this.pos,(er,bytesRead)=>{
146152
if(er){
147153
if(this.autoClose){
148154
this.destroy();
@@ -177,7 +183,7 @@ ReadStream.prototype._destroy = function(err, cb) {
177183
};
178184

179185
functioncloseFsStream(stream,cb,err){
180-
fs.close(stream.fd,(er)=>{
186+
lazyFs().close(stream.fd,(er)=>{
181187
er=er||err;
182188
cb(er);
183189
stream.closed=true;
@@ -242,7 +248,7 @@ WriteStream.prototype._final = function(callback) {
242248
};
243249

244250
WriteStream.prototype.open=function(){
245-
fs.open(this.path,this.flags,this.mode,(er,fd)=>{
251+
lazyFs().open(this.path,this.flags,this.mode,(er,fd)=>{
246252
if(er){
247253
if(this.autoClose){
248254
this.destroy();
@@ -270,7 +276,7 @@ WriteStream.prototype._write = function(data, encoding, cb) {
270276
});
271277
}
272278

273-
fs.write(this.fd,data,0,data.length,this.pos,(er,bytes)=>{
279+
lazyFs().write(this.fd,data,0,data.length,this.pos,(er,bytes)=>{
274280
if(er){
275281
if(this.autoClose){
276282
this.destroy();

0 commit comments

Comments
 (0)