sindresorhus/file-type

Cloudflare Workers support

Zerebokep opened this issue · 4 comments

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).

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

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...

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

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.