Skip to content

Commit

Permalink
Better document the BYOB-stream requirement (#691)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
Borewit and sindresorhus authored Nov 20, 2024
1 parent b688b76 commit 4927fae
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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)
Expand Down

0 comments on commit 4927fae

Please sign in to comment.