Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow requests to be modified in an async manner #4024

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion samples/advanced/extend.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,24 @@
modifyRequestURL: function (url) {
/* Modify url adding a custom query string parameter */
return url + '?customQuery=value';
}
},
modifyRequest(request) {
/* Modify the entire request. Allows for async modifications */
var url = new URL(request.url);

if (!/\.mpd$/.test(url.pathname)) {
return;
}

return fetch('https://time.akamai.com')
.then(function (response) {
return response.text();
})
.then(function (text) {
url.searchParams.set('now', text);
request.url = url.toString();
});
},
Comment on lines -41 to +58
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added example

};
});
player.initialize(video, url, true);
Expand Down
14 changes: 12 additions & 2 deletions src/streaming/net/FetchLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import FactoryMaker from '../../core/FactoryMaker';
import Settings from '../../core/Settings';
import Constants from '../constants/Constants';
import { modifyRequest } from '../utils/RequestModifier';

/**
* @module FetchLoader
Expand All @@ -54,7 +55,16 @@ function FetchLoader(cfg) {
}

function load(httpRequest) {
if (requestModifier && requestModifier.modifyRequest) {
modifyRequest(httpRequest, requestModifier)
.then(() => request(httpRequest));
}
else {
request(httpRequest);
}
}

function request(httpRequest) {
// Variables will be used in the callback functions
const requestStartTime = new Date();
const request = httpRequest.request;
Expand All @@ -77,7 +87,7 @@ function FetchLoader(cfg) {
request.requestStartDate = requestStartTime;
}

if (requestModifier) {
if (requestModifier && requestModifier.modifyRequestHeader) {
// modifyRequestHeader expects a XMLHttpRequest object so,
// to keep backward compatibility, we should expose a setRequestHeader method
// TODO: Remove RequestModifier dependency on XMLHttpRequest object and define
Expand Down Expand Up @@ -176,7 +186,7 @@ function FetchLoader(cfg) {
reader.read().then(function processFetch(args) {
const value = args.value;
const done = args.done;
markB = Date.now()
markB = Date.now();

if (value && value.length) {
const chunkDownloadDurationMS = markB - markA;
Expand Down
2 changes: 1 addition & 1 deletion src/streaming/net/HTTPLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function HTTPLoader(cfg) {
}

let headers = null;
let modifiedUrl = requestModifier.modifyRequestURL(request.url);
let modifiedUrl = requestModifier.modifyRequestURL ? requestModifier.modifyRequestURL(request.url) : request.url;
littlespex marked this conversation as resolved.
Show resolved Hide resolved
if (settings.get().streaming.cmcd && settings.get().streaming.cmcd.enabled) {
const cmcdMode = settings.get().streaming.cmcd.mode;
if (cmcdMode === Constants.CMCD_MODE_QUERY) {
Expand Down
12 changes: 11 additions & 1 deletion src/streaming/net/XHRLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
import FactoryMaker from '../../core/FactoryMaker';
import { modifyRequest } from '../utils/RequestModifier';

/**
* @module XHRLoader
Expand All @@ -44,7 +45,16 @@ function XHRLoader(cfg) {
let instance;

function load(httpRequest) {
if (requestModifier && requestModifier.modifyRequest) {
modifyRequest(httpRequest, requestModifier)
.then(() => request(httpRequest));
}
else {
request(httpRequest);
}
}

function request(httpRequest) {
// Variables will be used in the callback functions
const requestStartTime = new Date();
const request = httpRequest.request;
Expand All @@ -64,7 +74,7 @@ function XHRLoader(cfg) {
request.requestStartDate = requestStartTime;
}

if (requestModifier) {
if (requestModifier && requestModifier.modifyRequestHeader) {
xhr = requestModifier.modifyRequestHeader(xhr, {
url: httpRequest.url
});
Expand Down
17 changes: 16 additions & 1 deletion src/streaming/utils/RequestModifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@

import FactoryMaker from '../../core/FactoryMaker';

export function modifyRequest(httpRequest, requestModifier) {
const request = {
url: httpRequest.url,
method: httpRequest.method,
headers: Object.assign({}, httpRequest.headers),
credentials: httpRequest.withCredentials ? 'include' : undefined,
dsilhavy marked this conversation as resolved.
Show resolved Hide resolved
};

return Promise.resolve(requestModifier.modifyRequest(request))
.then(() =>
Object.assign(httpRequest, request, { withCredentials: request.credentials === 'include' })
);
}

function RequestModifier() {

let instance;
Expand All @@ -40,11 +54,12 @@ function RequestModifier() {
}

// eslint-disable-next-line no-unused-vars
function modifyRequestHeader(request, {url}) {
function modifyRequestHeader(request, { url }) {
return request;
}

instance = {
modifyRequest: null,
modifyRequestURL: modifyRequestURL,
modifyRequestHeader: modifyRequestHeader
};
Expand Down