sindresorhus / file-type

Detect the file type of a Buffer/Uint8Array/ArrayBuffer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consider alternate (browser friendly) buffer

rolanday opened this issue · comments

Hello! :-) I'd love to be able to use this module in my SvelteKit app, but node:buffer dep is not something I've been able to workaround via Vite polyfill (same issue for other frameworks like Nextjs as well).

Have or can you consider alt buffer impl that is browser friendly? Or, any tips on configuring for use in SvelteKit, Vue, Nextjs, etc?

Thanks!

Ideally, all code should be using Uint8Array instead of Buffer, but there's just too much Node.js code that depends on Buffer.

If anyone wants to work on this, the first task would be to convert the dependencies of file-type to use Uint8Array instead of Buffer.

That work has started to some extend Borewit/token-types#246, but yet there is much more work to do.

+1
this library really needs a browser version without node and webpack deps
something like fromFile(file: File)
because file.mimeType doesnt seems to work properly all times

+1 this library really needs a browser version without node and webpack deps something like fromFile(file: File) because file.mimeType doesnt seems to work properly all times

That function already exist @volarname : fileTypeFromBlob(blob). Where you can pass you file as blob, since File inherits from Blob.

+1 this library really needs a browser version without node and webpack deps something like fromFile(file: File) because file.mimeType doesnt seems to work properly all times

That function already exist @volarname : fileTypeFromBlob(blob). Where you can pass you file as blob, since File inherits from Blob.

Yes, but it doesn't work in modern frameworks like SvelteKit as example. It uses vite under the hood and shows the warnings:

Module "node:buffer" has been externalized for browser compatibility. Cannot access "node:buffer.Buffer" in client code.

This module has been primary written for Node.js. It does depend on Buffer. There is browser friendly buffer module available, but you depend on your framework how easy that is to fit it in.
There is not much more we can do, as these core dependencies are not shared between browser and Node.js.