axios 1.1.3 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of axios might be problematic. Click here for more details.

Files changed (47) hide show
  1. package/CHANGELOG.md +298 -75
  2. package/{UPGRADE_GUIDE.md → MIGRATION_GUIDE.md} +1 -1
  3. package/README.md +61 -25
  4. package/dist/axios.js +886 -582
  5. package/dist/axios.js.map +1 -1
  6. package/dist/axios.min.js +1 -1
  7. package/dist/axios.min.js.map +1 -1
  8. package/dist/browser/axios.cjs +3189 -0
  9. package/dist/browser/axios.cjs.map +1 -0
  10. package/dist/esm/axios.js +886 -625
  11. package/dist/esm/axios.js.map +1 -1
  12. package/dist/esm/axios.min.js +1 -1
  13. package/dist/esm/axios.min.js.map +1 -1
  14. package/dist/node/axios.cjs +972 -554
  15. package/dist/node/axios.cjs.map +1 -1
  16. package/index.d.cts +528 -0
  17. package/index.d.ts +116 -56
  18. package/index.js +12 -3
  19. package/lib/adapters/adapters.js +59 -0
  20. package/lib/adapters/http.js +87 -34
  21. package/lib/adapters/xhr.js +7 -4
  22. package/lib/axios.js +13 -3
  23. package/lib/core/Axios.js +10 -8
  24. package/lib/core/AxiosError.js +1 -1
  25. package/lib/core/AxiosHeaders.js +102 -80
  26. package/lib/core/dispatchRequest.js +7 -2
  27. package/lib/core/mergeConfig.js +50 -46
  28. package/lib/defaults/index.js +2 -21
  29. package/lib/env/classes/FormData.js +2 -2
  30. package/lib/env/data.js +1 -1
  31. package/lib/helpers/HttpStatusCode.js +71 -0
  32. package/lib/helpers/ZlibHeaderTransformStream.js +28 -0
  33. package/lib/helpers/formDataToStream.js +111 -0
  34. package/lib/helpers/readBlob.js +15 -0
  35. package/lib/helpers/speedometer.js +1 -1
  36. package/lib/helpers/toFormData.js +5 -15
  37. package/lib/platform/browser/classes/FormData.js +1 -1
  38. package/lib/platform/browser/index.js +20 -0
  39. package/lib/utils.js +107 -9
  40. package/package.json +86 -14
  41. package/bin/ssl_hotfix.js +0 -22
  42. package/gulpfile.js +0 -88
  43. package/karma.conf.cjs +0 -250
  44. package/lib/adapters/index.js +0 -33
  45. package/rollup.config.js +0 -90
  46. package/tsconfig.json +0 -14
  47. package/tslint.json +0 -6
package/index.d.ts CHANGED
@@ -1,30 +1,23 @@
1
- // TypeScript Version: 4.1
1
+ // TypeScript Version: 4.7
2
2
  type AxiosHeaderValue = AxiosHeaders | string | string[] | number | boolean | null;
3
- type RawAxiosHeaders = Record<string, AxiosHeaderValue>;
4
3
 
5
- type MethodsHeaders = {
6
- [Key in Method as Lowercase<Key>]: AxiosHeaders;
7
- };
8
-
9
- interface CommonHeaders {
10
- common: AxiosHeaders;
4
+ interface RawAxiosHeaders {
5
+ [key: string]: AxiosHeaderValue;
11
6
  }
12
7
 
13
- type AxiosHeaderMatcher = (this: AxiosHeaders, value: string, name: string, headers: RawAxiosHeaders) => boolean;
14
-
15
- type AxiosHeaderSetter = (value: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher) => AxiosHeaders;
16
-
17
- type AxiosHeaderGetter = ((parser?: RegExp) => RegExpExecArray | null) |
18
- ((matcher?: AxiosHeaderMatcher) => AxiosHeaderValue);
8
+ type MethodsHeaders = Partial<{
9
+ [Key in Method as Lowercase<Key>]: AxiosHeaders;
10
+ } & {common: AxiosHeaders}>;
19
11
 
20
- type AxiosHeaderTester = (matcher?: AxiosHeaderMatcher) => boolean;
12
+ type AxiosHeaderMatcher = (this: AxiosHeaders, value: string, name: string, headers: RawAxiosHeaders) => boolean;
21
13
 
22
14
  export class AxiosHeaders {
23
15
  constructor(
24
- headers?: RawAxiosHeaders | AxiosHeaders,
25
- defaultHeaders?: RawAxiosHeaders | AxiosHeaders
16
+ headers?: RawAxiosHeaders | AxiosHeaders
26
17
  );
27
18
 
19
+ [key: string]: any;
20
+
28
21
  set(headerName?: string, value?: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders;
29
22
  set(headers?: RawAxiosHeaders | AxiosHeaders, rewrite?: boolean): AxiosHeaders;
30
23
 
@@ -35,57 +28,87 @@ export class AxiosHeaders {
35
28
 
36
29
  delete(header: string | string[], matcher?: AxiosHeaderMatcher): boolean;
37
30
 
38
- clear(): boolean;
31
+ clear(matcher?: AxiosHeaderMatcher): boolean;
39
32
 
40
33
  normalize(format: boolean): AxiosHeaders;
41
34
 
35
+ concat(...targets: Array<AxiosHeaders | RawAxiosHeaders | string | undefined | null>): AxiosHeaders;
36
+
42
37
  toJSON(asStrings?: boolean): RawAxiosHeaders;
43
38
 
44
39
  static from(thing?: AxiosHeaders | RawAxiosHeaders | string): AxiosHeaders;
45
40
 
46
41
  static accessor(header: string | string[]): AxiosHeaders;
47
42
 
48
- setContentType: AxiosHeaderSetter;
49
- getContentType: AxiosHeaderGetter;
50
- hasContentType: AxiosHeaderTester;
43
+ static concat(...targets: Array<AxiosHeaders | RawAxiosHeaders | string | undefined | null>): AxiosHeaders;
44
+
45
+ setContentType(value: ContentType, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders;
46
+ getContentType(parser?: RegExp): RegExpExecArray | null;
47
+ getContentType(matcher?: AxiosHeaderMatcher): AxiosHeaderValue;
48
+ hasContentType(matcher?: AxiosHeaderMatcher): boolean;
49
+
50
+ setContentLength(value: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders;
51
+ getContentLength(parser?: RegExp): RegExpExecArray | null;
52
+ getContentLength(matcher?: AxiosHeaderMatcher): AxiosHeaderValue;
53
+ hasContentLength(matcher?: AxiosHeaderMatcher): boolean;
51
54
 
52
- setContentLength: AxiosHeaderSetter;
53
- getContentLength: AxiosHeaderGetter;
54
- hasContentLength: AxiosHeaderTester;
55
+ setAccept(value: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders;
56
+ getAccept(parser?: RegExp): RegExpExecArray | null;
57
+ getAccept(matcher?: AxiosHeaderMatcher): AxiosHeaderValue;
58
+ hasAccept(matcher?: AxiosHeaderMatcher): boolean;
55
59
 
56
- setAccept: AxiosHeaderSetter;
57
- getAccept: AxiosHeaderGetter;
58
- hasAccept: AxiosHeaderTester;
60
+ setUserAgent(value: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders;
61
+ getUserAgent(parser?: RegExp): RegExpExecArray | null;
62
+ getUserAgent(matcher?: AxiosHeaderMatcher): AxiosHeaderValue;
63
+ hasUserAgent(matcher?: AxiosHeaderMatcher): boolean;
59
64
 
60
- setUserAgent: AxiosHeaderSetter;
61
- getUserAgent: AxiosHeaderGetter;
62
- hasUserAgent: AxiosHeaderTester;
65
+ setContentEncoding(value: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders;
66
+ getContentEncoding(parser?: RegExp): RegExpExecArray | null;
67
+ getContentEncoding(matcher?: AxiosHeaderMatcher): AxiosHeaderValue;
68
+ hasContentEncoding(matcher?: AxiosHeaderMatcher): boolean;
63
69
 
64
- setContentEncoding: AxiosHeaderSetter;
65
- getContentEncoding: AxiosHeaderGetter;
66
- hasContentEncoding: AxiosHeaderTester;
70
+ setAuthorization(value: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders;
71
+ getAuthorization(parser?: RegExp): RegExpExecArray | null;
72
+ getAuthorization(matcher?: AxiosHeaderMatcher): AxiosHeaderValue;
73
+ hasAuthorization(matcher?: AxiosHeaderMatcher): boolean;
74
+
75
+ [Symbol.iterator](): IterableIterator<[string, AxiosHeaderValue]>;
67
76
  }
68
77
 
69
- export type RawAxiosRequestHeaders = Partial<RawAxiosHeaders & MethodsHeaders & CommonHeaders>;
78
+ type CommonRequestHeadersList = 'Accept' | 'Content-Length' | 'User-Agent' | 'Content-Encoding' | 'Authorization';
70
79
 
71
- export type AxiosRequestHeaders = Partial<RawAxiosHeaders & MethodsHeaders & CommonHeaders> & AxiosHeaders;
80
+ type ContentType = AxiosHeaderValue | 'text/html' | 'text/plain' | 'multipart/form-data' | 'application/json' | 'application/x-www-form-urlencoded' | 'application/octet-stream';
72
81
 
73
- export type RawAxiosResponseHeaders = Partial<Record<string, string> & {
74
- "set-cookie"?: string[]
82
+ export type RawAxiosRequestHeaders = Partial<RawAxiosHeaders & {
83
+ [Key in CommonRequestHeadersList]: AxiosHeaderValue;
84
+ } & {
85
+ 'Content-Type': ContentType
75
86
  }>;
76
87
 
88
+ export type AxiosRequestHeaders = RawAxiosRequestHeaders & AxiosHeaders;
89
+
90
+ type CommonResponseHeadersList = 'Server' | 'Content-Type' | 'Content-Length' | 'Cache-Control'| 'Content-Encoding';
91
+
92
+ type RawCommonResponseHeaders = {
93
+ [Key in CommonResponseHeadersList]: AxiosHeaderValue;
94
+ } & {
95
+ "set-cookie": string[];
96
+ };
97
+
98
+ export type RawAxiosResponseHeaders = Partial<RawAxiosHeaders & RawCommonResponseHeaders>;
99
+
77
100
  export type AxiosResponseHeaders = RawAxiosResponseHeaders & AxiosHeaders;
78
101
 
79
102
  export interface AxiosRequestTransformer {
80
- (this: AxiosRequestConfig, data: any, headers: AxiosRequestHeaders): any;
103
+ (this: InternalAxiosRequestConfig, data: any, headers: AxiosRequestHeaders): any;
81
104
  }
82
105
 
83
106
  export interface AxiosResponseTransformer {
84
- (this: AxiosRequestConfig, data: any, headers: AxiosResponseHeaders, status?: number): any;
107
+ (this: InternalAxiosRequestConfig, data: any, headers: AxiosResponseHeaders, status?: number): any;
85
108
  }
86
109
 
87
110
  export interface AxiosAdapter {
88
- (config: AxiosRequestConfig): AxiosPromise;
111
+ (config: InternalAxiosRequestConfig): AxiosPromise;
89
112
  }
90
113
 
91
114
  export interface AxiosBasicCredentials {
@@ -260,6 +283,8 @@ type MaxUploadRate = number;
260
283
 
261
284
  type MaxDownloadRate = number;
262
285
 
286
+ type BrowserProgressEvent = any;
287
+
263
288
  export interface AxiosProgressEvent {
264
289
  loaded: number;
265
290
  total?: number;
@@ -269,24 +294,29 @@ export interface AxiosProgressEvent {
269
294
  estimated?: number;
270
295
  upload?: boolean;
271
296
  download?: boolean;
297
+ event?: BrowserProgressEvent;
272
298
  }
273
299
 
274
300
  type Milliseconds = number;
275
301
 
302
+ type AxiosAdapterName = 'xhr' | 'http' | string;
303
+
304
+ type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName;
305
+
276
306
  export interface AxiosRequestConfig<D = any> {
277
307
  url?: string;
278
308
  method?: Method | string;
279
309
  baseURL?: string;
280
310
  transformRequest?: AxiosRequestTransformer | AxiosRequestTransformer[];
281
311
  transformResponse?: AxiosResponseTransformer | AxiosResponseTransformer[];
282
- headers?: RawAxiosRequestHeaders;
312
+ headers?: (RawAxiosRequestHeaders & MethodsHeaders) | AxiosHeaders;
283
313
  params?: any;
284
314
  paramsSerializer?: ParamsSerializerOptions;
285
315
  data?: D;
286
316
  timeout?: Milliseconds;
287
317
  timeoutErrorMessage?: string;
288
318
  withCredentials?: boolean;
289
- adapter?: AxiosAdapter;
319
+ adapter?: AxiosAdapterConfig | AxiosAdapterConfig[];
290
320
  auth?: AxiosBasicCredentials;
291
321
  responseType?: ResponseType;
292
322
  responseEncoding?: responseEncoding | string;
@@ -315,6 +345,13 @@ export interface AxiosRequestConfig<D = any> {
315
345
  formSerializer?: FormSerializerOptions;
316
346
  }
317
347
 
348
+ // Alias
349
+ export type RawAxiosRequestConfig<D = any> = AxiosRequestConfig<D>;
350
+
351
+ export interface InternalAxiosRequestConfig<D = any> extends AxiosRequestConfig<D> {
352
+ headers: AxiosRequestHeaders;
353
+ }
354
+
318
355
  export interface HeadersDefaults {
319
356
  common: RawAxiosRequestHeaders;
320
357
  delete: RawAxiosRequestHeaders;
@@ -334,15 +371,15 @@ export interface AxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'hea
334
371
  }
335
372
 
336
373
  export interface CreateAxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
337
- headers?: RawAxiosRequestHeaders | Partial<HeadersDefaults>;
374
+ headers?: RawAxiosRequestHeaders | AxiosHeaders | Partial<HeadersDefaults>;
338
375
  }
339
376
 
340
- export interface AxiosResponse<T = any, D = any> {
377
+ export interface AxiosResponse<T = any, D = any> {
341
378
  data: T;
342
379
  status: number;
343
380
  statusText: string;
344
381
  headers: RawAxiosResponseHeaders | AxiosResponseHeaders;
345
- config: AxiosRequestConfig<D>;
382
+ config: InternalAxiosRequestConfig<D>;
346
383
  request?: any;
347
384
  }
348
385
 
@@ -350,12 +387,12 @@ export class AxiosError<T = unknown, D = any> extends Error {
350
387
  constructor(
351
388
  message?: string,
352
389
  code?: string,
353
- config?: AxiosRequestConfig<D>,
390
+ config?: InternalAxiosRequestConfig<D>,
354
391
  request?: any,
355
392
  response?: AxiosResponse<T, D>
356
393
  );
357
394
 
358
- config?: AxiosRequestConfig<D>;
395
+ config?: InternalAxiosRequestConfig<D>;
359
396
  code?: string;
360
397
  request?: any;
361
398
  response?: AxiosResponse<T, D>;
@@ -363,6 +400,14 @@ export class AxiosError<T = unknown, D = any> extends Error {
363
400
  status?: number;
364
401
  toJSON: () => object;
365
402
  cause?: Error;
403
+ static from<T = unknown, D = any>(
404
+ error: Error | unknown,
405
+ code?: string,
406
+ config?: InternalAxiosRequestConfig<D>,
407
+ request?: any,
408
+ response?: AxiosResponse<T, D>,
409
+ customProps?: object,
410
+ ): AxiosError<T, D>;
366
411
  static readonly ERR_FR_TOO_MANY_REDIRECTS = "ERR_FR_TOO_MANY_REDIRECTS";
367
412
  static readonly ERR_BAD_OPTION_VALUE = "ERR_BAD_OPTION_VALUE";
368
413
  static readonly ERR_BAD_OPTION = "ERR_BAD_OPTION";
@@ -412,11 +457,11 @@ export interface CancelTokenSource {
412
457
 
413
458
  export interface AxiosInterceptorOptions {
414
459
  synchronous?: boolean;
415
- runWhen?: (config: AxiosRequestConfig) => boolean;
460
+ runWhen?: (config: InternalAxiosRequestConfig) => boolean;
416
461
  }
417
462
 
418
463
  export interface AxiosInterceptorManager<V> {
419
- use(onFulfilled?: (value: V) => V | Promise<V>, onRejected?: (error: any) => any, options?: AxiosInterceptorOptions): number;
464
+ use(onFulfilled?: ((value: V) => V | Promise<V>) | null, onRejected?: ((error: any) => any) | null, options?: AxiosInterceptorOptions): number;
420
465
  eject(id: number): void;
421
466
  clear(): void;
422
467
  }
@@ -425,7 +470,7 @@ export class Axios {
425
470
  constructor(config?: AxiosRequestConfig);
426
471
  defaults: AxiosDefaults;
427
472
  interceptors: {
428
- request: AxiosInterceptorManager<AxiosRequestConfig>;
473
+ request: AxiosInterceptorManager<InternalAxiosRequestConfig>;
429
474
  response: AxiosInterceptorManager<AxiosResponse>;
430
475
  };
431
476
  getUri(config?: AxiosRequestConfig): string;
@@ -463,19 +508,34 @@ export interface GenericHTMLFormElement {
463
508
  submit(): void;
464
509
  }
465
510
 
511
+ export function toFormData(sourceObj: object, targetFormData?: GenericFormData, options?: FormSerializerOptions): GenericFormData;
512
+
513
+ export function formToJSON(form: GenericFormData|GenericHTMLFormElement): object;
514
+
515
+ export function isAxiosError<T = any, D = any>(payload: any): payload is AxiosError<T, D>;
516
+
517
+ export function spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
518
+
519
+ export function isCancel(value: any): value is Cancel;
520
+
521
+ export function all<T>(values: Array<T | Promise<T>>): Promise<T[]>;
522
+
466
523
  export interface AxiosStatic extends AxiosInstance {
467
524
  create(config?: CreateAxiosDefaults): AxiosInstance;
468
525
  Cancel: CancelStatic;
469
526
  CancelToken: CancelTokenStatic;
470
527
  Axios: typeof Axios;
471
528
  AxiosError: typeof AxiosError;
529
+ HttpStatusCode: typeof HttpStatusCode;
472
530
  readonly VERSION: string;
473
- isCancel(value: any): value is Cancel;
474
- all<T>(values: Array<T | Promise<T>>): Promise<T[]>;
475
- spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
476
- isAxiosError<T = any, D = any>(payload: any): payload is AxiosError<T, D>;
477
- toFormData(sourceObj: object, targetFormData?: GenericFormData, options?: FormSerializerOptions): GenericFormData;
478
- formToJSON(form: GenericFormData|GenericHTMLFormElement): object;
531
+ isCancel: typeof isCancel;
532
+ all: typeof all;
533
+ spread: typeof spread;
534
+ isAxiosError: typeof isAxiosError;
535
+ toFormData: typeof toFormData;
536
+ formToJSON: typeof formToJSON;
537
+ CanceledError: typeof CanceledError;
538
+ AxiosHeaders: typeof AxiosHeaders;
479
539
  }
480
540
 
481
541
  declare const axios: AxiosStatic;
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import axios from './lib/axios.js';
2
2
 
3
+ // This module is intended to unwrap Axios default export as named.
3
4
  // Keep top-level export same with static properties
4
5
  // so that it can keep same with es module or cjs
5
6
  const {
@@ -13,11 +14,15 @@ const {
13
14
  Cancel,
14
15
  isAxiosError,
15
16
  spread,
16
- toFormData
17
+ toFormData,
18
+ AxiosHeaders,
19
+ HttpStatusCode,
20
+ formToJSON,
21
+ mergeConfig
17
22
  } = axios;
18
23
 
19
- export default axios;
20
24
  export {
25
+ axios as default,
21
26
  Axios,
22
27
  AxiosError,
23
28
  CanceledError,
@@ -28,5 +33,9 @@ export {
28
33
  Cancel,
29
34
  isAxiosError,
30
35
  spread,
31
- toFormData
36
+ toFormData,
37
+ AxiosHeaders,
38
+ HttpStatusCode,
39
+ formToJSON,
40
+ mergeConfig
32
41
  }
@@ -0,0 +1,59 @@
1
+ import utils from '../utils.js';
2
+ import httpAdapter from './http.js';
3
+ import xhrAdapter from './xhr.js';
4
+ import AxiosError from "../core/AxiosError.js";
5
+
6
+ const knownAdapters = {
7
+ http: httpAdapter,
8
+ xhr: xhrAdapter
9
+ }
10
+
11
+ utils.forEach(knownAdapters, (fn, value) => {
12
+ if(fn) {
13
+ try {
14
+ Object.defineProperty(fn, 'name', {value});
15
+ } catch (e) {
16
+ // eslint-disable-next-line no-empty
17
+ }
18
+ Object.defineProperty(fn, 'adapterName', {value});
19
+ }
20
+ });
21
+
22
+ export default {
23
+ getAdapter: (adapters) => {
24
+ adapters = utils.isArray(adapters) ? adapters : [adapters];
25
+
26
+ const {length} = adapters;
27
+ let nameOrAdapter;
28
+ let adapter;
29
+
30
+ for (let i = 0; i < length; i++) {
31
+ nameOrAdapter = adapters[i];
32
+ if((adapter = utils.isString(nameOrAdapter) ? knownAdapters[nameOrAdapter.toLowerCase()] : nameOrAdapter)) {
33
+ break;
34
+ }
35
+ }
36
+
37
+ if (!adapter) {
38
+ if (adapter === false) {
39
+ throw new AxiosError(
40
+ `Adapter ${nameOrAdapter} is not supported by the environment`,
41
+ 'ERR_NOT_SUPPORT'
42
+ );
43
+ }
44
+
45
+ throw new Error(
46
+ utils.hasOwnProp(knownAdapters, nameOrAdapter) ?
47
+ `Adapter '${nameOrAdapter}' is not available in the build` :
48
+ `Unknown adapter '${nameOrAdapter}'`
49
+ );
50
+ }
51
+
52
+ if (!utils.isFunction(adapter)) {
53
+ throw new TypeError('adapter is not a function');
54
+ }
55
+
56
+ return adapter;
57
+ },
58
+ adapters: knownAdapters
59
+ }
@@ -7,6 +7,7 @@ import buildURL from './../helpers/buildURL.js';
7
7
  import {getProxyForUrl} from 'proxy-from-env';
8
8
  import http from 'http';
9
9
  import https from 'https';
10
+ import util from 'util';
10
11
  import followRedirects from 'follow-redirects';
11
12
  import zlib from 'zlib';
12
13
  import {VERSION} from '../env/data.js';
@@ -19,6 +20,19 @@ import stream from 'stream';
19
20
  import AxiosHeaders from '../core/AxiosHeaders.js';
20
21
  import AxiosTransformStream from '../helpers/AxiosTransformStream.js';
21
22
  import EventEmitter from 'events';
23
+ import formDataToStream from "../helpers/formDataToStream.js";
24
+ import readBlob from "../helpers/readBlob.js";
25
+ import ZlibHeaderTransformStream from '../helpers/ZlibHeaderTransformStream.js';
26
+
27
+ const zlibOptions = {
28
+ flush: zlib.constants.Z_SYNC_FLUSH,
29
+ finishFlush: zlib.constants.Z_SYNC_FLUSH
30
+ };
31
+
32
+ const brotliOptions = {
33
+ flush: zlib.constants.BROTLI_OPERATION_FLUSH,
34
+ finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH
35
+ }
22
36
 
23
37
  const isBrotliSupported = utils.isFunction(zlib.createBrotliDecompress);
24
38
 
@@ -100,9 +114,12 @@ function setProxy(options, configProxy, location) {
100
114
  };
101
115
  }
102
116
 
117
+ const isHttpAdapterSupported = typeof process !== 'undefined' && utils.kindOf(process) === 'process';
118
+
103
119
  /*eslint consistent-return:0*/
104
- export default function httpAdapter(config) {
105
- return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {
120
+ export default isHttpAdapterSupported && function httpAdapter(config) {
121
+ /*eslint no-async-promise-executor:0*/
122
+ return new Promise(async function dispatchHttpRequest(resolvePromise, rejectPromise) {
106
123
  let data = config.data;
107
124
  const responseType = config.responseType;
108
125
  const responseEncoding = config.responseEncoding;
@@ -166,7 +183,7 @@ export default function httpAdapter(config) {
166
183
 
167
184
  // Parse url
168
185
  const fullPath = buildFullPath(config.baseURL, config.url);
169
- const parsed = new URL(fullPath);
186
+ const parsed = new URL(fullPath, 'http://localhost');
170
187
  const protocol = parsed.protocol || supportedProtocols[0];
171
188
 
172
189
  if (protocol === 'data:') {
@@ -193,7 +210,7 @@ export default function httpAdapter(config) {
193
210
  convertedData = convertedData.toString(responseEncoding);
194
211
 
195
212
  if (!responseEncoding || responseEncoding === 'utf8') {
196
- data = utils.stripBOM(convertedData);
213
+ convertedData = utils.stripBOM(convertedData);
197
214
  }
198
215
  } else if (responseType === 'stream') {
199
216
  convertedData = stream.Readable.from(convertedData);
@@ -203,7 +220,7 @@ export default function httpAdapter(config) {
203
220
  data: convertedData,
204
221
  status: 200,
205
222
  statusText: 'OK',
206
- headers: {},
223
+ headers: new AxiosHeaders(),
207
224
  config
208
225
  });
209
226
  }
@@ -230,9 +247,32 @@ export default function httpAdapter(config) {
230
247
  let maxUploadRate = undefined;
231
248
  let maxDownloadRate = undefined;
232
249
 
233
- // support for https://www.npmjs.com/package/form-data api
234
- if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
250
+ // support for spec compliant FormData objects
251
+ if (utils.isSpecCompliantForm(data)) {
252
+ const userBoundary = headers.getContentType(/boundary=([-_\w\d]{10,70})/i);
253
+
254
+ data = formDataToStream(data, (formHeaders) => {
255
+ headers.set(formHeaders);
256
+ }, {
257
+ tag: `axios-${VERSION}-boundary`,
258
+ boundary: userBoundary && userBoundary[1] || undefined
259
+ });
260
+ // support for https://www.npmjs.com/package/form-data api
261
+ } else if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
235
262
  headers.set(data.getHeaders());
263
+
264
+ if (!headers.hasContentLength()) {
265
+ try {
266
+ const knownLength = await util.promisify(data.getLength).call(data);
267
+ Number.isFinite(knownLength) && knownLength >= 0 && headers.setContentLength(knownLength);
268
+ /*eslint no-empty:0*/
269
+ } catch (e) {
270
+ }
271
+ }
272
+ } else if (utils.isBlob(data)) {
273
+ data.size && headers.setContentType(data.type || 'application/octet-stream');
274
+ headers.setContentLength(data.size || 0);
275
+ data = stream.Readable.from(readBlob(data));
236
276
  } else if (data && !utils.isStream(data)) {
237
277
  if (Buffer.isBuffer(data)) {
238
278
  // Nothing to do...
@@ -249,7 +289,7 @@ export default function httpAdapter(config) {
249
289
  }
250
290
 
251
291
  // Add Content-Length header if data exists
252
- headers.set('Content-Length', data.length, false);
292
+ headers.setContentLength(data.length, false);
253
293
 
254
294
  if (config.maxBodyLength > -1 && data.length > config.maxBodyLength) {
255
295
  return reject(new AxiosError(
@@ -260,7 +300,7 @@ export default function httpAdapter(config) {
260
300
  }
261
301
  }
262
302
 
263
- const contentLength = +headers.getContentLength();
303
+ const contentLength = utils.toFiniteNumber(headers.getContentLength());
264
304
 
265
305
  if (utils.isArray(maxRate)) {
266
306
  maxUploadRate = maxRate[0];
@@ -275,7 +315,7 @@ export default function httpAdapter(config) {
275
315
  }
276
316
 
277
317
  data = stream.pipeline([data, new AxiosTransformStream({
278
- length: utils.toFiniteNumber(contentLength),
318
+ length: contentLength,
279
319
  maxRate: utils.toFiniteNumber(maxUploadRate)
280
320
  })], utils.noop);
281
321
 
@@ -318,7 +358,10 @@ export default function httpAdapter(config) {
318
358
  return reject(customErr);
319
359
  }
320
360
 
321
- headers.set('Accept-Encoding', 'gzip, deflate, br', false);
361
+ headers.set(
362
+ 'Accept-Encoding',
363
+ 'gzip, compress, deflate' + (isBrotliSupported ? ', br' : ''), false
364
+ );
322
365
 
323
366
  const options = {
324
367
  path,
@@ -373,56 +416,66 @@ export default function httpAdapter(config) {
373
416
 
374
417
  const streams = [res];
375
418
 
376
- // uncompress the response body transparently if required
419
+ const responseLength = +res.headers['content-length'];
420
+
421
+ if (onDownloadProgress) {
422
+ const transformStream = new AxiosTransformStream({
423
+ length: utils.toFiniteNumber(responseLength),
424
+ maxRate: utils.toFiniteNumber(maxDownloadRate)
425
+ });
426
+
427
+ onDownloadProgress && transformStream.on('progress', progress => {
428
+ onDownloadProgress(Object.assign(progress, {
429
+ download: true
430
+ }));
431
+ });
432
+
433
+ streams.push(transformStream);
434
+ }
435
+
436
+ // decompress the response body transparently if required
377
437
  let responseStream = res;
378
438
 
379
439
  // return the last request in case of redirects
380
440
  const lastRequest = res.req || req;
381
441
 
382
442
  // if decompress disabled we should not decompress
383
- if (config.decompress !== false) {
443
+ if (config.decompress !== false && res.headers['content-encoding']) {
384
444
  // if no content, but headers still say that it is encoded,
385
445
  // remove the header not confuse downstream operations
386
- if (data && data.length === 0 && res.headers['content-encoding']) {
446
+ if (method === 'HEAD' || res.statusCode === 204) {
387
447
  delete res.headers['content-encoding'];
388
448
  }
389
449
 
390
450
  switch (res.headers['content-encoding']) {
391
451
  /*eslint default-case:0*/
392
452
  case 'gzip':
453
+ case 'x-gzip':
393
454
  case 'compress':
455
+ case 'x-compress':
456
+ // add the unzipper to the body stream processing pipeline
457
+ streams.push(zlib.createUnzip(zlibOptions));
458
+
459
+ // remove the content-encoding in order to not confuse downstream operations
460
+ delete res.headers['content-encoding'];
461
+ break;
394
462
  case 'deflate':
463
+ streams.push(new ZlibHeaderTransformStream());
464
+
395
465
  // add the unzipper to the body stream processing pipeline
396
- streams.push(zlib.createUnzip());
466
+ streams.push(zlib.createUnzip(zlibOptions));
397
467
 
398
468
  // remove the content-encoding in order to not confuse downstream operations
399
469
  delete res.headers['content-encoding'];
400
470
  break;
401
471
  case 'br':
402
472
  if (isBrotliSupported) {
403
- streams.push(zlib.createBrotliDecompress());
473
+ streams.push(zlib.createBrotliDecompress(brotliOptions));
404
474
  delete res.headers['content-encoding'];
405
475
  }
406
476
  }
407
477
  }
408
478
 
409
- if (onDownloadProgress) {
410
- const responseLength = +res.headers['content-length'];
411
-
412
- const transformStream = new AxiosTransformStream({
413
- length: utils.toFiniteNumber(responseLength),
414
- maxRate: utils.toFiniteNumber(maxDownloadRate)
415
- });
416
-
417
- onDownloadProgress && transformStream.on('progress', progress => {
418
- onDownloadProgress(Object.assign(progress, {
419
- download: true
420
- }));
421
- });
422
-
423
- streams.push(transformStream);
424
- }
425
-
426
479
  responseStream = streams.length > 1 ? stream.pipeline(streams, utils.noop) : streams[0];
427
480
 
428
481
  const offListeners = stream.finished(responseStream, () => {
@@ -588,4 +641,4 @@ export default function httpAdapter(config) {
588
641
  });
589
642
  }
590
643
 
591
- export const __setProxy = setProxy;
644
+ export const __setProxy = setProxy;
@@ -33,7 +33,8 @@ function progressEventReducer(listener, isDownloadStream) {
33
33
  progress: total ? (loaded / total) : undefined,
34
34
  bytes: progressBytes,
35
35
  rate: rate ? rate : undefined,
36
- estimated: rate && total && inRange ? (total - loaded) / rate : undefined
36
+ estimated: rate && total && inRange ? (total - loaded) / rate : undefined,
37
+ event: e
37
38
  };
38
39
 
39
40
  data[isDownloadStream ? 'download' : 'upload'] = true;
@@ -42,7 +43,9 @@ function progressEventReducer(listener, isDownloadStream) {
42
43
  };
43
44
  }
44
45
 
45
- export default function xhrAdapter(config) {
46
+ const isXHRAdapterSupported = typeof XMLHttpRequest !== 'undefined';
47
+
48
+ export default isXHRAdapterSupported && function (config) {
46
49
  return new Promise(function dispatchXhrRequest(resolve, reject) {
47
50
  let requestData = config.data;
48
51
  const requestHeaders = AxiosHeaders.from(config.headers).normalize();
@@ -58,7 +61,7 @@ export default function xhrAdapter(config) {
58
61
  }
59
62
  }
60
63
 
61
- if (utils.isFormData(requestData) && platform.isStandardBrowserEnv) {
64
+ if (utils.isFormData(requestData) && (platform.isStandardBrowserEnv || platform.isStandardBrowserWebWorkerEnv)) {
62
65
  requestHeaders.setContentType(false); // Let the browser set it
63
66
  }
64
67
 
@@ -86,7 +89,7 @@ export default function xhrAdapter(config) {
86
89
  const responseHeaders = AxiosHeaders.from(
87
90
  'getAllResponseHeaders' in request && request.getAllResponseHeaders()
88
91
  );
89
- const responseData = !responseType || responseType === 'text' || responseType === 'json' ?
92
+ const responseData = !responseType || responseType === 'text' || responseType === 'json' ?
90
93
  request.responseText : request.response;
91
94
  const response = {
92
95
  data: responseData,