Skip to content

Commit 484140e

Browse files
committed
fs: stop lazy loading stream constructors
Fixes: #21489 PR-URL: #21776 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent ec0ff70 commit 484140e

File tree

2 files changed

+19
-59
lines changed

2 files changed

+19
-59
lines changed

lib/fs.js

+7-53
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
const internalFS = require('internal/fs/utils');
5657
const { getPathFromURL } = require('internal/url');
5758
const internalUtil = require('internal/util');
@@ -91,13 +92,6 @@ let fs;
9192
let promises;
9293
let watchers;
9394
let ReadFileContext;
94-
let ReadStream;
95-
let WriteStream;
96-
97-
// These have to be separate because of how graceful-fs happens to do it's
98-
// monkeypatching.
99-
let FileReadStream;
100-
let FileWriteStream;
10195

10296
const isWindows = process.platform === 'win32';
10397

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

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

17121698
function createWriteStream(path, options) {
1713-
lazyLoadStreams();
17141699
return new WriteStream(path, options);
17151700
}
17161701

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

1796-
get ReadStream() {
1797-
lazyLoadStreams();
1798-
return ReadStream;
1799-
},
1800-
1801-
set ReadStream(val) {
1802-
ReadStream = val;
1803-
},
1804-
1805-
get WriteStream() {
1806-
lazyLoadStreams();
1807-
return WriteStream;
1808-
},
1809-
1810-
set WriteStream(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-
get FileReadStream() {
1817-
lazyLoadStreams();
1818-
return FileReadStream;
1819-
},
1820-
1821-
set FileReadStream(val) {
1822-
FileReadStream = val;
1823-
},
1824-
1825-
get FileWriteStream() {
1826-
lazyLoadStreams();
1827-
return FileWriteStream;
1828-
},
1829-
1830-
set FileWriteStream(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

+12-6
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-
const fs = 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
const util = require('util');
2019

20+
let fs;
21+
function lazyFs() {
22+
if (fs === undefined)
23+
fs = require('fs');
24+
return fs;
25+
}
26+
2127
const kMinPoolSpace = 128;
2228

2329
let pool;
@@ -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
return this.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
function closeFsStream(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)