node icon indicating copy to clipboard operation
node copied to clipboard

Allow ArrayBuffer as argument to writeFile and friends

Open Jamesernator opened this issue 2 years ago • 6 comments

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

Jamesernator avatar Mar 06 '22 03:03 Jamesernator

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)

JckXia avatar Mar 06 '22 22:03 JckXia

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.

Jamesernator avatar Mar 07 '22 04:03 Jamesernator

Okay that makes sense, thanks for the explanation!

JckXia avatar Mar 07 '22 05:03 JckXia

@nodejs/fs

benjamingr avatar Mar 07 '22 16:03 benjamingr

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[bot] avatar Sep 04 '22 01:09 github-actions[bot]

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

Jamesernator avatar Sep 18 '22 22:09 Jamesernator