Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

unicorn/prefer-blob-reading-methods

Require blob.text() and blob.arrayBuffer() instead of a FileReader

A FileReader delivers its result in a load event, so you end up writing a handler for it, another for error, and often a promise around both. blob.text() and blob.arrayBuffer() return a promise that you can await. A File is a Blob, so it has them too.

The rule reports any call to a method named readAsText or readAsArrayBuffer with one argument.

Configure

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/prefer-blob-reading-methods" = "error" # or "warn"

Examples

Reported:

function readText(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.addEventListener("load", () => resolve(reader.result));
    reader.addEventListener("error", () => reject(reader.error));
    reader.readAsText(file);
  });
}
reader.readAsArrayBuffer(file);

Not reported:

function readText(file) {
  return file.text();
}

const bytes = await file.arrayBuffer();

blob.text() always decodes as UTF-8. A call that passes an encoding has no replacement, so it isn't reported:

reader.readAsText(file, "shift-jis");