sindresorhus / file-type

Detect the file type of a file, stream, or data
MIT License
3.64k stars 345 forks source link

TypeError when upgrading from 19.0.0 to 19.1.0: "ReadableStreamBYOBReader needs a ReadableByteStreamController" #638

Closed JeffBeltran closed 1 month ago

JeffBeltran commented 1 month ago

Just upgraded to 19.1.0 from 19.0.0 and ran into an error. Rolling back to 19.0.0 fixes it, but thought I'd let you know.

Here's the error I'm seeing:

TypeError: ReadableStreamBYOBReader needs a ReadableByteStreamController
    at initializeReadableStreamBYOBReader (:1:11)
    at getReader (:1:11)
    at new WebStreamReader (.../node_modules/peek-readable/lib/WebStreamReader.js:12:30)
    at fromWebStream (.../node_modules/strtok3/lib/core.js:25:36)
    at .../node_modules/file-type/core.js:99:35
    at fromStream (.../node_modules/file-type/core.js:98:19)
    at fromBlob (.../node_modules/file-type/core.js:94:17)

I'm using Bun (1.1.18) for a simple script. Here's the relevant code:

const imageResponse = await fetch(url)
if (!imageResponse.ok) throw new Error(`Failed to fetch: ${imageResponse.statusText}`)
const imageBlob = await imageResponse.blob()
const fileType = await fileTypeFromBlob(imageBlob)

I can provide more details or a reproduction if needed, but figured I'd drop this here first in case it's an easy fix. Let me know if you need anything else!

Borewit commented 1 month ago

Since version v19.1.0 streams (instead of buffering all the data) the Blob provided via fileTypeFromBlob(blob).

Please note associated documentation:

It will stream the underlying Blob, and required a ReadableStreamBYOBReader which require Node.js ≥ 20.

Updated the release notes to warn for this change v19.1.0. A major change would have reflected these API this changes better.

JeffBeltran commented 1 month ago

yeah a major change (as this is breaking) would have been nice, but really not a big deal, thanks for pointing this out. A quick look, appears that ReadableStreamBYOBReader might not be supported in Bun but i can just swap out the blob for buffer for my simple use case.

const buffer = await imageResponse.arrayBuffer();
const fileType = await fileTypeFromBuffer(Buffer.from(buffer));
const fileName = changeCase.kebabCase(title)
Borewit commented 1 month ago

Exactly, that is how we used to parse the Blob prior to v19.1.0:

https://github.com/sindresorhus/file-type/blob/37233b1c9c5c55aab4308f5c1991569dd4e414cc/core.js#L90-L93