From e630331e85fb4ef7cb55e24d5c8bd42a6588c5aa Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Sun, 29 Jul 2018 21:54:34 +0100 Subject: [PATCH] fix: XMLHTTPRequest is deprecated and unavailable in service workers This PR switches to using the fetch API instead. License: MIT Signed-off-by: Alan Shaw --- src/core/runtime/preload-browser.js | 36 +++++++++++------------------ 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/core/runtime/preload-browser.js b/src/core/runtime/preload-browser.js index 583b0a2128..ff5b9b7698 100644 --- a/src/core/runtime/preload-browser.js +++ b/src/core/runtime/preload-browser.js @@ -9,29 +9,21 @@ log.error = debug('jsipfs:preload:error') module.exports = function preload (url, callback) { log(url) - const req = new self.XMLHttpRequest() - - req.open('GET', url) - - req.onreadystatechange = function () { - if (this.readyState !== this.DONE) { - return - } - - if (this.status < 200 || this.status >= 300) { - log.error('failed to preload', url, this.status, this.statusText) - return callback(new Error(`failed to preload ${url}`)) - } - - callback() - } - - req.send() + const controller = new AbortController() + const signal = controller.signal + + fetch(url, { signal }) + .then(res => { + if (!res.ok) { + log.error('failed to preload', url, res.status, res.statusText) + throw new Error(`failed to preload ${url}`) + } + return res.text() + }) + .then(() => callback()) + .catch(callback) return { - cancel: () => { - req.abort() - callback(new Error('request aborted')) - } + cancel: () => controller.abort() } }