Skip to content
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/promises API inconsitency (.close not there) #20548

Closed
ChALkeR opened this issue May 5, 2018 · 13 comments · Fixed by #20559
Closed

fs/promises API inconsitency (.close not there) #20548

ChALkeR opened this issue May 5, 2018 · 13 comments · Fixed by #20559
Labels
experimental Issues and PRs related to experimental features. fs Issues and PRs related to the fs subsystem / file system. promises Issues and PRs related to ECMAScript promises.

Comments

@ChALkeR
Copy link
Member

ChALkeR commented May 5, 2018

Currently, fs/promises provides a nearly identical API to fs, minus callbacks → promises change (obvious) and the fdfilenandle change (mostly invisible in most usecases).

The following parts of fs API are not present on fs/promises:

  • *Sync methods (obvious)
  • non-functions (ok)
  • exists — kinda ok, since it was deprecated and never worked in a promisified variant. access is there.
  • *Stream — well, kinda ok, not a promise.
  • *watch* — also ok, it doesn't behave like promises.
  • close — why?

All the other methods, except close, that are present on filehandle objects and in callback-based fs API are also present directly as methods of fs/promises API, accepting a filehandle. I assume that was done to ease the migration and provide 1:1 mapping between those where sensible, e.g. for people who were previously using promisified fs versions.

I don't see how close is different from other filehandle APIs, so in my opinion either of there two should happen:

  • All the methods of fs/promises that accept a filehandle should be dropped in favor of the same methods on filehandle objects.
    This will avoid overcomplicating things, will declutter the docs and will save time future users figuring out the «canonical» way to do something.
  • fs/promises should have as much of direct fs API duplicates as reasonable — basically, what we have now + .close should be added.
    This will ease migration for people who are already using the current fs API with (or without) promisify.

/cc @jasnell @Trott @BridgeAR

@ChALkeR ChALkeR added fs Issues and PRs related to the fs subsystem / file system. promises Issues and PRs related to ECMAScript promises. labels May 5, 2018
@ChALkeR ChALkeR changed the title fs/promises API inconsitency fs/promises API inconsitency (.close not there) May 5, 2018
@ChALkeR
Copy link
Member Author

ChALkeR commented May 5, 2018

The patch for adding close is trivial, I suppose:

async function close(handle) {
  validateFileHandle(handle);
  return handle.close();
}

Was it omitted for some actual reason?

@devsnek
Copy link
Member

devsnek commented May 5, 2018

@ChALkeR I believe the design is such that it automatically closes when the handle is gc'd so manually closing it is kinda useless

@ChALkeR
Copy link
Member Author

ChALkeR commented May 5, 2018

@devsnek It's there on the filehandle though: https://nodejs.org/api/fs.html#fs_filehandle_close

Also, manual closing is not useless, as close-on-gc could be delayed.

@jasnell
Copy link
Member

jasnell commented May 5, 2018

closing manually is still the preferred choice, even when using FileHandle.prototype.close()... in fact, a warning will be emitted if the handle is closed on gc.

@jasnell
Copy link
Member

jasnell commented May 5, 2018

@ChALkeR ... the reason there's no require('fs/promises').close is because it's already on FileHandle ... it would be redundant and largely unnecessary.

@ChALkeR
Copy link
Member Author

ChALkeR commented May 5, 2018

@jasnell What's the difference with write, for example? That reasoning for not including close is equally applicable for not including write and sync (and others).

@ChALkeR
Copy link
Member Author

ChALkeR commented May 5, 2018

@jasnell Ah, I suppose that I see what you are talking about — but those are mere implementation details mostly hidden from the users, It shouldn't infuence the API needlessly.

fs/promises exports sync in two variants: fsync(filehandle) FileHandle#sync(), write in two variants: write(filehandle, ... and FileHandle#write(..., the same for pretty much every method dealing with filehandles. But not close. What makes close special from the API PoV?

IMO, we should either export close(handle) method or not export fsync(handle)/write(handle,…/etc.

@jasnell
Copy link
Member

jasnell commented May 5, 2018

Ah, right, yeah the variants on FileHandle were added after. I forgot about those. Ugh, yeah, if we're going to have those we might as well have close also

@Trott
Copy link
Member

Trott commented May 5, 2018

Oooh, this will simplify #20439, so yes please! :-D

@ChALkeR
Copy link
Member Author

ChALkeR commented May 5, 2018

@jasnell @Trott As fs/promises is experimental, we could also remove methods that accept handles in favor of FileHande# variants. Both approaches (keeping 2 and switching to a single variant) have upsides and downsides. What do you think?

@Trott
Copy link
Member

Trott commented May 5, 2018

What do you think?

@ChALkeR No opinion from me other than whichever way we go, maximizing consistency in the API would be helpful (to me, at least).

@nodejs/fs

@Trott
Copy link
Member

Trott commented May 5, 2018

(OK, I guess I do slightly prefer having only one correct way to do something. But that would kind of argue against introducing fs.promises in core in the first place. In fact, it would probably argue against using JavaScript at all. So, again, I'll defer to others.)

@ChALkeR
Copy link
Member Author

ChALkeR commented May 6, 2018

After thinking about this, my personal opinion is that, if the API would be kept as it is now (separate from fs), we should remove duplicate methods that accept filehandle as the fist argument. That should simplify things for the new users. If we merge it with fs — not sure, but probably the same.

@ChALkeR ChALkeR added the experimental Issues and PRs related to experimental features. label May 8, 2018
ChALkeR added a commit to ChALkeR/io.js that referenced this issue May 19, 2018
This drops exporting duplicate methods that accept FileHandle as the
first argument (to mirror callback-based methods accepting 'fd').

Those methods were not adding actual value to the API because all of
those are already present as FileHandle methods, and they would
probably be confusing to the new users and making docs harder to read.

Also, the API was a bit inconsistent and lacked .close(handle).

Fixes: nodejs#20548
PR-URL: nodejs#20559
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
targos pushed a commit to targos/node that referenced this issue Jun 6, 2018
This drops exporting duplicate methods that accept FileHandle as the
first argument (to mirror callback-based methods accepting 'fd').

Those methods were not adding actual value to the API because all of
those are already present as FileHandle methods, and they would
probably be confusing to the new users and making docs harder to read.

Also, the API was a bit inconsistent and lacked .close(handle).

Fixes: nodejs#20548
PR-URL: nodejs#20559
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
targos pushed a commit that referenced this issue Jun 7, 2018
This drops exporting duplicate methods that accept FileHandle as the
first argument (to mirror callback-based methods accepting 'fd').

Those methods were not adding actual value to the API because all of
those are already present as FileHandle methods, and they would
probably be confusing to the new users and making docs harder to read.

Also, the API was a bit inconsistent and lacked .close(handle).

Fixes: #20548
PR-URL: #20559
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>

Backport-PR-URL: #21172
targos pushed a commit that referenced this issue Jun 13, 2018
This drops exporting duplicate methods that accept FileHandle as the
first argument (to mirror callback-based methods accepting 'fd').

Those methods were not adding actual value to the API because all of
those are already present as FileHandle methods, and they would
probably be confusing to the new users and making docs harder to read.

Also, the API was a bit inconsistent and lacked .close(handle).

Fixes: #20548
PR-URL: #20559
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>

Backport-PR-URL: #21172
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
experimental Issues and PRs related to experimental features. fs Issues and PRs related to the fs subsystem / file system. promises Issues and PRs related to ECMAScript promises.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants