sindresorhus/file-type

fileTypeFromBlob throws an exception on Mac

Closed this issue · 4 comments

Description

On a Mac, fileTypeFromBlob throws an exception that ReadableStreamBYOBReader is not available, even though the version of Node on the Mac is v20.12.0.

The file-type package docs claim that any version >= 20 should work: "It will stream the underlying Blob, and required a ReadableStreamBYOBReader which require Node.js ≥ 20."

Existing Issue Check

  • I have searched the existing issues and could not find any related to my problem.

ESM (ECMAScript Module) Requirement Acknowledgment

  • My project is an ESM project and my package.json contains the following entry: "type": "module".

File-Type Scope Acknowledgment

  • I understand that file-type detects binary file types and not text or other formats.

Note that the MDN web docs confirm that ReadableStreamBYOBReader is not available on Safari and iOS.
browsercompat

https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamBYOBReader

@adamwagman-customerinsightscdp Thank you for raising this issue.

The file-type module is fully compatible with Node.js ≥ 20; however, the fileTypeFromBlob function is not compatible in environments like Safari due to the lack of support for the ReadableStreamBYOBReader API.

Please note that the behavior of Safari's JavaScript engine is independent of your Node.js installation. Installing or upgrading Node.js will not affect Safari's capabilities.

To work around this limitation, you can use an alternative approach to read and process the Blob without relying on streaming. Here’s an example:

import {fileTypeFromBuffer} from 'file-type';

async function readFromBlobWithoutStreaming(blob) {
  const buffer = await blob.arrayBuffer();
  return fileTypeFromBuffer(buffer);
}

This approach converts the Blob into an ArrayBuffer and then determines the file type from the buffer, which should work seamlessly in Safari and other environments lacking BYOBReader support.