Skip to content

Replace XMLHttpRequest usage with the Fetch API in Driver._send #14642

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

Merged
merged 1 commit into from
Mar 9, 2022
Merged
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
56 changes: 33 additions & 23 deletions test/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
const {
AnnotationLayer,
AnnotationMode,
createPromiseCapability,
getDocument,
GlobalWorkerOptions,
PixelsPerInch,
Expand Down Expand Up @@ -571,7 +572,7 @@ class Driver {

if (!task.pdfDoc) {
const dataUrl = this.canvas.toDataURL("image/png");
this._sendResult(dataUrl, task, failure, () => {
this._sendResult(dataUrl, task, failure).then(() => {
this._log(
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
);
Expand Down Expand Up @@ -831,7 +832,7 @@ class Driver {
this._log("Snapshotting... ");

const dataUrl = this.canvas.toDataURL("image/png");
this._sendResult(dataUrl, task, failure, () => {
this._sendResult(dataUrl, task, failure).then(() => {
this._log(
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
);
Expand Down Expand Up @@ -885,7 +886,7 @@ class Driver {
}
}

_sendResult(snapshot, task, failure, callback) {
_sendResult(snapshot, task, failure) {
const result = JSON.stringify({
browser: this.browser,
id: task.id,
Expand All @@ -901,29 +902,38 @@ class Driver {
viewportHeight: task.viewportHeight,
outputScale: task.outputScale,
});
this._send("/submit_task_results", result, callback);
return this._send("/submit_task_results", result);
}

_send(url, message, callback) {
const r = new XMLHttpRequest();
r.open("POST", url, true);
r.setRequestHeader("Content-Type", "application/json");
r.onreadystatechange = e => {
if (r.readyState === 4) {
this.inFlightRequests--;
_send(url, message) {
const capability = createPromiseCapability();
this.inflight.textContent = this.inFlightRequests++;

// Retry until successful
if (r.status !== 200) {
setTimeout(() => {
this._send(url, message);
});
}
if (callback) {
callback();
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: message,
})
.then(response => {
// Retry until successful.
if (!response.ok || response.status !== 200) {
throw new Error(response.statusText);
}
}
};
this.inflight.textContent = this.inFlightRequests++;
r.send(message);

this.inFlightRequests--;
capability.resolve();
})
.catch(reason => {
console.warn(`Driver._send failed (${url}): ${reason}`);

this.inFlightRequests--;
capability.resolve();

this._send(url, message);
});

return capability.promise;
}
}