node
node copied to clipboard
Allow ArrayBuffer as argument to writeFile and friends
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
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)
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 aTypedArray
, 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.
Okay that makes sense, thanks for the explanation!
@nodejs/fs
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.
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
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.
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.
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.
Given the security risk unchecked readfile with callback is, this seems like a really bad idea