From 7b8d2495cab70336858f94b40d8815617a3cc23c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 20 Apr 2021 23:35:25 +0200 Subject: [PATCH] Convert the font-test `ttx` helper function to use the Fetch API 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 now, 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. --- test/font/fontutils.js | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/test/font/fontutils.js b/test/font/fontutils.js index e3af860bf..72938203e 100644 --- a/test/font/fontutils.js +++ b/test/font/fontutils.js @@ -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) {