From 98549e796ff947a9f3e9639a6c48dbfb125809ad Mon Sep 17 00:00:00 2001 From: Borewit Date: Tue, 19 Nov 2024 20:27:05 +0100 Subject: [PATCH 1/3] Better document the BYOB-stream requirement Resolves #690 --- readme.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index c4d89f6c..33c66e4c 100644 --- a/readme.md +++ b/readme.md @@ -178,10 +178,11 @@ 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 +205,20 @@ 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 **require Node.js ≥ 20** +> and [may not be available in all modern browser](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) From 0b48b181643855152b75f1e175de19ad677eb4f7 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 20 Nov 2024 18:39:49 +0700 Subject: [PATCH 2/3] Update readme.md --- readme.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index 33c66e4c..73028186 100644 --- a/readme.md +++ b/readme.md @@ -183,7 +183,6 @@ Detect the file type of a [`Blob`](https://developer.mozilla.org/docs/Web/API/Bl 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. Returns a `Promise` for an object with the detected file type: @@ -206,16 +205,17 @@ console.log(await fileTypeFromBlob(blob)); ``` > [!WARNING] -> This method depends on [ReadableStreamBYOBReader](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamBYOBReader) which **require Node.js ≥ 20** +> 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 browser](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: +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); + const buffer = await blob.arrayBuffer(); + return fileTypeFromBuffer(buffer); } ``` From 62739928fd646058061dc4684f854767720b347c Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 20 Nov 2024 18:40:28 +0700 Subject: [PATCH 3/3] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 73028186..e55839bd 100644 --- a/readme.md +++ b/readme.md @@ -206,7 +206,7 @@ console.log(await fileTypeFromBlob(blob)); > [!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 browser](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamBYOBReader#browser_compatibility). +> 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: