Skip to content

Commit 6279675

Browse files
authored
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.
1 parent b01ab72 commit 6279675

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/xhr.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ const xhrFactory = function() {
7474
}
7575
}
7676

77-
const request = videojsXHR(options, function(error, response) {
77+
// Use the standard videojs.xhr() method unless `videojs.Vhs.xhr` has been overriden
78+
// TODO: switch back to videojs.Vhs.xhr.name === 'XhrFunction' when we drop IE11
79+
const xhrMethod = videojs.Vhs.xhr.original === true ? videojsXHR : videojs.Vhs.xhr;
80+
81+
const request = xhrMethod(options, function(error, response) {
7882
return callbackWrapper(request, error, response, callback);
7983
});
8084
const originalAbort = request.abort;
@@ -88,6 +92,8 @@ const xhrFactory = function() {
8892
return request;
8993
};
9094

95+
xhr.original = true;
96+
9197
return xhr;
9298
};
9399

test/videojs-http-streaming.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4267,6 +4267,36 @@ QUnit.test('Allows specifying the beforeRequest function globally', function(ass
42674267
assert.equal(this.player.tech_.vhs.stats.bandwidth, 4194304, 'default');
42684268
});
42694269

4270+
QUnit.test('Allows specifying custom xhr() function globally', function(assert) {
4271+
const originalXhr = videojs.Vhs.xhr;
4272+
let customXhr = false;
4273+
4274+
videojs.Vhs.xhr = function(opts, callback) {
4275+
customXhr = true;
4276+
return videojs.xhr(opts, function(err, response, body) {
4277+
callback(err, response);
4278+
});
4279+
};
4280+
4281+
this.player.src({
4282+
src: 'master.m3u8',
4283+
type: 'application/vnd.apple.mpegurl'
4284+
});
4285+
4286+
this.clock.tick(1);
4287+
4288+
openMediaSource(this.player, this.clock);
4289+
// master
4290+
this.standardXHRResponse(this.requests.shift());
4291+
4292+
assert.ok(customXhr, 'customXhr was called');
4293+
4294+
videojs.Vhs.xhr = originalXhr;
4295+
4296+
// verify stats
4297+
assert.equal(this.player.tech_.vhs.stats.bandwidth, 4194304, 'default');
4298+
});
4299+
42704300
QUnit.test('Allows overriding the global beforeRequest function', function(assert) {
42714301
let beforeGlobalRequestCalled = 0;
42724302
let beforeLocalRequestCalled = 0;

0 commit comments

Comments
 (0)