videojs / http-streaming

HLS, DASH, and future HTTP streaming protocols library for video.js
https://videojs-http-streaming.netlify.app/
Other
2.45k stars 419 forks source link

feat(xhr): add request and response hook API #1393

Closed adrums86 closed 1 year ago

adrums86 commented 1 year ago

Description

This PR would add xhr request and response hooks via an onRequest(callback) and onResponse(callback) API. The user can add as many unique hook callbacks as they want, and they will be called in the order they were added before the request and after the response is received respectively. The user can un-register these callbacks via the offRequest(callback) and offResponse(callback) API. The callback functions can take the following parameters:

In addition, the user can use both global videojs.Vhs and player player.tech().xhr scoped API functions to add these hooks, where player hooks will override any global hooks.

Note: onRequest hooks are called after the beforeRequest function, so be aware that these hooks may further modify request options passed through that function.

Specific Changes proposed

Adding API functions to both Vhs and the VhsHandler for adding and removing unique xhr request and response hooks, passing those hooks to the xhr module, then calling them in the order they were added before an xhr request and in the response callback respectively.

Requirements Checklist

codecov[bot] commented 1 year ago

Codecov Report

Merging #1393 (e56d103) into main (bdd842a) will increase coverage by 0.04%. The diff coverage is 95.74%.

@@            Coverage Diff             @@
##             main    #1393      +/-   ##
==========================================
+ Coverage   85.40%   85.45%   +0.04%     
==========================================
  Files          40       40              
  Lines        9963    10010      +47     
  Branches     2308     2317       +9     
==========================================
+ Hits         8509     8554      +45     
- Misses       1454     1456       +2     
Impacted Files Coverage Δ
src/videojs-http-streaming.js 90.96% <94.73%> (+0.32%) :arrow_up:
src/xhr.js 100.00% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

dzianis-dashkevich commented 1 year ago

I thought about this kind of simplification

xhr._requestHooksSet = new Set();
xhr._responseHooksSet = new Set();

const addOnRequestHook = (callback) => xhr._requestHooksSet.add(callback);
const addOnResponseHook = (callback) => xhr._responseHooksSet.add(callback);

const removeOnRequestHook = (callback) => xhr._requestHooksSet.delete(callback);
const removeOnResponseHook = (callback) => xhr._responseHooksSet.delete(callback);

but then I realized that those 2 XHRs are not the same pointers...

LGTM!

adrums86 commented 1 year ago

@gkatsev I totally agree regarding namespacing these to the xhr object to separate the hooks from fetch. Update incoming.

adrums86 commented 1 year ago

@gkatsev and @misteroneill the namespace changes are pushed. Another quick look would be appreciated before merging. Thanks!