50fdc1d4 by jrivera

Emulate some of the old behavior of the original videojs.xhr in the videojs.Hls.xhr wrapper

Add a default timeout of 45 seconds for all requests
Create a boolean `timedout` property on request in response to the ETIMEOUT error
Create an error code for non-200 responses
1 parent dd45e17b
...@@ -5,12 +5,34 @@ ...@@ -5,12 +5,34 @@
5 * A wrapper for videojs.xhr that tracks bandwidth. 5 * A wrapper for videojs.xhr that tracks bandwidth.
6 */ 6 */
7 videojs.Hls.xhr = function(options, callback) { 7 videojs.Hls.xhr = function(options, callback) {
8 var request = videojs.xhr(options, function(error, request) { 8 // Add a default timeout for all hls requests
9 if (request.response) { 9 options = videojs.mergeOptions({
10 timeout: 45e3
11 }, options);
12
13 var request = videojs.xhr(options, function(error, response) {
14 if (!error && request.response) {
10 request.responseTime = (new Date()).getTime(); 15 request.responseTime = (new Date()).getTime();
11 request.roundTripTime = request.responseTime - request.requestTime; 16 request.roundTripTime = request.responseTime - request.requestTime;
12 request.bytesReceived = request.response.byteLength || request.response.length; 17 request.bytesReceived = request.response.byteLength || request.response.length;
13 request.bandwidth = Math.floor((request.bytesReceived / request.roundTripTime) * 8 * 1000); 18 if (!request.bandwidth) {
19 request.bandwidth = Math.floor((request.bytesReceived / request.roundTripTime) * 8 * 1000);
20 }
21 }
22
23 // videojs.xhr now uses a specific code on the error object to signal that a request has
24 // timed out errors of setting a boolean on the request object
25 if (error || request.timedout) {
26 request.timedout = request.timedout || (error.code === 'ETIMEDOUT');
27 } else {
28 request.timedout = false;
29 }
30
31 // videojs.xhr no longer consider status codes outside of 200 and 0 (for file uris) to be
32 // errors but the old XHR did so emulate that behavior
33 if (!error && response.statusCode !== 200 && response.statusCode !== 0) {
34 error = new Error('XHR Failed with a response of: ' +
35 (request && (request.response || request.responseText)));
14 } 36 }
15 37
16 callback(error, request); 38 callback(error, request);
......