From 829ae1dface2890e6dc8d8f39b62a3180d006853 Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford <7019101+cwillisf@users.noreply.github.com> Date: Tue, 28 Mar 2023 13:20:58 -0700 Subject: [PATCH] fix: tell fetchWithTimeout to use scratchFetch from storage instance --- src/engine/runtime.js | 2 ++ src/extensions/scratch3_text2speech/index.js | 2 +- src/extensions/scratch3_translate/index.js | 2 +- src/util/fetch-with-timeout.js | 29 ++++++++++++++++++-- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 87b44d0d3..2c5f8eef2 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -18,6 +18,7 @@ const StageLayering = require('./stage-layering'); const Variable = require('./variable'); const xmlEscape = require('../util/xml-escape'); const ScratchLinkWebSocket = require('../util/scratch-link-websocket'); +const fetchWithTimeout = require('../util/fetch-with-timeout'); // Virtual I/O devices. const Clock = require('../io/clock'); @@ -1627,6 +1628,7 @@ class Runtime extends EventEmitter { */ attachStorage (storage) { this.storage = storage; + fetchWithTimeout.setFetch(storage.scratchFetch.scratchFetch); this.resetRunId(); } diff --git a/src/extensions/scratch3_text2speech/index.js b/src/extensions/scratch3_text2speech/index.js index f45c7010b..8c76a6a11 100644 --- a/src/extensions/scratch3_text2speech/index.js +++ b/src/extensions/scratch3_text2speech/index.js @@ -7,7 +7,7 @@ const Cast = require('../../util/cast'); const MathUtil = require('../../util/math-util'); const Clone = require('../../util/clone'); const log = require('../../util/log'); -const fetchWithTimeout = require('../../util/fetch-with-timeout'); +const {fetchWithTimeout} = require('../../util/fetch-with-timeout'); /** * Icon svg to be displayed in the blocks category menu, encoded as a data URI. diff --git a/src/extensions/scratch3_translate/index.js b/src/extensions/scratch3_translate/index.js index 871ca326a..3964711fc 100644 --- a/src/extensions/scratch3_translate/index.js +++ b/src/extensions/scratch3_translate/index.js @@ -2,7 +2,7 @@ const ArgumentType = require('../../extension-support/argument-type'); const BlockType = require('../../extension-support/block-type'); const Cast = require('../../util/cast'); const log = require('../../util/log'); -const fetchWithTimeout = require('../../util/fetch-with-timeout'); +const {fetchWithTimeout} = require('../../util/fetch-with-timeout'); const languageNames = require('scratch-translate-extension-languages'); const formatMessage = require('format-message'); diff --git a/src/util/fetch-with-timeout.js b/src/util/fetch-with-timeout.js index 80c1b11f0..465ce1444 100644 --- a/src/util/fetch-with-timeout.js +++ b/src/util/fetch-with-timeout.js @@ -1,4 +1,24 @@ -const {scratchFetch} = require('scratch-storage/src/scratchFetch.js'); +/** + * @callback FetchFunction + * @param {RequestInfo|URL} input + * @param {RequestInit|undefined} [init] + * @returns {Promise} + */ + +/** + * @type {FetchFunction} + */ +let myFetch = global.fetch; + +/** + * Tell `fetchWithTimeout` to use a specific `fetch` function. + * By default, `fetchWithTimeout` will use the global `fetch` function. + * If there is no global `fetch`, then `fetchWithTimeout` will fail unless provided with an alternative. + * @param {FetchFunction} newFetch The new `fetch` function to use within fetchWithTimeout. + */ +const setFetch = newFetch => { + myFetch = newFetch; +}; /** * Fetch a remote resource like `fetch` does, but with a time limit. @@ -14,7 +34,7 @@ const fetchWithTimeout = (resource, init, timeout) => { const signal = controller ? controller.signal : null; // The fetch call races a timer. return Promise.race([ - scratchFetch(resource, Object.assign({signal}, init)).then(response => { + myFetch(resource, Object.assign({signal}, init)).then(response => { clearTimeout(timeoutID); return response; }), @@ -27,4 +47,7 @@ const fetchWithTimeout = (resource, init, timeout) => { ]); }; -module.exports = fetchWithTimeout; +module.exports = { + fetchWithTimeout, + setFetch +};