browser-polyfill.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. (function(self) {
  2. var irrelevant = (function (exports) {
  3. var support = {
  4. searchParams: 'URLSearchParams' in self,
  5. iterable: 'Symbol' in self && 'iterator' in Symbol,
  6. blob:
  7. 'FileReader' in self &&
  8. 'Blob' in self &&
  9. (function() {
  10. try {
  11. new Blob();
  12. return true
  13. } catch (e) {
  14. return false
  15. }
  16. })(),
  17. formData: 'FormData' in self,
  18. arrayBuffer: 'ArrayBuffer' in self
  19. };
  20. function isDataView(obj) {
  21. return obj && DataView.prototype.isPrototypeOf(obj)
  22. }
  23. if (support.arrayBuffer) {
  24. var viewClasses = [
  25. '[object Int8Array]',
  26. '[object Uint8Array]',
  27. '[object Uint8ClampedArray]',
  28. '[object Int16Array]',
  29. '[object Uint16Array]',
  30. '[object Int32Array]',
  31. '[object Uint32Array]',
  32. '[object Float32Array]',
  33. '[object Float64Array]'
  34. ];
  35. var isArrayBufferView =
  36. ArrayBuffer.isView ||
  37. function(obj) {
  38. return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1
  39. };
  40. }
  41. function normalizeName(name) {
  42. if (typeof name !== 'string') {
  43. name = String(name);
  44. }
  45. if (/[^a-z0-9\-#$%&'*+.^_`|~]/i.test(name)) {
  46. throw new TypeError('Invalid character in header field name')
  47. }
  48. return name.toLowerCase()
  49. }
  50. function normalizeValue(value) {
  51. if (typeof value !== 'string') {
  52. value = String(value);
  53. }
  54. return value
  55. }
  56. // Build a destructive iterator for the value list
  57. function iteratorFor(items) {
  58. var iterator = {
  59. next: function() {
  60. var value = items.shift();
  61. return {done: value === undefined, value: value}
  62. }
  63. };
  64. if (support.iterable) {
  65. iterator[Symbol.iterator] = function() {
  66. return iterator
  67. };
  68. }
  69. return iterator
  70. }
  71. function Headers(headers) {
  72. this.map = {};
  73. if (headers instanceof Headers) {
  74. headers.forEach(function(value, name) {
  75. this.append(name, value);
  76. }, this);
  77. } else if (Array.isArray(headers)) {
  78. headers.forEach(function(header) {
  79. this.append(header[0], header[1]);
  80. }, this);
  81. } else if (headers) {
  82. Object.getOwnPropertyNames(headers).forEach(function(name) {
  83. this.append(name, headers[name]);
  84. }, this);
  85. }
  86. }
  87. Headers.prototype.append = function(name, value) {
  88. name = normalizeName(name);
  89. value = normalizeValue(value);
  90. var oldValue = this.map[name];
  91. this.map[name] = oldValue ? oldValue + ', ' + value : value;
  92. };
  93. Headers.prototype['delete'] = function(name) {
  94. delete this.map[normalizeName(name)];
  95. };
  96. Headers.prototype.get = function(name) {
  97. name = normalizeName(name);
  98. return this.has(name) ? this.map[name] : null
  99. };
  100. Headers.prototype.has = function(name) {
  101. return this.map.hasOwnProperty(normalizeName(name))
  102. };
  103. Headers.prototype.set = function(name, value) {
  104. this.map[normalizeName(name)] = normalizeValue(value);
  105. };
  106. Headers.prototype.forEach = function(callback, thisArg) {
  107. for (var name in this.map) {
  108. if (this.map.hasOwnProperty(name)) {
  109. callback.call(thisArg, this.map[name], name, this);
  110. }
  111. }
  112. };
  113. Headers.prototype.keys = function() {
  114. var items = [];
  115. this.forEach(function(value, name) {
  116. items.push(name);
  117. });
  118. return iteratorFor(items)
  119. };
  120. Headers.prototype.values = function() {
  121. var items = [];
  122. this.forEach(function(value) {
  123. items.push(value);
  124. });
  125. return iteratorFor(items)
  126. };
  127. Headers.prototype.entries = function() {
  128. var items = [];
  129. this.forEach(function(value, name) {
  130. items.push([name, value]);
  131. });
  132. return iteratorFor(items)
  133. };
  134. if (support.iterable) {
  135. Headers.prototype[Symbol.iterator] = Headers.prototype.entries;
  136. }
  137. function consumed(body) {
  138. if (body.bodyUsed) {
  139. return Promise.reject(new TypeError('Already read'))
  140. }
  141. body.bodyUsed = true;
  142. }
  143. function fileReaderReady(reader) {
  144. return new Promise(function(resolve, reject) {
  145. reader.onload = function() {
  146. resolve(reader.result);
  147. };
  148. reader.onerror = function() {
  149. reject(reader.error);
  150. };
  151. })
  152. }
  153. function readBlobAsArrayBuffer(blob) {
  154. var reader = new FileReader();
  155. var promise = fileReaderReady(reader);
  156. reader.readAsArrayBuffer(blob);
  157. return promise
  158. }
  159. function readBlobAsText(blob) {
  160. var reader = new FileReader();
  161. var promise = fileReaderReady(reader);
  162. reader.readAsText(blob);
  163. return promise
  164. }
  165. function readArrayBufferAsText(buf) {
  166. var view = new Uint8Array(buf);
  167. var chars = new Array(view.length);
  168. for (var i = 0; i < view.length; i++) {
  169. chars[i] = String.fromCharCode(view[i]);
  170. }
  171. return chars.join('')
  172. }
  173. function bufferClone(buf) {
  174. if (buf.slice) {
  175. return buf.slice(0)
  176. } else {
  177. var view = new Uint8Array(buf.byteLength);
  178. view.set(new Uint8Array(buf));
  179. return view.buffer
  180. }
  181. }
  182. function Body() {
  183. this.bodyUsed = false;
  184. this._initBody = function(body) {
  185. this._bodyInit = body;
  186. if (!body) {
  187. this._bodyText = '';
  188. } else if (typeof body === 'string') {
  189. this._bodyText = body;
  190. } else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
  191. this._bodyBlob = body;
  192. } else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
  193. this._bodyFormData = body;
  194. } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
  195. this._bodyText = body.toString();
  196. } else if (support.arrayBuffer && support.blob && isDataView(body)) {
  197. this._bodyArrayBuffer = bufferClone(body.buffer);
  198. // IE 10-11 can't handle a DataView body.
  199. this._bodyInit = new Blob([this._bodyArrayBuffer]);
  200. } else if (support.arrayBuffer && (ArrayBuffer.prototype.isPrototypeOf(body) || isArrayBufferView(body))) {
  201. this._bodyArrayBuffer = bufferClone(body);
  202. } else {
  203. this._bodyText = body = Object.prototype.toString.call(body);
  204. }
  205. if (!this.headers.get('content-type')) {
  206. if (typeof body === 'string') {
  207. this.headers.set('content-type', 'text/plain;charset=UTF-8');
  208. } else if (this._bodyBlob && this._bodyBlob.type) {
  209. this.headers.set('content-type', this._bodyBlob.type);
  210. } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
  211. this.headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
  212. }
  213. }
  214. };
  215. if (support.blob) {
  216. this.blob = function() {
  217. var rejected = consumed(this);
  218. if (rejected) {
  219. return rejected
  220. }
  221. if (this._bodyBlob) {
  222. return Promise.resolve(this._bodyBlob)
  223. } else if (this._bodyArrayBuffer) {
  224. return Promise.resolve(new Blob([this._bodyArrayBuffer]))
  225. } else if (this._bodyFormData) {
  226. throw new Error('could not read FormData body as blob')
  227. } else {
  228. return Promise.resolve(new Blob([this._bodyText]))
  229. }
  230. };
  231. this.arrayBuffer = function() {
  232. if (this._bodyArrayBuffer) {
  233. return consumed(this) || Promise.resolve(this._bodyArrayBuffer)
  234. } else {
  235. return this.blob().then(readBlobAsArrayBuffer)
  236. }
  237. };
  238. }
  239. this.text = function() {
  240. var rejected = consumed(this);
  241. if (rejected) {
  242. return rejected
  243. }
  244. if (this._bodyBlob) {
  245. return readBlobAsText(this._bodyBlob)
  246. } else if (this._bodyArrayBuffer) {
  247. return Promise.resolve(readArrayBufferAsText(this._bodyArrayBuffer))
  248. } else if (this._bodyFormData) {
  249. throw new Error('could not read FormData body as text')
  250. } else {
  251. return Promise.resolve(this._bodyText)
  252. }
  253. };
  254. if (support.formData) {
  255. this.formData = function() {
  256. return this.text().then(decode)
  257. };
  258. }
  259. this.json = function() {
  260. return this.text().then(JSON.parse)
  261. };
  262. return this
  263. }
  264. // HTTP methods whose capitalization should be normalized
  265. var methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT'];
  266. function normalizeMethod(method) {
  267. var upcased = method.toUpperCase();
  268. return methods.indexOf(upcased) > -1 ? upcased : method
  269. }
  270. function Request(input, options) {
  271. options = options || {};
  272. var body = options.body;
  273. if (input instanceof Request) {
  274. if (input.bodyUsed) {
  275. throw new TypeError('Already read')
  276. }
  277. this.url = input.url;
  278. this.credentials = input.credentials;
  279. if (!options.headers) {
  280. this.headers = new Headers(input.headers);
  281. }
  282. this.method = input.method;
  283. this.mode = input.mode;
  284. this.signal = input.signal;
  285. if (!body && input._bodyInit != null) {
  286. body = input._bodyInit;
  287. input.bodyUsed = true;
  288. }
  289. } else {
  290. this.url = String(input);
  291. }
  292. this.credentials = options.credentials || this.credentials || 'same-origin';
  293. if (options.headers || !this.headers) {
  294. this.headers = new Headers(options.headers);
  295. }
  296. this.method = normalizeMethod(options.method || this.method || 'GET');
  297. this.mode = options.mode || this.mode || null;
  298. this.signal = options.signal || this.signal;
  299. this.referrer = null;
  300. if ((this.method === 'GET' || this.method === 'HEAD') && body) {
  301. throw new TypeError('Body not allowed for GET or HEAD requests')
  302. }
  303. this._initBody(body);
  304. }
  305. Request.prototype.clone = function() {
  306. return new Request(this, {body: this._bodyInit})
  307. };
  308. function decode(body) {
  309. var form = new FormData();
  310. body
  311. .trim()
  312. .split('&')
  313. .forEach(function(bytes) {
  314. if (bytes) {
  315. var split = bytes.split('=');
  316. var name = split.shift().replace(/\+/g, ' ');
  317. var value = split.join('=').replace(/\+/g, ' ');
  318. form.append(decodeURIComponent(name), decodeURIComponent(value));
  319. }
  320. });
  321. return form
  322. }
  323. function parseHeaders(rawHeaders) {
  324. var headers = new Headers();
  325. // Replace instances of \r\n and \n followed by at least one space or horizontal tab with a space
  326. // https://tools.ietf.org/html/rfc7230#section-3.2
  327. var preProcessedHeaders = rawHeaders.replace(/\r?\n[\t ]+/g, ' ');
  328. preProcessedHeaders.split(/\r?\n/).forEach(function(line) {
  329. var parts = line.split(':');
  330. var key = parts.shift().trim();
  331. if (key) {
  332. var value = parts.join(':').trim();
  333. headers.append(key, value);
  334. }
  335. });
  336. return headers
  337. }
  338. Body.call(Request.prototype);
  339. function Response(bodyInit, options) {
  340. if (!options) {
  341. options = {};
  342. }
  343. this.type = 'default';
  344. this.status = options.status === undefined ? 200 : options.status;
  345. this.ok = this.status >= 200 && this.status < 300;
  346. this.statusText = 'statusText' in options ? options.statusText : 'OK';
  347. this.headers = new Headers(options.headers);
  348. this.url = options.url || '';
  349. this._initBody(bodyInit);
  350. }
  351. Body.call(Response.prototype);
  352. Response.prototype.clone = function() {
  353. return new Response(this._bodyInit, {
  354. status: this.status,
  355. statusText: this.statusText,
  356. headers: new Headers(this.headers),
  357. url: this.url
  358. })
  359. };
  360. Response.error = function() {
  361. var response = new Response(null, {status: 0, statusText: ''});
  362. response.type = 'error';
  363. return response
  364. };
  365. var redirectStatuses = [301, 302, 303, 307, 308];
  366. Response.redirect = function(url, status) {
  367. if (redirectStatuses.indexOf(status) === -1) {
  368. throw new RangeError('Invalid status code')
  369. }
  370. return new Response(null, {status: status, headers: {location: url}})
  371. };
  372. exports.DOMException = self.DOMException;
  373. try {
  374. new exports.DOMException();
  375. } catch (err) {
  376. exports.DOMException = function(message, name) {
  377. this.message = message;
  378. this.name = name;
  379. var error = Error(message);
  380. this.stack = error.stack;
  381. };
  382. exports.DOMException.prototype = Object.create(Error.prototype);
  383. exports.DOMException.prototype.constructor = exports.DOMException;
  384. }
  385. function fetch(input, init) {
  386. return new Promise(function(resolve, reject) {
  387. var request = new Request(input, init);
  388. if (request.signal && request.signal.aborted) {
  389. return reject(new exports.DOMException('Aborted', 'AbortError'))
  390. }
  391. var xhr = new XMLHttpRequest();
  392. function abortXhr() {
  393. xhr.abort();
  394. }
  395. xhr.onload = function() {
  396. var options = {
  397. status: xhr.status,
  398. statusText: xhr.statusText,
  399. headers: parseHeaders(xhr.getAllResponseHeaders() || '')
  400. };
  401. options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL');
  402. var body = 'response' in xhr ? xhr.response : xhr.responseText;
  403. resolve(new Response(body, options));
  404. };
  405. xhr.onerror = function() {
  406. reject(new TypeError('Network request failed'));
  407. };
  408. xhr.ontimeout = function() {
  409. reject(new TypeError('Network request failed'));
  410. };
  411. xhr.onabort = function() {
  412. reject(new exports.DOMException('Aborted', 'AbortError'));
  413. };
  414. xhr.open(request.method, request.url, true);
  415. if (request.credentials === 'include') {
  416. xhr.withCredentials = true;
  417. } else if (request.credentials === 'omit') {
  418. xhr.withCredentials = false;
  419. }
  420. if ('responseType' in xhr && support.blob) {
  421. xhr.responseType = 'blob';
  422. }
  423. request.headers.forEach(function(value, name) {
  424. xhr.setRequestHeader(name, value);
  425. });
  426. if (request.signal) {
  427. request.signal.addEventListener('abort', abortXhr);
  428. xhr.onreadystatechange = function() {
  429. // DONE (success or failure)
  430. if (xhr.readyState === 4) {
  431. request.signal.removeEventListener('abort', abortXhr);
  432. }
  433. };
  434. }
  435. xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit);
  436. })
  437. }
  438. fetch.polyfill = true;
  439. if (!self.fetch) {
  440. self.fetch = fetch;
  441. self.Headers = Headers;
  442. self.Request = Request;
  443. self.Response = Response;
  444. }
  445. exports.Headers = Headers;
  446. exports.Request = Request;
  447. exports.Response = Response;
  448. exports.fetch = fetch;
  449. Object.defineProperty(exports, '__esModule', { value: true });
  450. return exports;
  451. })({});
  452. })(typeof self !== 'undefined' ? self : this);