You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow ArrayBuffer as argument to writeFile and friends #42228
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.
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.
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 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.
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.
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
Reacted by Toni Villena and Leonardo Vergara Márquez
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.
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.
Activity
JckXia commentedon Mar 6, 2022
Looking at the
writeFileimplementation 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 pureArrayBuffers.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)
Jamesernator commentedon Mar 7, 2022
The implementation literally just casts any
TypedArrayinto aUint8Array: (https://github.com/nodejs/node/blob/master/lib/internal/fs/promises.js#L395). There should be no difficulty in just adding anelse if (isArrayBuffer(data)) data = new Uint8Array(data).The answer you link would suggest
ArrayBufferis the more correct concept to read/write files with. From the answer:Which makes sense, like you actually write a chunk of bytes to a file, not a sequence of
Float32or whateverTypedArrayyou might happen to have.Blob(and it's subclassFile), andBodydo in fact expose their data viablobOrBody.arrayBuffer()not via typed arrays.JckXia commentedon Mar 7, 2022
Okay that makes sense, thanks for the explanation!
benjamingr commentedon Mar 7, 2022
@nodejs/fs
github-actions commentedon Sep 4, 2022
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.
Jamesernator commentedon Sep 18, 2022
keepalive
SRHerzog commentedon Feb 4, 2023
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:
I didn't touch any
writevfunctions yet, they'll take some more work.github-actions commentedon Aug 4, 2023
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.
github-actions commentedon Sep 4, 2023
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.
knowhatamine commentedon Mar 16, 2024
Given the security risk unchecked readfile with callback is, this seems like a really bad idea