From 4927fae7026477de469b3ea37ccaeac63f9bce91 Mon Sep 17 00:00:00 2001 From: Borewit Date: Wed, 20 Nov 2024 12:40:56 +0100 Subject: [PATCH] Better document the BYOB-stream requirement (#691) Co-authored-by: Sindre Sorhus --- readme.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index c4d89f6c..e55839bd 100644 --- a/readme.md +++ b/readme.md @@ -178,10 +178,10 @@ A readable stream representing file data. Detect the file type of a [`Blob`](https://developer.mozilla.org/docs/Web/API/Blob), -[!TIP] +> [!TIP] > A [`File` object](https://developer.mozilla.org/docs/Web/API/File) is a `Blob` and can be passed in here. -It will **stream** the underlying Blob, and required a [ReadableStreamBYOBReader](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamBYOBReader) which **require Node.js ≥ 20**. +It will **stream** the underlying Blob. The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the blob. @@ -204,6 +204,21 @@ console.log(await fileTypeFromBlob(blob)); //=> {ext: 'txt', mime: 'text/plain'} ``` +> [!WARNING] +> This method depends on [ReadableStreamBYOBReader](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamBYOBReader) which **requires Node.js ≥ 20** +> and [may not be available in all modern browsers](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamBYOBReader#browser_compatibility). + +To work around this limitation, you can use an alternative approach to read and process the `Blob` without relying on streaming: + +```js +import {fileTypeFromBuffer} from 'file-type'; + +async function readFromBlobWithoutStreaming(blob) { + const buffer = await blob.arrayBuffer(); + return fileTypeFromBuffer(buffer); +} +``` + #### blob Type: [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob)