Skip to content

more ways of getting files #49757

Description

@jimmywarting

What is the problem this feature will solve?

it's kind of a #developer-pain that

  1. there are no sync version of fs.openAsBlob (aka: fs.openAsBlobSync)
  2. they are a Blob instead of Files and therefore lacks a file name and lastModified. So upgrading them to a File requires figuring out more stuff yourself.

What is the feature you are proposing to solve the problem?

That we add:

What alternatives have you considered?

Only workaround today is to do:

const name = path.basename(filePath)
const stat = fs.statSync(filePath)
const blob = await fs.openAsBlob(filePath)
const file = new File([blob], name, { 
  type: blob.type,
  lastModified: Number(stat.mtime)
 })

would be a heck a lot easier if i could just do:

const file = fs.openAsFileSync(filePath)

and there is no way of getting around it cuz the async openAsBlob is the only thing you can work with. which makes your own code having to be async as well.

Activity

  1. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Sep 22, 2023
  2. LiviaMedeiros commented on Sep 22, 2023

    @LiviaMedeiros
    Member

    The fs.openAsBlob should be moved to Promises API and fs.openAsBlobSync should exist indeed. Drafted #49759 to address this.

     const name = path.basename(filePath)
     const stat = fs.statSync(filePath)
     const blob = await fs.openAsBlob(filePath)
     const file = new File([blob], name, {
    -  type: blob.type, // this is always empty string
    +  type: userProvidedMimeType,
       lastModified: Number(stat.mtime)
      })

    This makes me wonder, wouldn't it be inconsistent that we set lastModified automatically but don't do that with type (using extension-mime map or libmagic)? Because the guessing should probably be done in userland.
    type property is read-only; so user wouldn't be able to set it after reading returned File, and fs.readAsFile(path, { type }) would work only if they know type beforehand.

  3. jimmywarting commented on Sep 23, 2023

    @jimmywarting
    Author

    The fs.openAsBlob should be moved to Promises API

    Don't mind that

    Drafted #49759 to address this.

    👍

    I would not mind if it was able to guess the correct type.
    I think the mimesniff spec is being used on many web related things... such as
    whatwg/fs and <input type="file"> are able to figure out some common types when getting access to a File

    But i think we could focus on that later, it dose not really affect this issue directly.
    i would say that is a blocker if NodeJS dose not have a built in mime sniffer.
    (just created: #49843)

  4. github-actions commented on Mar 23, 2024

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

    For more information on how the project manages feature requests, please consult the feature request management document.

  5. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Mar 23, 2024
  6. github-actions commented on Apr 22, 2024

    @github-actions
    Contributor

    There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.

    For more information on how the project manages feature requests, please consult the feature request management document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.fsIssues and PRs related to file-system APIs and the fs module.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions