fetch.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import platform from "../platform/index.js";
  2. import utils from "../utils.js";
  3. import AxiosError from "../core/AxiosError.js";
  4. import composeSignals from "../helpers/composeSignals.js";
  5. import {trackStream} from "../helpers/trackStream.js";
  6. import AxiosHeaders from "../core/AxiosHeaders.js";
  7. import {progressEventReducer, progressEventDecorator, asyncDecorator} from "../helpers/progressEventReducer.js";
  8. import resolveConfig from "../helpers/resolveConfig.js";
  9. import settle from "../core/settle.js";
  10. const isFetchSupported = typeof fetch === 'function' && typeof Request === 'function' && typeof Response === 'function';
  11. const isReadableStreamSupported = isFetchSupported && typeof ReadableStream === 'function';
  12. // used only inside the fetch adapter
  13. const encodeText = isFetchSupported && (typeof TextEncoder === 'function' ?
  14. ((encoder) => (str) => encoder.encode(str))(new TextEncoder()) :
  15. async (str) => new Uint8Array(await new Response(str).arrayBuffer())
  16. );
  17. const test = (fn, ...args) => {
  18. try {
  19. return !!fn(...args);
  20. } catch (e) {
  21. return false
  22. }
  23. }
  24. const supportsRequestStream = isReadableStreamSupported && test(() => {
  25. let duplexAccessed = false;
  26. const hasContentType = new Request(platform.origin, {
  27. body: new ReadableStream(),
  28. method: 'POST',
  29. get duplex() {
  30. duplexAccessed = true;
  31. return 'half';
  32. },
  33. }).headers.has('Content-Type');
  34. return duplexAccessed && !hasContentType;
  35. });
  36. const DEFAULT_CHUNK_SIZE = 64 * 1024;
  37. const supportsResponseStream = isReadableStreamSupported &&
  38. test(() => utils.isReadableStream(new Response('').body));
  39. const resolvers = {
  40. stream: supportsResponseStream && ((res) => res.body)
  41. };
  42. isFetchSupported && (((res) => {
  43. ['text', 'arrayBuffer', 'blob', 'formData', 'stream'].forEach(type => {
  44. !resolvers[type] && (resolvers[type] = utils.isFunction(res[type]) ? (res) => res[type]() :
  45. (_, config) => {
  46. throw new AxiosError(`Response type '${type}' is not supported`, AxiosError.ERR_NOT_SUPPORT, config);
  47. })
  48. });
  49. })(new Response));
  50. const getBodyLength = async (body) => {
  51. if (body == null) {
  52. return 0;
  53. }
  54. if(utils.isBlob(body)) {
  55. return body.size;
  56. }
  57. if(utils.isSpecCompliantForm(body)) {
  58. return (await new Request(body).arrayBuffer()).byteLength;
  59. }
  60. if(utils.isArrayBufferView(body) || utils.isArrayBuffer(body)) {
  61. return body.byteLength;
  62. }
  63. if(utils.isURLSearchParams(body)) {
  64. body = body + '';
  65. }
  66. if(utils.isString(body)) {
  67. return (await encodeText(body)).byteLength;
  68. }
  69. }
  70. const resolveBodyLength = async (headers, body) => {
  71. const length = utils.toFiniteNumber(headers.getContentLength());
  72. return length == null ? getBodyLength(body) : length;
  73. }
  74. export default isFetchSupported && (async (config) => {
  75. let {
  76. url,
  77. method,
  78. data,
  79. signal,
  80. cancelToken,
  81. timeout,
  82. onDownloadProgress,
  83. onUploadProgress,
  84. responseType,
  85. headers,
  86. withCredentials = 'same-origin',
  87. fetchOptions
  88. } = resolveConfig(config);
  89. responseType = responseType ? (responseType + '').toLowerCase() : 'text';
  90. let [composedSignal, stopTimeout] = (signal || cancelToken || timeout) ?
  91. composeSignals([signal, cancelToken], timeout) : [];
  92. let finished, request;
  93. const onFinish = () => {
  94. !finished && setTimeout(() => {
  95. composedSignal && composedSignal.unsubscribe();
  96. });
  97. finished = true;
  98. }
  99. let requestContentLength;
  100. try {
  101. if (
  102. onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head' &&
  103. (requestContentLength = await resolveBodyLength(headers, data)) !== 0
  104. ) {
  105. let _request = new Request(url, {
  106. method: 'POST',
  107. body: data,
  108. duplex: "half"
  109. });
  110. let contentTypeHeader;
  111. if (utils.isFormData(data) && (contentTypeHeader = _request.headers.get('content-type'))) {
  112. headers.setContentType(contentTypeHeader)
  113. }
  114. if (_request.body) {
  115. const [onProgress, flush] = progressEventDecorator(
  116. requestContentLength,
  117. progressEventReducer(asyncDecorator(onUploadProgress))
  118. );
  119. data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, onProgress, flush, encodeText);
  120. }
  121. }
  122. if (!utils.isString(withCredentials)) {
  123. withCredentials = withCredentials ? 'include' : 'omit';
  124. }
  125. // Cloudflare Workers throws when credentials are defined
  126. // see https://github.com/cloudflare/workerd/issues/902
  127. const isCredentialsSupported = "credentials" in Request.prototype;
  128. request = new Request(url, {
  129. ...fetchOptions,
  130. signal: composedSignal,
  131. method: method.toUpperCase(),
  132. headers: headers.normalize().toJSON(),
  133. body: data,
  134. duplex: "half",
  135. credentials: isCredentialsSupported ? withCredentials : undefined
  136. });
  137. let response = await fetch(request);
  138. const isStreamResponse = supportsResponseStream && (responseType === 'stream' || responseType === 'response');
  139. if (supportsResponseStream && (onDownloadProgress || isStreamResponse)) {
  140. const options = {};
  141. ['status', 'statusText', 'headers'].forEach(prop => {
  142. options[prop] = response[prop];
  143. });
  144. const responseContentLength = utils.toFiniteNumber(response.headers.get('content-length'));
  145. const [onProgress, flush] = onDownloadProgress && progressEventDecorator(
  146. responseContentLength,
  147. progressEventReducer(asyncDecorator(onDownloadProgress), true)
  148. ) || [];
  149. response = new Response(
  150. trackStream(response.body, DEFAULT_CHUNK_SIZE, onProgress, () => {
  151. flush && flush();
  152. isStreamResponse && onFinish();
  153. }, encodeText),
  154. options
  155. );
  156. }
  157. responseType = responseType || 'text';
  158. let responseData = await resolvers[utils.findKey(resolvers, responseType) || 'text'](response, config);
  159. !isStreamResponse && onFinish();
  160. stopTimeout && stopTimeout();
  161. return await new Promise((resolve, reject) => {
  162. settle(resolve, reject, {
  163. data: responseData,
  164. headers: AxiosHeaders.from(response.headers),
  165. status: response.status,
  166. statusText: response.statusText,
  167. config,
  168. request
  169. })
  170. })
  171. } catch (err) {
  172. onFinish();
  173. if (err && err.name === 'TypeError' && /fetch/i.test(err.message)) {
  174. throw Object.assign(
  175. new AxiosError('Network Error', AxiosError.ERR_NETWORK, config, request),
  176. {
  177. cause: err.cause || err
  178. }
  179. )
  180. }
  181. throw AxiosError.from(err, err && err.code, config, request);
  182. }
  183. });