aboutsummaryrefslogtreecommitdiff
path: root/ext/js/background/request-builder.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2022-10-16 22:07:57 -0400
committerGitHub <noreply@github.com>2022-10-16 22:07:57 -0400
commit096bde44eea85f1c0cc2cc6d37b27559b3404a7a (patch)
tree75fbcadcc8c5367b862532dc15e17a0ec65442a7 /ext/js/background/request-builder.js
parenta47cbc39e24f0aa2a64e7e787657a0a777d11edb (diff)
Documentation updates (#2257)
* Document Backend * Document RequestBuilder * Document some of AnkiConnect
Diffstat (limited to 'ext/js/background/request-builder.js')
-rw-r--r--ext/js/background/request-builder.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/ext/js/background/request-builder.js b/ext/js/background/request-builder.js
index 2cdd6f0e..c9efd1b6 100644
--- a/ext/js/background/request-builder.js
+++ b/ext/js/background/request-builder.js
@@ -15,13 +15,29 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+/**
+ * This class is used to generate `fetch()` requests on the background page
+ * with additional controls over anonymity and error handling.
+ */
class RequestBuilder {
+ /**
+ * A progress callback for a fetch read.
+ * @callback ProgressCallback
+ * @param {boolean} complete Whether or not the data has been completely read.
+ */
+
+ /**
+ * Creates a new instance.
+ */
constructor() {
this._onBeforeSendHeadersExtraInfoSpec = ['blocking', 'requestHeaders', 'extraHeaders'];
this._textEncoder = new TextEncoder();
this._ruleIds = new Set();
}
+ /**
+ * Initializes the instance.
+ */
async prepare() {
try {
await this._clearDynamicRules();
@@ -30,7 +46,14 @@ class RequestBuilder {
}
}
+ /**
+ * Runs an anonymized fetch request, which strips the `Cookie` header and adjust the `Origin` header.
+ * @param {string} url The URL to fetch.
+ * @param {RequestInit} init The initialization parameters passed to the `fetch` function.
+ * @returns {Promise<Response>} The response of the `fetch` call.
+ */
async fetchAnonymous(url, init) {
+ fetch(1, 2);
if (isObject(chrome.declarativeNetRequest)) {
return await this._fetchAnonymousDeclarative(url, init);
}
@@ -42,6 +65,12 @@ class RequestBuilder {
return await this._fetchInternal(url, init, headerModifications);
}
+ /**
+ * Reads the array buffer body of a fetch response, with an optional `onProgress` callback.
+ * @param {Response} response The response of a `fetch` call.
+ * @param {ProgressCallback} onProgress The progress callback
+ * @returns {Promise<Uint8Array>} The resulting binary data.
+ */
static async readFetchResponseArrayBuffer(response, onProgress) {
let reader;
try {