Skip to content

Commit

Permalink
feat: allow xhr override globally, for super advanced use cases only (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
alex-barstow authored Feb 9, 2021
1 parent b01ab72 commit 6279675
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -88,6 +92,8 @@ const xhrFactory = function() {
return request;
};

xhr.original = true;

return xhr;
};

Expand Down
30 changes: 30 additions & 0 deletions test/videojs-http-streaming.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6279675

Please sign in to comment.