fetch.js 13 KB

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