Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add progressUpdateIntervalMs config option; #6379

Open
wants to merge 3 commits into
base: v1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ These are the available config options for making requests. Only the `url` is re
// Do whatever you want with the Axios progress event
},

// `progressUpdateIntervalMs` controls the frequency in milliseconds of download and upload progress events
// browser & node.js
progressUpdateIntervalMs: 100

// `maxContentLength` defines the max size of the http response content in bytes allowed in node.js
maxContentLength: 2000,

Expand Down
50 changes: 29 additions & 21 deletions dist/axios.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/axios.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/axios.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/axios.min.js.map

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions dist/browser/axios.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2530,14 +2530,19 @@ var xhrAdapter = isXHRAdapterSupported && function (config) {
request.responseType = _config.responseType;
}

let progressUpdateTicksRate = undefined;
if (utils$1.isNumber(config.progressUpdateIntervalMs)) {
progressUpdateTicksRate = 1000 / config.progressUpdateIntervalMs;
}

// Handle progress if needed
if (typeof _config.onDownloadProgress === 'function') {
request.addEventListener('progress', progressEventReducer(_config.onDownloadProgress, true));
request.addEventListener('progress', progressEventReducer(_config.onDownloadProgress, true, progressUpdateTicksRate));
}

// Not all browsers support upload events
if (typeof _config.onUploadProgress === 'function' && request.upload) {
request.upload.addEventListener('progress', progressEventReducer(_config.onUploadProgress));
request.upload.addEventListener('progress', progressEventReducer(_config.onUploadProgress, progressUpdateTicksRate));
}

if (_config.cancelToken || _config.signal) {
Expand Down Expand Up @@ -2781,6 +2786,11 @@ var fetchAdapter = isFetchSupported && (async (config) => {
};

try {
let progressUpdateTicksRate = undefined;
if (utils$1.isNumber(config.progressUpdateIntervalMs)) {
progressUpdateTicksRate = 1000 / config.progressUpdateIntervalMs;
}

if (onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head') {
let requestContentLength = await resolveBodyLength(headers, data);

Expand All @@ -2798,7 +2808,7 @@ var fetchAdapter = isFetchSupported && (async (config) => {

data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator(
requestContentLength,
progressEventReducer(onUploadProgress)
progressEventReducer(onUploadProgress, false, progressUpdateTicksRate)
));
}

Expand Down Expand Up @@ -2832,7 +2842,7 @@ var fetchAdapter = isFetchSupported && (async (config) => {
response = new Response(
trackStream(response.body, DEFAULT_CHUNK_SIZE, onDownloadProgress && fetchProgressDecorator(
responseContentLength,
progressEventReducer(onDownloadProgress, true)
progressEventReducer(onDownloadProgress, true, progressUpdateTicksRate)
), isStreamResponse && onFinish),
options
);
Expand Down
2 changes: 1 addition & 1 deletion dist/browser/axios.cjs.map

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions dist/esm/axios.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/esm/axios.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/axios.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/axios.min.js.map

Large diffs are not rendered by default.

29 changes: 23 additions & 6 deletions dist/node/axios.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2835,6 +2835,11 @@ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {

const contentLength = utils$1.toFiniteNumber(headers.getContentLength());

let progressUpdateTicksRate = undefined;
if (utils$1.isNumber(config.progressUpdateIntervalMs)) {
progressUpdateTicksRate = 1000 / config.progressUpdateIntervalMs;
}

if (utils$1.isArray(maxRate)) {
maxUploadRate = maxRate[0];
maxDownloadRate = maxRate[1];
Expand All @@ -2849,7 +2854,8 @@ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {

data = stream__default["default"].pipeline([data, new AxiosTransformStream$1({
length: contentLength,
maxRate: utils$1.toFiniteNumber(maxUploadRate)
maxRate: utils$1.toFiniteNumber(maxUploadRate),
ticksRate: progressUpdateTicksRate,
})], utils$1.noop);

onUploadProgress && data.on('progress', progress => {
Expand Down Expand Up @@ -2958,7 +2964,8 @@ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
if (onDownloadProgress) {
const transformStream = new AxiosTransformStream$1({
length: utils$1.toFiniteNumber(responseLength),
maxRate: utils$1.toFiniteNumber(maxDownloadRate)
maxRate: utils$1.toFiniteNumber(maxDownloadRate),
ticksRate: progressUpdateTicksRate,
});

onDownloadProgress && transformStream.on('progress', progress => {
Expand Down Expand Up @@ -3599,14 +3606,19 @@ const xhrAdapter = isXHRAdapterSupported && function (config) {
request.responseType = _config.responseType;
}

let progressUpdateTicksRate = undefined;
if (utils$1.isNumber(config.progressUpdateIntervalMs)) {
progressUpdateTicksRate = 1000 / config.progressUpdateIntervalMs;
}

// Handle progress if needed
if (typeof _config.onDownloadProgress === 'function') {
request.addEventListener('progress', progressEventReducer(_config.onDownloadProgress, true));
request.addEventListener('progress', progressEventReducer(_config.onDownloadProgress, true, progressUpdateTicksRate));
}

// Not all browsers support upload events
if (typeof _config.onUploadProgress === 'function' && request.upload) {
request.upload.addEventListener('progress', progressEventReducer(_config.onUploadProgress));
request.upload.addEventListener('progress', progressEventReducer(_config.onUploadProgress, progressUpdateTicksRate));
}

if (_config.cancelToken || _config.signal) {
Expand Down Expand Up @@ -3850,6 +3862,11 @@ const fetchAdapter = isFetchSupported && (async (config) => {
};

try {
let progressUpdateTicksRate = undefined;
if (utils$1.isNumber(config.progressUpdateIntervalMs)) {
progressUpdateTicksRate = 1000 / config.progressUpdateIntervalMs;
}

if (onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head') {
let requestContentLength = await resolveBodyLength(headers, data);

Expand All @@ -3867,7 +3884,7 @@ const fetchAdapter = isFetchSupported && (async (config) => {

data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator(
requestContentLength,
progressEventReducer(onUploadProgress)
progressEventReducer(onUploadProgress, false, progressUpdateTicksRate)
));
}

Expand Down Expand Up @@ -3901,7 +3918,7 @@ const fetchAdapter = isFetchSupported && (async (config) => {
response = new Response(
trackStream(response.body, DEFAULT_CHUNK_SIZE, onDownloadProgress && fetchProgressDecorator(
responseContentLength,
progressEventReducer(onDownloadProgress, true)
progressEventReducer(onDownloadProgress, true, progressUpdateTicksRate)
), isStreamResponse && onFinish),
options
);
Expand Down
2 changes: 1 addition & 1 deletion dist/node/axios.cjs.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions index.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ declare namespace axios {
xsrfHeaderName?: string;
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void;
progressUpdateIntervalMs?: number;
maxContentLength?: number;
validateStatus?: ((status: number) => boolean) | null;
maxBodyLength?: number;
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ export interface AxiosRequestConfig<D = any> {
xsrfHeaderName?: string;
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void;
progressUpdateIntervalMs?: number;
maxContentLength?: number;
validateStatus?: ((status: number) => boolean) | null;
maxBodyLength?: number;
Expand Down
9 changes: 7 additions & 2 deletions lib/adapters/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ export default isFetchSupported && (async (config) => {
}

try {
let progressUpdateTicksRate = undefined;
if (utils.isNumber(config.progressUpdateIntervalMs)) {
progressUpdateTicksRate = 1000 / config.progressUpdateIntervalMs;
}

if (onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head') {
let requestContentLength = await resolveBodyLength(headers, data);

Expand All @@ -135,7 +140,7 @@ export default isFetchSupported && (async (config) => {

data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator(
requestContentLength,
progressEventReducer(onUploadProgress)
progressEventReducer(onUploadProgress, false, progressUpdateTicksRate)
));
}

Expand Down Expand Up @@ -169,7 +174,7 @@ export default isFetchSupported && (async (config) => {
response = new Response(
trackStream(response.body, DEFAULT_CHUNK_SIZE, onDownloadProgress && fetchProgressDecorator(
responseContentLength,
progressEventReducer(onDownloadProgress, true)
progressEventReducer(onDownloadProgress, true, progressUpdateTicksRate)
), isStreamResponse && onFinish),
options
);
Expand Down