Skip to content

Commit

Permalink
feat(progressUpdateIntervalMs): add 'progressUpdateIntervalMs' config…
Browse files Browse the repository at this point in the history
… option;
  • Loading branch information
nick-michael committed May 3, 2024
1 parent 8e4314b commit efe0153
Show file tree
Hide file tree
Showing 19 changed files with 177 additions and 47 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,12 @@ 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 minimum interval in milliseconds between upload & download progress events
// browser & node.js
onDownloadProgress: function ({loaded, total, progress, bytes, estimated, rate, download = true}) {
// Do whatever you want with the Axios progress event
},

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

Expand Down
46 changes: 27 additions & 19 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 @@ -2769,6 +2774,11 @@ var fetchAdapter = async (config) => {
};

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

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

Expand All @@ -2786,7 +2796,7 @@ var fetchAdapter = async (config) => {

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

Expand Down Expand Up @@ -2820,7 +2830,7 @@ var fetchAdapter = 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 @@ -3838,6 +3850,11 @@ const fetchAdapter = async (config) => {
};

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

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

Expand All @@ -3855,7 +3872,7 @@ const fetchAdapter = async (config) => {

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

Expand Down Expand Up @@ -3889,7 +3906,7 @@ const fetchAdapter = 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 @@ -106,6 +106,11 @@ export default async (config) => {
}

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

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

Expand All @@ -123,7 +128,7 @@ export default async (config) => {

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

Expand Down Expand Up @@ -157,7 +162,7 @@ export default 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

0 comments on commit efe0153

Please sign in to comment.