Skip to content

Commit e92cf62

Browse files
szuendDevtools-frontend LUCI CQ
authored andcommitted
[host] Replace XHR with fetch for data URLs
It's less code and will work in both node.js and the browser. [email protected] Bug: 451502260 Change-Id: I4c3bd0553d87e35b9144cbbafbf63f179b370661 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/7185746 Auto-Submit: Simon Zünd <[email protected]> Reviewed-by: Alex Rudenko <[email protected]> Commit-Queue: Alex Rudenko <[email protected]>
1 parent 4056738 commit e92cf62

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

front_end/core/host/ResourceLoader.ts

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -195,28 +195,14 @@ function createErrorMessageFromResponse(response: LoadNetworkResourceResult): {
195195
return {success, description: {statusCode, netError, netErrorName, urlValid, message}};
196196
}
197197

198-
const loadXHR = (url: string): Promise<string> => {
199-
return new Promise((successCallback, failureCallback) => {
200-
function onReadyStateChanged(): void {
201-
if (xhr.readyState !== XMLHttpRequest.DONE) {
202-
return;
203-
}
204-
if (xhr.status !== 200) {
205-
xhr.onreadystatechange = null;
206-
failureCallback(new Error(String(xhr.status)));
207-
return;
208-
}
209-
xhr.onreadystatechange = null;
210-
successCallback(xhr.responseText);
211-
}
212-
213-
const xhr = new XMLHttpRequest();
214-
xhr.withCredentials = false;
215-
xhr.open('GET', url, true);
216-
xhr.onreadystatechange = onReadyStateChanged;
217-
xhr.send(null);
218-
});
219-
};
198+
async function fetchToString(url: string): Promise<string> {
199+
try {
200+
const response = await fetch(url);
201+
return await response.text();
202+
} catch (cause) {
203+
throw new Error(`Failed to fetch ${url}`, {cause});
204+
}
205+
}
220206

221207
function canBeRemoteFilePath(url: string): boolean {
222208
try {
@@ -237,7 +223,7 @@ export const loadAsStream = function(
237223
const streamId = bindOutputStream(stream);
238224
const parsedURL = new Common.ParsedURL.ParsedURL(url);
239225
if (parsedURL.isDataURL()) {
240-
loadXHR(url).then(dataURLDecodeSuccessful).catch(dataURLDecodeFailed);
226+
fetchToString(url).then(dataURLDecodeSuccessful).catch(dataURLDecodeFailed);
241227
return;
242228
}
243229

0 commit comments

Comments
 (0)