From 812163a9862ae0da3550bb95bda481cc28f13a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Velad=20Galv=C3=A1n?= Date: Thu, 23 May 2024 04:16:50 +0200 Subject: [PATCH] fix: Fix numBytesRemaining when the request is done (#6653) --- lib/net/http_fetch_plugin.js | 4 +++- lib/net/http_xhr_plugin.js | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/net/http_fetch_plugin.js b/lib/net/http_fetch_plugin.js index 249fdfdcab..9637d16fbb 100644 --- a/lib/net/http_fetch_plugin.js +++ b/lib/net/http_fetch_plugin.js @@ -167,8 +167,10 @@ shaka.net.HttpFetchPlugin = class { // is long enough, or if a whole segment is downloaded, call // progressUpdated(). if (currentTime - lastTime > 100 || readObj.done) { + const numBytesRemaining = + readObj.done ? 0 : contentLength - loaded; progressUpdated(currentTime - lastTime, loaded - lastLoaded, - contentLength - loaded); + numBytesRemaining); lastLoaded = loaded; lastTime = currentTime; } diff --git a/lib/net/http_xhr_plugin.js b/lib/net/http_xhr_plugin.js index aec4ec6de2..37f9db0670 100644 --- a/lib/net/http_xhr_plugin.js +++ b/lib/net/http_xhr_plugin.js @@ -67,6 +67,9 @@ shaka.net.HttpXHRPlugin = class { const xhrResponse = xhr.response; try { + const currentTime = Date.now(); + progressUpdated(currentTime - lastTime, event.loaded - lastLoaded, + /* numBytesRemaining= */ 0); const response = shaka.net.HttpPluginUtils.makeResponse(headers, xhrResponse, xhr.status, uri, xhr.responseURL, requestType); resolve(response); @@ -97,8 +100,10 @@ shaka.net.HttpXHRPlugin = class { // progressUpdated(). if (currentTime - lastTime > 100 || (event.lengthComputable && event.loaded == event.total)) { + const numBytesRemaining = + xhr.readyState == 4 ? 0 : event.total - event.loaded; progressUpdated(currentTime - lastTime, event.loaded - lastLoaded, - event.total - event.loaded); + numBytesRemaining); lastLoaded = event.loaded; lastTime = currentTime; }