Skip to content

Instantly share code, notes, and snippets.

@hanayashiki
Created December 16, 2019 04:08
Show Gist options
  • Save hanayashiki/8dac237671343e7f0b15de617b0051bd to your computer and use it in GitHub Desktop.
Save hanayashiki/8dac237671343e7f0b15de617b0051bd to your computer and use it in GitHub Desktop.
Safari 13.0.4: Blob.arrayBuffer is not a function
(function () {
File.prototype.arrayBuffer = File.prototype.arrayBuffer || myArrayBuffer;
Blob.prototype.arrayBuffer = Blob.prototype.arrayBuffer || myArrayBuffer;
function myArrayBuffer() {
// this: File or Blob
return new Promise((resolve) => {
let fr = new FileReader();
fr.onload = () => {
resolve(fr.result);
};
fr.readAsArrayBuffer(this);
})
}
})();
// This is a simple trick to implement Blob.arrayBuffer (https://developer.mozilla.org/en-US/docs/Web/API/Blob/arrayBuffer) using FileReader
@tomsaleeba
Copy link

Thanks for the useful patch 🚀

If you're targeting earlier Safari (I'm playing with 10.3), then you may see an error:
ReferenceError: Can't find variable: File

You can workaround this by wrapping line 2 in an if check:

...
  if ('File' in self) {
    File.prototype.arrayBuffer = File.prototype.arrayBuffer || myArrayBuffer
  }
...

@hanayashiki
Copy link
Author

Oh thank you for advice

@benjidotsh
Copy link

Thanks for this!

@seanmars
Copy link

seanmars commented Sep 5, 2020

This is awesome! You saved me!

@lucashmorais
Copy link

Amazing! For some reason Babel doesn't seem to be able to generate this right now. Those guys should add this polyfill for Safari.

@Directory
Copy link

clutch

@RamBoFe
Copy link

RamBoFe commented Jul 13, 2022

Thanks !!!!!! I use it in my unit tests with jest !

@manduriomane
Copy link

Thanks a lot!!!! It works fine!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment