Browse Source

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.
pull/1067/head
Alex Barstow 5 years ago
committed by GitHub
parent
commit
6279675d21
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/xhr.js
  2. 30
      test/videojs-http-streaming.test.js

8
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); return callbackWrapper(request, error, response, callback);
}); });
const originalAbort = request.abort; const originalAbort = request.abort;
@ -88,6 +92,8 @@ const xhrFactory = function() {
return request; return request;
}; };
xhr.original = true;
return xhr; return xhr;
}; };

30
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'); 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) { QUnit.test('Allows overriding the global beforeRequest function', function(assert) {
let beforeGlobalRequestCalled = 0; let beforeGlobalRequestCalled = 0;
let beforeLocalRequestCalled = 0; let beforeLocalRequestCalled = 0;

Loading…
Cancel
Save