blob-polyfill
Advanced tools
Comparing version 3.0.20180112 to 4.0.20190430
800
Blob.js
/* Blob.js | ||
* A Blob implementation. | ||
* 2018-01-12 | ||
* A Blob, File, FileReader & URL implementation. | ||
* 2019-04-30 | ||
* | ||
* By Eli Grey, http://eligrey.com | ||
* By Devin Samarin, https://github.com/dsamarin | ||
* By Jimmy Wärting, https://github.com/jimmywarting | ||
* License: MIT | ||
@@ -11,8 +11,2 @@ * See https://github.com/eligrey/Blob.js/blob/master/LICENSE.md | ||
/*global self, unescape */ | ||
/*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true, | ||
plusplus: true */ | ||
/*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */ | ||
(function(global) { | ||
@@ -33,197 +27,669 @@ (function (factory) { | ||
exports.URL = global.URL || global.webkitURL; | ||
var BlobBuilder = global.BlobBuilder | ||
|| global.WebKitBlobBuilder | ||
|| global.MSBlobBuilder | ||
|| global.MozBlobBuilder; | ||
if (global.Blob && global.URL) { | ||
try { | ||
new Blob; | ||
return; | ||
} catch (e) { | ||
// Blob did not instantiate, time to polyfill | ||
} | ||
var URL = global.URL || global.webkitURL || function (href, a) { | ||
a = document.createElement("a"); | ||
a.href = href; | ||
return a; | ||
}; | ||
var origBlob = global.Blob; | ||
var createObjectURL = URL.createObjectURL; | ||
var revokeObjectURL = URL.revokeObjectURL; | ||
var strTag = global.Symbol && global.Symbol.toStringTag; | ||
var blobSupported = false; | ||
var blobSupportsArrayBufferView = false; | ||
var arrayBufferSupported = !!global.ArrayBuffer; | ||
var blobBuilderSupported = BlobBuilder | ||
&& BlobBuilder.prototype.append | ||
&& BlobBuilder.prototype.getBlob; | ||
try { | ||
// Check if Blob constructor is supported | ||
blobSupported = new Blob(["ä"]).size === 2; | ||
// Check if Blob constructor supports ArrayBufferViews | ||
// Fails in Safari 6, so we need to map to ArrayBuffers there. | ||
blobSupportsArrayBufferView = new Blob([new Uint8Array([1, 2])]).size === 2; | ||
} catch (e) {/**/} | ||
// Helper function that maps ArrayBufferViews to ArrayBuffers | ||
// Used by BlobBuilder constructor and old browsers that didn't | ||
// support it in the Blob constructor. | ||
function mapArrayBufferViews (ary) { | ||
return ary.map(function (chunk) { | ||
if (chunk.buffer instanceof ArrayBuffer) { | ||
var buf = chunk.buffer; | ||
// if this is a subarray, make a copy so we only | ||
// include the subarray region from the underlying buffer | ||
if (chunk.byteLength !== buf.byteLength) { | ||
var copy = new Uint8Array(chunk.byteLength); | ||
copy.set(new Uint8Array(buf, chunk.byteOffset, chunk.byteLength)); | ||
buf = copy.buffer; | ||
} | ||
return buf; | ||
} | ||
return chunk; | ||
}); | ||
} | ||
// Internally we use a BlobBuilder implementation to base Blob off of | ||
// in order to support older browsers that only have BlobBuilder | ||
var BlobBuilder = global.BlobBuilder || global.WebKitBlobBuilder || global.MozBlobBuilder || (function() { | ||
var get_class = function(object) { | ||
return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1]; | ||
function BlobBuilderConstructor (ary, options) { | ||
options = options || {}; | ||
var bb = new BlobBuilder(); | ||
mapArrayBufferViews(ary).forEach(function (part) { | ||
bb.append(part); | ||
}); | ||
return options.type ? bb.getBlob(options.type) : bb.getBlob(); | ||
} | ||
function BlobConstructor (ary, options) { | ||
return new origBlob(mapArrayBufferViews(ary), options || {}); | ||
} | ||
if (global.Blob) { | ||
BlobBuilderConstructor.prototype = Blob.prototype; | ||
BlobConstructor.prototype = Blob.prototype; | ||
} | ||
/********************************************************/ | ||
/* String Encoder fallback */ | ||
/********************************************************/ | ||
function stringEncode (string) { | ||
var pos = 0; | ||
var len = string.length; | ||
var Arr = global.Uint8Array || Array; // Use byte array when possible | ||
var at = 0; // output position | ||
var tlen = Math.max(32, len + (len >> 1) + 7); // 1.5x size | ||
var target = new Arr((tlen >> 3) << 3); // ... but at 8 byte offset | ||
while (pos < len) { | ||
var value = string.charCodeAt(pos++); | ||
if (value >= 0xd800 && value <= 0xdbff) { | ||
// high surrogate | ||
if (pos < len) { | ||
var extra = string.charCodeAt(pos); | ||
if ((extra & 0xfc00) === 0xdc00) { | ||
++pos; | ||
value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000; | ||
} | ||
} | ||
if (value >= 0xd800 && value <= 0xdbff) { | ||
continue; // drop lone surrogate | ||
} | ||
} | ||
, FakeBlobBuilder = function BlobBuilder() { | ||
this.data = []; | ||
// expand the buffer if we couldn't write 4 bytes | ||
if (at + 4 > target.length) { | ||
tlen += 8; // minimum extra | ||
tlen *= (1.0 + (pos / string.length) * 2); // take 2x the remaining | ||
tlen = (tlen >> 3) << 3; // 8 byte offset | ||
var update = new Uint8Array(tlen); | ||
update.set(target); | ||
target = update; | ||
} | ||
, FakeBlob = function Blob(data, type, encoding) { | ||
this.data = data; | ||
this.size = data.length; | ||
this.type = type; | ||
this.encoding = encoding; | ||
if ((value & 0xffffff80) === 0) { // 1-byte | ||
target[at++] = value; // ASCII | ||
continue; | ||
} else if ((value & 0xfffff800) === 0) { // 2-byte | ||
target[at++] = ((value >> 6) & 0x1f) | 0xc0; | ||
} else if ((value & 0xffff0000) === 0) { // 3-byte | ||
target[at++] = ((value >> 12) & 0x0f) | 0xe0; | ||
target[at++] = ((value >> 6) & 0x3f) | 0x80; | ||
} else if ((value & 0xffe00000) === 0) { // 4-byte | ||
target[at++] = ((value >> 18) & 0x07) | 0xf0; | ||
target[at++] = ((value >> 12) & 0x3f) | 0x80; | ||
target[at++] = ((value >> 6) & 0x3f) | 0x80; | ||
} else { | ||
// FIXME: do we care | ||
continue; | ||
} | ||
, FBB_proto = FakeBlobBuilder.prototype | ||
, FB_proto = FakeBlob.prototype | ||
, FileReaderSync = global.FileReaderSync | ||
, FileException = function(type) { | ||
this.code = this[this.name = type]; | ||
target[at++] = (value & 0x3f) | 0x80; | ||
} | ||
return target.slice(0, at); | ||
} | ||
/********************************************************/ | ||
/* String Decoder fallback */ | ||
/********************************************************/ | ||
function stringDecode (buf) { | ||
var end = buf.length; | ||
var res = []; | ||
var i = 0; | ||
while (i < end) { | ||
var firstByte = buf[i]; | ||
var codePoint = null; | ||
var bytesPerSequence = (firstByte > 0xEF) ? 4 | ||
: (firstByte > 0xDF) ? 3 | ||
: (firstByte > 0xBF) ? 2 | ||
: 1; | ||
if (i + bytesPerSequence <= end) { | ||
var secondByte, thirdByte, fourthByte, tempCodePoint; | ||
switch (bytesPerSequence) { | ||
case 1: | ||
if (firstByte < 0x80) { | ||
codePoint = firstByte; | ||
} | ||
break; | ||
case 2: | ||
secondByte = buf[i + 1]; | ||
if ((secondByte & 0xC0) === 0x80) { | ||
tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F); | ||
if (tempCodePoint > 0x7F) { | ||
codePoint = tempCodePoint; | ||
} | ||
} | ||
break; | ||
case 3: | ||
secondByte = buf[i + 1]; | ||
thirdByte = buf[i + 2]; | ||
if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { | ||
tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F); | ||
if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { | ||
codePoint = tempCodePoint; | ||
} | ||
} | ||
break; | ||
case 4: | ||
secondByte = buf[i + 1]; | ||
thirdByte = buf[i + 2]; | ||
fourthByte = buf[i + 3]; | ||
if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { | ||
tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F); | ||
if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { | ||
codePoint = tempCodePoint; | ||
} | ||
} | ||
} | ||
} | ||
, file_ex_codes = ( | ||
"NOT_FOUND_ERR SECURITY_ERR ABORT_ERR NOT_READABLE_ERR ENCODING_ERR " | ||
+ "NO_MODIFICATION_ALLOWED_ERR INVALID_STATE_ERR SYNTAX_ERR" | ||
).split(" ") | ||
, file_ex_code = file_ex_codes.length | ||
, real_URL = global.URL || global.webkitURL || exports | ||
, real_create_object_URL = real_URL.createObjectURL | ||
, real_revoke_object_URL = real_URL.revokeObjectURL | ||
, URL = real_URL | ||
, btoa = global.btoa | ||
, atob = global.atob | ||
, ArrayBuffer = global.ArrayBuffer | ||
, Uint8Array = global.Uint8Array | ||
if (codePoint === null) { | ||
// we did not generate a valid codePoint so insert a | ||
// replacement char (U+FFFD) and advance only 1 byte | ||
codePoint = 0xFFFD; | ||
bytesPerSequence = 1; | ||
} else if (codePoint > 0xFFFF) { | ||
// encode to utf16 (surrogate pair dance) | ||
codePoint -= 0x10000; | ||
res.push(codePoint >>> 10 & 0x3FF | 0xD800); | ||
codePoint = 0xDC00 | codePoint & 0x3FF; | ||
} | ||
, origin = /^[\w-]+:\/*\[?[\w.:-]+\]?(?::[0-9]+)?/ | ||
; | ||
FakeBlob.fake = FB_proto.fake = true; | ||
while (file_ex_code--) { | ||
FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1; | ||
res.push(codePoint); | ||
i += bytesPerSequence; | ||
} | ||
// Polyfill URL | ||
if (!real_URL.createObjectURL) { | ||
URL = exports.URL = function(uri) { | ||
var uri_info = document.createElementNS("http://www.w3.org/1999/xhtml", "a") | ||
, uri_origin | ||
; | ||
uri_info.href = uri; | ||
if (!("origin" in uri_info)) { | ||
if (uri_info.protocol.toLowerCase() === "data:") { | ||
uri_info.origin = null; | ||
} else { | ||
uri_origin = uri.match(origin); | ||
uri_info.origin = uri_origin && uri_origin[1]; | ||
var len = res.length; | ||
var str = ""; | ||
var j = 0; | ||
while (j < len) { | ||
str += String.fromCharCode.apply(String, res.slice(j, j += 0x1000)); | ||
} | ||
return str; | ||
} | ||
// string -> buffer | ||
var textEncode = typeof TextEncoder === "function" | ||
? TextEncoder.prototype.encode.bind(new TextEncoder()) | ||
: stringEncode; | ||
// buffer -> string | ||
var textDecode = typeof TextDecoder === "function" | ||
? TextDecoder.prototype.decode.bind(new TextDecoder()) | ||
: stringDecode; | ||
function FakeBlobBuilder () { | ||
function isDataView (obj) { | ||
return obj && DataView.prototype.isPrototypeOf(obj); | ||
} | ||
function bufferClone (buf) { | ||
var view = new Array(buf.byteLength); | ||
var array = new Uint8Array(buf); | ||
var i = view.length; | ||
while (i--) { | ||
view[i] = array[i]; | ||
} | ||
return view; | ||
} | ||
function array2base64 (input) { | ||
var byteToCharMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; | ||
var output = []; | ||
for (var i = 0; i < input.length; i += 3) { | ||
var byte1 = input[i]; | ||
var haveByte2 = i + 1 < input.length; | ||
var byte2 = haveByte2 ? input[i + 1] : 0; | ||
var haveByte3 = i + 2 < input.length; | ||
var byte3 = haveByte3 ? input[i + 2] : 0; | ||
var outByte1 = byte1 >> 2; | ||
var outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4); | ||
var outByte3 = ((byte2 & 0x0F) << 2) | (byte3 >> 6); | ||
var outByte4 = byte3 & 0x3F; | ||
if (!haveByte3) { | ||
outByte4 = 64; | ||
if (!haveByte2) { | ||
outByte3 = 64; | ||
} | ||
} | ||
return uri_info; | ||
output.push( | ||
byteToCharMap[outByte1], byteToCharMap[outByte2], | ||
byteToCharMap[outByte3], byteToCharMap[outByte4] | ||
); | ||
} | ||
return output.join(""); | ||
} | ||
var create = Object.create || function (a) { | ||
function c () {} | ||
c.prototype = a; | ||
return new c(); | ||
}; | ||
if (arrayBufferSupported) { | ||
var viewClasses = [ | ||
"[object Int8Array]", | ||
"[object Uint8Array]", | ||
"[object Uint8ClampedArray]", | ||
"[object Int16Array]", | ||
"[object Uint16Array]", | ||
"[object Int32Array]", | ||
"[object Uint32Array]", | ||
"[object Float32Array]", | ||
"[object Float64Array]" | ||
]; | ||
var isArrayBufferView = ArrayBuffer.isView || function (obj) { | ||
return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1; | ||
}; | ||
} | ||
URL.createObjectURL = function(blob) { | ||
var | ||
type = blob.type | ||
, data_URI_header | ||
; | ||
if (type === null) { | ||
type = "application/octet-stream"; | ||
function concatTypedarrays (chunks) { | ||
var size = 0; | ||
var j = chunks.length; | ||
while (j--) { size += chunks[j].length; } | ||
var b = new Uint8Array(size); | ||
var offset = 0; | ||
for (var i = 0; i < chunks.length; i++) { | ||
var chunk = chunks[i]; | ||
b.set(chunk, offset); | ||
offset += chunk.byteLength || chunk.length; | ||
} | ||
if (blob instanceof FakeBlob) { | ||
data_URI_header = "data:" + type; | ||
if (blob.encoding === "base64") { | ||
return data_URI_header + ";base64," + blob.data; | ||
} else if (blob.encoding === "URI") { | ||
return data_URI_header + "," + decodeURIComponent(blob.data); | ||
} if (btoa) { | ||
return data_URI_header + ";base64," + btoa(blob.data); | ||
return b; | ||
} | ||
/********************************************************/ | ||
/* Blob constructor */ | ||
/********************************************************/ | ||
function Blob (chunks, opts) { | ||
chunks = chunks || []; | ||
opts = opts == null ? {} : opts; | ||
for (var i = 0, len = chunks.length; i < len; i++) { | ||
var chunk = chunks[i]; | ||
if (chunk instanceof Blob) { | ||
chunks[i] = chunk._buffer; | ||
} else if (typeof chunk === "string") { | ||
chunks[i] = textEncode(chunk); | ||
} else if (arrayBufferSupported && (ArrayBuffer.prototype.isPrototypeOf(chunk) || isArrayBufferView(chunk))) { | ||
chunks[i] = bufferClone(chunk); | ||
} else if (arrayBufferSupported && isDataView(chunk)) { | ||
chunks[i] = bufferClone(chunk.buffer); | ||
} else { | ||
return data_URI_header + "," + encodeURIComponent(blob.data); | ||
chunks[i] = textEncode(String(chunk)); | ||
} | ||
} else if (real_create_object_URL) { | ||
return real_create_object_URL.call(real_URL, blob); | ||
} | ||
}; | ||
URL.revokeObjectURL = function(object_URL) { | ||
if (object_URL.substring(0, 5) !== "data:" && real_revoke_object_URL) { | ||
real_revoke_object_URL.call(real_URL, object_URL); | ||
} | ||
}; | ||
FBB_proto.append = function(data/*, endings*/) { | ||
var bb = this.data; | ||
// decode data to a binary string | ||
if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) { | ||
var str = "" | ||
, buf = new Uint8Array(data) | ||
, i = 0 | ||
, buf_len = buf.length | ||
; | ||
for (; i < buf_len; i++) { | ||
str += String.fromCharCode(buf[i]); | ||
} | ||
bb.push(str); | ||
} else if (get_class(data) === "Blob" || get_class(data) === "File") { | ||
if (FileReaderSync) { | ||
var fr = new FileReaderSync; | ||
bb.push(fr.readAsBinaryString(data)); | ||
} else { | ||
// async FileReader won't work as BlobBuilder is sync | ||
throw new FileException("NOT_READABLE_ERR"); | ||
} | ||
} else if (data instanceof FakeBlob) { | ||
if (data.encoding === "base64" && atob) { | ||
bb.push(atob(data.data)); | ||
} else if (data.encoding === "URI") { | ||
bb.push(decodeURIComponent(data.data)); | ||
} else if (data.encoding === "raw") { | ||
bb.push(data.data); | ||
} | ||
this._buffer = global.Uint8Array | ||
? concatTypedarrays(chunks) | ||
: [].concat.apply([], chunks); | ||
this.size = this._buffer.length; | ||
this.type = opts.type || ""; | ||
if (/[^\u0020-\u007E]/.test(this.type)) { | ||
this.type = ""; | ||
} else { | ||
if (typeof data !== "string") { | ||
data += ""; // convert unsupported types to strings | ||
} | ||
// decode UTF-16 to binary string | ||
bb.push(unescape(encodeURIComponent(data))); | ||
this.type = this.type.toLowerCase(); | ||
} | ||
} | ||
Blob.prototype.arrayBuffer = function () { | ||
return Promise.resolve(this._buffer); | ||
}; | ||
FBB_proto.getBlob = function(type) { | ||
if (!arguments.length) { | ||
type = null; | ||
} | ||
return new FakeBlob(this.data.join(""), type, "raw"); | ||
Blob.prototype.text = function () { | ||
return Promise.resolve(textDecode(this._buffer)); | ||
}; | ||
FBB_proto.toString = function() { | ||
return "[object BlobBuilder]"; | ||
Blob.prototype.slice = function (start, end, type) { | ||
var slice = this._buffer.slice(start || 0, end || this._buffer.length); | ||
return new Blob([slice], {type: type}); | ||
}; | ||
FB_proto.slice = function(start, end, type) { | ||
var args = arguments.length; | ||
if (args < 3) { | ||
type = null; | ||
Blob.prototype.toString = function () { | ||
return "[object Blob]"; | ||
}; | ||
/********************************************************/ | ||
/* File constructor */ | ||
/********************************************************/ | ||
function File (chunks, name, opts) { | ||
opts = opts || {}; | ||
var a = Blob.call(this, chunks, opts) || this; | ||
a.name = name.replace(/\//g, ":"); | ||
a.lastModifiedDate = opts.lastModified ? new Date(opts.lastModified) : new Date(); | ||
a.lastModified = +a.lastModifiedDate; | ||
return a; | ||
} | ||
File.prototype = create(Blob.prototype); | ||
File.prototype.constructor = File; | ||
if (Object.setPrototypeOf) { | ||
Object.setPrototypeOf(File, Blob); | ||
} else { | ||
try { | ||
File.__proto__ = Blob; | ||
} catch (e) {/**/} | ||
} | ||
File.prototype.toString = function () { | ||
return "[object File]"; | ||
}; | ||
/********************************************************/ | ||
/* FileReader constructor */ | ||
/********************************************************/ | ||
function FileReader () { | ||
if (!(this instanceof FileReader)) { | ||
throw new TypeError("Failed to construct 'FileReader': Please use the 'new' operator, this DOM object constructor cannot be called as a function."); | ||
} | ||
return new FakeBlob(this.data.slice(start, args > 1 ? end : this.data.length) | ||
, type | ||
, this.encoding | ||
); | ||
var delegate = document.createDocumentFragment(); | ||
this.addEventListener = delegate.addEventListener; | ||
this.dispatchEvent = function (evt) { | ||
var local = this["on" + evt.type]; | ||
if (typeof local === "function") local(evt); | ||
delegate.dispatchEvent(evt); | ||
}; | ||
this.removeEventListener = delegate.removeEventListener; | ||
} | ||
function _read (fr, blob, kind) { | ||
if (!(blob instanceof Blob)) { | ||
throw new TypeError("Failed to execute '" + kind + "' on 'FileReader': parameter 1 is not of type 'Blob'."); | ||
} | ||
fr.result = ""; | ||
setTimeout(function () { | ||
this.readyState = FileReader.LOADING; | ||
fr.dispatchEvent(new Event("load")); | ||
fr.dispatchEvent(new Event("loadend")); | ||
}); | ||
} | ||
FileReader.EMPTY = 0; | ||
FileReader.LOADING = 1; | ||
FileReader.DONE = 2; | ||
FileReader.prototype.error = null; | ||
FileReader.prototype.onabort = null; | ||
FileReader.prototype.onerror = null; | ||
FileReader.prototype.onload = null; | ||
FileReader.prototype.onloadend = null; | ||
FileReader.prototype.onloadstart = null; | ||
FileReader.prototype.onprogress = null; | ||
FileReader.prototype.readAsDataURL = function (blob) { | ||
_read(this, blob, "readAsDataURL"); | ||
this.result = "data:" + blob.type + ";base64," + array2base64(blob._buffer); | ||
}; | ||
FB_proto.toString = function() { | ||
return "[object Blob]"; | ||
FileReader.prototype.readAsText = function (blob) { | ||
_read(this, blob, "readAsText"); | ||
this.result = textDecode(blob._buffer); | ||
}; | ||
FB_proto.close = function() { | ||
this.size = 0; | ||
delete this.data; | ||
FileReader.prototype.readAsArrayBuffer = function (blob) { | ||
_read(this, blob, "readAsText"); | ||
// return ArrayBuffer when possible | ||
this.result = (blob._buffer.buffer || blob._buffer).slice(); | ||
}; | ||
return FakeBlobBuilder; | ||
}()); | ||
exports.Blob = function(blobParts, options) { | ||
var type = options ? (options.type || "") : ""; | ||
var builder = new BlobBuilder(); | ||
if (blobParts) { | ||
for (var i = 0, len = blobParts.length; i < len; i++) { | ||
if (Uint8Array && blobParts[i] instanceof Uint8Array) { | ||
builder.append(blobParts[i].buffer); | ||
FileReader.prototype.abort = function () {}; | ||
/********************************************************/ | ||
/* URL */ | ||
/********************************************************/ | ||
URL.createObjectURL = function (blob) { | ||
return blob instanceof Blob | ||
? "data:" + blob.type + ";base64," + array2base64(blob._buffer) | ||
: createObjectURL.call(URL, blob); | ||
}; | ||
URL.revokeObjectURL = function (url) { | ||
revokeObjectURL && revokeObjectURL.call(URL, url); | ||
}; | ||
/********************************************************/ | ||
/* XHR */ | ||
/********************************************************/ | ||
var _send = global.XMLHttpRequest && global.XMLHttpRequest.prototype.send; | ||
if (_send) { | ||
XMLHttpRequest.prototype.send = function (data) { | ||
if (data instanceof Blob) { | ||
this.setRequestHeader("Content-Type", data.type); | ||
_send.call(this, textDecode(data._buffer)); | ||
} else { | ||
_send.call(this, data); | ||
} | ||
else { | ||
builder.append(blobParts[i]); | ||
}; | ||
} | ||
exports.Blob = Blob; | ||
exports.File = File; | ||
exports.FileReader = FileReader; | ||
exports.URL = URL; | ||
} | ||
function fixFileAndXHR () { | ||
var isIE = !!global.ActiveXObject || ( | ||
"-ms-scroll-limit" in document.documentElement.style && | ||
"-ms-ime-align" in document.documentElement.style | ||
); | ||
// Monkey patched | ||
// IE don't set Content-Type header on XHR whose body is a typed Blob | ||
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/6047383 | ||
var _send = global.XMLHttpRequest && global.XMLHttpRequest.prototype.send; | ||
if (isIE && _send) { | ||
XMLHttpRequest.prototype.send = function (data) { | ||
if (data instanceof Blob) { | ||
this.setRequestHeader("Content-Type", data.type); | ||
_send.call(this, data); | ||
} else { | ||
_send.call(this, data); | ||
} | ||
}; | ||
} | ||
try { | ||
new File([], ""); | ||
} catch (e) { | ||
try { | ||
exports.File = new Function("class File extends Blob {" + | ||
"constructor(chunks, name, opts) {" + | ||
"opts = opts || {};" + | ||
"super(chunks, opts || {});" + | ||
"this.name = name.replace(/\\//g, \":\");" + | ||
"this.lastModifiedDate = opts.lastModified ? new Date(opts.lastModified) : new Date();" + | ||
"this.lastModified = +this.lastModifiedDate;" + | ||
"}};" + | ||
"return new File([], \"\"), File" | ||
)(); | ||
} catch (e) { | ||
exports.File = function (b, d, c) { | ||
var blob = new Blob(b, c); | ||
var t = c && void 0 !== c.lastModified ? new Date(c.lastModified) : new Date(); | ||
blob.name = d.replace(/\//g, ":"); | ||
blob.lastModifiedDate = t; | ||
blob.lastModified = +t; | ||
blob.toString = function () { | ||
return "[object File]"; | ||
}; | ||
if (strTag) { | ||
blob[strTag] = "File"; | ||
} | ||
return blob; | ||
}; | ||
} | ||
} | ||
var blob = builder.getBlob(type); | ||
if (!blob.slice && blob.webkitSlice) { | ||
blob.slice = blob.webkitSlice; | ||
} | ||
if (blobSupported) { | ||
fixFileAndXHR(); | ||
exports.Blob = blobSupportsArrayBufferView ? global.Blob : BlobConstructor; | ||
} else if (blobBuilderSupported) { | ||
fixFileAndXHR(); | ||
exports.Blob = BlobBuilderConstructor; | ||
} else { | ||
FakeBlobBuilder(); | ||
} | ||
if (strTag) { | ||
exports.File.prototype[strTag] = "File"; | ||
exports.Blob.prototype[strTag] = "Blob"; | ||
exports.FileReader.prototype[strTag] = "FileReader"; | ||
} | ||
var blob = exports.Blob.prototype; | ||
var stream; | ||
try { | ||
new ReadableStream({ type: "bytes" }); | ||
stream = function stream() { | ||
var position = 0; | ||
var blob = this; | ||
return new ReadableStream({ | ||
type: "bytes", | ||
autoAllocateChunkSize: 524288, | ||
pull: function (controller) { | ||
var v = controller.byobRequest.view; | ||
var chunk = blob.slice(position, position + v.byteLength); | ||
return chunk.arrayBuffer() | ||
.then(function (buffer) { | ||
var uint8array = new Uint8Array(buffer); | ||
var bytesRead = uint8array.byteLength; | ||
position += bytesRead; | ||
v.set(uint8array); | ||
controller.byobRequest.respond(bytesRead); | ||
if(position >= blob.size) | ||
controller.close(); | ||
}); | ||
} | ||
}); | ||
}; | ||
} catch (e) { | ||
try { | ||
new ReadableStream({}); | ||
stream = function stream(blob){ | ||
var position = 0; | ||
return new ReadableStream({ | ||
pull: function (controller) { | ||
var chunk = blob.slice(position, position + 524288); | ||
return chunk.arrayBuffer().then(function (buffer) { | ||
position += buffer.byteLength; | ||
var uint8array = new Uint8Array(buffer); | ||
controller.enqueue(uint8array); | ||
if (position == blob.size) | ||
controller.close(); | ||
}); | ||
} | ||
}); | ||
}; | ||
} catch (e) { | ||
try { | ||
new Response("").body.getReader().read(); | ||
stream = function stream() { | ||
return (new Response(this)).body; | ||
}; | ||
} catch (e) { | ||
stream = function stream() { | ||
throw new Error("Include https://github.com/MattiasBuelens/web-streams-polyfill"); | ||
}; | ||
} | ||
} | ||
return blob; | ||
}; | ||
} | ||
var getPrototypeOf = Object.getPrototypeOf || function(object) { | ||
return object.__proto__; | ||
}; | ||
exports.Blob.prototype = getPrototypeOf(new exports.Blob()); | ||
function promisify(obj) { | ||
return new Promise(function(resolve, reject) { | ||
obj.onload = obj.onerror = function(evt) { | ||
obj.onload = obj.onerror = null; | ||
evt.type === "load" ? | ||
resolve(obj.result || obj) : | ||
reject(new Error("Failed to read the blob/file")); | ||
}; | ||
}); | ||
} | ||
if (!blob.arrayBuffer) { | ||
blob.arrayBuffer = function arrayBuffer() { | ||
var fr = new FileReader(); | ||
fr.readAsArrayBuffer(this); | ||
return promisify(fr); | ||
}; | ||
} | ||
if (!blob.text) { | ||
blob.text = function text() { | ||
var fr = new FileReader(); | ||
fr.readAsText(this); | ||
return promisify(fr); | ||
}; | ||
} | ||
if (!blob.stream) { | ||
blob.stream = stream; | ||
} | ||
}); | ||
})( | ||
typeof self !== "undefined" && self || | ||
typeof window !== "undefined" && window || | ||
typeof global !== "undefined" && global || | ||
this | ||
typeof window !== "undefined" && window || | ||
typeof global !== "undefined" && global || | ||
this | ||
); |
{ | ||
"name": "blob-polyfill", | ||
"version": "3.0.20180112", | ||
"version": "4.0.20190430", | ||
"homepage": "https://github.com/bjornstar/blob-polyfill", | ||
@@ -5,0 +5,0 @@ "authors": [ |
# `blob-polyfill` CHANGELOG | ||
## v4.0.20190430 | ||
* A complete rewrite of Blob.js (@jimmywarting) | ||
* Restore the UMD wrapper (@bjornstar) | ||
* Add some tests for File, FileReader, and URL (@bjornstar) | ||
## v3.0.20180112 | ||
@@ -4,0 +9,0 @@ * Resolve conflict from upstream based on date version change (@bjornstar) |
{ | ||
"name": "blob-polyfill", | ||
"version": "3.0.20180112", | ||
"version": "4.0.20190430", | ||
"description": "Blob.js implements the W3C Blob interface in browsers that do not natively support it.", | ||
@@ -25,5 +25,5 @@ "main": "Blob.js", | ||
"devDependencies": { | ||
"eslint": "^5.1.0", | ||
"mocha": "^5.2.0" | ||
"eslint": "^5.16.0", | ||
"mocha": "^6.1.4" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24087
623
1