Commit a26b32ce authored by Andrew Grieve's avatar Andrew Grieve Committed by Commit Bot

SuperSize: Fix missing access token for diffs

Also:
* Throws a more explicit exception for failed fetch.
* Stops setting Cache-Control: no-cache

Bug: None
Change-Id: I380207b32497f1cb5467ff62bcd3c857dc03ff58
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2418799
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Auto-Submit: Andrew Grieve <agrieve@chromium.org>
Reviewed-by: default avatarMohamed Heikal <mheikal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808406}
parent b757c47e
...@@ -28,10 +28,15 @@ const _NAMES_TO_FLAGS = Object.freeze({ ...@@ -28,10 +28,15 @@ const _NAMES_TO_FLAGS = Object.freeze({
* Wrapper around fetch for requesting the same resource multiple times. * Wrapper around fetch for requesting the same resource multiple times.
*/ */
class DataFetcher { class DataFetcher {
constructor(input) { constructor(accessToken) {
/** @type {string | null} */
this._accessToken = accessToken;
/** @type {AbortController | null} */ /** @type {AbortController | null} */
this._controller = null; this._controller = null;
this.setInput(input); /** @type {string | null} */
this._input = null;
/** @type {Uint8Array | null} */
this._cache = null;
} }
/** /**
...@@ -39,26 +44,15 @@ class DataFetcher { ...@@ -39,26 +44,15 @@ class DataFetcher {
* @param {string | Request} input URL to the resource you want to fetch. * @param {string | Request} input URL to the resource you want to fetch.
*/ */
setInput(input) { setInput(input) {
if (typeof this._input === 'string' && this._input.startsWith('blob:')) { if (this._input && this._input.startsWith('blob:')) {
// Revoke the previous Blob url to prevent memory leaks // Revoke the previous Blob url to prevent memory leaks
URL.revokeObjectURL(this._input); URL.revokeObjectURL(this._input);
} }
/** @type {Uint8Array | null} */
this._cache = null; this._cache = null;
this._input = input; this._input = input;
} }
/**
* Sets the access token to be used for authenticated requests. If accessToken
* is non-null and the URL is a google storage URL, an authenticated request
* is performed instead.
* @param {?string} accessToken
*/
setAccessToken(accessToken) {
this._accessToken = accessToken;
}
/** /**
* Starts a new request and aborts the previous one. * Starts a new request and aborts the previous one.
* @param {string | Request} url * @param {string | Request} url
...@@ -86,12 +80,15 @@ class DataFetcher { ...@@ -86,12 +80,15 @@ class DataFetcher {
if (!headers) { if (!headers) {
headers = new Headers(); headers = new Headers();
} }
headers.append('cache-control', 'no-cache'); let response = fetch(url, {
return fetch(url, {
headers, headers,
credentials: 'same-origin', credentials: 'same-origin',
signal: this._controller.signal, signal: this._controller.signal,
}); });
if (!response.ok) {
throw new Error('Fetch failed.');
}
return response;
} }
/** /**
...@@ -135,8 +132,7 @@ async function Open(name) { ...@@ -135,8 +132,7 @@ async function Open(name) {
}); });
} }
// Placeholder input name until supplied via setInput() let g_fetcher = null;
const g_fetcher = new DataFetcher('data.ndjson');
let g_beforeFetcher = null; let g_beforeFetcher = null;
let g_sizeFileLoaded = false; let g_sizeFileLoaded = false;
...@@ -181,7 +177,12 @@ async function buildTree( ...@@ -181,7 +177,12 @@ async function buildTree(
if (g_beforeFetcher !== null) { if (g_beforeFetcher !== null) {
load_promises.push(loadSizeFile(true, g_beforeFetcher)); load_promises.push(loadSizeFile(true, g_beforeFetcher));
} }
try {
await Promise.all(load_promises).then(loadSizeProperties); await Promise.all(load_promises).then(loadSizeProperties);
} catch (e) {
onProgress({percent: 1, id: 0});
throw e;
}
onProgress({percent: 0.4, id: 0}); onProgress({percent: 0.4, id: 0});
g_sizeFileLoaded = true; g_sizeFileLoaded = true;
} }
...@@ -268,8 +269,8 @@ const actions = { ...@@ -268,8 +269,8 @@ const actions = {
url, url,
beforeUrl, beforeUrl,
} = parseOptions(options); } = parseOptions(options);
if (accessToken) { if (!g_fetcher) {
g_fetcher.setAccessToken(accessToken); g_fetcher = new DataFetcher(accessToken);
} }
if (input === 'from-url://' && url) { if (input === 'from-url://' && url) {
// Display the data from the `load_url` query parameter // Display the data from the `load_url` query parameter
...@@ -281,7 +282,8 @@ const actions = { ...@@ -281,7 +282,8 @@ const actions = {
} }
if (beforeUrl) { if (beforeUrl) {
g_beforeFetcher = new DataFetcher(beforeUrl); g_beforeFetcher = new DataFetcher(accessToken);
g_beforeFetcher.setInput(beforeUrl);
} }
return buildTree( return buildTree(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment