Skip to content

Allow ArrayBuffer as argument to writeFile and friends #42228

Closed as not planned
Closed as not planned

Description

@Jamesernator

What is the problem this feature will solve?

Currently one can write a large number of types to a file. However one cannot currently write an ArrayBuffer to a file without wrapping it in a typed array.

It would be convenient to be able to write an ArrayBuffer directly to a file if one is received from some library without having to wrap directly. (The fact this isn't possible is something I forget pretty much everytime I go to write an ArrayBuffer).

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

I propose allowing fs.promises.writeFile("./some-file", someArrayBuffer) to work when someArrayBuffer is an ArrayBuffer. Typed arrays and such are already supported, so this shouldn't be particularly complicated to implement.

What alternatives have you considered?

No response

Activity

  1. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Mar 6, 2022
  2. JckXia commented on Mar 6, 2022

    @JckXia
    Member

    Looking at the writeFile implementation in (https://github.com/nodejs/node/blob/master/lib/internal/fs/promises.js#L809), it looks like Node enforce the data to be either a TypedArray, iterable, a string (or an object that we can serialize into a string). Not sure if there's a lot of wiggle room to loosen the check against pure ArrayBuffers.

    I also wonder if writing arrayBuffers directly to a file is considered to be an undefined behaviour, since they are byte arrays and their representation is dependent on the width of the view we are casting it to. (https://stackoverflow.com/questions/42416783/where-to-use-arraybuffer-vs-typed-array-in-javascript)

  3. Jamesernator commented on Mar 7, 2022

    @Jamesernator
    Author

    Looking at the writeFile implementation in (https://github.com/nodejs/node/blob/master/lib/internal/fs/promises.js#L809), it looks like Node enforce the data to be either a TypedArray, iterable, a string (or an object that we can serialize into a string). Not sure if there's a lot of wiggle room to loosen the check against pure ArrayBuffers.

    The implementation literally just casts any TypedArray into a Uint8Array: (https://github.com/nodejs/node/blob/master/lib/internal/fs/promises.js#L395). There should be no difficulty in just adding an else if (isArrayBuffer(data)) data = new Uint8Array(data).

    I also wonder if writing arrayBuffers directly to a file is considered to be an undefined behaviour, since they are byte arrays and their representation is dependent on the width of the view we are casting it to. (https://stackoverflow.com/questions/42416783/where-to-use-arraybuffer-vs-typed-array-in-javascript)

    The answer you link would suggest ArrayBuffer is the more correct concept to read/write files with. From the answer:

    They are on the other hand used for binary data transfers between server and client, or from the user's file system via Blobs.

    Which makes sense, like you actually write a chunk of bytes to a file, not a sequence of Float32 or whatever TypedArray you might happen to have. Blob (and it's subclass File), and Body do in fact expose their data via blobOrBody.arrayBuffer() not via typed arrays.

  4. JckXia commented on Mar 7, 2022

    @JckXia
    Member

    Okay that makes sense, thanks for the explanation!

  5. moved this from Pending Triage to Stale in Node.js feature requestson Mar 7, 2022
  6. moved this to Pending Triage in Node.js feature requestson Mar 7, 2022
  7. moved this from Stale to Pending Triage in Node.js feature requestson Mar 7, 2022
  8. benjamingr commented on Mar 7, 2022

    @benjamingr
    Member

    @nodejs/fs

  9. github-actions commented on Sep 4, 2022

    @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.

  10. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Sep 4, 2022
  11. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Sep 7, 2022
  12. Jamesernator commented on Sep 18, 2022

    @Jamesernator
    Author

    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.

    keepalive

  13. SRHerzog commented on Feb 4, 2023

    @SRHerzog
    Contributor

    Here's a draft PR to consider. It required some work in a few different places to apply the change to most of the write functions:

    • fsPromises.writeFile
    • fsPromises.write
    • filehandle.writeFile
    • filehandle.write
    • fs.writeFile
    • fs.writeFileSync
    • fs.write

    I didn't touch any writev functions yet, they'll take some more work.

  14. github-actions commented on Aug 4, 2023

    @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.

  15. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Aug 4, 2023
  16. github-actions commented on Sep 4, 2023

    @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.

  17. knowhatamine commented on Mar 16, 2024

    @knowhatamine

    Given the security risk unchecked readfile with callback is, this seems like a really bad idea

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