Skip to content

Commit

Permalink
feat(net): Add originalRequest to shaka.extern.Response (#7857)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored Jan 9, 2025
1 parent 0a9aeb7 commit 59c9989
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 14 deletions.
5 changes: 4 additions & 1 deletion externs/shaka/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ shaka.extern.Request;
* status: (number|undefined),
* headers: !Object.<string, string>,
* timeMs: (number|undefined),
* fromCache: (boolean|undefined)
* fromCache: (boolean|undefined),
* originalRequest: shaka.extern.Request
* }}
*
* @description
Expand Down Expand Up @@ -158,6 +159,8 @@ shaka.extern.Request;
* @property {(boolean|undefined)} fromCache
* Optional. If true, this response was from a cache and should be ignored
* for bandwidth estimation.
* @property {shaka.extern.Request} originalRequest
* The original request that gave rise to this response.
*
* @exportDoc
*/
Expand Down
1 change: 1 addition & 0 deletions lib/net/data_uri_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ shaka.net.DataUriPlugin = class {
headers: {
'content-type': parsed.contentType,
},
originalRequest: request,
};

return shaka.util.AbortableOperation.completed(response);
Expand Down
13 changes: 7 additions & 6 deletions lib/net/http_fetch_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ shaka.net.HttpFetchPlugin = class {
const minBytes = config.minBytesForProgressEvents || 0;

const pendingRequest = shaka.net.HttpFetchPlugin.request_(
uri, requestType, init, abortStatus, progressUpdated, headersReceived,
request.streamDataCallback, minBytes);
uri, request, requestType, init, abortStatus, progressUpdated,
headersReceived, request.streamDataCallback, minBytes);

/** @type {!shaka.util.AbortableOperation} */
const op = new shaka.util.AbortableOperation(pendingRequest, () => {
Expand Down Expand Up @@ -94,6 +94,7 @@ shaka.net.HttpFetchPlugin = class {

/**
* @param {string} uri
* @param {shaka.extern.Request} request
* @param {shaka.net.NetworkingEngine.RequestType} requestType
* @param {!RequestInit} init
* @param {shaka.net.HttpFetchPlugin.AbortStatus} abortStatus
Expand All @@ -104,8 +105,8 @@ shaka.net.HttpFetchPlugin = class {
* @return {!Promise<!shaka.extern.Response>}
* @private
*/
static async request_(uri, requestType, init, abortStatus, progressUpdated,
headersReceived, streamDataCallback, minBytes) {
static async request_(uri, request, requestType, init, abortStatus,
progressUpdated, headersReceived, streamDataCallback, minBytes) {
const fetch = shaka.net.HttpFetchPlugin.fetch_;
const ReadableStream = shaka.net.HttpFetchPlugin.ReadableStream_;
let response;
Expand Down Expand Up @@ -225,8 +226,8 @@ shaka.net.HttpFetchPlugin = class {
const headers = shaka.net.HttpFetchPlugin.headersToGenericObject_(
response.headers);

return shaka.net.HttpPluginUtils.makeResponse(
headers, arrayBuffer, response.status, uri, response.url, requestType);
return shaka.net.HttpPluginUtils.makeResponse(headers, arrayBuffer,
response.status, uri, response.url, request, requestType);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion lib/net/http_plugin_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ shaka.net.HttpPluginUtils = class {
* @param {number} status
* @param {string} uri
* @param {string} responseURL
* @param {shaka.extern.Request} request
* @param {shaka.net.NetworkingEngine.RequestType} requestType
* @return {!shaka.extern.Response}
*/
static makeResponse(headers, data, status, uri, responseURL, requestType) {
static makeResponse(headers, data, status, uri, responseURL, request,
requestType) {
goog.asserts.assert(data, 'Data should be non-null!');

if (status >= 200 && status <= 299 && status != 202) {
Expand All @@ -40,6 +42,7 @@ shaka.net.HttpPluginUtils = class {
status: status,
headers: headers,
fromCache: !!headers['x-shaka-from-cache'],
originalRequest: request,
};
return response;
} else {
Expand Down
3 changes: 2 additions & 1 deletion lib/net/http_xhr_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ shaka.net.HttpXHRPlugin = class {
progressUpdated(currentTime - lastTime, event.loaded - lastLoaded,
/* numBytesRemaining= */ 0);
const response = shaka.net.HttpPluginUtils.makeResponse(headers,
xhrResponse, xhr.status, uri, xhr.responseURL, requestType);
xhrResponse, xhr.status, uri, xhr.responseURL, request,
requestType);
resolve(response);
} catch (error) {
goog.asserts.assert(error instanceof shaka.util.Error,
Expand Down
12 changes: 8 additions & 4 deletions lib/offline/offline_scheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ shaka.offline.OfflineScheme = class {
const offlineUri = shaka.offline.OfflineUri.parse(uri);

if (offlineUri && offlineUri.isManifest()) {
return shaka.offline.OfflineScheme.getManifest_(uri);
return shaka.offline.OfflineScheme.getManifest_(uri, request);
}

if (offlineUri && offlineUri.isSegment()) {
return shaka.offline.OfflineScheme.getSegment_(
offlineUri.key(), offlineUri);
offlineUri.key(), offlineUri, request);
}

return shaka.util.AbortableOperation.failed(
Expand All @@ -50,16 +50,18 @@ shaka.offline.OfflineScheme = class {

/**
* @param {string} uri
* @param {shaka.extern.Request} request
* @return {!shaka.extern.IAbortableOperation.<shaka.extern.Response>}
* @private
*/
static getManifest_(uri) {
static getManifest_(uri, request) {
/** @type {shaka.extern.Response} */
const response = {
uri: uri,
originalUri: uri,
data: new ArrayBuffer(0),
headers: {'content-type': 'application/x-offline-manifest'},
originalRequest: request,
};

return shaka.util.AbortableOperation.completed(response);
Expand All @@ -68,10 +70,11 @@ shaka.offline.OfflineScheme = class {
/**
* @param {number} id
* @param {!shaka.offline.OfflineUri} uri
* @param {shaka.extern.Request} request
* @return {!shaka.extern.IAbortableOperation.<shaka.extern.Response>}
* @private
*/
static getSegment_(id, uri) {
static getSegment_(id, uri, request) {
goog.asserts.assert(
uri.isSegment(),
'Only segment uri\'s should be given to getSegment');
Expand All @@ -90,6 +93,7 @@ shaka.offline.OfflineScheme = class {
uri: uri,
data: segment.data,
headers: {},
originalRequest: request,
};
})
.finally(() => muxer.destroy());
Expand Down
1 change: 1 addition & 0 deletions test/media/streaming_engine_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3461,6 +3461,7 @@ describe('StreamingEngine', () => {
originalUri: request.uris[0],
data: buffer,
headers: {},
originalRequest: request,
};
return shaka.util.AbortableOperation.completed(response);
});
Expand Down
1 change: 1 addition & 0 deletions test/net/networking_engine_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,7 @@ describe('NetworkingEngine', /** @suppress {accessControls} */ () => {
originalUri: '',
data: new ArrayBuffer(5),
headers: {},
originalRequest: createRequest('fake'),
};
}
}); // describe('NetworkingEngine')
1 change: 1 addition & 0 deletions test/test/util/fake_networking_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ shaka.test.FakeNetworkingEngine = class {
originalUri: requestedUri,
data: result,
headers: headers,
originalRequest: request,
};

// Modify the response using the response filter, this allows the app
Expand Down
9 changes: 8 additions & 1 deletion test/test/util/test_scheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ shaka.test.TestScheme = class {
originalUri: uri,
data: new ArrayBuffer(0),
headers: {'content-type': 'application/x-test-manifest'},
originalRequest: request,
};
return shaka.util.AbortableOperation.completed(response);
}
Expand Down Expand Up @@ -127,7 +128,13 @@ shaka.test.TestScheme = class {
}

/** @type {shaka.extern.Response} */
const ret = {uri: uri, originalUri: uri, data: responseData, headers: {}};
const ret = {
uri: uri,
originalUri: uri,
data: responseData,
headers: {},
originalRequest: request,
};
return shaka.util.AbortableOperation.completed(ret);
}

Expand Down

0 comments on commit 59c9989

Please sign in to comment.