miniprogram-blob
Version:
mini program blob polyfill
1 lines • 2.95 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});const tslib_1=require("tslib"),miniprogram_text_encoder_1=tslib_1.__importDefault(require("miniprogram-text-encoder")),miniprogram_text_decoder_1=tslib_1.__importDefault(require("miniprogram-text-decoder"));let BlobPolyfill;if("undefined"==typeof Blob){const e=function(e){return e&&Object.prototype.isPrototypeOf.call(DataView.prototype,e)},t=function(e){const t=new Array(e.byteLength),r=new Uint8Array(e);let o=t.length;for(;o--;)t[o]=r[o];return t},r=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]"],o=function(e){return e&&r.indexOf(Object.prototype.toString.call(e))>-1},n=function(e){let t=0,r=e.length;for(;r--;)t+=e[r].length;const o=new Uint8Array(t);let n=0;const i=e.length;for(r=0;r<i;r++){const t=e[r];o.set(t,n),n+=t.byteLength||t.length}return o},i=miniprogram_text_encoder_1.default.prototype.encode.bind(new miniprogram_text_encoder_1.default),l=miniprogram_text_decoder_1.default.prototype.decode.bind(new miniprogram_text_decoder_1.default),a=function(e){return e.arrayBuffer&&e._buffer instanceof Uint8Array};class f{constructor(r=[],l){if((l=l||{}).endings=l.endings||"transparent",l.type=l.type||"","transparent"!==l.endings)throw new TypeError(`Failed to construct 'Blob': Failed to read the 'endings' property from 'BlobPropertyBag': The provided value '${l.endings}' is not a valid enum value of type EndingType.`);const f=new Array(r.length);for(let n=0,l=r.length;n<l;n++){const l=r[n];a(l)?f[n]=l._buffer:"string"==typeof l?f[n]=i(l):Object.prototype.isPrototypeOf.call(ArrayBuffer.prototype,l)||o(l)?f[n]=t(l):e(l)?f[n]=t(l.buffer):f[n]=i(String(l))}this._buffer=n(f),this.size=this._buffer.length,this.type=l.type,/[^\u0020-\u007E]/.test(this.type)?this.type="":this.type=this.type.toLowerCase()}slice(e,t,r){const o=this._buffer.slice(e||0,t||this._buffer.length);return new f([o],{type:r})}toString(){return"[object Blob]"}arrayBuffer(){return new Promise(((e,t)=>{try{e((this._buffer.buffer||this._buffer).slice(0))}catch(e){t(new Error("Failed to read the blob/file"))}}))}text(){return new Promise(((e,t)=>{try{e(l(this._buffer))}catch(e){t(new Error("Failed to read the blob/file"))}}))}stream(){throw new Error("Include https://github.com/MattiasBuelens/web-streams-polyfill")}}let s;"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(f.prototype,Symbol.toStringTag,{value:"Blob"});try{new ReadableStream({}),s=function(){let e=0;const t=this;return new ReadableStream({pull:function(r){return t.slice(e,e+524288).arrayBuffer().then((function(o){e+=o.byteLength;const n=new Uint8Array(o);r.enqueue(n),e==t.size&&r.close()}))}})}}catch(e){}s&&(f.prototype.stream=s),BlobPolyfill=f}else BlobPolyfill=Blob;exports.default=BlobPolyfill,module.exports=exports.default;