index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. "use strict";
  2. var window = require("global/window")
  3. var isFunction = require("is-function")
  4. var parseHeaders = require("parse-headers")
  5. var xtend = require("xtend")
  6. module.exports = createXHR
  7. createXHR.XMLHttpRequest = window.XMLHttpRequest || noop
  8. createXHR.XDomainRequest = "withCredentials" in (new createXHR.XMLHttpRequest()) ? createXHR.XMLHttpRequest : window.XDomainRequest
  9. forEachArray(["get", "put", "post", "patch", "head", "delete"], function(method) {
  10. createXHR[method === "delete" ? "del" : method] = function(uri, options, callback) {
  11. options = initParams(uri, options, callback)
  12. options.method = method.toUpperCase()
  13. return _createXHR(options)
  14. }
  15. })
  16. function forEachArray(array, iterator) {
  17. for (var i = 0; i < array.length; i++) {
  18. iterator(array[i])
  19. }
  20. }
  21. function isEmpty(obj){
  22. for(var i in obj){
  23. if(obj.hasOwnProperty(i)) return false
  24. }
  25. return true
  26. }
  27. function initParams(uri, options, callback) {
  28. var params = uri
  29. if (isFunction(options)) {
  30. callback = options
  31. if (typeof uri === "string") {
  32. params = {uri:uri}
  33. }
  34. } else {
  35. params = xtend(options, {uri: uri})
  36. }
  37. params.callback = callback
  38. return params
  39. }
  40. function createXHR(uri, options, callback) {
  41. options = initParams(uri, options, callback)
  42. return _createXHR(options)
  43. }
  44. function _createXHR(options) {
  45. if(typeof options.callback === "undefined"){
  46. throw new Error("callback argument missing")
  47. }
  48. var called = false
  49. var callback = function cbOnce(err, response, body){
  50. if(!called){
  51. called = true
  52. options.callback(err, response, body)
  53. }
  54. }
  55. function readystatechange() {
  56. if (xhr.readyState === 4) {
  57. setTimeout(loadFunc, 0)
  58. }
  59. }
  60. function getBody() {
  61. // Chrome with requestType=blob throws errors arround when even testing access to responseText
  62. var body = undefined
  63. if (xhr.response) {
  64. body = xhr.response
  65. } else {
  66. body = xhr.responseText || getXml(xhr)
  67. }
  68. if (isJson) {
  69. try {
  70. body = JSON.parse(body)
  71. } catch (e) {}
  72. }
  73. return body
  74. }
  75. function errorFunc(evt) {
  76. clearTimeout(timeoutTimer)
  77. if(!(evt instanceof Error)){
  78. evt = new Error("" + (evt || "Unknown XMLHttpRequest Error") )
  79. }
  80. evt.statusCode = 0
  81. return callback(evt, failureResponse)
  82. }
  83. // will load the data & process the response in a special response object
  84. function loadFunc() {
  85. if (aborted) return
  86. var status
  87. clearTimeout(timeoutTimer)
  88. if(options.useXDR && xhr.status===undefined) {
  89. //IE8 CORS GET successful response doesn't have a status field, but body is fine
  90. status = 200
  91. } else {
  92. status = (xhr.status === 1223 ? 204 : xhr.status)
  93. }
  94. var response = failureResponse
  95. var err = null
  96. if (status !== 0){
  97. response = {
  98. body: getBody(),
  99. statusCode: status,
  100. method: method,
  101. headers: {},
  102. url: uri,
  103. rawRequest: xhr
  104. }
  105. if(xhr.getAllResponseHeaders){ //remember xhr can in fact be XDR for CORS in IE
  106. response.headers = parseHeaders(xhr.getAllResponseHeaders())
  107. }
  108. } else {
  109. err = new Error("Internal XMLHttpRequest Error")
  110. }
  111. return callback(err, response, response.body)
  112. }
  113. var xhr = options.xhr || null
  114. if (!xhr) {
  115. if (options.cors || options.useXDR) {
  116. xhr = new createXHR.XDomainRequest()
  117. }else{
  118. xhr = new createXHR.XMLHttpRequest()
  119. }
  120. }
  121. var key
  122. var aborted
  123. var uri = xhr.url = options.uri || options.url
  124. var method = xhr.method = options.method || "GET"
  125. var body = options.body || options.data
  126. var headers = xhr.headers = options.headers || {}
  127. var sync = !!options.sync
  128. var isJson = false
  129. var timeoutTimer
  130. var failureResponse = {
  131. body: undefined,
  132. headers: {},
  133. statusCode: 0,
  134. method: method,
  135. url: uri,
  136. rawRequest: xhr
  137. }
  138. if ("json" in options && options.json !== false) {
  139. isJson = true
  140. headers["accept"] || headers["Accept"] || (headers["Accept"] = "application/json") //Don't override existing accept header declared by user
  141. if (method !== "GET" && method !== "HEAD") {
  142. headers["content-type"] || headers["Content-Type"] || (headers["Content-Type"] = "application/json") //Don't override existing accept header declared by user
  143. body = JSON.stringify(options.json === true ? body : options.json)
  144. }
  145. }
  146. xhr.onreadystatechange = readystatechange
  147. xhr.onload = loadFunc
  148. xhr.onerror = errorFunc
  149. // IE9 must have onprogress be set to a unique function.
  150. xhr.onprogress = function () {
  151. // IE must die
  152. }
  153. xhr.onabort = function(){
  154. aborted = true;
  155. }
  156. xhr.ontimeout = errorFunc
  157. xhr.open(method, uri, !sync, options.username, options.password)
  158. //has to be after open
  159. if(!sync) {
  160. xhr.withCredentials = !!options.withCredentials
  161. }
  162. // Cannot set timeout with sync request
  163. // not setting timeout on the xhr object, because of old webkits etc. not handling that correctly
  164. // both npm's request and jquery 1.x use this kind of timeout, so this is being consistent
  165. if (!sync && options.timeout > 0 ) {
  166. timeoutTimer = setTimeout(function(){
  167. if (aborted) return
  168. aborted = true//IE9 may still call readystatechange
  169. xhr.abort("timeout")
  170. var e = new Error("XMLHttpRequest timeout")
  171. e.code = "ETIMEDOUT"
  172. errorFunc(e)
  173. }, options.timeout )
  174. }
  175. if (xhr.setRequestHeader) {
  176. for(key in headers){
  177. if(headers.hasOwnProperty(key)){
  178. xhr.setRequestHeader(key, headers[key])
  179. }
  180. }
  181. } else if (options.headers && !isEmpty(options.headers)) {
  182. throw new Error("Headers cannot be set on an XDomainRequest object")
  183. }
  184. if ("responseType" in options) {
  185. xhr.responseType = options.responseType
  186. }
  187. if ("beforeSend" in options &&
  188. typeof options.beforeSend === "function"
  189. ) {
  190. options.beforeSend(xhr)
  191. }
  192. // Microsoft Edge browser sends "undefined" when send is called with undefined value.
  193. // XMLHttpRequest spec says to pass null as body to indicate no body
  194. // See https://github.com/naugtur/xhr/issues/100.
  195. xhr.send(body || null)
  196. return xhr
  197. }
  198. function getXml(xhr) {
  199. if (xhr.responseType === "document") {
  200. return xhr.responseXML
  201. }
  202. var firefoxBugTakenEffect = xhr.responseXML && xhr.responseXML.documentElement.nodeName === "parsererror"
  203. if (xhr.responseType === "" && !firefoxBugTakenEffect) {
  204. return xhr.responseXML
  205. }
  206. return null
  207. }
  208. function noop() {}