Skip to content

Commit

Permalink
Merge branch 'v1.x' into feat/http-adapter-family-lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalBrainJS committed Dec 4, 2022
2 parents 544d3b5 + a6efeaf commit 1c826b3
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/SUPPORT_QUESTION.yml
@@ -1,4 +1,4 @@
name: '🤔 Support or Usage Questiont'
name: '🤔 Support or Usage Question'
description: Get help using Axios.
labels: ['question']
body:
Expand Down
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -1297,7 +1297,6 @@ You can use Gitpod, an online IDE(which is free for Open Source) for contributin
## Resources
* [Changelog](https://github.com/axios/axios/blob/v1.x/CHANGELOG.md)
* [Upgrade Guide](https://github.com/axios/axios/blob/v1.x/UPGRADE_GUIDE.md)
* [Ecosystem](https://github.com/axios/axios/blob/v1.x/ECOSYSTEM.md)
* [Contributing Guide](https://github.com/axios/axios/blob/v1.x/CONTRIBUTING.md)
* [Code of Conduct](https://github.com/axios/axios/blob/v1.x/CODE_OF_CONDUCT.md)
Expand Down
3 changes: 3 additions & 0 deletions index.d.cts
Expand Up @@ -321,6 +321,8 @@ declare namespace axios {
serialize?: CustomParamsSerializer;
}

type BrowserProgressEvent = any;

interface AxiosProgressEvent {
loaded: number;
total?: number;
Expand All @@ -330,6 +332,7 @@ declare namespace axios {
estimated?: number;
upload?: boolean;
download?: boolean;
event?: BrowserProgressEvent;
}

interface AxiosRequestConfig<D = any> {
Expand Down
12 changes: 11 additions & 1 deletion index.d.ts
Expand Up @@ -263,6 +263,8 @@ type MaxUploadRate = number;

type MaxDownloadRate = number;

type BrowserProgressEvent = any;

export interface AxiosProgressEvent {
loaded: number;
total?: number;
Expand All @@ -272,7 +274,7 @@ export interface AxiosProgressEvent {
estimated?: number;
upload?: boolean;
download?: boolean;
event?: ProgressEvent;
event?: BrowserProgressEvent;
}

type Milliseconds = number;
Expand Down Expand Up @@ -374,6 +376,14 @@ export class AxiosError<T = unknown, D = any> extends Error {
status?: number;
toJSON: () => object;
cause?: Error;
static from<T = unknown, D = any>(
error: Error | unknown,
code?: string,
config?: AxiosRequestConfig<D>,
request?: any,
response?: AxiosResponse<T, D>,
customProps?: object,
): AxiosError<T, D>;
static readonly ERR_FR_TOO_MANY_REDIRECTS = "ERR_FR_TOO_MANY_REDIRECTS";
static readonly ERR_BAD_OPTION_VALUE = "ERR_BAD_OPTION_VALUE";
static readonly ERR_BAD_OPTION = "ERR_BAD_OPTION";
Expand Down
6 changes: 4 additions & 2 deletions index.js
Expand Up @@ -16,7 +16,8 @@ const {
spread,
toFormData,
AxiosHeaders,
formToJSON
formToJSON,
mergeConfig
} = axios;

export {
Expand All @@ -33,5 +34,6 @@ export {
spread,
toFormData,
AxiosHeaders,
formToJSON
formToJSON,
mergeConfig
}
24 changes: 16 additions & 8 deletions lib/adapters/http.js
Expand Up @@ -21,6 +21,11 @@ import AxiosTransformStream from '../helpers/AxiosTransformStream.js';
import EventEmitter from 'events';
import callbackify from "../helpers/callbackify.js";

const zlibOptions = {
flush: zlib.constants.Z_SYNC_FLUSH,
finishFlush: zlib.constants.Z_SYNC_FLUSH
}

const isBrotliSupported = utils.isFunction(zlib.createBrotliDecompress);

const {http: httpFollow, https: httpsFollow} = followRedirects;
Expand Down Expand Up @@ -272,7 +277,7 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
}
}

const contentLength = +headers.getContentLength();
const contentLength = utils.toFiniteNumber(headers.getContentLength());

if (utils.isArray(maxRate)) {
maxUploadRate = maxRate[0];
Expand All @@ -287,7 +292,7 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
}

data = stream.pipeline([data, new AxiosTransformStream({
length: utils.toFiniteNumber(contentLength),
length: contentLength,
maxRate: utils.toFiniteNumber(maxUploadRate)
})], utils.noop);

Expand Down Expand Up @@ -330,7 +335,10 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
return reject(customErr);
}

headers.set('Accept-Encoding', 'gzip, deflate, br', false);
headers.set(
'Accept-Encoding',
'gzip, compress, deflate' + (isBrotliSupported ? ', br' : ''), false
);

const options = {
path,
Expand Down Expand Up @@ -404,17 +412,17 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
streams.push(transformStream);
}

// uncompress the response body transparently if required
// decompress the response body transparently if required
let responseStream = res;

// return the last request in case of redirects
const lastRequest = res.req || req;

// if decompress disabled we should not decompress
if (config.decompress !== false) {
if (config.decompress !== false && res.headers['content-encoding']) {
// if no content, but headers still say that it is encoded,
// remove the header not confuse downstream operations
if ((!responseLength || res.statusCode === 204) && res.headers['content-encoding']) {
if (method === 'HEAD' || res.statusCode === 204) {
delete res.headers['content-encoding'];
}

Expand All @@ -424,14 +432,14 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
case 'compress':
case 'deflate':
// add the unzipper to the body stream processing pipeline
streams.push(zlib.createUnzip());
streams.push(zlib.createUnzip(zlibOptions));

// remove the content-encoding in order to not confuse downstream operations
delete res.headers['content-encoding'];
break;
case 'br':
if (isBrotliSupported) {
streams.push(zlib.createBrotliDecompress());
streams.push(zlib.createBrotliDecompress(zlibOptions));
delete res.headers['content-encoding'];
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/adapters/xhr.js
Expand Up @@ -61,7 +61,7 @@ export default isXHRAdapterSupported && function (config) {
}
}

if (utils.isFormData(requestData) && platform.isStandardBrowserEnv) {
if (utils.isFormData(requestData) && (platform.isStandardBrowserEnv || platform.isStandardBrowserWebWorkerEnv)) {
requestHeaders.setContentType(false); // Let the browser set it
}

Expand Down
3 changes: 3 additions & 0 deletions lib/axios.js
Expand Up @@ -70,6 +70,9 @@ axios.spread = spread;
// Expose isAxiosError
axios.isAxiosError = isAxiosError;

// Expose mergeConfig
axios.mergeConfig = mergeConfig;

axios.AxiosHeaders = AxiosHeaders;

axios.formToJSON = thing => formDataToJSON(utils.isHTMLForm(thing) ? new FormData(thing) : thing);
Expand Down
19 changes: 19 additions & 0 deletions lib/platform/browser/index.js
Expand Up @@ -31,6 +31,24 @@ const isStandardBrowserEnv = (() => {
return typeof window !== 'undefined' && typeof document !== 'undefined';
})();

/**
* Determine if we're running in a standard browser webWorker environment
*
* Although the `isStandardBrowserEnv` method indicates that
* `allows axios to run in a web worker`, the WebWorker will still be
* filtered out due to its judgment standard
* `typeof window !== 'undefined' && typeof document !== 'undefined'`.
* This leads to a problem when axios post `FormData` in webWorker
*/
const isStandardBrowserWebWorkerEnv = (() => {
return (
typeof WorkerGlobalScope !== 'undefined' &&
self instanceof WorkerGlobalScope &&
typeof self.importScripts === 'function'
);
})();


export default {
isBrowser: true,
classes: {
Expand All @@ -39,5 +57,6 @@ export default {
Blob
},
isStandardBrowserEnv,
isStandardBrowserWebWorkerEnv,
protocols: ['http', 'https', 'file', 'blob', 'url', 'data']
};
4 changes: 4 additions & 0 deletions test/specs/api.spec.js
Expand Up @@ -50,6 +50,10 @@ describe('static api', function () {
it('should have isAxiosError properties', function () {
expect(typeof axios.isAxiosError).toEqual('function');
});

it('should have mergeConfig properties', function () {
expect(typeof axios.mergeConfig).toEqual('function');
});
});

describe('instance api', function () {
Expand Down
1 change: 1 addition & 0 deletions test/specs/instance.spec.js
Expand Up @@ -23,6 +23,7 @@ describe('instance', function () {
'spread',
'getUri',
'isAxiosError',
'mergeConfig',
'VERSION',
'default',
'toFormData',
Expand Down
19 changes: 18 additions & 1 deletion test/unit/adapters/http.js
Expand Up @@ -33,6 +33,7 @@ function setTimeoutAsync(ms) {

const pipelineAsync = util.promisify(stream.pipeline);
const finishedAsync = util.promisify(stream.finished);
const gzip = util.promisify(zlib.gzip);

function toleranceRange(positive, negative) {
const p = (1 + 1 / positive);
Expand Down Expand Up @@ -499,6 +500,23 @@ describe('supports http with nodejs', function () {

await axios.get(LOCAL_SERVER_URL);
});

it('should not fail if response content-length header is missing', async () => {
this.timeout(10000);

const str = 'zipped';
const zipped = await gzip(str);

server = await startHTTPServer((req, res) => {
res.setHeader('Content-Encoding', 'gzip');
res.removeHeader('Content-Length');
res.end(zipped);
});

const {data} = await axios.get(LOCAL_SERVER_URL);

assert.strictEqual(data, str);
});
});

it('should support UTF8', function (done) {
Expand Down Expand Up @@ -1413,7 +1431,6 @@ describe('supports http with nodejs', function () {

it('should omit a user-agent if one is explicitly disclaimed', function (done) {
server = http.createServer(function (req, res) {
console.log(req.headers);
assert.equal("user-agent" in req.headers, false);
assert.equal("User-Agent" in req.headers, false);
res.end();
Expand Down

0 comments on commit 1c826b3

Please sign in to comment.