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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
37 additions and
1 deletions
src/xhr.js
test/videojs-http-streaming.test.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 ;
} ;
@ -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 ;