Pictures!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

polyfills.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Fetch
  3. * https://github.com/github/fetch
  4. */
  5. (function(self) {
  6. 'use strict';
  7. if (self.fetch) {
  8. return
  9. }
  10. var support = {
  11. searchParams: 'URLSearchParams' in self,
  12. iterable: 'Symbol' in self && 'iterator' in Symbol,
  13. blob: 'FileReader' in self && 'Blob' in self && (function() {
  14. try {
  15. new Blob()
  16. return true
  17. } catch(e) {
  18. return false
  19. }
  20. })(),
  21. formData: 'FormData' in self,
  22. arrayBuffer: 'ArrayBuffer' in self
  23. }
  24. function normalizeName(name) {
  25. if (typeof name !== 'string') {
  26. name = String(name)
  27. }
  28. if (/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(name)) {
  29. throw new TypeError('Invalid character in header field name')
  30. }
  31. return name.toLowerCase()
  32. }
  33. function normalizeValue(value) {
  34. if (typeof value !== 'string') {
  35. value = String(value)
  36. }
  37. return value
  38. }
  39. // Build a destructive iterator for the value list
  40. function iteratorFor(items) {
  41. var iterator = {
  42. next: function() {
  43. var value = items.shift()
  44. return {done: value === undefined, value: value}
  45. }
  46. }
  47. if (support.iterable) {
  48. iterator[Symbol.iterator] = function() {
  49. return iterator
  50. }
  51. }
  52. return iterator
  53. }
  54. function Headers(headers) {
  55. this.map = {}
  56. if (headers instanceof Headers) {
  57. headers.forEach(function(value, name) {
  58. this.append(name, value)
  59. }, this)
  60. } else if (headers) {
  61. Object.getOwnPropertyNames(headers).forEach(function(name) {
  62. this.append(name, headers[name])
  63. }, this)
  64. }
  65. }
  66. Headers.prototype.append = function(name, value) {
  67. name = normalizeName(name)
  68. value = normalizeValue(value)
  69. var list = this.map[name]
  70. if (!list) {
  71. list = []
  72. this.map[name] = list
  73. }
  74. list.push(value)
  75. }
  76. Headers.prototype['delete'] = function(name) {
  77. delete this.map[normalizeName(name)]
  78. }
  79. Headers.prototype.get = function(name) {
  80. var values = this.map[normalizeName(name)]
  81. return values ? values[0] : null
  82. }
  83. Headers.prototype.getAll = function(name) {
  84. return this.map[normalizeName(name)] || []
  85. }
  86. Headers.prototype.has = function(name) {
  87. return this.map.hasOwnProperty(normalizeName(name))
  88. }
  89. Headers.prototype.set = function(name, value) {
  90. this.map[normalizeName(name)] = [normalizeValue(value)]
  91. }
  92. Headers.prototype.forEach = function(callback, thisArg) {
  93. Object.getOwnPropertyNames(this.map).forEach(function(name) {
  94. this.map[name].forEach(function(value) {
  95. callback.call(thisArg, value, name, this)
  96. }, this)
  97. }, this)
  98. }
  99. Headers.prototype.keys = function() {
  100. var items = []
  101. this.forEach(function(value, name) { items.push(name) })
  102. return iteratorFor(items)
  103. }
  104. Headers.prototype.values = function() {
  105. var items = []
  106. this.forEach(function(value) { items.push(value) })
  107. return iteratorFor(items)
  108. }
  109. Headers.prototype.entries = function() {
  110. var items = []
  111. this.forEach(function(value, name) { items.push([name, value]) })
  112. return iteratorFor(items)
  113. }
  114. if (support.iterable) {
  115. Headers.prototype[Symbol.iterator] = Headers.prototype.entries
  116. }
  117. function consumed(body) {
  118. if (body.bodyUsed) {
  119. return Promise.reject(new TypeError('Already read'))
  120. }
  121. body.bodyUsed = true
  122. }
  123. function fileReaderReady(reader) {
  124. return new Promise(function(resolve, reject) {
  125. reader.onload = function() {
  126. resolve(reader.result)
  127. }
  128. reader.onerror = function() {
  129. reject(reader.error)
  130. }
  131. })
  132. }
  133. function readBlobAsArrayBuffer(blob) {
  134. var reader = new FileReader()
  135. reader.readAsArrayBuffer(blob)
  136. return fileReaderReady(reader)
  137. }
  138. function readBlobAsText(blob) {
  139. var reader = new FileReader()
  140. reader.readAsText(blob)
  141. return fileReaderReady(reader)
  142. }
  143. function Body() {
  144. this.bodyUsed = false
  145. this._initBody = function(body) {
  146. this._bodyInit = body
  147. if (typeof body === 'string') {
  148. this._bodyText = body
  149. } else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
  150. this._bodyBlob = body
  151. } else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
  152. this._bodyFormData = body
  153. } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
  154. this._bodyText = body.toString()
  155. } else if (!body) {
  156. this._bodyText = ''
  157. } else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) {
  158. // Only support ArrayBuffers for POST method.
  159. // Receiving ArrayBuffers happens via Blobs, instead.
  160. } else {
  161. throw new Error('unsupported BodyInit type')
  162. }
  163. if (!this.headers.get('content-type')) {
  164. if (typeof body === 'string') {
  165. this.headers.set('content-type', 'text/plain;charset=UTF-8')
  166. } else if (this._bodyBlob && this._bodyBlob.type) {
  167. this.headers.set('content-type', this._bodyBlob.type)
  168. } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
  169. this.headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8')
  170. }
  171. }
  172. }
  173. if (support.blob) {
  174. this.blob = function() {
  175. var rejected = consumed(this)
  176. if (rejected) {
  177. return rejected
  178. }
  179. if (this._bodyBlob) {
  180. return Promise.resolve(this._bodyBlob)
  181. } else if (this._bodyFormData) {
  182. throw new Error('could not read FormData body as blob')
  183. } else {
  184. return Promise.resolve(new Blob([this._bodyText]))
  185. }
  186. }
  187. this.arrayBuffer = function() {
  188. return this.blob().then(readBlobAsArrayBuffer)
  189. }
  190. this.text = function() {
  191. var rejected = consumed(this)
  192. if (rejected) {
  193. return rejected
  194. }
  195. if (this._bodyBlob) {
  196. return readBlobAsText(this._bodyBlob)
  197. } else if (this._bodyFormData) {
  198. throw new Error('could not read FormData body as text')
  199. } else {
  200. return Promise.resolve(this._bodyText)
  201. }
  202. }
  203. } else {
  204. this.text = function() {
  205. var rejected = consumed(this)
  206. return rejected ? rejected : Promise.resolve(this._bodyText)
  207. }
  208. }
  209. if (support.formData) {
  210. this.formData = function() {
  211. return this.text().then(decode)
  212. }
  213. }
  214. this.json = function() {
  215. return this.text().then(JSON.parse)
  216. }
  217. return this
  218. }
  219. // HTTP methods whose capitalization should be normalized
  220. var methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT']
  221. function normalizeMethod(method) {
  222. var upcased = method.toUpperCase()
  223. return (methods.indexOf(upcased) > -1) ? upcased : method
  224. }
  225. function Request(input, options) {
  226. options = options || {}
  227. var body = options.body
  228. if (Request.prototype.isPrototypeOf(input)) {
  229. if (input.bodyUsed) {
  230. throw new TypeError('Already read')
  231. }
  232. this.url = input.url
  233. this.credentials = input.credentials
  234. if (!options.headers) {
  235. this.headers = new Headers(input.headers)
  236. }
  237. this.method = input.method
  238. this.mode = input.mode
  239. if (!body) {
  240. body = input._bodyInit
  241. input.bodyUsed = true
  242. }
  243. } else {
  244. this.url = input
  245. }
  246. this.credentials = options.credentials || this.credentials || 'omit'
  247. if (options.headers || !this.headers) {
  248. this.headers = new Headers(options.headers)
  249. }
  250. this.method = normalizeMethod(options.method || this.method || 'GET')
  251. this.mode = options.mode || this.mode || null
  252. this.referrer = null
  253. if ((this.method === 'GET' || this.method === 'HEAD') && body) {
  254. throw new TypeError('Body not allowed for GET or HEAD requests')
  255. }
  256. this._initBody(body)
  257. }
  258. Request.prototype.clone = function() {
  259. return new Request(this)
  260. }
  261. function decode(body) {
  262. var form = new FormData()
  263. body.trim().split('&').forEach(function(bytes) {
  264. if (bytes) {
  265. var split = bytes.split('=')
  266. var name = split.shift().replace(/\+/g, ' ')
  267. var value = split.join('=').replace(/\+/g, ' ')
  268. form.append(decodeURIComponent(name), decodeURIComponent(value))
  269. }
  270. })
  271. return form
  272. }
  273. function headers(xhr) {
  274. var head = new Headers()
  275. var pairs = (xhr.getAllResponseHeaders() || '').trim().split('\n')
  276. pairs.forEach(function(header) {
  277. var split = header.trim().split(':')
  278. var key = split.shift().trim()
  279. var value = split.join(':').trim()
  280. head.append(key, value)
  281. })
  282. return head
  283. }
  284. Body.call(Request.prototype)
  285. function Response(bodyInit, options) {
  286. if (!options) {
  287. options = {}
  288. }
  289. this.type = 'default'
  290. this.status = options.status
  291. this.ok = this.status >= 200 && this.status < 300
  292. this.statusText = options.statusText
  293. this.headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers)
  294. this.url = options.url || ''
  295. this._initBody(bodyInit)
  296. }
  297. Body.call(Response.prototype)
  298. Response.prototype.clone = function() {
  299. return new Response(this._bodyInit, {
  300. status: this.status,
  301. statusText: this.statusText,
  302. headers: new Headers(this.headers),
  303. url: this.url
  304. })
  305. }
  306. Response.error = function() {
  307. var response = new Response(null, {status: 0, statusText: ''})
  308. response.type = 'error'
  309. return response
  310. }
  311. var redirectStatuses = [301, 302, 303, 307, 308]
  312. Response.redirect = function(url, status) {
  313. if (redirectStatuses.indexOf(status) === -1) {
  314. throw new RangeError('Invalid status code')
  315. }
  316. return new Response(null, {status: status, headers: {location: url}})
  317. }
  318. self.Headers = Headers
  319. self.Request = Request
  320. self.Response = Response
  321. self.fetch = function(input, init) {
  322. return new Promise(function(resolve, reject) {
  323. var request
  324. if (Request.prototype.isPrototypeOf(input) && !init) {
  325. request = input
  326. } else {
  327. request = new Request(input, init)
  328. }
  329. var xhr = new XMLHttpRequest()
  330. function responseURL() {
  331. if ('responseURL' in xhr) {
  332. return xhr.responseURL
  333. }
  334. // Avoid security warnings on getResponseHeader when not allowed by CORS
  335. if (/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())) {
  336. return xhr.getResponseHeader('X-Request-URL')
  337. }
  338. return
  339. }
  340. xhr.onload = function() {
  341. var options = {
  342. status: xhr.status,
  343. statusText: xhr.statusText,
  344. headers: headers(xhr),
  345. url: responseURL()
  346. }
  347. var body = 'response' in xhr ? xhr.response : xhr.responseText
  348. resolve(new Response(body, options))
  349. }
  350. xhr.onerror = function() {
  351. reject(new TypeError('Network request failed'))
  352. }
  353. xhr.ontimeout = function() {
  354. reject(new TypeError('Network request failed'))
  355. }
  356. xhr.open(request.method, request.url, true)
  357. if (request.credentials === 'include') {
  358. xhr.withCredentials = true
  359. }
  360. if ('responseType' in xhr && support.blob) {
  361. xhr.responseType = 'blob'
  362. }
  363. request.headers.forEach(function(value, name) {
  364. xhr.setRequestHeader(name, value)
  365. })
  366. xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit)
  367. })
  368. }
  369. self.fetch.polyfill = true
  370. })(typeof self !== 'undefined' ? self : this);