Skip to content

Commit

Permalink
Convert the font-test ttx helper function to use the Fetch API
Browse files Browse the repository at this point in the history
By replacing `XMLHttpRequest` with a `fetch` call, the helper function can be modernized to use async/await instead.
Note that the headers doesn't seem necessary to set, since:
 - The Fetch API provides a method for accessing the response as *text*, which renders the "Content-type" header unnecessary.
 - According to https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name, the "Content-length" header isn't necessary.
  • Loading branch information
Snuffleupagus committed Apr 20, 2021
1 parent 3d55b2b commit 9a7f095
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions test/font/fontutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,16 @@ function encodeFontData(data) {
return buffer;
}

function ttx(data) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open("POST", "/ttx");

const encodedData = encodeFontData(data);
xhr.setRequestHeader("Content-type", "text/plain");
xhr.setRequestHeader("Content-length", encodedData.length);

xhr.onreadystatechange = function getPdfOnreadystatechange(e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(xhr.responseText);
} else {
reject(new Error(xhr.statusText));
}
}
};
xhr.send(encodedData);
async function ttx(data) {
const response = await fetch("/ttx", {
method: "POST",
body: encodeFontData(data),
});

if (!response.ok) {
throw new Error(response.statusText);
}
return response.text();
}

function verifyTtxOutput(output) {
Expand Down

0 comments on commit 9a7f095

Please sign in to comment.