-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
fs: move fs.promises API to fs/promises #18777
Conversation
lib/fs.js
Outdated
if (constants.O_SYMLINK !== undefined) { | ||
const fd = await promises.open(path, | ||
constants.O_WRONLY | constants.O_SYMLINK); | ||
return promises.fschmod(fd, mode).finally(fd.close.bind(fd)); |
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.
this is the bug caught by moving out of an object
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.
@targos mind explaining why catching the bug required moving it out of an object?
I realize that eslint has a much harder time to figure out missing properties on objects than ReferenceErrors - but not catching such typos is kind of embarrassing for us - more so if this wasn't a stage 1 API.
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.
AFAIK eslint cannot check properties on objects. It is not something that can be easily done statically.
The best way to catch such errors is to increase code coverage.
lib/fs.js
Outdated
if (constants.O_SYMLINK !== undefined) { | ||
const fd = await promises.open(path, | ||
constants.O_WRONLY | constants.O_SYMLINK); | ||
return promises.fschmod(fd, uid, gid).finally(fd.close.bind(fd)); |
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.
ditto
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.
Overall bug fix and code movement lgtm.
@@ -3231,11 +3231,11 @@ in that, if the `FileHandle` is not explicitly closed using the | |||
and will emit a process warning, thereby helping to prevent memory leaks. | |||
|
|||
Instances of the `FileHandle` object are created internally by the | |||
`fs.promises.open()` method. | |||
`fsPromises.open()` method. |
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.
I'd prefer it if it was referred to as simply fs
in the examples (where it's clear it's the promise version) - that's because that's how people typically use it. Not blocking on it.
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.
I also prefer to use just fs
. Let's wait and see if someone disagrees.
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.
I prefer fsPromises.something
because that is clearer...if we use fs.something
then it looks confusing for new comers if they don't read through the document and notice that they need to get the promisified version that way.
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.
EDIT: s/fs.promise/fsPromises/
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.
After checking how the documentation is, I changed my mind and also prefer to use a different name.
Ideally, I think it should be possible to switch from the callback documentation to the Promise one instead of having one giant page with both at the same time.
lib/fs.js
Outdated
if (constants.O_SYMLINK !== undefined) { | ||
const fd = await promises.open(path, | ||
constants.O_WRONLY | constants.O_SYMLINK); | ||
return promises.fschmod(fd, mode).finally(fd.close.bind(fd)); |
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.
@targos mind explaining why catching the bug required moving it out of an object?
I realize that eslint has a much harder time to figure out missing properties on objects than ReferenceErrors - but not catching such typos is kind of embarrassing for us - more so if this wasn't a stage 1 API.
} = process.binding('constants').fs; | ||
const { statValues } = process.binding('fs'); | ||
|
||
const isWindows = process.platform === 'win32'; |
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.
Not that it matters, but we repeat this line in quite a few places.
|
||
function assertEncoding(encoding) { | ||
if (encoding && !Buffer.isEncoding(encoding)) { | ||
throw new errors.TypeError('ERR_INVALID_OPT_VALUE_ENCODING', encoding); | ||
} | ||
} | ||
|
||
function copyObject(source) { |
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.
I know this PR just moves this - but do you know the rationale for reimplementing this over?
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.
I don't
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.
Then I guess I'll just follow up on it after this lands
I'm +0 on We're still at a point where moving it isn't a big deal. Also ping @jasnell. |
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.
Thanks for doing this!
lib/fs/promises.js
Outdated
return fsync(this); | ||
} | ||
|
||
|
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.
extra space?
Landed in 760f86c...513d939 |
PR-URL: #18777 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: #18777 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
imo this should not have been merged unflagged, pending the outcome of nodejs/TSC#389 (comment) (specifically, making |
PR-URL: nodejs#18777 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: nodejs#18777 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
First commit moves utility functions that both
fs
andfs.promises
make use of tointernal/fs
. It will make backporting easier if needed.Second commit moves
fs.promises
to a separate file:fs/promises
. It also changes the async function definitions to regular declarations. This allowed to find a bug thanks to ESLint.I'm doing this for three reasons:
fs
was already a large file before the addition offs.promises
. This lightens it a bit.fs.promises
code is independent offs
code. It should not have to be loaded at the same time./cc @jasnell @joyeecheung
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
fs