Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback version for ie9 and others #15

Open
jimmywarting opened this issue Jul 27, 2018 · 0 comments
Open

Fallback version for ie9 and others #15

jimmywarting opened this issue Jul 27, 2018 · 0 comments

Comments

@jimmywarting
Copy link

jimmywarting commented Jul 27, 2018

Please consider adding this fallback version if blob is unsupported

function FakeBlobBuilder() {

  function toUTF8Array(str) {
    var utf8 = [];
    for (var i=0; i < str.length; i++) {
      var charcode = str.charCodeAt(i);
      if (charcode < 0x80) utf8.push(charcode);
      else if (charcode < 0x800) {
        utf8.push(0xc0 | (charcode >> 6), 
        0x80 | (charcode & 0x3f));
      }
      else if (charcode < 0xd800 || charcode >= 0xe000) {
        utf8.push(0xe0 | (charcode >> 12), 
        0x80 | ((charcode>>6) & 0x3f), 
        0x80 | (charcode & 0x3f));
      }
      // surrogate pair
      else {
        i++;
        // UTF-16 encodes 0x10000-0x10FFFF by
        // subtracting 0x10000 and splitting the
        // 20 bits of 0x0-0xFFFFF into two halves
        charcode = 0x10000 + (((charcode & 0x3ff)<<10)
        | (str.charCodeAt(i) & 0x3ff));
        utf8.push(0xf0 | (charcode >>18), 
        0x80 | ((charcode>>12) & 0x3f), 
        0x80 | ((charcode>>6) & 0x3f), 
        0x80 | (charcode & 0x3f));
      }
    }
    return utf8;
  }

  function Blob(chunks, opts) {
    for (var i = 0, len = chunks.length; i < len; i++) {
      var chunk = chunks[i]
      chunks[i] = 
        chunk instanceof Blob ? chunk._buffer :
        typeof chunk === 'string' ? toUTF8Array(chunk) : chunk
    }

    this._buffer = [].concat.apply([], chunks)
    this.size = this._buffer.length
    this.type = opts ? opts.type || '' : ''
  }

  Blob.prototype.slice = function(start, end, type) {
    var slice = this._buffer.slice(start || 0, end || this._buffer.length)
    return new Blob([slice], type || '')
  }

  Blob.prototype.toString = function() {
    return '[object Blob]'
  }

  if (typeof Symbol !== 'undefined') {
    Blob.prototype[Symbol.toStringTag] = 'Blob'
  }

  return Blob
}
module.exports = (function() {
  if (blobSupported) {
    return blobSupportsArrayBufferView ? global.Blob : BlobConstructor;
  } else if (blobBuilderSupported) {
    return BlobBuilderConstructor;
  } else {
    return FakeBlobBuilder();
  }
})();
@jimmywarting jimmywarting changed the title Fallback version for ie9 Fallback version for ie9 and others Jul 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant