-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[v10.x] fs: stop lazy loading stream constructors #21776
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ const { | |
ERR_INVALID_ARG_TYPE, | ||
ERR_OUT_OF_RANGE | ||
} = require('internal/errors').codes; | ||
const fs = require('fs'); | ||
const { Buffer } = require('buffer'); | ||
const { | ||
copyObject, | ||
|
@@ -18,6 +17,13 @@ const { Readable, Writable } = require('stream'); | |
const { getPathFromURL } = require('internal/url'); | ||
const util = require('util'); | ||
|
||
let fs; | ||
function lazyFs() { | ||
if (fs === undefined) | ||
fs = require('fs'); | ||
return fs; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not to just return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, cool. Thank you! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jasnell we might want to benchmark the influence of that again. It would actually be so clean if we'd just use require directly. |
||
} | ||
|
||
const kMinPoolSpace = 128; | ||
|
||
let pool; | ||
|
@@ -92,7 +98,7 @@ function ReadStream(path, options) { | |
util.inherits(ReadStream, Readable); | ||
|
||
ReadStream.prototype.open = function() { | ||
fs.open(this.path, this.flags, this.mode, (er, fd) => { | ||
lazyFs().open(this.path, this.flags, this.mode, (er, fd) => { | ||
if (er) { | ||
if (this.autoClose) { | ||
this.destroy(); | ||
|
@@ -142,7 +148,7 @@ ReadStream.prototype._read = function(n) { | |
return this.push(null); | ||
|
||
// the actual read. | ||
fs.read(this.fd, pool, pool.used, toRead, this.pos, (er, bytesRead) => { | ||
lazyFs().read(this.fd, pool, pool.used, toRead, this.pos, (er, bytesRead) => { | ||
if (er) { | ||
if (this.autoClose) { | ||
this.destroy(); | ||
|
@@ -177,7 +183,7 @@ ReadStream.prototype._destroy = function(err, cb) { | |
}; | ||
|
||
function closeFsStream(stream, cb, err) { | ||
fs.close(stream.fd, (er) => { | ||
lazyFs().close(stream.fd, (er) => { | ||
er = er || err; | ||
cb(er); | ||
stream.closed = true; | ||
|
@@ -242,7 +248,7 @@ WriteStream.prototype._final = function(callback) { | |
}; | ||
|
||
WriteStream.prototype.open = function() { | ||
fs.open(this.path, this.flags, this.mode, (er, fd) => { | ||
lazyFs().open(this.path, this.flags, this.mode, (er, fd) => { | ||
if (er) { | ||
if (this.autoClose) { | ||
this.destroy(); | ||
|
@@ -270,7 +276,7 @@ WriteStream.prototype._write = function(data, encoding, cb) { | |
}); | ||
} | ||
|
||
fs.write(this.fd, data, 0, data.length, this.pos, (er, bytes) => { | ||
lazyFs().write(this.fd, data, 0, data.length, this.pos, (er, bytes) => { | ||
if (er) { | ||
if (this.autoClose) { | ||
this.destroy(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative fix is adding setters here to allow users to override the lazy-loaded variables so we can still avoid loading the
internal/fs/streams
module when requiringfs
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's how it's implemented now. It doesn't work with jest-mock because accessors are ignored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right, I see that there are setters...so this looks more like a bug of Jest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, maybe. But this was still kind of a sever major change and I'm not sure it's worth to do it.