browser-polyfill.js 18 KB

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