mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
fix(progress): guard malformed XHR upload events (#10868)
Signed-off-by: Ravi <13908473+rkdfx@users.noreply.github.com> Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -7,6 +7,9 @@ export const progressEventReducer = (listener, isDownloadStream, freq = 3) => {
|
||||
const _speedometer = speedometer(50, 250);
|
||||
|
||||
return throttle((e) => {
|
||||
if (!e || typeof e.loaded !== 'number') {
|
||||
return;
|
||||
}
|
||||
const rawLoaded = e.loaded;
|
||||
const total = e.lengthComputable ? e.total : undefined;
|
||||
const loaded = total != null ? Math.min(rawLoaded, total) : rawLoaded;
|
||||
|
||||
@@ -24,4 +24,27 @@ describe('helpers::progressEventReducer', () => {
|
||||
expect(last.upload).toBe(true);
|
||||
expect(last.bytes).toBe(20);
|
||||
});
|
||||
|
||||
it('should ignore malformed events that lack a numeric loaded value', () => {
|
||||
const events = [];
|
||||
const [onProgress, flush] = progressEventReducer((data) => {
|
||||
events.push(data);
|
||||
}, false, Number.POSITIVE_INFINITY);
|
||||
|
||||
onProgress(undefined);
|
||||
onProgress(null);
|
||||
onProgress({});
|
||||
onProgress({ loaded: null, total: 100, lengthComputable: true });
|
||||
onProgress({ loaded: 'abc', total: 100, lengthComputable: true });
|
||||
flush();
|
||||
|
||||
expect(events.length).toBe(0);
|
||||
|
||||
onProgress({ lengthComputable: true, loaded: 50, total: 100 });
|
||||
flush();
|
||||
|
||||
expect(events.length).toBe(1);
|
||||
expect(events[0].loaded).toBe(50);
|
||||
expect(events[0].bytes).toBe(50);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user