drive icon indicating copy to clipboard operation
drive copied to clipboard

Add support for Uint8Array in addition to Buffer

Open targos opened this issue 3 years ago • 5 comments

It might just be a question of updating the TypeScript types. At least the Node.js fs APIs support both. I don't know about other drivers.

targos avatar Mar 08 '23 14:03 targos

I am less familiar with Uint8Array. Can you share when you will use it and does it make sense to convert it to buffer inside s3 or gcs drivers (if they don't support it)?

thetutlage avatar Mar 08 '23 15:03 thetutlage

Technically, Buffer is built on Uint8Array (class Buffer extends Uint8Array). It just adds some utility methods on top of it.

There are many libraries which return binary data as Uint8Array (often to be usable in the browser). It may make sense to convert internally in the drivers (there is a very fast way to create a Buffer from Uint8Array, without copying the underlying data).

In my project, I'm using an image processing library which returns encoded PNG images as Uint8Array and I save them to the disk using adonis/drive

targos avatar Mar 08 '23 17:03 targos

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 09 '23 05:05 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 12 '23 00:08 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 17 '24 10:03 stale[bot]

Added to flydrive. https://flydrive.dev/docs/disk_api#getarraybuffer

thetutlage avatar Aug 12 '24 11:08 thetutlage

Why is it called getArrayBuffer if it doesn't return an ArrayBuffer?

targos avatar Aug 12 '24 12:08 targos

Lol. That slipped through the cracks.

I initially created getArrayBuffer, but later decided to mimic Bun's API and instead return a Uint8Array and rename the method to bytes. https://bun.sh/guides/read-file/uint8array

Do you agree that bytes is a better name and there is no need to have dedicated method for returning ArrayBuffer, since you can write Uint8Array.buffer to access it?

thetutlage avatar Aug 12 '24 12:08 thetutlage

Yes, sgtm

targos avatar Aug 12 '24 13:08 targos