browser-ponyfill.js 15 KB

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