From 6279675d212abe095ad27843d67cbdaba1a9d9f7 Mon Sep 17 00:00:00 2001 From: Alex Barstow Date: Tue, 9 Feb 2021 13:57:50 -0500 Subject: [PATCH] feat: allow xhr override globally, for super advanced use cases only (#1059) We have the recommended `beforeRequest` method but due to timing, it's not possible to use this to be able to set options for the initial m3u8 manifest as well. This makes it so that the XHR method can be overridden completely. --- src/xhr.js | 8 +++++++- test/videojs-http-streaming.test.js | 30 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/xhr.js b/src/xhr.js index b77cd3eac..bb5923082 100644 --- a/src/xhr.js +++ b/src/xhr.js @@ -74,7 +74,11 @@ const xhrFactory = function() { } } - const request = videojsXHR(options, function(error, response) { + // Use the standard videojs.xhr() method unless `videojs.Vhs.xhr` has been overriden + // TODO: switch back to videojs.Vhs.xhr.name === 'XhrFunction' when we drop IE11 + const xhrMethod = videojs.Vhs.xhr.original === true ? videojsXHR : videojs.Vhs.xhr; + + const request = xhrMethod(options, function(error, response) { return callbackWrapper(request, error, response, callback); }); const originalAbort = request.abort; @@ -88,6 +92,8 @@ const xhrFactory = function() { return request; }; + xhr.original = true; + return xhr; }; diff --git a/test/videojs-http-streaming.test.js b/test/videojs-http-streaming.test.js index d20e48596..c59a5e252 100644 --- a/test/videojs-http-streaming.test.js +++ b/test/videojs-http-streaming.test.js @@ -4267,6 +4267,36 @@ QUnit.test('Allows specifying the beforeRequest function globally', function(ass assert.equal(this.player.tech_.vhs.stats.bandwidth, 4194304, 'default'); }); +QUnit.test('Allows specifying custom xhr() function globally', function(assert) { + const originalXhr = videojs.Vhs.xhr; + let customXhr = false; + + videojs.Vhs.xhr = function(opts, callback) { + customXhr = true; + return videojs.xhr(opts, function(err, response, body) { + callback(err, response); + }); + }; + + this.player.src({ + src: 'master.m3u8', + type: 'application/vnd.apple.mpegurl' + }); + + this.clock.tick(1); + + openMediaSource(this.player, this.clock); + // master + this.standardXHRResponse(this.requests.shift()); + + assert.ok(customXhr, 'customXhr was called'); + + videojs.Vhs.xhr = originalXhr; + + // verify stats + assert.equal(this.player.tech_.vhs.stats.bandwidth, 4194304, 'default'); +}); + QUnit.test('Allows overriding the global beforeRequest function', function(assert) { let beforeGlobalRequestCalled = 0; let beforeLocalRequestCalled = 0;