Blob.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Blob.js
  2. * A Blob implementation.
  3. * 2018-01-12
  4. *
  5. * By Eli Grey, http://eligrey.com
  6. * By Devin Samarin, https://github.com/dsamarin
  7. * License: MIT
  8. * See https://github.com/eligrey/Blob.js/blob/master/LICENSE.md
  9. */
  10. /*global self, unescape */
  11. /*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
  12. plusplus: true */
  13. /*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */
  14. (function (view) {
  15. "use strict";
  16. view.URL = view.URL || view.webkitURL;
  17. if (view.Blob && view.URL) {
  18. try {
  19. new Blob;
  20. return;
  21. } catch (e) {}
  22. }
  23. // Internally we use a BlobBuilder implementation to base Blob off of
  24. // in order to support older browsers that only have BlobBuilder
  25. var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || (function(view) {
  26. var
  27. get_class = function(object) {
  28. return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
  29. }
  30. , FakeBlobBuilder = function BlobBuilder() {
  31. this.data = [];
  32. }
  33. , FakeBlob = function Blob(data, type, encoding) {
  34. this.data = data;
  35. this.size = data.length;
  36. this.type = type;
  37. this.encoding = encoding;
  38. }
  39. , FBB_proto = FakeBlobBuilder.prototype
  40. , FB_proto = FakeBlob.prototype
  41. , FileReaderSync = view.FileReaderSync
  42. , FileException = function(type) {
  43. this.code = this[this.name = type];
  44. }
  45. , file_ex_codes = (
  46. "NOT_FOUND_ERR SECURITY_ERR ABORT_ERR NOT_READABLE_ERR ENCODING_ERR "
  47. + "NO_MODIFICATION_ALLOWED_ERR INVALID_STATE_ERR SYNTAX_ERR"
  48. ).split(" ")
  49. , file_ex_code = file_ex_codes.length
  50. , real_URL = view.URL || view.webkitURL || view
  51. , real_create_object_URL = real_URL.createObjectURL
  52. , real_revoke_object_URL = real_URL.revokeObjectURL
  53. , URL = real_URL
  54. , btoa = view.btoa
  55. , atob = view.atob
  56. , ArrayBuffer = view.ArrayBuffer
  57. , Uint8Array = view.Uint8Array
  58. , origin = /^[\w-]+:\/*\[?[\w\.:-]+\]?(?::[0-9]+)?/
  59. ;
  60. FakeBlob.fake = FB_proto.fake = true;
  61. while (file_ex_code--) {
  62. FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1;
  63. }
  64. // Polyfill URL
  65. if (!real_URL.createObjectURL) {
  66. URL = view.URL = function(uri) {
  67. var
  68. uri_info = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
  69. , uri_origin
  70. ;
  71. uri_info.href = uri;
  72. if (!("origin" in uri_info)) {
  73. if (uri_info.protocol.toLowerCase() === "data:") {
  74. uri_info.origin = null;
  75. } else {
  76. uri_origin = uri.match(origin);
  77. uri_info.origin = uri_origin && uri_origin[1];
  78. }
  79. }
  80. return uri_info;
  81. };
  82. }
  83. URL.createObjectURL = function(blob) {
  84. var
  85. type = blob.type
  86. , data_URI_header
  87. ;
  88. if (type === null) {
  89. type = "application/octet-stream";
  90. }
  91. if (blob instanceof FakeBlob) {
  92. data_URI_header = "data:" + type;
  93. if (blob.encoding === "base64") {
  94. return data_URI_header + ";base64," + blob.data;
  95. } else if (blob.encoding === "URI") {
  96. return data_URI_header + "," + decodeURIComponent(blob.data);
  97. } if (btoa) {
  98. return data_URI_header + ";base64," + btoa(blob.data);
  99. } else {
  100. return data_URI_header + "," + encodeURIComponent(blob.data);
  101. }
  102. } else if (real_create_object_URL) {
  103. return real_create_object_URL.call(real_URL, blob);
  104. }
  105. };
  106. URL.revokeObjectURL = function(object_URL) {
  107. if (object_URL.substring(0, 5) !== "data:" && real_revoke_object_URL) {
  108. real_revoke_object_URL.call(real_URL, object_URL);
  109. }
  110. };
  111. FBB_proto.append = function(data/*, endings*/) {
  112. var bb = this.data;
  113. // decode data to a binary string
  114. if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) {
  115. var
  116. str = ""
  117. , buf = new Uint8Array(data)
  118. , i = 0
  119. , buf_len = buf.length
  120. ;
  121. for (; i < buf_len; i++) {
  122. str += String.fromCharCode(buf[i]);
  123. }
  124. bb.push(str);
  125. } else if (get_class(data) === "Blob" || get_class(data) === "File") {
  126. if (FileReaderSync) {
  127. var fr = new FileReaderSync;
  128. bb.push(fr.readAsBinaryString(data));
  129. } else {
  130. // async FileReader won't work as BlobBuilder is sync
  131. throw new FileException("NOT_READABLE_ERR");
  132. }
  133. } else if (data instanceof FakeBlob) {
  134. if (data.encoding === "base64" && atob) {
  135. bb.push(atob(data.data));
  136. } else if (data.encoding === "URI") {
  137. bb.push(decodeURIComponent(data.data));
  138. } else if (data.encoding === "raw") {
  139. bb.push(data.data);
  140. }
  141. } else {
  142. if (typeof data !== "string") {
  143. data += ""; // convert unsupported types to strings
  144. }
  145. // decode UTF-16 to binary string
  146. bb.push(unescape(encodeURIComponent(data)));
  147. }
  148. };
  149. FBB_proto.getBlob = function(type) {
  150. if (!arguments.length) {
  151. type = null;
  152. }
  153. return new FakeBlob(this.data.join(""), type, "raw");
  154. };
  155. FBB_proto.toString = function() {
  156. return "[object BlobBuilder]";
  157. };
  158. FB_proto.slice = function(start, end, type) {
  159. var args = arguments.length;
  160. if (args < 3) {
  161. type = null;
  162. }
  163. return new FakeBlob(
  164. this.data.slice(start, args > 1 ? end : this.data.length)
  165. , type
  166. , this.encoding
  167. );
  168. };
  169. FB_proto.toString = function() {
  170. return "[object Blob]";
  171. };
  172. FB_proto.close = function() {
  173. this.size = 0;
  174. delete this.data;
  175. };
  176. return FakeBlobBuilder;
  177. }(view));
  178. view.Blob = function(blobParts, options) {
  179. var type = options ? (options.type || "") : "";
  180. var builder = new BlobBuilder();
  181. if (blobParts) {
  182. for (var i = 0, len = blobParts.length; i < len; i++) {
  183. if (Uint8Array && blobParts[i] instanceof Uint8Array) {
  184. builder.append(blobParts[i].buffer);
  185. }
  186. else {
  187. builder.append(blobParts[i]);
  188. }
  189. }
  190. }
  191. var blob = builder.getBlob(type);
  192. if (!blob.slice && blob.webkitSlice) {
  193. blob.slice = blob.webkitSlice;
  194. }
  195. return blob;
  196. };
  197. var getPrototypeOf = Object.getPrototypeOf || function(object) {
  198. return object.__proto__;
  199. };
  200. view.Blob.prototype = getPrototypeOf(new view.Blob());
  201. }(
  202. typeof self !== "undefined" && self
  203. || typeof window !== "undefined" && window
  204. || this
  205. ));