sindresorhus / file-type

Detect the file type of a file, stream, or data
MIT License
3.66k stars 345 forks source link

Cloudflare Workers support #595

Closed Zerebokep closed 1 year ago

Zerebokep commented 1 year ago

It would be great to see cloudflare workers support. Currently it's only possible with version 16.5.4 and node_compat enabled (because of the fs import).

sindresorhus commented 1 year ago

We don't plan any special support for Cloudflare Workers. You need to use it through node_compat.

skyf0l commented 6 months ago

Using compatibility_flags = [ "nodejs_compat" ], I got the following error: Error: Dynamic require of "node:events" is not supported with fileTypeFromStream or fileTypeFromBuffer. No idea where this node:events come from...

skyf0l commented 6 months ago

Ok, due to the readable-stream dependency...

skyf0l commented 6 months ago

I've found a workaround for Cloudflare worker, the error is raised by the fileTypeFromStream wrapper using readable-web-to-node-stream (the source of the error) in browser.js.

Simply import the function as follows: import { fileTypeFromBuffer } from "file-type/core"; and hope that the bundler gets rid of this dependency. Of course, you won't be able to use fileTypeFromStream, but fileTypeFromBuffer and fileTypeFromTokenizer are enough.

I hope this solves your problem.