sindresorhus/file-type

Chromium consistently crash after processing ~990 files (of several specific types) with `fileTypeFromBlob` - a.readFromStream WebStreamReader.js - TypeError: network error

Closed this issue · 2 comments

Description

This may be a Chrome bug because it works in Firefox. It crashes at around 990 iterations when the selected file is jpg/png/webp/mkv/gz/safetensors and possibly many other types, and for some reason it does not crash on some other file types I tested (like zip archives). You can open about:blank in your browser, then open DevTools and paste this, and select any random image from your computer:

let fileType = await import("https://cdn.jsdelivr.net/npm/file-type@19.5.0/+esm");
let fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.onchange = async e => {
  let file = e.target.files[0];
  for(let i = 0; i < 10000; i++) {
    // let t = await fileType.fileTypeFromBuffer(await file.arrayBuffer()); // ✅ This works fine
    let t = await fileType.fileTypeFromBlob(file); // ❌ This crashes after about 990 iterations
    console.log(i, t);
  }
};
fileInput.click();

Here's the error:

WebStreamReader.js:17 Uncaught (in promise) TypeError: network error
    at a.readFromStream (WebStreamReader.js:17:42)
    at a.readRemainderFromStream (AbstractStreamReader.js:70:41)
    at a.read (AbstractStreamReader.js:32:33)
    at a.peek (AbstractStreamReader.js:23:38)
    at s.peekBuffer (ReadStreamTokenizer.js:63:53)
    at x.parse (core.js:174:19)
    at x.fromTokenizer (core.js:81:15)
    at x.fromStream (core.js:105:22)

Notes:

  • I've tested in Chrome v129 and v130 on Ubuntu 22.04.
  • As noted above, it works fine in Firefox.
  • As noted above, it works fine if I use fileTypeFromBuffer.
  • It doesn't matter whether I'm processing the same blob multiple times, or processing a different blob at each iteration.
  • After it crashes, if you try again, it will immediately crash on the first iteration. You need to refresh the page to fix it.
  • It doesn't seem to be related to the size of the file. E.g. 50mb file and 12kb file both crash after ~990 iterations.
  • For me this is easy to work around (just use fileTypeFromBuffer), so this issue is just to document the bug, be it a Chromium one or a file-type one.

Example file:

It happens for any jpg/png/webp as far as I can tell, but just in case, here's an example I tested/replicated the issue on:

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.

I tested the above code, on Windows 11, with Chrome 129, loading some JPEG files, and it completed without crashing.

On Fedora Linux 40, with Chrome 129, I could reproduce the issue.

image