Skip to content

Change Dirent with stat in fs.readdir with withFileTypes #23055

@coderaiser

Description

@coderaiser

In node v10.10.0 with merge request #22020 was added a new flag withFileTypes to fs.readdir:

If options.withFileTypes is set to true, the files array will contain fs.Dirent objects.

It is really great addition and I have a couple ideas to share.

Would be great if fs.Dirent contain all information fs.Stat has. I can see no reason to create new entity for such simple data structure which is half duplicated.

Lets compare them:

fs.Dirent

  • dirent.isBlockDevice()
  • dirent.isCharacterDevice()
  • dirent.isDirectory()
  • dirent.isFIFO()
  • dirent.isFile()
  • dirent.isSocket()
  • dirent.isSymbolicLink()
  • dirent.name

fs.Stats

  • stats.isBlockDevice()
  • stats.isCharacterDevice()
  • stats.isDirectory()
  • stats.isFIFO()
  • stats.isFile()
  • stats.isSocket()
  • stats.isSymbolicLink()
  • stats.dev
  • stats.ino
  • stats.mode
  • stats.nlink
  • stats.uid
  • stats.gid
  • stats.rdev
  • stats.size
  • stats.blksize
  • stats.blocks
  • stats.atimeMs
  • stats.mtimeMs
  • stats.ctimeMs
  • stats.birthtimeMs
  • stats.atime
  • stats.mtime
  • stats.ctime
  • stats.birthtime

The only thing that missing in fs.Stats it's name.

Is your feature request related to a problem? Please describe.
I'm working on file manager for the web Cloud Commander, so I do such things all the time: read directory content and then get stat of every file. Now this all done in readify. And I would like to use the new approach of reading files with stats, it can simplify my code a lot, and make node API cleaner.

Right now if I use a flag withFileTypes I will need to call fs.stat anyways because I need not only divide files and directories but also get their size, mode, uid and mtime.

Describe the solution you'd like
I suggest to change withFileTypes behavior to get real stat information: like size, mtime, uid in method call:

fs.readdirSync('/', {withFileTypes: true});

And remove this fs.Dirent.

Describe alternatives you've considered
Also we can use something like withFileStats to get full stats (maybe with names).

fs.readdirSync('/', {withFileStats: true})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions